5d78d865a4
- Dockerfile: use explicit uvicorn command (uv run start fails without package mode) - docker-compose: mount claude-auth volume to /root/.claude (worker runs as root) - worker: update to Claude Code SDK async iterable API (query() returns iterator, not array) - main.py: fix docs_url from /doc to /docs - README: correct login instructions (exec not run --rm), add production URLs - Add CLAUDE.md with full project documentation Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
109 lines
2.6 KiB
JavaScript
109 lines
2.6 KiB
JavaScript
import express from "express";
|
|
import { query } from "@anthropic-ai/claude-code";
|
|
|
|
const app = express();
|
|
app.use(express.json({ limit: "10mb" }));
|
|
|
|
const PORT = process.env.WORKER_PORT || 3001;
|
|
const DEFAULT_MODEL = process.env.CLAUDE_MODEL || "claude-sonnet-4-6";
|
|
|
|
app.get("/health", (_req, res) => {
|
|
res.json({ status: "ok" });
|
|
});
|
|
|
|
app.post("/process", async (req, res) => {
|
|
const { prompt, system_prompt, model, session_id } = req.body;
|
|
|
|
if (!prompt) {
|
|
return res
|
|
.status(400)
|
|
.json({ success: false, error: "prompt is required" });
|
|
}
|
|
|
|
try {
|
|
const options = {
|
|
model: model || DEFAULT_MODEL,
|
|
systemPrompt: system_prompt || undefined,
|
|
allowedTools: [],
|
|
};
|
|
|
|
if (session_id) {
|
|
options.resume = session_id;
|
|
}
|
|
|
|
const conv = query({ prompt, options });
|
|
const messages = [];
|
|
for await (const msg of conv) {
|
|
messages.push(msg);
|
|
}
|
|
|
|
const resultMsg = messages.find((m) => m.type === "result");
|
|
if (resultMsg && resultMsg.is_error) {
|
|
return res.json({ success: false, error: resultMsg.result });
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
result: resultMsg?.result || "",
|
|
session_id: resultMsg?.session_id || session_id,
|
|
});
|
|
} catch (error) {
|
|
console.error("Process error:", error.message);
|
|
res.json({
|
|
success: false,
|
|
error: error.message,
|
|
});
|
|
}
|
|
});
|
|
|
|
app.post("/summarize", async (req, res) => {
|
|
const { exchanges, mode } = req.body;
|
|
|
|
if (!exchanges || exchanges.length === 0) {
|
|
return res
|
|
.status(400)
|
|
.json({ success: false, error: "exchanges is required" });
|
|
}
|
|
|
|
const exchangeText = exchanges
|
|
.map((e, i) => `Exchange ${i + 1}:\nQ: ${e.prompt}\nA: ${e.result}`)
|
|
.join("\n\n");
|
|
|
|
const summaryPrompt =
|
|
mode === "complete"
|
|
? `Summarize these exchanges into a comprehensive overview. Be concise but thorough:\n\n${exchangeText}`
|
|
: `Summarize this exchange in 1-2 sentences:\n\n${exchangeText}`;
|
|
|
|
try {
|
|
const conv = query({
|
|
prompt: summaryPrompt,
|
|
options: {
|
|
model: DEFAULT_MODEL,
|
|
systemPrompt:
|
|
"You are a summarizer. Respond with only the summary, no preamble.",
|
|
allowedTools: [],
|
|
},
|
|
});
|
|
const msgs = [];
|
|
for await (const msg of conv) {
|
|
msgs.push(msg);
|
|
}
|
|
|
|
const resultMsg = msgs.find((m) => m.type === "result");
|
|
res.json({
|
|
success: true,
|
|
summary: (resultMsg?.result || "").trim(),
|
|
});
|
|
} catch (error) {
|
|
console.error("Summarize error:", error.message);
|
|
res.json({
|
|
success: false,
|
|
error: error.message,
|
|
});
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Worker listening on port ${PORT}`);
|
|
});
|