跳转到主要内容

@ag-kit/server

用于服务AG-Kit Agent的轻量级HTTP/Express适配器。该包提供精简的类型化接口:
  • 创建并运行带有Agent路由的Express服务器
  • 在现有Express应用中挂载路由
  • 将单个Agent处理程序暴露为Fetch兼容函数
所有响应均通过服务器推送事件(SSE)流式传输。

安装

npm install @ag-kit/server

功能特性

  • Express辅助工具:runcreateExpressServercreateExpressRoutes
  • HTTP适配器:agui.sendMessage.createServerAdapter
  • 健康检查端点:agui.healthz.serverAdapter

快速开始

run(推荐方式)

import { run } from '@ag-kit/server';

run({ agent: () => ({ agent: myAgent }) });

端点

使用Express辅助工具时提供:
  • POST /send-message — 接收JSON格式的SendMessageInput并以SSE流式返回事件
  • GET /healthz — 返回OK

请求与事件

请求体结构(通过Zod验证):
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 }>;
};
SSE事件负载:
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 };
完整参考详见API和事件文档。

后续步骤