( result: ExecuteResult, )
| 69 | : value; |
| 70 | |
| 71 | export const formatExecuteResult = ( |
| 72 | result: ExecuteResult, |
| 73 | ): { |
| 74 | text: string; |
| 75 | structured: Record<string, unknown>; |
| 76 | isError: boolean; |
| 77 | } => { |
| 78 | const resultText = |
| 79 | result.result != null |
| 80 | ? typeof result.result === "string" |
| 81 | ? result.result |
| 82 | : JSON.stringify(result.result, null, 2) |
| 83 | : null; |
| 84 | |
| 85 | const logText = result.logs && result.logs.length > 0 ? result.logs.join("\n") : null; |
| 86 | |
| 87 | // `emit()` output is shown to the user, not returned to the model, so a |
| 88 | // script that only emits comes back with a null result. Acknowledge the |
| 89 | // emitted items in the envelope so an emit-without-return reads as "output |
| 90 | // went to the user" rather than a silent void. |
| 91 | const emitted = result.output?.length ?? 0; |
| 92 | const emittedNote = |
| 93 | emitted > 0 ? `${emitted} item${emitted === 1 ? "" : "s"} emitted to the user` : null; |
| 94 | const emittedField = emitted > 0 ? { emitted } : {}; |
| 95 | |
| 96 | if (result.error) { |
| 97 | const parts = [`Error: ${result.error}`, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 98 | return { |
| 99 | text: truncate(parts.join("\n"), MAX_PREVIEW_CHARS), |
| 100 | structured: { |
| 101 | status: "error", |
| 102 | error: result.error, |
| 103 | ...emittedField, |
| 104 | logs: result.logs ?? [], |
| 105 | }, |
| 106 | isError: true, |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | const resultPart = resultText |
| 111 | ? truncate(resultText, MAX_PREVIEW_CHARS) |
| 112 | : emittedNote |
| 113 | ? `(no return value; ${emittedNote})` |
| 114 | : "(no result)"; |
| 115 | const parts = [resultPart, ...(logText ? [`\nLogs:\n${logText}`] : [])]; |
| 116 | return { |
| 117 | text: parts.join("\n"), |
| 118 | structured: { |
| 119 | status: "completed", |
| 120 | result: result.result ?? null, |
| 121 | ...emittedField, |
| 122 | logs: result.logs ?? [], |
| 123 | }, |
| 124 | isError: false, |
| 125 | }; |
| 126 | }; |
| 127 | |
| 128 | export const formatPausedExecution = ( |
no test coverage detected