import { E as EvalMatchOptions, a as EvalTraceInput } from './MCPClientManager-D9Cqa92W.js';
import { z } from 'zod';

/**
 * State-based predicate system for deterministic eval gating.
 *
 * A {@link Predicate} is a declarative assertion over a single iteration's
 * transcript. Predicates are the **gate**: a pure function of the transcript
 * yields the same verdict every time (same transcript → same result), which is
 * the property a CI release gate requires and a stochastic LLM judge cannot
 * provide. The `serverQuality` LLM judge remains the advisory **insight** layer.
 *
 * The union is intentionally small (9 types). It grows only when a real corpus
 * task demands a new one — not speculatively.
 *
 * Hosted in `@mcpjam/sdk` (browser-safe; reuses the `../matchers` argument
 * engine) so the inspector GUI runner and the `mcpjam eval` CLI share one
 * implementation.
 */

/**
 * Argument-matching mode reused from the eval matcher
 * (`EvalMatchOptions.argumentMatching`):
 *
 *   - `"partial"` (default) — only the keys present in `args` are checked;
 *     the actual call may carry extra keys; placeholder strings like
 *     `"string"`/`"number"`/`"any"` are interpreted as type checks.
 *   - `"exact"`   — deep equality on the args object; no extras, no placeholders.
 *   - `"ignore"`  — arguments are not compared (only the tool name matters).
 */
type ArgMatchMode = NonNullable<EvalMatchOptions["argumentMatching"]>;
/**
 * Expected-argument matcher for {@link Predicate} `toolCalledWith`.
 *
 * `args` is the expected argument shape; `argumentMatching` selects the
 * semantics. Reuses the exact same engine as the tool-call matcher so a
 * predicate and the existing `expectedToolCalls` matcher agree on what
 * "these args match" means.
 */
type ArgMatcher = {
    args: Record<string, unknown>;
    /** Defaults to `"partial"` when omitted. */
    argumentMatching?: ArgMatchMode;
};
/**
 * The deterministic predicate library.
 *
 * Discriminated on `type`. Each variant is evaluated by a pure function over
 * the {@link IterationTranscript}.
 */
type Predicate = 
/** A call to `toolName` whose args satisfy `args` occurred at least `minCount` (default 1) times. */
{
    type: "toolCalledWith";
    toolName: string;
    args: ArgMatcher;
    minCount?: number;
}
/** `toolName` was called at least once (args irrelevant). */
 | {
    type: "toolCalledAtLeastOnce";
    toolName: string;
}
/** `toolName` was never called (forbidden tool). */
 | {
    type: "toolNeverCalled";
    toolName: string;
}
/** The first tool call observed in the transcript was `toolName`. */
 | {
    type: "firstToolWas";
    toolName: string;
}
/** The final assistant message contains `needle`. Case-insensitive unless `caseSensitive`. */
 | {
    type: "responseContains";
    needle: string;
    caseSensitive?: boolean;
}
/** The final assistant message matches the regular expression `pattern` (regex source, no flags). */
 | {
    type: "responseMatches";
    pattern: string;
}
/** No tool produced an error (neither MCP `isError: true` nor a JSON-RPC/transport failure). */
 | {
    type: "noToolErrors";
}
/** The final assistant message is a non-empty (non-whitespace) string. */
 | {
    type: "finalAssistantMessageNonEmpty";
}
/** Total token usage for the iteration is strictly under `tokens`. */
 | {
    type: "tokenBudgetUnder";
    tokens: number;
};
/** The `type` discriminants of {@link Predicate}, for validators. */
type PredicateType = Predicate["type"];
/**
 * Placeholder strings the matcher's `partial` mode treats as type checks
 * instead of literal equality. Exposed as a Zod literal union so authoring
 * UIs can offer them as drop-down options when constructing arg matchers.
 *
 * The actual leaf may also be any JSON literal (string/number/boolean/object/
 * array/null) — that's not captured here because the args blob is
 * `z.record(z.string(), z.unknown())` at the wire boundary.
 */
declare const PREDICATE_PLACEHOLDER_STRINGS: readonly ["any", "string", "number", "boolean", "object", "array", "null"];
/** Zod schema for {@link ArgMatcher}. `args` is unrestricted JSON. */
declare const argMatcherSchema: z.ZodObject<{
    args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
    argumentMatching: z.ZodOptional<z.ZodEnum<{
        ignore: "ignore";
        exact: "exact";
        partial: "partial";
    }>>;
}, z.core.$strip>;
/**
 * Zod schema for {@link Predicate}. Uses `z.discriminatedUnion` on `type`
 * so authoring-side validation surfaces a precise error (e.g. "unknown
 * predicate type 'firstToolWass'") rather than a generic union failure.
 */
