openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
279 lines (278 loc) • 8.9 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { s as redactSensitiveText } from "./redact-Mj7pppxC.mjs";
import { s as emitTrustedDiagnosticEvent } from "./diagnostic-events-BjAH1Kbx.mjs";
import { a as emitAgentEvent, i as emitAgentAuditEvent } from "./agent-events-BdfWYmot.mjs";
import { i as buildAgentRunTerminalOutcomeFromLifecycleEvent } from "./agent-run-terminal-outcome-wduf2occ.mjs";
import { p as resolveAgentRunAbortLifecycleFields } from "./run-termination-0Ig0-bk6.mjs";
import { t as PROXY_ENV_KEYS } from "./proxy-env-CZP0dXDG.mjs";
import { r as formatAcpErrorChain } from "./errors-DGA2X_Nu.mjs";
import "./errors-C4nx2HUe.mjs";
import { t as ACP_TURN_TIMEOUT_DETAIL_CODE } from "./manager.turn-timeout-BI2AZt1S.mjs";
import { t as resolveAcpToolTerminalOutcome } from "./tool-status-CTT8W9yj.mjs";
//#region src/agents/command/acp-lifecycle.ts
function buildAcpResult(e) {
const t = e.payloadText ? [{ text: e.payloadText }] : [];
const n = resolveAgentRunAbortLifecycleFields(e.abortSignal);
const r = e.resultStatus === "cancelled";
return {
payloads: t,
meta: {
durationMs: Date.now() - e.startedAt,
aborted: n.aborted ?? r,
stopReason: n.stopReason ?? (r ? "stop" : e.stopReason),
terminalReply: e.terminalReply,
finalAssistantVisibleText: e.payloadText
}
};
}
function acpRunIdentity(e) {
return {
runId: e.runId,
...e.sessionKey ? { sessionKey: e.sessionKey } : {},
...e.agentId ? { agentId: e.agentId } : {}
};
}
function emitAcpLifecycleEvent(e, t) {
(e.auditOnly ? emitAgentAuditEvent : emitAgentEvent)({
...acpRunIdentity(e),
...e.lifecycleGeneration ? { lifecycleGeneration: e.lifecycleGeneration } : {},
stream: "lifecycle",
data: t
});
}
function emitAcpLifecycleStart(e) {
emitAcpLifecycleEvent(e, {
phase: "start",
...e.completionSource ? { completionSource: e.completionSource } : {},
startedAt: e.startedAt
});
}
const MAX_TRACKED_ACP_TOOLS = 4096;
function createAcpToolLifecycleTracker() {
return {
active: /* @__PURE__ */ new Map(),
terminalToolCallIds: /* @__PURE__ */ new Set()
};
}
function acpAuditToolName(e) {
switch (e) {
case "read":
case "edit":
case "delete":
case "move":
case "search":
case "execute":
case "fetch":
case "switch_mode":
case "think":
case "other": return `acp_${e}`;
default: return "acp_tool";
}
}
function resolveAcpToolTerminalReason(e, t, n, r) {
const i = resolveAgentRunAbortLifecycleFields(e);
if (i.aborted) return i.stopReason === "timeout" ? "timed_out" : "cancelled";
const a = normalizeOptionalLowercaseString(t);
if (a === "timeout") return "timed_out";
if (r === "cancelled") return "cancelled";
if (n instanceof Error && n.detailCode === "TURN_TIMEOUT") return "timed_out";
if (a === "cancel" || a === "cancelled" || a === "manual-cancel") return "cancelled";
return "failed";
}
function resolveAcpLifecycleEndFields(e, t, n) {
const r = resolveAgentRunAbortLifecycleFields(e);
if (r.aborted) return r;
const i = resolveAcpToolTerminalReason(void 0, t, void 0, n);
if (i === "timed_out") return {
aborted: true,
stopReason: "timeout",
status: "timed_out"
};
if (i === "cancelled") return {
aborted: true,
stopReason: "stop",
status: "cancelled"
};
return {};
}
function emitAcpToolExecutionEvent(e) {
const { event: t } = e;
const n = Date.now();
const r = t.toolCallId?.trim() ? t.toolCallId : void 0;
const i = r ? e.toolTracker.active.get(r) : void 0;
const a = resolveAcpToolTerminalOutcome(t.status);
const o = acpAuditToolName(t.kind);
if (r && !i) {
if (e.toolTracker.terminalToolCallIds.has(r)) return;
if (e.toolTracker.active.size + e.toolTracker.terminalToolCallIds.size >= MAX_TRACKED_ACP_TOOLS) return;
}
if (!i && (r !== void 0 || r === void 0 && a !== void 0)) {
emitTrustedDiagnosticEvent({
type: "tool.execution.started",
...acpRunIdentity(e),
...r ? { toolCallId: r } : {},
toolName: o,
toolSource: "core",
toolOwner: "acp"
});
if (r) e.toolTracker.active.set(r, {
...acpRunIdentity(e),
toolCallId: r,
toolName: o,
startedAt: n
});
}
if (!a) return;
const s = resolveAcpToolTerminalReason(e.abortSignal, void 0, void 0, a === "cancelled" ? "cancelled" : void 0);
const c = Math.max(0, n - (i?.startedAt ?? n));
const l = {
...acpRunIdentity(e),
...r ? { toolCallId: r } : {},
toolName: i?.toolName ?? o,
toolSource: "core",
toolOwner: "acp",
durationMs: c
};
emitTrustedDiagnosticEvent(a === "completed" ? {
type: "tool.execution.completed",
...l
} : {
type: "tool.execution.error",
...l,
errorCategory: s === "cancelled" ? "aborted" : "acp_tool",
terminalReason: s
});
if (r) {
e.toolTracker.active.delete(r);
e.toolTracker.terminalToolCallIds.add(r);
}
}
function finalizeAcpToolsForRun(e, t, n) {
const r = Date.now();
for (const i of e.active.values()) emitTrustedDiagnosticEvent({
type: "tool.execution.error",
runId: t,
...i.sessionKey ? { sessionKey: i.sessionKey } : {},
...i.agentId ? { agentId: i.agentId } : {},
toolName: i.toolName,
toolSource: "core",
toolOwner: "acp",
toolCallId: i.toolCallId,
durationMs: Math.max(0, r - i.startedAt),
errorCategory: n === "cancelled" ? "aborted" : "acp_tool_incomplete",
terminalReason: n
});
e.active.clear();
e.terminalToolCallIds.clear();
}
function resolvePresentProxyEnvKeys(e = process.env) {
return PROXY_ENV_KEYS.filter((t) => Boolean(e[t]?.trim()));
}
function sanitizeAcpDiagnosticText(e) {
return truncateUtf16Safe(redactSensitiveText(e).replace(/\s+/g, " ").trim(), 240);
}
function acpRuntimeEventDiagnostics(e) {
if (e.type === "status" || e.type === "tool_call") return {
eventType: e.type,
text: sanitizeAcpDiagnosticText(e.text),
...e.tag ? { tag: e.tag } : {},
...e.type === "tool_call" ? {
...e.status ? { status: sanitizeAcpDiagnosticText(e.status) } : {},
...e.title ? { title: sanitizeAcpDiagnosticText(e.title) } : {},
...e.toolCallId ? { toolCallId: sanitizeAcpDiagnosticText(e.toolCallId) } : {}
} : {}
};
if (e.type === "error") return {
eventType: e.type,
message: sanitizeAcpDiagnosticText(e.message),
...e.code ? { code: sanitizeAcpDiagnosticText(e.code) } : {},
...typeof e.retryable === "boolean" ? { retryable: e.retryable } : {}
};
if (e.type === "done") return {
eventType: e.type,
...e.status ? { status: e.status } : {},
...e.stopReason ? { stopReason: sanitizeAcpDiagnosticText(e.stopReason) } : {}
};
return {
eventType: e.type,
stream: e.stream ?? "output"
};
}
function emitAcpPromptSubmitted(e) {
emitAgentEvent({
runId: e.runId,
stream: "acp",
...e.sessionKey ? { sessionKey: e.sessionKey } : {},
data: {
phase: "prompt_submitted",
at: e.at,
proxyEnvKeys: resolvePresentProxyEnvKeys()
}
});
}
function emitAcpRuntimeEvent(e) {
if (e.event.type === "tool_call") emitAcpToolExecutionEvent({
...e,
event: e.event
});
if (!e.auditOnly) emitAgentEvent({
...acpRunIdentity(e),
stream: "acp",
data: {
phase: "runtime_event",
...acpRuntimeEventDiagnostics(e.event)
}
});
}
function emitAcpTerminalLifecycle(e, t) {
const n = {
...t,
executionSettled: true,
...e.completionSource ? { completionSource: e.completionSource } : {}
};
emitAcpLifecycleEvent(e, n);
return buildAgentRunTerminalOutcomeFromLifecycleEvent({
phase: t.phase,
data: n,
endedAt: t.endedAt
});
}
function emitAcpLifecycleEnd(e) {
finalizeAcpToolsForRun(e.toolTracker, e.runId, e.endFields.stopReason === "timeout" ? "timed_out" : e.endFields.aborted ? "cancelled" : "failed");
return emitAcpTerminalLifecycle(e, {
phase: "end",
endedAt: Date.now(),
...e.endFields,
...e.terminalReply ? { terminalReply: e.terminalReply } : {}
});
}
function emitAcpLifecycleError(e) {
const t = resolveAcpToolTerminalReason(e.abortSignal, void 0, e.error);
finalizeAcpToolsForRun(e.toolTracker, e.runId, t);
const n = e.terminalOutcome === "blocked" ? { livenessState: "blocked" } : t === "timed_out" ? {
aborted: true,
stopReason: "timeout",
status: "timed_out"
} : resolveAgentRunAbortLifecycleFields(e.abortSignal);
return emitAcpTerminalLifecycle(e, {
phase: "error",
...!e.auditOnly ? { error: formatAcpErrorChain(e.error) } : {},
endedAt: Date.now(),
...n,
...e.terminalReply ? { terminalReply: e.terminalReply } : {}
});
}
function emitAcpAssistantDelta(e) {
emitAgentEvent({
runId: e.runId,
stream: "assistant",
data: {
itemId: e.runId,
text: e.text,
delta: e.delta
}
});
}
//#endregion
export { emitAcpLifecycleError as a, emitAcpRuntimeEvent as c, emitAcpLifecycleEnd as i, resolveAcpLifecycleEndFields as l, createAcpToolLifecycleTracker as n, emitAcpLifecycleStart as o, emitAcpAssistantDelta as r, emitAcpPromptSubmitted as s, buildAcpResult as t };