import {
  getConfig
} from "../chunk-2MAQ6JFL.js";
import "../chunk-HFDLUBWN.js";
import "../chunk-RII4M3J2.js";
import "../chunk-DDJOPRB7.js";
import "../chunk-TLEKKGH7.js";
import "../chunk-KBM3MISV.js";
import "../chunk-2ESYSVXG.js";

// src/sessions/grader.ts
var GRADER_SYSTEM = `You are an evaluation grader. You evaluate whether an agent's work output satisfies a rubric.

You MUST call the evaluate_outcome tool with your assessment. Do not respond with plain text.`;
var EVALUATE_TOOL = {
  name: "evaluate_outcome",
  description: "Submit your evaluation of the agent's output against the rubric.",
  input_schema: {
    type: "object",
    properties: {
      result: {
        type: "string",
        enum: ["satisfied", "needs_revision", "failed"],
        description: "satisfied: output meets the rubric. needs_revision: output is close but needs specific changes. failed: output fundamentally fails to meet the rubric."
      },
      feedback: {
        type: "string",
        description: "Brief explanation of the evaluation. If needs_revision, include specific actionable feedback for the agent."
      }
    },
    required: ["result", "feedback"]
  }
};
async function runGraderEvaluation(rubric, agentOutput, model) {
  const cfg = getConfig();
  const apiKey = cfg.anthropicApiKey;
  if (!apiKey) {
    return {
      result: "satisfied",
      feedback: "Grader skipped: no ANTHROPIC_API_KEY configured for direct API evaluation.",
      usage: { input_tokens: 0, output_tokens: 0 }
    };
  }
  const userMessage = `## Rubric
${rubric}

## Agent Output
${agentOutput}`;
  const response = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: {
      "content-type": "application/json",
      "x-api-key": apiKey,
      "anthropic-version": "2023-06-01"
    },
    body: JSON.stringify({
      model: model || "claude-sonnet-4-20250514",
      max_tokens: 1024,
      system: GRADER_SYSTEM,
      tools: [EVALUATE_TOOL],
      tool_choice: { type: "tool", name: "evaluate_outcome" },
      messages: [{ role: "user", content: userMessage }]
    })
  });
  if (!response.ok) {
    const errText = await response.text().catch(() => "");
    console.warn(`[grader] API call failed (${response.status}): ${errText}`);
    return {
      result: "satisfied",
      feedback: `Grader API error (${response.status}), defaulting to satisfied.`,
      usage: { input_tokens: 0, output_tokens: 0 }
    };
  }
  const data = await response.json();
  const toolUse = data.content.find(
    (b) => b.type === "tool_use" && b.name === "evaluate_outcome"
  );
  if (!toolUse?.input?.result) {
    return {
      result: "satisfied",
      feedback: "Grader returned no structured result, defaulting to satisfied.",
      usage: data.usage ?? { input_tokens: 0, output_tokens: 0 }
    };
  }
  const validResults = ["satisfied", "needs_revision", "failed"];
  const result = validResults.includes(toolUse.input.result) ? toolUse.input.result : "satisfied";
  return {
    result,
    feedback: toolUse.input.feedback ?? "",
    usage: data.usage ?? { input_tokens: 0, output_tokens: 0 }
  };
}
export {
  runGraderEvaluation
};
