@robota-sdk/agent-session 3.0.7 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ let e=require("@robota-sdk/agent-core");function t(e,t){return e.length===0?t:`${e}.${t}`}function n(e,t){return`${e}[${t}]`}function r(e,t,n){e.push({path:t,message:n})}function i(e){if(e===null)return`null`;if(Array.isArray(e))return`an array`;if(e instanceof Date)return`a Date`;switch(typeof e){case`undefined`:return`nothing`;case`string`:return`a string`;case`number`:return`a number`;case`boolean`:return`a boolean`;case`object`:return`an object`;default:return`a ${typeof e}`}}function a(e,t,n){n!==void 0&&(e[t]=n)}function o(e,t,n,r){if(e!==void 0)return r(e,t,n)}function s(e,t,n){if(typeof e==`string`)return e;r(n,t,`expected a string, received ${i(e)}`)}function c(e,t,n){if(typeof e==`number`&&Number.isFinite(e))return e;r(n,t,`expected a finite number, received ${i(e)}`)}function l(e,t,n){if(typeof e==`number`&&Number.isInteger(e))return e;r(n,t,`expected an integer, received ${i(e)}`)}function u(e,t,n){if(typeof e==`boolean`)return e;r(n,t,`expected a boolean, received ${i(e)}`)}function d(e,t,n){if(e===!0)return!0;r(n,t,`expected true, received ${i(e)}`)}function f(e,t,n,a){if(typeof e==`string`&&t.includes(e))return e;r(a,n,`expected one of ${t.join(` | `)}, received ${i(e)}`)}function p(e,t,n){if(typeof e!=`string`){r(n,t,`expected an ISO-8601 timestamp string, received ${i(e)}`);return}if(Number.isNaN(Date.parse(e))){r(n,t,`expected a timestamp a date can be parsed from`);return}return e}function m(e,t,n){if(e instanceof Date){if(Number.isNaN(e.getTime())){r(n,t,`expected a valid Date, received an invalid one`);return}return e}if(typeof e==`string`&&!Number.isNaN(Date.parse(e)))return new Date(e);r(n,t,`expected a Date or an ISO-8601 timestamp string, received ${i(e)}`)}function h(e,t,a,o){if(!Array.isArray(e)){r(a,t,`expected an array, received ${i(e)}`);return}let s=[];return e.forEach((e,r)=>{let i=o(e,n(t,r),a);i!==void 0&&s.push(i)}),s}function g(e,t,n){return h(e,t,n,s)}function _(e,n,a,o){if(typeof e!=`object`||!e||Array.isArray(e)){r(a,n,`expected an object, received ${i(e)}`);return}let s=e,c=new Set(o);for(let e of Object.keys(s))c.has(e)||r(a,t(n,e),`unknown key; the record contract does not declare it`);return s}function v(e,n,a,o){if(typeof e!=`object`||!e||Array.isArray(e)){r(a,n,`expected an object, received ${i(e)}`);return}let s={};for(let[r,i]of Object.entries(e)){let e=o(i,t(n,r),a);e!==void 0&&Object.defineProperty(s,r,{value:e,enumerable:!0,writable:!0,configurable:!0})}return s}function y(e,t,a){if(e===null||e instanceof Date||typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;if(Array.isArray(e)){let r=[];return e.forEach((e,i)=>{let o=y(e,n(t,i),a);o!==void 0&&r.push(o)}),r}if(typeof e==`object`)return v(e,t,a,y);r(a,t,`expected a JSON-compatible value, received ${i(e)}`)}function b(e,t,n){if(typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;r(n,t,`expected a string, number or boolean, received ${i(e)}`)}function x(e,t,n){let i=s(e,t,n);if(i!==void 0&&i.trim().length>0)return i;i!==void 0&&r(n,t,`expected a nonempty identity`)}function S(e,t=new Set){if(e===null||typeof e==`string`||typeof e==`boolean`)return!0;if(typeof e==`number`)return Number.isFinite(e);if(typeof e!=`object`||e instanceof Date||t.has(e)||!Array.isArray(e)&&Object.getPrototypeOf(e)!==Object.prototype&&Object.getPrototypeOf(e)!==null)return!1;t.add(e);let n=(Array.isArray(e)?e:Object.values(e)).every(e=>S(e,t));return t.delete(e),n}function C(e,n,i){let a=_(e,n,i,[`executionId`,`actionId`,`parentCallId`,`toolCallId`,`toolName`,`requestId`,`kind`,`data`]);if(!a)return;let o=x(a.executionId,t(n,`executionId`),i),s=x(a.actionId,t(n,`actionId`),i),c=x(a.parentCallId,t(n,`parentCallId`),i),l=x(a.toolCallId,t(n,`toolCallId`),i),u=x(a.toolName,t(n,`toolName`),i),d=x(a.requestId,t(n,`requestId`),i),f=x(a.kind,t(n,`kind`),i);if(!S(a.data)){r(i,t(n,`data`),`expected finite JSON`);return}let p=v(a.data,t(n,`data`),i,y);if(o!==void 0&&s!==void 0&&c!==void 0&&l!==void 0&&u!==void 0&&d!==void 0&&f!==void 0&&p!==void 0)return{executionId:o,actionId:s,parentCallId:c,toolCallId:l,toolName:u,requestId:d,kind:f,data:p}}function w(e,n,r){let i=_(e,n,r,[`executionId`,`requests`]);if(!i)return;let a=x(i.executionId,t(n,`executionId`),r),o=h(i.requests,t(n,`requests`),r,C);if(a!==void 0&&o!==void 0)return{executionId:a,requests:o}}function T(e,t,n,i=`pendingExecution`){let a=t.findLastIndex(e=>e.role===`assistant`),o=t[a];if(o?.role!==`assistant`||o.metadata?.executionId!==e.executionId||!t.slice(a+1).every(e=>e.role===`tool`)){r(n,i,`pending execution must match the terminal assistant round`);return}let s=new Map((o.toolCalls??[]).map(e=>[e.id,e]));s.size!==o.toolCalls?.length&&r(n,i,`tool call identities must be unique within a round`);let c=t.slice(a+1).flatMap(e=>e.role===`tool`?[e]:[]),l=new Set;for(let e of c){let t=s.get(e.toolCallId);(!t||l.has(e.toolCallId)||e.name!==void 0&&e.name!==t.function.name)&&r(n,i,`terminal receipts must uniquely bind this round`),l.add(e.toolCallId)}let u=new Set(t.slice(a+1).flatMap(e=>e.role===`tool`?[e.toolCallId]:[])),d=new Map((o.toolCalls??[]).filter(e=>!u.has(e.id)).map(e=>[e.id,e]));d.size===0&&r(n,i,`pending execution requires unanswered calls`);let f=new Set,p=new Map,m=new Map;for(let t of e.requests){let a=d.get(t.toolCallId),s=JSON.stringify([t.parentCallId,t.toolCallId,t.toolName]);(f.has(t.requestId)||t.executionId!==e.executionId||t.parentCallId!==o.metadata?.providerCallId||!a||a.function.name!==t.toolName||p.has(t.actionId)&&p.get(t.actionId)!==s||m.has(t.toolCallId)&&m.get(t.toolCallId)!==t.actionId)&&r(n,i,`wait identities must uniquely bind an open tool action`),f.add(t.requestId),p.set(t.actionId,s),m.set(t.toolCallId,t.actionId)}}const E=[`complete`,`interrupted`],D=[`id`,`timestamp`,`state`,`metadata`,`role`,`content`,`parts`],O={user:[...D,`name`],assistant:[...D,`toolCalls`],system:[...D,`name`],tool:[...D,`toolCallId`,`name`]};function k(e,t,n){if(e instanceof Date||typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;if(Array.isArray(e)){if(e.every(e=>typeof e==`string`)||e.every(e=>typeof e==`number`))return e;r(n,t,`expected an array of only strings or only numbers`);return}if(typeof e==`object`&&e)return v(e,t,n,(e,t,n)=>{if(typeof e==`number`)return e;r(n,t,`expected a number, received ${i(e)}`)});r(n,t,`expected a metadata value, received ${i(e)}`)}function A(e,t,n){return v(e,t,n,k)}function j(e,n,i){let l=f(e?.type,[`text`,`image_inline`,`image_uri`,`audio_inline`,`resource_link`,`resource_embedded`],t(n,`type`),i);if(l===void 0)return;if(l===`text`){let r=_(e,n,i,[`type`,`text`]);if(r===void 0)return;let a=s(r.text,t(n,`text`),i);return a===void 0?void 0:{type:`text`,text:a}}if(l===`image_inline`||l===`audio_inline`){let r=_(e,n,i,[`type`,`mimeType`,`data`]);if(r===void 0)return;let a=s(r.mimeType,t(n,`mimeType`),i),o=s(r.data,t(n,`data`),i);return a===void 0||o===void 0?void 0:{type:l,mimeType:a,data:o}}if(l===`resource_link`){let u=_(e,n,i,[`type`,`uri`,`name`,`mimeType`,`title`,`description`,`size`]);if(u===void 0)return;let d=s(u.uri,t(n,`uri`),i),f=s(u.name,t(n,`name`),i);if(d===void 0||f===void 0)return;let p={type:l,uri:d,name:f};for(let e of[`mimeType`,`title`,`description`])a(p,e,o(u[e],t(n,e),i,s));let m=o(u.size,t(n,`size`),i,c);return m!==void 0&&(!Number.isSafeInteger(m)||m<0)&&r(i,t(n,`size`),`expected a nonnegative safe integer`),a(p,`size`,m),p}if(l===`resource_embedded`){let a=_(e,n,i,[`type`,`uri`,`mimeType`,`text`,`blob`]);if(a===void 0)return;let c=s(a.uri,t(n,`uri`),i),u=o(a.mimeType,t(n,`mimeType`),i,s);if(c===void 0)return;let d={type:l,uri:c,...u===void 0?{}:{mimeType:u}};if(a.text!==void 0&&a.blob===void 0){let e=s(a.text,t(n,`text`),i);return e===void 0?void 0:{...d,text:e}}if(a.blob!==void 0&&a.text===void 0){let e=s(a.blob,t(n,`blob`),i);return e===void 0?void 0:{...d,blob:e}}r(i,n,`expected exactly one text or blob payload`);return}let u=_(e,n,i,[`type`,`uri`,`mimeType`]);if(u===void 0)return;let d=s(u.uri,t(n,`uri`),i);if(d===void 0)return;let p={type:`image_uri`,uri:d};return a(p,`mimeType`,o(u.mimeType,t(n,`mimeType`),i,s)),p}function M(e,n,r){let i=_(e,n,r,[`id`,`type`,`function`]);if(i===void 0)return;let a=s(i.id,t(n,`id`),r),o=f(i.type,[`function`],t(n,`type`),r),c=t(n,`function`),l=_(i.function,c,r,[`name`,`arguments`]),u=l===void 0?void 0:s(l.name,t(c,`name`),r),d=l===void 0?void 0:s(l.arguments,t(c,`arguments`),r);if(a!==void 0&&o!==void 0&&u!==void 0&&d!==void 0)return{id:a,type:o,function:{name:u,arguments:d}}}function N(e,n,r){let i=s(e.id,t(n,`id`),r),a=m(e.timestamp,t(n,`timestamp`),r),o=f(e.state,E,t(n,`state`),r);if(i!==void 0&&a!==void 0&&o!==void 0)return{id:i,timestamp:a,state:o}}function P(e,n,r,i){a(e,`metadata`,o(n.metadata,t(r,`metadata`),i,A)),a(e,`parts`,o(n.parts,t(r,`parts`),i,(e,t,n)=>h(e,t,n,j)))}function F(e,n,r){let i=f(e?.role,[`user`,`assistant`,`system`,`tool`],t(n,`role`),r);if(i===void 0)return;let c=_(e,n,r,O[i]);if(c===void 0)return;let l=N(c,n,r);if(l===void 0)return;let u=t(n,`content`);if(i===`assistant`){let e=c.content===null?null:s(c.content,u,r);if(e===void 0)return;let d={...l,role:i,content:e};return P(d,c,n,r),a(d,`toolCalls`,o(c.toolCalls,t(n,`toolCalls`),r,(e,t,n)=>h(e,t,n,M))),d}let d=s(c.content,u,r);if(d===void 0)return;if(i===`tool`){let e=s(c.toolCallId,t(n,`toolCallId`),r);if(e===void 0)return;let u={...l,role:i,content:d,toolCallId:e};return P(u,c,n,r),a(u,`name`,o(c.name,t(n,`name`),r,s)),u}let p={...l,role:i,content:d};return P(p,c,n,r),a(p,`name`,o(c.name,t(n,`name`),r,s)),p}function ee(e,n,r){let i=_(e,n,r,[`id`,`timestamp`,`category`,`type`,`data`]);if(i===void 0)return;let c=s(i.id,t(n,`id`),r),l=m(i.timestamp,t(n,`timestamp`),r),u=s(i.category,t(n,`category`),r),d=s(i.type,t(n,`type`),r);if(c===void 0||l===void 0||u===void 0||d===void 0)return;let f={id:c,timestamp:l,category:u,type:d};return a(f,`data`,o(i.data,t(n,`data`),r,y)),f}const te=[`agent`,`process`,`scheduled`,`tool-invocation`],ne=[`foreground`,`background`],re=[`none`,`worktree`],ie=[`queued`,`running`,`waiting_permission`,`sleeping`,`paused`,`completed`,`failed`,`cancelled`],ae=[`idle`,`max_runtime`,`output_limit`,`repetition`,`stale_worker`],oe=[`validation`,`capacity`,`permission`,`timeout`,`runner`,`crash`,`provider`,`process`],se=[`denied-by-person`,`no-approver`,`approver-error`,`cancelled`];function I(e,t,n){return v(e,t,n,b)}function ce(e,t,n){return v(e,t,n,s)}function le(e,n,r){let i=_(e,n,r,[`category`,`message`,`recoverable`]);if(i===void 0)return;let a=f(i.category,oe,t(n,`category`),r),o=s(i.message,t(n,`message`),r),c=u(i.recoverable,t(n,`recoverable`),r);if(a!==void 0&&o!==void 0&&c!==void 0)return{category:a,message:o,recoverable:c}}function ue(e,n,r){let i=_(e,n,r,[`promptTokens`,`completionTokens`,`totalTokens`]);if(i===void 0)return;let a=l(i.promptTokens,t(n,`promptTokens`),r),o=l(i.completionTokens,t(n,`completionTokens`),r),s=l(i.totalTokens,t(n,`totalTokens`),r);if(a!==void 0&&o!==void 0&&s!==void 0)return{promptTokens:a,completionTokens:o,totalTokens:s}}function de(e,n,r){let i=_(e,n,r,[`total`,`byReason`]);if(i===void 0)return;let a=l(i.total,t(n,`total`),r),o=t(n,`byReason`),s=_(i.byReason,o,r,se);if(a===void 0||s===void 0)return;let c={},u=!0;for(let e of se){let n=l(s[e],t(o,e),r);if(n===void 0){u=!1;continue}c[e]=n}if(u)return{total:a,byReason:c}}function fe(e,n,i){let c=_(e,n,i,[`taskId`,`kind`,`output`,`exitCode`,`signalCode`,`metadata`,`usage`,`deniedToolCalls`]);if(c===void 0)return;let u=s(c.taskId,t(n,`taskId`),i),d=f(c.kind,te,t(n,`kind`),i),p=s(c.output,t(n,`output`),i);if(u===void 0||d===void 0||p===void 0)return;let m=o(c.exitCode,t(n,`exitCode`),i,l),h=o(c.signalCode,t(n,`signalCode`),i,s),g=o(c.metadata,t(n,`metadata`),i,I),v=o(c.usage,t(n,`usage`),i,ue),y=o(c.deniedToolCalls,t(n,`deniedToolCalls`),i,de);switch(d!==`process`&&m!==void 0&&r(i,t(n,`exitCode`),`must not be set for a '${d}' result`),d!==`process`&&h!==void 0&&r(i,t(n,`signalCode`),`must not be set for a '${d}' result`),d!==`agent`&&v!==void 0&&r(i,t(n,`usage`),`must not be set for a '${d}' result`),d!==`agent`&&y!==void 0&&r(i,t(n,`deniedToolCalls`),`must not be set for a '${d}' result`),d){case`process`:{let e={taskId:u,kind:d,output:p};return a(e,`exitCode`,m),a(e,`signalCode`,h),a(e,`metadata`,g),e}case`agent`:{let e={taskId:u,kind:d,output:p};return a(e,`metadata`,g),a(e,`usage`,v),a(e,`deniedToolCalls`,y),e}case`scheduled`:{let e={taskId:u,kind:d,output:p};return a(e,`metadata`,g),e}case`tool-invocation`:{let e={taskId:u,kind:d,output:p};return a(e,`metadata`,g),e}}}function pe(e,n,r){let i=_(e,n,r,[`cronExpression`,`agentInstruction`,`command`,`shell`,`env`]);if(i===void 0)return;let c=s(i.cronExpression,t(n,`cronExpression`),r);if(c===void 0)return;let l={cronExpression:c};for(let e of[`agentInstruction`,`command`,`shell`])a(l,e,o(i[e],t(n,e),r,s));return a(l,`env`,o(i.env,t(n,`env`),r,ce)),l}const me=[`detached`,`wait_all`,`wait_any`,`manual`],he=[`running`,`completed`],ge=[`background_job_group_created`,`background_job_group_updated`,`background_job_group_completed`];function _e(e,n,r){let i=_(e,n,r,[`taskId`,`label`,`status`,`summary`,`outputRef`,`transcriptSealed`,`error`,`startedAt`,`completedAt`]);if(i===void 0)return;let c=s(i.taskId,t(n,`taskId`),r),l=s(i.label,t(n,`label`),r),u=f(i.status,ie,t(n,`status`),r);if(c===void 0||l===void 0||u===void 0)return;let m={taskId:c,label:l,status:u};for(let e of[`summary`,`outputRef`])a(m,e,o(i[e],t(n,e),r,s));for(let e of[`startedAt`,`completedAt`])a(m,e,o(i[e],t(n,e),r,p));return a(m,`transcriptSealed`,o(i.transcriptSealed,t(n,`transcriptSealed`),r,d)),a(m,`error`,o(i.error,t(n,`error`),r,le)),m}function ve(e,n,r){let i=_(e,n,r,[`id`,`parentSessionId`,`waitPolicy`,`taskIds`,`status`,`createdAt`,`updatedAt`,`label`,`completedAt`,`results`]);if(i===void 0)return;let c=s(i.id,t(n,`id`),r),l=s(i.parentSessionId,t(n,`parentSessionId`),r),u=f(i.waitPolicy,me,t(n,`waitPolicy`),r),d=g(i.taskIds,t(n,`taskIds`),r),m=f(i.status,he,t(n,`status`),r),v=p(i.createdAt,t(n,`createdAt`),r),y=p(i.updatedAt,t(n,`updatedAt`),r),b=h(i.results,t(n,`results`),r,_e);if(c===void 0||l===void 0||u===void 0||d===void 0||m===void 0||v===void 0||y===void 0||b===void 0)return;let x={id:c,parentSessionId:l,waitPolicy:u,taskIds:d,status:m,createdAt:v,updatedAt:y,results:b};return a(x,`label`,o(i.label,t(n,`label`),r,s)),a(x,`completedAt`,o(i.completedAt,t(n,`completedAt`),r,p)),x}function L(e,n,r){let i=f(e?.type,ge,t(n,`type`),r);if(i===void 0)return;let a=_(e,n,r,[`type`,`group`]);if(a===void 0)return;let o=ve(a.group,t(n,`group`),r);return o===void 0?void 0:{type:i,group:o}}const ye=[`parentTaskId`,`currentAction`,`logPath`,`transcriptPath`],be=[`agentType`,`resumeSessionId`,`promptPreview`,`worktreePath`,`branchName`,`worktreeStatus`,`worktreeNextAction`,`worktreeBaseRevision`,`parentWorktreeStatus`],xe=[`commandPreview`],Se=[`startedAt`,`lastActivityAt`,`completedAt`],Ce=[`id`,`kind`,`label`,`status`,`mode`,`parentSessionId`,`depth`,`cwd`,`pid`,`updatedAt`,`isolation`,`unread`,`result`,`error`,`timeoutReason`,`transcriptSealed`,`schedule`,`nextFireAt`,`metadata`,...ye,...be,...xe,...Se];function we(e,n,i){let c=_(e,n,i,Ce);if(c===void 0)return;let m=s(c.id,t(n,`id`),i),h=f(c.kind,te,t(n,`kind`),i),g=s(c.label,t(n,`label`),i),v=f(c.status,ie,t(n,`status`),i),y=f(c.mode,ne,t(n,`mode`),i),b=s(c.parentSessionId,t(n,`parentSessionId`),i),x=l(c.depth,t(n,`depth`),i),S=s(c.cwd,t(n,`cwd`),i),C=p(c.updatedAt,t(n,`updatedAt`),i),w=u(c.unread,t(n,`unread`),i);if(m===void 0||h===void 0||g===void 0||v===void 0||y===void 0||b===void 0||x===void 0||S===void 0||C===void 0||w===void 0)return;let T={id:m,label:g,status:v,mode:y,parentSessionId:b,depth:x,cwd:S,updatedAt:C,unread:w};for(let e of ye)a(T,e,o(c[e],t(n,e),i,s));for(let e of Se)a(T,e,o(c[e],t(n,e),i,p));a(T,`pid`,o(c.pid,t(n,`pid`),i,l)),a(T,`transcriptSealed`,o(c.transcriptSealed,t(n,`transcriptSealed`),i,d)),a(T,`timeoutReason`,o(c.timeoutReason,t(n,`timeoutReason`),i,(e,t,n)=>f(e,ae,t,n))),a(T,`metadata`,o(c.metadata,t(n,`metadata`),i,I)),a(T,`error`,o(c.error,t(n,`error`),i,le));let E=o(c.result,t(n,`result`),i,fe);E!==void 0&&(E.taskId!==m&&r(i,t(t(n,`result`),`taskId`),`must match the task ID`),E.kind!==h&&r(i,t(t(n,`result`),`kind`),`must match the task kind`));let D=o(c.agentType,t(n,`agentType`),i,s),O=o(c.resumeSessionId,t(n,`resumeSessionId`),i,s),k=o(c.promptPreview,t(n,`promptPreview`),i,s),A=o(c.isolation,t(n,`isolation`),i,(e,t,n)=>f(e,re,t,n)),j={};for(let e of[`worktreePath`,`branchName`,`worktreeStatus`,`worktreeNextAction`,`worktreeBaseRevision`,`parentWorktreeStatus`])j[e]=o(c[e],t(n,e),i,s);for(let e of be){let a=e===`agentType`?D:e===`resumeSessionId`?O:e===`promptPreview`?k:j[e];h!==`agent`&&a!==void 0&&r(i,t(n,e),`must not be set for a '${h}' task`)}h!==`agent`&&A!==void 0&&r(i,t(n,`isolation`),`must not be set for a '${h}' task`);let M=o(c.commandPreview,t(n,`commandPreview`),i,s);h===`agent`&&M!==void 0&&r(i,t(n,`commandPreview`),`must not be set for a '${h}' task`);let N=o(c.schedule,t(n,`schedule`),i,pe),P=o(c.nextFireAt,t(n,`nextFireAt`),i,p);switch(h!==`scheduled`&&N!==void 0&&r(i,t(n,`schedule`),`must not be set for a '${h}' task`),h!==`scheduled`&&P!==void 0&&r(i,t(n,`nextFireAt`),`must not be set for a '${h}' task`),h){case`agent`:{let e=E?.kind===`agent`?E:void 0,t={...T,kind:h};return a(t,`result`,e),a(t,`agentType`,D),a(t,`resumeSessionId`,O),a(t,`promptPreview`,k),a(t,`isolation`,A),a(t,`worktreePath`,j.worktreePath),a(t,`branchName`,j.branchName),a(t,`worktreeStatus`,j.worktreeStatus),a(t,`worktreeNextAction`,j.worktreeNextAction),a(t,`worktreeBaseRevision`,j.worktreeBaseRevision),a(t,`parentWorktreeStatus`,j.parentWorktreeStatus),t}case`process`:{let e=E?.kind===`process`?E:void 0,t={...T,kind:h};return a(t,`result`,e),a(t,`commandPreview`,M),t}case`tool-invocation`:{let e=E?.kind===`tool-invocation`?E:void 0,t={...T,kind:h};return a(t,`result`,e),a(t,`commandPreview`,M),t}case`scheduled`:{let e=E?.kind===`scheduled`?E:void 0,t={...T,kind:h};return a(t,`result`,e),a(t,`commandPreview`,M),a(t,`schedule`,N),a(t,`nextFireAt`,P),t}}}const Te=[`background_task_created`,`background_task_started`,`background_task_updated`,`background_task_completed`,`background_task_failed`,`background_task_cancelled`,`background_task_text_delta`,`background_task_tool_start`,`background_task_tool_end`,`background_task_permission_request`,`background_task_closed`,`background_task_waking`];function R(e,n,r){return s(e.taskId,t(n,`taskId`),r)}function Ee(e,n,r){let i=_(e,n,r,[`type`,`taskId`,`delta`]);if(i===void 0)return;let a=R(i,n,r),o=s(i.delta,t(n,`delta`),r);if(a!==void 0&&o!==void 0)return{type:`background_task_text_delta`,taskId:a,delta:o}}function De(e,n,r){let i=_(e,n,r,[`type`,`taskId`,`toolName`,`firstArg`]);if(i===void 0)return;let c=R(i,n,r),l=s(i.toolName,t(n,`toolName`),r);if(c===void 0||l===void 0)return;let u={type:`background_task_tool_start`,taskId:c,toolName:l};return a(u,`firstArg`,o(i.firstArg,t(n,`firstArg`),r,s)),u}function Oe(e,n,r){let i=_(e,n,r,[`type`,`taskId`,`toolName`,`success`,`error`]);if(i===void 0)return;let c=R(i,n,r),l=s(i.toolName,t(n,`toolName`),r),d=u(i.success,t(n,`success`),r);if(c===void 0||l===void 0||d===void 0)return;let f={type:`background_task_tool_end`,taskId:c,toolName:l,success:d};return a(f,`error`,o(i.error,t(n,`error`),r,s)),f}function ke(e,n,r){let i=_(e,n,r,[`type`,`taskId`,`requestId`,`toolName`,`toolArgs`]);if(i===void 0)return;let a=R(i,n,r),o=s(i.requestId,t(n,`requestId`),r),c=s(i.toolName,t(n,`toolName`),r),l=I(i.toolArgs,t(n,`toolArgs`),r);if(a!==void 0&&o!==void 0&&c!==void 0&&l!==void 0)return{type:`background_task_permission_request`,taskId:a,requestId:o,toolName:c,toolArgs:l}}function Ae(e,t,n){let r=_(e,t,n,[`type`,`taskId`]);if(r===void 0)return;let i=R(r,t,n);return i===void 0?void 0:{type:`background_task_closed`,taskId:i}}function je(e,n,r){let i=_(e,n,r,[`type`,`taskId`,`instruction`]);if(i===void 0)return;let c=R(i,n,r);if(c===void 0)return;let l={type:`background_task_waking`,taskId:c};return a(l,`instruction`,o(i.instruction,t(n,`instruction`),r,s)),l}function Me(e,n,r,i){let a=_(n,r,i,[`type`,`task`]);if(a===void 0)return;let o=we(a.task,t(r,`task`),i);if(o!==void 0)return{type:e,task:o}}const Ne={background_task_text_delta:Ee,background_task_tool_start:De,background_task_tool_end:Oe,background_task_permission_request:ke,background_task_closed:Ae,background_task_waking:je};function z(e,n,r){let i=f(e?.type,Te,t(n,`type`),r);if(i===void 0)return;let a=Ne[i];return a===void 0?Me(i,e,n,r):a(e,n,r)}const Pe=[`skill`,`plugin`],Fe=[`user-slash`,`model-tool`],Ie=[`inject`,`fork`],Le=[`started`,`completed`,`failed`],Re=[`memory_candidate_extracted`,`memory_candidate_queued`,`memory_candidate_saved`,`memory_candidate_skipped`,`memory_candidate_approved`,`memory_candidate_rejected`,`memory_retrieved`],ze=[`manual`,`prompt-reference`,`system`],Be=[`active`,`observed`];function Ve(e,n,r){let i=_(e,n,r,[`type`,`skillName`,`source`,`invocation`,`mode`,`status`,`timestamp`,`qualifiedName`,`error`]);if(i===void 0)return;let a=f(i.type,[`skill-activation`],t(n,`type`),r),c=s(i.skillName,t(n,`skillName`),r),l=f(i.source,Pe,t(n,`source`),r),u=f(i.invocation,Fe,t(n,`invocation`),r),d=f(i.mode,Ie,t(n,`mode`),r),m=f(i.status,Le,t(n,`status`),r),h=p(i.timestamp,t(n,`timestamp`),r);if(a===void 0||c===void 0||l===void 0||u===void 0||d===void 0||m===void 0||h===void 0)return;let g=o(i.qualifiedName,t(n,`qualifiedName`),r,s),v=o(i.error,t(n,`error`),r,s);return{type:a,skillName:c,source:l,invocation:u,mode:d,status:m,timestamp:h,...g===void 0?{}:{qualifiedName:g},...v===void 0?{}:{error:v}}}function He(e,n,r){let i=_(e,n,r,[`topic`,`path`,`score`,`truncated`]);if(i===void 0)return;let a=s(i.topic,t(n,`topic`),r),o=s(i.path,t(n,`path`),r),l=c(i.score,t(n,`score`),r),d=u(i.truncated,t(n,`truncated`),r);if(a!==void 0&&o!==void 0&&l!==void 0&&d!==void 0)return{topic:a,path:o,score:l,truncated:d}}function B(e,n,r){let i=_(e,n,r,[`type`,`at`,`candidateId`,`topic`,`reason`,`data`]);if(i===void 0)return;let c=f(i.type,Re,t(n,`type`),r),l=p(i.at,t(n,`at`),r);if(c===void 0||l===void 0)return;let u={type:c,at:l};for(let e of[`candidateId`,`topic`,`reason`])a(u,e,o(i[e],t(n,e),r,s));return a(u,`data`,o(i.data,t(n,`data`),r,(e,t,n)=>v(e,t,n,y))),u}function Ue(e,n,r){let i=_(e,n,r,[`id`,`sourcePath`,`relativePath`,`originalReference`,`loadType`,`status`,`byteLength`,`loadedAt`,`lastUsedAt`]);if(i===void 0)return;let c=s(i.id,t(n,`id`),r),u=s(i.sourcePath,t(n,`sourcePath`),r),d=s(i.relativePath,t(n,`relativePath`),r),m=s(i.originalReference,t(n,`originalReference`),r),h=f(i.loadType,ze,t(n,`loadType`),r),g=f(i.status,Be,t(n,`status`),r),v=l(i.byteLength,t(n,`byteLength`),r),y=p(i.loadedAt,t(n,`loadedAt`),r);if(c===void 0||u===void 0||d===void 0||m===void 0||h===void 0||g===void 0||v===void 0||y===void 0)return;let b={id:c,sourcePath:u,relativePath:d,originalReference:m,loadType:h,status:g,byteLength:v,loadedAt:y};return a(b,`lastUsedAt`,o(i.lastUsedAt,t(n,`lastUsedAt`),r,p)),b}const We=[`active`,`satisfied`,`stopped`],Ge=[`satisfied`,`max-iterations`,`cancelled`,`no-progress`],Ke=[`continue`,`satisfied`],qe=[`pending`,`in-progress`,`done`],Je=[`planning`,`awaiting-approval`,`executing`,`completed`];function Ye(e,n,r){let i=_(e,n,r,[`iteration`,`signal`,`reason`]);if(i===void 0)return;let a=l(i.iteration,t(n,`iteration`),r),o=f(i.signal,Ke,t(n,`signal`),r),c=s(i.reason,t(n,`reason`),r);if(a!==void 0&&o!==void 0&&c!==void 0)return{iteration:a,signal:o,reason:c}}function Xe(e,n,r){let i=_(e,n,r,[`id`,`objective`,`status`,`stopReason`,`iterations`,`maxIterations`,`startedAt`,`progress`]);if(i===void 0)return;let c=s(i.id,t(n,`id`),r),u=s(i.objective,t(n,`objective`),r),d=f(i.status,We,t(n,`status`),r),m=l(i.iterations,t(n,`iterations`),r),g=l(i.maxIterations,t(n,`maxIterations`),r),v=p(i.startedAt,t(n,`startedAt`),r),y=h(i.progress,t(n,`progress`),r,Ye);if(c===void 0||u===void 0||d===void 0||m===void 0||g===void 0||v===void 0||y===void 0)return;let b={id:c,objective:u,status:d,iterations:m,maxIterations:g,startedAt:v,progress:y};return a(b,`stopReason`,o(i.stopReason,t(n,`stopReason`),r,(e,t,n)=>f(e,Ge,t,n))),b}function Ze(e,n,r){let i=_(e,n,r,[`id`,`description`,`status`]);if(i===void 0)return;let a=s(i.id,t(n,`id`),r),o=s(i.description,t(n,`description`),r),c=f(i.status,qe,t(n,`status`),r);if(a!==void 0&&o!==void 0&&c!==void 0)return{id:a,description:o,status:c}}function Qe(e,n,r){let i=_(e,n,r,[`id`,`objective`,`steps`,`phase`,`createdAt`,`approvedAt`]);if(i===void 0)return;let c=s(i.id,t(n,`id`),r),l=s(i.objective,t(n,`objective`),r),u=h(i.steps,t(n,`steps`),r,Ze),d=f(i.phase,Je,t(n,`phase`),r),m=p(i.createdAt,t(n,`createdAt`),r);if(c===void 0||l===void 0||u===void 0||d===void 0||m===void 0)return;let g={id:c,objective:l,steps:u,phase:d,createdAt:m};return a(g,`approvedAt`,o(i.approvedAt,t(n,`approvedAt`),r,p)),g}function $e(e,n,r){let i=_(e,n,r,[`branchId`,`checkpointId`]);if(i===void 0)return;let a=s(i.branchId,t(n,`branchId`),r),o=i.checkpointId===null?null:s(i.checkpointId,t(n,`checkpointId`),r);if(a!==void 0&&o!==void 0)return{branchId:a,checkpointId:o}}const et=[`waiting`,`pending`,`running`,`stopped`,`expired`],tt=[`loopId`,`instruction`,`useDefaultPrompt`,`createdAt`,`expiresAt`,`revision`,`generation`,`phase`,`nextAllowedAt`,`delaySeconds`,`reason`,`fallbackUsed`,`terminalReason`];function nt(e,n,i){let c=_(e,n,i,tt);if(!c)return;let d=s(c.loopId,t(n,`loopId`),i),m=s(c.instruction,t(n,`instruction`),i),h=o(c.useDefaultPrompt,t(n,`useDefaultPrompt`),i,u),g=p(c.createdAt,t(n,`createdAt`),i),v=p(c.expiresAt,t(n,`expiresAt`),i),y=l(c.revision,t(n,`revision`),i),b=l(c.generation,t(n,`generation`),i),x=f(c.phase,et,t(n,`phase`),i),S=u(c.fallbackUsed,t(n,`fallbackUsed`),i),C=o(c.nextAllowedAt,t(n,`nextAllowedAt`),i,p),w=o(c.delaySeconds,t(n,`delaySeconds`),i,l),T=o(c.reason,t(n,`reason`),i,s),E=o(c.terminalReason,t(n,`terminalReason`),i,s);if(y!==void 0&&(!Number.isSafeInteger(y)||y<0)&&r(i,t(n,`revision`),`expected a non-negative safe integer`),b!==void 0&&(!Number.isSafeInteger(b)||b<0)&&r(i,t(n,`generation`),`expected a non-negative safe integer`),w!==void 0&&(w<60||w>3600)&&r(i,t(n,`delaySeconds`),`expected a delay between 60 and 3600 seconds`),d!==void 0&&!d.trim()&&r(i,t(n,`loopId`),`expected a non-empty loop ID`),m!==void 0&&!m.trim()&&r(i,t(n,`instruction`),`expected a non-empty instruction`),x===`waiting`&&C===void 0&&c.nextAllowedAt===void 0&&r(i,t(n,`nextAllowedAt`),`waiting loop needs a next wake time`),d===void 0||m===void 0||g===void 0||v===void 0||y===void 0||b===void 0||x===void 0||S===void 0)return;let D={loopId:d,instruction:m,createdAt:g,expiresAt:v,revision:y,generation:b,phase:x,fallbackUsed:S};return a(D,`useDefaultPrompt`,h),a(D,`nextAllowedAt`,C),a(D,`delaySeconds`,w),a(D,`reason`,T),a(D,`terminalReason`,E),D}const rt=[`string`,`number`,`integer`,`boolean`,`array`,`object`,`null`],it=[`minLength`,`maxLength`,`minItems`,`maxItems`,`minProperties`,`maxProperties`],at=[`type`,`description`,`enum`,`items`,`properties`,`required`,`anyOf`,`additionalProperties`,`minimum`,`maximum`,...it,`pattern`,`format`,`default`];function ot(e,t,n){if(typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;r(n,t,`expected a string, number or boolean, received ${i(e)}`)}function st(e,t,n){return e===null?null:ot(e,t,n)}function ct(e,t,n){if(typeof e==`number`&&Number.isInteger(e)&&e>=0)return e;r(n,t,`expected a non-negative integer, received ${i(e)}`)}function lt(e,t,n){return typeof e==`boolean`?e:V(e,t,n)}function V(e,n,r){let i=_(e,n,r,at);if(i===void 0)return;let l={};a(l,`type`,o(i.type,t(n,`type`),r,(e,t,n)=>f(e,rt,t,n))),a(l,`description`,o(i.description,t(n,`description`),r,s)),a(l,`enum`,o(i.enum,t(n,`enum`),r,(e,t,n)=>h(e,t,n,ot))),a(l,`items`,o(i.items,t(n,`items`),r,V)),a(l,`properties`,o(i.properties,t(n,`properties`),r,(e,t,n)=>v(e,t,n,V))),a(l,`required`,o(i.required,t(n,`required`),r,g)),a(l,`anyOf`,o(i.anyOf,t(n,`anyOf`),r,(e,t,n)=>h(e,t,n,V))),a(l,`additionalProperties`,o(i.additionalProperties,t(n,`additionalProperties`),r,lt)),a(l,`minimum`,o(i.minimum,t(n,`minimum`),r,c)),a(l,`maximum`,o(i.maximum,t(n,`maximum`),r,c));for(let e of it)a(l,e,o(i[e],t(n,e),r,ct));return a(l,`pattern`,o(i.pattern,t(n,`pattern`),r,s)),a(l,`format`,o(i.format,t(n,`format`),r,s)),a(l,`default`,o(i.default,t(n,`default`),r,st)),l}function ut(e,n,i){let a=V(e,n,i);if(a!==void 0){if(a.type!==`object`){r(i,t(n,`type`),`expected the root parameter schema to be type 'object'`);return}if(a.properties===void 0){r(i,t(n,`properties`),`expected the root parameter schema to name its properties`);return}return{...a,type:`object`,properties:a.properties}}}function dt(e,n,r){let i=_(e,n,r,[`name`,`description`,`parameters`,`outputSchema`]);if(i===void 0)return;let c=s(i.name,t(n,`name`),r),l=s(i.description,t(n,`description`),r),u=ut(i.parameters,t(n,`parameters`),r);if(c===void 0||l===void 0||u===void 0)return;let d={name:c,description:l,parameters:u};return a(d,`outputSchema`,o(i.outputSchema,t(n,`outputSchema`),r,V)),d}function H(e,n,r,i,a){return o(e[n],t(r,n),i,(e,t,n)=>h(e,t,n,a))}function ft(e,n,r,i){a(e,`pendingExecution`,o(n.pendingExecution,t(r,`pendingExecution`),i,w));for(let c of[`name`,`systemPrompt`,`sandboxSnapshotId`])a(e,c,o(n[c],t(r,c),i,s));a(e,`history`,H(n,`history`,r,i,ee)),a(e,`toolSchemas`,H(n,`toolSchemas`,r,i,dt)),a(e,`backgroundTasks`,H(n,`backgroundTasks`,r,i,we)),a(e,`backgroundTaskEvents`,H(n,`backgroundTaskEvents`,r,i,z)),a(e,`backgroundJobGroups`,H(n,`backgroundJobGroups`,r,i,ve)),a(e,`backgroundJobGroupEvents`,H(n,`backgroundJobGroupEvents`,r,i,L)),a(e,`sessionLoops`,H(n,`sessionLoops`,r,i,nt)),a(e,`skillActivationEvents`,H(n,`skillActivationEvents`,r,i,Ve)),a(e,`memoryEvents`,H(n,`memoryEvents`,r,i,B)),a(e,`usedMemoryReferences`,H(n,`usedMemoryReferences`,r,i,He)),a(e,`contextReferences`,H(n,`contextReferences`,r,i,Ue)),a(e,`goal`,o(n.goal,t(r,`goal`),i,Xe)),a(e,`plan`,o(n.plan,t(r,`plan`),i,Qe)),a(e,`activeBranch`,o(n.activeBranch,t(r,`activeBranch`),i,$e))}const pt=[`id`,`name`,`cwd`,`createdAt`,`updatedAt`,`messages`,`pendingExecution`,`history`,`systemPrompt`,`toolSchemas`,`backgroundTasks`,`backgroundTaskEvents`,`backgroundJobGroups`,`backgroundJobGroupEvents`,`sessionLoops`,`skillActivationEvents`,`memoryEvents`,`usedMemoryReferences`,`contextReferences`,`sandboxSnapshotId`,`goal`,`plan`,`activeBranch`];function mt(e){let t=[],n=gt(e,``,t);return n===void 0||t.length>0?{status:`corrupt`,issues:t}:{status:`valid`,record:n}}function U(e){if(typeof e!=`object`||!e)return;let t=e;if(typeof t.cwd==`string`)return t.cwd;let n=t.record;if(typeof n==`object`&&n){let e=n.cwd;if(typeof e==`string`)return e}}function W(e){return e===void 0?{}:{cwd:e}}function ht(e){let t=[],n=_(e,``,t,[`schemaVersion`,`record`]);if(n===void 0)return{status:`corrupt`,issues:t,...W(U(e))};let r=n.schemaVersion;if(typeof r!=`number`||!Number.isFinite(r))return{status:`unsupported`,schemaVersion:void 0,...W(U(e))};if(r!==1&&r!==2)return{status:`unsupported`,schemaVersion:r,...W(U(e))};r===1&&typeof n.record==`object`&&n.record!==null&&`pendingExecution`in n.record&&t.push({path:`record.pendingExecution`,message:`pending execution requires schema version 2`});let i=gt(n.record,`record`,t);return i===void 0||t.length>0?{status:`corrupt`,issues:t,...W(U(e))}:{status:`valid`,record:i}}function gt(e,n,r){let i=_(e,n,r,pt);if(i===void 0)return;let a=s(i.id,t(n,`id`),r),o=s(i.cwd,t(n,`cwd`),r),c=p(i.createdAt,t(n,`createdAt`),r),l=p(i.updatedAt,t(n,`updatedAt`),r),u=h(i.messages,t(n,`messages`),r,F);if(a===void 0||o===void 0||c===void 0||l===void 0||u===void 0)return;let d={id:a,cwd:o,createdAt:c,updatedAt:l,messages:u};return ft(d,i,n,r),d.pendingExecution&&T(d.pendingExecution,d.messages,r,t(n,`pendingExecution`)),d}const _t=/^[A-Za-z0-9][A-Za-z0-9._-]*$/;function vt(){return`session_${(0,e.randomId)()}`}function yt(e){return e.length>0&&e.length<=128&&_t.test(e)}function bt(e){if(!yt(e))throw Error(`Invalid session id: ${JSON.stringify(e)}. A session id must be 1-128 characters of letters, digits, dot, underscore or hyphen, starting with a letter or digit.`)}const xt={sessionInit:`session_init`,sessionShutdown:`session_shutdown`,sessionShutdownStepError:`session_shutdown_step_error`,context:`context`,contextCompact:`context_compact`,error:`error`,historyMutation:`history_mutation`,providerRequest:`provider_request`,providerNativeRawPayload:`provider_native_raw_payload`,providerStreamRawDelta:`provider_stream_raw_delta`,providerResponseRaw:`provider_response_raw`,providerResponseNormalized:`provider_response_normalized`,structuredOutputTransport:`structured_output_transport`,providerFallback:`provider_fallback`,assistantMessageCommitted:`assistant_message_committed`,toolExecutionRequest:`tool_execution_request`,toolExecutionResult:`tool_execution_result`,toolBatchStarted:`tool_batch_started`,toolMessageCommitted:`tool_message_committed`,backgroundTaskEvent:`background_task_event`,backgroundJobGroupEvent:`background_job_group_event`,memoryEvent:`memory_event`,user:`user`,preRun:`pre_run`,textDelta:`text_delta`,assistant:`assistant`,toolCall:`tool_call`,toolResult:`tool_result`,toolBlocked:`tool_blocked`,toolDenied:`tool_denied`,serverTool:`server_tool`};function St(e,t){return e.event===t}const Ct=/^[0-9a-f]{64}$/;function wt(t,n){let r=(0,e.sha256Hex)(n);if(!Ct.test(t)||t!==r)throw Error(`Invalid sha256: external JSON payloads require their exact content digest.`)}function Tt(t,n,r){return bt(t),wt(n,r),{kind:`external-payload`,encoding:`json`,sha256:n,byteLength:(0,e.utf8ByteLength)(r),relativePath:`${t}.payloads/${n}.json`}}const G=e=>({decode:e}),K=e=>({decode:e,optional:!0}),Et=(e,t,n)=>h(e,t,n,s),q=(e,t,n)=>h(e,t,n,F),Dt=(e,t,n)=>h(e,t,n,dt),Ot=(e,t,n)=>typeof e==`string`?e:F(e,t,n),J=(e,t,n)=>{let r=l(e,t,n);return r!==void 0&&r<0&&n.push({path:t,message:`expected a non-negative integer`}),r};function Y(e){if(typeof e!=`object`||!e||Array.isArray(e))return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function X(e,n,r){if(!Y(e)){r.push({path:n,message:`expected an object, received ${i(e)}`});return}for(let[i,a]of Object.entries(e))Z(a,t(n,i),r);return e}const kt=[`maxTokens`,`usedTokens`,`usedPercentage`,`remainingPercentage`];function At(e,n,r){let i=_(e,n,r,kt);if(i===void 0)return;let a=c(i.maxTokens,t(n,`maxTokens`),r),o=c(i.usedTokens,t(n,`usedTokens`),r),s=c(i.usedPercentage,t(n,`usedPercentage`),r),l=c(i.remainingPercentage,t(n,`remainingPercentage`),r);if(a!==void 0&&o!==void 0&&s!==void 0&&l!==void 0)return{maxTokens:a,usedTokens:o,usedPercentage:s,remainingPercentage:l}}const jt=(e,n,r)=>h(e,n,r,(e,n,r)=>{let i=_(e,n,r,[`type`,`id`]);if(i===void 0)return;let a=s(i.type,t(n,`type`),r),o=s(i.id,t(n,`id`),r);return a===void 0||o===void 0?void 0:{type:a,id:o}});function Z(e,r,a,o=new WeakSet){if(e===null||typeof e==`string`||typeof e==`boolean`||typeof e==`number`&&Number.isFinite(e))return e;if(typeof e!=`object`||e instanceof Date){a.push({path:r,message:`expected a JSON-compatible value, received ${i(e)}`});return}if(!Array.isArray(e)&&!Y(e)){a.push({path:r,message:`expected a JSON-compatible object, received ${i(e)}`});return}if(o.has(e)){a.push({path:r,message:`expected an acyclic JSON-compatible value`});return}if(o.add(e),Array.isArray(e))for(let t=0;t<e.length;t++)Z(e[t],n(r,t),a,o);else for(let[n,i]of Object.entries(e))Z(i,t(r,n),a,o);return o.delete(e),e}function Mt(e,r,i,a=new WeakSet){if(!(typeof e!=`object`||!e||e instanceof Date)){if(a.has(e)){i.push({path:r,message:`expected an acyclic value`});return}if(a.add(e),Array.isArray(e))for(let t=0;t<e.length;t++)Object.hasOwn(e,t)?Mt(e[t],n(r,t),i,a):i.push({path:n(r,t),message:`expected an array element, received nothing`});else for(let[n,o]of Object.entries(e))Mt(o,t(r,n),i,a);a.delete(e)}}const Q=(...e)=>(t,n,r)=>f(t,e,n,r),Nt={session_init:{cwd:G(s),systemPromptLength:G(J),systemPrompt:G(s),toolSchemas:G(Dt),model:G(s),provider:G(s)},session_shutdown:{reason:G(s)},session_shutdown_step_error:{step:G(s),error:G(s)},context:{maxTokens:G(c),usedTokens:G(c),usedPercentage:G(c),remainingPercentage:G(c)},context_compact:{trigger:G(s),before:G(At),after:G(At)},error:{message:G(s),stack:G(s),historyLength:G(J)},history_mutation:{executionId:K(s),conversationId:K(s),round:K(J),batchId:K(s),usageObservationId:K(s),providerId:K(s),modelId:K(s),providerError:K(u),contextOverflow:K(u),mutation:G(Q(`append_message`)),index:G(J),message:G(F)},provider_request:{executionId:G(s),conversationId:K(s),round:G(J),provider:G(s),model:G(s),effort:K(s),forcedSummary:K(u),messages:G(q),tools:K(Dt)},provider_native_raw_payload:{executionId:G(s),conversationId:K(s),round:G(J),provider:G(s),apiSurface:K(s),payloadKind:G(Q(`request`,`response`,`stream_event`)),sequence:G(J),payload:K(Z),metadata:K(X)},provider_stream_raw_delta:{executionId:G(s),conversationId:K(s),round:G(J),sequence:G(J),delta:G(s)},provider_response_raw:{executionId:G(s),conversationId:K(s),round:G(J),effort:K(s),response:G(F),responseKind:G(s)},provider_response_normalized:{executionId:G(s),conversationId:K(s),round:G(J),response:G(F),toolCallsCount:K(J)},structured_output_transport:{executionId:G(s),conversationId:K(s),round:G(J),provider:G(s),model:G(s),mechanism:G(Q(`response_schema`,`json_object`,`none`)),provenance:G(Q(`catalog`,`vendor-default`,`undeclared`,`unverified-endpoint`)),sent:G(u),schemaInPrompt:G(u),reason:K(s)},provider_fallback:{executionId:G(s),conversationId:K(s),round:G(J),fromProvider:G(s),fromModel:G(s),toProvider:G(s),toModel:G(s),reason:G(s)},assistant_message_committed:{executionId:G(s),conversationId:K(s),round:G(J),message:G(Ot)},tool_execution_request:{executionId:G(s),conversationId:K(s),round:G(J),batchId:K(s),index:K(J),toolName:G(s),toolCallId:G(s),parameters:G(X),ownerPath:K(jt)},tool_execution_result:{executionId:G(s),conversationId:K(s),round:G(J),batchId:K(s),index:K(J),toolName:K(s),toolCallId:K(s),success:G(u),result:K(Z),error:K(s),metadata:K(X)},tool_batch_started:{executionId:G(s),conversationId:K(s),round:G(J),batchId:K(s),mode:G(Q(`parallel`,`sequential`)),maxConcurrency:G(J),requestCount:G(J),tools:G(Et)},tool_message_committed:{executionId:G(s),conversationId:K(s),round:G(J),batchId:K(s),index:K(J),message:G(F)},background_task_event:{backgroundEventType:K(s),backgroundEvent:K(z),data:K(z),taskId:K(s),originToolCallId:K(s)},background_job_group_event:{backgroundJobGroupEvent:K(L),data:K(L)},memory_event:{memoryEvent:K(B),data:K(B)},user:{content:G(s)},pre_run:{historyLength:G(J),historyChars:G(J),historyEstTokens:G(J),input:G(s),history:G(q),model:G(s),provider:G(s),maxTokens:G(c),nativeWebSearchSupported:G(u),nativeWebSearchEnabled:G(u),nativeWebFetchSupported:G(u),nativeWebFetchEnabled:G(u)},text_delta:{delta:G(s)},assistant:{content:G(s),historyLength:G(J),estimatedChars:G(J),history:G(q),historyStructure:G((e,n,r)=>h(e,n,r,(e,n,r)=>{if(typeof e!=`object`||!e||Array.isArray(e)){r.push({path:n,message:`expected an object, received ${i(e)}`});return}let a=e;f(a.role,[`user`,`assistant`,`system`,`tool`],t(n,`role`),r),J(a.contentLength,t(n,`contentLength`),r),u(a.hasToolCalls,t(n,`hasToolCalls`),r),Et(a.toolCallNames,t(n,`toolCallNames`),r),a.metadata!==void 0&&X(a.metadata,t(n,`metadata`),r);for(let e of Object.keys(a))[`role`,`contentLength`,`hasToolCalls`,`toolCallNames`,`metadata`].includes(e)||r.push({path:t(n,e),message:`unknown history structure field`});return a}))},tool_call:{tool:G(s),args:G(X)},tool_result:{tool:G(s),success:G(u),dataChars:G(J),truncated:G(u)},tool_blocked:{tool:G(s),reason:G(s)},tool_denied:{tool:G(s),reason:G(s)},server_tool:{tool:G(s)}};var Pt=class extends Error{code;issues;schemaVersion;constructor(e,t,n={}){super(`Session log decode failed: ${e}${t[0]?` at ${t[0].path}: ${t[0].message}`:``}`,{cause:n.cause}),this.name=`SessionLogDecodeError`,this.code=e,this.issues=t,n.schemaVersion!==void 0&&(this.schemaVersion=n.schemaVersion)}};const Ft=new Set(Object.values(xt));function It(e,t={}){if(!Array.isArray(e))throw new Pt(`INVALID_EVENT`,[{path:`entries`,message:`expected an array, received ${i(e)}`}]);let n=[],r=[],a;for(let[i,o]of e.entries()){let e=Lt(o,t.lineNumbers?.[i]===void 0?`[${i}]`:`line ${t.lineNumbers[i]}`,n);e.entry!==void 0&&r.push(e.entry),a??=e.unsupported}if(n.length>0)throw new Pt(a===void 0?`INVALID_EVENT`:`UNSUPPORTED_VERSION`,n,{schemaVersion:a});return r}function Lt(e,t,n){if(!Y(e))return n.push({path:t,message:`expected an object, received ${i(e)}`}),{};let r=n.length;if(Mt(e,t,n),n.length!==r)return{};let a=Rt(e,t,n);return a.event===void 0?{unsupported:a.unsupported}:(zt(e,a.event,t,a.output,n),{entry:a.output,unsupported:a.unsupported})}function Rt(e,n,r){let a={schemaVersion:1},o;if(e.schemaVersion!==1){let a=e.schemaVersion;typeof a==`number`&&Number.isSafeInteger(a)&&a>=0?(o=a,r.push({path:t(n,`schemaVersion`),message:`unsupported schema version`})):r.push({path:t(n,`schemaVersion`),message:`expected schema version 1, received ${i(a)}`})}let c=p(e.timestamp,t(n,`timestamp`),r);c!==void 0&&(a.timestamp=c);let l=s(e.sessionId,t(n,`sessionId`),r);l===``&&r.push({path:t(n,`sessionId`),message:`expected a non-empty session ID`}),l!==void 0&&(a.sessionId=l);let u=e.event;return typeof u!=`string`||!Ft.has(u)?(r.push({path:t(n,`event`),message:`expected a declared event name, received ${i(u)}`}),{output:a,unsupported:o}):(a.event=u,{output:a,event:u,unsupported:o})}function zt(e,n,r,i,a){let o=Nt[n];for(let[n,s]of Object.entries(o)){if(!Object.hasOwn(e,n)&&s.optional)continue;let o=s.decode(e[n],t(r,n),a);o!==void 0&&(i[n]=o)}for(let s of Object.keys(e)){if([`schemaVersion`,`timestamp`,`sessionId`,`event`,...Object.keys(o)].includes(s))continue;if(n!==`server_tool`){a.push({path:t(r,s),message:`unknown event payload field`});continue}let c=Z(e[s],t(r,s),a);c!==void 0&&(i[s]=c)}Bt(e,n,r,a)}function Bt(e,t,n,r){t===`background_task_event`&&e.backgroundEvent===void 0&&e.data===void 0&&r.push({path:n,message:`expected a background task event payload`}),t===`background_job_group_event`&&e.backgroundJobGroupEvent===void 0&&e.data===void 0&&r.push({path:n,message:`expected a background job group event payload`}),t===`memory_event`&&e.memoryEvent===void 0&&e.data===void 0&&r.push({path:n,message:`expected a memory event payload`})}var Vt=class extends Error{code;metadata;constructor(e,t,n={},r){super(t,r===void 0?void 0:{cause:r}),this.name=`SessionLogPayloadResolutionError`,this.code=e,this.metadata=n}};function Ht(e,t){switch(t){case`locked`:return`Session ${e} is sealed and locked, so it was not overwritten; unlock to open it.`;case`does-not-open`:return`Session ${e} holds a sealed record that does not open for this session, so it was not overwritten.`;case`changed`:return`Session ${e} changed on disk since this process last wrote or read it, so it was not overwritten.`;case`unreadable`:return`Session ${e} could not be read, so it was not overwritten.`;case`newer-format`:return`Session ${e} was sealed by a newer build, so it was not overwritten.`;case`no-keys`:return`Session ${e} is sealed and this process has no keys that open it, so it was not overwritten.`}}var Ut=class extends Error{code=`SESSION_SEALED_UNREADABLE`;sessionId;reason;constructor(e,t){super(Ht(e,t)),this.name=`SealedSessionUnreadableError`,this.sessionId=e,this.reason=t}};function Wt(e){return typeof e==`object`&&!!e&&e.code===`SESSION_SEALED_UNREADABLE`}function Gt(e){return{...e===void 0?{}:{pid:e.pid},self:!0,liveness:`alive`}}function Kt(e){let t=e.owner;return{...t===void 0?{}:{pid:t.pid},...t?.hostname===void 0?{}:{hostname:t.hostname},...t?.startTime===void 0?{}:{startTime:t.startTime},self:e.self,liveness:e.liveness}}function $(e){let t=[e.pid===void 0?`a process this one cannot read`:`pid ${e.pid}`];return e.hostname!==void 0&&t.push(`on ${e.hostname}`),e.startTime!==void 0&&t.push(`started ${e.startTime}`),t.join(` `)}function qt(e,t){let n=t.holder??Gt(t.ownProcess);if(n.lent===!0)return n.self?`Session ${e} is lent to a subagent this process started, which has not taken it up yet; try again once that subagent has finished.`:`Session ${e} is lent to a subagent this process started (${$(n)}), which writes it; try again once that subagent has finished.`;if(n.self)return`Session ${e} is already open in this process${t.ownProcess===void 0?``:` (pid ${t.ownProcess.pid})`}: a session running or still closing here. Try again once that has ended.`;let r=t.releaseCommand?.(e);if(n.liveness===`gone`)return`Session ${e} is held by a process that is no longer running (${$(n)}), and another process is releasing it now. Try again${r===void 0?``:`, or run: ${r}`}.`;let i=`Session ${e} ${n.liveness===`alive`?`is open in another process`:`may be open in another process`} (${$(n)}), which writes it; close it there first.`+(r===void 0?``:` If that process is no longer running, release the session with: ${r}`);return t.ownProcess?.windows===!0&&(i+=` On Windows, a process that crashed may still be shown as holding it.`),i}function Jt(e,t,n){switch(t){case`has_writer`:return qt(e,n);case`released`:return n.lease===!0?`Session ${e} was not opened: the lease it was handed over with has ended.`:`Session ${e} was not saved: the claim it was saved under has ended or no longer holds its writer marker.`}}var Yt=class extends Error{code=`SESSION_CLAIMED`;sessionId;reason;holder;closing;constructor(e,t,n={}){super(Jt(e,t,n)),this.name=`SessionClaimedError`,this.sessionId=e,this.reason=t,this.holder=t===`has_writer`?n.holder??Gt(n.ownProcess):void 0,this.closing=this.holder?.self===!0&&this.holder.lent!==!0}};function Xt(e){return typeof e==`object`&&!!e&&e.code===`SESSION_CLAIMED`}var Zt=class{#e=new Map;#t=new Map;#n;#r;constructor(e={},t){this.#n=e,this.#r=t}has(e){return this.#e.has(e)}isLent(e){return this.#t.has(e)}refusal(e,t,n={}){return new Yt(e,t,{...this.#n,...this.#r===void 0?{}:{ownProcess:this.#r},...n})}assertUnclaimed(e){if(this.#e.has(e)||this.#t.has(e))throw this.#i(e)}#i(e){let t=this.#t.get(e);if(t===void 0)return this.refusal(e,`has_writer`);let n=t.holder(),r=n===void 0||n.self?Gt(this.#r):Kt(n);return this.refusal(e,`has_writer`,{holder:{...r,lent:!0}})}open(e,t,n,r){let i={marker:n},a=()=>{this.#e.get(e)===i&&this.#e.delete(e),n?.release()};t&&this.#e.set(e,i);let o;try{o=r()}catch(e){throw a(),e}this.#e.set(e,i);let s=!1,c=Object.freeze({outcome:o,release:()=>{s||(s=!0,a())}});return i.handle=c,c}markerForSave(e,t){let n=this.#e.get(e);if(t!==void 0){if(n===void 0||n.handle!==t||n.marker!==void 0&&!n.marker.holds())throw this.refusal(e,`released`);return n.marker}if(n!==void 0||this.#t.has(e))throw this.#i(e)}markerForWrite(e){if(this.#t.has(e))throw this.#i(e);let t=this.#e.get(e);if(t!==void 0){if(t.marker===void 0||!t.marker.holds())throw this.refusal(e,`has_writer`);return t.marker}}lend(e,t){let n={holder:t};return this.#t.set(e,n),()=>{this.#t.get(e)===n&&this.#t.delete(e)}}};Object.defineProperty(exports,"_",{enumerable:!0,get:function(){return pt}}),Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return Ut}}),Object.defineProperty(exports,"b",{enumerable:!0,get:function(){return ht}}),Object.defineProperty(exports,"c",{enumerable:!0,get:function(){return Pt}}),Object.defineProperty(exports,"d",{enumerable:!0,get:function(){return xt}}),Object.defineProperty(exports,"f",{enumerable:!0,get:function(){return 1}}),Object.defineProperty(exports,"g",{enumerable:!0,get:function(){return yt}}),Object.defineProperty(exports,"h",{enumerable:!0,get:function(){return vt}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return Kt}}),Object.defineProperty(exports,"l",{enumerable:!0,get:function(){return It}}),Object.defineProperty(exports,"m",{enumerable:!0,get:function(){return bt}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return Zt}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return Wt}}),Object.defineProperty(exports,"p",{enumerable:!0,get:function(){return St}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return Xt}}),Object.defineProperty(exports,"s",{enumerable:!0,get:function(){return Vt}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return Yt}}),Object.defineProperty(exports,"u",{enumerable:!0,get:function(){return Tt}}),Object.defineProperty(exports,"v",{enumerable:!0,get:function(){return 2}}),Object.defineProperty(exports,"y",{enumerable:!0,get:function(){return mt}});
@@ -0,0 +1,2 @@
1
+ import{randomId as e,sha256Hex as t,utf8ByteLength as n}from"@robota-sdk/agent-core";function r(e,t){return e.length===0?t:`${e}.${t}`}function i(e,t){return`${e}[${t}]`}function a(e,t,n){e.push({path:t,message:n})}function o(e){if(e===null)return`null`;if(Array.isArray(e))return`an array`;if(e instanceof Date)return`a Date`;switch(typeof e){case`undefined`:return`nothing`;case`string`:return`a string`;case`number`:return`a number`;case`boolean`:return`a boolean`;case`object`:return`an object`;default:return`a ${typeof e}`}}function s(e,t,n){n!==void 0&&(e[t]=n)}function c(e,t,n,r){if(e!==void 0)return r(e,t,n)}function l(e,t,n){if(typeof e==`string`)return e;a(n,t,`expected a string, received ${o(e)}`)}function u(e,t,n){if(typeof e==`number`&&Number.isFinite(e))return e;a(n,t,`expected a finite number, received ${o(e)}`)}function d(e,t,n){if(typeof e==`number`&&Number.isInteger(e))return e;a(n,t,`expected an integer, received ${o(e)}`)}function f(e,t,n){if(typeof e==`boolean`)return e;a(n,t,`expected a boolean, received ${o(e)}`)}function p(e,t,n){if(e===!0)return!0;a(n,t,`expected true, received ${o(e)}`)}function m(e,t,n,r){if(typeof e==`string`&&t.includes(e))return e;a(r,n,`expected one of ${t.join(` | `)}, received ${o(e)}`)}function h(e,t,n){if(typeof e!=`string`){a(n,t,`expected an ISO-8601 timestamp string, received ${o(e)}`);return}if(Number.isNaN(Date.parse(e))){a(n,t,`expected a timestamp a date can be parsed from`);return}return e}function g(e,t,n){if(e instanceof Date){if(Number.isNaN(e.getTime())){a(n,t,`expected a valid Date, received an invalid one`);return}return e}if(typeof e==`string`&&!Number.isNaN(Date.parse(e)))return new Date(e);a(n,t,`expected a Date or an ISO-8601 timestamp string, received ${o(e)}`)}function _(e,t,n,r){if(!Array.isArray(e)){a(n,t,`expected an array, received ${o(e)}`);return}let s=[];return e.forEach((e,a)=>{let o=r(e,i(t,a),n);o!==void 0&&s.push(o)}),s}function v(e,t,n){return _(e,t,n,l)}function y(e,t,n,i){if(typeof e!=`object`||!e||Array.isArray(e)){a(n,t,`expected an object, received ${o(e)}`);return}let s=e,c=new Set(i);for(let e of Object.keys(s))c.has(e)||a(n,r(t,e),`unknown key; the record contract does not declare it`);return s}function b(e,t,n,i){if(typeof e!=`object`||!e||Array.isArray(e)){a(n,t,`expected an object, received ${o(e)}`);return}let s={};for(let[a,o]of Object.entries(e)){let e=i(o,r(t,a),n);e!==void 0&&Object.defineProperty(s,a,{value:e,enumerable:!0,writable:!0,configurable:!0})}return s}function x(e,t,n){if(e===null||e instanceof Date||typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;if(Array.isArray(e)){let r=[];return e.forEach((e,a)=>{let o=x(e,i(t,a),n);o!==void 0&&r.push(o)}),r}if(typeof e==`object`)return b(e,t,n,x);a(n,t,`expected a JSON-compatible value, received ${o(e)}`)}function S(e,t,n){if(typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;a(n,t,`expected a string, number or boolean, received ${o(e)}`)}function C(e,t,n){let r=l(e,t,n);if(r!==void 0&&r.trim().length>0)return r;r!==void 0&&a(n,t,`expected a nonempty identity`)}function w(e,t=new Set){if(e===null||typeof e==`string`||typeof e==`boolean`)return!0;if(typeof e==`number`)return Number.isFinite(e);if(typeof e!=`object`||e instanceof Date||t.has(e)||!Array.isArray(e)&&Object.getPrototypeOf(e)!==Object.prototype&&Object.getPrototypeOf(e)!==null)return!1;t.add(e);let n=(Array.isArray(e)?e:Object.values(e)).every(e=>w(e,t));return t.delete(e),n}function T(e,t,n){let i=y(e,t,n,[`executionId`,`actionId`,`parentCallId`,`toolCallId`,`toolName`,`requestId`,`kind`,`data`]);if(!i)return;let o=C(i.executionId,r(t,`executionId`),n),s=C(i.actionId,r(t,`actionId`),n),c=C(i.parentCallId,r(t,`parentCallId`),n),l=C(i.toolCallId,r(t,`toolCallId`),n),u=C(i.toolName,r(t,`toolName`),n),d=C(i.requestId,r(t,`requestId`),n),f=C(i.kind,r(t,`kind`),n);if(!w(i.data)){a(n,r(t,`data`),`expected finite JSON`);return}let p=b(i.data,r(t,`data`),n,x);if(o!==void 0&&s!==void 0&&c!==void 0&&l!==void 0&&u!==void 0&&d!==void 0&&f!==void 0&&p!==void 0)return{executionId:o,actionId:s,parentCallId:c,toolCallId:l,toolName:u,requestId:d,kind:f,data:p}}function E(e,t,n){let i=y(e,t,n,[`executionId`,`requests`]);if(!i)return;let a=C(i.executionId,r(t,`executionId`),n),o=_(i.requests,r(t,`requests`),n,T);if(a!==void 0&&o!==void 0)return{executionId:a,requests:o}}function D(e,t,n,r=`pendingExecution`){let i=t.findLastIndex(e=>e.role===`assistant`),o=t[i];if(o?.role!==`assistant`||o.metadata?.executionId!==e.executionId||!t.slice(i+1).every(e=>e.role===`tool`)){a(n,r,`pending execution must match the terminal assistant round`);return}let s=new Map((o.toolCalls??[]).map(e=>[e.id,e]));s.size!==o.toolCalls?.length&&a(n,r,`tool call identities must be unique within a round`);let c=t.slice(i+1).flatMap(e=>e.role===`tool`?[e]:[]),l=new Set;for(let e of c){let t=s.get(e.toolCallId);(!t||l.has(e.toolCallId)||e.name!==void 0&&e.name!==t.function.name)&&a(n,r,`terminal receipts must uniquely bind this round`),l.add(e.toolCallId)}let u=new Set(t.slice(i+1).flatMap(e=>e.role===`tool`?[e.toolCallId]:[])),d=new Map((o.toolCalls??[]).filter(e=>!u.has(e.id)).map(e=>[e.id,e]));d.size===0&&a(n,r,`pending execution requires unanswered calls`);let f=new Set,p=new Map,m=new Map;for(let t of e.requests){let i=d.get(t.toolCallId),s=JSON.stringify([t.parentCallId,t.toolCallId,t.toolName]);(f.has(t.requestId)||t.executionId!==e.executionId||t.parentCallId!==o.metadata?.providerCallId||!i||i.function.name!==t.toolName||p.has(t.actionId)&&p.get(t.actionId)!==s||m.has(t.toolCallId)&&m.get(t.toolCallId)!==t.actionId)&&a(n,r,`wait identities must uniquely bind an open tool action`),f.add(t.requestId),p.set(t.actionId,s),m.set(t.toolCallId,t.actionId)}}const O=[`complete`,`interrupted`],k=[`id`,`timestamp`,`state`,`metadata`,`role`,`content`,`parts`],ee={user:[...k,`name`],assistant:[...k,`toolCalls`],system:[...k,`name`],tool:[...k,`toolCallId`,`name`]};function A(e,t,n){if(e instanceof Date||typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;if(Array.isArray(e)){if(e.every(e=>typeof e==`string`)||e.every(e=>typeof e==`number`))return e;a(n,t,`expected an array of only strings or only numbers`);return}if(typeof e==`object`&&e)return b(e,t,n,(e,t,n)=>{if(typeof e==`number`)return e;a(n,t,`expected a number, received ${o(e)}`)});a(n,t,`expected a metadata value, received ${o(e)}`)}function j(e,t,n){return b(e,t,n,A)}function M(e,t,n){let i=m(e?.type,[`text`,`image_inline`,`image_uri`,`audio_inline`,`resource_link`,`resource_embedded`],r(t,`type`),n);if(i===void 0)return;if(i===`text`){let i=y(e,t,n,[`type`,`text`]);if(i===void 0)return;let a=l(i.text,r(t,`text`),n);return a===void 0?void 0:{type:`text`,text:a}}if(i===`image_inline`||i===`audio_inline`){let a=y(e,t,n,[`type`,`mimeType`,`data`]);if(a===void 0)return;let o=l(a.mimeType,r(t,`mimeType`),n),s=l(a.data,r(t,`data`),n);return o===void 0||s===void 0?void 0:{type:i,mimeType:o,data:s}}if(i===`resource_link`){let o=y(e,t,n,[`type`,`uri`,`name`,`mimeType`,`title`,`description`,`size`]);if(o===void 0)return;let d=l(o.uri,r(t,`uri`),n),f=l(o.name,r(t,`name`),n);if(d===void 0||f===void 0)return;let p={type:i,uri:d,name:f};for(let e of[`mimeType`,`title`,`description`])s(p,e,c(o[e],r(t,e),n,l));let m=c(o.size,r(t,`size`),n,u);return m!==void 0&&(!Number.isSafeInteger(m)||m<0)&&a(n,r(t,`size`),`expected a nonnegative safe integer`),s(p,`size`,m),p}if(i===`resource_embedded`){let o=y(e,t,n,[`type`,`uri`,`mimeType`,`text`,`blob`]);if(o===void 0)return;let s=l(o.uri,r(t,`uri`),n),u=c(o.mimeType,r(t,`mimeType`),n,l);if(s===void 0)return;let d={type:i,uri:s,...u===void 0?{}:{mimeType:u}};if(o.text!==void 0&&o.blob===void 0){let e=l(o.text,r(t,`text`),n);return e===void 0?void 0:{...d,text:e}}if(o.blob!==void 0&&o.text===void 0){let e=l(o.blob,r(t,`blob`),n);return e===void 0?void 0:{...d,blob:e}}a(n,t,`expected exactly one text or blob payload`);return}let o=y(e,t,n,[`type`,`uri`,`mimeType`]);if(o===void 0)return;let d=l(o.uri,r(t,`uri`),n);if(d===void 0)return;let f={type:`image_uri`,uri:d};return s(f,`mimeType`,c(o.mimeType,r(t,`mimeType`),n,l)),f}function N(e,t,n){let i=y(e,t,n,[`id`,`type`,`function`]);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=m(i.type,[`function`],r(t,`type`),n),s=r(t,`function`),c=y(i.function,s,n,[`name`,`arguments`]),u=c===void 0?void 0:l(c.name,r(s,`name`),n),d=c===void 0?void 0:l(c.arguments,r(s,`arguments`),n);if(a!==void 0&&o!==void 0&&u!==void 0&&d!==void 0)return{id:a,type:o,function:{name:u,arguments:d}}}function te(e,t,n){let i=l(e.id,r(t,`id`),n),a=g(e.timestamp,r(t,`timestamp`),n),o=m(e.state,O,r(t,`state`),n);if(i!==void 0&&a!==void 0&&o!==void 0)return{id:i,timestamp:a,state:o}}function P(e,t,n,i){s(e,`metadata`,c(t.metadata,r(n,`metadata`),i,j)),s(e,`parts`,c(t.parts,r(n,`parts`),i,(e,t,n)=>_(e,t,n,M)))}function F(e,t,n){let i=m(e?.role,[`user`,`assistant`,`system`,`tool`],r(t,`role`),n);if(i===void 0)return;let a=y(e,t,n,ee[i]);if(a===void 0)return;let o=te(a,t,n);if(o===void 0)return;let u=r(t,`content`);if(i===`assistant`){let e=a.content===null?null:l(a.content,u,n);if(e===void 0)return;let d={...o,role:i,content:e};return P(d,a,t,n),s(d,`toolCalls`,c(a.toolCalls,r(t,`toolCalls`),n,(e,t,n)=>_(e,t,n,N))),d}let d=l(a.content,u,n);if(d===void 0)return;if(i===`tool`){let e=l(a.toolCallId,r(t,`toolCallId`),n);if(e===void 0)return;let u={...o,role:i,content:d,toolCallId:e};return P(u,a,t,n),s(u,`name`,c(a.name,r(t,`name`),n,l)),u}let f={...o,role:i,content:d};return P(f,a,t,n),s(f,`name`,c(a.name,r(t,`name`),n,l)),f}function ne(e,t,n){let i=y(e,t,n,[`id`,`timestamp`,`category`,`type`,`data`]);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=g(i.timestamp,r(t,`timestamp`),n),u=l(i.category,r(t,`category`),n),d=l(i.type,r(t,`type`),n);if(a===void 0||o===void 0||u===void 0||d===void 0)return;let f={id:a,timestamp:o,category:u,type:d};return s(f,`data`,c(i.data,r(t,`data`),n,x)),f}const re=[`agent`,`process`,`scheduled`,`tool-invocation`],ie=[`foreground`,`background`],ae=[`none`,`worktree`],oe=[`queued`,`running`,`waiting_permission`,`sleeping`,`paused`,`completed`,`failed`,`cancelled`],se=[`idle`,`max_runtime`,`output_limit`,`repetition`,`stale_worker`],ce=[`validation`,`capacity`,`permission`,`timeout`,`runner`,`crash`,`provider`,`process`],le=[`denied-by-person`,`no-approver`,`approver-error`,`cancelled`];function I(e,t,n){return b(e,t,n,S)}function ue(e,t,n){return b(e,t,n,l)}function de(e,t,n){let i=y(e,t,n,[`category`,`message`,`recoverable`]);if(i===void 0)return;let a=m(i.category,ce,r(t,`category`),n),o=l(i.message,r(t,`message`),n),s=f(i.recoverable,r(t,`recoverable`),n);if(a!==void 0&&o!==void 0&&s!==void 0)return{category:a,message:o,recoverable:s}}function fe(e,t,n){let i=y(e,t,n,[`promptTokens`,`completionTokens`,`totalTokens`]);if(i===void 0)return;let a=d(i.promptTokens,r(t,`promptTokens`),n),o=d(i.completionTokens,r(t,`completionTokens`),n),s=d(i.totalTokens,r(t,`totalTokens`),n);if(a!==void 0&&o!==void 0&&s!==void 0)return{promptTokens:a,completionTokens:o,totalTokens:s}}function pe(e,t,n){let i=y(e,t,n,[`total`,`byReason`]);if(i===void 0)return;let a=d(i.total,r(t,`total`),n),o=r(t,`byReason`),s=y(i.byReason,o,n,le);if(a===void 0||s===void 0)return;let c={},l=!0;for(let e of le){let t=d(s[e],r(o,e),n);if(t===void 0){l=!1;continue}c[e]=t}if(l)return{total:a,byReason:c}}function me(e,t,n){let i=y(e,t,n,[`taskId`,`kind`,`output`,`exitCode`,`signalCode`,`metadata`,`usage`,`deniedToolCalls`]);if(i===void 0)return;let o=l(i.taskId,r(t,`taskId`),n),u=m(i.kind,re,r(t,`kind`),n),f=l(i.output,r(t,`output`),n);if(o===void 0||u===void 0||f===void 0)return;let p=c(i.exitCode,r(t,`exitCode`),n,d),h=c(i.signalCode,r(t,`signalCode`),n,l),g=c(i.metadata,r(t,`metadata`),n,I),_=c(i.usage,r(t,`usage`),n,fe),v=c(i.deniedToolCalls,r(t,`deniedToolCalls`),n,pe);switch(u!==`process`&&p!==void 0&&a(n,r(t,`exitCode`),`must not be set for a '${u}' result`),u!==`process`&&h!==void 0&&a(n,r(t,`signalCode`),`must not be set for a '${u}' result`),u!==`agent`&&_!==void 0&&a(n,r(t,`usage`),`must not be set for a '${u}' result`),u!==`agent`&&v!==void 0&&a(n,r(t,`deniedToolCalls`),`must not be set for a '${u}' result`),u){case`process`:{let e={taskId:o,kind:u,output:f};return s(e,`exitCode`,p),s(e,`signalCode`,h),s(e,`metadata`,g),e}case`agent`:{let e={taskId:o,kind:u,output:f};return s(e,`metadata`,g),s(e,`usage`,_),s(e,`deniedToolCalls`,v),e}case`scheduled`:{let e={taskId:o,kind:u,output:f};return s(e,`metadata`,g),e}case`tool-invocation`:{let e={taskId:o,kind:u,output:f};return s(e,`metadata`,g),e}}}function he(e,t,n){let i=y(e,t,n,[`cronExpression`,`agentInstruction`,`command`,`shell`,`env`]);if(i===void 0)return;let a=l(i.cronExpression,r(t,`cronExpression`),n);if(a===void 0)return;let o={cronExpression:a};for(let e of[`agentInstruction`,`command`,`shell`])s(o,e,c(i[e],r(t,e),n,l));return s(o,`env`,c(i.env,r(t,`env`),n,ue)),o}const ge=[`detached`,`wait_all`,`wait_any`,`manual`],_e=[`running`,`completed`],ve=[`background_job_group_created`,`background_job_group_updated`,`background_job_group_completed`];function ye(e,t,n){let i=y(e,t,n,[`taskId`,`label`,`status`,`summary`,`outputRef`,`transcriptSealed`,`error`,`startedAt`,`completedAt`]);if(i===void 0)return;let a=l(i.taskId,r(t,`taskId`),n),o=l(i.label,r(t,`label`),n),u=m(i.status,oe,r(t,`status`),n);if(a===void 0||o===void 0||u===void 0)return;let d={taskId:a,label:o,status:u};for(let e of[`summary`,`outputRef`])s(d,e,c(i[e],r(t,e),n,l));for(let e of[`startedAt`,`completedAt`])s(d,e,c(i[e],r(t,e),n,h));return s(d,`transcriptSealed`,c(i.transcriptSealed,r(t,`transcriptSealed`),n,p)),s(d,`error`,c(i.error,r(t,`error`),n,de)),d}function be(e,t,n){let i=y(e,t,n,[`id`,`parentSessionId`,`waitPolicy`,`taskIds`,`status`,`createdAt`,`updatedAt`,`label`,`completedAt`,`results`]);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=l(i.parentSessionId,r(t,`parentSessionId`),n),u=m(i.waitPolicy,ge,r(t,`waitPolicy`),n),d=v(i.taskIds,r(t,`taskIds`),n),f=m(i.status,_e,r(t,`status`),n),p=h(i.createdAt,r(t,`createdAt`),n),g=h(i.updatedAt,r(t,`updatedAt`),n),b=_(i.results,r(t,`results`),n,ye);if(a===void 0||o===void 0||u===void 0||d===void 0||f===void 0||p===void 0||g===void 0||b===void 0)return;let x={id:a,parentSessionId:o,waitPolicy:u,taskIds:d,status:f,createdAt:p,updatedAt:g,results:b};return s(x,`label`,c(i.label,r(t,`label`),n,l)),s(x,`completedAt`,c(i.completedAt,r(t,`completedAt`),n,h)),x}function xe(e,t,n){let i=m(e?.type,ve,r(t,`type`),n);if(i===void 0)return;let a=y(e,t,n,[`type`,`group`]);if(a===void 0)return;let o=be(a.group,r(t,`group`),n);return o===void 0?void 0:{type:i,group:o}}const Se=[`parentTaskId`,`currentAction`,`logPath`,`transcriptPath`],Ce=[`agentType`,`resumeSessionId`,`promptPreview`,`worktreePath`,`branchName`,`worktreeStatus`,`worktreeNextAction`,`worktreeBaseRevision`,`parentWorktreeStatus`],we=[`commandPreview`],Te=[`startedAt`,`lastActivityAt`,`completedAt`],Ee=[`id`,`kind`,`label`,`status`,`mode`,`parentSessionId`,`depth`,`cwd`,`pid`,`updatedAt`,`isolation`,`unread`,`result`,`error`,`timeoutReason`,`transcriptSealed`,`schedule`,`nextFireAt`,`metadata`,...Se,...Ce,...we,...Te];function De(e,t,n){let i=y(e,t,n,Ee);if(i===void 0)return;let o=l(i.id,r(t,`id`),n),u=m(i.kind,re,r(t,`kind`),n),g=l(i.label,r(t,`label`),n),_=m(i.status,oe,r(t,`status`),n),v=m(i.mode,ie,r(t,`mode`),n),b=l(i.parentSessionId,r(t,`parentSessionId`),n),x=d(i.depth,r(t,`depth`),n),S=l(i.cwd,r(t,`cwd`),n),C=h(i.updatedAt,r(t,`updatedAt`),n),w=f(i.unread,r(t,`unread`),n);if(o===void 0||u===void 0||g===void 0||_===void 0||v===void 0||b===void 0||x===void 0||S===void 0||C===void 0||w===void 0)return;let T={id:o,label:g,status:_,mode:v,parentSessionId:b,depth:x,cwd:S,updatedAt:C,unread:w};for(let e of Se)s(T,e,c(i[e],r(t,e),n,l));for(let e of Te)s(T,e,c(i[e],r(t,e),n,h));s(T,`pid`,c(i.pid,r(t,`pid`),n,d)),s(T,`transcriptSealed`,c(i.transcriptSealed,r(t,`transcriptSealed`),n,p)),s(T,`timeoutReason`,c(i.timeoutReason,r(t,`timeoutReason`),n,(e,t,n)=>m(e,se,t,n))),s(T,`metadata`,c(i.metadata,r(t,`metadata`),n,I)),s(T,`error`,c(i.error,r(t,`error`),n,de));let E=c(i.result,r(t,`result`),n,me);E!==void 0&&(E.taskId!==o&&a(n,r(r(t,`result`),`taskId`),`must match the task ID`),E.kind!==u&&a(n,r(r(t,`result`),`kind`),`must match the task kind`));let D=c(i.agentType,r(t,`agentType`),n,l),O=c(i.resumeSessionId,r(t,`resumeSessionId`),n,l),k=c(i.promptPreview,r(t,`promptPreview`),n,l),ee=c(i.isolation,r(t,`isolation`),n,(e,t,n)=>m(e,ae,t,n)),A={};for(let e of[`worktreePath`,`branchName`,`worktreeStatus`,`worktreeNextAction`,`worktreeBaseRevision`,`parentWorktreeStatus`])A[e]=c(i[e],r(t,e),n,l);for(let e of Ce){let i=e===`agentType`?D:e===`resumeSessionId`?O:e===`promptPreview`?k:A[e];u!==`agent`&&i!==void 0&&a(n,r(t,e),`must not be set for a '${u}' task`)}u!==`agent`&&ee!==void 0&&a(n,r(t,`isolation`),`must not be set for a '${u}' task`);let j=c(i.commandPreview,r(t,`commandPreview`),n,l);u===`agent`&&j!==void 0&&a(n,r(t,`commandPreview`),`must not be set for a '${u}' task`);let M=c(i.schedule,r(t,`schedule`),n,he),N=c(i.nextFireAt,r(t,`nextFireAt`),n,h);switch(u!==`scheduled`&&M!==void 0&&a(n,r(t,`schedule`),`must not be set for a '${u}' task`),u!==`scheduled`&&N!==void 0&&a(n,r(t,`nextFireAt`),`must not be set for a '${u}' task`),u){case`agent`:{let e=E?.kind===`agent`?E:void 0,t={...T,kind:u};return s(t,`result`,e),s(t,`agentType`,D),s(t,`resumeSessionId`,O),s(t,`promptPreview`,k),s(t,`isolation`,ee),s(t,`worktreePath`,A.worktreePath),s(t,`branchName`,A.branchName),s(t,`worktreeStatus`,A.worktreeStatus),s(t,`worktreeNextAction`,A.worktreeNextAction),s(t,`worktreeBaseRevision`,A.worktreeBaseRevision),s(t,`parentWorktreeStatus`,A.parentWorktreeStatus),t}case`process`:{let e=E?.kind===`process`?E:void 0,t={...T,kind:u};return s(t,`result`,e),s(t,`commandPreview`,j),t}case`tool-invocation`:{let e=E?.kind===`tool-invocation`?E:void 0,t={...T,kind:u};return s(t,`result`,e),s(t,`commandPreview`,j),t}case`scheduled`:{let e=E?.kind===`scheduled`?E:void 0,t={...T,kind:u};return s(t,`result`,e),s(t,`commandPreview`,j),s(t,`schedule`,M),s(t,`nextFireAt`,N),t}}}const Oe=[`background_task_created`,`background_task_started`,`background_task_updated`,`background_task_completed`,`background_task_failed`,`background_task_cancelled`,`background_task_text_delta`,`background_task_tool_start`,`background_task_tool_end`,`background_task_permission_request`,`background_task_closed`,`background_task_waking`];function L(e,t,n){return l(e.taskId,r(t,`taskId`),n)}function ke(e,t,n){let i=y(e,t,n,[`type`,`taskId`,`delta`]);if(i===void 0)return;let a=L(i,t,n),o=l(i.delta,r(t,`delta`),n);if(a!==void 0&&o!==void 0)return{type:`background_task_text_delta`,taskId:a,delta:o}}function Ae(e,t,n){let i=y(e,t,n,[`type`,`taskId`,`toolName`,`firstArg`]);if(i===void 0)return;let a=L(i,t,n),o=l(i.toolName,r(t,`toolName`),n);if(a===void 0||o===void 0)return;let u={type:`background_task_tool_start`,taskId:a,toolName:o};return s(u,`firstArg`,c(i.firstArg,r(t,`firstArg`),n,l)),u}function je(e,t,n){let i=y(e,t,n,[`type`,`taskId`,`toolName`,`success`,`error`]);if(i===void 0)return;let a=L(i,t,n),o=l(i.toolName,r(t,`toolName`),n),u=f(i.success,r(t,`success`),n);if(a===void 0||o===void 0||u===void 0)return;let d={type:`background_task_tool_end`,taskId:a,toolName:o,success:u};return s(d,`error`,c(i.error,r(t,`error`),n,l)),d}function Me(e,t,n){let i=y(e,t,n,[`type`,`taskId`,`requestId`,`toolName`,`toolArgs`]);if(i===void 0)return;let a=L(i,t,n),o=l(i.requestId,r(t,`requestId`),n),s=l(i.toolName,r(t,`toolName`),n),c=I(i.toolArgs,r(t,`toolArgs`),n);if(a!==void 0&&o!==void 0&&s!==void 0&&c!==void 0)return{type:`background_task_permission_request`,taskId:a,requestId:o,toolName:s,toolArgs:c}}function Ne(e,t,n){let r=y(e,t,n,[`type`,`taskId`]);if(r===void 0)return;let i=L(r,t,n);return i===void 0?void 0:{type:`background_task_closed`,taskId:i}}function Pe(e,t,n){let i=y(e,t,n,[`type`,`taskId`,`instruction`]);if(i===void 0)return;let a=L(i,t,n);if(a===void 0)return;let o={type:`background_task_waking`,taskId:a};return s(o,`instruction`,c(i.instruction,r(t,`instruction`),n,l)),o}function Fe(e,t,n,i){let a=y(t,n,i,[`type`,`task`]);if(a===void 0)return;let o=De(a.task,r(n,`task`),i);if(o!==void 0)return{type:e,task:o}}const Ie={background_task_text_delta:ke,background_task_tool_start:Ae,background_task_tool_end:je,background_task_permission_request:Me,background_task_closed:Ne,background_task_waking:Pe};function R(e,t,n){let i=m(e?.type,Oe,r(t,`type`),n);if(i===void 0)return;let a=Ie[i];return a===void 0?Fe(i,e,t,n):a(e,t,n)}const Le=[`skill`,`plugin`],Re=[`user-slash`,`model-tool`],ze=[`inject`,`fork`],Be=[`started`,`completed`,`failed`],Ve=[`memory_candidate_extracted`,`memory_candidate_queued`,`memory_candidate_saved`,`memory_candidate_skipped`,`memory_candidate_approved`,`memory_candidate_rejected`,`memory_retrieved`],He=[`manual`,`prompt-reference`,`system`],Ue=[`active`,`observed`];function We(e,t,n){let i=y(e,t,n,[`type`,`skillName`,`source`,`invocation`,`mode`,`status`,`timestamp`,`qualifiedName`,`error`]);if(i===void 0)return;let a=m(i.type,[`skill-activation`],r(t,`type`),n),o=l(i.skillName,r(t,`skillName`),n),s=m(i.source,Le,r(t,`source`),n),u=m(i.invocation,Re,r(t,`invocation`),n),d=m(i.mode,ze,r(t,`mode`),n),f=m(i.status,Be,r(t,`status`),n),p=h(i.timestamp,r(t,`timestamp`),n);if(a===void 0||o===void 0||s===void 0||u===void 0||d===void 0||f===void 0||p===void 0)return;let g=c(i.qualifiedName,r(t,`qualifiedName`),n,l),_=c(i.error,r(t,`error`),n,l);return{type:a,skillName:o,source:s,invocation:u,mode:d,status:f,timestamp:p,...g===void 0?{}:{qualifiedName:g},..._===void 0?{}:{error:_}}}function Ge(e,t,n){let i=y(e,t,n,[`topic`,`path`,`score`,`truncated`]);if(i===void 0)return;let a=l(i.topic,r(t,`topic`),n),o=l(i.path,r(t,`path`),n),s=u(i.score,r(t,`score`),n),c=f(i.truncated,r(t,`truncated`),n);if(a!==void 0&&o!==void 0&&s!==void 0&&c!==void 0)return{topic:a,path:o,score:s,truncated:c}}function z(e,t,n){let i=y(e,t,n,[`type`,`at`,`candidateId`,`topic`,`reason`,`data`]);if(i===void 0)return;let a=m(i.type,Ve,r(t,`type`),n),o=h(i.at,r(t,`at`),n);if(a===void 0||o===void 0)return;let u={type:a,at:o};for(let e of[`candidateId`,`topic`,`reason`])s(u,e,c(i[e],r(t,e),n,l));return s(u,`data`,c(i.data,r(t,`data`),n,(e,t,n)=>b(e,t,n,x))),u}function Ke(e,t,n){let i=y(e,t,n,[`id`,`sourcePath`,`relativePath`,`originalReference`,`loadType`,`status`,`byteLength`,`loadedAt`,`lastUsedAt`]);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=l(i.sourcePath,r(t,`sourcePath`),n),u=l(i.relativePath,r(t,`relativePath`),n),f=l(i.originalReference,r(t,`originalReference`),n),p=m(i.loadType,He,r(t,`loadType`),n),g=m(i.status,Ue,r(t,`status`),n),_=d(i.byteLength,r(t,`byteLength`),n),v=h(i.loadedAt,r(t,`loadedAt`),n);if(a===void 0||o===void 0||u===void 0||f===void 0||p===void 0||g===void 0||_===void 0||v===void 0)return;let b={id:a,sourcePath:o,relativePath:u,originalReference:f,loadType:p,status:g,byteLength:_,loadedAt:v};return s(b,`lastUsedAt`,c(i.lastUsedAt,r(t,`lastUsedAt`),n,h)),b}const qe=[`active`,`satisfied`,`stopped`],Je=[`satisfied`,`max-iterations`,`cancelled`,`no-progress`],Ye=[`continue`,`satisfied`],Xe=[`pending`,`in-progress`,`done`],Ze=[`planning`,`awaiting-approval`,`executing`,`completed`];function Qe(e,t,n){let i=y(e,t,n,[`iteration`,`signal`,`reason`]);if(i===void 0)return;let a=d(i.iteration,r(t,`iteration`),n),o=m(i.signal,Ye,r(t,`signal`),n),s=l(i.reason,r(t,`reason`),n);if(a!==void 0&&o!==void 0&&s!==void 0)return{iteration:a,signal:o,reason:s}}function $e(e,t,n){let i=y(e,t,n,[`id`,`objective`,`status`,`stopReason`,`iterations`,`maxIterations`,`startedAt`,`progress`]);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=l(i.objective,r(t,`objective`),n),u=m(i.status,qe,r(t,`status`),n),f=d(i.iterations,r(t,`iterations`),n),p=d(i.maxIterations,r(t,`maxIterations`),n),g=h(i.startedAt,r(t,`startedAt`),n),v=_(i.progress,r(t,`progress`),n,Qe);if(a===void 0||o===void 0||u===void 0||f===void 0||p===void 0||g===void 0||v===void 0)return;let b={id:a,objective:o,status:u,iterations:f,maxIterations:p,startedAt:g,progress:v};return s(b,`stopReason`,c(i.stopReason,r(t,`stopReason`),n,(e,t,n)=>m(e,Je,t,n))),b}function et(e,t,n){let i=y(e,t,n,[`id`,`description`,`status`]);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=l(i.description,r(t,`description`),n),s=m(i.status,Xe,r(t,`status`),n);if(a!==void 0&&o!==void 0&&s!==void 0)return{id:a,description:o,status:s}}function tt(e,t,n){let i=y(e,t,n,[`id`,`objective`,`steps`,`phase`,`createdAt`,`approvedAt`]);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=l(i.objective,r(t,`objective`),n),u=_(i.steps,r(t,`steps`),n,et),d=m(i.phase,Ze,r(t,`phase`),n),f=h(i.createdAt,r(t,`createdAt`),n);if(a===void 0||o===void 0||u===void 0||d===void 0||f===void 0)return;let p={id:a,objective:o,steps:u,phase:d,createdAt:f};return s(p,`approvedAt`,c(i.approvedAt,r(t,`approvedAt`),n,h)),p}function nt(e,t,n){let i=y(e,t,n,[`branchId`,`checkpointId`]);if(i===void 0)return;let a=l(i.branchId,r(t,`branchId`),n),o=i.checkpointId===null?null:l(i.checkpointId,r(t,`checkpointId`),n);if(a!==void 0&&o!==void 0)return{branchId:a,checkpointId:o}}const rt=[`waiting`,`pending`,`running`,`stopped`,`expired`],it=[`loopId`,`instruction`,`useDefaultPrompt`,`createdAt`,`expiresAt`,`revision`,`generation`,`phase`,`nextAllowedAt`,`delaySeconds`,`reason`,`fallbackUsed`,`terminalReason`];function at(e,t,n){let i=y(e,t,n,it);if(!i)return;let o=l(i.loopId,r(t,`loopId`),n),u=l(i.instruction,r(t,`instruction`),n),p=c(i.useDefaultPrompt,r(t,`useDefaultPrompt`),n,f),g=h(i.createdAt,r(t,`createdAt`),n),_=h(i.expiresAt,r(t,`expiresAt`),n),v=d(i.revision,r(t,`revision`),n),b=d(i.generation,r(t,`generation`),n),x=m(i.phase,rt,r(t,`phase`),n),S=f(i.fallbackUsed,r(t,`fallbackUsed`),n),C=c(i.nextAllowedAt,r(t,`nextAllowedAt`),n,h),w=c(i.delaySeconds,r(t,`delaySeconds`),n,d),T=c(i.reason,r(t,`reason`),n,l),E=c(i.terminalReason,r(t,`terminalReason`),n,l);if(v!==void 0&&(!Number.isSafeInteger(v)||v<0)&&a(n,r(t,`revision`),`expected a non-negative safe integer`),b!==void 0&&(!Number.isSafeInteger(b)||b<0)&&a(n,r(t,`generation`),`expected a non-negative safe integer`),w!==void 0&&(w<60||w>3600)&&a(n,r(t,`delaySeconds`),`expected a delay between 60 and 3600 seconds`),o!==void 0&&!o.trim()&&a(n,r(t,`loopId`),`expected a non-empty loop ID`),u!==void 0&&!u.trim()&&a(n,r(t,`instruction`),`expected a non-empty instruction`),x===`waiting`&&C===void 0&&i.nextAllowedAt===void 0&&a(n,r(t,`nextAllowedAt`),`waiting loop needs a next wake time`),o===void 0||u===void 0||g===void 0||_===void 0||v===void 0||b===void 0||x===void 0||S===void 0)return;let D={loopId:o,instruction:u,createdAt:g,expiresAt:_,revision:v,generation:b,phase:x,fallbackUsed:S};return s(D,`useDefaultPrompt`,p),s(D,`nextAllowedAt`,C),s(D,`delaySeconds`,w),s(D,`reason`,T),s(D,`terminalReason`,E),D}const ot=[`string`,`number`,`integer`,`boolean`,`array`,`object`,`null`],st=[`minLength`,`maxLength`,`minItems`,`maxItems`,`minProperties`,`maxProperties`],ct=[`type`,`description`,`enum`,`items`,`properties`,`required`,`anyOf`,`additionalProperties`,`minimum`,`maximum`,...st,`pattern`,`format`,`default`];function lt(e,t,n){if(typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;a(n,t,`expected a string, number or boolean, received ${o(e)}`)}function ut(e,t,n){return e===null?null:lt(e,t,n)}function dt(e,t,n){if(typeof e==`number`&&Number.isInteger(e)&&e>=0)return e;a(n,t,`expected a non-negative integer, received ${o(e)}`)}function ft(e,t,n){return typeof e==`boolean`?e:B(e,t,n)}function B(e,t,n){let i=y(e,t,n,ct);if(i===void 0)return;let a={};s(a,`type`,c(i.type,r(t,`type`),n,(e,t,n)=>m(e,ot,t,n))),s(a,`description`,c(i.description,r(t,`description`),n,l)),s(a,`enum`,c(i.enum,r(t,`enum`),n,(e,t,n)=>_(e,t,n,lt))),s(a,`items`,c(i.items,r(t,`items`),n,B)),s(a,`properties`,c(i.properties,r(t,`properties`),n,(e,t,n)=>b(e,t,n,B))),s(a,`required`,c(i.required,r(t,`required`),n,v)),s(a,`anyOf`,c(i.anyOf,r(t,`anyOf`),n,(e,t,n)=>_(e,t,n,B))),s(a,`additionalProperties`,c(i.additionalProperties,r(t,`additionalProperties`),n,ft)),s(a,`minimum`,c(i.minimum,r(t,`minimum`),n,u)),s(a,`maximum`,c(i.maximum,r(t,`maximum`),n,u));for(let e of st)s(a,e,c(i[e],r(t,e),n,dt));return s(a,`pattern`,c(i.pattern,r(t,`pattern`),n,l)),s(a,`format`,c(i.format,r(t,`format`),n,l)),s(a,`default`,c(i.default,r(t,`default`),n,ut)),a}function pt(e,t,n){let i=B(e,t,n);if(i!==void 0){if(i.type!==`object`){a(n,r(t,`type`),`expected the root parameter schema to be type 'object'`);return}if(i.properties===void 0){a(n,r(t,`properties`),`expected the root parameter schema to name its properties`);return}return{...i,type:`object`,properties:i.properties}}}function mt(e,t,n){let i=y(e,t,n,[`name`,`description`,`parameters`,`outputSchema`]);if(i===void 0)return;let a=l(i.name,r(t,`name`),n),o=l(i.description,r(t,`description`),n),u=pt(i.parameters,r(t,`parameters`),n);if(a===void 0||o===void 0||u===void 0)return;let d={name:a,description:o,parameters:u};return s(d,`outputSchema`,c(i.outputSchema,r(t,`outputSchema`),n,B)),d}function V(e,t,n,i,a){return c(e[t],r(n,t),i,(e,t,n)=>_(e,t,n,a))}function ht(e,t,n,i){s(e,`pendingExecution`,c(t.pendingExecution,r(n,`pendingExecution`),i,E));for(let a of[`name`,`systemPrompt`,`sandboxSnapshotId`])s(e,a,c(t[a],r(n,a),i,l));s(e,`history`,V(t,`history`,n,i,ne)),s(e,`toolSchemas`,V(t,`toolSchemas`,n,i,mt)),s(e,`backgroundTasks`,V(t,`backgroundTasks`,n,i,De)),s(e,`backgroundTaskEvents`,V(t,`backgroundTaskEvents`,n,i,R)),s(e,`backgroundJobGroups`,V(t,`backgroundJobGroups`,n,i,be)),s(e,`backgroundJobGroupEvents`,V(t,`backgroundJobGroupEvents`,n,i,xe)),s(e,`sessionLoops`,V(t,`sessionLoops`,n,i,at)),s(e,`skillActivationEvents`,V(t,`skillActivationEvents`,n,i,We)),s(e,`memoryEvents`,V(t,`memoryEvents`,n,i,z)),s(e,`usedMemoryReferences`,V(t,`usedMemoryReferences`,n,i,Ge)),s(e,`contextReferences`,V(t,`contextReferences`,n,i,Ke)),s(e,`goal`,c(t.goal,r(n,`goal`),i,$e)),s(e,`plan`,c(t.plan,r(n,`plan`),i,tt)),s(e,`activeBranch`,c(t.activeBranch,r(n,`activeBranch`),i,nt))}const gt=2,_t=[`id`,`name`,`cwd`,`createdAt`,`updatedAt`,`messages`,`pendingExecution`,`history`,`systemPrompt`,`toolSchemas`,`backgroundTasks`,`backgroundTaskEvents`,`backgroundJobGroups`,`backgroundJobGroupEvents`,`sessionLoops`,`skillActivationEvents`,`memoryEvents`,`usedMemoryReferences`,`contextReferences`,`sandboxSnapshotId`,`goal`,`plan`,`activeBranch`];function vt(e){let t=[],n=bt(e,``,t);return n===void 0||t.length>0?{status:`corrupt`,issues:t}:{status:`valid`,record:n}}function H(e){if(typeof e!=`object`||!e)return;let t=e;if(typeof t.cwd==`string`)return t.cwd;let n=t.record;if(typeof n==`object`&&n){let e=n.cwd;if(typeof e==`string`)return e}}function U(e){return e===void 0?{}:{cwd:e}}function yt(e){let t=[],n=y(e,``,t,[`schemaVersion`,`record`]);if(n===void 0)return{status:`corrupt`,issues:t,...U(H(e))};let r=n.schemaVersion;if(typeof r!=`number`||!Number.isFinite(r))return{status:`unsupported`,schemaVersion:void 0,...U(H(e))};if(r!==1&&r!==2)return{status:`unsupported`,schemaVersion:r,...U(H(e))};r===1&&typeof n.record==`object`&&n.record!==null&&`pendingExecution`in n.record&&t.push({path:`record.pendingExecution`,message:`pending execution requires schema version 2`});let i=bt(n.record,`record`,t);return i===void 0||t.length>0?{status:`corrupt`,issues:t,...U(H(e))}:{status:`valid`,record:i}}function bt(e,t,n){let i=y(e,t,n,_t);if(i===void 0)return;let a=l(i.id,r(t,`id`),n),o=l(i.cwd,r(t,`cwd`),n),s=h(i.createdAt,r(t,`createdAt`),n),c=h(i.updatedAt,r(t,`updatedAt`),n),u=_(i.messages,r(t,`messages`),n,F);if(a===void 0||o===void 0||s===void 0||c===void 0||u===void 0)return;let d={id:a,cwd:o,createdAt:s,updatedAt:c,messages:u};return ht(d,i,t,n),d.pendingExecution&&D(d.pendingExecution,d.messages,n,r(t,`pendingExecution`)),d}const xt=/^[A-Za-z0-9][A-Za-z0-9._-]*$/;function St(){return`session_${e()}`}function Ct(e){return e.length>0&&e.length<=128&&xt.test(e)}function wt(e){if(!Ct(e))throw Error(`Invalid session id: ${JSON.stringify(e)}. A session id must be 1-128 characters of letters, digits, dot, underscore or hyphen, starting with a letter or digit.`)}const Tt=1,Et={sessionInit:`session_init`,sessionShutdown:`session_shutdown`,sessionShutdownStepError:`session_shutdown_step_error`,context:`context`,contextCompact:`context_compact`,error:`error`,historyMutation:`history_mutation`,providerRequest:`provider_request`,providerNativeRawPayload:`provider_native_raw_payload`,providerStreamRawDelta:`provider_stream_raw_delta`,providerResponseRaw:`provider_response_raw`,providerResponseNormalized:`provider_response_normalized`,structuredOutputTransport:`structured_output_transport`,providerFallback:`provider_fallback`,assistantMessageCommitted:`assistant_message_committed`,toolExecutionRequest:`tool_execution_request`,toolExecutionResult:`tool_execution_result`,toolBatchStarted:`tool_batch_started`,toolMessageCommitted:`tool_message_committed`,backgroundTaskEvent:`background_task_event`,backgroundJobGroupEvent:`background_job_group_event`,memoryEvent:`memory_event`,user:`user`,preRun:`pre_run`,textDelta:`text_delta`,assistant:`assistant`,toolCall:`tool_call`,toolResult:`tool_result`,toolBlocked:`tool_blocked`,toolDenied:`tool_denied`,serverTool:`server_tool`};function Dt(e,t){return e.event===t}const Ot=/^[0-9a-f]{64}$/;function kt(e,n){let r=t(n);if(!Ot.test(e)||e!==r)throw Error(`Invalid sha256: external JSON payloads require their exact content digest.`)}function At(e,t,r){return wt(e),kt(t,r),{kind:`external-payload`,encoding:`json`,sha256:t,byteLength:n(r),relativePath:`${e}.payloads/${t}.json`}}const W=e=>({decode:e}),G=e=>({decode:e,optional:!0}),jt=(e,t,n)=>_(e,t,n,l),K=(e,t,n)=>_(e,t,n,F),Mt=(e,t,n)=>_(e,t,n,mt),Nt=(e,t,n)=>typeof e==`string`?e:F(e,t,n),q=(e,t,n)=>{let r=d(e,t,n);return r!==void 0&&r<0&&n.push({path:t,message:`expected a non-negative integer`}),r};function J(e){if(typeof e!=`object`||!e||Array.isArray(e))return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function Y(e,t,n){if(!J(e)){n.push({path:t,message:`expected an object, received ${o(e)}`});return}for(let[i,a]of Object.entries(e))X(a,r(t,i),n);return e}const Pt=[`maxTokens`,`usedTokens`,`usedPercentage`,`remainingPercentage`];function Ft(e,t,n){let i=y(e,t,n,Pt);if(i===void 0)return;let a=u(i.maxTokens,r(t,`maxTokens`),n),o=u(i.usedTokens,r(t,`usedTokens`),n),s=u(i.usedPercentage,r(t,`usedPercentage`),n),c=u(i.remainingPercentage,r(t,`remainingPercentage`),n);if(a!==void 0&&o!==void 0&&s!==void 0&&c!==void 0)return{maxTokens:a,usedTokens:o,usedPercentage:s,remainingPercentage:c}}const It=(e,t,n)=>_(e,t,n,(e,t,n)=>{let i=y(e,t,n,[`type`,`id`]);if(i===void 0)return;let a=l(i.type,r(t,`type`),n),o=l(i.id,r(t,`id`),n);return a===void 0||o===void 0?void 0:{type:a,id:o}});function X(e,t,n,a=new WeakSet){if(e===null||typeof e==`string`||typeof e==`boolean`||typeof e==`number`&&Number.isFinite(e))return e;if(typeof e!=`object`||e instanceof Date){n.push({path:t,message:`expected a JSON-compatible value, received ${o(e)}`});return}if(!Array.isArray(e)&&!J(e)){n.push({path:t,message:`expected a JSON-compatible object, received ${o(e)}`});return}if(a.has(e)){n.push({path:t,message:`expected an acyclic JSON-compatible value`});return}if(a.add(e),Array.isArray(e))for(let r=0;r<e.length;r++)X(e[r],i(t,r),n,a);else for(let[i,o]of Object.entries(e))X(o,r(t,i),n,a);return a.delete(e),e}function Z(e,t,n,a=new WeakSet){if(!(typeof e!=`object`||!e||e instanceof Date)){if(a.has(e)){n.push({path:t,message:`expected an acyclic value`});return}if(a.add(e),Array.isArray(e))for(let r=0;r<e.length;r++)Object.hasOwn(e,r)?Z(e[r],i(t,r),n,a):n.push({path:i(t,r),message:`expected an array element, received nothing`});else for(let[i,o]of Object.entries(e))Z(o,r(t,i),n,a);a.delete(e)}}const Q=(...e)=>(t,n,r)=>m(t,e,n,r),Lt={session_init:{cwd:W(l),systemPromptLength:W(q),systemPrompt:W(l),toolSchemas:W(Mt),model:W(l),provider:W(l)},session_shutdown:{reason:W(l)},session_shutdown_step_error:{step:W(l),error:W(l)},context:{maxTokens:W(u),usedTokens:W(u),usedPercentage:W(u),remainingPercentage:W(u)},context_compact:{trigger:W(l),before:W(Ft),after:W(Ft)},error:{message:W(l),stack:W(l),historyLength:W(q)},history_mutation:{executionId:G(l),conversationId:G(l),round:G(q),batchId:G(l),usageObservationId:G(l),providerId:G(l),modelId:G(l),providerError:G(f),contextOverflow:G(f),mutation:W(Q(`append_message`)),index:W(q),message:W(F)},provider_request:{executionId:W(l),conversationId:G(l),round:W(q),provider:W(l),model:W(l),effort:G(l),forcedSummary:G(f),messages:W(K),tools:G(Mt)},provider_native_raw_payload:{executionId:W(l),conversationId:G(l),round:W(q),provider:W(l),apiSurface:G(l),payloadKind:W(Q(`request`,`response`,`stream_event`)),sequence:W(q),payload:G(X),metadata:G(Y)},provider_stream_raw_delta:{executionId:W(l),conversationId:G(l),round:W(q),sequence:W(q),delta:W(l)},provider_response_raw:{executionId:W(l),conversationId:G(l),round:W(q),effort:G(l),response:W(F),responseKind:W(l)},provider_response_normalized:{executionId:W(l),conversationId:G(l),round:W(q),response:W(F),toolCallsCount:G(q)},structured_output_transport:{executionId:W(l),conversationId:G(l),round:W(q),provider:W(l),model:W(l),mechanism:W(Q(`response_schema`,`json_object`,`none`)),provenance:W(Q(`catalog`,`vendor-default`,`undeclared`,`unverified-endpoint`)),sent:W(f),schemaInPrompt:W(f),reason:G(l)},provider_fallback:{executionId:W(l),conversationId:G(l),round:W(q),fromProvider:W(l),fromModel:W(l),toProvider:W(l),toModel:W(l),reason:W(l)},assistant_message_committed:{executionId:W(l),conversationId:G(l),round:W(q),message:W(Nt)},tool_execution_request:{executionId:W(l),conversationId:G(l),round:W(q),batchId:G(l),index:G(q),toolName:W(l),toolCallId:W(l),parameters:W(Y),ownerPath:G(It)},tool_execution_result:{executionId:W(l),conversationId:G(l),round:W(q),batchId:G(l),index:G(q),toolName:G(l),toolCallId:G(l),success:W(f),result:G(X),error:G(l),metadata:G(Y)},tool_batch_started:{executionId:W(l),conversationId:G(l),round:W(q),batchId:G(l),mode:W(Q(`parallel`,`sequential`)),maxConcurrency:W(q),requestCount:W(q),tools:W(jt)},tool_message_committed:{executionId:W(l),conversationId:G(l),round:W(q),batchId:G(l),index:G(q),message:W(F)},background_task_event:{backgroundEventType:G(l),backgroundEvent:G(R),data:G(R),taskId:G(l),originToolCallId:G(l)},background_job_group_event:{backgroundJobGroupEvent:G(xe),data:G(xe)},memory_event:{memoryEvent:G(z),data:G(z)},user:{content:W(l)},pre_run:{historyLength:W(q),historyChars:W(q),historyEstTokens:W(q),input:W(l),history:W(K),model:W(l),provider:W(l),maxTokens:W(u),nativeWebSearchSupported:W(f),nativeWebSearchEnabled:W(f),nativeWebFetchSupported:W(f),nativeWebFetchEnabled:W(f)},text_delta:{delta:W(l)},assistant:{content:W(l),historyLength:W(q),estimatedChars:W(q),history:W(K),historyStructure:W((e,t,n)=>_(e,t,n,(e,t,n)=>{if(typeof e!=`object`||!e||Array.isArray(e)){n.push({path:t,message:`expected an object, received ${o(e)}`});return}let i=e;m(i.role,[`user`,`assistant`,`system`,`tool`],r(t,`role`),n),q(i.contentLength,r(t,`contentLength`),n),f(i.hasToolCalls,r(t,`hasToolCalls`),n),jt(i.toolCallNames,r(t,`toolCallNames`),n),i.metadata!==void 0&&Y(i.metadata,r(t,`metadata`),n);for(let e of Object.keys(i))[`role`,`contentLength`,`hasToolCalls`,`toolCallNames`,`metadata`].includes(e)||n.push({path:r(t,e),message:`unknown history structure field`});return i}))},tool_call:{tool:W(l),args:W(Y)},tool_result:{tool:W(l),success:W(f),dataChars:W(q),truncated:W(f)},tool_blocked:{tool:W(l),reason:W(l)},tool_denied:{tool:W(l),reason:W(l)},server_tool:{tool:W(l)}};var $=class extends Error{code;issues;schemaVersion;constructor(e,t,n={}){super(`Session log decode failed: ${e}${t[0]?` at ${t[0].path}: ${t[0].message}`:``}`,{cause:n.cause}),this.name=`SessionLogDecodeError`,this.code=e,this.issues=t,n.schemaVersion!==void 0&&(this.schemaVersion=n.schemaVersion)}};const Rt=new Set(Object.values(Et));function zt(e,t={}){if(!Array.isArray(e))throw new $(`INVALID_EVENT`,[{path:`entries`,message:`expected an array, received ${o(e)}`}]);let n=[],r=[],i;for(let[a,o]of e.entries()){let e=Bt(o,t.lineNumbers?.[a]===void 0?`[${a}]`:`line ${t.lineNumbers[a]}`,n);e.entry!==void 0&&r.push(e.entry),i??=e.unsupported}if(n.length>0)throw new $(i===void 0?`INVALID_EVENT`:`UNSUPPORTED_VERSION`,n,{schemaVersion:i});return r}function Bt(e,t,n){if(!J(e))return n.push({path:t,message:`expected an object, received ${o(e)}`}),{};let r=n.length;if(Z(e,t,n),n.length!==r)return{};let i=Vt(e,t,n);return i.event===void 0?{unsupported:i.unsupported}:(Ht(e,i.event,t,i.output,n),{entry:i.output,unsupported:i.unsupported})}function Vt(e,t,n){let i={schemaVersion:1},a;if(e.schemaVersion!==1){let i=e.schemaVersion;typeof i==`number`&&Number.isSafeInteger(i)&&i>=0?(a=i,n.push({path:r(t,`schemaVersion`),message:`unsupported schema version`})):n.push({path:r(t,`schemaVersion`),message:`expected schema version 1, received ${o(i)}`})}let s=h(e.timestamp,r(t,`timestamp`),n);s!==void 0&&(i.timestamp=s);let c=l(e.sessionId,r(t,`sessionId`),n);c===``&&n.push({path:r(t,`sessionId`),message:`expected a non-empty session ID`}),c!==void 0&&(i.sessionId=c);let u=e.event;return typeof u!=`string`||!Rt.has(u)?(n.push({path:r(t,`event`),message:`expected a declared event name, received ${o(u)}`}),{output:i,unsupported:a}):(i.event=u,{output:i,event:u,unsupported:a})}function Ht(e,t,n,i,a){let o=Lt[t];for(let[t,s]of Object.entries(o)){if(!Object.hasOwn(e,t)&&s.optional)continue;let o=s.decode(e[t],r(n,t),a);o!==void 0&&(i[t]=o)}for(let s of Object.keys(e)){if([`schemaVersion`,`timestamp`,`sessionId`,`event`,...Object.keys(o)].includes(s))continue;if(t!==`server_tool`){a.push({path:r(n,s),message:`unknown event payload field`});continue}let c=X(e[s],r(n,s),a);c!==void 0&&(i[s]=c)}Ut(e,t,n,a)}function Ut(e,t,n,r){t===`background_task_event`&&e.backgroundEvent===void 0&&e.data===void 0&&r.push({path:n,message:`expected a background task event payload`}),t===`background_job_group_event`&&e.backgroundJobGroupEvent===void 0&&e.data===void 0&&r.push({path:n,message:`expected a background job group event payload`}),t===`memory_event`&&e.memoryEvent===void 0&&e.data===void 0&&r.push({path:n,message:`expected a memory event payload`})}var Wt=class extends Error{code;metadata;constructor(e,t,n={},r){super(t,r===void 0?void 0:{cause:r}),this.name=`SessionLogPayloadResolutionError`,this.code=e,this.metadata=n}};function Gt(e,t){switch(t){case`locked`:return`Session ${e} is sealed and locked, so it was not overwritten; unlock to open it.`;case`does-not-open`:return`Session ${e} holds a sealed record that does not open for this session, so it was not overwritten.`;case`changed`:return`Session ${e} changed on disk since this process last wrote or read it, so it was not overwritten.`;case`unreadable`:return`Session ${e} could not be read, so it was not overwritten.`;case`newer-format`:return`Session ${e} was sealed by a newer build, so it was not overwritten.`;case`no-keys`:return`Session ${e} is sealed and this process has no keys that open it, so it was not overwritten.`}}var Kt=class extends Error{code=`SESSION_SEALED_UNREADABLE`;sessionId;reason;constructor(e,t){super(Gt(e,t)),this.name=`SealedSessionUnreadableError`,this.sessionId=e,this.reason=t}};function qt(e){return typeof e==`object`&&!!e&&e.code===`SESSION_SEALED_UNREADABLE`}function Jt(e){return{...e===void 0?{}:{pid:e.pid},self:!0,liveness:`alive`}}function Yt(e){let t=e.owner;return{...t===void 0?{}:{pid:t.pid},...t?.hostname===void 0?{}:{hostname:t.hostname},...t?.startTime===void 0?{}:{startTime:t.startTime},self:e.self,liveness:e.liveness}}function Xt(e){let t=[e.pid===void 0?`a process this one cannot read`:`pid ${e.pid}`];return e.hostname!==void 0&&t.push(`on ${e.hostname}`),e.startTime!==void 0&&t.push(`started ${e.startTime}`),t.join(` `)}function Zt(e,t){let n=t.holder??Jt(t.ownProcess);if(n.lent===!0)return n.self?`Session ${e} is lent to a subagent this process started, which has not taken it up yet; try again once that subagent has finished.`:`Session ${e} is lent to a subagent this process started (${Xt(n)}), which writes it; try again once that subagent has finished.`;if(n.self)return`Session ${e} is already open in this process${t.ownProcess===void 0?``:` (pid ${t.ownProcess.pid})`}: a session running or still closing here. Try again once that has ended.`;let r=t.releaseCommand?.(e);if(n.liveness===`gone`)return`Session ${e} is held by a process that is no longer running (${Xt(n)}), and another process is releasing it now. Try again${r===void 0?``:`, or run: ${r}`}.`;let i=`Session ${e} ${n.liveness===`alive`?`is open in another process`:`may be open in another process`} (${Xt(n)}), which writes it; close it there first.`+(r===void 0?``:` If that process is no longer running, release the session with: ${r}`);return t.ownProcess?.windows===!0&&(i+=` On Windows, a process that crashed may still be shown as holding it.`),i}function Qt(e,t,n){switch(t){case`has_writer`:return Zt(e,n);case`released`:return n.lease===!0?`Session ${e} was not opened: the lease it was handed over with has ended.`:`Session ${e} was not saved: the claim it was saved under has ended or no longer holds its writer marker.`}}var $t=class extends Error{code=`SESSION_CLAIMED`;sessionId;reason;holder;closing;constructor(e,t,n={}){super(Qt(e,t,n)),this.name=`SessionClaimedError`,this.sessionId=e,this.reason=t,this.holder=t===`has_writer`?n.holder??Jt(n.ownProcess):void 0,this.closing=this.holder?.self===!0&&this.holder.lent!==!0}};function en(e){return typeof e==`object`&&!!e&&e.code===`SESSION_CLAIMED`}var tn=class{#e=new Map;#t=new Map;#n;#r;constructor(e={},t){this.#n=e,this.#r=t}has(e){return this.#e.has(e)}isLent(e){return this.#t.has(e)}refusal(e,t,n={}){return new $t(e,t,{...this.#n,...this.#r===void 0?{}:{ownProcess:this.#r},...n})}assertUnclaimed(e){if(this.#e.has(e)||this.#t.has(e))throw this.#i(e)}#i(e){let t=this.#t.get(e);if(t===void 0)return this.refusal(e,`has_writer`);let n=t.holder(),r=n===void 0||n.self?Jt(this.#r):Yt(n);return this.refusal(e,`has_writer`,{holder:{...r,lent:!0}})}open(e,t,n,r){let i={marker:n},a=()=>{this.#e.get(e)===i&&this.#e.delete(e),n?.release()};t&&this.#e.set(e,i);let o;try{o=r()}catch(e){throw a(),e}this.#e.set(e,i);let s=!1,c=Object.freeze({outcome:o,release:()=>{s||(s=!0,a())}});return i.handle=c,c}markerForSave(e,t){let n=this.#e.get(e);if(t!==void 0){if(n===void 0||n.handle!==t||n.marker!==void 0&&!n.marker.holds())throw this.refusal(e,`released`);return n.marker}if(n!==void 0||this.#t.has(e))throw this.#i(e)}markerForWrite(e){if(this.#t.has(e))throw this.#i(e);let t=this.#e.get(e);if(t!==void 0){if(t.marker===void 0||!t.marker.holds())throw this.refusal(e,`has_writer`);return t.marker}}lend(e,t){let n={holder:t};return this.#t.set(e,n),()=>{this.#t.get(e)===n&&this.#t.delete(e)}}};export{_t as _,Kt as a,yt as b,$ as c,Et as d,Tt as f,Ct as g,St as h,Yt as i,zt as l,wt as m,tn as n,qt as o,Dt as p,en as r,Wt as s,$t as t,At as u,gt as v,vt as y};
2
+ //# sourceMappingURL=session-claims-D-Azmxq6.js.map