"use strict";
/**
 * Zod schemas for SDK message validation.
 *
 * These schemas enforce the structural types defined in types.ts at runtime.
 * Use `safeParseSDKMessage` at ingestion boundaries (the for-await loop in
 * agent-interface.ts and the middleware pipeline) so malformed messages are
 * logged and skipped rather than causing uncaught property-access errors.
 *
 * The SDK message schema is a `z.discriminatedUnion('type', ...)` so each
 * variant (assistant, user, system, result, etc.) gets per-branch validation
 * and zod can narrow on `message.type` for downstream consumers. Each branch
 * uses `.passthrough()` so unknown forward-compatible fields don't cause
 * parse failures — the SDK adds new fields between minor versions and the
 * wizard has historically tolerated that.
 *
 * The `Other` branch (any unknown `type` string) is intentional: when the
 * SDK ships a brand new top-level message variant we want to log and skip
 * it, not throw. We accept any string `type` there and let the consuming
 * switch fall through to the default arm.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sdkMessageSchema = exports.sdkOtherMessageSchema = exports.sdkStreamEventMessageSchema = exports.sdkResultMessageSchema = exports.sdkSystemMessageSchema = exports.sdkUserMessageSchema = exports.sdkAssistantMessageSchema = exports.sdkModelUsageEntrySchema = exports.sdkCompactMetadataSchema = exports.sdkContentBlockSchema = exports.sdkUsageSchema = void 0;
exports.safeParseSDKMessage = safeParseSDKMessage;
const zod_1 = require("zod");
// ── Leaf schemas ────────────────────────────────────────────────────────────
exports.sdkUsageSchema = zod_1.z.object({
    input_tokens: zod_1.z.number().optional(),
    output_tokens: zod_1.z.number().optional(),
    cache_read_input_tokens: zod_1.z.number().optional(),
    cache_creation_input_tokens: zod_1.z.number().optional(),
    cache_creation: zod_1.z
        .object({
        ephemeral_5m_input_tokens: zod_1.z.number().optional(),
        ephemeral_1h_input_tokens: zod_1.z.number().optional(),
    })
        .optional(),
    total_cost_usd: zod_1.z.number().optional(),
});
exports.sdkContentBlockSchema = zod_1.z
    .object({
    type: zod_1.z.string(),
    text: zod_1.z.string().optional(),
    name: zod_1.z.string().optional(),
    input: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional(),
})
    .passthrough();
exports.sdkCompactMetadataSchema = zod_1.z.object({
    pre_tokens: zod_1.z.number().optional(),
    trigger: zod_1.z.string().optional(),
});
exports.sdkModelUsageEntrySchema = zod_1.z.object({
    inputTokens: zod_1.z.number().optional(),
    outputTokens: zod_1.z.number().optional(),
    cacheCreationInputTokens: zod_1.z.number().optional(),
    cacheReadInputTokens: zod_1.z.number().optional(),
});
const sdkMessageBodySchema = zod_1.z
    .object({
    id: zod_1.z.string().optional(),
    usage: exports.sdkUsageSchema.optional(),
    content: zod_1.z.array(exports.sdkContentBlockSchema).optional(),
})
    .passthrough();
const sdkMcpServerSchema = zod_1.z
    .object({ name: zod_1.z.string(), status: zod_1.z.string() })
    .passthrough();
// ── Per-type branch schemas ─────────────────────────────────────────────────
//
// Each branch covers a `type` literal observed in the SDK union
// (see node_modules/@anthropic-ai/claude-agent-sdk/sdk.d.ts → SDKMessage).
// Required fields stay required; optional / forward-compat fields are
// modeled loosely with `.passthrough()` on each branch.
exports.sdkAssistantMessageSchema = zod_1.z
    .object({
    type: zod_1.z.literal('assistant'),
    subtype: zod_1.z.string().optional(),
    message: sdkMessageBodySchema.optional(),
    usage: exports.sdkUsageSchema.optional(),
    total_cost_usd: zod_1.z.number().optional(),
    modelUsage: zod_1.z.record(zod_1.z.string(), exports.sdkModelUsageEntrySchema).optional(),
})
    .passthrough();
exports.sdkUserMessageSchema = zod_1.z
    .object({
    type: zod_1.z.literal('user'),
    subtype: zod_1.z.string().optional(),
    message: sdkMessageBodySchema.optional(),
})
    .passthrough();
exports.sdkSystemMessageSchema = zod_1.z
    .object({
    type: zod_1.z.literal('system'),
    subtype: zod_1.z.string().optional(),
    model: zod_1.z.string().optional(),
    tools: zod_1.z.array(zod_1.z.unknown()).optional(),
    mcp_servers: zod_1.z.array(sdkMcpServerSchema).optional(),
    compact_metadata: exports.sdkCompactMetadataSchema.optional(),
})
    .passthrough();
exports.sdkResultMessageSchema = zod_1.z
    .object({
    type: zod_1.z.literal('result'),
    subtype: zod_1.z.string().optional(),
    is_error: zod_1.z.boolean().optional(),
    result: zod_1.z.string().optional(),
    errors: zod_1.z.array(zod_1.z.string()).optional(),
    usage: exports.sdkUsageSchema.optional(),
    total_cost_usd: zod_1.z.number().optional(),
    num_turns: zod_1.z.number().optional(),
    modelUsage: zod_1.z.record(zod_1.z.string(), exports.sdkModelUsageEntrySchema).optional(),
})
    .passthrough();
exports.sdkStreamEventMessageSchema = zod_1.z
    .object({
    type: zod_1.z.literal('stream_event'),
    subtype: zod_1.z.string().optional(),
})
    .passthrough();
/**
 * Catch-all branch for SDK message types the wizard doesn't model explicitly
 * (status, auth_status, tool_progress, rate_limit_event, prompt_suggestion,
 * tool_use_summary, etc.). Captures any non-matching `type` string so unknown
 * variants parse successfully and downstream switch statements fall through
 * to their default arm rather than crashing.
 */
