/** * resources.ts * * Shared connector lifecycle for agent sessions — connect, watch, and build * tool injection for all connector types (filesystem-face and tool-face). * * Used by agent.ts (REPL), factory-assets/connectors/flow/run-flow.ts (flow), and serve.ts (WebSocket server). * Extracted from duplicated inline blocks in each of those files. */ import { type ConnectorChangeEvent, type ConnectorDeclaration, type PreMintedSecretProvider, type SyncStatusPayload, type TokenMediator } from "@skaile/workspaces/connectors"; import { type McpServerDeclaration, type RuntimeAssetsResult } from "@skaile/workspaces/core"; import type { ConnectorStatusEvent } from "@skaile/workspaces/types"; import type { CapabilityRegistry } from "./capability-registry.js"; import { ExternalMcpManager } from "./external-mcp.js"; /** * Result of {@link buildAgentResources}. * * @docLink packages/runner/dev-guide#flow-execution-turn-based-model */ export interface AgentResourcesResult { /** Connected ConnectorManager instance — null if no connectors declared or package missing. */ resourceManager: any | null; /** Markdown section to append to the system prompt — empty string if no resources. */ resourcePromptSection: string; /** * In-process MCP server map to pass as `mcpServers` in `AgentConfig`. * Populated only for the `claude-sdk` driver; undefined for all others. */ mcpServers: Record | undefined; /** * Resolved runtime assets — catalog entries, implicit refs, npm deps, warnings. * Held by the session so live-update paths (add_resource, configure) * can call `runtimeAssets.refresh()` and consume the same shared view. * `null` only when @skaile/workspaces/connectors or skaile.yaml parsing failed. */ runtimeAssets: RuntimeAssetsResult | null; /** Disconnect all connectors, unmount all mounts, and stop watchers. */ dispose: () => Promise; /** * Start filesystem and connector watchers. Call AFTER the transport is * listening — watching can be slow on large directory trees and must not * block session startup. */ startWatching: () => void; /** * Lazily construct (once) and return the session's external MCP manager — * the live target for `runner.add_mcp_server`. Reassigns the same local that * `dispose()` reads, so a runtime-created manager is torn down on teardown. * Serve mode only (requires a capability registry); throws otherwise. */ getOrCreateExternalMcpManager: () => ExternalMcpManager; /** The external MCP manager if constructed (boot or lazy), else null. */ getExternalMcpManager: () => ExternalMcpManager | null; /** * Markdown section for the agent system prompt: an inventory of the * connected MCP servers plus per-server guidance built from the materialized * MCP.md bodies. Empty string when there is neither. */ mcpPromptSection: string; } /** * Callback hooks for filesystem and connector lifecycle events. * * @docLink packages/runner/dev-guide#flow-execution-turn-based-model */ export interface ResourceWatchCallbacks { /** * Called by mountable adapters to report incremental sync progress. * Used by serve.ts to forward sync_status events to the frontend. */ onSyncStatus?: (connectorId: string, status: SyncStatusPayload) => void; /** * Called when a watched connector detects an in-process change. * Used by serve.ts to forward file_changed events to the frontend. * Change events are deduplicated within `dedupTtlMs` per path. */ onFileChanged?: (connectorId: string, event: ConnectorChangeEvent) => void; /** Dedup window for file_changed events in milliseconds. Default: 500. */ dedupTtlMs?: number; /** * Called whenever a connector's connection state changes * (`connecting` → `connected` | `error`, plus `disconnected`). Used by * serve.ts to forward `connector_status` events to the platform — the * channel deferred (background) filesystem mounts report progress through. */ onConnectorStatus?: (event: ConnectorStatusEvent) => void; } /** * Set up all connectors declared in `skaile.yaml` for an agent session. * * Steps: * 1. Register built-in connectors * 2. Load connector declarations from skaile.yaml * 3. Connect all connectors via ConnectorManager (filesystem + tool faces) * 4. Optionally start filesystem/connector watchers (serve.ts use case) * 5. Build the system prompt section (mount paths + connector tools) * 6. Build in-process MCP server (claude-sdk only) * * @param projectDir Workspace root — where skaile.yaml lives * @param driverType Active bridge driver ID: "omp" | "claude-sdk" * @param onLog Optional log callback; silently ignored if omitted * @param watch Optional watch callbacks; watching is skipped if omitted * @param secretProvider Optional secret provider chain for connector field * resolution and `env:NAME` lookups * @param sessionMeta Provider/model/sessionId metadata used for logging * @param tokenMediator Optional callback for `auth: backend` connectors to * mint short-lived tokens. Threaded into `ConnectorManager`. * @param preMintedSecrets Optional v3 pre-minted credential provider — when * supplied, `auth: backend` connectors find their initial * token in-process instead of paying a * `request_access_token` round-trip at connect time. * @param deferFilesystemMounts When true, rclone-backed filesystem-face * connectors (marked `deferMount`) are brought up in the * background instead of being awaited — so a slow remote * can't gate session bootstrap. Their mount path is still * rendered immediately; progress rides `onConnectorStatus`. * @param capabilityRegistry When supplied, external (declarative / recipe-backed) * MCP servers are spawned and connected BY THE RUNNER and * their tools registered into this registry as * `mcp`-origin capabilities — the bridge-agnostic path * that works on every driver. When omitted, the legacy * claude-sdk-only path is used (external stdio servers are * delegated to the SDK's `mcpServers` query option). * @param fallbackConnectorDeclarations Declarations used ONLY when the disk * (`skaile.yaml` + materialized assets) yields zero * connector declarations. Serve mode maps the * `session_init` resolvedConfig here so broker-mode * sessions (no yaml on disk) still get their resources. * Disk-first: any disk declaration disables the fallback. * @param extraMcpDeclarations MCP servers attached at runtime * (`runner.attach_instance`) that `skaile.yaml` does not * declare. Serve mode replays its live-attach registry * here so a session rebuild respawns them instead of * dropping them as undeclared "ghosts". Merged into the * disk set before spawn; a disk declaration of the same * id wins. * @docLink packages/runner/dev-guide#flow-execution-turn-based-model */ export declare function buildAgentResources(projectDir: string, driverType: string, _onLog?: (line: string) => void, watch?: ResourceWatchCallbacks, secretProvider?: unknown, sessionMeta?: { provider?: string; model?: string; sessionId?: string; managedCodex?: boolean; }, tokenMediator?: TokenMediator, preMintedSecrets?: PreMintedSecretProvider, deferFilesystemMounts?: boolean, capabilityRegistry?: CapabilityRegistry, fallbackConnectorDeclarations?: ConnectorDeclaration[], extraMcpDeclarations?: McpServerDeclaration[]): Promise; /** `MCP____AUTH` — cross-repo contract: must mirror the platform's bearer-provisioning key. */ export declare function mcpAuthSecretKey(id: string): string; //# sourceMappingURL=resources.d.ts.map