import { config } from "dotenv";
import { resolve, dirname, join } from "path";
import { fileURLToPath } from "url";
import { existsSync } from "fs";
import { homedir } from "os";
import { randomBytes } from "crypto";
// Skip dotenv loading if already handled by CLI entry (bin/jait.mjs)
if (!process.env["__JAIT_CLI"]) {
    const __dirname = dirname(fileURLToPath(import.meta.url));
    // Try .env locations in priority order:
    //   1. CWD/.env  (user project)
    //   2. ~/.jait/.env  (global config)
    //   3. Monorepo root (dev; 3 levels up from src/config.ts)
    const candidates = [
        resolve(process.cwd(), ".env"),
        join(homedir(), ".jait", ".env"),
        resolve(__dirname, "../../../.env"),
    ];
    const envPath = candidates.find((p) => existsSync(p));
    if (envPath)
        config({ path: envPath });
}
/** Infer context window size from model name. Conservative defaults. */
export function inferContextWindow(model) {
    const m = model.toLowerCase();
    if (m.includes("gpt-4o") || m.includes("gpt-4.1"))
        return 128_000;
    if (m.includes("gpt-4-turbo"))
        return 128_000;
    if (m.includes("gpt-4"))
        return 8_192;
    if (m.includes("gpt-3.5"))
        return 16_385;
    if (m.includes("claude-3") || m.includes("claude-4"))
        return 200_000;
    if (m.includes("claude"))
        return 100_000;
    if (m.includes("gemini"))
        return 128_000;
    if (m.includes("o1") || m.includes("o3") || m.includes("o4"))
        return 200_000;
    if (m.includes("deepseek"))
        return 64_000;
    if (m.includes("mistral") || m.includes("mixtral"))
        return 32_000;
    if (m.includes("llama"))
        return 8_192;
    return 128_000; // safe default
}
export function loadConfig() {
    // Auto-detect provider: if OPENAI_API_KEY is set, default to openai
    const hasOpenAiKey = !!process.env["OPENAI_API_KEY"];
    const explicitProvider = process.env["LLM_PROVIDER"];
    const jwtSecret = process.env["JWT_SECRET"]?.trim() || randomBytes(32).toString("hex");
    const hookSecret = process.env["HOOK_SECRET"]?.trim() || randomBytes(32).toString("hex");
    return {
        port: parseInt(process.env["PORT"] ?? "8000", 10),
        wsPort: parseInt(process.env["WS_PORT"] ?? "18789", 10),
        host: process.env["HOST"] ?? "0.0.0.0",
        logLevel: process.env["LOG_LEVEL"] ?? "info",
        corsOrigin: process.env["CORS_ORIGIN"] ?? "http://localhost:3000",
        nodeEnv: process.env["NODE_ENV"] ?? "development",
        jwtSecret,
        llmProvider: explicitProvider ?? (hasOpenAiKey ? "openai" : "ollama"),
        ollamaUrl: process.env["OLLAMA_URL"] ?? "http://localhost:11434",
        ollamaModel: process.env["OLLAMA_MODEL"] ??
            "CognitiveComputations/dolphin-mistral-nemo:12b",
        openaiApiKey: process.env["OPENAI_API_KEY"] ?? "",
        openaiModel: process.env["OPENAI_MODEL"] ?? "gpt-4o",
        openaiBaseUrl: process.env["OPENAI_BASE_URL"] ?? "https://api.openai.com/v1",
        contextWindow: parseInt(process.env["CONTEXT_WINDOW"] ?? "0", 10) || inferContextWindow(process.env["OPENAI_MODEL"] ?? "gpt-4o"),
        hookSecret,
        heartbeatCron: process.env["HEARTBEAT_CRON"] ?? "* * * * *",
        whisperUrl: process.env["WHISPER_URL"] ?? "http://localhost:8178",
        realtimeModel: process.env["OPENAI_REALTIME_MODEL"] ?? "gpt-4o-realtime-preview",
        realtimeVoice: process.env["OPENAI_REALTIME_VOICE"] ?? "alloy",
    };
}
//# sourceMappingURL=config.js.map