exports.sdkOtherMessageSchema = zod_1.z
    .object({
    type: zod_1.z.string(),
    subtype: zod_1.z.string().optional(),
})
    .passthrough()
    .refine((v) => v.type !== 'assistant' &&
    v.type !== 'user' &&
    v.type !== 'system' &&
    v.type !== 'result' &&
    v.type !== 'stream_event', { message: 'matched by a more-specific branch' });
// ── Discriminated union ─────────────────────────────────────────────────────
const sdkKnownMessageSchema = zod_1.z.discriminatedUnion('type', [
    exports.sdkAssistantMessageSchema,
    exports.sdkUserMessageSchema,
    exports.sdkSystemMessageSchema,
    exports.sdkResultMessageSchema,
    exports.sdkStreamEventMessageSchema,
]);
/**
 * Validates one SDK message. Tries the discriminated union of known types
 * first (assistant / user / system / result / stream_event); falls back to
 * the catch-all `sdkOtherMessageSchema` for forward-compatible unknown types.
 *
 * Cast to `z.ZodType<SDKMessage>` because the structural `SDKMessage` interface
 * in types.ts has a `[key: string]: unknown` index signature, so each branch
 * (with `.passthrough()`) is a structural subtype.
 */
exports.sdkMessageSchema = zod_1.z.union([
    sdkKnownMessageSchema,
    exports.sdkOtherMessageSchema,
]);
/**
 * Validate an unknown value as an SDKMessage.
 * Returns a discriminated result so callers can log and skip bad messages.
 */
function safeParseSDKMessage(value) {
    const result = exports.sdkMessageSchema.safeParse(value);
    if (result.success) {
        return { ok: true, message: result.data };
    }
    return { ok: false, error: result.error };
}
