MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / formatExecuteResult

Function formatExecuteResult

packages/core/execution/src/engine.ts:154–211  ·  view source on GitHub ↗
(
  result: ExecuteResult,
)

Source from the content-addressed store, hash-verified

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

Callers 5

executions.tsFile · 0.90
engine.test.tsFile · 0.90
toMcpOutputResultFunction · 0.90
toMcpResultFunction · 0.90

Calls 2

soleConnectedToolNameFunction · 0.85
truncateFunction · 0.70

Tested by

no test coverage detected