Some checks failed
CI / build-check-test (push) Has been cancelled
Restructure local extensions into per-feature directories, split WebUI into backend modules with slash commands and systemd support, and track prompts/skills under .pi/agent for portable Gitea deployment. Co-authored-by: Cursor <cursoragent@cursor.com>
77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
import { dispatchSlashCommand } from "../slash/dispatch.ts";
|
|
import { normalizeChatImages } from "../services/chat-images.ts";
|
|
import type { WebUiContext } from "../types/context.ts";
|
|
import { json, readBody } from "../http/request.ts";
|
|
|
|
export function handleChatRoute(
|
|
req: IncomingMessage,
|
|
res: ServerResponse,
|
|
ctx: WebUiContext,
|
|
pathname: string,
|
|
): boolean {
|
|
const { sendCmd, submitPrompt } = ctx.rpc;
|
|
|
|
if (req.method === "POST" && pathname === "/api/chat") {
|
|
void readBody(req)
|
|
.then(async ({ message, images }) => {
|
|
const msg = typeof message === "string" ? message : "";
|
|
const imgs = normalizeChatImages(images);
|
|
if (!msg.trim() && !imgs?.length) throw new Error("消息不能为空");
|
|
if (imgs?.length) console.log(`[webui] chat: ${imgs.length} image(s), message=${msg.length} chars`);
|
|
|
|
if (!imgs?.length && msg.trim().startsWith("/")) {
|
|
const slash = await dispatchSlashCommand(msg, sendCmd);
|
|
if (slash?.handled) {
|
|
return json(res, { ok: true, slash: true, ...slash });
|
|
}
|
|
}
|
|
|
|
submitPrompt({ message: msg, images: imgs });
|
|
return json(res, { ok: true, accepted: true }, 202);
|
|
})
|
|
.catch((err) => json(res, { error: err.message }, 500));
|
|
return true;
|
|
}
|
|
|
|
if (req.method === "POST" && pathname === "/api/bash") {
|
|
void readBody(req)
|
|
.then(async ({ command }) => {
|
|
const cmd = typeof command === "string" ? command.trim() : "";
|
|
if (!cmd) throw new Error("命令不能为空");
|
|
const result = await sendCmd({ type: "bash", command: cmd });
|
|
if (!result.success) throw new Error(result.error || "命令执行失败");
|
|
json(res, result.data);
|
|
})
|
|
.catch((err) => json(res, { error: err.message }, 500));
|
|
return true;
|
|
}
|
|
|
|
if (req.method === "GET" && pathname === "/api/events") {
|
|
res.writeHead(200, {
|
|
"Content-Type": "text/event-stream",
|
|
"Cache-Control": "no-cache",
|
|
Connection: "keep-alive",
|
|
});
|
|
ctx.rpc.connectSseClient(res);
|
|
req.on("close", () => ctx.rpc.removeSseClient(res));
|
|
return true;
|
|
}
|
|
|
|
if (req.method === "POST" && pathname === "/api/messages") {
|
|
void sendCmd({ type: "get_messages" })
|
|
.then((r: any) => (r.success ? json(res, r.data) : json(res, { error: r.error }, 500)))
|
|
.catch((err) => json(res, { error: err.message }, 500));
|
|
return true;
|
|
}
|
|
|
|
if (req.method === "POST" && pathname === "/api/abort") {
|
|
void sendCmd({ type: "abort" })
|
|
.then(() => json(res, { ok: true }))
|
|
.catch((err) => json(res, { error: err.message }, 500));
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|