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
mjs 133 lines 4.45 KB
Raw
1 import { readFileSync } from 'node:fs';
2 import { dirname, join, resolve } from 'node:path';
3 import { fileURLToPath } from 'node:url';
4 import { HarnessError } from './errors.mjs';
5
6 const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
7
8 export const BROWSER_GRANT_TOOLKITS = [
9 { exportPrefix: 'GMAIL', slug: 'gmail' },
10 { slug: 'googledrive' },
11 { exportPrefix: 'GITHUB', slug: 'github' },
12 { exportPrefix: 'SLACK', slug: 'slack' },
13 ];
14
15 export const DEMO_TOOLKIT = {
16 exportPrefix: 'APIKEY',
17 slug: 'serpapi',
18 demoValue: 'examples-demo-key',
19 };
20
21 const parseCsv = value =>
22 value
23 ? value
24 .split(',')
25 .map(part => part.trim())
26 .filter(Boolean)
27 : [];
28
29 const toolkitForId = id => {
30 for (const toolkit of [...BROWSER_GRANT_TOOLKITS, DEMO_TOOLKIT]) {
31 if (toolkit.exportPrefix && id.startsWith(`COMPOSIO_EXAMPLES_${toolkit.exportPrefix}_`)) {
32 return toolkit.slug;
33 }
34 }
35 return undefined;
36 };
37
38 export const entryToolkits = entry => {
39 const toolkits = new Set(entry.toolkits ?? []);
40 for (const id of entry.ids ?? []) {
41 const toolkit = toolkitForId(id);
42 if (toolkit) toolkits.add(toolkit);
43 }
44 return [...toolkits];
45 };
46
47 export const validateManifestEntries = entries => {
48 for (const entry of entries) {
49 if (!entry.id || !entry.lang || !entry.file || !entry.tier) {
50 throw new HarnessError(`manifest entry missing required fields: ${JSON.stringify(entry)}`);
51 }
52 if (entry.lang === 'ts' && entry.tier !== 'X' && !entry.pkg) {
53 throw new HarnessError(`ts entry ${entry.id} missing pkg`);
54 }
55 if (entry.tier === '3' && !entry.readiness) {
56 throw new HarnessError(`tier-3 entry ${entry.id} missing readiness regex`);
57 }
58 }
59 return entries;
60 };
61
62 export const loadManifest = () =>
63 validateManifestEntries(
64 JSON.parse(readFileSync(join(ROOT, 'examples-manifest.json'), 'utf8')).entries
65 );
66
67 export const selectManifestEntries = (
68 entries,
69 { lang, ids, tiers = '1,2,3', excludeToolkits } = {}
70 ) => {
71 const selectedIds = new Set(parseCsv(ids));
72 const selectedTiers = new Set(parseCsv(tiers));
73 const excludedToolkitSet = new Set(parseCsv(excludeToolkits));
74 const eligibleEntries = entries.filter(
75 entry =>
76 entry.tier !== 'X' &&
77 selectedTiers.has(entry.tier) &&
78 (!lang || entry.lang === lang) &&
79 (selectedIds.size === 0 || selectedIds.has(entry.id))
80 );
81 const excludedEntries = eligibleEntries.filter(entry =>
82 entryToolkits(entry).some(toolkit => excludedToolkitSet.has(toolkit))
83 );
84 const excludedIds = new Set(excludedEntries.map(entry => entry.id));
85
86 return {
87 entries: eligibleEntries.filter(entry => !excludedIds.has(entry.id)),
88 excludedEntries,
89 excludedToolkits: [...excludedToolkitSet],
90 };
91 };
92
93 // Selection flags shared by harness/run.mjs and scripts/examples-provision.mjs:
94 // [--lang ts|py] [--ids a,b] [--tiers 1,2,3] [--exclude-toolkits a,b]
95 export const parseOption = (argv, name, fallback) => {
96 const index = argv.indexOf(`--${name}`);
97 return index >= 0 && argv[index + 1] !== undefined ? argv[index + 1] : fallback;
98 };
99
100 export const parseSelectionOptions = argv => ({
101 lang: parseOption(argv, 'lang'),
102 ids: parseOption(argv, 'ids'),
103 tiers: parseOption(argv, 'tiers', '1,2,3'),
104 excludeToolkits: parseOption(argv, 'exclude-toolkits'),
105 });
106
107 // One line per excluded entry, preceded by a summary; empty when nothing was excluded.
108 export const describeExclusions = selection => {
109 if (selection.excludedEntries.length === 0) return [];
110 return [
111 `excluding ${selection.excludedEntries.length} entries requiring ${selection.excludedToolkits.join(', ')}:`,
112 ...selection.excludedEntries.map(entry => ` - ${entry.id}`),
113 ];
114 };
115
116 // Names the exclusion as the cause when it emptied the selection, so an
117 // operator is not left with a bare "no entries selected".
118 export const emptySelectionMessage = (selection, subject = 'entries') => {
119 const excluded = selection.excludedEntries;
120 if (excluded.length === 0) return `no ${subject} selected`;
121 return (
122 `no ${subject} selected: --exclude-toolkits ${selection.excludedToolkits.join(',')} ` +
123 `removed all ${excluded.length} matching ${subject} (${excluded.map(entry => entry.id).join(', ')})`
124 );
125 };
126
127 export const requiredBrowserGrantToolkits = entries =>
128 BROWSER_GRANT_TOOLKITS.filter(toolkit =>
129 entries.some(entry => entryToolkits(entry).includes(toolkit.slug))
130 );
131
132 export const requiresDemoToolkit = entries =>
133 entries.some(entry => entryToolkits(entry).includes(DEMO_TOOLKIT.slug));