@@ -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