Skip to content

Commit 4a402d8

Browse files
committed
fix output tool, write test for that
1 parent 07035b0 commit 4a402d8

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

src/tools/get-actor-output.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,14 @@ You also can retrieve only specific fields from the output if needed. Use this t
102102
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' not found.` }] };
103103
}
104104

105+
let { items } = response;
105106
// Apply field selection if specified
106-
const processedItems = response.items.map((item) => getValuesByDotKeys(item, fieldsArray));
107+
if (fieldsArray.length > 0) {
108+
items = items.map((item) => getValuesByDotKeys(item, fieldsArray));
109+
}
107110

108111
// Clean empty properties
109-
const cleanedItems = processedItems
112+
const cleanedItems = items
110113
.map((item) => cleanEmptyProperties(item))
111114
.filter((item) => item !== undefined);
112115

src/utils/actor.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,24 @@ export function ensureOutputWithinCharLimit(items: DatasetItem[], importantField
3939
return items;
4040
}
4141

42+
/**
43+
* Items used for the final fallback - removing items until within the limit.
44+
* If important fields are defined, use only those fields for that fallback step.
45+
*/
46+
let sourceItems = items;
4247
// Try only the important fields
43-
const importantItems = items.map((item) => getValuesByDotKeys(item, importantFields));
44-
const importantItemsString = JSON.stringify(importantItems);
45-
if (importantItemsString.length <= charLimit) {
46-
return importantItems;
48+
if (importantFields.length > 0) {
49+
const importantItems = items.map((item) => getValuesByDotKeys(item, importantFields));
50+
const importantItemsString = JSON.stringify(importantItems);
51+
if (importantItemsString.length <= charLimit) {
52+
return importantItems;
53+
}
54+
sourceItems = importantItems;
4755
}
4856

4957
// Start removing items until within the limit
5058
const result: DatasetItem[] = [];
51-
for (const item of importantItems) {
59+
for (const item of sourceItems) {
5260
if (JSON.stringify(result.concat(item)).length > charLimit) {
5361
break;
5462
}

src/utils/generic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* const value = getValuesByDotKeys(obj, ['a.b.c', 'a.b.d', 'nested']);
99
* value; // { 'a.b.c': 42, 'a.b.d': undefined, 'nested': { d: 100 } }
1010
*/
11-
export function getValuesByDotKeys<T extends object>(obj: T, keys: string[]): Record<string, unknown> {
11+
export function getValuesByDotKeys(obj: Record<string, unknown>, keys: string[]): Record<string, unknown> {
1212
const result: Record<string, unknown> = {};
1313
for (const key of keys) {
1414
const path = key.split('.');

tests/integration/suite.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,5 +870,45 @@ export function createIntegrationTestsSuite(
870870

871871
await client.close();
872872
});
873+
874+
it('should call apify/python-example and retrieve the full dataset using get-actor-output tool', async () => {
875+
client = await createClientFn({ actors: ['apify/python-example'] });
876+
const selectedToolName = actorNameToToolName('apify/python-example');
877+
const input = { first_number: 5, second_number: 7 };
878+
879+
const result = await client.callTool({
880+
name: selectedToolName,
881+
arguments: input,
882+
});
883+
884+
expect(result.content).toBeDefined();
885+
const content = result.content as { text: string; type: string }[];
886+
expect(content.length).toBe(2); // Call step returns text summary with embedded schema
887+
888+
// First content: text summary
889+
const runText = content[0].text;
890+
891+
// Extract datasetId from the text
892+
const runIdMatch = runText.match(/Run ID: ([^\n]+)\n Dataset ID: ([^\n]+)/);
893+
expect(runIdMatch).toBeTruthy();
894+
const datasetId = runIdMatch![2];
895+
896+
// Retrieve full dataset using get-actor-output tool
897+
const outputResult = await client.callTool({
898+
name: HelperTools.ACTOR_OUTPUT_GET,
899+
arguments: {
900+
datasetId,
901+
},
902+
});
903+
904+
expect(outputResult.content).toBeDefined();
905+
const outputContent = outputResult.content as { text: string; type: string }[];
906+
const output = JSON.parse(outputContent[0].text);
907+
expect(Array.isArray(output)).toBe(true);
908+
expect(output.length).toBe(1);
909+
expect(output[0]).toHaveProperty('first_number', input.first_number);
910+
expect(output[0]).toHaveProperty('second_number', input.second_number);
911+
expect(output[0]).toHaveProperty('sum', input.first_number + input.second_number);
912+
});
873913
});
874914
}

0 commit comments

Comments
 (0)