Skip to content

Commit 738597d

Browse files
authored
Fix live stop errors and status display (#58)
1 parent 018007c commit 738597d

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

functions/api/[[path]].ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,14 +1765,19 @@ async function listLiveConversations(env: Env, status?: string | null) {
17651765
}
17661766

17671767
async function updateLiveConversation(request: Request, env: Env, sessionId: string) {
1768-
const db = requireDb(env);
1769-
if (!db.ok) return json({ error: "Live conversations require durable storage." }, 503);
17701768
const input = await body(request);
17711769
if (!["active", "waiting_on_peer", "settled_by_agent", "operator_stop_needed", "stopped"].includes(String(input.status))) {
17721770
return json({ error: "Invalid live conversation status." }, 400);
17731771
}
1772+
const db = requireDb(env);
1773+
if (!db.ok) return json({ error: "Live conversations require durable storage." }, 503);
17741774
await db.db
1775-
.prepare("UPDATE live_conversation_sessions SET status = ?, stopped_at = CASE WHEN ? = 'stopped' THEN ? ELSE NULL END WHERE id = ?")
1775+
.prepare(
1776+
`UPDATE live_conversation_sessions
1777+
SET status = ?,
1778+
stopped_at = CASE WHEN ? = 'stopped' THEN CAST(? AS timestamptz) ELSE NULL END
1779+
WHERE id = ?`,
1780+
)
17761781
.bind(input.status, input.status, now(), sessionId)
17771782
.run();
17781783
const row = await db.db.prepare("SELECT * FROM live_conversation_sessions WHERE id = ?").bind(sessionId).first<Row>();

src/App.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ export AGENT_COMMS_TOKEN="${token}"
212212
`;
213213
}
214214

215+
function readableRequestError(value: unknown) {
216+
const message = String(value ?? "").trim();
217+
if (!message) return "Operator request failed.";
218+
if (message.includes("<!DOCTYPE html") || message.includes("<html")) {
219+
if (message.includes("Worker threw exception")) return "Cloudflare Worker threw an exception. Check the API logs.";
220+
return "Server returned an HTML error page.";
221+
}
222+
return message.length > 240 ? `${message.slice(0, 237)}...` : message;
223+
}
224+
215225
function agentTokenFileCommand(agent: AgentIdentity, token: string) {
216226
return `umask 077; cat > agent-comms-token.env <<'EOF'\n${agentTokenEnvFile(agent, token)}EOF\n`;
217227
}
@@ -1345,7 +1355,7 @@ export function App() {
13451355
const payload = contentType.includes("application/json")
13461356
? await response.json()
13471357
: { error: await response.text() };
1348-
if (!response.ok) throw new Error(payload.error ?? "Operator request failed.");
1358+
if (!response.ok) throw new Error(readableRequestError(payload.error ?? "Operator request failed."));
13491359
return payload;
13501360
},
13511361
[operatorToken],

tests/api-auth.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,26 @@ describe("API auth", () => {
185185
expect(response.status).toBe(200);
186186
expect(payload.schemas?.agent?.createDirectConversation).toEqual({ agentId: "string", peerAgentId: "string" });
187187
});
188+
189+
it("rejects invalid live conversation status before storage access", async () => {
190+
const request = new Request("https://example.test/api/operator/live-conversations/live_123/status", {
191+
method: "POST",
192+
headers: {
193+
authorization: "Bearer operator-token",
194+
"content-type": "application/json",
195+
},
196+
body: JSON.stringify({ status: "paused" }),
197+
});
198+
199+
const response = await onRequest({
200+
request,
201+
env: { OPERATOR_API_TOKEN: "operator-token" } as never,
202+
});
203+
expect(response).toBeDefined();
204+
if (!response) throw new Error("Expected response");
205+
const payload = await response.json() as { error?: string };
206+
207+
expect(response.status).toBe(400);
208+
expect(payload.error).toBe("Invalid live conversation status.");
209+
});
188210
});

0 commit comments

Comments
 (0)