mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-24 11:14:23 +01:00
abort
This commit is contained in:
@@ -158,8 +158,47 @@ export namespace Server {
|
||||
return c.json(sessions);
|
||||
},
|
||||
)
|
||||
.post(
|
||||
"/session_abort",
|
||||
describeRoute({
|
||||
description: "Abort a session",
|
||||
responses: {
|
||||
200: {
|
||||
description: "Aborted session",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(z.boolean()),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
zValidator(
|
||||
"json",
|
||||
z.object({
|
||||
sessionID: z.string(),
|
||||
}),
|
||||
),
|
||||
async (c) => {
|
||||
const body = c.req.valid("json");
|
||||
return c.json(Session.abort(body.sessionID));
|
||||
},
|
||||
)
|
||||
.post(
|
||||
"/session_chat",
|
||||
describeRoute({
|
||||
description: "Chat with a model",
|
||||
responses: {
|
||||
200: {
|
||||
description: "Chat with a model",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(SessionMessage),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
zValidator(
|
||||
"json",
|
||||
z.object({
|
||||
|
||||
@@ -129,6 +129,16 @@ export namespace Session {
|
||||
}
|
||||
}
|
||||
|
||||
const pending = new Map<string, AbortController>();
|
||||
|
||||
export function abort(sessionID: string) {
|
||||
const controller = pending.get(sessionID);
|
||||
if (!controller) return false;
|
||||
controller.abort();
|
||||
pending.delete(sessionID);
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function chat(input: {
|
||||
sessionID: string;
|
||||
providerID: string;
|
||||
@@ -225,6 +235,8 @@ export namespace Session {
|
||||
tool: {},
|
||||
},
|
||||
};
|
||||
const controller = new AbortController();
|
||||
pending.set(input.sessionID, controller);
|
||||
const result = streamText({
|
||||
onStepFinish: (step) => {
|
||||
update(input.sessionID, (draft) => {
|
||||
@@ -240,6 +252,8 @@ export namespace Session {
|
||||
.toNumber();
|
||||
});
|
||||
},
|
||||
abortSignal: controller.signal,
|
||||
maxRetries: 6,
|
||||
stopWhen: stepCountIs(1000),
|
||||
messages: convertToModelMessages(msgs),
|
||||
temperature: 0,
|
||||
@@ -251,7 +265,14 @@ export namespace Session {
|
||||
let text: TextUIPart | undefined;
|
||||
const reader = result.toUIMessageStream().getReader();
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
const result = await reader.read().catch((e) => {
|
||||
if (e instanceof DOMException && e.name === "AbortError") {
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
});
|
||||
if (!result) break;
|
||||
const { done, value } = result;
|
||||
if (done) break;
|
||||
l.info("part", {
|
||||
type: value.type,
|
||||
@@ -316,6 +337,7 @@ export namespace Session {
|
||||
}
|
||||
await write(next);
|
||||
}
|
||||
pending.delete(input.sessionID);
|
||||
next.metadata!.time.completed = Date.now();
|
||||
await write(next);
|
||||
return next;
|
||||
|
||||
Reference in New Issue
Block a user