@hivetechs/hive-ai
Version:
Real-time streaming AI consensus platform with HTTP+SSE MCP integration for Claude Code, VS Code, Cursor, and Windsurf - powered by OpenRouter's unified API
122 lines (121 loc) • 4.66 kB
TypeScript
/**
* Consensus Engine - SOURCE_OF_TRUTH Implementation
*
* Core 4-stage consensus pipeline per exact design specifications.
* Each complete cycle (4 stages) counts as one conversation against user's daily limit.
*/
import { OpenRouterMessage } from './openrouter-client.js';
declare global {
var lastD1ConversationCount: number | undefined;
var lastD1DailyLimit: number | undefined;
}
export interface ConsensusStageResult {
stageId: string;
stageName: string;
question: string;
answer: string;
model: string;
conversationId: string;
timestamp: string;
usage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
analytics?: {
duration: number;
cost: number;
provider: string;
modelInternalId: string;
qualityScore: number;
errorCount: number;
fallbackUsed: boolean;
rateLimitHit: boolean;
retryCount: number;
startTime: string;
endTime: string;
memoryUsage?: number;
features: {
streaming: boolean;
routing_variant: string;
optimization_applied?: boolean;
};
};
}
export interface StreamingConsensusCallbacks {
onStageStart?: (stageName: string, model?: string) => void;
onStageChunk?: (stageName: string, chunk: string, totalContent: string) => void;
onStageProgress?: (stageName: string, progress: {
tokens: number;
estimatedTotal?: number;
percentage: number;
}) => void;
onStageComplete?: (stageName: string, result: ConsensusStageResult) => void;
onError?: (stageName: string, error: Error) => void;
}
/**
* Run a single consensus stage per SOURCE_OF_TRUTH design
* Function: runConsensusStage(stageName, question, previousAnswer, model, conversationId)
*/
export declare function runConsensusStage(stageName: string, question: string, previousAnswer: string | null, model: string, conversationId: string, enableStreaming?: boolean, callbacks?: StreamingConsensusCallbacks, modelInternalId?: number): Promise<ConsensusStageResult>;
/**
* Build stage-specific messages per SOURCE_OF_TRUTH design
* CRITICAL: "Original user question + Stage X answer (as supplemental knowledge)"
*/
export declare function buildStageMessages(stageName: string, question: string, previousAnswer: string | null): Promise<OpenRouterMessage[]>;
/**
* Read previous stage result from SQLite per SOURCE_OF_TRUTH design
*/
export declare function getPreviousStageResult(conversationId: string, stageName: string): Promise<string | null>;
/**
* Run full 4-stage consensus pipeline per SOURCE_OF_TRUTH design
* Function: runConsensusPipeline(question, profileId, conversationId)
*
* SECURITY: Now includes server-side validation through D1 backend
*/
export interface ConsensusOutputOptions {
outputFormat?: 'sse' | 'stdio' | 'silent';
enableProgress?: boolean;
showTokenProgress?: boolean;
}
export declare function runConsensusPipeline(question: string, conversationId: string, modeOverride?: string | null, outputOptions?: ConsensusOutputOptions, enableStreaming?: boolean, callbacks?: StreamingConsensusCallbacks): Promise<string>;
/**
* Validate consensus prerequisites per SOURCE_OF_TRUTH design
*/
export declare function validateConsensusPrerequisites(): Promise<{
valid: boolean;
errors: string[];
}>;
/**
* Store conversation knowledge with improvement patterns tracking
* Enhanced to track what each stage improves for future learning
*/
export declare function storeConversationKnowledge(conversationId: string, question: string, curatorOutput: string): Promise<void>;
/**
* Verify that conversation was properly stored
*/
export declare function checkIfConversationStored(conversationId: string): Promise<boolean>;
/**
* Enhanced streaming pipeline function (alternative interface)
* Provides enhanced analytics and streaming support for clients that need it
*/
export declare function runEnhancedConsensusPipeline(question: string, profileNameOrId?: string, enableStreaming?: boolean, callbacks?: StreamingConsensusCallbacks): Promise<{
conversationId: string;
stages: ConsensusStageResult[];
finalAnswer: string;
totalUsage?: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
analytics?: {
totalCost: number;
totalDuration: number;
averageQuality: number;
totalErrors: number;
totalFallbacks: number;
totalRateLimits: number;
providersUsed: string[];
stageBreakdown: any[];
};
}>;