openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
212 lines (211 loc) • 6.69 kB
JavaScript
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs";
import "./session-key-CqAyZeVk.mjs";
import { a as resolveUnsuffixedSqliteTargetFromSessionStorePath } from "./session-sqlite-target-paths-Dw0pT36E.mjs";
import { m as readDatabasePathIdentitySync } from "./sqlite-worker-identity-BH8ZkJU9.mjs";
import { l as isCliPartialOutputRejected } from "./error-BCRqSFIY.mjs";
import { t as SessionMutationAuthorizationChangedError } from "./session-mutation-authorization-error-0K0ZVV7q.mjs";
import { v as retainPreparedSessionGenerationFacts } from "./session-accessor.sqlite-entry-cache-publication-state-B4mkg2yK.mjs";
import { t as loadGatewaySessionEntryReadOnlyInWorker } from "./session-utils-store-worker-BU_tdh8T.mjs";
import { i as loadIncognitoGatewaySessionEntryReadOnly } from "./session-utils-store-CiI_s_OK.mjs";
import "./session-utils-CavYkLyd.mjs";
import { n as broadcastChatError } from "./chat-broadcast-til2j59n.mjs";
//#region src/gateway/server-methods/chat-aborted-partial.ts
/** Prepare fallible transcript metadata before synchronous cancellation can signal a producer. */
async function prepareChatAbortSessionSnapshot(e) {
try {
return {
ok: true,
value: await loadGatewaySessionEntryReadOnlyInWorker(e)
};
} catch (e) {
return {
ok: false,
error: e
};
}
}
/** Follow the existing commit receipts until this run releases its cancellation owner. */
function retainChatAbortSessionSnapshot(e) {
if (!e.ok) return {
read: () => e,
release: () => {}
};
const t = e.value;
if (isIncognitoSessionKey(t.canonicalKey)) return {
read: () => {
try {
return {
ok: true,
value: loadIncognitoGatewaySessionEntryReadOnly(t.canonicalKey, { agentId: t.agentId })
};
} catch (e) {
return {
ok: false,
error: e
};
}
},
release: () => {}
};
try {
const e = readDatabasePathIdentitySync(resolveUnsuffixedSqliteTargetFromSessionStorePath(t.storePath).path);
const n = retainPreparedSessionGenerationFacts({
databaseIdentity: e.key,
sessionKey: t.canonicalKey,
entry: t.entry
});
return {
read: () => {
const e = n.readCurrent();
if (e === void 0) return {
ok: false,
error: /* @__PURE__ */ new Error("Aborted partial transcript session is unavailable")
};
return {
ok: true,
value: {
...t,
entry: e ? t.entry : void 0
}
};
},
release: n.release
};
} catch (e) {
return {
read: () => ({
ok: false,
error: e
}),
release: () => {}
};
}
}
function withAbortedPartialPersistenceWarning(e, t) {
return t ? {
...e,
message: `${e.message} ${t}`
} : e;
}
function withQueuedCollectorWarning(e, t) {
return e.ok ? {
ok: true,
value: {
...e.value,
warning: t
}
} : {
ok: false,
error: withAbortedPartialPersistenceWarning(e.error, t)
};
}
/** Retain a failed save when a later cancellation or terminal write also fails. */
function abortedPartialPersistenceError(e, t) {
if (!t) return e;
const n = `${formatErrorMessage(e)} ${t}`;
return e instanceof SessionMutationAuthorizationChangedError ? new SessionMutationAuthorizationChangedError({
...e.error,
message: n
}) : new Error(n, { cause: e });
}
/** Settle already accepted persistence even when a later cancellation effect failed. */
async function settleChatAbortPersistence(e, t) {
let n;
try {
n = await e();
} catch (e) {
if (!t) throw e;
throw new AggregateError([t.error, e], "Chat cancellation and persistence failed", { cause: e });
}
if (t) throw abortedPartialPersistenceError(t.error, n);
return n;
}
/** Capture before signaling cancellation, without loading asynchronous transcript writers. */
function captureAbortedPartial(e) {
const { runId: t, abortOrigin: n } = e;
try {
const r = e.session;
if (!r) throw new Error("Aborted partial transcript metadata is unavailable");
if (!r.ok) throw r.error;
const { cfg: i, storePath: a, entry: o, canonicalKey: s, agentId: c } = r.value;
if (o?.sessionId !== e.sessionId) throw new Error("Aborted partial transcript session changed before persistence");
const l = e.resolveTerminalProducer?.();
return {
runId: t,
abortOrigin: n,
ok: true,
settlement: {
deferred: false,
producer: l?.sessionId === e.sessionId && l.sessionKey === e.sessionKey ? l : void 0
},
value: {
sessionKey: s,
sessionId: e.sessionId,
expectedSessionId: e.sessionId,
expectedLifecycleRevision: o.lifecycleRevision ?? null,
agentId: c,
storePath: a,
config: i,
message: e.text,
idempotencyKey: `${t}:assistant`,
abortMeta: {
aborted: true,
origin: n,
runId: t
}
}
};
} catch (e) {
return {
runId: t,
abortOrigin: n,
ok: false,
error: e
};
}
}
/** Transfer the fallback before cancellation can clear the producer's session slot. */
function deferAbortedPartialPersistence(e, t) {
if (!e?.ok || e.settlement.deferred || !e.settlement.producer) return;
let n = false;
try {
e.settlement.deferred = e.settlement.producer.handoff((r) => t.trackExecution(async () => {
const i = await r;
if (n || isCliPartialOutputRejected(i)) return;
let a;
try {
const { persistAbortedPartial: n } = await import("./chat-transcript-persistence-yNS_vzBo.mjs");
a = await n({
context: t,
snapshot: e,
producerSettled: true
});
} catch (e) {
t.logGateway.warn(`chat.abort deferred transcript append failed: ${formatErrorMessage(e)}`);
a = ABORTED_PARTIAL_PERSISTENCE_WARNING;
}
if (a) try {
broadcastChatError({
terminalEntry: void 0,
context: t,
runId: e.runId,
sessionKey: e.value.sessionKey,
agentId: e.value.agentId,
errorMessage: a,
stopReason: "aborted-partial-persistence-failed"
});
} catch (e) {
t.logGateway.warn(`chat.abort persistence warning delivery failed: ${formatErrorMessage(e)}`);
}
}));
if (e.settlement.deferred) return () => {
n = true;
};
} catch (e) {
t.logGateway.warn(`chat.abort producer handoff failed: ${formatErrorMessage(e)}`);
}
}
const ABORTED_PARTIAL_PERSISTENCE_WARNING = "Stopped, but a reply could not be saved to history. Copy any visible text before leaving this chat.";
//#endregion
export { prepareChatAbortSessionSnapshot as a, withAbortedPartialPersistenceWarning as c, deferAbortedPartialPersistence as i, withQueuedCollectorWarning as l, abortedPartialPersistenceError as n, retainChatAbortSessionSnapshot as o, captureAbortedPartial as r, settleChatAbortPersistence as s, ABORTED_PARTIAL_PERSISTENCE_WARNING as t };