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}`); });