translate

package
v1.801.490 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package translate is text in, the same text out in the language you asked for.

POST /v1/translate is 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 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 MemoryEntry added in v1.801.350

type MemoryEntry struct {
	// Source is the ORIGINAL string this entry translates. Part of the identity.
	Source string `json:"source"`
	// Target is the target language tag (BCP-47, e.g. "es" or "pt-BR"). Part of the
	// identity.
	Target string `json:"target"`
	// Tier is the engine tier the entry belongs to, quality or bulk. Part of the
	// identity: the two tiers keep separate renderings of the same source.
	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 is the stored translation. A memory hit returns it verbatim, which is the
	// idempotence contract.
	Text string `json:"text"`
	// State is the entry's position on the review ladder: machine, suggested,
	// approved or published.
	State State `json:"state"`
	// Actor is the validated user id that last wrote this entry by hand. Empty on an
	// entry an engine produced, and on one written before attribution existed.
	Actor string `json:"actor,omitempty"`
	// UpdatedAt is the unix second the entry last changed.
	UpdatedAt int64 `json:"updated_at"`
}

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

type MemoryPage added in v1.801.350

type MemoryPage struct {
	// Data is the matching memory entries, newest first.
	Data []MemoryEntry `json:"data"`
}

MemoryPage is the review lane's read result: the org's own memory entries, newest first. Data is always a (possibly empty) array, never null.

type MemoryQuery added in v1.801.350

type MemoryQuery struct {
	// Target narrows to one target language tag (BCP-47, e.g. "es" or "pt-BR").
	Target string `json:"target"`
	// State narrows to one position on the review ladder: machine, suggested,
	// approved or published.
	State State `json:"state"`
	// Limit caps the rows returned. Non-positive or unparseable means the server
	// default (200); the ceiling is 1000.
	Limit int `json:"limit"`
}

MemoryQuery narrows a review-lane read to part of the org's translation memory. Every field is optional; an omitted one does not filter.

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 is the ORIGINAL string this entry translates. Required; part of the
	// entry's identity, so a different source is a different entry.
	Source string `json:"source"`
	// Target is the target language tag (BCP-47, e.g. "es" or "pt-BR"). Required;
	// part of the entry's identity.
	Target string `json:"target"`
	// Tier is the engine tier the entry belongs to, quality (the default) or bulk.
	// Part of the entry's identity: the two tiers keep separate renderings.
	Tier Tier `json:"tier"`
	// Glossary is the terminology the entry was translated under. Its VERSION — the
	// digest of the sorted terms — is part of the entry's identity, so editing a term
	// yields a new entry rather than overwriting the old rendering.
	Glossary map[string]string `json:"glossary"`
	// Text is the reviewed translation to store. A human write always wins over the
	// stored value.
	Text string `json:"text"`
	// State is the entry's new position on the review ladder: suggested, approved or
	// published. `machine` is engine-only and is refused here — a human may not demote
	// a string back into the churn.
	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