@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, 03:37 UTC

next
ts 144 lines 6.02 KB
Raw
1 /**
2 * Cumulative stages of the local-workbench PR reviewer's host orchestration
3 * file, built up one piece at a time. Each stage's `code` is the full file at
4 * that point; <FileBuildup> diffs consecutive stages so the reader watches
5 * `src/runner.ts` grow from a bare client into the full run loop.
6 *
7 * These are *teaching* stages: distilled and inlined from the real source
8 * (src/runner.ts + src/workbench.ts + src/sandbox/e2b.ts) so each Composio
9 * concept reads on its own, the way slack-bot-build.ts simplifies the Slack bot.
10 *
11 * Source: ComposioHQ/local-pr-reviewer @ c3baff810a6b8dbf1e2c1eff45ddc9785998e3e8
12 * Refresh this map (and local-workbench-source.json) when that repo changes.
13 *
14 * This data is rendered via @pierre/diffs, NOT twoslash, so it is not
15 * type-checked, because it deliberately uses the unreleased
16 * experimental_createLocalWorkbenchSession export.
17 */
18 import type { BuildStage } from './slack-bot-build';
19
20 const imports = `import { Composio } from '@composio/core';
21 import { experimental_createLocalWorkbenchSession } from '@composio/experimental/workbench';
22 import { createE2bSandbox } from './sandbox/e2b';
23
24 const composio = new Composio({ apiKey: process.env.COMPOSIO_API_KEY });
25 const userId = process.env.COMPOSIO_USER_ID ?? 'local-pr-reviewer-user';
26 `;
27
28 const stage1 = imports;
29
30 const stage2 = `${imports}
31 // Composio runs tools as a user. Before anything else, make sure this user has
32 // an active GitHub connection. There's no point booting a sandbox without one.
33 async function requireGithubConnection() {
34 const list = await composio.connectedAccounts.list({
35 userIds: [userId],
36 toolkitSlugs: ['github'],
37 statuses: ['ACTIVE'],
38 });
39 if (list.items?.[0]) return;
40
41 const request = await composio.toolkits.authorize(userId, 'github');
42 throw new Error(\`Connect GitHub first: \${request.redirectUrl}\`);
43 }
44 `;
45
46 const stage3 = `${stage2}
47 // The local sandbox session. Create a normal Composio session yourself with
48 // \`workbench.enable: false\` (Composio won't run code for you), then hand that
49 // session to the helper, which validates it's local and gives you the pieces to
50 // run code yourself, wherever you choose.
51 async function createWorkbench() {
52 const session = await composio.create(userId, {
53 toolkits: ['github'],
54 workbench: { enable: false },
55 });
56 return experimental_createLocalWorkbenchSession(composio, session);
57 // returns { helperSource, env }:
58 // helperSource: a Python helper exposing run_composio_tool / invoke_llm / web_search
59 // env: the variables that helper needs to reach Composio from inside your box
60 }
61 `;
62
63 const stage4 = `${stage3}
64 export async function runReview(repo: string, pr: number) {
65 await requireGithubConnection();
66 const workbench = await createWorkbench();
67
68 // Start a sandbox you own, inject the helper, and pass the env. E2B is just
69 // the sample runner; swap createE2bSandbox for any box that honors the same
70 // contract: write a file, set env, run a command, stream output, tear down.
71 const sandbox = await createE2bSandbox({
72 apiKey: process.env.E2B_API_KEY,
73 helperSource: workbench.helperSource, // written as composio_helper.py
74 env: workbench.env,
75 });
76 }
77 `;
78
79 const stage5 = `${stage3}
80 export async function runReview(repo: string, pr: number) {
81 await requireGithubConnection();
82 const workbench = await createWorkbench();
83
84 // Start a sandbox you own, inject the helper, and pass the env. E2B is just
85 // the sample runner; swap createE2bSandbox for any box that honors the same
86 // contract: write a file, set env, run a command, stream output, tear down.
87 const sandbox = await createE2bSandbox({
88 apiKey: process.env.E2B_API_KEY,
89 helperSource: workbench.helperSource, // written as composio_helper.py
90 env: workbench.env,
91 });
92
93 // Run the reviewer agent inside the sandbox and stream its output back. The
94 // agent calls run_composio_tool from composio_helper.py, which routes GitHub
95 // actions back through Composio under this user's connection.
96 const task = \`Review PR #\${pr} on \${repo}. Run the repo's real checks in this sandbox.\`;
97 try {
98 await sandbox.run('npx --yes tsx agent.ts', {
99 env: { ...workbench.env, TASK: task, OPENAI_API_KEY: process.env.OPENAI_API_KEY },
100 onStdout: (chunk) => process.stdout.write(chunk),
101 onStderr: (chunk) => process.stderr.write(chunk),
102 });
103 } finally {
104 await sandbox.teardown();
105 }
106 }
107 `;
108
109 const STAGES: { title: string; description: string; code: string }[] = [
110 {
111 title: 'Create the Composio client',
112 description:
113 'Start from scratch. One client, one stable user id. Everything below acts as this user, against the connections they own.',
114 code: stage1,
115 },
116 {
117 title: 'Check the GitHub connection',
118 description:
119 'A local sandbox still uses Composio for auth and tool discovery. Before booting any infrastructure, confirm this user has an active GitHub connection, and hand them a connect link if not.',
120 code: stage2,
121 },
122 {
123 title: 'Create the local sandbox session',
124 description:
125 'The whole idea. Create a Composio session yourself with workbench.enable: false (Composio will not run code for you), then hand that session to experimental_createLocalWorkbenchSession, which validates it is local and returns the helper source and env you run yourself.',
126 code: stage3,
127 },
128 {
129 title: 'Start your sandbox, inject the helper',
130 description:
131 'Boot a box you control, write helperSource into it as composio_helper.py, and pass env to the process. E2B is the sample runner; this is the only Composio-specific thing your sandbox must carry.',
132 code: stage4,
133 },
134 {
135 title: 'Run the reviewer and stream output',
136 description:
137 'Run the agent inside the sandbox. Whenever it calls run_composio_tool, the helper routes that GitHub action back through Composio under this user, so tool execution happens in your box, but discovery and auth stay managed.',
138 code: stage5,
139 },
140 ];
141
142 export const FILE_BUILDS: Record<string, { file: string; stages: BuildStage[] }> = {
143 reviewer: { file: 'src/runner.ts', stages: STAGES },
144 };