import { resolveClawdbotAgentDir } from "../agents/agent-paths.js";
import { upsertAuthProfile } from "../agents/auth-profiles.js";
export { CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF } from "../agents/cloudflare-ai-gateway.js";
const resolveAuthAgentDir = (agentDir) => agentDir ?? resolveClawdbotAgentDir();
export async function writeOAuthCredentials(provider, creds, agentDir) {
    const email = typeof creds.email === "string" && creds.email.trim() ? creds.email.trim() : "default";
    upsertAuthProfile({
        profileId: `${provider}:${email}`,
        credential: {
            type: "oauth",
            provider,
            ...creds,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setAnthropicApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "anthropic:default",
        credential: {
            type: "api_key",
            provider: "anthropic",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setGeminiApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "google:default",
        credential: {
            type: "api_key",
            provider: "google",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setMinimaxApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "minimax:default",
        credential: {
            type: "api_key",
            provider: "minimax",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setMoonshotApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "moonshot:default",
        credential: {
            type: "api_key",
            provider: "moonshot",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setKimiCodingApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "kimi-coding:default",
        credential: {
            type: "api_key",
            provider: "kimi-coding",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setSyntheticApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "synthetic:default",
        credential: {
            type: "api_key",
            provider: "synthetic",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setVeniceApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "venice:default",
        credential: {
            type: "api_key",
            provider: "venice",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export const ZAI_DEFAULT_MODEL_REF = "zai/glm-5";
export const XIAOMI_DEFAULT_MODEL_REF = "xiaomi/mimo-v2-flash";
export const OPENROUTER_DEFAULT_MODEL_REF = "openrouter/auto";
export const VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF = "vercel-ai-gateway/anthropic/claude-opus-4.5";
export async function setZaiApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "zai:default",
        credential: {
            type: "api_key",
            provider: "zai",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setXiaomiApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "xiaomi:default",
        credential: {
            type: "api_key",
            provider: "xiaomi",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setOpenrouterApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "openrouter:default",
        credential: {
            type: "api_key",
            provider: "openrouter",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setCloudflareAiGatewayConfig(accountId, gatewayId, apiKey, agentDir) {
    const normalizedAccountId = accountId.trim();
    const normalizedGatewayId = gatewayId.trim();
    const normalizedKey = apiKey.trim();
    upsertAuthProfile({
        profileId: "cloudflare-ai-gateway:default",
        credential: {
            type: "api_key",
            provider: "cloudflare-ai-gateway",
            key: normalizedKey,
            metadata: {
                accountId: normalizedAccountId,
                gatewayId: normalizedGatewayId,
            },
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setVercelAiGatewayApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "vercel-ai-gateway:default",
        credential: {
            type: "api_key",
            provider: "vercel-ai-gateway",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setOpencodeZenApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "opencode:default",
        credential: {
            type: "api_key",
            provider: "opencode",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setEphoneApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "ephone:default",
        credential: {
            type: "api_key",
            provider: "ephone",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
// 新增：硅基流动 API Key 写入到代理目录
export async function setSiliconflowApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "siliconflow:default",
        credential: {
            type: "api_key",
            provider: "siliconflow",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
// 新增：阿里云百炼（DashScope） API Key 写入到代理目录
export async function setDashscopeApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "dashscope:default",
        credential: {
            type: "api_key",
            provider: "dashscope",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
export async function setDashscopeCodingPlanApiKey(key, agentDir) {
    // Write to resolved agent dir so gateway finds credentials on startup.
    upsertAuthProfile({
        profileId: "dashscope-coding-plan:default",
        credential: {
            type: "api_key",
            provider: "dashscope-coding-plan",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
// 新增：火山引擎 Coding Plan API Key 写入到代理目录
export async function setVolcengineCodingPlanApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "volcengine-coding-plan:default",
        credential: {
            type: "api_key",
            provider: "volcengine-coding-plan",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
// 新增：Kimi Coding Plan API Key 写入到代理目录
export async function setMoonshotCodingPlanApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "moonshot-coding-plan:default",
        credential: {
            type: "api_key",
            provider: "moonshot-coding-plan",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
// 新增：DeepSeek API Key 写入到代理目录
export async function setDeepseekApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "deepseek:default",
        credential: {
            type: "api_key",
            provider: "deepseek",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
// 新增：火山引擎（Volcengine） API Key 写入到代理目录
export async function setVolcengineApiKey(key, agentDir) {
    upsertAuthProfile({
        profileId: "volcengine:default",
        credential: {
            type: "api_key",
            provider: "volcengine",
            key,
        },
        agentDir: resolveAuthAgentDir(agentDir),
    });
}
