Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { Effect } from 'effect'; |
| 2 | import { APP_CONFIG } from 'src/config'; |
| 3 | import { storeCliSessionArtifact } from 'src/services/cli-session-artifacts'; |
| 4 | import type { InstantCharge, ToolExecuteResponse } from 'src/services/tools-executor'; |
| 5 | import { serializeExecuteOutput } from './tools.execute.format'; |
| 6 | |
| 7 | // Responses larger than this are stored in a session file instead of printed, |
| 8 | // so a caller such as an agent does not take a huge payload into its context. |
| 9 | // Measured in UTF-8 bytes: about 10,000 tokens of JSON at the estimate below. |
| 10 | const EXECUTE_INLINE_OUTPUT_BYTE_THRESHOLD = 40_000; |
| 11 | |
| 12 | // JSON averages roughly four bytes per token. The `tokenCount` of a stored |
| 13 | // response is this estimate rather than a tokenizer count, since the real count |
| 14 | // depends on the model that reads the file. |
| 15 | const ESTIMATED_BYTES_PER_OUTPUT_TOKEN = 4; |
| 16 | |
| 17 | const shouldStoreLargeExecuteOutput = APP_CONFIG.CLI_INVOCATION_ORIGIN.pipe( |
| 18 | Effect.orDie, |
| 19 | Effect.map(origin => origin !== 'run') |
| 20 | ); |
| 21 | |
| 22 | type StoredExecuteOutputSummary = { |
| 23 | readonly successful: true; |
| 24 | readonly error: null; |
| 25 | readonly logId: string; |
| 26 | readonly instantCharge?: InstantCharge; |
| 27 | readonly storedInFile: true; |
| 28 | readonly tokenCount: number; |
| 29 | readonly sizeBytes: number; |
| 30 | readonly outputFilePath: string; |
| 31 | }; |
| 32 | |
| 33 | type PreparedExecuteOutput = |
| 34 | | { readonly kind: 'inline'; readonly json: string } |
| 35 | | { readonly kind: 'file'; readonly summary: StoredExecuteOutputSummary }; |
| 36 | |
| 37 | const persistLargeExecuteOutput = ( |
| 38 | toolSlug: string, |
| 39 | json: string, |
| 40 | sizeBytes: number, |
| 41 | sharedDirectory?: string |
| 42 | ) => |
| 43 | Effect.gen(function* () { |
| 44 | const runOutputDirectory = yield* APP_CONFIG.RUN_OUTPUT_DIR; |
| 45 | const outputFilePath = yield* storeCliSessionArtifact({ |
| 46 | contents: json, |
| 47 | name: `${toolSlug}_OUTPUT`, |
| 48 | extension: 'json', |
| 49 | directoryPath: sharedDirectory?.trim() || runOutputDirectory, |
| 50 | }); |
| 51 | |
| 52 | return { |
| 53 | successful: true, |
| 54 | error: null, |
| 55 | logId: '', |
| 56 | storedInFile: true, |
| 57 | tokenCount: Math.ceil(sizeBytes / ESTIMATED_BYTES_PER_OUTPUT_TOKEN), |
| 58 | sizeBytes, |
| 59 | outputFilePath: outputFilePath ?? '(could not write to disk)', |
| 60 | } satisfies StoredExecuteOutputSummary; |
| 61 | }); |
| 62 | |
| 63 | export const prepareExecuteOutput = ( |
| 64 | toolSlug: string, |
| 65 | result: ToolExecuteResponse, |
| 66 | sharedDirectory?: string |
| 67 | ) => |
| 68 | Effect.gen(function* () { |
| 69 | const json = serializeExecuteOutput(result); |
| 70 | const sizeBytes = new TextEncoder().encode(json).length; |
| 71 | // `composio run` always prints inline. |
| 72 | if ( |
| 73 | sizeBytes <= EXECUTE_INLINE_OUTPUT_BYTE_THRESHOLD || |
| 74 | !(yield* shouldStoreLargeExecuteOutput) |
| 75 | ) { |
| 76 | return { |
| 77 | kind: 'inline', |
| 78 | json, |
| 79 | } satisfies PreparedExecuteOutput; |
| 80 | } |
| 81 | |
| 82 | return { |
| 83 | kind: 'file', |
| 84 | summary: { |
| 85 | ...(yield* persistLargeExecuteOutput(toolSlug, json, sizeBytes, sharedDirectory)), |
| 86 | logId: result.logId, |
| 87 | ...(result.instantCharge ? { instantCharge: result.instantCharge } : {}), |
| 88 | } satisfies StoredExecuteOutputSummary, |
| 89 | } satisfies PreparedExecuteOutput; |
| 90 | }); |