( result: ExecuteResult, )
| 93 | : value; |
| 94 | |
| 95 | export const formatExecuteResult = ( |
| 96 | result: ExecuteResult, |
| 97 | ): { |
| 98 | text: string; |
| 99 | structured: Record<string, unknown>; |
| 100 | isError: boolean; |
| 101 | } => { |
| 102 | const resultText = |
| 103 | result.result != null |
| 104 | ? typeof result.result === "string" |
| 105 | ? result.result |
| 106 | : JSON.stringify(result.result, null, 2) |
| 107 | : null; |
| 108 | |
| 109 | const logText = result.logs && result.logs.length > 0 ? result.logs.join("\n") : null; |
| 110 | |
| 111 | // `emit()` output is shown to the user, not returned to the model, so a |
| 112 | // script that only emits comes back with a null result. Acknowledge the |
| 113 | // emitted items in the envelope so an emit-without-return reads as "output |
| 114 | // went to the user" rather than a silent void. |
| 115 | const emitted = result.output?.length ?? 0; |
| 116 | const emittedNote = |
| 117 | emitted > 0 ? `${emitted} item${emitted === 1 ? "" : "s"} emitted to the user` : null; |
| 118 | const emittedField = emitted > 0 ? { emitted } : {}; |
| 119 | |
| 120 | if (result.error) { |
| 121 | const parts = [`Error: ${result.error}`, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 122 | return { |
| 123 | text: truncate(parts.join("\n"), MAX_PREVIEW_CHARS), |
| 124 | structured: { |
| 125 | status: "error", |
| 126 | error: result.error, |
| 127 | ...emittedField, |
| 128 | logs: result.logs ?? [], |
| 129 | }, |
| 130 | isError: true, |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | const resultPart = resultText |
| 135 | ? truncate(resultText, MAX_PREVIEW_CHARS) |
| 136 | : emittedNote |
| 137 | ? `(no return value; ${emittedNote})` |
| 138 | : "(no result)"; |
| 139 | const parts = [resultPart, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 140 | return { |
| 141 | text: parts.join("\n"), |
| 142 | structured: { |
| 143 | status: "completed", |
| 144 | result: result.result ?? null, |
| 145 | ...emittedField, |
| 146 | logs: result.logs ?? [], |
| 147 | }, |
| 148 | isError: false, |
| 149 | }; |
| 150 | }; |
| 151 | |
| 152 | export const formatPausedExecution = ( |
no test coverage detected