| 327 | const ORPHAN_TOOL_RESULT_MAX_KEYS = 8; |
| 328 | |
| 329 | function summarizeToolResultForLog(output: unknown): Record<string, unknown> { |
| 330 | if (output === null) { |
| 331 | return { outputType: "null" }; |
| 332 | } |
| 333 | |
| 334 | if (typeof output === "string") { |
| 335 | return { |
| 336 | outputType: "string", |
| 337 | outputLength: output.length, |
| 338 | outputPreview: output.slice(0, ORPHAN_TOOL_RESULT_PREVIEW_CHARS), |
| 339 | }; |
| 340 | } |
| 341 | |
| 342 | if (typeof output === "number" || typeof output === "boolean") { |
| 343 | return { |
| 344 | outputType: typeof output, |
| 345 | outputPreview: String(output), |
| 346 | }; |
| 347 | } |
| 348 | |
| 349 | if (Array.isArray(output)) { |
| 350 | return { |
| 351 | outputType: "array", |
| 352 | outputLength: output.length, |
| 353 | firstItemType: |
| 354 | output.length > 0 && output[0] !== null |
| 355 | ? typeof output[0] |
| 356 | : output.length > 0 |
| 357 | ? "null" |
| 358 | : undefined, |
| 359 | }; |
| 360 | } |
| 361 | |
| 362 | if (typeof output === "object") { |
| 363 | const outputRecord = output as Record<string, unknown>; |
| 364 | const keys = Object.keys(outputRecord); |
| 365 | const summary: Record<string, unknown> = { |
| 366 | outputType: "object", |
| 367 | outputKeyCount: keys.length, |
| 368 | outputKeys: keys.slice(0, ORPHAN_TOOL_RESULT_MAX_KEYS), |
| 369 | }; |
| 370 | |
| 371 | if (typeof outputRecord.type === "string") { |
| 372 | summary.outputFormat = outputRecord.type; |
| 373 | } |
| 374 | |
| 375 | if (Array.isArray(outputRecord.value)) { |
| 376 | summary.outputValueLength = outputRecord.value.length; |
| 377 | } |
| 378 | |
| 379 | return summary; |
| 380 | } |
| 381 | |
| 382 | return { |
| 383 | outputType: typeof output, |
| 384 | outputPreview: JSON.stringify(output), |
| 385 | }; |
| 386 | } |