  onProgress?: (step: string, status: string, output?: string) => void;
  // v2.0: 子代理间消息历史
  messageHistory?: AgentMessage[];
}

export interface SpawnOptions {
  objective: string;
  parentId: string;
  chatId: string;
  model?: string;
  timeoutSeconds?: number;
  taskName?: string;
  toolRegistry?: any;
  agentIdForTools?: string;
  onProgress?: (step: string, status: string, output?: string) => void;
}

// v2.0: Agent 间消息
export interface AgentMessage {
  id: string;
  fromAgentId: string;
  toAgentId: string;
  content: string;
  type: 'request' | 'response' | 'broadcast' | 'steer';
  taskId?: string;        // 关联的子任务 ID
  replyTo?: string;       // 回复哪条消息
  timestamp: number;
  status: 'pending' | 'delivered' | 'read';
}

// v2.0: Agent 间消息处理器（由外部注入，用于触发目标 Agent 回复）
type AgentMessageHandler = (msg: AgentMessage) => Promise<string>;

// v2.1: 多智能体协作编排
export type CollabMode = 'voting' | 'debate' | 'roundtable' | 'pipeline' | 'review' | 'orchestrator';

export interface CollabTurn {
  round: number;
  model: string;
  role: 'speak' | 'judge' | 'summary';
  content: string;
  failed?: boolean;
  ts: number;
}

export interface CollabSession {
  id: string;
  mode: CollabMode;
  question: string;
  models: string[];
  rounds: number;
  status: 'running' | 'completed' | 'failed' | 'cancelled';
  createdAt: number;
  updatedAt: number;
  transcript: CollabTurn[];
  conclusion?: string;
  error?: string;
  chatId?: string;
}

export interface CollabOptions {
  mode: CollabMode;
  question: string;
  models?: string[];
  rounds?: number;
  parentId?: string;
  chatId?: string;
  maxSpeakSeconds?: number;
  onEvent?: (evt: any) => void;
}


/**
 * 子代理管理器 v2.0
 */
export class SubAgentManager {
  private tasks = new Map<string, SubAgentTask>();
  private running = new Map<string, AbortController>();
  private aiClient: any;
  private aiModel: string;
  private onResult: ((task: SubAgentTask) => void) | null = null;
  // v2.0.22: 全局进度回调（用于把子代理内部工具调用回传父级，修 G6 steps=0）
  private onGlobalProgress: ((task: SubAgentTask, step: string, status: string, output?: string) => void) | null = null;

  // v2.0: Agent 间消息系统