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 222 lines 7.72 KB
Raw
1 import { Effect } from 'effect';
2 import type { ComposioClientShape } from '@composio/client/effect';
3 import type { AuthConfigCreateParams } from '@composio/client/resources/auth-configs';
4 import type {
5 TriggerInstanceListActiveParams,
6 TriggerInstanceUpsertParams,
7 } from '@composio/client/resources/trigger-instances';
8 import {
9 AuthConfigCreateResponse,
10 AuthConfigItem,
11 AuthConfigListResponse,
12 } from 'src/models/auth-configs';
13 import { ConnectedAccountItem } from 'src/models/connected-accounts';
14 import { Toolkits, type ToolkitSearchResult } from 'src/models/toolkits';
15 import { TriggerInstanceItems } from 'src/models/triggers';
16 import { TriggerType } from 'src/models/trigger-types';
17 import type { ToolkitProjectScope } from './composio-clients';
18 import type { ClientError } from './composio-errors';
19 import { decode, idempotent } from './composio-request';
20
21 /**
22 * Auth config, connected account, and trigger instance operations: single-page
23 * CRUD with no catalog caching. These resources belong to a project, so every
24 * call takes the org and project the command resolved and goes through the
25 * client `clientFor` builds for them; none falls back to the project the
26 * backend would pick for the key.
27 */
28 export const resourceOperations = (
29 clientFor: (scope: ToolkitProjectScope) => Effect.Effect<ComposioClientShape, ClientError>
30 ) => ({
31 /**
32 * Lists auth configs with optional filters. Returns a single page of results.
33 * @param params - Search/filter parameters
34 */
35 listAuthConfigs: (
36 scope: ToolkitProjectScope,
37 params: {
38 search?: string;
39 toolkit_slug?: string;
40 limit?: number;
41 show_disabled?: boolean;
42 }
43 ) =>
44 clientFor(scope).pipe(
45 Effect.flatMap(c =>
46 c.authConfigs.list(
47 {
48 search: params.search,
49 toolkit_slug: params.toolkit_slug,
50 limit: params.limit,
51 show_disabled: params.show_disabled ?? true,
52 },
53 idempotent
54 )
55 ),
56 Effect.flatMap(decode(AuthConfigListResponse))
57 ),
58 /**
59 * Retrieves detailed info about a single auth config by its nanoid.
60 * @param nanoid - Auth config ID
61 */
62 getAuthConfig: (scope: ToolkitProjectScope, nanoid: string) =>
63 clientFor(scope).pipe(
64 Effect.flatMap(c => c.authConfigs.retrieve(nanoid, idempotent)),
65 Effect.flatMap(decode(AuthConfigItem))
66 ),
67 /**
68 * Creates a new auth config for a toolkit.
69 * @param params - Create parameters (discriminated union: use_composio_managed_auth | use_custom_auth)
70 */
71 createAuthConfig: (scope: ToolkitProjectScope, params: AuthConfigCreateParams) =>
72 clientFor(scope).pipe(
73 Effect.flatMap(c => c.authConfigs.create(params)),
74 Effect.flatMap(decode(AuthConfigCreateResponse))
75 ),
76 /**
77 * Retrieves detailed info about a single connected account by its nanoid.
78 * @param nanoid - Connected account ID (e.g. "con_1a2b3c4d5e6f")
79 */
80 getConnectedAccount: (scope: ToolkitProjectScope, nanoid: string) =>
81 clientFor(scope).pipe(
82 Effect.flatMap(c => c.connectedAccounts.retrieve(nanoid, idempotent)),
83 Effect.flatMap(decode(ConnectedAccountItem))
84 ),
85 /**
86 * Lists active trigger instances with optional filters.
87 * Returns a single page of results.
88 */
89 listActiveTriggers: (scope: ToolkitProjectScope, params: TriggerInstanceListActiveParams) =>
90 clientFor(scope).pipe(
91 Effect.flatMap(c =>
92 c.triggerInstances.listActive(
93 {
94 user_ids: params.user_ids,
95 connected_account_ids: params.connected_account_ids,
96 auth_config_ids: params.auth_config_ids,
97 trigger_ids: params.trigger_ids,
98 trigger_names: params.trigger_names,
99 show_disabled: params.show_disabled,
100 limit: params.limit,
101 },
102 idempotent
103 )
104 ),
105 Effect.flatMap(response =>
106 decode(TriggerInstanceItems)(response.items).pipe(
107 // `triggers status` prints `total_items` directly, so a response that
108 // omits a count renders as a number rather than `undefined`.
109 Effect.map(items => ({
110 ...response,
111 items,
112 total_items: response.total_items ?? items.length,
113 total_pages: response.total_pages ?? 1,
114 current_page: response.current_page ?? 1,
115 next_cursor: response.next_cursor ?? null,
116 }))
117 )
118 )
119 ),
120 createTrigger: (
121 scope: ToolkitProjectScope,
122 triggerSlug: string,
123 params?: TriggerInstanceUpsertParams
124 ) => Effect.flatMap(clientFor(scope), c => c.triggerInstances.upsert(triggerSlug, params)),
125 enableTrigger: (scope: ToolkitProjectScope, triggerId: string) =>
126 Effect.flatMap(clientFor(scope), c =>
127 c.triggerInstances.manage.update(triggerId, { status: 'enable' })
128 ),
129 disableTrigger: (scope: ToolkitProjectScope, triggerId: string) =>
130 Effect.flatMap(clientFor(scope), c =>
131 c.triggerInstances.manage.update(triggerId, { status: 'disable' })
132 ),
133 });
134
135 /**
136 * Single-page searches and single-item lookups that sit outside the catalog
137 * fetches: none of them is cached.
138 */
139 export const lookupOperations = (client: Effect.Effect<ComposioClientShape, ClientError>) => ({
140 /**
141 * Searches toolkits with optional filters. Returns a single page of results.
142 * @param params - Search/filter parameters
143 */
144 searchToolkits: (params: {
145 search?: string;
146 category?: string;
147 limit?: number;
148 cursor?: string;
149 }): Effect.Effect<ToolkitSearchResult, ClientError> =>
150 Effect.flatMap(client, c =>
151 c.toolkits.list(
152 {
153 search: params.search,
154 category: params.category,
155 limit: params.limit,
156 cursor: params.cursor,
157 },
158 idempotent
159 )
160 ).pipe(
161 Effect.flatMap(response =>
162 decode(Toolkits)(response.items).pipe(
163 Effect.map(
164 items =>
165 ({
166 items,
167 total_items: response.total_items,
168 total_pages: response.total_pages,
169 next_cursor: response.next_cursor ?? null,
170 }) satisfies ToolkitSearchResult
171 )
172 )
173 )
174 ),
175 /**
176 * Searches tools with optional filters. Returns a single page of results.
177 * @param params - Search/filter parameters
178 *
179 * Not client-decoded: `tools list` prints the slug, name, and description of
180 * each item and `tools info` suggests slugs from them, so drift in any other
181 * field of a tool must not fail the listing.
182 */
183 searchTools: (params: {
184 search?: string;
185 toolkit_slug?: string;
186 tags?: string;
187 limit?: number;
188 cursor?: string;
189 }) =>
190 Effect.flatMap(client, c =>
191 c.tools.list(
192 {
193 search: params.search,
194 toolkit_slug: params.toolkit_slug,
195 tags: params.tags ? params.tags.split(',').map(t => t.trim()) : undefined,
196 limit: params.limit,
197 cursor: params.cursor,
198 toolkit_versions: 'latest',
199 },
200 idempotent
201 )
202 ),
203 /**
204 * Retrieves detailed info about a single tool by slug.
205 * @param slug - Tool slug (e.g. "GMAIL_SEND_EMAIL")
206 *
207 * Not validated by the client: `execute` treats a failed lookup as "no cached
208 * definition" and would skip input validation and file uploads on drift
209 * in a field it never reads.
210 */
211 getToolDetailed: (slug: string) =>
212 Effect.flatMap(client, c => c.tools.retrieve(slug, { toolkit_versions: 'latest' }, idempotent)),
213 /**
214 * Retrieves detailed info about a single trigger type by slug.
215 * @param slug - Trigger type slug (e.g. "GMAIL_NEW_GMAIL_MESSAGE")
216 */
217 getTriggerTypeDetailed: (slug: string) =>
218 client.pipe(
219 Effect.flatMap(c => c.triggersTypes.retrieve(slug, undefined, idempotent)),
220 Effect.flatMap(decode(TriggerType))
221 ),
222 });