better-translation 0.1.5 → 0.1.6

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/dist/ai.d.mts CHANGED
@@ -8,8 +8,6 @@ interface CreateAiTranslateOptions {
8
8
  model?: AiModel;
9
9
  /** Primary translation brief for product, tone, glossary, or domain instructions. */
10
10
  prompt?: string;
11
- /** Maximum number of messages sent in a single translation request. */
12
- batchSize?: number;
13
11
  /** Optional temperature forwarded to the selected model provider. */
14
12
  temperature?: number;
15
13
  }
package/dist/ai.d.mts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ai.d.mts","names":[],"sources":["../src/ai.ts"],"mappings":";;;;KAOK,OAAA,GAAU,UAAA,QAAkB,YAAA;AAAA,UAKhB,wBAAA;EALZ;EAOH,KAAA,GAAQ,OAAA;;EAER,MAAA;EAT2C;EAW3C,SAAA;EANuC;EAQvC,WAAA;AAAA;AAAA,iBAGc,iBAAA,CAAkB,OAAA,GAAS,wBAAA,GAAgC,WAAA"}
1
+ {"version":3,"file":"ai.d.mts","names":[],"sources":["../src/ai.ts"],"mappings":";;;;KAKK,OAAA,GAAU,UAAA,QAAkB,YAAA;AAAA,UAEhB,wBAAA;EAFZ;EAIH,KAAA,GAAQ,OAAA;;EAER,MAAA;EAN2C;EAQ3C,WAAA;AAAA;AAAA,iBAGc,iBAAA,CAAkB,OAAA,GAAS,wBAAA,GAAgC,WAAA"}
package/dist/ai.mjs CHANGED
@@ -1,36 +1,24 @@
1
- import { z } from "zod";
2
1
  //#region src/ai.ts
3
2
  const DEFAULT_GATEWAY_MODEL = "openai/gpt-5.5";
4
- const DEFAULT_BATCH_SIZE = 25;
5
- const translationPayloadSchema = z.object({ translations: z.record(z.string(), z.string()) });
6
3
  function createAiTranslate(options = {}) {
7
4
  return async (messages, locale) => {
8
5
  const result = {};
9
- const batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE;
10
- for (let index = 0; index < messages.length; index += batchSize) {
11
- const batch = messages.slice(index, index + batchSize);
12
- Object.assign(result, await translateBatch(batch, locale, options));
13
- }
6
+ for (const message of messages) result[message.id] = await translateMessage(message, locale, options);
14
7
  return result;
15
8
  };
16
9
  }
