| "use server";
|
|
|
| import { NextResponse } from "next/server";
|
| import fs from "fs/promises";
|
| import path from "path";
|
| import { requireCliToolsAuth } from "@/lib/api/requireCliToolsAuth";
|
| import {
|
| ensureCliConfigWriteAllowed,
|
| getCliConfigPaths,
|
| getCliRuntimeStatus,
|
| } from "@/shared/services/cliRuntime";
|
| import { createMultiBackup } from "@/shared/services/backupService";
|
| import { saveCliToolLastConfigured, deleteCliToolLastConfigured } from "@/lib/db/cliToolState";
|
| import { cliModelConfigSchema } from "@/shared/validation/schemas";
|
| import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
|
| import { getApiKeyById } from "@/lib/localDb";
|
| import { normalizeCodexBaseUrl } from "@/shared/utils/codexBaseUrl";
|
|
|
| const getCodexConfigPath = () => getCliConfigPaths("codex").config;
|
| const getCodexAuthPath = () => getCliConfigPaths("codex").auth;
|
| const getCodexDir = () => path.dirname(getCodexConfigPath());
|
|
|
|
|
| const parseToml = (content: string) => {
|
| const result: Record<string, any> = { _root: {}, _sections: {} };
|
| let currentSection = "_root";
|
|
|
| content.split("\n").forEach((line) => {
|
| const trimmed = line.trim();
|
| if (!trimmed || trimmed.startsWith("#")) return;
|
|
|
|
|
| const sectionMatch = trimmed.match(/^\[(.+)\]$/);
|
| if (sectionMatch) {
|
| currentSection = sectionMatch[1];
|
| result._sections[currentSection] = {};
|
| return;
|
| }
|
|
|
|
|
| const kvMatch = trimmed.match(/^([^=]+)\s*=\s*(.+)$/);
|
| if (kvMatch) {
|
| let key = kvMatch[1].trim();
|
| const rawValue = kvMatch[2].trim();
|
|
|
| if (
|
| (key.startsWith('"') && key.endsWith('"')) ||
|
| (key.startsWith("'") && key.endsWith("'"))
|
| ) {
|
| key = key.slice(1, -1);
|
| }
|
|
|
| let parsedValue: string | number | boolean = rawValue;
|
| if (rawValue === "true") {
|
| parsedValue = true;
|
| } else if (rawValue === "false") {
|
| parsedValue = false;
|
| } else if (
|
| (rawValue.startsWith('"') && rawValue.endsWith('"')) ||
|
| (rawValue.startsWith("'") && rawValue.endsWith("'"))
|
| ) {
|
|
|
| parsedValue = rawValue.slice(1, -1);
|
| } else if (/^-?\d+$/.test(rawValue)) {
|
|
|
| parsedValue = parseInt(rawValue, 10);
|
| } else if (/^-?\d+\.\d+$/.test(rawValue)) {
|
|
|
| parsedValue = parseFloat(rawValue);
|
| }
|
|
|
| if (currentSection === "_root") {
|
| result._root[key] = parsedValue;
|
| } else {
|
| result._sections[currentSection][key] = parsedValue;
|
| }
|
| }
|
| });
|
|
|
| return result;
|
| };
|
|
|
|
|
| const formatTomlValue = (value: unknown): string => {
|
| if (typeof value === "boolean") return value ? "true" : "false";
|
| if (typeof value === "number") return String(value);
|
|
|
| if (typeof value === "string" && value.startsWith("[") && value.endsWith("]")) return value;
|
| if (typeof value === "string") return `"${value}"`;
|
| return `"${value}"`;
|
| };
|
|
|
|
|
| const toToml = (parsed: Record<string, any>) => {
|
| let lines: string[] = [];
|
|
|
|
|
| Object.entries(parsed._root).forEach(([key, value]) => {
|
| lines.push(`${key} = ${formatTomlValue(value)}`);
|
| });
|
|
|
|
|
| Object.entries(parsed._sections).forEach(([section, values]) => {
|
| lines.push("");
|
| lines.push(`[${section}]`);
|
| Object.entries(values).forEach(([key, value]) => {
|
| const formattedKey = key.includes(".") ? `"${key}"` : key;
|
| lines.push(`${formattedKey} = ${formatTomlValue(value)}`);
|
| });
|
| });
|
|
|
| return lines.join("\n") + "\n";
|
| };
|
|
|
|
|
| const readConfig = async () => {
|
| try {
|
| const configPath = getCodexConfigPath();
|
| const content = await fs.readFile(configPath, "utf-8");
|
| return content;
|
| } catch (error: any) {
|
| if (error.code === "ENOENT") return null;
|
| throw error;
|
| }
|
| };
|
|
|
|
|
| const hasOmniRouteConfig = (config: string | null) => {
|
| if (!config) return false;
|
| return (
|
| config.includes("openai_base_url") ||
|
| config.includes('model_provider = "omniroute"') ||
|
| config.includes("[model_providers.omniroute]")
|
| );
|
| };
|
|
|
|
|
| export async function GET(request: Request) {
|
| const authError = await requireCliToolsAuth(request);
|
| if (authError) return authError;
|
|
|
| try {
|
| const runtime = await getCliRuntimeStatus("codex");
|
|
|
| if (!runtime.installed || !runtime.runnable) {
|
| return NextResponse.json({
|
| installed: runtime.installed,
|
| runnable: runtime.runnable,
|
| command: runtime.command,
|
| commandPath: runtime.commandPath,
|
| runtimeMode: runtime.runtimeMode,
|
| reason: runtime.reason,
|
| config: null,
|
| message:
|
| runtime.installed && !runtime.runnable
|
| ? "Codex CLI is installed but not runnable"
|
| : "Codex CLI is not installed",
|
| });
|
| }
|
|
|
| const config = await readConfig();
|
|
|
| return NextResponse.json({
|
| installed: runtime.installed,
|
| runnable: runtime.runnable,
|
| command: runtime.command,
|
| commandPath: runtime.commandPath,
|
| runtimeMode: runtime.runtimeMode,
|
| reason: runtime.reason,
|
| config,
|
| hasOmniRoute: hasOmniRouteConfig(config),
|
| configPath: getCodexConfigPath(),
|
| });
|
| } catch (error) {
|
| console.log("Error checking codex settings:", error);
|
| return NextResponse.json({ error: "Failed to check codex settings" }, { status: 500 });
|
| }
|
| }
|
|
|
|
|
| export async function POST(request: Request) {
|
| const authError = await requireCliToolsAuth(request);
|
| if (authError) return authError;
|
|
|
| let rawBody;
|
| try {
|
| rawBody = await request.json();
|
| } catch {
|
| return NextResponse.json(
|
| {
|
| error: {
|
| message: "Invalid request",
|
| details: [{ field: "body", message: "Invalid JSON body" }],
|
| },
|
| },
|
| { status: 400 }
|
| );
|
| }
|
|
|
| try {
|
| const writeGuard = ensureCliConfigWriteAllowed();
|
| if (writeGuard) {
|
| return NextResponse.json({ error: writeGuard }, { status: 403 });
|
| }
|
|
|
|
|
|
|
|
|
| const keyId = typeof rawBody?.keyId === "string" ? rawBody.keyId.trim() : null;
|
|
|
| const validation = validateBody(cliModelConfigSchema, rawBody);
|
| if (isValidationFailure(validation)) {
|
| return NextResponse.json({ error: validation.error }, { status: 400 });
|
| }
|
| const { baseUrl, model, reasoningEffort, wireApi, modelMappings } = validation.data;
|
| let { apiKey } = validation.data;
|
| if (!apiKey) {
|
| return NextResponse.json(
|
| { error: "baseUrl, apiKey and model are required" },
|
| { status: 400 }
|
| );
|
| }
|
|
|
|
|
| if (keyId) {
|
| try {
|
| const keyRecord = await getApiKeyById(keyId);
|
| if (keyRecord?.key) {
|
| apiKey = keyRecord.key as string;
|
| }
|
| } catch {
|
|
|
| }
|
| }
|
|
|
| const codexDir = getCodexDir();
|
| const configPath = getCodexConfigPath();
|
| const authPath = getCodexAuthPath();
|
|
|
|
|
| await fs.mkdir(codexDir, { recursive: true });
|
|
|
|
|
| await createMultiBackup("codex", [configPath, authPath]);
|
|
|
|
|
| let parsed: Record<string, any> = { _root: {}, _sections: {} };
|
| try {
|
| const existingConfig = await fs.readFile(configPath, "utf-8");
|
| parsed = parseToml(existingConfig);
|
| } catch {
|
|
|
| }
|
|
|
|
|
| parsed._root.model = model;
|
|
|
| if (reasoningEffort && reasoningEffort !== "none") {
|
|
|
| parsed._root.model_reasoning_effort = reasoningEffort;
|
| } else {
|
| delete parsed._root.model_reasoning_effort;
|
| }
|
|
|
| const normalizedBaseUrl = normalizeCodexBaseUrl(baseUrl, wireApi || "chat");
|
|
|
|
|
| parsed._root.model_provider = "omniroute";
|
| parsed._sections["model_providers.omniroute"] = {
|
| name: "OmniRoute",
|
| base_url: normalizedBaseUrl,
|
| wire_api: wireApi || "chat",
|
| env_key: "OPENAI_API_KEY",
|
| };
|
| delete parsed._root.openai_base_url;
|
|
|
|
|
| if (modelMappings && Object.keys(modelMappings).length > 0) {
|
| if (!parsed._sections["notice.model_migrations"]) {
|
| parsed._sections["notice.model_migrations"] = {};
|
| }
|
| for (const [from, to] of Object.entries(modelMappings)) {
|
| parsed._sections["notice.model_migrations"][from] = to;
|
| }
|
| } else {
|
| delete parsed._sections["notice.model_migrations"];
|
| }
|
|
|
|
|
| const configContent = toToml(parsed);
|
| await fs.writeFile(configPath, configContent);
|
|
|
|
|
| let authData: Record<string, any> = {};
|
| try {
|
| const existingAuth = await fs.readFile(authPath, "utf-8");
|
| authData = JSON.parse(existingAuth);
|
| } catch {
|
|
|
| }
|
|
|
| authData.OPENAI_API_KEY = apiKey;
|
| await fs.writeFile(authPath, JSON.stringify(authData, null, 2));
|
|
|
|
|
| try {
|
| saveCliToolLastConfigured("codex");
|
| } catch {
|
|
|
| }
|
|
|
| return NextResponse.json({
|
| success: true,
|
| message: "Codex settings applied successfully!",
|
| configPath,
|
| });
|
| } catch (error) {
|
| console.log("Error updating codex settings:", error);
|
| return NextResponse.json({ error: "Failed to update codex settings" }, { status: 500 });
|
| }
|
| }
|
|
|
|
|
| export async function DELETE(request: Request) {
|
| const authError = await requireCliToolsAuth(request);
|
| if (authError) return authError;
|
|
|
| try {
|
| const writeGuard = ensureCliConfigWriteAllowed();
|
| if (writeGuard) {
|
| return NextResponse.json({ error: writeGuard }, { status: 403 });
|
| }
|
|
|
| const configPath = getCodexConfigPath();
|
|
|
|
|
| await createMultiBackup("codex", [configPath, getCodexAuthPath()]);
|
|
|
|
|
| let parsed: Record<string, any> = { _root: {}, _sections: {} };
|
| try {
|
| const existingConfig = await fs.readFile(configPath, "utf-8");
|
| parsed = parseToml(existingConfig);
|
| } catch (error: any) {
|
| if (error.code === "ENOENT") {
|
| return NextResponse.json({
|
| success: true,
|
| message: "No config file to reset",
|
| });
|
| }
|
| throw error;
|
| }
|
|
|
|
|
| delete parsed._root.openai_base_url;
|
|
|
| if (parsed._root.model_provider === "omniroute") {
|
| delete parsed._root.model;
|
| delete parsed._root.model_provider;
|
| }
|
|
|
|
|
| delete parsed._sections["model_providers.omniroute"];
|
|
|
|
|
| const configContent = toToml(parsed);
|
| await fs.writeFile(configPath, configContent);
|
|
|
|
|
| const authPath = getCodexAuthPath();
|
| try {
|
| const existingAuth = await fs.readFile(authPath, "utf-8");
|
| const authData = JSON.parse(existingAuth);
|
| delete authData.OPENAI_API_KEY;
|
|
|
|
|
| if (Object.keys(authData).length === 0) {
|
| await fs.unlink(authPath);
|
| } else {
|
| await fs.writeFile(authPath, JSON.stringify(authData, null, 2));
|
| }
|
| } catch {
|
|
|
| }
|
|
|
|
|
| try {
|
| deleteCliToolLastConfigured("codex");
|
| } catch {
|
|
|
| }
|
|
|
| return NextResponse.json({
|
| success: true,
|
| message: "OmniRoute settings removed successfully",
|
| });
|
| } catch (error) {
|
| console.log("Error resetting codex settings:", error);
|
| return NextResponse.json({ error: "Failed to reset codex settings" }, { status: 500 });
|
| }
|
| }
|
|
|