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, handle tool calls, 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

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)

If exported, onToolCall allows the strategy to handle a tool call inside the strategy runtime.

If this hook is not exported, AfferLab falls back to the host's default tool execution path.


onCleanup

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

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


onError

If exported, onError runs when a worker-side request throws an error.

On this page