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 123 lines 4.21 KB
Raw
1 import { Auth, Composio, createFetchTransport, type ClientOptions } from '@composio/client';
2 import { paginate } from '@composio/client/pagination';
3
4 /**
5 * Tools per page. Next.js does not cache an entry over 2 MiB, and when that
6 * write fails it keeps serving the previously cached response. An entry holds
7 * the body base64-encoded, so a response must stay under about 1.5 MiB. At 25
8 * the largest production tools page measured 1.23 MB (Tableau, a 1.64 MB
9 * entry); one Tableau tool dominates it, so a smaller page barely helps.
10 */
11 export const TOOL_PAGE_SIZE = 25;
12
13 /** Toolkits per page. At 500 the largest production page measured 404 KB. */
14 export const TOOLKIT_PAGE_SIZE = 500;
15
16 /** The most items one `/tools` or `/toolkits` list request returns; the API caps `limit` here. */
17 export const API_PAGE_LIMIT = 1000;
18
19 /** The most items one `/triggers_types` list request returns; the API caps `limit` here. */
20 export const TRIGGER_PAGE_LIMIT = 50;
21
22 /**
23 * Milliseconds a live list traversal may take during a page render. A larger
24 * toolkit takes many sequential pages, so the bound covers the whole traversal;
25 * when it fires the caller falls back to the committed snapshot.
26 */
27 export const LIVE_TRAVERSAL_TIMEOUT_MS = 15_000;
28
29 type Fetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
30
31 export interface ComposioClientOptions {
32 /**
33 * Seconds a response stays fresh in the Next.js Data Cache. Leave it unset
34 * outside Next.js; each page of a list is cached as its own entry.
35 */
36 revalidate?: number;
37 /** Retries for idempotent requests; the client defaults to 2. */
38 maxRetries?: number;
39 /** Milliseconds one request may take, including a custom `fetch`'s own retries. */
40 timeout?: number;
41 fetch?: Fetch;
42 }
43
44 function clientOverrides({ revalidate, maxRetries, timeout, fetch }: ComposioClientOptions) {
45 return {
46 transport: createFetchTransport({
47 fetch,
48 defaults: revalidate === undefined ? undefined : { next: { revalidate } },
49 }),
50 ...(maxRetries === undefined ? {} : { maxRetries }),
51 ...(timeout === undefined ? {} : { timeout }),
52 } satisfies Partial<ClientOptions>;
53 }
54
55 /**
56 * A production API client authenticated with a project API key. For callers
57 * that must reach production whatever the environment says: the generators,
58 * the Instant toolkit list, and the snapshot-miss toolkit lookup.
59 */
60 export function createComposioClient(apiKey: string, options: ComposioClientOptions = {}): Composio {
61 return new Composio({ auth: Auth.project(apiKey), ...clientOverrides(options) });
62 }
63
64 /**
65 * A client configured by `Composio.fromEnv`: credentials from `COMPOSIO_API_KEY`
66 * and the API host from `COMPOSIO_BASE_URL`, defaulting to production.
67 */
68 export function createComposioClientFromEnv(
69 environment: Readonly<Record<string, string | undefined>>,
70 options: ComposioClientOptions = {}
71 ): Composio {
72 return Composio.fromEnv(environment, clientOverrides(options));
73 }
74
75 interface ListOptions {
76 pageSize?: number;
77 /** Aborts the whole traversal, not one page. */
78 signal?: AbortSignal;
79 }
80
81 /** Every tool of a toolkit's latest version, with pricing. */
82 export function listToolkitTools(
83 client: Composio,
84 toolkitSlug: string,
85 { pageSize = TOOL_PAGE_SIZE, signal }: ListOptions = {}
86 ) {
87 return paginate(({ cursor }) =>
88 client.tools.list(
89 {
90 toolkit_slug: toolkitSlug.toUpperCase(),
91 toolkit_versions: 'latest',
92 include_pricing: true,
93 limit: pageSize,
94 cursor,
95 },
96 { signal }
97 )
98 ).toArray();
99 }
100
101 /** Every trigger type of a toolkit's latest version. */
102 export function listToolkitTriggers(
103 client: Composio,
104 toolkitSlug: string,
105 { pageSize = TRIGGER_PAGE_LIMIT, signal }: ListOptions = {}
106 ) {
107 return paginate(({ cursor }) =>
108 client.triggersTypes.list(
109 { toolkit_slugs: [toolkitSlug.toUpperCase()], toolkit_versions: 'latest', limit: pageSize, cursor },
110 { signal }
111 )
112 ).toArray();
113 }
114
115 /** Every toolkit in the catalog. Instant eligibility arrives without `include_pricing`. */
116 export function listToolkits(
117 client: Composio,
118 { pageSize = TOOLKIT_PAGE_SIZE, signal }: ListOptions = {}
119 ) {
120 return paginate(({ cursor }) =>
121 client.toolkits.list({ limit: pageSize, cursor }, { signal })
122 ).toArray();
123 }