Skip to content

Commit 9fbd123

Browse files
committed
Simplify Composio meta tool execution
1 parent 77df623 commit 9fbd123

13 files changed

Lines changed: 310 additions & 584 deletions

File tree

sdk/src/__tests__/composio.test.ts

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,65 @@
11
import { afterEach, describe, expect, mock, test } from 'bun:test'
22

3-
import { getComposioCustomToolDefinitions } from '../composio'
3+
import { COMPOSIO_META_TOOL_NAMES } from '@codebuff/common/constants/composio'
44

5-
describe('getComposioCustomToolDefinitions', () => {
5+
import { getComposioMetaToolDefinitions } from '../composio'
6+
7+
describe('getComposioMetaToolDefinitions', () => {
68
const originalFetch = globalThis.fetch
79

810
afterEach(() => {
911
globalThis.fetch = originalFetch
1012
})
1113

12-
test('does not cache an empty tool list after discovery timeout', async () => {
13-
const apiKey = `timeout-key-${Date.now()}`
14-
const timeoutFetch = mock(
15-
async (_url: string | URL | Request, init?: RequestInit) => {
16-
const signal = init?.signal
17-
return new Promise<Response>((_resolve, reject) => {
18-
if (signal?.aborted) {
19-
reject(new Error('aborted'))
20-
return
21-
}
14+
test('returns static Composio meta tool definitions without discovery fetch', () => {
15+
const fetchMock = mock(async () => new Response('{}'))
16+
globalThis.fetch = fetchMock as unknown as typeof fetch
17+
18+
const tools = getComposioMetaToolDefinitions({
19+
apiKey: 'codebuff-api-key',
20+
})
21+
22+
expect(tools.map((tool) => tool.toolName)).toEqual([
23+
...COMPOSIO_META_TOOL_NAMES,
24+
])
25+
expect(fetchMock).not.toHaveBeenCalled()
26+
})
2227

23-
signal?.addEventListener(
24-
'abort',
25-
() => reject(new Error('aborted')),
26-
{ once: true },
27-
)
28+
test('executes a meta tool through the server execute endpoint', async () => {
29+
const fetchMock = mock(
30+
async (_url: string | URL | Request, init?: RequestInit) => {
31+
expect(init?.method).toBe('POST')
32+
expect(init?.headers).toEqual({
33+
Authorization: 'Bearer codebuff-api-key',
34+
'Content-Type': 'application/json',
35+
})
36+
expect(JSON.parse(String(init?.body))).toEqual({
37+
toolName: 'COMPOSIO_SEARCH_TOOLS',
38+
input: {
39+
queries: ['find gmail tools'],
40+
session: { generate_id: true },
41+
},
2842
})
43+
return new Response(
44+
JSON.stringify({
45+
output: [{ type: 'json', value: { ok: true } }],
46+
}),
47+
{ status: 200 },
48+
)
2949
},
3050
)
31-
globalThis.fetch = timeoutFetch as unknown as typeof fetch
51+
globalThis.fetch = fetchMock as unknown as typeof fetch
3252

33-
const timedOutTools = await getComposioCustomToolDefinitions({
34-
apiKey,
35-
logger: { warn: mock(() => {}) },
36-
})
37-
expect(timedOutTools).toEqual([])
38-
expect(timeoutFetch).toHaveBeenCalledTimes(1)
39-
40-
const successFetch = mock(async () => {
41-
return new Response(
42-
JSON.stringify({
43-
sessionId: 'session-123',
44-
tools: [
45-
{
46-
toolName: 'COMPOSIO_SEARCH_TOOLS',
47-
inputSchema: { type: 'object', properties: {} },
48-
description: 'Search tools',
49-
},
50-
],
51-
}),
52-
{ status: 200 },
53-
)
54-
})
55-
globalThis.fetch = successFetch as unknown as typeof fetch
53+
const searchTool = getComposioMetaToolDefinitions({
54+
apiKey: 'codebuff-api-key',
55+
}).find((tool) => tool.toolName === 'COMPOSIO_SEARCH_TOOLS')
5656

57-
const tools = await getComposioCustomToolDefinitions({
58-
apiKey,
59-
logger: { warn: mock(() => {}) },
57+
const output = await searchTool?.execute({
58+
queries: ['find gmail tools'],
59+
session: { generate_id: true },
6060
})
6161

62-
expect(successFetch).toHaveBeenCalledTimes(1)
63-
expect(tools).toHaveLength(1)
64-
expect(tools[0]?.toolName).toBe('COMPOSIO_SEARCH_TOOLS')
62+
expect(output).toEqual([{ type: 'json', value: { ok: true } }])
63+
expect(fetchMock).toHaveBeenCalledTimes(1)
6564
})
6665
})

0 commit comments

Comments
 (0)