@verbaly/compiler 0.74.0 → 0.75.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 +3 -1
- package/dist/{claude-Baoc6pYL.js → claude-DcV4YBbO.js} +2 -2
- package/dist/{claude-Baoc6pYL.js.map → claude-DcV4YBbO.js.map} +1 -1
- package/dist/cli.js +39 -11
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +8 -15
- package/dist/index.js.map +1 -1
- package/dist/{text-BMBWE8tL.js → text-BtTS6qL7.js} +40 -4
- package/dist/text-BtTS6qL7.js.map +1 -0
- package/package.json +3 -2
- package/dist/text-BMBWE8tL.js.map +0 -1
package/README.md
CHANGED
|
@@ -378,7 +378,7 @@ import {
|
|
|
378
378
|
transformSource,
|
|
379
379
|
} from '@verbaly/compiler';
|
|
380
380
|
|
|
381
|
-
const cfg = await loadConfig(process.cwd());
|
|
381
|
+
const cfg = await loadConfig(process.cwd(), options, { plugin: true });
|
|
382
382
|
const catalogs = loadCatalogs(cfg);
|
|
383
383
|
const registry = new MessageRegistry();
|
|
384
384
|
|
|
@@ -389,6 +389,8 @@ const { messages, result } = transformSource(code, id, registry);
|
|
|
389
389
|
runBuildGate(cfg, registry);
|
|
390
390
|
```
|
|
391
391
|
|
|
392
|
+
Your plugin's options go second and `{ plugin: true }` third: when one the command line also reads (`sourceLocale`, `locales`, `dir`, `include`, `exclude`) differs from `verbaly.config`, the build says once to move it there, since `verbaly check` never reads a bundler config.
|
|
393
|
+
|
|
392
394
|
A build that scans first, as the Vite plugin does to sync the catalogs, keeps that scan with `extractProject(cfg, { keepSource: true })` and passes it as the fourth argument of `transformSource`: a file it read unchanged is not parsed a second time. Call `reportOutsideInclude(cfg)` once when your build starts: it names every file outside `include` that writes a `` t`…` ``, which no build translates. It reads the project itself, so a cached transform never hides one. `writeDts(cfg, catalog)` writes the types where the config resolves them and returns the file and whether it changed.
|
|
393
395
|
|
|
394
396
|
A dev server that writes the catalogs goes through `syncProject`, the same path `extract`, the MCP server and Studio take: it scans (or takes the registry you hold), syncs, writes the catalogs and the types, and keeps `verbaly.lock` in step, in that order; its `findings` say once when that file first appears. Pass `confirm: true` when your registry is built file by file, so a file that changed on disk unseen is read again before its text is written back. `createDevReporter` then says each of its findings once, says it again if it goes away and comes back, and keeps a long list to a few lines.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as counted, o as isModuleNotFound } from "./text-
|
|
1
|
+
import { n as counted, o as isModuleNotFound } from "./text-BtTS6qL7.js";
|
|
2
2
|
//#region src/providers/claude.ts
|
|
3
3
|
const DEFAULT_MODEL = "claude-sonnet-5-5";
|
|
4
4
|
const FALLBACK_MODELS = /* @__PURE__ */ new Set([
|
|
@@ -91,4 +91,4 @@ async function loadSdk(load = () => import("@anthropic-ai/sdk")) {
|
|
|
91
91
|
//#endregion
|
|
92
92
|
export { claudeProvider };
|
|
93
93
|
|
|
94
|
-
//# sourceMappingURL=claude-
|
|
94
|
+
//# sourceMappingURL=claude-DcV4YBbO.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude-
|
|
1
|
+
{"version":3,"file":"claude-DcV4YBbO.js","names":[],"sources":["../src/providers/claude.ts"],"sourcesContent":["import { isModuleNotFound } from '../config';\nimport { counted } from '../text';\nimport type { TranslateProvider, TranslateRequest } from '../translate';\n\nexport interface ClaudeProviderOptions {\n model?: string;\n apiKey?: string;\n maxTokens?: number;\n}\n\n// balanced default for translation: the current Sonnet, at the price of the one before it\nconst DEFAULT_MODEL = 'claude-sonnet-5-5';\n\n// the models that take fallbacks \"default\": a batch one declines is answered by another model\nconst FALLBACK_MODELS = new Set([\n 'claude-sonnet-5-5',\n 'claude-opus-5-5',\n 'claude-opus-5',\n 'claude-fable-5-1',\n]);\nconst FALLBACK_BETA = 'server-side-fallback-2026-07-01';\n\n// the least thinking each model takes: disabled is a 400 on the newest, and the oldest never think\nexport function thinkingFor(model: string): { type: 'between_tools' | 'disabled' } | undefined {\n if (model === 'claude-sonnet-5-5') return { type: 'between_tools' };\n if (model === 'claude-sonnet-5' || model === 'claude-opus-5') return { type: 'disabled' };\n return undefined;\n}\n\nconst SYSTEM = `You translate UI strings for the Verbaly i18n library.\nRules:\n- Translate only the human-readable text, naturally for the target locale.\n- Preserve verbatim: placeholders like {name}, format specs like {price:currency/EUR}, variant blocks like {count | one: ... | other: # ...} (translate only the text inside each variant, keep keys and # as-is), ICU syntax, named tags like <em>...</em> and escapes {{ }} || ##.\n- Keys are opaque identifiers: return exactly the same keys, never translate or rename them.`;\n\nexport function systemPrompt(instructions?: string): string {\n return instructions ? `${SYSTEM}\\n\\nProject instructions:\\n${instructions}` : SYSTEM;\n}\n\nexport function claudeProvider(options: ClaudeProviderOptions = {}): TranslateProvider {\n // one client per provider: a batch is a request, not a reason to rebuild the transport\n let client: Promise<InstanceType<typeof import('@anthropic-ai/sdk').default>> | undefined;\n\n return async (request: TranslateRequest) => {\n client ??= loadSdk().then(\n (Anthropic) => new Anthropic(options.apiKey ? { apiKey: options.apiKey } : {}),\n );\n const model = options.model ?? DEFAULT_MODEL;\n const thinking = thinkingFor(model);\n const response = await (\n await client\n ).beta.messages.create({\n model,\n max_tokens: options.maxTokens ?? 16000,\n ...(thinking && { thinking }),\n ...(FALLBACK_MODELS.has(model) && { betas: [FALLBACK_BETA], fallbacks: 'default' as const }),\n system: systemPrompt(request.instructions),\n messages: [{ role: 'user', content: buildPrompt(request) }],\n output_config: { format: batchFormat(request) },\n });\n const batch = counted(Object.keys(request.messages).length, 'message');\n if (response.stop_reason === 'max_tokens') {\n throw new Error(\n `[verbaly] the model ran out of output room on a batch of ${batch}: lower translate.batchSize (or raise maxTokens on the provider)`,\n );\n }\n // a decline is about the content, so asking again gets the same answer: a 400 is never retried\n if (response.stop_reason === 'refusal') {\n const category = response.stop_details?.category;\n throw Object.assign(\n new Error(\n `[verbaly] the model declined a batch of ${batch}${category ? ` (${category})` : ''}, so they stay untranslated: translate them by hand or with another provider`,\n ),\n { status: 400 },\n );\n }\n const text = response.content.find((block) => block.type === 'text')?.text ?? '{}';\n try {\n return JSON.parse(text) as Record<string, string>;\n } catch (error) {\n throw new Error('[verbaly] the model did not answer with the JSON object it was asked for', {\n cause: error,\n });\n }\n };\n}\n\nexport function buildPrompt(request: TranslateRequest): string {\n return (\n `Translate each value from \"${request.sourceLocale}\" to \"${request.targetLocale}\". ` +\n `Return a JSON object with the same keys and translated values.\\n\\n` +\n JSON.stringify(request.messages, null, 2) +\n glossarySection(request) +\n originsSection(request)\n );\n}\n\n// the glossary is a requirement, not a hint: it exists because a brand name came back translated\nfunction glossarySection(request: TranslateRequest): string {\n const glossary = request.glossary;\n if (!glossary || Object.keys(glossary).length === 0) return '';\n const lines = Object.entries(glossary).map(([term, rendering]) => ` ${term} -> ${rendering}`);\n return `\\n\\nGlossary, these renderings are required wherever the term appears:\\n${lines.join('\\n')}`;\n}\n\nfunction originsSection(request: TranslateRequest): string {\n const origins = request.origins;\n if (!origins || Object.keys(origins).length === 0) return '';\n const lines = Object.entries(origins).map(([key, files]) => ` ${key}: ${files.join(', ')}`);\n return `\\n\\nWhere each string appears (context for tone and length, do not translate these paths):\\n${lines.join('\\n')}`;\n}\n\nexport function batchFormat(request: TranslateRequest) {\n const keys = Object.keys(request.messages);\n return {\n type: 'json_schema' as const,\n schema: {\n type: 'object',\n properties: Object.fromEntries(keys.map((key) => [key, { type: 'string' }])),\n required: keys,\n additionalProperties: false,\n },\n };\n}\n\ntype SdkModule = { default: typeof import('@anthropic-ai/sdk').default };\n\n// injectable for tests\nexport async function loadSdk(\n load: () => Promise<SdkModule> = () => import('@anthropic-ai/sdk'),\n): Promise<SdkModule['default']> {\n try {\n const mod = await load();\n return mod.default;\n } catch (error) {\n if (isModuleNotFound(error, '@anthropic-ai/sdk')) {\n throw new Error(\n '[verbaly] the claude translate provider needs @anthropic-ai/sdk: install it as a dev dependency (e.g. `pnpm add -D @anthropic-ai/sdk`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AK,QAAQ,CAAC,KAAK,EAAE,MAAM,SAAS,CAAC,CAAC,CAAC;GAC3E,UAAU;GACV,sBAAsB;EACxB;CACF;AACF;AAKA,eAAsB,QACpB,aAAuC,OAAO,sBACf;CAC/B,IAAI;EAEF,QAAO,MADW,KAAK,EAAA,CACZ;CACb,SAAS,OAAO;EACd,IAAI,iBAAiB,OAAO,mBAAmB,GAC7C,MAAM,IAAI,MACR,mMACA,EAAE,OAAO,MAAM,CACjB;EAEF,MAAM;CACR;AACF"}
|
package/dist/cli.js
CHANGED
|
@@ -697,11 +697,14 @@ function tokenText(token) {
|
|
|
697
697
|
}
|
|
698
698
|
function blockEnd(tokens, start, pairs) {
|
|
699
699
|
let depth = 0;
|
|
700
|
+
let head = true;
|
|
700
701
|
for (let k = start; k < tokens.length; k++) {
|
|
701
702
|
const token = tokens[k];
|
|
702
703
|
if (typeof token !== "string") {
|
|
703
704
|
if (token.head) depth += 1;
|
|
704
|
-
} else if (token === "
|
|
705
|
+
} else if (token === "|" && depth === 1) head = false;
|
|
706
|
+
else if (token === "}" && head && depth === 1) return k;
|
|
707
|
+
else if (token === "{" || token === "}") {
|
|
705
708
|
if (tokens[k + 1] === token && (token === "{" || !(pairs && depth > 1))) k += 1;
|
|
706
709
|
else {
|
|
707
710
|
depth += token === "{" ? 1 : -1;
|
|
@@ -1620,6 +1623,14 @@ function pseudoCatalogs(cfg, catalogs, locale = PSEUDO_LOCALE) {
|
|
|
1620
1623
|
return Object.keys(target).filter((key) => target[key]);
|
|
1621
1624
|
}
|
|
1622
1625
|
//#endregion
|
|
1626
|
+
//#region src/warn.ts
|
|
1627
|
+
const warned = /* @__PURE__ */ new Set();
|
|
1628
|
+
function warnOnce(message, dedupeKey = message) {
|
|
1629
|
+
if (warned.has(dedupeKey)) return;
|
|
1630
|
+
warned.add(dedupeKey);
|
|
1631
|
+
console.warn(`[verbaly] ${message}`);
|
|
1632
|
+
}
|
|
1633
|
+
//#endregion
|
|
1623
1634
|
//#region src/config.ts
|
|
1624
1635
|
const SOURCE_EXTENSIONS = "{js,jsx,ts,tsx,mjs,mts,svelte,vue,astro}";
|
|
1625
1636
|
function resolveConfig(config = {}) {
|
|
@@ -1699,13 +1710,38 @@ function isModuleNotFound(error, name) {
|
|
|
1699
1710
|
function targetLocales(cfg, override) {
|
|
1700
1711
|
return (override ?? cfg.locales).filter((locale) => locale !== cfg.sourceLocale && cfg.locales.includes(locale));
|
|
1701
1712
|
}
|
|
1702
|
-
async function loadConfig(root, overrides = {}) {
|
|
1713
|
+
async function loadConfig(root, overrides = {}, from = {}) {
|
|
1714
|
+
const fileConfig = await loadConfigFile(root);
|
|
1715
|
+
if (from.plugin) warnPluginOnly(root, fileConfig, overrides);
|
|
1703
1716
|
return resolveConfig({
|
|
1704
|
-
...
|
|
1717
|
+
...fileConfig,
|
|
1705
1718
|
...definedOnly(overrides),
|
|
1706
1719
|
root
|
|
1707
1720
|
});
|
|
1708
1721
|
}
|
|
1722
|
+
const READ_BY_CLI = [
|
|
1723
|
+
"sourceLocale",
|
|
1724
|
+
"locales",
|
|
1725
|
+
"dir",
|
|
1726
|
+
"include",
|
|
1727
|
+
"exclude"
|
|
1728
|
+
];
|
|
1729
|
+
function warnPluginOnly(root, file, overrides) {
|
|
1730
|
+
const cli = resolveConfig({
|
|
1731
|
+
...file,
|
|
1732
|
+
root
|
|
1733
|
+
});
|
|
1734
|
+
const build = resolveConfig({
|
|
1735
|
+
...file,
|
|
1736
|
+
...definedOnly(overrides),
|
|
1737
|
+
root
|
|
1738
|
+
});
|
|
1739
|
+
const keys = READ_BY_CLI.filter((key) => overrides[key] !== void 0 && JSON.stringify(cli[key]) !== JSON.stringify(build[key]));
|
|
1740
|
+
if (keys.length === 0) return;
|
|
1741
|
+
const names = keys.length > 1 ? `${keys.slice(0, -1).join(", ")} and ${keys.at(-1)}` : keys[0];
|
|
1742
|
+
const [verb, pronoun] = keys.length > 1 ? ["are", "them"] : ["is", "it"];
|
|
1743
|
+
warnOnce(`${names} ${verb} set in the plugin options, which \`verbaly check\` and \`verbaly extract\` never read: move ${pronoun} to verbaly.config so the command line checks what the build ships`, `plugin-only:${root}`);
|
|
1744
|
+
}
|
|
1709
1745
|
function definedOnly(config) {
|
|
1710
1746
|
return Object.fromEntries(Object.entries(config).filter(([, value]) => value !== void 0));
|
|
1711
1747
|
}
|
|
@@ -2699,14 +2735,6 @@ function includeTypes(root, file) {
|
|
|
2699
2735
|
writeFileSync(read.path, applyEdits(read.text, edits));
|
|
2700
2736
|
return "added";
|
|
2701
2737
|
}
|
|
2702
|
-
//#endregion
|
|
2703
|
-
//#region src/warn.ts
|
|
2704
|
-
const warned = /* @__PURE__ */ new Set();
|
|
2705
|
-
function warnOnce(message, dedupeKey = message) {
|
|
2706
|
-
if (warned.has(dedupeKey)) return;
|
|
2707
|
-
warned.add(dedupeKey);
|
|
2708
|
-
console.warn(`[verbaly] ${message}`);
|
|
2709
|
-
}
|
|
2710
2738
|
const DTS_HEADER = "// generated by verbaly: do not edit";
|
|
2711
2739
|
function generateDts(catalog, options = {}) {
|
|
2712
2740
|
const lines = [];
|