/** * Society Wire Protocol (SWP) v1.0 — State of the Art * * Every message on the Society network is wrapped in an SWP envelope: * - Signed with the sender's Ed25519 key * - Includes TTL for expiration * - ULID for unique, time-sortable IDs * - Replay protection via (from.did, id) dedup * - Optional end-to-end encryption */ import { type Identity } from './identity.js'; export declare const SWP_VERSION = "swp/1.0"; export declare const DEFAULT_TTL_MS = 600000; export declare const MAX_PAYLOAD_BYTES: number; export type MessageType = 'presence.heartbeat' | 'presence.capabilities_update' | 'presence.status_change' | 'chat.msg' | 'chat.reaction' | 'chat.edit' | 'chat.delete' | 'coc.open' | 'coc.plan' | 'coc.assign' | 'coc.submit' | 'coc.review' | 'coc.merge' | 'coc.close' | 'coc.handoff' | 'coc.feedback' | 'coc.cancel' | 'adapter.register' | 'adapter.offer' | 'adapter.heartbeat' | 'adapter.capabilities' | 'mission.start' | 'mission.pause' | 'mission.resume' | 'mission.stop' | 'mission.checkpoint' | 'mission.alert' | 'capsule.publish' | 'capsule.import' | 'capsule.request' | 'federation.peer.request' | 'federation.peer.accept' | 'federation.peer.reject' | 'federation.peer.revoke' | 'federation.bridge.open' | 'federation.bridge.close' | 'federation.bridge.sync' | 'persona.memory.upsert' | 'persona.memory.delete' | 'persona.edge.upsert' | 'persona.claim.upsert' | 'persona.claim.revoke' | 'persona.zkp.proof' | 'persona.preference.update' | 'persona.sync.delta' | 'persona.sync.ack' | 'persona.capability.revoke' | 'persona.capability.attenuate' | 'latent.thought' | 'latent.architecture' | 'latent.query' | 'latent.merge' | 'swarm.role.assign' | 'swarm.reallocation' | 'swarm.window.open' | 'swarm.window.close' | 'swarm.affinity.update' | 'cot.stream.start' | 'cot.stream.token' | 'cot.stream.insight' | 'cot.stream.question' | 'cot.stream.answer' | 'cot.stream.branch' | 'cot.stream.merge' | 'cot.stream.end' | 'security.key_exchange' | 'knowledge.context_sync' | 'knowledge.sync' | 'artifact.offer' | 'artifact.request' | 'artifact.block' | 'identity.proof' | 'system.ping' | 'system.pong' | 'system.error'; export interface SwpSender { did: string; name: string; } export interface SwpEnvelope { v: string; id: string; t: MessageType; room: string; from: SwpSender; ts: number; ttl: number; body: Record; sig: string; enc?: { algorithm: 'x25519-xsalsa20-poly1305'; ephemeral_public_key: string; nonce: string; }; } export interface PresenceBody { status: 'online' | 'busy' | 'running' | 'offline' | 'away'; caps: string[]; load?: number; specialties?: string[]; adapter_endpoint?: string; reputation_score?: number; peer_id?: string; } export interface CapabilitiesUpdateBody { added_caps: string[]; removed_caps: string[]; new_specialties?: string[]; } export interface ChatMsgBody { text: string; reply_to?: string; mentions?: string[]; attachments?: string[]; formatting?: 'markdown' | 'plain' | 'html'; encrypted?: boolean; } export interface ChatReactionBody { message_id: string; emoji: string; action: 'add' | 'remove'; } export interface ChatEditBody { message_id: string; new_text: string; edit_timestamp: number; } export interface CocOpenBody { chain_id?: string; goal: string; template_id?: string; priority?: 'low' | 'normal' | 'high' | 'critical'; tags?: string[]; timeout_ms?: number; privacy_level?: 'public' | 'encrypted' | 'private'; } export interface CocDagNode { step_id: string; kind: 'task' | 'review' | 'merge' | 'decision' | 'synthesis' | 'verification'; title: string; description?: string; depends_on: string[]; requirements?: StepRequirements; timeout_ms?: number; retries?: number; max_retries?: number; artifacts_expected?: string[]; } export interface StepRequirements { capabilities: string[]; min_reputation?: number; max_cost?: number; required_providers?: string[]; } export interface CocPlanBody { chain_id: string; dag: CocDagNode[]; planner_version: string; plan_metadata?: { estimated_tokens?: number; confidence?: number; reasoning?: string; approach?: string; }; } export interface CocAssignBody { chain_id: string; step_id: string; assignee_did: string; lease_ms: number; lease_type: 'exclusive' | 'shared'; constraints?: { max_cost?: number; required_providers?: string[]; deadline?: number; }; } export interface Artifact { artifact_id: string; artifact_type: string; content_hash: string; size_bytes: number; encoding: 'utf8' | 'base64' | 'binary'; content?: string; uri?: string; metadata?: Record; provenance?: string[]; } export interface CocSubmitBody { chain_id: string; step_id: string; status: 'completed' | 'failed' | 'partial' | 'cancelled'; memo: string; artifacts: Artifact[]; metrics?: { tokens_used?: number; latency_ms?: number; cost?: number; retries?: number; }; } export interface CocReviewBody { chain_id: string; step_id: string; decision: 'approved' | 'rejected' | 'needs_revision' | 'escalated'; notes: string; suggestions?: string[]; approval_weight?: number; quality_score?: number; } export interface CocMergeBody { chain_id: string; summary: string; outputs: string[]; quality_score?: number; lessons_learned?: string[]; metrics?: { total_steps: number; completed_steps: number; failed_steps: number; total_latency_ms: number; total_cost?: number; }; } export interface CocCloseBody { chain_id: string; reason: 'completed' | 'cancelled' | 'timeout' | 'failed'; final_report?: string; } export interface CocHandoffBody { chain_id: string; step_id: string; previous_assignee: string; new_assignee: string; reason: 'timeout' | 'failure' | 'load_balancing' | 'capability_mismatch' | 'agent_offline' | 'worker_offline' | 'worker_overloaded' | 'scheduler_rebalance'; handoff_count: number; } export interface CocFeedbackBody { chain_id: string; step_id: string; feedback_type: 'clarification' | 'additional_request' | 'correction' | 'praise'; message: string; priority: 'low' | 'normal' | 'high' | 'critical'; from_did: string; } export interface CocCancelBody { chain_id: string; step_id?: string; reason: string; cancelled_by: string; } export interface AdapterCapabilities { specialties?: string[]; kinds?: string[]; max_concurrency?: number; capabilities?: string[]; mission_tags?: string[]; room_id?: string; } export interface AdapterProfile { adapter_id: string; runtime: 'claude-code' | 'nanobot' | 'ollama' | 'openai' | 'custom' | 'docker'; version: string; display_name: string; description?: string; specialties: string[]; kinds: string[]; max_concurrency: number; endpoint: string; auth_type: 'none' | 'token' | 'mtls'; pricing?: { per_token?: number; per_request?: number; currency: 'USD'; }; capabilities?: string[]; owner_did?: string; room_id?: string; peer_id?: string; host_id?: string; mission_tags?: string[]; } export interface AdapterRegisterBody { profile: AdapterProfile; } export interface AdapterOfferBody { adapter_id: string; available_kinds: string[]; current_load: number; estimated_wait_ms: number; active_tasks: number; worker_did?: string; room_id?: string; } export interface AdapterHeartbeatBody { adapter_id: string; active_tasks: number; queue_depth: number; health: 'healthy' | 'degraded' | 'unhealthy'; worker_did?: string; room_id?: string; metrics?: { avg_task_duration_ms?: number; success_rate?: number; }; } export interface MissionStartBody { mission_id: string; goal: string; template_id?: string; cadence_ms: number; } export interface MissionPauseBody { mission_id: string; } export interface MissionResumeBody { mission_id: string; } export interface MissionStopBody { mission_id: string; reason?: string; } export interface MissionCheckpointBody { mission_id: string; checkpoint_id: string; summary: string; } export interface MissionAlertBody { mission_id: string; level: 'info' | 'warning' | 'critical'; message: string; } export interface CapsulePublishBody { capsule_id: string; title: string; description: string; chain_id: string; room_id: string; tags: string[]; content_hash: string; pointers: { ipfs_cid?: string; url?: string; magnet?: string; }; } export interface CapsuleImportBody { capsule_id: string; import_mode: 'resume' | 'fork' | 'reference'; } export interface FederationPeeringPolicyBody { allowed_types: string[]; max_rate: number; privacy_mode: 'metadata-only' | 'summary' | 'full'; allowed_rooms?: string[]; blocked_rooms?: string[]; } export interface FederationPeerRequestBody { peering_id: string; source_federation_id: string; source_federation_did: string; target_federation_did: string; requested_by: string; policy: FederationPeeringPolicyBody; } export interface FederationPeerAcceptBody { peering_id: string; accepted_by: string; reason?: string; } export interface FederationPeerRejectBody { peering_id: string; rejected_by: string; reason?: string; } export interface FederationPeerRevokeBody { peering_id: string; revoked_by: string; reason?: string; } export interface FederationBridgeOpenBody { bridge_id: string; peering_id: string; local_federation_id: string; local_room_id: string; remote_room_id: string; rules: FederationPeeringPolicyBody; } export interface FederationBridgeCloseBody { bridge_id: string; peering_id: string; reason?: string; } export interface FederationBridgeSyncBody { bridge_id: string; peering_id: string; source_federation_id: string; target_federation_did: string; direction: 'in' | 'out'; cursor?: string; envelope: SwpEnvelope; } export interface ValidationResult { valid: boolean; error?: string; code?: 'invalid_version' | 'expired' | 'oversized' | 'invalid_signature' | 'replay_detected'; } export interface CotStreamStartBody { stream_id: string; chain_id?: string; step_id?: string; goal: string; model?: string; parent_stream_id?: string; } export interface CotStreamTokenBody { stream_id: string; seq: number; token: string; token_type: 'reasoning' | 'observation' | 'hypothesis' | 'conclusion' | 'evidence' | 'critique'; confidence?: number; domain?: string; references?: string[]; } export interface CotStreamInsightBody { stream_id: string; insight_id: string; title: string; content: string; insight_type: 'discovery' | 'pattern' | 'contradiction' | 'connection' | 'gap' | 'confirmation'; confidence: number; supporting_evidence: string[]; related_streams?: string[]; auto_create_card?: boolean; } export interface CotStreamQuestionBody { stream_id: string; question_id: string; question: string; question_type: 'clarification' | 'exploration' | 'validation' | 'critique' | 'suggestion'; context: string; target_did?: string; urgency: 'low' | 'normal' | 'high'; } export interface CotStreamAnswerBody { stream_id: string; question_id: string; answer: string; confidence: number; references?: string[]; } export interface CotStreamBranchBody { stream_id: string; parent_stream_id: string; branch_reason: string; hypothesis: string; } export interface CotStreamMergeBody { stream_id: string; merged_streams: string[]; synthesis: string; consensus_level: number; } export interface CotStreamEndBody { stream_id: string; status: 'completed' | 'paused' | 'abandoned' | 'merged'; summary: string; insights_generated: number; tokens_total: number; duration_ms: number; } export declare function createEnvelope(identity: Identity, type: MessageType, room: string, body: Record, ttl?: number): SwpEnvelope; export declare class ReplayCache { private cache; private maxSize; private ttlMs; private cleanupInterval; constructor(maxSize?: number, ttlMs?: number); private cleanup; /** * Check if message is a replay * @returns true if replay detected, false if new message */ check(envelope: SwpEnvelope): boolean; get size(): number; clear(): void; } export declare const globalReplayCache: ReplayCache; export interface ValidationOptions { skipReplayCheck?: boolean; replayCache?: ReplayCache; } export declare function validateEnvelope(envelope: SwpEnvelope, options?: ValidationOptions): ValidationResult; export declare function serializeEnvelope(envelope: SwpEnvelope): Uint8Array; export declare function deserializeEnvelope(data: Uint8Array): SwpEnvelope; export declare function isCocMessage(type: MessageType): boolean; export declare function isPresenceMessage(type: MessageType): boolean; export declare function isAdapterMessage(type: MessageType): boolean; export declare function isFederationMessage(type: MessageType): boolean; export declare function isPersonaMessage(type: MessageType): boolean; export declare function isMissionMessage(type: MessageType): boolean; export declare function getMessagePriority(type: MessageType): 'high' | 'normal' | 'low'; //# sourceMappingURL=swp.d.ts.map