UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.06 kB
import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{r as isIncognitoSessionKey}from"./session-key-DYtwLLn1.mjs";import"./session-key-BPgKfU2m.mjs";import{a as resolveUnsuffixedSqliteTargetFromSessionStorePath}from"./session-sqlite-target-paths-CA4CNfL-.mjs";import{m as readDatabasePathIdentitySync}from"./sqlite-worker-identity-CikEUuqF.mjs";import{m as isCliPartialOutputRejected}from"./error-oegzzI05.mjs";import{n as SessionMutationAuthorizationChangedError}from"./session-mutation-authorization-error-D4Cgq_Qr.mjs";import{y as retainPreparedSessionGenerationFacts}from"./session-accessor.sqlite-entry-cache-publication-state-D1mC85IV.mjs";import{t as loadGatewaySessionEntryReadOnlyInWorker}from"./session-utils-store-worker-43BDHG7V.mjs";import{i as loadIncognitoGatewaySessionEntryReadOnly}from"./session-utils-store-CbjJRFJe.mjs";import"./session-utils-Cd7daNqX.mjs";import{n as broadcastChatError}from"./chat-broadcast-B3al3md-.mjs"; //!src/gateway/server-methods/chat-aborted-partial.ts async function prepareChatAbortSessionSnapshot(e){try{return{ok:true,value:await loadGatewaySessionEntryReadOnlyInWorker(e)}}catch(e){return{ok:false,error:e}}}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)}}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})}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}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}}}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-CUyRZaSy.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.`;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};