Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/utils/tools-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* This eliminates duplication between stdio.ts and processParamsGetTools.
*/

import type { ValidateFunction } from 'ajv';
import type { ApifyClient } from 'apify';

import log from '@apify/log';
Expand All @@ -12,7 +13,7 @@ import { callActor } from '../tools/actor.js';
import { getActorOutput } from '../tools/get-actor-output.js';
import { addTool } from '../tools/helpers.js';
import { getActorsAsTools, toolCategories, toolCategoriesEnabledByDefault } from '../tools/index.js';
import type { Input, ToolCategory, ToolEntry } from '../types.js';
import type { Input, InternalTool, InternalToolArgs, ToolCategory, ToolEntry } from '../types.js';
import { getExpectedToolsByCategories } from './tools.js';

// Lazily-computed cache of internal tools by name to avoid circular init issues.
Expand Down Expand Up @@ -139,5 +140,37 @@ export async function loadToolsFromInput(

// De-duplicate by tool name for safety
const seen = new Set<string>();
return result.filter((entry) => !seen.has(entry.tool.name) && seen.add(entry.tool.name));
const filtered = result.filter((entry) => !seen.has(entry.tool.name) && seen.add(entry.tool.name));

// TODO: rework this solition as it was quickly hacked together for hotfix
// Deep clone except ajvValidate and call functions

// holds the original functions of the tools
const toolFunctions = new Map<string, { ajvValidate?: ValidateFunction<unknown>; call?:(args: InternalToolArgs) => Promise<object> }>();
for (const entry of filtered) {
if (entry.type === 'internal') {
toolFunctions.set(entry.tool.name, { ajvValidate: entry.tool.ajvValidate, call: (entry.tool as InternalTool).call });
} else {
toolFunctions.set(entry.tool.name, { ajvValidate: entry.tool.ajvValidate });
}
}

const cloned = JSON.parse(JSON.stringify(filtered, (key, value) => {
if (key === 'ajvValidate' || key === 'call') return undefined;
return value;
})) as ToolEntry[];

// restore the original functions
for (const entry of cloned) {
const funcs = toolFunctions.get(entry.tool.name);
if (funcs) {
if (funcs.ajvValidate) {
entry.tool.ajvValidate = funcs.ajvValidate;
}
if (entry.type === 'internal' && funcs.call) {
(entry.tool as InternalTool).call = funcs.call;
}
}
}
return cloned;
}
2 changes: 1 addition & 1 deletion tests/integration/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export function createIntegrationTestsSuite(

expect(infoResult.content).toBeDefined();
const content = infoResult.content as { text: string }[];
expect(content.some((item) => item.text.includes('Input Schema'))).toBe(true);
expect(content.some((item) => item.text.includes('Input schema'))).toBe(true);

// Step 2: Call with proper input (should work)
const callResult = await client.callTool({
Expand Down