mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-23 04:09:49 +00:00
feat(cli-tools): update CLI tools and add new models
- Add Droid and OpenClaw tool cards to CLI tools - Enhance ClaudeToolCard and CodexToolCard to display current base URLs
This commit is contained in:
@@ -102,6 +102,13 @@ export async function POST(request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize ANTHROPIC_BASE_URL to ensure /v1 suffix
|
||||
if (env.ANTHROPIC_BASE_URL) {
|
||||
env.ANTHROPIC_BASE_URL = env.ANTHROPIC_BASE_URL.endsWith("/v1")
|
||||
? env.ANTHROPIC_BASE_URL
|
||||
: `${env.ANTHROPIC_BASE_URL}/v1`;
|
||||
}
|
||||
|
||||
// Merge new env with existing settings
|
||||
const newSettings = {
|
||||
...currentSettings,
|
||||
|
||||
@@ -155,9 +155,11 @@ export async function POST(request) {
|
||||
parsed._root.model_provider = "9router";
|
||||
|
||||
// Update or create 9router provider section (no api_key - Codex reads from auth.json)
|
||||
// Ensure /v1 suffix is added only once
|
||||
const normalizedBaseUrl = baseUrl.endsWith("/v1") ? baseUrl : `${baseUrl}/v1`;
|
||||
parsed._sections["model_providers.9router"] = {
|
||||
name: "9Router",
|
||||
base_url: `${baseUrl}/v1`,
|
||||
base_url: normalizedBaseUrl,
|
||||
wire_api: "responses",
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
"use server";
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
import { exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
const getDroidDir = () => path.join(os.homedir(), ".factory");
|
||||
const getDroidSettingsPath = () => path.join(getDroidDir(), "settings.json");
|
||||
|
||||
// Check if droid CLI is installed
|
||||
const checkDroidInstalled = async () => {
|
||||
try {
|
||||
const isWindows = os.platform() === "win32";
|
||||
const command = isWindows ? "where droid" : "which droid";
|
||||
await execAsync(command);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Read current settings.json
|
||||
const readSettings = async () => {
|
||||
try {
|
||||
const settingsPath = getDroidSettingsPath();
|
||||
const content = await fs.readFile(settingsPath, "utf-8");
|
||||
return JSON.parse(content);
|
||||
} catch (error) {
|
||||
if (error.code === "ENOENT") return null;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Check if settings has 9Router customModels
|
||||
const has9RouterConfig = (settings) => {
|
||||
if (!settings || !settings.customModels) return false;
|
||||
return settings.customModels.some(m => m.id === "custom:9Router-0");
|
||||
};
|
||||
|
||||
// GET - Check droid CLI and read current settings
|
||||
export async function GET() {
|
||||
try {
|
||||
const isInstalled = await checkDroidInstalled();
|
||||
|
||||
if (!isInstalled) {
|
||||
return NextResponse.json({
|
||||
installed: false,
|
||||
settings: null,
|
||||
message: "Factory Droid CLI is not installed",
|
||||
});
|
||||
}
|
||||
|
||||
const settings = await readSettings();
|
||||
|
||||
return NextResponse.json({
|
||||
installed: true,
|
||||
settings,
|
||||
has9Router: has9RouterConfig(settings),
|
||||
settingsPath: getDroidSettingsPath(),
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error checking droid settings:", error);
|
||||
return NextResponse.json({ error: "Failed to check droid settings" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Update 9Router customModels (merge with existing settings)
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const { baseUrl, apiKey, model } = await request.json();
|
||||
|
||||
if (!baseUrl || !model) {
|
||||
return NextResponse.json({ error: "baseUrl and model are required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const droidDir = getDroidDir();
|
||||
const settingsPath = getDroidSettingsPath();
|
||||
|
||||
// Ensure directory exists
|
||||
await fs.mkdir(droidDir, { recursive: true });
|
||||
|
||||
// Read existing settings or create new
|
||||
let settings = {};
|
||||
try {
|
||||
const existingSettings = await fs.readFile(settingsPath, "utf-8");
|
||||
settings = JSON.parse(existingSettings);
|
||||
} catch { /* No existing settings */ }
|
||||
|
||||
// Ensure customModels array exists
|
||||
if (!settings.customModels) {
|
||||
settings.customModels = [];
|
||||
}
|
||||
|
||||
// Remove existing 9Router config if any
|
||||
settings.customModels = settings.customModels.filter(m => m.id !== "custom:9Router-0");
|
||||
|
||||
// Normalize baseUrl to ensure /v1 suffix
|
||||
const normalizedBaseUrl = baseUrl.endsWith("/v1") ? baseUrl : `${baseUrl}/v1`;
|
||||
|
||||
// Add new 9Router config
|
||||
const customModel = {
|
||||
model: model,
|
||||
id: "custom:9Router-0",
|
||||
index: 0,
|
||||
baseUrl: normalizedBaseUrl,
|
||||
apiKey: apiKey || "your_api_key",
|
||||
displayName: model,
|
||||
maxOutputTokens: 131072,
|
||||
noImageSupport: false,
|
||||
provider: "openai",
|
||||
};
|
||||
|
||||
settings.customModels.unshift(customModel);
|
||||
|
||||
// Write settings
|
||||
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Factory Droid settings applied successfully!",
|
||||
settingsPath,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error updating droid settings:", error);
|
||||
return NextResponse.json({ error: "Failed to update droid settings" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Remove 9Router customModels only (keep other settings)
|
||||
export async function DELETE() {
|
||||
try {
|
||||
const settingsPath = getDroidSettingsPath();
|
||||
|
||||
// Read existing settings
|
||||
let settings = {};
|
||||
try {
|
||||
const existingSettings = await fs.readFile(settingsPath, "utf-8");
|
||||
settings = JSON.parse(existingSettings);
|
||||
} catch (error) {
|
||||
if (error.code === "ENOENT") {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "No settings file to reset",
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Remove 9Router customModels
|
||||
if (settings.customModels) {
|
||||
settings.customModels = settings.customModels.filter(m => m.id !== "custom:9Router-0");
|
||||
|
||||
// Remove customModels array if empty
|
||||
if (settings.customModels.length === 0) {
|
||||
delete settings.customModels;
|
||||
}
|
||||
}
|
||||
|
||||
// Write updated settings
|
||||
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "9Router settings removed successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error resetting droid settings:", error);
|
||||
return NextResponse.json({ error: "Failed to reset droid settings" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
"use server";
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
import { exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
const getOpenClawDir = () => path.join(os.homedir(), ".openclaw");
|
||||
const getOpenClawSettingsPath = () => path.join(getOpenClawDir(), "openclaw.json");
|
||||
|
||||
// Check if openclaw CLI is installed
|
||||
const checkOpenClawInstalled = async () => {
|
||||
try {
|
||||
const isWindows = os.platform() === "win32";
|
||||
const command = isWindows ? "where openclaw" : "which openclaw";
|
||||
await execAsync(command);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Read current settings.json
|
||||
const readSettings = async () => {
|
||||
try {
|
||||
const settingsPath = getOpenClawSettingsPath();
|
||||
const content = await fs.readFile(settingsPath, "utf-8");
|
||||
return JSON.parse(content);
|
||||
} catch (error) {
|
||||
if (error.code === "ENOENT") return null;
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Check if settings has 9Router config
|
||||
const has9RouterConfig = (settings) => {
|
||||
if (!settings || !settings.models || !settings.models.providers) return false;
|
||||
return !!settings.models.providers["9router"];
|
||||
};
|
||||
|
||||
// GET - Check openclaw CLI and read current settings
|
||||
export async function GET() {
|
||||
try {
|
||||
const isInstalled = await checkOpenClawInstalled();
|
||||
|
||||
if (!isInstalled) {
|
||||
return NextResponse.json({
|
||||
installed: false,
|
||||
settings: null,
|
||||
message: "Open Claw CLI is not installed",
|
||||
});
|
||||
}
|
||||
|
||||
const settings = await readSettings();
|
||||
|
||||
return NextResponse.json({
|
||||
installed: true,
|
||||
settings,
|
||||
has9Router: has9RouterConfig(settings),
|
||||
settingsPath: getOpenClawSettingsPath(),
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error checking openclaw settings:", error);
|
||||
return NextResponse.json({ error: "Failed to check openclaw settings" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Update 9Router settings (merge with existing settings)
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const { baseUrl, apiKey, model } = await request.json();
|
||||
|
||||
if (!baseUrl || !model) {
|
||||
return NextResponse.json({ error: "baseUrl and model are required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const openclawDir = getOpenClawDir();
|
||||
const settingsPath = getOpenClawSettingsPath();
|
||||
|
||||
// Ensure directory exists
|
||||
await fs.mkdir(openclawDir, { recursive: true });
|
||||
|
||||
// Read existing settings or create new
|
||||
let settings = {};
|
||||
try {
|
||||
const existingSettings = await fs.readFile(settingsPath, "utf-8");
|
||||
settings = JSON.parse(existingSettings);
|
||||
} catch { /* No existing settings */ }
|
||||
|
||||
// Ensure structure exists
|
||||
if (!settings.agents) settings.agents = {};
|
||||
if (!settings.agents.defaults) settings.agents.defaults = {};
|
||||
if (!settings.agents.defaults.model) settings.agents.defaults.model = {};
|
||||
if (!settings.models) settings.models = {};
|
||||
if (!settings.models.providers) settings.models.providers = {};
|
||||
|
||||
// Normalize baseUrl to ensure /v1 suffix
|
||||
const normalizedBaseUrl = baseUrl.endsWith("/v1") ? baseUrl : `${baseUrl}/v1`;
|
||||
|
||||
// Update agents.defaults.model.primary
|
||||
settings.agents.defaults.model.primary = `9router/${model}`;
|
||||
|
||||
// Update models.providers.9router
|
||||
settings.models.providers["9router"] = {
|
||||
baseUrl: normalizedBaseUrl,
|
||||
apiKey: apiKey || "your_api_key",
|
||||
api: "openai-completions",
|
||||
models: [
|
||||
{
|
||||
id: model,
|
||||
name: model.split("/").pop() || model,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// Write settings
|
||||
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Open Claw settings applied successfully!",
|
||||
settingsPath,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error updating openclaw settings:", error);
|
||||
return NextResponse.json({ error: "Failed to update openclaw settings" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Remove 9Router settings only (keep other settings)
|
||||
export async function DELETE() {
|
||||
try {
|
||||
const settingsPath = getOpenClawSettingsPath();
|
||||
|
||||
// Read existing settings
|
||||
let settings = {};
|
||||
try {
|
||||
const existingSettings = await fs.readFile(settingsPath, "utf-8");
|
||||
settings = JSON.parse(existingSettings);
|
||||
} catch (error) {
|
||||
if (error.code === "ENOENT") {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "No settings file to reset",
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Remove 9Router from models.providers
|
||||
if (settings.models && settings.models.providers) {
|
||||
delete settings.models.providers["9router"];
|
||||
|
||||
// Remove providers object if empty
|
||||
if (Object.keys(settings.models.providers).length === 0) {
|
||||
delete settings.models.providers;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset agents.defaults.model.primary if it uses 9router
|
||||
if (settings.agents?.defaults?.model?.primary?.startsWith("9router/")) {
|
||||
delete settings.agents.defaults.model.primary;
|
||||
}
|
||||
|
||||
// Write updated settings
|
||||
await fs.writeFile(settingsPath, JSON.stringify(settings, null, 2));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "9Router settings removed successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error resetting openclaw settings:", error);
|
||||
return NextResponse.json({ error: "Failed to reset openclaw settings" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user