diff --git a/workspace/tools/client.ts b/workspace/tools/client.ts new file mode 100644 index 0000000..f3e0cf3 --- /dev/null +++ b/workspace/tools/client.ts @@ -0,0 +1,56 @@ +/** + * Tool Proxy Client + * + * Communicates with Kleap tool proxy server to execute AI tools from sandbox scripts. + * Auto-generated - do not modify manually. + */ + +const TOOL_PROXY_URL = "http://localhost:3000/api/tool-proxy"; +const APP_ID = 1857; +const USER_ID = "4981edbc-e162-498f-a681-00190c16a8c7"; + +interface ToolCallResult { + success: boolean; + result?: any; + error?: string; + executionTime?: number; +} + +/** + * Call a Kleap AI tool from sandbox code + * @param toolName - Name of the tool to execute + * @param params - Parameters for the tool + * @returns Tool execution result + */ +export async function callTool(toolName: string, params: any): Promise { + try { + const response = await fetch(TOOL_PROXY_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + tool: toolName, + params, + appId: APP_ID, + userId: USER_ID, + }), + }); + + if (!response.ok) { + const error = await response.text(); + throw new Error(`Tool ${toolName} failed: ${error}`); + } + + const data: ToolCallResult = await response.json(); + + if (!data.success) { + throw new Error(data.error || `Tool ${toolName} failed`); + } + + return data.result; + } catch (error: any) { + console.error(`[TOOL-CLIENT] Error calling ${toolName}:`, error.message); + throw error; + } +}