Skip to content

Commit ec8b21a

Browse files
committed
fix: address review feedback (docs update for batch-size flag and whitespace cleanup)
1 parent d29af01 commit ec8b21a

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default new Command()
125125
)
126126
.option(
127127
"--batch-size <number>",
128-
"Number of translations to process in a single batch",
128+
"Number of translations to process in a single batch (not applicable when using lingo.dev provider)",
129129
(val: string) => parseInt(val),
130130
)
131131
.action(async (args) => {

packages/cli/src/cli/localizer/explicit.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,16 @@ function createAiSdkLocalizer(params: {
279279
try {
280280
result = parseModelResponse(response.text);
281281
} catch (e2) {
282+
const snippet =
283+
response.text.length > 500
284+
? `${response.text.slice(0, 500)}…`
285+
: response.text;
282286
console.error(
283-
`Failed to parse response from Lingo.dev. Response: ${response.text}`,
287+
`Failed to parse response from Lingo.dev. Response snippet: ${snippet}`,
288+
);
289+
throw new Error(
290+
`Failed to parse response from Lingo.dev: ${e2} (Snippet: ${snippet})`,
284291
);
285-
throw new Error(`Failed to parse response from Lingo.dev: ${e2}`);
286292
}
287293
let finalResult: Record<string, any> = {};
288294

@@ -321,7 +327,7 @@ function createAiSdkLocalizer(params: {
321327
*/
322328
function extractPayloadChunks(
323329
payload: Record<string, string>,
324-
batchSize: number = 25,
330+
batchSize?: number,
325331
): Record<string, string>[] {
326332
const idealBatchItemSize = 250;
327333
const result: Record<string, string>[] = [];
@@ -335,9 +341,11 @@ function extractPayloadChunks(
335341
currentChunkItemCount++;
336342

337343
const currentChunkSize = countWordsInRecord(currentChunk);
344+
const effectiveBatchSize =
345+
batchSize && batchSize > 0 ? batchSize : payloadEntries.length || 1;
338346
if (
339347
currentChunkSize > idealBatchItemSize ||
340-
currentChunkItemCount >= batchSize ||
348+
currentChunkItemCount >= effectiveBatchSize ||
341349
i === payloadEntries.length - 1
342350
) {
343351
result.push(currentChunk);

packages/cli/src/cli/processor/basic.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ describe("createBasicTranslator", () => {
5959
);
6060
});
6161

62+
it("should process >25 keys in a single batch by default (infinite batch size)", async () => {
63+
const inputData: Record<string, string> = {};
64+
for (let i = 0; i < 30; i++) {
65+
inputData[`key${i}`] = `value${i}`;
66+
}
67+
68+
const input = {
69+
sourceLocale: "en",
70+
targetLocale: "fr",
71+
processableData: inputData,
72+
};
73+
74+
(generateText as any).mockResolvedValue({
75+
text: JSON.stringify({ data: {} }),
76+
});
77+
78+
const onProgress = vi.fn();
79+
const translator = createBasicTranslator(mockModel, mockSystemPrompt);
80+
81+
await translator(input, onProgress);
82+
83+
// Should be 1 call, not 2 (which would happen if default was 25)
84+
expect(generateText).toHaveBeenCalledTimes(1);
85+
});
86+
6287
it("should respect batchSize parameter", async () => {
6388
const input = {
6489
sourceLocale: "en",

packages/cli/src/cli/processor/basic.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function createBasicTranslator(
9797
*/
9898
function extractPayloadChunks(
9999
payload: Record<string, string>,
100-
batchSize: number = 25,
100+
batchSize?: number,
101101
): Record<string, string>[] {
102102
const idealBatchItemSize = 250;
103103
const result: Record<string, string>[] = [];
@@ -111,9 +111,11 @@ function extractPayloadChunks(
111111
currentChunkItemCount++;
112112

113113
const currentChunkSize = countWordsInRecord(currentChunk);
114+
const effectiveBatchSize =
115+
batchSize && batchSize > 0 ? batchSize : payloadEntries.length || 1;
114116
if (
115117
currentChunkSize > idealBatchItemSize ||
116-
currentChunkItemCount >= batchSize ||
118+
currentChunkItemCount >= effectiveBatchSize ||
117119
i === payloadEntries.length - 1
118120
) {
119121
result.push(currentChunk);

0 commit comments

Comments
 (0)