Agent 生命周期

Agent 的构造、就绪、执行与关闭

new Agent(options) 只创建 Agent 身份、模型、指令、自定义 Tool、Session 集合与 ActionSchedule。Power 不属于 Agent;City 注册 Agent 后,把自己持有的 Power 能力投影到 Agent 的执行入口。

同一个 Agent ID 在同一物理 Workspace 中只能运行一个活动 runtime。ActionSchedule 是本地调度器,不负责协调重复启动的 Agent 进程。

import { Agent } from "@downcity/agent";
import { City, Shell, Workspace } from "@downcity/city";
import { MicrosandboxProvider } from "@downcity/sandbox-microsandbox";

const shell = new Shell({ sandbox_provider: new MicrosandboxProvider() });
const agent = new Agent({
  id: "my-agent",
  model,
});
const workspace = new Workspace({
  id: "project",
  path: process.cwd(),
  shell,
});
const city = new City({ agents: [agent], workspaces: [workspace] });

const session = await agent.sessions.create({ workspace });
const turn = await session.prompt({ query: "开始" });
await turn.finished;

await city.close();

Session 会在执行前自动等待 Agent 的 City 执行绑定就绪。agent.dispose() 只释放 Agent 自身的 Session、ActionSchedule 与执行绑定;city.close() 会先停止 Agent Session,随后停止 City 持有的唯一 Power 实例,并关闭 Workspace、Shell 与 Transport。

继续阅读:Agent SDK。