/** * StreamInterceptor — wraps the Anthropic streaming API to pipe tokens * through the real-time verification pipeline transparently. * * Consumers get a verified text stream: code with bugs gets flagged * (and optionally corrected) mid-generation. * * Phase 2, Milestone 3 of the 10x roadmap. */ import type { EntropyDetectorOptions } from './entropy-detector.js'; import type { CheckSeverity, StreamingVerifierStats, VerificationEvent } from './types.js'; export interface VerifiedStreamOptions { /** Anthropic API key (falls back to ANTHROPIC_API_KEY env) */ apiKey?: string; /** Model to use. Default: 'claude-sonnet-4-20250514' */ model?: string; /** System prompt for code generation */ systemPrompt?: string; /** User prompt (the code request) */ userPrompt: string; /** Language of the code being generated */ language: string; /** Max tokens. Default: 4096 */ maxTokens?: number; /** Project path for loading learned rules */ projectPath?: string; /** Callback for each text chunk */ onText?: (text: string) => void; /** Callback for each verification event */ onVerification?: (event: VerificationEvent) => void; /** Callback when a correction is triggered */ onCorrection?: (info: { finding: VerificationEvent; generatedSoFar: string; }) => void; /** Enable auto-correction. Default: false for MVP */ autoCorrect?: boolean; /** Min severity for auto-correction. Default: 'high' */ minCorrectionSeverity?: CheckSeverity; /** * LLM provider. Default: 'anthropic'. * Use 'openai' for logprob-based entropy hallucination detection. */ provider?: 'anthropic' | 'openai'; /** OpenAI API key (falls back to OPENAI_API_KEY env). Required when provider='openai'. */ openaiApiKey?: string; /** Entropy detector options. Only active when provider='openai'. */ entropyDetector?: EntropyDetectorOptions; } export interface VerifiedStreamResult { /** The full generated text */ text: string; /** All verification events */ events: VerificationEvent[]; /** Only the findings (FAIL events) */ findings: VerificationEvent[]; /** Verifier statistics */ stats: StreamingVerifierStats; /** API usage */ inputTokens: number; outputTokens: number; /** Total wall time */ durationMs: number; /** * Collected per-token logprob data from the provider stream. * Present when provider supports logprobs (e.g., OpenAI), undefined otherwise. * Can be passed to BAS calculator or stream_verify MCP tool for analysis. */ collectedLogprobs?: Array<{ token: string; top_logprobs: Array<{ token: string; logprob: number; }>; }>; } /** * Create and run a verified stream using the Anthropic API. * Returns when generation is complete. */ export declare function createVerifiedStream(options: VerifiedStreamOptions): Promise; /** * Create a verified stream using the specified provider. * - provider='anthropic' (default): pattern checks only * - provider='openai': pattern checks + entropy hallucination detection */ export declare function createVerifiedStreamAuto(options: VerifiedStreamOptions): Promise; /** * Simulate a verified stream by pushing an array of token strings through * the verification pipeline. Same logic as createVerifiedStream but without * the Anthropic client. Useful for deterministic testing. */ export declare function createVerifiedStreamFromTokens(tokens: string[], options: Omit): VerifiedStreamResult;