( result: ExecuteResult, )
| 139 | : value; |
| 140 | |
| 141 | export const formatExecuteResult = ( |
| 142 | result: ExecuteResult, |
| 143 | ): { |
| 144 | text: string; |
| 145 | structured: Record<string, unknown>; |
| 146 | isError: boolean; |
| 147 | } => { |
| 148 | const resultText = |
| 149 | result.result != null |
| 150 | ? typeof result.result === "string" |
| 151 | ? result.result |
| 152 | : JSON.stringify(result.result, null, 2) |
| 153 | : null; |
| 154 | |
| 155 | const logText = result.logs && result.logs.length > 0 ? result.logs.join("\n") : null; |
| 156 | |
| 157 | // `emit()` output is shown to the user, not returned to the model, so a |
| 158 | // script that only emits comes back with a null result. Acknowledge the |
| 159 | // emitted items in the envelope so an emit-without-return reads as "output |
| 160 | // went to the user" rather than a silent void. |
| 161 | const emitted = result.output?.length ?? 0; |
| 162 | const emittedNote = |
| 163 | emitted > 0 ? `${emitted} item${emitted === 1 ? "" : "s"} emitted to the user` : null; |
| 164 | const emittedField = emitted > 0 ? { emitted } : {}; |
| 165 | |
| 166 | if (result.error) { |
| 167 | const parts = [`Error: ${result.error}`, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 168 | return { |
| 169 | text: truncate(parts.join("\n"), MAX_PREVIEW_CHARS), |
| 170 | structured: { |
| 171 | status: "error", |
| 172 | error: result.error, |
| 173 | ...emittedField, |
| 174 | logs: result.logs ?? [], |
| 175 | }, |
| 176 | isError: true, |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | const resultPart = resultText |
| 181 | ? truncate(resultText, MAX_PREVIEW_CHARS) |
| 182 | : emittedNote |
| 183 | ? `(no return value; ${emittedNote})` |
| 184 | : "(no result)"; |
| 185 | const parts = [resultPart, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 186 | return { |
| 187 | text: parts.join("\n"), |
| 188 | structured: { |
| 189 | status: "completed", |
| 190 | result: result.result ?? null, |
| 191 | ...emittedField, |
| 192 | logs: result.logs ?? [], |
| 193 | }, |
| 194 | isError: false, |
| 195 | }; |
| 196 | }; |
| 197 | |
| 198 | export const formatPausedExecution = ( |
no test coverage detected