Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { afterEach, describe, expect, it } from '@effect/vitest'; |
| 2 | import { vi } from 'vitest'; |
| 3 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 4 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 5 | import { ConfigProvider, Effect, Fiber, Layer } from 'effect'; |
| 6 | import { FetchHttpClient } from 'effect/http'; |
| 7 | import * as tempy from 'tempy'; |
| 8 | import { createToolRouterSessionContext } from 'src/effects/create-tool-router-session'; |
| 9 | import { ComposioCliUserConfigLive } from 'src/services/cli-user-config'; |
| 10 | import { ComposioClientSingleton, ComposioToolkitsRepository } from 'src/services/composio-clients'; |
| 11 | import { DashboardToolExecution } from 'src/services/dashboard-tool-execution'; |
| 12 | import { defaultNodeOs, NodeOs } from 'src/services/node-os'; |
| 13 | import { NodeProcess } from 'src/services/node-process'; |
| 14 | import { cliDebugFlagsLayer } from 'src/services/runtime-flags'; |
| 15 | import { ToolkitSlugCatalog } from 'src/services/toolkit-slug-catalog'; |
| 16 | import { ToolsExecutor, ToolsExecutorLive } from 'src/services/tools-executor'; |
| 17 | import { ComposioUserContextLive } from 'src/services/user-context'; |
| 18 | import { clearInProcessMemos } from 'src/utils/memoize-in-process'; |
| 19 | import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test'; |
| 20 | |
| 21 | // Exercise real user-context/config resolution without opening the OS credential store. |
| 22 | vi.mock('@composio/cli-keyring/effect', async importOriginal => { |
| 23 | const actual = await importOriginal<typeof import('@composio/cli-keyring/effect')>(); |
| 24 | const { Effect, Layer } = await import('effect'); |
| 25 | const { KeyringError } = await import('@composio/cli-keyring'); |
| 26 | return { |
| 27 | ...actual, |
| 28 | KeyringLiveWithBackend: () => |
| 29 | Layer.succeed(actual.KeyringService, { |
| 30 | getPassword: () => Effect.fail(new KeyringError({ kind: 'NoEntry' })), |
| 31 | getSecret: () => Effect.fail(new KeyringError({ kind: 'NoEntry' })), |
| 32 | setPassword: () => Effect.die(new Error('Unexpected credential write')), |
| 33 | setSecret: () => Effect.die(new Error('Unexpected credential write')), |
| 34 | deleteCredential: () => Effect.die(new Error('Unexpected credential deletion')), |
| 35 | isAvailable: Effect.succeed(true), |
| 36 | }), |
| 37 | }; |
| 38 | }); |
| 39 | |
| 40 | const BASE_URL = 'https://backend.composio.dev'; |
| 41 | |
| 42 | const platformLayer = () => |
| 43 | Layer.mergeAll( |
| 44 | BunFileSystem.layer, |
| 45 | BunPath.layer, |
| 46 | Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempy.temporaryDirectory() })), |
| 47 | Layer.succeed( |
| 48 | ConfigProvider.ConfigProvider, |
| 49 | ConfigProvider.fromEnv({ |
| 50 | env: { COMPOSIO_USER_API_KEY: 'uak_config', COMPOSIO_BASE_URL: BASE_URL }, |
| 51 | }) |
| 52 | ) |
| 53 | ); |
| 54 | |
| 55 | const withSingleton = () => Layer.provide(ComposioClientSingleton.Default, platformLayer()); |
| 56 | |
| 57 | // The real executor over the real client singleton, plus every service that |
| 58 | // `execute` and session creation require, with the credential store mocked above. |
| 59 | const withExecutor = () => { |
| 60 | const platform = platformLayer(); |
| 61 | const singleton = Layer.provide(ComposioClientSingleton.Default, platform); |
| 62 | const repository = Layer.provide(ComposioToolkitsRepository.Default, platform); |
| 63 | const catalog = Layer.provide(ToolkitSlugCatalog.Default, Layer.mergeAll(repository, platform)); |
| 64 | // Never called here: every execution in this file targets the backend. |
| 65 | const dashboard = Layer.provide( |
| 66 | DashboardToolExecution.Default, |
| 67 | Layer.mergeAll(Layer.provide(ComposioUserContextLive, platform), FetchHttpClient.layer) |
| 68 | ); |
| 69 | return Layer.mergeAll( |
| 70 | Layer.provide(ToolsExecutorLive, Layer.mergeAll(singleton, catalog, dashboard)), |
| 71 | singleton, |
| 72 | repository, |
| 73 | platform, |
| 74 | NodeProcess.Default, |
| 75 | Layer.provide(ComposioUserContextLive, platform), |
| 76 | Layer.provide(ComposioCliUserConfigLive, Layer.mergeAll(platform, NodeProcess.Default)), |
| 77 | TerminalUITest, |
| 78 | cliDebugFlagsLayer() |
| 79 | ); |
| 80 | }; |
| 81 | |
| 82 | const jsonResponse = (body: unknown) => |
| 83 | new Response(JSON.stringify(body), { |
| 84 | status: 200, |
| 85 | headers: { 'Content-Type': 'application/json' }, |
| 86 | }); |
| 87 | |
| 88 | /** |
| 89 | * A `fetch` answer that never settles on its own: it hands out the request's |
| 90 | * `AbortSignal` and rejects only once that signal fires. Pair it with |
| 91 | * `mockImplementationOnce` so one chosen request hangs. |
| 92 | */ |
| 93 | const pendingRequest = () => { |
| 94 | const started = Promise.withResolvers<AbortSignal>(); |
| 95 | const respond = (_input: string | URL | Request, init?: RequestInit) => { |
| 96 | const signal = init!.signal!; |
| 97 | started.resolve(signal); |
| 98 | return new Promise<Response>((_resolve, reject) => { |
| 99 | signal.addEventListener('abort', () => reject(signal.reason), { once: true }); |
| 100 | }); |
| 101 | }; |
| 102 | return { respond, signal: Effect.promise(() => started.promise) }; |
| 103 | }; |
| 104 | |
| 105 | const requestUrl = (call: readonly unknown[] | undefined) => new URL(String(call?.[0])); |
| 106 | |
| 107 | describe('interrupting a single-shot client request', () => { |
| 108 | afterEach(() => { |
| 109 | vi.restoreAllMocks(); |
| 110 | clearInProcessMemos(); |
| 111 | }); |
| 112 | |
| 113 | it.effect('connectedAccounts.list aborts the in-flight request', () => |
| 114 | Effect.gen(function* () { |
| 115 | const pending = pendingRequest(); |
| 116 | const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementationOnce(pending.respond); |
| 117 | const client = yield* Effect.flatMap(ComposioClientSingleton, singleton => singleton.get()); |
| 118 | |
| 119 | const fiber = yield* Effect.forkChild( |
| 120 | client.connectedAccounts.list({ user_ids: ['default'], statuses: ['ACTIVE'] }) |
| 121 | ); |
| 122 | const requestSignal = yield* pending.signal; |
| 123 | expect(requestSignal.aborted).toBe(false); |
| 124 | |
| 125 | yield* Fiber.interrupt(fiber); |
| 126 | |
| 127 | expect(requestSignal.aborted).toBe(true); |
| 128 | expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 129 | expect(requestUrl(fetchSpy.mock.calls[0]).pathname).toBe('/api/v3.1/connected_accounts'); |
| 130 | }).pipe(Effect.provide(withSingleton())) |
| 131 | ); |
| 132 | |
| 133 | it.effect('tool router session creation aborts the in-flight create request', () => |
| 134 | Effect.gen(function* () { |
| 135 | const pending = pendingRequest(); |
| 136 | const fetchSpy = vi |
| 137 | .spyOn(globalThis, 'fetch') |
| 138 | // The connection context the session is built from. |
| 139 | .mockResolvedValueOnce(jsonResponse({ items: [] })) |
| 140 | .mockImplementationOnce(pending.respond); |
| 141 | const client = yield* Effect.flatMap(ComposioClientSingleton, singleton => singleton.get()); |
| 142 | |
| 143 | const fiber = yield* Effect.forkChild(createToolRouterSessionContext(client, 'default')); |
| 144 | const requestSignal = yield* pending.signal; |
| 145 | expect(requestSignal.aborted).toBe(false); |
| 146 | |
| 147 | yield* Fiber.interrupt(fiber); |
| 148 | |
| 149 | expect(requestSignal.aborted).toBe(true); |
| 150 | expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 151 | expect(requestUrl(fetchSpy.mock.calls[1]).pathname).toBe('/api/v3.1/tool_router/session'); |
| 152 | }).pipe(Effect.provide(withExecutor())) |
| 153 | ); |
| 154 | |
| 155 | it.effect('a tool execution aborts the in-flight execute request', () => |
| 156 | Effect.gen(function* () { |
| 157 | const pending = pendingRequest(); |
| 158 | const fetchSpy = vi |
| 159 | .spyOn(globalThis, 'fetch') |
| 160 | // The connection context, then the session the execution runs in. |
| 161 | .mockResolvedValueOnce(jsonResponse({ items: [] })) |
| 162 | .mockResolvedValueOnce(jsonResponse({ session_id: 'trs_interrupted' })) |
| 163 | .mockImplementationOnce(pending.respond); |
| 164 | const executor = yield* ToolsExecutor; |
| 165 | |
| 166 | // A meta tool: its execution needs no toolkit lookup and no input |
| 167 | // schema, so the hanging request is the execution itself. |
| 168 | const fiber = yield* Effect.forkChild( |
| 169 | executor.execute('COMPOSIO_SEARCH_TOOLS', { |
| 170 | userId: 'default', |
| 171 | target: { kind: 'backend' }, |
| 172 | arguments: { queries: [{ use_case: 'send an email' }] }, |
| 173 | }) |
| 174 | ); |
| 175 | const requestSignal = yield* pending.signal; |
| 176 | expect(requestSignal.aborted).toBe(false); |
| 177 | |
| 178 | yield* Fiber.interrupt(fiber); |
| 179 | |
| 180 | expect(requestSignal.aborted).toBe(true); |
| 181 | expect(fetchSpy).toHaveBeenCalledTimes(3); |
| 182 | expect(requestUrl(fetchSpy.mock.calls[2]).pathname).toBe( |
| 183 | '/api/v3.1/tool_router/session/trs_interrupted/execute_meta' |
| 184 | ); |
| 185 | }).pipe(Effect.provide(withExecutor())) |
| 186 | ); |
| 187 | }); |