
























1+import type { AcpRuntime, AcpRuntimeHandle } from "@openclaw/acp-core/runtime/types";
2+import type { OpenClawConfig } from "../../config/types.openclaw.js";
3+import {
4+type AcpRuntimeError,
5+toAcpRuntimeError,
6+withAcpRuntimeErrorBoundary,
7+} from "../runtime/errors.js";
8+import type {
9+ActiveTurnState,
10+EnsureManagerRuntimeHandle,
11+ResolveManagerSession,
12+SetManagerSessionState,
13+WithManagerSessionActor,
14+} from "./manager.types.js";
15+import { normalizeActorKey, requireReadySessionMeta } from "./manager.utils.js";
16+17+export async function runManagerCancelSession(params: {
18+cfg: OpenClawConfig;
19+sessionKey: string;
20+reason?: string;
21+activeTurnBySession: Map<string, ActiveTurnState>;
22+withSessionActor: WithManagerSessionActor;
23+resolveSession: ResolveManagerSession;
24+ensureRuntimeHandle: EnsureManagerRuntimeHandle;
25+setSessionState: SetManagerSessionState;
26+}): Promise<void> {
27+const actorKey = normalizeActorKey(params.sessionKey);
28+const activeTurn = params.activeTurnBySession.get(actorKey);
29+if (activeTurn) {
30+await cancelActiveTurn({
31+ activeTurn,
32+reason: params.reason,
33+});
34+return;
35+}
36+37+await params.withSessionActor(params.sessionKey, async () => {
38+const resolution = params.resolveSession({
39+cfg: params.cfg,
40+sessionKey: params.sessionKey,
41+});
42+const resolvedMeta = requireReadySessionMeta(resolution);
43+const { runtime, handle } = await params.ensureRuntimeHandle({
44+cfg: params.cfg,
45+sessionKey: params.sessionKey,
46+meta: resolvedMeta,
47+});
48+try {
49+await cancelRuntimeHandle({
50+ runtime,
51+ handle,
52+reason: params.reason,
53+});
54+await params.setSessionState({
55+cfg: params.cfg,
56+sessionKey: params.sessionKey,
57+state: "idle",
58+clearLastError: true,
59+});
60+} catch (error) {
61+const acpError = normalizeCancelError(error);
62+await params.setSessionState({
63+cfg: params.cfg,
64+sessionKey: params.sessionKey,
65+state: "error",
66+lastError: acpError.message,
67+});
68+throw acpError;
69+}
70+});
71+}
72+73+async function cancelActiveTurn(params: {
74+activeTurn: ActiveTurnState;
75+reason?: string;
76+}): Promise<void> {
77+params.activeTurn.abortController.abort();
78+if (!params.activeTurn.cancelPromise) {
79+params.activeTurn.cancelPromise = params.activeTurn.runtime.cancel({
80+handle: params.activeTurn.handle,
81+reason: params.reason,
82+});
83+}
84+await withAcpRuntimeErrorBoundary({
85+run: async () => await params.activeTurn.cancelPromise!,
86+fallbackCode: "ACP_TURN_FAILED",
87+fallbackMessage: "ACP cancel failed before completion.",
88+});
89+}
90+91+async function cancelRuntimeHandle(params: {
92+runtime: AcpRuntime;
93+handle: AcpRuntimeHandle;
94+reason?: string;
95+}): Promise<void> {
96+await withAcpRuntimeErrorBoundary({
97+run: async () =>
98+await params.runtime.cancel({
99+handle: params.handle,
100+reason: params.reason,
101+}),
102+fallbackCode: "ACP_TURN_FAILED",
103+fallbackMessage: "ACP cancel failed before completion.",
104+});
105+}
106+107+function normalizeCancelError(error: unknown): AcpRuntimeError {
108+return toAcpRuntimeError({
109+ error,
110+fallbackCode: "ACP_TURN_FAILED",
111+fallbackMessage: "ACP cancel failed before completion.",
112+});
113+}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。