/** * Society Protocol — Collaborative Chain-of-Thought (CoT) Streaming v1.0 * * Killer feature: Real-time distributed reasoning between agents. * Each agent publishes reasoning tokens, insights, questions and answers * via GossipSub, enabling "pair programming between LLMs" — agents can * absorb each other's reasoning in real-time and build on it. * * Key concepts: * - CoT Stream: A reasoning session tied to a room/chain/step * - Tokens: Typed reasoning chunks (reasoning, observation, hypothesis, etc.) * - Insights: Distilled discoveries that can auto-create Knowledge Cards * - Questions/Answers: Inter-agent Socratic dialogue * - Branches: Divergent reasoning paths explored in parallel * - Merges: Synthesis of multiple reasoning branches * * Architecture: * Agent A ──┐ ┌── Agent C (absorbs A+B reasoning) * ├── GossipSub topic ─┤ * Agent B ──┘ └── Agent D (branches from A's insight) */ import { EventEmitter } from 'events'; import { type Identity } from './identity.js'; import { type RoomManager } from './rooms.js'; import { type KnowledgePool } from './knowledge.js'; import type { CotStreamTokenBody, CotStreamInsightBody, CotStreamQuestionBody, CotStreamEndBody } from './swp.js'; export interface CotStream { stream_id: string; room_id: string; chain_id?: string; step_id?: string; goal: string; model?: string; owner_did: string; parent_stream_id?: string; status: 'active' | 'paused' | 'completed' | 'abandoned' | 'merged'; started_at: number; ended_at?: number; token_count: number; insight_count: number; question_count: number; branches: string[]; merged_into?: string; } export interface CotInsight { insight_id: string; stream_id: string; title: string; content: string; insight_type: CotStreamInsightBody['insight_type']; confidence: number; supporting_evidence: string[]; author_did: string; created_at: number; knowledge_card_id?: string; } export interface CotQuestion { question_id: string; stream_id: string; question: string; question_type: CotStreamQuestionBody['question_type']; context: string; asked_by: string; target_did?: string; urgency: 'low' | 'normal' | 'high'; asked_at: number; answered: boolean; answers: CotAnswer[]; } export interface CotAnswer { answer_id: string; question_id: string; stream_id: string; answer: string; confidence: number; references: string[]; answered_by: string; answered_at: number; } export interface CotStreamConfig { /** Max tokens to buffer per stream before flushing */ maxBufferSize?: number; /** Auto-create knowledge cards from insights */ autoCreateCards?: boolean; /** Minimum confidence to auto-create a card */ autoCardMinConfidence?: number; /** Max concurrent streams per room */ maxStreamsPerRoom?: number; /** Token broadcast interval (ms) — batch tokens for efficiency */ batchIntervalMs?: number; } export interface ReasoningContext { stream_id: string; tokens: CotStreamTokenBody[]; insights: CotInsight[]; questions: CotQuestion[]; goal: string; branches: string[]; } export declare class CotStreamEngine extends EventEmitter { private identity; private rooms; private knowledge?; private streams; private insights; private questions; private tokenBuffers; private roomStreams; private batchTimers; private config; constructor(identity: Identity, rooms: RoomManager, knowledge?: KnowledgePool, config?: CotStreamConfig); /** * Start a new reasoning stream — begins broadcasting thinking tokens. */ startStream(roomId: string, goal: string, options?: { chain_id?: string; step_id?: string; model?: string; parent_stream_id?: string; }): Promise; /** * Emit a reasoning token — the core of collaborative thinking. * Tokens are batched for network efficiency. */ emitToken(streamId: string, token: string, tokenType: CotStreamTokenBody['token_type'], options?: { confidence?: number; domain?: string; references?: string[]; }): Promise; /** * Publish an insight — a distilled discovery from reasoning. * Can auto-create a Knowledge Card if configured. */ publishInsight(streamId: string, title: string, content: string, insightType: CotStreamInsightBody['insight_type'], options?: { confidence?: number; supporting_evidence?: string[]; related_streams?: string[]; auto_create_card?: boolean; }): Promise; /** * Ask a question to other agents in the room — Socratic dialogue. */ askQuestion(streamId: string, question: string, questionType: CotStreamQuestionBody['question_type'], options?: { context?: string; target_did?: string; urgency?: 'low' | 'normal' | 'high'; }): Promise; /** * Answer a question from another agent. */ answerQuestion(streamId: string, questionId: string, answer: string, options?: { confidence?: number; references?: string[]; }): Promise; /** * Branch the reasoning — explore a divergent hypothesis in parallel. */ branchStream(parentStreamId: string, hypothesis: string, branchReason: string): Promise; /** * Merge multiple reasoning branches into a synthesis. */ mergeStreams(targetStreamId: string, sourceStreamIds: string[], synthesis: string, consensusLevel: number): Promise; /** * End a reasoning stream. */ endStream(streamId: string, status: CotStreamEndBody['status'], summary: string): Promise; /** * Get the full reasoning context for a stream — enables agents * to "absorb" another agent's thinking and build on it. */ getReasoningContext(streamId: string): ReasoningContext | null; /** * Get all active streams in a room — see what everyone is thinking. */ getRoomStreams(roomId: string): CotStream[]; /** * Get active streams for a specific CoC chain. */ getChainStreams(chainId: string): CotStream[]; /** * Get all unanswered questions targeting this agent. */ getPendingQuestions(): CotQuestion[]; /** * Get the reasoning tree starting from a root stream, * including all branches and merges. */ getReasoningTree(rootStreamId: string): { root: CotStream; branches: CotStream[]; insights: CotInsight[]; depth: number; } | null; private bindEvents; private handleRemoteStreamStart; private handleRemoteToken; private handleRemoteInsight; private handleRemoteQuestion; private handleRemoteAnswer; private handleRemoteBranch; private handleRemoteMerge; private handleRemoteStreamEnd; private flushTokenBuffer; private isRemoteToken; private getDefaultSpaceId; private insightTypeToKnowledgeType; getStream(streamId: string): CotStream | undefined; getInsight(insightId: string): CotInsight | undefined; getQuestion(questionId: string): CotQuestion | undefined; getActiveStreams(): CotStream[]; getStats(): { totalStreams: number; activeStreams: number; totalInsights: number; totalQuestions: number; answeredQuestions: number; }; destroy(): void; } //# sourceMappingURL=cot-stream.d.ts.map