@agents-js/acp-host
v0.4.0
Published
Stateful ACP host embedding surface. Manages agent sessions, terminal processes, permissions, and file I/O for host applications.
Maintainers
Readme
@agents-js/acp-host
Stateful ACP host embedding surface. Manages agent sessions, terminal processes, permissions, and file I/O for host applications.
Installation
bun add @agents-js/acp-hostAPI
Classes
ACPSessionController— High-level session orchestrator for ACP agent communication. This is the top-level Provider for host applications. It composes an {ACPClientController} with host-level concerns: permission eval...AcpStreamingTranslator— Stateful per-session translator. Instantiate one per ACP session. The translator does no I/O and holds no transport handles — it's a pure data transformer with private accumulator state. Tests shou...CapabilityCache— Parses and caches agent capabilities from InitializeResponse. Provides boolean helpers for capability-gating UI and behavior. "Advertised" flags reflect what the CLIENT advertises to the agent. The...EvalTransport—LogTransportthat captures per-prompt evaluation records (prompt content, response, stop reason, errors) for offline regression suites.Logger— Structured console logger with category prefixes. Provides debug visibility for session lifecycle, permissions, and errors. All log calls are also pushed to the global logStore for the debug panel.PermissionEnginePermissionStore— Persistence layer for permission rules. Session-scoped rules are held in memory. Persistent rules are saved via a host-provided callback.SpanLogTransport—LogTransportthat buckets entries into spans keyed byentry.spanId, tracking active and completed spans for the debug panel and trace export.TerminalManager
Functions
applyAgentDefaultsatomicWrite— Atomic CAS write: stages content to a temp file, verifies the target is unchanged since the last read, then renames the staging file into place. Ifsnapshotis null (file was never read, or is a ...buildCompletedTurnSnapshotbuildControllerAdapters— Build theACPHostAdaptersconfig object forACPClientController. Wires each adapter closure to the session controller's internal handlers without exposing the full controller instance.buildForbiddenEnvKeys— Compute the set of env-var keys that callers must not override via agentextraEnv. The result is the union of {SYSTEM_FORBIDDEN_ENV_KEYS}, the policy'sagentSecretEnvKeys, and the policy's `for...buildInlineContext— Builds inline text with file content embedded in a fenced code block.buildMcpServersList— Build the list of MCP servers to pass when creating or loading a session.buildMinimalEnvcallHook— Safely invoke a session hook function. If the hook throws, the error is logged andundefinedis returned.cancelPendingElicitation— Cancel a pending elicitation request by resolving it with "cancel" action. Emits anelicitation_resolvedevent if there was a pending request.cancelPendingPermission— Cancel a pending permission request by resolving it as "cancelled". Emits apermission_resolvedevent if there was a pending request.cancelPendingWriteGate— Cancel a pending write-gate request by resolving it as "reject". Emits awrite_gate_resolvedevent if there was a pending gate.captureReadSnapshot— Capture a snapshot of a file for later CAS comparison. - Absolute path to the file (already resolved). A snapshot containing the content hash and mtime.cleanupStagingDir— Remove a session's staging directory. - Absolute path to the staging directory to remove.clearCancelSafetyTimer— Clear the cancel safety timer, if active. Returnsnullso the caller can assign back:this.timer = clearCancelSafetyTimer(this.timer).clonePlanEntriesclonePromptContentcloneTurnItemscollectToolCallStatsconfigureLoggingcreateCrashableAgent— Creates a mock agent whose transport can be aborted mid-session. Useful for host crash/recovery tests that need a hard stream failure.createEchoAgentcreateFallbackModescreateHangingAgentcreateHostACPProcesscreateInitialStatecreateMockAgentcreateNodeFileAdapters— Creates {HostFileAdapters} that use Node.jsfsAPIs for file I/O. Relative paths resolve against the session cwd. Reads and writes are then enforced against the normalized read/write/scratch root...createOpencodeDefaultAgentRecoveredConfigcreateTerminalHandlers— Create a scoped set of terminal handlers backed by a dedicated TerminalManager. Each ACPSessionController should call this once and own the returned handlers.createTurnStatederiveActivitySurfacederiveComposerSurfacederiveInterruptSurfacederivePlanSurfacederiveTerminalEntriesderiveTranscriptSurfacederiveWorkflowSurfaceStatedescribeElicitationTargetdescribeQueuedFollowUpBoundarydescribeRunningToolsdescribeWorkflowErrordetectFrontmatterOnlyWriteemitEvent— Emit an event to all registered listeners. Individual listener errors are caught and logged so one bad listener does not break others.emptyToolCallStatsensureStagingDir— Create the staging directory for a session under the configured scratch root. Idempotent -- safe to call multiple times. - Absolute path to the scratch root (for example<workspace>/.tmp). - Sess...evaluatePermission— Full permission evaluation pipeline: 1. beforePermission hook (fail-closed) 2. YOLO mode auto-approve 3. Read-only auto-approve 4. Remembered rules 5. Fall through to user promptfilterFiles— Filters open files by a query string (fuzzy match against name and basename).formatToolCallStatusgetHostManagedPermissionReasongetMentionAtCursor— Finds the mention token being typed at the cursor position in a textarea. Returns the query text after@(empty string if the user just typed@), ornullif the cursor is not currently inside...getPromptQueueCountgetToolCallByIdgetToolCallsgetTrailingToolBlockStatsgetValidMessageIdhandleSessionUpdate— Process a session notification and update state accordingly. Returns nothing -- all mutations happen on thestateparameter.isOpencodeDefaultAgentMissingisSessionRestoreFailureMessageisWithinAnyWorkspaceRootlogPermissionDecision— Log a structured permission decision.mapToolCallContentmapToolCallStatusnormalizeAgentNamenormalizeSessionModesparseMentions— Extracts mention tokens from text. Only matches tokens like.mdat word boundaries (after whitespace, start of line, or after newline).requestWriteGateApproval— Determine whether a write should be auto-approved or requires a UI modal. Returns a resolvedPromise<boolean>for auto-approved writes, or creates a pending write gate onstateand emits the re...resetLoggingresolveHostEnvPolicyresolveMentions— Resolves mention tokens to open file paths.resolveWorkflowCopy— Resolve a full copy map from optional partial overrides. Missing keys fall back to the default English copy.resolveWorkspaceContextresolveWorkspaceFilePathsandboxHomePath— Compute the deterministic sandbox HOME directory for a given workspace identity root. The hash ensures each logical workspace gets its own isolated HOME without collisions.snapshotToolCallssplitMarkdownFrontmattersummarizeToolGrouptoWorkspaceDisplayPathtrySetAgentModewaitForReady— Returns a promise that resolves when the session status becomes "ready", or rejects after the given timeout.
Interfaces
ACPSessionStateAcpStreamingSink— Sink contract: consumers implement these methods to receive translated streaming events. Methods are invoked synchronously; the sink owns any downstream async dispatch (e.g. publishing on an event ...ActivitySurfaceStateAgentConfigApplyDefaultsResultAvailableCommandsUpdateCall— Argument shape forAcpStreamingSink.onAvailableCommandsUpdate. Carries the FULL set of commands available from the harness via the SDK's typedAvailableCommandshape (preserves description, inp...BuildMinimalEnvInputCodeDescriptor— A code block with language annotationCompletedToolCallSnapshotCompletedTurnSnapshotComponentDescriptor— An escape hatch for host-specific componentsComposerSurfaceStateControllerAdapterContextCreateTerminalHandlersOptionsDependencyRegistry— Registry for querying host-provided integrations and capabilities at runtime. This is an Adapter interface that host platforms can implement to advertise available services (MCP servers, extern...DiffDescriptor— A file diffDirectoryPolicy— Directory policy: what the agent is ALLOWED to read, write, and stage to, plus the auto-approved write zones that bypass the write-gate modal. This is intentionally separate from {WorkspaceContextC...EvalRecordFileSnapshot— Snapshot of a file's content hash and modification time, captured after a read for later CAS comparison.FormDescriptor— A form rendered inline (not as a modal) — for read-only display of elicitation resultsHostACPProcessOptions— Host-specific process spawn options, extending the low-levelACPProcessOptionsfrom-js/acpwith fields that onlycreateHostACPProcess(in this package) interprets.workspaceFlaglived on `...HostElicitationAdapter— Optional elicitation adapter that the host platform can provide to handle agent elicitation requests (e.g., form-based user input). If omitted, the session controller manages elicitation state inte...HostEnvPolicyInput— Caller-supplied environment-variable policy.acp-hostcannot know which env vars are credentials for a given harness; embedders with that knowledge populate this shape so the spawn-env builder fo...HostFileAdapters— File I/O adapters that the host platform must implement. The writeTextFile adapter receives arequestApprovalcallback for write-gate approval flows.HostSessionStorageAdapter— Optional session storage adapter for persisting session state across backend restarts. Required for seamless transcript reloading if the host process crashes or restarts.IntegrationStatusInterruptActionStateInterruptSurfaceStateJsonRpcErrorJsonRpcRequest— MCP protocol types for the local workspace MCP server. Covers JSON-RPC 2.0 request/response shapes and MCP tool definitions needed for the HTTP transport. These are intentionally minimal -- only wh...JsonRpcResponseLogEntryLoggerConfigLogTransport— Sink for structured log entries. Implementors handle eachLogEntrysynchronously (any async work must be self-managed) and decide where it goes — console, span buffer, eval transport, or a host-s...ManagedTerminalMcpInitializeResultMcpPropertySchemaMcpToolCallParamsMcpToolCallResultMcpToolContentMcpToolDefinitionMcpToolInputSchemaMcpToolsListResultModeChangeCall— Argument shape forAcpStreamingSink.onModeChange. ACPcurrent_mode_updatenotifications only carrycurrentModeId(the available-modes list lives in initial session metadata, not on transition...ModeSyncResultNodeFileAdapterOptionsNormalizedAgentNamePendingElicitationPendingPermissionPendingWriteGatePermissionDecisionPermissionEvaluationContextPlanEntryInfoPlanSurfaceStatePlanUpdateCall— Argument shape forAcpStreamingSink.onPlanUpdate. ACPplannotifications always carry the FULL set of entries — consumers replace plan state wholesale on receipt. Entries reuse the SDK's typed ...RenderHint— Metadata hint that can be attached to ACP session_update _meta fields to suggest structured rendering for tool call outputs.ResolvedHostEnvPolicy— Resolved view of {HostEnvPolicyInput} with defaults applied.ResolvedWorkspaceContextSessionHooksSessionInfoUpdateCall— Argument shape forAcpStreamingSink.onSessionInfoUpdate. Mirrors ACPSessionInfoUpdate(title + updatedAt). Both fields arestring | null | undefinedper the SDK:nullis an explicit clear ...SessionUpdateContentHandlerHooksSpanStartConfig— Configuration for starting an ACP session controller. Replaces the old positional parameters with a structured config object.TerminalDescriptor— Terminal outputTerminalHandlersTerminalManagerOptionsTerminalWorkspaceContextTextDeltaCall— Argument shape forAcpStreamingSink.onTextDeltaandonThoughtDelta. Bothdelta(incremental text added by this chunk) andcumulativeText(running total permessageId) are surfaced so sink...TextDescriptor— A text block rendered as markdownToolBlockSurfaceStateToolCallContentHandlerToolCallContentHandlerContextToolCallContentInfo— Flattened ACPToolCallContentrepresentation. The SDK'sToolCallContentis a discriminated union of three nested shapes (content,diff,terminal); this type pulls the relevant fields up s...ToolCallInfoToolCallStartCall— Argument shape forAcpStreamingSink.onToolCallStart.ToolCallSummaryToolCallUpdateCall— Argument shape forAcpStreamingSink.onToolCallUpdate. Mirrors the ACPToolCallUpdateshape with three-state semantics forcontent/locations:undefined= no change,null= explicit cle...TranscriptSurfaceStateTurnStateUsageUpdateCall— Argument shape forAcpStreamingSink.onUsageUpdate.WorkflowActivityEntryStateWorkflowCopyMap— Type-safe interface for all workflow surface UI copy strings. Consumers may supply a partial override to customize any subset of strings.WorkflowErrorSummaryWorkflowPendingPermissionLikeWorkflowPendingWriteGateLikeWorkflowPlanEntryStateWorkflowSessionStateLikeWorkflowStatusOverrideWorkflowSurfaceContextWorkflowSurfaceStateWorkflowToolCallLikeWorkflowToolCallStatsWorkflowToolGroupSummaryWorkflowTurnStateLikeWorkspaceContextWorkspaceContextConfigWorkspaceContextProvider
Types
ACPSessionEventACPSessionStatusAgentScenarioElicitationScenarioFrontmatterWriteTarget— Browser-safe entry point for -js/acp-host/editor. The mention-parser helpers and the frontmatter-only-write detector are pure framework-agnostic TypeScript functions with no Node-specific runtime d...ListenerLogCategoryLogLevelModeFallbackReasonOpenFileEntryPermissionLifetimePermissionMode— Permission mode controlling the host's auto-approve/prompt strategy.PromptScenarioReadonlySpanRenderDescriptor— Discriminated union of all render descriptor typesSessionInfoTurnItem— Ordered item in a turn -- preserves interleaving of text and tool callsWorkflowActivityKindWorkflowActivityPhaseWorkflowActivityStatusWorkflowComposerModeWorkflowInterruptBlockerWorkflowSurfaceSeverityWriteGateResolution— Resolution returned by the write-gate modal UI.
Constants
DEFAULT_AGENT_CONFIGDEFAULT_INHERITED_ENV_KEYS— Default non-secret keys inherited from the host environment into the spawned agent process. Hosts can override the inherited set via {HostEnvPolicyInput.inheritedEnvKeys}; secret credentials (provi...DEFAULT_TERMINAL_ENV_KEYS— Default subset of {DEFAULT_INHERITED_ENV_KEYS} forwarded to terminal processes. SHELL is excluded because terminals spawn withshell: false(direct argv invocation), so the SHELL variable would b...defaultWorkflowCopy— Default English copy map used when no overrides are provided.DEV_AGENT_CONFIGJSON_RPC_INTERNAL_ERRORJSON_RPC_INVALID_PARAMSJSON_RPC_INVALID_REQUESTJSON_RPC_METHOD_NOT_FOUNDJSON_RPC_PARSE_ERRORlogStore— Singleton log store instance.SESSION_RESTORE_FAILURE_MESSAGESYSTEM_FORBIDDEN_ENV_KEYS— System/loader keys that agent config (extraEnv) must never override. These guards apply regardless of the harness or caller-supplied policy because they protect the spawned process's loader contr...
Exports
type BuildMinimalEnvInputtype NormalizedAgentName
Dependencies
@agentclientprotocol/sdk@agents-js/acp@agents-js/policy
License
MIT
