fix: session handling and SDK async iteration

- Add sdk_conversation_id column to track SDK conversation separately from user session_id
- Queue processor looks up SDK conversation ID for resume instead of passing user session_id
- session_id "new" stores NULL initially, gets set from SDK on completion
- Custom session_ids are preserved (not overwritten by SDK)
- Worker catches SDK process exit error after messages are collected

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adriano Dal Pastro
2026-05-25 20:59:55 +00:00
parent 98ad65d248
commit 9b68f772f6
3 changed files with 45 additions and 21 deletions
+18 -7
View File
@@ -33,19 +33,26 @@ app.post("/process", async (req, res) => {
const conv = query({ prompt, options });
const messages = [];
for await (const msg of conv) {
messages.push(msg);
try {
for await (const msg of conv) {
messages.push(msg);
}
} catch (_) {
// SDK throws after process exits; messages already collected
}
const resultMsg = messages.find((m) => m.type === "result");
if (resultMsg && resultMsg.is_error) {
if (!resultMsg) {
return res.json({ success: false, error: "No result received from Claude" });
}
if (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,
result: resultMsg.result || "",
session_id: resultMsg.session_id || session_id,
});
} catch (error) {
console.error("Process error:", error.message);
@@ -85,8 +92,12 @@ app.post("/summarize", async (req, res) => {
},
});
const msgs = [];
for await (const msg of conv) {
msgs.push(msg);
try {
for await (const msg of conv) {
msgs.push(msg);
}
} catch (_) {
// SDK throws after process exits; messages already collected
}
const resultMsg = msgs.find((m) => m.type === "result");