/**
 * Routes workspace AI chat to Gemini or an OpenAI-compatible API (e.g. DeepSeek).
 *
 * Env:
 *   VIBN_CHAT_PROVIDER=gemini | deepseek | openai_compatible
 *
 * Default: gemini (requires GOOGLE_API_KEY / studio key + VIBN_CHAT_MODEL).
 *
 * DeepSeek / OpenAI-compat:
 *   DEEPSEEK_API_KEY (or VIBN_OPENAI_COMPATIBLE_API_KEY)
 *   Optional: VIBN_OPENAI_COMPATIBLE_CHAT_URL (default https://api.deepseek.com/chat/completions)
 *   Optional: VIBN_OPENAI_COMPATIBLE_MODEL (default deepseek-chat)
 */

import type { ChatMessage, ToolDefinition } from "./gemini-chat";
import { callGeminiChat } from "./gemini-chat";
import { callOpenAiCompatibleChat } from "./openai-compatible-chat";

export type VibnChatCallOpts = {
  systemPrompt: string;
  messages: ChatMessage[];
  tools?: ToolDefinition[];
  temperature?: number;
  includeThoughts?: boolean;
  /** Forwarded to the provider so a Stop click cancels the live request. */
  signal?: AbortSignal;
};

export async function callVibnChat(opts: VibnChatCallOpts) {
  const p = (process.env.VIBN_CHAT_PROVIDER || "gemini").toLowerCase().trim();
  if (p === "deepseek" || p === "openai_compatible") {
    return callOpenAiCompatibleChat(opts);
  }
  return callGeminiChat(opts);
}
