aebedfc52e
Adds an API server with proxy routes for OpenAI and Anthropic clients, along with a frontend portal to interact with the API. Replit-Commit-Author: Agent Replit-Commit-Session-Id: dcd090ca-935a-42ae-8db4-503e1bbe165a Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 61b72c06-3c4d-47af-a97e-9d527892d9c5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/40608c50-a053-47e8-aa03-1bdcc1a2698b/dcd090ca-935a-42ae-8db4-503e1bbe165a/ZBSB1hW Replit-Helium-Checkpoint-Created: true
444 lines
12 KiB
TypeScript
444 lines
12 KiB
TypeScript
import { Router, type IRouter, type Request, type Response } from "express";
|
|
import OpenAI from "openai";
|
|
import Anthropic from "@anthropic-ai/sdk";
|
|
|
|
const router: IRouter = Router();
|
|
|
|
const openai = new OpenAI({
|
|
baseURL: process.env["AI_INTEGRATIONS_OPENAI_BASE_URL"],
|
|
apiKey: process.env["AI_INTEGRATIONS_OPENAI_API_KEY"],
|
|
});
|
|
|
|
const anthropic = new Anthropic({
|
|
baseURL: process.env["AI_INTEGRATIONS_ANTHROPIC_BASE_URL"],
|
|
apiKey: process.env["AI_INTEGRATIONS_ANTHROPIC_API_KEY"],
|
|
});
|
|
|
|
const MODELS = [
|
|
{ id: "gpt-5.2", provider: "openai" },
|
|
{ id: "gpt-5-mini", provider: "openai" },
|
|
{ id: "gpt-5-nano", provider: "openai" },
|
|
{ id: "o4-mini", provider: "openai" },
|
|
{ id: "o3", provider: "openai" },
|
|
{ id: "claude-opus-4-6", provider: "anthropic" },
|
|
{ id: "claude-sonnet-4-6", provider: "anthropic" },
|
|
{ id: "claude-haiku-4-5", provider: "anthropic" },
|
|
] as const;
|
|
|
|
function authorize(req: Request, res: Response): boolean {
|
|
const expected = process.env["PROXY_API_KEY"];
|
|
if (!expected) {
|
|
res.status(500).json({ error: { message: "PROXY_API_KEY not configured", type: "server_error" } });
|
|
return false;
|
|
}
|
|
const header = req.header("authorization") ?? "";
|
|
const match = header.match(/^Bearer\s+(.+)$/i);
|
|
if (!match || match[1] !== expected) {
|
|
res.status(401).json({ error: { message: "Invalid API key", type: "invalid_request_error" } });
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
router.get("/models", (req, res) => {
|
|
if (!authorize(req, res)) return;
|
|
const created = Math.floor(Date.now() / 1000);
|
|
res.json({
|
|
object: "list",
|
|
data: MODELS.map((m) => ({
|
|
id: m.id,
|
|
object: "model",
|
|
created,
|
|
owned_by: m.provider,
|
|
})),
|
|
});
|
|
});
|
|
|
|
interface ChatMessage {
|
|
role: "system" | "user" | "assistant" | "developer" | "tool";
|
|
content: unknown;
|
|
name?: string;
|
|
}
|
|
|
|
interface ChatCompletionRequest {
|
|
model: string;
|
|
messages: ChatMessage[];
|
|
stream?: boolean;
|
|
temperature?: number;
|
|
top_p?: number;
|
|
max_tokens?: number;
|
|
max_completion_tokens?: number;
|
|
stop?: string | string[];
|
|
frequency_penalty?: number;
|
|
presence_penalty?: number;
|
|
user?: string;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
function isOpenAIModel(model: string): boolean {
|
|
return /^(gpt|o\d|chatgpt)/i.test(model);
|
|
}
|
|
|
|
function isAnthropicModel(model: string): boolean {
|
|
return /^claude/i.test(model);
|
|
}
|
|
|
|
function newId(): string {
|
|
return "chatcmpl-" + Math.random().toString(36).slice(2, 14);
|
|
}
|
|
|
|
router.post("/chat/completions", async (req, res) => {
|
|
if (!authorize(req, res)) return;
|
|
|
|
const body = req.body as ChatCompletionRequest;
|
|
if (!body || typeof body !== "object" || !body.model || !Array.isArray(body.messages)) {
|
|
res.status(400).json({
|
|
error: { message: "model and messages are required", type: "invalid_request_error" },
|
|
});
|
|
return;
|
|
}
|
|
|
|
const stream = body.stream === true;
|
|
|
|
if (isOpenAIModel(body.model)) {
|
|
await handleOpenAI(req, res, body, stream);
|
|
} else if (isAnthropicModel(body.model)) {
|
|
await handleAnthropic(req, res, body, stream);
|
|
} else {
|
|
res.status(400).json({
|
|
error: {
|
|
message: `Unsupported model: ${body.model}`,
|
|
type: "invalid_request_error",
|
|
},
|
|
});
|
|
}
|
|
});
|
|
|
|
function setupSse(res: Response): NodeJS.Timeout {
|
|
res.setHeader("Content-Type", "text/event-stream");
|
|
res.setHeader("Cache-Control", "no-cache, no-transform");
|
|
res.setHeader("Connection", "keep-alive");
|
|
res.setHeader("X-Accel-Buffering", "no");
|
|
res.flushHeaders();
|
|
const keepalive = setInterval(() => {
|
|
try {
|
|
res.write(": keepalive\n\n");
|
|
res.flush?.();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}, 5000);
|
|
return keepalive;
|
|
}
|
|
|
|
async function handleOpenAI(
|
|
req: Request,
|
|
res: Response,
|
|
body: ChatCompletionRequest,
|
|
stream: boolean,
|
|
): Promise<void> {
|
|
const { stream: _ignored, ...rest } = body;
|
|
|
|
if (!stream) {
|
|
try {
|
|
const completion = await openai.chat.completions.create({
|
|
...rest,
|
|
stream: false,
|
|
} as Parameters<typeof openai.chat.completions.create>[0]);
|
|
res.json(completion);
|
|
} catch (err) {
|
|
handleProviderError(req, res, err, "openai");
|
|
}
|
|
return;
|
|
}
|
|
|
|
let keepalive: NodeJS.Timeout | undefined;
|
|
let closed = false;
|
|
req.on("close", () => {
|
|
closed = true;
|
|
if (keepalive) clearInterval(keepalive);
|
|
});
|
|
|
|
try {
|
|
const upstream = await openai.chat.completions.create({
|
|
...rest,
|
|
stream: true,
|
|
} as Parameters<typeof openai.chat.completions.create>[0]);
|
|
|
|
keepalive = setupSse(res);
|
|
|
|
for await (const chunk of upstream as AsyncIterable<unknown>) {
|
|
if (closed) break;
|
|
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
|
res.flush?.();
|
|
}
|
|
|
|
if (!closed) {
|
|
res.write("data: [DONE]\n\n");
|
|
res.flush?.();
|
|
}
|
|
} catch (err) {
|
|
if (!res.headersSent) {
|
|
handleProviderError(req, res, err, "openai");
|
|
return;
|
|
}
|
|
req.log?.error({ err }, "openai stream error");
|
|
try {
|
|
res.write(
|
|
`data: ${JSON.stringify({ error: { message: errorMessage(err), type: "upstream_error" } })}\n\n`,
|
|
);
|
|
res.flush?.();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
} finally {
|
|
if (keepalive) clearInterval(keepalive);
|
|
if (!closed) {
|
|
try {
|
|
res.end();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
interface AnthropicConvertedMessages {
|
|
system: string | undefined;
|
|
messages: Array<{ role: "user" | "assistant"; content: unknown }>;
|
|
}
|
|
|
|
function convertMessagesForAnthropic(messages: ChatMessage[]): AnthropicConvertedMessages {
|
|
const systemParts: string[] = [];
|
|
const out: Array<{ role: "user" | "assistant"; content: unknown }> = [];
|
|
for (const msg of messages) {
|
|
if (msg.role === "system" || msg.role === "developer") {
|
|
if (typeof msg.content === "string") {
|
|
systemParts.push(msg.content);
|
|
} else if (Array.isArray(msg.content)) {
|
|
for (const part of msg.content as Array<{ type?: string; text?: string }>) {
|
|
if (part?.type === "text" && typeof part.text === "string") {
|
|
systemParts.push(part.text);
|
|
}
|
|
}
|
|
}
|
|
} else if (msg.role === "user" || msg.role === "assistant") {
|
|
out.push({ role: msg.role, content: msg.content });
|
|
}
|
|
// ignore tool messages for this passthrough
|
|
}
|
|
return {
|
|
system: systemParts.length > 0 ? systemParts.join("\n\n") : undefined,
|
|
messages: out,
|
|
};
|
|
}
|
|
|
|
async function handleAnthropic(
|
|
req: Request,
|
|
res: Response,
|
|
body: ChatCompletionRequest,
|
|
stream: boolean,
|
|
): Promise<void> {
|
|
const { system, messages } = convertMessagesForAnthropic(body.messages);
|
|
const maxTokens =
|
|
body.max_completion_tokens ??
|
|
body.max_tokens ??
|
|
8192;
|
|
|
|
const baseParams: Record<string, unknown> = {
|
|
model: body.model,
|
|
max_tokens: maxTokens,
|
|
messages,
|
|
};
|
|
if (system) baseParams["system"] = system;
|
|
if (typeof body.temperature === "number") baseParams["temperature"] = body.temperature;
|
|
if (typeof body.top_p === "number") baseParams["top_p"] = body.top_p;
|
|
if (body.stop !== undefined) {
|
|
baseParams["stop_sequences"] = Array.isArray(body.stop) ? body.stop : [body.stop];
|
|
}
|
|
|
|
if (!stream) {
|
|
try {
|
|
const message = await anthropic.messages.create(
|
|
baseParams as Parameters<typeof anthropic.messages.create>[0],
|
|
);
|
|
const text = (message.content ?? [])
|
|
.map((b) => (b.type === "text" ? b.text : ""))
|
|
.join("");
|
|
const finishReason = mapAnthropicStopReason(message.stop_reason ?? null);
|
|
res.json({
|
|
id: message.id,
|
|
object: "chat.completion",
|
|
created: Math.floor(Date.now() / 1000),
|
|
model: message.model,
|
|
choices: [
|
|
{
|
|
index: 0,
|
|
message: { role: "assistant", content: text },
|
|
finish_reason: finishReason,
|
|
},
|
|
],
|
|
usage: {
|
|
prompt_tokens: message.usage?.input_tokens ?? 0,
|
|
completion_tokens: message.usage?.output_tokens ?? 0,
|
|
total_tokens:
|
|
(message.usage?.input_tokens ?? 0) + (message.usage?.output_tokens ?? 0),
|
|
},
|
|
});
|
|
} catch (err) {
|
|
handleProviderError(req, res, err, "anthropic");
|
|
}
|
|
return;
|
|
}
|
|
|
|
let keepalive: NodeJS.Timeout | undefined;
|
|
let closed = false;
|
|
req.on("close", () => {
|
|
closed = true;
|
|
if (keepalive) clearInterval(keepalive);
|
|
});
|
|
|
|
const id = newId();
|
|
const created = Math.floor(Date.now() / 1000);
|
|
|
|
try {
|
|
const upstream = anthropic.messages.stream(
|
|
baseParams as Parameters<typeof anthropic.messages.stream>[0],
|
|
);
|
|
|
|
keepalive = setupSse(res);
|
|
|
|
res.write(
|
|
`data: ${JSON.stringify({
|
|
id,
|
|
object: "chat.completion.chunk",
|
|
created,
|
|
model: body.model,
|
|
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }],
|
|
})}\n\n`,
|
|
);
|
|
res.flush?.();
|
|
|
|
let stopReason: string | null = null;
|
|
|
|
for await (const event of upstream) {
|
|
if (closed) break;
|
|
if (
|
|
event.type === "content_block_delta" &&
|
|
event.delta.type === "text_delta"
|
|
) {
|
|
const chunk = {
|
|
id,
|
|
object: "chat.completion.chunk",
|
|
created,
|
|
model: body.model,
|
|
choices: [
|
|
{
|
|
index: 0,
|
|
delta: { content: event.delta.text },
|
|
finish_reason: null,
|
|
},
|
|
],
|
|
};
|
|
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
|
res.flush?.();
|
|
} else if (event.type === "message_delta") {
|
|
const delta = event as unknown as { delta?: { stop_reason?: string | null } };
|
|
if (delta.delta?.stop_reason) {
|
|
stopReason = delta.delta.stop_reason;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!closed) {
|
|
const finishChunk = {
|
|
id,
|
|
object: "chat.completion.chunk",
|
|
created,
|
|
model: body.model,
|
|
choices: [
|
|
{
|
|
index: 0,
|
|
delta: {},
|
|
finish_reason: mapAnthropicStopReason(stopReason),
|
|
},
|
|
],
|
|
};
|
|
res.write(`data: ${JSON.stringify(finishChunk)}\n\n`);
|
|
res.write("data: [DONE]\n\n");
|
|
res.flush?.();
|
|
}
|
|
} catch (err) {
|
|
if (!res.headersSent) {
|
|
handleProviderError(req, res, err, "anthropic");
|
|
return;
|
|
}
|
|
req.log?.error({ err }, "anthropic stream error");
|
|
try {
|
|
res.write(
|
|
`data: ${JSON.stringify({ error: { message: errorMessage(err), type: "upstream_error" } })}\n\n`,
|
|
);
|
|
res.flush?.();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
} finally {
|
|
if (keepalive) clearInterval(keepalive);
|
|
if (!closed) {
|
|
try {
|
|
res.end();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function mapAnthropicStopReason(reason: string | null): string {
|
|
switch (reason) {
|
|
case "end_turn":
|
|
case "stop_sequence":
|
|
return "stop";
|
|
case "max_tokens":
|
|
return "length";
|
|
case "tool_use":
|
|
return "tool_calls";
|
|
default:
|
|
return "stop";
|
|
}
|
|
}
|
|
|
|
function errorMessage(err: unknown): string {
|
|
if (err instanceof Error) return err.message;
|
|
if (typeof err === "string") return err;
|
|
try {
|
|
return JSON.stringify(err);
|
|
} catch {
|
|
return "unknown error";
|
|
}
|
|
}
|
|
|
|
function handleProviderError(
|
|
req: Request,
|
|
res: Response,
|
|
err: unknown,
|
|
provider: string,
|
|
): void {
|
|
req.log?.error({ err, provider }, "provider error");
|
|
const status =
|
|
typeof (err as { status?: number })?.status === "number"
|
|
? (err as { status: number }).status
|
|
: 500;
|
|
if (!res.headersSent) {
|
|
res.status(status).json({
|
|
error: {
|
|
message: errorMessage(err),
|
|
type: "upstream_error",
|
|
provider,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
export default router;
|