UNPKG

autosnippet

Version:

Extract code patterns into a knowledge base for AI coding assistants

205 lines (204 loc) 5.5 kB
/** * AgentMessage — 统一消息信封 * * 核心抽象: Agent 永远不需要知道消息来自哪个渠道。 * Transport 适配器负责将渠道特定格式转换为 AgentMessage, * Agent 只处理 AgentMessage, 通过 replyFn 返回结果。 * * 这是将"飞书聊天"和"前端聊天"统一为同一概念的关键: * - HTTP/SSE (Dashboard) → AgentMessage { channel: 'http', ... } * - WebSocket (Lark/飞书) → AgentMessage { channel: 'lark', ... } * - CLI (终端) → AgentMessage { channel: 'cli', ... } * - MCP (IDE 扩展) → AgentMessage { channel: 'mcp', ... } * * @module AgentMessage */ /** Reply callback type */ type ReplyFn = (text: string) => Promise<void>; /** Sender identity */ interface Sender { id: string; name?: string; type: 'user' | 'system' | 'agent'; } /** Session context */ interface Session { id: string; history?: Array<{ role: string; content: string; }>; } /** AgentMessage constructor options */ interface AgentMessageOptions { content?: string; channel?: string; session?: Session; sender?: Sender; metadata?: Record<string, unknown>; replyFn?: ReplyFn | null; } /** HTTP request body shape */ interface HttpRequestBody { prompt?: string; message?: string; content?: string; conversationId?: string; sessionId?: string; history?: Array<{ role: string; content: string; }>; userId?: string; userName?: string; lang?: string; mode?: string; context?: unknown; stream?: boolean; } /** Minimal Express-like request */ interface HttpRequest { body?: HttpRequestBody; ip?: string; } /** Lark message shape */ interface LarkMessage { text?: string; content?: string; chatId?: string; senderId?: string; userId?: string; senderName?: string; messageId?: string; messageType?: string; [key: string]: unknown; } /** CLI options */ interface CliOptions { sessionId?: string; history?: Array<{ role: string; content: string; }>; cwd?: string; mode?: string; metadata?: Record<string, unknown>; } /** Internal message options */ interface InternalMessageOptions { session?: Session; sessionId?: string; history?: Array<{ role: string; content: string; }>; sourceAgentId?: string; parentAgentId?: string; dimension?: string; phase?: string; metadata?: Record<string, unknown>; /** Extra pass-through keys (e.g. 'source') */ [key: string]: unknown; } /** MCP request shape */ interface McpRequest { prompt?: string; content?: string; arguments?: Record<string, unknown> & { prompt?: string; }; sessionId?: string; history?: Array<{ role: string; content: string; }>; clientId?: string; clientName?: string; toolName?: string; mode?: string; metadata?: Record<string, unknown>; } /** 通信渠道枚举 */ export declare const Channel: Readonly<{ HTTP: "http"; LARK: "lark"; CLI: "cli"; MCP: "mcp"; INTERNAL: "internal"; }>; export declare class AgentMessage { /** 消息唯一 ID */ id: `${string}-${string}-${string}-${string}-${string}`; /** 用户输入内容 */ content: string; /** 通信渠道 */ channel: string; /** 会话信息 */ session: Session; /** 发送者 */ sender: Sender; /** 渠道特定元数据 */ metadata: Record<string, unknown>; /** 回复函数 (text: string) => Promise<void> */ replyFn: ReplyFn | null; /** 时间戳 */ timestamp: number; /** * @param opts.content 用户输入 * @param [opts.channel='http'] 渠道 * @param [opts.session] 会话 * @param [opts.sender] 发送者 * @param [opts.metadata] 元数据 * @param [opts.replyFn] 回复函数 */ constructor({ content, channel, session, sender, metadata, replyFn, }?: AgentMessageOptions); /** 对话历史 (快捷访问) */ get history(): { role: string; content: string; }[]; /** 向发送方回复 */ reply(text: string): Promise<void>; /** * 从 HTTP 请求构建 * @param req Express request * @param [replyFn] SSE 或 JSON 回复 */ static fromHttp(req: HttpRequest, replyFn?: ReplyFn): AgentMessage; /** * 从飞书消息构建 * @param larkMsg 飞书消息对象 * @param replyFn 飞书回复函数 */ static fromLark(larkMsg: LarkMessage, replyFn?: ReplyFn | null): AgentMessage; /** * 从 CLI 输入构建 * @param input 命令行输入 */ static fromCli(input: string, opts?: CliOptions): AgentMessage; /** * Agent 间内部消息 * @param content 消息内容 */ static internal(content: string, opts?: InternalMessageOptions): AgentMessage; /** * 从 MCP 请求构建 * @param mcpReq MCP tool call request * @param [replyFn] 回复函数 */ static fromMcp(mcpReq: McpRequest, replyFn?: ReplyFn): AgentMessage; /** 序列化 */ toJSON(): { id: `${string}-${string}-${string}-${string}-${string}`; content: string; channel: string; session: { id: string; historyLength: number; }; sender: Sender; metadata: Record<string, unknown>; timestamp: number; }; } export default AgentMessage;