feat: CLI Integration Suite for issue #2016

- Add tool-detector.ts (6 CLI tools: claude, codex, opencode, cline, kilocode, continue)
- Add config-generator/ factory + 6 generators (JSON + YAML)
- Add doctor/checks.ts for CLI tool health checks
- Add log-streamer.ts for usage log streaming
- Add @omniroute/opencode-provider npm package
- Add 5 CLI commands: config, status, logs, update, provider
- Add 3 API routes: config, detect, apply
- Update bin/omniroute.mjs, bin/cli/index.mjs, package.json
- Update docs: SETUP_GUIDE.md, CLI-TOOLS.md
- All tests pass (4302/4326, 24 pre-existing failures unchanged)
This commit is contained in:
oyi77
2026-05-14 07:38:01 +07:00
parent bf83aa55de
commit 2d601ea459
35 changed files with 3806 additions and 403 deletions

View File

@@ -0,0 +1,82 @@
import { NextResponse } from "next/server";
import { requireCliToolsAuth } from "@/lib/api/requireCliToolsAuth";
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
import { generateConfig } from "@/lib/cli-helper/config-generator";
const TOOL_CONFIG_PATHS: Record<string, string> = {
claude: path.join(os.homedir(), ".claude", "settings.json"),
codex: path.join(os.homedir(), ".codex", "config.yaml"),
opencode: path.join(os.homedir(), ".config", "opencode", "opencode.json"),
cline: path.join(os.homedir(), ".cline", "data", "globalState.json"),
kilocode: path.join(os.homedir(), ".config", "kilocode", "settings.json"),
continue: path.join(os.homedir(), ".continue", "config.yaml"),
};
function ensureBackup(configPath: string): string | null {
if (!fs.existsSync(configPath)) return null;
const backupDir = path.join(path.dirname(configPath), ".omniroute.bak");
if (!fs.existsSync(backupDir)) fs.mkdirSync(backupDir, { recursive: true });
const backupPath = path.join(backupDir, path.basename(configPath) + ".bak");
fs.copyFileSync(configPath, backupPath);
return backupPath;
}
// POST /api/cli-tools/apply - Apply config for a specific tool
export async function POST(request: Request) {
const authError = await requireCliToolsAuth(request);
if (authError) return authError;
try {
const body = await request.json();
const { toolId, baseUrl, apiKey, model, dryRun } = body;
if (!toolId) {
return NextResponse.json({ error: "toolId is required" }, { status: 400 });
}
if (!apiKey) {
return NextResponse.json({ error: "apiKey is required" }, { status: 400 });
}
const result = await generateConfig(toolId, {
baseUrl: baseUrl || "http://localhost:20128/v1",
apiKey,
model,
});
if (!result.success) {
return NextResponse.json({ error: result.error }, { status: 400 });
}
if (dryRun) {
return NextResponse.json({
dryRun: true,
configPath: result.configPath,
content: result.content,
});
}
const configPath = TOOL_CONFIG_PATHS[toolId];
if (!configPath) {
return NextResponse.json({ error: `Unknown tool: ${toolId}` }, { status: 400 });
}
const backupPath = ensureBackup(configPath);
const dir = path.dirname(configPath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(configPath, result.content!, "utf-8");
return NextResponse.json({
success: true,
configPath,
backupPath,
content: result.content,
});
} catch (error) {
console.log("Error applying config:", error);
return NextResponse.json({ error: "Failed to apply config" }, { status: 500 });
}
}

View File

@@ -0,0 +1,61 @@
import { NextResponse } from "next/server";
import { requireCliToolsAuth } from "@/lib/api/requireCliToolsAuth";
import { generateConfig, generateAllConfigs } from "@/lib/cli-helper/config-generator";
// GET /api/cli-tools/config - List generated configs for all tools
export async function GET(request: Request) {
const authError = await requireCliToolsAuth(request);
if (authError) return authError;
const { searchParams } = new URL(request.url);
const baseUrl = searchParams.get("baseUrl") || "http://localhost:20128/v1";
const apiKey = searchParams.get("apiKey") || "";
if (!apiKey) {
return NextResponse.json({ error: "API key is required" }, { status: 400 });
}
try {
const results = await generateAllConfigs({ baseUrl, apiKey });
return NextResponse.json({ configs: results });
} catch (error) {
console.log("Error generating configs:", error);
return NextResponse.json({ error: "Failed to generate configs" }, { status: 500 });
}
}
// POST /api/cli-tools/config - Generate config for a specific tool
export async function POST(request: Request) {
const authError = await requireCliToolsAuth(request);
if (authError) return authError;
try {
const body = await request.json();
const { toolId, baseUrl, apiKey, model } = body;
if (!toolId) {
return NextResponse.json({ error: "toolId is required" }, { status: 400 });
}
if (!apiKey) {
return NextResponse.json({ error: "apiKey is required" }, { status: 400 });
}
const result = await generateConfig(toolId, {
baseUrl: baseUrl || "http://localhost:20128/v1",
apiKey,
model,
});
if (!result.success) {
return NextResponse.json({ error: result.error }, { status: 400 });
}
return NextResponse.json({
configPath: result.configPath,
content: result.content,
});
} catch (error) {
console.log("Error generating config:", error);
return NextResponse.json({ error: "Failed to generate config" }, { status: 500 });
}
}

View File

@@ -0,0 +1,28 @@
import { NextResponse } from "next/server";
import { requireCliToolsAuth } from "@/lib/api/requireCliToolsAuth";
import { detectAllTools, detectTool } from "@/lib/cli-helper/tool-detector";
// GET /api/cli-tools/detect - Detect all installed CLI tools
export async function GET(request: Request) {
const authError = await requireCliToolsAuth(request);
if (authError) return authError;
const { searchParams } = new URL(request.url);
const toolId = searchParams.get("tool");
try {
if (toolId) {
const tool = await detectTool(toolId);
if (!tool) {
return NextResponse.json({ error: `Unknown tool: ${toolId}` }, { status: 400 });
}
return NextResponse.json(tool);
}
const tools = await detectAllTools();
return NextResponse.json({ tools });
} catch (error) {
console.log("Error detecting tools:", error);
return NextResponse.json({ error: "Failed to detect tools" }, { status: 500 });
}
}