@verbatra/sdk 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,43 +5,42 @@
5
5
  <h1 align="center">@verbatra/sdk</h1>
6
6
 
7
7
  <p align="center">
8
- Programmatic API to automate i18n translation and keep your locale files in sync across languages, using OpenAI, Anthropic, Gemini, DeepL, Google Cloud Translation, or an openai-compatible local or self-hosted model.
8
+ The programmatic API behind verbatra: translate your locale files incrementally, and never write a translation that breaks a placeholder.
9
9
  </p>
10
10
 
11
11
  <p align="center">
12
12
  <a href="https://www.npmjs.com/package/@verbatra/sdk"><img src="https://img.shields.io/npm/v/%40verbatra%2Fsdk?label=%40verbatra%2Fsdk&amp;color=7b1fa2&amp;labelColor=0b0b12" alt="@verbatra/sdk npm version" /></a>
13
13
  <a href="https://www.npmjs.com/package/@verbatra/sdk"><img src="https://img.shields.io/npm/types/%40verbatra%2Fsdk?color=7b1fa2&amp;labelColor=0b0b12" alt="Ships TypeScript types" /></a>
14
14
  <a href="https://github.com/verbatra/verbatra/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/verbatra/verbatra/ci.yml?branch=main&amp;label=CI&amp;labelColor=0b0b12" alt="CI status on main" /></a>
15
+ <a href="https://www.npmjs.com/package/@verbatra/sdk#provenance"><img src="https://img.shields.io/badge/npm%20provenance-SLSA%20v1-7b1fa2?labelColor=0b0b12" alt="npm provenance: SLSA v1 build attestation" /></a>
15
16
  <a href="https://github.com/verbatra/verbatra/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue?color=7b1fa2&amp;labelColor=0b0b12" alt="License: MIT" /></a>
16
17
  </p>
17
18
 
19
+ <p align="center">
20
+ <a href="https://verbatra.kreitz-webdev.de/docs/sdk">Documentation</a> |
21
+ <a href="https://verbatra.kreitz-webdev.de/docs/sdk-quickstart">Quickstart</a> |
22
+ <a href="https://github.com/verbatra/verbatra/blob/main/packages/sdk/CHANGELOG.md">Changelog</a> |
23
+ <a href="https://github.com/verbatra/verbatra/issues">Issues</a>
24
+ </p>
25
+
18
26
  ## Description
19
27
 
