@neuralnomads/codenomad-dev 0.20.1-dev-20261019-e56b286a → 0.20.1-dev-20261019-e32d9331

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.
Files changed (72) hide show
  1. package/dist/automation/browser-page.js +97 -0
  2. package/dist/automation/browser-page.js.map +1 -0
  3. package/dist/automation/browser-page.test.js +119 -0
  4. package/dist/automation/browser-page.test.js.map +1 -0
  5. package/dist/automation/page/actions.js +107 -83
  6. package/dist/automation/page/actions.js.map +1 -1
  7. package/dist/automation/page/input.js +130 -0
  8. package/dist/automation/page/input.js.map +1 -0
  9. package/dist/automation/page/input.test.js +71 -0
  10. package/dist/automation/page/input.test.js.map +1 -0
  11. package/dist/automation/page/keys.js +99 -0
  12. package/dist/automation/page/keys.js.map +1 -0
  13. package/dist/automation/page/keys.test.js +32 -0
  14. package/dist/automation/page/keys.test.js.map +1 -0
  15. package/dist/automation/page/parse.js +79 -0
  16. package/dist/automation/page/parse.js.map +1 -0
  17. package/dist/automation/page/protocol.js +143 -0
  18. package/dist/automation/page/protocol.js.map +1 -0
  19. package/dist/developer-cdp.js +7 -10
  20. package/dist/developer-cdp.js.map +1 -1
  21. package/dist/developer-cdp.test.js +49 -15
  22. package/dist/developer-cdp.test.js.map +1 -1
  23. package/dist/opencode/automation/desktop-plugin.js +10 -6
  24. package/dist/opencode/automation/desktop-plugin.js.map +1 -1
  25. package/dist/opencode/automation-plugin.js +33 -41
  26. package/dist/opencode/automation-plugin.js.map +1 -1
  27. package/dist/opencode/automation-plugin.test.js +42 -1
  28. package/dist/opencode/automation-plugin.test.js.map +1 -1
  29. package/dist/plugins/automation/plugin.mjs +230 -40
  30. package/dist/plugins/missions/plugin.mjs +21 -0
  31. package/dist/server/routes/automation-plugin.js +7 -5
  32. package/dist/server/routes/automation-plugin.js.map +1 -1
  33. package/dist/server/routes/automation-plugin.test.js +17 -4
  34. package/dist/server/routes/automation-plugin.test.js.map +1 -1
  35. package/package.json +1 -1
  36. package/public/assets/{App-z4rDGruy.js → App-DZMdkLQJ.js} +29 -29
  37. package/public/assets/App-DZMdkLQJ.js.map +1 -0
  38. package/public/assets/{FilesPanel-BR_6pmtI.js → FilesPanel-CgFgL4q7.js} +3 -3
  39. package/public/assets/FilesPanel-CgFgL4q7.js.map +1 -0
  40. package/public/assets/{StatusTab-7nJ4mv4I.js → StatusTab-CaQz8RwN.js} +2 -2
  41. package/public/assets/{StatusTab-7nJ4mv4I.js.map → StatusTab-CaQz8RwN.js.map} +1 -1
  42. package/public/assets/{client-state-CdvbHQef.js → client-state-DikXRKzG.js} +2 -2
  43. package/public/assets/{client-state-CdvbHQef.js.map → client-state-DikXRKzG.js.map} +1 -1
  44. package/public/assets/{client-state-Dy45bMLT.js → client-state-kpF1cbUu.js} +2 -2
  45. package/public/assets/{client-state-Dy45bMLT.js.map → client-state-kpF1cbUu.js.map} +1 -1
  46. package/public/assets/client-state-owner-commands-BMJJQYT-.js +2 -0
  47. package/public/assets/{client-state-owner-commands-CNRMpxmP.js.map → client-state-owner-commands-BMJJQYT-.js.map} +1 -1
  48. package/public/assets/{diff-viewer-DidBGGSg.js → diff-viewer-iFIylM56.js} +2 -2
  49. package/public/assets/{diff-viewer-DidBGGSg.js.map → diff-viewer-iFIylM56.js.map} +1 -1
  50. package/public/assets/{main-2ULMhXOZ.js → main-B0L3f7sD.js} +52 -52
  51. package/public/assets/main-B0L3f7sD.js.map +1 -0
  52. package/public/assets/{markdown-B2vmXRgM.js → markdown-Bh1QlJgA.js} +2 -2
  53. package/public/assets/{markdown-B2vmXRgM.js.map → markdown-Bh1QlJgA.js.map} +1 -1
  54. package/public/assets/{opener-startup-state-settings-card-Bj8IDBCX.js → opener-startup-state-settings-card-mzh22bq8.js} +2 -2
  55. package/public/assets/{opener-startup-state-settings-card-Bj8IDBCX.js.map → opener-startup-state-settings-card-mzh22bq8.js.map} +1 -1
  56. package/public/assets/{startup-state-settings-card-DAkduiFk.js → startup-state-settings-card-B1UMzsQK.js} +2 -2
  57. package/public/assets/{startup-state-settings-card-DAkduiFk.js.map → startup-state-settings-card-B1UMzsQK.js.map} +1 -1
  58. package/public/assets/{startup-state-settings-view-CQ6mkdWM.js → startup-state-settings-view-nZNbhUhQ.js} +2 -2
  59. package/public/assets/{startup-state-settings-view-CQ6mkdWM.js.map → startup-state-settings-view-nZNbhUhQ.js.map} +1 -1
  60. package/public/assets/{terminal-square-DeHY-xG8.js → terminal-square-CdJ4Gt6Y.js} +2 -2
  61. package/public/assets/{terminal-square-DeHY-xG8.js.map → terminal-square-CdJ4Gt6Y.js.map} +1 -1
  62. package/public/assets/{tool-call-DPmRZgHK.js → tool-call-C9ux0znU.js} +4 -4
  63. package/public/assets/{tool-call-DPmRZgHK.js.map → tool-call-C9ux0znU.js.map} +1 -1
  64. package/public/assets/{unified-picker-Dpt39UfN.js → unified-picker-DOP29DIt.js} +2 -2
  65. package/public/assets/{unified-picker-Dpt39UfN.js.map → unified-picker-DOP29DIt.js.map} +1 -1
  66. package/public/index.html +1 -1
  67. package/public/sw.js +1 -1
  68. package/public/sw.js.map +1 -1
  69. package/public/assets/App-z4rDGruy.js.map +0 -1
  70. package/public/assets/FilesPanel-BR_6pmtI.js.map +0 -1
  71. package/public/assets/client-state-owner-commands-CNRMpxmP.js +0 -2
  72. package/public/assets/main-2ULMhXOZ.js.map +0 -1
