@devries-cameron20 / composio Mirror · read-only / commits / 3b18bf38e
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, 12:15 UTC

perf(cli): defer the TypeScript compiler and generation pipeline

`composio --version` drops from 622ms to 408ms, and eager module evaluation from 363.8ms to 130.0ms, measured on the pinned toolchain (Bun 1.4.1+4661e494f, linux-x64, best of 7, analytics disabled). `commands/index.ts` builds the root command tree from every `.cmd.ts` module, so evaluating any one command evaluated all of them. Two of those modules reached the TypeScript compiler and the code generation pipeline, which nothing but `composio generate` and `composio run` ever calls: 155.8ms -> 8.0ms commands/run.cmd 63.5ms -> 2.5ms commands/generate The command tree itself is untouched. `Command.withHandler(input => Effect)` already runs lazily, so moving these imports inside the handler bodies is enough. Specs, flags, descriptions, subcommand wiring and `root-help.ts` introspection all still resolve eagerly, which is why parsing, help rendering and "did you mean" suggestions cannot shift. run.cmd.ts was the CLI's only consumer of `import ts from 'typescript'`, through three source rewrites that `composio run` applies to a user script. Those move to `run-source-transforms.ts`, which the handler imports dynamically. The test suite imports them from the new path. ts.generate.cmd.ts and py.generate.cmd.ts pulled `src/generation/*` at module scope. Both now resolve it inside the handler, immediately before the first use. A rejected import of a module bundled into this binary is an impossible invariant rather than a recoverable failure, so these use `Effect.promise` rather than `Effect.tryPromise`. The module registry memoizes each import, so repeat calls within one run cost nothing. Behavior is unchanged, checked rather than assumed. Eleven invocations, covering `--help` at root and for generate, generate ts, generate py, run, tools and execute, plus `version`, `--version`, an unknown command and an unknown flag, produce byte-identical stdout, stderr and exit codes before and after. Verified with typecheck (src and test), oxlint, validate:boundaries, validate:skills, and the full package suite: 1326 passed, 1 skipped. `test/src/cli-main.test.ts` times out in this container and does so identically on the parent commit (25.6s and 25.2s there, 24.5s and 24.3s here) because it spawns the CLI from source against a 15s timeout. Not caused by this change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EzaE7oGVgziJ5nRvBhcci2

