import { b as RunOutcome, R as Run, S as Span, c as SpanKind, L as LlmSpan, T as ToolSpan, d as RetrievalSpan, J as JudgeSpan, e as SandboxSpan, E as EventKind, a as TraceEvent, B as BudgetLedgerEntry, A as Artifact, M as Message } from './schema-m0gsnbt3.js';
import { T as TraceStore } from './store-CKUAgsJz.js';

/**
 * TraceEmitter — hierarchical span builder that auto-parents using an
 * internal stack. One emitter per Run; emitters do NOT share state.
 *
 * Convenience methods (`llm`, `tool`, `retrieval`, `judge`, `sandbox`)
 * return a `SpanHandle` with `.end()` / `.fail()` so callers don't
 * have to thread spanIds manually. For async workflows that can't use
 * the stack (e.g. fan-out parallel calls), pass `parentSpanId`
 * explicitly.
 */

interface SpanHandle<S extends Span = Span> {
    span: S;
    end(patch?: Partial<S>): Promise<void>;
    fail(error: string | Error, patch?: Partial<S>): Promise<void>;
}
interface RunCompleteHookContext {
    runId: string;
    emitter: TraceEmitter;
    store: TraceStore;
    /** Outcome the caller passed to `endRun` (undefined for `abortRun`). */
    outcome?: RunOutcome;
    /** Final run status. */
    status: 'completed' | 'failed' | 'aborted';
}
type RunCompleteHook = (ctx: RunCompleteHookContext) => Promise<void> | void;
interface TraceEmitterOptions {
    runId?: string;
    /** Inject a clock for deterministic tests. */
    now?: () => number;
    /** Inject an id generator for deterministic tests. */
    id?: () => string;
    /**
     * Hooks fired after `endRun` / `abortRun` writes the final run state.
     * Designed for trace-analyst auto-execution, integrity assertions, and
     * outbound notifications. Hooks run sequentially in the order supplied.
     *
     * By default a hook that throws is swallowed and logged as a `note` event
     * on the run — auto-orchestration must not crash the underlying flow.
     * Set `hookErrors: 'throw'` to propagate.
     */
    onRunComplete?: RunCompleteHook[];
    /** `'swallow'` (default) | `'throw'`. */
    hookErrors?: 'swallow' | 'throw';
}
declare class TraceEmitter {
    private store;
    private stack;
    private _runId;
    private now;
    private id;
    private hooks;
    private hookErrors;
    constructor(store: TraceStore, options?: TraceEmitterOptions);
    get runId(): string;
    get traceStore(): TraceStore;
    /** Append a hook after construction (e.g. attach the trace analyst). */
    addRunCompleteHook(hook: RunCompleteHook): void;
    /**
     * Begin a Run.
     *
     * `scenarioId` is required on the persisted Run shape — every Run downstream
     * gets a non-empty scenarioId so filters and aggregations stay simple — but
     * the INPUT here accepts it as optional. When omitted, startRun substitutes
     * a sensible default (`run.layer ?? run.tags?.['kind'] ?? 'runtime'`) so
     * runtime / operator / meta-eval runs that have no curated-scenario corpus
     * to anchor to don't have to invent placeholder strings at the call site.
     */
    startRun(run: Omit<Run, 'runId' | 'scenarioId' | 'startedAt' | 'status'> & {
        scenarioId?: string;
    }): Promise<Run>;
    endRun(outcome?: RunOutcome): Promise<void>;
    abortRun(reason: string): Promise<void>;
    private runHooks;
    span<S extends Span = Span>(init: {
        kind: SpanKind;
        name: string;
        parentSpanId?: string;
        attributes?: Record<string, unknown>;
    } & Partial<Omit<S, 'spanId' | 'runId' | 'startedAt' | 'kind' | 'name'>>): Promise<SpanHandle<S>>;
    private handle;
    private pop;
    llm(init: Omit<LlmSpan, 'spanId' | 'runId' | 'kind' | 'startedAt'>): Promise<SpanHandle<LlmSpan>>;
    tool(init: Omit<ToolSpan, 'spanId' | 'runId' | 'kind' | 'startedAt'>): Promise<SpanHandle<ToolSpan>>;
    retrieval(init: Omit<RetrievalSpan, 'spanId' | 'runId' | 'kind' | 'startedAt'>): Promise<SpanHandle<RetrievalSpan>>;
    recordJudge(verdict: Omit<JudgeSpan, 'spanId' | 'runId' | 'kind' | 'startedAt' | 'endedAt'>): Promise<JudgeSpan>;
    sandbox(init: Omit<SandboxSpan, 'spanId' | 'runId' | 'kind' | 'startedAt'>): Promise<SpanHandle<SandboxSpan>>;
    emit(event: {
        kind: EventKind;
        spanId?: string;
        payload?: Record<string, unknown>;
    }): Promise<TraceEvent>;
    recordBudget(entry: Omit<BudgetLedgerEntry, 'runId' | 'timestamp'> & {
        timestamp?: number;
    }): Promise<BudgetLedgerEntry>;
    recordArtifact(artifact: Omit<Artifact, 'artifactId' | 'runId'>): Promise<Artifact>;
    /**
     * Runs `fn` inside a span; auto-ends on success, auto-fails on throw.
     * Returns the fn's return value. Use this for the 95% case.
     */
    within<T>(init: Parameters<TraceEmitter['span']>[0], fn: (handle: SpanHandle) => Promise<T>): Promise<T>;
}
/** Helper to build an LLM span handle args object from a provider-shaped response. */
declare function llmSpanFromProvider(args: {
    name?: string;
    model: string;
    messages: Message[];
    output: string;
    usage?: {
        inputTokens?: number;
        outputTokens?: number;
        cachedTokens?: number;
        reasoningTokens?: number;
    };
    costUsd?: number;
    finishReason?: string;
}): Omit<LlmSpan, 'spanId' | 'runId' | 'kind' | 'startedAt'>;

export { type RunCompleteHook as R, type SpanHandle as S, TraceEmitter as T, type RunCompleteHookContext as a, type TraceEmitterOptions as b, llmSpanFromProvider as l };
