Files
openclaw/src/plugin-sdk/acp-runtime-backend.ts
Ted Li 6cb06f5fbc fix(reply): preserve sessions_send external routes (#88803)
* fix(reply): preserve sessions_send external routes

* fix(reply): preserve inherited route thread ids

* fix(reply): keep sessions_send delivery single-owner

* fix(reply): satisfy dispatch route lint

* fix(reply): preserve inherited ACP route metadata

* test(reply): type inherited route event assertions

* test(ci): satisfy current lint rules

* fix(reply): avoid stale inherited route threads

* fix(reply): trust explicit inherited route threads

* fix(reply): require trusted route thread sources

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-05-31 23:43:05 -04:00

107 lines
3.3 KiB
TypeScript

// Lightweight ACP runtime backend helpers for startup-loaded plugins.
import { hasExplicitCommandContextText } from "../auto-reply/reply/context-text.js";
import type {
PluginHookReplyDispatchContext,
PluginHookReplyDispatchEvent,
PluginHookReplyDispatchResult,
} from "../plugins/types.js";
export { AcpRuntimeError, isAcpRuntimeError } from "../acp/runtime/errors.js";
export type { AcpRuntimeErrorCode } from "../acp/runtime/errors.js";
export {
getAcpRuntimeBackend,
registerAcpRuntimeBackend,
requireAcpRuntimeBackend,
unregisterAcpRuntimeBackend,
} from "../acp/runtime/registry.js";
export type {
AcpRuntime,
AcpRuntimeCapabilities,
AcpRuntimeDoctorReport,
AcpRuntimeEnsureInput,
AcpRuntimeEvent,
AcpRuntimeHandle,
AcpRuntimeStatus,
AcpRuntimeTurn,
AcpRuntimeTurnAttachment,
AcpRuntimeTurnInput,
AcpRuntimeTurnResult,
AcpRuntimeTurnResultError,
AcpSessionUpdateTag,
} from "@openclaw/acp-core/runtime/types";
let dispatchAcpRuntimePromise: Promise<
typeof import("../auto-reply/reply/dispatch-acp.runtime.js")
> | null = null;
function loadDispatchAcpRuntime() {
dispatchAcpRuntimePromise ??= import("../auto-reply/reply/dispatch-acp.runtime.js");
return dispatchAcpRuntimePromise;
}
export async function tryDispatchAcpReplyHook(
event: PluginHookReplyDispatchEvent,
ctx: PluginHookReplyDispatchContext,
): Promise<PluginHookReplyDispatchResult | void> {
// Under sendPolicy: "deny", ACP-bound sessions still need their turns to flow
// through acpManager.runTurn so session state, tool calls, and memory stay
// consistent. Delivery suppression is handled by the ACP delivery path.
if (
event.sendPolicy === "deny" &&
!event.suppressUserDelivery &&
!hasExplicitCommandContextText(event.ctx) &&
!event.isTailDispatch
) {
return;
}
const runtime = await loadDispatchAcpRuntime();
const bypassForCommand = await runtime.shouldBypassAcpDispatchForCommand(event.ctx, ctx.cfg);
if (
event.sendPolicy === "deny" &&
!event.suppressUserDelivery &&
!bypassForCommand &&
!event.isTailDispatch
) {
return;
}
const result = await runtime.tryDispatchAcpReply({
ctx: event.ctx,
cfg: ctx.cfg,
dispatcher: ctx.dispatcher,
runId: event.runId,
sessionKey: event.sessionKey,
images: event.images,
abortSignal: ctx.abortSignal,
inboundAudio: event.inboundAudio,
sessionTtsAuto: event.sessionTtsAuto,
ttsChannel: event.ttsChannel,
suppressUserDelivery: event.suppressUserDelivery,
suppressReplyLifecycle: event.suppressReplyLifecycle === true || event.sendPolicy === "deny",
sourceReplyDeliveryMode: event.sourceReplyDeliveryMode,
shouldRouteToOriginating: event.shouldRouteToOriginating,
originatingChannel: event.originatingChannel,
originatingTo: event.originatingTo,
originatingAccountId: event.originatingAccountId,
originatingThreadId: event.originatingThreadId,
shouldSendToolSummaries: event.shouldSendToolSummaries,
shouldSendToolSummariesNow: () => event.shouldSendToolSummaries,
bypassForCommand,
onReplyStart: ctx.onReplyStart,
recordProcessed: ctx.recordProcessed,
markIdle: ctx.markIdle,
});
if (!result) {
return;
}
return {
handled: true,
queuedFinal: result.queuedFinal,
counts: result.counts,
};
}