@bitkyc08/opencodex
Version:
Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code
103 lines (96 loc) • 3.95 kB
text/typescript
import {
CliUsageError,
RuntimeApiError,
printData,
rejectArgs,
runCliAction,
runtimeRequest,
takeFlag,
takeIntegerOption,
type RuntimeApiDeps,
} from "./runtime-api";
const USAGE = `Usage:
ocx route policy create <id> --file <FILE|-> [--json]
ocx route policy update <id> --file <FILE|-> --expected-revision <revision> [--json]
ocx route policy remove <id> --yes [--json]
ocx route policy list [--json]
ocx route policy show <id> [--json]
ocx route policy dry-run <id> [--model-context <tokens>] [--tools]
[--image] [--structured-output] [--json]
ocx route policy evaluate <id> [--model-context <tokens>] [--tools]
[--image] [--structured-output] [--json]`;
interface ProfileRow {
id?: string;
model?: string;
revision?: string;
}
async function list(argv: string[], deps: RuntimeApiDeps): Promise<void> {
const args = [...argv];
const wantsJson = takeFlag(args, "--json");
rejectArgs(args, USAGE);
const result = await runtimeRequest<{ profiles?: ProfileRow[] }>("/api/routing-profiles", {}, deps);
const rows = result.profiles ?? [];
printData(
result,
wantsJson,
rows.length
? rows.map(row => `${String(row.id)} ${String(row.model ?? `policy/${row.id}`)} rev:${String(row.revision ?? "-")}`)
: ["No routing profiles configured."],
);
}
/** Show an existing routing profile, distinguishing malformed usage from a missing record. */
async function show(argv: string[], deps: RuntimeApiDeps): Promise<void> {
const args = [...argv];
const id = args.shift();
const wantsJson = takeFlag(args, "--json");
if (!id || id.startsWith("-")) throw new CliUsageError("profile id is required", USAGE);
rejectArgs(args, USAGE);
const result = await runtimeRequest<{ profiles?: ProfileRow[] }>("/api/routing-profiles", {}, deps);
const profile = (result.profiles ?? []).find(candidate => candidate.id === id);
if (!profile) throw new RuntimeApiError(`unknown routing profile: ${id}`, 404, null);
printData(profile, wantsJson);
}
async function dryRun(argv: string[], deps: RuntimeApiDeps): Promise<void> {
const args = [...argv];
const id = args.shift();
const wantsJson = takeFlag(args, "--json");
if (!id || id.startsWith("-")) throw new CliUsageError("profile id is required", USAGE);
const modelContext = takeIntegerOption(args, "--model-context", { min: 1 });
const tools = takeFlag(args, "--tools");
const image = takeFlag(args, "--image");
const structuredOutput = takeFlag(args, "--structured-output");
rejectArgs(args, USAGE);
const result = await runtimeRequest(
"/api/routing-profiles/dry-run",
{
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
profile: id,
evidence: {
...(modelContext !== undefined ? { contextWindow: modelContext } : {}),
...(tools ? { toolsRequired: true } : {}),
...(image ? { imageInputRequired: true } : {}),
...(structuredOutput ? { structuredOutputRequired: true } : {}),
},
}),
},
deps,
);
printData(result, wantsJson);
}
export async function handleRoutePolicyCommand(argv: string[], deps: RuntimeApiDeps = {}): Promise<number> {
const [write, ...rest] = argv;
if (write === "create" || write === "update" || write === "remove") {
const { handleRoutePolicyWriteCommand } = await import("./route-policy-write");
return handleRoutePolicyWriteCommand(write, rest, deps);
}
return runCliAction(async () => {
const [sub, ...rest] = argv;
if (!sub) throw new CliUsageError("route policy requires a subcommand (list, show, dry-run, evaluate)", USAGE);
if (sub === "list") await list(rest, deps);
else if (sub === "show") await show(rest, deps);
else if (sub === "dry-run" || sub === "evaluate") await dryRun(rest, deps);
else throw new CliUsageError(`unknown route policy command: ${sub}`, USAGE);
});
}