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 util from 'node:util'; |
| 2 | import { Effect, Result } from 'effect'; |
| 3 | import { commandHintStep } from 'src/services/command-hints'; |
| 4 | import type { TerminalUI } from 'src/services/terminal-ui'; |
| 5 | import { normalizeFileUploadSchema } from 'src/services/tool-file-uploads'; |
| 6 | import type { InstantCharge } from 'src/services/tools-executor'; |
| 7 | import { escapeTerminalLines, escapeTerminalText } from 'src/ui/escape-terminal-text'; |
| 8 | import { redact } from 'src/ui/redact'; |
| 9 | import { isPlainRecord } from 'src/utils/parse-json'; |
| 10 | |
| 11 | export const connectionTips = (params: { |
| 12 | readonly toolkit: string | undefined; |
| 13 | readonly toolSlug: string; |
| 14 | readonly surface: 'root' | 'dev'; |
| 15 | }) => { |
| 16 | const { toolkit, toolSlug, surface } = params; |
| 17 | const executeStep = |
| 18 | surface === 'dev' |
| 19 | ? commandHintStep('Retry', 'dev.playgroundExecute', { |
| 20 | slug: toolSlug, |
| 21 | userId: '<user-id>', |
| 22 | data: '...', |
| 23 | }) |
| 24 | : commandHintStep('Retry', 'root.execute', { slug: toolSlug, data: '...' }); |
| 25 | if (!toolkit) { |
| 26 | return executeStep; |
| 27 | } |
| 28 | return [ |
| 29 | commandHintStep( |
| 30 | 'Link the toolkit first', |
| 31 | surface === 'dev' ? 'dev.connectedAccounts.link' : 'root.link', |
| 32 | surface === 'dev' ? { toolkit, userId: '<user-id>' } : { toolkit } |
| 33 | ), |
| 34 | executeStep.replace('Retry:', 'Then retry:'), |
| 35 | ].join('\n'); |
| 36 | }; |
| 37 | |
| 38 | export const ciRedactReplacer = (_key: string, value: unknown): unknown => { |
| 39 | if (typeof value !== 'string') return value; |
| 40 | if (_key === 'logId') return redact({ value, prefix: 'log_' }); |
| 41 | if (_key === 'id' || _key.endsWith('Id') || _key.endsWith('_id')) { |
| 42 | return redact({ value }); |
| 43 | } |
| 44 | return value; |
| 45 | }; |
| 46 | |
| 47 | // JSON.stringify throws synchronously on circular or BigInt-bearing API |
| 48 | // payloads; fall back to util.inspect so the formatter stays a plain string |
| 49 | // function for its callers. |
| 50 | export const formatUnknownObject = (value: object): string => |
| 51 | Result.getOrElse( |
| 52 | Result.try(() => JSON.stringify(value, null, 2)), |
| 53 | () => util.inspect(value, { depth: 5, breakLength: 120 }) |
| 54 | ); |
| 55 | |
| 56 | export const redactRequestId = (value: object): object => { |
| 57 | if (!value || typeof value !== 'object' || Array.isArray(value)) { |
| 58 | return value; |
| 59 | } |
| 60 | if (!isPlainRecord(value)) return value; |
| 61 | const requestId = value.request_id; |
| 62 | if (typeof requestId !== 'string') { |
| 63 | return value; |
| 64 | } |
| 65 | return { |
| 66 | ...value, |
| 67 | request_id: redact({ value: requestId }), |
| 68 | }; |
| 69 | }; |
| 70 | |
| 71 | export const describeStoredOutputSize = ({ |
| 72 | sizeBytes, |
| 73 | tokenCount, |
| 74 | }: { |
| 75 | readonly sizeBytes: number; |
| 76 | readonly tokenCount: number; |
| 77 | }): string => `${Math.ceil(sizeBytes / 1024)} KB, ~${tokenCount} tokens`; |
| 78 | |
| 79 | export const serializeExecuteOutput = (result: unknown): string => |
| 80 | JSON.stringify(result, ciRedactReplacer, 2); |
| 81 | |
| 82 | const permissionApprovalLabel = (approval?: string): string | undefined => { |
| 83 | switch (approval) { |
| 84 | case 'always_approved': |
| 85 | return 'always approved'; |
| 86 | case 'cached_approved': |
| 87 | return 'cached approved'; |
| 88 | case 'approved_once': |
| 89 | return 'approved once'; |
| 90 | case 'approved_for_session': |
| 91 | return 'approved for session'; |
| 92 | default: |
| 93 | return undefined; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | const instantChargeLabel = (charge?: InstantCharge): string | undefined => { |
| 98 | if (!charge) return undefined; |
| 99 | return `instant charge: ${charge.amount} ${charge.currency}`; |
| 100 | }; |
| 101 | |
| 102 | export const executionSuccessSuffix = (result: { |
| 103 | readonly logId?: string; |
| 104 | readonly permissionApproval?: string; |
| 105 | readonly instantCharge?: InstantCharge; |
| 106 | }) => { |
| 107 | const metadata = [ |
| 108 | permissionApprovalLabel(result.permissionApproval), |
| 109 | instantChargeLabel(result.instantCharge), |
| 110 | result.logId ? `logId: ${redact({ value: result.logId, prefix: 'log_' })}` : undefined, |
| 111 | ].filter((value): value is string => Boolean(value)); |
| 112 | return metadata.length > 0 ? ` (${escapeTerminalText(metadata.join(', '))})` : ''; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * Prints serialized execute JSON on stdout, shown on a terminal too. JSON already escapes C0 |
| 117 | * controls in strings; DEL and C1 are escaped here as well, which decodes to the same values. A |
| 118 | * response stored in a file keeps its original bytes. |
| 119 | */ |
| 120 | export const writeExecuteStdout = (ui: TerminalUI, data: string) => |
| 121 | ui.output(escapeTerminalLines(data), { force: true }); |
| 122 | |
| 123 | export type DryRunSummary = { |
| 124 | readonly successful: true; |
| 125 | readonly dryRun: true; |
| 126 | readonly slug: string; |
| 127 | readonly arguments: Record<string, unknown>; |
| 128 | readonly userId: string; |
| 129 | readonly schemaPath?: string; |
| 130 | readonly schemaVersion?: string | null; |
| 131 | }; |
| 132 | |
| 133 | export const emitCachedSchema = ( |
| 134 | ui: TerminalUI, |
| 135 | slug: string, |
| 136 | definition: { |
| 137 | readonly version: string | null; |
| 138 | readonly schemaPath: string; |
| 139 | readonly schema: Record<string, unknown>; |
| 140 | } |
| 141 | ) => |
| 142 | Effect.gen(function* () { |
| 143 | const displaySchema = normalizeFileUploadSchema(definition.schema); |
| 144 | yield* ui.log.message( |
| 145 | `Schema saved, inspect keys like: jq '{required: (.inputSchema.required // []), keys: (.inputSchema.properties | keys)}' ${definition.schemaPath}` |
| 146 | ); |
| 147 | yield* writeExecuteStdout( |
| 148 | ui, |
| 149 | JSON.stringify( |
| 150 | { |
| 151 | slug, |
| 152 | version: definition.version, |
| 153 | schemaPath: definition.schemaPath, |
| 154 | inputSchema: displaySchema, |
| 155 | }, |
| 156 | null, |
| 157 | 2 |
| 158 | ) |
| 159 | ); |
| 160 | }); |