While the BaseEngine class is a singleton, normally, it is possible to have multiple instances of the class in an application if the class is used in multiple contexts that have different providers.
Returns true if ALL configured properties loaded successfully. Useful as a quick health check after engine startup.
Returns a COPY of the metadata configs array for the engine. This is a copy so you can't modify the original configs by modifying this array.
Returns the context user set for the object, this is set via the Config() method.
Current snapshot of conversations (non-reactive).
Observable stream of the current user's conversation list. Emits whenever conversations are loaded, created, deleted, archived, or pinned.
Observable that emits when any data property changes due to a refresh. Subscribe to this to react to engine data updates (e.g., sync Angular observables).
Events are emitted after data is refreshed in response to BaseEntity save/delete events. The event includes the full config and the new data array.
Returns a read-only snapshot of all engine property load states. Each entry maps a property name to its load status, including entity/dataset name, row count, success/failure flag, and error message if applicable. Used by dev tools for diagnostics and health monitoring.
ProtectedEngineControls the default RunView ResultType for all entity configs loaded by this engine. Override in subclasses to change the default for the entire engine without modifying each individual config entry.
Individual configs can still override this via their own ResultType property.
ProtectedEntityOverridable property to set the debounce time for entity events. Default is 1500 milliseconds (1.5 seconds). This debounce time is used when immediate array mutations cannot be applied (e.g., when Filter, OrderBy, or AdditionalLoading overrides are present) and a full view refresh is required.
Note: When immediate mutations ARE possible (no Filter, OrderBy, or AdditionalLoading override), updates happen synchronously without any debounce delay.
True when the engine loaded successfully but all entity configs were skipped because the current user lacks read permissions. Accessor properties will throw PermissionConstrainedError if accessed in this state. Check this flag first to degrade gracefully.
Returns true if the data has been loaded, false otherwise.
Returns the loading subject. You can call await Config() and after Config() comes back as true that means you're loaded. However you can also directly subscribe to this subject to get updates on the loading status.
ProtectedMaxMaximum number of retries for an event-triggered config refresh that failed transiently. Overridable by subclasses that want more or less persistence.
List of entity names that were skipped due to permission denial. Empty if not permission-constrained. Useful for logging/diagnostics.
Current snapshot of projects/folders (non-reactive).
Observable stream of the projects (conversation folders) for the current environment. Emits whenever projects are loaded or a project is created, renamed, or deleted (kept in sync via the entity event handler).
Returns the metadata provider to use for the engine. If a provider is set via the Config method, that provider will be used, otherwise the default provider will be used.
Returns the RunView provider to use for the engine. This is the same underlying object as the
StaticInstanceReturns the global instance of the class. This is a singleton class, so there is only one instance of it in the application. Do not directly create new instances of it, always use this method to get the instance.
Adds a detail (message) to the cached list for a conversation. If no cache entry exists, this is a no-op (caller should LoadConversationDetails first).
The conversation this detail belongs to
The detail entity to add
Adds a dynamic metadata configuration at runtime.
The metadata configuration to add
OptionalcontextUser: UserInfoThe context user information
ProtectedAdditionalSubclasses can override this method to perform additional loading tasks
OptionalcontextUser: UserInfoProtectedapplyApplies an immediate array mutation based on the entity event type. This is faster than running a full view refresh for simple add/update/delete operations.
On save, the cached entry is a clone owned by this engine's provider — not the saver's entity instance. Storing the saver's instance would pin the saver's provider (often a per-request provider) inside the engine's cache for the engine's full lifetime, which leaks the provider and all its associated state.
The configuration for the property being mutated
The entity event containing the affected entity and event type
ProtectedapplyRemoves a deleted record from the engine's in-memory arrays using the primary key values from the remote-invalidate event payload. No server round-trip needed.
true if successfully applied to all matching configs, false if fallback is needed
ProtectedapplyApplies record data from a remote-invalidate event directly to the engine's in-memory arrays. Creates a BaseEntity instance, loads the JSON data into it, then updates the matching config arrays — same as applyImmediateMutation but from serialized data instead of a live entity.
true if successfully applied to all matching configs, false if fallback is needed
Archives a conversation (sets IsArchived = true) and removes it from the active list.
The conversation ID to archive
The current user context
true if successful
ProtectedbeginOpens a new full-refresh "generation" for a property and returns its token. Each call bumps the property's monotonic counter, so a token is the latest iff no later refresh for that property has begun since. See _configRefreshGeneration.
ProtectedcanDetermines if an immediate array mutation can be used instead of running a full view refresh. Immediate mutations are only safe when:
The configuration to check
OptionalskipAdditionalLoadingCheck: booleanWhen true, skips the AdditionalLoading override check. Use this when the caller will invoke AdditionalLoading() itself after applying mutations (e.g., applyRemoteRecordData applies all config mutations then calls AdditionalLoading).
true if immediate mutation is safe, false if a full view refresh is needed
ProtectedCheckProtectedCheckAll-or-nothing permission gate: checks CanRead on every entity config. If ANY
entity is denied, ALL configs are skipped — the engine is marked permission-constrained
and its data arrays are set to empty []. This prevents noisy permission-denied errors
and endless retry loops for users with limited permissions (e.g., org-scoped SaaS roles).
On the server side with a system user (who has all permissions), this method returns the original configs unchanged — no behavior change for privileged users.
The original configs array (all permissions pass) or an empty array (any denied)
ProtectedclassifyClassifies a single entity event against a single config's backing array:
For deletes, the by-key membership check uses the event payload's pre-delete OldValues snapshot — BaseEntity.Delete() calls NewRecord() right after raising the event, which wipes field values and REGENERATES the primary key, so the live entity's key can never match the deleted row by the time the debounced handler runs.
Clears all cached data: conversations, details, and peripheral data. Typically called on logout or environment switch.
ProtectedcloneCreates a fresh BaseEntity owned by this engine's provider and populates it from the given source entity's field values. Used by applyImmediateMutation to avoid pinning the source entity's provider inside this engine's cache.
Configures the engine. Unlike other engines that bulk-load entity tables via BaseEngine.Load(), ConversationEngine manages its own caching because conversations are user-scoped and filtered by environment, which doesn't fit the standard "load all rows" pattern.
Call this once at startup to initialize the engine. Conversation data is loaded separately via LoadConversations().
OptionalforceRefresh: booleanOptionalcontextUser: UserInfoOptionalprovider: IMetadataProviderExtended configuration method with object-based options. This provides a more flexible API compared to Config() with positional parameters.
Internally calls Config() after setting up options that Load() can access.
Optionaloptions: ConfigExOptionsConfiguration options object
Promise that resolves when configuration is complete
ProtectedconfigTrue when the config's last load attempt left it in a successfully-loaded state. Reads the same map entry HandleSingleViewResult writes — a transient failure (network, server restart) records loadedSuccessfully=false; a permission denial is recorded as loaded-empty (true) and is deliberately NOT retryable.
ProtectedContextReturns false ONLY when we can positively determine that the effective user
lacks Read permission on entityName. Unknown cases (no entity name, no
resolvable user, entity not in metadata) return true — so the default is to
treat a failure as transient/retryable and server-side system-user loads
(full access) are unaffected.
Used by HandleSingleViewResult to classify a FAILED config load: a load that failed because the user can't read the entity is a PERMANENT condition (a retry will never succeed for this role), so the engine should load that property empty rather than loop on "not marking as loaded" — which is what hangs the Explorer shell for a restricted / app-scoped user (e.g. a magic-link guest). Security is unaffected: the user still receives no data.
This is a classifier consulted AFTER a failure, never a predictive pre-skip — so a readable entity is always actually queried, and stale/late client permission metadata can never cause a readable entity to be silently skipped.
Creates a new conversation, saves it to the database, and adds it to the cached list.
Display name for the conversation
The environment ID
The current user context
Optionaldescription: stringOptional description
OptionalprojectId: stringOptional project ID
Optionaloptions: CreateConversationOptionsThe newly created conversation entity
Creates a new conversation detail (message), saves it, and adds it to the cache.
The conversation this detail belongs to
The message role ('User', 'AI', 'System')
The message content
The current user context
OptionaladditionalFields: Partial<MJConversationDetailEntity>Optional extra fields to set on the entity
The saved conversation detail entity
ProtectedDebounceThis method handles the debouncing process, by default using the EntityEventDebounceTime property to set the debounce time. Debouncing is done on a per-entity basis, meaning that if the debounce time passes for a specific entity name, the events will be processed. This is done to prevent multiple events from being processed in quick succession for a single entity which would cause a lot of wasted processing.
ALL events raised during the debounce window are buffered and delivered as one batch to ProcessEntityEvents — not just the last one. The refresh-vs-skip decision must be an OR over every coalesced event: judging only the last event would let an already-applied write (e.g., an engine method's in-place save of a cached instance) mask an earlier fresh-instance save the array has never seen.
Override this method if you want to change how debouncing works, such as having variable debounce times per-entity, etc.
Deletes a conversation from the database and removes it from the cached list.
The conversation ID to delete
The current user context
true if successful
Deletes a conversation detail and removes it from the cache.
The conversation this detail belongs to
The detail ID to delete
The current user context
true if deleted successfully
Deletes multiple conversations in a batch operation with per-item error tracking.
Array of conversation IDs to delete
The current user context
Object with successful and failed deletions
Deletes a folder (project) in an FK-safe way. The Conversation→Project and Project→Project (ParentID) foreign keys are RESTRICT, so the row can't be deleted while anything references it. Before deleting, this:
Note: this does NOT reassign Tasks that reference the project; if a Task still references it, the final delete will fail and this throws with the DB message.
The project (folder) ID to delete
The current user context
true if deleted successfully
ProtectedemitNotifies subscribers of ObserveProperty(propertyName) that the array has changed.
No-op if no one has ever observed this property (BehaviorSubject not created).
Called from the array mutation sites in BaseEngine.
Folds a conversation that was created OUTSIDE this engine (e.g. server-side by a realtime-session mint, which never fires a client BaseEntity event) into the cached list so Conversations$ emits reactively — the sidebar list updates without a manual refresh. Costs at most ONE single-row query, and only when the conversation isn't already cached:
Idempotent and safe to call from a session-start hook on every start.
The conversation ID to ensure is present in the cache
The current user context
The cached/loaded conversation entity, or null when it can't be loaded
Ensures the engine is loaded before the caller reads engine state. This is
the right call to make at every consumption point — especially for engines
registered with @RegisterForStartup({ deferred: true }) whose initial load
runs in the background after app boot.
Idempotent: if the engine is already loaded, returns immediately. If a load
is in flight (e.g. the deferred startup or another consumer triggered it),
returns the same in-progress promise rather than starting a second load —
BaseEngine.Load handles this internally via _loadingSubject.
Equivalent to this.Config(false) but reads more clearly at call sites:
await AIEngineBase.Instance.EnsureLoaded();
const models = AIEngineBase.Instance.Models;
OptionalcontextUser: UserInfoOptional context user (server-side only)
Optionalprovider: IMetadataProviderOptional metadata provider override
ProtectedfindFinds an entity in the array by matching all primary key columns. Supports composite primary keys by comparing all PrimaryKey fields from EntityInfo.
The array of entities to search
The entity to find (using its primary key values)
The index of the matching entity, or -1 if not found
Builds the agent-facing context window for a conversation.
When a persisted cross-turn summary exists (the row with the highest Sequence
whose SummaryOfEarlierConversation is non-null), the window is
[summary-as-message, boundary row (raw), ...tail (raw)] — the summary covers every
row with Sequence below the boundary's, so there is no gap and no overlap. When no
summary exists, the window is all messages, optionally capped to the most recent
maxTailMessages (parity with the legacy sliding-window history load).
Served from the per-conversation _detailCache: the first touch of a conversation
pays one GetConversationComplete query; every subsequent call is a warm in-memory
slice kept current by the engine's entity-event handlers.
The conversation to build the window for
User context for the (cold-miss) cache load
Optionaloptions: { excludeDetailIds?: string[]; maxTailMessages?: number }OptionalexcludeDetailIds?: string[]Detail rows to omit — e.g. the in-flight agent-response placeholder row created before the agent executes
OptionalmaxTailMessages?: numberWith NO summary boundary present: cap the window to the most recent N messages. Ignored when a boundary exists — the summary already covers everything before it, and cutting into the post-boundary tail would create a coverage gap.
Messages in chronological order, each stamped with ConversationContextMetadata
Gets the cached agent run for a specific conversation detail.
The conversation ID
The conversation detail ID
The agent run entity, or undefined if not cached
Gets all cached agent runs for a conversation, keyed by detail ID.
The conversation ID
Map of detail ID to agent run, or empty map if not cached
Returns the full cache entry for a conversation, including all peripheral data (agent runs, artifacts, ratings, user avatars). Returns undefined if not cached.
This is the primary read method for UI components — returns instant cached data without any database round-trip.
The conversation ID
The full cache entry, or undefined
Returns cached conversation details (messages only) without hitting the database. Returns undefined if no cache entry exists for this conversation.
The conversation ID
Cached message entities, or undefined if not cached
ProtectedGetRetrieves engine-loaded data for a config property by name. This is the canonical accessor for engine getter properties — it checks the data map for permission denial and throws PermissionConstrainedError with the specific denied entity name(s) if the config was skipped.
Subclasses should use this in every getter that exposes engine-loaded data:
public get Models(): MJAIModelEntityExtended[] {
return this.GetConfigData<MJAIModelEntityExtended>('_models');
}
The config property name (e.g., '_models', '_agents'), matching the PropertyName used in the engine's Config() params array.
The data array for the property, or an empty array if not yet loaded.
Finds a conversation by ID in the cached list.
The conversation ID to find
The conversation entity, or undefined if not in cache
The Global Object Store is a place to store global objects that need to be shared across the application. Depending on the execution environment, this could be the window object in a browser, or the global object in a node environment, or something else in other contexts. The key here is that in some cases static variables are not truly shared because it is possible that a given class might have copies of its code in multiple paths in a deployed application. This approach ensures that no matter how many code copies might exist, there is only one instance of the object in question by using the Global Object Store.
Look up grantor info for a conversation the current user was shared into.
Returns null for conversations the user owns (not shared with them).
ProtectedHandleOverrides BaseEngine's entity event handler to watch for external mutations to Conversations and Conversation Details. When another piece of code (outside this engine) saves or deletes these entities, we sync our cache.
The _selfMutating guard prevents processing events from our own mutations.
ProtectedHandleSubclasses of BaseEngine can override this method to handle individual MJGlobal events. This is typically done to optimize the way refreshes are done when a BaseEntity is updated. If you are interested in only BaseEntity events, override the HandleIndividualBaseEntityEvent method instead as this method primarily serves to filter all the events we get from MJGlobal and only pass on BaseEntity events to HandleIndividualBaseEntityEvent.
ProtectedHandleHandles remote-invalidate events from cross-server cache invalidation. These events are fired by GraphQLDataProvider when it receives a cache invalidation notification via GraphQL subscription (originating from Redis pub/sub on another server).
When the event payload includes recordData (the saved entity as JSON), the engine applies the change directly to its in-memory array — no server round-trip needed. For delete events or events without recordData, falls back to LoadSingleConfig.
ProtectedHandleHandles the result of a single view load.
OptionalcontextUser: UserInfoAll BaseEngine sub-classes get an implementation of IStartupSink so they can be set the auto start in their app container, if desired, simply by adding the
OptionalcontextUser: UserInfoOptionalprovider: IMetadataProviderProtectedhasChecks if the current instance has overridden the AdditionalLoading method. We do this by comparing the method to the base class's method.
true if AdditionalLoading is overridden, false if using the base implementation
Returns true if conversation details are cached for the given conversation.
Invalidates (removes) the cached details for a specific conversation. The next call to LoadConversationDetails will fetch fresh data.
The conversation ID to invalidate
ProtectedisChecks if the exact entity object reference is already in the config's data array. Used to skip unnecessary refreshes for UPDATE events where the object was mutated in place.
The configuration to check
The entity to look for
true if the exact object reference is already in the array
ProtectedisChecks if an entity is in the config's data array by object reference OR by primary key match. Used for DELETE events where we need to know if the entity still exists in the array.
For deletes, pass preDeleteValues (the event payload's OldValues snapshot): by the time
the debounced handler runs, BaseEntity.Delete() has already called NewRecord(), which wipes
the entity's fields and regenerates its primary key — so a by-key check against the live
entity can never match the deleted row. Same hazard (and same OldValues workaround) as
LocalCacheManager.HandleBaseEntityEvent.
The configuration to check
The entity to look for
OptionalpreDeleteValues: Record<string, unknown>Pre-delete field snapshot (delete event payload's OldValues)
true if the entity is in the array (by reference or by primary key)
ProtectedisTrue when generation is still the most recent token handed out by
beginConfigRefresh for propertyName — i.e. no newer full refresh for this
property has started since. A refresh whose token is stale must NOT commit its results:
a newer refresh was initiated afterward and read a more-recent state.
Check if a specific property was skipped due to permission denial. Forward-compatible with a future partial-loading approach.
ProtectedLoadThis method should be called by sub-classes to load up their specific metadata requirements. For more complex metadata loading or for post-processing of metadata loading done here, overide the AdditionalLoading method to add your logic.
OptionalforceRefresh: booleanOptionalcontextUser: UserInfoProtectedLoadLoads the specified metadata configurations.
The metadata configurations to load
The context user information
OptionalbypassCache: booleanWhen true, bypasses all server-side caching (RunView and dataset) to fetch fresh data
directly from the database. Passed through from Load when forceRefresh is true (i.e., Config(true)).
Loads conversation details using the efficient GetConversationComplete query which returns messages, agent runs, artifacts, ratings, and user avatars in one round-trip. Results are cached for instant retrieval on subsequent calls.
The conversation to load details for
The current user context
If true, reloads even if cached
The full cache entry with all peripheral data
Loads conversations from the database for the given user and environment. Results are cached and emitted via Conversations$.
The environment to filter conversations by
The current user context
If true, reloads even if data is already cached
Optionaloptions: { includeApplicationScoped?: boolean }ProtectedLoadHandles the process of loading multiple entity configs in a single network call via RunViews()
OptionalbypassCache: booleanWhen true, bypasses server-side cache to get fresh data from the database
Loads the projects (conversation folders) for an environment and emits via Projects$. Projects are environment-scoped (not user-scoped) and small, so the full active set is cached. Skips reloading when already loaded for the same environment unless forced.
The environment to filter projects by
The current user context
If true, reloads even if already cached for this environment
ProtectedLoadLoads a single metadata configuration.
The metadata configuration to load
The context user information
OptionalbypassCache: booleanWhen true, bypasses server-side cache to get fresh data from the database
ProtectedLoadHandles the process of loading a single config of type 'dataset'.
OptionalbypassCache: booleanWhen true, bypasses server-side cache to get fresh data from the database.
Uses IMetadataProvider.GetDatasetByName with forceRefresh to skip all cache reads,
then IMetadataProvider.CacheDataset to store fresh results for subsequent non-forced calls.
ProtectedLoadHandles the process of loading a single config of type 'entity'.
OptionalbypassCache: booleanWhen true, bypasses server-side cache to get fresh data from the database
ProtectedMarkRecords a config as successfully loaded with an EMPTY result set. Used when a load failed permanently because the context user lacks Read on the entity: the engine exposes an empty array (not a hang) and is marked loaded so shell boot can complete for a restricted role.
Assigns a conversation to a folder (project), or removes it from its folder when projectId is null. Thin wrapper over SaveConversation that keeps the intent explicit at call sites.
The conversation to move
The target project ID, or null to ungroup
The current user context
true if saved successfully
Reparents a folder (project) under another folder, or to the top level when parentId is null. Callers are responsible for preventing cycles (don't pass a descendant of the folder as its new parent). Updates the cached entity in place and re-emits Projects$.
The folder to move
The new parent folder ID, or null for top level
The current user context
true if saved successfully
ProtectednotifyEmits change notifications for a config whose backing array ALREADY reflects the
entity event — e.g., engine code saved the array's own cached instance in place,
or manually pushed a newly created entity after Save. In those cases
ProcessEntityEvent safely skips the redundant refresh, but the notification
must NOT be skipped: without it, DataChange$ and ObserveProperty subscribers
(and anything derived from them downstream) never learn the array changed and are
stranded on stale state.
Engine subclasses that manually SPLICE a deleted row out of a config's array must call this themselves ('delete') right after splicing — the debounced event handler cannot distinguish "already spliced" from "never matched this config's Filter", so it stays silent for absent rows.
Deliberately does not run AdditionalLoading — the skip paths never did, and engines that maintain their arrays manually own any derived-data updates themselves.
ProtectedNotifyNotify listeners that a data property has changed. Called automatically by HandleSingleViewResult after data refresh and by applyImmediateMutation for array operations. Subclasses can also call this manually when modifying data arrays directly.
The configuration for the property that changed
The current data array
OptionalchangeType: "delete" | "update" | "add" | "refresh"The type of change: 'refresh', 'add', 'update', or 'delete'
OptionalaffectedEntity: BaseEntityFor add/update/delete, the entity that was affected
Returns an Observable for a specific engine array property. Subscribers receive the current array immediately (BehaviorSubject semantics), then re-receive the same array reference whenever the engine mutates it (save, delete, remote-invalidate, refresh).
The BehaviorSubject for a property is lazy-created on first call — engines where no one observes a property pay zero runtime cost.
The name of the backing array property on the engine (e.g. _UserNotifications).
ProtectedOnCalled when another server instance updates cached data that this engine is tracking. Default behavior: reload the affected config from the database.
Engines can override this for custom behavior (e.g., incremental update using the event's CacheChangedEvent.Data payload).
The engine property config whose data changed
The cache change event from the other server
Toggles or sets the pinned status of a conversation.
The conversation ID
Whether the conversation should be pinned
The current user context
true if successful
ProtectedProcessBack-compat single-event wrapper around ProcessEntityEvents. The debounced pipeline delivers full batches to ProcessEntityEvents — override THAT method to change event-processing behavior; this wrapper exists for subclasses/tests that process one event at a time.
ProtectedProcessDoes the actual work of processing all entity events coalesced into one debounce window. Not called directly from the event handler because we first debounce the events, which also introduces a delay that is usually desirable so processing happens outside the scope of any transaction processing that originated the events.
Per matching config, the decision is an OR over the whole batch:
A transiently-failed refresh schedules a bounded retry via scheduleEventRefreshRetry — without it, the consumed debounce event would leave observers permanently stale until an unrelated event arrived.
This is the best method to override if you want to change the actual processing of entity events but do NOT want to modify the debouncing behavior.
Returns true if the specified property loaded successfully during engine startup. Returns false if the property failed to load (e.g., RunView error) or was never loaded. Consumers can use this to detect partial load failures and trigger recovery.
Refreshes all items
Refreshes conversation details by re-running the GetConversationComplete query and surgically merging results into the existing cache. Existing objects that haven't changed keep their references (minimizing Angular re-renders).
If no cache exists yet, falls back to a full load.
The conversation to refresh
The current user context
The updated cache entry
Refreshes a specific item.
The name of the property to refresh
ProtectedRegisterRegisters cross-server cache change callbacks for entity configs. When another server instance updates cached data for an entity this engine tracks, the engine will automatically reload the affected config.
This enables multi-server deployments to keep engine in-memory arrays synchronized without polling. Requires a Redis-backed storage provider with pub/sub enabled (via RedisLocalStorageProvider.StartListening).
The entity configurations to register callbacks for
Removes a dynamic metadata configuration at runtime.
The name of the property to remove
Saves partial updates to a conversation (Name, Description, or any writable field). Loads the entity from DB, applies updates, saves, and updates the in-memory list.
The conversation ID to update
Partial fields to update
The current user context
true if saved successfully
Saves an existing conversation detail entity and updates the cache. Use this instead of calling detail.Save() directly to keep the engine cache in sync.
The conversation detail entity to save (must already be loaded)
true if saved successfully
ProtectedscheduleSchedules a bounded, backed-off retry of a config refresh that failed transiently during entity-event processing. Without this, one failed RunView after a save would permanently strand every observer on stale data — the debounced event is already consumed, so nothing else re-runs the refresh until an unrelated event for the same entity arrives.
At most one retry is pending per property at a time; a retry that succeeds notifies observers through the normal HandleSingleViewResult → NotifyDataChange path. Permission denials never reach here (HandleSingleViewResult marks them loaded-empty).
The config whose refresh failed
1-based attempt number; delays back off linearly (2s, 4s, ...)
Adds or updates an agent run in the cache for a specific detail.
The conversation ID
The detail ID the agent run is associated with
The agent run entity
ProtectedSetInternal method to set the provider when an engine is loaded. Once this engine instance has a provider bound, subsequent calls are no-ops — preventing transient per-request providers from displacing the persistent provider that first bound to this connection. The cache key is the connection (not the object), so the first persistent provider to load an engine for a connection "owns" the engine for that connection's lifetime.
ProtectedSetupThis method is responsible for registering for MJGlobal events and listening for BaseEntity events where those BaseEntity are related to the engine's configuration metadata. The idea is to auto-refresh the releated configs when the BaseEntity is updated.
ProtectedsyncSyncs an entity change to the LocalCacheManager for a config with CacheLocal enabled. This ensures that IndexedDB/localStorage stays in sync with the engine's in-memory array.
Only called for configs WITHOUT Filter/OrderBy (immediate mutation path). Filtered/sorted configs use debounced refresh which handles its own caching.
The configuration for the property being synced
The entity event containing the affected entity and event type
ProtectedTryHelper method for sub-classes to have a single line of code that will make sure the data is loaded before proceeding and will throw an error if not loaded.
Updates a detail entity in the cache. Finds by ID and replaces. If the detail is not in cache, this is a no-op.
The conversation this detail belongs to
The updated detail entity
ProtectedUpgradeUtility method to upgrade an object to a BaseEnginePropertyConfig object.
StaticAssemblePure window assembly over already-loaded rows: exclude → sort by Sequence → summary-boundary fold → tail cap. Extracted from GetAgentContextWindow so SERVER-SIDE callers (agent resolver, cross-turn compaction) can load rows fresh per request — via LoadWindowRowsFresh with the per-request provider and contextUser, applying entity RLS and never populating this engine's process-global detail cache — and still share the exact same fold math the cache-backed client path uses.
Accepts the minimal ConversationWindowSourceRow shape, satisfied by full
entities AND ResultType: 'simple' rows selecting ConversationWindowFields.
Optionaloptions: { excludeDetailIds?: string[]; maxTailMessages?: number }Protected StaticgetReturns the singleton instance of the class. If the instance does not exist, it is created and stored in the Global Object Store. If className is provided it will be used as part of the key in the Global Object Store, otherwise the actual class name will be used. NOTE: the class name used by default is the lowest level of the object hierarchy, so if you have a class that extends another class, the lowest level class name will be used.
OptionalclassName: stringStaticGetReturns the cached engine instance for this engine subclass on the connection the given
provider points to, creating one if none exists yet. Lookup is keyed by the provider's
InstanceConnectionString so multiple provider objects targeting the same connection
share a single cached engine.
StaticLoadLoads a conversation's window source rows FRESH — one RunView through the given provider with the contextUser (entity RLS applies), never touching this engine's process-global detail cache. The single source of the fresh-load query shape (entity name, filter, order, fields) for every server-side caller: the agent resolver's history loader and the cross-turn compaction pass both consume this, so the two can never drift apart. THROWS on load failure — servers must fail loudly rather than proceed against an empty history.
The conversation whose detail rows to load
The requesting user (entity RLS is applied under this user)
Optionalprovider: IMetadataProviderOptional per-request metadata provider; falls back to the global default
The conversation's rows in Sequence order, shaped for AssembleContextWindow
StaticRemoveRemoves all cached engine instances for the given connection. Call this when a connection is being torn down (e.g. multi-tenant client logging out) to release the cached engines' memory eagerly. For normal server operation this is rarely needed — the cache is bounded by (distinct connections × engine classes), which is small.
ConversationEngine provides centralized, reactive caching for conversations, conversation details (messages), and peripheral data (agent runs, artifacts).
This engine is the single source of truth for conversation data across all UI consumers (chat area, sidebar, overlay, etc.). It replaces per-component caching that previously lived in conversation-chat-area component, and other scattered locations.
Usage: