/** * conversationBridge —— 把 agentService 的 {@link AgentStreamEvent} 流编排成 * conversation-contract 的 {@link ConversationSseEvent} 流。 * * 这是 conversation bridge 的「有状态编排器」:负责事件生命周期配对—— * 发 ack → RUN_STARTED →(按需发 TEXT_MESSAGE_START/CONTENT/END 三元组、 * TOOL_CALL_START/ARGS/END)→ RUN_FINISHED,并对中途错误转成 transport-error。 * * 设计要点: * - **纯 generator + 不碰 HTTP/stdio/定时器**:只产出 `ConversationSseEvent` 的 * async iterable,「写出到哪里」由调用方(appwrite res.write、agentdev socket…) * 负责。这是跨场景可复用的关键。 * - **文本消息配对**:收到首个 delta 时发 `TEXT_MESSAGE_START`,后续 delta 发 * `CONTENT`,文本结束(下一个非 delta 事件前 / done 时)发 `END`。这样消费方拿到的 * 是符合 ag-ui 规范的完整消息三元组。 */ import { type ConversationSseEvent } from '../../vendor/linkque-agent-conversation-contract/index.js'; import type { AgentStreamEvent } from '../../core/types.js'; import { type IdGenerator } from './eventMapper.js'; /** bridge 编排上下文:标识本次 run,出现在 ack 与部分 ag-ui 事件里。 */ export interface IConversationBridgeContext { /** 会话线程 id(对应 ConversationRunRequest.threadId)。 */ threadId: string; /** 本次 run id(对应 ConversationRunRequest.runId)。 */ runId: string; } /** bridge 行为可选项。 */ export interface IConversationBridgeOptions { /** * 标识生成器:用于 messageId。默认 `crypto.randomUUID`。 * 注入便于单测确定性断言。 */ generateId?: IdGenerator; } /** * 把 agentService 的 {@link AgentStreamEvent} 流编排成 conversation SSE 事件流。 * * 产出事件序列: * ack → RUN_STARTED → [TEXT_MESSAGE 三元组 | TOOL_CALL 三元组]* → RUN_FINISHED * 普通结束发 `RUN_FINISHED`;若流中产出 `error` 事件或迭代抛错,则发 * `transport-error` 后结束(不再发 RUN_FINISHED,以让消费方明确区分成败)。 * * @param events agentService.streamChat 的事件流(消费即结束,不可复用) * @param ctx run/thread 标识 * @param options 可选:id 生成器 * @returns conversation-contract SSE 事件 async iterable */ export declare function streamToConversationEvents(events: AsyncIterable, ctx: IConversationBridgeContext, options?: IConversationBridgeOptions): AsyncIterable;