Keep backend runs alive on client disconnect
This commit is contained in:
@@ -110,10 +110,6 @@ export async function POST(req: Request) {
|
|||||||
writer.write({ type: "start-step" });
|
writer.write({ type: "start-step" });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cancelOnAbort = () => {
|
|
||||||
void cancelClawRun(account.id, sessionId, streamState.runId);
|
|
||||||
};
|
|
||||||
req.signal.addEventListener("abort", cancelOnAbort, { once: true });
|
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
writer.write({ type: "reasoning-start", id: "reasoning-1" });
|
writer.write({ type: "reasoning-start", id: "reasoning-1" });
|
||||||
writer.write({
|
writer.write({
|
||||||
@@ -129,9 +125,7 @@ export async function POST(req: Request) {
|
|||||||
resumeId,
|
resumeId,
|
||||||
writer,
|
writer,
|
||||||
streamState,
|
streamState,
|
||||||
req.signal,
|
|
||||||
);
|
);
|
||||||
req.signal.removeEventListener("abort", cancelOnAbort);
|
|
||||||
const elapsedMs = payload.elapsed_ms ?? Date.now() - startedAt;
|
const elapsedMs = payload.elapsed_ms ?? Date.now() - startedAt;
|
||||||
const text = formatClawResponse(payload);
|
const text = formatClawResponse(payload);
|
||||||
|
|
||||||
@@ -351,13 +345,11 @@ async function callClawBackendStream(
|
|||||||
resumeSessionId?: string,
|
resumeSessionId?: string,
|
||||||
writer?: UIMessageStreamWriter<UIMessage>,
|
writer?: UIMessageStreamWriter<UIMessage>,
|
||||||
streamState?: StreamState,
|
streamState?: StreamState,
|
||||||
signal?: AbortSignal,
|
|
||||||
): Promise<ClawChatResponse> {
|
): Promise<ClawChatResponse> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${CLAW_API_URL}/api/chat/stream`, {
|
const response = await fetch(`${CLAW_API_URL}/api/chat/stream`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "content-type": "application/json" },
|
headers: { "content-type": "application/json" },
|
||||||
signal,
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
prompt,
|
prompt,
|
||||||
runtime_context: runtimeContext,
|
runtime_context: runtimeContext,
|
||||||
@@ -390,26 +382,6 @@ async function callClawBackendStream(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cancelClawRun(
|
|
||||||
accountId: string,
|
|
||||||
sessionId: string,
|
|
||||||
runId?: string,
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
await fetch(`${CLAW_API_URL}/api/runs/cancel`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "content-type": "application/json" },
|
|
||||||
body: JSON.stringify({
|
|
||||||
account_id: accountId,
|
|
||||||
session_id: sessionId,
|
|
||||||
...(runId ? { run_id: runId } : {}),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// 浏览器关闭或网络中断时,取消请求本身也可能失败;后端流断开仍会结束本次响应。
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function consumeClawStream(
|
async function consumeClawStream(
|
||||||
body: ReadableStream<Uint8Array>,
|
body: ReadableStream<Uint8Array>,
|
||||||
writer?: UIMessageStreamWriter<UIMessage>,
|
writer?: UIMessageStreamWriter<UIMessage>,
|
||||||
|
|||||||
Reference in New Issue
Block a user