File size: 6,328 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | import type { OpenClawConfig } from "../config/config.js";
import type { SessionAcpMeta } from "../config/sessions/types.js";
import { logVerbose } from "../globals.js";
import { getAcpSessionManager } from "./control-plane/manager.js";
import { resolveAcpAgentFromSessionKey } from "./control-plane/manager.utils.js";
import { resolveConfiguredAcpBindingSpecBySessionKey } from "./persistent-bindings.resolve.js";
import {
buildConfiguredAcpSessionKey,
normalizeText,
type ConfiguredAcpBindingSpec,
type ResolvedConfiguredAcpBinding,
} from "./persistent-bindings.types.js";
import { readAcpSessionEntry } from "./runtime/session-meta.js";
function sessionMatchesConfiguredBinding(params: {
cfg: OpenClawConfig;
spec: ConfiguredAcpBindingSpec;
meta: SessionAcpMeta;
}): boolean {
const desiredAgent = (params.spec.acpAgentId ?? params.spec.agentId).trim().toLowerCase();
const currentAgent = (params.meta.agent ?? "").trim().toLowerCase();
if (!currentAgent || currentAgent !== desiredAgent) {
return false;
}
if (params.meta.mode !== params.spec.mode) {
return false;
}
const desiredBackend = params.spec.backend?.trim() || params.cfg.acp?.backend?.trim() || "";
if (desiredBackend) {
const currentBackend = (params.meta.backend ?? "").trim();
if (!currentBackend || currentBackend !== desiredBackend) {
return false;
}
}
const desiredCwd = params.spec.cwd?.trim();
if (desiredCwd !== undefined) {
const currentCwd = (params.meta.runtimeOptions?.cwd ?? params.meta.cwd ?? "").trim();
if (desiredCwd !== currentCwd) {
return false;
}
}
return true;
}
export async function ensureConfiguredAcpBindingSession(params: {
cfg: OpenClawConfig;
spec: ConfiguredAcpBindingSpec;
}): Promise<{ ok: true; sessionKey: string } | { ok: false; sessionKey: string; error: string }> {
const sessionKey = buildConfiguredAcpSessionKey(params.spec);
const acpManager = getAcpSessionManager();
try {
const resolution = acpManager.resolveSession({
cfg: params.cfg,
sessionKey,
});
if (
resolution.kind === "ready" &&
sessionMatchesConfiguredBinding({
cfg: params.cfg,
spec: params.spec,
meta: resolution.meta,
})
) {
return {
ok: true,
sessionKey,
};
}
if (resolution.kind !== "none") {
await acpManager.closeSession({
cfg: params.cfg,
sessionKey,
reason: "config-binding-reconfigure",
clearMeta: false,
allowBackendUnavailable: true,
requireAcpSession: false,
});
}
await acpManager.initializeSession({
cfg: params.cfg,
sessionKey,
agent: params.spec.acpAgentId ?? params.spec.agentId,
mode: params.spec.mode,
cwd: params.spec.cwd,
backendId: params.spec.backend,
});
return {
ok: true,
sessionKey,
};
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
logVerbose(
`acp-persistent-binding: failed ensuring ${params.spec.channel}:${params.spec.accountId}:${params.spec.conversationId} -> ${sessionKey}: ${message}`,
);
return {
ok: false,
sessionKey,
error: message,
};
}
}
export async function ensureConfiguredAcpBindingReady(params: {
cfg: OpenClawConfig;
configuredBinding: ResolvedConfiguredAcpBinding | null;
}): Promise<{ ok: true } | { ok: false; error: string }> {
if (!params.configuredBinding) {
return { ok: true };
}
const ensured = await ensureConfiguredAcpBindingSession({
cfg: params.cfg,
spec: params.configuredBinding.spec,
});
if (ensured.ok) {
return { ok: true };
}
return {
ok: false,
error: ensured.error ?? "unknown error",
};
}
export async function resetAcpSessionInPlace(params: {
cfg: OpenClawConfig;
sessionKey: string;
reason: "new" | "reset";
}): Promise<{ ok: true } | { ok: false; skipped?: boolean; error?: string }> {
const sessionKey = params.sessionKey.trim();
if (!sessionKey) {
return {
ok: false,
skipped: true,
};
}
const meta = readAcpSessionEntry({
cfg: params.cfg,
sessionKey,
})?.acp;
const configuredBinding =
!meta || !normalizeText(meta.agent)
? resolveConfiguredAcpBindingSpecBySessionKey({
cfg: params.cfg,
sessionKey,
})
: null;
if (!meta) {
if (configuredBinding) {
const ensured = await ensureConfiguredAcpBindingSession({
cfg: params.cfg,
spec: configuredBinding,
});
if (ensured.ok) {
return { ok: true };
}
return {
ok: false,
error: ensured.error,
};
}
return {
ok: false,
skipped: true,
};
}
const acpManager = getAcpSessionManager();
const agent =
normalizeText(meta.agent) ??
configuredBinding?.acpAgentId ??
configuredBinding?.agentId ??
resolveAcpAgentFromSessionKey(sessionKey, "main");
const mode = meta.mode === "oneshot" ? "oneshot" : "persistent";
const runtimeOptions = { ...meta.runtimeOptions };
const cwd = normalizeText(runtimeOptions.cwd ?? meta.cwd);
try {
await acpManager.closeSession({
cfg: params.cfg,
sessionKey,
reason: `${params.reason}-in-place-reset`,
clearMeta: false,
allowBackendUnavailable: true,
requireAcpSession: false,
});
await acpManager.initializeSession({
cfg: params.cfg,
sessionKey,
agent,
mode,
cwd,
backendId: normalizeText(meta.backend) ?? normalizeText(params.cfg.acp?.backend),
});
const runtimeOptionsPatch = Object.fromEntries(
Object.entries(runtimeOptions).filter(([, value]) => value !== undefined),
) as SessionAcpMeta["runtimeOptions"];
if (runtimeOptionsPatch && Object.keys(runtimeOptionsPatch).length > 0) {
await acpManager.updateSessionRuntimeOptions({
cfg: params.cfg,
sessionKey,
patch: runtimeOptionsPatch,
});
}
return { ok: true };
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
logVerbose(`acp-persistent-binding: failed reset for ${sessionKey}: ${message}`);
return {
ok: false,
error: message,
};
}
}
|