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.
Loop sequence
Section titled “Loop sequence”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
ChatTurnContextclosure. 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.
Stage index
Section titled “Stage index”| # | Stage | Guide | Ownership |
|---|---|---|---|
| 01 | buildChatTurnContext | 06.1: Build Context | Fetches message history, applies annotations, and builds prompt items. |
| 02 | createChatResponseSink | 06.2: Response Sink | Resolves webhook or channel delivery targets and lifecycle callbacks. |
| 03 | runGenerationTurn | 06.3: Generation Turn | Drives model fallback attempts, key rotation, and fallback text continuation. |
| 04 | runPostTurnEffects | 06.4: Post-Turn Effects | Dispatches empty retries, quota settlement, STM caching, thought logs, and boomerangs. |
Cross-references
Section titled “Cross-references”- Context-Build Pipeline: prompt composition, RAG, and memory hydration.
- Tool-Loop Pipeline: function calling, iteration limits, and tool execution.
- Provider Pipeline: streaming HTTP connections, token limits, and Discord delivery.
- Memory Pipeline: short-term memory caching and storage.