init
This commit is contained in:
+188
@@ -0,0 +1,188 @@
|
||||
import Fastify, { type FastifyInstance } from "fastify";
|
||||
import { ZodError } from "zod";
|
||||
import type { BridgeConfig } from "./config.js";
|
||||
import {
|
||||
CommandCodeError,
|
||||
inspectInstallation,
|
||||
runCommandCode,
|
||||
type InstallationStatus,
|
||||
} from "./command-code.js";
|
||||
import {
|
||||
buildCommandPrompt,
|
||||
chatCompletionRequestSchema,
|
||||
completionResponse,
|
||||
completionStreamResponse,
|
||||
openAIError,
|
||||
} from "./openai.js";
|
||||
|
||||
interface ActiveRequest {
|
||||
abortController: AbortController;
|
||||
}
|
||||
|
||||
export interface BridgeServer {
|
||||
app: FastifyInstance;
|
||||
installation: InstallationStatus;
|
||||
abortActive: () => boolean;
|
||||
}
|
||||
|
||||
export async function createServer(config: BridgeConfig): Promise<BridgeServer> {
|
||||
const installation = await inspectInstallation(config.command_code_executable);
|
||||
const app = Fastify({
|
||||
logger: false,
|
||||
bodyLimit: config.max_request_bytes,
|
||||
requestTimeout: 0,
|
||||
});
|
||||
let active: ActiveRequest | undefined;
|
||||
|
||||
app.setErrorHandler((error, _request, reply) => {
|
||||
const fastifyError = error as Error & { code?: string; statusCode?: number };
|
||||
if (fastifyError.code === "FST_ERR_CTP_BODY_TOO_LARGE") {
|
||||
reply.status(413).send(openAIError(
|
||||
`Request body exceeds max_request_bytes (${config.max_request_bytes})`,
|
||||
"request_too_large",
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
const errorText = error instanceof Error ? (error.stack ?? error.message) : String(error);
|
||||
process.stderr.write(`${errorText}\n`);
|
||||
reply.status(fastifyError.statusCode ?? 500).send(openAIError(
|
||||
"Internal bridge error",
|
||||
"bridge_error",
|
||||
"server_error",
|
||||
));
|
||||
});
|
||||
|
||||
app.get("/health", async () => ({
|
||||
status: installation.installed && installation.authenticated ? "ok" : "degraded",
|
||||
command_code: installation,
|
||||
busy: active !== undefined,
|
||||
}));
|
||||
|
||||
app.get("/v1/models", async () => ({
|
||||
object: "list",
|
||||
data: Object.keys(config.models).map((id) => ({
|
||||
id,
|
||||
object: "model",
|
||||
created: 0,
|
||||
owned_by: "command-code-local",
|
||||
})),
|
||||
}));
|
||||
|
||||
app.post("/v1/chat/completions", async (request, reply) => {
|
||||
if (active) {
|
||||
return reply.status(429).send(openAIError(
|
||||
"Another Command Code request is already running",
|
||||
"busy",
|
||||
"server_error",
|
||||
));
|
||||
}
|
||||
|
||||
if (!installation.installed) {
|
||||
return reply.status(503).send(openAIError(
|
||||
"Command Code CLI is not installed",
|
||||
"command_code_not_installed",
|
||||
"server_error",
|
||||
));
|
||||
}
|
||||
|
||||
if (!installation.authenticated) {
|
||||
return reply.status(503).send(openAIError(
|
||||
"Command Code is not authenticated; run command-code login",
|
||||
"command_code_not_authenticated",
|
||||
"server_error",
|
||||
));
|
||||
}
|
||||
|
||||
let parsed;
|
||||
try {
|
||||
parsed = chatCompletionRequestSchema.parse(request.body);
|
||||
} catch (error) {
|
||||
const message = error instanceof ZodError
|
||||
? error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join("; ")
|
||||
: "Invalid request body";
|
||||
return reply.status(400).send(openAIError(message, "invalid_request"));
|
||||
}
|
||||
|
||||
const model = config.models[parsed.model];
|
||||
if (!model) {
|
||||
return reply.status(404).send(openAIError(
|
||||
`Model '${parsed.model}' is not configured`,
|
||||
"model_not_found",
|
||||
));
|
||||
}
|
||||
|
||||
const abortController = new AbortController();
|
||||
active = { abortController };
|
||||
let responseCompleted = false;
|
||||
|
||||
const cancelOnDisconnect = () => {
|
||||
if (!responseCompleted) abortController.abort("client disconnected");
|
||||
};
|
||||
request.raw.once("aborted", cancelOnDisconnect);
|
||||
reply.raw.once("close", cancelOnDisconnect);
|
||||
|
||||
try {
|
||||
const result = await runCommandCode(
|
||||
config,
|
||||
model.cli_model,
|
||||
model.effort,
|
||||
buildCommandPrompt(parsed),
|
||||
abortController.signal,
|
||||
);
|
||||
responseCompleted = true;
|
||||
if (parsed.stream) {
|
||||
return reply
|
||||
.type("text/event-stream; charset=utf-8")
|
||||
.header("Cache-Control", "no-cache")
|
||||
.header("Connection", "keep-alive")
|
||||
.send(completionStreamResponse(
|
||||
parsed.model,
|
||||
result.finalText,
|
||||
result.usage,
|
||||
parsed.stream_options?.include_usage,
|
||||
));
|
||||
}
|
||||
return reply.send(completionResponse(parsed.model, result.finalText, result.usage));
|
||||
} catch (error) {
|
||||
responseCompleted = true;
|
||||
if (abortController.signal.aborted) {
|
||||
if (!reply.raw.destroyed) {
|
||||
return reply.status(499).send(openAIError(
|
||||
"Command Code request cancelled",
|
||||
"cancelled",
|
||||
"server_error",
|
||||
));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const code = error instanceof CommandCodeError ? error.exitCode : undefined;
|
||||
const status = code === 5 ? 429 : code === 10 ? 402 : 502;
|
||||
const errorCode = code === 5
|
||||
? "rate_limit_exceeded"
|
||||
: code === 10
|
||||
? "insufficient_credits"
|
||||
: "command_code_error";
|
||||
return reply.status(status).send(openAIError(
|
||||
"Command Code request failed",
|
||||
errorCode,
|
||||
"server_error",
|
||||
));
|
||||
} finally {
|
||||
request.raw.off("aborted", cancelOnDisconnect);
|
||||
reply.raw.off("close", cancelOnDisconnect);
|
||||
active = undefined;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
app,
|
||||
installation,
|
||||
abortActive: () => {
|
||||
if (!active) return false;
|
||||
active.abortController.abort("bridge interrupted");
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user