@@ -1,2 +1,2 @@
1
- import{e as ht}from"./git-diff-vendor-CINvIzmQ.js";import{loadNativeClientState as Pe,loadNativeClientStatePartition as je,commitNativeClientStatePartitions as Ee,saveNativeClientState as xe,setNativeRestoreEnabled as Ft,clearNativeClientState as Te}from"./client-state-Dy45bMLT.js";const G=t=>!!t&&typeof t=="object"&&!Array.isArray(t),K=(t,e)=>typeof t=="string"&&t.length<=e,Ht=t=>K(t,512)&&t.trim().length>0&&!["__proto__","constructor","prototype"].includes(t),q=t=>typeof t=="number"&&Number.isFinite(t)&&t>=0;function Kt(t){if(!Array.isArray(t))return;const e=[],n=new Set;for(const r of t){if(!G(r)||!Ht(r.id)||n.has(r.id)||!K(r.title,32768)||!(r.parentId===null||Ht(r.parentId))||!K(r.projectID,512)||!G(r.location)||!K(r.location.directory,4096)||!r.location.directory||!(r.location.workspaceID===void 0||K(r.location.workspaceID,512))||!G(r.time)||!q(r.time.created)||!q(r.time.updated)||!(r.time.archived===void 0||q(r.time.archived))||!K(r.agent,512)||!G(r.model)||!K(r.model.providerId,512)||!K(r.model.modelId,512)||!q(r.cost)||!G(r.tokens)||!G(r.tokens.cache)||![r.tokens.input,r.tokens.output,r.tokens.reasoning,r.tokens.cache.read,r.tokens.cache.write].every(q)||!(r.subpath===void 0||K(r.subpath,4096)))return;n.add(r.id),e.push({id:r.id,title:r.title,parentId:r.parentId,projectID:r.projectID,location:{directory:r.location.directory,...r.location.workspaceID===void 0?{}:{workspaceID:r.location.workspaceID}},time:{created:r.time.created,updated:r.time.updated,...r.time.archived===void 0?{}:{archived:r.time.archived}},agent:r.agent,model:{providerId:r.model.providerId,modelId:r.model.modelId},cost:r.cost,tokens:{input:r.tokens.input,output:r.tokens.output,reasoning:r.tokens.reasoning,cache:{read:r.tokens.cache.read,write:r.tokens.cache.write}},...r.subpath===void 0?{}:{subpath:r.subpath}})}return e}function Vn(t){return Kt(t)}const Lt=512,st=1024,At=4096,re=256,Q=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),Ne=t=>t!=="__proto__"&&t!=="constructor"&&t!=="prototype";function R(t,e,n=!1){if(!(typeof t!="string"||t.length>e)&&!(!n&&t.trim().length===0))return t}function oe(t){let e="";for(let n=0;n<t.length;n+=32768)e+=String.fromCharCode(...t.subarray(n,n+32768));return btoa(e)}const ie=t=>Uint8Array.from(atob(t),e=>e.charCodeAt(0));function ke(t){if(typeof t!="string"||t.length%4!==0||!/^[A-Za-z0-9+/]*={0,2}$/.test(t))return!1;try{return ie(t),!0}catch{return!1}}const se=t=>{var e;return typeof t=="string"?(e=t.match(/^data:[^;,]+;base64,([A-Za-z0-9+/]*={0,2})$/))==null?void 0:e[1]:void 0};function ve(t,e){const n=se(e)??t,r=n instanceof Uint8Array?oe(n):n;return ke(r)?r:void 0}function Gt(t){if(!(!Q(t)||!Number.isSafeInteger(t.line)||Number(t.line)<0)&&!(!Number.isSafeInteger(t.char)||Number(t.char)<0))return{line:Number(t.line),char:Number(t.char)}}function Ce(t,e){if(!Q(t))return;if(t.type==="text")return typeof t.value=="string"?{type:"text",value:t.value}:void 0;if(t.type==="agent"){const o=R(t.name,st);return o===void 0?void 0:{type:"agent",name:o}}if(t.type==="skill"){const o=R(t.id,Lt),a=R(t.name,st);return o===void 0||a===void 0?void 0:{type:"skill",id:o,name:a}}const n=R(t.path,At);if(t.type==="file"){const o=R(t.mime,re);if(n===void 0||o===void 0)return;const a=se(e)??t.data,c=ve(t.data,e);return a!==void 0&&c===void 0?void 0:c===void 0?{type:"file",path:n,mime:o}:{type:"file",path:n,mime:o,data:c}}if(t.type!=="symbol"||!Q(t.range))return;const r=R(t.name,st),s=Gt(t.range.start),i=Gt(t.range.end);if(!(n===void 0||r===void 0||!Number.isSafeInteger(t.kind)||!s||!i))return{type:"symbol",path:n,name:r,kind:Number(t.kind),range:{start:s,end:i}}}function Re(t){if(!Q(t)||!["file","text","symbol","agent","skill"].includes(String(t.type)))return;const e=R(t.id,Lt),n=R(t.display,st),r=R(t.filename,At),s=R(t.mediaType,re),i=Ce(t.source,t.url),o=typeof t.url=="string"&&!t.url.startsWith("data:")?t.url:"",a=R(o,At,!0);if(!(!e||!n||!r||!s||!i||i.type!==t.type||a===void 0))return{id:e,type:t.type,display:n,url:a,filename:r,mediaType:s,source:i}}function zt(t,e,n=[]){if(!Q(t))return null;const r=Object.create(null),s=[...new Set(n)],i=new Set(s),o=[...s.flatMap(a=>Object.prototype.hasOwnProperty.call(t,a)?[[a,t[a]]]:[]),...Object.entries(t).filter(([a])=>!i.has(a))];for(const[a,c]of o){if(!Ne(a)||!a||a.length>Lt||!Array.isArray(c))continue;const g=[];for(const S of c){const A=Re(S);A&&g.push(A)}g.length&&(r[a]=g)}return{attachments:r,drafts:{...e}}}function Un(t,e,n=[]){const r=zt(e,t,n);if(!r||Object.entries(e).some(([s,i])=>{var o;return i.length!==(((o=r.attachments[s])==null?void 0:o.length)??0)}))throw new Error("Draft attachments could not be persisted safely");return r}function Yn(t){let e,n=t.url;if(t.source.type==="file")try{const r=t.source.data===void 0?void 0:ie(t.source.data);e={type:"file",path:t.source.path,mime:t.source.mime,data:r},!n&&t.source.data&&(n=`data:${t.source.mime};base64,${t.source.data}`)}catch{return null}else e=t.source,t.source.type==="text"&&!n&&(n=`data:text/plain;base64,${oe(new TextEncoder().encode(t.source.value))}`);return{...t,url:n,source:e}}const Me=()=>({bytes:16*1024*1024,entries:2e5,indexes:16}),It=new WeakMap,Ke=new Set(["user","assistant","system","synthetic","skill","shell","compaction","idle","agent-switched","model-switched","location-switched"]),at=t=>!!t&&typeof t=="object"&&!Array.isArray(t),Z=(t,e)=>typeof t=="string"&&t.length>0&&t.length<=e,rt=(t,e=0)=>Number.isSafeInteger(t)&&Number(t)>=e;function $n(t){return t.entries.every(e=>!e.tools||e.toolName!==void 0)}function ae(t){if(!at(t))return;if(It.has(t))return t;if(t.format!==1||!Z(t.directory,4096)||!Z(t.projectID,256)||t.revert!==void 0&&!Z(t.revert,256)||!Array.isArray(t.entries)||t.entries.length>2e5||!Array.isArray(t.checkpoints)||t.checkpoints.length>512)return;const e=[],n=[],r=new Set;let s=-1,i=-1;for(const c of t.entries){if(!at(c)||!Z(c.id,256)||r.has(c.id)||!rt(c.seq)||c.seq<=s||typeof c.type!="string"||!Ke.has(c.type)||!rt(c.tools)||!rt(c.reasoning)||c.toolName!==void 0&&(typeof c.toolName!="string"||c.toolName.length>256))return;r.add(c.id),s=c.seq,e.push(Object.freeze({id:c.id,seq:s,type:c.type,tools:c.tools,reasoning:c.reasoning,...c.toolName===void 0?{}:{toolName:c.toolName}}))}for(const c of t.checkpoints){if(!at(c)||c.after!==i||!rt(c.through)||c.through<=i||typeof c.digest!="string"||!/^[a-f0-9]{64}$/.test(c.digest))return;n.push(Object.freeze({after:i,through:c.through,digest:c.digest})),i=c.through}if(!n.length||s>i)return;const o={format:1,directory:t.directory,projectID:t.projectID,...t.revert===void 0?{}:{revert:t.revert},entries:e,checkpoints:n},a=new TextEncoder().encode(JSON.stringify(o)).byteLength;if(!(a>16*1024*1024))return Object.freeze(e),Object.freeze(n),Object.freeze(o),It.set(o,a),o}function Le(t,e,n){if(!at(t))return;const r=Object.create(null),s=[n,...Object.keys(t).filter(i=>i!==n)].filter(i=>!!i);for(const i of s){if(!e.indexes||!e.entries||!e.bytes)break;if(!Z(i,256)||["__proto__","constructor","prototype"].includes(i))continue;const o=ae(t[i]);if(!o)continue;const a=It.get(o);a>e.bytes||o.entries.length>e.entries||(r[i]=o,e.bytes-=a,e.entries-=o.entries.length,e.indexes--)}return Object.keys(r).length?r:void 0}const ce=32,ze=64,De=24,Be=96,Xe=256,Ve=256,Ue=256,le=256,bt=4096,ct=512,Ye=4096,de=32*1024,$e=1024,Fe=96*1024,He=256,Jt=1024,Ge="__no_session_draft__";function fe(){return{remaining:Fe,scrollSnapshotsRemaining:He,outlines:Me()}}const L=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),ue=t=>t!=="__proto__"&&t!=="constructor"&&t!=="prototype";function k(t,e,n,r=!1){if(!(typeof t!="string"||t.length>e||t.length>n.remaining)&&!(!r&&t.trim().length===0))return n.remaining-=t.length,t}const D=(t,e,n)=>typeof t=="number"&&Number.isFinite(t)&&t>=e&&t<=n?t:void 0;function lt(t,e,n,r,s=()=>!0,i=()=>{},o=[]){if(!L(t))return null;const a=Object.create(null);let c=0;const g=[...new Set(o)],S=new Set(g),A=[...g.flatMap(y=>Object.prototype.hasOwnProperty.call(t,y)?[[y,t[y]]]:[]),...Object.entries(t).filter(([y])=>!S.has(y))];for(const[y,b]of A){if(c>=e)break;if(!ue(y)||!s(b))continue;const j=k(y,le,n),f=r(b);j===void 0||f===void 0||(a[j]=f,c+=1,i())}return a}function pe(t,e,n,r,s=[]){return lt(t,e,r,i=>k(i,n,r,!0),void 0,void 0,s)}function Je(t,e){if(!L(t))return;const n=D(t.scrollTop,0,1e9),r=D(t.updatedAt,0,Number.MAX_SAFE_INTEGER);if(n===void 0||r===void 0||typeof t.atBottom!="boolean")return;const s={scrollTop:n,atBottom:t.atBottom,updatedAt:r},i=D(t.scrollRatio,0,1),o=D(t.maxScrollTop,0,1e9),a=D(t.anchorOffset,-1e6,1e6),c=t.anchorKey===void 0?void 0:k(t.anchorKey,$e,e);i!==void 0&&(s.scrollRatio=i),o!==void 0&&(s.maxScrollTop=o),c!==void 0&&(s.anchorKey=c),a!==void 0&&(s.anchorOffset=a),(t.followModeType==="following"||t.followModeType==="escaped")&&(s.followModeType=t.followModeType),typeof t.windowIsLatest=="boolean"&&(s.windowIsLatest=t.windowIsLatest);const g=t.windowCursor===void 0?void 0:k(t.windowCursor,Jt,e);if(g!==void 0&&(s.windowCursor=g),Array.isArray(t.newerCursors)){const S=[];for(const A of t.newerCursors.slice(-32)){if(A===null||A===""){S.push(null);continue}const y=k(A,Jt,e);y!==void 0&&S.push(y)}S.length>0&&(s.newerCursors=S)}return s}function We(t,e){if(!Array.isArray(t))return null;const n=[],r=new Set;for(const s of t.slice(0,Ue)){if(typeof s!="string"||r.has(s))continue;const i=k(s,ct,e);i!==void 0&&(r.add(i),n.push(i))}return n}function qe(t,e,n,r){const s=e.priorityDrafts,i=Object.fromEntries(Object.entries(t.drafts??{}).filter(([m])=>!Object.prototype.hasOwnProperty.call(s,m))),o=pe(i,Math.max(0,De-Object.keys(s).length),de,n),a=o?{...s,...o}:null,c=Math.min(Be,n.scrollSnapshotsRemaining),g=lt(t.scrollSnapshots??{},c,n,m=>Je(m,n),void 0,()=>{n.scrollSnapshotsRemaining-=1}),S=lt(t.unseenIdleSince??{},Xe,n,m=>D(m,0,Number.MAX_SAFE_INTEGER)),A=lt(t.generationRecovery??{},Ve,n,m=>m,m=>m==="working"||m==="interrupted"),y=t.expandedSessionIds===void 0?void 0:We(t.expandedSessionIds,n);if(!a||!g||!S||!A||y===null)return null;const b=Object.fromEntries(Object.entries(t.attachments??{}).filter(([m])=>!e.prioritySessionIds.includes(m))),j=zt(b,a);if(!j)return null;const f={kind:"workspace",folder:e.folder,drafts:j.drafts,attachments:{...e.priorityAttachments,...j.attachments},scrollSnapshots:g,unseenIdleSince:S,generationRecovery:A};y!==void 0&&(f.expandedSessionIds=y);const h=Le(t.outlineIndexes,n.outlines,e.activeSessionId);h&&(f.outlineIndexes=h);const l=Kt(t.sessionCatalog);l&&(f.sessionCatalog=l),Number.isInteger(t.occurrence)&&Number(t.occurrence)>=0&&Number(t.occurrence)<ce&&(f.occurrence=Number(t.occurrence));const d=t.projectName===void 0?void 0:k(t.projectName,bt,n),p=t.binaryPath===void 0?void 0:k(t.binaryPath,bt,n);return d!==void 0&&(f.projectName=d),p!==void 0&&(f.binaryPath=p),e.activeParentSessionId!==void 0&&(f.activeParentSessionId=e.activeParentSessionId),e.activeSessionId!==void 0&&(f.activeSessionId=e.activeSessionId),f}function Ze(t,e){if(!L(t))return null;const n=t.kind??t.type;if(n==="sidecar"){const a=k(t.sidecarId,ct,e);return a===void 0?null:{kind:n,sidecarId:a}}if(n!=="workspace"&&n!=="instance"||!["drafts","attachments","scrollSnapshots","unseenIdleSince","generationRecovery"].every(a=>L(t[a]??{})))return null;const r=k(t.folder,bt,e);if(r===void 0)return null;const s=t.activeParentSessionId===void 0?void 0:k(t.activeParentSessionId,ct,e),i=t.activeSessionId===void 0?void 0:k(t.activeSessionId,ct,e),o=[i,Ge].filter(a=>!!a);return{kind:"workspace",value:t,folder:r,activeParentSessionId:s,activeSessionId:i,prioritySessionIds:o,priorityDrafts:Object.create(null),priorityAttachments:Object.create(null)}}function Qe(t,e){const n=t.value.drafts??{};if(!L(n))return;for(const o of t.prioritySessionIds){if(!ue(o)||!Object.prototype.hasOwnProperty.call(n,o))continue;const a=e.remaining,c=k(o,le,e),g=k(n[o],de,e,!0);if(c===void 0||g===void 0){e.remaining=a;continue}t.priorityDrafts[c]=g}const r=t.value.attachments??{};if(!L(r))return;const s=Object.fromEntries(t.prioritySessionIds.filter(o=>Object.prototype.hasOwnProperty.call(r,o)).map(o=>[o,r[o]])),i=zt(s,t.priorityDrafts,t.prioritySessionIds);i&&(t.priorityDrafts=i.drafts,t.priorityAttachments=i.attachments)}function Ot(t){return ye(t,fe())}function ye(t,e){if(!L(t)||!Array.isArray(t.tabs)||!Number.isInteger(t.activeTabIndex))return null;const n=Number(t.activeTabIndex),r=t.tabs.slice(0,ce),s=new Map,i=new Map,o=f=>{const h=Ze(r[f],e);h&&s.set(f,h)},a=(f,h)=>{const l=s.get(f);if(!l)return;const d=l.kind==="sidecar"?l:qe(l.value,l,e);d&&i.set(f,d)};r.forEach((f,h)=>o(h));const c=[n,...r.map((f,h)=>h).filter(f=>f!==n)];for(const f of c){const h=s.get(f);(h==null?void 0:h.kind)==="workspace"&&Qe(h,e)}r[n]!==void 0&&a(n),r.forEach((f,h)=>{h!==n&&a(h)});const S=r.flatMap((f,h)=>{const l=s.get(h);return l?[{originalIndex:h,identity:l}]:[]}).flatMap(({originalIndex:f})=>{const h=i.get(f);return h?[{originalIndex:f,tab:h}]:[]});if(t.tabs.length&&!S.length)return null;const A=S.map(({tab:f})=>f),y=S.findIndex(({originalIndex:f})=>f===n),b=S.findIndex(({originalIndex:f})=>f>n),j=A.length?y>=0?y:b>=0?b:A.length-1:-1;return{tabs:A,activeTabIndex:j,...t.homeActive===!0?{homeActive:!0}:{}}}function Dt(t){if(!L(t)||t.version!==void 0&&t.version!==1||!Number.isSafeInteger(t.revision)||Number(t.revision)<0)return null;const e=D(t.savedAt,0,Number.MAX_SAFE_INTEGER);if(e===void 0)return null;const n=fe(),r=t.session===null?null:ye(t.session,n);if(t.session!==null&&!r)return null;const s=pe(t.layout,ze,Ye,n);return s?{version:1,revision:Number(t.revision),savedAt:e,layout:s,session:r}:null}const tn=new TextEncoder;function _t(t){return Array.isArray(t)?t.map(_t):typeof t!="object"||t===null?t:Object.fromEntries(Object.keys(t).sort().map(e=>[e,_t(t[e])]))}function U(t){const e=JSON.stringify(_t(t));if(e===void 0)throw new TypeError("Client state partition must be JSON-serializable");return e}async function mt(t){const e=await crypto.subtle.digest("SHA-256",tn.encode(t));return Array.from(new Uint8Array(e),n=>n.toString(16).padStart(2,"0")).join("")}const Wt=new WeakMap;function en(t){let e=Wt.get(t);return e||(e=(async()=>{const n=Object.create(null),r=[];for(let o=0;o<t.entries.length;o+=512){const a=U({format:1,index:o/512,entries:t.entries.slice(o,o+512)}),c=await mt(a);n[c]=a,r.push(c)}const{entries:s,...i}=t;return{descriptor:{...i,entryPartitions:r},partitions:n}})(),Object.isFrozen(t)&&Wt.set(t,e)),e}async function nn(t,e,n,r){if(!t||typeof t!="object"||Array.isArray(t))return;const s=t,i=s.entryPartitions;if(!Array.isArray(i)||i.length>391||new Set(i).size!==i.length||!i.every(a=>typeof a=="string"&&/^[a-f0-9]{64}$/.test(a)&&e.has(a)))return;i.forEach(a=>n.add(a));const o=[];for(let a=0;a<i.length;a++){const c=await r(i[a]).catch(()=>null);if(!c||c.format!==1||c.index!==a||!Array.isArray(c.entries)||c.entries.length>512||a<i.length-1&&c.entries.length!==512)return;o.push(...c.entries)}return ae({...s,entries:o})}const rn=new TextEncoder,qt=768*1024;async function on(t,e){const n=[];let r=[],s=0;for(const i of t){const o=rn.encode(U(i)).byteLength+1;if(o>qt)throw new Error("Session catalog entry exceeds the native partition size");s+o>qt&&(n.push(await e({format:1,index:n.length,entries:r})),r=[],s=0),r.push(i),s+=o}return r.length&&n.push(await e({format:1,index:n.length,entries:r})),n}async function sn(t,e,n,r){if(!Array.isArray(t)||new Set(t).size!==t.length||!t.every(i=>typeof i=="string"&&/^[a-f0-9]{64}$/.test(i)&&e.has(i)))return;t.forEach(i=>n.add(i));const s=[];for(const[i,o]of t.entries()){const a=await r(o).catch(()=>null);if(!a||a.format!==1||a.index!==i||!Array.isArray(a.entries)||Object.keys(a).sort().join(",")!=="entries,format,index")return;for(const c of a.entries)s.push(c)}return Kt(s)}const tt=1024*1024,Pt=1024*1024,an=4096,Zt=256*1024*1024,cn=512,jt=768*1024,he=256,me=jt*he,B=/^[0-9a-f]{64}$/,ln=["layout","partitionKeys","revision","savedAt","sessionPartition","version"],dn=new Set(["__proto__","constructor","prototype"]),Y=new TextEncoder,C=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),O=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),ot=t=>typeof t=="string"&&t.length>0&&t.length<=cn&&t.trim().length>0&&!dn.has(t);function Se(t){return Array.isArray(t)&&t.every((e,n)=>typeof e=="string"&&B.test(e)&&(n===0||t[n-1]<e))}function v(t,e,n=[]){const r=new Set([...e,...n]);return e.every(s=>O(t,s))&&Object.keys(t).every(s=>r.has(s))}function Et(t,e){try{return U(t)===U(e)}catch{return!1}}function fn(t){let e="";for(let n=0;n<t.length;n+=32768)e+=String.fromCharCode(...t.subarray(n,n+32768));return btoa(e)}const un=t=>Uint8Array.from(atob(t),e=>e.charCodeAt(0)),xt=t=>typeof t=="string"&&t.length%4===0&&/^[A-Za-z0-9+/]*={0,2}$/.test(t);async function Qt(t,e){if(!xt(t)||t.length>me)throw new Error("Client state attachment exceeds the 192 MiB encoded blob limit");const n=[];for(let r=0,s=0;r<t.length||r===0;r+=jt,s+=1)n.push(await e({format:2,index:s,data:t.slice(r,r+jt)}));return n}async function pn(t,e){return Promise.all(t.map(async n=>{let r;return n.source.type==="file"?(r={type:"file",path:n.source.path,mime:n.source.mime},n.source.data!==void 0&&(r.dataPartitions=await Qt(n.source.data,e))):n.source.type==="text"?r={type:"text",valuePartitions:await Qt(fn(Y.encode(n.source.value)),e)}:r=n.source,{...n,source:r}}))}async function Tt(t){const e=Object.create(null),n=async o=>{const a=U(o);if(Y.encode(a).byteLength>tt)throw new Error("Client state partition exceeds the native size limit");const c=await mt(a);return e[c]=a,c},r=t===null?null:{tabs:await Promise.all(t.tabs.map(async o=>{var A;if(o.kind==="sidecar")return{kind:o.kind,sidecarId:o.sidecarId};const a=new Set([...Object.keys(o.drafts),...Object.keys(o.attachments),...Object.keys(o.scrollSnapshots),...Object.keys(o.unseenIdleSince),...Object.keys(o.generationRecovery),...Object.keys(o.outlineIndexes??{})]),c=Object.create(null);for(const y of[...a].sort()){const b={format:2};O(o.drafts,y)&&(b.draft=o.drafts[y]),O(o.attachments,y)&&(b.attachments=await pn(o.attachments[y],n)),O(o.scrollSnapshots,y)&&(b.scrollSnapshot=o.scrollSnapshots[y]),O(o.unseenIdleSince,y)&&(b.unseenIdleSince=o.unseenIdleSince[y]),O(o.generationRecovery,y)&&(b.generationRecovery=o.generationRecovery[y]);const j=(A=o.outlineIndexes)==null?void 0:A[y],f=j?await en(j):void 0;f&&(b.outline=f.descriptor,Object.assign(e,f.partitions));const h=await n(b),l=(b.attachments??[]).flatMap(d=>{if(!C(d)||!C(d.source))return[];const p=d.source.dataPartitions??d.source.valuePartitions;return Array.isArray(p)?p.filter(m=>typeof m=="string"):[]});c[y]={documentPartition:h,partitionKeys:[...new Set([h,...l,...Object.keys((f==null?void 0:f.partitions)??{})])].sort()}}const g={format:2,sessions:c};o.activeParentSessionId!==void 0&&(g.activeParentSessionId=o.activeParentSessionId),o.activeSessionId!==void 0&&(g.activeSessionId=o.activeSessionId),o.expandedSessionIds!==void 0&&(g.expandedSessionIds=o.expandedSessionIds),o.sessionCatalog!==void 0&&(g.catalogPartitions=await on(o.sessionCatalog,n));const S={kind:o.kind,folder:o.folder,workspacePartition:await n(g)};return o.occurrence!==void 0&&(S.occurrence=o.occurrence),o.projectName!==void 0&&(S.projectName=o.projectName),o.binaryPath!==void 0&&(S.binaryPath=o.binaryPath),S})),activeTabIndex:t.activeTabIndex,...t.homeActive!==void 0?{homeActive:t.homeActive}:{}},s=await n({format:2,session:r}),i=Object.keys(e).sort();return{sessionPartition:s,partitionKeys:i,partitions:Object.fromEntries(i.map(o=>[o,e[o]]))}}async function yn(t){var r,s;const e=await Tt(t.session),n=[];return(r=t.session)!=null&&r.tabs.some(i=>i.kind==="workspace"&&Object.keys(i.outlineIndexes??{}).length)&&n.push("outline-index-v1"),(s=t.session)!=null&&s.tabs.some(i=>i.kind==="workspace"&&i.sessionCatalog!==void 0)&&n.push("session-catalog-v1"),{root:{version:2,revision:t.revision,savedAt:t.savedAt,layout:t.layout,sessionPartition:e.sessionPartition,partitionKeys:e.partitionKeys,...n.length?{extensions:n}:{}},partitions:e.partitions,partitionKeys:e.partitionKeys}}function hn(t){if(t.partitionKeys.length>an)return!1;const e=JSON.stringify(t.root);if(e.length>Pt)return!1;let n=Y.encode(e).byteLength;if(n>Pt)return!1;for(const r in t.partitions){if(!O(t.partitions,r))continue;const s=t.partitions[r];if(s.length>tt||s.length>Zt-n)return!1;const i=Y.encode(s).byteLength;if(i>tt||i>Zt-n)return!1;n+=i}return!0}function mn(t){if(!C(t)||t.version!==2||typeof t.sessionPartition!="string"||!B.test(t.sessionPartition)||!Se(t.partitionKeys)||!t.partitionKeys.includes(t.sessionPartition)||Object.keys(t).sort().join("\0")!==[...ln,...t.extensions===void 0?[]:["extensions"]].sort().join("\0")||t.extensions!==void 0&&(!Array.isArray(t.extensions)||!t.extensions.length||!t.extensions.every((r,s,i)=>["outline-index-v1","session-catalog-v1"].includes(r)&&(s===0||i[s-1]<r))))return null;let e;try{e=JSON.stringify(t)}catch{return null}if(Y.encode(e).byteLength>Pt)return null;const n=Dt({...t,version:1,session:null});return!n||!Et(t,{version:2,revision:n.revision,savedAt:n.savedAt,layout:n.layout,sessionPartition:t.sessionPartition,partitionKeys:t.partitionKeys,...t.extensions===void 0?{}:{extensions:t.extensions}})?null:{root:n,partitionKey:t.sessionPartition,partitionKeys:t.partitionKeys}}function we(t){try{return{value:JSON.parse(t)}}catch{return null}}async function Sn(t,e,n,r,s){var h;if(!v(t,["format","session"])||U(t)!==r.get(e))return;if(t.session===null){const l=await Tt(null);return te(l,e,n,r)?null:void 0}if(!C(t.session)||!v(t.session,["tabs","activeTabIndex"],["homeActive"])||!Array.isArray(t.session.tabs))return;const i=async l=>{if(typeof l!="string"||!B.test(l))return null;let d=r.get(l);if(d===void 0){if(d=await s(l)??void 0,d===void 0||Y.encode(d).byteLength>tt||await mt(d)!==l)return null;r.set(l,d)}const p=we(d);return p&&C(p.value)&&U(p.value)===d?p.value:null},o=(l,d)=>{const p=[...new Set(l)].sort();return p.length===d.length&&p.every((m,x)=>m===d[x])},a=async(l,d,p)=>{if(!Array.isArray(l)||!l.length||l.length>he||!l.every(u=>typeof u=="string"&&B.test(u))||new Set(l).size!==l.length)return null;const m=[];let x=0;for(const[u,w]of l.entries()){if(!d.has(w))return null;p.add(w);const T=await i(w);if(!T||!xt(T.data))return null;if(T.format===1){if(!v(T,["format","data"]))return null}else if(T.format===2){if(!v(T,["format","index","data"])||T.index!==u)return null}else return null;if(T.data.length>me-x)return null;x+=T.data.length,m.push(T.data)}const I=m.join("");return xt(I)?I:null},c=async(l,d,p)=>{if(!Array.isArray(l))return null;const m=[];for(const x of l){if(!C(x)||!v(x,["id","type","display","url","filename","mediaType","source"])||!C(x.source))return null;const I=x.source;if(I.type==="file"){if(!v(I,["type","path","mime"],["dataPartitions"]))return null;if(O(I,"dataPartitions")){const u=await a(I.dataPartitions,d,p);if(u===null)return null;m.push({...x,source:{type:I.type,path:I.path,mime:I.mime,data:u}})}else m.push(x);continue}if(I.type==="text"){if(!v(I,["type","valuePartitions"]))return null;const u=await a(I.valuePartitions,d,p);if(u===null)return null;try{const w=new TextDecoder("utf-8",{fatal:!0}).decode(un(u));m.push({...x,source:{type:I.type,value:w}})}catch{return null}continue}m.push(x)}return m},g=[],S=new Set([e]);let A=!1,y=!1;for(const l of t.session.tabs){if(!C(l))return;if(l.kind==="sidecar"){if(!v(l,["kind","sidecarId"]))return;g.push({kind:l.kind,sidecarId:l.sidecarId});continue}if(l.kind!=="workspace"||!v(l,["kind","folder","workspacePartition"],["occurrence","projectName","binaryPath"])||typeof l.workspacePartition!="string"||!B.test(l.workspacePartition))return;S.add(l.workspacePartition);const d=await i(l.workspacePartition);if(!d||d.format!==1&&d.format!==2||!v(d,["format","sessions"],["activeParentSessionId","activeSessionId","expandedSessionIds","catalogPartitions"])||!C(d.sessions)||(y||(y=d.format===1),!Object.keys(d.sessions).every(ot)||O(d,"activeParentSessionId")&&!ot(d.activeParentSessionId)||O(d,"activeSessionId")&&!ot(d.activeSessionId)||O(d,"expandedSessionIds")&&(!Array.isArray(d.expandedSessionIds)||!d.expandedSessionIds.every(ot))))return;const p={kind:l.kind,folder:l.folder,drafts:Object.create(null),attachments:Object.create(null),scrollSnapshots:Object.create(null),unseenIdleSince:Object.create(null),generationRecovery:Object.create(null)};for(const u of["occurrence","projectName","binaryPath"])O(l,u)&&(p[u]=l[u]);for(const u of["activeParentSessionId","activeSessionId","expandedSessionIds"])O(d,u)&&(p[u]=d[u]);if(d.catalogPartitions!==void 0){const u=await sn(d.catalogPartitions,new Set(n),S,i);u?p.sessionCatalog=u:A=!0}const m=[];for(const[u,w]of Object.entries(d.sessions)){if(d.format===1){if(typeof w!="string"||!B.test(w))return;m.push({sessionId:u,documentKey:w,partitionKeys:[w]}),S.add(w);continue}if(!C(w)||!v(w,["documentPartition","partitionKeys"])||typeof w.documentPartition!="string"||!B.test(w.documentPartition)||!Se(w.partitionKeys)||!w.partitionKeys.includes(w.documentPartition))return;m.push({sessionId:u,documentKey:w.documentPartition,partitionKeys:w.partitionKeys}),w.partitionKeys.forEach(T=>S.add(T))}const x=typeof d.activeSessionId=="string"?d.activeSessionId:void 0;m.sort((u,w)=>u.sessionId===x?-1:w.sessionId===x?1:u.sessionId.localeCompare(w.sessionId));const I=new Set;for(const{sessionId:u,documentKey:w,partitionKeys:T}of m){const N=await i(w),Ut=new Set(T),wt=new Set([w]);if(!(N&&(N.format===1||N.format===2)&&v(N,["format"],["draft","attachments","scrollSnapshot","unseenIdleSince","generationRecovery","outline"])&&Object.keys(N).length>1)){A=!0,I.add(u);continue}const gt=O(N,"attachments")?N.format===1?N.attachments:await c(N.attachments,Ut,wt):void 0,W=N.outline===void 0?void 0:await nn(N.outline,Ut,wt,i);if(N.outline!==void 0&&!W&&(A=!0),O(N,"attachments")&&gt===null||!o(wt,T)){A=!0,I.add(u);continue}const M={kind:"workspace",folder:"/",drafts:Object.create(null),attachments:Object.create(null),scrollSnapshots:Object.create(null),unseenIdleSince:Object.create(null),generationRecovery:Object.create(null)},Yt=[["draft","drafts"],["scrollSnapshot","scrollSnapshots"],["unseenIdleSince","unseenIdleSince"],["generationRecovery","generationRecovery"]];for(const[H,_e]of Yt)O(N,H)&&(M[_e][u]=N[H]);gt!==void 0&&(M.attachments[u]=gt),W&&(M.outlineIndexes={[u]:W});const $t=(h=Ot({tabs:[M],activeTabIndex:0}))==null?void 0:h.tabs[0];if(!$t||!Et(M,$t)){A=!0,I.add(u);continue}for(const[,H]of Yt)O(M[H],u)&&(p[H][u]=M[H][u]);O(M.attachments,u)&&(p.attachments[u]=M.attachments[u]),W&&(p.outlineIndexes??(p.outlineIndexes=Object.create(null)),p.outlineIndexes[u]=W)}I.has(String(p.activeSessionId))&&delete p.activeSessionId,I.has(String(p.activeParentSessionId))&&delete p.activeParentSessionId,Array.isArray(p.expandedSessionIds)&&(p.expandedSessionIds=p.expandedSessionIds.filter(u=>!I.has(String(u)))),g.push(p)}if(!o(S,n))return;const b={tabs:g,activeTabIndex:t.session.activeTabIndex,...O(t.session,"homeActive")?{homeActive:t.session.homeActive}:{}},j=Ot(b);if(!j||!Et(b,j))return;if(A||y)return j;const f=await Tt(j);return te(f,e,n,r)?j:void 0}function te(t,e,n,r){return t.sessionPartition!==e||t.partitionKeys.length!==n.length||!t.partitionKeys.every((s,i)=>s===n[i])||t.partitionKeys.length!==r.size?!1:t.partitionKeys.every(s=>r.get(s)===t.partitions[s])}async function wn(t,e,n){const r=mn(t);if(!r||e!==1)return null;try{const s=await n(r.partitionKey);if(s===null||Y.encode(s).byteLength>tt||await mt(s)!==r.partitionKey)return null;const i=we(s);if(!i)return null;if(C(i.value)&&i.value.format===2){const o=new Map([[r.partitionKey,s]]),a=await Sn(i.value,r.partitionKey,r.partitionKeys,o,n);return a===void 0?null:{...r.root,session:a}}return null}catch{return null}}const gn=t=>C(t)&&t.version===2,An=250,In=3,bn=1024*1024,On=64,_n=256,Bt=4096,Pn="opencode-session-",jn=new Set(["__proto__","constructor","prototype"]),[z,pt]=ht(!0),[F,dt]=ht(!1),[En,xn]=ht(!1),[Nt,kt]=ht(null);let vt=!1,it=null,P=Object.create(null),ge=0,E=!1,_=!1,ft=null,V=Promise.resolve(),ee=Promise.resolve(),et,Ct;const X=new Set;function nt(t,e){try{return e(window.localStorage)}catch{return t}}function Xt(t){return t.length>0&&t.length<=_n&&!jn.has(t)}const ne=t=>nt(null,e=>{const n=e.getItem(t);return n!==null&&n.length<=Bt?n:null}),Rt=(t,e)=>nt(void 0,n=>n.setItem(t,e)),Mt=t=>nt(void 0,e=>e.removeItem(t));function Ae(t){const e=new Set(Object.keys(P));for(let n=0;n<t.length;n+=1){const r=t.key(n);r!=null&&r.startsWith(Pn)&&e.add(r)}return[...e]}const $=t=>Object.prototype.hasOwnProperty.call(P,t),yt=()=>Object.keys(P).length>=On;function Tn(){return nt(!1,t=>{const e=Object.keys(P).length;for(const n of Ae(t)){if($(n))continue;if(yt())break;const r=t.getItem(n);r===null||r.length>Bt||(P[n]=r)}return Object.keys(P).length!==e})}function Vt(){return vt&&z()&&F()&&!_}const St=()=>{ft!==null&&clearTimeout(ft),ft=null};function Ie(){if(St(),!E||!Vt())return V;const t={version:1,revision:++ge,savedAt:Date.now(),layout:{...P},session:Ct===1?Nt():Nn(Nt())},e=Dt(t);if(!e)return Promise.reject(new Error("Client snapshot normalization failed"));P={...e.layout},E=!1;const n=V.then(async()=>{try{const r=Ct===1?await yn(e):null;if(r&&!hn(r))throw new Error("Client snapshot exceeds the native partition count or size limits");if(!r&&new TextEncoder().encode(JSON.stringify(e)).byteLength>bn)throw new Error("Client snapshot exceeds the V1 1 MiB limit and partition persistence is unavailable");if(!(r?await Ee({protocolVersion:1,snapshot:r.root,partitions:r.partitions,partitionKeys:r.partitionKeys}):await xe(e)))throw pt(!1),new Error("Native client state save was rejected");et=void 0}catch(r){throw E=!0,et=r,r}});return V=n.catch(()=>{}),n}function Nn(t){return t&&{...t,tabs:t.tabs.map(e=>{if(e.kind!=="workspace")return e;const{outlineIndexes:n,sessionCatalog:r,...s}=e;return s})}}function J(){E=!0,Vt()&&(St(),ft=setTimeout(()=>void Ie().catch(t=>console.warn("[client-state] failed to save client snapshot",t)),An))}function ut(t=null,e=!1){P=t?{...t.layout}:Object.create(null),e&&(ge=(t==null?void 0:t.revision)??0),xn(t!==null),kt((t==null?void 0:t.session)??null)}async function be(t,e,n=!1){const r=_;let s=E;_="transaction",X.clear(),St(),E=!1;try{if(await V,s||(s=E),E=!1,!await t())throw n&&pt(!1),new Error(e);nt(void 0,i=>{for(const o of Ae(i))i.removeItem(o)}),ut(),X.clear(),_="snapshot"}catch(i){s||(s=E);for(const o of X)$(o)?Rt(o,P[o]):Mt(o);throw X.clear(),_=r,E=s,E&&J(),i}}function Oe(t){const e=ee.then(t);return ee=e.catch(()=>{}),e}function kn(t){const e=t===null?null:Ot(t);if(!(t!==null&&e===null)){if(_==="transaction"){kt(e),E=!0;return}_||!z()||!F()||(kt(e),J())}}function vn(t){if(!Xt(t))return null;if(!z())return ne(t);if(!F()||_)return null;if($(t))return P[t]??null;const e=ne(t);return e===null||yt()||(P[t]=e,J()),e}function Cn(t,e){if(!(!Xt(t)||e.length>Bt)){if(!z())return Rt(t,e);if(_==="transaction"){if(!$(t)&&yt())return;E||(E=P[t]!==e),P[t]=e,X.add(t);return}!F()||_||!$(t)&&yt()||(Rt(t,e),P[t]!==e&&(P[t]=e,J()))}}function Rn(t){if(Xt(t)){if(!z())return Mt(t);if(_==="transaction"){E||(E=$(t)),delete P[t],X.add(t);return}!F()||_||(Mt(t),$(t)&&(delete P[t],J()))}}async function Mn(){St();let t=et;for(let e=0;e<In;e+=1){for(;;){const n=V;if(await n,n===V)break}if(!E){if(t!==void 0)throw t;return}if(!Vt()){const n=et??t;if(n!==void 0)throw n;return}try{await Ie(),t=void 0}catch(n){t=n}}if(await V,!!E)throw t??new Error("Client state remained dirty after the final flush attempt")}async function Kn(){await Oe(async()=>{if(!z())throw new Error("Client state is not owned by this window");await be(Te,"Native client state clear was rejected",!0)})}async function Ln(t){await Oe(async()=>{if(!(t===F()&&(!t||_===!1))){if(!z())throw new Error("Client state is not owned by this window");if(t){if(!await Ft(!0))throw new Error("Native restore preference update was rejected");_=!1,dt(!0);return}dt(!1);try{await be(()=>Ft(!1),"Native restore preference update was rejected")}catch(e){throw dt(!0),e}}})}function zn(){return it||(it=(async()=>{try{const t=await Pe();if(Ct=t.partitionProtocolVersion,pt(t.isPrimary),dt(t.restoreEnabled),ut(null,!0),E=!1,et=void 0,_=!1,X.clear(),vt=!0,!t.isPrimary||!t.restoreEnabled)return;let e;if(gn(t.snapshot)){_="snapshot";try{e=await wn(t.snapshot,t.partitionProtocolVersion,je)}catch(n){e=null,console.warn("[client-state] failed to load client snapshot partition",n)}e&&(_=!1)}else e=Dt(t.snapshot),_=t.snapshot!==null&&e===null?"snapshot":!1;ut(e,!0),!_&&Tn()&&J()}catch(t){vt=!0,pt(!1),ut(),console.warn("[client-state] failed to initialize client state",t)}})(),it)}const Fn=Object.freeze(Object.defineProperty({__proto__:null,clearRestoredClientState:Kn,clientStateIsPrimary:z,flushClientState:Mn,initializeClientState:zn,loadedClientSnapshotExists:En,loadedRestorableSession:Nt,readClientLayoutValue:vn,removeClientLayoutValue:Rn,restorePreviousStateEnabled:F,setRestorePreviousStateEnabled:Ln,updateRestorableSession:kn,writeClientLayoutValue:Cn},Symbol.toStringTag,{value:"Module"}));export{z as a,vn as b,Kn as c,Rn as d,Yn as e,Mn as f,Vn as g,$n as h,zn as i,Un as j,Fn as k,Nt as l,ae as n,F as r,Ln as s,kn as u,Cn as w};
2
- //# sourceMappingURL=client-state-CdvbHQef.js.map
1
+ import{e as ht}from"./git-diff-vendor-CINvIzmQ.js";import{loadNativeClientState as Pe,loadNativeClientStatePartition as je,commitNativeClientStatePartitions as Ee,saveNativeClientState as xe,setNativeRestoreEnabled as Ft,clearNativeClientState as Te}from"./client-state-kpF1cbUu.js";const G=t=>!!t&&typeof t=="object"&&!Array.isArray(t),K=(t,e)=>typeof t=="string"&&t.length<=e,Ht=t=>K(t,512)&&t.trim().length>0&&!["__proto__","constructor","prototype"].includes(t),q=t=>typeof t=="number"&&Number.isFinite(t)&&t>=0;function Kt(t){if(!Array.isArray(t))return;const e=[],n=new Set;for(const r of t){if(!G(r)||!Ht(r.id)||n.has(r.id)||!K(r.title,32768)||!(r.parentId===null||Ht(r.parentId))||!K(r.projectID,512)||!G(r.location)||!K(r.location.directory,4096)||!r.location.directory||!(r.location.workspaceID===void 0||K(r.location.workspaceID,512))||!G(r.time)||!q(r.time.created)||!q(r.time.updated)||!(r.time.archived===void 0||q(r.time.archived))||!K(r.agent,512)||!G(r.model)||!K(r.model.providerId,512)||!K(r.model.modelId,512)||!q(r.cost)||!G(r.tokens)||!G(r.tokens.cache)||![r.tokens.input,r.tokens.output,r.tokens.reasoning,r.tokens.cache.read,r.tokens.cache.write].every(q)||!(r.subpath===void 0||K(r.subpath,4096)))return;n.add(r.id),e.push({id:r.id,title:r.title,parentId:r.parentId,projectID:r.projectID,location:{directory:r.location.directory,...r.location.workspaceID===void 0?{}:{workspaceID:r.location.workspaceID}},time:{created:r.time.created,updated:r.time.updated,...r.time.archived===void 0?{}:{archived:r.time.archived}},agent:r.agent,model:{providerId:r.model.providerId,modelId:r.model.modelId},cost:r.cost,tokens:{input:r.tokens.input,output:r.tokens.output,reasoning:r.tokens.reasoning,cache:{read:r.tokens.cache.read,write:r.tokens.cache.write}},...r.subpath===void 0?{}:{subpath:r.subpath}})}return e}function Vn(t){return Kt(t)}const Lt=512,st=1024,At=4096,re=256,Q=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),Ne=t=>t!=="__proto__"&&t!=="constructor"&&t!=="prototype";function R(t,e,n=!1){if(!(typeof t!="string"||t.length>e)&&!(!n&&t.trim().length===0))return t}function oe(t){let e="";for(let n=0;n<t.length;n+=32768)e+=String.fromCharCode(...t.subarray(n,n+32768));return btoa(e)}const ie=t=>Uint8Array.from(atob(t),e=>e.charCodeAt(0));function ke(t){if(typeof t!="string"||t.length%4!==0||!/^[A-Za-z0-9+/]*={0,2}$/.test(t))return!1;try{return ie(t),!0}catch{return!1}}const se=t=>{var e;return typeof t=="string"?(e=t.match(/^data:[^;,]+;base64,([A-Za-z0-9+/]*={0,2})$/))==null?void 0:e[1]:void 0};function ve(t,e){const n=se(e)??t,r=n instanceof Uint8Array?oe(n):n;return ke(r)?r:void 0}function Gt(t){if(!(!Q(t)||!Number.isSafeInteger(t.line)||Number(t.line)<0)&&!(!Number.isSafeInteger(t.char)||Number(t.char)<0))return{line:Number(t.line),char:Number(t.char)}}function Ce(t,e){if(!Q(t))return;if(t.type==="text")return typeof t.value=="string"?{type:"text",value:t.value}:void 0;if(t.type==="agent"){const o=R(t.name,st);return o===void 0?void 0:{type:"agent",name:o}}if(t.type==="skill"){const o=R(t.id,Lt),a=R(t.name,st);return o===void 0||a===void 0?void 0:{type:"skill",id:o,name:a}}const n=R(t.path,At);if(t.type==="file"){const o=R(t.mime,re);if(n===void 0||o===void 0)return;const a=se(e)??t.data,c=ve(t.data,e);return a!==void 0&&c===void 0?void 0:c===void 0?{type:"file",path:n,mime:o}:{type:"file",path:n,mime:o,data:c}}if(t.type!=="symbol"||!Q(t.range))return;const r=R(t.name,st),s=Gt(t.range.start),i=Gt(t.range.end);if(!(n===void 0||r===void 0||!Number.isSafeInteger(t.kind)||!s||!i))return{type:"symbol",path:n,name:r,kind:Number(t.kind),range:{start:s,end:i}}}function Re(t){if(!Q(t)||!["file","text","symbol","agent","skill"].includes(String(t.type)))return;const e=R(t.id,Lt),n=R(t.display,st),r=R(t.filename,At),s=R(t.mediaType,re),i=Ce(t.source,t.url),o=typeof t.url=="string"&&!t.url.startsWith("data:")?t.url:"",a=R(o,At,!0);if(!(!e||!n||!r||!s||!i||i.type!==t.type||a===void 0))return{id:e,type:t.type,display:n,url:a,filename:r,mediaType:s,source:i}}function zt(t,e,n=[]){if(!Q(t))return null;const r=Object.create(null),s=[...new Set(n)],i=new Set(s),o=[...s.flatMap(a=>Object.prototype.hasOwnProperty.call(t,a)?[[a,t[a]]]:[]),...Object.entries(t).filter(([a])=>!i.has(a))];for(const[a,c]of o){if(!Ne(a)||!a||a.length>Lt||!Array.isArray(c))continue;const g=[];for(const S of c){const A=Re(S);A&&g.push(A)}g.length&&(r[a]=g)}return{attachments:r,drafts:{...e}}}function Un(t,e,n=[]){const r=zt(e,t,n);if(!r||Object.entries(e).some(([s,i])=>{var o;return i.length!==(((o=r.attachments[s])==null?void 0:o.length)??0)}))throw new Error("Draft attachments could not be persisted safely");return r}function Yn(t){let e,n=t.url;if(t.source.type==="file")try{const r=t.source.data===void 0?void 0:ie(t.source.data);e={type:"file",path:t.source.path,mime:t.source.mime,data:r},!n&&t.source.data&&(n=`data:${t.source.mime};base64,${t.source.data}`)}catch{return null}else e=t.source,t.source.type==="text"&&!n&&(n=`data:text/plain;base64,${oe(new TextEncoder().encode(t.source.value))}`);return{...t,url:n,source:e}}const Me=()=>({bytes:16*1024*1024,entries:2e5,indexes:16}),It=new WeakMap,Ke=new Set(["user","assistant","system","synthetic","skill","shell","compaction","idle","agent-switched","model-switched","location-switched"]),at=t=>!!t&&typeof t=="object"&&!Array.isArray(t),Z=(t,e)=>typeof t=="string"&&t.length>0&&t.length<=e,rt=(t,e=0)=>Number.isSafeInteger(t)&&Number(t)>=e;function $n(t){return t.entries.every(e=>!e.tools||e.toolName!==void 0)}function ae(t){if(!at(t))return;if(It.has(t))return t;if(t.format!==1||!Z(t.directory,4096)||!Z(t.projectID,256)||t.revert!==void 0&&!Z(t.revert,256)||!Array.isArray(t.entries)||t.entries.length>2e5||!Array.isArray(t.checkpoints)||t.checkpoints.length>512)return;const e=[],n=[],r=new Set;let s=-1,i=-1;for(const c of t.entries){if(!at(c)||!Z(c.id,256)||r.has(c.id)||!rt(c.seq)||c.seq<=s||typeof c.type!="string"||!Ke.has(c.type)||!rt(c.tools)||!rt(c.reasoning)||c.toolName!==void 0&&(typeof c.toolName!="string"||c.toolName.length>256))return;r.add(c.id),s=c.seq,e.push(Object.freeze({id:c.id,seq:s,type:c.type,tools:c.tools,reasoning:c.reasoning,...c.toolName===void 0?{}:{toolName:c.toolName}}))}for(const c of t.checkpoints){if(!at(c)||c.after!==i||!rt(c.through)||c.through<=i||typeof c.digest!="string"||!/^[a-f0-9]{64}$/.test(c.digest))return;n.push(Object.freeze({after:i,through:c.through,digest:c.digest})),i=c.through}if(!n.length||s>i)return;const o={format:1,directory:t.directory,projectID:t.projectID,...t.revert===void 0?{}:{revert:t.revert},entries:e,checkpoints:n},a=new TextEncoder().encode(JSON.stringify(o)).byteLength;if(!(a>16*1024*1024))return Object.freeze(e),Object.freeze(n),Object.freeze(o),It.set(o,a),o}function Le(t,e,n){if(!at(t))return;const r=Object.create(null),s=[n,...Object.keys(t).filter(i=>i!==n)].filter(i=>!!i);for(const i of s){if(!e.indexes||!e.entries||!e.bytes)break;if(!Z(i,256)||["__proto__","constructor","prototype"].includes(i))continue;const o=ae(t[i]);if(!o)continue;const a=It.get(o);a>e.bytes||o.entries.length>e.entries||(r[i]=o,e.bytes-=a,e.entries-=o.entries.length,e.indexes--)}return Object.keys(r).length?r:void 0}const ce=32,ze=64,De=24,Be=96,Xe=256,Ve=256,Ue=256,le=256,bt=4096,ct=512,Ye=4096,de=32*1024,$e=1024,Fe=96*1024,He=256,Jt=1024,Ge="__no_session_draft__";function fe(){return{remaining:Fe,scrollSnapshotsRemaining:He,outlines:Me()}}const L=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),ue=t=>t!=="__proto__"&&t!=="constructor"&&t!=="prototype";function k(t,e,n,r=!1){if(!(typeof t!="string"||t.length>e||t.length>n.remaining)&&!(!r&&t.trim().length===0))return n.remaining-=t.length,t}const D=(t,e,n)=>typeof t=="number"&&Number.isFinite(t)&&t>=e&&t<=n?t:void 0;function lt(t,e,n,r,s=()=>!0,i=()=>{},o=[]){if(!L(t))return null;const a=Object.create(null);let c=0;const g=[...new Set(o)],S=new Set(g),A=[...g.flatMap(y=>Object.prototype.hasOwnProperty.call(t,y)?[[y,t[y]]]:[]),...Object.entries(t).filter(([y])=>!S.has(y))];for(const[y,b]of A){if(c>=e)break;if(!ue(y)||!s(b))continue;const j=k(y,le,n),f=r(b);j===void 0||f===void 0||(a[j]=f,c+=1,i())}return a}function pe(t,e,n,r,s=[]){return lt(t,e,r,i=>k(i,n,r,!0),void 0,void 0,s)}function Je(t,e){if(!L(t))return;const n=D(t.scrollTop,0,1e9),r=D(t.updatedAt,0,Number.MAX_SAFE_INTEGER);if(n===void 0||r===void 0||typeof t.atBottom!="boolean")return;const s={scrollTop:n,atBottom:t.atBottom,updatedAt:r},i=D(t.scrollRatio,0,1),o=D(t.maxScrollTop,0,1e9),a=D(t.anchorOffset,-1e6,1e6),c=t.anchorKey===void 0?void 0:k(t.anchorKey,$e,e);i!==void 0&&(s.scrollRatio=i),o!==void 0&&(s.maxScrollTop=o),c!==void 0&&(s.anchorKey=c),a!==void 0&&(s.anchorOffset=a),(t.followModeType==="following"||t.followModeType==="escaped")&&(s.followModeType=t.followModeType),typeof t.windowIsLatest=="boolean"&&(s.windowIsLatest=t.windowIsLatest);const g=t.windowCursor===void 0?void 0:k(t.windowCursor,Jt,e);if(g!==void 0&&(s.windowCursor=g),Array.isArray(t.newerCursors)){const S=[];for(const A of t.newerCursors.slice(-32)){if(A===null||A===""){S.push(null);continue}const y=k(A,Jt,e);y!==void 0&&S.push(y)}S.length>0&&(s.newerCursors=S)}return s}function We(t,e){if(!Array.isArray(t))return null;const n=[],r=new Set;for(const s of t.slice(0,Ue)){if(typeof s!="string"||r.has(s))continue;const i=k(s,ct,e);i!==void 0&&(r.add(i),n.push(i))}return n}function qe(t,e,n,r){const s=e.priorityDrafts,i=Object.fromEntries(Object.entries(t.drafts??{}).filter(([m])=>!Object.prototype.hasOwnProperty.call(s,m))),o=pe(i,Math.max(0,De-Object.keys(s).length),de,n),a=o?{...s,...o}:null,c=Math.min(Be,n.scrollSnapshotsRemaining),g=lt(t.scrollSnapshots??{},c,n,m=>Je(m,n),void 0,()=>{n.scrollSnapshotsRemaining-=1}),S=lt(t.unseenIdleSince??{},Xe,n,m=>D(m,0,Number.MAX_SAFE_INTEGER)),A=lt(t.generationRecovery??{},Ve,n,m=>m,m=>m==="working"||m==="interrupted"),y=t.expandedSessionIds===void 0?void 0:We(t.expandedSessionIds,n);if(!a||!g||!S||!A||y===null)return null;const b=Object.fromEntries(Object.entries(t.attachments??{}).filter(([m])=>!e.prioritySessionIds.includes(m))),j=zt(b,a);if(!j)return null;const f={kind:"workspace",folder:e.folder,drafts:j.drafts,attachments:{...e.priorityAttachments,...j.attachments},scrollSnapshots:g,unseenIdleSince:S,generationRecovery:A};y!==void 0&&(f.expandedSessionIds=y);const h=Le(t.outlineIndexes,n.outlines,e.activeSessionId);h&&(f.outlineIndexes=h);const l=Kt(t.sessionCatalog);l&&(f.sessionCatalog=l),Number.isInteger(t.occurrence)&&Number(t.occurrence)>=0&&Number(t.occurrence)<ce&&(f.occurrence=Number(t.occurrence));const d=t.projectName===void 0?void 0:k(t.projectName,bt,n),p=t.binaryPath===void 0?void 0:k(t.binaryPath,bt,n);return d!==void 0&&(f.projectName=d),p!==void 0&&(f.binaryPath=p),e.activeParentSessionId!==void 0&&(f.activeParentSessionId=e.activeParentSessionId),e.activeSessionId!==void 0&&(f.activeSessionId=e.activeSessionId),f}function Ze(t,e){if(!L(t))return null;const n=t.kind??t.type;if(n==="sidecar"){const a=k(t.sidecarId,ct,e);return a===void 0?null:{kind:n,sidecarId:a}}if(n!=="workspace"&&n!=="instance"||!["drafts","attachments","scrollSnapshots","unseenIdleSince","generationRecovery"].every(a=>L(t[a]??{})))return null;const r=k(t.folder,bt,e);if(r===void 0)return null;const s=t.activeParentSessionId===void 0?void 0:k(t.activeParentSessionId,ct,e),i=t.activeSessionId===void 0?void 0:k(t.activeSessionId,ct,e),o=[i,Ge].filter(a=>!!a);return{kind:"workspace",value:t,folder:r,activeParentSessionId:s,activeSessionId:i,prioritySessionIds:o,priorityDrafts:Object.create(null),priorityAttachments:Object.create(null)}}function Qe(t,e){const n=t.value.drafts??{};if(!L(n))return;for(const o of t.prioritySessionIds){if(!ue(o)||!Object.prototype.hasOwnProperty.call(n,o))continue;const a=e.remaining,c=k(o,le,e),g=k(n[o],de,e,!0);if(c===void 0||g===void 0){e.remaining=a;continue}t.priorityDrafts[c]=g}const r=t.value.attachments??{};if(!L(r))return;const s=Object.fromEntries(t.prioritySessionIds.filter(o=>Object.prototype.hasOwnProperty.call(r,o)).map(o=>[o,r[o]])),i=zt(s,t.priorityDrafts,t.prioritySessionIds);i&&(t.priorityDrafts=i.drafts,t.priorityAttachments=i.attachments)}function Ot(t){return ye(t,fe())}function ye(t,e){if(!L(t)||!Array.isArray(t.tabs)||!Number.isInteger(t.activeTabIndex))return null;const n=Number(t.activeTabIndex),r=t.tabs.slice(0,ce),s=new Map,i=new Map,o=f=>{const h=Ze(r[f],e);h&&s.set(f,h)},a=(f,h)=>{const l=s.get(f);if(!l)return;const d=l.kind==="sidecar"?l:qe(l.value,l,e);d&&i.set(f,d)};r.forEach((f,h)=>o(h));const c=[n,...r.map((f,h)=>h).filter(f=>f!==n)];for(const f of c){const h=s.get(f);(h==null?void 0:h.kind)==="workspace"&&Qe(h,e)}r[n]!==void 0&&a(n),r.forEach((f,h)=>{h!==n&&a(h)});const S=r.flatMap((f,h)=>{const l=s.get(h);return l?[{originalIndex:h,identity:l}]:[]}).flatMap(({originalIndex:f})=>{const h=i.get(f);return h?[{originalIndex:f,tab:h}]:[]});if(t.tabs.length&&!S.length)return null;const A=S.map(({tab:f})=>f),y=S.findIndex(({originalIndex:f})=>f===n),b=S.findIndex(({originalIndex:f})=>f>n),j=A.length?y>=0?y:b>=0?b:A.length-1:-1;return{tabs:A,activeTabIndex:j,...t.homeActive===!0?{homeActive:!0}:{}}}function Dt(t){if(!L(t)||t.version!==void 0&&t.version!==1||!Number.isSafeInteger(t.revision)||Number(t.revision)<0)return null;const e=D(t.savedAt,0,Number.MAX_SAFE_INTEGER);if(e===void 0)return null;const n=fe(),r=t.session===null?null:ye(t.session,n);if(t.session!==null&&!r)return null;const s=pe(t.layout,ze,Ye,n);return s?{version:1,revision:Number(t.revision),savedAt:e,layout:s,session:r}:null}const tn=new TextEncoder;function _t(t){return Array.isArray(t)?t.map(_t):typeof t!="object"||t===null?t:Object.fromEntries(Object.keys(t).sort().map(e=>[e,_t(t[e])]))}function U(t){const e=JSON.stringify(_t(t));if(e===void 0)throw new TypeError("Client state partition must be JSON-serializable");return e}async function mt(t){const e=await crypto.subtle.digest("SHA-256",tn.encode(t));return Array.from(new Uint8Array(e),n=>n.toString(16).padStart(2,"0")).join("")}const Wt=new WeakMap;function en(t){let e=Wt.get(t);return e||(e=(async()=>{const n=Object.create(null),r=[];for(let o=0;o<t.entries.length;o+=512){const a=U({format:1,index:o/512,entries:t.entries.slice(o,o+512)}),c=await mt(a);n[c]=a,r.push(c)}const{entries:s,...i}=t;return{descriptor:{...i,entryPartitions:r},partitions:n}})(),Object.isFrozen(t)&&Wt.set(t,e)),e}async function nn(t,e,n,r){if(!t||typeof t!="object"||Array.isArray(t))return;const s=t,i=s.entryPartitions;if(!Array.isArray(i)||i.length>391||new Set(i).size!==i.length||!i.every(a=>typeof a=="string"&&/^[a-f0-9]{64}$/.test(a)&&e.has(a)))return;i.forEach(a=>n.add(a));const o=[];for(let a=0;a<i.length;a++){const c=await r(i[a]).catch(()=>null);if(!c||c.format!==1||c.index!==a||!Array.isArray(c.entries)||c.entries.length>512||a<i.length-1&&c.entries.length!==512)return;o.push(...c.entries)}return ae({...s,entries:o})}const rn=new TextEncoder,qt=768*1024;async function on(t,e){const n=[];let r=[],s=0;for(const i of t){const o=rn.encode(U(i)).byteLength+1;if(o>qt)throw new Error("Session catalog entry exceeds the native partition size");s+o>qt&&(n.push(await e({format:1,index:n.length,entries:r})),r=[],s=0),r.push(i),s+=o}return r.length&&n.push(await e({format:1,index:n.length,entries:r})),n}async function sn(t,e,n,r){if(!Array.isArray(t)||new Set(t).size!==t.length||!t.every(i=>typeof i=="string"&&/^[a-f0-9]{64}$/.test(i)&&e.has(i)))return;t.forEach(i=>n.add(i));const s=[];for(const[i,o]of t.entries()){const a=await r(o).catch(()=>null);if(!a||a.format!==1||a.index!==i||!Array.isArray(a.entries)||Object.keys(a).sort().join(",")!=="entries,format,index")return;for(const c of a.entries)s.push(c)}return Kt(s)}const tt=1024*1024,Pt=1024*1024,an=4096,Zt=256*1024*1024,cn=512,jt=768*1024,he=256,me=jt*he,B=/^[0-9a-f]{64}$/,ln=["layout","partitionKeys","revision","savedAt","sessionPartition","version"],dn=new Set(["__proto__","constructor","prototype"]),Y=new TextEncoder,C=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),O=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),ot=t=>typeof t=="string"&&t.length>0&&t.length<=cn&&t.trim().length>0&&!dn.has(t);function Se(t){return Array.isArray(t)&&t.every((e,n)=>typeof e=="string"&&B.test(e)&&(n===0||t[n-1]<e))}function v(t,e,n=[]){const r=new Set([...e,...n]);return e.every(s=>O(t,s))&&Object.keys(t).every(s=>r.has(s))}function Et(t,e){try{return U(t)===U(e)}catch{return!1}}function fn(t){let e="";for(let n=0;n<t.length;n+=32768)e+=String.fromCharCode(...t.subarray(n,n+32768));return btoa(e)}const un=t=>Uint8Array.from(atob(t),e=>e.charCodeAt(0)),xt=t=>typeof t=="string"&&t.length%4===0&&/^[A-Za-z0-9+/]*={0,2}$/.test(t);async function Qt(t,e){if(!xt(t)||t.length>me)throw new Error("Client state attachment exceeds the 192 MiB encoded blob limit");const n=[];for(let r=0,s=0;r<t.length||r===0;r+=jt,s+=1)n.push(await e({format:2,index:s,data:t.slice(r,r+jt)}));return n}async function pn(t,e){return Promise.all(t.map(async n=>{let r;return n.source.type==="file"?(r={type:"file",path:n.source.path,mime:n.source.mime},n.source.data!==void 0&&(r.dataPartitions=await Qt(n.source.data,e))):n.source.type==="text"?r={type:"text",valuePartitions:await Qt(fn(Y.encode(n.source.value)),e)}:r=n.source,{...n,source:r}}))}async function Tt(t){const e=Object.create(null),n=async o=>{const a=U(o);if(Y.encode(a).byteLength>tt)throw new Error("Client state partition exceeds the native size limit");const c=await mt(a);return e[c]=a,c},r=t===null?null:{tabs:await Promise.all(t.tabs.map(async o=>{var A;if(o.kind==="sidecar")return{kind:o.kind,sidecarId:o.sidecarId};const a=new Set([...Object.keys(o.drafts),...Object.keys(o.attachments),...Object.keys(o.scrollSnapshots),...Object.keys(o.unseenIdleSince),...Object.keys(o.generationRecovery),...Object.keys(o.outlineIndexes??{})]),c=Object.create(null);for(const y of[...a].sort()){const b={format:2};O(o.drafts,y)&&(b.draft=o.drafts[y]),O(o.attachments,y)&&(b.attachments=await pn(o.attachments[y],n)),O(o.scrollSnapshots,y)&&(b.scrollSnapshot=o.scrollSnapshots[y]),O(o.unseenIdleSince,y)&&(b.unseenIdleSince=o.unseenIdleSince[y]),O(o.generationRecovery,y)&&(b.generationRecovery=o.generationRecovery[y]);const j=(A=o.outlineIndexes)==null?void 0:A[y],f=j?await en(j):void 0;f&&(b.outline=f.descriptor,Object.assign(e,f.partitions));const h=await n(b),l=(b.attachments??[]).flatMap(d=>{if(!C(d)||!C(d.source))return[];const p=d.source.dataPartitions??d.source.valuePartitions;return Array.isArray(p)?p.filter(m=>typeof m=="string"):[]});c[y]={documentPartition:h,partitionKeys:[...new Set([h,...l,...Object.keys((f==null?void 0:f.partitions)??{})])].sort()}}const g={format:2,sessions:c};o.activeParentSessionId!==void 0&&(g.activeParentSessionId=o.activeParentSessionId),o.activeSessionId!==void 0&&(g.activeSessionId=o.activeSessionId),o.expandedSessionIds!==void 0&&(g.expandedSessionIds=o.expandedSessionIds),o.sessionCatalog!==void 0&&(g.catalogPartitions=await on(o.sessionCatalog,n));const S={kind:o.kind,folder:o.folder,workspacePartition:await n(g)};return o.occurrence!==void 0&&(S.occurrence=o.occurrence),o.projectName!==void 0&&(S.projectName=o.projectName),o.binaryPath!==void 0&&(S.binaryPath=o.binaryPath),S})),activeTabIndex:t.activeTabIndex,...t.homeActive!==void 0?{homeActive:t.homeActive}:{}},s=await n({format:2,session:r}),i=Object.keys(e).sort();return{sessionPartition:s,partitionKeys:i,partitions:Object.fromEntries(i.map(o=>[o,e[o]]))}}async function yn(t){var r,s;const e=await Tt(t.session),n=[];return(r=t.session)!=null&&r.tabs.some(i=>i.kind==="workspace"&&Object.keys(i.outlineIndexes??{}).length)&&n.push("outline-index-v1"),(s=t.session)!=null&&s.tabs.some(i=>i.kind==="workspace"&&i.sessionCatalog!==void 0)&&n.push("session-catalog-v1"),{root:{version:2,revision:t.revision,savedAt:t.savedAt,layout:t.layout,sessionPartition:e.sessionPartition,partitionKeys:e.partitionKeys,...n.length?{extensions:n}:{}},partitions:e.partitions,partitionKeys:e.partitionKeys}}function hn(t){if(t.partitionKeys.length>an)return!1;const e=JSON.stringify(t.root);if(e.length>Pt)return!1;let n=Y.encode(e).byteLength;if(n>Pt)return!1;for(const r in t.partitions){if(!O(t.partitions,r))continue;const s=t.partitions[r];if(s.length>tt||s.length>Zt-n)return!1;const i=Y.encode(s).byteLength;if(i>tt||i>Zt-n)return!1;n+=i}return!0}function mn(t){if(!C(t)||t.version!==2||typeof t.sessionPartition!="string"||!B.test(t.sessionPartition)||!Se(t.partitionKeys)||!t.partitionKeys.includes(t.sessionPartition)||Object.keys(t).sort().join("\0")!==[...ln,...t.extensions===void 0?[]:["extensions"]].sort().join("\0")||t.extensions!==void 0&&(!Array.isArray(t.extensions)||!t.extensions.length||!t.extensions.every((r,s,i)=>["outline-index-v1","session-catalog-v1"].includes(r)&&(s===0||i[s-1]<r))))return null;let e;try{e=JSON.stringify(t)}catch{return null}if(Y.encode(e).byteLength>Pt)return null;const n=Dt({...t,version:1,session:null});return!n||!Et(t,{version:2,revision:n.revision,savedAt:n.savedAt,layout:n.layout,sessionPartition:t.sessionPartition,partitionKeys:t.partitionKeys,...t.extensions===void 0?{}:{extensions:t.extensions}})?null:{root:n,partitionKey:t.sessionPartition,partitionKeys:t.partitionKeys}}function we(t){try{return{value:JSON.parse(t)}}catch{return null}}async function Sn(t,e,n,r,s){var h;if(!v(t,["format","session"])||U(t)!==r.get(e))return;if(t.session===null){const l=await Tt(null);return te(l,e,n,r)?null:void 0}if(!C(t.session)||!v(t.session,["tabs","activeTabIndex"],["homeActive"])||!Array.isArray(t.session.tabs))return;const i=async l=>{if(typeof l!="string"||!B.test(l))return null;let d=r.get(l);if(d===void 0){if(d=await s(l)??void 0,d===void 0||Y.encode(d).byteLength>tt||await mt(d)!==l)return null;r.set(l,d)}const p=we(d);return p&&C(p.value)&&U(p.value)===d?p.value:null},o=(l,d)=>{const p=[...new Set(l)].sort();return p.length===d.length&&p.every((m,x)=>m===d[x])},a=async(l,d,p)=>{if(!Array.isArray(l)||!l.length||l.length>he||!l.every(u=>typeof u=="string"&&B.test(u))||new Set(l).size!==l.length)return null;const m=[];let x=0;for(const[u,w]of l.entries()){if(!d.has(w))return null;p.add(w);const T=await i(w);if(!T||!xt(T.data))return null;if(T.format===1){if(!v(T,["format","data"]))return null}else if(T.format===2){if(!v(T,["format","index","data"])||T.index!==u)return null}else return null;if(T.data.length>me-x)return null;x+=T.data.length,m.push(T.data)}const I=m.join("");return xt(I)?I:null},c=async(l,d,p)=>{if(!Array.isArray(l))return null;const m=[];for(const x of l){if(!C(x)||!v(x,["id","type","display","url","filename","mediaType","source"])||!C(x.source))return null;const I=x.source;if(I.type==="file"){if(!v(I,["type","path","mime"],["dataPartitions"]))return null;if(O(I,"dataPartitions")){const u=await a(I.dataPartitions,d,p);if(u===null)return null;m.push({...x,source:{type:I.type,path:I.path,mime:I.mime,data:u}})}else m.push(x);continue}if(I.type==="text"){if(!v(I,["type","valuePartitions"]))return null;const u=await a(I.valuePartitions,d,p);if(u===null)return null;try{const w=new TextDecoder("utf-8",{fatal:!0}).decode(un(u));m.push({...x,source:{type:I.type,value:w}})}catch{return null}continue}m.push(x)}return m},g=[],S=new Set([e]);let A=!1,y=!1;for(const l of t.session.tabs){if(!C(l))return;if(l.kind==="sidecar"){if(!v(l,["kind","sidecarId"]))return;g.push({kind:l.kind,sidecarId:l.sidecarId});continue}if(l.kind!=="workspace"||!v(l,["kind","folder","workspacePartition"],["occurrence","projectName","binaryPath"])||typeof l.workspacePartition!="string"||!B.test(l.workspacePartition))return;S.add(l.workspacePartition);const d=await i(l.workspacePartition);if(!d||d.format!==1&&d.format!==2||!v(d,["format","sessions"],["activeParentSessionId","activeSessionId","expandedSessionIds","catalogPartitions"])||!C(d.sessions)||(y||(y=d.format===1),!Object.keys(d.sessions).every(ot)||O(d,"activeParentSessionId")&&!ot(d.activeParentSessionId)||O(d,"activeSessionId")&&!ot(d.activeSessionId)||O(d,"expandedSessionIds")&&(!Array.isArray(d.expandedSessionIds)||!d.expandedSessionIds.every(ot))))return;const p={kind:l.kind,folder:l.folder,drafts:Object.create(null),attachments:Object.create(null),scrollSnapshots:Object.create(null),unseenIdleSince:Object.create(null),generationRecovery:Object.create(null)};for(const u of["occurrence","projectName","binaryPath"])O(l,u)&&(p[u]=l[u]);for(const u of["activeParentSessionId","activeSessionId","expandedSessionIds"])O(d,u)&&(p[u]=d[u]);if(d.catalogPartitions!==void 0){const u=await sn(d.catalogPartitions,new Set(n),S,i);u?p.sessionCatalog=u:A=!0}const m=[];for(const[u,w]of Object.entries(d.sessions)){if(d.format===1){if(typeof w!="string"||!B.test(w))return;m.push({sessionId:u,documentKey:w,partitionKeys:[w]}),S.add(w);continue}if(!C(w)||!v(w,["documentPartition","partitionKeys"])||typeof w.documentPartition!="string"||!B.test(w.documentPartition)||!Se(w.partitionKeys)||!w.partitionKeys.includes(w.documentPartition))return;m.push({sessionId:u,documentKey:w.documentPartition,partitionKeys:w.partitionKeys}),w.partitionKeys.forEach(T=>S.add(T))}const x=typeof d.activeSessionId=="string"?d.activeSessionId:void 0;m.sort((u,w)=>u.sessionId===x?-1:w.sessionId===x?1:u.sessionId.localeCompare(w.sessionId));const I=new Set;for(const{sessionId:u,documentKey:w,partitionKeys:T}of m){const N=await i(w),Ut=new Set(T),wt=new Set([w]);if(!(N&&(N.format===1||N.format===2)&&v(N,["format"],["draft","attachments","scrollSnapshot","unseenIdleSince","generationRecovery","outline"])&&Object.keys(N).length>1)){A=!0,I.add(u);continue}const gt=O(N,"attachments")?N.format===1?N.attachments:await c(N.attachments,Ut,wt):void 0,W=N.outline===void 0?void 0:await nn(N.outline,Ut,wt,i);if(N.outline!==void 0&&!W&&(A=!0),O(N,"attachments")&&gt===null||!o(wt,T)){A=!0,I.add(u);continue}const M={kind:"workspace",folder:"/",drafts:Object.create(null),attachments:Object.create(null),scrollSnapshots:Object.create(null),unseenIdleSince:Object.create(null),generationRecovery:Object.create(null)},Yt=[["draft","drafts"],["scrollSnapshot","scrollSnapshots"],["unseenIdleSince","unseenIdleSince"],["generationRecovery","generationRecovery"]];for(const[H,_e]of Yt)O(N,H)&&(M[_e][u]=N[H]);gt!==void 0&&(M.attachments[u]=gt),W&&(M.outlineIndexes={[u]:W});const $t=(h=Ot({tabs:[M],activeTabIndex:0}))==null?void 0:h.tabs[0];if(!$t||!Et(M,$t)){A=!0,I.add(u);continue}for(const[,H]of Yt)O(M[H],u)&&(p[H][u]=M[H][u]);O(M.attachments,u)&&(p.attachments[u]=M.attachments[u]),W&&(p.outlineIndexes??(p.outlineIndexes=Object.create(null)),p.outlineIndexes[u]=W)}I.has(String(p.activeSessionId))&&delete p.activeSessionId,I.has(String(p.activeParentSessionId))&&delete p.activeParentSessionId,Array.isArray(p.expandedSessionIds)&&(p.expandedSessionIds=p.expandedSessionIds.filter(u=>!I.has(String(u)))),g.push(p)}if(!o(S,n))return;const b={tabs:g,activeTabIndex:t.session.activeTabIndex,...O(t.session,"homeActive")?{homeActive:t.session.homeActive}:{}},j=Ot(b);if(!j||!Et(b,j))return;if(A||y)return j;const f=await Tt(j);return te(f,e,n,r)?j:void 0}function te(t,e,n,r){return t.sessionPartition!==e||t.partitionKeys.length!==n.length||!t.partitionKeys.every((s,i)=>s===n[i])||t.partitionKeys.length!==r.size?!1:t.partitionKeys.every(s=>r.get(s)===t.partitions[s])}async function wn(t,e,n){const r=mn(t);if(!r||e!==1)return null;try{const s=await n(r.partitionKey);if(s===null||Y.encode(s).byteLength>tt||await mt(s)!==r.partitionKey)return null;const i=we(s);if(!i)return null;if(C(i.value)&&i.value.format===2){const o=new Map([[r.partitionKey,s]]),a=await Sn(i.value,r.partitionKey,r.partitionKeys,o,n);return a===void 0?null:{...r.root,session:a}}return null}catch{return null}}const gn=t=>C(t)&&t.version===2,An=250,In=3,bn=1024*1024,On=64,_n=256,Bt=4096,Pn="opencode-session-",jn=new Set(["__proto__","constructor","prototype"]),[z,pt]=ht(!0),[F,dt]=ht(!1),[En,xn]=ht(!1),[Nt,kt]=ht(null);let vt=!1,it=null,P=Object.create(null),ge=0,E=!1,_=!1,ft=null,V=Promise.resolve(),ee=Promise.resolve(),et,Ct;const X=new Set;function nt(t,e){try{return e(window.localStorage)}catch{return t}}function Xt(t){return t.length>0&&t.length<=_n&&!jn.has(t)}const ne=t=>nt(null,e=>{const n=e.getItem(t);return n!==null&&n.length<=Bt?n:null}),Rt=(t,e)=>nt(void 0,n=>n.setItem(t,e)),Mt=t=>nt(void 0,e=>e.removeItem(t));function Ae(t){const e=new Set(Object.keys(P));for(let n=0;n<t.length;n+=1){const r=t.key(n);r!=null&&r.startsWith(Pn)&&e.add(r)}return[...e]}const $=t=>Object.prototype.hasOwnProperty.call(P,t),yt=()=>Object.keys(P).length>=On;function Tn(){return nt(!1,t=>{const e=Object.keys(P).length;for(const n of Ae(t)){if($(n))continue;if(yt())break;const r=t.getItem(n);r===null||r.length>Bt||(P[n]=r)}return Object.keys(P).length!==e})}function Vt(){return vt&&z()&&F()&&!_}const St=()=>{ft!==null&&clearTimeout(ft),ft=null};function Ie(){if(St(),!E||!Vt())return V;const t={version:1,revision:++ge,savedAt:Date.now(),layout:{...P},session:Ct===1?Nt():Nn(Nt())},e=Dt(t);if(!e)return Promise.reject(new Error("Client snapshot normalization failed"));P={...e.layout},E=!1;const n=V.then(async()=>{try{const r=Ct===1?await yn(e):null;if(r&&!hn(r))throw new Error("Client snapshot exceeds the native partition count or size limits");if(!r&&new TextEncoder().encode(JSON.stringify(e)).byteLength>bn)throw new Error("Client snapshot exceeds the V1 1 MiB limit and partition persistence is unavailable");if(!(r?await Ee({protocolVersion:1,snapshot:r.root,partitions:r.partitions,partitionKeys:r.partitionKeys}):await xe(e)))throw pt(!1),new Error("Native client state save was rejected");et=void 0}catch(r){throw E=!0,et=r,r}});return V=n.catch(()=>{}),n}function Nn(t){return t&&{...t,tabs:t.tabs.map(e=>{if(e.kind!=="workspace")return e;const{outlineIndexes:n,sessionCatalog:r,...s}=e;return s})}}function J(){E=!0,Vt()&&(St(),ft=setTimeout(()=>void Ie().catch(t=>console.warn("[client-state] failed to save client snapshot",t)),An))}function ut(t=null,e=!1){P=t?{...t.layout}:Object.create(null),e&&(ge=(t==null?void 0:t.revision)??0),xn(t!==null),kt((t==null?void 0:t.session)??null)}async function be(t,e,n=!1){const r=_;let s=E;_="transaction",X.clear(),St(),E=!1;try{if(await V,s||(s=E),E=!1,!await t())throw n&&pt(!1),new Error(e);nt(void 0,i=>{for(const o of Ae(i))i.removeItem(o)}),ut(),X.clear(),_="snapshot"}catch(i){s||(s=E);for(const o of X)$(o)?Rt(o,P[o]):Mt(o);throw X.clear(),_=r,E=s,E&&J(),i}}function Oe(t){const e=ee.then(t);return ee=e.catch(()=>{}),e}function kn(t){const e=t===null?null:Ot(t);if(!(t!==null&&e===null)){if(_==="transaction"){kt(e),E=!0;return}_||!z()||!F()||(kt(e),J())}}function vn(t){if(!Xt(t))return null;if(!z())return ne(t);if(!F()||_)return null;if($(t))return P[t]??null;const e=ne(t);return e===null||yt()||(P[t]=e,J()),e}function Cn(t,e){if(!(!Xt(t)||e.length>Bt)){if(!z())return Rt(t,e);if(_==="transaction"){if(!$(t)&&yt())return;E||(E=P[t]!==e),P[t]=e,X.add(t);return}!F()||_||!$(t)&&yt()||(Rt(t,e),P[t]!==e&&(P[t]=e,J()))}}function Rn(t){if(Xt(t)){if(!z())return Mt(t);if(_==="transaction"){E||(E=$(t)),delete P[t],X.add(t);return}!F()||_||(Mt(t),$(t)&&(delete P[t],J()))}}async function Mn(){St();let t=et;for(let e=0;e<In;e+=1){for(;;){const n=V;if(await n,n===V)break}if(!E){if(t!==void 0)throw t;return}if(!Vt()){const n=et??t;if(n!==void 0)throw n;return}try{await Ie(),t=void 0}catch(n){t=n}}if(await V,!!E)throw t??new Error("Client state remained dirty after the final flush attempt")}async function Kn(){await Oe(async()=>{if(!z())throw new Error("Client state is not owned by this window");await be(Te,"Native client state clear was rejected",!0)})}async function Ln(t){await Oe(async()=>{if(!(t===F()&&(!t||_===!1))){if(!z())throw new Error("Client state is not owned by this window");if(t){if(!await Ft(!0))throw new Error("Native restore preference update was rejected");_=!1,dt(!0);return}dt(!1);try{await be(()=>Ft(!1),"Native restore preference update was rejected")}catch(e){throw dt(!0),e}}})}function zn(){return it||(it=(async()=>{try{const t=await Pe();if(Ct=t.partitionProtocolVersion,pt(t.isPrimary),dt(t.restoreEnabled),ut(null,!0),E=!1,et=void 0,_=!1,X.clear(),vt=!0,!t.isPrimary||!t.restoreEnabled)return;let e;if(gn(t.snapshot)){_="snapshot";try{e=await wn(t.snapshot,t.partitionProtocolVersion,je)}catch(n){e=null,console.warn("[client-state] failed to load client snapshot partition",n)}e&&(_=!1)}else e=Dt(t.snapshot),_=t.snapshot!==null&&e===null?"snapshot":!1;ut(e,!0),!_&&Tn()&&J()}catch(t){vt=!0,pt(!1),ut(),console.warn("[client-state] failed to initialize client state",t)}})(),it)}const Fn=Object.freeze(Object.defineProperty({__proto__:null,clearRestoredClientState:Kn,clientStateIsPrimary:z,flushClientState:Mn,initializeClientState:zn,loadedClientSnapshotExists:En,loadedRestorableSession:Nt,readClientLayoutValue:vn,removeClientLayoutValue:Rn,restorePreviousStateEnabled:F,setRestorePreviousStateEnabled:Ln,updateRestorableSession:kn,writeClientLayoutValue:Cn},Symbol.toStringTag,{value:"Module"}));export{z as a,vn as b,Kn as c,Rn as d,Yn as e,Mn as f,Vn as g,$n as h,zn as i,Un as j,Fn as k,Nt as l,ae as n,F as r,Ln as s,kn as u,Cn as w};
2
+ //# sourceMappingURL=client-state-DikXRKzG.js.map