跳转到主要内容
@ag-kit/shared 包集中了 AG-Kit 服务器、客户端和 UI 使用的标准 Zod 模式与 TypeScript 类型。它定义了向 Agent 发送消息的请求载荷,以及通过服务器发送事件 (SSE) 返回的流式事件载荷,同时包含标准消息和工具形态。

安装

npm install @ag-kit/shared

包含内容

  • 模式 (Zod): sendMessageInputSchema, sendMessageEventSchema, systemMessageSchema, userMessageSchema, toolMessageSchema, assistantMessageSchema, clientMessageSchema, toolSchema
  • 类型 (生成): SendMessageInput, SendMessageEvent, SystemMessage, UserMessage, ToolMessage, AssistantMessage, ClientMessage, Tool

请求载荷

用于启动或继续与 Agent 对话的强类型请求体。
import type { SendMessageInput } from "@ag-kit/shared";

type SendMessageInput = {
  conversationId: string;
  messages?: Array<
    | { role: "system"; content: string }
    | { role: "user"; content: string }
    | { role: "tool"; content: string; toolCallId: string }
    | {
        role: "assistant";
        content?: string;
        toolCalls?: Array<{
          id: string;
          type: "function";
          function: { name: string; arguments: string };
        }>;
      }
  >;
  resume?: { interruptId: string; payload: unknown };
  tools?: Array<{ name: string; description: string; parameters: any }>;
};
需要时使用 Zod 进行运行时验证:
import { sendMessageInputSchema } from "@ag-kit/shared";

sendMessageInputSchema.parse(jsonBody);

流式事件 (SSE)

Agent 通过 SSE 流式传输一系列类型化事件进行响应。
import type { SendMessageEvent } from "@ag-kit/shared";

type SendMessageEvent =
  | { type: "text"; content: string }
  | { type: "tool-call-start"; toolCallId: string; toolCallName: string }
  | { type: "tool-call-args"; toolCallId: string; delta: string }
  | { type: "tool-call-end"; toolCallId: string }
  | { type: "tool-result"; toolCallId: string; result: string }
  | { type: "interrupt"; id: string; reason: string; payload: unknown };

消息形态

请求和 UI 使用的标准化消息变体:
type SystemMessage = { role: "system"; content: string };
type UserMessage = { role: "user"; content: string };
type ToolMessage = { role: "tool"; content: string; toolCallId: string };
type AssistantMessage = {
  role: "assistant";
  content?: string;
  toolCalls?: Array<{
    id: string;
    type: "function";
    function: { name: string; arguments: string };
  }>;
};
type ClientMessage = SystemMessage | UserMessage | ToolMessage | AssistantMessage;

工具定义形态

描述 Agent 可用工具 (仅类型;UI/客户端执行定义在 @ag-kit/ui-react 中)。
type Tool = {
  name: string;
  description: string;
  parameters: any; // 工具输入形态;在 UI 中通常为 Zod 模式
};

导出项

// 模式 (Zod)
systemMessageSchema
userMessageSchema
toolMessageSchema
assistantMessageSchema
clientMessageSchema
toolSchema
sendMessageInputSchema
sendMessageEventSchema

// 类型
SendMessageInput
ClientMessage
SystemMessage
UserMessage
ToolMessage
AssistantMessage
Tool
SendMessageEvent

另请参阅

  • 服务器概述 — 使用这些类型的 HTTP 适配器和 SSE 端点
  • React UI — 消费这些事件和消息类型的客户端钩子