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

next
ts 161 lines 6 KB
Raw
1 #!/usr/bin/env bun
2 /**
3 * Checks ten read-only Apollo responses against the CLI's generated client schemas.
4 *
5 * COMPOSIO_BASE_URL=https://staging-backend.composio.dev \
6 * COMPOSIO_API_KEY=ak_... COMPOSIO_USER_API_KEY=uak_... \
7 * bun scripts/apollo-contract.ts
8 *
9 * The project key covers toolkit, tool, trigger, auth-config and account reads.
10 * The user key covers organization/project discovery used during CLI login.
11 * Both keys are required. Requests run sequentially, without retries, and stop
12 * at the first 401, 403 or 429.
13 */
14 import { appendFileSync } from 'node:fs';
15 import { Config, Data, Effect, Match, Redacted } from 'effect';
16 import { FetchHttpClient, HttpClient } from 'effect/http';
17 import {
18 // eslint-disable-next-line no-restricted-imports -- a standalone script needs its own client
19 make as makeComposioClient,
20 Auth,
21 isAPIError,
22 type ComposioError,
23 } from '@composio/client/effect';
24 import { schemaLoaders } from '@composio/client/effect/schemas';
25
26 const ALLOWED_ORIGINS = [
27 'https://staging-backend.composio.dev',
28 'https://backend.composio.dev',
29 ] as const;
30 const validated = { decode: true } as const;
31
32 class ContractSetupError extends Data.TaggedError('ContractSetupError')<{
33 readonly message: string;
34 }> {}
35
36 type Outcome = {
37 readonly status: 'pass' | 'fail' | 'not run';
38 readonly detail: string;
39 readonly stop?: boolean;
40 };
41
42 const describeFailure = (error: ComposioError | ContractSetupError): Outcome =>
43 Match.value(error).pipe(
44 Match.tag('@composio/client/DecodeError', error => ({
45 status: 'fail' as const,
46 detail: `schema mismatch: ${error.issues
47 .slice(0, 5)
48 .map(issue => `${issue.path.join('.') || '(root)'}: ${issue.kind}`)
49 .join('; ')}`,
50 })),
51 Match.tag('ContractSetupError', error => ({ status: 'fail' as const, detail: error.message })),
52 Match.orElse(error => ({
53 status: 'fail' as const,
54 detail: isAPIError(error) ? `HTTP ${error.status}: ${error.message}` : error.message,
55 stop: isAPIError(error) && [401, 403, 429].includes(error.status),
56 }))
57 );
58
59 export const checkApolloContract = Effect.gen(function* () {
60 const baseURL = yield* Config.Literals(ALLOWED_ORIGINS, 'COMPOSIO_BASE_URL');
61 const projectKey = yield* Config.NonEmptyString('COMPOSIO_API_KEY').pipe(
62 Config.map(Redacted.make)
63 );
64 const userKey = yield* Config.NonEmptyString('COMPOSIO_USER_API_KEY').pipe(
65 Config.map(Redacted.make)
66 );
67 const httpClient = yield* HttpClient.HttpClient;
68 const clientFor = (auth: Auth) =>
69 makeComposioClient(
70 { auth, baseURL, maxRetries: 0, timeout: 10_000, schemas: schemaLoaders },
71 httpClient
72 );
73 const project = clientFor(Auth.project(projectKey));
74 const user = clientFor(Auth.user(userKey));
75 const cases: Record<string, Effect.Effect<unknown, ComposioError | ContractSetupError>> = {
76 'toolkits.list': project.toolkits.list({ limit: 5 }, validated),
77 'toolkits.retrieve hackernews': project.toolkits.retrieve('hackernews', null, validated),
78 'toolkits.retrieveCategories': project.toolkits.retrieveCategories(validated),
79 'tools.list hackernews': project.tools.list(
80 { toolkit_slug: 'hackernews', limit: 5 },
81 validated
82 ),
83 'tools.retrieve HACKERNEWS_GET_USER': project.tools.retrieve(
84 'HACKERNEWS_GET_USER',
85 null,
86 validated
87 ),
88 'triggersTypes.list': project.triggersTypes.list({ limit: 5 }, validated),
89 'authConfigs.list': project.authConfigs.list({ limit: 5 }, validated),
90 'connectedAccounts.list': project.connectedAccounts.list({ limit: 5 }, validated),
91 'org.list': user.org.list({ limit: 50 }, validated),
92 'org.project.list (first org)': Effect.gen(function* () {
93 const { organizations } = yield* user.org.list({ limit: 1 }, validated);
94 const [first] = organizations;
95 if (first === undefined) {
96 return yield* new ContractSetupError({
97 message: 'the user key belongs to no organization',
98 });
99 }
100 return yield* user.org.project.list(
101 { limit: 50 },
102 { ...validated, headers: { 'x-org-id': first.id } }
103 );
104 }),
105 };
106
107 let stopReason: string | undefined;
108 const results = yield* Effect.forEach(
109 Object.entries(cases),
110 ([name, request]) =>
111 Effect.gen(function* () {
112 const outcome: Outcome = stopReason
113 ? { status: 'not run', detail: stopReason }
114 : yield* request.pipe(
115 Effect.match({
116 onSuccess: (): Outcome => ({ status: 'pass', detail: '' }),
117 onFailure: describeFailure,
118 })
119 );
120 if (outcome.stop)
121 stopReason = `stopped after "${name}" failed with ${outcome.detail.split(':')[0]}`;
122 return { name, outcome };
123 }),
124 { concurrency: 1 }
125 );
126 return { baseURL, results };
127 });
128
129 /** Encode punctuation so response text cannot become Markdown or HTML. */
130 const escapeCell = (text: string) =>
131 text
132 .replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu, ' ')
133 .replace(/[&<>!"'`*_[\]{}()#+.\\|~:-]/g, char => `&#${char.charCodeAt(0)};`);
134
135 export const renderReport = ({ baseURL, results }: Effect.Success<typeof checkApolloContract>) => {
136 const passed = results.filter(result => result.outcome.status === 'pass').length;
137 return [
138 `### Apollo contract: ${baseURL}`,
139 '',
140 `${passed}/${results.length} cases passed against \`@composio/client\` response schemas.`,
141 '',
142 '| Case | Result | Detail |',
143 '| --- | --- | --- |',
144 ...results.map(
145 ({ name, outcome }) => `| ${name} | ${outcome.status} | ${escapeCell(outcome.detail)} |`
146 ),
147 '',
148 ].join('\n');
149 };
150
151 if (import.meta.main) {
152 const result = await Effect.runPromise(
153 checkApolloContract.pipe(Effect.provide(FetchHttpClient.layer))
154 );
155 const report = renderReport(result);
156 // eslint-disable-next-line no-console -- the report is this script's output
157 console.log(report);
158 if (process.env.GITHUB_STEP_SUMMARY)
159 appendFileSync(process.env.GITHUB_STEP_SUMMARY, `${report}\n`);
160 process.exitCode = result.results.every(result => result.outcome.status === 'pass') ? 0 : 1;
161 }