Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Add tests to all the toolsets

haxzie1b951eede8bc
diff --git a/packages/toolsets/cloudflare/test/cloudflare.test.ts b/packages/toolsets/cloudflare/test/cloudflare.test.ts
index 99693dbe2..2c7bac3be 100644
--- a/packages/toolsets/cloudflare/test/cloudflare.test.ts
+++ b/packages/toolsets/cloudflare/test/cloudflare.test.ts
@@ -1,7 +1,313 @@
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { CloudflareToolset } from '../src';
+import { Tool } from '@composio/core';
-describe('Vercel', () => {
- it('should be a test', () => {
- expect(true).toBe(true);
+// Instead of importing and mocking the actual Cloudflare types, let's define our own for testing
+interface AiTextGenerationFunction {
+ name: string;
+ description: string;
+ parameters: any;
+}
+
+interface MockedCloudflareToolInput {
+ type: 'function';
+ function: {
+ name: string;
+ description?: string;
+ parameters?: any;
+ };
+}
+
+// Mock the types we need from Cloudflare
+vi.mock('@cloudflare/workers-types', () => {
+ return {};
+});
+
+describe('CloudflareToolset', () => {
+ let toolset: CloudflareToolset;
+ let mockTool: Tool;
+ let mockExecuteToolFn: any;
+
+ beforeEach(() => {
+ toolset = new CloudflareToolset();
+
+ // Mock the global execute tool function
+ mockExecuteToolFn = vi.fn().mockResolvedValue({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ });
+ toolset._setExecuteToolFn(mockExecuteToolFn);
+
+ // Create a mock Composio tool
+ mockTool = {
+ slug: 'test-tool',
+ name: 'Test Tool',
+ description: 'A tool for testing',
+ inputParameters: {
+ type: 'object',
+ properties: {
+ input: {
+ type: 'string',
+ description: 'Test input',
+ },
+ },
+ required: ['input'],
+ },
+ tags: [],
+ };
+
+ // Reset mocks before each test
+ vi.clearAllMocks();
+ });
+
+ describe('name property', () => {
+ it('should have the correct name', () => {
+ expect(toolset.name).toBe('cloudflare');
+ });
+ });
+
+ describe('_isAgentic property', () => {
+ it('should be non-agentic', () => {
+ expect(toolset._isAgentic).toBe(false);
+ });
+ });
+
+ describe('wrapTool', () => {
+ it('should wrap a tool in Cloudflare AiTextGenerationToolInput format', () => {
+ const wrapped = toolset.wrapTool(mockTool) as MockedCloudflareToolInput;
+
+ expect(wrapped).toEqual({
+ type: 'function',
+ function: {
+ name: mockTool.slug,
+ description: mockTool.description,
+ parameters: mockTool.inputParameters,
+ },
+ });
+ });
+
+ it('should handle tools without input parameters', () => {
+ const toolWithoutParams: Tool = {
+ ...mockTool,
+ inputParameters: undefined,
+ };
+
+ // This would normally throw an error due to the type casting in the function,
+ // but we're testing the behavior, not the types
+ const wrapped = toolset.wrapTool(toolWithoutParams) as MockedCloudflareToolInput;
+
+ expect(wrapped).toEqual({
+ type: 'function',
+ function: {
+ name: toolWithoutParams.slug,
+ description: toolWithoutParams.description,
+ parameters: undefined,
+ },
+ });
+ });
+
+ it('should handle tools without description', () => {
+ const toolWithoutDescription: Tool = {
+ ...mockTool,
+ description: undefined,
+ };
+
+ const wrapped = toolset.wrapTool(toolWithoutDescription) as MockedCloudflareToolInput;
+
+ expect(wrapped).toEqual({
+ type: 'function',
+ function: {
+ name: toolWithoutDescription.slug,
+ description: undefined,
+ parameters: toolWithoutDescription.inputParameters,
+ },
+ });
+ });
+ });
+
+ describe('wrapTools', () => {
+ it('should wrap multiple tools into a dictionary', () => {
+ const anotherTool: Tool = {
+ ...mockTool,
+ slug: 'another-tool',
+ name: 'Another Tool',
+ };
+ const tools = [mockTool, anotherTool];
+
+ const wrapped = toolset.wrapTools(tools);
+
+ // Verify the result has the expected properties
+ expect(Object.keys(wrapped)).toHaveLength(2);
+ expect(wrapped['test-tool']).toBeDefined();
+ expect(wrapped['another-tool']).toBeDefined();
+
+ // Verify each tool was properly wrapped
+ expect(wrapped['test-tool']).toEqual({
+ type: 'function',
+ function: {
+ name: mockTool.slug,
+ description: mockTool.description,
+ parameters: mockTool.inputParameters,
+ },
+ });
+
+ expect(wrapped['another-tool']).toEqual({
+ type: 'function',
+ function: {
+ name: anotherTool.slug,
+ description: anotherTool.description,
+ parameters: anotherTool.inputParameters,
+ },
+ });
+ });
+
+ it('should return an empty object for empty tools array', () => {
+ const wrapped = toolset.wrapTools([]);
+ expect(wrapped).toEqual({});
+ });
+ });
+
+ describe('executeToolCall', () => {
+ it('should execute a tool call and return the result as string', async () => {
+ const userId = 'test-user';
+ const toolCall = {
+ name: 'test-tool',
+ arguments: { input: 'test-value' },
+ };
+
+ const result = await toolset.executeToolCall(userId, toolCall, {});
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
+ 'test-tool',
+ {
+ arguments: { input: 'test-value' },
+ userId: 'test-user',
+ connectedAccountId: undefined,
+ customAuthParams: undefined,
+ },
+ undefined
+ );
+ expect(result).toBe(
+ JSON.stringify({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ })
+ );
+ });
+
+ it('should handle string arguments by parsing them', async () => {
+ const userId = 'test-user';
+ const toolCall = {
+ name: 'test-tool',
+ arguments: JSON.stringify({ input: 'test-value' }),
+ };
+
+ const result = await toolset.executeToolCall(userId, toolCall, {});
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
+ 'test-tool',
+ {
+ arguments: { input: 'test-value' },
+ userId: 'test-user',
+ connectedAccountId: undefined,
+ customAuthParams: undefined,
+ },
+ undefined
+ );
+ expect(result).toBe(
+ JSON.stringify({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ })
+ );
+ });
+
+ it('should pass options to executeTool', async () => {
+ const userId = 'test-user';
+ const toolCall = {
+ name: 'test-tool',
+ arguments: { input: 'test-value' },
+ };
+
+ const options = {
+ connectedAccountId: 'conn-123',
+ customAuthParams: {
+ parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }],
+ },
+ };
+
+ const modifiers = {
+ beforeToolExecute: vi.fn(params => params),
+ afterToolExecute: vi.fn(response => response),
+ };
+
+ await toolset.executeToolCall(userId, toolCall, options, modifiers);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
+ 'test-tool',
+ {
+ arguments: { input: 'test-value' },
+ userId: 'test-user',
+ connectedAccountId: 'conn-123',
+ customAuthParams: options.customAuthParams,
+ },
+ modifiers
+ );
+ });
+ });
+
+ describe('executeTool', () => {
+ it('should execute a tool using the global execute function', async () => {
+ const toolSlug = 'test-tool';
+ const toolParams = {
+ userId: 'test-user',
+ arguments: { input: 'test-value' },
+ };
+
+ const result = await toolset.executeTool(toolSlug, toolParams);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, undefined);
+ expect(result).toEqual({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ });
+ });
+
+ it('should pass modifiers to the global execute function', async () => {
+ const toolSlug = 'test-tool';
+ const toolParams = {
+ userId: 'test-user',
+ arguments: { input: 'test-value' },
+ };
+
+ const modifiers = {
+ beforeToolExecute: vi.fn(params => params),
+ afterToolExecute: vi.fn(response => response),
+ };
+
+ await toolset.executeTool(toolSlug, toolParams, modifiers);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, modifiers);
+ });
+ });
+
+ describe('integration with Cloudflare AI', () => {
+ it('should produce tools compatible with Cloudflare AI', () => {
+ const wrapped = toolset.wrapTool(mockTool) as MockedCloudflareToolInput;
+
+ // Verify the wrapped tool has the expected structure
+ expect(wrapped).toHaveProperty('type', 'function');
+ expect(wrapped).toHaveProperty('function');
+ expect(wrapped.function).toHaveProperty('name', mockTool.slug);
+ expect(wrapped.function).toHaveProperty('description', mockTool.description);
+ expect(wrapped.function).toHaveProperty('parameters', mockTool.inputParameters);
+
+ // The structure should match what Cloudflare AI expects
+ expect(wrapped.type).toBe('function');
+ });
});
});
diff --git a/packages/toolsets/langchain/test/langchain.test.ts b/packages/toolsets/langchain/test/langchain.test.ts
index 99693dbe2..b1e244821 100644
--- a/packages/toolsets/langchain/test/langchain.test.ts
+++ b/packages/toolsets/langchain/test/langchain.test.ts
@@ -1,7 +1,239 @@
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { LangchainToolset } from '../src';
+import { Tool } from '@composio/core';
+import { DynamicStructuredTool } from '@langchain/core/tools';
-describe('Vercel', () => {
- it('should be a test', () => {
- expect(true).toBe(true);
+// Define an interface for our mocked LangChain tool
+interface MockedLangChainTool extends DynamicStructuredTool {
+ _isMockedLangChainTool: boolean;
+}
+
+// Mock the @langchain/core/tools module
+vi.mock('@langchain/core/tools', () => {
+ return {
+ DynamicStructuredTool: vi.fn().mockImplementation(config => {
+ return {
+ name: config.name,
+ description: config.description,
+ schema: config.schema,
+ func: config.func,
+ _isMockedLangChainTool: true,
+ } as MockedLangChainTool;
+ }),
+ };
+});
+
+// Mock the jsonSchemaToModel function from @composio/core
+vi.mock('@composio/core', async () => {
+ const actual = await vi.importActual('@composio/core');
+ return {
+ ...(actual as object),
+ jsonSchemaToModel: vi.fn().mockImplementation(schema => {
+ return { type: 'mock-schema', originalSchema: schema };
+ }),
+ };
+});
+
+describe('LangchainToolset', () => {
+ let toolset: LangchainToolset;
+ let mockTool: Tool;
+ let mockExecuteToolFn: any;
+
+ beforeEach(() => {
+ toolset = new LangchainToolset();
+
+ // Mock the global execute tool function
+ mockExecuteToolFn = vi.fn().mockResolvedValue({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ });
+ toolset._setExecuteToolFn(mockExecuteToolFn);
+
+ // Create a mock Composio tool
+ mockTool = {
+ slug: 'test-tool',
+ name: 'Test Tool',
+ description: 'A tool for testing',
+ inputParameters: {
+ type: 'object',
+ properties: {
+ input: {
+ type: 'string',
+ description: 'Test input',
+ },
+ },
+ required: ['input'],
+ },
+ toolkit: {
+ slug: 'test-toolkit',
+ name: 'Test Toolkit',
+ },
+ tags: [],
+ };
+
+ // Reset mocks before each test
+ vi.clearAllMocks();
+ });
+
+ describe('name property', () => {
+ it('should have the correct name', () => {
+ expect(toolset.name).toBe('langchain');
+ });
+ });
+
+ describe('_isAgentic property', () => {
+ it('should be agentic', () => {
+ expect(toolset._isAgentic).toBe(true);
+ });
+ });
+
+ describe('wrapTool', () => {
+ it('should wrap a tool in LangChain DynamicStructuredTool format', () => {
+ const wrapped = toolset.wrapTool(mockTool, mockExecuteToolFn) as MockedLangChainTool;
+
+ expect(DynamicStructuredTool).toHaveBeenCalledWith({
+ name: mockTool.slug,
+ description: mockTool.description,
+ schema: expect.objectContaining({
+ type: 'mock-schema',
+ originalSchema: mockTool.inputParameters,
+ }),
+ func: expect.any(Function),
+ });
+
+ expect(wrapped._isMockedLangChainTool).toBe(true);
+ });
+
+ it('should throw an error if toolkit name is not defined', () => {
+ const toolWithoutToolkit: Tool = {
+ ...mockTool,
+ toolkit: undefined,
+ };
+
+ expect(() => {
+ toolset.wrapTool(toolWithoutToolkit, mockExecuteToolFn);
+ }).toThrow('App name is not defined');
+ });
+
+ it('should throw an error if inputParameters are not defined', () => {
+ const toolWithoutParams: Tool = {
+ ...mockTool,
+ inputParameters: undefined,
+ };
+
+ expect(() => {
+ toolset.wrapTool(toolWithoutParams, mockExecuteToolFn);
+ }).toThrow('Tool input parameters are not defined');
+ });
+
+ it('should create a function that executes the tool with the right parameters', async () => {
+ const wrapped = toolset.wrapTool(mockTool, mockExecuteToolFn);
+
+ // Extract the function from the constructor call
+ const executeFunction = (DynamicStructuredTool as any).mock.calls[0][0].func;
+
+ // Test the execute function
+ const params = { input: 'test-value' };
+ const result = await executeFunction(params);
+
+ // Check that executeToolFn was called with the right parameters
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params);
+
+ // Check that the result was stringified
+ expect(result).toBe(
+ JSON.stringify({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ })
+ );
+ });
+ });
+
+ describe('wrapTools', () => {
+ it('should wrap multiple tools', () => {
+ const anotherTool: Tool = {
+ ...mockTool,
+ slug: 'another-tool',
+ name: 'Another Tool',
+ };
+ const tools = [mockTool, anotherTool];
+
+ const wrapped = toolset.wrapTools(tools, mockExecuteToolFn);
+
+ // Verify the result is an array of the right length
+ expect(wrapped).toHaveLength(2);
+
+ // Verify the DynamicStructuredTool constructor was called with the right parameters for each tool
+ expect(DynamicStructuredTool).toHaveBeenCalledTimes(2);
+ expect(DynamicStructuredTool).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name: mockTool.slug,
+ })
+ );
+ expect(DynamicStructuredTool).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name: anotherTool.slug,
+ })
+ );
+ });
+
+ it('should return an empty array for empty tools array', () => {
+ const wrapped = toolset.wrapTools([], mockExecuteToolFn);
+ expect(wrapped).toEqual([]);
+ expect(DynamicStructuredTool).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('executeTool', () => {
+ it('should execute a tool using the global execute function', async () => {
+ const toolSlug = 'test-tool';
+ const toolParams = {
+ userId: 'test-user',
+ arguments: { input: 'test-value' },
+ };
+
+ const result = await toolset.executeTool(toolSlug, toolParams);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, undefined);
+ expect(result).toEqual({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ });
+ });
+
+ it('should pass modifiers to the global execute function', async () => {
+ const toolSlug = 'test-tool';
+ const toolParams = {
+ userId: 'test-user',
+ arguments: { input: 'test-value' },
+ };
+
+ const modifiers = {
+ beforeToolExecute: vi.fn(params => params),
+ afterToolExecute: vi.fn(response => response),
+ };
+
+ await toolset.executeTool(toolSlug, toolParams, modifiers);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, modifiers);
+ });
+ });
+
+ describe('integration with LangChain', () => {
+ it('should produce tools compatible with LangChain', () => {
+ const wrapped = toolset.wrapTool(mockTool, mockExecuteToolFn) as MockedLangChainTool;
+
+ // Verify the wrapped tool has the expected structure
+ expect(wrapped).toHaveProperty('name');
+ expect(wrapped).toHaveProperty('description');
+ expect(wrapped).toHaveProperty('schema');
+ expect(wrapped).toHaveProperty('func');
+
+ // The func property should be a function
+ expect(typeof wrapped.func).toBe('function');
+ });
});
});
diff --git a/packages/toolsets/openai/test/openai.test.ts b/packages/toolsets/openai/test/openai.test.ts
index 99693dbe2..71394c740 100644
--- a/packages/toolsets/openai/test/openai.test.ts
+++ b/packages/toolsets/openai/test/openai.test.ts
@@ -1,7 +1,504 @@
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { OpenAIToolset } from '../src';
+import { Tool } from '@composio/core';
+import { OpenAI } from 'openai';
-describe('Vercel', () => {
- it('should be a test', () => {
- expect(true).toBe(true);
+// Mock the openai modules
+vi.mock('openai', () => {
+ return {
+ OpenAI: vi.fn().mockImplementation(() => ({
+ beta: {
+ threads: {
+ runs: {
+ retrieve: vi.fn().mockImplementation((threadId, runId) => {
+ return { id: runId, status: 'completed' };
+ }),
+ submitToolOutputs: vi.fn().mockImplementation((threadId, runId, options) => {
+ return { id: runId, status: 'completed' };
+ }),
+ },
+ },
+ },
+ })),
+ };
+});
+
+vi.mock('openai/streaming', () => {
+ return {
+ Stream: vi.fn(),
+ };
+});
+
+// Define interfaces for our OpenAI mocked types
+interface MockedOpenAIChatCompletionTool {
+ type: 'function';
+ function: {
+ name: string;
+ description?: string;
+ parameters?: any;
+ };
+}
+
+describe('OpenAIToolset', () => {
+ let toolset: OpenAIToolset;
+ let mockTool: Tool;
+ let mockExecuteToolFn: any;
+
+ beforeEach(() => {
+ toolset = new OpenAIToolset();
+
+ // Mock the global execute tool function
+ mockExecuteToolFn = vi.fn().mockResolvedValue({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ });
+ toolset._setExecuteToolFn(mockExecuteToolFn);
+
+ // Create a mock Composio tool
+ mockTool = {
+ slug: 'test-tool',
+ name: 'Test Tool',
+ description: 'A tool for testing',
+ inputParameters: {
+ type: 'object',
+ properties: {
+ input: {
+ type: 'string',
+ description: 'Test input',
+ },
+ },
+ required: ['input'],
+ },
+ tags: [],
+ };
+
+ // Reset mocks before each test
+ vi.clearAllMocks();
+ });
+
+ describe('name property', () => {
+ it('should have the correct name', () => {
+ expect(toolset.name).toBe('openai');
+ });
+ });
+
+ describe('_isAgentic property', () => {
+ it('should be non-agentic', () => {
+ expect(toolset._isAgentic).toBe(false);
+ });
+ });
+
+ describe('wrapTool', () => {
+ it('should wrap a tool in OpenAI function format', () => {
+ const wrapped = toolset.wrapTool(mockTool) as MockedOpenAIChatCompletionTool;
+
+ expect(wrapped).toEqual({
+ type: 'function',
+ function: {
+ name: mockTool.slug,
+ description: mockTool.description,
+ parameters: mockTool.inputParameters,
+ },
+ });
+ });
+
+ it('should handle tools without input parameters', () => {
+ const toolWithoutParams: Tool = {
+ ...mockTool,
+ inputParameters: undefined,
+ };
+
+ const wrapped = toolset.wrapTool(toolWithoutParams) as MockedOpenAIChatCompletionTool;
+
+ expect(wrapped).toEqual({
+ type: 'function',
+ function: {
+ name: toolWithoutParams.slug,
+ description: toolWithoutParams.description,
+ parameters: undefined,
+ },
+ });
+ });
+ });
+
+ describe('wrapTools', () => {
+ it('should wrap multiple tools', () => {
+ const anotherTool: Tool = {
+ ...mockTool,
+ slug: 'another-tool',
+ name: 'Another Tool',
+ };
+ const tools = [mockTool, anotherTool];
+
+ const wrapped = toolset.wrapTools(tools);
+
+ // Verify the result is an array of the right length
+ expect(wrapped).toHaveLength(2);
+
+ // Verify each tool was properly wrapped
+ expect(wrapped[0]).toEqual({
+ type: 'function',
+ function: {
+ name: mockTool.slug,
+ description: mockTool.description,
+ parameters: mockTool.inputParameters,
+ },
+ });
+
+ expect(wrapped[1]).toEqual({
+ type: 'function',
+ function: {
+ name: anotherTool.slug,
+ description: anotherTool.description,
+ parameters: anotherTool.inputParameters,
+ },
+ });
+ });
+
+ it('should return an empty array for empty tools array', () => {
+ const wrapped = toolset.wrapTools([]);
+ expect(wrapped).toEqual([]);
+ });
+ });
+
+ describe('executeToolCall', () => {
+ it('should execute a tool call and return the result as string', async () => {
+ const userId = 'test-user';
+ const toolCall = {
+ id: 'call-123',
+ type: 'function',
+ function: {
+ name: 'test-tool',
+ arguments: JSON.stringify({ input: 'test-value' }),
+ },
+ } as OpenAI.ChatCompletionMessageToolCall;
+
+ const result = await toolset.executeToolCall(userId, toolCall);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
+ 'test-tool',
+ {
+ arguments: { input: 'test-value' },
+ userId: 'test-user',
+ connectedAccountId: undefined,
+ customAuthParams: undefined,
+ },
+ undefined
+ );
+ expect(result).toBe(
+ JSON.stringify({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ })
+ );
+ });
+
+ it('should pass options to executeTool', async () => {
+ const userId = 'test-user';
+ const toolCall = {
+ id: 'call-123',
+ type: 'function',
+ function: {
+ name: 'test-tool',
+ arguments: JSON.stringify({ input: 'test-value' }),
+ },
+ } as OpenAI.ChatCompletionMessageToolCall;
+
+ const options = {
+ connectedAccountId: 'conn-123',
+ customAuthParams: {
+ parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }],
+ },
+ };
+
+ const modifiers = {
+ beforeToolExecute: vi.fn(params => params),
+ afterToolExecute: vi.fn(response => response),
+ };
+
+ await toolset.executeToolCall(userId, toolCall, options, modifiers);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
+ 'test-tool',
+ {
+ arguments: { input: 'test-value' },
+ userId: 'test-user',
+ connectedAccountId: 'conn-123',
+ customAuthParams: options.customAuthParams,
+ },
+ modifiers
+ );
+ });
+ });
+
+ describe('handleToolCall', () => {
+ it('should handle tool calls from chat completion', async () => {
+ const userId = 'test-user';
+ const chatCompletion = {
+ id: 'chat-123',
+ model: 'gpt-4',
+ created: 123456789,
+ object: 'chat.completion',
+ choices: [
+ {
+ message: {
+ role: 'assistant',
+ content: null,
+ tool_calls: [
+ {
+ id: 'call-123',
+ type: 'function',
+ function: {
+ name: 'test-tool',
+ arguments: JSON.stringify({ input: 'test-value' }),
+ },
+ } as const,
+ ],
+ },
+ index: 0,
+ finish_reason: 'tool_calls' as const,
+ },
+ ],
+ usage: {
+ prompt_tokens: 10,
+ completion_tokens: 20,
+ total_tokens: 30,
+ },
+ } as OpenAI.ChatCompletion;
+
+ const executeToolCallSpy = vi.spyOn(toolset, 'executeToolCall');
+ executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' }));
+
+ const results = await toolset.handleToolCall(userId, chatCompletion);
+
+ expect(executeToolCallSpy).toHaveBeenCalledWith(
+ userId,
+ chatCompletion.choices[0].message.tool_calls![0],
+ undefined,
+ undefined
+ );
+ expect(results).toEqual([JSON.stringify({ result: 'success' })]);
+ });
+
+ it('should handle multiple tool calls', async () => {
+ const userId = 'test-user';
+ const chatCompletion = {
+ id: 'chat-123',
+ model: 'gpt-4',
+ created: 123456789,
+ object: 'chat.completion',
+ choices: [
+ {
+ message: {
+ role: 'assistant',
+ content: null,
+ tool_calls: [
+ {
+ id: 'call-123',
+ type: 'function',
+ function: {
+ name: 'test-tool',
+ arguments: JSON.stringify({ input: 'test-value-1' }),
+ },
+ } as const,
+ ],
+ },
+ index: 0,
+ finish_reason: 'tool_calls' as const,
+ },
+ {
+ message: {
+ role: 'assistant',
+ content: null,
+ tool_calls: [
+ {
+ id: 'call-456',
+ type: 'function',
+ function: {
+ name: 'another-tool',
+ arguments: JSON.stringify({ input: 'test-value-2' }),
+ },
+ } as const,
+ ],
+ },
+ index: 1,
+ finish_reason: 'tool_calls' as const,
+ },
+ ],
+ usage: {
+ prompt_tokens: 10,
+ completion_tokens: 20,
+ total_tokens: 30,
+ },
+ } as OpenAI.ChatCompletion;
+
+ const executeToolCallSpy = vi.spyOn(toolset, 'executeToolCall');
+ executeToolCallSpy
+ .mockResolvedValueOnce(JSON.stringify({ result: 'success-1' }))
+ .mockResolvedValueOnce(JSON.stringify({ result: 'success-2' }));
+
+ const results = await toolset.handleToolCall(userId, chatCompletion);
+
+ expect(executeToolCallSpy).toHaveBeenCalledTimes(2);
+ expect(executeToolCallSpy).toHaveBeenNthCalledWith(
+ 1,
+ userId,
+ chatCompletion.choices[0].message.tool_calls![0],
+ undefined,
+ undefined
+ );
+ expect(executeToolCallSpy).toHaveBeenNthCalledWith(
+ 2,
+ userId,
+ chatCompletion.choices[1].message.tool_calls![0],
+ undefined,
+ undefined
+ );
+ expect(results).toEqual([
+ JSON.stringify({ result: 'success-1' }),
+ JSON.stringify({ result: 'success-2' }),
+ ]);
+ });
+ });
+
+ describe('handleAssistantMessage', () => {
+ it('should process tool calls from an assistant run', async () => {
+ const userId = 'test-user';
+ const run = {
+ id: 'run-123',
+ required_action: {
+ submit_tool_outputs: {
+ tool_calls: [
+ {
+ id: 'tool-call-123',
+ type: 'function',
+ function: {
+ name: 'test-tool',
+ arguments: JSON.stringify({ input: 'test-value' }),
+ },
+ },
+ ],
+ },
+ },
+ } as unknown as OpenAI.Beta.Threads.Run;
+
+ const executeToolCallSpy = vi.spyOn(toolset, 'executeToolCall');
+ executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' }));
+
+ const toolOutputs = await toolset.handleAssistantMessage(userId, run);
+
+ expect(executeToolCallSpy).toHaveBeenCalledWith(
+ userId,
+ run.required_action?.submit_tool_outputs
+ ?.tool_calls[0] as OpenAI.ChatCompletionMessageToolCall,
+ undefined,
+ undefined
+ );
+ expect(toolOutputs).toEqual([
+ {
+ tool_call_id: 'tool-call-123',
+ output: JSON.stringify(JSON.stringify({ result: 'success' })),
+ },
+ ]);
+ });
+
+ it('should handle runs without tool calls', async () => {
+ const userId = 'test-user';
+ const run = {
+ id: 'run-123',
+ } as OpenAI.Beta.Threads.Run;
+
+ const executeToolCallSpy = vi.spyOn(toolset, 'executeToolCall');
+
+ const toolOutputs = await toolset.handleAssistantMessage(userId, run);
+
+ expect(executeToolCallSpy).not.toHaveBeenCalled();
+ expect(toolOutputs).toEqual([]);
+ });
+ });
+
+ describe('executeTool', () => {
+ it('should execute a tool using the global execute function', async () => {
+ const toolSlug = 'test-tool';
+ const toolParams = {
+ userId: 'test-user',
+ arguments: { input: 'test-value' },
+ };
+
+ const result = await toolset.executeTool(toolSlug, toolParams);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, undefined);
+ expect(result).toEqual({
+ data: { result: 'success' },
+ error: null,
+ successful: true,
+ });
+ });
+
+ it('should pass modifiers to the global execute function', async () => {
+ const toolSlug = 'test-tool';
+ const toolParams = {
+ userId: 'test-user',
+ arguments: { input: 'test-value' },
+ };
+
+ const modifiers = {
+ beforeToolExecute: vi.fn(params => params),
+ afterToolExecute: vi.fn(response => response),
+ };
+
+ await toolset.executeTool(toolSlug, toolParams, modifiers);
+
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, modifiers);
+ });
+ });
+
+ describe('waitAndHandleAssistantToolCalls', () => {
+ it('should handle and submit tool outputs for an assistant run', async () => {
+ const userId = 'test-user';
+ const client = new OpenAI();
+ const thread = { id: 'thread-123' } as OpenAI.Beta.Threads.Thread;
+ const run = {
+ id: 'run-123',
+ status: 'requires_action',
+ required_action: {
+ submit_tool_outputs: {
+ tool_calls: [
+ {
+ id: 'tool-call-123',
+ type: 'function',
+ function: {
+ name: 'test-tool',
+ arguments: JSON.stringify({ input: 'test-value' }),
+ },
+ },
+ ],
+ },
+ },
+ } as OpenAI.Beta.Threads.Run;
+
+ const handleAssistantMessageSpy = vi.spyOn(toolset, 'handleAssistantMessage');
+ handleAssistantMessageSpy.mockResolvedValue([
+ {
+ tool_call_id: 'tool-call-123',
+ output: JSON.stringify({ result: 'success' }),
+ },
+ ]);
+
+ const result = await toolset.waitAndHandleAssistantToolCalls(userId, client, run, thread);
+
+ expect(handleAssistantMessageSpy).toHaveBeenCalledWith(userId, run, undefined, undefined);
+ expect(client.beta.threads.runs.submitToolOutputs).toHaveBeenCalledWith(thread.id, run.id, {
+ tool_outputs: [
+ {
+ tool_call_id: 'tool-call-123',
+ output: JSON.stringify({ result: 'success' }),
+ },
+ ],
+ });
+ expect(result).toEqual({ id: 'run-123', status: 'completed' });
+ });
});
});