import path from "node:path";
import { appendFile } from "node:fs/promises";
import { appendJsonl, createId, nowIso, readJsonl, sanitizeJson, sanitizeText } from "../../shared/index.js";
import { appendActivityJournalEntry } from "../activity-journal/index.js";
import { LAB_REPRESENTATIVE_AGENT } from "../../agents/identity.js";
import { redactSeedJson } from "../redaction/index.js";
export async function appendEvent(store, runId, input) {
    const event = {
        event_id: createId("event"),
        timestamp: nowIso(),
        run_id: runId,
        ...input,
        metadata: store.config.trace.sanitize_outputs ? redactSeedJson(sanitizeJson(input.metadata)) : input.metadata,
    };
    await appendJsonl(path.join(runRoot(store, runId), "events.jsonl"), event);
    await appendActivityJournalEntry(store, runId, { kind: "event", event });
    return event;
}
export async function appendAgentCall(store, runId, input) {
    const record = redactSeedJson(sanitizeJson(input));
    await appendJsonl(path.join(runRoot(store, runId), "agent_calls.jsonl"), record);
    for (const modelCall of modelCallsFromAgentCall(runId, record)) {
        await appendJsonl(path.join(runRoot(store, runId), "model_calls.jsonl"), sanitizeJson(modelCall));
    }
    await appendActivityJournalEntry(store, runId, { kind: "agent_call", agent_call: record });
}
export async function appendToolCall(store, runId, input) {
    const record = redactSeedJson(sanitizeJson(input));
    await appendJsonl(path.join(runRoot(store, runId), "tool_calls.jsonl"), record);
    await appendActivityJournalEntry(store, runId, { kind: "tool_call", tool_call: record });
}
export async function addEvidence(store, runId, input) {
    const anchor = `ev-${createId("evidence").replace("evidence_", "")}`;
    const refs = input.refs?.length ? `\nRefs: ${input.refs.map((ref) => `\`${sanitizeText(ref)}\``).join(", ")}\n` : "";
    const block = `\n<a id="${anchor}"></a>\n\n## ${sanitizeText(input.title)}\n\n${sanitizeText(input.body)}\n${refs}`;
    await appendFile(path.join(runRoot(store, runId), "evidence.md"), block, "utf8");
    await appendEvent(store, runId, {
        type: "evidence_added",
        actor: input.actor ?? LAB_REPRESENTATIVE_AGENT,
        summary: input.title,
        metadata: { anchor, refs: input.refs ?? [] },
    });
    return `evidence.md#${anchor}`;
}
export async function readTraceSummary(store, runId) {
    const dir = runRoot(store, runId);
    const [events, agentCalls, modelCalls, toolCalls] = await Promise.all([
        readJsonl(path.join(dir, "events.jsonl")),
        readJsonl(path.join(dir, "agent_calls.jsonl")),
        readJsonl(path.join(dir, "model_calls.jsonl")),
        readJsonl(path.join(dir, "tool_calls.jsonl")),
    ]);
    return { events, agentCalls, modelCalls, toolCalls };
}
function runRoot(store, runId) {
    return path.join(path.resolve(store.directory, store.config.trace.artifact_root), runId);
}
function modelCallsFromAgentCall(runId, call) {
    return (call.model_attempts ?? []).map((attempt) => ({
        model_call_id: createId("model_call"),
        timestamp: call.timestamp_end ?? call.timestamp_start,
        run_id: runId,
        agent_call_id: call.agent_call_id,
        parent_session_id: call.parent_session_id,
        child_session_id: attempt.child_session_id ?? call.child_session_id,
        caller_agent: call.caller_agent,
        target_agent: call.target_agent,
        mode: call.mode,
        prompt_summary: call.prompt_summary,
        prompt_ref: call.full_prompt_ref,
        result_summary: call.result_summary,
        attempt_id: attempt.attempt_id,
        attempt: attempt.attempt,
        source: attempt.source,
        model: attempt.model,
        providerID: attempt.providerID,
        modelID: attempt.modelID,
        variant: attempt.variant,
        model_profile: attempt.model_profile ?? call.model_profile,
        status: attempt.status,
        resolved_model: call.resolved_model,
        token_usage: call.token_usage,
        error: attempt.error ?? call.error,
    }));
}
//# sourceMappingURL=store.js.map