@devries-cameron20 / composio-1 / commits / cbcdecf3a
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.

perf(cli): skip the tokenizer for run-origin executes and count tokens once

Check the invocation origin before building the tokenizer, since composio run always prints inline, and pass the token count from the threshold check into the stored-output summary instead of encoding the payload twice. Add a test for a response past the byte pre-filter but under the token threshold, and drop the unused ts-morph dependency.

jkomyno committed Sep 14, 2026 at 14:03 UTC cbcdecf3a956b100279867ee7dd154df418d7d31
4 files changed +62 -38
pnpm-lock.yaml
-30
@@ -1257,9 +1257,6 @@ importers:
1257 superjson:
1258 specifier: ^2.2.6
1259 version: 2.2.6
1260 - ts-morph:
1261 - specifier: ^28.0.0
1262 - version: 28.0.0
1260 zod:
1261 specifier: 'catalog:'
1262 version: 4.5.4
@@ -3770,9 +3767,6 @@ packages:
3767 '@standard-schema/spec@1.1.0':
3768 resolution: {integrity: sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==}
3769
3773 - '@ts-morph/common@0.29.0':
3774 - resolution: {integrity: sha512-35oUmphHbJvQ/+UTwFNme/t2p3FoKiGJ5auTjjpNTop2dyREspirjMy82PLSC1pnDJ8ah1GU98hwpVt64YXQsg==}
3775 -
3770 '@turbo/darwin-64@2.10.12':
3771 resolution: {integrity: sha512-9nKgKoF6ZOUsM+or0OtNf+TTJSfGvDNP7ZFv/ZGWVwOSCkumyctQiTeHwB4UNljHTnC41AqylgbunLDHoccNrA==}
3772 cpu: [x64]
@@ -4417,9 +4411,6 @@ packages:
4411 cliui@7.0.4:
4412 resolution: {integrity: sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==}
4413
4420 - code-block-writer@13.0.3:
4421 - resolution: {integrity: sha512-Oofo0pq3IKnsFtuHqSF7TqBfr71aeyZDVJ0HpmqB7FBM2qEigL0iPONSCZSO9pE9dZTAxANe5XHG9Uy0YMv8cg==}
4422 -
4414 color-convert@2.0.1:
4415 resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==}
4416 engines: {node: '>=7.0.0'}
@@ -5614,9 +5605,6 @@ packages:
5605 partial-json@0.1.7:
5606 resolution: {integrity: sha512-Njv/59hHaokb/hRUjce3Hdv12wd60MtM9Z5Olmn+nehe0QDAsRtRbJPvJ0Z91TusF0SuZRIvnM+S4l6EIP8leA==}
5607
5617 - path-browserify@1.0.1:
5618 - resolution: {integrity: sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==}
5619 -
5608 path-key@3.1.1:
5609 resolution: {integrity: sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==}
5610 engines: {node: '>=8'}
@@ -6051,9 +6039,6 @@ packages:
6039 ts-algebra@2.0.0:
6040 resolution: {integrity: sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==}
6041
6054 - ts-morph@28.0.0:
6055 - resolution: {integrity: sha512-Wp3tnZ2bzwxyTZMtgWVzXDfm7lB1Drz+y9DmmYH/L702PQhPyVrp3pkou3yIz4qjS14GY9kcpmLiOOMvl8oG1g==}
6056 -
6042 tsdown@0.23.0:
6043 resolution: {integrity: sha512-BaT+ep1xnj5hdyICLd5r5SYYE+TXnI1ATePLykv9vcKpHpFTWUWRH+x1AF/E2qcu3YB6+vI8IdyxIIIffEqw5Q==}
6044 engines: {node: ^22.18.0 || ^24.11.0 || >=26.0.0}
@@ -8603,12 +8588,6 @@ snapshots:
8588
8589 '@standard-schema/spec@1.1.0': {}
8590
8606 - '@ts-morph/common@0.29.0':
8607 - dependencies:
8608 - minimatch: 10.2.5
8609 - path-browserify: 1.0.1
8610 - tinyglobby: 0.2.17
8611 -
8591 '@turbo/darwin-64@2.10.12':
8592 optional: true
8593
@@ -9130,8 +9109,6 @@ snapshots:
9109 strip-ansi: 6.0.1
9110 wrap-ansi: 7.0.0
9111
9133 - code-block-writer@13.0.3: {}
9134 -
9112 color-convert@2.0.1:
9113 dependencies:
9114 color-name: 1.1.4
@@ -10470,8 +10447,6 @@ snapshots:
10447
10448 partial-json@0.1.7: {}
10449
10473 - path-browserify@1.0.1: {}
10474 -
10450 path-key@3.1.1: {}
10451
10452 path-key@4.0.0: {}
@@ -10985,11 +10960,6 @@ snapshots:
10960
10961 ts-algebra@2.0.0: {}
10962
10988 - ts-morph@28.0.0:
10989 - dependencies:
10990 - '@ts-morph/common': 0.29.0
10991 - code-block-writer: 13.0.3
10992 -
10963 tsdown@0.23.0(@arethetypeswrong/core@0.18.5)(@typescript/typescript6@6.0.2)(publint@0.3.24)(tsx@4.23.13)(unrun@0.2.27(@emnapi/core@1.11.2)(@emnapi/runtime@1.11.3)):
10964 dependencies:
10965 cac: 7.0.0
ts/packages/cli/package.json
-1
@@ -90,7 +90,6 @@
90 "semver": "^7.8.5",
91 "source-map-js": "^1.2.1",
92 "superjson": "^2.2.6",
93 - "ts-morph": "^28.0.0",
93 "zod": "catalog:"
94 },
95 "gitHead": "4fae6e54d5c150fba955cc5fa314281da5a1e064",
ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
+23 -7
@@ -332,14 +332,15 @@ const getExecuteOutputEncoder = () => {
332 return executeOutputEncoder;
333 };
334
335 +const countOutputTokens = (json: string): number => getExecuteOutputEncoder().encode(json).length;
336 +
337 // A BPE token always covers at least one UTF-8 byte, so a payload of at most
338 // THRESHOLD bytes can never exceed THRESHOLD tokens. Checking the byte length
339 // first keeps the common (small) response off the tokenizer entirely: building
340 // the o200k rank table measured ~390ms in a compiled binary, against ~4ms to
341 // encode a 7.5KB payload once it exists, and microseconds to measure the bytes.
340 -const exceedsInlineOutputThreshold = (json: string): boolean =>
341 - new TextEncoder().encode(json).length > EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD &&
342 - getExecuteOutputEncoder().encode(json).length > EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD;
342 +const mayExceedInlineOutputThreshold = (json: string): boolean =>
343 + new TextEncoder().encode(json).length > EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD;
344
345 const shouldStoreLargeExecuteOutput = APP_CONFIG.CLI_INVOCATION_ORIGIN.pipe(
346 Effect.orDie,
@@ -388,7 +389,12 @@ const executionSuccessSuffix = (result: {
389 return metadata.length > 0 ? ` (${metadata.join(', ')})` : '';
390 };
391
391 -const persistLargeExecuteOutput = (toolSlug: string, json: string, sharedDirectory?: string) =>
392 +const persistLargeExecuteOutput = (
393 + toolSlug: string,
394 + json: string,
395 + tokenCount: number,
396 + sharedDirectory?: string
397 +) =>
398 Effect.gen(function* () {
399 const runOutputDirectory = yield* APP_CONFIG.RUN_OUTPUT_DIR;
400 const outputFilePath = yield* storeCliSessionArtifact({
@@ -403,7 +409,7 @@ const persistLargeExecuteOutput = (toolSlug: string, json: string, sharedDirecto
409 error: null,
410 logId: '',
411 storedInFile: true,
406 - tokenCount: getExecuteOutputEncoder().encode(json).length,
412 + tokenCount,
413 outputFilePath: outputFilePath ?? '(could not write to disk)',
414 } satisfies StoredExecuteOutputSummary;
415 });
@@ -417,7 +423,17 @@ const prepareExecuteOutput = (
423 ) =>
424 Effect.gen(function* () {
425 const json = serializeExecuteOutput(result);
420 - if (!exceedsInlineOutputThreshold(json) || !(yield* shouldStoreLargeExecuteOutput)) {
426 + // `composio run` always prints inline, so its origin is checked before the
427 + // tokenizer is built: the count would be thrown away.
428 + if (!mayExceedInlineOutputThreshold(json) || !(yield* shouldStoreLargeExecuteOutput)) {
429 + return {
430 + kind: 'inline',
431 + json,
432 + } satisfies PreparedExecuteOutput;
433 + }
434 +
435 + const tokenCount = countOutputTokens(json);
436 + if (tokenCount <= EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD) {
437 return {
438 kind: 'inline',
439 json,
@@ -427,7 +443,7 @@ const prepareExecuteOutput = (
443 return {
444 kind: 'file',
445 summary: {
430 - ...(yield* persistLargeExecuteOutput(toolSlug, json, sharedDirectory)),
446 + ...(yield* persistLargeExecuteOutput(toolSlug, json, tokenCount, sharedDirectory)),
447 logId: result.logId,
448 } satisfies StoredExecuteOutputSummary,
449 } satisfies PreparedExecuteOutput;
ts/packages/cli/test/src/commands/tools/tools.execute.cmd.test.ts
+39
@@ -1152,6 +1152,45 @@ describe('CLI: composio execute', () => {
1152 );
1153 });
1154
1155 + layer(
1156 + TestLive({
1157 + baseConfigProvider: largeOutputConfigProvider,
1158 + fixture: 'global-test-user-id',
1159 + stdin: { isTTY: true, data: '' },
1160 + toolsExecutor: {
1161 + respondWith: {
1162 + data: {
1163 + // ~18KB that o200k encodes in ~4k tokens: past the byte pre-filter,
1164 + // under the token threshold.
1165 + content: 'composio '.repeat(2_000),
1166 + },
1167 + error: null,
1168 + successful: true,
1169 + logId: 'log_dense_output',
1170 + },
1171 + },
1172 + })
1173 + )(
1174 + '[Given] a response over 10KB that stays under the token threshold [Then] it prints inline',
1175 + it => {
1176 + it.effect('does not store the payload in a file', () =>
1177 + Effect.gen(function* () {
1178 + yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
1179 + const lines = yield* MockConsole.getLines({ stripAnsi: true });
1180 + const output = parseLastJson(lines) as unknown as {
1181 + successful: boolean;
1182 + storedInFile?: boolean;
1183 + data: { content: string };
1184 + };
1185 +
1186 + expect(output.successful).toBe(true);
1187 + expect(output.storedInFile).toBeUndefined();
1188 + expect(output.data.content).toHaveLength(18_000);
1189 + })
1190 + );
1191 + }
1192 + );
1193 +
1194 layer(
1195 TestLive({
1196 baseConfigProvider: testConfigProvider,