UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 5.75 kB
import{i as resolveGlobalMap}from"./global-singleton-DolL9xGt.mjs";import{r as runOutsideStoreWriterContext}from"./store-writer-queue-CEDy9o_2.mjs";import{n as runWithSessionControllerCleanup}from"./session-controller.cleanup-context-9qzwfFSP.mjs";import{C as runWithGatewayDetachedWorkContinuation}from"./gateway-work-admission-CnMW3_qr.mjs";import"./session-controller.state-BIDbpLPn.mjs";import{i as MAX_VISIBLE_MESSAGE_MAX_MESSAGES,l as normalizeVisibleMessageLimit,r as MAX_VISIBLE_MESSAGE_MAX_BYTES}from"./session-accessor.sqlite-visible-cursor-CWhnCe4q.mjs";import{n as createSessionResetBoundaryId}from"./session-reset-boundary-event--qLax8_-.mjs";import{r as attachSessionEndTranscriptSource}from"./hooks-Bu2gwS_g.mjs";import{t as capArrayByJsonBytes}from"./session-utils.fs-_wntqfxY.mjs";import{c as readSessionMessagesAroundIdWithStatsAsync,n as readRecentSessionMessagesWithStatsAsync}from"./session-transcript-readers-D9Cuet3N.mjs"; //!src/gateway/session-end-transcript-reader.ts const ENDED_TRANSCRIPT_MAX_BYTES=Math.min(MAX_VISIBLE_MESSAGE_MAX_BYTES,8388608);function normalizeEndedTranscriptOptions(e){return{maxMessages:normalizeVisibleMessageLimit(e.maxMessages,1,MAX_VISIBLE_MESSAGE_MAX_MESSAGES,`maxMessages`),maxBytes:normalizeVisibleMessageLimit(e.maxBytes,1024,ENDED_TRANSCRIPT_MAX_BYTES,`maxBytes`)}}function finalizeEndedTranscriptRead(e,t,n){const r=capArrayByJsonBytes(e,n.maxBytes);const i=r.bytes<=n.maxBytes?r.items:[];return{messages:i,totalMessages:t,truncated:i.length<t}}function createClosedSessionTranscriptSource(e){return{available:true,async readTail(t){const n=normalizeEndedTranscriptOptions(t);const r=await readRecentSessionMessagesWithStatsAsync(e,{maxMessages:n.maxMessages,maxLines:n.maxMessages*20+20,maxBytes:n.maxBytes});return finalizeEndedTranscriptRead(r.messages,r.totalMessages,n)}}}function createResetBoundaryTranscriptSource(e,t){return{available:true,async readTail(n){const r=normalizeEndedTranscriptOptions(n);const i=await readSessionMessagesAroundIdWithStatsAsync(e,{closedResetInterval:true,messageId:t,maxMessages:r.maxMessages,maxBytes:r.maxBytes,direction:`older`});if(!i.found)throw new Error(`Ended session reset boundary is no longer available`);return finalizeEndedTranscriptRead(i.messages,i.totalMessages,r)}}}function createArchivedSessionTranscriptSource(e){return{available:true,async readTail(t){const n=normalizeEndedTranscriptOptions(t);const{readSessionHistoryPageInWorker:r}=await import(`./session-history-worker-runtime-DwT_7nJS.mjs`);const i=await r({kind:`recent-page`,params:{target:{sessionId:e.sessionId,...e.agentId?{agentId:e.agentId}:{},...e.storePath?{storePath:e.storePath}:{}},exactArchivePath:e.archivedPath,options:{maxMessages:n.maxMessages,maxLines:n.maxMessages*20+20,maxBytes:n.maxBytes}}});if(!i.transcriptPath)throw new Error(`Ended session archive is no longer available`);return finalizeEndedTranscriptRead(i.messages,i.totalMessages,n)}}} //!src/auto-reply/reply/session-hooks.ts function resolveExplicitSessionEndReason(e){return e===`/reset`?`reset`:`new`}function createReplySessionResetBoundary(e){const t=e.previousReason===`idle`||e.previousReason===`daily`?e.previousReason:`reset`;const n=e.resetTriggered?{context:`clear`,reason:e.explicitReason}:{context:`preserve-tail`,reason:t};return{boundaryId:createSessionResetBoundaryId(),cwd:e.cwd,...n}}function buildSessionHookContext(e){return{sessionId:e.sessionId,sessionKey:e.sessionKey,agentId:e.agentId}}function buildSessionStartHookPayload(e){return{event:{sessionId:e.sessionId,sessionKey:e.sessionKey,resumedFrom:e.resumedFrom},context:buildSessionHookContext(e)}}function emitReplySessionStartHook(e,t){const n=buildSessionStartHookPayload(t);runWithSessionControllerCleanup(()=>runWithGatewayDetachedWorkContinuation(async()=>{await runOutsideStoreWriterContext(()=>e.runSessionStart(n.event,n.context))},`hooks:session-start`)).catch(()=>{})}function buildSessionEndHookPayload(e){const t=buildSessionHookContext(e);attachSessionEndTranscriptSource(t,e.endedTranscript??{available:false,reason:`unsupported-source`});return{event:{sessionId:e.sessionId,sessionKey:e.sessionKey,messageCount:e.messageCount??0,durationMs:e.durationMs,reason:e.reason,sessionFile:e.sessionFile,transcriptArchived:e.transcriptArchived,nextSessionId:e.nextSessionId,nextSessionKey:e.nextSessionKey},context:t}}function emitReplySessionEndHook(e){const t=e.resetBoundaryId?createResetBoundaryTranscriptSource({agentId:e.agentId,sessionId:e.sessionId,sessionKey:e.sessionKey,storePath:e.storePath},e.resetBoundaryId):{available:false,reason:`unsupported-source`};const n=buildSessionEndHookPayload({...e,endedTranscript:t});runWithSessionControllerCleanup(()=>runWithGatewayDetachedWorkContinuation(async()=>{await runOutsideStoreWriterContext(()=>e.hookRunner.runSessionEnd(n.event,n.context))},`hooks:session-end`)).catch(()=>{})} //!src/gateway/active-sessions-shutdown-tracker.ts const trackedSessions=resolveGlobalMap(Symbol.for(`openclaw.activeSessionsForShutdown`),`close-and-restart`);function noteActiveSessionForShutdown(e){if(!e.sessionId)return;trackedSessions.set(e.sessionId,e)}function forgetActiveSessionForShutdown(e){if(!e)return;trackedSessions.delete(e)}function listActiveSessionsForShutdown(){return Array.from(trackedSessions.values())}export{buildSessionStartHookPayload as a,emitReplySessionStartHook as c,createClosedSessionTranscriptSource as d,createResetBoundaryTranscriptSource as f,buildSessionEndHookPayload as i,resolveExplicitSessionEndReason as l,listActiveSessionsForShutdown as n,createReplySessionResetBoundary as o,noteActiveSessionForShutdown as r,emitReplySessionEndHook as s,forgetActiveSessionForShutdown as t,createArchivedSessionTranscriptSource as u};