Claude committed Sep 13, 2026 at 05:59 UTC 3b18bf38eb6d6d4f98ffb1992df695ab238be79a
5 files changed +159 -95
ts/packages/cli/src/commands/py/commands/py.generate.cmd.ts
+18 -3
@@ -6,10 +6,7 @@ import { ComposioToolkitsRepository } from 'src/services/composio-clients';
6 import { logMetrics } from 'src/effects/log-metrics';
7 import type { GetCmdParams } from 'src/type-utils';
8 import { NodeProcess } from 'src/services/node-process';
9 -import { createToolkitIndex } from 'src/generation/create-toolkit-index';
9 import { pyFindComposioCoreGenerated } from 'src/effects/find-composio-core-generated';
11 -import { BANNER } from 'src/generation/constants';
12 -import { generatePythonSources } from 'src/generation/python/generate';
10 import {
11 getToolkitVersionOverrides,
12 type ToolkitVersionOverrides,
@@ -61,6 +58,23 @@ const _pyCmd$Generate = Command.make('generate', { outputOpt, toolkitsOpt }).pip
58
59 export const pyCmd$Generate = _pyCmd$Generate.pipe(Command.withHandler(generatePythonTypeStubs));
60
61 +/**
62 + * Deferred for the same reason as the TypeScript pipeline: only
63 + * `composio generate` needs it, so the import stays inside the handler instead
64 + * of on every command's startup path.
65 + */
66 +const loadPythonGeneration = Effect.promise(() =>
67 + Promise.all([
68 + import('src/generation/create-toolkit-index'),
69 + import('src/generation/python/generate'),
70 + import('src/generation/constants'),
71 + ]).then(([toolkitIndex, generate, constants]) => ({
72 + createToolkitIndex: toolkitIndex.createToolkitIndex,
73 + generatePythonSources: generate.generatePythonSources,
74 + BANNER: constants.BANNER,
75 + }))
76 +);
77 +
78 export function generatePythonTypeStubs({
79 outputOpt,
80 toolkitsOpt,
@@ -171,6 +185,7 @@ export function generatePythonTypeStubs({
185 const typeableTools = { withTypes: false as const, tools };
186
187 yield* spinner.message('Generating Python type stubs...');
188 + const { createToolkitIndex, generatePythonSources, BANNER } = yield* loadPythonGeneration;
189 const index = createToolkitIndex({ toolkits, typeableTools, triggerTypes, versionMap });
190
191 // Generate Python sources
ts/packages/cli/src/commands/run-source-transforms.ts new
+97
@@ -0,0 +1,97 @@
1 +/**
2 + * Source rewrites that `composio run` applies to a user script before handing it
3 + * to Bun: slug extraction for definition warming, and the two wrappers that turn
4 + * a trailing expression into a return value.
5 + *
6 + * These live outside `run.cmd.ts` because they are the CLI's only consumer of
7 + * the TypeScript compiler, which costs roughly 95ms to evaluate. `run.cmd.ts`
8 + * imports this module dynamically from inside its handler, so a `composio
9 + * execute` or `composio whoami` invocation never pays for it.
10 + */
11 +import ts from 'typescript';
12 +
13 +export const extractInlineExecuteToolSlugs = (source: string): ReadonlyArray<string> => {
14 + if (!source.trim()) {
15 + return [];
16 + }
17 +
18 + const parsed = ts.createSourceFile(
19 + 'composio-run-inline.ts',
20 + source,
21 + ts.ScriptTarget.Latest,
22 + true,
23 + ts.ScriptKind.TSX
24 + );
25 + const slugs = new Set<string>();
26 +
27 + const visit = (node: ts.Node) => {
28 + if (
29 + ts.isCallExpression(node) &&
30 + ts.isIdentifier(node.expression) &&
31 + node.expression.text === 'execute'
32 + ) {
33 + const [slugArg] = node.arguments;
34 + if (slugArg && ts.isStringLiteralLike(slugArg)) {
35 + slugs.add(slugArg.text);
36 + }
37 + }
38 +
39 + ts.forEachChild(node, visit);
40 + };
41 +
42 + visit(parsed);
43 + return [...slugs];
44 +};
45 +
46 +export const wrapInlineCodeForRun = (source: string): string => {
47 + const parsed = ts.createSourceFile(
48 + 'composio-run-inline.ts',
49 + source,
50 + ts.ScriptTarget.Latest,
51 + true,
52 + ts.ScriptKind.TSX
53 + );
54 + const statements = [...parsed.statements];
55 + if (statements.length === 0) {
56 + return source;
57 + }
58 +
59 + const lastStatement = statements.at(-1);
60 + if (!lastStatement || !ts.isExpressionStatement(lastStatement)) {
61 + return source;
62 + }
63 +
64 + const prefix = source.slice(0, lastStatement.getFullStart());
65 + const suffix = source.slice(lastStatement.getEnd());
66 + const expressionText = lastStatement.expression.getText(parsed);
67 + return `${prefix}return (${expressionText});${suffix}`;
68 +};
69 +
70 +export const wrapFileSourceForRun = (source: string): string => {
71 + const parsed = ts.createSourceFile(
72 + 'composio-run-file.ts',
73 + source,
74 + ts.ScriptTarget.Latest,
75 + true,
76 + ts.ScriptKind.TSX
77 + );
78 + const statements = [...parsed.statements];
79 + const firstNonImportIndex = statements.findIndex(statement => !ts.isImportDeclaration(statement));
80 + if (firstNonImportIndex === -1) {
81 + return source;
82 + }
83 +
84 + const bodyStart = statements[firstNonImportIndex]!.getFullStart();
85 + const importPrefix = source.slice(0, bodyStart);
86 + const body = source.slice(bodyStart);
87 + return [
88 + importPrefix,
89 + 'const __composioResult = await (async () => {',
90 + wrapInlineCodeForRun(body),
91 + '})();',
92 + 'if (__composioResult !== undefined) {',
93 + ' console.log(__composioResult);',
94 + '}',
95 + '',
96 + ].join('\n');
97 +};
ts/packages/cli/src/commands/run.cmd.ts
+12 -86
@@ -4,7 +4,6 @@ import * as FileSystem from 'effect/FileSystem';
4 import * as Path from 'effect/Path';
5 import { ChildProcess, ChildProcessSpawner } from 'effect/unstable/process';
6 import { Context, Data, Deferred, Duration, Effect, MutableRef, Option, Result } from 'effect';
7 -import ts from 'typescript';
7 import { APP_VERSION } from 'src/constants';
8 import { APP_CONFIG, UNPREFIXED_CONFIG } from 'src/effects/app-config';
9 import { resolveCommandProject } from 'src/services/command-project';
@@ -105,91 +104,16 @@ export const RunPassthroughArgs = Context.Reference<ReadonlyArray<string> | unde
104 { defaultValue: () => undefined }
105 );
106
108 -export const extractInlineExecuteToolSlugs = (source: string): ReadonlyArray<string> => {
109 - if (!source.trim()) {
110 - return [];
111 - }
112 -
113 - const parsed = ts.createSourceFile(
114 - 'composio-run-inline.ts',
115 - source,
116 - ts.ScriptTarget.Latest,
117 - true,
118 - ts.ScriptKind.TSX
119 - );
120 - const slugs = new Set<string>();
121 -
122 - const visit = (node: ts.Node) => {
123 - if (
124 - ts.isCallExpression(node) &&
125 - ts.isIdentifier(node.expression) &&
126 - node.expression.text === 'execute'
127 - ) {
128 - const [slugArg] = node.arguments;
129 - if (slugArg && ts.isStringLiteralLike(slugArg)) {
130 - slugs.add(slugArg.text);
131 - }
132 - }
133 -
134 - ts.forEachChild(node, visit);
135 - };
136 -
137 - visit(parsed);
138 - return [...slugs];
139 -};
140 -
141 -export const wrapInlineCodeForRun = (source: string): string => {
142 - const parsed = ts.createSourceFile(
143 - 'composio-run-inline.ts',
144 - source,
145 - ts.ScriptTarget.Latest,
146 - true,
147 - ts.ScriptKind.TSX
148 - );
149 - const statements = [...parsed.statements];
150 - if (statements.length === 0) {
151 - return source;
152 - }
153 -
154 - const lastStatement = statements.at(-1);
155 - if (!lastStatement || !ts.isExpressionStatement(lastStatement)) {
156 - return source;
157 - }
158 -
159 - const prefix = source.slice(0, lastStatement.getFullStart());
160 - const suffix = source.slice(lastStatement.getEnd());
161 - const expressionText = lastStatement.expression.getText(parsed);
162 - return `${prefix}return (${expressionText});${suffix}`;
163 -};
164 -
165 -export const wrapFileSourceForRun = (source: string): string => {
166 - const parsed = ts.createSourceFile(
167 - 'composio-run-file.ts',
168 - source,
169 - ts.ScriptTarget.Latest,
170 - true,
171 - ts.ScriptKind.TSX
172 - );
173 - const statements = [...parsed.statements];
174 - const firstNonImportIndex = statements.findIndex(statement => !ts.isImportDeclaration(statement));
175 - if (firstNonImportIndex === -1) {
176 - return source;
177 - }
178 -
179 - const bodyStart = statements[firstNonImportIndex]!.getFullStart();
180 - const importPrefix = source.slice(0, bodyStart);
181 - const body = source.slice(bodyStart);
182 - return [
183 - importPrefix,
184 - 'const __composioResult = await (async () => {',
185 - wrapInlineCodeForRun(body),
186 - '})();',
187 - 'if (__composioResult !== undefined) {',
188 - ' console.log(__composioResult);',
189 - '}',
190 - '',
191 - ].join('\n');
192 -};
107 +/**
108 + * `run-source-transforms` pulls in the TypeScript compiler, roughly 95ms of
109 + * module evaluation. Only `composio run` rewrites a script, so the import stays
110 + * inside the handler rather than on every command's startup path. A rejected
111 + * import of a module bundled into this binary is an impossible invariant, not a
112 + * recoverable failure, which is why this is `Effect.promise` and not
113 + * `Effect.tryPromise`. The module registry memoizes the import, so repeat calls
114 + * within one run cost nothing.
115 + */
116 +const loadSourceTransforms = Effect.promise(() => import('./run-source-transforms'));
117
118 export const inferCliInvocationPrefix = (
119 path: Path.Path,
@@ -323,6 +247,7 @@ export const buildRunCommand = ({
247 }) =>
248 Effect.gen(function* () {
249 const fs = yield* FileSystem.FileSystem;
250 + const { wrapFileSourceForRun, wrapInlineCodeForRun } = yield* loadSourceTransforms;
251 // Use process.execPath directly — the child is spawned with BUN_BE_BUN=1
252 // which makes compiled Bun binaries act as a plain Bun runtime.
253 // Avoid the `run` subcommand entirely since Bun intercepts it as its own
@@ -621,6 +546,7 @@ export const runCmd = Command.make('run', {
546 const telemetryDebug = yield* isTelemetryDebugEnabled;
547 if (Option.isNone(file)) {
548 const [inlineCode] = args;
549 + const { extractInlineExecuteToolSlugs } = yield* loadSourceTransforms;
550 const preloadSlugs = extractInlineExecuteToolSlugs(inlineCode ?? '');
551 if (preloadSlugs.length > 0) {
552 yield* warmToolInputDefinitions(preloadSlugs).pipe(
ts/packages/cli/src/commands/ts/commands/ts.generate.cmd.ts
+28 -4
@@ -26,12 +26,8 @@ import {
26 } from 'src/services/composio-clients';
27 import { logMetrics } from 'src/effects/log-metrics';
28 import { NodeProcess } from 'src/services/node-process';
29 -import { createToolkitIndex } from 'src/generation/create-toolkit-index';
29 import type { GetCmdParams } from 'src/type-utils';
31 -import { generateTypeScriptSources } from 'src/generation/typescript/generate';
30 import { jsFindComposioCoreGenerated } from 'src/effects/find-composio-core-generated';
33 -import { transpileTypeScriptSources } from 'src/generation/typescript/transpile';
34 -import { BANNER } from 'src/generation/constants';
31 import type { Toolkit } from 'src/models/toolkits';
32 import type { TriggerType } from 'src/models/trigger-types';
33 import type { Tool, ToolsAsEnums } from 'src/models/tools';
@@ -365,6 +361,28 @@ export const handleTsGenerate = (params: GetCmdParams<typeof _tsCmd$Generate>) =
361 });
362 };
363
364 +/**
365 + * The generation pipeline pulls in the TypeScript compiler, together roughly
366 + * 165ms of module evaluation, and only `composio generate` uses it. Importing it
367 + * from inside the handler keeps it off every other command's startup path. A
368 + * rejected import of a module bundled into this binary is an impossible
369 + * invariant rather than a recoverable failure, so this is `Effect.promise`. The
370 + * module registry memoizes the import.
371 + */
372 +const loadTypeScriptGeneration = Effect.promise(() =>
373 + Promise.all([
374 + import('src/generation/create-toolkit-index'),
375 + import('src/generation/typescript/generate'),
376 + import('src/generation/typescript/transpile'),
377 + import('src/generation/constants'),
378 + ]).then(([toolkitIndex, generate, transpile, constants]) => ({
379 + createToolkitIndex: toolkitIndex.createToolkitIndex,
380 + generateTypeScriptSources: generate.generateTypeScriptSources,
381 + transpileTypeScriptSources: transpile.transpileTypeScriptSources,
382 + BANNER: constants.BANNER,
383 + }))
384 +);
385 +
386 export function generateTypescriptTypeStubs({
387 outputOpt,
388 compact,
@@ -420,6 +438,12 @@ export function generateTypescriptTypeStubs({
438 : fetchAllData(client, typeTools, validatedOverrides, spinner);
439
440 yield* spinner.message('Generating TypeScript type stubs...');
441 + const {
442 + createToolkitIndex,
443 + generateTypeScriptSources,
444 + transpileTypeScriptSources,
445 + BANNER,
446 + } = yield* loadTypeScriptGeneration;
447 const index = createToolkitIndex({ toolkits, typeableTools, triggerTypes, versionMap });
448
449 // Generate TypeScript sources
ts/packages/cli/test/src/commands/run.cmd.test.ts
+4 -2
@@ -11,11 +11,13 @@ import { afterEach, it, vi } from 'vitest';
11 import { createCliCommandTelemetryContext } from 'src/analytics/events';
12 import {
13 buildRunHelpersSource,
14 - extractInlineExecuteToolSlugs,
14 inferCliInvocationPrefix,
15 MissingRunSourceError,
17 - wrapInlineCodeForRun,
16 } from 'src/commands/run.cmd';
17 +import {
18 + extractInlineExecuteToolSlugs,
19 + wrapInlineCodeForRun,
20 +} from 'src/commands/run-source-transforms';
21 import {
22 RUN_COMPANION_MODULE_FILENAMES,
23 hasInstalledRunCompanionModules,