/** * Phase 8: TurnPipeline — 5-stage pipeline architecture for Runtime.stream(). * * Pipeline stages: * 1. INTAKE — Session load, triage routing, input processors, user message * 2. CONTEXT GATHER — autoRetrieve + extraction + memory + routes (parallel) * 3. CONTEXT ASSEMBLE — System prompt + single context manager pass * 4. AGENT EXECUTE — Routing, streamText / FlowManager, tool loop, handoffs * 5. POST-STREAM — done event, persistence (blocking or deferred) * * Each stage is implemented in its own file. Runtime.ts delegates to these stages * where possible, while maintaining backward compatibility. */ import type { AgentConfig, AgentRoute, ExtractionSnapshot, HarnessStreamPart, RunContext } from '../../types/index.js'; import type { InjectionQueue } from '../InjectionQueue.js'; /** Output of the context gather stage (Stage 2). */ export interface ContextGatherResult { /** Events collected from autoRetrieve to emit after gather completes. */ autoRetrieveEvents: HarnessStreamPart[]; /** Auto-context for system prompt injection. */ autoContext?: { label: string; text: string; }; /** Extraction snapshot for inclusion in system prompt. */ extractionSnapshot: ExtractionSnapshot | null; /** Pre-fetched long-term memory context. */ preloadedMemory: string | null; /** Active routes after condition evaluation (for triage agents). */ activeRoutes?: AgentRoute[]; /** Knowledge retrieval results from the KnowledgeProvider (Layers 1-3). */ knowledgeContext?: { label: string; text: string; }; /** Events from knowledge retrieval to emit after gather completes. */ knowledgeEvents?: HarnessStreamPart[]; } /** Output of the context assemble stage (Stage 3). */ export interface ContextAssembleResult { /** Fully assembled system prompt. */ systemPrompt: string; } /** Generic pipeline stage interface. */ export interface PipelineStage { run(input: TInput): AsyncGenerator; } /** Configuration for a full turn pipeline execution. */ export interface TurnPipelineConfig { /** Agent resolved for this turn. */ agent: AgentConfig; /** Runtime context for this turn. */ context: RunContext; /** User input after processing. */ input: string; /** Policy injection queue for this turn. */ injectionQueue: InjectionQueue; /** Abort signal for cancellation. */ abortSignal?: AbortSignal; }