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 type { ComposioClientShape } from '@composio/client/effect'; |
| 2 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 3 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 4 | import { ConfigProvider, Console, Effect, Layer, Option } from 'effect'; |
| 5 | import { HttpClient } from 'effect/http'; |
| 6 | import * as tempy from 'tempy'; |
| 7 | import { ComposioClientSingleton } from 'src/services/composio-clients'; |
| 8 | import { NodeOs } from 'src/services/node-os'; |
| 9 | import { NodeProcess } from 'src/services/node-process'; |
| 10 | import { ProjectContext } from 'src/services/project-context'; |
| 11 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 12 | import { ComposioUserContext } from 'src/services/user-context'; |
| 13 | import { hostOnlyEnvConfigProvider } from '../live-env-config-provider'; |
| 14 | import { makeSessionInfo } from '../models/account'; |
| 15 | import * as MockConsole from '../services/mock-console'; |
| 16 | import { terminalUITestImpl } from '../services/terminal-ui-test'; |
| 17 | import type { MockRequestScope } from './accounts'; |
| 18 | import { ToolkitsTest } from './toolkits'; |
| 19 | import { makeShadowClient } from './shadow-client'; |
| 20 | |
| 21 | /** What the CLI is logged in with when an agent handler test starts. */ |
| 22 | export interface AgentHandlerFixture { |
| 23 | /** The stored user API key; omitted means logged out. */ |
| 24 | readonly apiKey?: string; |
| 25 | } |
| 26 | |
| 27 | /** One `ui.output` call: the stdout payload and whether it bypassed the TTY check. */ |
| 28 | export interface AgentHandlerOutput { |
| 29 | readonly data: string; |
| 30 | readonly force: boolean; |
| 31 | } |
| 32 | |
| 33 | const clientFor = (scope: MockRequestScope): ComposioClientShape => |
| 34 | // The shadows answer only the fields `loginWithAgentIdentity` reads; `test-layer.ts` shadows |
| 35 | // its mock client the same way. |
| 36 | makeShadowClient( |
| 37 | { |
| 38 | sessionInfo: makeSessionInfo({ |
| 39 | orgId: scope.orgId ?? 'org_agent', |
| 40 | orgName: 'Agent Org', |
| 41 | orgMemberId: 'mem_agent', |
| 42 | email: 'agent@agent.composio.ai', |
| 43 | }), |
| 44 | }, |
| 45 | scope |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * What an `agent` handler needs without argv or `TestLive`: a headless terminal whose stdout |
| 50 | * payloads land in `outputs` (decoration goes to `MockConsole`), a user context that records |
| 51 | * `login` calls, the real filesystem under a temporary home (where `~/.composio/agent.json` |
| 52 | * lives), and the services the agent login step reaches (session info for the analytics |
| 53 | * link, and the consumer-toolkits cache refresh it starts in the background). The agents API |
| 54 | * itself is plain `fetch`; tests stub it with `vi.spyOn(globalThis, 'fetch')`. |
| 55 | */ |
| 56 | export const AgentHandlerTest = (fixture: AgentHandlerFixture = {}) => { |
| 57 | const outputs: Array<AgentHandlerOutput> = []; |
| 58 | const logins: Array<{ readonly apiKey: string; readonly orgId?: string }> = []; |
| 59 | const apiKey = Option.fromUndefinedOr(fixture.apiKey); |
| 60 | const homedir = tempy.temporaryDirectory({ prefix: 'agent-handler' }); |
| 61 | |
| 62 | const layer = Layer.mergeAll( |
| 63 | Layer.effect(Console.Console, MockConsole.make), |
| 64 | Layer.succeed( |
| 65 | TerminalUI, |
| 66 | TerminalUI.of({ |
| 67 | ...terminalUITestImpl, |
| 68 | output: (data, options) => |
| 69 | Effect.sync(() => { |
| 70 | outputs.push({ data, force: options?.force ?? false }); |
| 71 | }), |
| 72 | }) |
| 73 | ), |
| 74 | Layer.succeed( |
| 75 | ComposioUserContext, |
| 76 | ComposioUserContext.of({ |
| 77 | data: { |
| 78 | apiKey, |
| 79 | baseURL: 'https://mock.invalid', |
| 80 | webURL: 'https://platform.mock.invalid/', |
| 81 | orgId: Option.none(), |
| 82 | projectId: Option.none(), |
| 83 | testUserId: Option.none(), |
| 84 | }, |
| 85 | backendMismatch: Option.none(), |
| 86 | isLoggedIn: () => Option.isSome(apiKey), |
| 87 | logout: Effect.void, |
| 88 | login: (loginApiKey, orgId) => |
| 89 | Effect.sync(() => { |
| 90 | logins.push({ apiKey: loginApiKey, orgId }); |
| 91 | }), |
| 92 | update: () => Effect.void, |
| 93 | }) |
| 94 | ), |
| 95 | Layer.succeed( |
| 96 | ComposioClientSingleton, |
| 97 | ComposioClientSingleton.of({ |
| 98 | get: () => Effect.succeed(clientFor({})), |
| 99 | getFor: (scope: MockRequestScope) => Effect.succeed(clientFor(scope)), |
| 100 | getMetrics: () => Effect.succeed({ byteSize: 0, requests: 0 }), |
| 101 | }) |
| 102 | ), |
| 103 | ToolkitsTest(), |
| 104 | Layer.succeed(ProjectContext, ProjectContext.of({ resolve: Effect.succeed(Option.none()) })), |
| 105 | Layer.succeed( |
| 106 | HttpClient.HttpClient, |
| 107 | HttpClient.make(request => |
| 108 | Effect.die(new Error(`unexpected HTTP call from an agent handler test: ${request.url}`)) |
| 109 | ) |
| 110 | ), |
| 111 | BunFileSystem.layer, |
| 112 | BunPath.layer, |
| 113 | Layer.succeed( |
| 114 | NodeOs, |
| 115 | NodeOs.of({ homedir, tmpdir: homedir, arch: 'arm64', platform: 'darwin' }) |
| 116 | ), |
| 117 | Layer.succeed( |
| 118 | NodeProcess, |
| 119 | NodeProcess.of({ |
| 120 | cwd: homedir, |
| 121 | execPath: `${homedir}/composio`, |
| 122 | platform: 'darwin', |
| 123 | arch: 'arm64', |
| 124 | }) |
| 125 | ), |
| 126 | ConfigProvider.layer(hostOnlyEnvConfigProvider) |
| 127 | ); |
| 128 | return { layer, outputs, logins }; |
| 129 | }; |