17
- async function translateBatch(messages, locale, options) {
18
- const { translations } = await translateWithAi(messages, locale, options);
19
- return Object.fromEntries(messages.map((message) => {
20
- const translated = translations[message.id]?.trim();
21
- return [message.id, translated || message.text];
22
- }));
10
+ async function translateMessage(message, locale, options) {
11
+ return (await translateWithAi(message, locale, options)).trim() || message.text;
23
12
  }
24
- async function translateWithAi(messages, locale, options) {
25
- const { generateText, Output } = await import("ai");
26
- const { output } = await generateText({
13
+ async function translateWithAi(message, locale, options) {
14
+ const { generateText } = await import("ai");
15
+ const { text } = await generateText({
27
16
  model: options.model ?? DEFAULT_GATEWAY_MODEL,
28
- output: Output.object({ schema: translationPayloadSchema }),
29
17
  system: createSystemPrompt(locale, options.prompt),
30
- prompt: createUserPrompt(messages, locale),
18
+ prompt: createUserPrompt(message, locale),
31
19
  ...options.temperature === void 0 ? {} : { temperature: options.temperature }
32
20
  });
33
- return output;
21
+ return text;
34
22
  }
35
23
  function createSystemPrompt(locale, prompt) {
36
24
  return [`## Translation Brief
@@ -39,19 +27,19 @@ ${prompt?.trim() || "Translate the provided UI messages as concise, natural appl
39
27
  ## Target Locale
40
28
  ${locale}
41
29
 
42
- ## Required Output Contract
43
- Return translations keyed by message id in the requested structured output.
44
- Use each message context when provided.
45
- Do not add labels, explanations, markdown, or code fences.`].join("\n\n");
30
+ ## Output Contract
31
+ Return only the translated text for the provided source message.
32
+ Do not include the message id, labels, explanations, markdown, code fences, or surrounding quotes.
33
+ Use the message context when provided.`].join("\n\n");
46
34
  }
47
- function createUserPrompt(messages, locale) {
35
+ function createUserPrompt(message, locale) {
48
36
  return JSON.stringify({
49
37
  targetLocale: locale,
50
- messages: messages.map((message) => ({
38
+ message: {
51
39
  id: message.id,
52
40
  text: message.text,
53
41
  context: message.meta.context
54
- }))
42
+ }
55
43
  });
56
44
  }
57
45
  //#endregion
package/dist/ai.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ai.mjs","names":[],"sources":["../src/ai.ts"],"sourcesContent":["import type { generateText } from \"ai\"\nimport { z } from \"zod\"\n\nimport type { TranslateFn, TranslateMessage } from \"./types.js\"\n\nconst DEFAULT_GATEWAY_MODEL = \"openai/gpt-5.5\"\nconst DEFAULT_BATCH_SIZE = 25\ntype AiModel = Parameters<typeof generateText>[0][\"model\"]\nconst translationPayloadSchema = z.object({\n translations: z.record(z.string(), z.string()),\n})\n\nexport interface CreateAiTranslateOptions {\n /** AI SDK model value. Defaults to a Vercel AI Gateway model string. */\n model?: AiModel\n /** Primary translation brief for product, tone, glossary, or domain instructions. */\n prompt?: string\n /** Maximum number of messages sent in a single translation request. */\n batchSize?: number\n /** Optional temperature forwarded to the selected model provider. */\n temperature?: number\n}\n\nexport function createAiTranslate(options: CreateAiTranslateOptions = {}): TranslateFn {\n return async (messages, locale) => {\n const result: Record<string, string> = {}\n const batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE\n\n for (let index = 0; index < messages.length; index += batchSize) {\n const batch = messages.slice(index, index + batchSize)\n Object.assign(result, await translateBatch(batch, locale, options))\n }\n\n return result\n }\n}\n\nasync function translateBatch(messages: TranslateMessage[], locale: string, options: CreateAiTranslateOptions) {\n const { translations } = await translateWithAi(messages, locale, options)\n\n return Object.fromEntries(\n messages.map((message) => {\n const translated = translations[message.id]?.trim()\n return [message.id, translated || message.text]\n }),\n )\n}\n\nasync function translateWithAi(messages: TranslateMessage[], locale: string, options: CreateAiTranslateOptions) {\n const { generateText, Output } = await import(\"ai\")\n const { output } = await generateText({\n model: options.model ?? DEFAULT_GATEWAY_MODEL,\n output: Output.object({ schema: translationPayloadSchema }),\n system: createSystemPrompt(locale, options.prompt),\n prompt: createUserPrompt(messages, locale),\n ...(options.temperature === undefined ? {} : { temperature: options.temperature }),\n })\n\n return output\n}\n\nfunction createSystemPrompt(locale: string, prompt?: string) {\n const translationBrief = prompt?.trim() || \"Translate the provided UI messages as concise, natural application UI copy.\"\n\n return [\n `## Translation Brief\n${translationBrief}\n\n## Target Locale\n${locale}\n\n## Required Output Contract\nReturn translations keyed by message id in the requested structured output.\nUse each message context when provided.\nDo not add labels, explanations, markdown, or code fences.`,\n ].join(\"\\n\\n\")\n}\n\nfunction createUserPrompt(messages: TranslateMessage[], locale: string) {\n return JSON.stringify({\n targetLocale: locale,\n messages: messages.map((message) => ({\n id: message.id,\n text: message.text,\n context: message.meta.context,\n })),\n })\n}\n"],"mappings":";;AAKA,MAAM,wBAAwB;AAC9B,MAAM,qBAAqB;AAE3B,MAAM,2BAA2B,EAAE,OAAO,EACxC,cAAc,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,QAAQ,CAAC,EAC/C,CAAC;AAaF,SAAgB,kBAAkB,UAAoC,EAAE,EAAe;CACrF,OAAO,OAAO,UAAU,WAAW;EACjC,MAAM,SAAiC,EAAE;EACzC,MAAM,YAAY,QAAQ,aAAa;EAEvC,KAAK,IAAI,QAAQ,GAAG,QAAQ,SAAS,QAAQ,SAAS,WAAW;GAC/D,MAAM,QAAQ,SAAS,MAAM,OAAO,QAAQ,UAAU;GACtD,OAAO,OAAO,QAAQ,MAAM,eAAe,OAAO,QAAQ,QAAQ,CAAC;;EAGrE,OAAO;;;AAIX,eAAe,eAAe,UAA8B,QAAgB,SAAmC;CAC7G,MAAM,EAAE,iBAAiB,MAAM,gBAAgB,UAAU,QAAQ,QAAQ;CAEzE,OAAO,OAAO,YACZ,SAAS,KAAK,YAAY;EACxB,MAAM,aAAa,aAAa,QAAQ,KAAK,MAAM;EACnD,OAAO,CAAC,QAAQ,IAAI,cAAc,QAAQ,KAAK;GAC/C,CACH;;AAGH,eAAe,gBAAgB,UAA8B,QAAgB,SAAmC;CAC9G,MAAM,EAAE,cAAc,WAAW,MAAM,OAAO;CAC9C,MAAM,EAAE,WAAW,MAAM,aAAa;EACpC,OAAO,QAAQ,SAAS;EACxB,QAAQ,OAAO,OAAO,EAAE,QAAQ,0BAA0B,CAAC;EAC3D,QAAQ,mBAAmB,QAAQ,QAAQ,OAAO;EAClD,QAAQ,iBAAiB,UAAU,OAAO;EAC1C,GAAI,QAAQ,gBAAgB,KAAA,IAAY,EAAE,GAAG,EAAE,aAAa,QAAQ,aAAa;EAClF,CAAC;CAEF,OAAO;;AAGT,SAAS,mBAAmB,QAAgB,QAAiB;CAG3D,OAAO,CACL;EAHuB,QAAQ,MAAM,IAAI,8EAI1B;;;EAGjB,OAAO;;;;;4DAMN,CAAC,KAAK,OAAO;;AAGhB,SAAS,iBAAiB,UAA8B,QAAgB;CACtE,OAAO,KAAK,UAAU;EACpB,cAAc;EACd,UAAU,SAAS,KAAK,aAAa;GACnC,IAAI,QAAQ;GACZ,MAAM,QAAQ;GACd,SAAS,QAAQ,KAAK;GACvB,EAAE;EACJ,CAAC"}
1
+ {"version":3,"file":"ai.mjs","names":[],"sources":["../src/ai.ts"],"sourcesContent":["import type { generateText } from \"ai\"\n\nimport type { TranslateFn, TranslateMessage } from \"./types.js\"\n\nconst DEFAULT_GATEWAY_MODEL = \"openai/gpt-5.5\"\ntype AiModel = Parameters<typeof generateText>[0][\"model\"]\n\nexport interface CreateAiTranslateOptions {\n /** AI SDK model value. Defaults to a Vercel AI Gateway model string. */\n model?: AiModel\n /** Primary translation brief for product, tone, glossary, or domain instructions. */\n prompt?: string\n /** Optional temperature forwarded to the selected model provider. */\n temperature?: number\n}\n\nexport function createAiTranslate(options: CreateAiTranslateOptions = {}): TranslateFn {\n return async (messages, locale) => {\n const result: Record<string, string> = {}\n\n for (const message of messages) {\n result[message.id] = await translateMessage(message, locale, options)\n }\n\n return result\n }\n}\n\nasync function translateMessage(message: TranslateMessage, locale: string, options: CreateAiTranslateOptions) {\n const translated = (await translateWithAi(message, locale, options)).trim()\n\n return translated || message.text\n}\n\nasync function translateWithAi(message: TranslateMessage, locale: string, options: CreateAiTranslateOptions) {\n const { generateText } = await import(\"ai\")\n const { text } = await generateText({\n model: options.model ?? DEFAULT_GATEWAY_MODEL,\n system: createSystemPrompt(locale, options.prompt),\n prompt: createUserPrompt(message, locale),\n ...(options.temperature === undefined ? {} : { temperature: options.temperature }),\n })\n\n return text\n}\n\nfunction createSystemPrompt(locale: string, prompt?: string) {\n const translationBrief = prompt?.trim() || \"Translate the provided UI messages as concise, natural application UI copy.\"\n\n return [\n `## Translation Brief\n${translationBrief}\n\n## Target Locale\n${locale}\n\n## Output Contract\nReturn only the translated text for the provided source message.\nDo not include the message id, labels, explanations, markdown, code fences, or surrounding quotes.\nUse the message context when provided.`,\n ].join(\"\\n\\n\")\n}\n\nfunction createUserPrompt(message: TranslateMessage, locale: string) {\n return JSON.stringify({\n targetLocale: locale,\n message: {\n id: message.id,\n text: message.text,\n context: message.meta.context,\n },\n })\n}\n"],"mappings":";AAIA,MAAM,wBAAwB;AAY9B,SAAgB,kBAAkB,UAAoC,EAAE,EAAe;CACrF,OAAO,OAAO,UAAU,WAAW;EACjC,MAAM,SAAiC,EAAE;EAEzC,KAAK,MAAM,WAAW,UACpB,OAAO,QAAQ,MAAM,MAAM,iBAAiB,SAAS,QAAQ,QAAQ;EAGvE,OAAO;;;AAIX,eAAe,iBAAiB,SAA2B,QAAgB,SAAmC;CAG5G,QAFoB,MAAM,gBAAgB,SAAS,QAAQ,QAAQ,EAAE,MAEpD,IAAI,QAAQ;;AAG/B,eAAe,gBAAgB,SAA2B,QAAgB,SAAmC;CAC3G,MAAM,EAAE,iBAAiB,MAAM,OAAO;CACtC,MAAM,EAAE,SAAS,MAAM,aAAa;EAClC,OAAO,QAAQ,SAAS;EACxB,QAAQ,mBAAmB,QAAQ,QAAQ,OAAO;EAClD,QAAQ,iBAAiB,SAAS,OAAO;EACzC,GAAI,QAAQ,gBAAgB,KAAA,IAAY,EAAE,GAAG,EAAE,aAAa,QAAQ,aAAa;EAClF,CAAC;CAEF,OAAO;;AAGT,SAAS,mBAAmB,QAAgB,QAAiB;CAG3D,OAAO,CACL;EAHuB,QAAQ,MAAM,IAAI,8EAI1B;;;EAGjB,OAAO;;;;;wCAMN,CAAC,KAAK,OAAO;;AAGhB,SAAS,iBAAiB,SAA2B,QAAgB;CACnE,OAAO,KAAK,UAAU;EACpB,cAAc;EACd,SAAS;GACP,IAAI,QAAQ;GACZ,MAAM,QAAQ;GACd,SAAS,QAAQ,KAAK;GACvB;EACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-translation",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Vite plugin and runtime helpers for Better Translation.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -38,8 +38,7 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "ai": "6.0.193",
41
- "oxc-parser": "0.133.0",
42
- "zod": "4.3.6"
41
+ "oxc-parser": "0.133.0"
43
42
  },
44
43
  "devDependencies": {
45
44
  "@better-translation/tsconfig": "workspace:*",