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 +0 -2
- package/dist/ai.d.mts.map +1 -1
- package/dist/ai.mjs +15 -27
- package/dist/ai.mjs.map +1 -1
- package/package.json +2 -3
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":";;;;
|
|
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
|
|
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
|
|
18
|
-
|
|
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(
|
|
25
|
-
const { generateText
|
|
26
|
-
const {
|
|
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(
|
|
18
|
+
prompt: createUserPrompt(message, locale),
|
|
31
19
|
...options.temperature === void 0 ? {} : { temperature: options.temperature }
|
|
32
20
|
});
|
|
33
|
-
return
|
|
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
|
-
##
|
|
43
|
-
Return
|
|
44
|
-
|
|
45
|
-
|
|
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(
|
|
35
|
+
function createUserPrompt(message, locale) {
|
|
48
36
|
return JSON.stringify({
|
|
49
37
|
targetLocale: locale,
|
|
50
|
-
|
|
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\"\
|
|
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.
|
|
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:*",
|