/** * Society Protocol — Demand-Driven Agent Spawner * * Inspired by: * - AutoAgents (arXiv:2309.17288): Dynamic role generation per task * - MaAS (arXiv:2502.04180): Per-query architecture sampling from supernet * - IoA (arXiv:2505.07176): Ephemeral team assembly + dissolution * - DyLAN (arXiv:2310.02170): Agent importance scoring for selection * * Given a request, the DemandSpawner: * 1. Uses CapabilityRouter to determine roles needed * 2. Tries to select existing agents from the SwarmController pool * 3. Spawns ephemeral agents (Ollama, HTTP, Docker) for missing roles * 4. Opens a Chain of Collaboration for the team * 5. Monitors execution and collects results * 6. Dissolves ephemeral agents when done */ import { EventEmitter } from 'events'; import { type ChildProcess } from 'child_process'; import type { Storage } from '../storage.js'; import type { Identity } from '../identity.js'; import type { RoomManager } from '../rooms.js'; import type { CocEngine } from '../coc.js'; import { SwarmController } from '../proactive/swarm-controller.js'; import type { P2PSwarmRegistry } from '../proactive/swarm-registry.js'; import { CapabilityRouter, type IncomingRequest, type RoleSpec, type RoutingDecision } from './capability-router.js'; export type SpawnRuntime = 'ollama' | 'http' | 'docker' | 'process'; export interface SpawnConfig { /** Default runtime for ephemeral agents */ defaultRuntime: SpawnRuntime; /** Ollama base URL */ ollamaUrl: string; /** Ollama model for spawned agents */ ollamaModel: string; /** Max concurrent spawned agents */ maxSpawnedAgents: number; /** Timeout for spawned agent tasks (ms) */ taskTimeoutMs: number; /** Whether to auto-dissolve agents after task completion */ autoDissolvе: boolean; /** Room to use for spawned swarms */ defaultRoom: string; } export declare const DEFAULT_SPAWN_CONFIG: SpawnConfig; export interface SpawnedAgent { id: string; role: RoleSpec; runtime: SpawnRuntime; did: string; status: 'spawning' | 'ready' | 'working' | 'done' | 'failed' | 'dissolved'; process?: ChildProcess; endpoint?: string; createdAt: number; dissolvedAt?: number; } export interface SpawnedTeam { teamId: string; request: IncomingRequest; routing: RoutingDecision; agents: SpawnedAgent[]; chainId?: string; roomId: string; status: 'assembling' | 'working' | 'completed' | 'failed' | 'dissolved'; results: Map; createdAt: number; completedAt?: number; } export interface SpawnResult { teamId: string; chainId: string; status: 'completed' | 'failed'; results: Record; agents: Array<{ id: string; role: string; runtime: SpawnRuntime; status: string; }>; durationMs: number; } export declare class DemandSpawner extends EventEmitter { private storage; private rooms; private coc; private identity?; private config; private router; private swarm; private teams; private spawnedAgents; constructor(storage: Storage, rooms: RoomManager, coc: CocEngine, registry: P2PSwarmRegistry, config?: Partial, identity?: Identity); /** * Handle an incoming request end-to-end: * Route → Select/Spawn → Execute → Collect → Dissolve */ handleRequest(request: IncomingRequest): Promise; /** * Assemble a team by selecting existing agents or spawning new ones. */ private assembleTeam; /** * Wrap an existing swarm agent as a SpawnedAgent. */ private wrapExistingAgent; /** * Spawn an ephemeral agent for a role. */ private spawnAgent; /** * Open a Chain of Collaboration for the team. */ private openChain; /** * Execute the team's tasks using Ollama or other runtimes. */ private executeTeam; /** * Execute a single agent's task against Ollama or other runtime. */ private executeAgentTask; /** * Build a prompt for an agent based on its role and context. */ private buildAgentPrompt; /** * Call Ollama API to execute a task. */ private callOllama; /** * Call an HTTP endpoint for existing agents. */ private callHttp; /** * Dissolve ephemeral agents after task completion. * IoA-inspired: ephemeral teams are assembled and dissolved per request. */ private dissolveTeam; /** Get the capability router instance */ getRouter(): CapabilityRouter; /** Get the swarm controller instance */ getSwarm(): SwarmController; /** Get active teams */ getActiveTeams(): SpawnedTeam[]; /** Get team by ID */ getTeam(teamId: string): SpawnedTeam | undefined; /** Get all spawned agents */ getSpawnedAgents(): SpawnedAgent[]; /** Get spawn config */ getConfig(): SpawnConfig; /** Update spawn config */ updateConfig(updates: Partial): void; /** Clean up */ destroy(): void; } //# sourceMappingURL=demand-spawner.d.ts.map