Skip to content

oracular / observe

Function: observe() ​

ts
function observe(state, event): EngineState;

Defined in: observe.ts:49

Feeds one event to the engine.

For every intent the event is a signal of: decay its evidence to event.at, then add the signal's effective weight to α (positive) or β (negative) and remember the signal for this episode. Repeats within an episode saturate: the n-th occurrence of a signal adds w × log2(1 + 1/n), counted since the episode started or the intent's outcome last arrived. That is the default when config.episodeTimeout is set, and IntentConfig.saturate overrides it either way; otherwise every occurrence adds all of w. For every intent the event is the outcome of: give the remembered signals a hit, count the episode toward the base rate, credit the closed episodes still waiting under IntentConfig.attribution by 2^(−age / halfLife) each, record lastOutcomeAt, and (by default) reset the evidence to the prior. Any event opens or extends the current episode; an idle gap longer than config.episodeTimeout closes the previous episode first.

Parameters ​

ParameterTypeDescription
stateEngineStateThe current state. Not mutated.
eventIntentEventname is matched against every intent's signals and outcome; at is the event's timestamp, in the same unit as your durations; props are accepted and ignored, and never stored.

Returns ​

EngineState

The next state. Intents the event does not touch keep their stored numbers; their decay is applied when they are next read or updated.

Throws ​

TypeError when state is not an engine state, or the event has no non-empty name, no finite at, or a props that is not a plain object.

Example ​

ts
let state = createState(config);
state = observe(state, { name: 'pricing.viewed', at: Date.now() });
state = observe(state, { name: 'upgrade.completed', at: Date.now() }); // outcome

Released under the MIT License.