export async function persistToolOutput(input) {
    const { output, resultMetadata, runtime, shellDebugBase, toolCallId, toolInput, toolName, turn } = input;
    const toolResultItem = await runtime.toolResults.createToolResultItemWithTrace(turn, toolName, output, toolCallId, input.structuredContent ?? resultMetadata?.structuredContent, undefined, {
        forceTracePersistence: resultMetadata?.forceTracePersistence,
        outputManaged: resultMetadata?.outputManaged,
        traceOutput: resultMetadata?.traceOutput,
    });
    const toolEnvelope = runtime.toolResults.buildToolResultEnvelope(turn, toolName, toolInput, output, toolResultItem, input.envelopeOverride ?? resultMetadata?.envelope);
    const evidenceRecords = input.appendEvidence === false
        ? undefined
        : await runtime.evidence.buildToolEvidenceRecords(turn, toolName, toolInput, output, toolResultItem, toolEnvelope);
    if (shellDebugBase && input.debugEventPrefix) {
        runtime.tracing.logShellToolDebug(runtime.cwd, `${input.debugEventPrefix}.start`, {
            ...shellDebugBase,
            callId: toolCallId,
            evidenceRecords: evidenceRecords?.length ?? 0,
            outputBytes: Buffer.byteLength(output, "utf8"),
        });
    }
    await runtime.checkpointPersistence.saveToolOutputCheckpoint(turn, input.resumeContext, output, toolResultItem, toolEnvelope, evidenceRecords);
    await runtime.items.pushItems([toolResultItem]);
    if (evidenceRecords) {
        await runtime.evidence.appendEvidenceRecords(evidenceRecords);
    }
    if (shellDebugBase && input.debugEventPrefix) {
        runtime.tracing.logShellToolDebug(runtime.cwd, `${input.debugEventPrefix}.done`, {
            ...shellDebugBase,
            callId: toolCallId,
        });
    }
}
//# sourceMappingURL=tool-result-persistence.js.map