openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
142 lines (141 loc) • 4.84 kB
JavaScript
import { g as readStringValue } from "./string-coerce-CCIiB4H7.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { t as classifyGatewayStorageFailure } from "./sqlite-error-diagnostics-6_hREUnq.mjs";
import { n as AGENT_RUN_RESTART_ABORT_STOP_REASON } from "./agent-run-terminal-outcome-CurjWZiA.mjs";
import { a as emitAgentEvent } from "./agent-events-BdfWYmot.mjs";
import { c as getFailoverErrorCode } from "./error-BCRqSFIY.mjs";
import "./run-termination-0Ig0-bk6.mjs";
import { t as renderAgentHarnessPreflightUserMessage } from "./user-facing-text-Dqp3fcZx.mjs";
import { v as renderFailoverCodeUserCopy } from "./user-copy-DFvWpx47.mjs";
import { c as classifyOAuthRefreshFailureError } from "./oauth-refresh-failure-DxrQiYzV.mjs";
import { u as renderStorageFailureSummary } from "./assistant-request-failure-copy-CDAkRIru.mjs";
import { t as captureAgentWorkspaceReadiness } from "./workspace-readiness-ClUjI0tp.mjs";
//#region src/auto-reply/reply/agent-lifecycle-terminal.ts
const DEFERRED_TERMINAL_METADATA_KEYS = [
"stopReason",
"yielded",
"timeoutPhase",
"executionStarted",
"providerStarted",
"aborted",
"livenessState",
"replayInvalid",
"errorObservation",
"suppressErrorNotice",
"assistantTranscriptIdempotencyKey"
];
function resolveAgentLifecycleTerminalMetadata(e) {
const t = {};
if (!e || typeof e !== "object") return t;
const n = e;
for (const e of DEFERRED_TERMINAL_METADATA_KEYS) if (Object.hasOwn(n, e)) t[e] = n[e];
return t;
}
function createAgentLifecycleTerminalBackstop(e) {
const t = captureAgentWorkspaceReadiness(e.sessionKey);
let n = {
kind: "pending",
metadata: {}
};
let r = e.startedAt ?? Date.now();
const beginAttempt = () => {
if (n.kind !== "emitted") n = {
kind: "pending",
metadata: {}
};
};
const note = (e) => {
if (n.kind === "emitted" || e.stream !== "lifecycle") return;
const t = readStringValue(e.data.phase);
if (t === "start") {
beginAttempt();
if (typeof e.data.startedAt === "number") r = e.data.startedAt;
}
if (t === "finishing" && n.kind === "pending") {
n.deferredError = readStringValue(e.data.error) ?? n.deferredError;
Object.assign(n.metadata, resolveAgentLifecycleTerminalMetadata(e.data));
}
if (t === "end" || t === "error") n = { kind: "emitted" };
};
const prepareTerminal = (n, i, a, o) => {
const s = t?.getFailure?.();
const c = s ? "error" : i;
const l = s ?? a;
const u = s ? {} : e.resolveTerminationFields(c === "error" ? l : void 0);
const d = u.stopReason === AGENT_RUN_RESTART_ABORT_STOP_REASON;
const f = {
...n.metadata,
phase: d ? "end" : c,
endedAt: Date.now(),
startedAt: r
};
if (d) {
f.aborted = true;
f.stopReason = AGENT_RUN_RESTART_ABORT_STOP_REASON;
} else if (c === "error") {
const e = classifyOAuthRefreshFailureError(l);
f.error = renderStorageFailureSummary(classifyGatewayStorageFailure(l)) ?? renderAgentHarnessPreflightUserMessage(l) ?? renderFailoverCodeUserCopy(getFailoverErrorCode(l)) ?? (e?.summary ? `⚠️ ${e.summary}` : void 0) ?? formatErrorMessage(l);
if (e?.summary) f.errorObservation = {
...e.provider ? { provider: e.provider } : {},
...e.reason ? { failoverReason: e.reason } : {},
providerRuntimeFailureKind: "auth_refresh",
...e.errorType ? { providerErrorType: e.errorType } : {},
...e.status ? { httpStatus: e.status } : {}
};
Object.assign(f, u);
} else {
const e = l && typeof l === "object" && "meta" in l ? l.meta : void 0;
Object.assign(f, resolveAgentLifecycleTerminalMetadata(e));
if (u.aborted === true) f.aborted = true;
if (u.stopReason && !readStringValue(f.stopReason)) f.stopReason = u.stopReason;
}
Object.assign(f, o);
if (s) {
f.phase = "error";
f.error = s.message;
delete f.aborted;
delete f.stopReason;
delete f.timeoutPhase;
delete f.providerStarted;
delete f.yielded;
}
return {
runId: e.runId,
lifecycleGeneration: e.getLifecycleGeneration(),
...e.sessionKey ? { sessionKey: e.sessionKey } : {},
stream: "lifecycle",
data: f
};
};
const capture = (e, t, r) => {
if (n.kind === "pending") n = {
kind: "captured",
event: prepareTerminal(n, e, t, r)
};
};
const emit = (t, r, i) => {
const a = n;
if (a.kind === "emitted") return;
n = { kind: "emitted" };
const o = a.kind === "captured" ? a.event : prepareTerminal(a, t, r, i);
const s = {
...o,
data: {
...o.data,
executionSettled: true
}
};
emitAgentEvent(s);
e.onTerminalEvent?.(s);
};
return {
beginAttempt,
replaceAttemptTerminal: beginAttempt,
capture,
emit,
getDeferredError: () => n.kind === "pending" ? n.deferredError : void 0,
note
};
}
//#endregion
export { resolveAgentLifecycleTerminalMetadata as n, createAgentLifecycleTerminalBackstop as t };