Agent SDK

Build local or remote Agents with project files, sandboxed commands, plugins, and persistent Sessions

Agent SDK

@downcity/agent provides the local Agent execution runtime. @downcity/city provides the composition root, Workspace/Plugin resources, transports, and RemoteAgent. Both client shapes are session-first: Agent composes execution capability, while Session owns a continuous conversation.

One-minute mental model

ObjectOwnsDoes not own
WorkspaceProject files, env, Tools, and optional ShellAgent identity, plugins, Session persistence, and Sandbox implementation
ShellSandbox Provider, persistent Workspace Sandbox, and Shell SessionsChat Session identity and City lifecycle
AgentModel, instructions, custom Tools, and SessionsPlugin instances, Workspace resources, HTTP/RPC transports
AgentSessionsAll Sessions owned by one Agent; each Session may receive a WorkspaceAgent identity and Plugin definitions
SessionConversation state, message history, execution order, and approvalsProject resource ownership
CityWorkspace and Plugin resources, lifecycle, Storage, and transportsSession execution semantics
SessionTurnContextExecution context and lifecycle for one TurnLong-lived Agent state
RemoteAgentAccess to a server-side Agent and its SessionsServer-side model configuration

Choose local or remote

ScenarioUse
Agent and application run in the same Node.js processAgent
The application injects models, Tools, plugins, and Shell directlyAgent
Another process already exposes the Agent over HTTP/RPCRemoteAgent
The client only creates Sessions, sends prompts, and subscribesRemoteAgent

Network serving does not belong to Agent. Create a City, add the Agent with city.agents.add(agent), then use city.http() or city.rpc() to expose the City.

Shell is the standard entry for project commands and processes. This embedded example injects a microsandbox Provider directly into Shell:

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

const shell = new Shell({
  sandbox_provider: new MicrosandboxProvider(),
});

const workspace = new Workspace({
  id: "project",
  path: process.cwd(),
  shell,
  runtime_path: "/path/to/downcity-runtime/project",
});

const agent = new Agent({
  id: "repo-helper",
  model,
});
// Session 在创建时选择 Workspace。

try {
  const session = await agent.sessions.create({ workspace });
  const turn = await session.prompt({ query: "Summarize this repository" });
  const result = await turn.finished;
  console.log(result.text);
} finally {
  await agent.dispose();
}

Remember this primary path:

Agent + Workspace + Shell → agent.sessions.create({ workspace }) → Session → prompt() → turn.finished → dispose()

agent.dispose() releases Session resources and the current City binding. City closes shared Plugin instances, lifecycle, and ActionSchedule resources. The project directory never receives .downcity.

Model ownership

In pure SDK embedding, the host owns model instances:

  • Set the default with new Agent({ model }).
  • Or override it for one local Session with await session.set({ model }).

In a Downcity project, the CLI host resolves the model from project configuration and the City AIService catalog. Remote Sessions do not expose client-side model configuration; the server host always owns the model.

  1. Local Agent quickstart
  2. Sessions overview
  3. Shell and Sandbox
  4. Tools and plugins
  5. Remote Agent
  6. SDK Surface
  7. Agent SDK architecture