openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 8.66 kB
JavaScript
import{M as e}from"./transcript-chat-message-content-Cx-VPFHv.mjs";import{o as t}from"./transcript-record-coerce-eN4p5mcq.mjs";import{o as n}from"./transcript-string-coerce-BxRUqIXb.mjs";import{o as r}from"./transcript-number-coercion-Dr06QpMa.mjs";import{t as i}from"./transcript-goals-operations.types-B9w9O4_9.mjs";import{n as a}from"./transcript-node-crypto-2Kpo-Xy-.mjs";import o from"node:crypto";import{isDeepStrictEqual as s}from"node:util";function normalizeSessionRuntimeModelFields(e){let t=n(e.model),r=n(e.modelProvider),i=t?r:void 0;if(e.model===t&&e.modelProvider===i)return e;let a={...e};return t?a.model=t:delete a.model,i?a.modelProvider=i:t&&e.modelProvider===void 0||delete a.modelProvider,a}function resolveMergedUpdatedAt(e,t,n){let r=Date.now(),i=normalizeMergedUpdatedAt(e?.updatedAt,r),a=normalizeMergedUpdatedAt(t.updatedAt,r);return n&&e?i??a??r:Math.max(i??0,a??0,r)}function normalizeMergedUpdatedAt(e,t){let n=r(e);return n===void 0?void 0:Math.min(n,t)}function mergeSessionEntryWithPolicy(e,t,r=!1){let i=t.sessionId??e?.sessionId??o.randomUUID(),a=resolveMergedUpdatedAt(e,t,r);if(!e)return stripRetiredSessionEntryLocators(normalizeSessionRuntimeModelFields({...t,sessionId:i,updatedAt:a,sessionStartedAt:t.sessionStartedAt??a}));let s={...e,...t,sessionId:i,updatedAt:a,sessionStartedAt:t.sessionStartedAt??(e.sessionId===i?e.sessionStartedAt:a)};if(e.createdVia!==void 0&&(s.createdVia=e.createdVia),s.createdSurface=e.createdSurface,s.createdIndependently=e.createdIndependently,e.createdActor!==void 0&&(s.createdActor=e.createdActor),s.inheritedGitContributorProfileIds=e.inheritedGitContributorProfileIds,e.sandbox===`required`?s.sandbox=e.sandbox:delete s.sandbox,e.createdAt!==void 0&&(s.createdAt=e.createdAt),e.projectId!==void 0&&(s.projectId=e.projectId),e.repositoryWorkspaceId!==void 0&&(s.repositoryWorkspaceId=e.repositoryWorkspaceId),e.forkSource!==void 0&&(s.forkSource=e.forkSource),Object.hasOwn(t,`model`)&&!Object.hasOwn(t,`modelProvider`)){let r=n(t.model),i=n(e.model);r&&r!==i&&delete s.modelProvider}return stripRetiredSessionEntryLocators(normalizeSessionRuntimeModelFields(s))}function stripRetiredSessionEntryLocators(e){let t=e;return delete t.sessionFile,delete t.transcriptPath,e}function mergeSessionEntry(e,t){return mergeSessionEntryWithPolicy(e,t)}function mergeSessionEntryPreserveActivity(e,t){return mergeSessionEntryWithPolicy(e,t,!0)}function resolveSessionTotalTokens(e){return r(e?.totalTokens)}function resolveFreshSessionTotalTokens(e){let t=resolveSessionTotalTokens(e);if(t!==void 0&&e?.totalTokensFresh===!0&&e.totalTokensVersion===2)return t}var SessionGoalTransitionError=class extends Error{constructor(...e){super(...e),this.name=`SessionGoalTransitionError`}};function normalizeTokenCount(e){return typeof e==`number`&&Number.isFinite(e)&&e>=0?Math.floor(e):void 0}function resolveEntryFreshTotalTokens(e){return normalizeTokenCount(resolveFreshSessionTotalTokens(e))}function normalizeTokenBudget(e){let t=normalizeTokenCount(e);return t&&t>0?t:void 0}function accountSessionGoalUsage(e,t,n){let r=e.goal;if(!r)return;let i=resolveEntryFreshTotalTokens(e),a=r.tokenStartFresh!==!1,o=!a&&n?.adoptFreshBaseline===!1,s=!o&&i!==void 0&&!a,c=s?i:normalizeTokenCount(r.tokenStart)??i??0,l=i===void 0||s||o?r.tokensUsed:Math.max(r.tokensUsed,Math.max(0,i-c)),u={...r,tokenStart:c,tokenStartFresh:a||s,tokensUsed:l};return u.status===`active`&&u.tokenBudget!==void 0&&l>=u.tokenBudget&&(u.status=`budget_limited`,u.budgetLimitedAt=t,u.updatedAt=t),u}function buildCreatedSessionGoal(e,t,n){let r=t.objective;if(!r.trim())throw new SessionGoalTransitionError(`objective required`);if(e.goal&&e.goal.status!==`complete`)throw new SessionGoalTransitionError(`goal already exists`);let i=normalizeTokenBudget(t.tokenBudget),a=resolveEntryFreshTotalTokens(e);return{schemaVersion:1,id:o.randomUUID(),objective:r,status:`active`,createdAt:n,updatedAt:n,tokenStart:a??0,tokenStartFresh:a!==void 0,tokensUsed:0,...i?{tokenBudget:i}:{},continuationTurns:0}}function buildUpdatedSessionGoalStatus(e,t,n){let r=accountSessionGoalUsage(e,n);if(!r)throw new SessionGoalTransitionError(`goal not found`);if(r.status===`complete`&&t.status!==`complete`&&t.status!==`active`)throw new SessionGoalTransitionError(`goal is already ${r.status}`);let i=t.status===`active`&&(r.status===`budget_limited`||r.status===`usage_limited`||r.tokenBudget!==void 0&&r.tokensUsed>=r.tokenBudget),a=i?resolveEntryFreshTotalTokens(e):void 0,o={...r,status:t.status,updatedAt:n,...t.note?{lastStatusNote:t.note}:{},...t.status===`paused`?{pausedAt:n}:{},...t.status===`blocked`?{blockedAt:n}:{},...t.status===`complete`?{completedAt:r.completedAt??n}:{}};return r.status===`complete`&&t.status===`active`&&delete o.completedAt,i&&(o.tokenStart=a??0,o.tokenStartFresh=a!==void 0,o.tokensUsed=0,delete o.budgetLimitedAt,delete o.usageLimitedAt),o.status===`active`&&o.tokenBudget!==void 0&&o.tokensUsed>=o.tokenBudget&&(o.status=`budget_limited`,o.budgetLimitedAt=n),o}function buildUpdatedSessionGoalObjective(e,t,n){if(!t.trim())throw new SessionGoalTransitionError(`objective required`);let r=accountSessionGoalUsage(e,n);if(!r)throw new SessionGoalTransitionError(`goal not found`);if(r.status===`complete`)throw new SessionGoalTransitionError(`goal is already ${r.status}`);return{...r,objective:t,updatedAt:n}}var c=/* @__PURE__ */ e({MAX_SESSION_RECEIPTS:()=>u,OPERATION_VALIDITY_MS:()=>l,applySessionGoalOperation:()=>applySessionGoalOperation,assertSessionGoalOperationTime:()=>assertSessionGoalOperationTime,createSessionGoalOperationResult:()=>createSessionGoalOperationResult,operationFingerprint:()=>operationFingerprint,prepareSessionTurnGoalMessage:()=>prepareSessionTurnGoalMessage});const l=864e5,u=4096;function assertSessionGoalOperationTime(e,t){if(!Number.isSafeInteger(e.issuedAtMs)||e.issuedAtMs>t+3e5)throw new i(`invalid`,`Goal operation time is invalid; refresh and try again.`);if(e.issuedAtMs+864e5<=t)throw new i(`expired`,`Goal operation expired; review the current Goal before trying again.`)}function operationFingerprint(e){return a(JSON.stringify([e.issuedAtMs,e.requestFingerprint,e.action,`goalId`in e?e.goalId:null,`objective`in e?e.objective:null,`tokenBudget`in e?e.tokenBudget:null,`note`in e?e.note:null,...e.action===`resume`&&e.preserveBudget?[`preserve-budget`]:[],...e.action===`replace`?[e.expectedRunId]:[]]))}function applySessionGoalOperation(e,t,n){try{if(t.action===`start`)return buildCreatedSessionGoal(e,t,n);if(!e.goal||e.goal.id!==t.goalId)throw new i(`goal-rebound`,`Goal changed or was cleared; refresh before trying again.`);if(t.action===`replace`){if((e.lifecycleRunId??e.lastRunId)!==t.expectedRunId||e.goal.status===`complete`)throw new i(`goal-rebound`,`Goal or turn changed before interruption was recorded.`);return e.goal}if(t.action===`clear`)return;if(t.action===`edit`)return buildUpdatedSessionGoalObjective(e,t.objective,n);if(t.action===`resume`&&t.replacementSource&&((e.lifecycleRunId??e.lastRunId)!==t.replacementSource.runId||!s(e.goal,t.replacementSource.goal)))throw new i(`goal-rebound`,`The goal or turn changed before replacement resumed; review its current state.`);if(t.action===`resume`&&t.preserveBudget){let t=accountSessionGoalUsage(e,n);if(t?.tokenBudget!==void 0&&t.tokensUsed>=t.tokenBudget)throw new i(`invalid`,`The goal budget was reached. Replacement remains paused; explicitly review its budget before resuming.`)}return buildUpdatedSessionGoalStatus(e,{status:t.action===`resume`?`active`:t.action===`pause`?`paused`:t.action===`block`?`blocked`:`complete`,note:t.note},n)}catch(e){throw e instanceof SessionGoalTransitionError?new i(`invalid`,e.message):e}}function prepareSessionTurnGoalMessage(e,n,r){return!n||!r||!t(e)||e.role!==`user`?e:{...e,__openclaw:{...t(e.__openclaw)?e.__openclaw:{},intent:{kind:n.operation.action===`start`?`session-goal-start`:`session-goal-resume`,version:1,goalId:r,operationId:n.operation.operationId}}}}function createSessionGoalOperationResult(e,t,n,r){let i=n?.id??(`goalId`in t?t.goalId:void 0);if(!i)throw Error(`Goal creation did not produce a Goal identity.`);return{operationId:t.operationId,action:t.action,sessionId:e,goalId:i,status:r?`started`:t.action===`clear`?`cleared`:`updated`,...n?{goal:n}:{},...r?{runId:r}:{}}}export{createSessionGoalOperationResult as a,prepareSessionTurnGoalMessage as c,mergeSessionEntry as d,mergeSessionEntryPreserveActivity as f,assertSessionGoalOperationTime as i,accountSessionGoalUsage as l,l as n,c as o,resolveFreshSessionTotalTokens as p,applySessionGoalOperation as r,operationFingerprint as s,u as t,buildUpdatedSessionGoalStatus as u};