20
28
  `@verbatra/sdk` is the engine behind verbatra: load and validate a config, translate every target locale once, watch the source and re-translate on each change, check or diff your locales without writing, validate the whole project setup before you spend anything, extract keys from your application source, or hand strings off to a human translator and read them back. Each run diffs your source against the lock file and sends only what changed, and every candidate value, whatever produced it, passes one integrity gate before it is written. [`@verbatra/cli`](https://www.npmjs.com/package/@verbatra/cli) is a thin wrapper over this package.
21
29
 
22
- ## Requirements
23
-
24
- Node.js `>=22.14.0`.
25
-
26
30
  ## Installation
27
31
 
28
32
  ```bash
29
33
  npm install --save-dev @verbatra/sdk
30
- # pnpm
31
- pnpm add -D @verbatra/sdk
32
- # yarn
33
- yarn add -D @verbatra/sdk
34
34
  ```
35
35
 
36
+ Needs Node.js `^22.18.0 || >=24`. The package ships its own TypeScript types and a CommonJS and an ES module build.
37
+
36
38
  ## Quick start
37
39
 
38
40
  ```ts
39
41
  import { loadConfig, translate } from "@verbatra/sdk";
40
42
 
41
- // Discovers and validates verbatra.config.ts (or .verbatrarc.json, or a package.json "verbatra" key).
42
43
  const config = await loadConfig();
43
-
44
- // The provider reads its API key from the environment (e.g. GEMINI_API_KEY). No key is passed.
45
44
  const summary = await translate({ config });
46
45
 
47
46
  console.log(
@@ -49,7 +48,9 @@ console.log(
49
48
  );
50
49
  ```
51
50
 
52
- ## Defining config
51
+ `loadConfig` finds and validates `verbatra.config.ts` (or `.js` or `.cjs`), a `.verbatrarc` file with no extension or a `.json`, `.yaml`, `.yml`, `.js`, `.cjs` or `.ts` one, or a `package.json` `"verbatra"` key. The provider reads its API key from the environment, for example `GEMINI_API_KEY`; no key is ever passed to the SDK. The [SDK quickstart](https://verbatra.kreitz-webdev.de/docs/sdk-quickstart) walks through the first run step by step, from a keyless dry run to a CI gate, and [SDK recipes](https://verbatra.kreitz-webdev.de/docs/programmatic-api) has complete examples for the other entry points.
52
+
53
+ ## Configuration
53
54
 
54
55
  `defineConfig` is an identity helper that gives you full type inference while authoring `verbatra.config.ts`:
55
56
 
@@ -66,104 +67,52 @@ export default defineConfig({
66
67
  provider: {
67
68
  id: "gemini",
68
69
  options: {
69
- model: "gemini-2.5-flash", // replace with your provider's model id
70
+ model: "gemini-2.5-flash",
70
71
  maxOutputTokens: 4096,
71
72
  },
72
73
  },
73
74
  });
74
75
  ```
75
76
 
76
- `files.pattern` must contain the `{locale}` token, and `targetLocales` must neither include `sourceLocale` nor list one locale twice. Beyond the required keys, the config carries optional `glossary` and `tone`, opt-in `prune` and `generatePlurals`, the `files.localeStyle` path layout, opt-in `fuzzyCache` reuse, the `maxBatchSize` request size, per-key `maxLength` review budgets, the `maxTokens`/`budgetBehavior` run budget, dated `rates` for cost estimates, and the `extract` source-scanning settings. Every key, every default, and every provider's option shape is documented on the [Configuration page](https://verbatra.kreitz-webdev.de/docs/config-file); the providers and their key variables are on the [Providers page](https://verbatra.kreitz-webdev.de/docs/providers).
77
-
78
- API keys are never part of the config. Each provider reads its own environment variable, and the SDK never accepts one as an argument.
79
-
80
- ## API reference
81
-
82
- Every flow takes an `input` object and an optional `deps` object for injecting the file system, the provider factory, or an adapter registry of your own; the two config helpers below take neither. The signatures below are the ones the package publishes in `dist/index.d.ts`.
83
-
84
- ### `defineConfig(config): VerbatraConfigInput`
85
-
86
- Identity helper that types a config literal against the provider you name in `provider.id`. See [Configuration](https://verbatra.kreitz-webdev.de/docs/config-file).
87
-
88
- ### `loadConfig(options?): Promise<VerbatraConfig>`
89
-
90
- Discover, load, and validate the project config, searching upward from the working directory. Throws an `SdkError` when no config is found or validation fails.
91
-
92
- ### `translate(input, deps?): Promise<RunSummary>`
93
-
94
- Translate every configured target locale once. The summary splits locales into succeeded, partial, and failed, and carries the run's notices, review flags, token usage, and budget standing. See [`verbatra translate`](https://verbatra.kreitz-webdev.de/docs/cli/translate).
95
-
96
- ### `watch(input, deps?): Promise<WatchController>`
97
-
98
- Watch the source locale file and re-translate on every change until the returned controller is stopped. Each run gets a fresh budget. See [`verbatra watch`](https://verbatra.kreitz-webdev.de/docs/cli/watch).
99
-
100
- ### `check(input, deps?): Promise<CheckSummary>`
101
-
102
- Count missing, stale, and up-to-date keys per locale. Calls no provider and writes nothing. See [`verbatra check`](https://verbatra.kreitz-webdev.de/docs/cli/check).
77
+ Every optional key, every default, and every provider's option shape is documented on the [Configuration page](https://verbatra.kreitz-webdev.de/docs/config-file); the providers and their key variables are on the [Providers page](https://verbatra.kreitz-webdev.de/docs/providers). API keys are never part of the config.
103
78
 
104
- ### `diff(input, deps?): Promise<DiffSummary>`
79
+ ## API overview
105
80
 
106
- Name the keys that would be added, re-translated, or orphaned per locale. Calls no provider and writes nothing. See [`verbatra diff`](https://verbatra.kreitz-webdev.de/docs/cli/diff).
81
+ Every flow takes an `input` object and an optional `deps` object for injecting the file system, the provider factory, or an adapter registry of your own. The signatures are the ones the package publishes in `dist/index.d.ts`; each row links to the docs page that holds the full reference.
107
82
 
108
- ### `doctor(input?, deps?): Promise<DoctorResult>`
83
+ | Area | Exports | Reference |
84
+ | --- | --- | --- |
85
+ | Run | `translate`, `watch`, `pseudolocalize`, `generateTypes`, `resolveDryRun` | [Run](https://verbatra.kreitz-webdev.de/docs/sdk/run) |
86
+ | Inspect, read-only | `check`, `checkFile`, `diff`, `doctor`, `dataFlow`, `extract`, `lockState`, `runStatus`, `reviewQueue`, `provenanceReport`, `localeIntegrity` | [Inspect](https://verbatra.kreitz-webdev.de/docs/sdk/inspect) |
87
+ | Single keys and review | `keyValue`, `keyContext`, `keyIntegrity`, `localeValues`, `editEntry`, `retranslateEntry`, `approveEntry`, `rejectEntry`, `approveLocale`, `approveEntries`, `rejectEntries`, `retranslateEntries`, `localeHistory` | [Review](https://verbatra.kreitz-webdev.de/docs/sdk/review) |
88
+ | Handoff | `exportWorkbook`, `importWorkbook`, `exportTmx`, `importTmx`, `readLocaleFileSnapshot`, `diffLocaleSnapshots`, `createLocalePathResolver` | [Exchange](https://verbatra.kreitz-webdev.de/docs/sdk/exchange) |
89
+ | Config and glossary | `defineConfig`, `loadConfig`, `loadConfigWithMeta`, `detectProject`, `verbatraConfigSchema`, `readGlossaryFile`, `updateGlossaryTerm`, `glossaryHits`, `glossaryDraftCheck`, `isMachineTranslationEnabled`, `assertMachineTranslationEnabled` | [Config](https://verbatra.kreitz-webdev.de/docs/sdk/config) |
90
+ | Extension points | `createDefaultRegistry`, `createTreeFileAdapter`, `createFlatFileAdapter`, `AdapterRegistry`, `nodeAdapterFs`, `redact`, `declareProviderKeyEnvVar`, `releaseHeldLocks` | [Extend](https://verbatra.kreitz-webdev.de/docs/sdk/extend) |
91
+ | JSON schemas | `SDK_JSON_SCHEMAS`, `renderJsonSchemas`, `renderOutputJsonSchema`, `jsonSchemaUrl`, and one zod schema per result, such as `runSummarySchema` and `checkSummarySchema` | [JSON schemas](https://verbatra.kreitz-webdev.de/docs/sdk/json-schemas) |
92
+ | Constants | `LOCK_FILE_NAME`, `CACHE_FILE_NAME`, `PROVENANCE_FILE_NAME`, `EXCHANGE_FORMATS`, `INTEGRITY_GATE_REASONS`, `REVIEW_REASON_CODES`, `QA_SEVERITIES` | [Inspect](https://verbatra.kreitz-webdev.de/docs/sdk/inspect), [Run](https://verbatra.kreitz-webdev.de/docs/sdk/run), [Exchange](https://verbatra.kreitz-webdev.de/docs/sdk/exchange), [Review](https://verbatra.kreitz-webdev.de/docs/sdk/review) |
109
93
 
110
- Validate the config, the format adapter, the provider, its key variable, and the source locale file in one pass, reporting every problem at once. Reads no key value. See [`verbatra doctor`](https://verbatra.kreitz-webdev.de/docs/cli/doctor).
111
-
112
- ### `extract(input, deps?): Promise<ExtractResult>`
113
-
114
- Scan your application source for translation call sites and add the new keys to the source locale file. See [`verbatra extract`](https://verbatra.kreitz-webdev.de/docs/cli/extract).
115
-
116
- ### `generateTypes(input, deps?): Promise<GenerateTypesResult>`
117
-
118
- Generate a TypeScript declaration of every source catalog key and the arguments its message interpolates. See [`verbatra types`](https://verbatra.kreitz-webdev.de/docs/cli/types).
119
-
120
- ### `pseudolocalize(input, deps?): Promise<PseudolocalizeResult>`
121
-
122
- Build a pseudolocale from the source strings, accented, expanded, and bracketed, without calling a provider. See [`verbatra pseudo`](https://verbatra.kreitz-webdev.de/docs/cli/pseudo).
123
-
124
- ### `exportWorkbook(input, deps?): Promise<ExportWorkbookResult>`
125
-
126
- Write the strings that need translating to a translator handoff: a styled Excel workbook, or one CSV or TSV file per locale. See [Manual translation](https://verbatra.kreitz-webdev.de/docs/manual-translation).
127
-
128
- ### `importWorkbook(input, deps?): Promise<RunSummary>`
129
-
130
- Read a filled handoff back into the locale files, through the same integrity gate a translate run applies, returning the same `RunSummary` shape.
131
-
132
- ### `exportTmx(input, deps?)` and `importTmx(input, deps?)`
133
-
134
- Move the whole translation memory out as a TMX file any other translation tool can read, or read one in. See [`verbatra tmx`](https://verbatra.kreitz-webdev.de/docs/cli/tmx).
135
-
136
- ### More entry points
137
-
138
- The remaining exports are the building blocks Verbatra Studio, the MCP server, and other tooling sit on. Most are a one-call read or a locked single-key write.
139
-
140
- | Export | What it does |
141
- | --- | --- |
142
- | `editEntry`, `retranslateEntry` | Save a manual translation for one key, or re-run the provider for one key. Both run the candidate through the same integrity gate as a full run and hold the same per-locale write lock; a rejection names an `IntegrityGateReason` and writes nothing |
143
- | `keyValue`, `localeValues` | Read one key's current source and target values, or a whole locale's key/value pairs |
144
- | `keyIntegrity` | Report, per changed key, whether its placeholders, inline markup, and ICU still match the locked baseline |
145
- | `lockState`, `loadLockFile` | Read the lock file's existence, version, and per-locale drift, or the lock file itself |
146
- | `runStatus`, `budgetStanding` | Read the review-flag and token-usage snapshot the last non-dry run left behind, and turn a `RunBudget` into a standing |
147
- | `readLocaleFileSnapshot`, `diffLocaleSnapshots` | Snapshot one locale file as per-key content hashes and compare two snapshots: the primitives behind live-refresh watching |
148
- | `loadConfigWithMeta`, `readGlossaryFile`, `updateGlossaryTerm` | `loadConfig` plus config-source and glossary provenance, and the file-backed glossary read and single-term write that take that provenance rather than a path |
149
- | `createLocalePathResolver` | Build the two-way locale-to-path mapping from a config, so a watcher can decide whether a changed file concerns verbatra at all |
150
- | `createDefaultRegistry`, `createTreeFileAdapter`, `createFlatFileAdapter`, `AdapterRegistry`, `nodeAdapterFs` | The format-adapter construction surface: build an adapter for a format verbatra does not ship and hand the registry to a flow as `deps.adapterRegistry` |
151
- | `verbatraConfigSchema`, `scaffoldingMetadata` | The zod schema `loadConfig` validates against (also published as `@verbatra/sdk/config-schema.json`), and the facts a project generator needs to write a first config |
152
- | `LOCK_FILE_NAME`, `CACHE_FILE_NAME`, `EXCHANGE_FORMATS`, `DEFAULT_EXCHANGE_FORMAT`, `DEFAULT_WORKBOOK_PATH`, `DEFAULT_DELIMITED_PATH`, `DEFAULT_TMX_PATH`, `DEFAULT_TYPES_PATH` | The file names and handoff formats a run uses, so tooling can find, offer, or gitignore them without restating the list |
153
- | `SdkFs`, `redact`, `resolveDryRun`, `isCustomFormatId`, `tmxErrorLocation` | The file-system port every file the SDK touches goes through, the secret-redaction pass, and small helpers for agreeing with verbatra rather than restating it |
94
+ The package also publishes the config JSON Schema as `@verbatra/sdk/config-schema.json` and the result schemas under `@verbatra/sdk/schemas/*`, for editor validation and for consumers outside TypeScript.
154
95
 
155
96
  ## Errors and results
156
97
 
157
- `SdkError` is the SDK's own structured error type, thrown for whole-run failures such as a missing or invalid config or an unreadable source file. It carries a stable `code` from the exported `SdkErrorCode` union and never contains an API key. It is not the only error a caller can see: `retranslateEntry` propagates the provider's own `ProviderError`, and a target locale file that exists but is malformed rejects with the adapter's own `AdapterError`.
158
-
159
- Per-locale failures do not throw. They are recorded on the `RunSummary` so one failing locale never aborts the others, including a locale whose write lock could not be acquired. A locale's `error.code` is a preserved string from the underlying provider or adapter failure (`"LOCALE_FAILED"` is only the fallback), deliberately wider than `SdkErrorCode`, so do not treat it as a closed set.
98
+ `SdkError` is the SDK's own structured error, thrown for whole-run failures such as a missing or invalid config or an unreadable source file. It carries a stable `code` from the exported `SdkErrorCode` union and never contains an API key. Per-locale failures do not throw: they are recorded on the `RunSummary` so one failing locale never aborts the others. The full model, including `ProviderError` and `AdapterError`, is on the [Errors page](https://verbatra.kreitz-webdev.de/docs/sdk/errors), and every code is listed in the [error code reference](https://verbatra.kreitz-webdev.de/docs/error-codes).
160
99
 
161
100
  ## Documentation
162
101
 
163
- - [Documentation site](https://verbatra.kreitz-webdev.de)
164
102
  - [SDK reference](https://verbatra.kreitz-webdev.de/docs/sdk)
103
+ - [SDK quickstart](https://verbatra.kreitz-webdev.de/docs/sdk-quickstart)
165
104
  - [Configuration](https://verbatra.kreitz-webdev.de/docs/config-file)
166
- - [`@verbatra/cli`](https://www.npmjs.com/package/@verbatra/cli) for the command-line tool
105
+ - [Custom format adapters](https://verbatra.kreitz-webdev.de/docs/custom-format-adapters)
106
+
107
+ ## Related packages
108
+
109
+ - [`@verbatra/cli`](https://www.npmjs.com/package/@verbatra/cli): the `verbatra` command, a thin wrapper over this package.
110
+ - [`@verbatra/studio`](https://www.npmjs.com/package/@verbatra/studio): the local dashboard.
111
+ - [`@verbatra/mcp`](https://www.npmjs.com/package/@verbatra/mcp): a stdio MCP server for coding agents.
112
+
113
+ ## Contributing
114
+
115
+ See [CONTRIBUTING.md](https://github.com/verbatra/verbatra/blob/main/CONTRIBUTING.md).
167
116
 
168
117
  ## License
169
118