refactor: centralize acp session resolution guards

This commit is contained in:
Peter Steinberger
2026-03-08 17:11:05 +00:00
parent 25d0aa7296
commit f6cb77134c
3 changed files with 50 additions and 101 deletions

View File

@@ -2,6 +2,7 @@ import type { OpenClawConfig } from "../../config/config.js";
import type { SessionAcpMeta } from "../../config/sessions/types.js";
import { normalizeAgentId, parseAgentSessionKey } from "../../routing/session-key.js";
import { ACP_ERROR_CODES, AcpRuntimeError } from "../runtime/errors.js";
import type { AcpSessionResolution } from "./manager.types.js";
export function resolveAcpAgentFromSessionKey(sessionKey: string, fallback = "main"): string {
const parsed = parseAgentSessionKey(sessionKey);
@@ -15,6 +16,29 @@ export function resolveMissingMetaError(sessionKey: string): AcpRuntimeError {
);
}
export function resolveAcpSessionResolutionError(
resolution: AcpSessionResolution,
): AcpRuntimeError | null {
if (resolution.kind === "ready") {
return null;
}
if (resolution.kind === "stale") {
return resolution.error;
}
return new AcpRuntimeError(
"ACP_SESSION_INIT_FAILED",
`Session is not ACP-enabled: ${resolution.sessionKey}`,
);
}
export function requireReadySessionMeta(resolution: AcpSessionResolution): SessionAcpMeta {
const error = resolveAcpSessionResolutionError(resolution);
if (error) {
throw error;
}
return resolution.meta;
}
export function normalizeSessionKey(sessionKey: string): string {
return sessionKey.trim();
}