Local Agent

通过 City 暴露 RPC 与 HTTP

使用 City 持有的 HTTP 和 RPC 服务暴露 Agent

通过 City 暴露 RPC 与 HTTP

外部服务属于 City。Agent 仍然是执行主体,City 负责网络监听,并把请求路由到对应的 Agent 和 Workspace。

pnpm add @downcity/agent @downcity/workspace

启动 City transport

import { Agent, City } from "@downcity/agent";
import { Workspace } from "@downcity/workspace";

const workspace = new Workspace({ id: "project", path: process.cwd() });
const agent = new Agent({ id: "assistant", model });
const city = new City({ workspaces: [workspace] });
city.agents.add(agent);

const rpc = await city.rpc({ host: "127.0.0.1", port: 15314 });
const http = await city.http({ host: "127.0.0.1", port: 5314 });
console.log(rpc.binding()?.url); // rpc://127.0.0.1:15314
console.log(http.binding()?.url); // http://127.0.0.1:5314

await city.close();

City 统一拥有一个 HTTP transport 和一个 RPC transport,按 Agent ID 与 Workspace ID 路由:

  • HTTP:http://127.0.0.1:5314/agents/assistant/workspaces/project
  • RPC:rpc://127.0.0.1:15314/assistant/project

RPC 使用 NDJSON 协议。鉴权、CORS 和其他宿主级策略由 City 宿主负责。

同时启动两个 transport

await city.listen({
  http: { host: "127.0.0.1", port: 5314 },
  rpc: { host: "127.0.0.1", port: 15314 },
});

await city.close();

需要独立控制生命周期时,分别调用 city.http()city.rpc()

与 RemoteAgent 的关系

import { RemoteAgent } from "@downcity/agent";

const agent = new RemoteAgent({
  url: "http://127.0.0.1:5314/agents/assistant/workspaces/project",
});
const session = await agent.sessions.create();

RemoteAgent 只消费 City 暴露的地址,不创建或配置服务端 Agent。

相关文档