multica/packages/sdk/src/actions/stream.ts
Naiyuan Qing d5b31eeddb fix(sdk,store): align frontend streaming protocol with new AgentEvent format
Backend Hub now sends raw AgentEvent in stream payloads instead of the
old delta/final/error state machine. Update StreamPayload type to use
event field, add extractTextFromEvent helper, and rewrite gateway store
handler to dispatch on event.type (message_start/update/end).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 16:54:34 +08:00

49 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** Stream Action - 流式消息传输 */
export const StreamAction = "stream" as const;
/**
* AgentEvent types forwarded by the Hub to frontend clients.
* These mirror the subset of AgentEvent from @mariozechner/pi-agent-core
* that the Hub forwards (filtered at the Hub layer).
*/
export interface StreamMessageEvent {
type: "message_start" | "message_update" | "message_end";
message: {
id?: string;
role: string;
content?: Array<{ type: string; text?: string }>;
};
assistantMessageEvent?: unknown;
}
export interface StreamToolEvent {
type: "tool_execution_start" | "tool_execution_end";
toolCallId: string;
toolName: string;
args?: unknown;
result?: unknown;
isError?: boolean;
}
export type StreamEvent = StreamMessageEvent | StreamToolEvent;
/** 流消息 payload — wraps a raw AgentEvent with stream/agent identifiers */
export interface StreamPayload {
/** 流 ID关联同一个流的所有消息 */
streamId: string;
/** 所属 agent ID */
agentId: string;
/** Raw agent event from the engine */
event: StreamEvent;
}
/** Extract plain text from an AgentMessage content array */
export function extractTextFromEvent(event: StreamMessageEvent): string {
const content = event.message?.content;
if (!Array.isArray(content)) return "";
return content
.filter((c) => c.type === "text")
.map((c) => c.text ?? "")
.join("");
}