85 lines
3.1 KiB
JavaScript
85 lines
3.1 KiB
JavaScript
import { randomUUID } from "node:crypto";
|
|
import cors from "cors";
|
|
import express from "express";
|
|
import { ChatSessionBridge } from "./chat/sessionBridge.js";
|
|
import { config } from "./config.js";
|
|
import { logger } from "./logger.js";
|
|
import { buildChatRouter } from "./routes/chat.js";
|
|
import { opencodeRuntime } from "./runtime/opencode.js";
|
|
import { SessionRegistry } from "./session/registry.js";
|
|
import { dynamicHttpExecutor } from "./tools/dynamicHttpExecutor.js";
|
|
const app = express();
|
|
const registry = new SessionRegistry(config.SESSION_TTL_SECONDS);
|
|
const sessionBridge = new ChatSessionBridge(registry, opencodeRuntime);
|
|
const internalToken = config.AGENT_INTERNAL_TOKEN ?? randomUUID();
|
|
// 这个 token 只用于 .opencode/tools 回调本服务,避免把 internal endpoint 暴露成无鉴权入口。
|
|
process.env.TJWATER_AGENT_INTERNAL_TOKEN = internalToken;
|
|
app.use(cors());
|
|
app.use(express.json({ limit: "1mb" }));
|
|
app.get("/health", async (_req, res) => {
|
|
try {
|
|
const runtime = await opencodeRuntime.health();
|
|
res.json({
|
|
ok: true,
|
|
runtime,
|
|
sessions: sessionBridge.count(),
|
|
});
|
|
}
|
|
catch (error) {
|
|
const detail = error instanceof Error ? error.message : String(error);
|
|
res.status(503).json({
|
|
ok: false,
|
|
message: "opencode runtime unavailable",
|
|
detail,
|
|
sessions: sessionBridge.count(),
|
|
});
|
|
}
|
|
});
|
|
app.post("/internal/tools/dynamic-http-call", async (req, res) => {
|
|
if (req.header("x-agent-internal-token") !== internalToken) {
|
|
res.status(403).json({ message: "forbidden" });
|
|
return;
|
|
}
|
|
const sessionId = typeof req.body?.sessionId === "string" ? req.body.sessionId : "";
|
|
const context = sessionBridge.getSessionContext(sessionId);
|
|
if (!context) {
|
|
res.status(404).json({
|
|
message: "session context not found",
|
|
detail: sessionId,
|
|
});
|
|
return;
|
|
}
|
|
try {
|
|
// opencode 工具运行在 .opencode 侧,这里负责把工具调用重新绑定到当前用户/项目上下文。
|
|
const result = await dynamicHttpExecutor.execute({
|
|
path: req.body?.path,
|
|
method: req.body?.method,
|
|
arguments: req.body?.arguments,
|
|
}, context);
|
|
res.json(result);
|
|
}
|
|
catch (error) {
|
|
const detail = error instanceof Error ? error.message : String(error);
|
|
res.status(400).json({
|
|
message: "dynamic http execution failed",
|
|
detail,
|
|
});
|
|
}
|
|
});
|
|
app.use("/api/v1/copilot/chat", buildChatRouter(sessionBridge, opencodeRuntime));
|
|
const server = app.listen(config.PORT, config.HOST, () => {
|
|
logger.info({ host: config.HOST, port: config.PORT }, "TJWaterAgent listening");
|
|
});
|
|
const shutdown = async () => {
|
|
logger.info("shutting down TJWaterAgent");
|
|
server.close();
|
|
// 同步关闭 embedded opencode server,避免本服务退出后留下孤儿进程。
|
|
await opencodeRuntime.dispose();
|
|
};
|
|
process.on("SIGINT", () => {
|
|
void shutdown();
|
|
});
|
|
process.on("SIGTERM", () => {
|
|
void shutdown();
|
|
});
|