import { BestAgentError } from "@best-agent/foundation";
import type { ConversationMessage, ProviderToolOption } from "../../provider/provider-core.js";
import { validateProviderResponseFinalAnswer } from "./provider-final-answer-handler.js";

export function assertValidProviderProseResponse(input: {
  assertValidTerminalAssistantText: (text: string) => string;
  availableTools: readonly ProviderToolOption[];
  messages: ConversationMessage[];
  objective?: string;
  text: string;
}): string {
  const text = input.assertValidTerminalAssistantText(input.text);
  const finalValidation = validateProviderResponseFinalAnswer({
    availableTools: input.availableTools,
    messages: input.messages,
    objective: input.objective,
    text,
  });
  if (!finalValidation.ok) {
    throw new BestAgentError("PROVIDER_DECISION_INVALID", finalValidation.observation.message);
  }
  return text;
}
