Skip to content

Commit 69484fa

Browse files
cherkanovartclaude
andauthored
feat(cli): send metadata to vNext localization endpoint (#2020)
* feat(cli): send metadata to vNext localization endpoint Add sessionId, triggerType, and file path metadata to the vNext localization API requests for improved request logging and analytics. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: add changeset for metadata endpoint feature Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a40757b commit 69484fa

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
Send sessionId, triggerType, and file path metadata to the vNext localization endpoint

packages/cli/src/cli/cmd/run/execute.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ function createWorkerTask(args: {
261261
targetData: args.ctx.flags.force ? {} : targetData,
262262
processableData,
263263
hints: relevantHints,
264+
filePath: assignedTask.bucketPathPattern,
264265
},
265266
async (progress, _sourceChunk, processedChunk) => {
266267
// write translated chunks as they are received from LLM

packages/cli/src/cli/localizer/_types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type LocalizerData = {
77
targetLocale: string;
88
targetData: Record<string, any>;
99
hints: Record<string, string[]>;
10+
filePath?: string;
1011
};
1112

1213
export type LocalizerProgressFn = (
@@ -16,7 +17,11 @@ export type LocalizerProgressFn = (
1617
) => void;
1718

1819
export interface ILocalizer {
19-
id: "Lingo.dev" | "Lingo.dev vNext" | "pseudo" | NonNullable<I18nConfig["provider"]>["id"];
20+
id:
21+
| "Lingo.dev"
22+
| "Lingo.dev vNext"
23+
| "pseudo"
24+
| NonNullable<I18nConfig["provider"]>["id"];
2025
checkAuth: () => Promise<{
2126
authenticated: boolean;
2227
username?: string;

packages/cli/src/cli/localizer/lingodotdev-vnext.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import { createId } from "@paralleldrive/cuid2";
99
* Creates a custom engine for Lingo.dev vNext that sends requests to:
1010
* https://api.lingo.dev/process/<processId>/localize
1111
*/
12-
function createVNextEngine(config: { apiKey: string; apiUrl: string; processId: string }) {
12+
function createVNextEngine(config: {
13+
apiKey: string;
14+
apiUrl: string;
15+
processId: string;
16+
sessionId: string;
17+
triggerType: "cli" | "ci";
18+
}) {
1319
const endpoint = `${config.apiUrl}/process/${config.processId}/localize`;
1420

1521
return {
@@ -21,8 +27,8 @@ function createVNextEngine(config: { apiKey: string; apiUrl: string; processId:
2127
reference?: Record<string, Record<string, any>>;
2228
hints?: Record<string, string[]>;
2329
},
24-
workflowId: string,
2530
fast: boolean,
31+
filePath?: string,
2632
signal?: AbortSignal,
2733
): Promise<Record<string, string>> {
2834
const res = await fetch(endpoint, {
@@ -33,12 +39,15 @@ function createVNextEngine(config: { apiKey: string; apiUrl: string; processId:
3339
},
3440
body: JSON.stringify(
3541
{
36-
params: { workflowId, fast },
42+
params: { fast },
3743
sourceLocale,
3844
targetLocale,
3945
data: payload.data,
4046
reference: payload.reference,
4147
hints: payload.hints,
48+
sessionId: config.sessionId,
49+
triggerType: config.triggerType,
50+
metadata: filePath ? { filePath } : undefined,
4251
},
4352
null,
4453
2,
@@ -69,7 +78,9 @@ function createVNextEngine(config: { apiKey: string; apiUrl: string; processId:
6978
return jsonResponse.data || {};
7079
},
7180

72-
async whoami(signal?: AbortSignal): Promise<{ email: string; id: string } | null> {
81+
async whoami(
82+
signal?: AbortSignal,
83+
): Promise<{ email: string; id: string } | null> {
7384
// vNext uses a simple response for whoami
7485
return { email: "vnext-user", id: config.processId };
7586
},
@@ -82,6 +93,7 @@ function createVNextEngine(config: { apiKey: string; apiUrl: string; processId:
8293
fast?: boolean;
8394
reference?: Record<string, Record<string, any>>;
8495
hints?: Record<string, string[]>;
96+
filePath?: string;
8597
},
8698
progressCallback?: (
8799
progress: number,
@@ -93,7 +105,6 @@ function createVNextEngine(config: { apiKey: string; apiUrl: string; processId:
93105
const chunkedPayload = extractPayloadChunks(obj);
94106
const processedPayloadChunks: Record<string, string>[] = [];
95107

96-
const workflowId = createId();
97108
for (let i = 0; i < chunkedPayload.length; i++) {
98109
const chunk = chunkedPayload[i];
99110
const percentageCompleted = Math.round(
@@ -104,8 +115,8 @@ function createVNextEngine(config: { apiKey: string; apiUrl: string; processId:
104115
params.sourceLocale,
105116
params.targetLocale,
106117
{ data: chunk, reference: params.reference, hints: params.hints },
107-
workflowId,
108118
params.fast || false,
119+
params.filePath,
109120
signal,
110121
);
111122

@@ -158,10 +169,7 @@ function countWordsInRecord(
158169
payload: any | Record<string, any> | Array<any>,
159170
): number {
160171
if (Array.isArray(payload)) {
161-
return payload.reduce(
162-
(acc, item) => acc + countWordsInRecord(item),
163-
0,
164-
);
172+
return payload.reduce((acc, item) => acc + countWordsInRecord(item), 0);
165173
} else if (typeof payload === "object" && payload !== null) {
166174
return Object.values(payload).reduce(
167175
(acc: number, item) => acc + countWordsInRecord(item),
@@ -186,8 +194,8 @@ export default function createLingoDotDevVNextLocalizer(
186194
throw new Error(
187195
dedent`
188196
You're trying to use ${chalk.hex(colors.green)(
189-
"Lingo.dev vNext",
190-
)} provider, however, no API key is configured.
197+
"Lingo.dev vNext",
198+
)} provider, however, no API key is configured.
191199
192200
To fix this issue:
193201
1. Set ${chalk.dim("LINGO_API_KEY")} environment variable, or
@@ -199,10 +207,15 @@ export default function createLingoDotDevVNextLocalizer(
199207
// Use LINGO_API_URL from environment or default to api.lingo.dev
200208
const apiUrl = process.env.LINGO_API_URL || "https://api.lingo.dev";
201209

210+
const sessionId = createId();
211+
const triggerType = process.env.CI ? "ci" : "cli";
212+
202213
const engine = createVNextEngine({
203214
apiKey,
204215
apiUrl,
205216
processId,
217+
sessionId,
218+
triggerType,
206219
});
207220

208221
return {
@@ -236,6 +249,7 @@ export default function createLingoDotDevVNextLocalizer(
236249
[input.targetLocale]: input.targetData,
237250
},
238251
hints: input.hints,
252+
filePath: input.filePath,
239253
},
240254
onProgress,
241255
);

0 commit comments

Comments
 (0)