AppGitHub
Developing Strategies

Strategy File

A AfferLab strategy is a single TypeScript module with exported metadata and hooks.

Required Structure

If you download the starter template from AfferLab, keep both files from the zip in the same folder. The .d.ts file provides editor types for @afferlab/strategy-sdk and for ctx.

If your strategy does not define any config fields, omit configSchema entirely. Do not use configSchema: []. An empty schema breaks TypeScript inference and can cause hook parameters like ctx to lose their types.

At minimum, a strategy file must export:

  • meta

  • onContextBuild

  • configSchema (optional)

onContextBuild is the only required hook.

Minimal Example

import { defineStrategy } from '@afferlab/strategy-sdk'

export default defineStrategy({
    meta: {
        name: 'Base',
        description: 'Minimal',
        version: '0.2.2',
        features: { memoryCloud: false },
    },

    configSchema: [
        {
            key: 'historyDepth',
            type: 'number',
            default: 10,
            min: 1,
            max: 20,
        },
        {
            key: 'systemPrompt',
            type: 'text',
            default: 'You are a helpful assistant.',
        },
    ],

    onContextBuild(ctx) {
        const history = ctx.history.recent(ctx.config.historyDepth)

        ctx.slots.add('system', ctx.config.systemPrompt, {
            priority: 3,
        })

        ctx.slots.add('history', history, {
            priority: 1,
        })

        ctx.slots.add('input', ctx.input, {
            priority: 2,
        })

        return {
            prompt: { messages: ctx.slots.render() },
        }
    },
})

Optional Exports

The worker loader currently recognizes:

  • onInit
  • onTurnEnd
  • onCloudAdd
  • onCloudRemove
  • onReplayTurn
  • onCleanup

You can export them through a hooks object. For upcoming hooks, see the lifecycle documentation.

On this page