openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.28 kB
JavaScript
import{c as writeVoiceSessionRecordInTransaction,n as assertVoiceSessionOwnership,o as readVoiceSessionRecordInTransaction}from"./client-voice-session-store-B3THQi07.mjs";
//!src/talk/client-voice-session-write.kernel.ts
function mutateVoiceSessionInDatabase(e,t){if(t.kind===`effect`&&!t.event.runId)return;let n=readVoiceSessionRecordInTransaction(e,t.voiceSessionId);if(!n){if(t.kind===`delivered`||t.kind===`effect`)return;if(t.kind!==`create`)throw new Error(`voice session not found`);n={version:1,voiceSessionId:t.voiceSessionId,agentId:t.agentId,sessionKey:t.sessionKey,...t.provider?{provider:t.provider}:{},origin:t.origin,...t.transcriptCapable===true?{transcriptCapable:true}:{},status:`open`,createdAt:t.now,updatedAt:t.now,consultRunIds:[],effects:[],transcriptFailureKeys:[]}}assertVoiceSessionOwnership(n,t);switch(t.kind){case`consult`:if(n.consultRunIds.includes(t.runId))return n;n.consultRunIds.push(t.runId);break;case`effect`:{const{event:e}=t;const r=e.runId;const i=e.toolCallId?n.effects.find(t=>t.runId===r&&t.toolCallId===e.toolCallId):n.effects.findLast(t=>t.runId===r&&t.toolName===e.toolName&&t.status===`started`);if(e.type!==`tool.execution.started`&&!i)return n;if(e.type!==`tool.execution.started`&&i){i.status=e.type===`tool.execution.completed`?`succeeded`:e.type===`tool.execution.blocked`?`blocked`:e.terminalReason===`cancelled`?`cancelled`:`failed`;i.finishedAt=e.ts}else if(e.mutatingAction===true&&(!e.toolCallId||!i))n.effects.push({runId:r,...e.toolCallId?{toolCallId:e.toolCallId}:{},toolName:e.toolName,startedAt:e.ts,status:`started`});break}case`create`:if(n.origin!==t.origin)throw new Error(`voice session origin does not match`);if(n.status!==`open`)throw new Error(`voice session is already closed`);if(n.provider&&t.provider&&n.provider!==t.provider)throw new Error(`voice session provider does not match`);n.provider??=t.provider;if(t.transcriptCapable===true)n.transcriptCapable=true;break;case`reserve`:if(n.status!==`open`)throw new Error(`voice session is closed`);if(n.origin!==t.origin)throw new Error(`voice session origin does not allow this transcript source`);if(!n.transcriptFailureKeys.includes(t.failureKey)){if(n.transcriptFailureKeys.length>=41)throw new Error(`voice transcript persistence has too many unresolved entries`);n.transcriptFailureKeys.push(t.failureKey)}break;case`confirm`:if(t.role===`user`)n.hasUserTranscript=true;n.transcriptFailureKeys=n.transcriptFailureKeys.filter(e=>e!==t.failureKey);break;case`close`:if(t.expectedOrigin&&n.origin!==t.expectedOrigin)throw new Error(`relay-owned voice sessions close through talk.session.close`);if(t.staleBefore!==void 0&&(n.status!==`open`||n.updatedAt>t.staleBefore))return;if(n.transcriptFailureKeys.length>0&&t.transcriptFailurePolicy===`require-success`)throw new Error(`voice transcript persistence must be retried before close`);if(t.transcriptFailurePolicy===`retain-and-close`&&n.origin!==`relay`)throw new Error(`only relay voice sessions may close with unresolved transcripts`);if(n.status===`closed`)return n;n.status=`closed`;n.closedAt=t.now;break;case`delivered`:if(n.digestDeliveredAt)return n;n.digestDeliveredAt=t.deliveredAt}n.updatedAt=t.now;writeVoiceSessionRecordInTransaction(e,n);return n}export{mutateVoiceSessionInDatabase};