translate

package
v1.801.307 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package translate serves POST /v1/translate — the ONE translation surface, two tiers behind one endpoint, one auth path, one meter (HIP-0516).

POST /v1/translate  { text | batch[], target, source?, tier?, glossary?, format? }
                 -> { translations[], detected_source?, tier, usage }

`tier` picks the engine and defaults to quality:

  • quality routes to the model plane (deps.AI — zen through the gateway), which carries context, terminology and tone.
  • bulk routes to MADLAD-400 under CTranslate2 (engine.go), for high-volume, low-latency work where a model is overkill. A deployment with no bulk backend answers 503; bulk NEVER falls back to quality, so a caller is never quietly served — or charged — at a tier it did not ask for.

The translation memory (memory.go) is NORMATIVE, not a cache. Every string keys on (source_text, target, glossary_version, tier); a hit returns the stored value unchanged, so only new or changed source strings reach an engine. That is what makes a locale rebuild idempotent under a non-deterministic model, and it is what makes the bill proportional to what actually changed.

The review lane rides the same memory: an entry carries a state on the ladder machine -> suggested -> approved -> published, and human work is IMMUNE to machine churn (memory.put). A rebuild can never silently revert an approved string.

Tenancy: org-scoped by the validated principal. The memory is a per-org SQLite file (HIP-0302 physical isolation), so a request cannot reach another org's memory. Submitted text is customer content, held only in that org's own memory.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoEngine = errors.New("translate: tier backend not configured")

ErrNoEngine marks a tier whose backend this deployment does not serve. The handler renders it 503 for that tier and never re-routes to another one.

Functions

func Mount

func Mount(app cloud.Router, deps cloud.Deps) error

Mount wires the /v1/translate surface: the quality engine over the model plane deps.AI already gates and meters, and the bulk engine over the MADLAD backend named by TRANSLATE_BULK_URL (unset ⇒ the tier answers 503).

func Shutdown

func Shutdown() error

Shutdown closes every open per-org memory.

Types

type Engine

type Engine interface {
	Translate(ctx context.Context, j Job) (Result, error)
}

Engine translates a job's strings into one target language. Two implementations, one per tier; the endpoint, the memory, and the meter are shared.

type Entry

type Entry struct {
	Source string `json:"source"`
	Target string `json:"target"`
	Tier   Tier   `json:"tier"`
	// Glossary is the glossary VERSION the entry was translated under — the digest
	// version() derives from the terms, so changing a term changes the key and the
	// stale rendering can never be served.
	Glossary  string `json:"glossary_version,omitempty"`
	Text      string `json:"text"`
	State     State  `json:"state"`
	Actor     string `json:"actor,omitempty"`
	UpdatedAt int64  `json:"updated_at"`
}

Entry is one remembered translation. Source+Target+Tier+Glossary are its identity (the HIP-0516 tuple); Text and State are what the lane holds.

type Format

type Format string

Format tells an engine what markup the source carries so placeholders and tags survive the round trip.

const (
	FormatText     Format = "text"
	FormatHTML     Format = "html"
	FormatMarkdown Format = "markdown"
)

type Job

type Job struct {
	Texts    []string
	Source   string // "" ⇒ the engine detects and reports it
	Target   string
	Format   Format
	Glossary map[string]string

	Org        string // the effective org — the data scope
	BillingOrg string // the home org that pays
	Project    string
}

Job is one engine call: the strings to translate plus the billing scope, carried on the value exactly as types.ChatRequest carries it, so an engine can never run unattributed.

type Request

type Request struct {
	Text     string            `json:"text"`
	Batch    []string          `json:"batch"`
	Target   string            `json:"target"`
	Source   string            `json:"source"`
	Tier     Tier              `json:"tier"`
	Glossary map[string]string `json:"glossary"`
	Format   Format            `json:"format"`
}

Request is the POST /v1/translate body. Exactly one of Text or Batch carries the work; Batch preserves order in the reply.

type Response

type Response struct {
	Translations   []Translation `json:"translations"`
	DetectedSource string        `json:"detected_source,omitempty"`
	Tier           Tier          `json:"tier"`
	Usage          Usage         `json:"usage"`
}

Response is the POST /v1/translate reply.

type Result

type Result struct {
	Texts    []string
	Detected string
}

Result is one engine call's answer: one translation per input, in input order, plus the language the engine detected when the caller named none.

type ReviewRequest

type ReviewRequest struct {
	Source   string            `json:"source"`
	Target   string            `json:"target"`
	Tier     Tier              `json:"tier"`
	Glossary map[string]string `json:"glossary"`
	Text     string            `json:"text"`
	State    State             `json:"state"`
}

ReviewRequest is the human write: the same tuple a translate call carries, plus the reviewed text and its new position on the ladder.

type State

type State string

State is an entry's position on the review ladder.

const (
	StateMachine   State = "machine"
	StateSuggested State = "suggested"
	StateApproved  State = "approved"
	StatePublished State = "published"
)

type Tier

type Tier string

Tier selects the engine. Two values, one endpoint.

const (
	TierQuality Tier = "quality"
	TierBulk    Tier = "bulk"
)

type Translation

type Translation struct {
	Source string `json:"source"`
	Text   string `json:"text"`
	State  State  `json:"state"`
	Cached bool   `json:"cached"`
}

Translation is one string's result: the translation, where on the review ladder it sits, and whether it came from the memory rather than an engine.

type Usage

type Usage struct {
	Strings    int `json:"strings"`
	Cached     int `json:"cached"`
	Translated int `json:"translated"`
	Characters int `json:"characters"`
}

Usage reports what the call actually did — real counts, never an estimate. Characters counts only the source that REACHED an engine, which is what the bulk tier bills on; a fully-cached rebuild therefore reports (and costs) zero.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL