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 230 lines 8.22 KB
Raw
1 /**
2 * Manual verification for saved Session configs.
3 *
4 * Runs each check against a real project and prints PASS or FAIL. Any failure
5 * sets a non-zero exit code. Sessions created here are deleted at the end.
6 *
7 * Required environment:
8 * - COMPOSIO_API_KEY: a project with Session configs enabled
9 * - SESSION_CONFIG_ID: an active `sc_…` config in that project
10 * - ARCHIVED_SESSION_CONFIG_ID: an archived `sc_…` config in that project
11 *
12 * Optional:
13 * - COMPOSIO_API_KEY_NO_FEATURE: a project without Session configs (403 check)
14 *
15 * Run: pnpm start:session-configs
16 */
17 import { APIError } from '@composio/client';
18 import {
19 Composio,
20 SessionPreset,
21 type SessionConfigPolicy,
22 type ToolRouterCreateSessionConfig,
23 } from '@composio/core';
24 import 'dotenv/config';
25 import { cleanupVerificationSessions, listAllSessionConfigs } from './session-config-verification';
26
27 const required = ['COMPOSIO_API_KEY', 'SESSION_CONFIG_ID', 'ARCHIVED_SESSION_CONFIG_ID'] as const;
28 const missing = required.filter(name => !process.env[name]);
29 if (missing.length > 0) {
30 console.error(`Missing required environment variables: ${missing.join(', ')}`);
31 process.exit(1);
32 }
33
34 const sessionConfigId = process.env.SESSION_CONFIG_ID!;
35 const archivedSessionConfigId = process.env.ARCHIVED_SESSION_CONFIG_ID!;
36 const noFeatureApiKey = process.env.COMPOSIO_API_KEY_NO_FEATURE;
37 const userId = `session-configs-check-${Date.now()}`;
38
39 const composio = new Composio({ apiKey: process.env.COMPOSIO_API_KEY });
40 const createdSessionIds: string[] = [];
41 let failures = 0;
42
43 async function createTrackedSession(config: ToolRouterCreateSessionConfig) {
44 const session = await composio.sessions.create(userId, config);
45 createdSessionIds.push(session.sessionId);
46 return session;
47 }
48
49 async function check(label: string, run: () => Promise<void>): Promise<void> {
50 try {
51 await run();
52 console.log(`PASS ${label}`);
53 } catch (error) {
54 failures += 1;
55 console.log(`FAIL ${label}\n ${error instanceof Error ? error.message : String(error)}`);
56 }
57 }
58
59 function assert(condition: unknown, message: string): asserts condition {
60 if (!condition) {
61 throw new Error(message);
62 }
63 }
64
65 /** HTTP status of a client error, or undefined for anything else. */
66 function statusOf(error: unknown): number | undefined {
67 return error instanceof APIError ? error.status : undefined;
68 }
69
70 async function expectStatus(status: number, run: () => Promise<unknown>): Promise<void> {
71 let result: unknown;
72 try {
73 result = await run();
74 } catch (error) {
75 assert(statusOf(error) === status, `expected ${status}, got ${statusOf(error) ?? error}`);
76 return;
77 }
78 throw new Error(`expected ${status}, but the call succeeded with ${JSON.stringify(result)}`);
79 }
80
81 /** The saved policy uses enable/disable; the session snapshot uses enabled/disabled. */
82 function toolkitsAsSnapshot(policy: SessionConfigPolicy): unknown {
83 const toolkits = policy.toolkits;
84 if (!toolkits) return undefined;
85 if ('enable' in toolkits) return { enabled: toolkits.enable };
86 if ('disable' in toolkits) return { disabled: toolkits.disable };
87 // An approval-only policy filters no toolkit.
88 return undefined;
89 }
90
91 function sameToolkits(actual: unknown, expected: unknown): boolean {
92 return JSON.stringify(actual) === JSON.stringify(expected);
93 }
94
95 async function main(): Promise<void> {
96 let policy: SessionConfigPolicy | undefined;
97
98 await check('list() returns the active config', async () => {
99 const items = await listAllSessionConfigs(composio.sessionConfigs);
100 assert(
101 items.some(item => item.id === sessionConfigId),
102 `${sessionConfigId} missing from active configs`
103 );
104 });
105
106 await check('list({ archived: true }) returns only archived configs', async () => {
107 const items = await listAllSessionConfigs(composio.sessionConfigs, { archived: true });
108 assert(
109 items.some(item => item.id === archivedSessionConfigId),
110 `${archivedSessionConfigId} missing`
111 );
112 assert(!items.some(item => item.id === sessionConfigId), `${sessionConfigId} is listed`);
113 assert(
114 items.every(item => item.archived),
115 'a non-archived config is listed'
116 );
117 });
118
119 await check('get() returns the policy', async () => {
120 const sessionConfig = await composio.sessionConfigs.get(sessionConfigId);
121 assert(sessionConfig.id === sessionConfigId, `got ${sessionConfig.id}`);
122 policy = sessionConfig.config;
123 console.log(` policy: ${JSON.stringify(policy)}`);
124 });
125
126 await check('create() from the config sets sourceSessionConfig and its access', async () => {
127 const session = await createTrackedSession({
128 experimental: { sessionConfigId },
129 });
130 assert(
131 session.experimental.sourceSessionConfig?.id === sessionConfigId,
132 `sourceSessionConfig is ${JSON.stringify(session.experimental.sourceSessionConfig)}`
133 );
134 assert(policy, 'policy was not read');
135 assert(
136 sameToolkits(session.config.toolkits, toolkitsAsSnapshot(policy)),
137 `session toolkits ${JSON.stringify(session.config.toolkits)} do not match the policy`
138 );
139 const tools = await session.tools();
140 console.log(` tools(): ${tools.length} tools`);
141 });
142
143 let narrowedSessionId: string | undefined;
144 await check('update() applies the config to a broad inline session', async () => {
145 const session = await createTrackedSession({
146 toolkits: ['github', 'gmail', 'slack', 'googlecalendar'],
147 });
148 await session.update({ experimental: { sessionConfigId } });
149 narrowedSessionId = session.sessionId;
150 assert(
151 session.experimental.sourceSessionConfig?.id === sessionConfigId,
152 `sourceSessionConfig is ${JSON.stringify(session.experimental.sourceSessionConfig)}`
153 );
154 assert(policy, 'policy was not read');
155 assert(
156 sameToolkits(session.config.toolkits, toolkitsAsSnapshot(policy)),
157 `session toolkits ${JSON.stringify(session.config.toolkits)} do not match the policy`
158 );
159 });
160
161 await check('create() with an archived config fails with 404', () =>
162 expectStatus(404, () =>
163 createTrackedSession({
164 experimental: { sessionConfigId: archivedSessionConfigId },
165 })
166 )
167 );
168
169 await check('create() with a missing config fails with 404', () =>
170 expectStatus(404, () =>
171 createTrackedSession({
172 experimental: { sessionConfigId: 'sc_doesnotexist' },
173 })
174 )
175 );
176
177 await check('update() with an archived config fails with 404 and changes nothing', async () => {
178 assert(narrowedSessionId, 'the narrowed session was not created');
179 const before = await composio.sessions.use(narrowedSessionId);
180 await expectStatus(404, () =>
181 before.update({ experimental: { sessionConfigId: archivedSessionConfigId } })
182 );
183 const after = await composio.sessions.use(narrowedSessionId);
184 assert(
185 JSON.stringify(after.config) === JSON.stringify(before.config),
186 'config changed after a failed update'
187 );
188 assert(
189 after.experimental.sourceSessionConfig?.id === before.experimental.sourceSessionConfig?.id,
190 'sourceSessionConfig changed after a failed update'
191 );
192 });
193
194 if (noFeatureApiKey) {
195 await check('list() without the feature fails with 403', () =>
196 expectStatus(403, () => new Composio({ apiKey: noFeatureApiKey }).sessionConfigs.list())
197 );
198 } else {
199 console.log('SKIP list() without the feature (COMPOSIO_API_KEY_NO_FEATURE not set)');
200 }
201
202 // Informational: records whether the direct tools preset works with a saved
203 // config. The SDK surfaces a backend rejection unchanged either way.
204 try {
205 await createTrackedSession({
206 sessionPreset: SessionPreset.DIRECT_TOOLS,
207 experimental: { sessionConfigId },
208 });
209 console.log('INFO DIRECT_TOOLS with a saved config: accepted');
210 } catch (error) {
211 console.log(
212 `INFO DIRECT_TOOLS with a saved config: rejected with ${statusOf(error) ?? 'no status'}: ${
213 error instanceof Error ? error.message : String(error)
214 }`
215 );
216 }
217 }
218
219 main()
220 .catch(error => {
221 failures += 1;
222 console.error(error);
223 })
224 .finally(async () => {
225 await check('delete all created Sessions', () =>
226 cleanupVerificationSessions(createdSessionIds, id => composio.sessions.delete(id))
227 );
228 console.log(failures === 0 ? '\nAll checks passed' : `\n${failures} check(s) failed`);
229 process.exitCode = failures === 0 ? 0 : 1;
230 });