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