@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 125 lines 4.31 KB
Raw
1 /**
2 * @fileoverview The `composio.sessionConfigs` namespace: read the saved
3 * Session configs of the project.
4 *
5 * @module SessionConfigs
6 */
7 import ComposioClient from '@composio/client';
8 import {
9 SessionConfig,
10 SessionConfigListParams,
11 SessionConfigListParamsSchema,
12 SessionConfigListResponse,
13 } from '../types/sessionConfigs.types';
14 import { ComposioRequestOptions } from '../types/requestOptions.types';
15 import { ValidationError } from '../errors/ValidationErrors';
16 import { telemetry } from '../telemetry/Telemetry';
17 import { withCancellation } from '../utils/cancellation';
18 import logger from '../utils/logger';
19 import {
20 transformSessionConfig,
21 transformSessionConfigListResponse,
22 } from '../utils/transformers/sessionConfigs';
23
24 /**
25 * `composio.sessionConfigs` — saved, reusable Session configs.
26 *
27 * A Session config is a named access policy (`sc_…`) built in the Composio
28 * Dashboard under Sessions → Configs. Pass its id as
29 * `experimental.sessionConfigId` to `composio.sessions.create()` or
30 * `session.update()` to apply it.
31 *
32 * These endpoints require Session configs to be enabled for the project;
33 * otherwise the backend returns 403.
34 *
35 * @example
36 * ```typescript
37 * const { items } = await composio.sessionConfigs.list();
38 * const sessionConfig = await composio.sessionConfigs.get(items[0].id);
39 * const session = await composio.sessions.create('user_123', {
40 * experimental: { sessionConfigId: sessionConfig.id },
41 * });
42 * ```
43 */
44 export class SessionConfigs {
45 private client: ComposioClient;
46
47 constructor(client: ComposioClient) {
48 this.client = client;
49 telemetry.instrument(this, 'SessionConfigs');
50 }
51
52 /**
53 * List the project's saved Session configs with cursor pagination.
54 *
55 * Omitting `archived` returns active configs; `archived: true` returns only
56 * archived ones. The backend defaults `limit` to 20 and caps it at 100.
57 *
58 * Requires Session configs to be enabled for the project.
59 *
60 * @param {SessionConfigListParams} [params] - `archived`, `limit` and `cursor`; the
61 * deprecated `search` is ignored
62 * @returns {Promise<SessionConfigListResponse>} Config summaries and the next cursor
63 * @throws {ValidationError} If the params fail validation
64 *
65 * @example
66 * ```typescript
67 * let cursor: string | undefined;
68 * do {
69 * const page = await composio.sessionConfigs.list({ cursor });
70 * for (const item of page.items) console.log(item.id, item.name);
71 * cursor = page.nextCursor ?? undefined;
72 * } while (cursor);
73 * ```
74 */
75 async list(
76 params?: SessionConfigListParams,
77 requestOptions?: ComposioRequestOptions
78 ): Promise<SessionConfigListResponse> {
79 const parsedParams = SessionConfigListParamsSchema.safeParse(params ?? {});
80 if (!parsedParams.success) {
81 throw new ValidationError('Failed to parse session config list params', {
82 cause: parsedParams.error,
83 });
84 }
85 const { search, ...query } = parsedParams.data;
86 if (search !== undefined) {
87 logger.warn(
88 'sessionConfigs.list(): the Composio API no longer accepts `search`; the option is ignored.'
89 );
90 }
91 const result = await withCancellation(
92 () => this.client.sessionConfigs.list(query, requestOptions),
93 requestOptions?.signal
94 );
95 return transformSessionConfigListResponse(result);
96 }
97
98 /**
99 * Retrieve one saved Session config with its description and access
100 * policy. A missing or cross-project id surfaces as the backend's 404.
101 *
102 * Requires Session configs to be enabled for the project.
103 *
104 * @param {string} id - The `sc_…` id of the Session config
105 * @returns {Promise<SessionConfig>} The config and its policy
106 * @throws {ValidationError} If `id` is empty
107 *
108 * @example
109 * ```typescript
110 * const sessionConfig = await composio.sessionConfigs.get('sc_abc123');
111 * console.log(sessionConfig.config.toolkits);
112 * ```
113 */
114 async get(id: string, requestOptions?: ComposioRequestOptions): Promise<SessionConfig> {
115 // An empty id would request the collection path and return a list page.
116 if (id.trim() === '') {
117 throw new ValidationError('Session config id must be a non-empty string');
118 }
119 const result = await withCancellation(
120 () => this.client.sessionConfigs.retrieve(id, requestOptions),
121 requestOptions?.signal
122 );
123 return transformSessionConfig(result);
124 }
125 }