コンテンツにスキップ

06: Per-Turn Loop

このコンテンツはまだ日本語訳がありません。

The per-turn loop executes inside runWithChannelLock(). It iterates through each planned persona turn, building the LLM context, configuring the Discord response sink, executing provider generation, and dispatching post-turn effects.

for (const turn of turnPlan.turns) {
const context = await buildChatTurnContext(turn); // 01
const responseSink = createChatResponseSink(context); // 02
const result = await runGenerationTurn(context, responseSink); // 03
await runPostTurnEffects(context, result); // 04
}
  • Iteration scope: each iteration handles one persona’s reply attempt. Because planChatTurns() places additional matching personas at the front of the channel queue, a single invocation typically processes one persona turn; subsequent personas execute as distinct replayed turns.
  • Shared closure: stages 01 through 04 operate on the ChatTurnContext closure. Stage 01 constructs it, stage 02 attaches the response sink, stage 03 records attempt metadata, and stage 04 consumes the result.
  • Turn independence: each iteration executes independently. Subsequent turns observe the Discord-visible messages sent by earlier turns through channel history rather than shared memory.
#StageGuideOwnership
01buildChatTurnContext06.1: Build ContextFetches message history, applies annotations, and builds prompt items.
02createChatResponseSink06.2: Response SinkResolves webhook or channel delivery targets and lifecycle callbacks.
03runGenerationTurn06.3: Generation TurnDrives model fallback attempts, key rotation, and fallback text continuation.
04runPostTurnEffects06.4: Post-Turn EffectsDispatches empty retries, quota settlement, STM caching, thought logs, and boomerangs.