( result: ExecuteResult, )
| 115 | : value; |
| 116 | |
| 117 | export const formatExecuteResult = ( |
| 118 | result: ExecuteResult, |
| 119 | ): { |
| 120 | text: string; |
| 121 | structured: Record<string, unknown>; |
| 122 | isError: boolean; |
| 123 | } => { |
| 124 | const resultText = |
| 125 | result.result != null |
| 126 | ? typeof result.result === "string" |
| 127 | ? result.result |
| 128 | : JSON.stringify(result.result, null, 2) |
| 129 | : null; |
| 130 | |
| 131 | const logText = result.logs && result.logs.length > 0 ? result.logs.join("\n") : null; |
| 132 | |
| 133 | // `emit()` output is shown to the user, not returned to the model, so a |
| 134 | // script that only emits comes back with a null result. Acknowledge the |
| 135 | // emitted items in the envelope so an emit-without-return reads as "output |
| 136 | // went to the user" rather than a silent void. |
| 137 | const emitted = result.output?.length ?? 0; |
| 138 | const emittedNote = |
| 139 | emitted > 0 ? `${emitted} item${emitted === 1 ? "" : "s"} emitted to the user` : null; |
| 140 | const emittedField = emitted > 0 ? { emitted } : {}; |
| 141 | |
| 142 | if (result.error) { |
| 143 | const parts = [`Error: ${result.error}`, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 144 | return { |
| 145 | text: truncate(parts.join("\n"), MAX_PREVIEW_CHARS), |
| 146 | structured: { |
| 147 | status: "error", |
| 148 | error: result.error, |
| 149 | ...emittedField, |
| 150 | logs: result.logs ?? [], |
| 151 | }, |
| 152 | isError: true, |
| 153 | }; |
| 154 | } |
| 155 | |
| 156 | const resultPart = resultText |
| 157 | ? truncate(resultText, MAX_PREVIEW_CHARS) |
| 158 | : emittedNote |
| 159 | ? `(no return value; ${emittedNote})` |
| 160 | : "(no result)"; |
| 161 | const parts = [resultPart, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 162 | return { |
| 163 | text: parts.join("\n"), |
| 164 | structured: { |
| 165 | status: "completed", |
| 166 | result: result.result ?? null, |
| 167 | ...emittedField, |
| 168 | logs: result.logs ?? [], |
| 169 | }, |
| 170 | isError: false, |
| 171 | }; |
| 172 | }; |
| 173 | |
| 174 | export const formatPausedExecution = ( |
no test coverage detected