agent_sdk library

Anthropic's Claude Agent SDK as a tom_llm provider.

AgentSdkSessionProvider serves every request from a session over the transport an AgentSdkTransportSelector chose: HostedCliServerAiTransport (a running VS Code with the Tom extension's CLI server) or StandaloneNodeAiTransport (a Node server the app starts and owns). The run streams tom_llm events; a request's tools are relayed back to the app through an in-process MCP server.

Classes

AgentSdkCompletionEvent
One thing a running completion says: an event of its trail, or the fact that it is still alive.
AgentSdkCompletionRequest
One completion request at the transport layer.
AgentSdkInstallableTransport
A transport that can say which kind of unavailable it is, and that can install what it is missing when the user asks it to.
AgentSdkInstallOutcome
What an on-demand install of a transport's server ended with.
AgentSdkLiveness
The far end showed it is alive and said nothing the trail records: a partial message's bookkeeping (message_start, a block opening or closing), a progress event, a tool call being run.
AgentSdkNodeRuntime
What the installing Node said about itself.
AgentSdkNotice
Something about AI the shell should show once.
AgentSdkPayloadVerification
What verifyAgentSdkPayloadFor found.
AgentSdkPlatformChoice
Which Claude Code CLI an install will get, or why it can get none.
AgentSdkServerInstaller
Installs the AI server's dependencies into the app's data folder, on the user's request.
AgentSdkServerInstallResult
What AgentSdkServerInstaller.install ended with.
AgentSdkServerTransport
The seam. One contract; the hosted and standalone paths implement it and are selected at startup.
AgentSdkSessionManager
The sessions of one app.
AgentSdkSessionProvider
Anthropic, reached indirectly through whichever transport was selected.
AgentSdkSourceVerification
What verifyAgentSdkServerSource found: everything wrong with the AI server source a build carries. Empty problems means shippable.
AgentSdkStagingReport
What a staging run did, so a build log says something a human can check.
AgentSdkToolSet
The tools a request offers, reduced to what a transport needs to relay a call.
AgentSdkTrailed
Something the run did, for its trail: the far end's start, thinking, text, a tool call or its result, a retry, the usage, the end.
AgentSdkTransportSelector
Picks the transport to use and owns the availability verdict.
AgentSdkWorkspaceCandidate
One VS Code CLI server that answered a discovery scan.
CliServerAiChannel
A connected CLI-server endpoint, reduced to the one operation the AI adapter needs.
HostedCliServerAiTransport
Reaches Anthropic through the CLI server of a running VS Code.
HostedServerResolution
What one discovery scan concluded for a transport's configuration.
StandaloneNodeAiTransport
Reaches Anthropic through a Node server the app starts and owns.

Enums

AgentSdkServerInstallState
The installer's state as of the last look at the disk.
AgentSdkSessionHosting
Where a session runs.
AgentSdkUnavailableKind
Why the availability gate is shut, as a kind a program can branch on.

Constants

agentRunMaxTurns → const int
How many model turns a tool-using run may take before the SDK ends it.
agentSdkInstallExplanationUrl → const String
Where a user is sent to read what Install does and why it is needed.
agentSdkInstallMarkerName → const String
The file an install writes last, after its tree passed the post-install check. Its presence is what "installed" means: a tree without it is one an interrupted install left behind.
agentSdkServerLockfileName → const String
The lockfile the shipped source carries and npm ci installs from.
agentSdkServerPayloadDir → const String
The name of the AI server's folder in both places it lives: beside the app executable, where a packaged app carries the server's source (the script, its package.json and its package-lock.json — no node_modules), and in the app's data folder, where the user's install puts the runnable server.
agentSdkServerScriptName → const String
The file a packaged app ships and this transport runs.
agentSdkServerSourceFiles → const List<String>
The three files a packaged app carries of the AI server, and the only ones.
defaultAiStallTimeout → const Duration
How long a completion may go without hearing anything at all before it is declared stalled.
requiredNodeMajorVersion → const int
The lowest Node major version the shipped server and the Agent SDK accept.
standaloneReadyPrefix → const String
The line the child prints once it is listening. Anything else on stdout is diagnostics.
standaloneScriptEnvVar → const String
Environment variable naming the server script, for deployments that place it somewhere the default resolution cannot guess.

Properties

agentSdkHostPlatform → String?
The platform suffix for the machine this Dart process is running on.
no setter

Functions

agentSdkChildEnvironment({required Map<String, String> parent, required String nodeExecutable, Set<String> scrubbed = const {}, ({String apiKey, String configDirectory})? credential}) → Map<String, String>
The environment the standalone server child is started with: parent — the app's own — with nodeExecutable's directory at the front of PATH, and without the variables in scrubbed — credentials the app was handed for itself, such as a session token it was launched with.
agentSdkInLockfile(String lockfile) → ({Set<String> platforms, String? version})?
The locked SDK version and the platform suffixes it ships a CLI for, read from a package-lock.json's text. Null when the lockfile does not lock the SDK.
agentSdkInstallOutcomeOf(AgentSdkServerInstallResult result) → AgentSdkInstallOutcome
The AgentSdkInstallOutcome a transport reports for result.
agentSdkMcpToolName(String server, String name) → String
The SDK name of the tool name declared on the in-process MCP server server.
agentSdkNodeMissingReason(LlmNodeResolution node) → String
The sentence for a search that found no Node at or above the floor: where it looked and what it found, then the remedy.
agentSdkNodeNotRunnableReason(String nodeExecutable) → String
The sentence for a Node that cannot be run at all.
agentSdkNodeProblem(String nodeExecutable) → Future<String?>
Why nodeExecutable cannot run the AI server, as a sentence naming the remedy, or null when it can.
agentSdkNodeProblemFor(LlmNodeResolution node) → Future<String?>
Why the Node node resolved cannot run the AI server, as a sentence naming the remedy, or null when it can: the search's own account when it found nothing (agentSdkNodeMissingReason), else agentSdkNodeProblem for the executable it named.
agentSdkNodeTooOldReason(String nodeExecutable, int major) → String
The sentence for a Node below requiredNodeMajorVersion.
agentSdkPlatformFor({required String operatingSystem, required bool isArm64}) → String?
The Agent SDK's name for a host's platform package suffix — darwin-arm64, linux-x64, win32-x64 — or null for a host it ships no CLI for.
agentSdkServerScriptCandidates({required String executableDir}) → List<String>
Every place a runnable server script is looked for, in order: source checkouts only.
agentSdkServerSourceCandidates({required String executableDir}) → List<String>
Every folder the server's source — the three files an install starts from — is looked for in, in order.
boundedAiRun(Stream<AgentSdkCompletionEvent> events, {required Duration stallGap, required String transportId, List<LlmEvent> prelude = const [], void onFailure(LlmTransportException failure)?, void onUnmapped(String messageType)?}) → Stream<LlmEvent>
events as the run's trail: prelude first, then every AgentSdkTrailed event in order, ending in exactly one LlmRunEnded — the far end's, completed when the stream ends without one, failed after a LlmRunError when it fails, and timeout after a LlmStall when nothing at all arrives for stallGap. The stream itself never errors: a failure is part of the trail. onFailure observes a transport failure (the provider closes the availability gate on unreachable); onUnmapped is told, once per run, of each message type it does not map.
chooseAgentSdkPlatform({required String nodePlatform, required String nodeArch, required Set<String> shipped, String? sdkVersion}) → AgentSdkPlatformChoice
Decides which Claude Code CLI package an install for a Node reporting nodePlatform / nodeArch (process.platform, process.arch) gets, from the set of platform suffixes the locked SDK version ships (shipped).
discoverHostedAiWorkspaces({String host = '127.0.0.1', int minPort = defaultVSCodeBridgePort, int maxPort = maxVSCodeBridgePort}) → Future<List<AgentSdkWorkspaceCandidate>>
Every VS Code CLI server answering right now, for the workspace pin.
resolveAgentSdkServerSourceFor({required String executableDir}) → String
The folder an install takes the server's source from: the first of agentSdkServerSourceCandidates holding all three source files, else the packaged location, so a failure names where the source was expected.
resolveDefaultServerScript() → String
Locates a runnable agent_sdk_server.js for a transport with no installer — a developer's tool, not the packaged app's path.
resolveServerScriptFor({required String executableDir}) → String
The resolution proper, with the one unmockable input passed in.
runAgentSdkCommand(String executable, List<String> arguments, {required String workingDirectory, required Map<String, String> environment, required void onOutput(String line)}) → Future<int>
The production AgentSdkCommandRunner: Process.start, no shell.
stageAgentSdkServer({required String nodeSourceDir, required String destination}) → AgentSdkStagingReport
Copies the server's three source files from nodeSourceDir into destination — the ai/ folder beside the built executable.
verifyAgentSdkPayloadFor({required String payloadDir, required String? expectedPlatform}) → AgentSdkPayloadVerification
The post-install check: whether payloadDir — an installed AI server — holds a tree the transport can actually run.
verifyAgentSdkServerSource({required String sourceDir}) → AgentSdkSourceVerification
The release gate: checks that sourceDir — a build's ai/ folder — carries exactly what a release may ship of the AI server.

Typedefs

AgentSdkCommandRunner = Future<int> Function(String executable, List<String> arguments, {required Map<String, String> environment, required void onOutput(String line), required String workingDirectory})
Runs one command and reports its output line by line; returns its exit code. Throws ProcessException when the command cannot be started.
AgentSdkToolDeclaration = ({String description, Map<String, Object?> inputSchema, String name})
One tool as the model is told about it: the registry entry's name, summary and JSON-Schema arguments, unchanged.

Exceptions / Errors

AgentSdkStagingException
Thrown when the build cannot stage the server's source. A build failure, never a runtime one: at run time a missing server is the availability gate.