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
34 changes: 26 additions & 8 deletions src/tests/tool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,41 @@ describe('StackOneTool', () => {
const tool = createMockTool();
const aiSdkTool = tool.toAISDK();

// Mock the ToolExecutionOptions
const mockOptions = {
toolCallId: 'test-tool-call-id',
messages: [],
};

// Execute the AI SDK tool
if (!aiSdkTool.test_tool.execute) {
throw new Error('test_tool.execute is undefined');
}
const result = await aiSdkTool.test_tool.execute({ id: '123' }, mockOptions);

const result = await aiSdkTool.test_tool.execute(
{ id: '123' },
{ toolCallId: 'test-tool-call-id', messages: [] }
);
expect(result).toEqual({ id: '123', name: 'Test' });

// Restore the original fetch
fetchMock.restore();
});

it('should return error message as string when AI SDK tool execution fails', async () => {
const tool = createMockTool();

// Mock the execute method to throw an error
const mockError = new Error('Test execution error');
spyOn(tool, 'execute').mockImplementation(() => {
throw mockError;
});

const aiSdkTool = tool.toAISDK();

if (!aiSdkTool.test_tool.execute) {
throw new Error('test_tool.execute is undefined');
}

const result = await aiSdkTool.test_tool.execute(
{ id: '123' },
{ toolCallId: 'test-tool-call-id', messages: [] }
);
expect(result).toBe('Error executing tool: Test execution error');
});
});

describe('Tools', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ export class BaseTool {
description: this.description,
...(options.executable && {
execute: async (args: Record<string, unknown>) => {
return await this.execute(args as JsonDict);
try {
return await this.execute(args as JsonDict);
} catch (error) {
return `Error executing tool: ${error instanceof Error ? error.message : String(error)}`;
}
},
}),
},
Expand Down