declare const predicateSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
    type: z.ZodLiteral<"toolCalledWith">;
    toolName: z.ZodString;
    args: z.ZodObject<{
        args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
        argumentMatching: z.ZodOptional<z.ZodEnum<{
            ignore: "ignore";
            exact: "exact";
            partial: "partial";
        }>>;
    }, z.core.$strip>;
    minCount: z.ZodOptional<z.ZodNumber>;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"toolCalledAtLeastOnce">;
    toolName: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"toolNeverCalled">;
    toolName: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"firstToolWas">;
    toolName: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"responseContains">;
    needle: z.ZodString;
    caseSensitive: z.ZodOptional<z.ZodBoolean>;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"responseMatches">;
    pattern: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"noToolErrors">;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"finalAssistantMessageNonEmpty">;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"tokenBudgetUnder">;
    tokens: z.ZodNumber;
}, z.core.$strip>], "type">;
/** Array of predicates — used for both suite defaults and case overrides. */
declare const predicateArraySchema: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
    type: z.ZodLiteral<"toolCalledWith">;
    toolName: z.ZodString;
    args: z.ZodObject<{
        args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
        argumentMatching: z.ZodOptional<z.ZodEnum<{
            ignore: "ignore";
            exact: "exact";
            partial: "partial";
        }>>;
    }, z.core.$strip>;
    minCount: z.ZodOptional<z.ZodNumber>;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"toolCalledAtLeastOnce">;
    toolName: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"toolNeverCalled">;
    toolName: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"firstToolWas">;
    toolName: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"responseContains">;
    needle: z.ZodString;
    caseSensitive: z.ZodOptional<z.ZodBoolean>;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"responseMatches">;
    pattern: z.ZodString;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"noToolErrors">;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"finalAssistantMessageNonEmpty">;
}, z.core.$strip>, z.ZodObject<{
    type: z.ZodLiteral<"tokenBudgetUnder">;
    tokens: z.ZodNumber;
}, z.core.$strip>], "type">>;
/**
 * Case-level predicate override envelope. The {@link mode} eliminates the
 * `predicates`/`additionalPredicates` ambiguity (see plan Phase 2):
 *
 *   - `inherit` — effective predicates = suite defaults (`list` ignored).
 *   - `replace` — effective predicates = `list`.
 *   - `extend`  — effective predicates = suite defaults followed by `list`.
 */
declare const casePredicatesSchema: z.ZodObject<{
    mode: z.ZodEnum<{
        inherit: "inherit";
        replace: "replace";
        extend: "extend";
    }>;
    list: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
        type: z.ZodLiteral<"toolCalledWith">;
        toolName: z.ZodString;
        args: z.ZodObject<{
            args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
            argumentMatching: z.ZodOptional<z.ZodEnum<{
                ignore: "ignore";
                exact: "exact";
                partial: "partial";
            }>>;
        }, z.core.$strip>;
        minCount: z.ZodOptional<z.ZodNumber>;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"toolCalledAtLeastOnce">;
        toolName: z.ZodString;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"toolNeverCalled">;
        toolName: z.ZodString;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"firstToolWas">;
        toolName: z.ZodString;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"responseContains">;
        needle: z.ZodString;
        caseSensitive: z.ZodOptional<z.ZodBoolean>;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"responseMatches">;
        pattern: z.ZodString;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"noToolErrors">;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"finalAssistantMessageNonEmpty">;
    }, z.core.$strip>, z.ZodObject<{
        type: z.ZodLiteral<"tokenBudgetUnder">;
        tokens: z.ZodNumber;
    }, z.core.$strip>], "type">>;
}, z.core.$strip>;
type CasePredicates = z.infer<typeof casePredicatesSchema>;
type PredicatePlaceholder = (typeof PREDICATE_PLACEHOLDER_STRINGS)[number];
/**
 * How a tool failure surfaced. The plan requires `noToolErrors` to distinguish
 * these two cases (and report which fired), matching the runner's existing
 * `traceIndicatesToolExecutionFailure` gate, which treats both as failures:
 *
 *   - `"content-error"`  — an MCP `CallToolResult` with `isError: true`. The
 *     tool executed and reported a domain error the protocol-correct way.
 *   - `"protocol-error"` — a JSON-RPC / transport-level failure (the AI SDK
 *     `tool-error` stream part, or an errored tool span). The call itself
 *     failed; no protocol-correct result was produced.
 */
