AppGitHub
Developing Strategies

Lifecycle Hooks

Strategy hooks are invoked by the worker runtime during different phases of a conversation lifecycle.

Overview

Strategies in AfferLab can export a small set of lifecycle hooks.

These hooks are invoked by the worker runtime at different stages of a conversation, allowing strategies to control prompt construction, write memory, and perform cleanup.


onContextBuild

This is the required hook.

It runs before the host starts the model stream and must return the prompt payload for the turn.

Most strategy logic begins here.


onInit

If exported, onInit runs when the worker loads the strategy for runtime use.

This hook runs once per worker instance and can be used to initialize worker-local state.

It is not related to the settings page or strategy installation.


onTurnEnd

If exported, onTurnEnd runs after the host finishes writing the assistant message and prepares the turn-end context.

This is typically used for:

  • memory writes
  • background updates
  • cleanup-style post-processing

onCloudAdd

If exported, onCloudAdd runs after a Memory Cloud asset is successfully added to the current conversation.

This hook receives the normal strategy context plus a small payload:

type CloudAddPayload = {
  assetId: string
}

Use it when a strategy wants to react to newly added assets without changing the base Cloud storage flow.

The hook is observational. The asset is already stored before the hook runs.


onCloudRemove

If exported, onCloudRemove runs after a Memory Cloud asset is successfully removed from the current conversation.

This hook receives:

type CloudRemovePayload = {
  assetId: string
}

Use it for strategy-level cleanup or bookkeeping tied to Cloud asset removal.

The hook runs after deletion, so strategies should treat the payload as an event notification rather than a request to inspect the removed asset.


onReplayTurn

If exported, onReplayTurn runs when the host replays existing turns during a strategy replay job.

Replay jobs may occur when a conversation switches strategies or when background reindexing is required.


onToolCall (coming soon)

This hook is reserved for future tool-capable flows.

It is not part of the supported v0.1 conversation flow yet.


onCleanup

If exported, onCleanup runs when the worker instance is disposed.

This hook can be used to release worker-local resources.

On this page