Skip to content

Commit 6f6163d

Browse files
authored
fix: preserve original index parameter name in batch reconstruct transformer (#51)
- Update array-transformer-batch-reconstruct to use original index parameter name instead of hardcoded '__idx' - Add test case for map callback with index parameter to verify correct behavior
1 parent dfae141 commit 6f6163d

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

__tests__/e2e/atp-llm-callbacks.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,61 @@ return { results, count: results.length };
229229
console.log(`✅ Batch LLM with object preservation works!`);
230230
}, 180000);
231231

232+
test('should handle map callback with index parameter', async () => {
233+
let callCount = 0;
234+
235+
client.provideLLM({
236+
call: async (prompt: string) => {
237+
callCount++;
238+
return `Result for: ${prompt}`;
239+
},
240+
});
241+
242+
const code = `
243+
const items = ["a", "b", "c"];
244+
245+
const results = await Promise.all(
246+
items.map(async (item, idx) => {
247+
const llmResult = await atp.llm.call({ prompt: item });
248+
return { index: idx, value: item.toUpperCase(), llmResult };
249+
})
250+
);
251+
252+
return results;
253+
`;
254+
255+
console.log('[TEST] Starting map callback with index parameter test...');
256+
const result = await client.execute(code);
257+
258+
console.log('[TEST] Result:', JSON.stringify(result, null, 2));
259+
260+
expect(result.status).toBe('completed');
261+
expect(result.result).toHaveLength(3);
262+
263+
const results = result.result as any[];
264+
265+
// Verify index parameter is preserved correctly
266+
expect(results[0]).toEqual({
267+
index: 0,
268+
value: 'A',
269+
llmResult: 'Result for: a',
270+
});
271+
expect(results[1]).toEqual({
272+
index: 1,
273+
value: 'B',
274+
llmResult: 'Result for: b',
275+
});
276+
expect(results[2]).toEqual({
277+
index: 2,
278+
value: 'C',
279+
llmResult: 'Result for: c',
280+
});
281+
282+
expect(callCount).toBe(3);
283+
284+
console.log(`✅ Map callback with index parameter works correctly`);
285+
}, 180000);
286+
232287
test('should handle multiple LLM calls per map callback (batch_reconstruct)', async () => {
233288
let callCount = 0;
234289
const callLog: string[] = [];

packages/atp-compiler/src/transformer/array-transformer-batch-reconstruct.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export function transformToBatchWithReconstruction(
3939
}
4040

4141
const methodId = generateUniqueId(`${methodName}_batch_reconstruct`);
42-
const indexVar = '__idx';
42+
43+
const originalIndexParam = callback.params[1];
44+
const indexVar =
45+
originalIndexParam && t.isIdentifier(originalIndexParam) ? originalIndexParam.name : '__idx';
4346

4447
// Create batch declarations - one per LLM call (in order of appearance)
4548
// This ensures each call gets its own result array

0 commit comments

Comments
 (0)