Agent
Agent 生命周期
Agent 的构造、就绪、执行与关闭
Agent 生命周期
new Agent(options) 完成装配后会立即创建并启动 Plugin lifecycle 与 ActionSchedule。不存在 agent.start() 或 agent.stop();由宿主负责调用 agent.dispose() 释放资源。
同一个 Agent ID 在同一物理 Workspace 中只能运行一个活动 runtime。ActionSchedule 是本地调度器,不负责协调重复启动的 Agent 进程。
import { Agent } from "@downcity/agent";
import { Workspace } from "@downcity/workspace";
import { Shell } from "@downcity/workspace";
import { MacOsSeatbeltSandbox } from "@downcity/sandbox-macos";
const shell = new Shell({ sandbox: new MacOsSeatbeltSandbox() });
const agent = new Agent({
id: "my-agent",
model,
});
const workspace = new Workspace({
id: "project",
path: process.cwd(),
shell,
});
const session = await agent.sessions.create({ workspace });
const turn = await session.prompt({ query: "开始" });
await turn.finished;
await agent.dispose();Session 会在执行前自动等待 Agent 初始化完成。dispose() 会关闭 plugin lifecycle、ActionSchedule 和 Shell 等 Agent 自有运行时资源;HTTP/RPC transport 由所属 City 管理并关闭。
继续阅读:Agent SDK。