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