@asaidimu/utils-workspace 6.2.6 → 6.3.0

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.
package/index.d.mts CHANGED
@@ -2641,6 +2641,7 @@ declare class SessionManager {
2641
2641
  open(sessionId: UUID): Promise<Session>;
2642
2642
  close(sessionId: UUID): void;
2643
2643
  delete(sessionId: UUID): Promise<Result<undefined, WorkspaceError>>;
2644
+ has(sessionId: UUID): Promise<boolean>;
2644
2645
  create(params: {
2645
2646
  label: string;
2646
2647
  role: string;
@@ -2880,6 +2881,25 @@ interface CreateWorkspaceParams {
2880
2881
  /** @deprecated use extensions instead */
2881
2882
  extensionStores?: (ctx: Omit<WorkspaceContext, "workspace">) => Record<string, any>;
2882
2883
  }
2884
+ interface BootstrapConfig {
2885
+ workspace: {
2886
+ id: string;
2887
+ settings: {
2888
+ language: string;
2889
+ defaultRole?: string;
2890
+ prompt?: string;
2891
+ };
2892
+ project: {
2893
+ id: string;
2894
+ name: string;
2895
+ };
2896
+ };
2897
+ roles?: Role[];
2898
+ }
2899
+ interface BootstrapResult {
2900
+ workspace: Workspace;
2901
+ roles: string[];
2902
+ }
2883
2903
  /**
2884
2904
  * Boot factory for the Workspace library.
2885
2905
  * Resolves all collections and initializes entity stores.
@@ -2889,6 +2909,7 @@ declare function createWorkspace(params: CreateWorkspaceParams): Promise<{
2889
2909
  sessions: SessionManager;
2890
2910
  ctx: Omit<any, "workspace">;
2891
2911
  services: WorkspaceServices;
2912
+ bootstrap: (config: BootstrapConfig) => Promise<BootstrapResult>;
2892
2913
  }>;
2893
2914
 
2894
- export { type AddContext, type AddPreference, type AddRole, type AddSessionTopics, type AddTopic, type AddTurn, type AuthRequest, type BackendError, type BaseCommand, type BaseContentBlock, type BlobCommand, type BlobContextContent, type BlobError, type BlobMediaType, type BlobRecord, type BlobRef, type BlobResolver, type BranchInfo, type BranchTurn, COLLECTIONS, type Collections, type Command, type ContentBlock, type Context, type ContextContent, type ContextDefinition, type ContextRetriever, type ContextSummary, type CreateSession, type CreateWorkspace, type CreateWorkspaceParams, type DeepPartial, type DefaultContextContent, type DeleteContext, type DeletePreference, type DeleteRole, type DeleteSession, type DeleteTopic, type DeleteTurn, type DocumentBlock, type DocumentMediaType, type DuplicateKeyError, EMPTY_SYSTEM_ROLE, type EditTurn, type ForkSession, index as GoogleAdapter, type ImageBlock, type ImageMediaType, type Index, type IndexExtensions, type IndexedDBBlobConfig, IndexedDBBlobStorage, type Indexer, type InvalidCommandError, type JsonContextContent, type LLMAdapter, type LLMAdapterStatic, LRUCache, MemoryBlobStorage, type MergeTopics, type ModelConstraint, type ModelConstraintMap, type ModelName, type ModelProfile, type ModelRegistry, type NotFoundError, type OverrideSessionPreferences, type PermissionDeniedError, type PermissionGuard, type Preference, type PreferenceSummary, type Project, type PromptBuilder, type PromptBuilderOptions, type PurgeBlob, type RecordBlobRemoteId, type RegisterBlob, type ReleaseBlob, type RemoteContextContent, type ResolvedBlob, type ResolvedSession, type Result, type RetainBlob, type Role, type RoleSummary, type RoleTransitionBlock, type SHA256, Session, SessionManager, type SessionMetadata, type SessionSnapshot, type Settings, type Store, type Summarizer, type SummaryBlock, type SwitchSessionRole, type SyncWorkspace, type SystemActor, type TextBlock, type TextContextContent, type ThinkingBlock, type Timestamp, type ToolCall, type ToolCallCommand, type ToolRegistry, type ToolResultBlock, type ToolSummary, type ToolUseBlock, type Topic, type TopicIndex, type Turn, TurnBuilder, type TurnKey, type TurnNode, type TurnProcessor, type TurnRef, TurnTree, type URI, type UUID, type UpdateContext, type UpdatePreference, type UpdateRole, type UpdateSession, type UpdateTopic, type UpdateTurn, type Workspace, type WorkspaceBundle, type WorkspaceContext, type WorkspaceDatabase, type WorkspaceError, type WorkspaceEvents, type WorkspaceExtension, WorkspaceManager, type WorkspaceMiddleware, type WorkspaceReducer, bufferToBase64, computeSHA256, createEmptySession, createEmptyTurn, createEmptyWorkspace, createWorkspace, createWorkspaceDatabase, del, error, getExtension, merge, omitNullUndefined, shortHash, success };
2915
+ export { type AddContext, type AddPreference, type AddRole, type AddSessionTopics, type AddTopic, type AddTurn, type AuthRequest, type BackendError, type BaseCommand, type BaseContentBlock, type BlobCommand, type BlobContextContent, type BlobError, type BlobMediaType, type BlobRecord, type BlobRef, type BlobResolver, type BootstrapConfig, type BootstrapResult, type BranchInfo, type BranchTurn, COLLECTIONS, type Collections, type Command, type ContentBlock, type Context, type ContextContent, type ContextDefinition, type ContextRetriever, type ContextSummary, type CreateSession, type CreateWorkspace, type CreateWorkspaceParams, type DeepPartial, type DefaultContextContent, type DeleteContext, type DeletePreference, type DeleteRole, type DeleteSession, type DeleteTopic, type DeleteTurn, type DocumentBlock, type DocumentMediaType, type DuplicateKeyError, EMPTY_SYSTEM_ROLE, type EditTurn, type ForkSession, index as GoogleAdapter, type ImageBlock, type ImageMediaType, type Index, type IndexExtensions, type IndexedDBBlobConfig, IndexedDBBlobStorage, type Indexer, type InvalidCommandError, type JsonContextContent, type LLMAdapter, type LLMAdapterStatic, LRUCache, MemoryBlobStorage, type MergeTopics, type ModelConstraint, type ModelConstraintMap, type ModelName, type ModelProfile, type ModelRegistry, type NotFoundError, type OverrideSessionPreferences, type PermissionDeniedError, type PermissionGuard, type Preference, type PreferenceSummary, type Project, type PromptBuilder, type PromptBuilderOptions, type PurgeBlob, type RecordBlobRemoteId, type RegisterBlob, type ReleaseBlob, type RemoteContextContent, type ResolvedBlob, type ResolvedSession, type Result, type RetainBlob, type Role, type RoleSummary, type RoleTransitionBlock, type SHA256, Session, SessionManager, type SessionMetadata, type SessionSnapshot, type Settings, type Store, type Summarizer, type SummaryBlock, type SwitchSessionRole, type SyncWorkspace, type SystemActor, type TextBlock, type TextContextContent, type ThinkingBlock, type Timestamp, type ToolCall, type ToolCallCommand, type ToolRegistry, type ToolResultBlock, type ToolSummary, type ToolUseBlock, type Topic, type TopicIndex, type Turn, TurnBuilder, type TurnKey, type TurnNode, type TurnProcessor, type TurnRef, TurnTree, type URI, type UUID, type UpdateContext, type UpdatePreference, type UpdateRole, type UpdateSession, type UpdateTopic, type UpdateTurn, type Workspace, type WorkspaceBundle, type WorkspaceContext, type WorkspaceDatabase, type WorkspaceError, type WorkspaceEvents, type WorkspaceExtension, WorkspaceManager, type WorkspaceMiddleware, type WorkspaceReducer, bufferToBase64, computeSHA256, createEmptySession, createEmptyTurn, createEmptyWorkspace, createWorkspace, createWorkspaceDatabase, del, error, getExtension, merge, omitNullUndefined, shortHash, success };
package/index.d.ts CHANGED
@@ -2641,6 +2641,7 @@ declare class SessionManager {
2641
2641
  open(sessionId: UUID): Promise<Session>;
2642
2642
  close(sessionId: UUID): void;
2643
2643
  delete(sessionId: UUID): Promise<Result<undefined, WorkspaceError>>;
2644
+ has(sessionId: UUID): Promise<boolean>;
2644
2645
  create(params: {
2645
2646
  label: string;
2646
2647
  role: string;
@@ -2880,6 +2881,25 @@ interface CreateWorkspaceParams {
2880
2881
  /** @deprecated use extensions instead */
2881
2882
  extensionStores?: (ctx: Omit<WorkspaceContext, "workspace">) => Record<string, any>;
2882
2883
  }
2884
+ interface BootstrapConfig {
2885
+ workspace: {
2886
+ id: string;
2887
+ settings: {
2888
+ language: string;
2889
+ defaultRole?: string;
2890
+ prompt?: string;
2891
+ };
2892
+ project: {
2893
+ id: string;
2894
+ name: string;
2895
+ };
2896
+ };
2897
+ roles?: Role[];
2898
+ }
2899
+ interface BootstrapResult {
2900
+ workspace: Workspace;
2901
+ roles: string[];
2902
+ }
2883
2903
  /**
2884
2904
  * Boot factory for the Workspace library.
2885
2905
  * Resolves all collections and initializes entity stores.
@@ -2889,6 +2909,7 @@ declare function createWorkspace(params: CreateWorkspaceParams): Promise<{
2889
2909
  sessions: SessionManager;
2890
2910
  ctx: Omit<any, "workspace">;
2891
2911
  services: WorkspaceServices;
2912
+ bootstrap: (config: BootstrapConfig) => Promise<BootstrapResult>;
2892
2913
  }>;
2893
2914
 
2894
- export { type AddContext, type AddPreference, type AddRole, type AddSessionTopics, type AddTopic, type AddTurn, type AuthRequest, type BackendError, type BaseCommand, type BaseContentBlock, type BlobCommand, type BlobContextContent, type BlobError, type BlobMediaType, type BlobRecord, type BlobRef, type BlobResolver, type BranchInfo, type BranchTurn, COLLECTIONS, type Collections, type Command, type ContentBlock, type Context, type ContextContent, type ContextDefinition, type ContextRetriever, type ContextSummary, type CreateSession, type CreateWorkspace, type CreateWorkspaceParams, type DeepPartial, type DefaultContextContent, type DeleteContext, type DeletePreference, type DeleteRole, type DeleteSession, type DeleteTopic, type DeleteTurn, type DocumentBlock, type DocumentMediaType, type DuplicateKeyError, EMPTY_SYSTEM_ROLE, type EditTurn, type ForkSession, index as GoogleAdapter, type ImageBlock, type ImageMediaType, type Index, type IndexExtensions, type IndexedDBBlobConfig, IndexedDBBlobStorage, type Indexer, type InvalidCommandError, type JsonContextContent, type LLMAdapter, type LLMAdapterStatic, LRUCache, MemoryBlobStorage, type MergeTopics, type ModelConstraint, type ModelConstraintMap, type ModelName, type ModelProfile, type ModelRegistry, type NotFoundError, type OverrideSessionPreferences, type PermissionDeniedError, type PermissionGuard, type Preference, type PreferenceSummary, type Project, type PromptBuilder, type PromptBuilderOptions, type PurgeBlob, type RecordBlobRemoteId, type RegisterBlob, type ReleaseBlob, type RemoteContextContent, type ResolvedBlob, type ResolvedSession, type Result, type RetainBlob, type Role, type RoleSummary, type RoleTransitionBlock, type SHA256, Session, SessionManager, type SessionMetadata, type SessionSnapshot, type Settings, type Store, type Summarizer, type SummaryBlock, type SwitchSessionRole, type SyncWorkspace, type SystemActor, type TextBlock, type TextContextContent, type ThinkingBlock, type Timestamp, type ToolCall, type ToolCallCommand, type ToolRegistry, type ToolResultBlock, type ToolSummary, type ToolUseBlock, type Topic, type TopicIndex, type Turn, TurnBuilder, type TurnKey, type TurnNode, type TurnProcessor, type TurnRef, TurnTree, type URI, type UUID, type UpdateContext, type UpdatePreference, type UpdateRole, type UpdateSession, type UpdateTopic, type UpdateTurn, type Workspace, type WorkspaceBundle, type WorkspaceContext, type WorkspaceDatabase, type WorkspaceError, type WorkspaceEvents, type WorkspaceExtension, WorkspaceManager, type WorkspaceMiddleware, type WorkspaceReducer, bufferToBase64, computeSHA256, createEmptySession, createEmptyTurn, createEmptyWorkspace, createWorkspace, createWorkspaceDatabase, del, error, getExtension, merge, omitNullUndefined, shortHash, success };
2915
+ export { type AddContext, type AddPreference, type AddRole, type AddSessionTopics, type AddTopic, type AddTurn, type AuthRequest, type BackendError, type BaseCommand, type BaseContentBlock, type BlobCommand, type BlobContextContent, type BlobError, type BlobMediaType, type BlobRecord, type BlobRef, type BlobResolver, type BootstrapConfig, type BootstrapResult, type BranchInfo, type BranchTurn, COLLECTIONS, type Collections, type Command, type ContentBlock, type Context, type ContextContent, type ContextDefinition, type ContextRetriever, type ContextSummary, type CreateSession, type CreateWorkspace, type CreateWorkspaceParams, type DeepPartial, type DefaultContextContent, type DeleteContext, type DeletePreference, type DeleteRole, type DeleteSession, type DeleteTopic, type DeleteTurn, type DocumentBlock, type DocumentMediaType, type DuplicateKeyError, EMPTY_SYSTEM_ROLE, type EditTurn, type ForkSession, index as GoogleAdapter, type ImageBlock, type ImageMediaType, type Index, type IndexExtensions, type IndexedDBBlobConfig, IndexedDBBlobStorage, type Indexer, type InvalidCommandError, type JsonContextContent, type LLMAdapter, type LLMAdapterStatic, LRUCache, MemoryBlobStorage, type MergeTopics, type ModelConstraint, type ModelConstraintMap, type ModelName, type ModelProfile, type ModelRegistry, type NotFoundError, type OverrideSessionPreferences, type PermissionDeniedError, type PermissionGuard, type Preference, type PreferenceSummary, type Project, type PromptBuilder, type PromptBuilderOptions, type PurgeBlob, type RecordBlobRemoteId, type RegisterBlob, type ReleaseBlob, type RemoteContextContent, type ResolvedBlob, type ResolvedSession, type Result, type RetainBlob, type Role, type RoleSummary, type RoleTransitionBlock, type SHA256, Session, SessionManager, type SessionMetadata, type SessionSnapshot, type Settings, type Store, type Summarizer, type SummaryBlock, type SwitchSessionRole, type SyncWorkspace, type SystemActor, type TextBlock, type TextContextContent, type ThinkingBlock, type Timestamp, type ToolCall, type ToolCallCommand, type ToolRegistry, type ToolResultBlock, type ToolSummary, type ToolUseBlock, type Topic, type TopicIndex, type Turn, TurnBuilder, type TurnKey, type TurnNode, type TurnProcessor, type TurnRef, TurnTree, type URI, type UUID, type UpdateContext, type UpdatePreference, type UpdateRole, type UpdateSession, type UpdateTopic, type UpdateTurn, type Workspace, type WorkspaceBundle, type WorkspaceContext, type WorkspaceDatabase, type WorkspaceError, type WorkspaceEvents, type WorkspaceExtension, WorkspaceManager, type WorkspaceMiddleware, type WorkspaceReducer, bufferToBase64, computeSHA256, createEmptySession, createEmptyTurn, createEmptyWorkspace, createWorkspace, createWorkspaceDatabase, del, error, getExtension, merge, omitNullUndefined, shortHash, success };
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("@asaidimu/utils-events"),t=require("uuid"),s=Object.defineProperty,r=[async e=>{const t=await e.workspaceStore.list();if(0===t.length)return{};const s=t[0];return{id:s.id,settings:s.settings,project:s.project}},async e=>{const t=await e.roles.list(),s={};for(const r of t)s[r.name]=e.roles.summarize(r);return{index:{roles:s}}},async e=>{const t=await e.preferences.list(),s={};for(const r of t)s[r.id]=e.preferences.summarize(r);return{index:{preferences:s}}},async e=>{const t=await e.context.list(),s={};for(const r of t)s[r.key]=e.context.summarize(r);return{index:{context:s}}},async e=>{const t=await e.sessions.list(),s={};for(const r of t)s[r.id]=e.sessions.summarize(r);return{index:{sessions:s}}},async e=>{const t=await e.topics.list(),s={};for(const r of t)s[r.name]=e.topics.summarize(r);return{index:{topics:s}}},async e=>({index:{blobs:await e.blobs.getAllRecords()}})],n=[{name:"workspace",version:"1.0.0",description:"Workspace root containing global settings and project metadata.",fields:{id:{name:"id",type:"string",required:!0},settings:{name:"settings",type:"record",required:!0},project:{name:"project",type:"record",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"}],constraints:[],migrations:[]},{name:"role",version:"1.0.0",description:"AI persona with a system prompt and associated preference defaults.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!0},description:{name:"description",type:"string",required:!1},persona:{name:"persona",type:"string",required:!0},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},topics:{name:"topics",type:"array",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_name",fields:["name"],type:"unique"},{name:"by_label",fields:["label"],type:"normal"}],constraints:[],migrations:[]},{name:"preference",version:"1.0.0",description:"A user behavioural instruction, scoped to zero or more topics.",fields:{id:{name:"id",type:"string",required:!0},content:{name:"content",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},timestamp:{name:"timestamp",type:"string",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"context",version:"1.0.0",description:"Injected background knowledge, scoped to topics. Content is a discriminated union.",fields:{key:{name:"key",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},content:{name:"content",type:"record",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},metadata:{name:"metadata",type:"record",required:!1}},indexes:[{name:"by_key",fields:["key"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"session",version:"1.0.0",description:"Session metadata. The head field tracks the current tip of the turn DAG.",fields:{id:{name:"id",type:"string",required:!0},label:{name:"label",type:"string",required:!0},role:{name:"role",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},metadata:{name:"metadata",type:"record",required:!1},head:{name:"head",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_role",fields:["role"],type:"normal"}],constraints:[],migrations:[]},{name:"turn",version:"1.0.0",description:["A single message in a session transcript, stored as a flat document.","The DAG is reconstructed in memory by TurnTree.buildNodeGraph() at session open time."].join(" "),fields:{id:{name:"id",type:"string",required:!0},session:{name:"session",type:"string",required:!0},version:{name:"version",type:"number",required:!0},actor:{name:"actor",type:"enum",required:!0,values:["user","assistant","tool"]},blocks:{name:"blocks",type:"array",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},role:{name:"role",type:"string",required:!1},parent:{name:"parent",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_session",fields:["session"],type:"normal"},{name:"by_session_parent",fields:["session","parent"],type:"composite"},{name:"by_session_id_ver",fields:["session","id","version"],type:"composite",unique:!0}],constraints:[],migrations:[]},{name:"topic",version:"1.0.0",description:"A categorization for context and preferences, used for retrieval.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!1},description:{name:"description",type:"string",required:!1},metadata:{name:"metadata",type:"record",required:!1},created:{name:"created",type:"string",required:!0},updated:{name:"updated",type:"string",required:!0}},indexes:[{name:"by_name",fields:["name"],type:"unique"}],constraints:[],migrations:[]}],i=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},o=class extends i{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},a=class extends i{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},c=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let r;await Promise.race([s.then((()=>clearTimeout(r))),new Promise(((s,n)=>{r=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new o("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},d=class{mutex=new c({yieldMode:"microtask"});promise=null;_value=null;_error;_done=!1;retry;throws;constructor({retry:e,throws:t}={}){this.retry=Boolean(e),this.throws=Boolean(t)}async do(e,t){return this._done?this.peek():this.promise?this._awaitWithTimeout(this.promise,t,"Once do() timed out"):(await this.mutex.lock(),this.promise?(this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")):(this.promise=(async()=>{try{this._value=await e(),this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.retry&&!this._done&&(this.promise=null)}return this.peek()})(),this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")))}doSync(e){if(this._done){if(this.throws&&this._error)throw this._error;return this.peek()}if(this.promise){const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}if(!this.mutex.tryLock()){const e=new Error("Cannot execute doSync: lock is currently held.");if(this.throws)throw e;return{value:null,error:e}}if(this.promise||this._done){if(this.mutex.unlock(),this._done){if(this.throws&&this._error)throw this._error;return this.peek()}const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}try{const t=e();this._value=t,this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.mutex.unlock()}return this.peek()}running(){return null!==this.promise&&!this.done()}peek(){return{value:this._value,error:this._error}}get(){if(!this._done)throw new Error("Once operation is not yet complete");if(this._error)throw this._error;return this._value}reset(){if(this.running())throw new Error("Cannot reset Once while an operation is in progress.");this._done=!1,this.promise=null,this._value=null,this._error=void 0}done(){return this._done}current(){return this.promise}_awaitWithTimeout(e,t,s="Operation timed out"){if(null==t)return e;let r;return Promise.race([e.then((e=>(clearTimeout(r),e))),new Promise(((e,n)=>{r=setTimeout((()=>n(new o(s))),t)}))])}},u=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new c({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new a};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,r=null;try{if(this._done)throw new a;r=await e(),this._lastValue=r,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:r,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}};function l(e){const t=new d({retry:!0,throws:!0}),s=new d({retry:!0,throws:!0});return{open:async(s=[])=>(await t.do((async()=>{const t=[...n,...s];await e.setupCollections(t)}))).value,collection:async t=>e.collection(t),close:async()=>(await s.do((()=>{e.close()}))).value,database:()=>e}}var p={WORKSPACE:"workspace",ROLE:"role",PREFERENCE:"preference",CONTEXT:"context",SESSION:"session",TURN:"turn",BLOB:"blob",TOPIC:"topic"},h=Symbol.for("delete"),m=e=>Array.isArray(e)?[...e]:{...e};function f(e){return{ok:!0,value:e}}function y(e){return{ok:!1,error:e}}var w=function(e){const t=e?.deleteMarker||h;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const r={};for(const[n,i]of Object.entries(e))if(i!==t)if("object"==typeof i&&null!==i){const e=s(i);void 0!==e&&(r[n]=e)}else r[n]=i;return r}return e===t?void 0:e}return function(e,r){if("object"!=typeof e||null===e)return"object"==typeof r&&null!==r?s(r):r===t?{}:r;if("object"!=typeof r||null===r)return e;const n=m(e),i=[{target:n,source:r}];for(;i.length>0;){const{target:e,source:s}=i.pop();for(const r of Object.keys(s)){const n=s[r];if(n!==t)if(Array.isArray(n))e[r]=n;else if("object"==typeof n&&null!==n){const t=r in e&&"object"==typeof e[r]&&null!==e[r]?e[r]:{};e[r]=m(t),i.push({target:e[r],source:n})}else e[r]=n;else delete e[r]}}return n}}({deleteMarker:h});async function g(e){const t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t)).map((e=>e.toString(16).padStart(2,"0"))).join("")}function b(e){if("undefined"!=typeof Buffer)return Buffer.from(e).toString("base64");const t=[];for(let s=0;s<e.length;s+=32768){const r=e.subarray(s,s+32768);t.push(String.fromCharCode.apply(null,Array.from(r)))}return btoa(t.join(""))}var k=class{registry=new Map;middlewares=[];serializer=new u;bus;_getWorkspace;updateWorkspace;guard;_ctx;constructor(e){this._ctx=e.ctx,this._getWorkspace=e.getWorkspace,this.updateWorkspace=e.updateWorkspace,this.guard=e.guard,this.bus=e.bus}register(e,t){return this.registry.set(e,t),this}use(e){return this.middlewares.push(e),this}workspace(){return this._getWorkspace()}async dispatch(e){const t=await this.serializer.do((async()=>{if(this.guard){const t=await this.guard.authenticate({type:"command",payload:e});if(!t.ok)return y(t.error)}const t=this.registry.get(e.type);if(!t)return y({code:"INVALID_COMMAND",reason:`No reducer registered for command type: ${e.type}`});const s=this._getWorkspace(),r=await t({workspace:s,...this._ctx},e.payload);if(!r.ok)return r;let n=r.value;for(const t of this.middlewares){const r=await t({workspace:s,command:e,patch:n,...this._ctx});n=w(n,r)}return await this.updateWorkspace(n),this.bus.emit({name:"workspace:changed",payload:n}),"workspace:sync"===e.type&&this.bus.emit({name:"workspace:synced",payload:void 0}),f(n)}));if(t.error)throw t.error;return t.value}subscribe(e,t){return this.bus.subscribe(e,t)}ctx(){return{workspace:this._getWorkspace(),...this._ctx}}},x=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async getMany(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?t.push(e):s.push(r)}if(s.length>0){const e=await this.collection.filter({operator:"or",conditions:s.map((e=>({field:"name",operator:"eq",value:e})))});for(const s of e){const e=s.state();this.cache.set(e.name,e),t.push(e)}}return t}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}referencedBy(e,t){for(const s in t.roles)if(t.roles[s].topics?.includes(e))return!0;for(const s in t.sessions)if(t.sessions[s].topics?.includes(e))return!0;for(const s in t.context)if(t.context[s].topics?.includes(e))return!0;for(const s in t.preferences)if(t.preferences[s].topics?.includes(e))return!0;return!1}summarize(e){return{topic:e.name,contextKeys:[],preferences:[],metadata:{created:e.created,updated:e.updated,entries:0}}}};function v(e,t,s,r,n,i){const o={};for(const a of t){let t=e[a];if(!t){if("add"!==n||!i)continue;t={topic:a,contextKeys:[],preferences:[],metadata:{created:(new Date).toISOString(),updated:(new Date).toISOString(),entries:0}},i.add({name:a,created:t.metadata.created,updated:t.metadata.updated})}const c=t[r],d=c.includes(s);if("add"!==n||d){if("remove"===n&&d){const e=Math.max(0,(t.metadata.entries||0)-1);o[a]={...t,[r]:c.filter((e=>e!==s)),metadata:{...t.metadata,updated:(new Date).toISOString(),entries:e}}}}else o[a]={...t,[r]:[...c,s],metadata:{...t.metadata,updated:(new Date).toISOString(),entries:(t.metadata.entries||0)+1}}}return{index:{topics:o}}}function _(e,t,s,r){return v(e,t,s,"contextKeys","add",r)}function O(e,t,s){return v(e,t,s,"contextKeys","remove")}function S(e,t,s,r){return v(e,t,s,"preferences","add",r)}function T(e,t,s){return v(e,t,s,"preferences","remove")}var N=async e=>{if(!e.patch)return;const{workspace:t,patch:s}=e,r=t.index.topics;let n={};const i=e=>{const t=e?.index?.topics;t&&(n=w(n,t))};if(s.index?.context)for(const[n,o]of Object.entries(s.index.context)){const s=t.index.context[n];if(void 0===o&&s)i(O(r,s.topics,s.key));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(O(r,c,s.key)),a.length&&i(_(r,a,s.key,e.topics))}}else i(_(r,o.topics??[],o.key,e.topics))}if(s.index?.preferences)for(const[n,o]of Object.entries(s.index.preferences)){const s=t.index.preferences[n];if(void 0===o&&s)i(T(r,s.topics,s.id));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(T(r,c,s.id)),a.length&&i(S(r,a,s.id,e.topics))}}else i(S(r,o.topics??[],o.id,e.topics))}return Object.keys(n).length?{index:{topics:n}}:{}},R="\n# WORKSPACE OPERATING SYSTEM\nYou are operating within a structured workspace. Your output MUST be a JSON object matching the provided schema.\nYour entire response MUST be valid JSON. Nothing else.\n".trim();function D(e){if(!e||"append"===e)return{placement:"append",label:null};if("prepend"===e)return{placement:"prepend",label:null};const[t,...s]=e.split(":");return("before"===t||"after"===t)&&s.length>0?{placement:t,label:s.join(":")}:(console.warn(`[SystemPromptAssembler] Unrecognised position "${e}", defaulting to "append".`),{placement:"append",label:null})}var I=class{build(e,t=[]){const s=[...e.system.extensions||[],...t],r=[];var n,i,o;r.push({label:"operating-system",content:R}),e.role.persona&&r.push({label:"persona",content:(n=e.role.persona,`# Persona\n${n}`)}),e.system.preferences.length>0&&r.push({label:"preferences",content:(i=e.system.preferences,`# User Preferences\n${i.map((e=>`- ${e.content}`)).join("\n")}`),metadata:{count:e.system.preferences.length}}),e.system.context.length>0&&r.push({label:"context",content:(o=e.system.context,`# Context\n${o.map((e=>{switch(e.content.kind){case"text":return`[${e.key}] ${e.content.value}`;case"json":return`[${e.key}] JSON: ${JSON.stringify(e.content.value)}`;case"blob":return`[${e.key}] Blob: ${e.content.filename??"unnamed"} (${e.content.mediaType}, ${e.content.sizeBytes} bytes)`;case"remote":return`[${e.key}] Remote: ${e.content.uri}`;default:return`[${e.key}] Unsupported context type`}})).join("\n")}`),metadata:{count:e.system.context.length}}),e.system.instructions&&r.push({label:"instructions",content:`# Instructions\n${e.system.instructions}`});const a=[...r],c=[];for(const e of s){const{position:t,...s}=e,r=D(t);if("prepend"===r.placement){c.push(s);continue}if("append"===r.placement){a.push(s);continue}const n=a.findIndex((e=>e.label===r.label));if(-1===n){console.warn(`[SystemPromptAssembler] Anchor target "${r.label}" not found in sections. Extension "${e.label}" will be appended.`),a.push(s);continue}const i="before"===r.placement?n:n+1;a.splice(i,0,s)}return[...c,...a]}join(e){return e.map((e=>e.content)).join("\n\n---\n\n")}};var E={"workspace:create":async function(e,t){const{workspaceStore:s}=e,{id:r,settings:n,project:i}=t;return await s.add({id:r,settings:n,project:i}),f({id:r,settings:n,project:i})},"workspace:sync":async function(e,t){let s={};t&&(t.id&&(s.id=t.id),t.settings&&(s.settings=t.settings),t.project&&(s.project=t.project));for(const t of e.indexers){const r=await t(e);s=w(s,r)}return f(s)},"role:add":async function(e,t){const{workspace:s,roles:r}=e,n=t;return s.index.roles[n.name]?y({code:"DUPLICATE_KEY",resource:"Role",key:n.name}):(await r.add(n),f({index:{roles:{[n.name]:r.summarize(n)}}}))},"role:update":async function(e,t){const{workspace:s,roles:r}=e,{name:n,...i}=t;if(!s.index.roles[n])return y({code:"NOT_FOUND",resource:"Role",id:n});const o=await r.update(n,i);return o?f({index:{roles:{[n]:r.summarize(o)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update role ${n} in store.`})},"role:delete":async function(e,t){const{workspace:s,roles:r}=e,{name:n}=t;return s.index.roles[n]?await r.delete(n)?f({index:{roles:{[n]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete role ${n} from store.`}):y({code:"NOT_FOUND",resource:"Role",id:n})},"preference:add":async function(e,t){const{workspace:s,preferences:r}=e,n=t;return s.index.preferences[n.id]?y({code:"DUPLICATE_KEY",resource:"Preference",key:n.id}):(await r.add(n),f({index:{preferences:{[n.id]:r.summarize(n)}}}))},"preference:update":async function(e,t){const{workspace:s,preferences:r}=e,{id:n,...i}=t;if(!s.index.preferences[n])return y({code:"NOT_FOUND",resource:"Preference",id:n});const o=await r.update(n,i);return o?f({index:{preferences:{[n]:r.summarize(o)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update preference ${n} in store.`})},"preference:delete":async function(e,t){const{workspace:s,preferences:r}=e,{id:n}=t;return s.index.preferences[n]?await r.delete(n)?f({index:{preferences:{[n]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete preference ${n} from store.`}):y({code:"NOT_FOUND",resource:"Preference",id:n})},"context:add":async function(e,t){const{workspace:s,context:r}=e,n=t;return s.index.context[n.key]?y({code:"DUPLICATE_KEY",resource:"Context",key:n.key}):(await r.add(n),f({index:{context:{[n.key]:r.summarize(n)}}}))},"context:update":async function(e,t){const{workspace:s,context:r}=e,{key:n,...i}=t,o=s.index.context[n];if(!o)return y({code:"NOT_FOUND",resource:"Context",id:n});const a=await r.update(n,i,o.kind);return a?f({index:{context:{[n]:r.summarize(a)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update context ${n} in store.`})},"context:delete":async function(e,t){const{workspace:s,context:r}=e,{key:n}=t,i=s.index.context[n];return i?await r.delete(n,i.kind)?f({index:{context:{[n]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete context ${n} from store.`}):y({code:"NOT_FOUND",resource:"Context",id:n})},"topic:add":async function(e,t){const{workspace:s,topics:r}=e,n=t;return s.index.topics[n.name]?y({code:"DUPLICATE_KEY",resource:"Topic",key:n.name}):(await r.add(n),f({index:{topics:{[n.name]:r.summarize(n)}}}))},"topic:update":async function(e,t){const{workspace:s,topics:r}=e,{name:n,...i}=t;if(!s.index.topics[n])return y({code:"NOT_FOUND",resource:"Topic",id:n});const o=await r.update(n,i);return o?f({index:{topics:{[n]:{metadata:{updated:o.updated}}}}}):y({code:"BACKEND_ERROR",reason:`Failed to update topic ${n} in store.`})},"topic:delete":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{name:o,cascade:a}=t;if(!s.index.topics[o])return y({code:"NOT_FOUND",resource:"Topic",id:o});if(r.referencedBy(o,s.index)&&!a)return y({code:"INVALID_COMMAND",reason:`Topic '${o}' is referenced by other entities. Use 'cascade: true' to force deletion.`});if(a){for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);t&&await n.update(e,{topics:t.topics.filter((e=>e!==o))})}}for(const e in s.index.sessions){const t=s.index.sessions[e];t.topics?.includes(o)&&await i.update(e,{topics:t.topics.filter((e=>e!==o))})}}return await r.delete(o)?f({index:{topics:{[o]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete topic ${o} from store.`})},"topic:merge":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{source:o,target:a}=t;if(!s.index.topics[o]||!s.index.topics[a])return y({code:"NOT_FOUND",resource:"Topic",id:s.index.topics[o]?a:o});for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);if(t){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await n.update(e,{topics:s})}}}for(const e in s.index.sessions){const t=s.index.sessions[e];if(t.topics?.includes(o)){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await i.update(e,{topics:s})}}return await r.delete(o),f({index:{topics:{[o]:h}}})},"session:create":async function(e,t){const{workspace:s,sessions:r}=e,n=t;return s.index.sessions[n.id]?y({code:"DUPLICATE_KEY",resource:"Session",key:n.id}):(await r.add(n),f({index:{sessions:{[n.id]:r.summarize(n)}}}))},"session:update":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,...i}=t;if(!s.index.sessions[n])return y({code:"NOT_FOUND",resource:"Session",id:n});const o=await r.update(n,i);return o?f({index:{sessions:{[n]:r.summarize(o)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update session ${n} in store.`})},"session:fork":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,newSessionId:i,label:o}=t,a=s.index.sessions[n];if(!a)return y({code:"NOT_FOUND",resource:"Session",id:n});const c={...a,id:i,label:o||`Fork of ${a.label}`,role:t.role?t.role:a.role,topics:t.topics?t.topics:a.topics};return await r.add(c),f({index:{sessions:{[i]:r.summarize(c)}}})},"session:delete":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n}=t;return s.index.sessions[n]?(await r.delete(n),f({index:{sessions:{[n]:h}}})):y({code:"NOT_FOUND",resource:"Session",id:n})},"session:role:switch":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,roleName:i}=t;return s.index.sessions[n]?(await r.update(n,{role:i}),f({index:{sessions:{[n]:{role:i}}}})):y({code:"NOT_FOUND",resource:"Session",id:n})},"session:topics:add":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,topics:i}=t,o=s.index.sessions[n];if(!o)return y({code:"NOT_FOUND",resource:"Session",id:n});const a=Array.from(new Set([...o.topics,...i]));return await r.update(n,{topics:a}),f({index:{sessions:{[n]:{topics:a}}}})},"session:preferences:override":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,preferences:i}=t;return s.index.sessions[n]?(await r.update(n,{preferences:i}),f({index:{sessions:{[n]:{preferences:i}}}})):y({code:"NOT_FOUND",resource:"Session",id:n})},"turn:add":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return y({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})},"turn:update":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return y({code:"NOT_FOUND",resource:"Session",id:i});await n.update(o,o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})},"turn:edit":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newBlocks:a,newVersion:c,roleSnapshot:d}=t,u=s.index.sessions[i];if(!u)return y({code:"NOT_FOUND",resource:"Session",id:i});const l=u.head;let p={};if(l&&l.id===o){const e=await n.get({id:l.id,version:l.version,session:i});e&&(p={...e})}const h={...p,id:o,version:c,blocks:a,role:d??p.role,timestamp:(new Date).toISOString(),parent:p.parent,actor:p.actor||"assistant",session:i};if(await n.add(h),u.head?.id===o){const e={id:o,version:c};return await r.update(i,{head:e}),f({index:{sessions:{[i]:{head:e}}}})}return f({})},"turn:branch":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return y({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})},"turn:delete":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newHead:a}=t;return s.index.sessions[i]?(await n.delete({session:i,id:o,version:t.version}),a?(await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})):f({})):y({code:"NOT_FOUND",resource:"Session",id:i})},"blob:register":async function(e,t){const{blobs:s}=e,r=await s.register(t.data,t.mediaType,t.filename);if(!r.ok)return r;const n=await s.getRecord(r.value.sha256);return n?f({index:{blobs:{[n.sha256]:n}}}):y({code:"BACKEND_ERROR",reason:`Failed to retrieve registered blob record for ${r.value.sha256}`})},"blob:retain":async function(e,t){const{blobs:s}=e,r=await s.retain(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return f({index:{blobs:{[t.sha256]:n}}})},"blob:release":async function(e,t){const{blobs:s}=e,r=await s.release(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return f({index:{blobs:{[t.sha256]:n||h}}})},"blob:purge":async function(e,t){const{blobs:s}=e,r=await s.purge(t.sha256);return r.ok?f({index:{blobs:{[t.sha256]:h}}}):r},"blob:record_remote_id":async function(e,t){const{blobs:s}=e,{sha256:r,providerId:n,fileId:i,timestamp:o}=t,a=await s.recordRemoteId(r,n,i,o);if(!a.ok)return a;const c=await s.getRecord(r);return f({index:{blobs:{[r]:c}}})},"tool:call":async function(e,t){return f({})}},j=class{profiles;constructor(e=[]){this.profiles=new Map;for(const t of e)this.profiles.set(t.name,t)}get(e){return this.profiles.get(e)}list(e){const t=Array.from(this.profiles.values());return e?t.filter((t=>t.provider===e)):t}register(e){this.profiles.set(e.name,e)}},B=[{type:"text",emittable:!0,description:["## Block: `text`","Primary communication block. Use for all conversational responses, explanations, and prose.","A response may contain multiple `text` blocks interleaved with other block types."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["text"]},text:{type:"string",description:"The raw markdown or plain text content of the response."}},required:["type","text"]}},{type:"summary",emittable:!0,description:["## Block: `summary`","Context compression block. Replaces older conversation history with a concise digest."].join("\n"),rules:["Only emit when explicitly instructed by the system to summarise.","Never emit more than one summary block per turn.","Preserve key decisions, outcomes, and unresolved questions.","Do not summarise the current turn — only prior history."],schema:{type:"object",properties:{type:{type:"string",enum:["summary"]},text:{type:"string",description:"A concise summary replacing older conversation context."}},required:["type","text"]}},{type:"thinking",emittable:!1,description:["## Block: `thinking`","Internal chain-of-thought produced by the model before its final response.","Injected by the workspace from provider reasoning tokens — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["thinking"]},thinking:{type:"string",description:"The internal reasoning text produced before the final response."}},required:["type","thinking"]}},{type:"tool:use",emittable:!1,description:["## Block: `tool:use`","Captures a model request to execute a registered tool.","Constructed by the workspace from the provider native function-call response — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:use"]},name:{type:"string",description:"The exact registered name of the tool to execute."},input:{type:"object",description:"The parameter arguments for the tool execution.",additionalProperties:!0}},required:["type","name","input"]}},{type:"tool:result",emittable:!1,description:["## Block: `tool:result`","Contains the output of a tool execution, including any errors.","Injected by the workspace after a tool runs — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:result"]},useId:{type:"string",description:"The UUID of the ToolUseBlock that triggered this execution."},content:{oneOf:[{type:"string"},{type:"object",additionalProperties:!0}],description:"The serialized output of the tool, or a structured JSON response."},isError:{type:"boolean",description:"True if the tool execution resulted in an error."}},required:["type","useId","content"]}},{type:"image",emittable:!1,description:["## Block: `image`","An image asset attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["image"]},altText:{type:"string",description:"Accessible description of the image content."}},required:["type"]}},{type:"document",emittable:!1,description:["## Block: `document`","A document asset (PDF, DOCX, etc.) attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["document"]},title:{type:"string",description:"Human-readable title or filename of the document."}},required:["type"]}},{type:"role:transition",emittable:!1,description:["## Block: `role:transition`","Marks that the active session persona has switched from one role to another.","Emitted by the workspace when the active persona changes — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["role:transition"]},previousRole:{type:"string",description:"The name of the role the session is leaving."},newRole:{type:"string",description:"The name of the role the session is adopting."}},required:["type","newRole"]}}];function A(e,t){let s=e.filter((e=>e.emittable));if(!t)return s;if(t.only&&t.only.length>0){const e=new Set(t.only);return s.filter((t=>e.has(t.type)))}if(t.exclude&&t.exclude.length>0){const e=new Set(t.exclude);return s.filter((t=>!e.has(t.type)))}return s}var C=class{store=new Map;constructor(){for(const e of B)this.store.set(e.type,e)}register(e){if(this.store.has(e.type))throw new Error(`[BlockRegistry] Block type "${e.type}" is already registered. Use update() to modify it.`);this.store.set(e.type,{...e,emittable:!0,rules:[]})}unregister(e){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot unregister unknown block type "${e}".`);this.store.delete(e)}update(e,t){const s=this.store.get(e);s?this.store.set(e,{...s,...t,type:e}):this.store.set(e,{emittable:!0,rules:[],...t,type:e})}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}isType(e,t){return e.type===t}guard(e){return t=>t.type===e}schema(e){return{type:"object",description:"The model's structured response, containing an ordered sequence of content blocks.",properties:{blocks:{type:"array",description:"An ordered array of content blocks comprising the model's response.",items:{anyOf:A(Array.from(this.store.values()),e).map((e=>e.schema))}}},required:["blocks"]}}description(e){return["# BLOCK TYPES & USAGE RULES","","Your response MUST be a JSON object with a `blocks` array.","Each element of `blocks` must be one of the following types:","",A(Array.from(this.store.values()),e).map((e=>{const t=[e.description];return e.rules.length>0&&(t.push("**Rules:**"),t.push(...e.rules.map((e=>`- ${e}`)))),t.join("\n")})).join("\n\n"),"","## GLOBAL CONSTRAINTS","","- Output must be **valid JSON only** — no prose before or after the JSON object.","- Responses may contain multiple blocks. Blocks are rendered in order.","- **Do NOT generate IDs.** The workspace is solely responsible for block ID assignment.","- Do not infer or invent fields not defined in the block schemas above."].join("\n")}rules(e){const t=A(Array.from(this.store.values()),e);return Object.fromEntries(t.map((e=>[e.type,[...e.rules]])))}setRules(e,t){const s=this.store.get(e);if(!s)throw new Error(`[BlockRegistry] Cannot set rules on unknown block type "${e}".`);this.store.set(e,{...s,rules:t})}parse(e,s){const r=this.store.get(e.type);if(!r)return console.warn(`[BlockRegistry] parse() received unknown block type "${e.type}". Returning null.`),null;if(s&&r.mappings?.[s])return r.mappings[s].from(e);const{id:n,...i}=e;return{id:t.v7(),...i}}create(e,s){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot create block of unknown type "${e}".`);return{...s,id:t.v7(),type:e}}clone(e,s){return{...structuredClone(e),...s,id:t.v7()}}},q=class{store=new Map;constructor(){this.registerDefaults()}registerDefaults(){this.register({kind:"text",target:"system",render:e=>({id:e.key,type:"text",text:e.content.value}),toString:e=>e.content.value,summarize:e=>({key:e.key,kind:"text",topics:e.topics,timestamp:e.timestamp,preview:e.content.value.slice(0,100)})}),this.register({kind:"json",target:"system",render:e=>({id:e.key,type:"text",text:JSON.stringify(e.content.value,null,2)}),toString:e=>JSON.stringify(e.content.value),summarize:e=>({key:e.key,kind:"json",topics:e.topics,timestamp:e.timestamp,preview:"JSON Data"})}),this.register({kind:"blob",target:"transcript",render:e=>{const{sha256:t,mediaType:s,sizeBytes:r,filename:n}=e.content,i={sha256:t,mediaType:s,sizeBytes:r,filename:n};return s.startsWith("image/")?{id:e.key,type:"image",ref:i}:{id:e.key,type:"document",ref:i,title:n}},toString:e=>e.content.filename||"unnamed blob",summarize:e=>({key:e.key,kind:"blob",topics:e.topics,timestamp:e.timestamp,mime:e.content.mediaType,size:e.content.sizeBytes,preview:e.content.filename})})}register(e){if(this.store.has(e.kind))throw new Error(`[ContextRegistry] Context kind "${e.kind}" is already registered.`);this.store.set(e.kind,e)}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}},U="__system__",$=class{constructor(e,s,r){this._actor=e,this._turn=r?JSON.parse(JSON.stringify(r)):{id:t.v7(),session:s,version:0,actor:this._actor,blocks:[],timestamp:(new Date).toISOString(),role:void 0}}_turn;addText(e){const s={id:t.v7(),type:"text",text:e};return this._turn.blocks.push(s),this}addImage(e,s){const r={id:t.v7(),type:"image",ref:e,altText:s};return this._turn.blocks.push(r),this}addDocument(e,s){const r={id:t.v7(),type:"document",ref:e,title:s};return this._turn.blocks.push(r),this}addToolUse(e,s){const r={id:t.v7(),type:"tool:use",name:e,input:s};return this._turn.blocks.push(r),this}addToolResult(e,s,r){const n={id:t.v7(),type:"tool:result",useId:e,content:s,isError:r};return this._turn.blocks.push(n),this}addSummary(e){const s={id:t.v7(),type:"summary",text:e};return this._turn.blocks.push(s),this}addRoleTransition(e,s){const r={id:t.v7(),type:"role:transition",previousRole:e,newRole:s};return this._turn.blocks.push(r),this}addThinking(e){const s={id:t.v7(),type:"thinking",thinking:e};return this._turn.blocks.push(s),this}addBlock(e){return this._turn.blocks.push(e),this}deleteBlock(e){return this._turn.blocks=this._turn.blocks.filter((t=>t.id!==e)),this}editTextBlock(e,t){const s=this._turn.blocks.findIndex((t=>t.id===e));if(-1===s)throw new Error(`Block with ID ${e} not found.`);const r=this._turn.blocks[s];if("text"!==r.type)throw new Error(`Block with ID ${e} is not a TextBlock.`);return this._turn.blocks[s]={...r,text:t},this}withId(e){return this._turn.id=e,this}withVersion(e){return this._turn.version=e,this}withTimestamp(e){return this._turn.timestamp=e,this}withParent(e){return this._turn.parent=e,this}withRoleSnapshot(e){return this._turn.role=e,this}build(){return JSON.parse(JSON.stringify(this._turn))}},M=class{constructor(e,t){this.turnStore=e,this.sessionStore=t}async loadAllTurns(e){return this.turnStore.listBySession(e)}async loadHead(e){const t=await this.sessionStore.get(e);return t?.head??null}},z=class e{constructor(e,t){this.nodes=e,this._head=t}static async build(t,s){const[r,n]=await Promise.all([s.loadAllTurns(t),s.loadHead(t)]),i=function(e,t){const s=function(e,t){if(!t)return new Set;const s=new Map;for(const t of e)s.set(`${t.id}:${t.version}`,t);const r=new Set;let n=t;for(;n;){const e=`${n.id}:${n.version}`;if(r.has(e))break;r.add(e);const t=s.get(e);if(!t)break;n=t.parent}return r}(e,t),r={},n={};for(const t of e){r[t.id]||(r[t.id]={id:t.id,versions:{},activeVersion:t.version,actor:t.actor,blocks:t.blocks,timestamp:t.timestamp,roleSnapshot:t.role,parent:t.parent,children:{}},n[t.id]=new Set);const e=r[t.id];e.versions[t.version]=t;const i=s.has(`${t.id}:${t.version}`),o=s.has(`${t.id}:${e.activeVersion}`);if(i&&(!o||t.version>=e.activeVersion)&&(e.activeVersion=t.version,e.blocks=t.blocks,e.timestamp=t.timestamp,e.roleSnapshot=t.role,e.parent=t.parent),t.parent){const e=`${t.parent.id}:${t.parent.version}`;if(n[t.parent.id]||(n[t.parent.id]=new Set),!n[t.parent.id].has(e+":"+t.id)){n[t.parent.id].add(e+":"+t.id),r[t.parent.id]||(r[t.parent.id]={id:t.parent.id,versions:{},activeVersion:t.parent.version,actor:"user",blocks:[],timestamp:"",roleSnapshot:void 0,children:{}});const s=r[t.parent.id];s.children[t.parent.version]||(s.children[t.parent.version]=[]),s.children[t.parent.version].push(t.id)}}}return r}(r,n);return new e(i,n)}head(){return this._head}chain(){if(!this._head)return[];const e=[];let t=this._head.id;for(;t;){const s=this.nodes[t];if(!s)break;e.push(s),t=s.parent?.id??null}return e.reverse()}getTurnSiblings(e){const t=this.nodes[e];if(!t)return[];if(!t.parent)return Object.values(this.nodes).filter((e=>!e.parent));const s=this.nodes[t.parent.id];if(!s)return[t];const r=s.children[t.parent.version];return r?r.map((e=>this.nodes[e])).filter((e=>!!e)):[t]}branchInfo(e){const t=this.nodes[e];if(!t)return{versions:[],currentIndex:-1,total:0,hasPrev:!1,hasNext:!1};const s=Object.keys(t.versions).map(Number).sort(((e,t)=>e-t)),r=s.indexOf(t.activeVersion);return{versions:s,currentIndex:r,total:s.length,hasPrev:r>0,hasNext:r<s.length-1}}graph(){return{...this.nodes}}};var F=class{constructor(e){this.manager=e}get workspace(){return this.manager.workspace()}roles(){return this.manager.ctx().roles.list()}role(e){return this.manager.ctx().roles.get(e)}async createRole(e,t,s,r,n=[]){if(this.workspace.index.roles[e])return y({code:"DUPLICATE_KEY",resource:"role",key:e});const i={name:e,label:t,description:r,persona:s,preferences:n,topics:[]};return this.manager.dispatch({type:"role:add",payload:i,timestamp:(new Date).toISOString()})}async updateRole(e,t){return this.workspace.index.roles[e]?this.manager.dispatch({type:"role:update",payload:{name:e,...t},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"role",id:e})}async deleteRole(e){const t=this.workspace;if(!t.index.roles[e])return y({code:"NOT_FOUND",resource:"role",id:e});return Object.values(t.index.sessions).some((t=>t.role===e))?y({code:"INVALID_COMMAND",reason:`Cannot delete role "${e}" — it is still referenced by one or more sessions`}):this.manager.dispatch({type:"role:delete",payload:{name:e},timestamp:(new Date).toISOString()})}async addPreference(e,t){const s={id:crypto.randomUUID(),content:e,topics:t,timestamp:(new Date).toISOString()};return this.manager.dispatch({type:"preference:add",payload:s,timestamp:s.timestamp})}preferences(){return this.manager.ctx().preferences.list()}async addContext(e,t,s,r){const n={key:e,topics:s,content:t,timestamp:(new Date).toISOString(),metadata:r};return this.manager.dispatch({type:"context:add",payload:n,timestamp:n.timestamp})}context(){return this.manager.ctx().context.list()}async registerBlob(e,t,s){const r=await this.manager.dispatch({type:"blob:register",payload:{data:e,mediaType:t,filename:s},timestamp:(new Date).toISOString()});if(!r.ok)return y(r.error);const n=r.value,i=n?.index?.blobs;if(!i)return y({code:"BACKEND_ERROR",reason:"Blob registration succeeded but no blobs patch returned"});const o=Object.keys(i)[0];if(!o)return y({code:"BACKEND_ERROR",reason:"No SHA256 in patch"});const a=i[o];if(!a)return y({code:"BACKEND_ERROR",reason:"Blob record missing"});return f({sha256:o,ref:{sha256:o,mediaType:a.mediaType,sizeBytes:a.sizeBytes,filename:a.filename,previewUrl:a.previewUrl}})}},P=class e{constructor(e,t,s,r){this._id=e,this.manager=t,this.processor=s,this.turnRepository=r,this.workspace=new F(t),this.unsubscribe=t.subscribe("workspace:synced",(()=>this.sync()))}workspace;_role=void 0;_preferences=[];tree;unsubscribe;static async create(t,s,r){s.ctx().workspace;const n=new M(s.ctx().turns,s.ctx().sessions),i=new e(t,s,r,n);return await i.sync(),i}async sync(){await this._setRole(),await this._setPreference(),await this.refreshTurnTree()}close(){this.unsubscribe?.()}async _setRole(){const e=this.meta(),t=await this.manager.ctx().roles.get(e.role);t&&(this._role=t)}async _setPreference(){const e=this.meta();this._preferences=await this.manager.ctx().preferences.load(e.preferences)}id(){return this._id}ws(){return this.manager.workspace()}meta(){return this.ws().index.sessions[this._id]}label(){return this.meta()?.label}role(){return this._role}topics(){return this.meta()?.topics??[]}preferences(){return this._preferences}head(){return this.meta()?.head??null}turns(){return this.tree.chain()}async siblings(e){return this.tree.getTurnSiblings(e)}async branchInfo(e){return this.tree.branchInfo(e)}async getTurn(e){return this.tree.graph()[e]}async rename(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,label:e},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async setTopics(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async addTopics(e){return this.meta()?this.dispatch({type:"session:topics:add",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async setPreferences(e){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const t=await this.dispatch({type:"session:preferences:override",payload:{sessionId:this._id,preferences:e},timestamp:(new Date).toISOString()});return t.ok&&await this._setPreference(),t}async fork(e,t,s,r){return this.meta()?this.dispatch({type:"session:fork",payload:{sessionId:this._id,newSessionId:e,label:t,role:s,topics:r},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async dispatch(e){return this.manager.dispatch(e)}async switchRole(e){const t=this.ws();if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});if(e!==U&&!t.index.roles[e])return y({code:"NOT_FOUND",resource:"role",id:e});const s=await this.dispatch({type:"session:update",payload:{sessionId:this._id,role:e},timestamp:(new Date).toISOString()});return s.ok&&await this._setRole(),s}async addTurn(e){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const t={...e,session:this._id,version:e.version??0,parent:!e.parent&&this.head()?this.head():void 0},s=await this.dispatch({type:"turn:add",payload:{sessionId:this._id,turn:t},timestamp:t.timestamp});return s.ok&&await this.refreshTurnTree(),s}async recordUserTurn(e){return this.meta()?this.addTurn(e):y({code:"NOT_FOUND",resource:"session",id:this._id})}async recordAssistantTurn(e,t){const s=this.ws();if(!s)return y({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!s.index.sessions[this._id])return y({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.processor.process(e,this._id);let n={};const i=[];for(const e of r){const t=await this.manager.dispatch(e);if(t.ok)n=w(n,t.value);else{if("PERMISSION_DENIED"!==t.error.code||e.synthetic)return t;{const t=this.describeCommand(e);i.push({cmd:e,description:t})}}}const o=await this.addTurn(e);if(!o.ok)return o;if(n=w(n,o.value),t&&i.length>0){const e=this.buildDenialTurn(i),t=await this.addTurn(e);t.ok&&(n=w(n,t.value))}return f(n)}buildDenialTurn(e){const t=new $("user",this._id);for(const s of e)t.addText(`[System] User denied: ${s.description}.`);return t.build()}describeCommand(e){return"tool:call"===e.type?`tool execution: ${e.payload.tool}`:`command: ${e.type}`}async editTurn(e,t,s){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph()[e];if(!r)return y({code:"NOT_FOUND",resource:"turn",id:e});const n=r.versions[r.activeVersion],i=r.activeVersion+1,o=await this.dispatch({type:"turn:edit",payload:{sessionId:this._id,turnId:e,newBlocks:t,roleSnapshot:s??n.role,newVersion:i},timestamp:(new Date).toISOString()});return o.ok&&(await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:{id:e,version:i}},timestamp:(new Date).toISOString()}),await this.refreshTurnTree()),o}async branch(e){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});if(!e.parent)return y({code:"INVALID_COMMAND",reason:"branch requires turn.parent to be set"});const t=await this.dispatch({type:"turn:branch",payload:{sessionId:this._id,turn:{...e,session:this._id}},timestamp:e.timestamp});return t.ok&&await this.refreshTurnTree(),t}async deleteTurn(e,t,s){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph();if(!r[e]?.versions[t])return y({code:"NOT_FOUND",resource:"turn version",id:`${e}:${t}`});const n=await this.dispatch({type:"turn:delete",payload:{sessionId:this._id,turnId:e,version:t,newHead:s},timestamp:(new Date).toISOString()});return n.ok&&await this.refreshTurnTree(),n}async switchVersionLeft(e){return this.switchVersion(e,-1)}async switchVersionRight(e){return this.switchVersion(e,1)}async switchVersion(e,t){if(!this.ws())return y({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const s=this.tree.graph()[e];if(!s)return y({code:"NOT_FOUND",resource:"turn",id:e});const r=Object.keys(s.versions).map(Number).sort(((e,t)=>e-t)),n=r.indexOf(s.activeVersion);if(-1===n)return y({code:"INVALID_COMMAND",reason:"Active version not found"});const i=n+t;if(i<0||i>=r.length)return y({code:"INVALID_COMMAND",reason:`No ${t<0?"previous":"next"} version available for turn ${e}`});const o=r[i],a=this.findSubtreeTip(this.tree.graph(),e,o),c=await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:a},timestamp:(new Date).toISOString()});return c.ok&&await this.refreshTurnTree(),c}async snapshot(){const e=this,t=e.meta();if(!t)return;const s=e.ws(),r=e._role;if(!r)return;const n=await e.manager.ctx().context.getByTopics(s.index,t.topics),i=Array.from(new Set([...r.preferences,...t.preferences])),o=i.length>0?await e.manager.ctx().preferences.load(i):e._preferences,a=e.tree.chain().map((e=>e.versions[e.activeVersion])).filter((e=>!!e)),c=[...a].reverse().find((e=>"user"===e.actor));return{id:e._id,meta:t,role:r,preferences:o,context:n,model:t.metadata?.model,transcript:a,topics:t.topics,instructions:s.settings?.prompt,constraints:{role:r.constraints,session:t.constraints,turn:c?.constraints}}}async refreshTurnTree(){this.tree=await z.build(this._id,this.turnRepository)}findSubtreeTip(e,t,s){let r=t,n=s;for(;;){const t=e[r];if(!t)break;const s=t.children[n];if(!s||0===s.length)break;const i=s[0],o=e[i];if(!o)break;r=i,n=o.activeVersion}return{id:r,version:n}}},K=class{constructor(e,t){this.manager=e,this.processor=t}openOnce=new Map;async open(e){let t=this.openOnce.get(e);t||(t=new d,this.openOnce.set(e,t));const s=await t.do((async()=>{if(!this.manager.workspace().index.sessions[e])throw new Error(`Session ${e} not found in workspace index`);return P.create(e,this.manager,this.processor)}));if(s.error)throw s.error;return s.value}close(e){this.openOnce.delete(e)}async delete(e){const t=await this.manager.dispatch({type:"session:delete",payload:{sessionId:e},timestamp:(new Date).toISOString()});if(t.ok)return this.close(e),t.value=void 0,t;throw t.error}async create(e){const s=t.v7(),r=await this.manager.dispatch({type:"session:create",payload:{...e,id:s},timestamp:(new Date).toISOString()});if(r.ok)return this.open(s);throw r.error}list(){return Object.values(this.manager.workspace().index.sessions)}metadata(e){return this.manager.workspace().index.sessions[e]}},L=class{constructor(e,t,s){this.storage=e,this.cache=t,this.bus=s}async register(e,t,s){const r=await g(e),n=await this.getRecord(r);if(n){const e={...n,refCount:n.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(e),this.cache.set(r,e),f(this.reference(e))}const i={sha256:r,mediaType:t,sizeBytes:e.byteLength,filename:s,refCount:1,remoteIds:{},createdAt:(new Date).toISOString(),lastUsedAt:(new Date).toISOString()};return this.storage.registerBlob?await this.storage.registerBlob(i,e):(await this.storage.storeBytes(r,e),await this.storage.saveRecord(i)),this.cache.set(r,i),this.bus.emit({name:"blobs:changed",payload:{sha256:r,record:i}}),f(this.reference(i))}async retain(e){const t=await this.getRecord(e);if(!t)return y({code:"NOT_FOUND",resource:"blob",id:e});const s={...t,refCount:t.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(s),this.cache.set(e,s),f(void 0)}async release(e){const t=await this.getRecord(e);if(!t)return y({code:"NOT_FOUND",resource:"blob",id:e});const s=Math.max(0,t.refCount-1),r={...t,refCount:s,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(r),this.cache.set(e,r),f(void 0)}async purge(e){return await this.storage.deleteBytes(e),await this.storage.deleteRecord(e),this.cache.delete(e),this.bus.emit({name:"blobs:changed",payload:{sha256:e}}),f(void 0)}async recordRemoteId(e,t,s,r){const n=await this.getRecord(e);if(!n)return y({code:"NOT_FOUND",resource:"blob",id:e});const i={...n,remoteIds:{...n.remoteIds,[t]:{id:s,timestamp:r||(new Date).toISOString()}},lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(i),this.cache.set(e,i),this.bus.emit({name:"blobs:changed",payload:{sha256:e,record:i}}),f(void 0)}async getRecord(e){const t=this.cache.get(e);if(t)return t;const s=await this.storage.loadRecord(e);return s&&this.cache.set(e,s),s}async getAllRecords(){const e=await this.storage.listRecords(),t={};for(const s of e)t[s.sha256]=s,this.cache.set(s.sha256,s);return t}reference(e){return{sha256:e.sha256,mediaType:e.mediaType,sizeBytes:e.sizeBytes,filename:e.filename}}async resolve(e,t){const s=await this.getRecord(e.sha256);if(!s)return f(null);if(t){const e=s.remoteIds[t];if(e)return f({kind:"remote",sha256:s.sha256,mediaType:s.mediaType,fileId:e.id,providerId:t,timestamp:e.timestamp});const r=await this.storage.loadBytes(s.sha256);return f(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}const r=await this.storage.loadBytes(s.sha256);return f(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}async resolveMany(e,t){const s=new Map;for(const r of e){const e=await this.resolve(r,t);if(!e.ok)return y(e.error);if(null===e.value)return y({code:"BLOB_ERROR",reason:`Unable to resolve blob ${r.sha256}${t?` with adapter ${t}`:""}`});s.set(r.sha256,e.value)}return f(s)}},V=class{constructor(e,t,s){this.collection=e,this.cache=t,this.registry=s}delegates=new Map;registerDelegate(e,t){this.delegates.set(e,t)}async get(e,t){const s=this.cache.get(e);if(s)return s;if(t){const s=this.delegates.get(t);if(s){const t=await s.get(e);return t&&this.cache.set(e,t),t}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;const n=r.state();return this.cache.set(e,n),n}async add(e){const t=this.delegates.get(e.content.kind);t?await t.add(e):await this.collection.create(e),this.cache.set(e.key,e)}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"}),t=((await e.next()).value||[]).map((e=>e.state()));for(const e of this.delegates.values()){const s=await e.list();t.push(...s)}return t}async update(e,t,s){if(s){const r=this.delegates.get(s);if(r){const s=await r.update(e,t);return s&&this.cache.set(e,s),s}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;await r.update(t);const n=r.state();return this.cache.set(e,n),n}async delete(e,t){if(t){const s=this.delegates.get(t);if(s){const t=await s.delete(e);return t&&this.cache.delete(e),t}}const s=await this.collection.find({field:"key",operator:"eq",value:e});if(!s)return!1;const r=await s.delete();return r&&this.cache.delete(e),r}async getByTopics(e,t){const s=new Map;for(const r of t){const t=e.topics[r];t&&t.contextKeys.forEach((t=>{const r=e.context[t],n=r?.kind||"unknown";s.has(n)||s.set(n,new Set),s.get(n).add(t)}))}if(0===s.size)return[];const r=[],n=[];for(const[e,t]of s.entries()){if(this.delegates.get(e))for(const s of t){const t=await this.get(s,e);t&&r.push(t)}else t.forEach((e=>n.push(e)))}if(n.length>0){const e=[];for(const t of n){const s=this.cache.get(t);s?r.push(s):e.push(t)}if(e.length>0){const t=await this.collection.filter({operator:"or",conditions:e.map((e=>({field:"key",operator:"eq",value:e})))});for(const e of t){const t=e.state();this.cache.set(t.key,t),r.push(t)}}}return r.sort(((e,t)=>new Date(t.timestamp).getTime()-new Date(e.timestamp).getTime()))}summarize(e){const t=this.registry?.get(e.content.kind);return t?t.summarize(e):{kind:e.content.kind,key:e.key,topics:e.topics,timestamp:e.timestamp,metadata:e.metadata}}},W=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async load(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?s.push(e):t.push(r)}if(t.length>0){const e=await this.collection.filter({operator:"or",conditions:t.map((e=>({field:"id",operator:"eq",value:e})))});for(const t of e){const e=t.state();this.cache.set(e.id,e),s.push(e)}}return s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{id:e.id,topics:e.topics,timestamp:e.timestamp,snippet:e.content.slice(0,100)}}},J=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{name:e.name,label:e.label,description:e.description,preferences:e.preferences?.length??0,topics:e.topics??[],constraints:e.constraints}}},G=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},Y=class{constructor(e,t){this.collection=e,this.cache=t}key({id:e,session:t,version:s}){return`${t}:${s}:${e}`}async find({id:e,session:t,version:s},r=!0){const n=this.key({id:e,session:t,version:s});this.cache.get(n)&&this.cache.get(n);const i=await this.collection.find({operator:"and",conditions:[{field:"id",operator:"eq",value:e},{field:"session",operator:"eq",value:t},{field:"version",operator:"eq",value:s}]});return i?(r&&this.cache.set(n,i),i):null}async get(e){return this.find(e)}async add(e){const t=await this.collection.create(e);this.cache.set(this.key(e),t)}async update(e,t){const s=await this.find(e);return s?(await s.update(t),s.state()):null}async listBySession(e){return(await this.collection.filter({field:"session",operator:"eq",value:e})).map((e=>e.state()))}async delete(e){const t=await this.find(e,!1);if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(this.key(e)),s}},H=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:100,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},X=class{cache;maxSize;constructor(e){this.cache=new Map,this.maxSize=e}get(e){const t=this.cache.get(e);return void 0!==t&&(this.cache.delete(e),this.cache.set(e,t)),t}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxSize){const e=this.cache.keys().next().value;void 0!==e&&this.cache.delete(e)}this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}},Q="blob_bytes",Z="blob_records";function ee(e){return new Promise(((t,s)=>{e.onsuccess=()=>t(e.result),e.onerror=()=>s(e.error)}))}function te(e){return new Promise(((t,s)=>{e.oncomplete=()=>t(),e.onerror=()=>s(e.error),e.onabort=()=>s(new Error("Transaction aborted"))}))}var se={};((e,t)=>{for(var r in t)s(e,r,{get:t[r],enumerable:!0})})(se,{GOOGLE_MODELS:()=>ne,GoogleGenAIAdapter:()=>ue,mapBlockToPart:()=>ae,mapTurnToContent:()=>oe,mappings:()=>re,parseModelResponse:()=>ie});var re={text:{to:e=>({text:e.text}),from:e=>({id:t.v4(),type:"text",text:e.text})},summary:{to:e=>({text:`[Summary]: ${e.text}`}),from:e=>({id:t.v4(),type:"summary",text:e.text})},thinking:{to:e=>({text:e.thinking}),from:e=>({id:t.v4(),type:"thinking",thinking:e.thinking})},"tool:use":{to:e=>({functionCall:{name:e.name,args:e.input}}),from:e=>({id:t.v4(),type:"tool:use",name:e.functionCall?.name??e.name,input:e.functionCall?.args??e.input??{}})},"tool:result":{to:e=>({functionResponse:{name:e.useId,response:{result:e.content}}}),from:e=>({id:t.v4(),type:"tool:result",useId:e.useId,content:e.content,isError:e.isError})},"role:transition":{to:e=>({text:`[Role transition: ${e.previousRole??"none"} → ${e.newRole}]`}),from:e=>({id:t.v4(),type:"role:transition",previousRole:e.previousRole,newRole:e.newRole})}},ne=[{provider:"google",name:"gemini-3.1-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3-flash-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3,cache:{read:.05,write:1}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3.1-flash-lite-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.25,output:1.5,cache:{read:.025,write:1}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-pro",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10,cache:{read:.125,write:4.5}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.3,output:2.5,cache:{read:.03,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-lite",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4,cache:{read:.01,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-3-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-image",window:{size:4096,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"image",scale:0,cost:{input:.02,output:0}}],capacity:[{unit:"call",max:100,period:60}]},{provider:"google",name:"gemini-2.5-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-3.1-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-2.5-computer-use-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10}}],capacity:[{unit:"call",max:200,period:60},{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-001",window:{size:2048,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.5,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-2",window:{size:8192,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.3,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]}];function ie(e,s){const r=e.candidates?.[0];if(!r?.content?.parts)return y({code:"BACKEND_ERROR",reason:"No valid content parts in response"});const n=[];let i;for(const e of r.content.parts)if(e.thought)n.push((o=e.text??"",{id:t.v7(),type:"thinking",thinking:o}));else if(e.text){i=e;break}var o;if(!i)return y({code:"BACKEND_ERROR",reason:"Response missing JSON part"});const a=function(e){try{const t=JSON.parse(e);return t.blocks&&Array.isArray(t.blocks)?f(t.blocks):y({code:"BACKEND_ERROR",reason:'Invalid response: missing or malformed "blocks" array'})}catch(e){return y({code:"BACKEND_ERROR",reason:`Failed to parse response JSON: ${e instanceof Error?e.message:String(e)}`})}}(i.text);if(!a.ok)return a;for(const e of a.value){const t=s.parse(e,ue.provider());t&&n.push(t)}return f(n)}async function oe(e,t,s){const r="assistant"===e.actor?"model":"user",n=[];for(const r of e.blocks){if("image"===r.type||"document"===r.type){const e=r.ref;if(!e)continue;const s=await ce(e,t);s&&n.push(s);continue}const e=ae(r,s);e&&n.push(e)}return{role:r,parts:n}}function ae(e,t){const s=t.get(e.type);if(!s)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: block type "${e.type}" is not registered. Register it via registry.register() before use.`);const r=s.mappings?.[ue.provider()];if(!r)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: no "${ue.provider()}" mapping for block type "${e.type}". Add a mapping via registry.update("${e.type}", { mappings: { ${ue.provider()}: { to, from } } }).`);return r.to(e)}async function ce(e,t){const s=await t(e,ue.provider());if(!s.ok||!s.value)return null;const{value:r}=s;return"remote"===r.kind?{fileData:{fileUri:r.fileId,mimeType:r.mediaType}}:{inlineData:{data:b(r.data),mimeType:r.mediaType}}}function de(e){return Math.ceil(e.length/4)}var ue=class e{constructor(e,t,s={model:"gemini-2.5-flash"}){this.client=e,this.services=t,this._models=t.models;const r=this._models.get(s.model);if(!r)throw new Error(`Could not get model: ${s.model} profile in registry.`);this.model=r,this.registerGeminiMappings()}model;_models;registerGeminiMappings(){const t=this.services.blockRegistry;for(const[s,r]of Object.entries(re))t.update(s,{mappings:{[e.provider()]:r}})}async status(t){return{provider:e.provider(),model:this.model.name,ready:!0,window:this.model.window,feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4}}],rate:{load:0,capacity:[{unit:"call",max:1e3,period:60},{unit:"token",max:4e6,period:60}]}}}async resolve(e){const{prompt:s}=e,{assembler:r,blockRegistry:n,blobResolver:i}=this.services,o=s.model&&this._models.get(s.model)||this.model,a=r.build(s,[{label:"block-architecture",content:n.description(),position:"after:instructions"}]),c=r.join(a),d={role:"system",parts:[{text:c}]},u=s.constraints[o.name]??{tokens:{}},l=await Promise.all(s.transcript.map((e=>oe(e,i,n)))),p=n.schema(),h={model:o.name,contents:l,config:{systemInstruction:d,thinkingConfig:{includeThoughts:!0},responseMimeType:"application/json",responseSchema:p,...void 0!==u.temperature&&{temperature:u.temperature},...void 0!==u.tokens.max&&{maxOutputTokens:u.tokens.max},...u.tokens.stops?.length&&{stopSequences:u.tokens.stops},...void 0!==u.tokens.thought&&{thinkingConfig:{includeThoughts:!0,thinkingBudget:u.tokens.thought}}}},m=l.flatMap((e=>e.parts??[])).map((e=>e.text??"")).join(""),w=de(c),g=de(m),b=w+g,k=this;return{model:o.name,instructions:a,transcript:s.transcript,context:s.system.context,preferences:s.system.preferences,constraints:u,tokens:{breakdown:{system:w,transcript:g},total:b,source:"estimated",output:u.tokens.max??o.window.out,remaining:o.window.size-b},async execute(){let e;try{e=await k.client.models.generateContent(h)}catch(e){return y({code:"BACKEND_ERROR",reason:e instanceof Error?e.message:String(e)})}const t=ie(e,k.services.blockRegistry);if(!t.ok)return t;const r=t.value,n=new $("assistant",s.session);for(const e of r)n.addBlock(e);return f({turn:n.build(),effects:[]})},async*executeStream(){let e;try{e=await k.client.models.generateContentStream(h)}catch(e){const r=e instanceof Error?e.message:String(e),n=new $("assistant",s.session);return n.addBlock({id:t.v7(),type:"text",text:`[Stream error: ${r}]`}),void(yield{turn:n.build(),effects:[]})}const r=[];for await(const s of e){const e=s.candidates?.[0];if(e?.content?.parts)for(const s of e.content.parts)s.thought||s.text&&(r.push(s.text),yield{blocks:[{id:t.v7(),type:"text",text:s.text}]})}const n=r.join(""),i=new $("assistant",s.session);i.addBlock({id:t.v7(),type:"text",text:n}),yield{turn:i.build(),effects:[]}}}}static provider(){return"google"}static models(){return ne}};exports.COLLECTIONS=p,exports.EMPTY_SYSTEM_ROLE=U,exports.GoogleAdapter=se,exports.IndexedDBBlobStorage=class{dbName;db=null;constructor(e={}){this.dbName=e.dbName??"aiworkspace-blobs"}async open(){this.db||(this.db=await new Promise(((e,t)=>{const s=indexedDB.open(this.dbName,1);s.onupgradeneeded=e=>{const t=e.target.result;this.createSchema(t)},s.onsuccess=()=>e(s.result),s.onerror=()=>t(s.error),s.onblocked=()=>t(new Error(`[IndexedDBBlobStorage] Database "${this.dbName}" blocked.`))})),this.db.onversionchange=()=>{this.db?.close(),this.db=null})}createSchema(e){e.objectStoreNames.contains(Q)||e.createObjectStore(Q,{keyPath:"sha256"}),e.objectStoreNames.contains(Z)||e.createObjectStore(Z,{keyPath:"sha256"})}close(){this.db?.close(),this.db=null}async deleteDatabase(){this.close(),await new Promise(((e,t)=>{const s=indexedDB.deleteDatabase(this.dbName);s.onsuccess=()=>e(),s.onerror=()=>t(s.error)}))}getDB(){if(!this.db)throw new Error("[IndexedDBBlobStorage] Database not open. Call open() first.");return this.db}readTx(...e){return this.getDB().transaction(e,"readonly")}writeTx(...e){return this.getDB().transaction(e,"readwrite")}async storeBytes(e,t){const s=this.readTx(Q);if(await ee(s.objectStore(Q).get(e)))return;const r=this.writeTx(Q);r.objectStore(Q).put({sha256:e,data:t}),await te(r)}async loadBytes(e){const t=this.readTx(Q),s=await ee(t.objectStore(Q).get(e));return s?.data??null}async hasBytes(e){const t=this.readTx(Q);return await ee(t.objectStore(Q).count(e))>0}async deleteBytes(e){const t=this.writeTx(Q);t.objectStore(Q).delete(e),await te(t)}async saveRecord(e){const t=this.writeTx(Z);t.objectStore(Z).put(e),await te(t)}async loadRecord(e){const t=this.readTx(Z);return await ee(t.objectStore(Z).get(e))??null}async deleteRecord(e){const t=this.writeTx(Z);t.objectStore(Z).delete(e),await te(t)}async listRecords(){return ee(this.readTx(Z).objectStore(Z).getAll())}async exportAllBytes(){const e=this.readTx(Q);return(await ee(e.objectStore(Q).getAll())).map((({sha256:e,data:t})=>[e,t]))}async registerBlob(e,t){await new Promise(((s,r)=>{const n=this.getDB().transaction([Q,Z],"readwrite"),i=n.objectStore(Q),o=n.objectStore(Z),a=i.count(e.sha256);a.onsuccess=()=>{0===a.result&&i.put({sha256:e.sha256,data:t}),o.put(e)},a.onerror=()=>r(a.error),n.oncomplete=()=>s(),n.onerror=()=>r(n.error),n.onabort=()=>r(new Error("registerBlob transaction aborted"))}))}},exports.LRUCache=X,exports.MemoryBlobStorage=class{bytes=new Map;records=new Map;async storeBytes(e,t){this.bytes.has(e)||this.bytes.set(e,t)}async loadBytes(e){return this.bytes.get(e)??null}async hasBytes(e){return this.bytes.has(e)}async deleteBytes(e){this.bytes.delete(e)}async saveRecord(e){this.records.set(e.sha256,{...e})}async loadRecord(e){return this.records.get(e)??null}async deleteRecord(e){this.records.delete(e)}async listRecords(){return Array.from(this.records.values()).map((e=>({...e})))}async exportAllBytes(){return Array.from(this.bytes.entries()).map((([e,t])=>[e,new Uint8Array(t)]))}async registerBlob(e,t){this.bytes.has(e.sha256)||this.bytes.set(e.sha256,t),this.records.set(e.sha256,{...e})}},exports.Session=P,exports.SessionManager=K,exports.TurnBuilder=$,exports.TurnTree=z,exports.WorkspaceManager=k,exports.bufferToBase64=b,exports.computeSHA256=g,exports.createEmptySession=(e={})=>({id:t.v7(),label:"New Conversation",role:"assistant-default",topics:[],preferences:[],metadata:{created:(new Date).toString(),updated:(new Date).toString()},...e,head:e.head?e.head:void 0}),exports.createEmptyTurn=(e={},s)=>({id:e.id??t.v7(),version:e.version??0,session:e.session??s?.id??t.v7(),actor:"user",blocks:[{id:t.v7(),type:"text",content:"Hello, world!"}],timestamp:(new Date).toString(),role:s?.role??"default",...e,parent:e.parent?e.parent:void 0}),exports.createEmptyWorkspace=(e={})=>({id:e.id??t.v7(),settings:w({language:"en-US",defaultRole:void 0,prompt:void 0},e.settings),project:w({id:t.v7(),name:"Unnamed Project"},e.project),index:w({roles:{},preferences:{},context:{},sessions:{},topics:{},blobs:{},tools:{},extensions:{}},e.index)}),exports.createWorkspace=async function(t){const{blobStorage:s,eventBus:n=e.createEventBus(),getWorkspace:i,setWorkspace:o,processor:a,guard:c,toolRegistry:d,models:u=[],extensions:h=[],extensionReducers:m={},extensionMiddleware:f=[],extensionIndexers:y=[],extensionStores:w,extensionSchemas:g=[]}=t,b=l(t.db),v=[...g,...h.flatMap((e=>e.schemas??[]))],_={...m,...h.reduce(((e,t)=>({...e,...t.reducers})),{})},O=[...f,...h.flatMap((e=>e.middleware??[]))],S=[...y,...h.flatMap((e=>e.indexers??[]))],T=h.flatMap((e=>e.blocks??[]));await b.open(v);const R={workspace:await b.collection(p.WORKSPACE),role:await b.collection(p.ROLE),preference:await b.collection(p.PREFERENCE),context:await b.collection(p.CONTEXT),session:await b.collection(p.SESSION),topic:await b.collection(p.TOPIC),turn:await b.collection(p.TURN)},D=new L(s,new X(200),n),B=new q,A={workspaceStore:new H(R.workspace,new X(1)),roles:new J(R.role,new X(100)),preferences:new W(R.preference,new X(500)),context:new V(R.context,new X(500),B),topics:new x(R.topic,new X(100)),sessions:new G(R.session,new X(50)),turns:new Y(R.turn,new X(50)),blobs:D,tools:d,db:b,indexers:[...r,...S]};if(w){const e=w(A);Object.assign(A,e)}await Promise.all(h.map((async e=>{if(e.stores){const t=await e.stores(A);Object.assign(A,t)}})));const U=new k({ctx:A,getWorkspace:i,updateWorkspace:async e=>{if(await o(e),e?.id||e?.settings||e?.project){const e=i();await A.workspaceStore.update(e.id,{id:e.id,settings:e.settings,project:e.project})}},guard:c,bus:n});Object.entries(E).forEach((([e,t])=>{U.register(e,t)})),Object.entries(_).forEach((([e,t])=>{U.register(e,t)})),U.use(N),O.forEach((e=>{U.use(e)}));const $=new K(U,a),M=new C;T.forEach((e=>M.register(e))),h.forEach((e=>{e.contexts&&e.contexts.forEach((e=>{if(B.register(e),e.store){const t=e.store(A);A.context.registerDelegate(e.kind,t)}}))}));const z=new j(u),F={assembler:new I,processor:a,blobResolver:A.blobs.resolve.bind(A.blobs),blockRegistry:M,contextRegistry:B,models:z};return{manager:U,sessions:$,ctx:A,services:F}},exports.createWorkspaceDatabase=l,exports.del=function(){return h},exports.error=y,exports.getExtension=function(e,t){return e.extensions[t]},exports.merge=w,exports.omitNullUndefined=function(e){return Object.fromEntries(Object.entries(e).filter((([e,t])=>null!=t)))},exports.shortHash=function(e,t=4){let s=0;for(let t=0;t<e.length;t++)s=(s<<5)-s+e.charCodeAt(t),s|=0;return Math.abs(s).toString(36).padEnd(t,"0").slice(0,t)},exports.success=f;
1
+ "use strict";var e=require("@asaidimu/utils-events"),t=require("uuid"),s=Object.defineProperty,r=[async e=>{const t=await e.workspaceStore.list();if(0===t.length)return{};const s=t[0];return{id:s.id,settings:s.settings,project:s.project}},async e=>{const t=await e.roles.list(),s={};for(const r of t)s[r.name]=e.roles.summarize(r);return{index:{roles:s}}},async e=>{const t=await e.preferences.list(),s={};for(const r of t)s[r.id]=e.preferences.summarize(r);return{index:{preferences:s}}},async e=>{const t=await e.context.list(),s={};for(const r of t)s[r.key]=e.context.summarize(r);return{index:{context:s}}},async e=>{const t=await e.sessions.list(),s={};for(const r of t)s[r.id]=e.sessions.summarize(r);return{index:{sessions:s}}},async e=>{const t=await e.topics.list(),s={};for(const r of t)s[r.name]=e.topics.summarize(r);return{index:{topics:s}}},async e=>({index:{blobs:await e.blobs.getAllRecords()}})],n=[{name:"workspace",version:"1.0.0",description:"Workspace root containing global settings and project metadata.",fields:{id:{name:"id",type:"string",required:!0},settings:{name:"settings",type:"record",required:!0},project:{name:"project",type:"record",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"}],constraints:[],migrations:[]},{name:"role",version:"1.0.0",description:"AI persona with a system prompt and associated preference defaults.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!0},description:{name:"description",type:"string",required:!1},persona:{name:"persona",type:"string",required:!0},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},topics:{name:"topics",type:"array",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_name",fields:["name"],type:"unique"},{name:"by_label",fields:["label"],type:"normal"}],constraints:[],migrations:[]},{name:"preference",version:"1.0.0",description:"A user behavioural instruction, scoped to zero or more topics.",fields:{id:{name:"id",type:"string",required:!0},content:{name:"content",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},timestamp:{name:"timestamp",type:"string",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"context",version:"1.0.0",description:"Injected background knowledge, scoped to topics. Content is a discriminated union.",fields:{key:{name:"key",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},content:{name:"content",type:"record",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},metadata:{name:"metadata",type:"record",required:!1}},indexes:[{name:"by_key",fields:["key"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"session",version:"1.0.0",description:"Session metadata. The head field tracks the current tip of the turn DAG.",fields:{id:{name:"id",type:"string",required:!0},label:{name:"label",type:"string",required:!0},role:{name:"role",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},metadata:{name:"metadata",type:"record",required:!1},head:{name:"head",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_role",fields:["role"],type:"normal"}],constraints:[],migrations:[]},{name:"turn",version:"1.0.0",description:["A single message in a session transcript, stored as a flat document.","The DAG is reconstructed in memory by TurnTree.buildNodeGraph() at session open time."].join(" "),fields:{id:{name:"id",type:"string",required:!0},session:{name:"session",type:"string",required:!0},version:{name:"version",type:"number",required:!0},actor:{name:"actor",type:"enum",required:!0,values:["user","assistant","tool"]},blocks:{name:"blocks",type:"array",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},role:{name:"role",type:"string",required:!1},parent:{name:"parent",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_session",fields:["session"],type:"normal"},{name:"by_session_parent",fields:["session","parent"],type:"composite"},{name:"by_session_id_ver",fields:["session","id","version"],type:"composite",unique:!0}],constraints:[],migrations:[]},{name:"topic",version:"1.0.0",description:"A categorization for context and preferences, used for retrieval.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!1},description:{name:"description",type:"string",required:!1},metadata:{name:"metadata",type:"record",required:!1},created:{name:"created",type:"string",required:!0},updated:{name:"updated",type:"string",required:!0}},indexes:[{name:"by_name",fields:["name"],type:"unique"}],constraints:[],migrations:[]}],i=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},o=class extends i{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},a=class extends i{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},c=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let r;await Promise.race([s.then((()=>clearTimeout(r))),new Promise(((s,n)=>{r=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new o("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},d=class{mutex=new c({yieldMode:"microtask"});promise=null;_value=null;_error;_done=!1;retry;throws;constructor({retry:e,throws:t}={}){this.retry=Boolean(e),this.throws=Boolean(t)}async do(e,t){return this._done?this.peek():this.promise?this._awaitWithTimeout(this.promise,t,"Once do() timed out"):(await this.mutex.lock(),this.promise?(this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")):(this.promise=(async()=>{try{this._value=await e(),this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.retry&&!this._done&&(this.promise=null)}return this.peek()})(),this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")))}doSync(e){if(this._done){if(this.throws&&this._error)throw this._error;return this.peek()}if(this.promise){const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}if(!this.mutex.tryLock()){const e=new Error("Cannot execute doSync: lock is currently held.");if(this.throws)throw e;return{value:null,error:e}}if(this.promise||this._done){if(this.mutex.unlock(),this._done){if(this.throws&&this._error)throw this._error;return this.peek()}const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}try{const t=e();this._value=t,this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.mutex.unlock()}return this.peek()}running(){return null!==this.promise&&!this.done()}peek(){return{value:this._value,error:this._error}}get(){if(!this._done)throw new Error("Once operation is not yet complete");if(this._error)throw this._error;return this._value}reset(){if(this.running())throw new Error("Cannot reset Once while an operation is in progress.");this._done=!1,this.promise=null,this._value=null,this._error=void 0}done(){return this._done}current(){return this.promise}_awaitWithTimeout(e,t,s="Operation timed out"){if(null==t)return e;let r;return Promise.race([e.then((e=>(clearTimeout(r),e))),new Promise(((e,n)=>{r=setTimeout((()=>n(new o(s))),t)}))])}},l=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new c({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new a};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,r=null;try{if(this._done)throw new a;r=await e(),this._lastValue=r,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:r,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}};function u(e){const t=new d({retry:!0,throws:!0}),s=new d({retry:!0,throws:!0});return{open:async(s=[])=>(await t.do((async()=>{const t=[...n,...s];await e.setupCollections(t)}))).value,collection:async t=>e.collection(t),close:async()=>(await s.do((()=>{e.close()}))).value,database:()=>e}}var p={WORKSPACE:"workspace",ROLE:"role",PREFERENCE:"preference",CONTEXT:"context",SESSION:"session",TURN:"turn",BLOB:"blob",TOPIC:"topic"},h=Symbol.for("delete"),m=e=>Array.isArray(e)?[...e]:{...e};function f(e){return{ok:!0,value:e}}function y(e){return{ok:!1,error:e}}var w=function(e){const t=e?.deleteMarker||h;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const r={};for(const[n,i]of Object.entries(e))if(i!==t)if("object"==typeof i&&null!==i){const e=s(i);void 0!==e&&(r[n]=e)}else r[n]=i;return r}return e===t?void 0:e}return function(e,r){if("object"!=typeof e||null===e)return"object"==typeof r&&null!==r?s(r):r===t?{}:r;if("object"!=typeof r||null===r)return e;const n=m(e),i=[{target:n,source:r}];for(;i.length>0;){const{target:e,source:s}=i.pop();for(const r of Object.keys(s)){const n=s[r];if(n!==t)if(Array.isArray(n))e[r]=n;else if("object"==typeof n&&null!==n){const t=r in e&&"object"==typeof e[r]&&null!==e[r]?e[r]:{};e[r]=m(t),i.push({target:e[r],source:n})}else e[r]=n;else delete e[r]}}return n}}({deleteMarker:h});async function g(e){const t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t)).map((e=>e.toString(16).padStart(2,"0"))).join("")}function b(e){if("undefined"!=typeof Buffer)return Buffer.from(e).toString("base64");const t=[];for(let s=0;s<e.length;s+=32768){const r=e.subarray(s,s+32768);t.push(String.fromCharCode.apply(null,Array.from(r)))}return btoa(t.join(""))}var k=class{registry=new Map;middlewares=[];serializer=new l;bus;_getWorkspace;updateWorkspace;guard;_ctx;constructor(e){this._ctx=e.ctx,this._getWorkspace=e.getWorkspace,this.updateWorkspace=e.updateWorkspace,this.guard=e.guard,this.bus=e.bus}register(e,t){return this.registry.set(e,t),this}use(e){return this.middlewares.push(e),this}workspace(){return this._getWorkspace()}async dispatch(e){const t=await this.serializer.do((async()=>{if(this.guard){const t=await this.guard.authenticate({type:"command",payload:e});if(!t.ok)return y(t.error)}const t=this.registry.get(e.type);if(!t)return y({code:"INVALID_COMMAND",reason:`No reducer registered for command type: ${e.type}`});const s=this._getWorkspace(),r=await t({workspace:s,...this._ctx},e.payload);if(!r.ok)return r;let n=r.value;for(const t of this.middlewares){const r=await t({workspace:s,command:e,patch:n,...this._ctx});n=w(n,r)}return await this.updateWorkspace(n),this.bus.emit({name:"workspace:changed",payload:n}),"workspace:sync"===e.type&&this.bus.emit({name:"workspace:synced",payload:void 0}),f(n)}));if(t.error)throw t.error;return t.value}subscribe(e,t){return this.bus.subscribe(e,t)}ctx(){return{workspace:this._getWorkspace(),...this._ctx}}},x=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async getMany(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?t.push(e):s.push(r)}if(s.length>0){const e=await this.collection.filter({operator:"or",conditions:s.map((e=>({field:"name",operator:"eq",value:e})))});for(const s of e){const e=s.state();this.cache.set(e.name,e),t.push(e)}}return t}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}referencedBy(e,t){for(const s in t.roles)if(t.roles[s].topics?.includes(e))return!0;for(const s in t.sessions)if(t.sessions[s].topics?.includes(e))return!0;for(const s in t.context)if(t.context[s].topics?.includes(e))return!0;for(const s in t.preferences)if(t.preferences[s].topics?.includes(e))return!0;return!1}summarize(e){return{topic:e.name,contextKeys:[],preferences:[],metadata:{created:e.created,updated:e.updated,entries:0}}}};function v(e,t,s,r,n,i){const o={};for(const a of t){let t=e[a];if(!t){if("add"!==n||!i)continue;t={topic:a,contextKeys:[],preferences:[],metadata:{created:(new Date).toISOString(),updated:(new Date).toISOString(),entries:0}},i.add({name:a,created:t.metadata.created,updated:t.metadata.updated})}const c=t[r],d=c.includes(s);if("add"!==n||d){if("remove"===n&&d){const e=Math.max(0,(t.metadata.entries||0)-1);o[a]={...t,[r]:c.filter((e=>e!==s)),metadata:{...t.metadata,updated:(new Date).toISOString(),entries:e}}}}else o[a]={...t,[r]:[...c,s],metadata:{...t.metadata,updated:(new Date).toISOString(),entries:(t.metadata.entries||0)+1}}}return{index:{topics:o}}}function _(e,t,s,r){return v(e,t,s,"contextKeys","add",r)}function O(e,t,s){return v(e,t,s,"contextKeys","remove")}function S(e,t,s,r){return v(e,t,s,"preferences","add",r)}function T(e,t,s){return v(e,t,s,"preferences","remove")}var N=async e=>{if(!e.patch)return;const{workspace:t,patch:s}=e,r=t.index.topics;let n={};const i=e=>{const t=e?.index?.topics;t&&(n=w(n,t))};if(s.index?.context)for(const[n,o]of Object.entries(s.index.context)){const s=t.index.context[n];if(void 0===o&&s)i(O(r,s.topics,s.key));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(O(r,c,s.key)),a.length&&i(_(r,a,s.key,e.topics))}}else i(_(r,o.topics??[],o.key,e.topics))}if(s.index?.preferences)for(const[n,o]of Object.entries(s.index.preferences)){const s=t.index.preferences[n];if(void 0===o&&s)i(T(r,s.topics,s.id));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(T(r,c,s.id)),a.length&&i(S(r,a,s.id,e.topics))}}else i(S(r,o.topics??[],o.id,e.topics))}return Object.keys(n).length?{index:{topics:n}}:{}},R="\n# WORKSPACE OPERATING SYSTEM\nYou are operating within a structured workspace. Your output MUST be a JSON object matching the provided schema.\nYour entire response MUST be valid JSON. Nothing else.\n".trim();function D(e){if(!e||"append"===e)return{placement:"append",label:null};if("prepend"===e)return{placement:"prepend",label:null};const[t,...s]=e.split(":");return("before"===t||"after"===t)&&s.length>0?{placement:t,label:s.join(":")}:(console.warn(`[SystemPromptAssembler] Unrecognised position "${e}", defaulting to "append".`),{placement:"append",label:null})}var I=class{build(e,t=[]){const s=[...e.system.extensions||[],...t],r=[];var n,i,o;r.push({label:"operating-system",content:R}),e.role.persona&&r.push({label:"persona",content:(n=e.role.persona,`# Persona\n${n}`)}),e.system.preferences.length>0&&r.push({label:"preferences",content:(i=e.system.preferences,`# User Preferences\n${i.map((e=>`- ${e.content}`)).join("\n")}`),metadata:{count:e.system.preferences.length}}),e.system.context.length>0&&r.push({label:"context",content:(o=e.system.context,`# Context\n${o.map((e=>{switch(e.content.kind){case"text":return`[${e.key}] ${e.content.value}`;case"json":return`[${e.key}] JSON: ${JSON.stringify(e.content.value)}`;case"blob":return`[${e.key}] Blob: ${e.content.filename??"unnamed"} (${e.content.mediaType}, ${e.content.sizeBytes} bytes)`;case"remote":return`[${e.key}] Remote: ${e.content.uri}`;default:return`[${e.key}] Unsupported context type`}})).join("\n")}`),metadata:{count:e.system.context.length}}),e.system.instructions&&r.push({label:"instructions",content:`# Instructions\n${e.system.instructions}`});const a=[...r],c=[];for(const e of s){const{position:t,...s}=e,r=D(t);if("prepend"===r.placement){c.push(s);continue}if("append"===r.placement){a.push(s);continue}const n=a.findIndex((e=>e.label===r.label));if(-1===n){console.warn(`[SystemPromptAssembler] Anchor target "${r.label}" not found in sections. Extension "${e.label}" will be appended.`),a.push(s);continue}const i="before"===r.placement?n:n+1;a.splice(i,0,s)}return[...c,...a]}join(e){return e.map((e=>e.content)).join("\n\n---\n\n")}};var E={"workspace:create":async function(e,t){const{workspaceStore:s}=e,{id:r,settings:n,project:i}=t,o=await s.get(r);return o?f({id:o.id,settings:o.settings,project:o.project}):(await s.add({id:r,settings:n,project:i}),f({id:r,settings:n,project:i}))},"workspace:sync":async function(e,t){let s={};for(const t of e.indexers){const r=await t(e);s=w(s,r)}return t&&(t.id&&(s.id=t.id),t.settings&&(s.settings=w(s.settings||{},t.settings)),t.project&&(s.project=w(s.project||{},t.project))),f(s)},"role:add":async function(e,t){const{workspace:s,roles:r}=e,n=t;return s.index.roles[n.name]?y({code:"DUPLICATE_KEY",resource:"Role",key:n.name}):(await r.add(n),f({index:{roles:{[n.name]:r.summarize(n)}}}))},"role:update":async function(e,t){const{workspace:s,roles:r}=e,{name:n,...i}=t;if(!s.index.roles[n])return y({code:"NOT_FOUND",resource:"Role",id:n});const o=await r.update(n,i);return o?f({index:{roles:{[n]:r.summarize(o)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update role ${n} in store.`})},"role:delete":async function(e,t){const{workspace:s,roles:r}=e,{name:n}=t;return s.index.roles[n]?await r.delete(n)?f({index:{roles:{[n]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete role ${n} from store.`}):y({code:"NOT_FOUND",resource:"Role",id:n})},"preference:add":async function(e,t){const{workspace:s,preferences:r}=e,n=t;return s.index.preferences[n.id]?y({code:"DUPLICATE_KEY",resource:"Preference",key:n.id}):(await r.add(n),f({index:{preferences:{[n.id]:r.summarize(n)}}}))},"preference:update":async function(e,t){const{workspace:s,preferences:r}=e,{id:n,...i}=t;if(!s.index.preferences[n])return y({code:"NOT_FOUND",resource:"Preference",id:n});const o=await r.update(n,i);return o?f({index:{preferences:{[n]:r.summarize(o)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update preference ${n} in store.`})},"preference:delete":async function(e,t){const{workspace:s,preferences:r}=e,{id:n}=t;return s.index.preferences[n]?await r.delete(n)?f({index:{preferences:{[n]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete preference ${n} from store.`}):y({code:"NOT_FOUND",resource:"Preference",id:n})},"context:add":async function(e,t){const{workspace:s,context:r}=e,n=t;return s.index.context[n.key]?y({code:"DUPLICATE_KEY",resource:"Context",key:n.key}):(await r.add(n),f({index:{context:{[n.key]:r.summarize(n)}}}))},"context:update":async function(e,t){const{workspace:s,context:r}=e,{key:n,...i}=t,o=s.index.context[n];if(!o)return y({code:"NOT_FOUND",resource:"Context",id:n});const a=await r.update(n,i,o.kind);return a?f({index:{context:{[n]:r.summarize(a)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update context ${n} in store.`})},"context:delete":async function(e,t){const{workspace:s,context:r}=e,{key:n}=t,i=s.index.context[n];return i?await r.delete(n,i.kind)?f({index:{context:{[n]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete context ${n} from store.`}):y({code:"NOT_FOUND",resource:"Context",id:n})},"topic:add":async function(e,t){const{workspace:s,topics:r}=e,n=t;return s.index.topics[n.name]?y({code:"DUPLICATE_KEY",resource:"Topic",key:n.name}):(await r.add(n),f({index:{topics:{[n.name]:r.summarize(n)}}}))},"topic:update":async function(e,t){const{workspace:s,topics:r}=e,{name:n,...i}=t;if(!s.index.topics[n])return y({code:"NOT_FOUND",resource:"Topic",id:n});const o=await r.update(n,i);return o?f({index:{topics:{[n]:{metadata:{updated:o.updated}}}}}):y({code:"BACKEND_ERROR",reason:`Failed to update topic ${n} in store.`})},"topic:delete":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{name:o,cascade:a}=t;if(!s.index.topics[o])return y({code:"NOT_FOUND",resource:"Topic",id:o});if(r.referencedBy(o,s.index)&&!a)return y({code:"INVALID_COMMAND",reason:`Topic '${o}' is referenced by other entities. Use 'cascade: true' to force deletion.`});if(a){for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);t&&await n.update(e,{topics:t.topics.filter((e=>e!==o))})}}for(const e in s.index.sessions){const t=s.index.sessions[e];t.topics?.includes(o)&&await i.update(e,{topics:t.topics.filter((e=>e!==o))})}}return await r.delete(o)?f({index:{topics:{[o]:h}}}):y({code:"BACKEND_ERROR",reason:`Failed to delete topic ${o} from store.`})},"topic:merge":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{source:o,target:a}=t;if(!s.index.topics[o]||!s.index.topics[a])return y({code:"NOT_FOUND",resource:"Topic",id:s.index.topics[o]?a:o});for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);if(t){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await n.update(e,{topics:s})}}}for(const e in s.index.sessions){const t=s.index.sessions[e];if(t.topics?.includes(o)){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await i.update(e,{topics:s})}}return await r.delete(o),f({index:{topics:{[o]:h}}})},"session:create":async function(e,t){const{workspace:s,sessions:r}=e,n=t;return s.index.sessions[n.id]?y({code:"DUPLICATE_KEY",resource:"Session",key:n.id}):(await r.add(n),f({index:{sessions:{[n.id]:r.summarize(n)}}}))},"session:update":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,...i}=t;if(!s.index.sessions[n])return y({code:"NOT_FOUND",resource:"Session",id:n});const o=await r.update(n,i);return o?f({index:{sessions:{[n]:r.summarize(o)}}}):y({code:"BACKEND_ERROR",reason:`Failed to update session ${n} in store.`})},"session:fork":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,newSessionId:i,label:o}=t,a=s.index.sessions[n];if(!a)return y({code:"NOT_FOUND",resource:"Session",id:n});const c={...a,id:i,label:o||`Fork of ${a.label}`,role:t.role?t.role:a.role,topics:t.topics?t.topics:a.topics};return await r.add(c),f({index:{sessions:{[i]:r.summarize(c)}}})},"session:delete":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n}=t;return s.index.sessions[n]?(await r.delete(n),f({index:{sessions:{[n]:h}}})):y({code:"NOT_FOUND",resource:"Session",id:n})},"session:role:switch":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,roleName:i}=t;return s.index.sessions[n]?(await r.update(n,{role:i}),f({index:{sessions:{[n]:{role:i}}}})):y({code:"NOT_FOUND",resource:"Session",id:n})},"session:topics:add":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,topics:i}=t,o=s.index.sessions[n];if(!o)return y({code:"NOT_FOUND",resource:"Session",id:n});const a=Array.from(new Set([...o.topics,...i]));return await r.update(n,{topics:a}),f({index:{sessions:{[n]:{topics:a}}}})},"session:preferences:override":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,preferences:i}=t;return s.index.sessions[n]?(await r.update(n,{preferences:i}),f({index:{sessions:{[n]:{preferences:i}}}})):y({code:"NOT_FOUND",resource:"Session",id:n})},"turn:add":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return y({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})},"turn:update":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return y({code:"NOT_FOUND",resource:"Session",id:i});await n.update(o,o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})},"turn:edit":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newBlocks:a,newVersion:c,roleSnapshot:d}=t,l=s.index.sessions[i];if(!l)return y({code:"NOT_FOUND",resource:"Session",id:i});const u=l.head;let p={};if(u&&u.id===o){const e=await n.get({id:u.id,version:u.version,session:i});e&&(p={...e})}const h={...p,id:o,version:c,blocks:a,role:d??p.role,timestamp:(new Date).toISOString(),parent:p.parent,actor:p.actor||"assistant",session:i};if(await n.add(h),l.head?.id===o){const e={id:o,version:c};return await r.update(i,{head:e}),f({index:{sessions:{[i]:{head:e}}}})}return f({})},"turn:branch":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return y({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})},"turn:delete":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newHead:a}=t;return s.index.sessions[i]?(await n.delete({session:i,id:o,version:t.version}),a?(await r.update(i,{head:a}),f({index:{sessions:{[i]:{head:a}}}})):f({})):y({code:"NOT_FOUND",resource:"Session",id:i})},"blob:register":async function(e,t){const{blobs:s}=e,r=await s.register(t.data,t.mediaType,t.filename);if(!r.ok)return r;const n=await s.getRecord(r.value.sha256);return n?f({index:{blobs:{[n.sha256]:n}}}):y({code:"BACKEND_ERROR",reason:`Failed to retrieve registered blob record for ${r.value.sha256}`})},"blob:retain":async function(e,t){const{blobs:s}=e,r=await s.retain(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return f({index:{blobs:{[t.sha256]:n}}})},"blob:release":async function(e,t){const{blobs:s}=e,r=await s.release(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return f({index:{blobs:{[t.sha256]:n||h}}})},"blob:purge":async function(e,t){const{blobs:s}=e,r=await s.purge(t.sha256);return r.ok?f({index:{blobs:{[t.sha256]:h}}}):r},"blob:record_remote_id":async function(e,t){const{blobs:s}=e,{sha256:r,providerId:n,fileId:i,timestamp:o}=t,a=await s.recordRemoteId(r,n,i,o);if(!a.ok)return a;const c=await s.getRecord(r);return f({index:{blobs:{[r]:c}}})},"tool:call":async function(e,t){return f({})}},j=class{profiles;constructor(e=[]){this.profiles=new Map;for(const t of e)this.profiles.set(t.name,t)}get(e){return this.profiles.get(e)}list(e){const t=Array.from(this.profiles.values());return e?t.filter((t=>t.provider===e)):t}register(e){this.profiles.set(e.name,e)}},B=[{type:"text",emittable:!0,description:["## Block: `text`","Primary communication block. Use for all conversational responses, explanations, and prose.","A response may contain multiple `text` blocks interleaved with other block types."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["text"]},text:{type:"string",description:"The raw markdown or plain text content of the response."}},required:["type","text"]}},{type:"summary",emittable:!0,description:["## Block: `summary`","Context compression block. Replaces older conversation history with a concise digest."].join("\n"),rules:["Only emit when explicitly instructed by the system to summarise.","Never emit more than one summary block per turn.","Preserve key decisions, outcomes, and unresolved questions.","Do not summarise the current turn — only prior history."],schema:{type:"object",properties:{type:{type:"string",enum:["summary"]},text:{type:"string",description:"A concise summary replacing older conversation context."}},required:["type","text"]}},{type:"thinking",emittable:!1,description:["## Block: `thinking`","Internal chain-of-thought produced by the model before its final response.","Injected by the workspace from provider reasoning tokens — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["thinking"]},thinking:{type:"string",description:"The internal reasoning text produced before the final response."}},required:["type","thinking"]}},{type:"tool:use",emittable:!1,description:["## Block: `tool:use`","Captures a model request to execute a registered tool.","Constructed by the workspace from the provider native function-call response — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:use"]},name:{type:"string",description:"The exact registered name of the tool to execute."},input:{type:"object",description:"The parameter arguments for the tool execution.",additionalProperties:!0}},required:["type","name","input"]}},{type:"tool:result",emittable:!1,description:["## Block: `tool:result`","Contains the output of a tool execution, including any errors.","Injected by the workspace after a tool runs — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:result"]},useId:{type:"string",description:"The UUID of the ToolUseBlock that triggered this execution."},content:{oneOf:[{type:"string"},{type:"object",additionalProperties:!0}],description:"The serialized output of the tool, or a structured JSON response."},isError:{type:"boolean",description:"True if the tool execution resulted in an error."}},required:["type","useId","content"]}},{type:"image",emittable:!1,description:["## Block: `image`","An image asset attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["image"]},altText:{type:"string",description:"Accessible description of the image content."}},required:["type"]}},{type:"document",emittable:!1,description:["## Block: `document`","A document asset (PDF, DOCX, etc.) attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["document"]},title:{type:"string",description:"Human-readable title or filename of the document."}},required:["type"]}},{type:"role:transition",emittable:!1,description:["## Block: `role:transition`","Marks that the active session persona has switched from one role to another.","Emitted by the workspace when the active persona changes — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["role:transition"]},previousRole:{type:"string",description:"The name of the role the session is leaving."},newRole:{type:"string",description:"The name of the role the session is adopting."}},required:["type","newRole"]}}];function A(e,t){let s=e.filter((e=>e.emittable));if(!t)return s;if(t.only&&t.only.length>0){const e=new Set(t.only);return s.filter((t=>e.has(t.type)))}if(t.exclude&&t.exclude.length>0){const e=new Set(t.exclude);return s.filter((t=>!e.has(t.type)))}return s}var C=class{store=new Map;constructor(){for(const e of B)this.store.set(e.type,e)}register(e){if(this.store.has(e.type))throw new Error(`[BlockRegistry] Block type "${e.type}" is already registered. Use update() to modify it.`);this.store.set(e.type,{...e,emittable:!0,rules:[]})}unregister(e){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot unregister unknown block type "${e}".`);this.store.delete(e)}update(e,t){const s=this.store.get(e);s?this.store.set(e,{...s,...t,type:e}):this.store.set(e,{emittable:!0,rules:[],...t,type:e})}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}isType(e,t){return e.type===t}guard(e){return t=>t.type===e}schema(e){return{type:"object",description:"The model's structured response, containing an ordered sequence of content blocks.",properties:{blocks:{type:"array",description:"An ordered array of content blocks comprising the model's response.",items:{anyOf:A(Array.from(this.store.values()),e).map((e=>e.schema))}}},required:["blocks"]}}description(e){return["# BLOCK TYPES & USAGE RULES","","Your response MUST be a JSON object with a `blocks` array.","Each element of `blocks` must be one of the following types:","",A(Array.from(this.store.values()),e).map((e=>{const t=[e.description];return e.rules.length>0&&(t.push("**Rules:**"),t.push(...e.rules.map((e=>`- ${e}`)))),t.join("\n")})).join("\n\n"),"","## GLOBAL CONSTRAINTS","","- Output must be **valid JSON only** — no prose before or after the JSON object.","- Responses may contain multiple blocks. Blocks are rendered in order.","- **Do NOT generate IDs.** The workspace is solely responsible for block ID assignment.","- Do not infer or invent fields not defined in the block schemas above."].join("\n")}rules(e){const t=A(Array.from(this.store.values()),e);return Object.fromEntries(t.map((e=>[e.type,[...e.rules]])))}setRules(e,t){const s=this.store.get(e);if(!s)throw new Error(`[BlockRegistry] Cannot set rules on unknown block type "${e}".`);this.store.set(e,{...s,rules:t})}parse(e,s){const r=this.store.get(e.type);if(!r)return console.warn(`[BlockRegistry] parse() received unknown block type "${e.type}". Returning null.`),null;if(s&&r.mappings?.[s])return r.mappings[s].from(e);const{id:n,...i}=e;return{id:t.v7(),...i}}create(e,s){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot create block of unknown type "${e}".`);return{...s,id:t.v7(),type:e}}clone(e,s){return{...structuredClone(e),...s,id:t.v7()}}},q=class{store=new Map;constructor(){this.registerDefaults()}registerDefaults(){this.register({kind:"text",target:"system",render:e=>({id:e.key,type:"text",text:e.content.value}),toString:e=>e.content.value,summarize:e=>({key:e.key,kind:"text",topics:e.topics,timestamp:e.timestamp,preview:e.content.value.slice(0,100)})}),this.register({kind:"json",target:"system",render:e=>({id:e.key,type:"text",text:JSON.stringify(e.content.value,null,2)}),toString:e=>JSON.stringify(e.content.value),summarize:e=>({key:e.key,kind:"json",topics:e.topics,timestamp:e.timestamp,preview:"JSON Data"})}),this.register({kind:"blob",target:"transcript",render:e=>{const{sha256:t,mediaType:s,sizeBytes:r,filename:n}=e.content,i={sha256:t,mediaType:s,sizeBytes:r,filename:n};return s.startsWith("image/")?{id:e.key,type:"image",ref:i}:{id:e.key,type:"document",ref:i,title:n}},toString:e=>e.content.filename||"unnamed blob",summarize:e=>({key:e.key,kind:"blob",topics:e.topics,timestamp:e.timestamp,mime:e.content.mediaType,size:e.content.sizeBytes,preview:e.content.filename})})}register(e){if(this.store.has(e.kind))throw new Error(`[ContextRegistry] Context kind "${e.kind}" is already registered.`);this.store.set(e.kind,e)}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}},U="__system__",$=class{constructor(e,s,r){this._actor=e,this._turn=r?JSON.parse(JSON.stringify(r)):{id:t.v7(),session:s,version:0,actor:this._actor,blocks:[],timestamp:(new Date).toISOString(),role:void 0}}_turn;addText(e){const s={id:t.v7(),type:"text",text:e};return this._turn.blocks.push(s),this}addImage(e,s){const r={id:t.v7(),type:"image",ref:e,altText:s};return this._turn.blocks.push(r),this}addDocument(e,s){const r={id:t.v7(),type:"document",ref:e,title:s};return this._turn.blocks.push(r),this}addToolUse(e,s){const r={id:t.v7(),type:"tool:use",name:e,input:s};return this._turn.blocks.push(r),this}addToolResult(e,s,r){const n={id:t.v7(),type:"tool:result",useId:e,content:s,isError:r};return this._turn.blocks.push(n),this}addSummary(e){const s={id:t.v7(),type:"summary",text:e};return this._turn.blocks.push(s),this}addRoleTransition(e,s){const r={id:t.v7(),type:"role:transition",previousRole:e,newRole:s};return this._turn.blocks.push(r),this}addThinking(e){const s={id:t.v7(),type:"thinking",thinking:e};return this._turn.blocks.push(s),this}addBlock(e){return this._turn.blocks.push(e),this}deleteBlock(e){return this._turn.blocks=this._turn.blocks.filter((t=>t.id!==e)),this}editTextBlock(e,t){const s=this._turn.blocks.findIndex((t=>t.id===e));if(-1===s)throw new Error(`Block with ID ${e} not found.`);const r=this._turn.blocks[s];if("text"!==r.type)throw new Error(`Block with ID ${e} is not a TextBlock.`);return this._turn.blocks[s]={...r,text:t},this}withId(e){return this._turn.id=e,this}withVersion(e){return this._turn.version=e,this}withTimestamp(e){return this._turn.timestamp=e,this}withParent(e){return this._turn.parent=e,this}withRoleSnapshot(e){return this._turn.role=e,this}build(){return JSON.parse(JSON.stringify(this._turn))}},M=class{constructor(e,t){this.turnStore=e,this.sessionStore=t}async loadAllTurns(e){return this.turnStore.listBySession(e)}async loadHead(e){const t=await this.sessionStore.get(e);return t?.head??null}},z=class e{constructor(e,t){this.nodes=e,this._head=t}static async build(t,s){const[r,n]=await Promise.all([s.loadAllTurns(t),s.loadHead(t)]),i=function(e,t){const s=function(e,t){if(!t)return new Set;const s=new Map;for(const t of e)s.set(`${t.id}:${t.version}`,t);const r=new Set;let n=t;for(;n;){const e=`${n.id}:${n.version}`;if(r.has(e))break;r.add(e);const t=s.get(e);if(!t)break;n=t.parent}return r}(e,t),r={},n={};for(const t of e){r[t.id]||(r[t.id]={id:t.id,versions:{},activeVersion:t.version,actor:t.actor,blocks:t.blocks,timestamp:t.timestamp,roleSnapshot:t.role,parent:t.parent,children:{}},n[t.id]=new Set);const e=r[t.id];e.versions[t.version]=t;const i=s.has(`${t.id}:${t.version}`),o=s.has(`${t.id}:${e.activeVersion}`);if(i&&(!o||t.version>=e.activeVersion)&&(e.activeVersion=t.version,e.blocks=t.blocks,e.timestamp=t.timestamp,e.roleSnapshot=t.role,e.parent=t.parent),t.parent){const e=`${t.parent.id}:${t.parent.version}`;if(n[t.parent.id]||(n[t.parent.id]=new Set),!n[t.parent.id].has(e+":"+t.id)){n[t.parent.id].add(e+":"+t.id),r[t.parent.id]||(r[t.parent.id]={id:t.parent.id,versions:{},activeVersion:t.parent.version,actor:"user",blocks:[],timestamp:"",roleSnapshot:void 0,children:{}});const s=r[t.parent.id];s.children[t.parent.version]||(s.children[t.parent.version]=[]),s.children[t.parent.version].push(t.id)}}}return r}(r,n);return new e(i,n)}head(){return this._head}chain(){if(!this._head)return[];const e=[];let t=this._head.id;for(;t;){const s=this.nodes[t];if(!s)break;e.push(s),t=s.parent?.id??null}return e.reverse()}getTurnSiblings(e){const t=this.nodes[e];if(!t)return[];if(!t.parent)return Object.values(this.nodes).filter((e=>!e.parent));const s=this.nodes[t.parent.id];if(!s)return[t];const r=s.children[t.parent.version];return r?r.map((e=>this.nodes[e])).filter((e=>!!e)):[t]}branchInfo(e){const t=this.nodes[e];if(!t)return{versions:[],currentIndex:-1,total:0,hasPrev:!1,hasNext:!1};const s=Object.keys(t.versions).map(Number).sort(((e,t)=>e-t)),r=s.indexOf(t.activeVersion);return{versions:s,currentIndex:r,total:s.length,hasPrev:r>0,hasNext:r<s.length-1}}graph(){return{...this.nodes}}};var F=class{constructor(e){this.manager=e}get workspace(){return this.manager.workspace()}roles(){return this.manager.ctx().roles.list()}role(e){return this.manager.ctx().roles.get(e)}async createRole(e,t,s,r,n=[]){if(this.workspace.index.roles[e])return y({code:"DUPLICATE_KEY",resource:"role",key:e});const i={name:e,label:t,description:r,persona:s,preferences:n,topics:[]};return this.manager.dispatch({type:"role:add",payload:i,timestamp:(new Date).toISOString()})}async updateRole(e,t){return this.workspace.index.roles[e]?this.manager.dispatch({type:"role:update",payload:{name:e,...t},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"role",id:e})}async deleteRole(e){const t=this.workspace;if(!t.index.roles[e])return y({code:"NOT_FOUND",resource:"role",id:e});return Object.values(t.index.sessions).some((t=>t.role===e))?y({code:"INVALID_COMMAND",reason:`Cannot delete role "${e}" — it is still referenced by one or more sessions`}):this.manager.dispatch({type:"role:delete",payload:{name:e},timestamp:(new Date).toISOString()})}async addPreference(e,t){const s={id:crypto.randomUUID(),content:e,topics:t,timestamp:(new Date).toISOString()};return this.manager.dispatch({type:"preference:add",payload:s,timestamp:s.timestamp})}preferences(){return this.manager.ctx().preferences.list()}async addContext(e,t,s,r){const n={key:e,topics:s,content:t,timestamp:(new Date).toISOString(),metadata:r};return this.manager.dispatch({type:"context:add",payload:n,timestamp:n.timestamp})}context(){return this.manager.ctx().context.list()}async registerBlob(e,t,s){const r=await this.manager.dispatch({type:"blob:register",payload:{data:e,mediaType:t,filename:s},timestamp:(new Date).toISOString()});if(!r.ok)return y(r.error);const n=r.value,i=n?.index?.blobs;if(!i)return y({code:"BACKEND_ERROR",reason:"Blob registration succeeded but no blobs patch returned"});const o=Object.keys(i)[0];if(!o)return y({code:"BACKEND_ERROR",reason:"No SHA256 in patch"});const a=i[o];if(!a)return y({code:"BACKEND_ERROR",reason:"Blob record missing"});return f({sha256:o,ref:{sha256:o,mediaType:a.mediaType,sizeBytes:a.sizeBytes,filename:a.filename,previewUrl:a.previewUrl}})}},P=class e{constructor(e,t,s,r){this._id=e,this.manager=t,this.processor=s,this.turnRepository=r,this.workspace=new F(t),this.unsubscribe=t.subscribe("workspace:synced",(()=>this.sync()))}workspace;_role=void 0;_preferences=[];tree;unsubscribe;static async create(t,s,r){s.ctx().workspace;const n=new M(s.ctx().turns,s.ctx().sessions),i=new e(t,s,r,n);return await i.sync(),i}async sync(){await this._setRole(),await this._setPreference(),await this.refreshTurnTree()}close(){this.unsubscribe?.()}async _setRole(){const e=this.meta(),t=await this.manager.ctx().roles.get(e.role);t&&(this._role=t)}async _setPreference(){const e=this.meta();this._preferences=await this.manager.ctx().preferences.load(e.preferences)}id(){return this._id}ws(){return this.manager.workspace()}meta(){return this.ws().index.sessions[this._id]}label(){return this.meta()?.label}role(){return this._role}topics(){return this.meta()?.topics??[]}preferences(){return this._preferences}head(){return this.meta()?.head??null}turns(){return this.tree.chain()}async siblings(e){return this.tree.getTurnSiblings(e)}async branchInfo(e){return this.tree.branchInfo(e)}async getTurn(e){return this.tree.graph()[e]}async rename(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,label:e},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async setTopics(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async addTopics(e){return this.meta()?this.dispatch({type:"session:topics:add",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async setPreferences(e){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const t=await this.dispatch({type:"session:preferences:override",payload:{sessionId:this._id,preferences:e},timestamp:(new Date).toISOString()});return t.ok&&await this._setPreference(),t}async fork(e,t,s,r){return this.meta()?this.dispatch({type:"session:fork",payload:{sessionId:this._id,newSessionId:e,label:t,role:s,topics:r},timestamp:(new Date).toISOString()}):y({code:"NOT_FOUND",resource:"session",id:this._id})}async dispatch(e){return this.manager.dispatch(e)}async switchRole(e){const t=this.ws();if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});if(e!==U&&!t.index.roles[e])return y({code:"NOT_FOUND",resource:"role",id:e});const s=await this.dispatch({type:"session:update",payload:{sessionId:this._id,role:e},timestamp:(new Date).toISOString()});return s.ok&&await this._setRole(),s}async addTurn(e){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const t={...e,session:this._id,version:e.version??0,parent:!e.parent&&this.head()?this.head():void 0},s=await this.dispatch({type:"turn:add",payload:{sessionId:this._id,turn:t},timestamp:t.timestamp});return s.ok&&await this.refreshTurnTree(),s}async recordUserTurn(e){return this.meta()?this.addTurn(e):y({code:"NOT_FOUND",resource:"session",id:this._id})}async recordAssistantTurn(e,t){const s=this.ws();if(!s)return y({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!s.index.sessions[this._id])return y({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.processor.process(e,this._id);let n={};const i=[];for(const e of r){const t=await this.manager.dispatch(e);if(t.ok)n=w(n,t.value);else{if("PERMISSION_DENIED"!==t.error.code||e.synthetic)return t;{const t=this.describeCommand(e);i.push({cmd:e,description:t})}}}const o=await this.addTurn(e);if(!o.ok)return o;if(n=w(n,o.value),t&&i.length>0){const e=this.buildDenialTurn(i),t=await this.addTurn(e);t.ok&&(n=w(n,t.value))}return f(n)}buildDenialTurn(e){const t=new $("user",this._id);for(const s of e)t.addText(`[System] User denied: ${s.description}.`);return t.build()}describeCommand(e){return"tool:call"===e.type?`tool execution: ${e.payload.tool}`:`command: ${e.type}`}async editTurn(e,t,s){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph()[e];if(!r)return y({code:"NOT_FOUND",resource:"turn",id:e});const n=r.versions[r.activeVersion],i=r.activeVersion+1,o=await this.dispatch({type:"turn:edit",payload:{sessionId:this._id,turnId:e,newBlocks:t,roleSnapshot:s??n.role,newVersion:i},timestamp:(new Date).toISOString()});return o.ok&&(await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:{id:e,version:i}},timestamp:(new Date).toISOString()}),await this.refreshTurnTree()),o}async branch(e){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});if(!e.parent)return y({code:"INVALID_COMMAND",reason:"branch requires turn.parent to be set"});const t=await this.dispatch({type:"turn:branch",payload:{sessionId:this._id,turn:{...e,session:this._id}},timestamp:e.timestamp});return t.ok&&await this.refreshTurnTree(),t}async deleteTurn(e,t,s){if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph();if(!r[e]?.versions[t])return y({code:"NOT_FOUND",resource:"turn version",id:`${e}:${t}`});const n=await this.dispatch({type:"turn:delete",payload:{sessionId:this._id,turnId:e,version:t,newHead:s},timestamp:(new Date).toISOString()});return n.ok&&await this.refreshTurnTree(),n}async switchVersionLeft(e){return this.switchVersion(e,-1)}async switchVersionRight(e){return this.switchVersion(e,1)}async switchVersion(e,t){if(!this.ws())return y({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!this.meta())return y({code:"NOT_FOUND",resource:"session",id:this._id});const s=this.tree.graph()[e];if(!s)return y({code:"NOT_FOUND",resource:"turn",id:e});const r=Object.keys(s.versions).map(Number).sort(((e,t)=>e-t)),n=r.indexOf(s.activeVersion);if(-1===n)return y({code:"INVALID_COMMAND",reason:"Active version not found"});const i=n+t;if(i<0||i>=r.length)return y({code:"INVALID_COMMAND",reason:`No ${t<0?"previous":"next"} version available for turn ${e}`});const o=r[i],a=this.findSubtreeTip(this.tree.graph(),e,o),c=await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:a},timestamp:(new Date).toISOString()});return c.ok&&await this.refreshTurnTree(),c}async snapshot(){const e=this,t=e.meta();if(!t)return;const s=e.ws(),r=e._role;if(!r)return;const n=await e.manager.ctx().context.getByTopics(s.index,t.topics),i=Array.from(new Set([...r.preferences,...t.preferences])),o=i.length>0?await e.manager.ctx().preferences.load(i):e._preferences,a=e.tree.chain().map((e=>e.versions[e.activeVersion])).filter((e=>!!e)),c=[...a].reverse().find((e=>"user"===e.actor));return{id:e._id,meta:t,role:r,preferences:o,context:n,model:t.metadata?.model,transcript:a,topics:t.topics,instructions:s.settings?.prompt,constraints:{role:r.constraints,session:t.constraints,turn:c?.constraints}}}async refreshTurnTree(){this.tree=await z.build(this._id,this.turnRepository)}findSubtreeTip(e,t,s){let r=t,n=s;for(;;){const t=e[r];if(!t)break;const s=t.children[n];if(!s||0===s.length)break;const i=s[0],o=e[i];if(!o)break;r=i,n=o.activeVersion}return{id:r,version:n}}},K=class{constructor(e,t){this.manager=e,this.processor=t}openOnce=new Map;async open(e){let t=this.openOnce.get(e);t||(t=new d,this.openOnce.set(e,t));const s=await t.do((async()=>{if(!this.manager.workspace().index.sessions[e])throw new Error(`Session ${e} not found in workspace index`);return P.create(e,this.manager,this.processor)}));if(s.error)throw s.error;return s.value}close(e){this.openOnce.delete(e)}async delete(e){const t=await this.manager.dispatch({type:"session:delete",payload:{sessionId:e},timestamp:(new Date).toISOString()});if(t.ok)return this.close(e),t.value=void 0,t;throw t.error}async has(e){return!this.manager.workspace().index.sessions[e]}async create(e){const s=t.v7(),r=await this.manager.dispatch({type:"session:create",payload:{...e,id:s},timestamp:(new Date).toISOString()});if(r.ok)return this.open(s);throw r.error}list(){return Object.values(this.manager.workspace().index.sessions)}metadata(e){return this.manager.workspace().index.sessions[e]}},L=class{constructor(e,t,s){this.storage=e,this.cache=t,this.bus=s}async register(e,t,s){const r=await g(e),n=await this.getRecord(r);if(n){const e={...n,refCount:n.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(e),this.cache.set(r,e),f(this.reference(e))}const i={sha256:r,mediaType:t,sizeBytes:e.byteLength,filename:s,refCount:1,remoteIds:{},createdAt:(new Date).toISOString(),lastUsedAt:(new Date).toISOString()};return this.storage.registerBlob?await this.storage.registerBlob(i,e):(await this.storage.storeBytes(r,e),await this.storage.saveRecord(i)),this.cache.set(r,i),this.bus.emit({name:"blobs:changed",payload:{sha256:r,record:i}}),f(this.reference(i))}async retain(e){const t=await this.getRecord(e);if(!t)return y({code:"NOT_FOUND",resource:"blob",id:e});const s={...t,refCount:t.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(s),this.cache.set(e,s),f(void 0)}async release(e){const t=await this.getRecord(e);if(!t)return y({code:"NOT_FOUND",resource:"blob",id:e});const s=Math.max(0,t.refCount-1),r={...t,refCount:s,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(r),this.cache.set(e,r),f(void 0)}async purge(e){return await this.storage.deleteBytes(e),await this.storage.deleteRecord(e),this.cache.delete(e),this.bus.emit({name:"blobs:changed",payload:{sha256:e}}),f(void 0)}async recordRemoteId(e,t,s,r){const n=await this.getRecord(e);if(!n)return y({code:"NOT_FOUND",resource:"blob",id:e});const i={...n,remoteIds:{...n.remoteIds,[t]:{id:s,timestamp:r||(new Date).toISOString()}},lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(i),this.cache.set(e,i),this.bus.emit({name:"blobs:changed",payload:{sha256:e,record:i}}),f(void 0)}async getRecord(e){const t=this.cache.get(e);if(t)return t;const s=await this.storage.loadRecord(e);return s&&this.cache.set(e,s),s}async getAllRecords(){const e=await this.storage.listRecords(),t={};for(const s of e)t[s.sha256]=s,this.cache.set(s.sha256,s);return t}reference(e){return{sha256:e.sha256,mediaType:e.mediaType,sizeBytes:e.sizeBytes,filename:e.filename}}async resolve(e,t){const s=await this.getRecord(e.sha256);if(!s)return f(null);if(t){const e=s.remoteIds[t];if(e)return f({kind:"remote",sha256:s.sha256,mediaType:s.mediaType,fileId:e.id,providerId:t,timestamp:e.timestamp});const r=await this.storage.loadBytes(s.sha256);return f(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}const r=await this.storage.loadBytes(s.sha256);return f(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}async resolveMany(e,t){const s=new Map;for(const r of e){const e=await this.resolve(r,t);if(!e.ok)return y(e.error);if(null===e.value)return y({code:"BLOB_ERROR",reason:`Unable to resolve blob ${r.sha256}${t?` with adapter ${t}`:""}`});s.set(r.sha256,e.value)}return f(s)}},V=class{constructor(e,t,s){this.collection=e,this.cache=t,this.registry=s}delegates=new Map;registerDelegate(e,t){this.delegates.set(e,t)}async get(e,t){const s=this.cache.get(e);if(s)return s;if(t){const s=this.delegates.get(t);if(s){const t=await s.get(e);return t&&this.cache.set(e,t),t}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;const n=r.state();return this.cache.set(e,n),n}async add(e){const t=this.delegates.get(e.content.kind);t?await t.add(e):await this.collection.create(e),this.cache.set(e.key,e)}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"}),t=((await e.next()).value||[]).map((e=>e.state()));for(const e of this.delegates.values()){const s=await e.list();t.push(...s)}return t}async update(e,t,s){if(s){const r=this.delegates.get(s);if(r){const s=await r.update(e,t);return s&&this.cache.set(e,s),s}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;await r.update(t);const n=r.state();return this.cache.set(e,n),n}async delete(e,t){if(t){const s=this.delegates.get(t);if(s){const t=await s.delete(e);return t&&this.cache.delete(e),t}}const s=await this.collection.find({field:"key",operator:"eq",value:e});if(!s)return!1;const r=await s.delete();return r&&this.cache.delete(e),r}async getByTopics(e,t){const s=new Map;for(const r of t){const t=e.topics[r];t&&t.contextKeys.forEach((t=>{const r=e.context[t],n=r?.kind||"unknown";s.has(n)||s.set(n,new Set),s.get(n).add(t)}))}if(0===s.size)return[];const r=[],n=[];for(const[e,t]of s.entries()){if(this.delegates.get(e))for(const s of t){const t=await this.get(s,e);t&&r.push(t)}else t.forEach((e=>n.push(e)))}if(n.length>0){const e=[];for(const t of n){const s=this.cache.get(t);s?r.push(s):e.push(t)}if(e.length>0){const t=await this.collection.filter({operator:"or",conditions:e.map((e=>({field:"key",operator:"eq",value:e})))});for(const e of t){const t=e.state();this.cache.set(t.key,t),r.push(t)}}}return r.sort(((e,t)=>new Date(t.timestamp).getTime()-new Date(e.timestamp).getTime()))}summarize(e){const t=this.registry?.get(e.content.kind);return t?t.summarize(e):{kind:e.content.kind,key:e.key,topics:e.topics,timestamp:e.timestamp,metadata:e.metadata}}},W=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async load(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?s.push(e):t.push(r)}if(t.length>0){const e=await this.collection.filter({operator:"or",conditions:t.map((e=>({field:"id",operator:"eq",value:e})))});for(const t of e){const e=t.state();this.cache.set(e.id,e),s.push(e)}}return s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{id:e.id,topics:e.topics,timestamp:e.timestamp,snippet:e.content.slice(0,100)}}},J=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{name:e.name,label:e.label,description:e.description,preferences:e.preferences?.length??0,topics:e.topics??[],constraints:e.constraints}}},G=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},Y=class{constructor(e,t){this.collection=e,this.cache=t}key({id:e,session:t,version:s}){return`${t}:${s}:${e}`}async find({id:e,session:t,version:s},r=!0){const n=this.key({id:e,session:t,version:s});this.cache.get(n)&&this.cache.get(n);const i=await this.collection.find({operator:"and",conditions:[{field:"id",operator:"eq",value:e},{field:"session",operator:"eq",value:t},{field:"version",operator:"eq",value:s}]});return i?(r&&this.cache.set(n,i),i):null}async get(e){return this.find(e)}async add(e){const t=await this.collection.create(e);this.cache.set(this.key(e),t)}async update(e,t){const s=await this.find(e);return s?(await s.update(t),s.state()):null}async listBySession(e){return(await this.collection.filter({field:"session",operator:"eq",value:e})).map((e=>e.state()))}async delete(e){const t=await this.find(e,!1);if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(this.key(e)),s}},H=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:100,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},X=class{cache;maxSize;constructor(e){this.cache=new Map,this.maxSize=e}get(e){const t=this.cache.get(e);return void 0!==t&&(this.cache.delete(e),this.cache.set(e,t)),t}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxSize){const e=this.cache.keys().next().value;void 0!==e&&this.cache.delete(e)}this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}},Q="blob_bytes",Z="blob_records";function ee(e){return new Promise(((t,s)=>{e.onsuccess=()=>t(e.result),e.onerror=()=>s(e.error)}))}function te(e){return new Promise(((t,s)=>{e.oncomplete=()=>t(),e.onerror=()=>s(e.error),e.onabort=()=>s(new Error("Transaction aborted"))}))}var se={};((e,t)=>{for(var r in t)s(e,r,{get:t[r],enumerable:!0})})(se,{GOOGLE_MODELS:()=>ne,GoogleGenAIAdapter:()=>le,mapBlockToPart:()=>ae,mapTurnToContent:()=>oe,mappings:()=>re,parseModelResponse:()=>ie});var re={text:{to:e=>({text:e.text}),from:e=>({id:t.v4(),type:"text",text:e.text})},summary:{to:e=>({text:`[Summary]: ${e.text}`}),from:e=>({id:t.v4(),type:"summary",text:e.text})},thinking:{to:e=>({text:e.thinking}),from:e=>({id:t.v4(),type:"thinking",thinking:e.thinking})},"tool:use":{to:e=>({functionCall:{name:e.name,args:e.input}}),from:e=>({id:t.v4(),type:"tool:use",name:e.functionCall?.name??e.name,input:e.functionCall?.args??e.input??{}})},"tool:result":{to:e=>({functionResponse:{name:e.useId,response:{result:e.content}}}),from:e=>({id:t.v4(),type:"tool:result",useId:e.useId,content:e.content,isError:e.isError})},"role:transition":{to:e=>({text:`[Role transition: ${e.previousRole??"none"} → ${e.newRole}]`}),from:e=>({id:t.v4(),type:"role:transition",previousRole:e.previousRole,newRole:e.newRole})}},ne=[{provider:"google",name:"gemini-3.1-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3-flash-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3,cache:{read:.05,write:1}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3.1-flash-lite-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.25,output:1.5,cache:{read:.025,write:1}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-pro",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10,cache:{read:.125,write:4.5}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.3,output:2.5,cache:{read:.03,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-lite",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4,cache:{read:.01,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-3-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-image",window:{size:4096,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"image",scale:0,cost:{input:.02,output:0}}],capacity:[{unit:"call",max:100,period:60}]},{provider:"google",name:"gemini-2.5-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-3.1-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-2.5-computer-use-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10}}],capacity:[{unit:"call",max:200,period:60},{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-001",window:{size:2048,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.5,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-2",window:{size:8192,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.3,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]}];function ie(e,s){const r=e.candidates?.[0];if(!r?.content?.parts)return y({code:"BACKEND_ERROR",reason:"No valid content parts in response"});const n=[];let i;for(const e of r.content.parts)if(e.thought)n.push((o=e.text??"",{id:t.v7(),type:"thinking",thinking:o}));else if(e.text){i=e;break}var o;if(!i)return y({code:"BACKEND_ERROR",reason:"Response missing JSON part"});const a=function(e){try{const t=JSON.parse(e);return t.blocks&&Array.isArray(t.blocks)?f(t.blocks):y({code:"BACKEND_ERROR",reason:'Invalid response: missing or malformed "blocks" array'})}catch(e){return y({code:"BACKEND_ERROR",reason:`Failed to parse response JSON: ${e instanceof Error?e.message:String(e)}`})}}(i.text);if(!a.ok)return a;for(const e of a.value){const t=s.parse(e,le.provider());t&&n.push(t)}return f(n)}async function oe(e,t,s){const r="assistant"===e.actor?"model":"user",n=[];for(const r of e.blocks){if("image"===r.type||"document"===r.type){const e=r.ref;if(!e)continue;const s=await ce(e,t);s&&n.push(s);continue}const e=ae(r,s);e&&n.push(e)}return{role:r,parts:n}}function ae(e,t){const s=t.get(e.type);if(!s)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: block type "${e.type}" is not registered. Register it via registry.register() before use.`);const r=s.mappings?.[le.provider()];if(!r)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: no "${le.provider()}" mapping for block type "${e.type}". Add a mapping via registry.update("${e.type}", { mappings: { ${le.provider()}: { to, from } } }).`);return r.to(e)}async function ce(e,t){const s=await t(e,le.provider());if(!s.ok||!s.value)return null;const{value:r}=s;return"remote"===r.kind?{fileData:{fileUri:r.fileId,mimeType:r.mediaType}}:{inlineData:{data:b(r.data),mimeType:r.mediaType}}}function de(e){return Math.ceil(e.length/4)}var le=class e{constructor(e,t,s={model:"gemini-2.5-flash"}){this.client=e,this.services=t,this._models=t.models;const r=this._models.get(s.model);if(!r)throw new Error(`Could not get model: ${s.model} profile in registry.`);this.model=r,this.registerGeminiMappings()}model;_models;registerGeminiMappings(){const t=this.services.blockRegistry;for(const[s,r]of Object.entries(re))t.update(s,{mappings:{[e.provider()]:r}})}async status(t){return{provider:e.provider(),model:this.model.name,ready:!0,window:this.model.window,feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4}}],rate:{load:0,capacity:[{unit:"call",max:1e3,period:60},{unit:"token",max:4e6,period:60}]}}}async resolve(e){const{prompt:s}=e,{assembler:r,blockRegistry:n,blobResolver:i}=this.services,o=s.model&&this._models.get(s.model)||this.model,a=r.build(s,[{label:"block-architecture",content:n.description(),position:"after:instructions"}]),c=r.join(a),d={role:"system",parts:[{text:c}]},l=s.constraints[o.name]??{tokens:{}},u=await Promise.all(s.transcript.map((e=>oe(e,i,n)))),p=n.schema(),h={model:o.name,contents:u,config:{systemInstruction:d,thinkingConfig:{includeThoughts:!0},responseMimeType:"application/json",responseSchema:p,...void 0!==l.temperature&&{temperature:l.temperature},...void 0!==l.tokens.max&&{maxOutputTokens:l.tokens.max},...l.tokens.stops?.length&&{stopSequences:l.tokens.stops},...void 0!==l.tokens.thought&&{thinkingConfig:{includeThoughts:!0,thinkingBudget:l.tokens.thought}}}},m=u.flatMap((e=>e.parts??[])).map((e=>e.text??"")).join(""),w=de(c),g=de(m),b=w+g,k=this;return{model:o.name,instructions:a,transcript:s.transcript,context:s.system.context,preferences:s.system.preferences,constraints:l,tokens:{breakdown:{system:w,transcript:g},total:b,source:"estimated",output:l.tokens.max??o.window.out,remaining:o.window.size-b},async execute(){let e;try{e=await k.client.models.generateContent(h)}catch(e){return y({code:"BACKEND_ERROR",reason:e instanceof Error?e.message:String(e)})}const t=ie(e,k.services.blockRegistry);if(!t.ok)return t;const r=t.value,n=new $("assistant",s.session);for(const e of r)n.addBlock(e);return f({turn:n.build(),effects:[]})},async*executeStream(){let e;try{e=await k.client.models.generateContentStream(h)}catch(e){const r=e instanceof Error?e.message:String(e),n=new $("assistant",s.session);return n.addBlock({id:t.v7(),type:"text",text:`[Stream error: ${r}]`}),void(yield{turn:n.build(),effects:[]})}const r=[];for await(const s of e){const e=s.candidates?.[0];if(e?.content?.parts)for(const s of e.content.parts)s.thought||s.text&&(r.push(s.text),yield{blocks:[{id:t.v7(),type:"text",text:s.text}]})}const n=r.join(""),i=new $("assistant",s.session);i.addBlock({id:t.v7(),type:"text",text:n}),yield{turn:i.build(),effects:[]}}}}static provider(){return"google"}static models(){return ne}};exports.COLLECTIONS=p,exports.EMPTY_SYSTEM_ROLE=U,exports.GoogleAdapter=se,exports.IndexedDBBlobStorage=class{dbName;db=null;constructor(e={}){this.dbName=e.dbName??"aiworkspace-blobs"}async open(){this.db||(this.db=await new Promise(((e,t)=>{const s=indexedDB.open(this.dbName,1);s.onupgradeneeded=e=>{const t=e.target.result;this.createSchema(t)},s.onsuccess=()=>e(s.result),s.onerror=()=>t(s.error),s.onblocked=()=>t(new Error(`[IndexedDBBlobStorage] Database "${this.dbName}" blocked.`))})),this.db.onversionchange=()=>{this.db?.close(),this.db=null})}createSchema(e){e.objectStoreNames.contains(Q)||e.createObjectStore(Q,{keyPath:"sha256"}),e.objectStoreNames.contains(Z)||e.createObjectStore(Z,{keyPath:"sha256"})}close(){this.db?.close(),this.db=null}async deleteDatabase(){this.close(),await new Promise(((e,t)=>{const s=indexedDB.deleteDatabase(this.dbName);s.onsuccess=()=>e(),s.onerror=()=>t(s.error)}))}getDB(){if(!this.db)throw new Error("[IndexedDBBlobStorage] Database not open. Call open() first.");return this.db}readTx(...e){return this.getDB().transaction(e,"readonly")}writeTx(...e){return this.getDB().transaction(e,"readwrite")}async storeBytes(e,t){const s=this.readTx(Q);if(await ee(s.objectStore(Q).get(e)))return;const r=this.writeTx(Q);r.objectStore(Q).put({sha256:e,data:t}),await te(r)}async loadBytes(e){const t=this.readTx(Q),s=await ee(t.objectStore(Q).get(e));return s?.data??null}async hasBytes(e){const t=this.readTx(Q);return await ee(t.objectStore(Q).count(e))>0}async deleteBytes(e){const t=this.writeTx(Q);t.objectStore(Q).delete(e),await te(t)}async saveRecord(e){const t=this.writeTx(Z);t.objectStore(Z).put(e),await te(t)}async loadRecord(e){const t=this.readTx(Z);return await ee(t.objectStore(Z).get(e))??null}async deleteRecord(e){const t=this.writeTx(Z);t.objectStore(Z).delete(e),await te(t)}async listRecords(){return ee(this.readTx(Z).objectStore(Z).getAll())}async exportAllBytes(){const e=this.readTx(Q);return(await ee(e.objectStore(Q).getAll())).map((({sha256:e,data:t})=>[e,t]))}async registerBlob(e,t){await new Promise(((s,r)=>{const n=this.getDB().transaction([Q,Z],"readwrite"),i=n.objectStore(Q),o=n.objectStore(Z),a=i.count(e.sha256);a.onsuccess=()=>{0===a.result&&i.put({sha256:e.sha256,data:t}),o.put(e)},a.onerror=()=>r(a.error),n.oncomplete=()=>s(),n.onerror=()=>r(n.error),n.onabort=()=>r(new Error("registerBlob transaction aborted"))}))}},exports.LRUCache=X,exports.MemoryBlobStorage=class{bytes=new Map;records=new Map;async storeBytes(e,t){this.bytes.has(e)||this.bytes.set(e,t)}async loadBytes(e){return this.bytes.get(e)??null}async hasBytes(e){return this.bytes.has(e)}async deleteBytes(e){this.bytes.delete(e)}async saveRecord(e){this.records.set(e.sha256,{...e})}async loadRecord(e){return this.records.get(e)??null}async deleteRecord(e){this.records.delete(e)}async listRecords(){return Array.from(this.records.values()).map((e=>({...e})))}async exportAllBytes(){return Array.from(this.bytes.entries()).map((([e,t])=>[e,new Uint8Array(t)]))}async registerBlob(e,t){this.bytes.has(e.sha256)||this.bytes.set(e.sha256,t),this.records.set(e.sha256,{...e})}},exports.Session=P,exports.SessionManager=K,exports.TurnBuilder=$,exports.TurnTree=z,exports.WorkspaceManager=k,exports.bufferToBase64=b,exports.computeSHA256=g,exports.createEmptySession=(e={})=>({id:t.v7(),label:"New Conversation",role:"assistant-default",topics:[],preferences:[],metadata:{created:(new Date).toString(),updated:(new Date).toString()},...e,head:e.head?e.head:void 0}),exports.createEmptyTurn=(e={},s)=>({id:e.id??t.v7(),version:e.version??0,session:e.session??s?.id??t.v7(),actor:"user",blocks:[{id:t.v7(),type:"text",content:"Hello, world!"}],timestamp:(new Date).toString(),role:s?.role??"default",...e,parent:e.parent?e.parent:void 0}),exports.createEmptyWorkspace=(e={})=>({id:e.id??t.v7(),settings:w({language:"en-US",defaultRole:void 0,prompt:void 0},e.settings),project:w({id:t.v7(),name:"Unnamed Project"},e.project),index:w({roles:{},preferences:{},context:{},sessions:{},topics:{},blobs:{},tools:{},extensions:{}},e.index)}),exports.createWorkspace=async function(t){const{blobStorage:s,eventBus:n=e.createEventBus(),getWorkspace:i,setWorkspace:o,processor:a,guard:c,toolRegistry:d,models:l=[],extensions:h=[],extensionReducers:m={},extensionMiddleware:f=[],extensionIndexers:y=[],extensionStores:w,extensionSchemas:g=[]}=t,b=u(t.db),v=[...g,...h.flatMap((e=>e.schemas??[]))],_={...m,...h.reduce(((e,t)=>({...e,...t.reducers})),{})},O=[...f,...h.flatMap((e=>e.middleware??[]))],S=[...y,...h.flatMap((e=>e.indexers??[]))],T=h.flatMap((e=>e.blocks??[]));await b.open(v);const R={workspace:await b.collection(p.WORKSPACE),role:await b.collection(p.ROLE),preference:await b.collection(p.PREFERENCE),context:await b.collection(p.CONTEXT),session:await b.collection(p.SESSION),topic:await b.collection(p.TOPIC),turn:await b.collection(p.TURN)},D=new L(s,new X(200),n),B=new q,A={workspaceStore:new H(R.workspace,new X(1)),roles:new J(R.role,new X(100)),preferences:new W(R.preference,new X(500)),context:new V(R.context,new X(500),B),topics:new x(R.topic,new X(100)),sessions:new G(R.session,new X(50)),turns:new Y(R.turn,new X(50)),blobs:D,tools:d,db:b,indexers:[...r,...S]};if(w){const e=w(A);Object.assign(A,e)}await Promise.all(h.map((async e=>{if(e.stores){const t=await e.stores(A);Object.assign(A,t)}})));const U=new k({ctx:A,getWorkspace:i,updateWorkspace:async e=>{if(await o(e),e?.id||e?.settings||e?.project){const e=i();await A.workspaceStore.update(e.id,{id:e.id,settings:e.settings,project:e.project})}},guard:c,bus:n});Object.entries(E).forEach((([e,t])=>{U.register(e,t)})),Object.entries(_).forEach((([e,t])=>{U.register(e,t)})),U.use(N),O.forEach((e=>{U.use(e)}));const $=new K(U,a),M=new C;T.forEach((e=>M.register(e))),h.forEach((e=>{e.contexts&&e.contexts.forEach((e=>{if(B.register(e),e.store){const t=e.store(A);A.context.registerDelegate(e.kind,t)}}))}));const z=new j(l),F={assembler:new I,processor:a,blobResolver:A.blobs.resolve.bind(A.blobs),blockRegistry:M,contextRegistry:B,models:z};return{manager:U,sessions:$,ctx:A,services:F,bootstrap:async e=>{await U.dispatch({type:"workspace:sync",payload:void 0,timestamp:(new Date).toISOString()});let t=i();t.id&&""!==t.id&&"00000000-0000-0000-0000-000000000000"!==t.id||(await U.dispatch({type:"workspace:create",payload:e.workspace,timestamp:(new Date).toISOString()}),t=i());const s=[];if(e.roles)for(const r of e.roles)t.index.roles[r.name]||(await U.dispatch({type:"role:add",payload:r,timestamp:(new Date).toISOString()}),s.push(r.name));return e.workspace.settings.defaultRole&&t.settings.defaultRole!==e.workspace.settings.defaultRole&&await U.dispatch({type:"workspace:sync",payload:{settings:{...t.settings,defaultRole:e.workspace.settings.defaultRole}},timestamp:(new Date).toISOString()}),{workspace:i(),roles:s}}}},exports.createWorkspaceDatabase=u,exports.del=function(){return h},exports.error=y,exports.getExtension=function(e,t){return e.extensions[t]},exports.merge=w,exports.omitNullUndefined=function(e){return Object.fromEntries(Object.entries(e).filter((([e,t])=>null!=t)))},exports.shortHash=function(e,t=4){let s=0;for(let t=0;t<e.length;t++)s=(s<<5)-s+e.charCodeAt(t),s|=0;return Math.abs(s).toString(36).padEnd(t,"0").slice(0,t)},exports.success=f;
package/index.mjs CHANGED
@@ -1 +1 @@
1
- import{createEventBus as e}from"@asaidimu/utils-events";import{v7 as t,v4 as s}from"uuid";var r=Object.defineProperty,n=[async e=>{const t=await e.workspaceStore.list();if(0===t.length)return{};const s=t[0];return{id:s.id,settings:s.settings,project:s.project}},async e=>{const t=await e.roles.list(),s={};for(const r of t)s[r.name]=e.roles.summarize(r);return{index:{roles:s}}},async e=>{const t=await e.preferences.list(),s={};for(const r of t)s[r.id]=e.preferences.summarize(r);return{index:{preferences:s}}},async e=>{const t=await e.context.list(),s={};for(const r of t)s[r.key]=e.context.summarize(r);return{index:{context:s}}},async e=>{const t=await e.sessions.list(),s={};for(const r of t)s[r.id]=e.sessions.summarize(r);return{index:{sessions:s}}},async e=>{const t=await e.topics.list(),s={};for(const r of t)s[r.name]=e.topics.summarize(r);return{index:{topics:s}}},async e=>({index:{blobs:await e.blobs.getAllRecords()}})],i=[{name:"workspace",version:"1.0.0",description:"Workspace root containing global settings and project metadata.",fields:{id:{name:"id",type:"string",required:!0},settings:{name:"settings",type:"record",required:!0},project:{name:"project",type:"record",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"}],constraints:[],migrations:[]},{name:"role",version:"1.0.0",description:"AI persona with a system prompt and associated preference defaults.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!0},description:{name:"description",type:"string",required:!1},persona:{name:"persona",type:"string",required:!0},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},topics:{name:"topics",type:"array",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_name",fields:["name"],type:"unique"},{name:"by_label",fields:["label"],type:"normal"}],constraints:[],migrations:[]},{name:"preference",version:"1.0.0",description:"A user behavioural instruction, scoped to zero or more topics.",fields:{id:{name:"id",type:"string",required:!0},content:{name:"content",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},timestamp:{name:"timestamp",type:"string",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"context",version:"1.0.0",description:"Injected background knowledge, scoped to topics. Content is a discriminated union.",fields:{key:{name:"key",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},content:{name:"content",type:"record",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},metadata:{name:"metadata",type:"record",required:!1}},indexes:[{name:"by_key",fields:["key"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"session",version:"1.0.0",description:"Session metadata. The head field tracks the current tip of the turn DAG.",fields:{id:{name:"id",type:"string",required:!0},label:{name:"label",type:"string",required:!0},role:{name:"role",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},metadata:{name:"metadata",type:"record",required:!1},head:{name:"head",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_role",fields:["role"],type:"normal"}],constraints:[],migrations:[]},{name:"turn",version:"1.0.0",description:["A single message in a session transcript, stored as a flat document.","The DAG is reconstructed in memory by TurnTree.buildNodeGraph() at session open time."].join(" "),fields:{id:{name:"id",type:"string",required:!0},session:{name:"session",type:"string",required:!0},version:{name:"version",type:"number",required:!0},actor:{name:"actor",type:"enum",required:!0,values:["user","assistant","tool"]},blocks:{name:"blocks",type:"array",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},role:{name:"role",type:"string",required:!1},parent:{name:"parent",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_session",fields:["session"],type:"normal"},{name:"by_session_parent",fields:["session","parent"],type:"composite"},{name:"by_session_id_ver",fields:["session","id","version"],type:"composite",unique:!0}],constraints:[],migrations:[]},{name:"topic",version:"1.0.0",description:"A categorization for context and preferences, used for retrieval.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!1},description:{name:"description",type:"string",required:!1},metadata:{name:"metadata",type:"record",required:!1},created:{name:"created",type:"string",required:!0},updated:{name:"updated",type:"string",required:!0}},indexes:[{name:"by_name",fields:["name"],type:"unique"}],constraints:[],migrations:[]}],o=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},a=class extends o{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},c=class extends o{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},d=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let r;await Promise.race([s.then((()=>clearTimeout(r))),new Promise(((s,n)=>{r=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new a("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},l=class{mutex=new d({yieldMode:"microtask"});promise=null;_value=null;_error;_done=!1;retry;throws;constructor({retry:e,throws:t}={}){this.retry=Boolean(e),this.throws=Boolean(t)}async do(e,t){return this._done?this.peek():this.promise?this._awaitWithTimeout(this.promise,t,"Once do() timed out"):(await this.mutex.lock(),this.promise?(this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")):(this.promise=(async()=>{try{this._value=await e(),this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.retry&&!this._done&&(this.promise=null)}return this.peek()})(),this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")))}doSync(e){if(this._done){if(this.throws&&this._error)throw this._error;return this.peek()}if(this.promise){const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}if(!this.mutex.tryLock()){const e=new Error("Cannot execute doSync: lock is currently held.");if(this.throws)throw e;return{value:null,error:e}}if(this.promise||this._done){if(this.mutex.unlock(),this._done){if(this.throws&&this._error)throw this._error;return this.peek()}const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}try{const t=e();this._value=t,this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.mutex.unlock()}return this.peek()}running(){return null!==this.promise&&!this.done()}peek(){return{value:this._value,error:this._error}}get(){if(!this._done)throw new Error("Once operation is not yet complete");if(this._error)throw this._error;return this._value}reset(){if(this.running())throw new Error("Cannot reset Once while an operation is in progress.");this._done=!1,this.promise=null,this._value=null,this._error=void 0}done(){return this._done}current(){return this.promise}_awaitWithTimeout(e,t,s="Operation timed out"){if(null==t)return e;let r;return Promise.race([e.then((e=>(clearTimeout(r),e))),new Promise(((e,n)=>{r=setTimeout((()=>n(new a(s))),t)}))])}},u=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new d({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new c};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,r=null;try{if(this._done)throw new c;r=await e(),this._lastValue=r,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:r,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}};function p(e){const t=new l({retry:!0,throws:!0}),s=new l({retry:!0,throws:!0});return{open:async(s=[])=>(await t.do((async()=>{const t=[...i,...s];await e.setupCollections(t)}))).value,collection:async t=>e.collection(t),close:async()=>(await s.do((()=>{e.close()}))).value,database:()=>e}}var h={WORKSPACE:"workspace",ROLE:"role",PREFERENCE:"preference",CONTEXT:"context",SESSION:"session",TURN:"turn",BLOB:"blob",TOPIC:"topic"},m=Symbol.for("delete"),f=e=>Array.isArray(e)?[...e]:{...e};function y(e){return{ok:!0,value:e}}function w(e){return{ok:!1,error:e}}function g(e){return Object.fromEntries(Object.entries(e).filter((([e,t])=>null!=t)))}function b(){return m}var k=function(e){const t=e?.deleteMarker||m;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const r={};for(const[n,i]of Object.entries(e))if(i!==t)if("object"==typeof i&&null!==i){const e=s(i);void 0!==e&&(r[n]=e)}else r[n]=i;return r}return e===t?void 0:e}return function(e,r){if("object"!=typeof e||null===e)return"object"==typeof r&&null!==r?s(r):r===t?{}:r;if("object"!=typeof r||null===r)return e;const n=f(e),i=[{target:n,source:r}];for(;i.length>0;){const{target:e,source:s}=i.pop();for(const r of Object.keys(s)){const n=s[r];if(n!==t)if(Array.isArray(n))e[r]=n;else if("object"==typeof n&&null!==n){const t=r in e&&"object"==typeof e[r]&&null!==e[r]?e[r]:{};e[r]=f(t),i.push({target:e[r],source:n})}else e[r]=n;else delete e[r]}}return n}}({deleteMarker:m});async function x(e){const t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t)).map((e=>e.toString(16).padStart(2,"0"))).join("")}function v(e){if("undefined"!=typeof Buffer)return Buffer.from(e).toString("base64");const t=[];for(let s=0;s<e.length;s+=32768){const r=e.subarray(s,s+32768);t.push(String.fromCharCode.apply(null,Array.from(r)))}return btoa(t.join(""))}function _(e,t=4){let s=0;for(let t=0;t<e.length;t++)s=(s<<5)-s+e.charCodeAt(t),s|=0;return Math.abs(s).toString(36).padEnd(t,"0").slice(0,t)}function O(e,t){return e.extensions[t]}var S=class{registry=new Map;middlewares=[];serializer=new u;bus;_getWorkspace;updateWorkspace;guard;_ctx;constructor(e){this._ctx=e.ctx,this._getWorkspace=e.getWorkspace,this.updateWorkspace=e.updateWorkspace,this.guard=e.guard,this.bus=e.bus}register(e,t){return this.registry.set(e,t),this}use(e){return this.middlewares.push(e),this}workspace(){return this._getWorkspace()}async dispatch(e){const t=await this.serializer.do((async()=>{if(this.guard){const t=await this.guard.authenticate({type:"command",payload:e});if(!t.ok)return w(t.error)}const t=this.registry.get(e.type);if(!t)return w({code:"INVALID_COMMAND",reason:`No reducer registered for command type: ${e.type}`});const s=this._getWorkspace(),r=await t({workspace:s,...this._ctx},e.payload);if(!r.ok)return r;let n=r.value;for(const t of this.middlewares){const r=await t({workspace:s,command:e,patch:n,...this._ctx});n=k(n,r)}return await this.updateWorkspace(n),this.bus.emit({name:"workspace:changed",payload:n}),"workspace:sync"===e.type&&this.bus.emit({name:"workspace:synced",payload:void 0}),y(n)}));if(t.error)throw t.error;return t.value}subscribe(e,t){return this.bus.subscribe(e,t)}ctx(){return{workspace:this._getWorkspace(),...this._ctx}}},T=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async getMany(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?t.push(e):s.push(r)}if(s.length>0){const e=await this.collection.filter({operator:"or",conditions:s.map((e=>({field:"name",operator:"eq",value:e})))});for(const s of e){const e=s.state();this.cache.set(e.name,e),t.push(e)}}return t}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}referencedBy(e,t){for(const s in t.roles)if(t.roles[s].topics?.includes(e))return!0;for(const s in t.sessions)if(t.sessions[s].topics?.includes(e))return!0;for(const s in t.context)if(t.context[s].topics?.includes(e))return!0;for(const s in t.preferences)if(t.preferences[s].topics?.includes(e))return!0;return!1}summarize(e){return{topic:e.name,contextKeys:[],preferences:[],metadata:{created:e.created,updated:e.updated,entries:0}}}};function N(e,t,s,r,n,i){const o={};for(const a of t){let t=e[a];if(!t){if("add"!==n||!i)continue;t={topic:a,contextKeys:[],preferences:[],metadata:{created:(new Date).toISOString(),updated:(new Date).toISOString(),entries:0}},i.add({name:a,created:t.metadata.created,updated:t.metadata.updated})}const c=t[r],d=c.includes(s);if("add"!==n||d){if("remove"===n&&d){const e=Math.max(0,(t.metadata.entries||0)-1);o[a]={...t,[r]:c.filter((e=>e!==s)),metadata:{...t.metadata,updated:(new Date).toISOString(),entries:e}}}}else o[a]={...t,[r]:[...c,s],metadata:{...t.metadata,updated:(new Date).toISOString(),entries:(t.metadata.entries||0)+1}}}return{index:{topics:o}}}function R(e,t,s,r){return N(e,t,s,"contextKeys","add",r)}function D(e,t,s){return N(e,t,s,"contextKeys","remove")}function I(e,t,s,r){return N(e,t,s,"preferences","add",r)}function E(e,t,s){return N(e,t,s,"preferences","remove")}var j=async e=>{if(!e.patch)return;const{workspace:t,patch:s}=e,r=t.index.topics;let n={};const i=e=>{const t=e?.index?.topics;t&&(n=k(n,t))};if(s.index?.context)for(const[n,o]of Object.entries(s.index.context)){const s=t.index.context[n];if(void 0===o&&s)i(D(r,s.topics,s.key));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(D(r,c,s.key)),a.length&&i(R(r,a,s.key,e.topics))}}else i(R(r,o.topics??[],o.key,e.topics))}if(s.index?.preferences)for(const[n,o]of Object.entries(s.index.preferences)){const s=t.index.preferences[n];if(void 0===o&&s)i(E(r,s.topics,s.id));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(E(r,c,s.id)),a.length&&i(I(r,a,s.id,e.topics))}}else i(I(r,o.topics??[],o.id,e.topics))}return Object.keys(n).length?{index:{topics:n}}:{}},A="\n# WORKSPACE OPERATING SYSTEM\nYou are operating within a structured workspace. Your output MUST be a JSON object matching the provided schema.\nYour entire response MUST be valid JSON. Nothing else.\n".trim();function B(e){if(!e||"append"===e)return{placement:"append",label:null};if("prepend"===e)return{placement:"prepend",label:null};const[t,...s]=e.split(":");return("before"===t||"after"===t)&&s.length>0?{placement:t,label:s.join(":")}:(console.warn(`[SystemPromptAssembler] Unrecognised position "${e}", defaulting to "append".`),{placement:"append",label:null})}var q=class{build(e,t=[]){const s=[...e.system.extensions||[],...t],r=[];var n,i,o;r.push({label:"operating-system",content:A}),e.role.persona&&r.push({label:"persona",content:(n=e.role.persona,`# Persona\n${n}`)}),e.system.preferences.length>0&&r.push({label:"preferences",content:(i=e.system.preferences,`# User Preferences\n${i.map((e=>`- ${e.content}`)).join("\n")}`),metadata:{count:e.system.preferences.length}}),e.system.context.length>0&&r.push({label:"context",content:(o=e.system.context,`# Context\n${o.map((e=>{switch(e.content.kind){case"text":return`[${e.key}] ${e.content.value}`;case"json":return`[${e.key}] JSON: ${JSON.stringify(e.content.value)}`;case"blob":return`[${e.key}] Blob: ${e.content.filename??"unnamed"} (${e.content.mediaType}, ${e.content.sizeBytes} bytes)`;case"remote":return`[${e.key}] Remote: ${e.content.uri}`;default:return`[${e.key}] Unsupported context type`}})).join("\n")}`),metadata:{count:e.system.context.length}}),e.system.instructions&&r.push({label:"instructions",content:`# Instructions\n${e.system.instructions}`});const a=[...r],c=[];for(const e of s){const{position:t,...s}=e,r=B(t);if("prepend"===r.placement){c.push(s);continue}if("append"===r.placement){a.push(s);continue}const n=a.findIndex((e=>e.label===r.label));if(-1===n){console.warn(`[SystemPromptAssembler] Anchor target "${r.label}" not found in sections. Extension "${e.label}" will be appended.`),a.push(s);continue}const i="before"===r.placement?n:n+1;a.splice(i,0,s)}return[...c,...a]}join(e){return e.map((e=>e.content)).join("\n\n---\n\n")}};var C={"workspace:create":async function(e,t){const{workspaceStore:s}=e,{id:r,settings:n,project:i}=t;return await s.add({id:r,settings:n,project:i}),y({id:r,settings:n,project:i})},"workspace:sync":async function(e,t){let s={};t&&(t.id&&(s.id=t.id),t.settings&&(s.settings=t.settings),t.project&&(s.project=t.project));for(const t of e.indexers){const r=await t(e);s=k(s,r)}return y(s)},"role:add":async function(e,t){const{workspace:s,roles:r}=e,n=t;return s.index.roles[n.name]?w({code:"DUPLICATE_KEY",resource:"Role",key:n.name}):(await r.add(n),y({index:{roles:{[n.name]:r.summarize(n)}}}))},"role:update":async function(e,t){const{workspace:s,roles:r}=e,{name:n,...i}=t;if(!s.index.roles[n])return w({code:"NOT_FOUND",resource:"Role",id:n});const o=await r.update(n,i);return o?y({index:{roles:{[n]:r.summarize(o)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update role ${n} in store.`})},"role:delete":async function(e,t){const{workspace:s,roles:r}=e,{name:n}=t;return s.index.roles[n]?await r.delete(n)?y({index:{roles:{[n]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete role ${n} from store.`}):w({code:"NOT_FOUND",resource:"Role",id:n})},"preference:add":async function(e,t){const{workspace:s,preferences:r}=e,n=t;return s.index.preferences[n.id]?w({code:"DUPLICATE_KEY",resource:"Preference",key:n.id}):(await r.add(n),y({index:{preferences:{[n.id]:r.summarize(n)}}}))},"preference:update":async function(e,t){const{workspace:s,preferences:r}=e,{id:n,...i}=t;if(!s.index.preferences[n])return w({code:"NOT_FOUND",resource:"Preference",id:n});const o=await r.update(n,i);return o?y({index:{preferences:{[n]:r.summarize(o)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update preference ${n} in store.`})},"preference:delete":async function(e,t){const{workspace:s,preferences:r}=e,{id:n}=t;return s.index.preferences[n]?await r.delete(n)?y({index:{preferences:{[n]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete preference ${n} from store.`}):w({code:"NOT_FOUND",resource:"Preference",id:n})},"context:add":async function(e,t){const{workspace:s,context:r}=e,n=t;return s.index.context[n.key]?w({code:"DUPLICATE_KEY",resource:"Context",key:n.key}):(await r.add(n),y({index:{context:{[n.key]:r.summarize(n)}}}))},"context:update":async function(e,t){const{workspace:s,context:r}=e,{key:n,...i}=t,o=s.index.context[n];if(!o)return w({code:"NOT_FOUND",resource:"Context",id:n});const a=await r.update(n,i,o.kind);return a?y({index:{context:{[n]:r.summarize(a)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update context ${n} in store.`})},"context:delete":async function(e,t){const{workspace:s,context:r}=e,{key:n}=t,i=s.index.context[n];return i?await r.delete(n,i.kind)?y({index:{context:{[n]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete context ${n} from store.`}):w({code:"NOT_FOUND",resource:"Context",id:n})},"topic:add":async function(e,t){const{workspace:s,topics:r}=e,n=t;return s.index.topics[n.name]?w({code:"DUPLICATE_KEY",resource:"Topic",key:n.name}):(await r.add(n),y({index:{topics:{[n.name]:r.summarize(n)}}}))},"topic:update":async function(e,t){const{workspace:s,topics:r}=e,{name:n,...i}=t;if(!s.index.topics[n])return w({code:"NOT_FOUND",resource:"Topic",id:n});const o=await r.update(n,i);return o?y({index:{topics:{[n]:{metadata:{updated:o.updated}}}}}):w({code:"BACKEND_ERROR",reason:`Failed to update topic ${n} in store.`})},"topic:delete":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{name:o,cascade:a}=t;if(!s.index.topics[o])return w({code:"NOT_FOUND",resource:"Topic",id:o});if(r.referencedBy(o,s.index)&&!a)return w({code:"INVALID_COMMAND",reason:`Topic '${o}' is referenced by other entities. Use 'cascade: true' to force deletion.`});if(a){for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);t&&await n.update(e,{topics:t.topics.filter((e=>e!==o))})}}for(const e in s.index.sessions){const t=s.index.sessions[e];t.topics?.includes(o)&&await i.update(e,{topics:t.topics.filter((e=>e!==o))})}}return await r.delete(o)?y({index:{topics:{[o]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete topic ${o} from store.`})},"topic:merge":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{source:o,target:a}=t;if(!s.index.topics[o]||!s.index.topics[a])return w({code:"NOT_FOUND",resource:"Topic",id:s.index.topics[o]?a:o});for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);if(t){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await n.update(e,{topics:s})}}}for(const e in s.index.sessions){const t=s.index.sessions[e];if(t.topics?.includes(o)){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await i.update(e,{topics:s})}}return await r.delete(o),y({index:{topics:{[o]:m}}})},"session:create":async function(e,t){const{workspace:s,sessions:r}=e,n=t;return s.index.sessions[n.id]?w({code:"DUPLICATE_KEY",resource:"Session",key:n.id}):(await r.add(n),y({index:{sessions:{[n.id]:r.summarize(n)}}}))},"session:update":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,...i}=t;if(!s.index.sessions[n])return w({code:"NOT_FOUND",resource:"Session",id:n});const o=await r.update(n,i);return o?y({index:{sessions:{[n]:r.summarize(o)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update session ${n} in store.`})},"session:fork":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,newSessionId:i,label:o}=t,a=s.index.sessions[n];if(!a)return w({code:"NOT_FOUND",resource:"Session",id:n});const c={...a,id:i,label:o||`Fork of ${a.label}`,role:t.role?t.role:a.role,topics:t.topics?t.topics:a.topics};return await r.add(c),y({index:{sessions:{[i]:r.summarize(c)}}})},"session:delete":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n}=t;return s.index.sessions[n]?(await r.delete(n),y({index:{sessions:{[n]:m}}})):w({code:"NOT_FOUND",resource:"Session",id:n})},"session:role:switch":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,roleName:i}=t;return s.index.sessions[n]?(await r.update(n,{role:i}),y({index:{sessions:{[n]:{role:i}}}})):w({code:"NOT_FOUND",resource:"Session",id:n})},"session:topics:add":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,topics:i}=t,o=s.index.sessions[n];if(!o)return w({code:"NOT_FOUND",resource:"Session",id:n});const a=Array.from(new Set([...o.topics,...i]));return await r.update(n,{topics:a}),y({index:{sessions:{[n]:{topics:a}}}})},"session:preferences:override":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,preferences:i}=t;return s.index.sessions[n]?(await r.update(n,{preferences:i}),y({index:{sessions:{[n]:{preferences:i}}}})):w({code:"NOT_FOUND",resource:"Session",id:n})},"turn:add":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return w({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})},"turn:update":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return w({code:"NOT_FOUND",resource:"Session",id:i});await n.update(o,o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})},"turn:edit":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newBlocks:a,newVersion:c,roleSnapshot:d}=t,l=s.index.sessions[i];if(!l)return w({code:"NOT_FOUND",resource:"Session",id:i});const u=l.head;let p={};if(u&&u.id===o){const e=await n.get({id:u.id,version:u.version,session:i});e&&(p={...e})}const h={...p,id:o,version:c,blocks:a,role:d??p.role,timestamp:(new Date).toISOString(),parent:p.parent,actor:p.actor||"assistant",session:i};if(await n.add(h),l.head?.id===o){const e={id:o,version:c};return await r.update(i,{head:e}),y({index:{sessions:{[i]:{head:e}}}})}return y({})},"turn:branch":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return w({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})},"turn:delete":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newHead:a}=t;return s.index.sessions[i]?(await n.delete({session:i,id:o,version:t.version}),a?(await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})):y({})):w({code:"NOT_FOUND",resource:"Session",id:i})},"blob:register":async function(e,t){const{blobs:s}=e,r=await s.register(t.data,t.mediaType,t.filename);if(!r.ok)return r;const n=await s.getRecord(r.value.sha256);return n?y({index:{blobs:{[n.sha256]:n}}}):w({code:"BACKEND_ERROR",reason:`Failed to retrieve registered blob record for ${r.value.sha256}`})},"blob:retain":async function(e,t){const{blobs:s}=e,r=await s.retain(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return y({index:{blobs:{[t.sha256]:n}}})},"blob:release":async function(e,t){const{blobs:s}=e,r=await s.release(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return y({index:{blobs:{[t.sha256]:n||m}}})},"blob:purge":async function(e,t){const{blobs:s}=e,r=await s.purge(t.sha256);return r.ok?y({index:{blobs:{[t.sha256]:m}}}):r},"blob:record_remote_id":async function(e,t){const{blobs:s}=e,{sha256:r,providerId:n,fileId:i,timestamp:o}=t,a=await s.recordRemoteId(r,n,i,o);if(!a.ok)return a;const c=await s.getRecord(r);return y({index:{blobs:{[r]:c}}})},"tool:call":async function(e,t){return y({})}},U=class{profiles;constructor(e=[]){this.profiles=new Map;for(const t of e)this.profiles.set(t.name,t)}get(e){return this.profiles.get(e)}list(e){const t=Array.from(this.profiles.values());return e?t.filter((t=>t.provider===e)):t}register(e){this.profiles.set(e.name,e)}},$=[{type:"text",emittable:!0,description:["## Block: `text`","Primary communication block. Use for all conversational responses, explanations, and prose.","A response may contain multiple `text` blocks interleaved with other block types."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["text"]},text:{type:"string",description:"The raw markdown or plain text content of the response."}},required:["type","text"]}},{type:"summary",emittable:!0,description:["## Block: `summary`","Context compression block. Replaces older conversation history with a concise digest."].join("\n"),rules:["Only emit when explicitly instructed by the system to summarise.","Never emit more than one summary block per turn.","Preserve key decisions, outcomes, and unresolved questions.","Do not summarise the current turn — only prior history."],schema:{type:"object",properties:{type:{type:"string",enum:["summary"]},text:{type:"string",description:"A concise summary replacing older conversation context."}},required:["type","text"]}},{type:"thinking",emittable:!1,description:["## Block: `thinking`","Internal chain-of-thought produced by the model before its final response.","Injected by the workspace from provider reasoning tokens — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["thinking"]},thinking:{type:"string",description:"The internal reasoning text produced before the final response."}},required:["type","thinking"]}},{type:"tool:use",emittable:!1,description:["## Block: `tool:use`","Captures a model request to execute a registered tool.","Constructed by the workspace from the provider native function-call response — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:use"]},name:{type:"string",description:"The exact registered name of the tool to execute."},input:{type:"object",description:"The parameter arguments for the tool execution.",additionalProperties:!0}},required:["type","name","input"]}},{type:"tool:result",emittable:!1,description:["## Block: `tool:result`","Contains the output of a tool execution, including any errors.","Injected by the workspace after a tool runs — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:result"]},useId:{type:"string",description:"The UUID of the ToolUseBlock that triggered this execution."},content:{oneOf:[{type:"string"},{type:"object",additionalProperties:!0}],description:"The serialized output of the tool, or a structured JSON response."},isError:{type:"boolean",description:"True if the tool execution resulted in an error."}},required:["type","useId","content"]}},{type:"image",emittable:!1,description:["## Block: `image`","An image asset attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["image"]},altText:{type:"string",description:"Accessible description of the image content."}},required:["type"]}},{type:"document",emittable:!1,description:["## Block: `document`","A document asset (PDF, DOCX, etc.) attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["document"]},title:{type:"string",description:"Human-readable title or filename of the document."}},required:["type"]}},{type:"role:transition",emittable:!1,description:["## Block: `role:transition`","Marks that the active session persona has switched from one role to another.","Emitted by the workspace when the active persona changes — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["role:transition"]},previousRole:{type:"string",description:"The name of the role the session is leaving."},newRole:{type:"string",description:"The name of the role the session is adopting."}},required:["type","newRole"]}}];function z(e,t){let s=e.filter((e=>e.emittable));if(!t)return s;if(t.only&&t.only.length>0){const e=new Set(t.only);return s.filter((t=>e.has(t.type)))}if(t.exclude&&t.exclude.length>0){const e=new Set(t.exclude);return s.filter((t=>!e.has(t.type)))}return s}var M=class{store=new Map;constructor(){for(const e of $)this.store.set(e.type,e)}register(e){if(this.store.has(e.type))throw new Error(`[BlockRegistry] Block type "${e.type}" is already registered. Use update() to modify it.`);this.store.set(e.type,{...e,emittable:!0,rules:[]})}unregister(e){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot unregister unknown block type "${e}".`);this.store.delete(e)}update(e,t){const s=this.store.get(e);s?this.store.set(e,{...s,...t,type:e}):this.store.set(e,{emittable:!0,rules:[],...t,type:e})}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}isType(e,t){return e.type===t}guard(e){return t=>t.type===e}schema(e){return{type:"object",description:"The model's structured response, containing an ordered sequence of content blocks.",properties:{blocks:{type:"array",description:"An ordered array of content blocks comprising the model's response.",items:{anyOf:z(Array.from(this.store.values()),e).map((e=>e.schema))}}},required:["blocks"]}}description(e){return["# BLOCK TYPES & USAGE RULES","","Your response MUST be a JSON object with a `blocks` array.","Each element of `blocks` must be one of the following types:","",z(Array.from(this.store.values()),e).map((e=>{const t=[e.description];return e.rules.length>0&&(t.push("**Rules:**"),t.push(...e.rules.map((e=>`- ${e}`)))),t.join("\n")})).join("\n\n"),"","## GLOBAL CONSTRAINTS","","- Output must be **valid JSON only** — no prose before or after the JSON object.","- Responses may contain multiple blocks. Blocks are rendered in order.","- **Do NOT generate IDs.** The workspace is solely responsible for block ID assignment.","- Do not infer or invent fields not defined in the block schemas above."].join("\n")}rules(e){const t=z(Array.from(this.store.values()),e);return Object.fromEntries(t.map((e=>[e.type,[...e.rules]])))}setRules(e,t){const s=this.store.get(e);if(!s)throw new Error(`[BlockRegistry] Cannot set rules on unknown block type "${e}".`);this.store.set(e,{...s,rules:t})}parse(e,s){const r=this.store.get(e.type);if(!r)return console.warn(`[BlockRegistry] parse() received unknown block type "${e.type}". Returning null.`),null;if(s&&r.mappings?.[s])return r.mappings[s].from(e);const{id:n,...i}=e;return{id:t(),...i}}create(e,s){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot create block of unknown type "${e}".`);return{...s,id:t(),type:e}}clone(e,s){return{...structuredClone(e),...s,id:t()}}},F=class{store=new Map;constructor(){this.registerDefaults()}registerDefaults(){this.register({kind:"text",target:"system",render:e=>({id:e.key,type:"text",text:e.content.value}),toString:e=>e.content.value,summarize:e=>({key:e.key,kind:"text",topics:e.topics,timestamp:e.timestamp,preview:e.content.value.slice(0,100)})}),this.register({kind:"json",target:"system",render:e=>({id:e.key,type:"text",text:JSON.stringify(e.content.value,null,2)}),toString:e=>JSON.stringify(e.content.value),summarize:e=>({key:e.key,kind:"json",topics:e.topics,timestamp:e.timestamp,preview:"JSON Data"})}),this.register({kind:"blob",target:"transcript",render:e=>{const{sha256:t,mediaType:s,sizeBytes:r,filename:n}=e.content,i={sha256:t,mediaType:s,sizeBytes:r,filename:n};return s.startsWith("image/")?{id:e.key,type:"image",ref:i}:{id:e.key,type:"document",ref:i,title:n}},toString:e=>e.content.filename||"unnamed blob",summarize:e=>({key:e.key,kind:"blob",topics:e.topics,timestamp:e.timestamp,mime:e.content.mediaType,size:e.content.sizeBytes,preview:e.content.filename})})}register(e){if(this.store.has(e.kind))throw new Error(`[ContextRegistry] Context kind "${e.kind}" is already registered.`);this.store.set(e.kind,e)}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}},P="__system__",K=class{constructor(e,s,r){this._actor=e,this._turn=r?JSON.parse(JSON.stringify(r)):{id:t(),session:s,version:0,actor:this._actor,blocks:[],timestamp:(new Date).toISOString(),role:void 0}}_turn;addText(e){const s={id:t(),type:"text",text:e};return this._turn.blocks.push(s),this}addImage(e,s){const r={id:t(),type:"image",ref:e,altText:s};return this._turn.blocks.push(r),this}addDocument(e,s){const r={id:t(),type:"document",ref:e,title:s};return this._turn.blocks.push(r),this}addToolUse(e,s){const r={id:t(),type:"tool:use",name:e,input:s};return this._turn.blocks.push(r),this}addToolResult(e,s,r){const n={id:t(),type:"tool:result",useId:e,content:s,isError:r};return this._turn.blocks.push(n),this}addSummary(e){const s={id:t(),type:"summary",text:e};return this._turn.blocks.push(s),this}addRoleTransition(e,s){const r={id:t(),type:"role:transition",previousRole:e,newRole:s};return this._turn.blocks.push(r),this}addThinking(e){const s={id:t(),type:"thinking",thinking:e};return this._turn.blocks.push(s),this}addBlock(e){return this._turn.blocks.push(e),this}deleteBlock(e){return this._turn.blocks=this._turn.blocks.filter((t=>t.id!==e)),this}editTextBlock(e,t){const s=this._turn.blocks.findIndex((t=>t.id===e));if(-1===s)throw new Error(`Block with ID ${e} not found.`);const r=this._turn.blocks[s];if("text"!==r.type)throw new Error(`Block with ID ${e} is not a TextBlock.`);return this._turn.blocks[s]={...r,text:t},this}withId(e){return this._turn.id=e,this}withVersion(e){return this._turn.version=e,this}withTimestamp(e){return this._turn.timestamp=e,this}withParent(e){return this._turn.parent=e,this}withRoleSnapshot(e){return this._turn.role=e,this}build(){return JSON.parse(JSON.stringify(this._turn))}},V=class{constructor(e,t){this.turnStore=e,this.sessionStore=t}async loadAllTurns(e){return this.turnStore.listBySession(e)}async loadHead(e){const t=await this.sessionStore.get(e);return t?.head??null}},L=class e{constructor(e,t){this.nodes=e,this._head=t}static async build(t,s){const[r,n]=await Promise.all([s.loadAllTurns(t),s.loadHead(t)]),i=function(e,t){const s=function(e,t){if(!t)return new Set;const s=new Map;for(const t of e)s.set(`${t.id}:${t.version}`,t);const r=new Set;let n=t;for(;n;){const e=`${n.id}:${n.version}`;if(r.has(e))break;r.add(e);const t=s.get(e);if(!t)break;n=t.parent}return r}(e,t),r={},n={};for(const t of e){r[t.id]||(r[t.id]={id:t.id,versions:{},activeVersion:t.version,actor:t.actor,blocks:t.blocks,timestamp:t.timestamp,roleSnapshot:t.role,parent:t.parent,children:{}},n[t.id]=new Set);const e=r[t.id];e.versions[t.version]=t;const i=s.has(`${t.id}:${t.version}`),o=s.has(`${t.id}:${e.activeVersion}`);if(i&&(!o||t.version>=e.activeVersion)&&(e.activeVersion=t.version,e.blocks=t.blocks,e.timestamp=t.timestamp,e.roleSnapshot=t.role,e.parent=t.parent),t.parent){const e=`${t.parent.id}:${t.parent.version}`;if(n[t.parent.id]||(n[t.parent.id]=new Set),!n[t.parent.id].has(e+":"+t.id)){n[t.parent.id].add(e+":"+t.id),r[t.parent.id]||(r[t.parent.id]={id:t.parent.id,versions:{},activeVersion:t.parent.version,actor:"user",blocks:[],timestamp:"",roleSnapshot:void 0,children:{}});const s=r[t.parent.id];s.children[t.parent.version]||(s.children[t.parent.version]=[]),s.children[t.parent.version].push(t.id)}}}return r}(r,n);return new e(i,n)}head(){return this._head}chain(){if(!this._head)return[];const e=[];let t=this._head.id;for(;t;){const s=this.nodes[t];if(!s)break;e.push(s),t=s.parent?.id??null}return e.reverse()}getTurnSiblings(e){const t=this.nodes[e];if(!t)return[];if(!t.parent)return Object.values(this.nodes).filter((e=>!e.parent));const s=this.nodes[t.parent.id];if(!s)return[t];const r=s.children[t.parent.version];return r?r.map((e=>this.nodes[e])).filter((e=>!!e)):[t]}branchInfo(e){const t=this.nodes[e];if(!t)return{versions:[],currentIndex:-1,total:0,hasPrev:!1,hasNext:!1};const s=Object.keys(t.versions).map(Number).sort(((e,t)=>e-t)),r=s.indexOf(t.activeVersion);return{versions:s,currentIndex:r,total:s.length,hasPrev:r>0,hasNext:r<s.length-1}}graph(){return{...this.nodes}}};var W=class{constructor(e){this.manager=e}get workspace(){return this.manager.workspace()}roles(){return this.manager.ctx().roles.list()}role(e){return this.manager.ctx().roles.get(e)}async createRole(e,t,s,r,n=[]){if(this.workspace.index.roles[e])return w({code:"DUPLICATE_KEY",resource:"role",key:e});const i={name:e,label:t,description:r,persona:s,preferences:n,topics:[]};return this.manager.dispatch({type:"role:add",payload:i,timestamp:(new Date).toISOString()})}async updateRole(e,t){return this.workspace.index.roles[e]?this.manager.dispatch({type:"role:update",payload:{name:e,...t},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"role",id:e})}async deleteRole(e){const t=this.workspace;if(!t.index.roles[e])return w({code:"NOT_FOUND",resource:"role",id:e});return Object.values(t.index.sessions).some((t=>t.role===e))?w({code:"INVALID_COMMAND",reason:`Cannot delete role "${e}" — it is still referenced by one or more sessions`}):this.manager.dispatch({type:"role:delete",payload:{name:e},timestamp:(new Date).toISOString()})}async addPreference(e,t){const s={id:crypto.randomUUID(),content:e,topics:t,timestamp:(new Date).toISOString()};return this.manager.dispatch({type:"preference:add",payload:s,timestamp:s.timestamp})}preferences(){return this.manager.ctx().preferences.list()}async addContext(e,t,s,r){const n={key:e,topics:s,content:t,timestamp:(new Date).toISOString(),metadata:r};return this.manager.dispatch({type:"context:add",payload:n,timestamp:n.timestamp})}context(){return this.manager.ctx().context.list()}async registerBlob(e,t,s){const r=await this.manager.dispatch({type:"blob:register",payload:{data:e,mediaType:t,filename:s},timestamp:(new Date).toISOString()});if(!r.ok)return w(r.error);const n=r.value,i=n?.index?.blobs;if(!i)return w({code:"BACKEND_ERROR",reason:"Blob registration succeeded but no blobs patch returned"});const o=Object.keys(i)[0];if(!o)return w({code:"BACKEND_ERROR",reason:"No SHA256 in patch"});const a=i[o];if(!a)return w({code:"BACKEND_ERROR",reason:"Blob record missing"});return y({sha256:o,ref:{sha256:o,mediaType:a.mediaType,sizeBytes:a.sizeBytes,filename:a.filename,previewUrl:a.previewUrl}})}},J=class e{constructor(e,t,s,r){this._id=e,this.manager=t,this.processor=s,this.turnRepository=r,this.workspace=new W(t),this.unsubscribe=t.subscribe("workspace:synced",(()=>this.sync()))}workspace;_role=void 0;_preferences=[];tree;unsubscribe;static async create(t,s,r){s.ctx().workspace;const n=new V(s.ctx().turns,s.ctx().sessions),i=new e(t,s,r,n);return await i.sync(),i}async sync(){await this._setRole(),await this._setPreference(),await this.refreshTurnTree()}close(){this.unsubscribe?.()}async _setRole(){const e=this.meta(),t=await this.manager.ctx().roles.get(e.role);t&&(this._role=t)}async _setPreference(){const e=this.meta();this._preferences=await this.manager.ctx().preferences.load(e.preferences)}id(){return this._id}ws(){return this.manager.workspace()}meta(){return this.ws().index.sessions[this._id]}label(){return this.meta()?.label}role(){return this._role}topics(){return this.meta()?.topics??[]}preferences(){return this._preferences}head(){return this.meta()?.head??null}turns(){return this.tree.chain()}async siblings(e){return this.tree.getTurnSiblings(e)}async branchInfo(e){return this.tree.branchInfo(e)}async getTurn(e){return this.tree.graph()[e]}async rename(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,label:e},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async setTopics(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async addTopics(e){return this.meta()?this.dispatch({type:"session:topics:add",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async setPreferences(e){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const t=await this.dispatch({type:"session:preferences:override",payload:{sessionId:this._id,preferences:e},timestamp:(new Date).toISOString()});return t.ok&&await this._setPreference(),t}async fork(e,t,s,r){return this.meta()?this.dispatch({type:"session:fork",payload:{sessionId:this._id,newSessionId:e,label:t,role:s,topics:r},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async dispatch(e){return this.manager.dispatch(e)}async switchRole(e){const t=this.ws();if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});if(e!==P&&!t.index.roles[e])return w({code:"NOT_FOUND",resource:"role",id:e});const s=await this.dispatch({type:"session:update",payload:{sessionId:this._id,role:e},timestamp:(new Date).toISOString()});return s.ok&&await this._setRole(),s}async addTurn(e){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const t={...e,session:this._id,version:e.version??0,parent:!e.parent&&this.head()?this.head():void 0},s=await this.dispatch({type:"turn:add",payload:{sessionId:this._id,turn:t},timestamp:t.timestamp});return s.ok&&await this.refreshTurnTree(),s}async recordUserTurn(e){return this.meta()?this.addTurn(e):w({code:"NOT_FOUND",resource:"session",id:this._id})}async recordAssistantTurn(e,t){const s=this.ws();if(!s)return w({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!s.index.sessions[this._id])return w({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.processor.process(e,this._id);let n={};const i=[];for(const e of r){const t=await this.manager.dispatch(e);if(t.ok)n=k(n,t.value);else{if("PERMISSION_DENIED"!==t.error.code||e.synthetic)return t;{const t=this.describeCommand(e);i.push({cmd:e,description:t})}}}const o=await this.addTurn(e);if(!o.ok)return o;if(n=k(n,o.value),t&&i.length>0){const e=this.buildDenialTurn(i),t=await this.addTurn(e);t.ok&&(n=k(n,t.value))}return y(n)}buildDenialTurn(e){const t=new K("user",this._id);for(const s of e)t.addText(`[System] User denied: ${s.description}.`);return t.build()}describeCommand(e){return"tool:call"===e.type?`tool execution: ${e.payload.tool}`:`command: ${e.type}`}async editTurn(e,t,s){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph()[e];if(!r)return w({code:"NOT_FOUND",resource:"turn",id:e});const n=r.versions[r.activeVersion],i=r.activeVersion+1,o=await this.dispatch({type:"turn:edit",payload:{sessionId:this._id,turnId:e,newBlocks:t,roleSnapshot:s??n.role,newVersion:i},timestamp:(new Date).toISOString()});return o.ok&&(await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:{id:e,version:i}},timestamp:(new Date).toISOString()}),await this.refreshTurnTree()),o}async branch(e){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});if(!e.parent)return w({code:"INVALID_COMMAND",reason:"branch requires turn.parent to be set"});const t=await this.dispatch({type:"turn:branch",payload:{sessionId:this._id,turn:{...e,session:this._id}},timestamp:e.timestamp});return t.ok&&await this.refreshTurnTree(),t}async deleteTurn(e,t,s){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph();if(!r[e]?.versions[t])return w({code:"NOT_FOUND",resource:"turn version",id:`${e}:${t}`});const n=await this.dispatch({type:"turn:delete",payload:{sessionId:this._id,turnId:e,version:t,newHead:s},timestamp:(new Date).toISOString()});return n.ok&&await this.refreshTurnTree(),n}async switchVersionLeft(e){return this.switchVersion(e,-1)}async switchVersionRight(e){return this.switchVersion(e,1)}async switchVersion(e,t){if(!this.ws())return w({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const s=this.tree.graph()[e];if(!s)return w({code:"NOT_FOUND",resource:"turn",id:e});const r=Object.keys(s.versions).map(Number).sort(((e,t)=>e-t)),n=r.indexOf(s.activeVersion);if(-1===n)return w({code:"INVALID_COMMAND",reason:"Active version not found"});const i=n+t;if(i<0||i>=r.length)return w({code:"INVALID_COMMAND",reason:`No ${t<0?"previous":"next"} version available for turn ${e}`});const o=r[i],a=this.findSubtreeTip(this.tree.graph(),e,o),c=await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:a},timestamp:(new Date).toISOString()});return c.ok&&await this.refreshTurnTree(),c}async snapshot(){const e=this,t=e.meta();if(!t)return;const s=e.ws(),r=e._role;if(!r)return;const n=await e.manager.ctx().context.getByTopics(s.index,t.topics),i=Array.from(new Set([...r.preferences,...t.preferences])),o=i.length>0?await e.manager.ctx().preferences.load(i):e._preferences,a=e.tree.chain().map((e=>e.versions[e.activeVersion])).filter((e=>!!e)),c=[...a].reverse().find((e=>"user"===e.actor));return{id:e._id,meta:t,role:r,preferences:o,context:n,model:t.metadata?.model,transcript:a,topics:t.topics,instructions:s.settings?.prompt,constraints:{role:r.constraints,session:t.constraints,turn:c?.constraints}}}async refreshTurnTree(){this.tree=await L.build(this._id,this.turnRepository)}findSubtreeTip(e,t,s){let r=t,n=s;for(;;){const t=e[r];if(!t)break;const s=t.children[n];if(!s||0===s.length)break;const i=s[0],o=e[i];if(!o)break;r=i,n=o.activeVersion}return{id:r,version:n}}},G=class{constructor(e,t){this.manager=e,this.processor=t}openOnce=new Map;async open(e){let t=this.openOnce.get(e);t||(t=new l,this.openOnce.set(e,t));const s=await t.do((async()=>{if(!this.manager.workspace().index.sessions[e])throw new Error(`Session ${e} not found in workspace index`);return J.create(e,this.manager,this.processor)}));if(s.error)throw s.error;return s.value}close(e){this.openOnce.delete(e)}async delete(e){const t=await this.manager.dispatch({type:"session:delete",payload:{sessionId:e},timestamp:(new Date).toISOString()});if(t.ok)return this.close(e),t.value=void 0,t;throw t.error}async create(e){const s=t(),r=await this.manager.dispatch({type:"session:create",payload:{...e,id:s},timestamp:(new Date).toISOString()});if(r.ok)return this.open(s);throw r.error}list(){return Object.values(this.manager.workspace().index.sessions)}metadata(e){return this.manager.workspace().index.sessions[e]}},Y=class{constructor(e,t,s){this.storage=e,this.cache=t,this.bus=s}async register(e,t,s){const r=await x(e),n=await this.getRecord(r);if(n){const e={...n,refCount:n.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(e),this.cache.set(r,e),y(this.reference(e))}const i={sha256:r,mediaType:t,sizeBytes:e.byteLength,filename:s,refCount:1,remoteIds:{},createdAt:(new Date).toISOString(),lastUsedAt:(new Date).toISOString()};return this.storage.registerBlob?await this.storage.registerBlob(i,e):(await this.storage.storeBytes(r,e),await this.storage.saveRecord(i)),this.cache.set(r,i),this.bus.emit({name:"blobs:changed",payload:{sha256:r,record:i}}),y(this.reference(i))}async retain(e){const t=await this.getRecord(e);if(!t)return w({code:"NOT_FOUND",resource:"blob",id:e});const s={...t,refCount:t.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(s),this.cache.set(e,s),y(void 0)}async release(e){const t=await this.getRecord(e);if(!t)return w({code:"NOT_FOUND",resource:"blob",id:e});const s=Math.max(0,t.refCount-1),r={...t,refCount:s,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(r),this.cache.set(e,r),y(void 0)}async purge(e){return await this.storage.deleteBytes(e),await this.storage.deleteRecord(e),this.cache.delete(e),this.bus.emit({name:"blobs:changed",payload:{sha256:e}}),y(void 0)}async recordRemoteId(e,t,s,r){const n=await this.getRecord(e);if(!n)return w({code:"NOT_FOUND",resource:"blob",id:e});const i={...n,remoteIds:{...n.remoteIds,[t]:{id:s,timestamp:r||(new Date).toISOString()}},lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(i),this.cache.set(e,i),this.bus.emit({name:"blobs:changed",payload:{sha256:e,record:i}}),y(void 0)}async getRecord(e){const t=this.cache.get(e);if(t)return t;const s=await this.storage.loadRecord(e);return s&&this.cache.set(e,s),s}async getAllRecords(){const e=await this.storage.listRecords(),t={};for(const s of e)t[s.sha256]=s,this.cache.set(s.sha256,s);return t}reference(e){return{sha256:e.sha256,mediaType:e.mediaType,sizeBytes:e.sizeBytes,filename:e.filename}}async resolve(e,t){const s=await this.getRecord(e.sha256);if(!s)return y(null);if(t){const e=s.remoteIds[t];if(e)return y({kind:"remote",sha256:s.sha256,mediaType:s.mediaType,fileId:e.id,providerId:t,timestamp:e.timestamp});const r=await this.storage.loadBytes(s.sha256);return y(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}const r=await this.storage.loadBytes(s.sha256);return y(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}async resolveMany(e,t){const s=new Map;for(const r of e){const e=await this.resolve(r,t);if(!e.ok)return w(e.error);if(null===e.value)return w({code:"BLOB_ERROR",reason:`Unable to resolve blob ${r.sha256}${t?` with adapter ${t}`:""}`});s.set(r.sha256,e.value)}return y(s)}},H=class{constructor(e,t,s){this.collection=e,this.cache=t,this.registry=s}delegates=new Map;registerDelegate(e,t){this.delegates.set(e,t)}async get(e,t){const s=this.cache.get(e);if(s)return s;if(t){const s=this.delegates.get(t);if(s){const t=await s.get(e);return t&&this.cache.set(e,t),t}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;const n=r.state();return this.cache.set(e,n),n}async add(e){const t=this.delegates.get(e.content.kind);t?await t.add(e):await this.collection.create(e),this.cache.set(e.key,e)}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"}),t=((await e.next()).value||[]).map((e=>e.state()));for(const e of this.delegates.values()){const s=await e.list();t.push(...s)}return t}async update(e,t,s){if(s){const r=this.delegates.get(s);if(r){const s=await r.update(e,t);return s&&this.cache.set(e,s),s}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;await r.update(t);const n=r.state();return this.cache.set(e,n),n}async delete(e,t){if(t){const s=this.delegates.get(t);if(s){const t=await s.delete(e);return t&&this.cache.delete(e),t}}const s=await this.collection.find({field:"key",operator:"eq",value:e});if(!s)return!1;const r=await s.delete();return r&&this.cache.delete(e),r}async getByTopics(e,t){const s=new Map;for(const r of t){const t=e.topics[r];t&&t.contextKeys.forEach((t=>{const r=e.context[t],n=r?.kind||"unknown";s.has(n)||s.set(n,new Set),s.get(n).add(t)}))}if(0===s.size)return[];const r=[],n=[];for(const[e,t]of s.entries()){if(this.delegates.get(e))for(const s of t){const t=await this.get(s,e);t&&r.push(t)}else t.forEach((e=>n.push(e)))}if(n.length>0){const e=[];for(const t of n){const s=this.cache.get(t);s?r.push(s):e.push(t)}if(e.length>0){const t=await this.collection.filter({operator:"or",conditions:e.map((e=>({field:"key",operator:"eq",value:e})))});for(const e of t){const t=e.state();this.cache.set(t.key,t),r.push(t)}}}return r.sort(((e,t)=>new Date(t.timestamp).getTime()-new Date(e.timestamp).getTime()))}summarize(e){const t=this.registry?.get(e.content.kind);return t?t.summarize(e):{kind:e.content.kind,key:e.key,topics:e.topics,timestamp:e.timestamp,metadata:e.metadata}}},X=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async load(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?s.push(e):t.push(r)}if(t.length>0){const e=await this.collection.filter({operator:"or",conditions:t.map((e=>({field:"id",operator:"eq",value:e})))});for(const t of e){const e=t.state();this.cache.set(e.id,e),s.push(e)}}return s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{id:e.id,topics:e.topics,timestamp:e.timestamp,snippet:e.content.slice(0,100)}}},Q=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{name:e.name,label:e.label,description:e.description,preferences:e.preferences?.length??0,topics:e.topics??[],constraints:e.constraints}}},Z=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},ee=class{constructor(e,t){this.collection=e,this.cache=t}key({id:e,session:t,version:s}){return`${t}:${s}:${e}`}async find({id:e,session:t,version:s},r=!0){const n=this.key({id:e,session:t,version:s});this.cache.get(n)&&this.cache.get(n);const i=await this.collection.find({operator:"and",conditions:[{field:"id",operator:"eq",value:e},{field:"session",operator:"eq",value:t},{field:"version",operator:"eq",value:s}]});return i?(r&&this.cache.set(n,i),i):null}async get(e){return this.find(e)}async add(e){const t=await this.collection.create(e);this.cache.set(this.key(e),t)}async update(e,t){const s=await this.find(e);return s?(await s.update(t),s.state()):null}async listBySession(e){return(await this.collection.filter({field:"session",operator:"eq",value:e})).map((e=>e.state()))}async delete(e){const t=await this.find(e,!1);if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(this.key(e)),s}},te=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:100,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},se=class{cache;maxSize;constructor(e){this.cache=new Map,this.maxSize=e}get(e){const t=this.cache.get(e);return void 0!==t&&(this.cache.delete(e),this.cache.set(e,t)),t}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxSize){const e=this.cache.keys().next().value;void 0!==e&&this.cache.delete(e)}this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}},re=(e={})=>({id:t(),label:"New Conversation",role:"assistant-default",topics:[],preferences:[],metadata:{created:(new Date).toString(),updated:(new Date).toString()},...e,head:e.head?e.head:void 0}),ne=(e={},s)=>({id:e.id??t(),version:e.version??0,session:e.session??s?.id??t(),actor:"user",blocks:[{id:t(),type:"text",content:"Hello, world!"}],timestamp:(new Date).toString(),role:s?.role??"default",...e,parent:e.parent?e.parent:void 0}),ie=(e={})=>({id:e.id??t(),settings:k({language:"en-US",defaultRole:void 0,prompt:void 0},e.settings),project:k({id:t(),name:"Unnamed Project"},e.project),index:k({roles:{},preferences:{},context:{},sessions:{},topics:{},blobs:{},tools:{},extensions:{}},e.index)}),oe=class{bytes=new Map;records=new Map;async storeBytes(e,t){this.bytes.has(e)||this.bytes.set(e,t)}async loadBytes(e){return this.bytes.get(e)??null}async hasBytes(e){return this.bytes.has(e)}async deleteBytes(e){this.bytes.delete(e)}async saveRecord(e){this.records.set(e.sha256,{...e})}async loadRecord(e){return this.records.get(e)??null}async deleteRecord(e){this.records.delete(e)}async listRecords(){return Array.from(this.records.values()).map((e=>({...e})))}async exportAllBytes(){return Array.from(this.bytes.entries()).map((([e,t])=>[e,new Uint8Array(t)]))}async registerBlob(e,t){this.bytes.has(e.sha256)||this.bytes.set(e.sha256,t),this.records.set(e.sha256,{...e})}},ae="blob_bytes",ce="blob_records";function de(e){return new Promise(((t,s)=>{e.onsuccess=()=>t(e.result),e.onerror=()=>s(e.error)}))}function le(e){return new Promise(((t,s)=>{e.oncomplete=()=>t(),e.onerror=()=>s(e.error),e.onabort=()=>s(new Error("Transaction aborted"))}))}var ue=class{dbName;db=null;constructor(e={}){this.dbName=e.dbName??"aiworkspace-blobs"}async open(){this.db||(this.db=await new Promise(((e,t)=>{const s=indexedDB.open(this.dbName,1);s.onupgradeneeded=e=>{const t=e.target.result;this.createSchema(t)},s.onsuccess=()=>e(s.result),s.onerror=()=>t(s.error),s.onblocked=()=>t(new Error(`[IndexedDBBlobStorage] Database "${this.dbName}" blocked.`))})),this.db.onversionchange=()=>{this.db?.close(),this.db=null})}createSchema(e){e.objectStoreNames.contains(ae)||e.createObjectStore(ae,{keyPath:"sha256"}),e.objectStoreNames.contains(ce)||e.createObjectStore(ce,{keyPath:"sha256"})}close(){this.db?.close(),this.db=null}async deleteDatabase(){this.close(),await new Promise(((e,t)=>{const s=indexedDB.deleteDatabase(this.dbName);s.onsuccess=()=>e(),s.onerror=()=>t(s.error)}))}getDB(){if(!this.db)throw new Error("[IndexedDBBlobStorage] Database not open. Call open() first.");return this.db}readTx(...e){return this.getDB().transaction(e,"readonly")}writeTx(...e){return this.getDB().transaction(e,"readwrite")}async storeBytes(e,t){const s=this.readTx(ae);if(await de(s.objectStore(ae).get(e)))return;const r=this.writeTx(ae);r.objectStore(ae).put({sha256:e,data:t}),await le(r)}async loadBytes(e){const t=this.readTx(ae),s=await de(t.objectStore(ae).get(e));return s?.data??null}async hasBytes(e){const t=this.readTx(ae);return await de(t.objectStore(ae).count(e))>0}async deleteBytes(e){const t=this.writeTx(ae);t.objectStore(ae).delete(e),await le(t)}async saveRecord(e){const t=this.writeTx(ce);t.objectStore(ce).put(e),await le(t)}async loadRecord(e){const t=this.readTx(ce);return await de(t.objectStore(ce).get(e))??null}async deleteRecord(e){const t=this.writeTx(ce);t.objectStore(ce).delete(e),await le(t)}async listRecords(){return de(this.readTx(ce).objectStore(ce).getAll())}async exportAllBytes(){const e=this.readTx(ae);return(await de(e.objectStore(ae).getAll())).map((({sha256:e,data:t})=>[e,t]))}async registerBlob(e,t){await new Promise(((s,r)=>{const n=this.getDB().transaction([ae,ce],"readwrite"),i=n.objectStore(ae),o=n.objectStore(ce),a=i.count(e.sha256);a.onsuccess=()=>{0===a.result&&i.put({sha256:e.sha256,data:t}),o.put(e)},a.onerror=()=>r(a.error),n.oncomplete=()=>s(),n.onerror=()=>r(n.error),n.onabort=()=>r(new Error("registerBlob transaction aborted"))}))}},pe={};((e,t)=>{for(var s in t)r(e,s,{get:t[s],enumerable:!0})})(pe,{GOOGLE_MODELS:()=>me,GoogleGenAIAdapter:()=>ke,mapBlockToPart:()=>we,mapTurnToContent:()=>ye,mappings:()=>he,parseModelResponse:()=>fe});var he={text:{to:e=>({text:e.text}),from:e=>({id:s(),type:"text",text:e.text})},summary:{to:e=>({text:`[Summary]: ${e.text}`}),from:e=>({id:s(),type:"summary",text:e.text})},thinking:{to:e=>({text:e.thinking}),from:e=>({id:s(),type:"thinking",thinking:e.thinking})},"tool:use":{to:e=>({functionCall:{name:e.name,args:e.input}}),from:e=>({id:s(),type:"tool:use",name:e.functionCall?.name??e.name,input:e.functionCall?.args??e.input??{}})},"tool:result":{to:e=>({functionResponse:{name:e.useId,response:{result:e.content}}}),from:e=>({id:s(),type:"tool:result",useId:e.useId,content:e.content,isError:e.isError})},"role:transition":{to:e=>({text:`[Role transition: ${e.previousRole??"none"} → ${e.newRole}]`}),from:e=>({id:s(),type:"role:transition",previousRole:e.previousRole,newRole:e.newRole})}},me=[{provider:"google",name:"gemini-3.1-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3-flash-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3,cache:{read:.05,write:1}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3.1-flash-lite-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.25,output:1.5,cache:{read:.025,write:1}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-pro",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10,cache:{read:.125,write:4.5}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.3,output:2.5,cache:{read:.03,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-lite",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4,cache:{read:.01,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-3-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-image",window:{size:4096,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"image",scale:0,cost:{input:.02,output:0}}],capacity:[{unit:"call",max:100,period:60}]},{provider:"google",name:"gemini-2.5-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-3.1-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-2.5-computer-use-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10}}],capacity:[{unit:"call",max:200,period:60},{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-001",window:{size:2048,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.5,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-2",window:{size:8192,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.3,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]}];function fe(e,s){const r=e.candidates?.[0];if(!r?.content?.parts)return w({code:"BACKEND_ERROR",reason:"No valid content parts in response"});const n=[];let i;for(const e of r.content.parts)if(e.thought)n.push((o=e.text??"",{id:t(),type:"thinking",thinking:o}));else if(e.text){i=e;break}var o;if(!i)return w({code:"BACKEND_ERROR",reason:"Response missing JSON part"});const a=function(e){try{const t=JSON.parse(e);return t.blocks&&Array.isArray(t.blocks)?y(t.blocks):w({code:"BACKEND_ERROR",reason:'Invalid response: missing or malformed "blocks" array'})}catch(e){return w({code:"BACKEND_ERROR",reason:`Failed to parse response JSON: ${e instanceof Error?e.message:String(e)}`})}}(i.text);if(!a.ok)return a;for(const e of a.value){const t=s.parse(e,ke.provider());t&&n.push(t)}return y(n)}async function ye(e,t,s){const r="assistant"===e.actor?"model":"user",n=[];for(const r of e.blocks){if("image"===r.type||"document"===r.type){const e=r.ref;if(!e)continue;const s=await ge(e,t);s&&n.push(s);continue}const e=we(r,s);e&&n.push(e)}return{role:r,parts:n}}function we(e,t){const s=t.get(e.type);if(!s)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: block type "${e.type}" is not registered. Register it via registry.register() before use.`);const r=s.mappings?.[ke.provider()];if(!r)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: no "${ke.provider()}" mapping for block type "${e.type}". Add a mapping via registry.update("${e.type}", { mappings: { ${ke.provider()}: { to, from } } }).`);return r.to(e)}async function ge(e,t){const s=await t(e,ke.provider());if(!s.ok||!s.value)return null;const{value:r}=s;return"remote"===r.kind?{fileData:{fileUri:r.fileId,mimeType:r.mediaType}}:{inlineData:{data:v(r.data),mimeType:r.mediaType}}}function be(e){return Math.ceil(e.length/4)}var ke=class e{constructor(e,t,s={model:"gemini-2.5-flash"}){this.client=e,this.services=t,this._models=t.models;const r=this._models.get(s.model);if(!r)throw new Error(`Could not get model: ${s.model} profile in registry.`);this.model=r,this.registerGeminiMappings()}model;_models;registerGeminiMappings(){const t=this.services.blockRegistry;for(const[s,r]of Object.entries(he))t.update(s,{mappings:{[e.provider()]:r}})}async status(t){return{provider:e.provider(),model:this.model.name,ready:!0,window:this.model.window,feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4}}],rate:{load:0,capacity:[{unit:"call",max:1e3,period:60},{unit:"token",max:4e6,period:60}]}}}async resolve(e){const{prompt:s}=e,{assembler:r,blockRegistry:n,blobResolver:i}=this.services,o=s.model&&this._models.get(s.model)||this.model,a=r.build(s,[{label:"block-architecture",content:n.description(),position:"after:instructions"}]),c=r.join(a),d={role:"system",parts:[{text:c}]},l=s.constraints[o.name]??{tokens:{}},u=await Promise.all(s.transcript.map((e=>ye(e,i,n)))),p=n.schema(),h={model:o.name,contents:u,config:{systemInstruction:d,thinkingConfig:{includeThoughts:!0},responseMimeType:"application/json",responseSchema:p,...void 0!==l.temperature&&{temperature:l.temperature},...void 0!==l.tokens.max&&{maxOutputTokens:l.tokens.max},...l.tokens.stops?.length&&{stopSequences:l.tokens.stops},...void 0!==l.tokens.thought&&{thinkingConfig:{includeThoughts:!0,thinkingBudget:l.tokens.thought}}}},m=u.flatMap((e=>e.parts??[])).map((e=>e.text??"")).join(""),f=be(c),g=be(m),b=f+g,k=this;return{model:o.name,instructions:a,transcript:s.transcript,context:s.system.context,preferences:s.system.preferences,constraints:l,tokens:{breakdown:{system:f,transcript:g},total:b,source:"estimated",output:l.tokens.max??o.window.out,remaining:o.window.size-b},async execute(){let e;try{e=await k.client.models.generateContent(h)}catch(e){return w({code:"BACKEND_ERROR",reason:e instanceof Error?e.message:String(e)})}const t=fe(e,k.services.blockRegistry);if(!t.ok)return t;const r=t.value,n=new K("assistant",s.session);for(const e of r)n.addBlock(e);return y({turn:n.build(),effects:[]})},async*executeStream(){let e;try{e=await k.client.models.generateContentStream(h)}catch(e){const r=e instanceof Error?e.message:String(e),n=new K("assistant",s.session);return n.addBlock({id:t(),type:"text",text:`[Stream error: ${r}]`}),void(yield{turn:n.build(),effects:[]})}const r=[];for await(const s of e){const e=s.candidates?.[0];if(e?.content?.parts)for(const s of e.content.parts)s.thought||s.text&&(r.push(s.text),yield{blocks:[{id:t(),type:"text",text:s.text}]})}const n=r.join(""),i=new K("assistant",s.session);i.addBlock({id:t(),type:"text",text:n}),yield{turn:i.build(),effects:[]}}}}static provider(){return"google"}static models(){return me}};async function xe(t){const{blobStorage:s,eventBus:r=e(),getWorkspace:i,setWorkspace:o,processor:a,guard:c,toolRegistry:d,models:l=[],extensions:u=[],extensionReducers:m={},extensionMiddleware:f=[],extensionIndexers:y=[],extensionStores:w,extensionSchemas:g=[]}=t,b=p(t.db),k=[...g,...u.flatMap((e=>e.schemas??[]))],x={...m,...u.reduce(((e,t)=>({...e,...t.reducers})),{})},v=[...f,...u.flatMap((e=>e.middleware??[]))],_=[...y,...u.flatMap((e=>e.indexers??[]))],O=u.flatMap((e=>e.blocks??[]));await b.open(k);const N={workspace:await b.collection(h.WORKSPACE),role:await b.collection(h.ROLE),preference:await b.collection(h.PREFERENCE),context:await b.collection(h.CONTEXT),session:await b.collection(h.SESSION),topic:await b.collection(h.TOPIC),turn:await b.collection(h.TURN)},R=new Y(s,new se(200),r),D=new F,I={workspaceStore:new te(N.workspace,new se(1)),roles:new Q(N.role,new se(100)),preferences:new X(N.preference,new se(500)),context:new H(N.context,new se(500),D),topics:new T(N.topic,new se(100)),sessions:new Z(N.session,new se(50)),turns:new ee(N.turn,new se(50)),blobs:R,tools:d,db:b,indexers:[...n,..._]};if(w){const e=w(I);Object.assign(I,e)}await Promise.all(u.map((async e=>{if(e.stores){const t=await e.stores(I);Object.assign(I,t)}})));const E=new S({ctx:I,getWorkspace:i,updateWorkspace:async e=>{if(await o(e),e?.id||e?.settings||e?.project){const e=i();await I.workspaceStore.update(e.id,{id:e.id,settings:e.settings,project:e.project})}},guard:c,bus:r});Object.entries(C).forEach((([e,t])=>{E.register(e,t)})),Object.entries(x).forEach((([e,t])=>{E.register(e,t)})),E.use(j),v.forEach((e=>{E.use(e)}));const A=new G(E,a),B=new M;O.forEach((e=>B.register(e))),u.forEach((e=>{e.contexts&&e.contexts.forEach((e=>{if(D.register(e),e.store){const t=e.store(I);I.context.registerDelegate(e.kind,t)}}))}));const $=new U(l),z={assembler:new q,processor:a,blobResolver:I.blobs.resolve.bind(I.blobs),blockRegistry:B,contextRegistry:D,models:$};return{manager:E,sessions:A,ctx:I,services:z}}export{h as COLLECTIONS,P as EMPTY_SYSTEM_ROLE,pe as GoogleAdapter,ue as IndexedDBBlobStorage,se as LRUCache,oe as MemoryBlobStorage,J as Session,G as SessionManager,K as TurnBuilder,L as TurnTree,S as WorkspaceManager,v as bufferToBase64,x as computeSHA256,re as createEmptySession,ne as createEmptyTurn,ie as createEmptyWorkspace,xe as createWorkspace,p as createWorkspaceDatabase,b as del,w as error,O as getExtension,k as merge,g as omitNullUndefined,_ as shortHash,y as success};
1
+ import{createEventBus as e}from"@asaidimu/utils-events";import{v7 as t,v4 as s}from"uuid";var r=Object.defineProperty,n=[async e=>{const t=await e.workspaceStore.list();if(0===t.length)return{};const s=t[0];return{id:s.id,settings:s.settings,project:s.project}},async e=>{const t=await e.roles.list(),s={};for(const r of t)s[r.name]=e.roles.summarize(r);return{index:{roles:s}}},async e=>{const t=await e.preferences.list(),s={};for(const r of t)s[r.id]=e.preferences.summarize(r);return{index:{preferences:s}}},async e=>{const t=await e.context.list(),s={};for(const r of t)s[r.key]=e.context.summarize(r);return{index:{context:s}}},async e=>{const t=await e.sessions.list(),s={};for(const r of t)s[r.id]=e.sessions.summarize(r);return{index:{sessions:s}}},async e=>{const t=await e.topics.list(),s={};for(const r of t)s[r.name]=e.topics.summarize(r);return{index:{topics:s}}},async e=>({index:{blobs:await e.blobs.getAllRecords()}})],i=[{name:"workspace",version:"1.0.0",description:"Workspace root containing global settings and project metadata.",fields:{id:{name:"id",type:"string",required:!0},settings:{name:"settings",type:"record",required:!0},project:{name:"project",type:"record",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"}],constraints:[],migrations:[]},{name:"role",version:"1.0.0",description:"AI persona with a system prompt and associated preference defaults.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!0},description:{name:"description",type:"string",required:!1},persona:{name:"persona",type:"string",required:!0},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},topics:{name:"topics",type:"array",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_name",fields:["name"],type:"unique"},{name:"by_label",fields:["label"],type:"normal"}],constraints:[],migrations:[]},{name:"preference",version:"1.0.0",description:"A user behavioural instruction, scoped to zero or more topics.",fields:{id:{name:"id",type:"string",required:!0},content:{name:"content",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},timestamp:{name:"timestamp",type:"string",required:!0}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"context",version:"1.0.0",description:"Injected background knowledge, scoped to topics. Content is a discriminated union.",fields:{key:{name:"key",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},content:{name:"content",type:"record",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},metadata:{name:"metadata",type:"record",required:!1}},indexes:[{name:"by_key",fields:["key"],type:"unique"},{name:"by_topics",fields:["topics"],type:"normal"},{name:"by_timestamp",fields:["timestamp"],type:"btree"}],constraints:[],migrations:[]},{name:"session",version:"1.0.0",description:"Session metadata. The head field tracks the current tip of the turn DAG.",fields:{id:{name:"id",type:"string",required:!0},label:{name:"label",type:"string",required:!0},role:{name:"role",type:"string",required:!0},topics:{name:"topics",type:"array",required:!1,itemsType:"string"},preferences:{name:"preferences",type:"array",required:!1,itemsType:"string"},metadata:{name:"metadata",type:"record",required:!1},head:{name:"head",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_id",fields:["id"],type:"unique"},{name:"by_role",fields:["role"],type:"normal"}],constraints:[],migrations:[]},{name:"turn",version:"1.0.0",description:["A single message in a session transcript, stored as a flat document.","The DAG is reconstructed in memory by TurnTree.buildNodeGraph() at session open time."].join(" "),fields:{id:{name:"id",type:"string",required:!0},session:{name:"session",type:"string",required:!0},version:{name:"version",type:"number",required:!0},actor:{name:"actor",type:"enum",required:!0,values:["user","assistant","tool"]},blocks:{name:"blocks",type:"array",required:!0},timestamp:{name:"timestamp",type:"string",required:!0},role:{name:"role",type:"string",required:!1},parent:{name:"parent",type:"record",required:!1},constraints:{name:"constraints",type:"record",required:!1}},indexes:[{name:"by_session",fields:["session"],type:"normal"},{name:"by_session_parent",fields:["session","parent"],type:"composite"},{name:"by_session_id_ver",fields:["session","id","version"],type:"composite",unique:!0}],constraints:[],migrations:[]},{name:"topic",version:"1.0.0",description:"A categorization for context and preferences, used for retrieval.",fields:{name:{name:"name",type:"string",required:!0},label:{name:"label",type:"string",required:!1},description:{name:"description",type:"string",required:!1},metadata:{name:"metadata",type:"record",required:!1},created:{name:"created",type:"string",required:!0},updated:{name:"updated",type:"string",required:!0}},indexes:[{name:"by_name",fields:["name"],type:"unique"}],constraints:[],migrations:[]}],o=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},a=class extends o{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},c=class extends o{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},d=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let r;await Promise.race([s.then((()=>clearTimeout(r))),new Promise(((s,n)=>{r=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new a("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},l=class{mutex=new d({yieldMode:"microtask"});promise=null;_value=null;_error;_done=!1;retry;throws;constructor({retry:e,throws:t}={}){this.retry=Boolean(e),this.throws=Boolean(t)}async do(e,t){return this._done?this.peek():this.promise?this._awaitWithTimeout(this.promise,t,"Once do() timed out"):(await this.mutex.lock(),this.promise?(this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")):(this.promise=(async()=>{try{this._value=await e(),this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.retry&&!this._done&&(this.promise=null)}return this.peek()})(),this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")))}doSync(e){if(this._done){if(this.throws&&this._error)throw this._error;return this.peek()}if(this.promise){const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}if(!this.mutex.tryLock()){const e=new Error("Cannot execute doSync: lock is currently held.");if(this.throws)throw e;return{value:null,error:e}}if(this.promise||this._done){if(this.mutex.unlock(),this._done){if(this.throws&&this._error)throw this._error;return this.peek()}const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}try{const t=e();this._value=t,this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.mutex.unlock()}return this.peek()}running(){return null!==this.promise&&!this.done()}peek(){return{value:this._value,error:this._error}}get(){if(!this._done)throw new Error("Once operation is not yet complete");if(this._error)throw this._error;return this._value}reset(){if(this.running())throw new Error("Cannot reset Once while an operation is in progress.");this._done=!1,this.promise=null,this._value=null,this._error=void 0}done(){return this._done}current(){return this.promise}_awaitWithTimeout(e,t,s="Operation timed out"){if(null==t)return e;let r;return Promise.race([e.then((e=>(clearTimeout(r),e))),new Promise(((e,n)=>{r=setTimeout((()=>n(new a(s))),t)}))])}},u=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new d({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new c};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,r=null;try{if(this._done)throw new c;r=await e(),this._lastValue=r,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:r,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}};function p(e){const t=new l({retry:!0,throws:!0}),s=new l({retry:!0,throws:!0});return{open:async(s=[])=>(await t.do((async()=>{const t=[...i,...s];await e.setupCollections(t)}))).value,collection:async t=>e.collection(t),close:async()=>(await s.do((()=>{e.close()}))).value,database:()=>e}}var h={WORKSPACE:"workspace",ROLE:"role",PREFERENCE:"preference",CONTEXT:"context",SESSION:"session",TURN:"turn",BLOB:"blob",TOPIC:"topic"},m=Symbol.for("delete"),f=e=>Array.isArray(e)?[...e]:{...e};function y(e){return{ok:!0,value:e}}function w(e){return{ok:!1,error:e}}function g(e){return Object.fromEntries(Object.entries(e).filter((([e,t])=>null!=t)))}function b(){return m}var k=function(e){const t=e?.deleteMarker||m;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const r={};for(const[n,i]of Object.entries(e))if(i!==t)if("object"==typeof i&&null!==i){const e=s(i);void 0!==e&&(r[n]=e)}else r[n]=i;return r}return e===t?void 0:e}return function(e,r){if("object"!=typeof e||null===e)return"object"==typeof r&&null!==r?s(r):r===t?{}:r;if("object"!=typeof r||null===r)return e;const n=f(e),i=[{target:n,source:r}];for(;i.length>0;){const{target:e,source:s}=i.pop();for(const r of Object.keys(s)){const n=s[r];if(n!==t)if(Array.isArray(n))e[r]=n;else if("object"==typeof n&&null!==n){const t=r in e&&"object"==typeof e[r]&&null!==e[r]?e[r]:{};e[r]=f(t),i.push({target:e[r],source:n})}else e[r]=n;else delete e[r]}}return n}}({deleteMarker:m});async function x(e){const t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t)).map((e=>e.toString(16).padStart(2,"0"))).join("")}function v(e){if("undefined"!=typeof Buffer)return Buffer.from(e).toString("base64");const t=[];for(let s=0;s<e.length;s+=32768){const r=e.subarray(s,s+32768);t.push(String.fromCharCode.apply(null,Array.from(r)))}return btoa(t.join(""))}function _(e,t=4){let s=0;for(let t=0;t<e.length;t++)s=(s<<5)-s+e.charCodeAt(t),s|=0;return Math.abs(s).toString(36).padEnd(t,"0").slice(0,t)}function O(e,t){return e.extensions[t]}var S=class{registry=new Map;middlewares=[];serializer=new u;bus;_getWorkspace;updateWorkspace;guard;_ctx;constructor(e){this._ctx=e.ctx,this._getWorkspace=e.getWorkspace,this.updateWorkspace=e.updateWorkspace,this.guard=e.guard,this.bus=e.bus}register(e,t){return this.registry.set(e,t),this}use(e){return this.middlewares.push(e),this}workspace(){return this._getWorkspace()}async dispatch(e){const t=await this.serializer.do((async()=>{if(this.guard){const t=await this.guard.authenticate({type:"command",payload:e});if(!t.ok)return w(t.error)}const t=this.registry.get(e.type);if(!t)return w({code:"INVALID_COMMAND",reason:`No reducer registered for command type: ${e.type}`});const s=this._getWorkspace(),r=await t({workspace:s,...this._ctx},e.payload);if(!r.ok)return r;let n=r.value;for(const t of this.middlewares){const r=await t({workspace:s,command:e,patch:n,...this._ctx});n=k(n,r)}return await this.updateWorkspace(n),this.bus.emit({name:"workspace:changed",payload:n}),"workspace:sync"===e.type&&this.bus.emit({name:"workspace:synced",payload:void 0}),y(n)}));if(t.error)throw t.error;return t.value}subscribe(e,t){return this.bus.subscribe(e,t)}ctx(){return{workspace:this._getWorkspace(),...this._ctx}}},T=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async getMany(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?t.push(e):s.push(r)}if(s.length>0){const e=await this.collection.filter({operator:"or",conditions:s.map((e=>({field:"name",operator:"eq",value:e})))});for(const s of e){const e=s.state();this.cache.set(e.name,e),t.push(e)}}return t}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}referencedBy(e,t){for(const s in t.roles)if(t.roles[s].topics?.includes(e))return!0;for(const s in t.sessions)if(t.sessions[s].topics?.includes(e))return!0;for(const s in t.context)if(t.context[s].topics?.includes(e))return!0;for(const s in t.preferences)if(t.preferences[s].topics?.includes(e))return!0;return!1}summarize(e){return{topic:e.name,contextKeys:[],preferences:[],metadata:{created:e.created,updated:e.updated,entries:0}}}};function N(e,t,s,r,n,i){const o={};for(const a of t){let t=e[a];if(!t){if("add"!==n||!i)continue;t={topic:a,contextKeys:[],preferences:[],metadata:{created:(new Date).toISOString(),updated:(new Date).toISOString(),entries:0}},i.add({name:a,created:t.metadata.created,updated:t.metadata.updated})}const c=t[r],d=c.includes(s);if("add"!==n||d){if("remove"===n&&d){const e=Math.max(0,(t.metadata.entries||0)-1);o[a]={...t,[r]:c.filter((e=>e!==s)),metadata:{...t.metadata,updated:(new Date).toISOString(),entries:e}}}}else o[a]={...t,[r]:[...c,s],metadata:{...t.metadata,updated:(new Date).toISOString(),entries:(t.metadata.entries||0)+1}}}return{index:{topics:o}}}function R(e,t,s,r){return N(e,t,s,"contextKeys","add",r)}function D(e,t,s){return N(e,t,s,"contextKeys","remove")}function I(e,t,s,r){return N(e,t,s,"preferences","add",r)}function E(e,t,s){return N(e,t,s,"preferences","remove")}var j=async e=>{if(!e.patch)return;const{workspace:t,patch:s}=e,r=t.index.topics;let n={};const i=e=>{const t=e?.index?.topics;t&&(n=k(n,t))};if(s.index?.context)for(const[n,o]of Object.entries(s.index.context)){const s=t.index.context[n];if(void 0===o&&s)i(D(r,s.topics,s.key));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(D(r,c,s.key)),a.length&&i(R(r,a,s.key,e.topics))}}else i(R(r,o.topics??[],o.key,e.topics))}if(s.index?.preferences)for(const[n,o]of Object.entries(s.index.preferences)){const s=t.index.preferences[n];if(void 0===o&&s)i(E(r,s.topics,s.id));else if(s||!o){if(s&&o){const t=s.topics??[],n=o.topics??t,a=n.filter((e=>!t.includes(e))),c=t.filter((e=>!n.includes(e)));c.length&&i(E(r,c,s.id)),a.length&&i(I(r,a,s.id,e.topics))}}else i(I(r,o.topics??[],o.id,e.topics))}return Object.keys(n).length?{index:{topics:n}}:{}},A="\n# WORKSPACE OPERATING SYSTEM\nYou are operating within a structured workspace. Your output MUST be a JSON object matching the provided schema.\nYour entire response MUST be valid JSON. Nothing else.\n".trim();function B(e){if(!e||"append"===e)return{placement:"append",label:null};if("prepend"===e)return{placement:"prepend",label:null};const[t,...s]=e.split(":");return("before"===t||"after"===t)&&s.length>0?{placement:t,label:s.join(":")}:(console.warn(`[SystemPromptAssembler] Unrecognised position "${e}", defaulting to "append".`),{placement:"append",label:null})}var q=class{build(e,t=[]){const s=[...e.system.extensions||[],...t],r=[];var n,i,o;r.push({label:"operating-system",content:A}),e.role.persona&&r.push({label:"persona",content:(n=e.role.persona,`# Persona\n${n}`)}),e.system.preferences.length>0&&r.push({label:"preferences",content:(i=e.system.preferences,`# User Preferences\n${i.map((e=>`- ${e.content}`)).join("\n")}`),metadata:{count:e.system.preferences.length}}),e.system.context.length>0&&r.push({label:"context",content:(o=e.system.context,`# Context\n${o.map((e=>{switch(e.content.kind){case"text":return`[${e.key}] ${e.content.value}`;case"json":return`[${e.key}] JSON: ${JSON.stringify(e.content.value)}`;case"blob":return`[${e.key}] Blob: ${e.content.filename??"unnamed"} (${e.content.mediaType}, ${e.content.sizeBytes} bytes)`;case"remote":return`[${e.key}] Remote: ${e.content.uri}`;default:return`[${e.key}] Unsupported context type`}})).join("\n")}`),metadata:{count:e.system.context.length}}),e.system.instructions&&r.push({label:"instructions",content:`# Instructions\n${e.system.instructions}`});const a=[...r],c=[];for(const e of s){const{position:t,...s}=e,r=B(t);if("prepend"===r.placement){c.push(s);continue}if("append"===r.placement){a.push(s);continue}const n=a.findIndex((e=>e.label===r.label));if(-1===n){console.warn(`[SystemPromptAssembler] Anchor target "${r.label}" not found in sections. Extension "${e.label}" will be appended.`),a.push(s);continue}const i="before"===r.placement?n:n+1;a.splice(i,0,s)}return[...c,...a]}join(e){return e.map((e=>e.content)).join("\n\n---\n\n")}};var C={"workspace:create":async function(e,t){const{workspaceStore:s}=e,{id:r,settings:n,project:i}=t,o=await s.get(r);return o?y({id:o.id,settings:o.settings,project:o.project}):(await s.add({id:r,settings:n,project:i}),y({id:r,settings:n,project:i}))},"workspace:sync":async function(e,t){let s={};for(const t of e.indexers){const r=await t(e);s=k(s,r)}return t&&(t.id&&(s.id=t.id),t.settings&&(s.settings=k(s.settings||{},t.settings)),t.project&&(s.project=k(s.project||{},t.project))),y(s)},"role:add":async function(e,t){const{workspace:s,roles:r}=e,n=t;return s.index.roles[n.name]?w({code:"DUPLICATE_KEY",resource:"Role",key:n.name}):(await r.add(n),y({index:{roles:{[n.name]:r.summarize(n)}}}))},"role:update":async function(e,t){const{workspace:s,roles:r}=e,{name:n,...i}=t;if(!s.index.roles[n])return w({code:"NOT_FOUND",resource:"Role",id:n});const o=await r.update(n,i);return o?y({index:{roles:{[n]:r.summarize(o)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update role ${n} in store.`})},"role:delete":async function(e,t){const{workspace:s,roles:r}=e,{name:n}=t;return s.index.roles[n]?await r.delete(n)?y({index:{roles:{[n]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete role ${n} from store.`}):w({code:"NOT_FOUND",resource:"Role",id:n})},"preference:add":async function(e,t){const{workspace:s,preferences:r}=e,n=t;return s.index.preferences[n.id]?w({code:"DUPLICATE_KEY",resource:"Preference",key:n.id}):(await r.add(n),y({index:{preferences:{[n.id]:r.summarize(n)}}}))},"preference:update":async function(e,t){const{workspace:s,preferences:r}=e,{id:n,...i}=t;if(!s.index.preferences[n])return w({code:"NOT_FOUND",resource:"Preference",id:n});const o=await r.update(n,i);return o?y({index:{preferences:{[n]:r.summarize(o)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update preference ${n} in store.`})},"preference:delete":async function(e,t){const{workspace:s,preferences:r}=e,{id:n}=t;return s.index.preferences[n]?await r.delete(n)?y({index:{preferences:{[n]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete preference ${n} from store.`}):w({code:"NOT_FOUND",resource:"Preference",id:n})},"context:add":async function(e,t){const{workspace:s,context:r}=e,n=t;return s.index.context[n.key]?w({code:"DUPLICATE_KEY",resource:"Context",key:n.key}):(await r.add(n),y({index:{context:{[n.key]:r.summarize(n)}}}))},"context:update":async function(e,t){const{workspace:s,context:r}=e,{key:n,...i}=t,o=s.index.context[n];if(!o)return w({code:"NOT_FOUND",resource:"Context",id:n});const a=await r.update(n,i,o.kind);return a?y({index:{context:{[n]:r.summarize(a)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update context ${n} in store.`})},"context:delete":async function(e,t){const{workspace:s,context:r}=e,{key:n}=t,i=s.index.context[n];return i?await r.delete(n,i.kind)?y({index:{context:{[n]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete context ${n} from store.`}):w({code:"NOT_FOUND",resource:"Context",id:n})},"topic:add":async function(e,t){const{workspace:s,topics:r}=e,n=t;return s.index.topics[n.name]?w({code:"DUPLICATE_KEY",resource:"Topic",key:n.name}):(await r.add(n),y({index:{topics:{[n.name]:r.summarize(n)}}}))},"topic:update":async function(e,t){const{workspace:s,topics:r}=e,{name:n,...i}=t;if(!s.index.topics[n])return w({code:"NOT_FOUND",resource:"Topic",id:n});const o=await r.update(n,i);return o?y({index:{topics:{[n]:{metadata:{updated:o.updated}}}}}):w({code:"BACKEND_ERROR",reason:`Failed to update topic ${n} in store.`})},"topic:delete":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{name:o,cascade:a}=t;if(!s.index.topics[o])return w({code:"NOT_FOUND",resource:"Topic",id:o});if(r.referencedBy(o,s.index)&&!a)return w({code:"INVALID_COMMAND",reason:`Topic '${o}' is referenced by other entities. Use 'cascade: true' to force deletion.`});if(a){for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);t&&await n.update(e,{topics:t.topics.filter((e=>e!==o))})}}for(const e in s.index.sessions){const t=s.index.sessions[e];t.topics?.includes(o)&&await i.update(e,{topics:t.topics.filter((e=>e!==o))})}}return await r.delete(o)?y({index:{topics:{[o]:m}}}):w({code:"BACKEND_ERROR",reason:`Failed to delete topic ${o} from store.`})},"topic:merge":async function(e,t){const{workspace:s,topics:r,roles:n,sessions:i}=e,{source:o,target:a}=t;if(!s.index.topics[o]||!s.index.topics[a])return w({code:"NOT_FOUND",resource:"Topic",id:s.index.topics[o]?a:o});for(const e in s.index.roles){const t=s.index.roles[e];if(t.topics?.includes(o)){const t=await n.get(e);if(t){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await n.update(e,{topics:s})}}}for(const e in s.index.sessions){const t=s.index.sessions[e];if(t.topics?.includes(o)){const s=t.topics.filter((e=>e!==o));s.includes(a)||s.push(a),await i.update(e,{topics:s})}}return await r.delete(o),y({index:{topics:{[o]:m}}})},"session:create":async function(e,t){const{workspace:s,sessions:r}=e,n=t;return s.index.sessions[n.id]?w({code:"DUPLICATE_KEY",resource:"Session",key:n.id}):(await r.add(n),y({index:{sessions:{[n.id]:r.summarize(n)}}}))},"session:update":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,...i}=t;if(!s.index.sessions[n])return w({code:"NOT_FOUND",resource:"Session",id:n});const o=await r.update(n,i);return o?y({index:{sessions:{[n]:r.summarize(o)}}}):w({code:"BACKEND_ERROR",reason:`Failed to update session ${n} in store.`})},"session:fork":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,newSessionId:i,label:o}=t,a=s.index.sessions[n];if(!a)return w({code:"NOT_FOUND",resource:"Session",id:n});const c={...a,id:i,label:o||`Fork of ${a.label}`,role:t.role?t.role:a.role,topics:t.topics?t.topics:a.topics};return await r.add(c),y({index:{sessions:{[i]:r.summarize(c)}}})},"session:delete":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n}=t;return s.index.sessions[n]?(await r.delete(n),y({index:{sessions:{[n]:m}}})):w({code:"NOT_FOUND",resource:"Session",id:n})},"session:role:switch":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,roleName:i}=t;return s.index.sessions[n]?(await r.update(n,{role:i}),y({index:{sessions:{[n]:{role:i}}}})):w({code:"NOT_FOUND",resource:"Session",id:n})},"session:topics:add":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,topics:i}=t,o=s.index.sessions[n];if(!o)return w({code:"NOT_FOUND",resource:"Session",id:n});const a=Array.from(new Set([...o.topics,...i]));return await r.update(n,{topics:a}),y({index:{sessions:{[n]:{topics:a}}}})},"session:preferences:override":async function(e,t){const{workspace:s,sessions:r}=e,{sessionId:n,preferences:i}=t;return s.index.sessions[n]?(await r.update(n,{preferences:i}),y({index:{sessions:{[n]:{preferences:i}}}})):w({code:"NOT_FOUND",resource:"Session",id:n})},"turn:add":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return w({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})},"turn:update":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return w({code:"NOT_FOUND",resource:"Session",id:i});await n.update(o,o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})},"turn:edit":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newBlocks:a,newVersion:c,roleSnapshot:d}=t,l=s.index.sessions[i];if(!l)return w({code:"NOT_FOUND",resource:"Session",id:i});const u=l.head;let p={};if(u&&u.id===o){const e=await n.get({id:u.id,version:u.version,session:i});e&&(p={...e})}const h={...p,id:o,version:c,blocks:a,role:d??p.role,timestamp:(new Date).toISOString(),parent:p.parent,actor:p.actor||"assistant",session:i};if(await n.add(h),l.head?.id===o){const e={id:o,version:c};return await r.update(i,{head:e}),y({index:{sessions:{[i]:{head:e}}}})}return y({})},"turn:branch":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turn:o}=t;if(!s.index.sessions[i])return w({code:"NOT_FOUND",resource:"Session",id:i});await n.add(o);const a={id:o.id,version:o.version};return await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})},"turn:delete":async function(e,t){const{workspace:s,sessions:r,turns:n}=e,{sessionId:i,turnId:o,newHead:a}=t;return s.index.sessions[i]?(await n.delete({session:i,id:o,version:t.version}),a?(await r.update(i,{head:a}),y({index:{sessions:{[i]:{head:a}}}})):y({})):w({code:"NOT_FOUND",resource:"Session",id:i})},"blob:register":async function(e,t){const{blobs:s}=e,r=await s.register(t.data,t.mediaType,t.filename);if(!r.ok)return r;const n=await s.getRecord(r.value.sha256);return n?y({index:{blobs:{[n.sha256]:n}}}):w({code:"BACKEND_ERROR",reason:`Failed to retrieve registered blob record for ${r.value.sha256}`})},"blob:retain":async function(e,t){const{blobs:s}=e,r=await s.retain(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return y({index:{blobs:{[t.sha256]:n}}})},"blob:release":async function(e,t){const{blobs:s}=e,r=await s.release(t.sha256);if(!r.ok)return r;const n=await s.getRecord(t.sha256);return y({index:{blobs:{[t.sha256]:n||m}}})},"blob:purge":async function(e,t){const{blobs:s}=e,r=await s.purge(t.sha256);return r.ok?y({index:{blobs:{[t.sha256]:m}}}):r},"blob:record_remote_id":async function(e,t){const{blobs:s}=e,{sha256:r,providerId:n,fileId:i,timestamp:o}=t,a=await s.recordRemoteId(r,n,i,o);if(!a.ok)return a;const c=await s.getRecord(r);return y({index:{blobs:{[r]:c}}})},"tool:call":async function(e,t){return y({})}},U=class{profiles;constructor(e=[]){this.profiles=new Map;for(const t of e)this.profiles.set(t.name,t)}get(e){return this.profiles.get(e)}list(e){const t=Array.from(this.profiles.values());return e?t.filter((t=>t.provider===e)):t}register(e){this.profiles.set(e.name,e)}},$=[{type:"text",emittable:!0,description:["## Block: `text`","Primary communication block. Use for all conversational responses, explanations, and prose.","A response may contain multiple `text` blocks interleaved with other block types."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["text"]},text:{type:"string",description:"The raw markdown or plain text content of the response."}},required:["type","text"]}},{type:"summary",emittable:!0,description:["## Block: `summary`","Context compression block. Replaces older conversation history with a concise digest."].join("\n"),rules:["Only emit when explicitly instructed by the system to summarise.","Never emit more than one summary block per turn.","Preserve key decisions, outcomes, and unresolved questions.","Do not summarise the current turn — only prior history."],schema:{type:"object",properties:{type:{type:"string",enum:["summary"]},text:{type:"string",description:"A concise summary replacing older conversation context."}},required:["type","text"]}},{type:"thinking",emittable:!1,description:["## Block: `thinking`","Internal chain-of-thought produced by the model before its final response.","Injected by the workspace from provider reasoning tokens — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["thinking"]},thinking:{type:"string",description:"The internal reasoning text produced before the final response."}},required:["type","thinking"]}},{type:"tool:use",emittable:!1,description:["## Block: `tool:use`","Captures a model request to execute a registered tool.","Constructed by the workspace from the provider native function-call response — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:use"]},name:{type:"string",description:"The exact registered name of the tool to execute."},input:{type:"object",description:"The parameter arguments for the tool execution.",additionalProperties:!0}},required:["type","name","input"]}},{type:"tool:result",emittable:!1,description:["## Block: `tool:result`","Contains the output of a tool execution, including any errors.","Injected by the workspace after a tool runs — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["tool:result"]},useId:{type:"string",description:"The UUID of the ToolUseBlock that triggered this execution."},content:{oneOf:[{type:"string"},{type:"object",additionalProperties:!0}],description:"The serialized output of the tool, or a structured JSON response."},isError:{type:"boolean",description:"True if the tool execution resulted in an error."}},required:["type","useId","content"]}},{type:"image",emittable:!1,description:["## Block: `image`","An image asset attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["image"]},altText:{type:"string",description:"Accessible description of the image content."}},required:["type"]}},{type:"document",emittable:!1,description:["## Block: `document`","A document asset (PDF, DOCX, etc.) attached to a turn, resolved from the workspace blob store.","Constructed by the workspace from user uploads — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["document"]},title:{type:"string",description:"Human-readable title or filename of the document."}},required:["type"]}},{type:"role:transition",emittable:!1,description:["## Block: `role:transition`","Marks that the active session persona has switched from one role to another.","Emitted by the workspace when the active persona changes — never emit it yourself."].join("\n"),rules:[],schema:{type:"object",properties:{type:{type:"string",enum:["role:transition"]},previousRole:{type:"string",description:"The name of the role the session is leaving."},newRole:{type:"string",description:"The name of the role the session is adopting."}},required:["type","newRole"]}}];function z(e,t){let s=e.filter((e=>e.emittable));if(!t)return s;if(t.only&&t.only.length>0){const e=new Set(t.only);return s.filter((t=>e.has(t.type)))}if(t.exclude&&t.exclude.length>0){const e=new Set(t.exclude);return s.filter((t=>!e.has(t.type)))}return s}var M=class{store=new Map;constructor(){for(const e of $)this.store.set(e.type,e)}register(e){if(this.store.has(e.type))throw new Error(`[BlockRegistry] Block type "${e.type}" is already registered. Use update() to modify it.`);this.store.set(e.type,{...e,emittable:!0,rules:[]})}unregister(e){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot unregister unknown block type "${e}".`);this.store.delete(e)}update(e,t){const s=this.store.get(e);s?this.store.set(e,{...s,...t,type:e}):this.store.set(e,{emittable:!0,rules:[],...t,type:e})}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}isType(e,t){return e.type===t}guard(e){return t=>t.type===e}schema(e){return{type:"object",description:"The model's structured response, containing an ordered sequence of content blocks.",properties:{blocks:{type:"array",description:"An ordered array of content blocks comprising the model's response.",items:{anyOf:z(Array.from(this.store.values()),e).map((e=>e.schema))}}},required:["blocks"]}}description(e){return["# BLOCK TYPES & USAGE RULES","","Your response MUST be a JSON object with a `blocks` array.","Each element of `blocks` must be one of the following types:","",z(Array.from(this.store.values()),e).map((e=>{const t=[e.description];return e.rules.length>0&&(t.push("**Rules:**"),t.push(...e.rules.map((e=>`- ${e}`)))),t.join("\n")})).join("\n\n"),"","## GLOBAL CONSTRAINTS","","- Output must be **valid JSON only** — no prose before or after the JSON object.","- Responses may contain multiple blocks. Blocks are rendered in order.","- **Do NOT generate IDs.** The workspace is solely responsible for block ID assignment.","- Do not infer or invent fields not defined in the block schemas above."].join("\n")}rules(e){const t=z(Array.from(this.store.values()),e);return Object.fromEntries(t.map((e=>[e.type,[...e.rules]])))}setRules(e,t){const s=this.store.get(e);if(!s)throw new Error(`[BlockRegistry] Cannot set rules on unknown block type "${e}".`);this.store.set(e,{...s,rules:t})}parse(e,s){const r=this.store.get(e.type);if(!r)return console.warn(`[BlockRegistry] parse() received unknown block type "${e.type}". Returning null.`),null;if(s&&r.mappings?.[s])return r.mappings[s].from(e);const{id:n,...i}=e;return{id:t(),...i}}create(e,s){if(!this.store.has(e))throw new Error(`[BlockRegistry] Cannot create block of unknown type "${e}".`);return{...s,id:t(),type:e}}clone(e,s){return{...structuredClone(e),...s,id:t()}}},F=class{store=new Map;constructor(){this.registerDefaults()}registerDefaults(){this.register({kind:"text",target:"system",render:e=>({id:e.key,type:"text",text:e.content.value}),toString:e=>e.content.value,summarize:e=>({key:e.key,kind:"text",topics:e.topics,timestamp:e.timestamp,preview:e.content.value.slice(0,100)})}),this.register({kind:"json",target:"system",render:e=>({id:e.key,type:"text",text:JSON.stringify(e.content.value,null,2)}),toString:e=>JSON.stringify(e.content.value),summarize:e=>({key:e.key,kind:"json",topics:e.topics,timestamp:e.timestamp,preview:"JSON Data"})}),this.register({kind:"blob",target:"transcript",render:e=>{const{sha256:t,mediaType:s,sizeBytes:r,filename:n}=e.content,i={sha256:t,mediaType:s,sizeBytes:r,filename:n};return s.startsWith("image/")?{id:e.key,type:"image",ref:i}:{id:e.key,type:"document",ref:i,title:n}},toString:e=>e.content.filename||"unnamed blob",summarize:e=>({key:e.key,kind:"blob",topics:e.topics,timestamp:e.timestamp,mime:e.content.mediaType,size:e.content.sizeBytes,preview:e.content.filename})})}register(e){if(this.store.has(e.kind))throw new Error(`[ContextRegistry] Context kind "${e.kind}" is already registered.`);this.store.set(e.kind,e)}get(e){return this.store.get(e)}list(){return Array.from(this.store.values())}},P="__system__",K=class{constructor(e,s,r){this._actor=e,this._turn=r?JSON.parse(JSON.stringify(r)):{id:t(),session:s,version:0,actor:this._actor,blocks:[],timestamp:(new Date).toISOString(),role:void 0}}_turn;addText(e){const s={id:t(),type:"text",text:e};return this._turn.blocks.push(s),this}addImage(e,s){const r={id:t(),type:"image",ref:e,altText:s};return this._turn.blocks.push(r),this}addDocument(e,s){const r={id:t(),type:"document",ref:e,title:s};return this._turn.blocks.push(r),this}addToolUse(e,s){const r={id:t(),type:"tool:use",name:e,input:s};return this._turn.blocks.push(r),this}addToolResult(e,s,r){const n={id:t(),type:"tool:result",useId:e,content:s,isError:r};return this._turn.blocks.push(n),this}addSummary(e){const s={id:t(),type:"summary",text:e};return this._turn.blocks.push(s),this}addRoleTransition(e,s){const r={id:t(),type:"role:transition",previousRole:e,newRole:s};return this._turn.blocks.push(r),this}addThinking(e){const s={id:t(),type:"thinking",thinking:e};return this._turn.blocks.push(s),this}addBlock(e){return this._turn.blocks.push(e),this}deleteBlock(e){return this._turn.blocks=this._turn.blocks.filter((t=>t.id!==e)),this}editTextBlock(e,t){const s=this._turn.blocks.findIndex((t=>t.id===e));if(-1===s)throw new Error(`Block with ID ${e} not found.`);const r=this._turn.blocks[s];if("text"!==r.type)throw new Error(`Block with ID ${e} is not a TextBlock.`);return this._turn.blocks[s]={...r,text:t},this}withId(e){return this._turn.id=e,this}withVersion(e){return this._turn.version=e,this}withTimestamp(e){return this._turn.timestamp=e,this}withParent(e){return this._turn.parent=e,this}withRoleSnapshot(e){return this._turn.role=e,this}build(){return JSON.parse(JSON.stringify(this._turn))}},V=class{constructor(e,t){this.turnStore=e,this.sessionStore=t}async loadAllTurns(e){return this.turnStore.listBySession(e)}async loadHead(e){const t=await this.sessionStore.get(e);return t?.head??null}},L=class e{constructor(e,t){this.nodes=e,this._head=t}static async build(t,s){const[r,n]=await Promise.all([s.loadAllTurns(t),s.loadHead(t)]),i=function(e,t){const s=function(e,t){if(!t)return new Set;const s=new Map;for(const t of e)s.set(`${t.id}:${t.version}`,t);const r=new Set;let n=t;for(;n;){const e=`${n.id}:${n.version}`;if(r.has(e))break;r.add(e);const t=s.get(e);if(!t)break;n=t.parent}return r}(e,t),r={},n={};for(const t of e){r[t.id]||(r[t.id]={id:t.id,versions:{},activeVersion:t.version,actor:t.actor,blocks:t.blocks,timestamp:t.timestamp,roleSnapshot:t.role,parent:t.parent,children:{}},n[t.id]=new Set);const e=r[t.id];e.versions[t.version]=t;const i=s.has(`${t.id}:${t.version}`),o=s.has(`${t.id}:${e.activeVersion}`);if(i&&(!o||t.version>=e.activeVersion)&&(e.activeVersion=t.version,e.blocks=t.blocks,e.timestamp=t.timestamp,e.roleSnapshot=t.role,e.parent=t.parent),t.parent){const e=`${t.parent.id}:${t.parent.version}`;if(n[t.parent.id]||(n[t.parent.id]=new Set),!n[t.parent.id].has(e+":"+t.id)){n[t.parent.id].add(e+":"+t.id),r[t.parent.id]||(r[t.parent.id]={id:t.parent.id,versions:{},activeVersion:t.parent.version,actor:"user",blocks:[],timestamp:"",roleSnapshot:void 0,children:{}});const s=r[t.parent.id];s.children[t.parent.version]||(s.children[t.parent.version]=[]),s.children[t.parent.version].push(t.id)}}}return r}(r,n);return new e(i,n)}head(){return this._head}chain(){if(!this._head)return[];const e=[];let t=this._head.id;for(;t;){const s=this.nodes[t];if(!s)break;e.push(s),t=s.parent?.id??null}return e.reverse()}getTurnSiblings(e){const t=this.nodes[e];if(!t)return[];if(!t.parent)return Object.values(this.nodes).filter((e=>!e.parent));const s=this.nodes[t.parent.id];if(!s)return[t];const r=s.children[t.parent.version];return r?r.map((e=>this.nodes[e])).filter((e=>!!e)):[t]}branchInfo(e){const t=this.nodes[e];if(!t)return{versions:[],currentIndex:-1,total:0,hasPrev:!1,hasNext:!1};const s=Object.keys(t.versions).map(Number).sort(((e,t)=>e-t)),r=s.indexOf(t.activeVersion);return{versions:s,currentIndex:r,total:s.length,hasPrev:r>0,hasNext:r<s.length-1}}graph(){return{...this.nodes}}};var W=class{constructor(e){this.manager=e}get workspace(){return this.manager.workspace()}roles(){return this.manager.ctx().roles.list()}role(e){return this.manager.ctx().roles.get(e)}async createRole(e,t,s,r,n=[]){if(this.workspace.index.roles[e])return w({code:"DUPLICATE_KEY",resource:"role",key:e});const i={name:e,label:t,description:r,persona:s,preferences:n,topics:[]};return this.manager.dispatch({type:"role:add",payload:i,timestamp:(new Date).toISOString()})}async updateRole(e,t){return this.workspace.index.roles[e]?this.manager.dispatch({type:"role:update",payload:{name:e,...t},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"role",id:e})}async deleteRole(e){const t=this.workspace;if(!t.index.roles[e])return w({code:"NOT_FOUND",resource:"role",id:e});return Object.values(t.index.sessions).some((t=>t.role===e))?w({code:"INVALID_COMMAND",reason:`Cannot delete role "${e}" — it is still referenced by one or more sessions`}):this.manager.dispatch({type:"role:delete",payload:{name:e},timestamp:(new Date).toISOString()})}async addPreference(e,t){const s={id:crypto.randomUUID(),content:e,topics:t,timestamp:(new Date).toISOString()};return this.manager.dispatch({type:"preference:add",payload:s,timestamp:s.timestamp})}preferences(){return this.manager.ctx().preferences.list()}async addContext(e,t,s,r){const n={key:e,topics:s,content:t,timestamp:(new Date).toISOString(),metadata:r};return this.manager.dispatch({type:"context:add",payload:n,timestamp:n.timestamp})}context(){return this.manager.ctx().context.list()}async registerBlob(e,t,s){const r=await this.manager.dispatch({type:"blob:register",payload:{data:e,mediaType:t,filename:s},timestamp:(new Date).toISOString()});if(!r.ok)return w(r.error);const n=r.value,i=n?.index?.blobs;if(!i)return w({code:"BACKEND_ERROR",reason:"Blob registration succeeded but no blobs patch returned"});const o=Object.keys(i)[0];if(!o)return w({code:"BACKEND_ERROR",reason:"No SHA256 in patch"});const a=i[o];if(!a)return w({code:"BACKEND_ERROR",reason:"Blob record missing"});return y({sha256:o,ref:{sha256:o,mediaType:a.mediaType,sizeBytes:a.sizeBytes,filename:a.filename,previewUrl:a.previewUrl}})}},J=class e{constructor(e,t,s,r){this._id=e,this.manager=t,this.processor=s,this.turnRepository=r,this.workspace=new W(t),this.unsubscribe=t.subscribe("workspace:synced",(()=>this.sync()))}workspace;_role=void 0;_preferences=[];tree;unsubscribe;static async create(t,s,r){s.ctx().workspace;const n=new V(s.ctx().turns,s.ctx().sessions),i=new e(t,s,r,n);return await i.sync(),i}async sync(){await this._setRole(),await this._setPreference(),await this.refreshTurnTree()}close(){this.unsubscribe?.()}async _setRole(){const e=this.meta(),t=await this.manager.ctx().roles.get(e.role);t&&(this._role=t)}async _setPreference(){const e=this.meta();this._preferences=await this.manager.ctx().preferences.load(e.preferences)}id(){return this._id}ws(){return this.manager.workspace()}meta(){return this.ws().index.sessions[this._id]}label(){return this.meta()?.label}role(){return this._role}topics(){return this.meta()?.topics??[]}preferences(){return this._preferences}head(){return this.meta()?.head??null}turns(){return this.tree.chain()}async siblings(e){return this.tree.getTurnSiblings(e)}async branchInfo(e){return this.tree.branchInfo(e)}async getTurn(e){return this.tree.graph()[e]}async rename(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,label:e},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async setTopics(e){return this.meta()?this.dispatch({type:"session:update",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async addTopics(e){return this.meta()?this.dispatch({type:"session:topics:add",payload:{sessionId:this._id,topics:e},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async setPreferences(e){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const t=await this.dispatch({type:"session:preferences:override",payload:{sessionId:this._id,preferences:e},timestamp:(new Date).toISOString()});return t.ok&&await this._setPreference(),t}async fork(e,t,s,r){return this.meta()?this.dispatch({type:"session:fork",payload:{sessionId:this._id,newSessionId:e,label:t,role:s,topics:r},timestamp:(new Date).toISOString()}):w({code:"NOT_FOUND",resource:"session",id:this._id})}async dispatch(e){return this.manager.dispatch(e)}async switchRole(e){const t=this.ws();if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});if(e!==P&&!t.index.roles[e])return w({code:"NOT_FOUND",resource:"role",id:e});const s=await this.dispatch({type:"session:update",payload:{sessionId:this._id,role:e},timestamp:(new Date).toISOString()});return s.ok&&await this._setRole(),s}async addTurn(e){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const t={...e,session:this._id,version:e.version??0,parent:!e.parent&&this.head()?this.head():void 0},s=await this.dispatch({type:"turn:add",payload:{sessionId:this._id,turn:t},timestamp:t.timestamp});return s.ok&&await this.refreshTurnTree(),s}async recordUserTurn(e){return this.meta()?this.addTurn(e):w({code:"NOT_FOUND",resource:"session",id:this._id})}async recordAssistantTurn(e,t){const s=this.ws();if(!s)return w({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!s.index.sessions[this._id])return w({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.processor.process(e,this._id);let n={};const i=[];for(const e of r){const t=await this.manager.dispatch(e);if(t.ok)n=k(n,t.value);else{if("PERMISSION_DENIED"!==t.error.code||e.synthetic)return t;{const t=this.describeCommand(e);i.push({cmd:e,description:t})}}}const o=await this.addTurn(e);if(!o.ok)return o;if(n=k(n,o.value),t&&i.length>0){const e=this.buildDenialTurn(i),t=await this.addTurn(e);t.ok&&(n=k(n,t.value))}return y(n)}buildDenialTurn(e){const t=new K("user",this._id);for(const s of e)t.addText(`[System] User denied: ${s.description}.`);return t.build()}describeCommand(e){return"tool:call"===e.type?`tool execution: ${e.payload.tool}`:`command: ${e.type}`}async editTurn(e,t,s){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph()[e];if(!r)return w({code:"NOT_FOUND",resource:"turn",id:e});const n=r.versions[r.activeVersion],i=r.activeVersion+1,o=await this.dispatch({type:"turn:edit",payload:{sessionId:this._id,turnId:e,newBlocks:t,roleSnapshot:s??n.role,newVersion:i},timestamp:(new Date).toISOString()});return o.ok&&(await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:{id:e,version:i}},timestamp:(new Date).toISOString()}),await this.refreshTurnTree()),o}async branch(e){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});if(!e.parent)return w({code:"INVALID_COMMAND",reason:"branch requires turn.parent to be set"});const t=await this.dispatch({type:"turn:branch",payload:{sessionId:this._id,turn:{...e,session:this._id}},timestamp:e.timestamp});return t.ok&&await this.refreshTurnTree(),t}async deleteTurn(e,t,s){if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const r=this.tree.graph();if(!r[e]?.versions[t])return w({code:"NOT_FOUND",resource:"turn version",id:`${e}:${t}`});const n=await this.dispatch({type:"turn:delete",payload:{sessionId:this._id,turnId:e,version:t,newHead:s},timestamp:(new Date).toISOString()});return n.ok&&await this.refreshTurnTree(),n}async switchVersionLeft(e){return this.switchVersion(e,-1)}async switchVersionRight(e){return this.switchVersion(e,1)}async switchVersion(e,t){if(!this.ws())return w({code:"NOT_FOUND",resource:"workspace",id:"current"});if(!this.meta())return w({code:"NOT_FOUND",resource:"session",id:this._id});const s=this.tree.graph()[e];if(!s)return w({code:"NOT_FOUND",resource:"turn",id:e});const r=Object.keys(s.versions).map(Number).sort(((e,t)=>e-t)),n=r.indexOf(s.activeVersion);if(-1===n)return w({code:"INVALID_COMMAND",reason:"Active version not found"});const i=n+t;if(i<0||i>=r.length)return w({code:"INVALID_COMMAND",reason:`No ${t<0?"previous":"next"} version available for turn ${e}`});const o=r[i],a=this.findSubtreeTip(this.tree.graph(),e,o),c=await this.dispatch({type:"session:update",payload:{sessionId:this._id,head:a},timestamp:(new Date).toISOString()});return c.ok&&await this.refreshTurnTree(),c}async snapshot(){const e=this,t=e.meta();if(!t)return;const s=e.ws(),r=e._role;if(!r)return;const n=await e.manager.ctx().context.getByTopics(s.index,t.topics),i=Array.from(new Set([...r.preferences,...t.preferences])),o=i.length>0?await e.manager.ctx().preferences.load(i):e._preferences,a=e.tree.chain().map((e=>e.versions[e.activeVersion])).filter((e=>!!e)),c=[...a].reverse().find((e=>"user"===e.actor));return{id:e._id,meta:t,role:r,preferences:o,context:n,model:t.metadata?.model,transcript:a,topics:t.topics,instructions:s.settings?.prompt,constraints:{role:r.constraints,session:t.constraints,turn:c?.constraints}}}async refreshTurnTree(){this.tree=await L.build(this._id,this.turnRepository)}findSubtreeTip(e,t,s){let r=t,n=s;for(;;){const t=e[r];if(!t)break;const s=t.children[n];if(!s||0===s.length)break;const i=s[0],o=e[i];if(!o)break;r=i,n=o.activeVersion}return{id:r,version:n}}},G=class{constructor(e,t){this.manager=e,this.processor=t}openOnce=new Map;async open(e){let t=this.openOnce.get(e);t||(t=new l,this.openOnce.set(e,t));const s=await t.do((async()=>{if(!this.manager.workspace().index.sessions[e])throw new Error(`Session ${e} not found in workspace index`);return J.create(e,this.manager,this.processor)}));if(s.error)throw s.error;return s.value}close(e){this.openOnce.delete(e)}async delete(e){const t=await this.manager.dispatch({type:"session:delete",payload:{sessionId:e},timestamp:(new Date).toISOString()});if(t.ok)return this.close(e),t.value=void 0,t;throw t.error}async has(e){return!this.manager.workspace().index.sessions[e]}async create(e){const s=t(),r=await this.manager.dispatch({type:"session:create",payload:{...e,id:s},timestamp:(new Date).toISOString()});if(r.ok)return this.open(s);throw r.error}list(){return Object.values(this.manager.workspace().index.sessions)}metadata(e){return this.manager.workspace().index.sessions[e]}},Y=class{constructor(e,t,s){this.storage=e,this.cache=t,this.bus=s}async register(e,t,s){const r=await x(e),n=await this.getRecord(r);if(n){const e={...n,refCount:n.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(e),this.cache.set(r,e),y(this.reference(e))}const i={sha256:r,mediaType:t,sizeBytes:e.byteLength,filename:s,refCount:1,remoteIds:{},createdAt:(new Date).toISOString(),lastUsedAt:(new Date).toISOString()};return this.storage.registerBlob?await this.storage.registerBlob(i,e):(await this.storage.storeBytes(r,e),await this.storage.saveRecord(i)),this.cache.set(r,i),this.bus.emit({name:"blobs:changed",payload:{sha256:r,record:i}}),y(this.reference(i))}async retain(e){const t=await this.getRecord(e);if(!t)return w({code:"NOT_FOUND",resource:"blob",id:e});const s={...t,refCount:t.refCount+1,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(s),this.cache.set(e,s),y(void 0)}async release(e){const t=await this.getRecord(e);if(!t)return w({code:"NOT_FOUND",resource:"blob",id:e});const s=Math.max(0,t.refCount-1),r={...t,refCount:s,lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(r),this.cache.set(e,r),y(void 0)}async purge(e){return await this.storage.deleteBytes(e),await this.storage.deleteRecord(e),this.cache.delete(e),this.bus.emit({name:"blobs:changed",payload:{sha256:e}}),y(void 0)}async recordRemoteId(e,t,s,r){const n=await this.getRecord(e);if(!n)return w({code:"NOT_FOUND",resource:"blob",id:e});const i={...n,remoteIds:{...n.remoteIds,[t]:{id:s,timestamp:r||(new Date).toISOString()}},lastUsedAt:(new Date).toISOString()};return await this.storage.saveRecord(i),this.cache.set(e,i),this.bus.emit({name:"blobs:changed",payload:{sha256:e,record:i}}),y(void 0)}async getRecord(e){const t=this.cache.get(e);if(t)return t;const s=await this.storage.loadRecord(e);return s&&this.cache.set(e,s),s}async getAllRecords(){const e=await this.storage.listRecords(),t={};for(const s of e)t[s.sha256]=s,this.cache.set(s.sha256,s);return t}reference(e){return{sha256:e.sha256,mediaType:e.mediaType,sizeBytes:e.sizeBytes,filename:e.filename}}async resolve(e,t){const s=await this.getRecord(e.sha256);if(!s)return y(null);if(t){const e=s.remoteIds[t];if(e)return y({kind:"remote",sha256:s.sha256,mediaType:s.mediaType,fileId:e.id,providerId:t,timestamp:e.timestamp});const r=await this.storage.loadBytes(s.sha256);return y(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}const r=await this.storage.loadBytes(s.sha256);return y(r?{kind:"inline",sha256:s.sha256,mediaType:s.mediaType,data:r}:null)}async resolveMany(e,t){const s=new Map;for(const r of e){const e=await this.resolve(r,t);if(!e.ok)return w(e.error);if(null===e.value)return w({code:"BLOB_ERROR",reason:`Unable to resolve blob ${r.sha256}${t?` with adapter ${t}`:""}`});s.set(r.sha256,e.value)}return y(s)}},H=class{constructor(e,t,s){this.collection=e,this.cache=t,this.registry=s}delegates=new Map;registerDelegate(e,t){this.delegates.set(e,t)}async get(e,t){const s=this.cache.get(e);if(s)return s;if(t){const s=this.delegates.get(t);if(s){const t=await s.get(e);return t&&this.cache.set(e,t),t}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;const n=r.state();return this.cache.set(e,n),n}async add(e){const t=this.delegates.get(e.content.kind);t?await t.add(e):await this.collection.create(e),this.cache.set(e.key,e)}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"}),t=((await e.next()).value||[]).map((e=>e.state()));for(const e of this.delegates.values()){const s=await e.list();t.push(...s)}return t}async update(e,t,s){if(s){const r=this.delegates.get(s);if(r){const s=await r.update(e,t);return s&&this.cache.set(e,s),s}}const r=await this.collection.find({field:"key",operator:"eq",value:e});if(!r)return null;await r.update(t);const n=r.state();return this.cache.set(e,n),n}async delete(e,t){if(t){const s=this.delegates.get(t);if(s){const t=await s.delete(e);return t&&this.cache.delete(e),t}}const s=await this.collection.find({field:"key",operator:"eq",value:e});if(!s)return!1;const r=await s.delete();return r&&this.cache.delete(e),r}async getByTopics(e,t){const s=new Map;for(const r of t){const t=e.topics[r];t&&t.contextKeys.forEach((t=>{const r=e.context[t],n=r?.kind||"unknown";s.has(n)||s.set(n,new Set),s.get(n).add(t)}))}if(0===s.size)return[];const r=[],n=[];for(const[e,t]of s.entries()){if(this.delegates.get(e))for(const s of t){const t=await this.get(s,e);t&&r.push(t)}else t.forEach((e=>n.push(e)))}if(n.length>0){const e=[];for(const t of n){const s=this.cache.get(t);s?r.push(s):e.push(t)}if(e.length>0){const t=await this.collection.filter({operator:"or",conditions:e.map((e=>({field:"key",operator:"eq",value:e})))});for(const e of t){const t=e.state();this.cache.set(t.key,t),r.push(t)}}}return r.sort(((e,t)=>new Date(t.timestamp).getTime()-new Date(e.timestamp).getTime()))}summarize(e){const t=this.registry?.get(e.content.kind);return t?t.summarize(e):{kind:e.content.kind,key:e.key,topics:e.topics,timestamp:e.timestamp,metadata:e.metadata}}},X=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async load(e){if(0===e.length)return[];const t=[],s=[];for(const r of e){const e=this.cache.get(r);e?s.push(e):t.push(r)}if(t.length>0){const e=await this.collection.filter({operator:"or",conditions:t.map((e=>({field:"id",operator:"eq",value:e})))});for(const t of e){const e=t.state();this.cache.set(e.id,e),s.push(e)}}return s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{id:e.id,topics:e.topics,timestamp:e.timestamp,snippet:e.content.slice(0,100)}}},Q=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.name,e)}async update(e,t){const s=await this.collection.find({field:"name",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"name",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return{name:e.name,label:e.label,description:e.description,preferences:e.preferences?.length??0,topics:e.topics??[],constraints:e.constraints}}},Z=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:1e3,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},ee=class{constructor(e,t){this.collection=e,this.cache=t}key({id:e,session:t,version:s}){return`${t}:${s}:${e}`}async find({id:e,session:t,version:s},r=!0){const n=this.key({id:e,session:t,version:s});this.cache.get(n)&&this.cache.get(n);const i=await this.collection.find({operator:"and",conditions:[{field:"id",operator:"eq",value:e},{field:"session",operator:"eq",value:t},{field:"version",operator:"eq",value:s}]});return i?(r&&this.cache.set(n,i),i):null}async get(e){return this.find(e)}async add(e){const t=await this.collection.create(e);this.cache.set(this.key(e),t)}async update(e,t){const s=await this.find(e);return s?(await s.update(t),s.state()):null}async listBySession(e){return(await this.collection.filter({field:"session",operator:"eq",value:e})).map((e=>e.state()))}async delete(e){const t=await this.find(e,!1);if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(this.key(e)),s}},te=class{constructor(e,t){this.collection=e,this.cache=t}async get(e){const t=this.cache.get(e);if(t)return t;const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;const r=s.state();return this.cache.set(e,r),r}async add(e){await this.collection.create(e),this.cache.set(e.id,e)}async update(e,t){const s=await this.collection.find({field:"id",operator:"eq",value:e});if(!s)return null;await s.update(t);const r=s.state();return this.cache.set(e,r),r}async delete(e){const t=await this.collection.find({field:"id",operator:"eq",value:e});if(!t)return!1;const s=await t.delete();return s&&this.cache.delete(e),s}async list(){const e=await this.collection.list({limit:100,type:"cursor",direction:"forward"});return((await e.next()).value||[]).map((e=>e.state()))}summarize(e){return e}},se=class{cache;maxSize;constructor(e){this.cache=new Map,this.maxSize=e}get(e){const t=this.cache.get(e);return void 0!==t&&(this.cache.delete(e),this.cache.set(e,t)),t}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.maxSize){const e=this.cache.keys().next().value;void 0!==e&&this.cache.delete(e)}this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}},re=(e={})=>({id:t(),label:"New Conversation",role:"assistant-default",topics:[],preferences:[],metadata:{created:(new Date).toString(),updated:(new Date).toString()},...e,head:e.head?e.head:void 0}),ne=(e={},s)=>({id:e.id??t(),version:e.version??0,session:e.session??s?.id??t(),actor:"user",blocks:[{id:t(),type:"text",content:"Hello, world!"}],timestamp:(new Date).toString(),role:s?.role??"default",...e,parent:e.parent?e.parent:void 0}),ie=(e={})=>({id:e.id??t(),settings:k({language:"en-US",defaultRole:void 0,prompt:void 0},e.settings),project:k({id:t(),name:"Unnamed Project"},e.project),index:k({roles:{},preferences:{},context:{},sessions:{},topics:{},blobs:{},tools:{},extensions:{}},e.index)}),oe=class{bytes=new Map;records=new Map;async storeBytes(e,t){this.bytes.has(e)||this.bytes.set(e,t)}async loadBytes(e){return this.bytes.get(e)??null}async hasBytes(e){return this.bytes.has(e)}async deleteBytes(e){this.bytes.delete(e)}async saveRecord(e){this.records.set(e.sha256,{...e})}async loadRecord(e){return this.records.get(e)??null}async deleteRecord(e){this.records.delete(e)}async listRecords(){return Array.from(this.records.values()).map((e=>({...e})))}async exportAllBytes(){return Array.from(this.bytes.entries()).map((([e,t])=>[e,new Uint8Array(t)]))}async registerBlob(e,t){this.bytes.has(e.sha256)||this.bytes.set(e.sha256,t),this.records.set(e.sha256,{...e})}},ae="blob_bytes",ce="blob_records";function de(e){return new Promise(((t,s)=>{e.onsuccess=()=>t(e.result),e.onerror=()=>s(e.error)}))}function le(e){return new Promise(((t,s)=>{e.oncomplete=()=>t(),e.onerror=()=>s(e.error),e.onabort=()=>s(new Error("Transaction aborted"))}))}var ue=class{dbName;db=null;constructor(e={}){this.dbName=e.dbName??"aiworkspace-blobs"}async open(){this.db||(this.db=await new Promise(((e,t)=>{const s=indexedDB.open(this.dbName,1);s.onupgradeneeded=e=>{const t=e.target.result;this.createSchema(t)},s.onsuccess=()=>e(s.result),s.onerror=()=>t(s.error),s.onblocked=()=>t(new Error(`[IndexedDBBlobStorage] Database "${this.dbName}" blocked.`))})),this.db.onversionchange=()=>{this.db?.close(),this.db=null})}createSchema(e){e.objectStoreNames.contains(ae)||e.createObjectStore(ae,{keyPath:"sha256"}),e.objectStoreNames.contains(ce)||e.createObjectStore(ce,{keyPath:"sha256"})}close(){this.db?.close(),this.db=null}async deleteDatabase(){this.close(),await new Promise(((e,t)=>{const s=indexedDB.deleteDatabase(this.dbName);s.onsuccess=()=>e(),s.onerror=()=>t(s.error)}))}getDB(){if(!this.db)throw new Error("[IndexedDBBlobStorage] Database not open. Call open() first.");return this.db}readTx(...e){return this.getDB().transaction(e,"readonly")}writeTx(...e){return this.getDB().transaction(e,"readwrite")}async storeBytes(e,t){const s=this.readTx(ae);if(await de(s.objectStore(ae).get(e)))return;const r=this.writeTx(ae);r.objectStore(ae).put({sha256:e,data:t}),await le(r)}async loadBytes(e){const t=this.readTx(ae),s=await de(t.objectStore(ae).get(e));return s?.data??null}async hasBytes(e){const t=this.readTx(ae);return await de(t.objectStore(ae).count(e))>0}async deleteBytes(e){const t=this.writeTx(ae);t.objectStore(ae).delete(e),await le(t)}async saveRecord(e){const t=this.writeTx(ce);t.objectStore(ce).put(e),await le(t)}async loadRecord(e){const t=this.readTx(ce);return await de(t.objectStore(ce).get(e))??null}async deleteRecord(e){const t=this.writeTx(ce);t.objectStore(ce).delete(e),await le(t)}async listRecords(){return de(this.readTx(ce).objectStore(ce).getAll())}async exportAllBytes(){const e=this.readTx(ae);return(await de(e.objectStore(ae).getAll())).map((({sha256:e,data:t})=>[e,t]))}async registerBlob(e,t){await new Promise(((s,r)=>{const n=this.getDB().transaction([ae,ce],"readwrite"),i=n.objectStore(ae),o=n.objectStore(ce),a=i.count(e.sha256);a.onsuccess=()=>{0===a.result&&i.put({sha256:e.sha256,data:t}),o.put(e)},a.onerror=()=>r(a.error),n.oncomplete=()=>s(),n.onerror=()=>r(n.error),n.onabort=()=>r(new Error("registerBlob transaction aborted"))}))}},pe={};((e,t)=>{for(var s in t)r(e,s,{get:t[s],enumerable:!0})})(pe,{GOOGLE_MODELS:()=>me,GoogleGenAIAdapter:()=>ke,mapBlockToPart:()=>we,mapTurnToContent:()=>ye,mappings:()=>he,parseModelResponse:()=>fe});var he={text:{to:e=>({text:e.text}),from:e=>({id:s(),type:"text",text:e.text})},summary:{to:e=>({text:`[Summary]: ${e.text}`}),from:e=>({id:s(),type:"summary",text:e.text})},thinking:{to:e=>({text:e.thinking}),from:e=>({id:s(),type:"thinking",thinking:e.thinking})},"tool:use":{to:e=>({functionCall:{name:e.name,args:e.input}}),from:e=>({id:s(),type:"tool:use",name:e.functionCall?.name??e.name,input:e.functionCall?.args??e.input??{}})},"tool:result":{to:e=>({functionResponse:{name:e.useId,response:{result:e.content}}}),from:e=>({id:s(),type:"tool:result",useId:e.useId,content:e.content,isError:e.isError})},"role:transition":{to:e=>({text:`[Role transition: ${e.previousRole??"none"} → ${e.newRole}]`}),from:e=>({id:s(),type:"role:transition",previousRole:e.previousRole,newRole:e.newRole})}},me=[{provider:"google",name:"gemini-3.1-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3-flash-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3,cache:{read:.05,write:1}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-3.1-flash-lite-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.25,output:1.5,cache:{read:.025,write:1}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-pro",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10,cache:{read:.125,write:4.5}}}],capacity:[{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.3,output:2.5,cache:{read:.03,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-lite",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4,cache:{read:.01,write:1}}}],capacity:[{unit:"call",max:4e3,period:60},{unit:"token",max:4e6,period:60}]},{provider:"google",name:"gemini-3-pro-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!0,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:2,output:12,cache:{read:.2,write:4.5}}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-2.5-flash-image",window:{size:4096,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"image",scale:0,cost:{input:.02,output:0}}],capacity:[{unit:"call",max:100,period:60}]},{provider:"google",name:"gemini-2.5-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-3.1-flash-live-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.5,output:3}}],capacity:[{unit:"concurrent",max:10,period:0}]},{provider:"google",name:"gemini-2.5-computer-use-preview",window:{size:1048576,out:65536},feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:1.25,output:10}}],capacity:[{unit:"call",max:200,period:60},{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-001",window:{size:2048,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.5,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]},{provider:"google",name:"gemini-embedding-2",window:{size:8192,out:0},feature:{vision:!1,tools:!1,json:!1,cache:!1,streaming:!1,thinking:!1},pricing:[{unit:"token",scale:6,cost:{input:.3,output:0}}],capacity:[{unit:"token",max:1e6,period:60}]}];function fe(e,s){const r=e.candidates?.[0];if(!r?.content?.parts)return w({code:"BACKEND_ERROR",reason:"No valid content parts in response"});const n=[];let i;for(const e of r.content.parts)if(e.thought)n.push((o=e.text??"",{id:t(),type:"thinking",thinking:o}));else if(e.text){i=e;break}var o;if(!i)return w({code:"BACKEND_ERROR",reason:"Response missing JSON part"});const a=function(e){try{const t=JSON.parse(e);return t.blocks&&Array.isArray(t.blocks)?y(t.blocks):w({code:"BACKEND_ERROR",reason:'Invalid response: missing or malformed "blocks" array'})}catch(e){return w({code:"BACKEND_ERROR",reason:`Failed to parse response JSON: ${e instanceof Error?e.message:String(e)}`})}}(i.text);if(!a.ok)return a;for(const e of a.value){const t=s.parse(e,ke.provider());t&&n.push(t)}return y(n)}async function ye(e,t,s){const r="assistant"===e.actor?"model":"user",n=[];for(const r of e.blocks){if("image"===r.type||"document"===r.type){const e=r.ref;if(!e)continue;const s=await ge(e,t);s&&n.push(s);continue}const e=we(r,s);e&&n.push(e)}return{role:r,parts:n}}function we(e,t){const s=t.get(e.type);if(!s)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: block type "${e.type}" is not registered. Register it via registry.register() before use.`);const r=s.mappings?.[ke.provider()];if(!r)throw new Error(`[GoogleGenAIAdapter] mapBlockToPart: no "${ke.provider()}" mapping for block type "${e.type}". Add a mapping via registry.update("${e.type}", { mappings: { ${ke.provider()}: { to, from } } }).`);return r.to(e)}async function ge(e,t){const s=await t(e,ke.provider());if(!s.ok||!s.value)return null;const{value:r}=s;return"remote"===r.kind?{fileData:{fileUri:r.fileId,mimeType:r.mediaType}}:{inlineData:{data:v(r.data),mimeType:r.mediaType}}}function be(e){return Math.ceil(e.length/4)}var ke=class e{constructor(e,t,s={model:"gemini-2.5-flash"}){this.client=e,this.services=t,this._models=t.models;const r=this._models.get(s.model);if(!r)throw new Error(`Could not get model: ${s.model} profile in registry.`);this.model=r,this.registerGeminiMappings()}model;_models;registerGeminiMappings(){const t=this.services.blockRegistry;for(const[s,r]of Object.entries(he))t.update(s,{mappings:{[e.provider()]:r}})}async status(t){return{provider:e.provider(),model:this.model.name,ready:!0,window:this.model.window,feature:{vision:!0,tools:!0,json:!0,cache:!1,streaming:!0,thinking:!0},pricing:[{unit:"token",scale:6,cost:{input:.1,output:.4}}],rate:{load:0,capacity:[{unit:"call",max:1e3,period:60},{unit:"token",max:4e6,period:60}]}}}async resolve(e){const{prompt:s}=e,{assembler:r,blockRegistry:n,blobResolver:i}=this.services,o=s.model&&this._models.get(s.model)||this.model,a=r.build(s,[{label:"block-architecture",content:n.description(),position:"after:instructions"}]),c=r.join(a),d={role:"system",parts:[{text:c}]},l=s.constraints[o.name]??{tokens:{}},u=await Promise.all(s.transcript.map((e=>ye(e,i,n)))),p=n.schema(),h={model:o.name,contents:u,config:{systemInstruction:d,thinkingConfig:{includeThoughts:!0},responseMimeType:"application/json",responseSchema:p,...void 0!==l.temperature&&{temperature:l.temperature},...void 0!==l.tokens.max&&{maxOutputTokens:l.tokens.max},...l.tokens.stops?.length&&{stopSequences:l.tokens.stops},...void 0!==l.tokens.thought&&{thinkingConfig:{includeThoughts:!0,thinkingBudget:l.tokens.thought}}}},m=u.flatMap((e=>e.parts??[])).map((e=>e.text??"")).join(""),f=be(c),g=be(m),b=f+g,k=this;return{model:o.name,instructions:a,transcript:s.transcript,context:s.system.context,preferences:s.system.preferences,constraints:l,tokens:{breakdown:{system:f,transcript:g},total:b,source:"estimated",output:l.tokens.max??o.window.out,remaining:o.window.size-b},async execute(){let e;try{e=await k.client.models.generateContent(h)}catch(e){return w({code:"BACKEND_ERROR",reason:e instanceof Error?e.message:String(e)})}const t=fe(e,k.services.blockRegistry);if(!t.ok)return t;const r=t.value,n=new K("assistant",s.session);for(const e of r)n.addBlock(e);return y({turn:n.build(),effects:[]})},async*executeStream(){let e;try{e=await k.client.models.generateContentStream(h)}catch(e){const r=e instanceof Error?e.message:String(e),n=new K("assistant",s.session);return n.addBlock({id:t(),type:"text",text:`[Stream error: ${r}]`}),void(yield{turn:n.build(),effects:[]})}const r=[];for await(const s of e){const e=s.candidates?.[0];if(e?.content?.parts)for(const s of e.content.parts)s.thought||s.text&&(r.push(s.text),yield{blocks:[{id:t(),type:"text",text:s.text}]})}const n=r.join(""),i=new K("assistant",s.session);i.addBlock({id:t(),type:"text",text:n}),yield{turn:i.build(),effects:[]}}}}static provider(){return"google"}static models(){return me}};async function xe(t){const{blobStorage:s,eventBus:r=e(),getWorkspace:i,setWorkspace:o,processor:a,guard:c,toolRegistry:d,models:l=[],extensions:u=[],extensionReducers:m={},extensionMiddleware:f=[],extensionIndexers:y=[],extensionStores:w,extensionSchemas:g=[]}=t,b=p(t.db),k=[...g,...u.flatMap((e=>e.schemas??[]))],x={...m,...u.reduce(((e,t)=>({...e,...t.reducers})),{})},v=[...f,...u.flatMap((e=>e.middleware??[]))],_=[...y,...u.flatMap((e=>e.indexers??[]))],O=u.flatMap((e=>e.blocks??[]));await b.open(k);const N={workspace:await b.collection(h.WORKSPACE),role:await b.collection(h.ROLE),preference:await b.collection(h.PREFERENCE),context:await b.collection(h.CONTEXT),session:await b.collection(h.SESSION),topic:await b.collection(h.TOPIC),turn:await b.collection(h.TURN)},R=new Y(s,new se(200),r),D=new F,I={workspaceStore:new te(N.workspace,new se(1)),roles:new Q(N.role,new se(100)),preferences:new X(N.preference,new se(500)),context:new H(N.context,new se(500),D),topics:new T(N.topic,new se(100)),sessions:new Z(N.session,new se(50)),turns:new ee(N.turn,new se(50)),blobs:R,tools:d,db:b,indexers:[...n,..._]};if(w){const e=w(I);Object.assign(I,e)}await Promise.all(u.map((async e=>{if(e.stores){const t=await e.stores(I);Object.assign(I,t)}})));const E=new S({ctx:I,getWorkspace:i,updateWorkspace:async e=>{if(await o(e),e?.id||e?.settings||e?.project){const e=i();await I.workspaceStore.update(e.id,{id:e.id,settings:e.settings,project:e.project})}},guard:c,bus:r});Object.entries(C).forEach((([e,t])=>{E.register(e,t)})),Object.entries(x).forEach((([e,t])=>{E.register(e,t)})),E.use(j),v.forEach((e=>{E.use(e)}));const A=new G(E,a),B=new M;O.forEach((e=>B.register(e))),u.forEach((e=>{e.contexts&&e.contexts.forEach((e=>{if(D.register(e),e.store){const t=e.store(I);I.context.registerDelegate(e.kind,t)}}))}));const $=new U(l),z={assembler:new q,processor:a,blobResolver:I.blobs.resolve.bind(I.blobs),blockRegistry:B,contextRegistry:D,models:$};return{manager:E,sessions:A,ctx:I,services:z,bootstrap:async e=>{await E.dispatch({type:"workspace:sync",payload:void 0,timestamp:(new Date).toISOString()});let t=i();t.id&&""!==t.id&&"00000000-0000-0000-0000-000000000000"!==t.id||(await E.dispatch({type:"workspace:create",payload:e.workspace,timestamp:(new Date).toISOString()}),t=i());const s=[];if(e.roles)for(const r of e.roles)t.index.roles[r.name]||(await E.dispatch({type:"role:add",payload:r,timestamp:(new Date).toISOString()}),s.push(r.name));return e.workspace.settings.defaultRole&&t.settings.defaultRole!==e.workspace.settings.defaultRole&&await E.dispatch({type:"workspace:sync",payload:{settings:{...t.settings,defaultRole:e.workspace.settings.defaultRole}},timestamp:(new Date).toISOString()}),{workspace:i(),roles:s}}}}export{h as COLLECTIONS,P as EMPTY_SYSTEM_ROLE,pe as GoogleAdapter,ue as IndexedDBBlobStorage,se as LRUCache,oe as MemoryBlobStorage,J as Session,G as SessionManager,K as TurnBuilder,L as TurnTree,S as WorkspaceManager,v as bufferToBase64,x as computeSHA256,re as createEmptySession,ne as createEmptyTurn,ie as createEmptyWorkspace,xe as createWorkspace,p as createWorkspaceDatabase,b as del,w as error,O as getExtension,k as merge,g as omitNullUndefined,_ as shortHash,y as success};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asaidimu/utils-workspace",
3
- "version": "6.2.6",
3
+ "version": "6.3.0",
4
4
  "description": "Content-addressed workspace and conversation management for AI applications.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",