Shared state between QiForge plugins

How one QiForge plugin exposes read-only values for other plugins to read without coupling their code: a flat namespace, a read-only contract, lazy accessors.

Shared state lets one plugin read live data from another without importing its code. A producer plugin declares an accessor, and consumer plugins read it through RuntimeContext.shared. It is a typed, read-only channel, and it is the only sanctioned way for two plugins to share live data. Plugins do not import each other's classes or call each other's services.

When to use it

Good fitBad fit
A derived value other plugins might want (for example userProfile, lastWeatherQuery, activePageId)Mutable state with multiple writers
Cheap reads that abstract graph-state field namesLarge blobs consumers might read but ignore
Stable shape worth declaring on SharedAccessors for typed readsPlugin internals other plugins shouldn't see

If only your plugin reads it, keep it private.

The contract

  • Read-only. Consumers never mutate the value. Producers update their own internal state through tool handlers or middleware, and the accessor only exposes a view.
  • Flat namespace. Keys live in a single global record. Two plugins registering the same key fail boot, with no quiet overrides.
  • Lazy. Each accessor is a function (state, runCtx) => value. It runs every time a consumer reads. Keep reads cheap, and cache in the producer if derivation is expensive.
  • Optional. A consumer must assume the producer may not be installed: rtCtx.shared.someKey can be undefined. Pair shared state with softDependsOn so the soft dependency is explicit in the app.plugins.status() report.

Typed reads

SharedAccessors is an open interface. Producer plugins extend it through TypeScript declaration merging, so consumers see the right type on rtCtx.shared.someKey. Without merging, the key types as unknown and consumers cast.

Use declaration merging when the shape is stable, and skip it for ad-hoc keys.

Source: SharedAccessors.