type ToolErrorKind = "content-error" | "protocol-error";
/** A single detected tool failure, used by the `noToolErrors` predicate. */
type ToolErrorRecord = {
    toolName?: string;
    kind: ToolErrorKind;
    /** Optional human-readable detail surfaced in the predicate reason. */
    message?: string;
};
/** Token usage totals for an iteration. */
type TranscriptUsage = {
    inputTokens?: number;
    outputTokens?: number;
    totalTokens?: number;
};
/** A tool call observed in the transcript: `{ toolName, arguments }`. */
type TranscriptToolCall = {
    toolName: string;
    arguments: Record<string, unknown>;
};
/**
 * The stable input shape predicates evaluate against.
 *
 * Deliberately minimal: it carries exactly what the 8 V1 predicates need and
 * nothing else, so it can be produced both by the live eval runner (which maps
 * its internal per-iteration state onto this shape) and by hand-authored test
 * fixtures. New predicates that need more signal extend this type.
 */
type IterationTranscript = {
    /** Ordered tool calls across all turns of the iteration. */
    toolCalls: TranscriptToolCall[];
    /** Tool failures detected over the iteration trace. Absent/empty ⇒ no errors. */
    toolErrors?: ToolErrorRecord[];
    /** Text of the final assistant message of the iteration, if any. */
    finalAssistantMessage?: string;
    /** Token usage totals for the whole iteration, if measured. */
    usage?: TranscriptUsage;
};
/** Per-predicate verdict row, persisted to `testIteration.metadata.predicates`. */
type PredicateResult = {
    predicate: Predicate;
    passed: boolean;
    /** Structured, deterministic explanation — names the expected vs actual on failure. */
    reason: string;
};

/**
 * True when an MCP {@link https://modelcontextprotocol.io CallToolResult}
 * indicates a tool execution error (`isError: true`).
 */
declare function isCallToolResultError(result: unknown): boolean;
/**
 * Detect MCP-style tool failure on a single persisted trace message part
 * (aligns with inspector trace viewer heuristics). Behaviour-preserving
 * boolean over {@link classifyToolFailurePart}.
 */
declare function traceMessagePartIndicatesToolFailure(part: unknown): boolean;
/**
 * Whether a stored eval trace shows at least one tool execution failure
 * (errored tool spans and/or MCP error tool-results in messages).
 */
declare function traceIndicatesToolExecutionFailure(trace: EvalTraceInput | undefined): boolean;
/**
 * Extract classified tool failures from a stored eval trace, for the
 * `noToolErrors` predicate. Mirrors {@link traceIndicatesToolExecutionFailure}'s
 * detection but returns one classified record per failure rather than a boolean.
 */
declare function extractToolErrors(trace: EvalTraceInput | undefined): ToolErrorRecord[];
type FinalizeEvalPassedParams = {
    /** Pass/fail from tool-call matching or user test assertion */
    matchPassed: boolean;
    trace?: EvalTraceInput;
    /** Non-empty when the runner aborted due to a step/tool/network error */
    iterationError?: string | null;
    /**
     * When not `false`, failed tool executions and iteration errors fail the case.
     * Default: treat as `true`.
     */
    failOnToolError?: boolean;
    /**
     * State-based predicate verdicts (see `./predicates`). When present, the case
     * additionally fails unless every predicate passed. Predicates are their own
     * assertion layer — they apply regardless of `failOnToolError`.
     */
    predicateResults?: ReadonlyArray<{
        passed: boolean;
    }>;
};
/**
 * Combine structural pass/fail with tool execution outcomes for eval reporting.
 */
declare function finalizePassedForEval(params: FinalizeEvalPassedParams): boolean;

export { type ArgMatcher as A, type CasePredicates as C, type FinalizeEvalPassedParams as F, type IterationTranscript as I, type PredicateResult as P, type TranscriptToolCall as T, type Predicate as a, type TranscriptUsage as b, type ArgMatchMode as c, PREDICATE_PLACEHOLDER_STRINGS as d, type PredicatePlaceholder as e, type PredicateType as f, type ToolErrorKind as g, type ToolErrorRecord as h, argMatcherSchema as i, casePredicatesSchema as j, extractToolErrors as k, predicateSchema as l, finalizePassedForEval as m, isCallToolResultError as n, traceMessagePartIndicatesToolFailure as o, predicateArraySchema as p, traceIndicatesToolExecutionFailure as t };
