From 9aed2c41d482e7e094edce74c20567f69f10c92b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A4=A7=E7=8C=AB?= <16399091+binaricat@users.noreply.github.com> Date: Fri, 26 Jun 2026 00:08:39 +0800 Subject: [PATCH] feat(ai): unify capability registry, harness tools, and agent runtime (#1685) * feat(ai): unify capability registry, harness tools, and agent runtime Centralize MCP/CLI/Catty tool surfaces in the capability catalog with codegen, move Catty-only harness tools into catalog with local execution, converge mcpServerBridge builtin dispatch via registry, and wire AgentRuntime with vault bridge, permission grants, and Round 2 catalog parity. Co-authored-by: Cursor * feat(ai): vault host tools, artifact cards, and external agent vault guidance Unify vault host create/import with capability RPC routing, add chat artifact cards with Vault navigation, and align external MCP agents with Catty rules so host creation never falls back to Vault Notes. Co-authored-by: Cursor * fix(ai): address Bugbot findings for streaming, vault bridge, and grants Clear Catty streaming by chat sessionId, read latest vault state via getters on agent mutations, and stack grant persisters so unmounting one AI panel does not break others. Co-authored-by: Cursor * fix(vault): clear pending openNoteId after chat navigation focus NotesManager notifies VaultView once the one-shot openNoteId request is applied so the id does not stick for the rest of the session. Co-authored-by: Cursor * fix(vault): serialize vault agent IPC mutations to prevent lost writes Queue vault agent bridge handlers so concurrent hosts.create or note.create tool calls cannot overwrite each other via stale read-modify-write snapshots. Co-authored-by: Cursor * fix(vault): keep synchronous vault snapshot for agent IPC reads Mirror hosts, notes, and groups in a ref updated immediately on agent writes so serialized mutations see the latest state before React re-renders. Co-authored-by: Cursor * fix(ai): sync Catty MCP sessions and defer runtime cleanup on delete Register terminal sessions with the main process before Catty tool calls, and clear ToolOutputStore only after stopping an active turn when deleting a chat session. Co-authored-by: Cursor * fix(ai): avoid deleting a newer abort controller on late stop Only remove the session abort-controller entry when stop finishes if it still refers to the controller that was stopped. Co-authored-by: Cursor * fix(ai): sync vault snapshot on render and await active turn on stop Refresh vault agent reads when React props change without clobbering in-flight agent writes, and wait for runTurn to finish before clearing ToolOutputStore on session delete. Co-authored-by: Cursor * fix(ai): reload permission grants from storage before auto-approve Re-read persisted grant rules on each approval check so revocations in Settings take effect without requiring a chat hook remount. Co-authored-by: Cursor * fix(ai): sync permission grants to main process immediately on write Push MCP permissionGrantsSnapshot as soon as Always allow persists a rule so the next tool call in the same agent turn sees the grant without waiting for useEffect. Co-authored-by: Cursor --------- Co-authored-by: Cursor --- .github/workflows/test.yml | 5 + AGENTS.md | 48 + App.tsx | 50 +- application/app/AppView.tsx | 14 +- application/i18n/locales/en/ai.ts | 28 +- application/i18n/locales/ru/ai.ts | 28 +- application/i18n/locales/zh-CN/ai.ts | 28 +- .../state/useAIPermissionGrantsState.ts | 104 ++ application/state/useVaultAgentBridge.ts | 103 ++ components/AIChatPanelContent.tsx | 18 +- components/AIChatSidePanel.tsx | 56 +- components/AIChatSidePanel.types.ts | 8 + components/TerminalLayer.tsx | 11 + components/VaultView.tsx | 17 +- components/ai-elements/tool-call.tsx | 50 +- components/ai/ChatInput.tsx | 9 +- components/ai/ChatMessageList.tsx | 140 ++- components/ai/externalAgentHistory.ts | 448 +------ components/ai/hooks/aiChatStreamingSupport.ts | 11 + components/ai/hooks/useAIChatStreaming.ts | 1054 +--------------- .../toolArtifacts/VaultArtifactCard.test.tsx | 42 + .../ai/toolArtifacts/VaultArtifactCard.tsx | 130 ++ .../VaultArtifactNavigationContext.tsx | 89 ++ .../toolArtifacts/VaultArtifactToolResult.tsx | 38 + .../formatVaultToolTooltip.test.ts | 18 + .../toolArtifacts/formatVaultToolTooltip.ts | 37 + .../vaultArtifactPresentation.test.ts | 45 + .../vaultArtifactPresentation.tsx | 115 ++ .../toolArtifacts/vaultToolArtifact.test.ts | 74 ++ .../ai/toolArtifacts/vaultToolArtifact.ts | 183 +++ components/notes/NotesManager.tsx | 12 +- components/settings/tabs/SettingsAITab.tsx | 18 + .../tabs/ai/PermissionGrantsSettings.tsx | 255 ++++ .../settings/tabs/ai/managedAgentState.ts | 7 +- .../TerminalLayerSidePanelSection.tsx | 8 + .../terminalLayer/TerminalLayerSupport.tsx | 66 + .../terminalLayer/TerminalLayerTabBridge.tsx | 3 + .../terminalLayerStableSnapshot.ts | 2 + .../terminalLayer/useTerminalAiContexts.ts | 164 +-- components/terminalLayerMemo.ts | 3 + components/vault/VaultViewLayout.tsx | 4 +- components/vault/useVaultImportHandlers.ts | 15 +- domain/vaultHostCreate.test.ts | 58 + domain/vaultHostCreate.ts | 190 +++ domain/vaultImport.test.ts | 34 +- domain/vaultImport.ts | 122 +- electron/bridges/aiBridge.cjs | 32 +- .../bridges/aiBridge.externalContext.test.cjs | 16 + electron/bridges/aiBridge.test.cjs | 1 + .../bridges/aiBridge/agentProcessHandlers.cjs | 6 + .../bridges/aiBridge/cattyExecHandlers.cjs | 27 + .../aiBridge/sdk/sdkStreamHandlers.cjs | 36 +- .../bridges/aiBridge/vaultAgentBridge.cjs | 75 ++ electron/bridges/mcpServerBridge.cjs | 177 ++- .../mcpServerBridge/builtinRpcHandlers.cjs | 35 + .../builtinRpcHandlers.test.cjs | 67 + .../mcpServerBridge/capabilityRpcDispatch.cjs | 128 ++ .../capabilityRpcDispatch.test.cjs | 188 +++ electron/capabilities/adapters/cliAdapter.cjs | 79 +- .../capabilities/adapters/cliAdapter.test.cjs | 43 +- electron/capabilities/catalog/harness.cjs | 100 ++ electron/capabilities/catalog/index.cjs | 3 + .../capabilities/catalog/integrity.test.cjs | 12 +- electron/capabilities/catalog/portforward.cjs | 8 +- electron/capabilities/catalog/sftp.cjs | 2 + electron/capabilities/catalog/vault.cjs | 151 ++- .../capabilities/codegen/mcpToolRegistry.cjs | 184 +++ .../capabilities/codegen/toolSurfaces.cjs | 204 ++++ .../codegen/toolSurfaces.test.cjs | 162 +++ electron/capabilities/constants.cjs | 16 +- electron/capabilities/dispatch.test.cjs | 4 +- electron/capabilities/policy.cjs | 27 + electron/capabilities/policy.test.cjs | 49 + electron/capabilities/registry.cjs | 10 + electron/capabilities/registry.test.cjs | 12 +- electron/capabilities/schemas/toolInputs.cjs | 207 ++++ .../services/portforwardService.cjs | 41 +- .../services/portforwardService.test.cjs | 28 + .../capabilities/services/vaultService.cjs | 96 +- .../services/vaultService.test.cjs | 20 +- electron/capabilities/types.cjs | 3 + .../netcatty-tool-cli.capabilities.test.cjs | 2 + electron/cli/netcatty-tool-cli.cjs | 95 +- electron/mcp/catalogToolMetadata.cjs | 20 + electron/mcp/netcatty-mcp-server.cjs | 179 +-- electron/preload/api.cjs | 17 + electron/shared/approvalConstants.cjs | 10 + electron/shared/pathLikeCommand.cjs | 18 + electron/shared/permissionGrants.cjs | 133 ++ electron/shared/sdkSessionIdentity.cjs | 32 + electron/shared/vaultAgentGuidance.cjs | 27 + electron/shared/vaultAgentGuidance.test.cjs | 24 + infrastructure/ai/agentKinds.ts | 12 + infrastructure/ai/cattyAgent/executor.ts | 6 + infrastructure/ai/cattyAgent/systemPrompt.ts | 46 +- infrastructure/ai/contextCompaction.ts | 6 +- .../ai/harness/agentEventAdapter.ts | 171 +++ .../ai/harness/agentRuntime.test.ts | 68 ++ infrastructure/ai/harness/agentRuntime.ts | 177 +++ infrastructure/ai/harness/agentStop.test.ts | 40 + infrastructure/ai/harness/agentStop.ts | 73 ++ .../ai/harness/capabilityTools.test.ts | 31 + infrastructure/ai/harness/capabilityTools.ts | 419 +++++++ infrastructure/ai/harness/cattyRuntime.ts | 124 ++ .../ai/harness/contextManager.test.ts | 102 ++ infrastructure/ai/harness/contextManager.ts | 251 ++++ .../ai/harness/contextReplay.test.ts | 37 + .../ai/harness/externalBridgeContext.ts | 445 +++++++ .../ai/harness/generated/cattyToolSpecs.json | 1079 +++++++++++++++++ .../generated/globalAgentToolSpecs.json | 985 +++++++++++++++ .../ai/harness/globalAgentRuntime.ts | 11 + infrastructure/ai/harness/index.ts | 99 ++ .../ai/harness/permissionGrants.test.ts | 87 ++ infrastructure/ai/harness/permissionGrants.ts | 286 +++++ .../ai/harness/sdkSessionIdentity.ts | 35 + .../ai/harness/terminalCompression.ts | 93 ++ infrastructure/ai/harness/tokenEstimator.ts | 71 ++ .../ai/harness/toolOutputStore.test.ts | 26 + infrastructure/ai/harness/toolOutputStore.ts | 87 ++ infrastructure/ai/harness/toolResultDedup.ts | 54 + infrastructure/ai/harness/traceStore.ts | 60 + .../turnDrivers/cattyMessageBuilder.ts | 266 ++++ .../turnDrivers/cattyStreamProcessor.ts | 358 ++++++ .../ai/harness/turnDrivers/cattyTurnDriver.ts | 318 +++++ .../turnDrivers/externalSdkTurnDriver.ts | 174 +++ .../ai/harness/turnDrivers/types.ts | 139 +++ infrastructure/ai/harness/types.ts | 145 +++ infrastructure/ai/managedAgents.ts | 22 +- infrastructure/ai/quickMessages.ts | 13 + infrastructure/ai/sdk/tools.ts | 196 +-- infrastructure/ai/sdkAgentAdapter.ts | 63 +- infrastructure/ai/shared/approvalConstants.ts | 8 + infrastructure/ai/shared/approvalGate.ts | 161 ++- infrastructure/ai/shared/bashArity.test.ts | 29 + infrastructure/ai/shared/bashArity.ts | 163 +++ infrastructure/ai/shared/pathLikeCommand.ts | 12 + .../ai/shared/shellCommandGrant.test.ts | 23 + infrastructure/ai/shared/shellCommandGrant.ts | 65 + .../ai/vaultAgentBridgeClient.test.ts | 310 +++++ infrastructure/ai/vaultAgentBridgeClient.ts | 582 +++++++++ infrastructure/config/storageKeys.ts | 2 + package-lock.json | 93 +- package.json | 3 +- scripts/generate-capability-tools.cjs | 27 + scripts/generate-capability-tools.test.cjs | 33 + types/global/netcatty-bridge-ai.d.ts | 8 + 146 files changed, 12689 insertions(+), 2273 deletions(-) create mode 100644 application/state/useAIPermissionGrantsState.ts create mode 100644 application/state/useVaultAgentBridge.ts create mode 100644 components/ai/toolArtifacts/VaultArtifactCard.test.tsx create mode 100644 components/ai/toolArtifacts/VaultArtifactCard.tsx create mode 100644 components/ai/toolArtifacts/VaultArtifactNavigationContext.tsx create mode 100644 components/ai/toolArtifacts/VaultArtifactToolResult.tsx create mode 100644 components/ai/toolArtifacts/formatVaultToolTooltip.test.ts create mode 100644 components/ai/toolArtifacts/formatVaultToolTooltip.ts create mode 100644 components/ai/toolArtifacts/vaultArtifactPresentation.test.ts create mode 100644 components/ai/toolArtifacts/vaultArtifactPresentation.tsx create mode 100644 components/ai/toolArtifacts/vaultToolArtifact.test.ts create mode 100644 components/ai/toolArtifacts/vaultToolArtifact.ts create mode 100644 components/settings/tabs/ai/PermissionGrantsSettings.tsx create mode 100644 domain/vaultHostCreate.test.ts create mode 100644 domain/vaultHostCreate.ts create mode 100644 electron/bridges/aiBridge.externalContext.test.cjs create mode 100644 electron/bridges/aiBridge/vaultAgentBridge.cjs create mode 100644 electron/bridges/mcpServerBridge/builtinRpcHandlers.cjs create mode 100644 electron/bridges/mcpServerBridge/builtinRpcHandlers.test.cjs create mode 100644 electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs create mode 100644 electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs create mode 100644 electron/capabilities/catalog/harness.cjs create mode 100644 electron/capabilities/codegen/mcpToolRegistry.cjs create mode 100644 electron/capabilities/codegen/toolSurfaces.cjs create mode 100644 electron/capabilities/codegen/toolSurfaces.test.cjs create mode 100644 electron/capabilities/schemas/toolInputs.cjs create mode 100644 electron/capabilities/services/portforwardService.test.cjs create mode 100644 electron/mcp/catalogToolMetadata.cjs create mode 100644 electron/shared/approvalConstants.cjs create mode 100644 electron/shared/pathLikeCommand.cjs create mode 100644 electron/shared/permissionGrants.cjs create mode 100644 electron/shared/sdkSessionIdentity.cjs create mode 100644 electron/shared/vaultAgentGuidance.cjs create mode 100644 electron/shared/vaultAgentGuidance.test.cjs create mode 100644 infrastructure/ai/agentKinds.ts create mode 100644 infrastructure/ai/harness/agentEventAdapter.ts create mode 100644 infrastructure/ai/harness/agentRuntime.test.ts create mode 100644 infrastructure/ai/harness/agentRuntime.ts create mode 100644 infrastructure/ai/harness/agentStop.test.ts create mode 100644 infrastructure/ai/harness/agentStop.ts create mode 100644 infrastructure/ai/harness/capabilityTools.test.ts create mode 100644 infrastructure/ai/harness/capabilityTools.ts create mode 100644 infrastructure/ai/harness/cattyRuntime.ts create mode 100644 infrastructure/ai/harness/contextManager.test.ts create mode 100644 infrastructure/ai/harness/contextManager.ts create mode 100644 infrastructure/ai/harness/contextReplay.test.ts create mode 100644 infrastructure/ai/harness/externalBridgeContext.ts create mode 100644 infrastructure/ai/harness/generated/cattyToolSpecs.json create mode 100644 infrastructure/ai/harness/generated/globalAgentToolSpecs.json create mode 100644 infrastructure/ai/harness/globalAgentRuntime.ts create mode 100644 infrastructure/ai/harness/index.ts create mode 100644 infrastructure/ai/harness/permissionGrants.test.ts create mode 100644 infrastructure/ai/harness/permissionGrants.ts create mode 100644 infrastructure/ai/harness/sdkSessionIdentity.ts create mode 100644 infrastructure/ai/harness/terminalCompression.ts create mode 100644 infrastructure/ai/harness/tokenEstimator.ts create mode 100644 infrastructure/ai/harness/toolOutputStore.test.ts create mode 100644 infrastructure/ai/harness/toolOutputStore.ts create mode 100644 infrastructure/ai/harness/toolResultDedup.ts create mode 100644 infrastructure/ai/harness/traceStore.ts create mode 100644 infrastructure/ai/harness/turnDrivers/cattyMessageBuilder.ts create mode 100644 infrastructure/ai/harness/turnDrivers/cattyStreamProcessor.ts create mode 100644 infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts create mode 100644 infrastructure/ai/harness/turnDrivers/externalSdkTurnDriver.ts create mode 100644 infrastructure/ai/harness/turnDrivers/types.ts create mode 100644 infrastructure/ai/harness/types.ts create mode 100644 infrastructure/ai/shared/approvalConstants.ts create mode 100644 infrastructure/ai/shared/bashArity.test.ts create mode 100644 infrastructure/ai/shared/bashArity.ts create mode 100644 infrastructure/ai/shared/pathLikeCommand.ts create mode 100644 infrastructure/ai/shared/shellCommandGrant.test.ts create mode 100644 infrastructure/ai/shared/shellCommandGrant.ts create mode 100644 infrastructure/ai/vaultAgentBridgeClient.test.ts create mode 100644 infrastructure/ai/vaultAgentBridgeClient.ts create mode 100644 scripts/generate-capability-tools.cjs create mode 100644 scripts/generate-capability-tools.test.cjs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cf7e49fe7..cd065fa13 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,5 +38,10 @@ jobs: - name: Lint run: npm run lint + - name: Verify generated Catty tool specs + run: | + npm run generate:capability-tools + git diff --exit-code infrastructure/ai/harness/generated/cattyToolSpecs.json + - name: Test run: npm test diff --git a/AGENTS.md b/AGENTS.md index 4640bd4f3..52198855a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,6 +22,54 @@ This project is wired around three layers: domain (pure logic), application stat - `App.tsx` wires hooks to components; no business logic should live in components beyond view glue. - Local storage keys are centralized in `infrastructure/config/storageKeys.ts`; avoid ad-hoc `localStorage` calls elsewhere. +## AI Agent Harness (`infrastructure/ai/harness/`) + +Turn orchestration is centralized in **AgentRuntime**; the React hook `useAIChatStreaming` only manages UI state and delegates `runTurn` / `stopTurn`. + +| Layer | Module | Role | +|-------|--------|------| +| Runtime | `agentRuntime.ts`, `globalAgentRuntime.ts` | Turn lifecycle, trace fan-out, per-turn ToolOutputStore / ToolResultDedup | +| Drivers | `turnDrivers/cattyTurnDriver.ts`, `turnDrivers/externalSdkTurnDriver.ts` | Catty `streamText` + External SDK IPC; emit unified `AgentEvent`s | +| Context | `contextManager.ts`, `tokenEstimator.ts`, `cattyRuntime.ts` | Pre-turn / 413 compaction, `prepareStepContext`, provider-aware token estimates | +| Tools | `capabilityTools.ts`, `toolOutputStore.ts`, `toolResultDedup.ts` | Catalog tools, truncated output handles (`tool_output_read`), duplicate-read notices | +| Trace | `traceStore.ts`, `agentEventAdapter.ts` | Session event log incl. `usage` and `CompactionTrace` | + +**Stop** always goes through `stopAgentTurn()` (UI, `/stop`, MCP). Do not add parallel abort paths in hooks. + +### Capability exposure (Round 2 + gap fill) + +Single source of truth: `electron/capabilities/catalog/` + `electron/capabilities/codegen/toolSurfaces.cjs`. + +**Agent kinds** (where an in-app agent runs — orthogonal to MCP/CLI/RPC surfaces): + +| Kind | UI | Tool list | Notes | +|------|-----|-----------|--------| +| `sidebar` | Chat side panel (Catty) | `listAgentToolSpecs('sidebar')` → `cattyToolSpecs.json` | Includes `harness.*` renderer-local tools (`surfaces.catty`) | +| `global` | Future app-wide agent | `listAgentToolSpecs('global')` → `globalAgentToolSpecs.json` | Shared RPC tools (terminal, SFTP, vault, …); **no** sidebar-only harness tools unless opted in | + +Placement rules (`resolveAgentKinds` in `toolSurfaces.cjs`): + +- Explicit `agentKinds` on a catalog entry overrides inference. +- `surfaces.globalAgent` only → global agent (future global-only local tools). +- `surfaces.catty` only (harness) → sidebar only. +- RPC/MCP-backed tools → both agents unless restricted via `agentKinds`. + +| Surface | Codegen / consumer | Notes | +|---------|-------------------|--------| +| Catty (sidebar) tools | `npm run generate:capability-tools` → `infrastructure/ai/harness/generated/cattyToolSpecs.json` | Sidebar agent tool set. CI verifies JSON drift. | +| Global agent tools | same script → `globalAgentToolSpecs.json` | Prepared for future global agent runtime; shared RPC tools only today. | +| MCP stdio | `electron/capabilities/codegen/mcpToolRegistry.cjs` → `electron/mcp/netcatty-mcp-server.cjs` | Registry-driven; external agents. Harness tools are **not** on MCP. | +| CLI | `electron/cli/netcatty-tool-cli.cjs` + `electron/capabilities/adapters/cliAdapter.cjs` | **30** catalog commands; exec/sftp/session remain special-case; vault/portforward/snippets use catalog fallback dispatch | +| RPC dispatch | `electron/bridges/mcpServerBridge.cjs` + `capabilityRpcDispatch.cjs` | `netcatty/*` builtin handlers via `buildBuiltinRpcHandlerRegistry` (catalog-aligned); `public/*`, `vault/*`, `portforward/*` → services | +| Vault bridge | `electron/bridges/aiBridge/vaultAgentBridge.cjs` + `infrastructure/ai/vaultAgentBridgeClient.ts` | Renderer vault state; **never** returns password/privateKey | +| AI context | `buildAITerminalSessionInfo` + `useTerminalAiContexts` | Per-session `hostChain` + `activePortForwards`; mirrored in `getContext` and Catty system prompt | + +**Policy:** SFTP writes/transfers, `portforward_start`, and `host_notes_set` require confirm-mode approval. Observer mode blocks writes. + +**Handles:** `ToolOutputStore` persists across turns per chat session; cleared on chat session delete. Large `sftp.read` results spill to `tool_output_read`. + +**Harness domain (`catalog/harness.cjs`):** Catty-only surface (`surfaces.catty.toolName`). Registered in the capability catalog but executed locally in `capabilityTools.executeLocalCattyCapability` (not MCP/CLI). `harness.web.search` is omitted when web search is not configured. + ## Extending the System 1) **New domain logic**: Add pure functions/types under `domain/`; avoid side effects. 2) **New stateful behavior**: Wrap it in a hook under `application/state/`; keep external I/O behind adapters. diff --git a/App.tsx b/App.tsx index 05bcd84be..9e6c704dd 100755 --- a/App.tsx +++ b/App.tsx @@ -7,6 +7,7 @@ import { useSessionState } from './application/state/useSessionState'; import { useSettingsState } from './application/state/useSettingsState'; import { useUpdateCheck } from './application/state/useUpdateCheck'; import { useVaultState } from './application/state/useVaultState'; +import { useVaultAgentBridge } from './application/state/useVaultAgentBridge'; import { useWindowControls } from './application/state/useWindowControls'; import { useEditorTabs } from './application/state/editorTabStore'; import { @@ -702,6 +703,12 @@ function App({ settings }: { settings: SettingsState }) { noteId: string; requestId: number; } | null>(null); + const vaultFocusRequestIdRef = useRef(0); + const [vaultFocusRequest, setVaultFocusRequest] = useState<{ + type: 'note'; + noteId: string; + requestId: number; + } | null>(null); // Populated below so the hotkey dispatcher can open the Settings window // even though `handleOpenSettings` is declared further down in the file. const handleOpenSettingsRef = useRef<() => void>(() => {}); @@ -897,6 +904,23 @@ function App({ settings }: { settings: SettingsState }) { return materializeHostProxyProfile(withGroupDefaults, proxyProfiles); }, [groupConfigs, proxyProfileIdSet, proxyProfiles]); + useVaultAgentBridge({ + hosts, + snippets, + portForwardingRules, + keys, + identities, + terminalSettings, + resolveEffectiveHost, + updateHosts, + customGroups, + updateCustomGroups, + notes, + updateNotes, + startTunnel, + stopTunnel, + }); + // Wrapper to connect to host with logging const handleConnectToHost = useCallback((host: Host) => { return handleConnectToHostImpl(() => ({ addConnectionLog, connectToHost, host, identities, keys, resolveEffectiveHost, resolveHostAuth, systemInfoRef }), host); }, [addConnectionLog, connectToHost, resolveEffectiveHost, identities, keys]); @@ -953,6 +977,30 @@ function App({ settings }: { settings: SettingsState }) { return tabId; }, [handleConnectToHost]); + const handleOpenVaultNoteFromChat = useCallback((noteId: string) => { + vaultFocusRequestIdRef.current += 1; + setVaultFocusRequest({ + type: 'note', + noteId, + requestId: vaultFocusRequestIdRef.current, + }); + setNavigateToSection('notes'); + setActiveTabId('vault'); + }, [setActiveTabId]); + + const handleOpenVaultHostFromChat = useCallback((hostId: string) => { + const host = hosts.find((candidate) => candidate.id === hostId); + if (!host) return; + setDeepLinkHostDraft(host); + setNavigateToSection('hosts'); + setActiveTabId('vault'); + }, [hosts, setActiveTabId]); + + const handleOpenVaultSectionFromChat = useCallback((section: 'notes' | 'hosts') => { + setNavigateToSection(section); + setActiveTabId('vault'); + }, [setActiveTabId]); + // Wrap updateSessionStatus to track lastConnectedAt on successful connection const handleSessionStatusChange = useCallback((sessionId: string, status: TerminalSession['status']) => { updateSessionStatus(sessionId, status); @@ -1091,7 +1139,7 @@ function App({ settings }: { settings: SettingsState }) { logViews={logViews} t={t} /> - + ); } diff --git a/application/app/AppView.tsx b/application/app/AppView.tsx index b7739885b..83e2687f8 100644 --- a/application/app/AppView.tsx +++ b/application/app/AppView.tsx @@ -60,20 +60,20 @@ export function AppView({ ctx }: { ctx: AppViewContext }) { customGroups, currentTerminalTheme, deepLinkHostDraft, deleteConnectionLog, draggingSessionId, effectiveKnownHosts, editorTabs, editorWordWrap, emptyVaultConflict, followAppTerminalTheme, groupConfigs, handleAddKnownHost, handleConnectSerial, handleConnectToHost, handleCreateLocalTerminal, handleDeleteHost, handleEndSessionDrag, handleFollowAppTerminalThemeChange, handleHostConnectWithProtocolCheck, handleHotkeyAction, handleKeyboardInteractiveCancel, handleKeyboardInteractiveSubmit, - handleOpenHostFromVaultNote, handleOpenQuickSwitcher, handleOpenSettings, handleRootContextMenu, handlePassphraseCancel, handlePassphraseSkip, handlePassphraseSubmit, handleProtocolSelect, + handleOpenHostFromVaultNote, handleOpenQuickSwitcher, handleOpenSettings, handleOpenVaultHostFromChat, handleOpenVaultNoteFromChat, handleOpenVaultSectionFromChat, handleRootContextMenu, handlePassphraseCancel, handlePassphraseSkip, handlePassphraseSubmit, handleProtocolSelect, handleRequestCloseEditorTabRef, handleSessionStatusChange, handleSyncNowManual, handleTerminalDataCapture, handleToggleTheme, handleUpdateHostFromTerminal, hostById, hosts, hotkeyScheme, identities, importOrReuseKey, isBroadcastEnabled, isCreateWorkspaceOpen, isMacClient, isQuickSwitcherOpen, keyBindings, keyboardInteractiveQueue, keys, logViews, managedSources, navigateToSection, noteGroups, notes, openLogView, openNoteRequest, orderedTabsWithEditors, orphanSessions, - passphraseQueue, protocolSelectHost, proxyProfiles, quickResults, quickSearch, removeSessionFromWorkspace, reorderWorkTabs, reorderWorkspaceSessions, resetSessionRename, + passphraseQueue, protocolSelectHost, proxyProfiles, portForwardingRules, quickResults, quickSearch, removeSessionFromWorkspace, reorderWorkTabs, reorderWorkspaceSessions, resetSessionRename, resetWorkspaceRename, resolveEmptyVaultConflict, resolvedTheme, runSnippet, sessionLogsDir, sessionLogsEnabled, sessionLogsFormat, sessionLogsTimestampsEnabled, sessionRenameTarget, sshDebugLogsEnabled, sessionRenameValue, sessions, setActiveTabId, setAddToWorkspaceDialog, setDeepLinkHostDraft, setDraggingSessionId, setEditorWordWrap, setIsCreateWorkspaceOpen, setIsQuickSwitcherOpen, - setNavigateToSection, setProtocolSelectHost, setQuickSearch, setSessionRenameValue, setTerminalFontFamilyId, setTerminalFontSize, setTerminalThemeId, updateSessionFontSize, updateSessionRestoreCwd, updateSessionDynamicTitle, updateSessionCodingCliProvider, clearSessionFontSizeOverride, + setNavigateToSection, setProtocolSelectHost, setQuickSearch, setSessionRenameValue, setTerminalFontFamilyId, setTerminalFontSize, setTerminalThemeId, setVaultFocusRequest, updateSessionFontSize, updateSessionRestoreCwd, updateSessionDynamicTitle, updateSessionCodingCliProvider, clearSessionFontSizeOverride, setWorkspaceFocusedSession, setWorkspaceRenameValue, settings, sftpAutoOpenSidebar, sftpFollowTerminalCwd, setSftpFollowTerminalCwd, sftpAutoSync, sftpDefaultViewMode, sftpDoubleClickBehavior, sftpShowHiddenFiles, sftpUseCompressedUpload, shellHistory, snippetPackages, snippets, splitSessionWithCurrentShell, startSessionRename, startWorkspaceRename, submitSessionRename, submitWorkspaceRename, t, terminalFontFamilyId, terminalFontSize, terminalSettings, terminalThemeId, themeById, toggleBroadcast, toggleConnectionLogSaved, toggleScriptsSidePanelRef, toggleSidePanelRef, toggleWorkspaceViewMode, unmanageSource, updateConnectionLog, updateCustomGroups, updateGroupConfigs, updateHostDistro, updateHosts, updateIdentities, updateKeys, updateKnownHosts, updateManagedSources, - updateNoteGroups, updateNotes, updateProxyProfiles, updateSnippetPackages, updateSnippets, updateSplitSizes, updateTerminalSetting, workspaceRenameTarget, workspaceRenameValue, workspaces, + updateNoteGroups, updateNotes, updateProxyProfiles, updateSnippetPackages, updateSnippets, updateSplitSizes, updateTerminalSetting, vaultFocusRequest, workspaceRenameTarget, workspaceRenameValue, workspaces, VaultViewContainer, SftpViewMount, TerminalLayerMount, LogViewWrapper, } = ctx; @@ -253,6 +253,8 @@ export function AppView({ ctx }: { ctx: AppViewContext }) { onNavigateToSectionHandled={() => setNavigateToSection(null)} deepLinkHostDraft={deepLinkHostDraft} onDeepLinkHostDraftHandled={() => setDeepLinkHostDraft(null)} + vaultFocusRequest={vaultFocusRequest} + onVaultFocusRequestHandled={() => setVaultFocusRequest(null)} terminalSettings={terminalSettings} /> @@ -280,6 +282,7 @@ export function AppView({ ctx }: { ctx: AppViewContext }) { (STORAGE_KEY_AI_PERMISSION_GRANTS)); +} + +function syncPermissionGrantsToMainProcess(rules: PermissionGrantRule[]): void { + void getAIBridge()?.aiMcpSyncPermissionGrants?.(rules)?.catch(() => {}); +} + +function writePermissionGrants(rules: PermissionGrantRule[]): void { + localStorageAdapter.write(STORAGE_KEY_AI_PERMISSION_GRANTS, rules); + setActivePermissionGrants(rules); + syncPermissionGrantsToMainProcess(rules); + emitAIStateChanged(STORAGE_KEY_AI_PERMISSION_GRANTS); +} + +export function useAIPermissionGrantsState() { + const [rules, setRulesRaw] = useState(() => { + const initial = readPermissionGrants(); + setActivePermissionGrants(initial); + return initial; + }); + + const setRules = useCallback((value: PermissionGrantRule[] | ((prev: PermissionGrantRule[]) => PermissionGrantRule[])) => { + setRulesRaw((prev) => { + const next = typeof value === 'function' ? value(prev) : value; + writePermissionGrants(next); + return next; + }); + }, []); + + const addGrant = useCallback((rule: PermissionGrantRule) => { + setRules((prev) => [...prev, rule]); + }, [setRules]); + + const updateGrant = useCallback(( + id: string, + updates: Partial>, + ) => { + setRules((prev) => prev.map((rule) => ( + rule.id === id ? { ...rule, ...updates } : rule + ))); + }, [setRules]); + + const removeGrant = useCallback((id: string) => { + setRules((prev) => prev.filter((rule) => rule.id !== id)); + }, [setRules]); + + const importGrants = useCallback((raw: unknown, mode: 'merge' | 'replace' = 'replace') => { + const imported = sanitizePermissionGrants(raw); + setRules((prev) => (mode === 'merge' ? [...prev, ...imported] : imported)); + }, [setRules]); + + const exportGrants = useCallback((): PermissionGrantRule[] => { + return readPermissionGrants(); + }, []); + + useEffect(() => { + const syncFromStorageKey = (key: string | null) => { + if (key !== STORAGE_KEY_AI_PERMISSION_GRANTS) return; + const next = readPermissionGrants(); + setActivePermissionGrants(next); + syncPermissionGrantsToMainProcess(next); + setRulesRaw(next); + }; + + const handleStorage = (event: StorageEvent) => syncFromStorageKey(event.key); + const handleLocalStateChanged = (event: Event) => { + syncFromStorageKey((event as CustomEvent<{ key?: string }>).detail?.key ?? null); + }; + + window.addEventListener('storage', handleStorage); + window.addEventListener(AI_STATE_CHANGED_EVENT, handleLocalStateChanged); + return () => { + window.removeEventListener('storage', handleStorage); + window.removeEventListener(AI_STATE_CHANGED_EVENT, handleLocalStateChanged); + }; + }, []); + + useEffect(() => { + const bridge = getAIBridge(); + bridge?.aiMcpSyncPermissionGrants?.(rules); + }, [rules]); + + return useMemo(() => ({ + permissionGrants: rules, + setPermissionGrants: setRules, + addGrant, + updateGrant, + removeGrant, + importGrants, + exportGrants, + }), [rules, setRules, addGrant, updateGrant, removeGrant, importGrants, exportGrants]); +} diff --git a/application/state/useVaultAgentBridge.ts b/application/state/useVaultAgentBridge.ts new file mode 100644 index 000000000..720a23417 --- /dev/null +++ b/application/state/useVaultAgentBridge.ts @@ -0,0 +1,103 @@ +import { useEffect, useRef } from 'react'; +import type { Host, Identity, PortForwardingRule, Snippet, SSHKey, TerminalSettings, VaultNote } from '../../domain/models'; +import { + handleVaultAgentOp, + registerVaultAgentHandler, + setupVaultAgentBridge, + type VaultAgentApiDeps, +} from '../../infrastructure/ai/vaultAgentBridgeClient'; + +export interface UseVaultAgentBridgeInput { + hosts: Host[]; + snippets: Snippet[]; + portForwardingRules: PortForwardingRule[]; + keys: SSHKey[]; + identities: Identity[]; + terminalSettings?: Pick; + resolveEffectiveHost: (host: Host) => Host; + updateHosts: (hosts: Host[]) => void; + customGroups: string[]; + updateCustomGroups: (groups: string[]) => void; + notes: VaultNote[]; + updateNotes: (notes: VaultNote[]) => void; + startTunnel: VaultAgentApiDeps['startTunnel']; + stopTunnel: VaultAgentApiDeps['stopTunnel']; +} + +type VaultAgentSnapshot = { + hosts: Host[]; + notes: VaultNote[]; + customGroups: string[]; +}; + +export function useVaultAgentBridge(input: UseVaultAgentBridgeInput): void { + const inputRef = useRef(input); + inputRef.current = input; + + const vaultSnapshotRef = useRef({ + hosts: input.hosts, + notes: input.notes, + customGroups: input.customGroups, + }); + const lastSyncedVaultInputRef = useRef({ + hosts: input.hosts, + notes: input.notes, + customGroups: input.customGroups, + }); + + if ( + input.hosts !== lastSyncedVaultInputRef.current.hosts + || input.notes !== lastSyncedVaultInputRef.current.notes + || input.customGroups !== lastSyncedVaultInputRef.current.customGroups + ) { + vaultSnapshotRef.current = { + hosts: input.hosts, + notes: input.notes, + customGroups: input.customGroups, + }; + lastSyncedVaultInputRef.current = { + hosts: input.hosts, + notes: input.notes, + customGroups: input.customGroups, + }; + } + + useEffect(() => { + registerVaultAgentHandler(async (op, params) => { + const current = inputRef.current; + return handleVaultAgentOp(op, params, { + getHosts: () => vaultSnapshotRef.current.hosts, + getNotes: () => vaultSnapshotRef.current.notes, + getCustomGroups: () => vaultSnapshotRef.current.customGroups, + snippets: current.snippets, + portForwardingRules: current.portForwardingRules, + keys: current.keys, + identities: current.identities, + terminalSettings: current.terminalSettings, + resolveEffectiveHost: current.resolveEffectiveHost, + updateHostNotes: (hostId, notes) => { + const nextHosts = vaultSnapshotRef.current.hosts.map((host) => ( + host.id === hostId ? { ...host, notes } : host + )); + vaultSnapshotRef.current.hosts = nextHosts; + current.updateHosts(nextHosts); + }, + updateCustomGroups: (groups) => { + vaultSnapshotRef.current.customGroups = groups; + current.updateCustomGroups(groups); + }, + updateHosts: (hosts) => { + vaultSnapshotRef.current.hosts = hosts; + current.updateHosts(hosts); + }, + updateNotes: (notes) => { + vaultSnapshotRef.current.notes = notes; + current.updateNotes(notes); + }, + startTunnel: current.startTunnel, + stopTunnel: current.stopTunnel, + }); + }); + return setupVaultAgentBridge(); + }, []); +} diff --git a/components/AIChatPanelContent.tsx b/components/AIChatPanelContent.tsx index 5ae1a0bab..7500a8931 100644 --- a/components/AIChatPanelContent.tsx +++ b/components/AIChatPanelContent.tsx @@ -1,6 +1,7 @@ import React, { type Dispatch, type SetStateAction } from 'react'; import { History, Plus } from 'lucide-react'; import type { AIPermissionMode, AISession, ChatMessage, DiscoveredAgent, ExternalAgentConfig, AgentModelPreset, ProviderConfig, UploadedFile } from '../infrastructure/ai/types'; +import type { Host, VaultNote } from '../types'; import type { UserSkillOption } from './ai/userSkillsState'; import type { AIQuickMessage } from '../infrastructure/ai/quickMessages'; import { Button } from './ui/button'; @@ -71,6 +72,11 @@ interface AIChatPanelContentProps { removeSelectedUserSkill: (slug: string) => void; globalPermissionMode: AIPermissionMode; setGlobalPermissionMode?: (mode: AIPermissionMode) => void; + notes?: VaultNote[]; + hosts?: Host[]; + onOpenVaultNote?: (noteId: string) => void; + onOpenVaultHost?: (hostId: string) => void; + onOpenVaultSection?: (section: 'notes' | 'hosts') => void; } export const AIChatPanelContent: React.FC = ({ @@ -118,7 +124,12 @@ export const AIChatPanelContent: React.FC = ({ addSelectedUserSkill, removeSelectedUserSkill, globalPermissionMode, - setGlobalPermissionMode + setGlobalPermissionMode, + notes = [], + hosts = [], + onOpenVaultNote, + onOpenVaultHost, + onOpenVaultSection, }) => { const hiddenParts = getAIPanelDiagnosticHiddenParts(); const hideHeader = isAIPanelDiagnosticPartHidden('header', hiddenParts); @@ -199,6 +210,11 @@ export const AIChatPanelContent: React.FC = ({ messages={messages} isStreaming={isStreaming} activeSessionId={activeSessionId} + notes={notes} + hosts={hosts} + onOpenVaultNote={onOpenVaultNote} + onOpenVaultHost={onOpenVaultHost} + onOpenVaultSection={onOpenVaultSection} /> )} diff --git a/components/AIChatSidePanel.tsx b/components/AIChatSidePanel.tsx index 241640738..53dc51352 100644 --- a/components/AIChatSidePanel.tsx +++ b/components/AIChatSidePanel.tsx @@ -14,7 +14,7 @@ import type { } from '../infrastructure/ai/types'; import type { ExecutorContext } from '../infrastructure/ai/cattyAgent/executor'; import { getAgentModelPresets } from '../infrastructure/ai/types'; -import { getExternalAgentSdkBackend, matchesManagedAgentConfig } from '../infrastructure/ai/managedAgents'; +import { getExternalAgentSdkBackend, getManualAgentCommand, matchesManagedAgentConfig } from '../infrastructure/ai/managedAgents'; import { useAgentDiscovery } from '../application/state/useAgentDiscovery'; import { getReadyUserSkillOptions, @@ -48,7 +48,10 @@ import { import { getScopedHistorySessions } from './ai/scopedHistorySessions'; import { buildExternalAgentHistoryMessagesForBridge } from './ai/externalAgentHistory'; import { canSendWithAgent, findEnabledExternalAgent } from './ai/agentSendEligibility'; -import { clearAllPendingApprovals } from '../infrastructure/ai/shared/approvalGate'; +import { registerGrantPersister } from '../infrastructure/ai/shared/approvalGate'; +import { stopAgentTurn } from '../infrastructure/ai/harness/agentStop'; +import { getAgentRuntime } from '../infrastructure/ai/harness/globalAgentRuntime'; +import { useAIPermissionGrantsState } from '../application/state/useAIPermissionGrantsState'; import { useConversationExport } from './ai/hooks/useConversationExport'; import type { AIChatSidePanelProps } from './AIChatSidePanel.types'; import { @@ -95,11 +98,6 @@ if (typeof window !== 'undefined') { subscribeUserSkillsStatusChanged(invalidateUserSkillsStatusCache); } -function getManualAgentCommand(config: ExternalAgentConfig | null | undefined): string | undefined { - const command = String(config?.command || '').trim(); - return config?.commandSource === 'manual' && command ? command : undefined; -} - function loadUserSkillsStatus( bridge: ReturnType, ): Promise { @@ -265,6 +263,11 @@ const AIChatSidePanelActive: React.FC = ({ terminalSessions = [], resolveExecutorContext, isVisible = true, + notes = [], + hosts = [], + onOpenVaultNote, + onOpenVaultHost, + onOpenVaultSection, }) => { const { t } = useI18n(); const scopeKey = `${scopeType}:${scopeTargetId ?? ''}`; @@ -989,22 +992,32 @@ const AIChatSidePanelActive: React.FC = ({ clearScopeDraft, showScopeSessionView, setActiveSessionId, ]); - const stopStreamingForSession = useCallback((sessionId: string) => { + const stopStreamingForSession = useCallback(async (sessionId: string) => { const controller = abortControllersRef.current.get(sessionId); - controller?.abort(); - abortControllersRef.current.delete(sessionId); setStreamingForScope(sessionId, false); updateLastMessage(sessionId, (msg) => ({ ...msg, statusText: '', executionStatus: msg.executionStatus === 'running' ? 'cancelled' : msg.executionStatus, })); - clearAllPendingApprovals(sessionId); - const bridge = getNetcattyBridge(); - bridge?.aiCattyCancelExec?.(sessionId); - bridge?.aiSdkAgentCancel?.('', sessionId); + await stopAgentTurn({ + chatSessionId: sessionId, + abortController: controller, + bridge: getNetcattyBridge(), + reason: 'user', + }); + await getAgentRuntime().waitForActiveTurn(sessionId); + if (controller && abortControllersRef.current.get(sessionId) === controller) { + abortControllersRef.current.delete(sessionId); + } }, [setStreamingForScope, updateLastMessage, abortControllersRef]); + const { addGrant } = useAIPermissionGrantsState(); + + useEffect(() => { + return registerGrantPersister((rule) => { addGrant(rule); }); + }, [addGrant]); + const handleStop = useCallback(() => { if (!activeSessionId) return; stopStreamingForSession(activeSessionId); @@ -1022,7 +1035,7 @@ const AIChatSidePanelActive: React.FC = ({ ); const handleDeleteSession = useCallback( - (e: React.MouseEvent, sessionId: string) => { + async (e: React.MouseEvent, sessionId: string) => { e.stopPropagation(); const deletingActiveSession = activeSessionId === sessionId @@ -1038,10 +1051,11 @@ const AIChatSidePanelActive: React.FC = ({ ?? currentAgentId; if (abortControllersRef.current.has(sessionId) || streamingSessionIds.has(sessionId)) { - stopStreamingForSession(sessionId); + await stopStreamingForSession(sessionId); } deleteSession(sessionId, scopeKey); + getAgentRuntime().clearChatSession(sessionId); if (deletingActiveSession || deletingLastScopedSession) { setShowHistory(false); @@ -1125,6 +1139,11 @@ const AIChatSidePanelActive: React.FC = ({ removeSelectedUserSkill={removeSelectedUserSkill} globalPermissionMode={globalPermissionMode} setGlobalPermissionMode={setGlobalPermissionMode} + notes={notes} + hosts={hosts} + onOpenVaultNote={onOpenVaultNote} + onOpenVaultHost={onOpenVaultHost} + onOpenVaultSection={onOpenVaultSection} /> ); @@ -1190,6 +1209,11 @@ export function aiChatSidePanelPropsAreEqual( if (prev.scopeHostIds !== next.scopeHostIds) return false; if (prev.terminalSessions !== next.terminalSessions) return false; if (prev.resolveExecutorContext !== next.resolveExecutorContext) return false; + if (prev.notes !== next.notes) return false; + if (prev.hosts !== next.hosts) return false; + if (prev.onOpenVaultNote !== next.onOpenVaultNote) return false; + if (prev.onOpenVaultHost !== next.onOpenVaultHost) return false; + if (prev.onOpenVaultSection !== next.onOpenVaultSection) return false; for (const key of AI_CHAT_SIDE_PANEL_AI_STATE_KEYS) { if (prev[key] !== next[key]) return false; diff --git a/components/AIChatSidePanel.types.ts b/components/AIChatSidePanel.types.ts index 7a48a8d66..2326c07f6 100644 --- a/components/AIChatSidePanel.types.ts +++ b/components/AIChatSidePanel.types.ts @@ -12,6 +12,7 @@ import type { } from '../infrastructure/ai/types'; import type { AIQuickMessage } from '../infrastructure/ai/quickMessages'; import type { ExecutorContext } from '../infrastructure/ai/cattyAgent/executor'; +import type { Host, VaultNote } from '../types'; // ------------------------------------------------------------------- // Props @@ -103,6 +104,13 @@ export interface AIChatSidePanelProps { // Visibility isVisible?: boolean; + + // Vault artifact navigation (from AI chat tool results) + notes?: VaultNote[]; + hosts?: Host[]; + onOpenVaultNote?: (noteId: string) => void; + onOpenVaultHost?: (hostId: string) => void; + onOpenVaultSection?: (section: 'notes' | 'hosts') => void; } // ------------------------------------------------------------------- diff --git a/components/TerminalLayer.tsx b/components/TerminalLayer.tsx index 7f52df147..2635c893e 100644 --- a/components/TerminalLayer.tsx +++ b/components/TerminalLayer.tsx @@ -97,6 +97,7 @@ const removeMountedSidePanelTabId = ( const TerminalLayerInner: React.FC = ({ hosts, + portForwardingRules = [], customGroups, groupConfigs, proxyProfiles, @@ -107,6 +108,9 @@ const TerminalLayerInner: React.FC = ({ notes, noteGroups, openNoteRequest, + onOpenVaultNoteFromChat, + onOpenVaultHostFromChat, + onOpenVaultSectionFromChat, sessions, workspaces, knownHosts = [], @@ -465,6 +469,8 @@ const TerminalLayerInner: React.FC = ({ workspacesRef.current = workspaces; const hostsRef = useRef(hosts); hostsRef.current = hosts; + const portForwardingRulesRef = useRef(portForwardingRules); + portForwardingRulesRef.current = portForwardingRules; const onSetWorkspaceFocusedSessionRef = useRef(onSetWorkspaceFocusedSession); onSetWorkspaceFocusedSessionRef.current = onSetWorkspaceFocusedSession; @@ -1343,6 +1349,8 @@ const TerminalLayerInner: React.FC = ({ hostMap, hosts, hostsRef, + portForwardingRules, + portForwardingRulesRef, hotkeyScheme, disableTerminalFontZoom, restoreTerminalCwd, @@ -1447,6 +1455,9 @@ const TerminalLayerInner: React.FC = ({ snippets, noteGroups, notes, + onOpenVaultHostFromChat, + onOpenVaultNoteFromChat, + onOpenVaultSectionFromChat, splitHorizontalHandlersRef, splitVerticalHandlersRef, sshDebugLogsEnabled, diff --git a/components/VaultView.tsx b/components/VaultView.tsx index 5611aca1b..8cd1c773d 100644 --- a/components/VaultView.tsx +++ b/components/VaultView.tsx @@ -225,6 +225,8 @@ interface VaultViewProps { onNavigateToSectionHandled?: () => void; deepLinkHostDraft?: Host | null; onDeepLinkHostDraftHandled?: () => void; + vaultFocusRequest?: { type: 'note'; noteId: string; requestId: number } | null; + onVaultFocusRequestHandled?: () => void; terminalSettings?: { verifyHostKeys: boolean; keepaliveInterval: number; keepaliveCountMax: number }; } @@ -283,6 +285,8 @@ const VaultViewInner: React.FC = ({ onNavigateToSectionHandled, deepLinkHostDraft, onDeepLinkHostDraftHandled, + vaultFocusRequest, + onVaultFocusRequestHandled, terminalSettings, }) => { const { t } = useI18n(); @@ -364,6 +368,7 @@ const VaultViewInner: React.FC = ({ const [isHostPanelOpen, setIsHostPanelOpen] = useState(false); const [editingHost, setEditingHost] = useState(null); const [newHostGroupPath, setNewHostGroupPath] = useState(null); + const [pendingOpenNoteId, setPendingOpenNoteId] = useState(null); useEffect(() => { if (!deepLinkHostDraft) return; @@ -375,6 +380,15 @@ const VaultViewInner: React.FC = ({ onDeepLinkHostDraftHandled?.(); }, [deepLinkHostDraft, onDeepLinkHostDraftHandled]); + useEffect(() => { + if (!vaultFocusRequest) return; + if (vaultFocusRequest.type === 'note') { + setCurrentSection('notes'); + setPendingOpenNoteId(vaultFocusRequest.noteId); + } + onVaultFocusRequestHandled?.(); + }, [vaultFocusRequest, onVaultFocusRequestHandled]); + // When the side panel is open, Tailwind's viewport-based grid-cols-* can't // react to the narrowed content area, so we drive the host-grid column count // off the actual container width (measured below). A fixed column count keeps @@ -1158,7 +1172,7 @@ const VaultViewInner: React.FC = ({ managedGroupPaths={managedGroupPaths} onConfirmDelete={deleteGroupPath} /> - + setPendingOpenNoteId(null), Pin, pinnedHosts, pinnedRecentIds, Plug, Plus, PortForwarding, protocolSelectHost, proxyProfiles, ProxyProfilesManager, quickConnectTarget, quickConnectWarnings, QuickConnectWizard, recentHosts, renameGroupError, renameGroupName, renameTargetPath, reorderGroup, reorderHost, RippleButton, rootRef, sanitizeHost, search, Search, selectedGroupPath, selectedHostIds, selectedTags, SerialConnectModal, SerialHostDetailsPanel, sessionCount, Set, setCurrentSection, setDeleteGroupWithHosts, setDeleteTargetPath, setDragOverDropTarget, setEditingGroupPath, setEditingHost, setGroupDragOverDropTarget, setIsDeleteGroupOpen, setIsGroupPanelOpen, setIsHostPanelOpen, setIsImportOpen, setIsMultiSelectMode, setIsNewFolderOpen, setIsQuickConnectOpen, setIsRenameGroupOpen, setIsSerialModalOpen, setLastPinnedId, setNewFolderName, setNewHostGroupPath, setProtocolSelectHost, setQuickConnectTarget, setQuickConnectWarnings, setRenameGroupError, setRenameGroupName, setRenameTargetPath, setSearch, setSelectedGroupPath, setSelectedHostIds, setSelectedTags, setSidebarCollapsed, setSidebarWidth, handleSidebarWidthCommit, setSortMode, setTargetParentPath, Settings, setViewMode, shellHistory, shouldHideEmptyRootHostsSection, showRecentHosts, sidebarCollapsed, sidebarWidth, snippetPackages, snippets, SnippetsManager, SortDropdown, sortMode, splitViewGridStyle, Square, Star, startInlineDeleteGroup, startInlineNewGroup, startInlineRenameGroup, submitNewFolder, submitRenameGroup, Suspense, t, TagFilterDropdown, targetParentPath, terminalFontSize, terminalSettings, TerminalSquare, terminalThemeId, toggleHostPinned, toggleHostSelection, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, Trash2, treeExpandedState, treeViewGroupTree, treeViewHosts, Upload, upsertHostById, Usb, viewMode, visibleDisplayedHosts, X, Zap }} /> ); }; @@ -1188,6 +1202,7 @@ export const vaultViewAreEqual = ( prev.terminalFontSize === next.terminalFontSize && prev.navigateToSection === next.navigateToSection && prev.deepLinkHostDraft === next.deepLinkHostDraft && + prev.vaultFocusRequest === next.vaultFocusRequest && // Only these terminal connection settings are forwarded to // PortForwarding inside the vault, so compare them directly. Other // terminal settings (fonts, themes, etc.) don't affect this subtree diff --git a/components/ai-elements/tool-call.tsx b/components/ai-elements/tool-call.tsx index b2ee12a1c..8e053b58c 100644 --- a/components/ai-elements/tool-call.tsx +++ b/components/ai-elements/tool-call.tsx @@ -141,11 +141,15 @@ export interface ToolCallProps extends HTMLAttributes { onApprove?: () => void; /** Called when user rejects this tool call. */ onReject?: () => void; + /** Called when user approves once without persisting a grant rule. */ + onApproveOnce?: () => void; + /** Called when user approves and persists an always-allow grant rule. */ + onAlwaysAllow?: () => void; } export const ToolCall = ({ name, args, result, isError, isLoading, isInterrupted, - approvalStatus, onApprove, onReject, + approvalStatus, onApprove, onReject, onApproveOnce, onAlwaysAllow, className, ...props }: ToolCallProps) => { const { t } = useI18n(); @@ -156,11 +160,17 @@ export const ToolCall = ({ const isPendingApproval = approvalStatus === 'pending' && !responded; - const handleApprove = useCallback(() => { + const handleApproveOnce = useCallback(() => { if (!isPendingApproval) return; setResponded(true); - onApprove?.(); - }, [isPendingApproval, onApprove]); + (onApproveOnce ?? onApprove)?.(); + }, [isPendingApproval, onApproveOnce, onApprove]); + + const handleAlwaysAllow = useCallback(() => { + if (!isPendingApproval) return; + setResponded(true); + (onAlwaysAllow ?? onApprove)?.(); + }, [isPendingApproval, onAlwaysAllow, onApprove]); const handleReject = useCallback(() => { if (!isPendingApproval) return; @@ -171,9 +181,9 @@ export const ToolCall = ({ // Keyboard: Enter = approve, Escape = reject (when pending) const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (!isPendingApproval) return; - if (e.key === 'Enter') { e.preventDefault(); handleApprove(); } + if (e.key === 'Enter') { e.preventDefault(); handleApproveOnce(); } else if (e.key === 'Escape') { e.preventDefault(); handleReject(); } - }, [isPendingApproval, handleApprove, handleReject]); + }, [isPendingApproval, handleApproveOnce, handleReject]); // Auto-focus and auto-scroll when approval is pending useEffect(() => { @@ -274,28 +284,38 @@ export const ToolCall = ({ {/* Inline approval buttons */} {isPendingApproval && (
-
- +
+ {t('ai.chat.toolApprovalHint')} -
+
+
diff --git a/components/ai/ChatInput.tsx b/components/ai/ChatInput.tsx index 5c479dea0..cc6a9e943 100644 --- a/components/ai/ChatInput.tsx +++ b/components/ai/ChatInput.tsx @@ -7,7 +7,7 @@ */ import { AtSign, Check, ChevronDown, ChevronRight, Cpu, Expand, Eye, FileText, ImageIcon, MessageSquare, Package, Plus, ShieldCheck, SquareTerminal, X, Zap } from 'lucide-react'; -import { filterQuickMessages, buildSlashCommandItems, filterUserSkillsForSlash, getSlashCommandItemKey, type AIQuickMessage, type SlashCommandItem, type UserSkillSlashOption } from '../../infrastructure/ai/quickMessages'; +import { filterQuickMessages, buildSlashCommandItems, filterUserSkillsForSlash, getSlashCommandItemKey, isSystemStopSlashCommand, type AIQuickMessage, type SlashCommandItem, type UserSkillSlashOption } from '../../infrastructure/ai/quickMessages'; import { SlashCommandPicker } from './SlashCommandPicker'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useI18n } from '../../application/i18n/I18nProvider'; @@ -460,9 +460,14 @@ const ChatInput: React.FC = ({ const handleSubmit = useCallback( (_text: string, _event: FormEvent) => { + if (isSystemStopSlashCommand(value)) { + onStop?.(); + onChange(''); + return; + } onSend(); }, - [onSend], + [onSend, onStop, onChange, value], ); const status: PromptInputStatus = isStreaming ? 'streaming' : 'idle'; diff --git a/components/ai/ChatMessageList.tsx b/components/ai/ChatMessageList.tsx index c952ae7d5..75a009fc4 100644 --- a/components/ai/ChatMessageList.tsx +++ b/components/ai/ChatMessageList.tsx @@ -20,6 +20,12 @@ import { Message, MessageContent, MessageResponse } from '../ai-elements/message import { ToolCall } from '../ai-elements/tool-call'; import ThinkingBlock from './ThinkingBlock'; import ToolCallGroup from './ToolCallGroup'; +import { + VaultArtifactNavigationProvider, +} from './toolArtifacts/VaultArtifactNavigationContext'; +import { parseVaultToolArtifact } from './toolArtifacts/vaultToolArtifact'; +import { VaultArtifactToolResult } from './toolArtifacts/VaultArtifactToolResult'; +import type { Host, VaultNote } from '../../types'; import { onApprovalRequest, onApprovalCleared, @@ -27,6 +33,10 @@ import { resolveApproval, type ApprovalRequest, } from '../../infrastructure/ai/shared/approvalGate'; +import { + buildGrantsFromApproval, + resolveCapabilityId, +} from '../../infrastructure/ai/harness/permissionGrants'; import { getAIPanelDiagnosticHiddenParts, getAIPanelProfilerProps, @@ -38,12 +48,26 @@ interface ChatMessageListProps { isStreaming?: boolean; /** Active chat session ID — used to filter standalone MCP approval blocks */ activeSessionId?: string | null; + notes?: VaultNote[]; + hosts?: Host[]; + onOpenVaultNote?: (noteId: string) => void; + onOpenVaultHost?: (hostId: string) => void; + onOpenVaultSection?: (section: 'notes' | 'hosts') => void; } const MESSAGE_RENDER_BATCH = 50; const MESSAGE_RENDER_STEP = 50; -const ChatMessageList: React.FC = ({ messages, isStreaming, activeSessionId }) => { +const ChatMessageList: React.FC = ({ + messages, + isStreaming, + activeSessionId, + notes = [], + hosts = [], + onOpenVaultNote, + onOpenVaultHost, + onOpenVaultSection, +}) => { // Track pending approvals from the approval gate const [pendingApprovals, setPendingApprovals] = useState>(new Map()); const [resolvedApprovals, setResolvedApprovals] = useState>(new Map()); @@ -70,12 +94,20 @@ const ChatMessageList: React.FC = ({ messages, isStreaming }); }, []); - const handleApprove = useCallback((toolCallId: string) => { + const handleApproveOnce = useCallback((toolCallId: string) => { resolveApproval(toolCallId, true); setPendingApprovals(prev => { const m = new Map(prev); m.delete(toolCallId); return m; }); setResolvedApprovals(prev => new Map(prev).set(toolCallId, true)); }, []); + const handleAlwaysAllow = useCallback((toolCallId: string, request: ApprovalRequest) => { + const capabilityId = request.capabilityId ?? resolveCapabilityId(request.toolName); + const persistGrants = buildGrantsFromApproval(capabilityId, request.args, request.chatSessionId); + resolveApproval(toolCallId, { approved: true, persistGrants }); + setPendingApprovals(prev => { const m = new Map(prev); m.delete(toolCallId); return m; }); + setResolvedApprovals(prev => new Map(prev).set(toolCallId, true)); + }, []); + const handleReject = useCallback((toolCallId: string) => { resolveApproval(toolCallId, false); setPendingApprovals(prev => { const m = new Map(prev); m.delete(toolCallId); return m; }); @@ -196,7 +228,7 @@ const ChatMessageList: React.FC = ({ messages, isStreaming const lastAssistantMessage = displayedMessages.findLast(m => m.role === 'assistant'); - return ( + const conversation = ( <> @@ -220,34 +252,64 @@ const ChatMessageList: React.FC = ({ messages, isStreaming let end = idx + 1; while (end < displayedMessages.length && displayedMessages[end].role === "tool") end++; const group = displayedMessages.slice(idx, end); - const groupTotal = group.reduce( - (sum, m) => sum + (m.toolResults?.length ?? 0), 0, + const toolResults = group.flatMap((toolMsg) => + (toolMsg.toolResults ?? []).map((tr) => ({ + toolCallId: tr.toolCallId, + name: toolCallNames.get(tr.toolCallId) || tr.toolCallId, + args: toolCallArgs.get(tr.toolCallId), + content: tr.content, + isError: tr.isError, + })), ); + const groupTotal = toolResults.length; // Expanded while the agent is still working (no assistant response follows) const hasAssistantAfter = end < displayedMessages.length && displayedMessages[end].role === "assistant"; + const renderToolResultItem = (item: typeof toolResults[number]) => { + const artifact = parseVaultToolArtifact(item.name, item.content); + if (artifact) { + return ( + + ); + } + return ( + +
+ +
+
+ ); + }; + + if (groupTotal === 1) { + return ( +
+ {renderToolResultItem(toolResults[0])} +
+ ); + } + return ( - {group.map((toolMsg) => - toolMsg.toolResults?.map((tr) => ( - -
- -
-
- )), - )} + {toolResults.map(renderToolResultItem)}
); } @@ -343,7 +405,13 @@ const ChatMessageList: React.FC = ({ messages, isStreaming args={tc.arguments} isInterrupted={!isPending} approvalStatus={approvalStatus} - onApprove={() => handleApprove(tc.id)} + onApproveOnce={() => handleApproveOnce(tc.id)} + onAlwaysAllow={() => handleAlwaysAllow(tc.id, pendingApprovals.get(tc.id) ?? { + toolCallId: tc.id, + toolName: tc.name, + args: tc.arguments ?? {}, + chatSessionId: activeSessionId ?? undefined, + })} onReject={() => handleReject(tc.id)} />
@@ -408,7 +476,13 @@ const ChatMessageList: React.FC = ({ messages, isStreaming args={tc.arguments} isLoading={isToolRunning && !isPending} approvalStatus={approvalStatus} - onApprove={() => handleApprove(tc.id)} + onApproveOnce={() => handleApproveOnce(tc.id)} + onAlwaysAllow={() => handleAlwaysAllow(tc.id, pendingApprovals.get(tc.id) ?? { + toolCallId: tc.id, + toolName: tc.name, + args: tc.arguments ?? {}, + chatSessionId: activeSessionId ?? undefined, + })} onReject={() => handleReject(tc.id)} />
@@ -431,7 +505,8 @@ const ChatMessageList: React.FC = ({ messages, isStreaming isLoading={false} isInterrupted={false} approvalStatus={'pending'} - onApprove={() => handleApprove(id)} + onApproveOnce={() => handleApproveOnce(id)} + onAlwaysAllow={() => handleAlwaysAllow(id, req)} onReject={() => handleReject(id)} />
@@ -527,11 +602,32 @@ const ChatMessageList: React.FC = ({ messages, isStreaming ); + + if (onOpenVaultNote && onOpenVaultHost && onOpenVaultSection) { + return ( + + {conversation} + + ); + } + + return conversation; }; function areMessagesEqual(prev: ChatMessageListProps, next: ChatMessageListProps): boolean { if (prev.isStreaming !== next.isStreaming) return false; if (prev.activeSessionId !== next.activeSessionId) return false; + if (prev.notes !== next.notes) return false; + if (prev.hosts !== next.hosts) return false; + if (prev.onOpenVaultNote !== next.onOpenVaultNote) return false; + if (prev.onOpenVaultHost !== next.onOpenVaultHost) return false; + if (prev.onOpenVaultSection !== next.onOpenVaultSection) return false; if (prev.messages.length !== next.messages.length) return false; if (prev.messages === next.messages) return true; diff --git a/components/ai/externalAgentHistory.ts b/components/ai/externalAgentHistory.ts index e322421ec..d750c185a 100644 --- a/components/ai/externalAgentHistory.ts +++ b/components/ai/externalAgentHistory.ts @@ -1,454 +1,18 @@ -import type { ChatMessage } from "../../infrastructure/ai/types.ts"; -import { buildHistoricalToolResultReplayText, buildHistoricalUserReplayContent } from "./cattyHistoryReplay.ts"; +import type { ChatMessage } from '../../infrastructure/ai/types'; +import { + buildExternalBridgeContextMessages, +} from '../../infrastructure/ai/harness/externalBridgeContext'; -type ExternalAgentHistoryMessage = { role: "user" | "assistant"; content: string }; -type RawHistoryMessage = ExternalAgentHistoryMessage & { sourceId: string }; -type DurableUserLine = { - line: string; - messageIndex: number; - priority: number; -}; - -const MAX_RECENT_RAW_MESSAGES = 6; -const MAX_MESSAGES_TO_SCAN = 20; -// Bound the scan by user turns, not raw message count: a tool-heavy external agent -// chat can produce 5+ messages per logical turn (user + assistant + -// several tool_results + follow-up assistant), so a plain -// message-count cap ages out early constraints much sooner than intended. -const MAX_DURABLE_SCAN_TURNS = 100; -const MAX_COMPACT_CONTEXT_CHARS = 3000; -const MAX_RAW_MESSAGE_CHARS = 2000; -const MAX_TOOL_SUMMARY_CHARS = 500; -const MAX_DURABLE_USER_CONTEXT_CHARS = 1400; -const MAX_DURABLE_ASSISTANT_CONTEXT_CHARS = 900; -const MAX_RECENT_SUMMARY_CONTEXT_CHARS = 1200; -const MAX_DURABLE_USER_MESSAGE_CHARS = 280; -const MAX_DURABLE_ASSISTANT_MESSAGE_CHARS = 360; -const MAX_TOOL_CALL_LABEL_CHARS = 200; - -type ToolCallInfo = { name: string; arguments: unknown }; - -const IMPORTANT_PATTERNS = [ - /不要|别|不能|不允许|必须|希望|只|最小|先|暂时|fallback|pwsh|powershell|cmd\.exe|windows|mcp|skills|cli|commit|\bpr\b|打包|内存|历史|压缩|慢/i, - /error|failed|failure|exit code|exception|cannot|unable|timeout|crash|fallback|commit|pull request|PR #\d+/i, -]; -const DURABLE_CONSTRAINT_PATTERNS = [ - /\bdo not\b|\bdon't\b|\bkeep\b|\bpreserve\b|\bavoid\b|\bonly\b|\bunchanged\b|\blocal only\b|\bwithout\b|\bleave\b/i, - /不要|别|保留|保持|维持|不改|别改|不要改|仅限本地/i, -]; -const TRIVIAL_USER_MESSAGE_PATTERNS = [ - /^(ok|okay|yes|no|thanks|thank you|continue|继续|好的|收到|行|嗯|好|继续处理|继续吧|开始吧)[.!? ]*$/i, -]; -const TRIVIAL_ASSISTANT_MESSAGE_PATTERNS = [ - /^(ok|okay|understood|got it|working|proceeding|ready|ack(?: \d+)?|收到|明白|继续处理|准备实现|开始处理|处理中)[.!? ]*$/i, -]; - -function truncateText(value: string, maxChars: number): string { - if (value.length <= maxChars) return value; - return `${value.slice(0, Math.max(0, maxChars - 24)).trimEnd()}\n[truncated]`; -} - -function normalizeWhitespace(value: string): string { - return value.replace(/\s+/g, " ").trim(); -} - -function isImportantText(value: string): boolean { - return IMPORTANT_PATTERNS.some((pattern) => pattern.test(value)); -} - -function isDurableConstraintText(value: string): boolean { - return DURABLE_CONSTRAINT_PATTERNS.some((pattern) => pattern.test(value)); -} - -function getUserHistoryContent(message: ChatMessage): string { - if (message.role !== "user") return message.content || ""; - return buildHistoricalUserReplayContent( - message.content || "", - message.attachments ?? [], - ); -} - -function isTrivialUserMessage(value: string): boolean { - const normalized = normalizeWhitespace(value); - if (isImportantText(normalized) || isDurableConstraintText(normalized)) return false; - // Don't blanket-drop short messages — short user turns are often - // load-bearing constraints ("Use ssh2", "中文输出", "no logs", "more - // verbose") that the IMPORTANT/DURABLE regexes can't realistically - // enumerate. The trivial-phrase regex already catches actual filler - // ("ok", "yes", "thanks", "继续"). - return TRIVIAL_USER_MESSAGE_PATTERNS.some((pattern) => pattern.test(normalized)); -} - -function getDurableUserPriority(value: string): number { - const normalized = normalizeWhitespace(value); - if (isImportantText(normalized) || isDurableConstraintText(normalized)) return 2; - return 1; -} - -function isSubstantiveAssistantMessage(value: string): boolean { - const normalized = normalizeWhitespace(value); - if (!normalized) return false; - // Mirror the user-side loosening: don't blanket-drop short assistant - // messages just because they're under 40 chars or don't match the small - // English keyword list. Short but load-bearing decisions ("Use ssh2", - // "rebase instead", "中文输出") aren't realistically enumerable and - // they're the exact things a later "do what you suggested" references. - // TRIVIAL_ASSISTANT_MESSAGE_PATTERNS still catches the actual filler - // ("ok", "ack", "got it", "明白"). - return !TRIVIAL_ASSISTANT_MESSAGE_PATTERNS.some((pattern) => pattern.test(normalized)); -} - -function getDurableAssistantPriority(value: string): number { - const normalized = normalizeWhitespace(value); - if (isImportantText(normalized)) return 2; - return 1; -} - -function appendUniqueLine( - target: string[], - seen: Set, - line: string, - maxSectionChars: number, - sectionCharsRef: { value: number }, -): void { - const normalized = normalizeWhitespace(line); - if (!normalized || seen.has(normalized)) return; - const nextChars = sectionCharsRef.value + normalized.length; - if (nextChars > maxSectionChars) return; - seen.add(normalized); - target.push(normalized); - sectionCharsRef.value = nextChars; -} - -function summarizeToolMessage( - message: ChatMessage, - toolCallIndex: Map, -): string[] { - if (!message.toolResults?.length) return []; - return message.toolResults.map((result) => { - const prefix = result.isError ? "Tool error" : "Tool result"; - // Same provenance problem as the raw-window path: once a tool result - // lands in the compact section (older than the 6-item raw window), - // its paired assistant tool_call is almost always gone. Without the - // call label, multiple older results collapse into indistinguishable - // "Tool result (callN): ..." lines and follow-ups like "use the - // resolv.conf output" can't be resolved. Inline the name+args here - // the same way toRawHistoryMessage does. - const callInfo = lookupToolCallInfo(toolCallIndex, message.id, result.toolCallId); - const callLabel = callInfo - ? ` [from ${callInfo.name}(${truncateText(JSON.stringify(callInfo.arguments ?? {}), MAX_TOOL_CALL_LABEL_CHARS)})]` - : ""; - const replayContent = buildHistoricalToolResultReplayText(result, callInfo - ? { id: result.toolCallId, name: callInfo.name, arguments: callInfo.arguments as Record } - : undefined); - return `${prefix}${callLabel} (${result.toolCallId}): ${truncateText(normalizeWhitespace(replayContent), MAX_TOOL_SUMMARY_CHARS)}`; - }); -} - -function summarizeMessage( - message: ChatMessage, - toolCallIndex: Map, -): string[] { - if (message.role === "system") return []; - if (message.role === "tool") return summarizeToolMessage(message, toolCallIndex); - - const lines: string[] = []; - const content = getUserHistoryContent(message); - if (content && isImportantText(content)) { - const label = message.role === "user" ? "User" : "Assistant"; - lines.push(`${label}: ${truncateText(normalizeWhitespace(content), MAX_TOOL_SUMMARY_CHARS)}`); - } - - if (message.role === "assistant" && message.toolCalls?.length) { - for (const toolCall of message.toolCalls) { - const args = JSON.stringify(toolCall.arguments ?? {}); - const summary = `Tool call: ${toolCall.name}(${truncateText(args, 220)})`; - if (isImportantText(summary)) lines.push(summary); - } - } - - return lines; -} - -function summarizeDurableUserMessage(message: ChatMessage): string | null { - if (message.role !== "user") return null; - const content = getUserHistoryContent(message); - if (!content) return null; - if (isTrivialUserMessage(content)) return null; - return `User request: ${truncateText(normalizeWhitespace(content), MAX_DURABLE_USER_MESSAGE_CHARS)}`; -} - -function summarizeDurableAssistantMessage(message: ChatMessage): string | null { - if (message.role !== "assistant" || !message.content) return null; - if (!isSubstantiveAssistantMessage(message.content)) return null; - return `Assistant context: ${truncateText(normalizeWhitespace(message.content), MAX_DURABLE_ASSISTANT_MESSAGE_CHARS)}`; -} - -/** - * Build a per-tool-result provenance index. Keys are - * `${toolResultMessageId}:${toolCallId}` rather than the bare toolCall.id - * so that provider-reused ids (e.g. "call1" across unrelated turns) don't - * cause later calls to overwrite older ones in the lookup — each - * tool_result resolves to the most recent assistant tool_call that - * preceded it with matching id, which preserves historical correctness - * when rebuilding older compact summaries. - */ -function buildToolCallIndex(messages: ChatMessage[]): Map { - const provenance = new Map(); - // Rolling map of the latest tool_call seen (by id) up to the current - // point in the message stream. - const latestByCallId = new Map(); - for (const message of messages) { - if (message.role === "assistant" && message.toolCalls?.length) { - for (const toolCall of message.toolCalls) { - if (!toolCall.id) continue; - latestByCallId.set(toolCall.id, { name: toolCall.name, arguments: toolCall.arguments }); - } - continue; - } - if (message.role === "tool" && message.toolResults?.length) { - for (const result of message.toolResults) { - const info = latestByCallId.get(result.toolCallId); - if (info) { - provenance.set(`${message.id}:${result.toolCallId}`, info); - } - } - } - } - return provenance; -} - -function lookupToolCallInfo( - index: Map, - toolMessageId: string, - toolCallId: string, -): ToolCallInfo | undefined { - return index.get(`${toolMessageId}:${toolCallId}`); -} - -function toRawHistoryMessage( - message: ChatMessage, - toolCallIndex: Map, -): RawHistoryMessage[] { - if (message.role === "user") { - const content = getUserHistoryContent(message); - return content - ? [{ sourceId: message.id, role: "user", content: truncateText(content, MAX_RAW_MESSAGE_CHARS) }] - : []; - } - - if (message.role === "assistant") { - const parts: string[] = []; - if (message.content) parts.push(message.content); - if (message.toolCalls?.length) { - parts.push(...message.toolCalls.map((tc) => `Tool call: ${tc.name}(${JSON.stringify(tc.arguments ?? {})})`)); - } - return parts.length - ? [{ sourceId: message.id, role: "assistant", content: truncateText(parts.join("\n\n"), MAX_RAW_MESSAGE_CHARS) }] - : []; - } - - if (message.role === "tool" && message.toolResults?.length) { - // Keep recent tool results self-describing while replacing terminal - // output with placeholders, so stale-session recovery doesn't replay - // bulky command output on every follow-up. External agent replay only - // supports user/assistant roles, so we flatten to "assistant" — the - // tool results were produced during the assistant's turn. - // - // Inline the originating tool_call's name+args. Tool calls and their - // results live in separate messages; if the last six raw items start - // in the middle of a tool interaction, the preceding assistant tool - // call can be outside the window. Without the call label the result - // is opaque bytes and "use that output" becomes ambiguous. - const parts = message.toolResults.map((result) => { - const prefix = result.isError ? "Tool error" : "Tool result"; - const callInfo = lookupToolCallInfo(toolCallIndex, message.id, result.toolCallId); - const callLabel = callInfo - ? ` [from ${callInfo.name}(${truncateText(JSON.stringify(callInfo.arguments ?? {}), MAX_TOOL_CALL_LABEL_CHARS)})]` - : ""; - const replayContent = buildHistoricalToolResultReplayText(result, callInfo - ? { id: result.toolCallId, name: callInfo.name, arguments: callInfo.arguments as Record } - : undefined); - return `${prefix}${callLabel} (${result.toolCallId}): ${replayContent}`; - }); - return [{ - sourceId: message.id, - role: "assistant", - content: truncateText(parts.join("\n\n"), MAX_RAW_MESSAGE_CHARS), - }]; - } - - return []; -} - -function buildCompactContext( - messages: ChatMessage[], - durableScanStart: number, - recentRawSourceIds: Set, - toolCallIndex: Map, -): ExternalAgentHistoryMessage[] { - const scanned = messages.slice(-MAX_MESSAGES_TO_SCAN); - const summaryLines: string[] = []; - const durableUserCandidates: DurableUserLine[] = []; - const selectedDurableUserLines: DurableUserLine[] = []; - const durableAssistantCandidates: DurableUserLine[] = []; - const selectedDurableAssistantLines: DurableUserLine[] = []; - const seen = new Set(); - const durableChars = { value: 0 }; - const durableAssistantChars = { value: 0 }; - const summaryChars = { value: 0 }; - - for (let messageIndex = durableScanStart; messageIndex < messages.length; messageIndex += 1) { - const message = messages[messageIndex]; - if (recentRawSourceIds.has(message.id)) continue; - const durableUserLine = summarizeDurableUserMessage(message); - if (durableUserLine) { - durableUserCandidates.push({ - line: durableUserLine, - messageIndex, - priority: getDurableUserPriority(getUserHistoryContent(message)), - }); - } - const durableAssistantLine = summarizeDurableAssistantMessage(message); - if (durableAssistantLine) { - durableAssistantCandidates.push({ - line: durableAssistantLine, - messageIndex, - priority: getDurableAssistantPriority(message.content || ""), - }); - } - } - - durableUserCandidates - .sort((left, right) => right.priority - left.priority || right.messageIndex - left.messageIndex) - .forEach((candidate) => { - const normalized = normalizeWhitespace(candidate.line); - if (!normalized || seen.has(normalized)) return; - const nextChars = durableChars.value + normalized.length; - if (nextChars > MAX_DURABLE_USER_CONTEXT_CHARS) return; - seen.add(normalized); - selectedDurableUserLines.push(candidate); - durableChars.value = nextChars; - }); - - durableAssistantCandidates - .sort((left, right) => right.priority - left.priority || right.messageIndex - left.messageIndex) - .forEach((candidate) => { - const normalized = normalizeWhitespace(candidate.line); - if (!normalized || seen.has(normalized)) return; - const nextChars = durableAssistantChars.value + normalized.length; - if (nextChars > MAX_DURABLE_ASSISTANT_CONTEXT_CHARS) return; - seen.add(normalized); - selectedDurableAssistantLines.push(candidate); - durableAssistantChars.value = nextChars; - }); - - const durableUserLines = selectedDurableUserLines - .sort((left, right) => left.messageIndex - right.messageIndex) - .map((candidate) => candidate.line); - const durableAssistantLines = selectedDurableAssistantLines - .sort((left, right) => left.messageIndex - right.messageIndex) - .map((candidate) => candidate.line); - - for (const line of [...durableUserLines, ...durableAssistantLines]) { - seen.add(normalizeWhitespace(line)); - } - - // Skip messages that are already appended verbatim in the raw window — - // otherwise the same last-6 turns get summarized here AND re-sent as - // raw, doubling the budget cost of important user turns / large tool - // output and crowding out older durable context the replay is meant - // to preserve. Matches the recentRawSourceIds skip in the durable pass. - for (const message of scanned) { - if (recentRawSourceIds.has(message.id)) continue; - for (const line of summarizeMessage(message, toolCallIndex)) { - appendUniqueLine(summaryLines, seen, line, MAX_RECENT_SUMMARY_CONTEXT_CHARS, summaryChars); - } - } - - if (!durableUserLines.length && !durableAssistantLines.length && !summaryLines.length) return []; - - const contentLines = [ - "[Compact prior Netcatty UI context]", - "The external SDK agent may already have its own persisted session context. Use this compact Netcatty UI context only as fallback/background, and prefer the current user request when there is any conflict.", - ]; - if (durableUserLines.length) { - contentLines.push("Earlier user requests that may still apply:"); - contentLines.push(...durableUserLines.map((line) => `- ${line}`)); - } - if (durableAssistantLines.length) { - contentLines.push("Earlier assistant context that may still matter:"); - contentLines.push(...durableAssistantLines.map((line) => `- ${line}`)); - } - if (summaryLines.length) { - contentLines.push("Recent noteworthy context:"); - contentLines.push(...summaryLines.map((line) => `- ${line}`)); - } - - return [{ - role: "user", - content: truncateText( - contentLines.join("\n"), - MAX_COMPACT_CONTEXT_CHARS, - ), - }]; -} - -/** - * Find the index of the first message to include in the scan window, - * bounded by MAX_DURABLE_SCAN_TURNS user turns (not raw message count). - * Walking backwards stops at the target turn count, so the cost is - * bounded even when the transcript is huge. - */ -function computeDurableScanStart(messages: ChatMessage[]): number { - let userTurns = 0; - for (let i = messages.length - 1; i >= 0; i -= 1) { - if (messages[i].role === "user") { - userTurns += 1; - if (userTurns >= MAX_DURABLE_SCAN_TURNS) return i; - } - } - return 0; -} +export type ExternalAgentHistoryMessage = { role: 'user' | 'assistant'; content: string }; export function buildExternalAgentHistoryMessages(messages: ChatMessage[]): ExternalAgentHistoryMessage[] { - // Compute the scan start once, then do all subsequent work over the - // already-sliced tail. This avoids O(N) walks over the whole transcript - // on every send — previously buildToolCallIndex + the flatMap-to-take- - // last-6 raw history both traversed every message in the chat. - const durableScanStart = computeDurableScanStart(messages); - const scannedTail = messages.slice(durableScanStart); - - // The tool-call provenance index only needs entries for tool_results - // that might appear in our output. Building from the scanned tail is - // correct for any tool_result whose paired assistant tool_call is - // also within the window, which covers >99% of realistic patterns - // (tool_calls and tool_results are always adjacent or near-adjacent). - // If an ancient tool_call's result stays within the window while the - // call itself is outside, that single result loses its [from X(Y)] - // label — an acceptable trade for eliminating the per-send O(N) walk. - const toolCallIndex = buildToolCallIndex(scannedTail); - - const rawHistory = scannedTail - .flatMap((message) => toRawHistoryMessage(message, toolCallIndex)) - .slice(-MAX_RECENT_RAW_MESSAGES); - const compactContext = buildCompactContext( - messages, - durableScanStart, - new Set(rawHistory.map((message) => message.sourceId)), - toolCallIndex, - ); - const recentRaw = rawHistory.map(({ role, content }) => ({ role, content })); - - return [...compactContext, ...recentRaw]; + return buildExternalBridgeContextMessages(messages); } export function buildExternalAgentHistoryMessagesForBridge( messages: ChatMessage[], _existingSessionId?: string | null, ): ExternalAgentHistoryMessage[] | undefined { - // The main process bridge only consumes this payload during stale-session - // fallback replay, so keep it available even when a session id exists. const historyMessages = buildExternalAgentHistoryMessages(messages); return historyMessages.length ? historyMessages : undefined; } diff --git a/components/ai/hooks/aiChatStreamingSupport.ts b/components/ai/hooks/aiChatStreamingSupport.ts index 7f508f63b..575916925 100644 --- a/components/ai/hooks/aiChatStreamingSupport.ts +++ b/components/ai/hooks/aiChatStreamingSupport.ts @@ -102,6 +102,9 @@ export interface PanelBridge extends NetcattyBridge { agentEnv?: Record, agentCommand?: string, ) => Promise<{ ok: boolean; models?: Array<{ id: string; name: string; description?: string; thinkingLevels?: string[] }>; currentModelId?: string | null; error?: string }>; + aiCattyCancelExec?(chatSessionId: string): Promise; + aiSetChatSessionCancelled?(chatSessionId: string, cancelled?: boolean): Promise<{ ok: boolean; error?: string }>; + aiMcpSyncPermissionGrants?(grants: Array>): Promise<{ ok: boolean; count?: number; error?: string }>; aiSdkAgentCancel?: (requestId: string, chatSessionId?: string) => Promise<{ ok: boolean; error?: string }>; aiSdkAgentCleanup?: (chatSessionId: string) => Promise<{ ok: boolean }>; aiUserSkillsGetStatus?: () => Promise<{ @@ -130,6 +133,14 @@ export interface TerminalSessionInfo { shellType?: string; deviceType?: string; connected: boolean; + hostChain?: Array<{ hostId: string; label?: string; hostname?: string }>; + activePortForwards?: Array<{ + ruleId: string; + label?: string; + type?: string; + localPort?: number; + status?: string; + }>; } export interface DefaultTargetSessionHint extends TerminalSessionInfo { diff --git a/components/ai/hooks/useAIChatStreaming.ts b/components/ai/hooks/useAIChatStreaming.ts index 8788cddba..ab7afea57 100644 --- a/components/ai/hooks/useAIChatStreaming.ts +++ b/components/ai/hooks/useAIChatStreaming.ts @@ -1,17 +1,11 @@ /** - * useAIChatStreaming — Encapsulates all streaming logic for the AI chat panel. + * useAIChatStreaming — React UI layer for AI chat streaming. * - * Handles: - * - Catty agent streaming via Vercel AI SDK `streamText` - * - External agent streaming through official SDK backends - * - Text-delta batching via requestAnimationFrame - * - Abort controller management - * - Stream state tracking (per-session) - * - Error reporting + * Turn orchestration lives in AgentRuntime + TurnDrivers; this hook only + * manages streaming state, abort controllers, and UI callbacks. */ import React, { useCallback, useEffect, useRef, useState } from 'react'; -import { generateText, pruneMessages, streamText, stepCountIs, type ModelMessage } from 'ai'; import type { AIPermissionMode, AIToolIntegrationMode, @@ -19,75 +13,18 @@ import type { ChatMessage, ChatMessageAttachment, ExternalAgentConfig, - ProviderAdvancedParams, ProviderConfig, - ToolResult, WebSearchConfig, } from '../../../infrastructure/ai/types'; -import { isWebSearchReady } from '../../../infrastructure/ai/types'; -import { buildSystemPrompt } from '../../../infrastructure/ai/cattyAgent/systemPrompt'; -import { - CONTEXT_COMPACTION_SYSTEM_PROMPT, - DEFAULT_CONTEXT_WINDOW_TOKENS, - DEFAULT_PROTECT_RECENT_MESSAGES, - formatMessagesForCompaction, - estimateUnknownTokens, - keepRecentContextMessages, - prepareContextCompaction, - resolveContextWindow, -} from '../../../infrastructure/ai/contextCompaction'; -import { - compressMessagesForRequestTooLargeRetry, -} from '../../../infrastructure/ai/requestPayloadCompression'; -import { - createCattyRequestTooLargeRetryError, - hadToolProgressBeforeRequestTooLarge, -} from '../../../infrastructure/ai/cattyRequestTooLargeRetry'; -import { createModelFromConfig } from '../../../infrastructure/ai/sdk/providers'; -import { createCattyTools } from '../../../infrastructure/ai/sdk/tools'; import type { ExecutorContext } from '../../../infrastructure/ai/cattyAgent/executor'; -import { getExternalAgentSdkBackend } from '../../../infrastructure/ai/managedAgents'; -import { runSdkAgentTurn } from '../../../infrastructure/ai/sdkAgentAdapter'; -import { classifyError, isRequestTooLargeError } from '../../../infrastructure/ai/errorClassifier'; -import { isSdkStreamStateError } from '../../../infrastructure/ai/shared/streamStateErrors'; -import { - buildPromptWithTerminalSelectionAttachments, - isTerminalSelectionAttachment, -} from '../../../application/state/terminalSelectionAttachment'; +import { getAgentRuntime } from '../../../infrastructure/ai/harness/globalAgentRuntime'; +import { classifyError } from '../../../infrastructure/ai/errorClassifier'; import { latestAISessionsSnapshot } from '../../../application/state/aiStateSnapshots'; import { - buildHistoricalToolReplayMaps, - buildHistoricalToolResultReplayText, - buildHistoricalUserReplayContent, -} from '../cattyHistoryReplay'; -import { - extractProviderContinuationFromRawChunk, - getOpenAIChatAssistantFieldsForHistoryMessage, - isProviderContinuationForSource, - mergeProviderContinuation, - normalizeProviderContinuationOptions, - withProviderContinuationSource, - type OpenAIChatAssistantFields, - type ProviderContinuation, -} from '../../../infrastructure/ai/providerContinuation'; - -import { - getNetcattyBridge, generateId, - isToolResultError, - resolveUserSkillsContext, - toAssistantModelContent, - type AssistantContentPart, - type CattyProviderContinuationContext, + getNetcattyBridge, type DefaultTargetSessionHint, - type ErrorChunk, - type RawChunk, - type ReasoningChunk, - type StreamChunk, type TerminalSessionInfo, - type TextDeltaChunk, - type ToolCallChunk, - type ToolResultChunk, } from './aiChatStreamingSupport'; export { getNetcattyBridge } from './aiChatStreamingSupport'; @@ -101,11 +38,6 @@ const streamingSubscribers = new Set<() => void>(); export function isAIChatSessionStreaming(sessionId: string | null | undefined): boolean { return !!sessionId && sharedStreamingSessionIds.has(sessionId); } -const OPENAI_CHAT_ASSISTANT_FIELDS = Symbol('netcatty.openAIChatAssistantFields'); - -type ModelMessageWithOpenAIChatFields = ModelMessage & { - [OPENAI_CHAT_ASSISTANT_FIELDS]?: OpenAIChatAssistantFields; -}; function emitStreamingStoreChange(): void { streamingSubscribers.forEach(listener => { @@ -117,49 +49,6 @@ function emitStreamingStoreChange(): void { }); } -function rememberOpenAIChatAssistantFields( - message: ModelMessage, - fields: OpenAIChatAssistantFields | undefined, - fieldsByMessage: Map, -): void { - fieldsByMessage.set(message, fields); - (message as ModelMessageWithOpenAIChatFields)[OPENAI_CHAT_ASSISTANT_FIELDS] = fields; -} - -function getRememberedOpenAIChatAssistantFields( - message: ModelMessage, - fieldsByMessage: Map, -): OpenAIChatAssistantFields | undefined { - if (fieldsByMessage.has(message)) return fieldsByMessage.get(message); - return (message as ModelMessageWithOpenAIChatFields)[OPENAI_CHAT_ASSISTANT_FIELDS]; -} - -function modelMessageHasToolCall(message: ModelMessage): boolean { - if (message.role !== 'assistant' || !Array.isArray(message.content)) return false; - return message.content.some((part) => part && typeof part === 'object' && (part as { type?: string }).type === 'tool-call'); -} - -function collectOpenAIChatAssistantFieldsForMessages( - messages: ModelMessage[], - fieldsByMessage: Map, -): Array { - const fields: Array = []; - let previousMessageWasTool = false; - for (const message of messages) { - const needsContinuationFields = message.role === 'assistant' - && (modelMessageHasToolCall(message) || previousMessageWasTool); - if (needsContinuationFields) { - fields.push(getRememberedOpenAIChatAssistantFields(message, fieldsByMessage)); - } - previousMessageWasTool = message.role === 'tool'; - } - return fields; -} - -// ------------------------------------------------------------------- -// Hook parameters -// ------------------------------------------------------------------- - export interface UseAIChatStreamingParams { maxIterations: number; addMessageToSession: (sessionId: string, message: ChatMessage) => void; @@ -167,30 +56,10 @@ export interface UseAIChatStreamingParams { updateMessageById: (sessionId: string, messageId: string, updater: (msg: ChatMessage) => ChatMessage) => void; } -// ------------------------------------------------------------------- -// Hook return type -// ------------------------------------------------------------------- - export interface UseAIChatStreamingReturn { - /** Set of session IDs currently streaming. */ streamingSessionIds: Set; - /** Set or unset streaming state for a session. */ setStreamingForScope: (key: string, val: boolean) => void; - /** Ref to per-session abort controllers. */ abortControllersRef: React.MutableRefObject>; - /** Process a Catty agent stream. */ - processCattyStream: ( - streamSessionId: string, - model: ReturnType, - systemPrompt: string, - tools: ReturnType, - sdkMessages: Array, - signal: AbortSignal, - currentAssistantMsgId: string, - advancedParams?: ProviderAdvancedParams, - continuationContext?: CattyProviderContinuationContext, - ) => Promise; - /** Send a message to the Catty agent (built-in). */ sendToCattyAgent: ( sessionId: string, sendScopeKey: string, @@ -201,7 +70,6 @@ export interface UseAIChatStreamingReturn { context: SendToCattyContext, attachments?: ChatMessageAttachment[], ) => Promise; - /** Send a message to an external SDK agent. */ sendToExternalAgent: ( sessionId: string, trimmed: string, @@ -210,11 +78,9 @@ export interface UseAIChatStreamingReturn { attachedImages: Array<{ base64Data: string; mediaType: string; filename?: string; filePath?: string }>, context: SendToExternalContext, ) => Promise; - /** Report a streaming error to the chat. */ reportStreamError: (sessionId: string, abortSignal: AbortSignal, err: unknown) => void; } -/** Context values needed by sendToCattyAgent that change frequently (avoids stale closures). */ export interface SendToCattyContext { activeProvider: ProviderConfig | undefined; activeModelId: string; @@ -229,9 +95,9 @@ export interface SendToCattyContext { autoTitleSession: (sessionId: string, text: string) => void; titleText?: string; selectedUserSkillSlugs?: string[]; + permissionMode?: AIPermissionMode; } -/** Context values needed by sendToExternalAgent that change frequently. */ export interface SendToExternalContext { existingSessionId?: string; updateExternalSessionId?: (sessionId: string, externalSessionId: string | undefined) => void; @@ -244,20 +110,16 @@ export interface SendToExternalContext { selectedUserSkillSlugs?: string[]; } -// ------------------------------------------------------------------- -// Hook implementation -// ------------------------------------------------------------------- - export function useAIChatStreaming({ maxIterations, addMessageToSession, updateLastMessage, updateMessageById, }: UseAIChatStreamingParams): UseAIChatStreamingReturn { - // Per-session streaming state (keyed by sessionId) const [streamingSessionIds, setStreamingSessions] = useState>( () => new Set(sharedStreamingSessionIds), ); + useEffect(() => { const syncFromStore = () => { setStreamingSessions(new Set(sharedStreamingSessionIds)); @@ -281,25 +143,15 @@ export function useAIChatStreaming({ } }, []); - // Per-scope abort controllers const abortControllersRef = useRef>(sharedAbortControllers); - // ------------------------------------------------------------------- - // reportStreamError - // ------------------------------------------------------------------- - const reportStreamError = useCallback(( sessionId: string, abortSignal: AbortSignal, err: unknown, ) => { if (abortSignal.aborted) return; - // Log the full unsanitized error for debugging console.error('[AIChatSidePanel] Stream error (full):', err); - // Pass the raw error to classifyError so it can inspect structured - // fields (statusCode, responseBody) from APICallError and friends; - // string-coercing here would strip the metadata we need to detect - // 413 / HTML-error-page / parse-failure scenarios. const errorInfo = classifyError(err); updateLastMessage(sessionId, msg => ({ ...msg, @@ -315,306 +167,14 @@ export function useAIChatStreaming({ }); }, [updateLastMessage, addMessageToSession]); - // ------------------------------------------------------------------- - // processCattyStream - // ------------------------------------------------------------------- - - const processCattyStream = useCallback(async ( - streamSessionId: string, - model: ReturnType, - systemPrompt: string, - tools: ReturnType, - sdkMessages: Array, - signal: AbortSignal, - currentAssistantMsgId: string, - advancedParams?: ProviderAdvancedParams, - continuationContext?: CattyProviderContinuationContext, - ): Promise => { - const result = streamText({ - model, - messages: sdkMessages, - system: systemPrompt, - tools, - stopWhen: stepCountIs(maxIterations), - abortSignal: signal, - includeRawChunks: true, - ...(advancedParams?.maxTokens != null && { maxOutputTokens: advancedParams.maxTokens }), - ...(advancedParams?.temperature != null && { temperature: advancedParams.temperature }), - ...(advancedParams?.topP != null && { topP: advancedParams.topP }), - ...(advancedParams?.frequencyPenalty != null && { frequencyPenalty: advancedParams.frequencyPenalty }), - ...(advancedParams?.presencePenalty != null && { presencePenalty: advancedParams.presencePenalty }), - }); - - // Track the current assistant message ID so updates target the correct message - let activeMsgId = currentAssistantMsgId; - let lastAddedRole: 'assistant' | 'tool' = 'assistant'; - let hadToolProgress = false; - const reader = result.fullStream.getReader(); - - // -- Text-delta batching: accumulate deltas and flush periodically -- - let pendingText = ''; - let rafId: number | null = null; - const ensureAssistantMessage = (): string => { - if (lastAddedRole !== 'tool') return activeMsgId; - - const newId = generateId(); - addMessageToSession(streamSessionId, { - id: newId, - role: 'assistant', - content: '', - timestamp: Date.now(), - }); - activeMsgId = newId; - lastAddedRole = 'assistant'; - return activeMsgId; - }; - - const updateAssistantContinuation = ( - messageId: string, - continuation: ProviderContinuation | undefined, - thinkingText = '', - ) => { - if (!continuation && !thinkingText) return; - const sourcedContinuation = withProviderContinuationSource(continuation, continuationContext?.source); - updateMessageById(streamSessionId, messageId, msg => { - const providerContinuation = mergeProviderContinuation(msg.providerContinuation, sourcedContinuation); - return { - ...msg, - ...(providerContinuation ? { providerContinuation } : {}), - ...(thinkingText ? { thinking: (msg.thinking || '') + thinkingText } : {}), - }; - }); - }; - - const getOpenAIReasoningText = (continuation: ProviderContinuation | undefined): string => { - const reasoningContent = continuation?.openAIChatAssistantFields?.reasoning_content; - return typeof reasoningContent === 'string' ? reasoningContent : ''; - }; - - const flushText = () => { - if (pendingText) { - const text = pendingText; - pendingText = ''; - if (lastAddedRole === 'tool') { - const newId = generateId(); - addMessageToSession(streamSessionId, { - id: newId, - role: 'assistant', - content: text, - timestamp: Date.now(), - }); - activeMsgId = newId; - lastAddedRole = 'assistant'; - } else { - updateMessageById(streamSessionId, activeMsgId, msg => ({ - ...msg, - content: msg.content + text, - })); - } - } - rafId = null; - }; - - const cancelPendingFlush = () => { - if (rafId !== null) { - cancelAnimationFrame(rafId); - rafId = null; - } - }; - - try { - while (true) { - let readResult: ReadableStreamReadResult; - try { - readResult = await reader.read(); - } catch (readErr) { - if (isRequestTooLargeError(readErr)) { - throw createCattyRequestTooLargeRetryError(readErr, hadToolProgress); - } - throw readErr; - } - const { done, value } = readResult; - if (done) break; - // Use the StreamChunk union for type narrowing instead of unsafe casts - const chunk = value as StreamChunk; - switch (chunk.type) { - case 'text': - case 'text-delta': { - const typedChunk = chunk as TextDeltaChunk; - const text = typedChunk.text ?? typedChunk.textDelta; - const providerOptions = normalizeProviderContinuationOptions(typedChunk.providerMetadata); - if (providerOptions) { - const messageId = ensureAssistantMessage(); - updateAssistantContinuation(messageId, { textProviderOptions: providerOptions }); - } - if (text) { - pendingText += text; - if (rafId === null) { - rafId = requestAnimationFrame(flushText); - } - } - break; - } - case 'reasoning': - case 'reasoning-start': - case 'reasoning-delta': { - cancelPendingFlush(); - flushText(); - const typedChunk = chunk as ReasoningChunk; - const rText = typedChunk.text ?? typedChunk.textDelta ?? typedChunk.delta ?? ''; - const providerOptions = normalizeProviderContinuationOptions(typedChunk.providerMetadata); - const continuation = rText || providerOptions - ? { - reasoningParts: [{ - text: rText, - ...(providerOptions ? { providerOptions } : {}), - }], - } satisfies ProviderContinuation - : undefined; - if (continuation || rText) { - const messageId = ensureAssistantMessage(); - updateAssistantContinuation(messageId, continuation, rText); - } - break; - } - case 'raw': { - const typedChunk = chunk as RawChunk; - const continuation = extractProviderContinuationFromRawChunk(typedChunk.rawValue); - if (continuation) { - cancelPendingFlush(); - flushText(); - const messageId = ensureAssistantMessage(); - updateAssistantContinuation(messageId, continuation, getOpenAIReasoningText(continuation)); - } - break; - } - case 'reasoning-end': - case 'text-start': - case 'text-end': - case 'start': - case 'finish': - case 'start-step': - case 'finish-step': - break; - case 'tool-call': { - cancelPendingFlush(); - flushText(); - const typedChunk = chunk as ToolCallChunk; - hadToolProgress = true; - const messageId = ensureAssistantMessage(); - const providerOptions = normalizeProviderContinuationOptions(typedChunk.providerMetadata); - updateMessageById(streamSessionId, messageId, msg => ({ - ...msg, - toolCalls: [...(msg.toolCalls || []), { - id: typedChunk.toolCallId, - name: typedChunk.toolName, - arguments: (typedChunk.input ?? typedChunk.args) as Record, - }], - executionStatus: 'running', - statusText: undefined, - })); - if (providerOptions) { - updateAssistantContinuation(messageId, { - toolCallProviderOptionsById: { - [typedChunk.toolCallId]: providerOptions, - }, - }); - } - break; - } - case 'tool-result': { - cancelPendingFlush(); - flushText(); - const typedChunk = chunk as ToolResultChunk; - hadToolProgress = true; - // Mark the assistant message's tool execution as completed - updateMessageById(streamSessionId, activeMsgId, msg => - msg.role === 'assistant' && msg.executionStatus === 'running' - ? { ...msg, executionStatus: 'completed', statusText: undefined } : msg, - ); - const toolOutput = typedChunk.output ?? typedChunk.result; - const toolError = isToolResultError(toolOutput); - addMessageToSession(streamSessionId, { - id: generateId(), - role: 'tool', - content: '', - toolResults: [{ - toolCallId: typedChunk.toolCallId, - content: typeof toolOutput === 'string' - ? toolOutput - : JSON.stringify(toolOutput), - isError: toolError, - }], - timestamp: Date.now(), - executionStatus: 'completed', - }); - lastAddedRole = 'tool'; - break; - } - // tool-approval-request is no longer handled here — approval is now - // inside the tool's execute function via the approvalGate module. - // The SDK may still emit this chunk type but we simply ignore it. - case 'error': { - const typedChunk = chunk as ErrorChunk; - // Internal SDK reasoning/text state-machine errors (e.g. a - // third-party Anthropic-compat backend like DeepSeek's - // `-v4-flash` streaming thinking deltas without first emitting - // the `reasoning-start` content-block signal) leak through - // fullStream once per orphan delta. They're not user-facing - // errors — and worse, surfacing one assistant message per - // event breaks tool_use/tool_result contiguity on the next - // turn, which the Anthropic backend then rejects as - // `messages.N: tool_use ids were found without tool_result - // blocks immediately after`. Filter them out at the chunk - // boundary: drop the placeholder assistant message and keep - // accepting subsequent chunks, so the rest of the stream - // (real text, tool calls, the genuine `finish`) lands intact. - if (isSdkStreamStateError(typedChunk.error)) { - console.warn('[Catty] suppressed SDK stream state error:', typedChunk.error); - break; - } - if (isRequestTooLargeError(typedChunk.error)) { - cancelPendingFlush(); - flushText(); - throw createCattyRequestTooLargeRetryError( - typedChunk.error, - hadToolProgress, - ); - } - cancelPendingFlush(); - flushText(); - updateMessageById(streamSessionId, activeMsgId, msg => ({ - ...msg, - statusText: '', - executionStatus: msg.executionStatus === 'running' ? 'failed' : msg.executionStatus, - })); - addMessageToSession(streamSessionId, { - id: generateId(), - role: 'assistant', - content: '', - // Pass the raw error so classifyError can detect 413 / HTML / - // schema-parse scenarios via structured fields (statusCode, - // responseBody) instead of lossy string conversion. - errorInfo: classifyError(typedChunk.error), - timestamp: Date.now(), - }); - break; - } - default: - break; - } - } - } finally { - cancelPendingFlush(); - flushText(); - reader.releaseLock(); - } - return; - }, [maxIterations, addMessageToSession, updateMessageById]); - - // ------------------------------------------------------------------- - // sendToExternalAgent - // ------------------------------------------------------------------- + const uiCallbacks = useCallback(() => ({ + addMessageToSession, + updateLastMessage, + updateMessageById, + reportStreamError, + setStreamingForScope, + getLatestSession: (sessionId: string) => latestAISessionsSnapshot?.find(s => s.id === sessionId), + }), [addMessageToSession, updateLastMessage, updateMessageById, reportStreamError, setStreamingForScope]); const sendToExternalAgent = useCallback(async ( sessionId: string, @@ -625,130 +185,18 @@ export function useAIChatStreaming({ context: SendToExternalContext, ) => { const bridge = getNetcattyBridge(); - const userSkillsContext = await resolveUserSkillsContext( + await getAgentRuntime().runTurn({ + backend: 'external-sdk', + chatSessionId: sessionId, + userText: trimmed, + signal: abortController.signal, + agentConfig, + attachedImages, + context, bridge, - trimmed, - context.selectedUserSkillSlugs, - ); - - const sdkBackend = getExternalAgentSdkBackend(agentConfig); - if (sdkBackend && bridge) { - const requestId = `sdk_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`; - - // Push terminal session metadata to MCP bridge - if (bridge?.aiMcpUpdateSessions) { - await bridge.aiMcpUpdateSessions(context.terminalSessions, sessionId); - } - - // Mutable flag: set after tool-result, cleared when new assistant msg is created - let needsNewAssistantMsg = false; - const maybeCreateAssistantMsg = () => { - if (needsNewAssistantMsg) { - needsNewAssistantMsg = false; - addMessageToSession(sessionId, { - id: generateId(), role: 'assistant', content: '', timestamp: Date.now(), - model: agentConfig.name || 'external', - }); - } - }; - - await runSdkAgentTurn( - bridge, - requestId, - sessionId, - agentConfig, - trimmed, - { - onTextDelta: (text: string) => { - maybeCreateAssistantMsg(); - updateLastMessage(sessionId, msg => ({ - ...msg, - content: msg.content + text, - statusText: undefined, - thinkingDurationMs: msg.thinking && !msg.thinkingDurationMs - ? Date.now() - msg.timestamp : msg.thinkingDurationMs, - })); - }, - onThinkingDelta: (text: string) => { - maybeCreateAssistantMsg(); - updateLastMessage(sessionId, msg => ({ - ...msg, thinking: (msg.thinking || '') + text, - })); - }, - onThinkingDone: () => { - updateLastMessage(sessionId, msg => ({ - ...msg, thinkingDurationMs: msg.thinkingDurationMs || (Date.now() - msg.timestamp), - })); - }, - onToolCall: (toolName: string, args: Record, toolCallId?: string) => { - maybeCreateAssistantMsg(); - updateLastMessage(sessionId, msg => ({ - ...msg, - toolCalls: [...(msg.toolCalls || []), { id: toolCallId || `tc_${Date.now()}`, name: toolName, arguments: args }], - executionStatus: 'running', - statusText: undefined, - })); - }, - onToolResult: (toolCallId: string, result: string, toolName?: string) => { - updateLastMessage(sessionId, msg => { - if (msg.role !== 'assistant' || msg.executionStatus !== 'running') return msg; - // Only patch tool call name if the existing name is missing/generic - // (don't overwrite a good name from onToolCall with a wrapper name from tool-result) - const updatedToolCalls = toolName && !toolName.includes('sdk_agent_dynamic_tool') && msg.toolCalls - ? msg.toolCalls.map(tc => tc.id === toolCallId && !tc.name ? { ...tc, name: toolName } : tc) - : msg.toolCalls; - return { ...msg, toolCalls: updatedToolCalls, executionStatus: 'completed', statusText: undefined }; - }); - const toolError = isToolResultError(result); - addMessageToSession(sessionId, { - id: generateId(), role: 'tool', content: '', - toolResults: [{ toolCallId, content: result, isError: toolError }], - timestamp: Date.now(), executionStatus: 'completed', - }); - needsNewAssistantMsg = true; - }, - onStatus: (message: string) => { - maybeCreateAssistantMsg(); - updateLastMessage(sessionId, msg => ({ ...msg, statusText: message })); - }, - onSessionId: (externalSessionId: string) => { - context.updateExternalSessionId?.(sessionId, externalSessionId); - }, - onError: (error: string) => { - reportStreamError(sessionId, abortController.signal, error); - setStreamingForScope(sessionId, false); - }, - onDone: () => {}, - }, - abortController.signal, - // Managed SDK agents (codex, claude) must resolve auth from their own - // CLI config/login state, so we deliberately pass no providerId here. - // See issue #705 for Codex; same reasoning for Claude. - undefined, - context.selectedAgentModel, - context.existingSessionId, - context.historyMessages, - attachedImages.length > 0 ? attachedImages : undefined, - context.toolIntegrationMode, - context.defaultTargetSession, - userSkillsContext, - ); - } else { - // Managed agents always route through the SDK path above. - reportStreamError( - sessionId, - abortController.signal, - 'This agent has no SDK backend configured. Re-discover it in Settings -> AI.', - ); - setStreamingForScope(sessionId, false); - } - }, [ - addMessageToSession, updateLastMessage, setStreamingForScope, reportStreamError, - ]); - - // ------------------------------------------------------------------- - // sendToCattyAgent - // ------------------------------------------------------------------- + ui: uiCallbacks(), + }); + }, [uiCallbacks]); const sendToCattyAgent = useCallback(async ( sessionId: string, @@ -761,450 +209,30 @@ export function useAIChatStreaming({ attachments?: ChatMessageAttachment[], ) => { const bridge = getNetcattyBridge(); - const userSkillsContext = await resolveUserSkillsContext( - bridge, - trimmed, - context.selectedUserSkillSlugs, - ); - const getExecutorContext = context.getExecutorContext ?? (() => ({ - sessions: context.terminalSessions, - workspaceId: context.scopeType === 'workspace' ? context.scopeTargetId : undefined, - workspaceName: context.scopeType === 'workspace' ? context.scopeLabel : undefined, - })); - const tools = createCattyTools( - bridge, - getExecutorContext, - context.commandBlocklist, - context.globalPermissionMode, - context.webSearchConfig ?? undefined, - sessionId, - ); - - const systemPrompt = buildSystemPrompt({ - scopeType: context.scopeType, scopeLabel: context.scopeLabel, - hosts: context.terminalSessions.map(s => ({ - sessionId: s.sessionId, hostname: s.hostname, label: s.label, - os: s.os, - username: s.username, - protocol: s.protocol, - shellType: s.shellType, - deviceType: s.deviceType, - connected: s.connected, - })), - permissionMode: context.globalPermissionMode, - webSearchEnabled: isWebSearchReady(context.webSearchConfig), - userSkillsContext, - }); - - // Guard: activeProvider must exist for Catty agent path - if (!context.activeProvider) { - reportStreamError(sessionId, abortController.signal, 'No AI provider configured. Please configure a provider in Settings → AI.'); - return; - } - - const activeModelId = context.activeModelId || context.activeProvider.defaultModel || ''; - const continuationContext: CattyProviderContinuationContext = { - source: { - providerConfigId: context.activeProvider.id, - providerType: context.activeProvider.providerId, - modelId: activeModelId, - }, - openAIChatAssistantFields: [], - }; - try { - let openAIChatAssistantFieldsByMessage = new Map(); - const buildSdkMessages = ( - allMessages: ChatMessage[], - includeCurrentUserMessage: boolean, - { - preserveTerminalToolResults = new Set(), - }: { - preserveTerminalToolResults?: ReadonlySet; - } = {}, - ): Array => { - const { resolvedToolCallsByAssistant, toolCallByToolResult } = buildHistoricalToolReplayMaps(allMessages); - const nextFieldsByMessage = new Map(); - const sdkMessages: Array = []; - let previousHistoryMessageWasToolResult = false; - - for (const m of allMessages) { - const currentMessageFollowsToolResult = previousHistoryMessageWasToolResult; - if (m.role === 'user') { - // Historical attachments are replayed as placeholders so screenshots, - // files, and terminal selections do not balloon every follow-up request. - const messageAttachments = m.attachments ?? m.images; - sdkMessages.push({ - role: 'user', - content: buildHistoricalUserReplayContent(m.content, messageAttachments ?? []), - }); - } else if (m.role === 'assistant') { - const activeContinuation = isProviderContinuationForSource( - m.providerContinuation, - continuationContext.source, - ) - ? m.providerContinuation - : undefined; - const openAIChatAssistantFields = getOpenAIChatAssistantFieldsForHistoryMessage( - m, - continuationContext.source, - ); - if (m.toolCalls?.length) { - // Only include tool calls that have matching results - const resolvedToolCalls = resolvedToolCallsByAssistant.get(m); - const resolvedCalls = resolvedToolCalls - ? m.toolCalls.filter(tc => resolvedToolCalls.has(tc)) - : []; - const contentParts: AssistantContentPart[] = []; - if (resolvedCalls.length > 0) { - for (const part of activeContinuation?.reasoningParts ?? []) { - if (!part.text && !part.providerOptions) continue; - contentParts.push({ - type: 'reasoning' as const, - text: part.text, - ...(part.providerOptions ? { providerOptions: part.providerOptions } : {}), - }); - } - } - if (m.content) { - contentParts.push({ - type: 'text' as const, - text: m.content, - ...(activeContinuation?.textProviderOptions ? { providerOptions: activeContinuation.textProviderOptions } : {}), - }); - } - for (const tc of resolvedCalls) { - const providerOptions = activeContinuation?.toolCallProviderOptionsById?.[tc.id]; - contentParts.push({ - type: 'tool-call' as const, - toolCallId: tc.id, - toolName: tc.name, - input: tc.arguments ?? {}, - ...(providerOptions ? { providerOptions } : {}), - }); - } - // If all tool calls were orphaned, just include the text content - if (contentParts.length > 0) { - const message: ModelMessage = { role: 'assistant', content: toAssistantModelContent(contentParts) }; - sdkMessages.push(message); - if (resolvedCalls.length > 0) { - rememberOpenAIChatAssistantFields(message, openAIChatAssistantFields, nextFieldsByMessage); - } - } - } else if (m.content) { - const contentParts: AssistantContentPart[] = []; - for (const part of activeContinuation?.reasoningParts ?? []) { - if (!part.text && !part.providerOptions) continue; - contentParts.push({ - type: 'reasoning' as const, - text: part.text, - ...(part.providerOptions ? { providerOptions: part.providerOptions } : {}), - }); - } - contentParts.push({ - type: 'text' as const, - text: m.content, - ...(activeContinuation?.textProviderOptions ? { providerOptions: activeContinuation.textProviderOptions } : {}), - }); - const message: ModelMessage = { - role: 'assistant', - content: toAssistantModelContent(contentParts), - }; - sdkMessages.push(message); - if (currentMessageFollowsToolResult) { - rememberOpenAIChatAssistantFields(message, openAIChatAssistantFields, nextFieldsByMessage); - } - } - } else if (m.role === 'tool' && m.toolResults?.length) { - sdkMessages.push({ - role: 'tool', - content: m.toolResults.map(tr => { - const toolCall = toolCallByToolResult.get(tr); - return { - type: 'tool-result' as const, - toolCallId: tr.toolCallId, - toolName: toolCall?.name ?? 'unknown', - output: { - type: 'text' as const, - value: buildHistoricalToolResultReplayText(tr, toolCall, { - preserveTerminalOutput: preserveTerminalToolResults.has(tr), - }), - }, - }; - }), - }); - } - previousHistoryMessageWasToolResult = m.role === 'tool' && !!m.toolResults?.length; - } - - if (includeCurrentUserMessage) { - // Build the current user message — include attachments as multimodal content - if (attachments?.length) { - const modelText = buildPromptWithTerminalSelectionAttachments(trimmed, attachments); - const modelAttachments = attachments.filter( - (attachment) => !isTerminalSelectionAttachment(attachment), - ); - if (!modelAttachments.length) { - sdkMessages.push({ role: 'user', content: modelText }); - } else { - const parts: Array<{ type: 'text'; text: string } | { type: 'image'; image: string; mediaType?: string } | { type: 'file'; data: string; mediaType: string; filename?: string }> = []; - parts.push({ type: 'text', text: modelText }); - for (const att of modelAttachments) { - if (att.mediaType.startsWith('image/')) { - parts.push({ type: 'image', image: att.base64Data, mediaType: att.mediaType }); - } else { - parts.push({ type: 'file', data: att.base64Data, mediaType: att.mediaType, filename: att.filename }); - } - } - sdkMessages.push({ role: 'user', content: parts }); - } - } else { - sdkMessages.push({ role: 'user', content: trimmed }); - } - } - - openAIChatAssistantFieldsByMessage = nextFieldsByMessage; - return sdkMessages; - }; - - const sdkMessages = buildSdkMessages(currentSession?.messages ?? [], true); - const collectToolResultsAfterMessage = ( - messages: ChatMessage[], - messageId: string, - ): Set => { - const results = new Set(); - let afterMessage = false; - for (const message of messages) { - if (message.id === messageId) { - afterMessage = true; - continue; - } - if (!afterMessage || message.role !== 'tool' || !message.toolResults?.length) continue; - for (const result of message.toolResults) { - results.add(result); - } - } - return results; - }; - - // Create model with placeholder API key — the main process injects the real - // decrypted key when the HTTP request is proxied through IPC, so plaintext - // keys never transit the renderer ↔ main IPC boundary. - let model; - try { - model = createModelFromConfig( - { - ...context.activeProvider, - defaultModel: activeModelId, - }, - { - getOpenAIChatAssistantFields: () => continuationContext.openAIChatAssistantFields, - }, - ); - } catch (e) { - console.error('[Catty] Model creation failed:', e); - reportStreamError(sessionId, abortController.signal, `Model creation failed: ${e instanceof Error ? e.message : String(e)}`); - return; - } - - const contextWindow = resolveContextWindow({ - provider: context.activeProvider, - modelId: activeModelId, - defaultContextWindow: DEFAULT_CONTEXT_WINDOW_TOKENS, + await getAgentRuntime().runTurn({ + backend: 'catty', + chatSessionId: sessionId, + sendScopeKey, + userText: trimmed, + signal: abortController.signal, + currentSession, + assistantMsgId, + context, + attachments, + maxIterations, + bridge, + ui: uiCallbacks(), }); - const outputReserveTokens = Math.min(4096, Math.ceil(contextWindow * 0.05)); - const getRequestReserveTokens = () => outputReserveTokens + estimateUnknownTokens({ - systemPrompt, - toolNames: Object.keys(tools), - openAIChatAssistantFields: Array.from(openAIChatAssistantFieldsByMessage.values()), - }); - - const summarizeForCompaction = async (messagesToSummarize: ModelMessage[]) => { - updateLastMessage(sessionId, msg => ({ ...msg, statusText: 'Compacting earlier context...' })); - const result = await generateText({ - model, - system: CONTEXT_COMPACTION_SYSTEM_PROMPT, - messages: [{ - role: 'user', - content: `Summarize this earlier conversation context for the next model turn:\n\n${formatMessagesForCompaction(messagesToSummarize)}`, - }], - abortSignal: abortController.signal, - maxOutputTokens: 1600, - temperature: 0, - }); - return result.text; - }; - const prepareMessagesForStream = (messages: ModelMessage[]): ModelMessage[] => { - const pruned = pruneMessages({ - messages, - reasoning: 'all', - emptyMessages: 'remove', - }); - continuationContext.openAIChatAssistantFields = collectOpenAIChatAssistantFieldsForMessages( - pruned, - openAIChatAssistantFieldsByMessage, - ); - return pruned; - }; - const compactMessages = async ( - messages: ModelMessage[], - { - force = false, - statusText, - fallbackLog, - compressForRequestTooLargeRetry = false, - compressionLog, - }: { - force?: boolean; - statusText?: string; - fallbackLog: string; - compressForRequestTooLargeRetry?: boolean; - compressionLog?: string; - }, - ): Promise => { - const compressRetryMessages = (candidateMessages: ModelMessage[], log?: string): ModelMessage[] => { - if (!compressForRequestTooLargeRetry) return candidateMessages; - const compressed = compressMessagesForRequestTooLargeRetry(candidateMessages); - if (compressed.didAdjust && log) { - console.warn(log); - } - return compressed.messages; - }; - - try { - if (statusText) { - updateLastMessage(sessionId, msg => ({ ...msg, statusText })); - } - const inputMessages = compressRetryMessages(messages, compressionLog); - const compacted = await prepareContextCompaction({ - messages: inputMessages, - contextWindow, - reservedTokens: getRequestReserveTokens(), - thresholdRatio: force ? 0 : undefined, - protectRecentMessages: DEFAULT_PROTECT_RECENT_MESSAGES, - summarize: summarizeForCompaction, - }); - let nextMessages = force && !compacted.didCompact - ? keepRecentContextMessages(inputMessages, DEFAULT_PROTECT_RECENT_MESSAGES) - : compacted.messages; - return compressRetryMessages(nextMessages); - } catch (err) { - if (abortController.signal.aborted) throw err; - console.warn(fallbackLog, err); - const fallbackMessages = keepRecentContextMessages(messages, DEFAULT_PROTECT_RECENT_MESSAGES); - if (!compressForRequestTooLargeRetry) { - return fallbackMessages; - } - const compressed = compressMessagesForRequestTooLargeRetry(fallbackMessages); - if (compressed.didAdjust) { - console.warn('[Catty] Request content compressed after compaction fallback.'); - } - return compressed.messages; - } - }; - let messagesForStream = sdkMessages; - messagesForStream = await compactMessages(messagesForStream, { - fallbackLog: '[Catty] Context compaction failed; falling back to recent messages only:', - }); - - messagesForStream = prepareMessagesForStream(messagesForStream); - - try { - await processCattyStream( - sessionId, - model, - systemPrompt, - tools, - messagesForStream, - abortController.signal, - assistantMsgId, - context.activeProvider?.advancedParams, - continuationContext, - ); - } catch (streamErr) { - if (abortController.signal.aborted || !isRequestTooLargeError(streamErr)) { - throw streamErr; - } - - console.warn('[Catty] Request hit HTTP 413; forcing context compaction and retrying once.', streamErr); - const statusText = 'Request was too large. Compacting context and retrying...'; - const hadToolProgress = hadToolProgressBeforeRequestTooLarge(streamErr); - let retryBaseMessages = messagesForStream; - let retryAssistantMsgId = assistantMsgId; - if (hadToolProgress) { - const latestSession = latestAISessionsSnapshot?.find(session => session.id === sessionId); - if (latestSession) { - retryBaseMessages = buildSdkMessages(latestSession.messages, false, { - preserveTerminalToolResults: collectToolResultsAfterMessage( - latestSession.messages, - assistantMsgId, - ), - }); - } - retryAssistantMsgId = generateId(); - addMessageToSession(sessionId, { - id: retryAssistantMsgId, - role: 'assistant', - content: '', - timestamp: Date.now(), - model: activeModelId || context.activeProvider?.defaultModel || '', - providerId: context.activeProvider?.providerId, - statusText, - }); - } else { - updateMessageById(sessionId, assistantMsgId, msg => ({ - ...msg, - content: '', - thinking: undefined, - thinkingDurationMs: undefined, - providerContinuation: undefined, - toolCalls: undefined, - errorInfo: undefined, - executionStatus: undefined, - pendingApproval: undefined, - statusText, - })); - } - const retryMessages = prepareMessagesForStream(await compactMessages(retryBaseMessages, { - force: true, - statusText, - fallbackLog: '[Catty] Forced context compaction after 413 failed; falling back to recent messages only:', - compressForRequestTooLargeRetry: true, - compressionLog: '[Catty] Request content compressed after forced context compaction.', - })); - - await processCattyStream( - sessionId, - model, - systemPrompt, - tools, - retryMessages, - abortController.signal, - retryAssistantMsgId, - context.activeProvider?.advancedParams, - continuationContext, - ); - } - } catch (err) { - console.error('[Catty] streamText error:', err); - reportStreamError(sessionId, abortController.signal, err); } finally { - // Clear any lingering statusText when the stream finishes - updateLastMessage(sessionId, msg => msg.statusText ? { ...msg, statusText: '' } : msg); - setStreamingForScope(sessionId, false); abortControllersRef.current.delete(sessionId); - context.autoTitleSession(sessionId, context.titleText ?? trimmed); } - }, [ - processCattyStream, reportStreamError, setStreamingForScope, - addMessageToSession, updateLastMessage, updateMessageById, - ]); + }, [maxIterations, uiCallbacks]); return { streamingSessionIds, setStreamingForScope, abortControllersRef, - processCattyStream, sendToCattyAgent, sendToExternalAgent, reportStreamError, diff --git a/components/ai/toolArtifacts/VaultArtifactCard.test.tsx b/components/ai/toolArtifacts/VaultArtifactCard.test.tsx new file mode 100644 index 000000000..880197392 --- /dev/null +++ b/components/ai/toolArtifacts/VaultArtifactCard.test.tsx @@ -0,0 +1,42 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import React from 'react'; +import { renderToStaticMarkup } from 'react-dom/server'; + +import { I18nProvider } from '../../../application/i18n/I18nProvider.tsx'; +import { VaultArtifactCard } from './VaultArtifactCard.tsx'; + +test('VaultArtifactCard renders note artifact title', () => { + const html = renderToStaticMarkup( + + + , + ); + + assert.match(html, /Runbook/); + assert.match(html, /ops\/prod/); +}); + +test('VaultArtifactCard renders host batch summary', () => { + const html = renderToStaticMarkup( + + + , + ); + + assert.match(html, /Added 2 hosts/); + assert.match(html, /Web/); +}); diff --git a/components/ai/toolArtifacts/VaultArtifactCard.tsx b/components/ai/toolArtifacts/VaultArtifactCard.tsx new file mode 100644 index 000000000..845ca5dc0 --- /dev/null +++ b/components/ai/toolArtifacts/VaultArtifactCard.tsx @@ -0,0 +1,130 @@ +import { ChevronRight } from 'lucide-react'; +import React from 'react'; +import { useI18n } from '../../../application/i18n/I18nProvider'; +import { cn } from '../../../lib/utils'; +import type { VaultToolArtifact } from './vaultToolArtifact'; +import { navigateVaultArtifact, useVaultArtifactNavigation } from './VaultArtifactNavigationContext'; +import { VaultArtifactIcon } from './vaultArtifactPresentation'; + +interface VaultArtifactCardProps { + artifact: VaultToolArtifact; + toolName?: string; + className?: string; +} + +function getArtifactPresentation( + artifact: VaultToolArtifact, + t: (key: string, values?: Record) => string, +): { + title: string; + subtitle?: string; + clickable: boolean; +} { + switch (artifact.kind) { + case 'vault.note': + return { + title: artifact.title, + subtitle: artifact.group ?? t('ai.chat.artifact.noteFallback'), + clickable: true, + }; + case 'vault.host': + return { + title: artifact.label, + subtitle: artifact.port + ? `${artifact.hostname}:${artifact.port}` + : artifact.hostname, + clickable: true, + }; + case 'vault.hosts.batch': { + const preview = artifact.preview + .slice(0, 2) + .map((host) => host.label || host.hostname) + .filter(Boolean) + .join(', '); + return { + title: artifact.dryRun + ? t('ai.chat.artifact.hostsPreview', { count: artifact.addedCount }) + : t('ai.chat.artifact.hostsAdded', { count: artifact.addedCount }), + subtitle: preview || t('ai.chat.artifact.openHosts'), + clickable: true, + }; + } + case 'vault.summary': + return { + title: artifact.section === 'notes' + ? t('ai.chat.artifact.notesSummary', { count: artifact.count }) + : t('ai.chat.artifact.hostsSummary', { count: artifact.count }), + subtitle: artifact.section === 'notes' + ? t('ai.chat.artifact.openNotes') + : t('ai.chat.artifact.openHosts'), + clickable: true, + }; + case 'error': + return { + title: t('ai.chat.artifact.failed'), + subtitle: artifact.message, + clickable: false, + }; + default: + return { + title: '', + clickable: false, + }; + } +} + +export const VaultArtifactCard: React.FC = ({ + artifact, + toolName, + className, +}) => { + const { t } = useI18n(); + const navigation = useVaultArtifactNavigation(); + const presentation = getArtifactPresentation(artifact, t); + + const handleClick = () => { + if (!presentation.clickable || !navigation) return; + navigateVaultArtifact(artifact, navigation); + }; + + const content = ( + <> + +
+
+ {presentation.title} +
+ {presentation.subtitle && ( +
+ {presentation.subtitle} +
+ )} +
+ {presentation.clickable && ( + + )} + + ); + + if (!presentation.clickable) { + return ( +
+ {content} +
+ ); + } + + return ( + + ); +}; diff --git a/components/ai/toolArtifacts/VaultArtifactNavigationContext.tsx b/components/ai/toolArtifacts/VaultArtifactNavigationContext.tsx new file mode 100644 index 000000000..07d8d7e14 --- /dev/null +++ b/components/ai/toolArtifacts/VaultArtifactNavigationContext.tsx @@ -0,0 +1,89 @@ +import React, { createContext, useCallback, useContext, useMemo } from 'react'; +import type { Host, VaultNote } from '../../../types'; +import { useI18n } from '../../../application/i18n/I18nProvider'; +import { toast } from '../../ui/toast'; +import type { VaultToolArtifact } from './vaultToolArtifact'; + +export interface VaultArtifactNavigationActions { + openVaultNote: (noteId: string) => void; + openVaultHost: (hostId: string) => void; + openVaultSection: (section: 'notes' | 'hosts') => void; +} + +interface VaultArtifactNavigationProviderProps { + notes: VaultNote[]; + hosts: Host[]; + onOpenVaultNote: (noteId: string) => void; + onOpenVaultHost: (hostId: string) => void; + onOpenVaultSection: (section: 'notes' | 'hosts') => void; + children: React.ReactNode; +} + +const VaultArtifactNavigationContext = createContext(null); + +export function VaultArtifactNavigationProvider({ + notes, + hosts, + onOpenVaultNote, + onOpenVaultHost, + onOpenVaultSection, + children, +}: VaultArtifactNavigationProviderProps) { + const { t } = useI18n(); + + const openVaultNote = useCallback((noteId: string) => { + const exists = notes.some((note) => note.id === noteId); + if (!exists) { + toast.warning(t('ai.chat.artifact.noteMissing'), t('ai.chat.artifact.unavailableTitle')); + return; + } + onOpenVaultNote(noteId); + }, [notes, onOpenVaultNote, t]); + + const openVaultHost = useCallback((hostId: string) => { + const exists = hosts.some((host) => host.id === hostId); + if (!exists) { + toast.warning(t('ai.chat.artifact.hostMissing'), t('ai.chat.artifact.unavailableTitle')); + return; + } + onOpenVaultHost(hostId); + }, [hosts, onOpenVaultHost, t]); + + const value = useMemo(() => ({ + openVaultNote, + openVaultHost, + openVaultSection: onOpenVaultSection, + }), [onOpenVaultHost, onOpenVaultNote, onOpenVaultSection, openVaultHost, openVaultNote]); + + return ( + + {children} + + ); +} + +export function useVaultArtifactNavigation(): VaultArtifactNavigationActions | null { + return useContext(VaultArtifactNavigationContext); +} + +export function navigateVaultArtifact( + artifact: VaultToolArtifact, + navigation: VaultArtifactNavigationActions, +): void { + switch (artifact.kind) { + case 'vault.note': + navigation.openVaultNote(artifact.noteId); + break; + case 'vault.host': + navigation.openVaultHost(artifact.hostId); + break; + case 'vault.hosts.batch': + navigation.openVaultSection('hosts'); + break; + case 'vault.summary': + navigation.openVaultSection(artifact.section); + break; + default: + break; + } +} diff --git a/components/ai/toolArtifacts/VaultArtifactToolResult.tsx b/components/ai/toolArtifacts/VaultArtifactToolResult.tsx new file mode 100644 index 000000000..edc9cc418 --- /dev/null +++ b/components/ai/toolArtifacts/VaultArtifactToolResult.tsx @@ -0,0 +1,38 @@ +import React from 'react'; +import { Tooltip, TooltipContent, TooltipTrigger } from '../../ui/tooltip'; +import { VaultArtifactCard } from './VaultArtifactCard'; +import { formatVaultToolTooltip } from './formatVaultToolTooltip'; +import type { VaultToolArtifact } from './vaultToolArtifact'; + +interface VaultArtifactToolResultProps { + artifact: VaultToolArtifact; + toolName: string; + args?: Record; + result?: unknown; + isError?: boolean; +} + +export const VaultArtifactToolResult: React.FC = ({ + artifact, + toolName, + args, + result, + isError, +}) => { + const tooltip = formatVaultToolTooltip(toolName, args, result, isError); + + return ( + + + + + + {tooltip} + + + ); +}; diff --git a/components/ai/toolArtifacts/formatVaultToolTooltip.test.ts b/components/ai/toolArtifacts/formatVaultToolTooltip.test.ts new file mode 100644 index 000000000..877016b81 --- /dev/null +++ b/components/ai/toolArtifacts/formatVaultToolTooltip.test.ts @@ -0,0 +1,18 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +import { formatVaultToolTooltip } from './formatVaultToolTooltip.ts'; + +test('formatVaultToolTooltip joins tool name, args, and result with line breaks', () => { + const text = formatVaultToolTooltip( + 'vault_notes_create', + { title: 'Runbook' }, + { ok: true, note: { id: 'n1', title: 'Runbook' } }, + ); + + assert.match(text, /^vault_notes_create/); + assert.match(text, /Arguments:/); + assert.match(text, /"title": "Runbook"/); + assert.match(text, /Result:/); + assert.match(text, /"ok": true/); +}); diff --git a/components/ai/toolArtifacts/formatVaultToolTooltip.ts b/components/ai/toolArtifacts/formatVaultToolTooltip.ts new file mode 100644 index 000000000..d00e4653c --- /dev/null +++ b/components/ai/toolArtifacts/formatVaultToolTooltip.ts @@ -0,0 +1,37 @@ +function stringifyToolPayload(value: unknown): string { + if (value == null) return ''; + if (typeof value === 'string') { + try { + const parsed = JSON.parse(value) as unknown; + if (parsed && typeof parsed === 'object') { + return JSON.stringify(parsed, null, 2); + } + } catch { + return value; + } + return value; + } + if (typeof value === 'object') { + return JSON.stringify(value, null, 2); + } + return String(value); +} + +export function formatVaultToolTooltip( + toolName: string, + args?: Record, + result?: unknown, + isError?: boolean, +): string { + const lines: string[] = [toolName]; + + if (args && Object.keys(args).length > 0) { + lines.push('', 'Arguments:', stringifyToolPayload(args)); + } + + if (result !== undefined) { + lines.push('', isError ? 'Error:' : 'Result:', stringifyToolPayload(result)); + } + + return lines.join('\n'); +} diff --git a/components/ai/toolArtifacts/vaultArtifactPresentation.test.ts b/components/ai/toolArtifacts/vaultArtifactPresentation.test.ts new file mode 100644 index 000000000..567e3defe --- /dev/null +++ b/components/ai/toolArtifacts/vaultArtifactPresentation.test.ts @@ -0,0 +1,45 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +import { resolveVaultArtifactVisualKind } from './vaultArtifactPresentation.tsx'; + +test('resolveVaultArtifactVisualKind distinguishes note vs host tools', () => { + assert.equal( + resolveVaultArtifactVisualKind( + { kind: 'vault.note', noteId: 'n1', title: 'Doc' }, + 'vault_notes_create', + ), + 'noteCreate', + ); + assert.equal( + resolveVaultArtifactVisualKind( + { kind: 'vault.host', hostId: 'h1', label: 'Web', hostname: '10.0.0.1' }, + 'host_get', + ), + 'host', + ); + assert.equal( + resolveVaultArtifactVisualKind( + { + kind: 'vault.hosts.batch', + sourceTool: 'vault_hosts_import', + addedCount: 2, + preview: [], + }, + 'vault_hosts_import', + ), + 'hostImport', + ); + assert.equal( + resolveVaultArtifactVisualKind( + { + kind: 'vault.hosts.batch', + sourceTool: 'vault_hosts_create', + addedCount: 2, + preview: [], + }, + 'vault_hosts_create', + ), + 'hostCreate', + ); +}); diff --git a/components/ai/toolArtifacts/vaultArtifactPresentation.tsx b/components/ai/toolArtifacts/vaultArtifactPresentation.tsx new file mode 100644 index 000000000..e1705bba0 --- /dev/null +++ b/components/ai/toolArtifacts/vaultArtifactPresentation.tsx @@ -0,0 +1,115 @@ +import { + AlertCircle, + FilePenLine, + FileText, + FolderInput, + LayoutGrid, + Library, + NotebookPen, + Server, + ServerCog, +} from 'lucide-react'; +import React from 'react'; +import { cn } from '../../../lib/utils'; +import type { VaultToolArtifact } from './vaultToolArtifact'; + +export type VaultArtifactVisualKind = + | 'noteCreate' + | 'noteUpdate' + | 'noteRead' + | 'noteList' + | 'host' + | 'hostCreate' + | 'hostImport' + | 'hostList' + | 'error'; + +const ARTIFACT_ICON_SIZE = 18; + +const VISUAL_STYLES: Record = { + noteCreate: { wrapper: 'bg-violet-500/12', icon: 'text-violet-400' }, + noteUpdate: { wrapper: 'bg-violet-500/10', icon: 'text-violet-300/90' }, + noteRead: { wrapper: 'bg-violet-500/10', icon: 'text-violet-300/80' }, + noteList: { wrapper: 'bg-muted/30', icon: 'text-muted-foreground/70' }, + host: { wrapper: 'bg-emerald-500/12', icon: 'text-emerald-400' }, + hostCreate: { wrapper: 'bg-sky-500/12', icon: 'text-sky-400' }, + hostImport: { wrapper: 'bg-amber-500/12', icon: 'text-amber-400' }, + hostList: { wrapper: 'bg-muted/30', icon: 'text-muted-foreground/70' }, + error: { wrapper: 'bg-destructive/10', icon: 'text-destructive/80' }, +}; + +export function resolveVaultArtifactVisualKind( + artifact: VaultToolArtifact, + toolName?: string, +): VaultArtifactVisualKind { + if (artifact.kind === 'error') return 'error'; + + if (artifact.kind === 'vault.note') { + if (toolName === 'vault_notes_create') return 'noteCreate'; + if (toolName === 'vault_notes_update') return 'noteUpdate'; + return 'noteRead'; + } + + if (artifact.kind === 'vault.host') return 'host'; + + if (artifact.kind === 'vault.hosts.batch') { + if (artifact.sourceTool === 'vault_hosts_import' || toolName === 'vault_hosts_import') { + return 'hostImport'; + } + return 'hostCreate'; + } + + if (artifact.kind === 'vault.summary') { + return artifact.section === 'notes' ? 'noteList' : 'hostList'; + } + + return 'host'; +} + +function renderVisualIcon(kind: VaultArtifactVisualKind): React.ReactNode { + const className = VISUAL_STYLES[kind].icon; + switch (kind) { + case 'noteCreate': + return ; + case 'noteUpdate': + return ; + case 'noteRead': + return ; + case 'noteList': + return ; + case 'host': + return ; + case 'hostCreate': + return ; + case 'hostImport': + return ; + case 'hostList': + return ; + case 'error': + return ; + default: + return ; + } +} + +export function VaultArtifactIcon({ + artifact, + toolName, +}: { + artifact: VaultToolArtifact; + toolName?: string; +}) { + const kind = resolveVaultArtifactVisualKind(artifact, toolName); + const styles = VISUAL_STYLES[kind]; + + return ( + + {renderVisualIcon(kind)} + + ); +} diff --git a/components/ai/toolArtifacts/vaultToolArtifact.test.ts b/components/ai/toolArtifacts/vaultToolArtifact.test.ts new file mode 100644 index 000000000..c6b9557f2 --- /dev/null +++ b/components/ai/toolArtifacts/vaultToolArtifact.test.ts @@ -0,0 +1,74 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +import { parseVaultToolArtifact } from './vaultToolArtifact.ts'; + +test('parseVaultToolArtifact maps note create results', () => { + const artifact = parseVaultToolArtifact('vault_notes_create', { + ok: true, + note: { id: 'note-1', title: 'Runbook', group: 'ops/prod' }, + }); + assert.deepEqual(artifact, { + kind: 'vault.note', + noteId: 'note-1', + title: 'Runbook', + group: 'ops/prod', + }); +}); + +test('parseVaultToolArtifact maps single host create to host artifact', () => { + const artifact = parseVaultToolArtifact('vault_hosts_create', { + ok: true, + addedCount: 1, + previewHosts: [ + { id: 'host-1', label: 'Dokploy', hostname: '10.2.0.209' }, + ], + }); + assert.deepEqual(artifact, { + kind: 'vault.host', + hostId: 'host-1', + label: 'Dokploy', + hostname: '10.2.0.209', + }); +}); + +test('parseVaultToolArtifact maps host batch import results', () => { + const artifact = parseVaultToolArtifact('vault_hosts_create', { + ok: true, + addedCount: 2, + previewHosts: [ + { id: 'host-1', label: 'Web', hostname: '10.0.0.1' }, + { id: 'host-2', hostname: 'db.internal' }, + ], + }); + assert.equal(artifact?.kind, 'vault.hosts.batch'); + if (artifact?.kind !== 'vault.hosts.batch') return; + assert.equal(artifact.addedCount, 2); + assert.equal(artifact.preview.length, 2); +}); + +test('parseVaultToolArtifact maps host get results', () => { + const artifact = parseVaultToolArtifact('host_get', { + ok: true, + host: { id: 'host-9', label: 'Prod', hostname: 'prod.example.com', port: 22 }, + }); + assert.deepEqual(artifact, { + kind: 'vault.host', + hostId: 'host-9', + label: 'Prod', + hostname: 'prod.example.com', + port: 22, + group: undefined, + }); +}); + +test('parseVaultToolArtifact maps errors', () => { + const artifact = parseVaultToolArtifact('vault_notes_get', { + ok: false, + error: 'Vault note "missing" was not found.', + }); + assert.deepEqual(artifact, { + kind: 'error', + message: 'Vault note "missing" was not found.', + }); +}); diff --git a/components/ai/toolArtifacts/vaultToolArtifact.ts b/components/ai/toolArtifacts/vaultToolArtifact.ts new file mode 100644 index 000000000..6b23517da --- /dev/null +++ b/components/ai/toolArtifacts/vaultToolArtifact.ts @@ -0,0 +1,183 @@ +export type VaultToolArtifact = + | { + kind: 'vault.note'; + noteId: string; + title: string; + group?: string; + } + | { + kind: 'vault.host'; + hostId: string; + label: string; + hostname: string; + port?: number; + group?: string; + } + | { + kind: 'vault.hosts.batch'; + sourceTool?: 'vault_hosts_create' | 'vault_hosts_import'; + addedCount: number; + dryRun?: boolean; + preview: Array<{ hostId?: string; label?: string; hostname?: string }>; + } + | { + kind: 'vault.summary'; + section: 'notes' | 'hosts'; + count: number; + } + | { + kind: 'error'; + message: string; + }; + +const VAULT_ARTIFACT_TOOL_NAMES = new Set([ + 'vault_notes_create', + 'vault_notes_update', + 'vault_notes_get', + 'vault_notes_list', + 'vault_hosts_create', + 'vault_hosts_import', + 'vault_hosts_list', + 'host_get', +]); + +function parseResultPayload(result: unknown): Record | null { + if (result == null) return null; + if (typeof result === 'string') { + try { + const parsed = JSON.parse(result) as unknown; + if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { + return parsed as Record; + } + return null; + } catch { + return null; + } + } + if (typeof result === 'object' && !Array.isArray(result)) { + return result as Record; + } + return null; +} + +function readString(value: unknown): string | undefined { + return typeof value === 'string' && value.trim() ? value.trim() : undefined; +} + +function readNumber(value: unknown): number | undefined { + return typeof value === 'number' && Number.isFinite(value) ? value : undefined; +} + +function parseNoteArtifact(note: unknown): VaultToolArtifact | null { + if (!note || typeof note !== 'object') return null; + const record = note as Record; + const noteId = readString(record.id); + const title = readString(record.title); + if (!noteId || !title) return null; + return { + kind: 'vault.note', + noteId, + title, + group: readString(record.group), + }; +} + +function parseHostArtifact(host: unknown): VaultToolArtifact | null { + if (!host || typeof host !== 'object') return null; + const record = host as Record; + const hostId = readString(record.id); + const hostname = readString(record.hostname); + if (!hostId || !hostname) return null; + return { + kind: 'vault.host', + hostId, + label: readString(record.label) ?? hostname, + hostname, + port: readNumber(record.port), + group: readString(record.group), + }; +} + +function parsePreviewHosts(value: unknown): Array<{ hostId?: string; label?: string; hostname?: string }> { + if (!Array.isArray(value)) return []; + return value + .map((entry) => { + if (!entry || typeof entry !== 'object') return null; + const record = entry as Record; + const hostname = readString(record.hostname); + if (!hostname) return null; + return { + hostId: readString(record.id), + label: readString(record.label), + hostname, + }; + }) + .filter((entry): entry is { hostId?: string; label?: string; hostname: string } => entry !== null); +} + +export function isVaultArtifactToolName(toolName: string): boolean { + return VAULT_ARTIFACT_TOOL_NAMES.has(toolName); +} + +export function parseVaultToolArtifact( + toolName: string, + result: unknown, +): VaultToolArtifact | null { + if (!isVaultArtifactToolName(toolName)) return null; + + const payload = parseResultPayload(result); + if (!payload) return null; + + if (payload.ok === false || payload.isError === true) { + const message = readString(payload.error) ?? 'Operation failed.'; + return { kind: 'error', message }; + } + + switch (toolName) { + case 'vault_notes_create': + case 'vault_notes_update': + case 'vault_notes_get': + return parseNoteArtifact(payload.note); + case 'vault_notes_list': { + const notes = Array.isArray(payload.notes) ? payload.notes : []; + return { kind: 'vault.summary', section: 'notes', count: notes.length }; + } + case 'vault_hosts_create': + case 'vault_hosts_import': { + const preview = parsePreviewHosts(payload.previewHosts); + const addedCount = readNumber(payload.addedCount) + ?? (payload.dryRun === true ? readNumber(payload.validCount) : undefined) + ?? preview.length; + if (addedCount <= 0 && preview.length === 0) return null; + + const dryRun = payload.dryRun === true; + if (!dryRun && addedCount === 1 && preview.length === 1 && preview[0].hostname) { + const single = preview[0]; + if (single.hostId) { + return { + kind: 'vault.host', + hostId: single.hostId, + label: single.label ?? single.hostname, + hostname: single.hostname, + }; + } + } + + return { + kind: 'vault.hosts.batch', + sourceTool: toolName === 'vault_hosts_import' ? 'vault_hosts_import' : 'vault_hosts_create', + addedCount, + dryRun, + preview, + }; + } + case 'vault_hosts_list': { + const hosts = Array.isArray(payload.hosts) ? payload.hosts : []; + return { kind: 'vault.summary', section: 'hosts', count: hosts.length }; + } + case 'host_get': + return parseHostArtifact(payload.host); + default: + return null; + } +} diff --git a/components/notes/NotesManager.tsx b/components/notes/NotesManager.tsx index 684150c67..4fe216cd9 100644 --- a/components/notes/NotesManager.tsx +++ b/components/notes/NotesManager.tsx @@ -115,6 +115,8 @@ export interface NotesManagerProps { onOpenHost?: (host: Host, source?: { noteId: string }) => void; displayMode?: "full" | "sidebar"; openNoteId?: string | null; + /** Called after a one-shot openNoteId focus request has been applied. */ + onOpenNoteIdHandled?: () => void; } type HoverActionMenuProps = { @@ -326,11 +328,12 @@ export const NotesManager: React.FC = ({ onOpenHost, displayMode = "full", openNoteId = null, + onOpenNoteIdHandled, }) => { const { t } = useI18n(); const { openExternal } = useApplicationBackend(); const isSidebarMode = displayMode === "sidebar"; - const initialOpenNoteId = isSidebarMode && openNoteId && notes.some((note) => note.id === openNoteId) + const initialOpenNoteId = openNoteId && notes.some((note) => note.id === openNoteId) ? openNoteId : null; const [query, setQuery] = useState(""); @@ -413,17 +416,18 @@ export const NotesManager: React.FC = ({ }, [selectedNote?.group]); useEffect(() => { - if (!isSidebarMode || !openNoteId) return; + if (!openNoteId) return; const note = sortedNotes.find((item) => item.id === openNoteId); if (!note) return; - const nextSelection = getNoteSelectionState(note, true); + const nextSelection = getNoteSelectionState(note, isSidebarMode); setSelectedNoteId(nextSelection.selectedNoteId); setSelectedGroup(nextSelection.selectedGroup); setOverlayNoteId(nextSelection.overlayNoteId); if (note.group) { setExpandedGroups((current) => new Set([...current, ...ancestorNoteGroupPaths(note.group || "")])); } - }, [isSidebarMode, openNoteId, sortedNotes]); + onOpenNoteIdHandled?.(); + }, [isSidebarMode, onOpenNoteIdHandled, openNoteId, sortedNotes]); useEffect(() => { if (expandedPanel !== "search") return; diff --git a/components/settings/tabs/SettingsAITab.tsx b/components/settings/tabs/SettingsAITab.tsx index 0f34a52dd..708ecc9ea 100644 --- a/components/settings/tabs/SettingsAITab.tsx +++ b/components/settings/tabs/SettingsAITab.tsx @@ -44,6 +44,8 @@ import { ClaudeCodeCard } from "./ai/ClaudeCodeCard"; import { CopilotCliCard } from "./ai/CopilotCliCard"; import { CodebuddyCard } from "./ai/CodebuddyCard"; import { SafetySettings } from "./ai/SafetySettings"; +import { PermissionGrantsSettings } from "./ai/PermissionGrantsSettings"; +import { useAIPermissionGrantsState } from "../../../application/state/useAIPermissionGrantsState"; import { WebSearchSettings } from "./ai/WebSearchSettings"; import { QuickMessagesSettings } from "./ai/QuickMessagesSettings"; import type { AIQuickMessage } from "../../../infrastructure/ai/quickMessages"; @@ -187,6 +189,14 @@ const SettingsAITab: React.FC = ({ showTerminalSelectionAIAction, setShowTerminalSelectionAIAction, }) => { + const { + permissionGrants, + addGrant, + updateGrant, + removeGrant, + importGrants, + exportGrants, + } = useAIPermissionGrantsState(); const { t } = useI18n(); const [editingProviderId, setEditingProviderId] = useState(null); const [codexIntegration, setCodexIntegration] = useState(null); @@ -1154,6 +1164,14 @@ const SettingsAITab: React.FC = ({ maxIterations={maxIterations} setMaxIterations={setMaxIterations} /> + diff --git a/components/settings/tabs/ai/PermissionGrantsSettings.tsx b/components/settings/tabs/ai/PermissionGrantsSettings.tsx new file mode 100644 index 000000000..cfa797843 --- /dev/null +++ b/components/settings/tabs/ai/PermissionGrantsSettings.tsx @@ -0,0 +1,255 @@ +import React, { useCallback, useMemo, useRef, useState } from 'react'; +import { Download, Plus, Trash2, Upload } from 'lucide-react'; +import { useI18n } from '../../../../application/i18n/I18nProvider'; +import { Button } from '../../../ui/button'; +import { SettingCard, SettingsSection } from '../../settings-ui'; +import { Tooltip, TooltipContent, TooltipTrigger } from '../../../ui/tooltip'; +import type { PermissionGrantRule } from '../../../../infrastructure/ai/harness/permissionGrants'; +import { + capabilitySupportsCommandPatternGrant, + createPermissionGrantId, + listGrantableCapabilityIds, +} from '../../../../infrastructure/ai/harness/permissionGrants'; + +const cellInputClass = + 'w-full min-w-0 max-w-full h-7 rounded border border-input bg-background px-2 text-xs font-mono focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring overflow-x-auto whitespace-nowrap scrollbar-thin'; + +const cellSelectClass = + `${cellInputClass} font-sans truncate pr-6`; + +const GrantCellInput: React.FC<{ + value: string; + placeholder?: string; + mono?: boolean; + onChange: (value: string) => void; +}> = ({ value, placeholder, mono = true, onChange }) => ( + onChange(e.target.value)} + className={mono ? cellInputClass : `${cellInputClass} font-sans whitespace-normal`} + title={value} + /> +); + +const GrantCapabilitySelect: React.FC<{ + value: string; + options: readonly string[]; + onChange: (value: string) => void; +}> = ({ value, options, onChange }) => { + const selectOptions = useMemo(() => { + if (options.includes(value)) return options; + return [value, ...options]; + }, [options, value]); + + return ( + + ); +}; + +export const PermissionGrantsSettings: React.FC<{ + grants: PermissionGrantRule[]; + addGrant: (rule: PermissionGrantRule) => void; + updateGrant: (id: string, updates: Partial>) => void; + removeGrant: (id: string) => void; + importGrants: (raw: unknown, mode?: 'merge' | 'replace') => void; + exportGrants: () => PermissionGrantRule[]; +}> = ({ + grants, + addGrant, + updateGrant, + removeGrant, + importGrants, + exportGrants, +}) => { + const { t } = useI18n(); + const fileInputRef = useRef(null); + const [importError, setImportError] = useState(null); + const grantableCapabilityIds = useMemo(() => listGrantableCapabilityIds(), []); + + const handleAdd = useCallback(() => { + addGrant({ + id: createPermissionGrantId(), + capabilityId: grantableCapabilityIds[0] ?? 'terminal.execute', + sessionPattern: '*', + createdAt: Date.now(), + }); + }, [addGrant, grantableCapabilityIds]); + + const handleExport = useCallback(() => { + const payload = exportGrants(); + const blob = new Blob([JSON.stringify(payload, null, 2)], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + const anchor = document.createElement('a'); + anchor.href = url; + anchor.download = 'netcatty-permission-grants.json'; + anchor.click(); + URL.revokeObjectURL(url); + }, [exportGrants]); + + const handleImportFile = useCallback(async (file: File) => { + setImportError(null); + try { + const text = await file.text(); + const parsed = JSON.parse(text) as unknown; + importGrants(parsed, 'replace'); + } catch (error) { + setImportError(error instanceof Error ? error.message : String(error)); + } + }, [importGrants]); + + return ( + + +
+
+

{t('ai.safety.grants.heading')}

+

{t('ai.safety.grants.description')}

+
+
+ + + + { + const file = event.target.files?.[0]; + event.target.value = ''; + if (file) void handleImportFile(file); + }} + /> +
+
+ + {importError && ( +

{importError}

+ )} + + {grants.length === 0 ? ( +

+ {t('ai.safety.grants.empty')} +

+ ) : ( +
+ + + + + + + + + + + + + + + + {grants.map((grant) => { + const supportsCommandPattern = capabilitySupportsCommandPatternGrant(grant.capabilityId); + + return ( + + + + + + + ); + })} + +
+ {t('ai.safety.grants.capability')} + + {t('ai.safety.grants.commandPattern')} + + {t('ai.safety.grants.note')} + +
+ { + const updates: Partial> = { + capabilityId, + }; + if (!capabilitySupportsCommandPatternGrant(capabilityId)) { + updates.commandPattern = undefined; + } + updateGrant(grant.id, updates); + }} + /> + + {supportsCommandPattern ? ( + updateGrant(grant.id, { + commandPattern: commandPattern.trim() || undefined, + })} + /> + ) : ( + — + )} + + updateGrant(grant.id, { + note: note.trim() || undefined, + })} + /> + + + + + + {t('ai.safety.grants.remove')} + +
+
+ )} +
+
+ ); +}; diff --git a/components/settings/tabs/ai/managedAgentState.ts b/components/settings/tabs/ai/managedAgentState.ts index 2548a0ef3..b78e419ab 100644 --- a/components/settings/tabs/ai/managedAgentState.ts +++ b/components/settings/tabs/ai/managedAgentState.ts @@ -1,16 +1,13 @@ import type { ExternalAgentConfig } from "../../../../infrastructure/ai/types"; import { type ManagedAgentKey, + getExternalAgentSdkBackend, + isPathLikeCommand, } from "../../../../infrastructure/ai/managedAgents"; import type { AgentPathInfo } from "./types"; import { AGENT_DEFAULTS } from "./types"; import { buildCodebuddyEnv } from "./codebuddyConfigEnv"; -function isPathLikeCommand(command: string | undefined): boolean { - const normalized = String(command || "").trim(); - return normalized.includes("/") || normalized.includes("\\"); -} - function getAutoManagedAgentStoredPath( agents: ExternalAgentConfig[], agentKey: ManagedAgentKey, diff --git a/components/terminalLayer/TerminalLayerSidePanelSection.tsx b/components/terminalLayer/TerminalLayerSidePanelSection.tsx index b3a1414bd..5974b101e 100644 --- a/components/terminalLayer/TerminalLayerSidePanelSection.tsx +++ b/components/terminalLayer/TerminalLayerSidePanelSection.tsx @@ -143,6 +143,9 @@ function TerminalLayerSidePanelTabBody({ ctx }: { ctx: SidePanelContext }) { NotesManager, noteGroups, notes, + onOpenVaultHostFromChat, + onOpenVaultNoteFromChat, + onOpenVaultSectionFromChat, scriptsMountedTabIds, systemMountedTabIds, themeMountedTabIds, @@ -676,6 +679,11 @@ function TerminalLayerSidePanelTabBody({ ctx }: { ctx: SidePanelContext }) { resolveExecutorContext={resolveAIExecutorContext} pendingTerminalSelection={pendingTerminalSelectionForAI} onPendingTerminalSelectionConsumed={handlePendingTerminalSelectionConsumed} + notes={notes} + hosts={hosts} + onOpenVaultNoteFromChat={onOpenVaultNoteFromChat} + onOpenVaultHostFromChat={onOpenVaultHostFromChat} + onOpenVaultSectionFromChat={onOpenVaultSectionFromChat} /> )} diff --git a/components/terminalLayer/TerminalLayerSupport.tsx b/components/terminalLayer/TerminalLayerSupport.tsx index 30540203a..49b5164f6 100644 --- a/components/terminalLayer/TerminalLayerSupport.tsx +++ b/components/terminalLayer/TerminalLayerSupport.tsx @@ -268,8 +268,31 @@ export type AITerminalSessionInfo = { shellType?: string; deviceType?: string; connected: boolean; + hostChain?: Array<{ hostId: string; label?: string; hostname?: string }>; + activePortForwards?: Array<{ + ruleId: string; + label?: string; + type?: string; + localPort?: number; + status?: string; + }>; }; +function summarizeHostChain( + host: Host | undefined, + allHosts: Host[], +): AITerminalSessionInfo['hostChain'] | undefined { + if (!host?.hostChain?.hostIds?.length) return undefined; + return host.hostChain.hostIds.map((hostId) => { + const jumpHost = allHosts.find((entry) => entry.id === hostId); + return { + hostId, + label: jumpHost?.label, + hostname: jumpHost?.hostname, + }; + }); +} + export type AIPanelContext = { scopeType: 'terminal' | 'workspace'; scopeTargetId?: string; @@ -286,9 +309,26 @@ export const buildAITerminalSessionInfo = ( session: TerminalSession | undefined, host: Host | undefined, localOs: 'linux' | 'macos' | 'windows', + options?: { + allHosts?: Host[]; + portForwardingRules?: import('../../domain/models').PortForwardingRule[]; + }, ): AITerminalSessionInfo => { const protocol = session?.protocol || host?.protocol; const isLocalSession = protocol === 'local' || session?.hostId?.startsWith('local-'); + const allHosts = options?.allHosts ?? (host ? [host] : []); + const hostChain = summarizeHostChain(host, allHosts); + const activePortForwards = host?.id && options?.portForwardingRules + ? options.portForwardingRules + .filter((rule) => rule.hostId === host.id && (rule.status === 'active' || rule.status === 'connecting')) + .map((rule) => ({ + ruleId: rule.id, + label: rule.label, + type: rule.type, + localPort: rule.localPort, + status: rule.status, + })) + : undefined; return { sessionId: session?.id || '', hostId: session?.hostId || '', @@ -302,6 +342,8 @@ export const buildAITerminalSessionInfo = ( // PTY and cannot connect to vendor CLIs, so network device mode doesn't apply. deviceType: (session?.moshEnabled || host?.moshEnabled || session?.etEnabled || host?.etEnabled) ? undefined : host?.deviceType, connected: session?.status === 'connected', + ...(hostChain?.length ? { hostChain } : {}), + ...(activePortForwards?.length ? { activePortForwards } : {}), }; }; @@ -317,6 +359,11 @@ interface AIChatPanelsHostProps { }) => ExecutorContext; pendingTerminalSelection?: PendingTerminalSelectionForAI | null; onPendingTerminalSelectionConsumed?: (requestId: string) => void; + notes: VaultNote[]; + hosts: Host[]; + onOpenVaultNoteFromChat?: (noteId: string) => void; + onOpenVaultHostFromChat?: (hostId: string) => void; + onOpenVaultSectionFromChat?: (section: 'notes' | 'hosts') => void; } interface AIStateMaintenanceHostProps { @@ -384,6 +431,11 @@ function aiChatPanelsHostAreEqual( if (prev.pendingTerminalSelection !== next.pendingTerminalSelection) return false; if (prev.onPendingTerminalSelectionConsumed !== next.onPendingTerminalSelectionConsumed) return false; if (prev.resolveExecutorContext !== next.resolveExecutorContext) return false; + if (prev.notes !== next.notes) return false; + if (prev.hosts !== next.hosts) return false; + if (prev.onOpenVaultNoteFromChat !== next.onOpenVaultNoteFromChat) return false; + if (prev.onOpenVaultHostFromChat !== next.onOpenVaultHostFromChat) return false; + if (prev.onOpenVaultSectionFromChat !== next.onOpenVaultSectionFromChat) return false; if (prev.activeTabId === next.activeTabId) return true; for (let i = 0; i < prev.mountedTabIds.length; i += 1) { @@ -403,6 +455,11 @@ const AIChatPanelsHostInner: React.FC = ({ resolveExecutorContext, pendingTerminalSelection, onPendingTerminalSelectionConsumed, + notes, + hosts, + onOpenVaultNoteFromChat, + onOpenVaultHostFromChat, + onOpenVaultSectionFromChat, }) => { const aiState = useContext(AIStateContext); @@ -511,6 +568,11 @@ const AIChatPanelsHostInner: React.FC = ({ terminalSessions={context.terminalSessions} resolveExecutorContext={resolveExecutorContext} isVisible={isVisible} + notes={notes} + hosts={hosts} + onOpenVaultNote={onOpenVaultNoteFromChat} + onOpenVaultHost={onOpenVaultHostFromChat} + onOpenVaultSection={onOpenVaultSectionFromChat} /> @@ -526,6 +588,7 @@ AIChatPanelsHost.displayName = 'AIChatPanelsHost'; export interface TerminalLayerProps { hosts: Host[]; + portForwardingRules?: import('../../domain/models').PortForwardingRule[]; customGroups: string[]; groupConfigs: GroupConfig[]; proxyProfiles: ProxyProfile[]; @@ -536,6 +599,9 @@ export interface TerminalLayerProps { notes: VaultNote[]; noteGroups: string[]; openNoteRequest?: { tabId: string; noteId: string; requestId: number } | null; + onOpenVaultNoteFromChat?: (noteId: string) => void; + onOpenVaultHostFromChat?: (hostId: string) => void; + onOpenVaultSectionFromChat?: (section: 'notes' | 'hosts') => void; sessions: TerminalSession[]; workspaces: Workspace[]; knownHosts?: KnownHost[]; diff --git a/components/terminalLayer/TerminalLayerTabBridge.tsx b/components/terminalLayer/TerminalLayerTabBridge.tsx index 6c890d371..148fa842b 100644 --- a/components/terminalLayer/TerminalLayerTabBridge.tsx +++ b/components/terminalLayer/TerminalLayerTabBridge.tsx @@ -197,7 +197,10 @@ export function TerminalLayerTabBridge({ stableRef }: { stableRef: StableRef }) }); const { aiContextsByTabId, resolveAIExecutorContext } = useTerminalAiContexts({ + hosts: s.hosts, hostsRef: s.hostsRef, + portForwardingRules: s.portForwardingRules, + portForwardingRulesRef: s.portForwardingRulesRef, mountedAiTabIds: s.mountedAiTabIds, sessionHostsMap, sessions, diff --git a/components/terminalLayer/terminalLayerStableSnapshot.ts b/components/terminalLayer/terminalLayerStableSnapshot.ts index 29d5e6bb0..29a086e58 100644 --- a/components/terminalLayer/terminalLayerStableSnapshot.ts +++ b/components/terminalLayer/terminalLayerStableSnapshot.ts @@ -52,6 +52,8 @@ export type TerminalLayerStableSnapshot = { sessionsRef: React.MutableRefObject; workspacesRef: React.MutableRefObject; hostsRef: React.MutableRefObject; + portForwardingRules: import('../../domain/models').PortForwardingRule[]; + portForwardingRulesRef: React.MutableRefObject; onSetWorkspaceFocusedSessionRef: React.MutableRefObject; sidePanelOpenTabs: Map; setSidePanelOpenTabs: React.Dispatch>>; diff --git a/components/terminalLayer/useTerminalAiContexts.ts b/components/terminalLayer/useTerminalAiContexts.ts index 8b00f2284..4f05f8a9c 100644 --- a/components/terminalLayer/useTerminalAiContexts.ts +++ b/components/terminalLayer/useTerminalAiContexts.ts @@ -3,11 +3,14 @@ import { useCallback, useMemo } from 'react'; import { collectSessionIds } from '../../domain/workspace'; import { detectLocalOs } from '../../lib/localShell'; -import type { Host, TerminalSession, Workspace } from '../../types'; +import type { Host, PortForwardingRule, TerminalSession, Workspace } from '../../types'; import { buildAITerminalSessionInfo, type AIPanelContext } from './TerminalLayerSupport'; interface UseTerminalAiContextsOptions { + hosts: Host[]; hostsRef: React.MutableRefObject; + portForwardingRules: PortForwardingRule[]; + portForwardingRulesRef: React.MutableRefObject; mountedAiTabIds: string[]; sessionHostsMap: Map; sessions: TerminalSession[]; @@ -17,7 +20,10 @@ interface UseTerminalAiContextsOptions { } export function useTerminalAiContexts({ + hosts, hostsRef, + portForwardingRules, + portForwardingRulesRef, mountedAiTabIds, sessionHostsMap, sessions, @@ -25,89 +31,93 @@ export function useTerminalAiContexts({ workspaces, workspacesRef, }: UseTerminalAiContextsOptions) { - // Build per-tab AI contexts so hidden panels can stay mounted without - // recomputing scope resolution from scratch on every tab switch. - const aiContextsByTabId = useMemo(() => { - const localOs = detectLocalOs(navigator.userAgent || navigator.platform); - const sessionById = new Map(sessions.map((session) => [session.id, session])); - const workspaceById = new Map(workspaces.map((workspace) => [workspace.id, workspace])); - const tabIds = new Set(mountedAiTabIds); - - const contexts = new Map(); - - for (const tabId of tabIds) { - const workspace = workspaceById.get(tabId); - if (workspace) { - const sessionIds = collectSessionIds(workspace.root); - contexts.set(tabId, { - scopeType: 'workspace', - scopeTargetId: workspace.id, - scopeHostIds: sessionIds - .map((sessionId) => sessionById.get(sessionId)?.hostId) - .filter((hostId): hostId is string => !!hostId), - scopeLabel: workspace.title, - terminalSessions: sessionIds.map((sessionId) => - buildAITerminalSessionInfo( - sessionById.get(sessionId), - sessionHostsMap.get(sessionId), - localOs, - ), - ), - }); - continue; - } - - const session = sessionById.get(tabId); - if (!session) continue; - + const aiContextsByTabId = useMemo(() => { + const localOs = detectLocalOs(navigator.userAgent || navigator.platform); + const sessionById = new Map(sessions.map((session) => [session.id, session])); + const workspaceById = new Map(workspaces.map((workspace) => [workspace.id, workspace])); + const allHosts = hostsRef.current; + const tabIds = new Set(mountedAiTabIds); + + const contexts = new Map(); + + for (const tabId of tabIds) { + const workspace = workspaceById.get(tabId); + if (workspace) { + const sessionIds = collectSessionIds(workspace.root); contexts.set(tabId, { - scopeType: 'terminal', - scopeTargetId: session.id, - scopeHostIds: session.hostId ? [session.hostId] : [], - scopeLabel: session.hostLabel ?? '', - terminalSessions: [ + scopeType: 'workspace', + scopeTargetId: workspace.id, + scopeHostIds: sessionIds + .map((sessionId) => sessionById.get(sessionId)?.hostId) + .filter((hostId): hostId is string => !!hostId), + scopeLabel: workspace.title, + terminalSessions: sessionIds.map((sessionId) => buildAITerminalSessionInfo( - session, - sessionHostsMap.get(session.id), + sessionById.get(sessionId), + sessionHostsMap.get(sessionId), localOs, + { allHosts, portForwardingRules }, ), - ], + ), }); + continue; } - - return contexts; - }, [sessions, workspaces, mountedAiTabIds, sessionHostsMap]); - + + const session = sessionById.get(tabId); + if (!session) continue; + + contexts.set(tabId, { + scopeType: 'terminal', + scopeTargetId: session.id, + scopeHostIds: session.hostId ? [session.hostId] : [], + scopeLabel: session.hostLabel ?? '', + terminalSessions: [ + buildAITerminalSessionInfo( + session, + sessionHostsMap.get(session.id), + localOs, + { allHosts, portForwardingRules }, + ), + ], + }); + } + + return contexts; + }, [sessions, workspaces, mountedAiTabIds, sessionHostsMap, hosts, portForwardingRules]); + const resolveAIExecutorContext = useCallback((scope: { - type: 'terminal' | 'workspace'; - targetId?: string; - label?: string; - }) => { - const latestWorkspaces = workspacesRef.current; - const latestSessions = sessionsRef.current; - const latestHosts = hostsRef.current; - const localOs = detectLocalOs(navigator.userAgent || navigator.platform); - const sessionIds = scope.type === 'workspace' - ? (() => { - const workspace = scope.targetId ? latestWorkspaces.find((w) => w.id === scope.targetId) : undefined; - return workspace?.root ? collectSessionIds(workspace.root) : []; - })() - : scope.targetId ? [scope.targetId] : []; - - const workspaceName = scope.type === 'workspace' - ? latestWorkspaces.find((w) => w.id === scope.targetId)?.title ?? scope.label - : undefined; - - return { - sessions: sessionIds.map((sid) => { - const session = latestSessions.find((s) => s.id === sid); - const host = session?.hostId ? latestHosts.find((h) => h.id === session.hostId) : undefined; - return buildAITerminalSessionInfo(session, host, localOs); - }), - workspaceId: scope.type === 'workspace' ? scope.targetId : undefined, - workspaceName, - }; - }, [hostsRef, sessionsRef, workspacesRef]); + type: 'terminal' | 'workspace'; + targetId?: string; + label?: string; + }) => { + const latestWorkspaces = workspacesRef.current; + const latestSessions = sessionsRef.current; + const latestHosts = hostsRef.current; + const localOs = detectLocalOs(navigator.userAgent || navigator.platform); + const sessionIds = scope.type === 'workspace' + ? (() => { + const workspace = scope.targetId ? latestWorkspaces.find((w) => w.id === scope.targetId) : undefined; + return workspace?.root ? collectSessionIds(workspace.root) : []; + })() + : scope.targetId ? [scope.targetId] : []; + + const workspaceName = scope.type === 'workspace' + ? latestWorkspaces.find((w) => w.id === scope.targetId)?.title ?? scope.label + : undefined; + + return { + sessions: sessionIds.map((sid) => { + const session = latestSessions.find((s) => s.id === sid); + const host = session?.hostId ? latestHosts.find((h) => h.id === session.hostId) : undefined; + return buildAITerminalSessionInfo(session, host, localOs, { + allHosts: latestHosts, + portForwardingRules: portForwardingRulesRef.current, + }); + }), + workspaceId: scope.type === 'workspace' ? scope.targetId : undefined, + workspaceName, + }; + }, [hostsRef, portForwardingRulesRef, sessionsRef, workspacesRef]); return { aiContextsByTabId, diff --git a/components/terminalLayerMemo.ts b/components/terminalLayerMemo.ts index b91923b7e..dab4e08eb 100644 --- a/components/terminalLayerMemo.ts +++ b/components/terminalLayerMemo.ts @@ -12,6 +12,9 @@ export const terminalLayerAreEqual = ( prev.notes === next.notes && prev.noteGroups === next.noteGroups && prev.openNoteRequest === next.openNoteRequest && + prev.onOpenVaultNoteFromChat === next.onOpenVaultNoteFromChat && + prev.onOpenVaultHostFromChat === next.onOpenVaultHostFromChat && + prev.onOpenVaultSectionFromChat === next.onOpenVaultSectionFromChat && prev.sessions === next.sessions && prev.workspaces === next.workspaces && prev.knownHosts === next.knownHosts && diff --git a/components/vault/VaultViewLayout.tsx b/components/vault/VaultViewLayout.tsx index 3e862676c..2459d6265 100644 --- a/components/vault/VaultViewLayout.tsx +++ b/components/vault/VaultViewLayout.tsx @@ -18,7 +18,7 @@ const VaultSectionLoading = () => ( export function VaultViewLayout({ ctx }: { ctx: VaultViewLayoutContext }) { const { Activity, allGroupPaths, allTags, AppLogo, Array, Badge, BookMarked, Boolean, Button, cancelInlineGroupEdit, CheckSquare, ChevronDown, clearHostSelection, ClipboardCopy, Clock, cn, commitInlineGroupRename, connectionLogs, connectSelectedHosts, ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, Copy, currentSection, customGroups, deleteGroupPath, deleteGroupWithHosts, deleteSelectedHosts, deleteTargetPath, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, displayedGroups, displayedHosts, DistroAvatar, Download, Dropdown, DropdownContent, DropdownTrigger, Edit2, editingGroupPath, editingHost, editingHostGroupDefaults, FileCode, FileSymlink, FolderPlus, FolderTree, getDropTargetClasses, getEffectiveHostDistro, Globe, groupConfigs, GroupDetailsPanel, groupedDisplayHosts, handleConnectClick, handleCopyCredentials, handleDeleteTag, handleDuplicateHost, handleEditGroupConfig, handleEditHost, handleEditTag, handleExportHosts, handleHostConnect, handleImportFileSelected, handleNewHost, handleProtocolSelect, handleQuickConnect, handleQuickConnectSaveHost, handleSaveGroupConfig, handleSearchKeyDown, handleUnmanageGroup, handleSidebarWidthCommit, hasHostsSidePanel, HostDetailsPanel, hostListScrollRef, hosts, HostTreeView, hotkeyScheme, identities, ImportVaultDialog, Input, isDeleteGroupOpen, isGroupPanelOpen, isHostPanelOpen, isHostsSectionActive, isImportOpen, isMultiSelectMode, isNewFolderOpen, isQuickConnectOpen, isRenameGroupOpen, isSearchQuickConnect, isSerialModalOpen, Key, keyBindings, KeychainManager, keys, knownHostsManagerElement, Label, lastPinnedId, LayoutGrid, LazyConnectionLogsManager, LazyProtocolSelectDialog, List, managedGroupPaths, managedSources, moveGroup, moveHostToGroup, Network, newFolderName, newHostGroupPath, onClearUnsavedConnectionLogs, onConnectSerial, onCreateLocalTerminal, onDeleteConnectionLog, onDeleteHost, onImportOrReuseKey, onOpenLogView, onOpenSettings, onRunSnippet, onToggleConnectionLogSaved, onUpdateCustomGroups, onUpdateGroupConfigs, onUpdateHosts, onUpdateIdentities, onUpdateKeys, onUpdateProxyProfiles, onUpdateSnippetPackages, onUpdateSnippets, Pin, pinnedHosts, pinnedRecentIds, Plug, Plus, PortForwarding, protocolSelectHost, proxyProfiles, ProxyProfilesManager, quickConnectTarget, quickConnectWarnings, QuickConnectWizard, recentHosts, renameGroupError, renameGroupName, renameTargetPath, reorderGroup, reorderHost, RippleButton, rootRef, sanitizeHost, search, selectedGroupPath, selectedHostIds, selectedTags, SerialConnectModal, SerialHostDetailsPanel, sessionCount, Set, setCurrentSection, setDeleteGroupWithHosts, setDeleteTargetPath, setDragOverDropTarget, setEditingGroupPath, setEditingHost, setGroupDragOverDropTarget, setIsDeleteGroupOpen, setIsGroupPanelOpen, setIsHostPanelOpen, setIsImportOpen, setIsMultiSelectMode, setIsNewFolderOpen, setIsQuickConnectOpen, setIsRenameGroupOpen, setIsSerialModalOpen, setLastPinnedId, setNewFolderName, setNewHostGroupPath, setProtocolSelectHost, setQuickConnectTarget, setQuickConnectWarnings, setRenameGroupError, setRenameGroupName, setRenameTargetPath, setSearch, setSelectedGroupPath, setSelectedHostIds, setSelectedTags, setSidebarCollapsed, setSidebarWidth, setSortMode, setTargetParentPath, Settings, setViewMode, shellHistory, shouldHideEmptyRootHostsSection, showRecentHosts, sidebarCollapsed, sidebarWidth, snippetPackages, snippets, SnippetsManager, SortDropdown, sortMode, splitViewGridStyle, Square, Star, startInlineDeleteGroup, startInlineNewGroup, startInlineRenameGroup, submitNewFolder, submitRenameGroup, Suspense, t, TagFilterDropdown, targetParentPath, terminalFontSize, terminalSettings, TerminalSquare, terminalThemeId, toggleHostPinned, toggleHostSelection, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, Trash2, treeExpandedState, treeViewGroupTree, treeViewHosts, Upload, upsertHostById, Usb, viewMode, visibleDisplayedHosts, X, Zap } = ctx; - const { knownHosts, noteGroups, NotebookText, notes, NotesManager, onOpenHostFromNote, onUpdateNoteGroups, onUpdateNotes } = ctx; + const { knownHosts, noteGroups, NotebookText, notes, NotesManager, onOpenHostFromNote, onOpenNoteIdHandled, onUpdateNoteGroups, onUpdateNotes, openNoteId } = ctx; const [isSidebarResizing, setIsSidebarResizing] = React.useState(false); const sidebarMinWidth = 56; const sidebarMaxWidth = 320; @@ -604,6 +604,8 @@ export function VaultViewLayout({ ctx }: { ctx: VaultViewLayoutContext }) { hosts={hosts} onUpdateNotes={onUpdateNotes} onUpdateNoteGroups={onUpdateNoteGroups} + openNoteId={openNoteId ?? null} + onOpenNoteIdHandled={onOpenNoteIdHandled} onOpenHost={(host: any, source: any) => { if (source?.noteId && onOpenHostFromNote) { onOpenHostFromNote(host, source); diff --git a/components/vault/useVaultImportHandlers.ts b/components/vault/useVaultImportHandlers.ts index da1a92f4c..a5049daec 100644 --- a/components/vault/useVaultImportHandlers.ts +++ b/components/vault/useVaultImportHandlers.ts @@ -2,7 +2,7 @@ import { useCallback } from "react"; import { sanitizeHost } from "../../domain/host"; import { readTextFile } from "../../lib/readTextFile"; -import { importVaultHostsFromText, type VaultImportFormat } from "../../domain/vaultImport"; +import { applyVaultHostImport, importVaultHostsFromText, type VaultImportFormat } from "../../domain/vaultImport"; import type { Host, ManagedSource } from "../../types"; import type { ImportOptions } from "./ImportVaultDialog"; import { toast } from "../ui/toast"; @@ -153,16 +153,9 @@ export function useVaultImportHandlers({ ) as string[]; onUpdateCustomGroups(nextGroups); } else if (newHosts.length > 0) { - onUpdateHosts([...hosts, ...newHosts].map(sanitizeHost)); - - const nextGroups = Array.from( - new Set([ - ...customGroups, - ...result.groups, - ...newHosts.map((h) => h.group).filter(Boolean), - ]), - ) as string[]; - onUpdateCustomGroups(nextGroups); + const merged = applyVaultHostImport(hosts, customGroups, result, { skipDuplicates: true }); + onUpdateHosts(merged.hosts); + onUpdateCustomGroups(merged.customGroups); } // Count total hosts affected (new + converted to managed) diff --git a/domain/vaultHostCreate.test.ts b/domain/vaultHostCreate.test.ts new file mode 100644 index 000000000..a4d4e5ba3 --- /dev/null +++ b/domain/vaultHostCreate.test.ts @@ -0,0 +1,58 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +import type { Host } from './models.ts'; +import { + applyVaultHostCreates, + buildVaultHostFromDraft, + buildVaultHostsFromDrafts, + parseVaultHostDraftsInput, +} from './vaultHostCreate.ts'; + +test('buildVaultHostFromDraft maps minimal unstructured fields to a vault host', () => { + const built = buildVaultHostFromDraft({ + hostname: '192.168.1.10', + username: 'ubuntu', + label: 'prod web', + group: 'infra/prod', + tags: 'web, nginx', + }); + + assert.equal(built.ok, true); + if (!built.ok) return; + assert.equal(built.host.hostname, '192.168.1.10'); + assert.equal(built.host.username, 'ubuntu'); + assert.equal(built.host.group, 'infra/prod'); + assert.deepEqual(built.host.tags, ['web', 'nginx']); +}); + +test('parseVaultHostDraftsInput accepts JSON array strings', () => { + const parsed = parseVaultHostDraftsInput(JSON.stringify([ + { hostname: '10.0.0.1', username: 'root' }, + { hostname: '10.0.0.2', username: 'deploy', port: 2222 }, + ])); + + assert.equal(parsed.ok, true); + if (!parsed.ok) return; + assert.equal(parsed.drafts.length, 2); +}); + +test('applyVaultHostCreates writes sanitized hosts into the vault list', () => { + const existing: Host = { + id: 'host-1', + label: 'db', + hostname: '10.0.0.99', + username: 'root', + port: 22, + tags: [], + os: 'linux', + }; + const { hosts: built } = buildVaultHostsFromDrafts([ + { hostname: '10.0.0.10', username: 'deploy', group: 'prod' }, + ]); + const merged = applyVaultHostCreates([existing], ['legacy'], built); + + assert.equal(merged.addedCount, 1); + assert.equal(merged.hosts.length, 2); + assert.ok(merged.customGroups.includes('prod')); +}); diff --git a/domain/vaultHostCreate.ts b/domain/vaultHostCreate.ts new file mode 100644 index 000000000..33c62c587 --- /dev/null +++ b/domain/vaultHostCreate.ts @@ -0,0 +1,190 @@ +import type { Host, HostProtocol } from './models'; +import { sanitizeHost } from './host'; + +const DEFAULT_SSH_PORT = 22; + +export type VaultHostDraftProtocol = Exclude; + +export interface VaultHostDraft { + label?: unknown; + hostname?: unknown; + port?: unknown; + username?: unknown; + password?: unknown; + group?: unknown; + tags?: unknown; + notes?: unknown; + protocol?: unknown; +} + +export interface VaultHostCreateIssue { + index: number; + error: string; +} + +const normalizeGroupPath = (raw: unknown): string | undefined => { + if (typeof raw !== 'string') return undefined; + const trimmed = raw.trim(); + if (!trimmed) return undefined; + const parts = trimmed.replace(/\\/g, '/').split('/').map((part) => part.trim()).filter(Boolean); + return parts.length ? parts.join('/') : undefined; +}; + +const normalizeProtocol = (raw: unknown): VaultHostDraftProtocol | undefined => { + if (typeof raw !== 'string') return undefined; + const value = raw.trim().toLowerCase(); + if (value === 'ssh' || value === 'ssh2') return 'ssh'; + if (value === 'telnet') return 'telnet'; + if (value === 'local') return 'local'; + return undefined; +}; + +const parsePort = (raw: unknown): number | undefined => { + if (typeof raw === 'number' && Number.isFinite(raw)) { + const port = Math.trunc(raw); + return port >= 1 && port <= 65535 ? port : undefined; + } + if (typeof raw !== 'string') return undefined; + const trimmed = raw.trim(); + if (!trimmed) return undefined; + const port = parseInt(trimmed, 10); + return Number.isFinite(port) && port >= 1 && port <= 65535 ? port : undefined; +}; + +const parseTags = (raw: unknown): string[] => { + if (Array.isArray(raw)) { + return Array.from( + new Set(raw.map((entry) => String(entry).trim()).filter(Boolean)), + ); + } + if (typeof raw !== 'string') return []; + return raw + .split(/[,;,]/g) + .map((entry) => entry.trim()) + .filter(Boolean); +}; + +export const buildVaultHostMergeKey = ( + host: Pick, +): string => + `${(host.protocol ?? 'ssh').toLowerCase()}|${host.hostname.toLowerCase()}|${host.port}|${(host.username ?? '').toLowerCase()}`; + +export function buildVaultHostFromDraft( + draft: VaultHostDraft, +): { ok: true; host: Host } | { ok: false; error: string } { + const hostname = typeof draft.hostname === 'string' ? draft.hostname.trim() : ''; + if (!hostname) { + return { ok: false, error: 'hostname is required.' }; + } + + const protocol = normalizeProtocol(draft.protocol) ?? 'ssh'; + const port = parsePort(draft.port) ?? (protocol === 'telnet' ? 23 : DEFAULT_SSH_PORT); + const label = typeof draft.label === 'string' && draft.label.trim() + ? draft.label.trim() + : hostname; + const username = typeof draft.username === 'string' ? draft.username.trim() : ''; + const password = typeof draft.password === 'string' && draft.password ? draft.password : undefined; + const notes = typeof draft.notes === 'string' && draft.notes.trim() ? draft.notes.trim() : undefined; + const now = Date.now(); + + return { + ok: true, + host: { + id: crypto.randomUUID(), + label, + hostname, + port, + username, + password, + group: normalizeGroupPath(draft.group), + tags: parseTags(draft.tags), + os: 'linux', + protocol, + createdAt: now, + ...(notes ? { notes } : {}), + }, + }; +} + +export function parseVaultHostDraftsInput( + value: unknown, +): { ok: true; drafts: VaultHostDraft[] } | { ok: false; error: string } { + let parsed: unknown = value; + if (typeof value === 'string') { + const trimmed = value.trim(); + if (!trimmed) return { ok: false, error: 'hosts is required.' }; + try { + parsed = JSON.parse(trimmed) as unknown; + } catch { + return { ok: false, error: 'hosts must be a JSON array string.' }; + } + } + + if (!Array.isArray(parsed)) { + return { ok: false, error: 'hosts must be a JSON array of host objects.' }; + } + if (parsed.length === 0) { + return { ok: false, error: 'hosts array is empty.' }; + } + + return { ok: true, drafts: parsed as VaultHostDraft[] }; +} + +export function buildVaultHostsFromDrafts( + drafts: VaultHostDraft[], +): { hosts: Host[]; issues: VaultHostCreateIssue[] } { + const hosts: Host[] = []; + const issues: VaultHostCreateIssue[] = []; + + drafts.forEach((draft, index) => { + const built = buildVaultHostFromDraft(draft); + if (!built.ok) { + issues.push({ index, error: built.error }); + return; + } + hosts.push(built.host); + }); + + return { hosts, issues }; +} + +export function applyVaultHostCreates( + existingHosts: Host[], + existingGroups: string[], + createdHosts: Host[], + options?: { skipDuplicates?: boolean }, +): { + hosts: Host[]; + customGroups: string[]; + addedCount: number; + skippedExistingCount: number; + addedHosts: Host[]; +} { + const skipDuplicates = options?.skipDuplicates !== false; + const existingKeys = new Set(existingHosts.map(buildVaultHostMergeKey)); + let newHosts = createdHosts; + let skippedExistingCount = 0; + + if (skipDuplicates) { + newHosts = createdHosts.filter((host) => { + const duplicate = existingKeys.has(buildVaultHostMergeKey(host)); + if (duplicate) skippedExistingCount++; + return !duplicate; + }); + } + + const customGroups = Array.from( + new Set([ + ...existingGroups, + ...newHosts.map((host) => host.group).filter(Boolean), + ]), + ) as string[]; + + return { + hosts: [...existingHosts, ...newHosts].map(sanitizeHost), + customGroups, + addedCount: newHosts.length, + skippedExistingCount, + addedHosts: newHosts, + }; +} diff --git a/domain/vaultImport.test.ts b/domain/vaultImport.test.ts index 26f1ec7f8..f196a8419 100644 --- a/domain/vaultImport.test.ts +++ b/domain/vaultImport.test.ts @@ -1,7 +1,8 @@ import test from "node:test"; import assert from "node:assert/strict"; -import { importVaultHostsFromText } from "./vaultImport.ts"; +import { importVaultHostsFromText, detectVaultImportFormat, applyVaultHostImport } from "./vaultImport.ts"; +import type { Host } from "./models.ts"; test("ssh_config import maps ForwardX11 yes to host X11 forwarding", () => { const result = importVaultHostsFromText("ssh_config", [ @@ -26,3 +27,34 @@ test("ssh_config import maps ForwardX11 no to disabled host X11 forwarding", () assert.equal(result.hosts.length, 1); assert.equal(result.hosts[0].x11Forwarding, false); }); + +test("detectVaultImportFormat recognizes csv and ssh_config exports", () => { + assert.equal( + detectVaultImportFormat("Label,Hostname,Port,Username\nweb,10.0.0.1,22,root"), + "csv", + ); + assert.equal( + detectVaultImportFormat(["Host prod", " HostName prod.example.com", " User deploy"].join("\n")), + "ssh_config", + ); +}); + +test("applyVaultHostImport skips duplicates by default", () => { + const existing: Host = { + id: "existing-1", + label: "web", + hostname: "10.0.0.10", + username: "deploy", + port: 22, + }; + const imported = importVaultHostsFromText("csv", [ + "Label,Hostname,Port,Username", + "web-1,10.0.0.10,22,deploy", + "db-1,10.0.0.20,22,root", + ].join("\n")); + + const merged = applyVaultHostImport([existing], [], imported); + assert.equal(merged.addedCount, 1); + assert.equal(merged.skippedExistingCount, 1); + assert.equal(merged.hosts.length, 2); +}); diff --git a/domain/vaultImport.ts b/domain/vaultImport.ts index 65a38dc83..8879e00ad 100644 --- a/domain/vaultImport.ts +++ b/domain/vaultImport.ts @@ -1,4 +1,12 @@ import { Host, HostChainConfig, HostProtocol } from "./models"; +import { sanitizeHost } from "./host"; +import { + buildVaultHostFromDraft, + buildVaultHostMergeKey, + type VaultHostDraftProtocol, +} from "./vaultHostCreate"; + +export { buildVaultHostMergeKey } from "./vaultHostCreate"; import { parseQuickConnectInput } from "./quickConnect"; import { findHeaderIndex, parseCsv } from "./vaultImport/csvUtils"; export { exportHostsToCsvWithStats, getVaultCsvTemplate } from "./vaultImport/csvExport"; @@ -85,21 +93,29 @@ export type VaultImportFormat = | "securecrt" | "ssh_config"; +export const VAULT_IMPORT_FORMATS: VaultImportFormat[] = [ + "csv", + "putty", + "mobaxterm", + "securecrt", + "ssh_config", +]; + type VaultImportIssueLevel = "warning" | "error"; -interface VaultImportIssue { +export interface VaultImportIssue { level: VaultImportIssueLevel; message: string; } -interface VaultImportStats { +export interface VaultImportStats { parsed: number; imported: number; skipped: number; duplicates: number; } -interface VaultImportResult { +export interface VaultImportResult { hosts: Host[]; groups: string[]; issues: VaultImportIssue[]; @@ -145,8 +161,7 @@ const splitTags = (raw: string | undefined): string[] => { .filter(Boolean); }; -const hostKey = (h: Pick) => - `${(h.protocol ?? "ssh").toLowerCase()}|${h.hostname.toLowerCase()}|${h.port}|${(h.username ?? "").toLowerCase()}`; +const hostKey = buildVaultHostMergeKey; const createHost = (input: { label?: string; @@ -154,27 +169,16 @@ const createHost = (input: { username?: string; password?: string; port?: number; - protocol?: Exclude; + protocol?: VaultHostDraftProtocol; group?: string; tags?: string[]; notes?: string; }): Host => { - const now = Date.now(); - const notes = input.notes?.trim() || undefined; - return { - id: crypto.randomUUID(), - label: input.label?.trim() || input.hostname, - hostname: input.hostname.trim(), - port: input.port ?? DEFAULT_SSH_PORT, - username: input.username?.trim() ?? "", - password: input.password || undefined, - group: normalizeGroupPath(input.group), - tags: (input.tags ?? []).filter(Boolean), - os: "linux", - protocol: input.protocol ?? "ssh", - createdAt: now, - ...(notes ? { notes } : {}), - }; + const built = buildVaultHostFromDraft(input); + if (!built.ok) { + throw new Error(built.error); + } + return built.host; }; const dedupeHosts = (hosts: Host[]): { hosts: Host[]; duplicates: number } => { @@ -933,3 +937,77 @@ export const importVaultHostsFromText = ( } } }; + +export function detectVaultImportFormat(text: string): VaultImportFormat | null { + const input = (text ?? "").trim(); + if (!input) return null; + + if ( + /Windows Registry Editor Version/i.test(input) + || /\\Software\\SimonTatham\\PuTTY\\Sessions/i.test(input) + ) { + return "putty"; + } + + if (/\[Bookmarks_\]/m.test(input) || /\[MobaXterm\]/i.test(input)) { + return "mobaxterm"; + } + + if (/^Host\s+/m.test(input) && (/^\s+HostName\s+/m.test(input) || /^\s+User\s+/m.test(input))) { + return "ssh_config"; + } + + if (/S:"Hostname"/m.test(input) && (/S:"Username"/m.test(input) || /D:"\[Sessions\]/i.test(input))) { + return "securecrt"; + } + + const firstLine = input.split(/\r?\n/, 1)[0] ?? ""; + if ( + /hostname|host(name)?|server/i.test(firstLine) + && (firstLine.includes(",") || firstLine.includes("\t")) + ) { + return "csv"; + } + + return null; +} + +export function applyVaultHostImport( + existingHosts: Host[], + existingGroups: string[], + importResult: VaultImportResult, + options?: { skipDuplicates?: boolean }, +): { + hosts: Host[]; + customGroups: string[]; + addedCount: number; + skippedExistingCount: number; +} { + const skipDuplicates = options?.skipDuplicates !== false; + const existingKeys = new Set(existingHosts.map(buildVaultHostMergeKey)); + let newHosts = importResult.hosts; + let skippedExistingCount = 0; + + if (skipDuplicates) { + newHosts = importResult.hosts.filter((host) => { + const duplicate = existingKeys.has(buildVaultHostMergeKey(host)); + if (duplicate) skippedExistingCount++; + return !duplicate; + }); + } + + const customGroups = Array.from( + new Set([ + ...existingGroups, + ...importResult.groups, + ...newHosts.map((host) => host.group).filter(Boolean), + ]), + ) as string[]; + + return { + hosts: [...existingHosts, ...newHosts].map(sanitizeHost), + customGroups, + addedCount: newHosts.length, + skippedExistingCount, + }; +} diff --git a/electron/bridges/aiBridge.cjs b/electron/bridges/aiBridge.cjs index c4b70bd99..ee72dd471 100644 --- a/electron/bridges/aiBridge.cjs +++ b/electron/bridges/aiBridge.cjs @@ -13,6 +13,7 @@ const { randomUUID } = require("node:crypto"); const { spawn, execFileSync } = require("node:child_process"); const fs = require("node:fs"); const { existsSync } = fs; +const { appendVaultAgentGuidance } = require("../shared/vaultAgentGuidance.cjs"); const mcpServerBridge = require("./mcpServerBridge.cjs"); const { getCliLauncherPath, TOOL_CLI_DISCOVERY_ENV_VAR } = require("../cli/discoveryPath.cjs"); @@ -22,6 +23,7 @@ const { toPublicUserSkillsStatus, } = require("./ai/userSkills.cjs"); const { registerProviderHandlers } = require("./aiBridge/providerHandlers.cjs"), { registerCattyExecHandlers } = require("./aiBridge/cattyExecHandlers.cjs"), { createAgentCliHelpers } = require("./aiBridge/agentCliHelpers.cjs"); +const { createVaultAgentBridge } = require("./aiBridge/vaultAgentBridge.cjs"); const { registerAgentDiscoveryHandlers } = require("./aiBridge/agentDiscoveryHandlers.cjs"), { registerAgentProcessHandlers } = require("./aiBridge/agentProcessHandlers.cjs"), { registerSdkStreamHandlers } = require("./aiBridge/sdk/sdkStreamHandlers.cjs"); const { probeClaudeAuth, probeCopilotAuth, probeCodexAuth, probeCodebuddyAuth } = require("./aiBridge/agentAuthProbes.cjs"); @@ -187,7 +189,7 @@ function buildExternalAgentSystemContext({ mode, chatSessionId, defaultTargetSes ); } - return ( + return appendVaultAgentGuidance( `${userSkillsPreamble}` + `[Context: You are inside Netcatty, a multi-session terminal manager. ` + `Use the "netcatty-remote-hosts" MCP tools to operate only on the terminal sessions exposed by Netcatty. ` + @@ -197,7 +199,7 @@ function buildExternalAgentSystemContext({ mode, chatSessionId, defaultTargetSes `Use terminal_execute only for commands likely to finish within about 60 seconds. ` + `For long-running commands such as builds, scans, follow/log streaming, watch commands, or anything likely to exceed 60 seconds on PTY-backed shell sessions, use terminal_start, then terminal_poll until completed is true. Reuse the returned nextOffset for the next poll. If terminal_poll reports outputTruncated=true, only the retained tail starting at outputBaseOffset is still available. Do not poll aggressively: wait at least about 30 seconds between polls, and increase the interval further when there is no new output, to avoid wasting tokens. As soon as completed is true, stop polling and analyze the result immediately. ` + `Use terminal_stop if you need to interrupt a started long-running command. Note: terminal_start requires a PTY-backed session; for sessions that only support exec-channel execution (no writable PTY), use terminal_execute instead. ` + - `For serial/raw sessions and network device sessions (deviceType: network), commands are sent as-is without shell wrapping and exit codes are unavailable. Use vendor CLI commands directly.]` + `For serial/raw sessions and network device sessions (deviceType: network), commands are sent as-is without shell wrapping and exit codes are unavailable. Use vendor CLI commands directly.]`, ); } @@ -219,6 +221,7 @@ let electronModule = null; let mainWebContentsId = null; let cliDiscoveryFilePath = null; let registeredContext = null; +let registeredVaultAgentBridge = null; // Active streaming requests (for cancellation) const activeStreams = new Map(); @@ -740,6 +743,23 @@ function registerHandlers(ipcMain) { Object.assign(context, createAgentCliHelpers(context)); registeredContext = context; + if (!registeredVaultAgentBridge) { + registeredVaultAgentBridge = createVaultAgentBridge({ + getMainWindowFn: () => { + try { + const windowManager = require("./windowManager.cjs"); + const mainWin = windowManager.getMainWindow?.(); + return (mainWin && !mainWin.isDestroyed()) ? mainWin : null; + } catch { + return null; + } + }, + validateSender, + }); + mcpServerBridge.setVaultAgentInvoker(registeredVaultAgentBridge.invokeVaultAgent); + } + registeredVaultAgentBridge.registerHandlers(ipcMain); + registerProviderHandlers(context); registerCattyExecHandlers(context); registerAgentDiscoveryHandlers(context); @@ -774,4 +794,10 @@ function cleanup() { mcpServerBridge.cleanup(); } -module.exports = { init, registerHandlers, cleanup }; +module.exports = { + init, + registerHandlers, + cleanup, + buildExternalAgentSystemContext, + buildExternalAgentContextualPrompt, +}; diff --git a/electron/bridges/aiBridge.externalContext.test.cjs b/electron/bridges/aiBridge.externalContext.test.cjs new file mode 100644 index 000000000..5925414a8 --- /dev/null +++ b/electron/bridges/aiBridge.externalContext.test.cjs @@ -0,0 +1,16 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { buildExternalAgentSystemContext } = require("./aiBridge.cjs"); + +test("buildExternalAgentSystemContext (MCP mode) includes vault host vs notes guidance", () => { + const context = buildExternalAgentSystemContext({ + mode: "mcp", + chatSessionId: "chat-1", + }); + assert.match(context, /vault_hosts_create/i); + assert.match(context, /NOT vault_notes_create/i); + assert.match(context, /do not silently create a Vault note/i); +}); diff --git a/electron/bridges/aiBridge.test.cjs b/electron/bridges/aiBridge.test.cjs index 06446d539..4d62550e9 100644 --- a/electron/bridges/aiBridge.test.cjs +++ b/electron/bridges/aiBridge.test.cjs @@ -21,6 +21,7 @@ function loadBridgeWithMocks(options = {}) { "./mcpServerBridge.cjs": { init() {}, setMainWindowGetter() {}, + setVaultAgentInvoker() {}, getOrCreateHost: async () => 4010, getScopedSessionIds: () => [], buildMcpServerConfig: () => ({ name: "netcatty-remote-hosts", type: "http", url: "http://127.0.0.1:4010" }), diff --git a/electron/bridges/aiBridge/agentProcessHandlers.cjs b/electron/bridges/aiBridge/agentProcessHandlers.cjs index 4ff47ec76..97feb98d4 100644 --- a/electron/bridges/aiBridge/agentProcessHandlers.cjs +++ b/electron/bridges/aiBridge/agentProcessHandlers.cjs @@ -69,6 +69,12 @@ function registerAgentProcessHandlers(ctx) { return { ok: true }; }); + ipcMain.handle("netcatty:ai:mcp:sync-permission-grants", async (event, { grants }) => { + if (!validateSenderOrSettings(event)) return { ok: false, error: "Unauthorized IPC sender" }; + mcpServerBridge.setPermissionGrants(grants); + return { ok: true, count: mcpServerBridge.getPermissionGrants().length }; + }); + // ── MCP Approval response (renderer → main) ── ipcMain.handle("netcatty:ai:mcp:approval-response", async (event, { approvalId, approved }) => { if (!validateSender(event)) return { ok: false, error: "Unauthorized IPC sender" }; diff --git a/electron/bridges/aiBridge/cattyExecHandlers.cjs b/electron/bridges/aiBridge/cattyExecHandlers.cjs index dc3ce2448..23d573e76 100644 --- a/electron/bridges/aiBridge/cattyExecHandlers.cjs +++ b/electron/bridges/aiBridge/cattyExecHandlers.cjs @@ -160,6 +160,33 @@ function registerCattyExecHandlers(ctx) { void mcpServerBridge.cancelSftpOpsForSession?.(chatSessionId); return { ok: true }; }); + + ipcMain.handle("netcatty:ai:chat-session:set-cancelled", async (event, { chatSessionId, cancelled }) => { + if (!validateSender(event)) { + return { ok: false, error: "Unauthorized IPC sender" }; + } + if (!chatSessionId || typeof chatSessionId !== "string") { + return { ok: false, error: "chatSessionId is required" }; + } + try { + return await mcpServerBridge.applyChatSessionCancelled(chatSessionId, cancelled !== false); + } catch (err) { + return { ok: false, error: err?.message || String(err) }; + } + }); + + ipcMain.handle("netcatty:ai:capability", async (event, { rpcMethod, params, chatSessionId }) => { + if (!validateSender(event)) { + return { ok: false, error: "Unauthorized IPC sender" }; + } + if (!rpcMethod || typeof rpcMethod !== "string") { + return { ok: false, error: "rpcMethod is required" }; + } + return mcpServerBridge.dispatchBuiltinRpc(rpcMethod, { + ...(params || {}), + chatSessionId, + }); + }); } } diff --git a/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs b/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs index 10633cd6d..c12f9bebf 100644 --- a/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs +++ b/electron/bridges/aiBridge/sdk/sdkStreamHandlers.cjs @@ -17,7 +17,18 @@ const VALID_BACKENDS = new Set(listBackends()); const MODEL_CACHE_TTL_MS = 5 * 60 * 1000; const MODEL_LIST_TIMEOUT_MS = 10000; const sdkModelCache = new Map(); -const SDK_SESSION_ID_PREFIX = "netcatty-sdk-session:"; +const { parseSdkSessionIdentity: parseSdkSessionIdentityPayload, SDK_SESSION_ID_PREFIX } = require("../../../shared/sdkSessionIdentity.cjs"); +const { isPathLikeCommand } = require("../../../shared/pathLikeCommand.cjs"); + +function parseSdkSessionIdentity(value) { + const parsed = parseSdkSessionIdentityPayload(value); + if (!parsed) return null; + return { + sessionId: parsed.id, + backendKey: parsed.backend, + binPath: parsed.binPath || "", + }; +} function buildSdkSessionKey(chatSessionId, backendKey, binPath) { return [ @@ -42,29 +53,6 @@ function normalizeSdkListModelsResult(raw) { return { currentModelId, models }; } -function isPathLikeCommand(command) { - const raw = String(command || "").trim(); - return Boolean(raw && (raw.includes("/") || raw.includes("\\") || /^[a-z]:/i.test(raw))); -} - -function parseSdkSessionIdentity(value) { - const raw = String(value || ""); - if (!raw.startsWith(SDK_SESSION_ID_PREFIX)) return null; - try { - const parsed = JSON.parse(decodeURIComponent(raw.slice(SDK_SESSION_ID_PREFIX.length))); - const sessionId = String(parsed?.id || "").trim(); - const backendKey = String(parsed?.backend || "").trim(); - if (!sessionId || !backendKey) return null; - return { - sessionId, - backendKey, - binPath: String(parsed?.binPath || ""), - }; - } catch { - return null; - } -} - function deleteSdkSessionKeysForChat(sdkSessionIds, chatSessionId) { const prefix = `${String(chatSessionId || "")}\u0000`; for (const key of sdkSessionIds.keys()) { diff --git a/electron/bridges/aiBridge/vaultAgentBridge.cjs b/electron/bridges/aiBridge/vaultAgentBridge.cjs new file mode 100644 index 000000000..90aedb64b --- /dev/null +++ b/electron/bridges/aiBridge/vaultAgentBridge.cjs @@ -0,0 +1,75 @@ +"use strict"; + +const crypto = require("node:crypto"); + +const VAULT_AGENT_TIMEOUT_MS = 15_000; +const pendingVaultRequests = new Map(); + +function createVaultAgentBridge({ getMainWindowFn, validateSender }) { + function registerHandlers(ipcMain) { + ipcMain.handle("netcatty:ai:vault-agent:response", (event, { requestId, result }) => { + if (!validateSender(event)) { + return { ok: false, error: "Unauthorized IPC sender" }; + } + if (!requestId || typeof requestId !== "string") { + return { ok: false, error: "requestId is required" }; + } + const entry = pendingVaultRequests.get(requestId); + if (!entry) { + return { ok: false, error: "Unknown or expired vault agent request." }; + } + clearTimeout(entry.timer); + pendingVaultRequests.delete(requestId); + entry.resolve(result); + return { ok: true }; + }); + } + + async function invokeVaultAgent(op, params = {}, options = {}) { + const mainWin = typeof getMainWindowFn === "function" ? getMainWindowFn() : null; + if (!mainWin || mainWin.isDestroyed()) { + return { + ok: false, + error: "No active Netcatty window is available for vault access.", + }; + } + + const requestId = crypto.randomUUID(); + return new Promise((resolve) => { + const timer = setTimeout(() => { + if (!pendingVaultRequests.has(requestId)) return; + pendingVaultRequests.delete(requestId); + resolve({ + ok: false, + error: "Vault agent bridge timed out waiting for renderer.", + }); + }, options.timeoutMs ?? VAULT_AGENT_TIMEOUT_MS); + + pendingVaultRequests.set(requestId, { resolve, timer }); + try { + mainWin.webContents.send("netcatty:ai:vault-agent:request", { + requestId, + op, + params, + }); + } catch (err) { + clearTimeout(timer); + pendingVaultRequests.delete(requestId); + resolve({ + ok: false, + error: err?.message || String(err), + }); + } + }); + } + + return { + registerHandlers, + invokeVaultAgent, + }; +} + +module.exports = { + createVaultAgentBridge, + VAULT_AGENT_TIMEOUT_MS, +}; diff --git a/electron/bridges/mcpServerBridge.cjs b/electron/bridges/mcpServerBridge.cjs index c9204b40b..2a66e94c3 100644 --- a/electron/bridges/mcpServerBridge.cjs +++ b/electron/bridges/mcpServerBridge.cjs @@ -13,10 +13,12 @@ const path = require("node:path"); const { existsSync } = require("node:fs"); const { toUnpackedAsarPath, getFreshIdlePrompt } = require("./ai/shellUtils.cjs"); +const { appendVaultAgentGuidance } = require("../shared/vaultAgentGuidance.cjs"); const { execViaPty, startPtyJob, execViaChannel, execViaRawPty } = require("./ai/ptyExec.cjs"); const { safeSend } = require("./ipcUtils.cjs"); const { getCliDiscoveryFilePath } = require("../cli/discoveryPath.cjs"); const sftpBridge = require("./sftpBridge.cjs"); +const portForwardingBridge = require("./portForwardingBridge.cjs"); const DEBUG_MCP = process.env.NETCATTY_MCP_DEBUG === "1"; @@ -55,6 +57,9 @@ let maxIterations = 20; // Permission mode: 'observer' | 'confirm' | 'autonomous' (synced from user settings) let permissionMode = "confirm"; +// Cached permission grants synced from renderer (confirm-mode memory table) +let permissionGrantsSnapshot = []; + // Track active PTY executions for cancellation const activePtyExecs = new Map(); // marker → { ptyStream, cleanup } const cancelledChatSessions = new Set(); @@ -89,7 +94,8 @@ function setMainWindowGetter(fn) { // after 120s"). Keep the Netcatty-side approval window below that with a small // buffer so a stale approval cannot still be accepted after the agent has // already timed out and abandoned the call. -const APPROVAL_TIMEOUT_MS = 110 * 1000; // 110 seconds +const { MCP_APPROVAL_TIMEOUT_MS } = require("../shared/approvalConstants.cjs"); +const APPROVAL_TIMEOUT_MS = MCP_APPROVAL_TIMEOUT_MS; function requestApprovalFromRenderer(toolName, args, chatSessionId) { return new Promise((resolve) => { @@ -424,6 +430,9 @@ function updateSessionMetadata(sessionList, chatSessionId) { shellType: s.shellType || "", deviceType: s.deviceType || "", connected: s.connected !== false, + hostId: s.hostId || "", + hostChain: Array.isArray(s.hostChain) ? s.hostChain : [], + activePortForwards: Array.isArray(s.activePortForwards) ? s.activePortForwards : [], }); } @@ -782,10 +791,41 @@ async function handleMessage(socket, line) { const { evaluateRpcPermission, + evaluatePermissionWithGrants, USER_DENIED_MESSAGE, } = require("../capabilities/policy.cjs"); const { CAPABILITY_SURFACES } = require("../capabilities/constants.cjs"); const { getCapabilityByRpcMethod } = require("../capabilities/registry.cjs"); +const { + createCapabilityRpcDispatcher, + UNROUTED, +} = require("./mcpServerBridge/capabilityRpcDispatch.cjs"); +const { buildBuiltinRpcHandlerRegistry } = require("./mcpServerBridge/builtinRpcHandlers.cjs"); + +let invokeVaultAgentFn = null; + +function setVaultAgentInvoker(fn) { + invokeVaultAgentFn = typeof fn === "function" ? fn : null; +} + +const dispatchCapabilityRpc = createCapabilityRpcDispatcher({ + invokeVaultAgent: (...args) => { + if (typeof invokeVaultAgentFn !== "function") { + return Promise.resolve({ ok: false, error: "Vault agent bridge is unavailable." }); + } + return invokeVaultAgentFn(...args); + }, + evaluatePermissionWithGrants, + get permissionMode() { + return permissionMode; + }, + get permissionGrantsSnapshot() { + return permissionGrantsSnapshot; + }, + isChatSessionCancelled, + requestApprovalFromRenderer, + USER_DENIED_MESSAGE, +}); /** * Validate that a sessionId is allowed in the current scope. @@ -813,15 +853,53 @@ function validateSessionScope(sessionId, chatSessionId, explicitScopedIds = null return null; } +let builtinRpcHandlerRegistry = null; + +function getBuiltinRpcHandlerRegistry() { + if (!builtinRpcHandlerRegistry) { + builtinRpcHandlerRegistry = buildBuiltinRpcHandlerRegistry({ + "session.environment": handleGetContext, + "meta.status": handleGetStatus, + "attachment.list": handleListAttachments, + "attachment.read": handleReadAttachment, + "terminal.execute": handleExec, + "sftp.list": handleSftpList, + "sftp.read": handleSftpRead, + "sftp.write": handleSftpWrite, + "sftp.download": handleSftpDownload, + "sftp.upload": handleSftpUpload, + "sftp.mkdir": handleSftpMkdir, + "sftp.delete": handleSftpDelete, + "sftp.rename": handleSftpRename, + "sftp.stat": handleSftpStat, + "sftp.chmod": handleSftpChmod, + "sftp.home": handleSftpHome, + "session.cancel": handleSetCancelled, + "terminal.start": handleJobStart, + "terminal.poll": handleJobPoll, + "terminal.stop": handleJobStop, + }); + } + return builtinRpcHandlerRegistry; +} + async function dispatch(method, params) { debugLog("dispatch", { method, params, permissionMode }); + + if (!method.startsWith("netcatty/")) { + const capabilityResult = await dispatchCapabilityRpc(method, params || {}); + if (capabilityResult !== UNROUTED) { + return capabilityResult; + } + } + const capability = getCapabilityByRpcMethod(method, CAPABILITY_SURFACES.BUILTIN); const sessionWriteLockId = (capability?.id === "terminal.execute" || capability?.id === "terminal.start") ? params?.sessionId : null; pruneCompletedBackgroundJobs(); - const permission = evaluateRpcPermission({ + const permission = evaluatePermissionWithGrants({ rpcMethod: method, surface: CAPABILITY_SURFACES.BUILTIN, permissionMode, @@ -829,7 +907,7 @@ async function dispatch(method, params) { context: { chatSessionCancelled: isChatSessionCancelled(params?.chatSessionId), }, - }); + }, permissionGrantsSnapshot); if (!permission.allowed) { return { ok: false, error: permission.error }; } @@ -870,50 +948,11 @@ async function dispatch(method, params) { return { ok: false, error: USER_DENIED_MESSAGE }; } } - switch (method) { - case "netcatty/getContext": - return handleGetContext(params); - case "netcatty/getStatus": - return handleGetStatus(); - case "netcatty/listAttachments": - return handleListAttachments(params); - case "netcatty/readAttachment": - return handleReadAttachment(params); - case "netcatty/exec": - return handleExec(params); - case "netcatty/sftp/list": - return handleSftpList(params); - case "netcatty/sftp/read": - return handleSftpRead(params); - case "netcatty/sftp/write": - return handleSftpWrite(params); - case "netcatty/sftp/download": - return handleSftpDownload(params); - case "netcatty/sftp/upload": - return handleSftpUpload(params); - case "netcatty/sftp/mkdir": - return handleSftpMkdir(params); - case "netcatty/sftp/delete": - return handleSftpDelete(params); - case "netcatty/sftp/rename": - return handleSftpRename(params); - case "netcatty/sftp/stat": - return handleSftpStat(params); - case "netcatty/sftp/chmod": - return handleSftpChmod(params); - case "netcatty/sftp/home": - return handleSftpHome(params); - case "netcatty/setCancelled": - return handleSetCancelled(params); - case "netcatty/jobStart": - return handleJobStart(params); - case "netcatty/jobPoll": - return handleJobPoll(params); - case "netcatty/jobStop": - return handleJobStop(params); - default: - throw new Error(`Unknown method: ${method}`); + const handler = getBuiltinRpcHandlerRegistry().get(method); + if (!handler) { + throw new Error(`Unknown method: ${method}`); } + return handler(params); } finally { if (sessionWriteLockId) { pendingSessionWriteApprovals.delete(sessionWriteLockId); @@ -923,7 +962,7 @@ async function dispatch(method, params) { // ── Handler: getContext ── -function handleGetContext(params) { +async function handleGetContext(params) { debugLog("handleGetContext:start", { params, sessionCount: sessions?.size || 0 }); if (!sessions) return { hosts: [], instructions: "No sessions available." }; @@ -971,19 +1010,33 @@ function handleGetContext(params) { shellType: meta.shellType || session.shellKind || "", deviceType: meta.deviceType || "", connected: meta.connected !== undefined ? meta.connected : !!(session.sshClient || session.conn || ptyStream || session.serialPort), + hostId: meta.hostId || "", + hostChain: meta.hostChain || [], + activePortForwards: meta.activePortForwards || [], }); } + let activePortForwardTunnels = []; + try { + activePortForwardTunnels = await portForwardingBridge.listPortForwards() || []; + } catch { + activePortForwardTunnels = []; + } + return { environment: "netcatty-terminal", - description: "You are operating inside Netcatty, a multi-session terminal manager. " + + description: appendVaultAgentGuidance( + "You are operating inside Netcatty, a multi-session terminal manager. " + "The available sessions may be remote hosts, local terminals, Mosh-backed shells, or serial port connections (network devices, embedded systems). " + "Use the provided tools to execute commands through the sessions exposed by Netcatty. " + "Serial sessions (protocol: serial, shellType: raw) do not run a standard shell — commands are sent as-is. " + "Network device sessions (deviceType: network) use vendor CLIs (Huawei VRP, Cisco IOS, etc.) — commands are sent as-is without shell wrapping, and exit codes are unavailable. " + + "Vault snippets, port forwarding rules/tunnels, and SFTP read/write tools are available when exposed in the tool list. " + "Always prefer these tools over suggesting the user to do things manually.", + ), hosts, hostCount: hosts.length, + activePortForwardTunnels, }; } @@ -1007,13 +1060,19 @@ function handleGetStatus() { }; } -async function handleSetCancelled(params) { - const chatSessionId = params?.chatSessionId; - const cancelled = params?.cancelled !== false; +function setPermissionGrants(grants) { + const { sanitizePermissionGrants } = require("../shared/permissionGrants.cjs"); + permissionGrantsSnapshot = sanitizePermissionGrants(grants); +} + +function getPermissionGrants() { + return permissionGrantsSnapshot; +} + +function applyChatSessionCancelled(chatSessionId, cancelled) { if (!chatSessionId || typeof chatSessionId !== "string") { throw new Error("chatSessionId is required"); } - if (cancelled) { setChatSessionCancelled(chatSessionId, true); cancelPtyExecsForSession(chatSessionId); @@ -1023,14 +1082,19 @@ async function handleSetCancelled(params) { } else { setChatSessionCancelled(chatSessionId, false); } - return { ok: true, chatSessionId, - cancelled, + cancelled: !!cancelled, }; } +async function handleSetCancelled(params) { + const chatSessionId = params?.chatSessionId; + const cancelled = params?.cancelled !== false; + return applyChatSessionCancelled(chatSessionId, cancelled); +} + const { createSftpHandlerApi } = require("./mcpServerBridge/sftpHandlers.cjs"); const sftpHandlerApi = createSftpHandlerApi({ get commandTimeoutMs() { return commandTimeoutMs; }, @@ -1096,7 +1160,10 @@ module.exports = { getMaxIterations, setPermissionMode, getPermissionMode, + setPermissionGrants, + getPermissionGrants, setChatSessionCancelled, + applyChatSessionCancelled, checkCommandSafety, updateSessionMetadata, updateAttachmentMetadata, @@ -1115,9 +1182,11 @@ module.exports = { cleanup, shutdownHost, setMainWindowGetter, + setVaultAgentInvoker, resolveApprovalFromRenderer, clearPendingApprovals, reserveSessionExecution, releaseSessionExecution, getSessionBusyError, + dispatchBuiltinRpc: dispatch, }; diff --git a/electron/bridges/mcpServerBridge/builtinRpcHandlers.cjs b/electron/bridges/mcpServerBridge/builtinRpcHandlers.cjs new file mode 100644 index 000000000..a3963862a --- /dev/null +++ b/electron/bridges/mcpServerBridge/builtinRpcHandlers.cjs @@ -0,0 +1,35 @@ +"use strict"; + +const { getCapabilityById } = require("../../capabilities/registry.cjs"); + +/** + * Build rpcMethod → handler map from capability-id-keyed handlers. + * Keeps dispatch aligned with catalog builtin RPC names. + * + * @param {Record Promise | unknown>} handlersByCapabilityId + * @returns {{ get: (rpcMethod: string) => ((params: object) => unknown) | null, has: (rpcMethod: string) => boolean }} + */ +function buildBuiltinRpcHandlerRegistry(handlersByCapabilityId) { + const byRpcMethod = new Map(); + + for (const [capabilityId, handler] of Object.entries(handlersByCapabilityId)) { + if (typeof handler !== "function") continue; + const capability = getCapabilityById(capabilityId); + const rpcMethod = capability?.surfaces?.builtin?.rpcMethod; + if (!rpcMethod) continue; + byRpcMethod.set(rpcMethod, handler); + } + + return { + get(rpcMethod) { + return byRpcMethod.get(rpcMethod) || null; + }, + has(rpcMethod) { + return byRpcMethod.has(rpcMethod); + }, + }; +} + +module.exports = { + buildBuiltinRpcHandlerRegistry, +}; diff --git a/electron/bridges/mcpServerBridge/builtinRpcHandlers.test.cjs b/electron/bridges/mcpServerBridge/builtinRpcHandlers.test.cjs new file mode 100644 index 000000000..2d0b5f944 --- /dev/null +++ b/electron/bridges/mcpServerBridge/builtinRpcHandlers.test.cjs @@ -0,0 +1,67 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { CAPABILITY_STATUS } = require("../../capabilities/constants.cjs"); +const { ALL_CAPABILITIES } = require("../../capabilities/catalog/index.cjs"); +const { buildBuiltinRpcHandlerRegistry } = require("./builtinRpcHandlers.cjs"); + +/** Capability ids wired in mcpServerBridge.getBuiltinRpcHandlerRegistry(). */ +const MCP_BRIDGE_BUILTIN_CAPABILITY_IDS = [ + "session.environment", + "meta.status", + "attachment.list", + "attachment.read", + "terminal.execute", + "sftp.list", + "sftp.read", + "sftp.write", + "sftp.download", + "sftp.upload", + "sftp.mkdir", + "sftp.delete", + "sftp.rename", + "sftp.stat", + "sftp.chmod", + "sftp.home", + "session.cancel", + "terminal.start", + "terminal.poll", + "terminal.stop", +]; + +test("buildBuiltinRpcHandlerRegistry maps catalog builtin rpcMethod to handlers", () => { + const handlersByCapabilityId = Object.fromEntries( + MCP_BRIDGE_BUILTIN_CAPABILITY_IDS.map((id) => [id, async () => ({ ok: true, id })]), + ); + const registry = buildBuiltinRpcHandlerRegistry(handlersByCapabilityId); + + for (const capabilityId of MCP_BRIDGE_BUILTIN_CAPABILITY_IDS) { + const capability = ALL_CAPABILITIES.find((entry) => entry.id === capabilityId); + assert.ok(capability, `missing catalog entry for ${capabilityId}`); + const rpcMethod = capability.surfaces?.builtin?.rpcMethod; + assert.ok(rpcMethod, `missing builtin rpcMethod for ${capabilityId}`); + assert.equal(typeof registry.get(rpcMethod), "function", rpcMethod); + } +}); + +test("every implemented netcatty/* builtin rpc has a bridge handler", () => { + const handlersByCapabilityId = Object.fromEntries( + MCP_BRIDGE_BUILTIN_CAPABILITY_IDS.map((id) => [id, async () => ({ ok: true })]), + ); + const registry = buildBuiltinRpcHandlerRegistry(handlersByCapabilityId); + + const implementedBuiltinRpcMethods = ALL_CAPABILITIES + .filter((capability) => capability.status === CAPABILITY_STATUS.IMPLEMENTED) + .map((capability) => capability.surfaces?.builtin?.rpcMethod) + .filter((rpcMethod) => typeof rpcMethod === "string" && rpcMethod.startsWith("netcatty/")); + + const uniqueRpcMethods = [...new Set(implementedBuiltinRpcMethods)]; + for (const rpcMethod of uniqueRpcMethods) { + assert.equal( + registry.has(rpcMethod), + true, + `no handler registered for ${rpcMethod}`, + ); + } +}); diff --git a/electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs new file mode 100644 index 000000000..365be4ceb --- /dev/null +++ b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.cjs @@ -0,0 +1,128 @@ +"use strict"; + +const { CAPABILITY_STATUS, CAPABILITY_SURFACES } = require("../../capabilities/constants.cjs"); +const { getCapabilityByRpcMethod } = require("../../capabilities/registry.cjs"); +const { getMcpToolNameForRpcMethod } = require("../../capabilities/adapters/mcpAdapter.cjs"); +const { createVaultService } = require("../../capabilities/services/vaultService.cjs"); +const { createPortForwardService } = require("../../capabilities/services/portforwardService.cjs"); + +const UNROUTED = Symbol("capability-rpc-unrouted"); + +const SERVICE_BINDINGS = Object.freeze({ + "vault.host.get": { domain: "vault", method: "getHost" }, + "vault.host.list": { domain: "vault", method: "listHosts" }, + "vault.hosts.create": { domain: "vault", method: "createHosts" }, + "vault.host.import": { domain: "vault", method: "importHosts" }, + "vault.host.notes.get": { domain: "vault", method: "getHostNotes" }, + "vault.host.notes.set": { domain: "vault", method: "setHostNotes" }, + "vault.note.list": { domain: "vault", method: "listNotes" }, + "vault.note.get": { domain: "vault", method: "getNote" }, + "vault.note.create": { domain: "vault", method: "createNote" }, + "vault.note.update": { domain: "vault", method: "updateNote" }, + "vault.snippets.list": { domain: "vault", method: "listSnippets" }, + "vault.snippets.get": { domain: "vault", method: "getSnippet" }, + "vault.snippets.run": { domain: "vault", method: "runSnippet" }, + "portforward.rules.list": { domain: "portforward", method: "listRules" }, + "portforward.tunnels.list": { domain: "portforward", method: "listTunnels" }, + "portforward.start": { domain: "portforward", method: "start" }, + "portforward.stop": { domain: "portforward", method: "stop" }, +}); + +function resolveCapabilitySurface(rpcMethod) { + for (const surface of [ + CAPABILITY_SURFACES.PUBLIC, + CAPABILITY_SURFACES.GLOBAL, + CAPABILITY_SURFACES.BUILTIN, + ]) { + if (rpcMethod.startsWith("netcatty/") && surface !== CAPABILITY_SURFACES.BUILTIN) { + continue; + } + const capability = getCapabilityByRpcMethod(rpcMethod, surface); + if (capability) { + return { capability, surface }; + } + } + return null; +} + +function createCapabilityRpcDispatcher(deps) { + const { + invokeVaultAgent, + evaluatePermissionWithGrants, + isChatSessionCancelled, + requestApprovalFromRenderer, + USER_DENIED_MESSAGE, + } = deps; + + const vaultService = createVaultService({ invokeVaultAgent }); + const portforwardService = createPortForwardService({ invokeVaultAgent }); + const services = { + vault: vaultService, + portforward: portforwardService, + }; + + return async function dispatchCapabilityRpc(rpcMethod, params = {}) { + if (typeof rpcMethod !== "string" || rpcMethod.startsWith("netcatty/")) { + return UNROUTED; + } + + const resolved = resolveCapabilitySurface(rpcMethod); + if (!resolved) { + return UNROUTED; + } + + const { capability, surface } = resolved; + if (capability.status !== CAPABILITY_STATUS.IMPLEMENTED) { + return { + ok: false, + code: "CAPABILITY_NOT_IMPLEMENTED", + error: `Capability "${capability.id}" is not implemented yet.`, + }; + } + + const binding = SERVICE_BINDINGS[capability.id]; + if (!binding) { + return UNROUTED; + } + + const permission = evaluatePermissionWithGrants({ + rpcMethod, + surface, + permissionMode: deps.permissionMode, + params, + context: { + chatSessionCancelled: isChatSessionCancelled(params?.chatSessionId), + }, + }, deps.permissionGrantsSnapshot); + + if (!permission.allowed) { + return { ok: false, error: permission.error }; + } + + if (permission.requiresApproval) { + const { chatSessionId, ...toolArgs } = params || {}; + const toolName = getMcpToolNameForRpcMethod(rpcMethod, surface) || capability.id; + const approved = await requestApprovalFromRenderer(toolName, toolArgs, chatSessionId); + if (!approved) { + return { ok: false, error: USER_DENIED_MESSAGE }; + } + } + + const service = services[binding.domain]; + const handler = service?.[binding.method]; + if (typeof handler !== "function") { + return { + ok: false, + error: `Capability handler "${capability.id}" is unavailable.`, + }; + } + + return handler(params); + }; +} + +module.exports = { + UNROUTED, + createCapabilityRpcDispatcher, + SERVICE_BINDINGS, +}; diff --git a/electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs new file mode 100644 index 000000000..9d0e809ac --- /dev/null +++ b/electron/bridges/mcpServerBridge/capabilityRpcDispatch.test.cjs @@ -0,0 +1,188 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { createCapabilityRpcDispatcher, UNROUTED } = require("./capabilityRpcDispatch.cjs"); +const { CAPABILITY_SURFACES, PERMISSION_MODES } = require("../../capabilities/constants.cjs"); + +function createTestDispatcher(overrides = {}) { + const invokeVaultAgent = overrides.invokeVaultAgent || (async (op, params) => ({ + ok: true, + op, + params, + })); + const requestApprovalFromRenderer = overrides.requestApprovalFromRenderer + || (async () => true); + + return createCapabilityRpcDispatcher({ + invokeVaultAgent, + evaluatePermissionWithGrants: overrides.evaluatePermissionWithGrants || ((input, grants) => ({ + allowed: true, + requiresApproval: false, + grants, + ...input, + })), + permissionMode: overrides.permissionMode || PERMISSION_MODES.CONFIRM, + permissionGrantsSnapshot: [], + isChatSessionCancelled: () => false, + requestApprovalFromRenderer, + USER_DENIED_MESSAGE: "User denied the operation.", + ...overrides, + }); +} + +test("dispatchCapabilityRpc returns UNROUTED for netcatty builtin methods", async () => { + const dispatch = createTestDispatcher(); + const result = await dispatch("netcatty/exec", { chatSessionId: "chat-1" }); + assert.equal(result, UNROUTED); +}); + +test("dispatchCapabilityRpc routes vault host notes get to vault service", async () => { + let invokedOp = null; + const dispatch = createTestDispatcher({ + invokeVaultAgent: async (op, params) => { + invokedOp = op; + return { ok: true, hostId: params.hostId, notes: "notes" }; + }, + }); + + const result = await dispatch("vault/host/notes/get", { hostId: "host-1" }); + assert.equal(invokedOp, "host.notes.get"); + assert.equal(result.ok, true); + assert.equal(result.notes, "notes"); +}); + +test("dispatchCapabilityRpc routes public vault host notes set through approval", async () => { + const approvalCalls = []; + const dispatch = createTestDispatcher({ + evaluatePermissionWithGrants: () => ({ + allowed: true, + requiresApproval: true, + }), + requestApprovalFromRenderer: async (toolName, args, chatSessionId) => { + approvalCalls.push({ toolName, args, chatSessionId }); + return true; + }, + invokeVaultAgent: async (op, params) => ({ + ok: true, + op, + hostId: params.hostId, + notes: params.notes, + }), + }); + + const result = await dispatch("public/vault/hostNotes/set", { + chatSessionId: "chat-1", + hostId: "host-1", + notes: "updated", + }); + + assert.equal(approvalCalls.length, 1); + assert.equal(approvalCalls[0].toolName, "host_notes_set"); + assert.equal(result.ok, true); + assert.equal(result.notes, "updated"); +}); + +test("dispatchCapabilityRpc denies public vault host notes set when approval rejected", async () => { + const dispatch = createTestDispatcher({ + evaluatePermissionWithGrants: () => ({ + allowed: true, + requiresApproval: true, + }), + requestApprovalFromRenderer: async () => false, + }); + + const result = await dispatch("public/vault/hostNotes/set", { + chatSessionId: "chat-1", + hostId: "host-1", + notes: "updated", + }); + + assert.equal(result.ok, false); + assert.match(result.error, /denied/i); +}); + +test("dispatchCapabilityRpc routes vault hosts create to vault service", async () => { + let invokedOp = null; + const dispatch = createTestDispatcher({ + invokeVaultAgent: async (op, params) => { + invokedOp = op; + return { ok: true, addedCount: 1, previewHosts: [] , params }; + }, + }); + + const result = await dispatch("vault/hosts/create", { + hosts: JSON.stringify([{ hostname: "10.2.0.209", username: "root" }]), + dryRun: "true", + }); + assert.equal(invokedOp, "hosts.create"); + assert.equal(result.ok, true); +}); + +test("dispatchCapabilityRpc routes vault hosts import to vault service", async () => { + let invokedOp = null; + const dispatch = createTestDispatcher({ + invokeVaultAgent: async (op) => { + invokedOp = op; + return { ok: true, addedCount: 0 }; + }, + }); + + const result = await dispatch("vault/hosts/import", { + format: "csv", + text: "hostname,username\n10.0.0.1,root\n", + dryRun: "true", + }); + assert.equal(invokedOp, "host.import"); + assert.equal(result.ok, true); +}); + +test("dispatchCapabilityRpc routes portforward start to portforward service", async () => { + let invokedOp = null; + const dispatch = createTestDispatcher({ + invokeVaultAgent: async (op, params) => { + invokedOp = op; + return { ok: true, ruleId: params.ruleId, status: "active" }; + }, + }); + + const result = await dispatch("portforward/start", { + chatSessionId: "chat-1", + ruleId: "rule-1", + }); + assert.equal(invokedOp, "portforward.start"); + assert.equal(result.ok, true); + assert.equal(result.ruleId, "rule-1"); +}); + +test("dispatchCapabilityRpc reads permissionMode from deps on each call", async () => { + const seenModes = []; + const mutableDeps = { permissionMode: PERMISSION_MODES.CONFIRM }; + const liveDispatch = createCapabilityRpcDispatcher({ + invokeVaultAgent: async () => ({ ok: true }), + evaluatePermissionWithGrants: (input) => { + seenModes.push(input.permissionMode); + return { allowed: true, requiresApproval: false }; + }, + get permissionMode() { + return mutableDeps.permissionMode; + }, + permissionGrantsSnapshot: [], + isChatSessionCancelled: () => false, + requestApprovalFromRenderer: async () => true, + USER_DENIED_MESSAGE: "User denied the operation.", + }); + + await liveDispatch("vault/host/get", { hostId: "host-1" }); + mutableDeps.permissionMode = PERMISSION_MODES.AUTONOMOUS; + await liveDispatch("vault/host/get", { hostId: "host-2" }); + + assert.deepEqual(seenModes, [PERMISSION_MODES.CONFIRM, PERMISSION_MODES.AUTONOMOUS]); +}); + +test("implemented vault capabilities do not return CAPABILITY_NOT_IMPLEMENTED", async () => { + const dispatch = createTestDispatcher(); + const result = await dispatch("vault/host/get", { hostId: "host-1" }); + assert.notEqual(result.code, "CAPABILITY_NOT_IMPLEMENTED"); +}); diff --git a/electron/capabilities/adapters/cliAdapter.cjs b/electron/capabilities/adapters/cliAdapter.cjs index 31dba5f88..31a70c874 100644 --- a/electron/capabilities/adapters/cliAdapter.cjs +++ b/electron/capabilities/adapters/cliAdapter.cjs @@ -5,11 +5,39 @@ const { getCapabilityByCliCommand, listCapabilities, } = require("../registry.cjs"); +const { TOOL_INPUT_FIELDS } = require("../schemas/toolInputs.cjs"); + +/** Maps TOOL_INPUT_FIELDS keys to CLI flag names and opts property names. */ +const CLI_FIELD_BINDINGS = Object.freeze({ + hostId: { flag: "--host-id", optKey: "hostId" }, + snippetId: { flag: "--snippet-id", optKey: "snippetId" }, + ruleId: { flag: "--rule-id", optKey: "ruleId" }, + notes: { flag: "--notes", optKey: "notes" }, + sessionId: { flag: "--session", optKey: "sessionId" }, + variables: { flag: "--variables", optKey: "variables" }, + path: { flag: "--remote-path", optKey: "remotePath" }, + remotePath: { flag: "--remote-path", optKey: "remotePath" }, + localPath: { flag: "--local-path", optKey: "localPath" }, + oldPath: { flag: "--old-remote-path", optKey: "oldRemotePath" }, + newPath: { flag: "--new-remote-path", optKey: "newRemotePath" }, + content: { flag: "--content", optKey: "content" }, + mode: { flag: "--mode", optKey: "mode" }, + command: { flag: "--", optKey: "command" }, + jobId: { flag: "--job", optKey: "jobId" }, + offset: { flag: "--offset", optKey: "offset" }, +}); + +function resolveCliRpcMethod(capability) { + if (!capability) return null; + return capability.surfaces?.[CAPABILITY_SURFACES.BUILTIN]?.rpcMethod + || capability.surfaces?.[CAPABILITY_SURFACES.GLOBAL]?.rpcMethod + || capability.surfaces?.[CAPABILITY_SURFACES.PUBLIC]?.rpcMethod + || null; +} function getCliRpcMethod(commandParts) { const capability = getCapabilityByCliCommand(commandParts); - if (!capability) return null; - return capability.surfaces?.[CAPABILITY_SURFACES.BUILTIN]?.rpcMethod || null; + return resolveCliRpcMethod(capability); } function listCliCapabilities(options = {}) { @@ -25,7 +53,7 @@ function listCliCapabilities(options = {}) { status: capability.status, description: capability.description, command: capability.surfaces[surface].command, - rpcMethod: capability.surfaces?.[CAPABILITY_SURFACES.BUILTIN]?.rpcMethod || null, + rpcMethod: resolveCliRpcMethod(capability), policy: capability.policy, })); } @@ -37,8 +65,53 @@ function formatCliHelpLines(options = {}) { }); } +function buildCatalogCliParams(capabilityId, opts, createError) { + const fields = TOOL_INPUT_FIELDS[capabilityId]; + if (!fields) { + return {}; + } + + const params = {}; + for (const [fieldName, fieldDef] of Object.entries(fields)) { + const binding = CLI_FIELD_BINDINGS[fieldName]; + if (!binding) continue; + + let value = opts[binding.optKey]; + if (fieldName === "command" && Array.isArray(value)) { + value = value.length === 1 ? value[0] : null; + } + if (fieldName === "variables" && typeof value === "string" && value.trim()) { + try { + value = JSON.parse(value); + } catch { + throw createError("INVALID_ARGUMENT", `--variables must be valid JSON for ${capabilityId}.`); + } + } + if (fieldName === "offset" && value != null) { + value = Number(value); + } + + if (value == null || value === "") { + if (!fieldDef.optional) { + throw createError( + "INVALID_ARGUMENT", + `Missing required ${binding.flag} for ${capabilityId}.`, + ); + } + continue; + } + + params[fieldName] = value; + } + + return params; +} + module.exports = { + CLI_FIELD_BINDINGS, + buildCatalogCliParams, getCliRpcMethod, listCliCapabilities, formatCliHelpLines, + resolveCliRpcMethod, }; diff --git a/electron/capabilities/adapters/cliAdapter.test.cjs b/electron/capabilities/adapters/cliAdapter.test.cjs index a0ba57dfb..2afa27685 100644 --- a/electron/capabilities/adapters/cliAdapter.test.cjs +++ b/electron/capabilities/adapters/cliAdapter.test.cjs @@ -3,22 +3,59 @@ const test = require("node:test"); const assert = require("node:assert/strict"); -const { getCliRpcMethod, listCliCapabilities } = require("./cliAdapter.cjs"); +const { + getCliRpcMethod, + listCliCapabilities, + buildCatalogCliParams, +} = require("./cliAdapter.cjs"); const { CAPABILITY_STATUS } = require("../constants.cjs"); -test("getCliRpcMethod resolves implemented cli commands to builtin rpc methods", () => { +function fakeCreateError(code, message) { + const err = new Error(message); + err.code = code; + return err; +} + +test("getCliRpcMethod resolves implemented cli commands to rpc methods", () => { assert.equal(getCliRpcMethod(["exec"]), "netcatty/exec"); assert.equal(getCliRpcMethod(["sftp", "list"]), "netcatty/sftp/list"); + assert.equal(getCliRpcMethod(["vault", "host", "get"]), "vault/host/get"); + assert.equal(getCliRpcMethod(["portforward", "rules", "list"]), "portforward/rules/list"); assert.equal(getCliRpcMethod(["capabilities"]), null); }); test("listCliCapabilities returns implemented commands by default", () => { const entries = listCliCapabilities(); assert.ok(entries.some((entry) => entry.id === "terminal.execute")); + assert.ok(entries.some((entry) => entry.id === "vault.host.get")); assert.ok(entries.every((entry) => entry.status === CAPABILITY_STATUS.IMPLEMENTED)); + assert.ok(entries.every((entry) => entry.rpcMethod)); }); test("listCliCapabilities can include planned commands", () => { const entries = listCliCapabilities({ status: CAPABILITY_STATUS.PLANNED }); - assert.ok(entries.some((entry) => entry.id === "vault.host.notes.get")); + assert.ok(entries.length >= 0); +}); + +test("buildCatalogCliParams maps vault host get flags", () => { + const params = buildCatalogCliParams("vault.host.get", { hostId: "host-1" }, fakeCreateError); + assert.deepEqual(params, { hostId: "host-1" }); +}); + +test("buildCatalogCliParams parses snippet variables JSON", () => { + const params = buildCatalogCliParams("vault.snippets.run", { + snippetId: "snip-1", + sessionId: "sess-1", + variables: "{\"name\":\"prod\"}", + }, fakeCreateError); + assert.equal(params.snippetId, "snip-1"); + assert.equal(params.sessionId, "sess-1"); + assert.deepEqual(params.variables, { name: "prod" }); +}); + +test("buildCatalogCliParams throws for missing required fields", () => { + assert.throws( + () => buildCatalogCliParams("vault.host.get", {}, fakeCreateError), + /Missing required --host-id/, + ); }); diff --git a/electron/capabilities/catalog/harness.cjs b/electron/capabilities/catalog/harness.cjs new file mode 100644 index 000000000..a0dcb803d --- /dev/null +++ b/electron/capabilities/catalog/harness.cjs @@ -0,0 +1,100 @@ +"use strict"; + +const { CAPABILITY_STATUS } = require("../constants.cjs"); + +/** Catty-only harness tools (sidebar agent; renderer-local; not MCP/CLI). */ +/** @type {import("../types.cjs").CapabilityDefinition[]} */ +const HARNESS_CAPABILITIES = [ + { + id: "harness.tool_output.read", + domain: "harness", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Read stored tool output by handle id when a prior tool result was truncated.", + policy: { + write: false, + sensitiveRead: false, + longRunning: false, + requiresChatSession: true, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + catty: { toolName: "tool_output_read" }, + }, + }, + { + id: "harness.workspace.get_info", + domain: "harness", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Get information about the current workspace, including all terminal sessions and their connection status.", + policy: { + write: false, + sensitiveRead: false, + longRunning: false, + requiresChatSession: true, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + catty: { toolName: "workspace_get_info" }, + }, + }, + { + id: "harness.workspace.get_session_info", + domain: "harness", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Get detailed information about a specific terminal or SFTP session.", + policy: { + write: false, + sensitiveRead: false, + longRunning: false, + requiresChatSession: true, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + catty: { toolName: "workspace_get_session_info" }, + }, + }, + { + id: "harness.web.search", + domain: "harness", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Search the web for current information when configured in AI settings.", + policy: { + write: false, + sensitiveRead: false, + longRunning: false, + requiresChatSession: true, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + catty: { toolName: "web_search" }, + }, + }, + { + id: "harness.url.fetch", + domain: "harness", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Fetch and read the content of an HTTPS URL.", + policy: { + write: false, + sensitiveRead: false, + longRunning: false, + requiresChatSession: true, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + catty: { toolName: "url_fetch" }, + }, + }, +]; + +module.exports = { HARNESS_CAPABILITIES }; diff --git a/electron/capabilities/catalog/index.cjs b/electron/capabilities/catalog/index.cjs index c353c96ac..984649ca6 100644 --- a/electron/capabilities/catalog/index.cjs +++ b/electron/capabilities/catalog/index.cjs @@ -5,6 +5,7 @@ const { TERMINAL_CAPABILITIES } = require("./terminal.cjs"); const { SFTP_CAPABILITIES } = require("./sftp.cjs"); const { VAULT_CAPABILITIES } = require("./vault.cjs"); const { PORT_FORWARD_CAPABILITIES } = require("./portforward.cjs"); +const { HARNESS_CAPABILITIES } = require("./harness.cjs"); const ALL_CAPABILITIES = Object.freeze([ ...META_CAPABILITIES, @@ -12,6 +13,7 @@ const ALL_CAPABILITIES = Object.freeze([ ...SFTP_CAPABILITIES, ...VAULT_CAPABILITIES, ...PORT_FORWARD_CAPABILITIES, + ...HARNESS_CAPABILITIES, ]); module.exports = { @@ -20,5 +22,6 @@ module.exports = { SFTP_CAPABILITIES, VAULT_CAPABILITIES, PORT_FORWARD_CAPABILITIES, + HARNESS_CAPABILITIES, ALL_CAPABILITIES, }; diff --git a/electron/capabilities/catalog/integrity.test.cjs b/electron/capabilities/catalog/integrity.test.cjs index 8f7899412..ebfcde465 100644 --- a/electron/capabilities/catalog/integrity.test.cjs +++ b/electron/capabilities/catalog/integrity.test.cjs @@ -28,9 +28,19 @@ const IMPLEMENTED_CLI_COMMANDS = [ ["sftp", "home"], ["cancel"], ["resume"], + ["vault", "host", "get"], + ["vault", "host-notes", "get"], + ["vault", "host-notes", "set"], + ["snippets", "list"], + ["snippets", "get"], + ["snippets", "run"], + ["portforward", "rules", "list"], + ["portforward", "tunnels", "list"], + ["portforward", "start"], + ["portforward", "stop"], ]; -test("every implemented cli command maps to a builtin rpc method", () => { +test("every implemented cli command maps to an rpc method", () => { for (const command of IMPLEMENTED_CLI_COMMANDS) { const rpcMethod = getCliRpcMethod(command); assert.ok(rpcMethod, `missing rpc mapping for ${command.join(" ")}`); diff --git a/electron/capabilities/catalog/portforward.cjs b/electron/capabilities/catalog/portforward.cjs index a9375a3b3..502e20955 100644 --- a/electron/capabilities/catalog/portforward.cjs +++ b/electron/capabilities/catalog/portforward.cjs @@ -7,7 +7,7 @@ const PORT_FORWARD_CAPABILITIES = [ { id: "portforward.rules.list", domain: "portforward", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "List persisted port forwarding rules.", policy: { write: false, @@ -27,7 +27,7 @@ const PORT_FORWARD_CAPABILITIES = [ { id: "portforward.tunnels.list", domain: "portforward", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "List active port forwarding tunnels.", policy: { write: false, @@ -47,7 +47,7 @@ const PORT_FORWARD_CAPABILITIES = [ { id: "portforward.start", domain: "portforward", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "Start a port forwarding tunnel for a rule.", policy: { write: true, @@ -67,7 +67,7 @@ const PORT_FORWARD_CAPABILITIES = [ { id: "portforward.stop", domain: "portforward", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "Stop an active port forwarding tunnel.", policy: { write: true, diff --git a/electron/capabilities/catalog/sftp.cjs b/electron/capabilities/catalog/sftp.cjs index 200522c09..2faa2caae 100644 --- a/electron/capabilities/catalog/sftp.cjs +++ b/electron/capabilities/catalog/sftp.cjs @@ -60,6 +60,7 @@ const SFTP_CAPABILITIES = [ { write: true, bypassesApproval: false, bypassesChatCancel: false }, { builtin: { rpcMethod: "netcatty/sftp/download" }, + public: { rpcMethod: "public/sftp/download", mcpTool: "sftp_download" }, cli: { command: ["sftp", "download"] }, }, ), @@ -69,6 +70,7 @@ const SFTP_CAPABILITIES = [ { write: true, bypassesApproval: false, bypassesChatCancel: false }, { builtin: { rpcMethod: "netcatty/sftp/upload" }, + public: { rpcMethod: "public/sftp/upload", mcpTool: "sftp_upload" }, cli: { command: ["sftp", "upload"] }, }, ), diff --git a/electron/capabilities/catalog/vault.cjs b/electron/capabilities/catalog/vault.cjs index f030c9114..b17b261b5 100644 --- a/electron/capabilities/catalog/vault.cjs +++ b/electron/capabilities/catalog/vault.cjs @@ -7,7 +7,7 @@ const VAULT_CAPABILITIES = [ { id: "vault.host.get", domain: "vault", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "Get host metadata from the vault.", policy: { write: false, @@ -21,13 +21,71 @@ const VAULT_CAPABILITIES = [ surfaces: { cli: { command: ["vault", "host", "get"] }, global: { rpcMethod: "vault/host/get" }, + public: { rpcMethod: "public/vault/host/get", mcpTool: "host_get" }, + }, + }, + { + id: "vault.host.list", + domain: "vault", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "List saved hosts in the vault (metadata only — no passwords or keys).", + policy: { + write: false, + sensitiveRead: true, + longRunning: false, + requiresChatSession: false, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + global: { rpcMethod: "vault/hosts/list" }, + public: { rpcMethod: "public/vault/hosts/list", mcpTool: "vault_hosts_list" }, + }, + }, + { + id: "vault.hosts.create", + domain: "vault", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Create vault hosts from structured host objects. Use when the user wants to add/create a host (Vault → Hosts). NOT for Vault → Notes sidebar documentation.", + policy: { + write: true, + sensitiveRead: false, + longRunning: false, + requiresChatSession: false, + bypassesObserverBlock: false, + bypassesApproval: false, + bypassesChatCancel: false, + }, + surfaces: { + global: { rpcMethod: "vault/hosts/create" }, + public: { rpcMethod: "public/vault/hosts/create", mcpTool: "vault_hosts_create" }, + }, + }, + { + id: "vault.host.import", + domain: "vault", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Parse known host export file formats (PuTTY, MobaXterm, CSV, SecureCRT, ssh_config) into vault hosts. For arbitrary unstructured text, map to host objects and use vault_hosts_create instead.", + policy: { + write: true, + sensitiveRead: false, + longRunning: false, + requiresChatSession: false, + bypassesObserverBlock: false, + bypassesApproval: false, + bypassesChatCancel: false, + }, + surfaces: { + global: { rpcMethod: "vault/hosts/import" }, + public: { rpcMethod: "public/vault/hosts/import", mcpTool: "vault_hosts_import" }, }, }, { id: "vault.host.notes.get", domain: "vault", - status: CAPABILITY_STATUS.PLANNED, - description: "Read host notes from the vault.", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Read host metadata notes attached to a saved host (Host Details panel — not Vault sidebar Notes).", policy: { write: false, sensitiveRead: true, @@ -46,8 +104,8 @@ const VAULT_CAPABILITIES = [ { id: "vault.host.notes.set", domain: "vault", - status: CAPABILITY_STATUS.PLANNED, - description: "Update host notes in the vault.", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Update host metadata notes on a saved host (Host Details panel — not Vault sidebar Notes).", policy: { write: true, sensitiveRead: false, @@ -60,12 +118,89 @@ const VAULT_CAPABILITIES = [ surfaces: { cli: { command: ["vault", "host-notes", "set"] }, global: { rpcMethod: "vault/host/notes/set" }, + public: { rpcMethod: "public/vault/hostNotes/set", mcpTool: "host_notes_set" }, + }, + }, + { + id: "vault.note.list", + domain: "vault", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "List notes in Vault → Notes (markdown notes visible in the vault sidebar).", + policy: { + write: false, + sensitiveRead: false, + longRunning: false, + requiresChatSession: false, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + global: { rpcMethod: "vault/notes/list" }, + public: { rpcMethod: "public/vault/notes/list", mcpTool: "vault_notes_list" }, + }, + }, + { + id: "vault.note.get", + domain: "vault", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Get a Vault → Notes entry by id (full markdown content).", + policy: { + write: false, + sensitiveRead: false, + longRunning: false, + requiresChatSession: false, + bypassesObserverBlock: false, + bypassesApproval: true, + bypassesChatCancel: true, + }, + surfaces: { + global: { rpcMethod: "vault/notes/get" }, + public: { rpcMethod: "public/vault/notes/get", mcpTool: "vault_notes_get" }, + }, + }, + { + id: "vault.note.create", + domain: "vault", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Create a note in Vault → Notes sidebar (markdown documentation). NOT for adding SSH hosts — use vault_hosts_create for that.", + policy: { + write: true, + sensitiveRead: false, + longRunning: false, + requiresChatSession: false, + bypassesObserverBlock: false, + bypassesApproval: false, + bypassesChatCancel: false, + }, + surfaces: { + global: { rpcMethod: "vault/notes/create" }, + public: { rpcMethod: "public/vault/notes/create", mcpTool: "vault_notes_create" }, + }, + }, + { + id: "vault.note.update", + domain: "vault", + status: CAPABILITY_STATUS.IMPLEMENTED, + description: "Update an existing Vault → Notes entry by id.", + policy: { + write: true, + sensitiveRead: false, + longRunning: false, + requiresChatSession: false, + bypassesObserverBlock: false, + bypassesApproval: false, + bypassesChatCancel: false, + }, + surfaces: { + global: { rpcMethod: "vault/notes/update" }, + public: { rpcMethod: "public/vault/notes/update", mcpTool: "vault_notes_update" }, }, }, { id: "vault.snippets.list", domain: "vault", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "List code snippets stored in the vault.", policy: { write: false, @@ -85,7 +220,7 @@ const VAULT_CAPABILITIES = [ { id: "vault.snippets.get", domain: "vault", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "Get a single code snippet from the vault.", policy: { write: false, @@ -105,7 +240,7 @@ const VAULT_CAPABILITIES = [ { id: "vault.snippets.run", domain: "vault", - status: CAPABILITY_STATUS.PLANNED, + status: CAPABILITY_STATUS.IMPLEMENTED, description: "Resolve snippet variables and execute a snippet in a terminal session.", policy: { write: true, diff --git a/electron/capabilities/codegen/mcpToolRegistry.cjs b/electron/capabilities/codegen/mcpToolRegistry.cjs new file mode 100644 index 000000000..4c3caad10 --- /dev/null +++ b/electron/capabilities/codegen/mcpToolRegistry.cjs @@ -0,0 +1,184 @@ +"use strict"; + +const { z } = require("zod"); +const { listMcpTools } = require("./toolSurfaces.cjs"); + +const TERMINAL_EXECUTE_TOOLS = new Set(["terminal_execute"]); +const TERMINAL_START_TOOLS = new Set(["terminal_start"]); +const TERMINAL_POLL_TOOLS = new Set(["terminal_poll"]); +const TERMINAL_STOP_TOOLS = new Set(["terminal_stop"]); +const CONTEXT_TOOLS = new Set(["get_environment"]); +const ATTACHMENT_LIST_TOOLS = new Set(["list_attachments"]); +const ATTACHMENT_READ_TOOLS = new Set(["read_attachment"]); +const SFTP_WRITE_TOOLS = new Set([ + "sftp_write_file", + "sftp_mkdir", + "sftp_delete", + "sftp_rename", + "sftp_chmod", +]); + +function buildZodField(field) { + let schema = field.type === "number" ? z.number() : z.string(); + if (field.type === "number") { + schema = schema.int().min(0); + } + if (field.description) { + schema = schema.describe(field.description); + } + if (field.optional) { + schema = schema.optional(); + } + return schema; +} + +function buildZodShapeObject(inputShape) { + const shape = {}; + for (const [key, field] of Object.entries(inputShape || {})) { + shape[key] = buildZodField(field); + } + return shape; +} + +function buildZodSchema(inputShape) { + return z.object(buildZodShapeObject(inputShape)); +} + +function formatRpcError(result) { + return `Error: ${result?.error || "Operation failed"}`; +} + +function formatTerminalExecuteResult(result) { + const parts = []; + if (result.stdout) parts.push(result.stdout); + if (result.stderr) parts.push(`[stderr] ${result.stderr}`); + if (result.exitCode != null) { + parts.push(`[exit code: ${result.exitCode}]`); + } + return parts.join("\n"); +} + +function createToolHandler(toolDef, deps) { + const { + rpcCall, + scopeParams, + guardWriteOperation, + catalogDescription, + } = deps; + const { mcpTool, rpcMethod, description, inputShape, policy } = toolDef; + + return async (args) => { + if (TERMINAL_EXECUTE_TOOLS.has(mcpTool) || TERMINAL_START_TOOLS.has(mcpTool)) { + const { sessionId, command } = args; + const guardErr = guardWriteOperation(command, { skipBlocklist: true }); + if (guardErr) { + return { content: [{ type: "text", text: `Error: ${guardErr}` }], isError: true }; + } + const result = await rpcCall(rpcMethod, { ...scopeParams, sessionId, command }); + if (!result.ok) { + return { content: [{ type: "text", text: formatRpcError(result) }], isError: true }; + } + if (TERMINAL_START_TOOLS.has(mcpTool)) { + return { + content: [{ + type: "text", + text: JSON.stringify({ + jobId: result.jobId, + sessionId: result.sessionId, + status: result.status, + startedAt: result.startedAt, + outputMode: result.outputMode, + recommendedPollIntervalMs: result.recommendedPollIntervalMs, + }, null, 2), + }], + }; + } + return { content: [{ type: "text", text: formatTerminalExecuteResult(result) }] }; + } + + if (TERMINAL_POLL_TOOLS.has(mcpTool) || TERMINAL_STOP_TOOLS.has(mcpTool)) { + const params = TERMINAL_POLL_TOOLS.has(mcpTool) + ? { ...scopeParams, jobId: args.jobId, offset: args.offset || 0 } + : { ...scopeParams, jobId: args.jobId }; + const result = await rpcCall(rpcMethod, params); + if (!result.ok) { + return { content: [{ type: "text", text: formatRpcError(result) }], isError: true }; + } + return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; + } + + if (CONTEXT_TOOLS.has(mcpTool)) { + const ctx = await rpcCall(rpcMethod, scopeParams); + return { content: [{ type: "text", text: JSON.stringify(ctx, null, 2) }] }; + } + + if (ATTACHMENT_LIST_TOOLS.has(mcpTool)) { + const result = await rpcCall(rpcMethod, scopeParams); + if (!result.ok) { + return { + content: [{ type: "text", text: formatRpcError(result) }], + isError: true, + }; + } + return { + content: [{ type: "text", text: JSON.stringify(result.attachments || [], null, 2) }], + }; + } + + if (ATTACHMENT_READ_TOOLS.has(mcpTool)) { + const { filePath, filename } = args; + const result = await rpcCall(rpcMethod, { ...scopeParams, filePath, filename }); + if (!result.ok) { + return { + content: [{ type: "text", text: formatRpcError(result) }], + isError: true, + }; + } + const payload = { + filename: result.filename, + mediaType: result.mediaType, + filePath: result.filePath, + sizeBytes: result.sizeBytes, + ...(result.text != null ? { text: result.text } : { base64Data: result.base64Data }), + }; + return { content: [{ type: "text", text: JSON.stringify(payload, null, 2) }] }; + } + + if (policy?.write) { + const guardErr = guardWriteOperation("", { skipBlocklist: true }); + if (guardErr) { + return { content: [{ type: "text", text: `Error: ${guardErr}` }], isError: true }; + } + } + + const result = await rpcCall(rpcMethod, { ...scopeParams, ...args }); + if (result && typeof result === "object" && result.ok === false) { + return { + content: [{ type: "text", text: formatRpcError(result) }], + isError: true, + }; + } + return { + content: [{ type: "text", text: JSON.stringify(result, null, 2) }], + }; + }; +} + +function registerMcpTools(server, deps) { + const tools = listMcpTools(); + for (const toolDef of tools) { + if (!toolDef.mcpTool || !toolDef.rpcMethod) continue; + const schemaShape = buildZodShapeObject(toolDef.inputShape); + const handler = createToolHandler(toolDef, deps); + const toolDescription = deps.catalogDescription(toolDef.mcpTool, toolDef.description); + server.tool(toolDef.mcpTool, toolDescription, schemaShape, handler); + } + return tools.length; +} + +module.exports = { + buildZodSchema, + buildZodShapeObject, + registerMcpTools, + SFTP_WRITE_TOOLS, +}; diff --git a/electron/capabilities/codegen/toolSurfaces.cjs b/electron/capabilities/codegen/toolSurfaces.cjs new file mode 100644 index 000000000..2a858fc7e --- /dev/null +++ b/electron/capabilities/codegen/toolSurfaces.cjs @@ -0,0 +1,204 @@ +"use strict"; + +const { AGENT_KINDS, CAPABILITY_STATUS, CAPABILITY_SURFACES } = require("../constants.cjs"); +const { ALL_CAPABILITIES } = require("../catalog/index.cjs"); +const { TOOL_INPUT_FIELDS, MODEL_DESCRIPTION_HINTS } = require("../schemas/toolInputs.cjs"); + +function buildZodShape(fields) { + const shape = {}; + for (const [key, field] of Object.entries(fields || {})) { + shape[key] = { + type: field.type, + optional: Boolean(field.optional), + description: field.description || "", + }; + } + return shape; +} + +function getMcpToolName(capability) { + return capability.surfaces?.public?.mcpTool + || capability.surfaces?.builtin?.mcpTool + || null; +} + +function getCattyToolName(capability) { + return capability.surfaces?.[CAPABILITY_SURFACES.CATTY]?.toolName + || getMcpToolName(capability) + || capability.id.replace(/\./g, "_"); +} + +function getCattyRpcMethod(capability) { + return capability.surfaces?.builtin?.rpcMethod + || capability.surfaces?.global?.rpcMethod + || capability.surfaces?.public?.rpcMethod + || null; +} + +function getAgentToolName(capability, agentKind) { + if (agentKind === AGENT_KINDS.GLOBAL) { + return capability.surfaces?.[CAPABILITY_SURFACES.GLOBAL_AGENT]?.toolName + || getMcpToolName(capability) + || capability.id.replace(/\./g, "_"); + } + return getCattyToolName(capability); +} + +function getAgentRpcMethod(capability) { + return getCattyRpcMethod(capability); +} + +function buildToolDescription(capability) { + const hint = MODEL_DESCRIPTION_HINTS[capability.id]; + if (!hint) return capability.description; + return `${capability.description} ${hint}`; +} + +function listToolSurfaces(options = {}) { + const { + surface = CAPABILITY_SURFACES.PUBLIC, + status = CAPABILITY_STATUS.IMPLEMENTED, + includeCatty = true, + } = options; + + const tools = []; + + for (const capability of ALL_CAPABILITIES) { + if (capability.status !== status) continue; + const binding = capability.surfaces?.[surface] || capability.surfaces?.public || capability.surfaces?.builtin; + if (!binding) continue; + + const mcpTool = getMcpToolName(capability); + const cattyToolName = getCattyToolName(capability); + if (!includeCatty && !mcpTool) continue; + + const builtinRpc = capability.surfaces?.builtin?.rpcMethod || binding.rpcMethod || null; + + tools.push({ + capabilityId: capability.id, + domain: capability.domain, + toolName: cattyToolName, + mcpTool, + rpcMethod: builtinRpc, + publicRpcMethod: capability.surfaces?.public?.rpcMethod || null, + description: buildToolDescription(capability), + policy: capability.policy, + inputShape: buildZodShape(TOOL_INPUT_FIELDS[capability.id]), + cattyEnabled: includeCatty && Boolean(TOOL_INPUT_FIELDS[capability.id] != null || mcpTool), + }); + } + + return tools; +} + +function listMcpTools() { + return listToolSurfaces({ surface: CAPABILITY_SURFACES.PUBLIC, includeCatty: false }) + .filter((tool) => tool.mcpTool); +} + +/** Capabilities excluded from Catty even when implemented (CLI-only / meta). */ +const CATTY_CAPABILITY_DENYLIST = new Set([ + "meta.status", + "session.cancel", + "session.resume", + "session.get", +]); + +function isCattyOnlyCapability(capability) { + return isAgentLocalOnlyCapability(capability, AGENT_KINDS.SIDEBAR); +} + +function isAgentLocalOnlyCapability(capability, agentKind) { + if (agentKind === AGENT_KINDS.GLOBAL) { + return Boolean(capability.surfaces?.[CAPABILITY_SURFACES.GLOBAL_AGENT]?.toolName) + && !getAgentRpcMethod(capability) + && !getMcpToolName(capability); + } + return Boolean(capability.surfaces?.[CAPABILITY_SURFACES.CATTY]?.toolName) + && !getAgentRpcMethod(capability) + && !getMcpToolName(capability); +} + +/** + * Resolve which agents may use a capability when agentKinds is not set explicitly. + * - surfaces.globalAgent only → global agent + * - surfaces.catty only (harness) → sidebar agent + * - RPC/MCP-backed tools → both agents (shared infrastructure) + */ +function resolveAgentKinds(capability) { + if (Array.isArray(capability.agentKinds) && capability.agentKinds.length > 0) { + return capability.agentKinds; + } + if (capability.surfaces?.[CAPABILITY_SURFACES.GLOBAL_AGENT]) { + return [AGENT_KINDS.GLOBAL]; + } + if (isAgentLocalOnlyCapability(capability, AGENT_KINDS.SIDEBAR)) { + return [AGENT_KINDS.SIDEBAR]; + } + if (isAgentEligibleForKind(capability, AGENT_KINDS.SIDEBAR, { skipAgentKindCheck: true })) { + return [AGENT_KINDS.SIDEBAR, AGENT_KINDS.GLOBAL]; + } + return []; +} + +function isAgentEligibleForKind(capability, agentKind, options = {}) { + if (capability.status !== CAPABILITY_STATUS.IMPLEMENTED) return false; + if (CATTY_CAPABILITY_DENYLIST.has(capability.id)) return false; + if (!options.skipAgentKindCheck && !resolveAgentKinds(capability).includes(agentKind)) { + return false; + } + const hasInputFields = Object.prototype.hasOwnProperty.call(TOOL_INPUT_FIELDS, capability.id); + if (!hasInputFields) return false; + if (isAgentLocalOnlyCapability(capability, agentKind)) return true; + const hasBuiltinRpc = Boolean(capability.surfaces?.builtin?.rpcMethod); + const hasGlobalRpc = Boolean(capability.surfaces?.global?.rpcMethod); + return hasBuiltinRpc || hasGlobalRpc || Boolean(getMcpToolName(capability)); +} + +function isCattyEligible(capability) { + return isAgentEligibleForKind(capability, AGENT_KINDS.SIDEBAR); +} + +function listAgentToolSpecs(agentKind = AGENT_KINDS.SIDEBAR) { + return ALL_CAPABILITIES + .filter((capability) => isAgentEligibleForKind(capability, agentKind)) + .map((capability) => { + const spec = { + capabilityId: capability.id, + toolName: getAgentToolName(capability, agentKind), + rpcMethod: getAgentRpcMethod(capability), + localExecution: isAgentLocalOnlyCapability(capability, agentKind), + description: buildToolDescription(capability), + inputShape: buildZodShape(TOOL_INPUT_FIELDS[capability.id]), + policy: capability.policy, + }; + if (agentKind !== AGENT_KINDS.SIDEBAR) { + spec.agentKind = agentKind; + } + return spec; + }); +} + +function listCattyToolSpecs() { + return listAgentToolSpecs(AGENT_KINDS.SIDEBAR); +} + +module.exports = { + AGENT_KINDS, + buildZodShape, + buildToolDescription, + CATTY_CAPABILITY_DENYLIST, + getAgentRpcMethod, + getAgentToolName, + getCattyToolName, + getCattyRpcMethod, + isAgentEligibleForKind, + isAgentLocalOnlyCapability, + isCattyEligible, + isCattyOnlyCapability, + listAgentToolSpecs, + listToolSurfaces, + listMcpTools, + listCattyToolSpecs, + resolveAgentKinds, +}; diff --git a/electron/capabilities/codegen/toolSurfaces.test.cjs b/electron/capabilities/codegen/toolSurfaces.test.cjs new file mode 100644 index 000000000..9e7b49388 --- /dev/null +++ b/electron/capabilities/codegen/toolSurfaces.test.cjs @@ -0,0 +1,162 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const { CAPABILITY_STATUS } = require("../constants.cjs"); +const { ALL_CAPABILITIES } = require("../catalog/index.cjs"); +const { TOOL_INPUT_FIELDS } = require("../schemas/toolInputs.cjs"); +const { + listMcpTools, + listCattyToolSpecs, + CATTY_CAPABILITY_DENYLIST, + isCattyEligible, +} = require("./toolSurfaces.cjs"); +const { registerMcpTools, buildZodShapeObject } = require("./mcpToolRegistry.cjs"); + +test("listCattyToolSpecs includes terminal long-running tools", () => { + const specs = listCattyToolSpecs(); + const names = specs.map((spec) => spec.toolName); + assert.ok(names.includes("terminal_execute")); + assert.ok(names.includes("terminal_start")); + assert.ok(names.includes("terminal_poll")); + assert.ok(names.includes("terminal_stop")); +}); + +test("listCattyToolSpecs includes SFTP write tools and attachments", () => { + const capabilityIds = listCattyToolSpecs().map((spec) => spec.capabilityId); + assert.ok(capabilityIds.includes("attachment.list")); + assert.ok(capabilityIds.includes("attachment.read")); + assert.ok(capabilityIds.includes("sftp.write")); + assert.ok(capabilityIds.includes("sftp.mkdir")); + assert.ok(capabilityIds.includes("sftp.delete")); + assert.ok(capabilityIds.includes("sftp.rename")); + assert.ok(capabilityIds.includes("sftp.chmod")); + assert.ok(!capabilityIds.includes("meta.status")); + assert.ok(!capabilityIds.includes("session.cancel")); +}); + +test("listCattyToolSpecs includes vault host tools and SFTP transfer", () => { + const capabilityIds = listCattyToolSpecs().map((spec) => spec.capabilityId); + assert.ok(capabilityIds.includes("vault.host.get")); + assert.ok(capabilityIds.includes("vault.host.list")); + assert.ok(capabilityIds.includes("vault.hosts.create")); + assert.ok(capabilityIds.includes("vault.host.import")); + assert.ok(capabilityIds.includes("vault.note.create")); + assert.ok(capabilityIds.includes("vault.note.list")); + assert.ok(capabilityIds.includes("sftp.download")); + assert.ok(capabilityIds.includes("sftp.upload")); +}); + +test("listCattyToolSpecs binds vault note tools to global RPC methods", () => { + const specs = listCattyToolSpecs(); + const noteCreate = specs.find((spec) => spec.capabilityId === "vault.note.create"); + assert.equal(noteCreate?.rpcMethod, "vault/notes/create"); + const noteList = specs.find((spec) => spec.capabilityId === "vault.note.list"); + assert.equal(noteList?.rpcMethod, "vault/notes/list"); +}); + +test("listCattyToolSpecs binds vault and portforward tools to global RPC methods", () => { + const specs = listCattyToolSpecs(); + const hostNotesSet = specs.find((spec) => spec.capabilityId === "vault.host.notes.set"); + assert.equal(hostNotesSet?.rpcMethod, "vault/host/notes/set"); + const portforwardStart = specs.find((spec) => spec.capabilityId === "portforward.start"); + assert.equal(portforwardStart?.rpcMethod, "portforward/start"); +}); + +test("listAgentToolSpecs splits sidebar harness tools from shared RPC tools", () => { + const { AGENT_KINDS, listAgentToolSpecs } = require("./toolSurfaces.cjs"); + const sidebarIds = listAgentToolSpecs(AGENT_KINDS.SIDEBAR).map((spec) => spec.capabilityId); + const globalIds = listAgentToolSpecs(AGENT_KINDS.GLOBAL).map((spec) => spec.capabilityId); + + assert.ok(sidebarIds.includes("harness.workspace.get_info")); + assert.ok(!globalIds.includes("harness.workspace.get_info")); + + assert.ok(sidebarIds.includes("terminal.execute")); + assert.ok(globalIds.includes("terminal.execute")); + assert.ok(globalIds.includes("vault.note.create")); + + assert.ok(globalIds.every((id) => sidebarIds.includes(id) || id.startsWith("harness.") === false)); +}); + +test("listCattyToolSpecs includes harness catty-only tools with local execution", () => { + const specs = listCattyToolSpecs(); + assert.ok(specs.length >= 40); + const harness = specs.filter((spec) => spec.capabilityId.startsWith("harness.")); + assert.equal(harness.length, 5); + for (const spec of harness) { + assert.equal(spec.localExecution, true); + assert.equal(spec.rpcMethod, null); + } + const harnessIds = harness.map((spec) => spec.capabilityId); + assert.ok(harnessIds.includes("harness.tool_output.read")); + assert.ok(harnessIds.includes("harness.workspace.get_info")); +}); + +test("harness capabilities are not exposed on MCP", () => { + const mcpCapabilityIds = listMcpTools().map((tool) => tool.capabilityId); + for (const capabilityId of mcpCapabilityIds) { + assert.ok(!capabilityId.startsWith("harness.")); + } + assert.equal(listMcpTools().length, 35); +}); + +test("listMcpTools descriptions stay aligned with catalog capability ids", () => { + const mcpTools = listMcpTools(); + assert.ok(mcpTools.length >= 35); + for (const tool of mcpTools) { + assert.ok(tool.capabilityId); + assert.ok(tool.mcpTool); + assert.ok(tool.description.length > 0); + assert.ok(tool.rpcMethod); + } +}); + +test("catty and mcp terminal tools share capability ids", () => { + const catty = listCattyToolSpecs().find((spec) => spec.toolName === "terminal_execute"); + const mcp = listMcpTools().find((tool) => tool.mcpTool === "terminal_execute"); + assert.equal(catty?.capabilityId, "terminal.execute"); + assert.equal(mcp?.capabilityId, "terminal.execute"); +}); + +test("implemented catalog tools with inputs are catty-eligible unless denylisted", () => { + const implemented = ALL_CAPABILITIES.filter((cap) => cap.status === CAPABILITY_STATUS.IMPLEMENTED); + for (const capability of implemented) { + const hasInputs = Object.prototype.hasOwnProperty.call(TOOL_INPUT_FIELDS, capability.id); + if (!hasInputs) continue; + if (CATTY_CAPABILITY_DENYLIST.has(capability.id)) { + assert.equal(isCattyEligible(capability), false); + continue; + } + const hasRpc = Boolean( + capability.surfaces?.builtin?.rpcMethod + || capability.surfaces?.public?.mcpTool, + ); + if (hasRpc) { + assert.equal(isCattyEligible(capability), true); + } + } +}); + +test("mcp registry builds zod shapes for every MCP tool", () => { + for (const tool of listMcpTools()) { + const shape = buildZodShapeObject(tool.inputShape); + assert.equal(typeof shape, "object"); + } +}); + +test("registerMcpTools registers one handler per catalog MCP tool", () => { + const registered = []; + const fakeServer = { + tool(name, _description, _shape, handler) { + registered.push({ name, handler: typeof handler }); + }, + }; + const count = registerMcpTools(fakeServer, { + rpcCall: async () => ({ ok: true }), + scopeParams: {}, + guardWriteOperation: () => null, + catalogDescription: (_name, fallback) => fallback, + }); + assert.equal(count, listMcpTools().length); + assert.equal(registered.length, listMcpTools().length); +}); diff --git a/electron/capabilities/constants.cjs b/electron/capabilities/constants.cjs index ee96c70bd..48d5bc0d0 100644 --- a/electron/capabilities/constants.cjs +++ b/electron/capabilities/constants.cjs @@ -1,14 +1,27 @@ "use strict"; -/** @typedef {'builtin' | 'public' | 'cli' | 'global'} CapabilitySurface */ +/** @typedef {'builtin' | 'public' | 'cli' | 'global' | 'catty' | 'globalAgent'} CapabilitySurface */ /** @typedef {'implemented' | 'planned'} CapabilityStatus */ /** @typedef {'observer' | 'confirm' | 'autonomous'} PermissionMode */ +/** @typedef {'sidebar' | 'global'} AgentKind */ const CAPABILITY_SURFACES = Object.freeze({ BUILTIN: "builtin", PUBLIC: "public", CLI: "cli", GLOBAL: "global", + /** Renderer-local sidebar (Catty) harness tools (no MCP/CLI exposure). */ + CATTY: "catty", + /** Renderer-local global agent tools (no MCP/CLI exposure). */ + GLOBAL_AGENT: "globalAgent", +}); + +/** Where in the app an agent runs — orthogonal to RPC/MCP/CLI capability surfaces. */ +const AGENT_KINDS = Object.freeze({ + /** Chat side panel (Catty). */ + SIDEBAR: "sidebar", + /** Future app-wide agent (cross-window / proactive). */ + GLOBAL: "global", }); const CAPABILITY_STATUS = Object.freeze({ @@ -30,6 +43,7 @@ const RPC_TIMEOUT_DEFAULTS = Object.freeze({ }); module.exports = { + AGENT_KINDS, CAPABILITY_SURFACES, CAPABILITY_STATUS, PERMISSION_MODES, diff --git a/electron/capabilities/dispatch.test.cjs b/electron/capabilities/dispatch.test.cjs index df0559fbd..b56d2d17d 100644 --- a/electron/capabilities/dispatch.test.cjs +++ b/electron/capabilities/dispatch.test.cjs @@ -22,7 +22,7 @@ test("createRegistryDispatcher routes implemented capabilities to handlers", asy assert.equal(calls.length, 0); }); -test("createRegistryDispatcher returns not-implemented for planned capabilities", async () => { +test("createRegistryDispatcher falls back for implemented capabilities without handlers", async () => { const dispatch = createRegistryDispatcher({ surface: CAPABILITY_SURFACES.GLOBAL, handlers: {}, @@ -31,7 +31,7 @@ test("createRegistryDispatcher returns not-implemented for planned capabilities" const result = await dispatch("vault/host/notes/get", { hostId: "host-1" }); assert.equal(result.ok, false); - assert.equal(result.code, "CAPABILITY_NOT_IMPLEMENTED"); + assert.equal(result.error, "unknown:vault/host/notes/get"); }); test("createRegistryDispatcher falls back for unknown rpc methods", async () => { diff --git a/electron/capabilities/policy.cjs b/electron/capabilities/policy.cjs index 1198c2c67..927cde9eb 100644 --- a/electron/capabilities/policy.cjs +++ b/electron/capabilities/policy.cjs @@ -84,6 +84,32 @@ function evaluateRpcPermission({ }; } +function evaluatePermissionWithGrants(ctx, grants = []) { + const base = evaluateRpcPermission(ctx); + if (!base.allowed || !base.requiresApproval || !base.capability) { + return base; + } + + const { matchPermissionGrant } = require("../shared/permissionGrants.cjs"); + const params = ctx?.params && typeof ctx.params === "object" ? ctx.params : {}; + const matched = matchPermissionGrant(grants, { + capabilityId: base.capability.id, + chatSessionId: params.chatSessionId, + sessionId: params.sessionId, + args: params, + }); + + if (matched) { + return { + ...base, + requiresApproval: false, + matchedGrantId: matched.id, + }; + } + + return base; +} + function buildRpcMethodSet(surface, predicate) { const { getRpcMethodsForSurface } = require("./registry.cjs"); const methods = new Set(); @@ -132,6 +158,7 @@ module.exports = { requiresApprovalInConfirmMode, isBlockedInObserverMode, evaluateRpcPermission, + evaluatePermissionWithGrants, BUILTIN_WRITE_RPC_METHODS, BUILTIN_APPROVAL_RPC_METHODS, PUBLIC_WRITE_RPC_METHODS, diff --git a/electron/capabilities/policy.test.cjs b/electron/capabilities/policy.test.cjs index dd692db6f..ec6230c17 100644 --- a/electron/capabilities/policy.test.cjs +++ b/electron/capabilities/policy.test.cjs @@ -8,6 +8,7 @@ const { BUILTIN_APPROVAL_RPC_METHODS, PUBLIC_CONFIRM_RPC_METHODS, evaluateRpcPermission, + evaluatePermissionWithGrants, OBSERVER_DENY_MESSAGE, } = require("./policy.cjs"); const { CAPABILITY_SURFACES, PERMISSION_MODES } = require("./constants.cjs"); @@ -146,3 +147,51 @@ test("unknown rpc methods pass through policy checks", () => { assert.equal(decision.requiresApproval, false); assert.equal(decision.capability, null); }); + +test("confirm mode requires approval for portforward start and host notes set", () => { + const portforwardDecision = evaluateRpcPermission({ + rpcMethod: "public/portforward/start", + surface: CAPABILITY_SURFACES.PUBLIC, + permissionMode: PERMISSION_MODES.CONFIRM, + params: { chatSessionId: "chat-1", ruleId: "rule-1" }, + }); + assert.equal(portforwardDecision.requiresApproval, true); + + const notesDecision = evaluateRpcPermission({ + rpcMethod: "vault/host/notes/set", + surface: CAPABILITY_SURFACES.GLOBAL, + permissionMode: PERMISSION_MODES.CONFIRM, + params: { chatSessionId: "chat-1", hostId: "host-1" }, + }); + assert.equal(notesDecision.requiresApproval, true); + + const publicNotesDecision = evaluateRpcPermission({ + rpcMethod: "public/vault/hostNotes/set", + surface: CAPABILITY_SURFACES.PUBLIC, + permissionMode: PERMISSION_MODES.CONFIRM, + params: { chatSessionId: "chat-1", hostId: "host-1" }, + }); + assert.equal(publicNotesDecision.requiresApproval, true); +}); + +test("evaluatePermissionWithGrants skips approval when a grant matches", () => { + const decision = evaluatePermissionWithGrants({ + rpcMethod: "netcatty/exec", + permissionMode: PERMISSION_MODES.CONFIRM, + params: { + chatSessionId: "chat-1", + sessionId: "session-a", + command: "ls -la", + }, + context: { chatSessionCancelled: false }, + }, [{ + id: "grant-1", + capabilityId: "terminal.execute", + sessionPattern: "session-a", + commandPattern: "ls *", + createdAt: Date.now(), + }]); + + assert.equal(decision.allowed, true); + assert.equal(decision.requiresApproval, false); +}); diff --git a/electron/capabilities/registry.cjs b/electron/capabilities/registry.cjs index 0179343f1..77b1a45a5 100644 --- a/electron/capabilities/registry.cjs +++ b/electron/capabilities/registry.cjs @@ -9,6 +9,7 @@ function buildRegistryIndex(capabilities) { const byMcpTool = new Map(); const byCliCommand = new Map(); const byDomain = new Map(); + const byCattyTool = new Map(); for (const capability of capabilities) { byId.set(capability.id, capability); @@ -27,6 +28,9 @@ function buildRegistryIndex(capabilities) { if (Array.isArray(binding?.command) && binding.command.length > 0) { byCliCommand.set(binding.command.join(" "), capability); } + if (binding?.toolName) { + byCattyTool.set(binding.toolName, capability); + } } } @@ -36,6 +40,7 @@ function buildRegistryIndex(capabilities) { byRpcMethod, byMcpTool, byCliCommand, + byCattyTool, byDomain, }; } @@ -69,6 +74,10 @@ function getCapabilityByCliCommand(commandParts) { return registryIndex.byCliCommand.get(key) || null; } +function getCapabilityByCattyToolName(toolName) { + return registryIndex.byCattyTool.get(toolName) || null; +} + function getRpcMethodsForSurface(surface, filter = {}) { const methods = new Set(); for (const capability of registryIndex.capabilities) { @@ -95,6 +104,7 @@ module.exports = { getCapabilityByRpcMethod, getCapabilityByMcpTool, getCapabilityByCliCommand, + getCapabilityByCattyToolName, getRpcMethodsForSurface, getImplementedRpcMethodsForSurface, buildRegistryIndex, diff --git a/electron/capabilities/registry.test.cjs b/electron/capabilities/registry.test.cjs index bf56bd5ed..c3ea1e32b 100644 --- a/electron/capabilities/registry.test.cjs +++ b/electron/capabilities/registry.test.cjs @@ -13,12 +13,10 @@ const { } = require("./registry.cjs"); const { CAPABILITY_STATUS, CAPABILITY_SURFACES } = require("./constants.cjs"); -test("registry contains implemented and planned capabilities", () => { +test("registry contains implemented capabilities", () => { assert.ok(ALL_CAPABILITIES.length >= 20); const implemented = listCapabilities({ status: CAPABILITY_STATUS.IMPLEMENTED }); - const planned = listCapabilities({ status: CAPABILITY_STATUS.PLANNED }); - assert.ok(implemented.length > 0); - assert.ok(planned.length > 0); + assert.ok(implemented.length >= 20); }); test("registry resolves builtin rpc methods and mcp tools", () => { @@ -41,7 +39,7 @@ test("registry resolves cli commands", () => { assert.equal(sftpList?.id, "sftp.list"); }); -test("planned vault and portforward capabilities are registered", () => { - assert.equal(getCapabilityById("vault.host.notes.get")?.status, CAPABILITY_STATUS.PLANNED); - assert.equal(getCapabilityById("portforward.start")?.status, CAPABILITY_STATUS.PLANNED); +test("vault and portforward capabilities are implemented", () => { + assert.equal(getCapabilityById("vault.host.notes.get")?.status, CAPABILITY_STATUS.IMPLEMENTED); + assert.equal(getCapabilityById("portforward.start")?.status, CAPABILITY_STATUS.IMPLEMENTED); }); diff --git a/electron/capabilities/schemas/toolInputs.cjs b/electron/capabilities/schemas/toolInputs.cjs new file mode 100644 index 000000000..1d2c9de69 --- /dev/null +++ b/electron/capabilities/schemas/toolInputs.cjs @@ -0,0 +1,207 @@ +"use strict"; + +/** + * Input field definitions keyed by capability id. + * Single source for MCP, Catty, and CLI tool schemas. + */ +const TOOL_INPUT_FIELDS = Object.freeze({ + "terminal.execute": { + sessionId: { type: "string", description: "The terminal session ID to execute on." }, + command: { type: "string", description: "The shell command to execute in the target session." }, + }, + "terminal.start": { + sessionId: { type: "string", description: "The terminal session ID to start a long-running command on." }, + command: { type: "string", description: "The command to start in the target session." }, + }, + "terminal.poll": { + jobId: { type: "string", description: "The background job ID returned by terminal_start." }, + offset: { type: "number", optional: true, description: "Character offset from a previous poll (default 0)." }, + }, + "terminal.stop": { + jobId: { type: "string", description: "The background job ID returned by terminal_start." }, + }, + "session.environment": {}, + "attachment.list": {}, + "attachment.read": { + filePath: { type: "string", optional: true, description: "Exact local attachment path." }, + filename: { type: "string", optional: true, description: "Attachment filename from list_attachments." }, + }, + "sftp.list": { + sessionId: { type: "string", description: "SFTP session ID." }, + path: { type: "string", description: "Remote directory path." }, + }, + "sftp.read": { + sessionId: { type: "string", description: "SFTP session ID." }, + path: { type: "string", description: "Remote file path to read." }, + }, + "sftp.stat": { + sessionId: { type: "string", description: "SFTP session ID." }, + path: { type: "string", description: "Remote path to stat." }, + }, + "sftp.home": { + sessionId: { type: "string", description: "SFTP session ID." }, + }, + "sftp.write": { + sessionId: { type: "string", description: "SFTP session ID." }, + path: { type: "string", description: "Remote file path to write." }, + content: { type: "string", description: "File content to write." }, + }, + "sftp.download": { + sessionId: { type: "string", description: "SFTP session ID." }, + remotePath: { type: "string", description: "Remote file path to download." }, + localPath: { type: "string", description: "Local destination path." }, + }, + "sftp.upload": { + sessionId: { type: "string", description: "SFTP session ID." }, + localPath: { type: "string", description: "Local file path to upload." }, + remotePath: { type: "string", description: "Remote destination path." }, + }, + "sftp.mkdir": { + sessionId: { type: "string", description: "SFTP session ID." }, + path: { type: "string", description: "Remote directory path to create." }, + }, + "sftp.delete": { + sessionId: { type: "string", description: "SFTP session ID." }, + path: { type: "string", description: "Remote file or directory path to delete." }, + }, + "sftp.rename": { + sessionId: { type: "string", description: "SFTP session ID." }, + oldPath: { type: "string", description: "Current remote path." }, + newPath: { type: "string", description: "New remote path." }, + }, + "sftp.chmod": { + sessionId: { type: "string", description: "SFTP session ID." }, + path: { type: "string", description: "Remote file path." }, + mode: { type: "string", description: "Octal permission mode (e.g. 755)." }, + }, + "vault.host.get": { + hostId: { type: "string", description: "Vault host ID." }, + }, + "vault.host.list": {}, + "vault.hosts.create": { + hosts: { + type: "string", + description: + "JSON array of host objects you extracted from the user's text. Each object: hostname (required), label, port, username, password, group, tags (array or comma-separated string), notes (Host Details remarks — NOT Vault sidebar Notes), protocol (ssh|telnet|local).", + }, + dryRun: { + type: "string", + optional: true, + description: "Set to true to validate and preview without writing to the vault.", + }, + skipDuplicates: { + type: "string", + optional: true, + description: "Set to false to create even when a matching host already exists (default true).", + }, + }, + "vault.host.import": { + format: { + type: "string", + description: "Import format: csv, putty, mobaxterm, securecrt, ssh_config, or auto to detect from text.", + }, + text: { type: "string", description: "Exported host data text to import." }, + dryRun: { + type: "string", + optional: true, + description: "Set to true to parse and preview without writing to the vault.", + }, + skipDuplicates: { + type: "string", + optional: true, + description: "Set to false to import even when a matching host already exists (default true).", + }, + fileName: { + type: "string", + optional: true, + description: "Optional source file name (helps SecureCRT .ini parsing).", + }, + }, + "vault.host.notes.get": { + hostId: { type: "string", description: "Vault host ID." }, + }, + "vault.host.notes.set": { + hostId: { type: "string", description: "Vault host ID." }, + notes: { type: "string", description: "Host metadata notes (Host Details — not Vault sidebar Notes)." }, + }, + "vault.note.list": {}, + "vault.note.get": { + noteId: { type: "string", description: "Vault note ID from vault_notes_list." }, + }, + "vault.note.create": { + title: { type: "string", description: "Note title shown in Vault → Notes." }, + content: { type: "string", description: "Markdown note body." }, + group: { type: "string", optional: true, description: "Optional folder path (e.g. infra/prod)." }, + linkedHostIds: { type: "string", optional: true, description: "Optional JSON array of vault host IDs to link." }, + tags: { type: "string", optional: true, description: "Optional JSON array of tag strings." }, + }, + "vault.note.update": { + noteId: { type: "string", description: "Vault note ID to update." }, + title: { type: "string", optional: true, description: "New title." }, + content: { type: "string", optional: true, description: "New markdown body." }, + group: { type: "string", optional: true, description: "New folder path." }, + linkedHostIds: { type: "string", optional: true, description: "Optional JSON array of vault host IDs to link." }, + tags: { type: "string", optional: true, description: "Optional JSON array of tag strings." }, + }, + "vault.snippets.list": {}, + "vault.snippets.get": { + snippetId: { type: "string", description: "Snippet ID." }, + }, + "vault.snippets.run": { + snippetId: { type: "string", description: "Snippet ID to run." }, + sessionId: { type: "string", description: "Terminal session ID to execute on." }, + variables: { type: "string", optional: true, description: "JSON object of snippet variable values." }, + }, + "portforward.rules.list": {}, + "portforward.tunnels.list": {}, + "portforward.start": { + ruleId: { type: "string", description: "Port forwarding rule ID." }, + }, + "portforward.stop": { + ruleId: { type: "string", description: "Port forwarding rule ID." }, + }, + "harness.tool_output.read": { + handleId: { type: "string", description: "Tool output handle id from a prior truncated result." }, + mode: { type: "string", optional: true, description: "Which portion to read: head, tail, or full." }, + maxChars: { type: "number", optional: true, description: "Maximum characters to return." }, + }, + "harness.workspace.get_info": {}, + "harness.workspace.get_session_info": { + sessionId: { type: "string", description: "The session ID to get information about." }, + }, + "harness.web.search": { + query: { type: "string", description: "The search query to look up on the web." }, + maxResults: { type: "number", optional: true, description: "Maximum number of search results to return." }, + }, + "harness.url.fetch": { + url: { type: "string", description: "The HTTPS URL to fetch." }, + maxLength: { type: "number", optional: true, description: "Maximum characters to return (default 50000)." }, + }, +}); + +/** Long-form model guidance appended to terminal tool descriptions from catalog. */ +const MODEL_DESCRIPTION_HINTS = Object.freeze({ + "terminal.execute": + "Use only for commands expected to finish within about 60 seconds. For long-running commands use terminal_start and terminal_poll.", + "terminal.start": + "Prefer for builds, scans, log-following, or anything likely to exceed about 2 minutes.", + "terminal.poll": + "Wait at least about 30 seconds between polls unless output justifies checking sooner.", + "vault.host.notes.get": + "Host metadata notes on a saved host — not Vault → Notes sidebar entries.", + "vault.host.notes.set": + "Host metadata notes on a saved host — not Vault → Notes sidebar entries. Prefer vault_notes_create/update when the user wants vault notes they can open in the Notes sidebar.", + "vault.host.import": + "Only for text in known export formats (PuTTY reg, MobaXterm ini, CSV template, SecureCRT, ssh_config). For arbitrary unstructured host lists from the user, extract fields yourself and call vault_hosts_create.", + "vault.hosts.create": + "Use when the user wants to add/create a host in Vault → Hosts (创建主机、SSH 连接凭据). NOT for Vault → Notes sidebar docs. Put SSH password in password field; long remarks/admin tables in host notes field. Never fall back to vault_notes_create if this fails.", + "vault.note.create": + "Use ONLY when the user wants markdown documentation in Vault → Notes sidebar (保险箱笔记). Do NOT use when the user asked to create/add a host — use vault_hosts_create instead.", + "vault.note.update": + "Update an existing Vault → Notes entry (visible in the vault notes sidebar).", +}); + +module.exports = { + TOOL_INPUT_FIELDS, + MODEL_DESCRIPTION_HINTS, +}; diff --git a/electron/capabilities/services/portforwardService.cjs b/electron/capabilities/services/portforwardService.cjs index b3bba4b25..124236fbd 100644 --- a/electron/capabilities/services/portforwardService.cjs +++ b/electron/capabilities/services/portforwardService.cjs @@ -1,17 +1,42 @@ "use strict"; -const { createNotImplementedHandler } = require("./notImplemented.cjs"); +const portForwardingBridge = require("../../bridges/portForwardingBridge.cjs"); /** - * Port forwarding domain service skeleton. Implementation will delegate to - * portForwardingBridge once rules are accessible from the main process. + * Port forwarding domain service. Tunnels live in main; rules live in renderer vault. */ -function createPortForwardService(_ctx = {}) { +function createPortForwardService(ctx = {}) { + const { invokeVaultAgent } = ctx; + return { - listRules: createNotImplementedHandler("portforward.rules.list"), - listTunnels: createNotImplementedHandler("portforward.tunnels.list"), - start: createNotImplementedHandler("portforward.start"), - stop: createNotImplementedHandler("portforward.stop"), + listRules: async () => { + if (typeof invokeVaultAgent !== "function") { + return { ok: false, error: "Vault agent bridge is unavailable." }; + } + return invokeVaultAgent("portforward.rules.list", {}); + }, + listTunnels: async () => { + const tunnels = await portForwardingBridge.listPortForwards(); + return { ok: true, tunnels }; + }, + start: async (params = {}) => { + if (typeof invokeVaultAgent !== "function") { + return { ok: false, error: "Vault agent bridge is unavailable." }; + } + return invokeVaultAgent("portforward.start", { + ruleId: params.ruleId, + chatSessionId: params.chatSessionId, + }); + }, + stop: async (params = {}) => { + if (typeof invokeVaultAgent !== "function") { + return { ok: false, error: "Vault agent bridge is unavailable." }; + } + return invokeVaultAgent("portforward.stop", { + ruleId: params.ruleId, + chatSessionId: params.chatSessionId, + }); + }, }; } diff --git a/electron/capabilities/services/portforwardService.test.cjs b/electron/capabilities/services/portforwardService.test.cjs new file mode 100644 index 000000000..aa7cfeb4f --- /dev/null +++ b/electron/capabilities/services/portforwardService.test.cjs @@ -0,0 +1,28 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { createPortForwardService } = require("./portforwardService.cjs"); + +test("portforward service lists active tunnels from main bridge", async () => { + const service = createPortForwardService({ + invokeVaultAgent: async () => ({ ok: true, rules: [] }), + }); + const result = await service.listTunnels(); + assert.equal(result.ok, true); + assert.ok(Array.isArray(result.tunnels)); +}); + +test("portforward start delegates to vault agent bridge after approval path", async () => { + let invokedOp = null; + const service = createPortForwardService({ + invokeVaultAgent: async (op, params) => { + invokedOp = op; + return { ok: true, ruleId: params.ruleId }; + }, + }); + const result = await service.start({ ruleId: "rule-1", chatSessionId: "chat-1" }); + assert.equal(invokedOp, "portforward.start"); + assert.equal(result.ok, true); +}); diff --git a/electron/capabilities/services/vaultService.cjs b/electron/capabilities/services/vaultService.cjs index 5f2ca1255..7af5b766a 100644 --- a/electron/capabilities/services/vaultService.cjs +++ b/electron/capabilities/services/vaultService.cjs @@ -1,19 +1,93 @@ "use strict"; -const { createNotImplementedHandler } = require("./notImplemented.cjs"); - /** - * Vault domain service skeleton. Handlers will be wired to a VaultFacade bridge - * in a follow-up PR; registry entries already describe the public contract. + * Vault domain service. Read-only metadata and notes/snippets are served from + * renderer vault state via VaultAgentBridge; credentials never cross the bridge. */ -function createVaultService(_ctx = {}) { +function createVaultService(ctx = {}) { + const { invokeVaultAgent } = ctx; + + function requireBridge() { + if (typeof invokeVaultAgent !== "function") { + return { ok: false, error: "Vault agent bridge is unavailable." }; + } + return null; + } + return { - getHost: createNotImplementedHandler("vault.host.get"), - getHostNotes: createNotImplementedHandler("vault.host.notes.get"), - setHostNotes: createNotImplementedHandler("vault.host.notes.set"), - listSnippets: createNotImplementedHandler("vault.snippets.list"), - getSnippet: createNotImplementedHandler("vault.snippets.get"), - runSnippet: createNotImplementedHandler("vault.snippets.run"), + getHost: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("host.get", { hostId: params.hostId }); + }, + listHosts: async () => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("host.list", {}); + }, + createHosts: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("hosts.create", params); + }, + importHosts: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("host.import", params); + }, + getHostNotes: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("host.notes.get", { hostId: params.hostId }); + }, + setHostNotes: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("host.notes.set", { + hostId: params.hostId, + notes: params.notes, + }); + }, + listNotes: async () => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("note.list", {}); + }, + getNote: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("note.get", { noteId: params.noteId }); + }, + createNote: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("note.create", params); + }, + updateNote: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("note.update", params); + }, + listSnippets: async () => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("snippets.list", {}); + }, + getSnippet: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("snippets.get", { snippetId: params.snippetId }); + }, + runSnippet: async (params = {}) => { + const bridgeErr = requireBridge(); + if (bridgeErr) return bridgeErr; + return invokeVaultAgent("snippets.run", { + snippetId: params.snippetId, + sessionId: params.sessionId, + variables: params.variables, + chatSessionId: params.chatSessionId, + }); + }, }; } diff --git a/electron/capabilities/services/vaultService.test.cjs b/electron/capabilities/services/vaultService.test.cjs index a174fc30c..1f1c021ca 100644 --- a/electron/capabilities/services/vaultService.test.cjs +++ b/electron/capabilities/services/vaultService.test.cjs @@ -5,9 +5,23 @@ const assert = require("node:assert/strict"); const { createVaultService } = require("./vaultService.cjs"); -test("vault service skeleton returns not-implemented for all handlers", async () => { - const service = createVaultService(); +test("vault service delegates host notes read to vault agent bridge", async () => { + let invokedOp = null; + const service = createVaultService({ + invokeVaultAgent: async (op, params) => { + invokedOp = op; + return { ok: true, hostId: params.hostId, notes: "hello" }; + }, + }); const result = await service.getHostNotes({ hostId: "host-1" }); + assert.equal(invokedOp, "host.notes.get"); + assert.equal(result.ok, true); + assert.equal(result.notes, "hello"); +}); + +test("vault service returns bridge unavailable when renderer bridge missing", async () => { + const service = createVaultService({}); + const result = await service.listSnippets(); assert.equal(result.ok, false); - assert.equal(result.code, "CAPABILITY_NOT_IMPLEMENTED"); + assert.match(result.error, /unavailable/i); }); diff --git a/electron/capabilities/types.cjs b/electron/capabilities/types.cjs index 28689262e..0a4e5a866 100644 --- a/electron/capabilities/types.cjs +++ b/electron/capabilities/types.cjs @@ -6,10 +6,12 @@ * @typedef {import('./constants.cjs').CapabilitySurface} CapabilitySurface * @typedef {import('./constants.cjs').CapabilityStatus} CapabilityStatus * @typedef {import('./constants.cjs').PermissionMode} PermissionMode + * @typedef {import('./constants.cjs').AgentKind} AgentKind * * @typedef {Object} CapabilitySurfaceBinding * @property {string} [rpcMethod] * @property {string} [mcpTool] + * @property {string} [toolName] * @property {string[]} [command] * @property {boolean} [confirmInConfirmMode] * @@ -29,6 +31,7 @@ * @property {string} description * @property {CapabilityPolicy} policy * @property {Partial>} surfaces + * @property {AgentKind[]} [agentKinds] Explicit agent placement; inferred when omitted (see resolveAgentKinds). * * @typedef {Object} RpcPermissionContext * @property {boolean} [chatSessionCancelled] diff --git a/electron/cli/netcatty-tool-cli.capabilities.test.cjs b/electron/cli/netcatty-tool-cli.capabilities.test.cjs index 6ee6402cd..2463aa55d 100644 --- a/electron/cli/netcatty-tool-cli.capabilities.test.cjs +++ b/electron/cli/netcatty-tool-cli.capabilities.test.cjs @@ -14,4 +14,6 @@ test("netcatty-tool-cli capabilities lists implemented commands without app conn const payload = JSON.parse(result.stdout); assert.equal(payload.ok, true); assert.ok(payload.capabilities.some((entry) => entry.id === "terminal.execute")); + assert.ok(payload.capabilities.some((entry) => entry.id === "vault.host.get")); + assert.ok(payload.capabilities.some((entry) => entry.id === "portforward.rules.list")); }); diff --git a/electron/cli/netcatty-tool-cli.cjs b/electron/cli/netcatty-tool-cli.cjs index 90b8ee483..67508d24a 100644 --- a/electron/cli/netcatty-tool-cli.cjs +++ b/electron/cli/netcatty-tool-cli.cjs @@ -4,44 +4,29 @@ const path = require("node:path"); const { connectClient, createError } = require("./netcattyRpcClient.cjs"); -const { listCliCapabilities } = require("../capabilities/adapters/cliAdapter.cjs"); +const { + buildCatalogCliParams, + formatCliHelpLines, + getCliRpcMethod, + listCliCapabilities, +} = require("../capabilities/adapters/cliAdapter.cjs"); +const { getCapabilityByCliCommand } = require("../capabilities/registry.cjs"); const { CAPABILITY_STATUS } = require("../capabilities/constants.cjs"); function printHelp() { + const catalogLines = formatCliHelpLines().join("\n"); process.stdout.write( "Netcatty Tool CLI\n\n" + "Usage:\n" + - " netcatty-tool-cli status [--json]\n" + - " netcatty-tool-cli env --chat-session [--json] [--scope-session ...]\n" + - " netcatty-tool-cli session --session --chat-session [--json] [--scope-session ...]\n" + - " netcatty-tool-cli exec --session --chat-session [--json] [--] \n" + - " netcatty-tool-cli job-start --session --chat-session [--json] [--] \n" + - " netcatty-tool-cli job-poll --job --chat-session [--offset ] [--json]\n" + - " netcatty-tool-cli job-stop --job --chat-session [--json]\n" + - " netcatty-tool-cli sftp list --session --remote-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp read --session --remote-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp write --session --remote-path --content --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp download --session --remote-path --local-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp upload --session --local-path --remote-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp mkdir --session --remote-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp delete --session --remote-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp rename --session --old-remote-path --new-remote-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp stat --session --remote-path --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp chmod --session --remote-path --mode --chat-session [--encoding ] [--json] [--scope-session ...]\n" + - " netcatty-tool-cli sftp home --session --chat-session [--json] [--scope-session ...]\n" + - " netcatty-tool-cli cancel --chat-session [--json]\n" + - " netcatty-tool-cli resume --chat-session [--json]\n" + - " netcatty-tool-cli capabilities [--json] [--status implemented|planned|all]\n" + - " netcatty-tool-cli help\n\n" + + catalogLines + "\n\n" + "Examples:\n" + " netcatty-tool-cli status --json\n" + " netcatty-tool-cli env --chat-session ai_123 --json\n" + " netcatty-tool-cli session --session sess_123 --json --chat-session ai_123\n" + " netcatty-tool-cli exec --session sess_123 --chat-session ai_123 --json -- \"pwd\"\n" + - " netcatty-tool-cli job-start --session sess_123 --chat-session ai_123 --json -- \"npm run dev\"\n" + - " netcatty-tool-cli job-poll --job job_123 --chat-session ai_123 --offset 0 --json\n" + - " netcatty-tool-cli sftp list --session sess_123 --remote-path /etc --chat-session ai_123 --json\n" + - " netcatty-tool-cli sftp download --session sess_123 --remote-path /etc/hosts --local-path ./hosts.txt --chat-session ai_123 --json\n\n" + + " netcatty-tool-cli vault host get --host-id host_123 --json\n" + + " netcatty-tool-cli snippets run --snippet-id snip_1 --session sess_123 --chat-session ai_123 --json\n" + + " netcatty-tool-cli portforward rules list --json\n\n" + "Notes:\n" + " - Start the Netcatty desktop app before using this CLI.\n" + " - This CLI is intended as an internal Skills + CLI transport, not a general customer-facing shell tool.\n" + @@ -50,6 +35,7 @@ function printHelp() { " - `job-start` always requires both --session and --chat-session .\n" + " - `job-poll` and `job-stop` always require both --job and --chat-session .\n" + " - Every `sftp ` always requires both --session and --chat-session , and only works on connected SSH-backed sessions.\n" + + " - Vault/portforward/snippet commands use catalog-driven dispatch; see `capabilities --json` for the full list.\n" + " - After `--`, pass exactly one shell-ready command string. Preserve quoting inside that one argument.\n" + " - `cancel` stops in-flight execs, session-backed SFTP transfers, and running jobs for that chat session, then blocks further execs until `resume`.\n", ); @@ -85,6 +71,11 @@ function parseArgs(argv) { content: null, mode: null, encoding: null, + hostId: null, + snippetId: null, + ruleId: null, + notes: null, + variables: null, command: [], }; @@ -161,6 +152,31 @@ function parseArgs(argv) { i += 1; continue; } + if (arg === "--host-id") { + opts.hostId = readFlagValue(args, i + 1); + i += 1; + continue; + } + if (arg === "--snippet-id") { + opts.snippetId = readFlagValue(args, i + 1); + i += 1; + continue; + } + if (arg === "--rule-id") { + opts.ruleId = readFlagValue(args, i + 1); + i += 1; + continue; + } + if (arg === "--notes") { + opts.notes = readFlagValue(args, i + 1); + i += 1; + continue; + } + if (arg === "--variables") { + opts.variables = readFlagValue(args, i + 1); + i += 1; + continue; + } positionals.push(arg); } @@ -694,6 +710,31 @@ async function run() { return; } + const catalogCapability = getCapabilityByCliCommand(positionals); + if (catalogCapability) { + const rpcMethod = getCliRpcMethod(positionals); + if (!rpcMethod) { + throw createError("INVALID_ARGUMENT", `No RPC mapping for command: ${positionals.join(" ")}`); + } + if (catalogCapability.policy?.requiresChatSession && !opts.chatSessionId) { + throw createError( + "INVALID_ARGUMENT", + `Missing required --chat-session for ${positionals.join(" ")}.`, + ); + } + const params = buildCatalogCliParams(catalogCapability.id, opts, createError); + const result = ensureBridgeCallOk( + await client.call(rpcMethod, { ...params, ...buildScopeParams(opts) }), + "CAPABILITY_RPC_FAILED", + `Failed to execute ${catalogCapability.id}`, + ); + const payload = { ok: true, ...result }; + process.stdout.write(opts.json + ? `${JSON.stringify(payload, null, 2)}\n` + : `${JSON.stringify(result, null, 2)}\n`); + return; + } + throw createError("INVALID_ARGUMENT", `Unknown command: ${positionals.join(" ")}`); } catch (err) { const payload = toErrorPayload(err); diff --git a/electron/mcp/catalogToolMetadata.cjs b/electron/mcp/catalogToolMetadata.cjs new file mode 100644 index 000000000..a38a8e677 --- /dev/null +++ b/electron/mcp/catalogToolMetadata.cjs @@ -0,0 +1,20 @@ +"use strict"; + +const { listMcpTools } = require("../capabilities/codegen/toolSurfaces.cjs"); + +const mcpToolDescriptions = new Map( + listMcpTools().map((tool) => [tool.mcpTool, tool.description]), +); + +function getCatalogToolDescription(toolName) { + return mcpToolDescriptions.get(toolName) || null; +} + +function listCatalogMcpToolNames() { + return [...mcpToolDescriptions.keys()]; +} + +module.exports = { + getCatalogToolDescription, + listCatalogMcpToolNames, +}; diff --git a/electron/mcp/netcatty-mcp-server.cjs b/electron/mcp/netcatty-mcp-server.cjs index 4b2fcf2eb..be72fec20 100644 --- a/electron/mcp/netcatty-mcp-server.cjs +++ b/electron/mcp/netcatty-mcp-server.cjs @@ -10,7 +10,12 @@ const net = require("node:net"); const { McpServer } = require("@modelcontextprotocol/sdk/server/mcp.js"); const { StdioServerTransport } = require("@modelcontextprotocol/sdk/server/stdio.js"); -const { z } = require("zod"); +const { getCatalogToolDescription } = require("./catalogToolMetadata.cjs"); +const { registerMcpTools } = require("../capabilities/codegen/mcpToolRegistry.cjs"); + +function catalogDescription(toolName, fallback) { + return getCatalogToolDescription(toolName) || fallback; +} const DEBUG_MCP = process.env.NETCATTY_MCP_DEBUG === "1"; @@ -205,171 +210,13 @@ server.resource( }, ); -// Tool: get_environment -server.tool( - "get_environment", - "Get information about the current Netcatty scope: all terminal sessions exposed by Netcatty, their session IDs, OS, shell hints, and connection status. Sessions may be remote hosts, a local terminal, Mosh-backed shells, or serial port connections (network devices, embedded systems). Serial sessions have protocol 'serial' and shellType 'raw'. SSH sessions with deviceType 'network' are network equipment (Huawei VRP, Cisco IOS, etc.) using vendor CLIs instead of a standard shell. Call this first before executing commands.", - {}, - async () => { - process.stderr.write(`[netcatty-mcp] get_environment called, SCOPED_SESSION_IDS: ${JSON.stringify(SCOPED_SESSION_IDS)}, CHAT_SESSION_ID: ${CHAT_SESSION_ID}\n`); - const ctx = await rpcCall("netcatty/getContext", scopeParams); - process.stderr.write(`[netcatty-mcp] get_environment result: hostCount=${ctx.hostCount}, hosts=${JSON.stringify(ctx.hosts?.map(h => h.sessionId))}\n`); - debugLog("get_environment payload", { - hostCount: ctx?.hostCount, - hosts: Array.isArray(ctx?.hosts) ? ctx.hosts.map(h => ({ - sessionId: h.sessionId, - hostname: h.hostname, - connected: h.connected, - protocol: h.protocol, - deviceType: h.deviceType, - })) : [], - }); - return { - content: [{ - type: "text", - text: JSON.stringify(ctx, null, 2), - }], - }; - }, -); - -server.tool( - "list_attachments", - "List local files explicitly attached by the user in this Netcatty AI chat. Use this before read_attachment when the user asks about a pasted, dropped, or uploaded local file.", - {}, - async () => { - const result = await rpcCall("netcatty/listAttachments", scopeParams); - if (!result.ok) { - return { content: [{ type: "text", text: `Error: ${result.error || "Failed to list attachments"}` }], isError: true }; - } - return { content: [{ type: "text", text: JSON.stringify(result.attachments || [], null, 2) }] }; - }, -); - -server.tool( - "read_attachment", - "Read a local file explicitly attached by the user in this Netcatty AI chat. This tool only accepts file paths or filenames that were registered from user attachments; it cannot read arbitrary local files.", - { - filePath: z.string().optional().describe("Exact local attachment path shown in the prompt or returned by list_attachments."), - filename: z.string().optional().describe("Attachment filename returned by list_attachments. Use filePath when available."), - }, - async ({ filePath, filename }) => { - const result = await rpcCall("netcatty/readAttachment", { ...scopeParams, filePath, filename }); - if (!result.ok) { - return { content: [{ type: "text", text: `Error: ${result.error || "Failed to read attachment"}` }], isError: true }; - } - const payload = { - filename: result.filename, - mediaType: result.mediaType, - filePath: result.filePath, - sizeBytes: result.sizeBytes, - ...(result.text != null ? { text: result.text } : { base64Data: result.base64Data }), - }; - return { content: [{ type: "text", text: JSON.stringify(payload, null, 2) }] }; - }, -); - -// Tool: terminal_execute -server.tool( - "terminal_execute", - "Execute a short command on a Netcatty terminal session and wait for the full result. Use this only for commands expected to finish within about 60 seconds. For long-running commands such as builds, scans, log-following, or anything likely to exceed that budget, use terminal_start and then terminal_poll instead.", - { - sessionId: z.string().describe("The terminal session ID (from get_environment) to execute on."), - command: z.string().describe("The command to execute in the target session."), - }, - async ({ sessionId, command }) => { - debugLog("terminal_execute called", { sessionId, command }); - // skipBlocklist: bridge layer does session-aware blocklist (serial sessions skip shell patterns) - const guardErr = guardWriteOperation(command, { skipBlocklist: true }); - if (guardErr) { - debugLog("terminal_execute blocked locally", { sessionId, guardErr }); - return { content: [{ type: "text", text: `Error: ${guardErr}` }], isError: true }; - } - const result = await rpcCall("netcatty/exec", { ...scopeParams, sessionId, command }); - debugLog("terminal_execute result", { - sessionId, - ok: result?.ok, - error: result?.error, - exitCode: result?.exitCode, - stdoutLength: result?.stdout?.length || 0, - stderrLength: result?.stderr?.length || 0, - }); - if (!result.ok) { - return { content: [{ type: "text", text: `Error: ${result.error || "Command failed"}` }], isError: true }; - } - const parts = []; - if (result.stdout) parts.push(result.stdout); - if (result.stderr) parts.push(`[stderr] ${result.stderr}`); - // Serial/raw and network device sessions return null exitCode (vendor CLIs have no exit codes) - if (result.exitCode != null) { - parts.push(`[exit code: ${result.exitCode}]`); - } - return { content: [{ type: "text", text: parts.join("\n") }] }; - }, -); - -server.tool( - "terminal_start", - "Start a long-running command on a Netcatty terminal session without waiting for final completion. The command still runs in the visible terminal/PTTY so the user can watch live output. Prefer this whenever the command may exceed about 2 minutes, or when it streams output for an extended period, such as builds, scans, watch commands, and log-follow commands. After starting, wait at least about 30 seconds before the first terminal_poll unless you have a strong reason to check sooner.", - { - sessionId: z.string().describe("The terminal session ID (from get_environment) to execute on."), - command: z.string().describe("The command to start in the target session."), - }, - async ({ sessionId, command }) => { - const guardErr = guardWriteOperation(command, { skipBlocklist: true }); - if (guardErr) { - return { content: [{ type: "text", text: `Error: ${guardErr}` }], isError: true }; - } - const result = await rpcCall("netcatty/jobStart", { ...scopeParams, sessionId, command }); - if (!result.ok) { - return { content: [{ type: "text", text: `Error: ${result.error || "Failed to start background command"}` }], isError: true }; - } - return { - content: [{ - type: "text", - text: JSON.stringify({ - jobId: result.jobId, - sessionId: result.sessionId, - status: result.status, - startedAt: result.startedAt, - outputMode: result.outputMode, - recommendedPollIntervalMs: result.recommendedPollIntervalMs, - }, null, 2), - }], - }; - }, -); - -server.tool( - "terminal_poll", - "Poll a long-running Netcatty command that was started with terminal_start. Returns incremental output since the given offset and the current status. Use the returned nextOffset for the next poll. If outputTruncated is true, only the retained tail starting at outputBaseOffset is still available. Do not poll aggressively: wait at least about 30 seconds between polls unless the tool output explicitly justifies checking sooner. As soon as completed is true, stop polling and analyze the final result immediately.", - { - jobId: z.string().describe("The background job ID returned by terminal_start."), - offset: z.number().int().min(0).optional().describe("Character offset previously returned as nextOffset. Omit or use 0 on the first poll."), - }, - async ({ jobId, offset }) => { - const result = await rpcCall("netcatty/jobPoll", { ...scopeParams, jobId, offset: offset || 0 }); - if (!result.ok) { - return { content: [{ type: "text", text: `Error: ${result.error || "Failed to poll background command"}` }], isError: true }; - } - return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; - }, -); - -server.tool( - "terminal_stop", - "Stop a long-running Netcatty command that was started with terminal_start. This sends Ctrl+C to the running terminal job and returns its latest state.", - { - jobId: z.string().describe("The background job ID returned by terminal_start."), - }, - async ({ jobId }) => { - const result = await rpcCall("netcatty/jobStop", { ...scopeParams, jobId }); - if (!result.ok) { - return { content: [{ type: "text", text: `Error: ${result.error || "Failed to stop background command"}` }], isError: true }; - } - return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; - }, -); +// Register catalog-driven MCP tools (terminal, SFTP, attachments, vault, portforward). +registerMcpTools(server, { + rpcCall, + scopeParams, + guardWriteOperation, + catalogDescription, +}); // ── Start ── diff --git a/electron/preload/api.cjs b/electron/preload/api.cjs index feb957f7f..a5174cc21 100644 --- a/electron/preload/api.cjs +++ b/electron/preload/api.cjs @@ -934,6 +934,12 @@ function createPreloadApi(ctx) { aiCattyCancelExec: async (chatSessionId) => { return ipcRenderer.invoke("netcatty:ai:catty:cancel", { chatSessionId }); }, + aiSetChatSessionCancelled: async (chatSessionId, cancelled = true) => { + return ipcRenderer.invoke("netcatty:ai:chat-session:set-cancelled", { chatSessionId, cancelled }); + }, + aiCapability: async (rpcMethod, params, chatSessionId) => { + return ipcRenderer.invoke("netcatty:ai:capability", { rpcMethod, params, chatSessionId }); + }, aiDiscoverAgents: async (options) => { return ipcRenderer.invoke("netcatty:ai:agents:discover", options); }, @@ -977,6 +983,9 @@ function createPreloadApi(ctx) { aiMcpSetToolIntegrationMode: async (mode) => { return ipcRenderer.invoke("netcatty:ai:mcp:set-tool-integration-mode", { mode }); }, + aiMcpSyncPermissionGrants: async (grants) => { + return ipcRenderer.invoke("netcatty:ai:mcp:sync-permission-grants", { grants }); + }, aiUserSkillsGetStatus: async () => { return ipcRenderer.invoke("netcatty:ai:user-skills:status"); }, @@ -1001,6 +1010,14 @@ function createPreloadApi(ctx) { ipcRenderer.on("netcatty:ai:mcp:approval-cleared", handler); return () => ipcRenderer.removeListener("netcatty:ai:mcp:approval-cleared", handler); }, + onVaultAgentRequest: (cb) => { + const handler = (_event, payload) => cb(payload); + ipcRenderer.on("netcatty:ai:vault-agent:request", handler); + return () => ipcRenderer.removeListener("netcatty:ai:vault-agent:request", handler); + }, + respondVaultAgent: async (requestId, result) => { + return ipcRenderer.invoke("netcatty:ai:vault-agent:response", { requestId, result }); + }, // SDK external agent streaming aiSdkAgentStream: async (requestId, chatSessionId, sdkBackend, prompt, cwd, providerId, model, existingSessionId, historyMessages, images, toolIntegrationMode, defaultTargetSession, userSkillsContext, agentEnv, agentCommand) => { return ipcRenderer.invoke("netcatty:ai:sdk-agent:stream", { requestId, chatSessionId, sdkBackend, prompt, cwd, providerId, model, existingSessionId, historyMessages, images, toolIntegrationMode, defaultTargetSession, userSkillsContext, agentEnv, agentCommand }); diff --git a/electron/shared/approvalConstants.cjs b/electron/shared/approvalConstants.cjs new file mode 100644 index 000000000..3c6bb4cdb --- /dev/null +++ b/electron/shared/approvalConstants.cjs @@ -0,0 +1,10 @@ +"use strict"; + +/** Shared approval timeout constants for main-process bridges. */ +const CATTY_APPROVAL_TIMEOUT_MS = 5 * 60 * 1000; +const MCP_APPROVAL_TIMEOUT_MS = 110 * 1000; + +module.exports = { + CATTY_APPROVAL_TIMEOUT_MS, + MCP_APPROVAL_TIMEOUT_MS, +}; diff --git a/electron/shared/pathLikeCommand.cjs b/electron/shared/pathLikeCommand.cjs new file mode 100644 index 000000000..144d3e5ae --- /dev/null +++ b/electron/shared/pathLikeCommand.cjs @@ -0,0 +1,18 @@ +"use strict"; + +function isPathLikeCommand(command) { + const normalized = String(command || "").trim(); + return normalized.includes("/") || normalized.includes("\\") || /^[a-z]:/i.test(normalized); +} + +function getCommandBasename(command) { + const normalized = String(command || "").trim(); + if (!normalized) return ""; + const parts = normalized.split(/[\\/]/); + return (parts.pop() || "").toLowerCase(); +} + +module.exports = { + isPathLikeCommand, + getCommandBasename, +}; diff --git a/electron/shared/permissionGrants.cjs b/electron/shared/permissionGrants.cjs new file mode 100644 index 000000000..829add637 --- /dev/null +++ b/electron/shared/permissionGrants.cjs @@ -0,0 +1,133 @@ +"use strict"; + +/** + * Permission grant pattern matching — shared between main (MCP) and renderer. + */ + +function patternMatches(pattern, value) { + if (typeof pattern !== "string" || pattern.length === 0) return false; + if (pattern === "*") return true; + if (typeof value !== "string") return false; + + if (pattern.startsWith("host:")) { + const hostPattern = pattern.slice("host:".length); + return globOrRegexMatch(hostPattern, value); + } + + return globOrRegexMatch(pattern, value); +} + +function globOrRegexMatch(pattern, value) { + if (pattern.startsWith("/") && pattern.lastIndexOf("/") > 0) { + const lastSlash = pattern.lastIndexOf("/"); + const body = pattern.slice(1, lastSlash); + const flags = pattern.slice(lastSlash + 1); + try { + return new RegExp(body, flags).test(value); + } catch { + return false; + } + } + + if (!pattern.includes("*") && !pattern.includes("?")) { + return value === pattern; + } + + // OpenCode Wildcard.match semantics (trailing " *" allows optional args). + let escaped = pattern + .replace(/[.+^${}()|[\]\\]/g, "\\$&") + .replace(/\*/g, ".*") + .replace(/\?/g, "."); + if (escaped.endsWith(" .*")) { + escaped = `${escaped.slice(0, -3)}( .*)?`; + } + + return new RegExp(`^${escaped}$`, "s").test(value); +} + +function argsPatternMatches(argsPattern, args) { + if (!argsPattern || typeof argsPattern !== "object") return true; + if (!args || typeof args !== "object") return false; + + for (const [key, pattern] of Object.entries(argsPattern)) { + const argValue = args[key]; + if (typeof argValue === "undefined") return false; + if (!patternMatches(String(pattern), String(argValue))) return false; + } + return true; +} + +function matchPermissionGrant(rules, ctx) { + if (!Array.isArray(rules) || rules.length === 0) return null; + + const args = ctx?.args && typeof ctx.args === "object" ? ctx.args : {}; + + for (const rule of rules) { + if (!rule || typeof rule.capabilityId !== "string") continue; + + if (rule.capabilityId !== ctx?.capabilityId) continue; + + if (rule.commandPattern) { + const command = typeof args.command === "string" ? args.command : ""; + if (!patternMatches(rule.commandPattern, command)) continue; + } + + if (!argsPatternMatches(rule.argsPattern, args)) continue; + + return rule; + } + + return null; +} + +function sanitizePermissionGrants(raw) { + if (!Array.isArray(raw)) return []; + + const result = []; + for (const entry of raw) { + if (!entry || typeof entry !== "object") continue; + const capabilityId = typeof entry.capabilityId === "string" ? entry.capabilityId.trim() : ""; + if (!capabilityId) continue; + + const rule = { + id: typeof entry.id === "string" && entry.id.trim() + ? entry.id.trim().slice(0, 64) + : `grant_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`, + capabilityId, + sessionPattern: typeof entry.sessionPattern === "string" && entry.sessionPattern.trim() + ? entry.sessionPattern.trim() + : "*", + createdAt: typeof entry.createdAt === "number" && Number.isFinite(entry.createdAt) + ? entry.createdAt + : Date.now(), + }; + + if (typeof entry.commandPattern === "string" && entry.commandPattern.trim()) { + rule.commandPattern = entry.commandPattern.trim(); + } + if (entry.argsPattern && typeof entry.argsPattern === "object" && !Array.isArray(entry.argsPattern)) { + const argsPattern = {}; + for (const [key, value] of Object.entries(entry.argsPattern)) { + if (typeof value === "string" && value.trim()) { + argsPattern[key] = value.trim(); + } + } + if (Object.keys(argsPattern).length > 0) { + rule.argsPattern = argsPattern; + } + } + if (typeof entry.note === "string" && entry.note.trim()) { + rule.note = entry.note.trim().slice(0, 240); + } + + result.push(rule); + } + + return result; +} + +module.exports = { + patternMatches, + matchPermissionGrant, + sanitizePermissionGrants, +}; diff --git a/electron/shared/sdkSessionIdentity.cjs b/electron/shared/sdkSessionIdentity.cjs new file mode 100644 index 000000000..3d859fb61 --- /dev/null +++ b/electron/shared/sdkSessionIdentity.cjs @@ -0,0 +1,32 @@ +"use strict"; + +const SDK_SESSION_ID_PREFIX = "netcatty-sdk-session:"; + +function encodeSdkSessionIdentity(sessionId, sdkBackend, binPath) { + if (!sessionId || !sdkBackend) return sessionId; + const payload = { + v: 1, + id: sessionId, + backend: sdkBackend, + binPath: binPath || "", + }; + return `${SDK_SESSION_ID_PREFIX}${encodeURIComponent(JSON.stringify(payload))}`; +} + +function parseSdkSessionIdentity(value) { + const raw = String(value || "").trim(); + if (!raw.startsWith(SDK_SESSION_ID_PREFIX)) return null; + try { + const parsed = JSON.parse(decodeURIComponent(raw.slice(SDK_SESSION_ID_PREFIX.length))); + if (!parsed || parsed.v !== 1 || !parsed.id || !parsed.backend) return null; + return parsed; + } catch { + return null; + } +} + +module.exports = { + SDK_SESSION_ID_PREFIX, + encodeSdkSessionIdentity, + parseSdkSessionIdentity, +}; diff --git a/electron/shared/vaultAgentGuidance.cjs b/electron/shared/vaultAgentGuidance.cjs new file mode 100644 index 000000000..97fd77a33 --- /dev/null +++ b/electron/shared/vaultAgentGuidance.cjs @@ -0,0 +1,27 @@ +"use strict"; + +/** + * Shared Vault tool-selection guidance for MCP external agents and get_environment. + * Keep in sync with infrastructure/ai/cattyAgent/systemPrompt.ts (Catty sidebar). + */ +const VAULT_HOSTS_VS_NOTES_GUIDANCE = + "Vault → Hosts vs Vault → Notes: When the user asks to add/create/import a host " + + "(创建主机、添加主机、保存 SSH 连接凭据), use vault_hosts_create with dryRun=true first, " + + "or vault_hosts_import for known export formats (PuTTY, MobaXterm, CSV, SecureCRT, ssh_config) — " + + "NOT vault_notes_create. Extract hostname, username, password, port, group, tags, and label from the user's text; " + + "put long admin tables or remarks in the host notes field (host_notes_set / Host Details metadata), " + + "not Vault sidebar Notes. Use vault_notes_create or vault_notes_update ONLY when the user explicitly wants " + + "markdown documentation in Vault → Notes (保险箱笔记 sidebar). " + + "If vault_hosts_create or vault_hosts_import fails, report the error — do not silently create a Vault note instead."; + +function appendVaultAgentGuidance(description) { + const base = typeof description === "string" ? description.trim() : ""; + if (!base) return VAULT_HOSTS_VS_NOTES_GUIDANCE; + if (base.includes("Vault → Hosts vs Vault → Notes")) return base; + return `${base} ${VAULT_HOSTS_VS_NOTES_GUIDANCE}`; +} + +module.exports = { + VAULT_HOSTS_VS_NOTES_GUIDANCE, + appendVaultAgentGuidance, +}; diff --git a/electron/shared/vaultAgentGuidance.test.cjs b/electron/shared/vaultAgentGuidance.test.cjs new file mode 100644 index 000000000..3bd6416de --- /dev/null +++ b/electron/shared/vaultAgentGuidance.test.cjs @@ -0,0 +1,24 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { + VAULT_HOSTS_VS_NOTES_GUIDANCE, + appendVaultAgentGuidance, +} = require("./vaultAgentGuidance.cjs"); + +test("VAULT_HOSTS_VS_NOTES_GUIDANCE forbids note fallback for host creation", () => { + assert.match(VAULT_HOSTS_VS_NOTES_GUIDANCE, /vault_hosts_create/i); + assert.match(VAULT_HOSTS_VS_NOTES_GUIDANCE, /NOT vault_notes_create/i); + assert.match(VAULT_HOSTS_VS_NOTES_GUIDANCE, /do not silently create a Vault note/i); +}); + +test("appendVaultAgentGuidance appends guidance once", () => { + const once = appendVaultAgentGuidance("Netcatty terminal manager."); + assert.match(once, /Netcatty terminal manager/); + assert.match(once, /Vault → Hosts vs Vault → Notes/); + + const twice = appendVaultAgentGuidance(once); + assert.equal(twice, once); +}); diff --git a/infrastructure/ai/agentKinds.ts b/infrastructure/ai/agentKinds.ts new file mode 100644 index 000000000..9f2e0f4a1 --- /dev/null +++ b/infrastructure/ai/agentKinds.ts @@ -0,0 +1,12 @@ +/** Where an in-app agent runs — distinct from RPC/MCP/CLI capability surfaces. */ +export const AGENT_KINDS = { + /** Chat side panel (Catty). */ + SIDEBAR: 'sidebar', + /** Future app-wide agent (cross-window / proactive). */ + GLOBAL: 'global', +} as const; + +export type AgentKind = (typeof AGENT_KINDS)[keyof typeof AGENT_KINDS]; + +export const SIDEBAR_AGENT_KIND: AgentKind = AGENT_KINDS.SIDEBAR; +export const GLOBAL_AGENT_KIND: AgentKind = AGENT_KINDS.GLOBAL; diff --git a/infrastructure/ai/cattyAgent/executor.ts b/infrastructure/ai/cattyAgent/executor.ts index df01f8c02..f60616c92 100644 --- a/infrastructure/ai/cattyAgent/executor.ts +++ b/infrastructure/ai/cattyAgent/executor.ts @@ -33,6 +33,12 @@ export interface NetcattyBridge { * `aiExec` but before the main process has registered it. */ aiCattyCancelExec?(chatSessionId: string): Promise; + aiSetChatSessionCancelled?(chatSessionId: string, cancelled?: boolean): Promise<{ ok: boolean; error?: string }>; + aiCapability?( + rpcMethod: string, + params: Record, + chatSessionId?: string, + ): Promise; } // Workspace context provided to the executor diff --git a/infrastructure/ai/cattyAgent/systemPrompt.ts b/infrastructure/ai/cattyAgent/systemPrompt.ts index 6ff79f2d1..4841c7911 100644 --- a/infrastructure/ai/cattyAgent/systemPrompt.ts +++ b/infrastructure/ai/cattyAgent/systemPrompt.ts @@ -3,6 +3,7 @@ export interface SystemPromptContext { scopeLabel?: string; hosts: Array<{ sessionId: string; + hostId?: string; hostname: string; label: string; os?: string; @@ -11,6 +12,14 @@ export interface SystemPromptContext { shellType?: string; deviceType?: string; connected: boolean; + hostChain?: Array<{ hostId: string; label?: string; hostname?: string }>; + activePortForwards?: Array<{ + ruleId: string; + label?: string; + type?: string; + localPort?: number; + status?: string; + }>; }>; permissionMode: 'observer' | 'confirm' | 'autonomous'; webSearchEnabled?: boolean; @@ -42,7 +51,15 @@ ${permissionRules} 1. **Plan before acting.** When a task involves multiple steps, present a brief numbered plan to the user before executing. -2. **Use the right tool.** For normal shell commands, use \`terminal_execute\` so you receive the command output. When operating on multiple sessions, call \`terminal_execute\` for each target session. +2. **Use the right tool.** For normal shell commands, use \`terminal_execute\`. SFTP read/write, vault snippets, port forwarding, vault notes, and vault host tools are available when listed in your tool set — prefer them over manual shell workarounds. + + **Vault → Hosts (SSH connections):** When the user asks to **add/create/import a host** (创建主机、添加主机、保存服务器连接凭据), use \`vault_hosts_create\` — NOT \`vault_notes_create\`. Extract \`hostname\`, \`username\`, \`password\`, \`port\`, \`group\`, \`tags\`, and \`label\` from the user's text; put long admin tables or remarks in the host's \`notes\` field (Host Details metadata). Call with \`dryRun: true\` first to preview, then write. Only use \`vault_hosts_import\` for known export formats (PuTTY, MobaXterm, CSV, SecureCRT, ssh_config). Use \`vault_hosts_list\` to check existing hosts. + + **Vault → Notes (sidebar markdown docs):** When the user explicitly wants documentation saved to **Vault → Notes** (the notes sidebar / 保险箱笔记), use \`vault_notes_create\` or \`vault_notes_update\` — **not** \`host_notes_set\` (Host Details only) and **not** as a substitute for creating a host. + + **Never fallback:** If \`vault_hosts_create\` or \`vault_hosts_import\` fails, report the error to the user. Do **not** silently create a Vault note instead of the requested host. + + When the user pastes unstructured text with host/server info, **you** extract fields and call \`vault_hosts_create\`. When operating on multiple sessions, call \`terminal_execute\` for each target session. 3. **Never execute dangerous commands.** Commands matching the blocklist (e.g. \`rm -rf /\`, \`mkfs\`, \`dd\` to disk devices, \`shutdown\`, fork bombs, recursive chmod 777 on root) are strictly forbidden and will be automatically denied. Do not attempt to bypass these restrictions. @@ -78,6 +95,29 @@ function buildScopeDescription( } } +function formatHostChain( + hostChain: SystemPromptContext['hosts'][number]['hostChain'], +): string | null { + if (!hostChain?.length) return null; + return hostChain + .map((hop) => hop.label || hop.hostname || hop.hostId) + .join(' → '); +} + +function formatActivePortForwards( + activePortForwards: SystemPromptContext['hosts'][number]['activePortForwards'], +): string | null { + if (!activePortForwards?.length) return null; + return activePortForwards + .map((rule) => { + const label = rule.label || rule.ruleId; + const port = rule.localPort != null ? `:${rule.localPort}` : ''; + const status = rule.status ? ` (${rule.status})` : ''; + return `${label}${port}${status}`; + }) + .join(', '); +} + function buildHostList( hosts: SystemPromptContext['hosts'], ): string { @@ -87,6 +127,8 @@ function buildHostList( const lines = hosts.map(host => { const status = host.connected ? 'connected' : 'disconnected'; + const hostChain = formatHostChain(host.hostChain); + const portForwards = formatActivePortForwards(host.activePortForwards); const details = [ `hostname: ${host.hostname}`, `label: ${host.label}`, @@ -95,6 +137,8 @@ function buildHostList( host.username ? `user: ${host.username}` : null, host.shellType ? `shell: ${host.shellType}` : null, host.deviceType ? `deviceType: ${host.deviceType}` : null, + hostChain ? `hostChain: ${hostChain}` : null, + portForwards ? `portForwards: ${portForwards}` : null, `status: ${status}`, ] .filter(Boolean) diff --git a/infrastructure/ai/contextCompaction.ts b/infrastructure/ai/contextCompaction.ts index 7ef9a1f24..6efc0359d 100644 --- a/infrastructure/ai/contextCompaction.ts +++ b/infrastructure/ai/contextCompaction.ts @@ -1,5 +1,6 @@ import type { ModelMessage } from "ai"; import type { ProviderConfig } from "./types"; +import { estimateModelMessagesTokensWithKind } from "./harness/tokenEstimator"; const DEFAULT_COMPACTION_RATIO = 0.85; const TOKEN_CHARS = 4; @@ -76,10 +77,7 @@ export function sanitizeContextWindow(value: unknown): number | undefined { } export function estimateModelMessagesTokens(messages: ModelMessage[]): number { - const chars = messages.reduce((total, message) => { - return total + estimateUnknownChars(message.role) + estimateUnknownChars(message.content); - }, 0); - return Math.ceil(chars / TOKEN_CHARS); + return estimateModelMessagesTokensWithKind({ messages }).tokens; } export function estimateUnknownTokens(value: unknown): number { diff --git a/infrastructure/ai/harness/agentEventAdapter.ts b/infrastructure/ai/harness/agentEventAdapter.ts new file mode 100644 index 000000000..16dbea152 --- /dev/null +++ b/infrastructure/ai/harness/agentEventAdapter.ts @@ -0,0 +1,171 @@ +import type { AgentEvent, AgentEventListener } from './types'; + +let eventCounter = 0; + +function nextEventId(prefix: string): string { + eventCounter += 1; + return `${prefix}-${Date.now()}-${eventCounter}`; +} + +export interface StreamEventContext { + sessionId: string; + chatSessionId?: string; + turnId?: string; +} + +export interface CattyStreamChunk { + type: string; + text?: string; + textDelta?: string; + delta?: string; + toolCallId?: string; + toolName?: string; + input?: unknown; + args?: unknown; + output?: unknown; + result?: unknown; + error?: unknown; +} + +function isToolResultError(output: unknown): boolean { + if (output == null) return false; + if (typeof output === 'object') { + const obj = output as Record; + if ('error' in obj && typeof obj.error === 'string') return true; + if ('ok' in obj && obj.ok === false) return true; + } + if (typeof output === 'string') { + try { + const parsed = JSON.parse(output) as Record; + if ('error' in parsed && typeof parsed.error === 'string') return true; + if ('ok' in parsed && parsed.ok === false) return true; + } catch { + return false; + } + } + return false; +} + +export function mapSdkStreamEventToAgentEvents( + event: Record, + ctx: StreamEventContext, +): AgentEvent[] { + const base = { + sessionId: ctx.sessionId, + chatSessionId: ctx.chatSessionId, + backend: 'external-sdk' as const, + timestamp: Date.now(), + turnId: ctx.turnId, + }; + + switch (event.type) { + case 'text-delta': + return [{ + ...base, + id: nextEventId('model-delta'), + type: 'model_delta', + text: String(event.text ?? event.textDelta ?? event.delta ?? ''), + }]; + case 'thinking-delta': + case 'reasoning-delta': + return [{ + ...base, + id: nextEventId('reasoning-delta'), + type: 'reasoning_delta', + text: String(event.text ?? event.textDelta ?? event.delta ?? ''), + }]; + case 'tool-call': + return [{ + ...base, + id: nextEventId('tool-call'), + type: 'tool_call', + toolCallId: String(event.toolCallId ?? event.id ?? ''), + toolName: String(event.toolName ?? event.name ?? 'unknown'), + args: (event.args ?? event.input ?? {}) as Record, + }]; + case 'tool-result': + return [{ + ...base, + id: nextEventId('tool-result'), + type: 'tool_result', + toolCallId: String(event.toolCallId ?? ''), + toolName: typeof event.toolName === 'string' ? event.toolName : undefined, + result: String(event.result ?? event.output ?? ''), + isError: Boolean(event.isError), + }]; + case 'error': + return [{ + ...base, + id: nextEventId('error'), + type: 'error', + message: String(event.error ?? event.message ?? 'Unknown SDK error'), + recoverable: false, + }]; + default: + return []; + } +} + +export function mapCattyStreamChunkToAgentEvents( + chunk: CattyStreamChunk, + ctx: StreamEventContext, +): AgentEvent[] { + const base = { + sessionId: ctx.sessionId, + chatSessionId: ctx.chatSessionId, + backend: 'catty' as const, + timestamp: Date.now(), + turnId: ctx.turnId, + }; + + if (chunk.type === 'text' || chunk.type === 'text-delta') { + const text = chunk.text ?? chunk.textDelta ?? ''; + if (!text) return []; + return [{ ...base, id: nextEventId('model-delta'), type: 'model_delta', text }]; + } + + if (chunk.type === 'reasoning' || chunk.type === 'reasoning-start' || chunk.type === 'reasoning-delta') { + const text = chunk.text ?? chunk.textDelta ?? chunk.delta ?? ''; + if (!text) return []; + return [{ ...base, id: nextEventId('reasoning-delta'), type: 'reasoning_delta', text }]; + } + + if (chunk.type === 'tool-call' && chunk.toolCallId && chunk.toolName) { + return [{ + ...base, + id: nextEventId('tool-call'), + type: 'tool_call', + toolCallId: chunk.toolCallId, + toolName: chunk.toolName, + args: (chunk.input ?? chunk.args ?? {}) as Record, + }]; + } + + if (chunk.type === 'tool-result' && chunk.toolCallId) { + const output = chunk.output ?? chunk.result; + const resultText = typeof output === 'string' ? output : JSON.stringify(output ?? ''); + return [{ + ...base, + id: nextEventId('tool-result'), + type: 'tool_result', + toolCallId: chunk.toolCallId, + result: resultText, + isError: isToolResultError(output), + }]; + } + + if (chunk.type === 'error') { + const message = chunk.error instanceof Error + ? chunk.error.message + : String(chunk.error ?? 'Unknown stream error'); + return [{ ...base, id: nextEventId('error'), type: 'error', message }]; + } + + return []; +} + +export function createHarnessEventSink( + listener: AgentEventListener, +): AgentEventListener { + return (event) => listener(event); +} diff --git a/infrastructure/ai/harness/agentRuntime.test.ts b/infrastructure/ai/harness/agentRuntime.test.ts new file mode 100644 index 000000000..ed956472d --- /dev/null +++ b/infrastructure/ai/harness/agentRuntime.test.ts @@ -0,0 +1,68 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { AgentRuntime } from './agentRuntime'; +import { TraceStore } from './traceStore'; +import type { TurnDriver, TurnDriverContext, TurnInput } from './turnDrivers/types'; + +class MockTurnDriver implements TurnDriver { + readonly backend = 'catty' as const; + readonly runs: TurnInput[] = []; + + async run(input: TurnInput, ctx: TurnDriverContext): Promise { + this.runs.push(input); + ctx.emit({ + id: 'model-delta-1', + type: 'model_delta', + text: 'hello', + } as import('./types').AgentEvent); + if (input.signal.aborted) return; + } + + abort(): void {} +} + +test('AgentRuntime runTurn emits turn lifecycle and records trace', async () => { + const traceStore = new TraceStore(); + const driver = new MockTurnDriver(); + const runtime = new AgentRuntime({ drivers: [driver], traceStore }); + const events: string[] = []; + runtime.subscribe(event => events.push(event.type)); + + const controller = new AbortController(); + const result = await runtime.runTurn({ + backend: 'catty', + chatSessionId: 'chat-1', + sendScopeKey: 'chat-1', + userText: 'hi', + signal: controller.signal, + currentSession: undefined, + assistantMsgId: 'assistant-1', + context: { + activeProvider: undefined, + activeModelId: '', + scopeType: 'terminal', + globalPermissionMode: 'confirm', + terminalSessions: [], + autoTitleSession: () => {}, + }, + maxIterations: 5, + ui: { + addMessageToSession: () => {}, + updateLastMessage: () => {}, + updateMessageById: () => {}, + reportStreamError: () => {}, + setStreamingForScope: () => {}, + }, + }); + + assert.equal(result.reason, 'completed'); + assert.equal(driver.runs.length, 1); + assert.deepEqual(events, ['turn_start', 'model_delta', 'turn_end']); + assert.equal(traceStore.getEvents('chat-1').length, 3); +}); + +test('AgentRuntime stopTurn delegates to active driver', async () => { + const driver = new MockTurnDriver(); + const runtime = new AgentRuntime({ drivers: [driver] }); + await runtime.stopTurn('chat-2'); +}); diff --git a/infrastructure/ai/harness/agentRuntime.ts b/infrastructure/ai/harness/agentRuntime.ts new file mode 100644 index 000000000..585a77740 --- /dev/null +++ b/infrastructure/ai/harness/agentRuntime.ts @@ -0,0 +1,177 @@ +import { globalTraceStore } from './traceStore'; +import { stopAgentTurn } from './agentStop'; +import type { AgentBackend, AgentEvent, AgentEventListener } from './types'; +import { ToolOutputStore } from './toolOutputStore'; +import { ToolResultDedup } from './toolResultDedup'; +import type { TurnDriver, TurnInput, TurnResult } from './turnDrivers/types'; + +let turnCounter = 0; + +function nextTurnId(): string { + turnCounter += 1; + return `turn-${Date.now()}-${turnCounter}`; +} + +interface ActiveTurn { + turnId: string; + backend: AgentBackend; + driver: TurnDriver; +} + +export interface AgentRuntimeOptions { + drivers: TurnDriver[]; + traceStore?: typeof globalTraceStore; +} + +export class AgentRuntime { + private readonly drivers = new Map(); + private readonly listeners = new Set(); + private readonly activeTurns = new Map(); + private readonly activeTurnPromises = new Map>(); + private readonly toolOutputStores = new Map(); + private readonly traceStore: typeof globalTraceStore; + + constructor(options: AgentRuntimeOptions) { + for (const driver of options.drivers) { + this.drivers.set(driver.backend, driver); + } + this.traceStore = options.traceStore ?? globalTraceStore; + } + + getToolOutputStore(chatSessionId: string): ToolOutputStore { + let store = this.toolOutputStores.get(chatSessionId); + if (!store) { + store = new ToolOutputStore(); + this.toolOutputStores.set(chatSessionId, store); + } + return store; + } + + clearChatSession(chatSessionId: string): void { + this.toolOutputStores.get(chatSessionId)?.prune(chatSessionId); + this.toolOutputStores.delete(chatSessionId); + } + + async waitForActiveTurn(chatSessionId: string): Promise { + await this.activeTurnPromises.get(chatSessionId)?.catch(() => {}); + } + + subscribe(listener: AgentEventListener): () => void { + this.listeners.add(listener); + return () => { + this.listeners.delete(listener); + }; + } + + async runTurn(input: TurnInput): Promise { + const driver = this.drivers.get(input.backend); + if (!driver) { + throw new Error(`No TurnDriver registered for backend "${input.backend}"`); + } + + const chatSessionId = input.chatSessionId; + const priorTurn = this.activeTurnPromises.get(chatSessionId); + if (priorTurn) { + await priorTurn.catch(() => {}); + } + + const turnPromise = this.runTurnInternal(input, driver); + this.activeTurnPromises.set(chatSessionId, turnPromise); + try { + return await turnPromise; + } finally { + if (this.activeTurnPromises.get(chatSessionId) === turnPromise) { + this.activeTurnPromises.delete(chatSessionId); + } + } + } + + private async runTurnInternal(input: TurnInput, driver: TurnDriver): Promise { + const turnId = nextTurnId(); + const chatSessionId = input.chatSessionId; + const toolOutputStore = this.getToolOutputStore(chatSessionId); + const toolResultDedup = new ToolResultDedup(); + toolResultDedup.beginTurn(); + + this.activeTurns.set(chatSessionId, { + turnId, + backend: input.backend, + driver, + }); + + const emit = ( + partial: Omit + & Partial>, + ) => { + const event = { + id: partial.id, + type: partial.type, + sessionId: partial.sessionId ?? chatSessionId, + chatSessionId: partial.chatSessionId ?? chatSessionId, + backend: partial.backend ?? input.backend, + timestamp: partial.timestamp ?? Date.now(), + turnId: partial.turnId ?? turnId, + ...partial, + } as AgentEvent; + this.traceStore.append(event); + for (const listener of this.listeners) { + listener(event); + } + }; + + emit({ + id: `turn-start-${turnId}`, + type: 'turn_start', + backendLabel: input.backend === 'external-sdk' + ? ('agentConfig' in input ? input.agentConfig.name : undefined) + : 'catty', + } as AgentEvent); + + let reason: TurnResult['reason'] = 'completed'; + + try { + await driver.run(input, { + turnId, + chatSessionId, + sessionId: chatSessionId, + backend: input.backend, + signal: input.signal, + emit, + toolOutputStore, + toolResultDedup, + }); + if (input.signal.aborted) { + reason = 'aborted'; + } + } catch (err) { + reason = 'error'; + emit({ + id: `turn-error-${turnId}`, + type: 'error', + message: err instanceof Error ? err.message : String(err), + recoverable: false, + } as AgentEvent); + throw err; + } finally { + emit({ + id: `turn-end-${turnId}`, + type: 'turn_end', + reason, + } as AgentEvent); + this.activeTurns.delete(chatSessionId); + } + + return { turnId, reason }; + } + + async stopTurn(chatSessionId: string, reason: 'user' | 'slash' = 'user'): Promise { + const active = this.activeTurns.get(chatSessionId); + active?.driver.abort?.(chatSessionId); + await stopAgentTurn({ + chatSessionId, + reason, + backend: active?.backend ?? 'catty', + }); + await this.waitForActiveTurn(chatSessionId); + } +} diff --git a/infrastructure/ai/harness/agentStop.test.ts b/infrastructure/ai/harness/agentStop.test.ts new file mode 100644 index 000000000..3a5dbfea7 --- /dev/null +++ b/infrastructure/ai/harness/agentStop.test.ts @@ -0,0 +1,40 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { stopAgentTurn } from './agentStop'; +import { globalTraceStore } from './traceStore'; + +describe('stopAgentTurn', () => { + it('aborts, cancels bridge surfaces, and emits turn_end', async () => { + const calls: string[] = []; + const controller = new AbortController(); + const sessionId = 'chat-stop-test'; + + globalTraceStore.clear(sessionId); + + await stopAgentTurn({ + chatSessionId: sessionId, + abortController: controller, + bridge: { + aiCattyCancelExec: async (id) => { calls.push(`catty:${id}`); }, + aiSdkAgentCancel: async (_req, id) => { calls.push(`sdk:${id}`); return { ok: true }; }, + aiSetChatSessionCancelled: async (id, cancelled) => { + calls.push(`cancelled:${id}:${cancelled}`); + return { ok: true }; + }, + }, + reason: 'slash', + backend: 'catty', + }); + + assert.equal(controller.signal.aborted, true); + assert.deepEqual(calls, [ + `catty:${sessionId}`, + `sdk:${sessionId}`, + `cancelled:${sessionId}:true`, + ]); + + const events = globalTraceStore.getEvents(sessionId); + assert.equal(events.at(-1)?.type, 'turn_end'); + assert.equal((events.at(-1) as { reason?: string }).reason, 'aborted'); + }); +}); diff --git a/infrastructure/ai/harness/agentStop.ts b/infrastructure/ai/harness/agentStop.ts new file mode 100644 index 000000000..55e66f99f --- /dev/null +++ b/infrastructure/ai/harness/agentStop.ts @@ -0,0 +1,73 @@ +import { clearAllPendingApprovals } from '../shared/approvalGate'; +import { globalTraceStore } from './traceStore'; +import type { AgentBackend } from './types'; + +export type StopAgentTurnReason = 'user' | 'slash'; + +export interface AgentStopBridge { + aiCattyCancelExec?(chatSessionId: string): Promise; + aiSdkAgentCancel?(requestId: string, chatSessionId?: string): Promise<{ ok: boolean; error?: string }>; + aiSetChatSessionCancelled?(chatSessionId: string, cancelled?: boolean): Promise<{ ok: boolean; error?: string }>; +} + +export interface StopAgentTurnParams { + chatSessionId: string; + abortController?: AbortController | null; + bridge?: AgentStopBridge | null; + reason?: StopAgentTurnReason; + backend?: AgentBackend; +} + +let stopEventCounter = 0; + +function nextStopEventId(): string { + stopEventCounter += 1; + return `turn-end-${Date.now()}-${stopEventCounter}`; +} + +/** + * Unified stop entry for Catty, external SDK, and MCP tool surfaces. + */ +export async function stopAgentTurn({ + chatSessionId, + abortController, + bridge, + reason = 'user', + backend = 'catty', +}: StopAgentTurnParams): Promise { + abortController?.abort(); + clearAllPendingApprovals(chatSessionId); + + const tasks: Array> = []; + if (bridge?.aiCattyCancelExec) { + tasks.push(bridge.aiCattyCancelExec(chatSessionId).catch(() => {})); + } + if (bridge?.aiSdkAgentCancel) { + tasks.push(bridge.aiSdkAgentCancel('', chatSessionId).catch(() => {})); + } + if (bridge?.aiSetChatSessionCancelled) { + tasks.push(bridge.aiSetChatSessionCancelled(chatSessionId, true).catch(() => {})); + } + await Promise.all(tasks); + + globalTraceStore.append({ + id: nextStopEventId(), + type: 'turn_end', + sessionId: chatSessionId, + chatSessionId, + backend, + timestamp: Date.now(), + reason: 'aborted', + ...(reason === 'slash' ? { backendLabel: 'slash-stop' } : {}), + }); +} + +/** + * Clear the main-process cancelled flag when a new agent turn begins. + */ +export async function clearChatSessionCancelled( + chatSessionId: string, + bridge?: AgentStopBridge | null, +): Promise { + await bridge?.aiSetChatSessionCancelled?.(chatSessionId, false).catch(() => {}); +} diff --git a/infrastructure/ai/harness/capabilityTools.test.ts b/infrastructure/ai/harness/capabilityTools.test.ts new file mode 100644 index 000000000..4bd1dae8a --- /dev/null +++ b/infrastructure/ai/harness/capabilityTools.test.ts @@ -0,0 +1,31 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { resolveSessionQueueKeyForTests } from './capabilityTools'; + +describe('capabilityTools session queue keys', () => { + it('does not queue read-only harness tools behind terminal session writes', () => { + const key = resolveSessionQueueKeyForTests( + { + capabilityId: 'harness.workspace.get_session_info', + toolName: 'workspace_get_session_info', + policy: { write: false, bypassesApproval: true }, + }, + { sessionId: 'session-a' }, + 'chat-1', + ); + assert.equal(key, null); + }); + + it('still serializes terminal.execute on the same session', () => { + const key = resolveSessionQueueKeyForTests( + { + capabilityId: 'terminal.execute', + toolName: 'terminal_execute', + policy: { write: true, bypassesApproval: false }, + }, + { sessionId: 'session-a', command: 'ls' }, + 'chat-1', + ); + assert.equal(key, 'chat-1:session-a'); + }); +}); diff --git a/infrastructure/ai/harness/capabilityTools.ts b/infrastructure/ai/harness/capabilityTools.ts new file mode 100644 index 000000000..98c7d0f6c --- /dev/null +++ b/infrastructure/ai/harness/capabilityTools.ts @@ -0,0 +1,419 @@ +import { tool } from 'ai'; +import { z } from 'zod'; +import type { NetcattyBridge } from '../cattyAgent/executor'; +import type { AIPermissionMode } from '../types'; +import type { WebSearchConfig } from '../types'; +import { isWebSearchReady } from '../types'; +import { + executeTerminalExecute, + executeWorkspaceGetInfo, + executeWorkspaceGetSessionInfo, + executeWebSearch, + executeUrlFetch, + type ToolDeps, + type ToolExecResult, +} from '../shared/toolExecutors'; +import { requestApproval } from '../shared/approvalGate'; +import { reserveSessionSlot } from '../shared/sessionExecutionQueue'; +import { fitTerminalExecuteResultForModel } from './terminalCompression'; +import type { ToolOutputStore } from './toolOutputStore'; +import { + hashScopeKey, + previewToolResult, + type ToolResultDedup, +} from './toolResultDedup'; +import cattyToolSpecs from './generated/cattyToolSpecs.json'; + +type FieldShape = { + type: string; + optional?: boolean; + description?: string; +}; + +type CattyToolSpec = { + capabilityId: string; + toolName: string; + rpcMethod: string | null; + localExecution?: boolean; + description: string; + inputShape: Record; + policy: { + write: boolean; + bypassesApproval: boolean; + }; +}; + +function buildZodObject(shape: Record): z.ZodObject> { + const entries: Record = {}; + for (const [key, field] of Object.entries(shape)) { + let schema: z.ZodTypeAny = field.type === 'number' ? z.number() : z.string(); + if (field.description) { + schema = schema.describe(field.description); + } + entries[key] = field.optional ? schema.optional() : schema; + } + return z.object(entries); +} + +function unwrap(r: ToolExecResult): T | { error: string } { + if (r.ok === false) return { error: r.error }; + return r.data; +} + +async function invokeCapabilityRpc( + bridge: NetcattyBridge, + rpcMethod: string, + params: Record, + chatSessionId?: string, +): Promise { + if (!bridge.aiCapability) { + return { error: 'Capability bridge is unavailable in this environment.' }; + } + const result = await bridge.aiCapability(rpcMethod, params, chatSessionId); + if (result && typeof result === 'object' && 'ok' in result && (result as { ok: boolean }).ok === false) { + return { error: (result as { error?: string }).error || 'Capability call failed.' }; + } + return result; +} + +async function tryFetchHostEnvironment( + bridge: NetcattyBridge, + chatSessionId?: string, +): Promise | null> { + if (!bridge.aiCapability || !chatSessionId) return null; + try { + const environment = await invokeCapabilityRpc( + bridge, + 'netcatty/getContext', + {}, + chatSessionId, + ); + if (environment && typeof environment === 'object' && !('error' in environment)) { + return environment as Record; + } + } catch { + // IPC failures must not block read-only harness tools. + } + return null; +} + +function applyToolDedup( + toolName: string, + fingerprint: string, + result: unknown, + dedup?: ToolResultDedup, +): unknown { + if (!dedup) return result; + const cached = dedup.check(fingerprint); + if (cached) { + return dedup.buildCachedNotice(cached); + } + dedup.remember(toolName, fingerprint, previewToolResult(result)); + return result; +} + +interface LocalExecutionContext { + deps: ToolDeps; + spec: CattyToolSpec; + args: Record; + toolOutputStore?: ToolOutputStore; + toolResultDedup?: ToolResultDedup; + chatSessionId?: string; +} + +async function executeLocalCattyCapability(ctx: LocalExecutionContext): Promise { + const { deps, spec, args, toolOutputStore, toolResultDedup, chatSessionId } = ctx; + const resolveContext = () => (typeof deps.context === 'function' ? deps.context() : deps.context); + + switch (spec.capabilityId) { + case 'harness.tool_output.read': { + const { handleId, mode, maxChars } = args as { + handleId: string; + mode?: 'head' | 'tail' | 'full'; + maxChars?: number; + }; + if (!toolOutputStore || !chatSessionId) { + return { error: 'Tool output store is unavailable.' }; + } + const content = toolOutputStore.read({ handleId, mode, maxChars }, chatSessionId); + if (content == null) { + return { error: `Handle "${handleId}" was not found for this chat session.` }; + } + return { handleId, mode: mode ?? 'head', content }; + } + case 'harness.workspace.get_info': { + const scopeCtx = resolveContext(); + const fingerprint = toolResultDedup?.fingerprintFor( + spec.toolName, + hashScopeKey([chatSessionId, scopeCtx.workspaceId, String(scopeCtx.sessions?.length ?? 0)]), + ); + const local = executeWorkspaceGetInfo(deps); + if (local.ok === false) { + return unwrap(local); + } + let merged: unknown = local.data; + const environment = await tryFetchHostEnvironment(deps.bridge, chatSessionId); + if (environment) { + const hosts = Array.isArray(environment.hosts) + ? (environment.hosts as Array>) + : []; + const hostBySessionId = new Map(hosts.map((host) => [String(host.sessionId), host])); + merged = { + ...local.data, + sessions: local.data.sessions.map((session) => ({ + ...session, + ...(hostBySessionId.get(session.sessionId) ?? {}), + })), + activePortForwardTunnels: environment.activePortForwardTunnels, + }; + } + if (fingerprint) { + return applyToolDedup(spec.toolName, fingerprint, merged, toolResultDedup); + } + return merged; + } + case 'harness.workspace.get_session_info': { + const { sessionId } = args as { sessionId: string }; + const local = executeWorkspaceGetSessionInfo(deps, { sessionId }); + if (local.ok === false) { + return unwrap(local); + } + const environment = await tryFetchHostEnvironment(deps.bridge, chatSessionId); + if (environment) { + const hosts = Array.isArray(environment.hosts) + ? (environment.hosts as Array>) + : []; + const match = hosts.find((host) => String(host.sessionId) === sessionId); + if (match) { + return { ...local.data, ...match }; + } + } + return local.data; + } + case 'harness.web.search': { + const { query, maxResults } = args as { query: string; maxResults?: number }; + return unwrap(await executeWebSearch(deps, { query, maxResults })); + } + case 'harness.url.fetch': { + const { url, maxLength } = args as { url: string; maxLength?: number }; + const fingerprint = toolResultDedup?.fingerprintFor(spec.toolName, url); + const raw = unwrap(await executeUrlFetch(deps, { url, maxLength })); + if (fingerprint) { + return applyToolDedup(spec.toolName, fingerprint, raw, toolResultDedup); + } + return raw; + } + default: + return { error: `No local executor registered for "${spec.capabilityId}".` }; + } +} + +function resolveSessionQueueKey( + spec: CattyToolSpec, + args: Record, + chatSessionId?: string, +): string | null { + // Read-only harness tools only inspect renderer context (plus optional host + // metadata). They must not queue behind terminal.execute on the same sessionId. + if (spec.capabilityId.startsWith('harness.') && !spec.policy.write) { + return null; + } + + const sessionId = typeof args.sessionId === 'string' ? args.sessionId : undefined; + if (sessionId) { + return `${chatSessionId ?? 'global'}:${sessionId}`; + } + return `${chatSessionId ?? 'global'}:${spec.toolName}`; +} + +export function resolveSessionQueueKeyForTests( + spec: Pick, + args: Record, + chatSessionId?: string, +): string | null { + return resolveSessionQueueKey(spec as CattyToolSpec, args, chatSessionId); +} + +function createCatalogTool( + spec: CattyToolSpec, + deps: ToolDeps, + permissionMode: AIPermissionMode, + toolOutputStore?: ToolOutputStore, + toolResultDedup?: ToolResultDedup, +) { + const inputSchema = buildZodObject(spec.inputShape); + + return tool({ + description: spec.description, + inputSchema, + execute: async (args, { toolCallId, abortSignal }) => { + const queueKey = resolveSessionQueueKey( + spec, + args as Record, + deps.chatSessionId, + ); + const slot = queueKey ? reserveSessionSlot(queueKey) : null; + + try { + if ( + permissionMode === 'confirm' + && spec.policy.write + && !spec.policy.bypassesApproval + ) { + const approved = await requestApproval( + toolCallId, + spec.toolName, + args as Record, + deps.chatSessionId, + undefined, + spec.capabilityId, + ); + if (!approved) { + return { error: 'User denied tool execution.' }; + } + } + + if (abortSignal?.aborted) { + return { error: 'Tool call cancelled before it could start.' }; + } + + await slot?.ready; + + if (spec.capabilityId === 'terminal.execute') { + const { sessionId: sid, command } = args as { sessionId: string; command: string }; + const cancelOnAbort = () => { + if (deps.chatSessionId) { + void deps.bridge.aiCattyCancelExec?.(deps.chatSessionId); + } + }; + abortSignal?.addEventListener('abort', cancelOnAbort, { once: true }); + try { + const result = await executeTerminalExecute(deps, { sessionId: sid, command }); + if (result.ok === false) return unwrap(result); + return fitTerminalExecuteResultForModel({ + ...result.data, + command, + sessionId: sid, + }, { + chatSessionId: deps.chatSessionId, + toolOutputStore, + }); + } finally { + abortSignal?.removeEventListener('abort', cancelOnAbort); + } + } + + if (spec.localExecution || spec.capabilityId.startsWith('harness.')) { + return executeLocalCattyCapability({ + deps, + spec, + args: args as Record, + toolOutputStore, + toolResultDedup, + chatSessionId: deps.chatSessionId, + }); + } + + if (!spec.rpcMethod) { + return { error: `Capability "${spec.capabilityId}" has no RPC binding.` }; + } + + const raw = await invokeCapabilityRpc( + deps.bridge, + spec.rpcMethod, + args as Record, + deps.chatSessionId, + ); + + if (spec.toolName === 'get_environment' || spec.capabilityId === 'session.environment') { + const ctx = typeof deps.context === 'function' ? deps.context() : deps.context; + const fingerprint = toolResultDedup?.fingerprintFor( + spec.toolName, + hashScopeKey([deps.chatSessionId, ctx.workspaceId, String(ctx.sessions?.length ?? 0)]), + ); + if (fingerprint) { + return applyToolDedup(spec.toolName, fingerprint, raw, toolResultDedup); + } + } + + if (spec.capabilityId.includes('sftp') && spec.capabilityId.includes('read')) { + const { sessionId: sid, path } = args as { sessionId?: string; path?: string }; + const fingerprint = toolResultDedup?.fingerprintFor( + spec.toolName, + hashScopeKey([sid, path]), + ); + if (fingerprint) { + return applyToolDedup(spec.toolName, fingerprint, raw, toolResultDedup); + } + + if ( + raw + && typeof raw === 'object' + && 'content' in raw + && toolOutputStore + && deps.chatSessionId + ) { + const content = String((raw as { content?: string }).content ?? ''); + const MAX_LIVE_SFTP_READ_CHARS = 24_000; + if (content.length > MAX_LIVE_SFTP_READ_CHARS) { + const handle = toolOutputStore.store({ + chatSessionId: deps.chatSessionId, + capabilityId: spec.capabilityId, + content, + sessionId: sid, + }); + return { + ok: true, + path: (raw as { path?: string }).path ?? path, + preview: handle.preview, + totalChars: handle.totalChars, + handleId: handle.id, + note: 'Full file content stored. Use tool_output_read with this handleId to read more.', + }; + } + } + } + + return raw; + } finally { + slot?.release(); + } + }, + }); +} + +export function createCattyToolsFromCatalog( + bridge: NetcattyBridge, + context: ToolDeps['context'], + commandBlocklist?: string[], + permissionMode: AIPermissionMode = 'confirm', + webSearchConfig?: WebSearchConfig, + chatSessionId?: string, + toolOutputStore?: ToolOutputStore, + toolResultDedup?: ToolResultDedup, +) { + const deps: ToolDeps = { + bridge, + context, + commandBlocklist, + permissionMode, + webSearchConfig, + chatSessionId, + }; + + const catalogTools: Record> = {}; + for (const rawSpec of cattyToolSpecs as CattyToolSpec[]) { + if (rawSpec.capabilityId === 'harness.web.search' && !isWebSearchReady(webSearchConfig)) { + continue; + } + catalogTools[rawSpec.toolName] = createCatalogTool( + rawSpec, + deps, + permissionMode, + toolOutputStore, + toolResultDedup, + ); + } + + return catalogTools; +} diff --git a/infrastructure/ai/harness/cattyRuntime.ts b/infrastructure/ai/harness/cattyRuntime.ts new file mode 100644 index 000000000..2e3f0059d --- /dev/null +++ b/infrastructure/ai/harness/cattyRuntime.ts @@ -0,0 +1,124 @@ +import type { ModelMessage } from 'ai'; +import { generateText, pruneMessages } from 'ai'; +import { + CONTEXT_COMPACTION_SYSTEM_PROMPT, + DEFAULT_PROTECT_RECENT_MESSAGES, + formatMessagesForCompaction, + resolveContextWindow, +} from '../contextCompaction'; +import type { ProviderConfig } from '../types'; +import { + extractLatestUserGoal, + prepareTurnContext, +} from './contextManager'; +import type { AgentEventListener, CompactionTrace } from './types'; + +export interface CompactCattyMessagesInput { + messages: ModelMessage[]; + sessionId: string; + chatSessionId?: string; + provider?: Pick | null; + modelId?: string | null; + reservedTokens?: () => number; + model: Parameters[0]['model']; + abortSignal: AbortSignal; + trigger?: 'pre-turn' | '413-retry' | 'force'; + force?: boolean; + compressForRequestTooLargeRetry?: boolean; + onStatusText?: (text: string) => void; + onCompaction?: (trace: CompactionTrace) => void; + reinjection?: { + permissionMode?: import('../types').AIPermissionMode; + sessionScopeSummary?: string; + }; +} + +export interface CompactCattyMessagesResult { + messages: ModelMessage[]; + trace?: CompactionTrace; +} + +function createEventListener( + input: CompactCattyMessagesInput, +): AgentEventListener | undefined { + if (!input.onCompaction) return undefined; + return (event) => { + if (event.type === 'compaction') { + input.onCompaction?.(event.trace); + } + }; +} + +export async function compactCattyMessages( + input: CompactCattyMessagesInput, +): Promise { + const contextWindow = resolveContextWindow({ + provider: input.provider, + modelId: input.modelId, + }); + + const summarize = async (messagesToSummarize: ModelMessage[]) => { + input.onStatusText?.('Compacting earlier context...'); + const result = await generateText({ + model: input.model, + system: CONTEXT_COMPACTION_SYSTEM_PROMPT, + messages: [{ + role: 'user', + content: `Summarize this earlier conversation context for the next model turn:\n\n${formatMessagesForCompaction(messagesToSummarize)}`, + }], + abortSignal: input.abortSignal, + maxOutputTokens: 1600, + temperature: 0, + }); + return result.text; + }; + + const trigger = input.trigger ?? (input.force ? 'force' : 'pre-turn'); + + try { + const prepared = await prepareTurnContext({ + messages: input.messages, + backend: 'catty', + contextWindow, + reservedTokens: input.reservedTokens?.() ?? 0, + trigger, + force: input.force, + compressForRequestTooLargeRetry: input.compressForRequestTooLargeRetry, + protectRecentMessages: DEFAULT_PROTECT_RECENT_MESSAGES, + summarize, + sessionId: input.sessionId, + chatSessionId: input.chatSessionId, + onEvent: createEventListener(input), + reinjection: { + ...input.reinjection, + userGoal: extractLatestUserGoal(input.messages), + }, + providerId: input.provider?.providerId, + }); + return { messages: prepared.messages, trace: prepared.trace }; + } catch (err) { + if (input.abortSignal.aborted) throw err; + console.warn('[Harness] Context compaction failed; falling back to recent messages only:', err); + const fallback = await prepareTurnContext({ + messages: input.messages, + backend: 'catty', + contextWindow, + trigger: 'force', + force: true, + compressForRequestTooLargeRetry: input.compressForRequestTooLargeRetry, + protectRecentMessages: DEFAULT_PROTECT_RECENT_MESSAGES, + sessionId: input.sessionId, + chatSessionId: input.chatSessionId, + onEvent: createEventListener(input), + }); + return { messages: fallback.messages, trace: fallback.trace }; + } +} + +export function prepareCattyMessagesForStream(messages: ModelMessage[]): ModelMessage[] { + return pruneMessages({ + messages, + reasoning: 'all', + emptyMessages: 'remove', + }); +} diff --git a/infrastructure/ai/harness/contextManager.test.ts b/infrastructure/ai/harness/contextManager.test.ts new file mode 100644 index 000000000..69b4ea42f --- /dev/null +++ b/infrastructure/ai/harness/contextManager.test.ts @@ -0,0 +1,102 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import type { ModelMessage } from 'ai'; +import { prepareTurnContext } from './contextManager.ts'; +import { TraceStore } from './traceStore.ts'; + +test('prepareTurnContext applies typed compression before LLM summarize threshold', async () => { + const longOutput = 'line\n'.repeat(20_000); + const messages: ModelMessage[] = [ + { + role: 'user', + content: 'Check nginx error logs on prod-web-01 and summarize failures.', + }, + { + role: 'assistant', + content: [{ + type: 'tool-call', + toolCallId: 'call-1', + toolName: 'terminal_execute', + input: { sessionId: 'sess-1', command: 'tail -n 500 /var/log/nginx/error.log' }, + }], + }, + { + role: 'tool', + content: [{ + type: 'tool-result', + toolCallId: 'call-1', + toolName: 'terminal_execute', + output: { type: 'text', value: longOutput }, + }], + }, + { + role: 'assistant', + content: 'Found repeated upstream timeout errors.', + }, + { + role: 'user', + content: 'Fix only the upstream timeout issue, do not restart nginx yet.', + }, + ]; + + const traces: string[] = []; + const prepared = await prepareTurnContext({ + messages, + backend: 'catty', + contextWindow: 128_000, + trigger: 'pre-turn', + sessionId: 'chat-1', + onEvent: (event) => { + if (event.type === 'compaction') traces.push(event.trace.trigger); + }, + reinjection: { + permissionMode: 'confirm', + userGoal: 'Fix upstream timeout without restarting nginx.', + }, + }); + + assert.ok(prepared.messages.length >= messages.length); + const serialized = JSON.stringify(prepared.messages); + assert.match(serialized, /Fix only the upstream timeout issue/); + assert.match(serialized, /Permission mode: confirm/); + assert.ok(serialized.length < JSON.stringify(messages).length); +}); + +test('prepareTurnContext force trigger retains recent user goal in replay', async () => { + const messages: ModelMessage[] = Array.from({ length: 40 }, (_, index) => ({ + role: index % 2 === 0 ? 'user' : 'assistant', + content: index === 38 + ? 'SSH into db-01 and inspect /var/log/postgresql/postgresql.log for crash signatures.' + : `filler message ${index}`, + })) as ModelMessage[]; + + const prepared = await prepareTurnContext({ + messages, + backend: 'catty', + contextWindow: 128_000, + trigger: 'force', + force: true, + sessionId: 'chat-2', + }); + + const serialized = JSON.stringify(prepared.messages); + assert.match(serialized, /SSH into db-01/); + assert.match(serialized, /postgresql\.log/); +}); + +test('TraceStore records compaction events for export', async () => { + const store = new TraceStore(); + await prepareTurnContext({ + messages: [{ role: 'user', content: 'x'.repeat(500_000) }], + backend: 'catty', + contextWindow: 1000, + trigger: 'force', + force: true, + sessionId: 'chat-3', + onEvent: (event) => store.append(event), + }); + + const exported = store.exportTrace('chat-3'); + assert.ok(exported.compactions.length >= 1); + assert.equal(exported.compactions[0]?.trigger, 'force'); +}); diff --git a/infrastructure/ai/harness/contextManager.ts b/infrastructure/ai/harness/contextManager.ts new file mode 100644 index 000000000..138477711 --- /dev/null +++ b/infrastructure/ai/harness/contextManager.ts @@ -0,0 +1,251 @@ +import type { ModelMessage } from 'ai'; +import type { ChatMessage, AIPermissionMode } from '../types'; +import { + DEFAULT_CONTEXT_WINDOW_TOKENS, + DEFAULT_PROTECT_RECENT_MESSAGES, + keepRecentContextMessages, + prepareContextCompaction, +} from '../contextCompaction'; +import { compressMessagesForRequestTooLargeRetry } from '../requestPayloadCompression'; +import { estimateModelMessagesTokensWithKind } from './tokenEstimator'; +import type { PrepareStepContextInput } from './turnDrivers/types'; +import type { + AgentEventListener, + CompactionTrace, + ContextPrepareResult, + ContextPrepareTrigger, + ExternalBridgeHistoryMessage, +} from './types'; +import { buildExternalBridgeContextMessages } from './externalBridgeContext'; + +export interface PrepareTurnContextInput { + messages: ModelMessage[]; + backend: 'catty' | 'external-bridge'; + contextWindow?: number; + reservedTokens?: number; + trigger: ContextPrepareTrigger; + protectRecentMessages?: number; + force?: boolean; + compressForRequestTooLargeRetry?: boolean; + summarize?: (messagesToSummarize: ModelMessage[]) => Promise; + onEvent?: AgentEventListener; + sessionId?: string; + chatSessionId?: string; + providerId?: string | null; + reinjection?: PostCompactReinjection; +} + +export interface PostCompactReinjection { + permissionMode?: AIPermissionMode; + sessionScopeSummary?: string; + userGoal?: string; + pendingToolHandleIds?: string[]; +} + +function emitCompactionEvent( + onEvent: AgentEventListener | undefined, + input: PrepareTurnContextInput, + trace: CompactionTrace, +): void { + if (!onEvent || !input.sessionId) return; + onEvent({ + id: `compaction-${Date.now()}`, + type: 'compaction', + sessionId: input.sessionId, + chatSessionId: input.chatSessionId, + backend: input.backend === 'catty' ? 'catty' : 'external-sdk', + timestamp: Date.now(), + trace, + }); +} + +function applyTypedMessageCompression(messages: ModelMessage[]): { + messages: ModelMessage[]; + didAdjust: boolean; +} { + const compressed = compressMessagesForRequestTooLargeRetry(messages); + return { messages: compressed.messages, didAdjust: compressed.didAdjust }; +} + +function buildReinjectionMessages(reinjection?: PostCompactReinjection): ModelMessage[] { + if (!reinjection) return []; + const lines: string[] = ['[Netcatty session context — preserved after compaction]']; + if (reinjection.permissionMode) { + lines.push(`Permission mode: ${reinjection.permissionMode}`); + } + if (reinjection.sessionScopeSummary) { + lines.push(reinjection.sessionScopeSummary); + } + if (reinjection.userGoal) { + lines.push(`Current user goal: ${reinjection.userGoal}`); + } + if (reinjection.pendingToolHandleIds?.length) { + lines.push(`Unresolved tool output handles: ${reinjection.pendingToolHandleIds.join(', ')}`); + } + if (lines.length <= 1) return []; + return [{ + role: 'user', + content: lines.join('\n'), + }]; +} + +export async function prepareTurnContext( + input: PrepareTurnContextInput, +): Promise { + const contextWindow = input.contextWindow ?? DEFAULT_CONTEXT_WINDOW_TOKENS; + const protectRecent = input.protectRecentMessages ?? DEFAULT_PROTECT_RECENT_MESSAGES; + const tokensBeforeResult = estimateModelMessagesTokensWithKind({ + messages: input.messages, + providerId: input.providerId, + }); + const tokensBefore = tokensBeforeResult.tokens; + const estimatorKind = tokensBeforeResult.estimatorKind; + + let working = input.messages; + let didAdjust = false; + let didTypedCompression = false; + let didLlmSummarize = false; + let did413Fallback = false; + let summaryLength: number | undefined; + let compressedMessageCount = 0; + let retainedTailCount = working.length; + + if (input.compressForRequestTooLargeRetry || input.trigger === '413-retry') { + const typed = applyTypedMessageCompression(working); + working = typed.messages; + didTypedCompression = typed.didAdjust; + did413Fallback = typed.didAdjust; + didAdjust = didAdjust || typed.didAdjust; + } else { + const typed = applyTypedMessageCompression(working); + if (typed.didAdjust) { + working = typed.messages; + didTypedCompression = true; + didAdjust = true; + } + } + + if (input.summarize) { + const compacted = await prepareContextCompaction({ + messages: working, + contextWindow, + reservedTokens: input.reservedTokens ?? 0, + thresholdRatio: input.force || input.trigger === 'force' ? 0 : undefined, + protectRecentMessages: protectRecent, + summarize: input.summarize, + }); + + if (compacted.didCompact) { + working = compacted.messages; + didLlmSummarize = true; + didAdjust = true; + summaryLength = compacted.summary?.length; + compressedMessageCount = Math.max(0, input.messages.length - protectRecent); + retainedTailCount = protectRecent; + } else if (input.force || input.trigger === '413-retry' || input.trigger === 'force') { + working = keepRecentContextMessages(working, protectRecent); + didAdjust = true; + retainedTailCount = working.length; + } + } else if (input.force || input.trigger === '413-retry' || input.trigger === 'force') { + working = keepRecentContextMessages(working, protectRecent); + didAdjust = true; + retainedTailCount = working.length; + } + + const reinjection = buildReinjectionMessages(input.reinjection); + if (reinjection.length > 0) { + working = [...reinjection, ...working]; + didAdjust = true; + } + + const tokensAfter = estimateModelMessagesTokensWithKind({ + messages: working, + providerId: input.providerId, + }).tokens; + + if (didAdjust) { + const trace: CompactionTrace = { + trigger: input.trigger, + estimatedTokensBefore: tokensBefore, + estimatedTokensAfter: tokensAfter, + messagesBefore: input.messages.length, + messagesAfter: working.length, + compressedMessageCount, + retainedTailCount, + summaryLength, + didTypedCompression, + didLlmSummarize, + did413Fallback, + estimatorKind, + }; + emitCompactionEvent(input.onEvent, input, trace); + return { messages: working, didAdjust: true, trace }; + } + + return { messages: working, didAdjust: false }; +} + +export function buildExternalBridgeContext( + messages: ChatMessage[], +): ExternalBridgeHistoryMessage[] { + return buildExternalBridgeContextMessages(messages); +} + +export function extractLatestUserGoal(messages: ModelMessage[] | ChatMessage[]): string | undefined { + for (let i = messages.length - 1; i >= 0; i -= 1) { + const message = messages[i]; + if (message.role !== 'user') continue; + const content = typeof message.content === 'string' + ? message.content.trim() + : ''; + if (content) return content.slice(0, 500); + } + return undefined; +} + +/** Step-level typed pruning — no LLM summarize (reserved for pre-turn / 413). */ +export async function prepareStepContext( + input: PrepareStepContextInput, +): Promise { + const typed = compressMessagesForRequestTooLargeRetry(input.messages); + let working = typed.messages; + let didAdjust = typed.didAdjust; + + const pendingHandles = input.toolOutputStore?.listPendingHandles(input.chatSessionId ?? input.sessionId) ?? []; + if (pendingHandles.length > 0 && input.stepNumber > 0) { + const notice: ModelMessage = { + role: 'user', + content: `[step ${input.stepNumber}] Tool output handles available: ${pendingHandles.map(h => h.id).join(', ')}`, + }; + working = [notice, ...working]; + didAdjust = true; + } + + const before = estimateModelMessagesTokensWithKind({ + messages: input.messages, + providerId: input.providerId, + }); + const after = estimateModelMessagesTokensWithKind({ + messages: working, + providerId: input.providerId, + }); + + return { + messages: working, + didAdjust, + trace: didAdjust ? { + trigger: 'pre-turn', + estimatedTokensBefore: before.tokens, + estimatedTokensAfter: after.tokens, + messagesBefore: input.messages.length, + messagesAfter: working.length, + compressedMessageCount: 0, + retainedTailCount: working.length, + didTypedCompression: typed.didAdjust, + didLlmSummarize: false, + did413Fallback: false, + estimatorKind: before.estimatorKind, + } : undefined, + }; +} diff --git a/infrastructure/ai/harness/contextReplay.test.ts b/infrastructure/ai/harness/contextReplay.test.ts new file mode 100644 index 000000000..cbfa974e4 --- /dev/null +++ b/infrastructure/ai/harness/contextReplay.test.ts @@ -0,0 +1,37 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import type { ModelMessage } from 'ai'; +import { prepareTurnContext, extractLatestUserGoal } from './contextManager'; + +const fixtureMessages: ModelMessage[] = [ + { role: 'user', content: 'Deploy nginx on prod-web-01 and verify port 443.' }, + { role: 'assistant', content: 'I will check the server first.' }, + { role: 'user', content: 'Command failed: systemctl restart nginx returned exit code 1.' }, + { role: 'assistant', content: 'The error log shows missing ssl_certificate path.' }, +]; + +test('context replay compaction retains user goal and recent tail', async () => { + const goal = extractLatestUserGoal(fixtureMessages); + assert.ok(goal?.includes('Command failed')); + + const prepared = await prepareTurnContext({ + messages: [...fixtureMessages], + backend: 'catty', + contextWindow: 500, + reservedTokens: 100, + trigger: 'force', + force: true, + protectRecentMessages: 2, + providerId: 'anthropic', + reinjection: { + userGoal: goal, + permissionMode: 'confirm', + }, + }); + + assert.equal(prepared.didAdjust, true); + assert.ok(prepared.messages.length <= fixtureMessages.length + 1); + const joined = JSON.stringify(prepared.messages); + assert.match(joined, /Deploy nginx|Command failed|ssl_certificate/); + assert.ok(prepared.trace?.estimatorKind); +}); diff --git a/infrastructure/ai/harness/externalBridgeContext.ts b/infrastructure/ai/harness/externalBridgeContext.ts new file mode 100644 index 000000000..51555ef70 --- /dev/null +++ b/infrastructure/ai/harness/externalBridgeContext.ts @@ -0,0 +1,445 @@ +import type { ChatMessage } from '../types'; +import type { ExternalBridgeHistoryMessage } from './types'; +import { buildHistoricalToolResultReplayText, buildHistoricalUserReplayContent } from '../../../components/ai/cattyHistoryReplay'; + +type ExternalAgentHistoryMessage = ExternalBridgeHistoryMessage; +type RawHistoryMessage = ExternalAgentHistoryMessage & { sourceId: string }; +type DurableUserLine = { + line: string; + messageIndex: number; + priority: number; +}; + +const MAX_RECENT_RAW_MESSAGES = 6; +const MAX_MESSAGES_TO_SCAN = 20; +// Bound the scan by user turns, not raw message count: a tool-heavy external agent +// chat can produce 5+ messages per logical turn (user + assistant + +// several tool_results + follow-up assistant), so a plain +// message-count cap ages out early constraints much sooner than intended. +const MAX_DURABLE_SCAN_TURNS = 100; +const MAX_COMPACT_CONTEXT_CHARS = 3000; +const MAX_RAW_MESSAGE_CHARS = 2000; +const MAX_TOOL_SUMMARY_CHARS = 500; +const MAX_DURABLE_USER_CONTEXT_CHARS = 1400; +const MAX_DURABLE_ASSISTANT_CONTEXT_CHARS = 900; +const MAX_RECENT_SUMMARY_CONTEXT_CHARS = 1200; +const MAX_DURABLE_USER_MESSAGE_CHARS = 280; +const MAX_DURABLE_ASSISTANT_MESSAGE_CHARS = 360; +const MAX_TOOL_CALL_LABEL_CHARS = 200; + +type ToolCallInfo = { name: string; arguments: unknown }; + +const IMPORTANT_PATTERNS = [ + /不要|别|不能|不允许|必须|希望|只|最小|先|暂时|fallback|pwsh|powershell|cmd\.exe|windows|mcp|skills|cli|commit|\bpr\b|打包|内存|历史|压缩|慢/i, + /error|failed|failure|exit code|exception|cannot|unable|timeout|crash|fallback|commit|pull request|PR #\d+/i, +]; +const DURABLE_CONSTRAINT_PATTERNS = [ + /\bdo not\b|\bdon't\b|\bkeep\b|\bpreserve\b|\bavoid\b|\bonly\b|\bunchanged\b|\blocal only\b|\bwithout\b|\bleave\b/i, + /不要|别|保留|保持|维持|不改|别改|不要改|仅限本地/i, +]; +const TRIVIAL_USER_MESSAGE_PATTERNS = [ + /^(ok|okay|yes|no|thanks|thank you|continue|继续|好的|收到|行|嗯|好|继续处理|继续吧|开始吧)[.!? ]*$/i, +]; +const TRIVIAL_ASSISTANT_MESSAGE_PATTERNS = [ + /^(ok|okay|understood|got it|working|proceeding|ready|ack(?: \d+)?|收到|明白|继续处理|准备实现|开始处理|处理中)[.!? ]*$/i, +]; + +function truncateText(value: string, maxChars: number): string { + if (value.length <= maxChars) return value; + return `${value.slice(0, Math.max(0, maxChars - 24)).trimEnd()}\n[truncated]`; +} + +function normalizeWhitespace(value: string): string { + return value.replace(/\s+/g, " ").trim(); +} + +function isImportantText(value: string): boolean { + return IMPORTANT_PATTERNS.some((pattern) => pattern.test(value)); +} + +function isDurableConstraintText(value: string): boolean { + return DURABLE_CONSTRAINT_PATTERNS.some((pattern) => pattern.test(value)); +} + +function getUserHistoryContent(message: ChatMessage): string { + if (message.role !== "user") return message.content || ""; + return buildHistoricalUserReplayContent( + message.content || "", + message.attachments ?? [], + ); +} + +function isTrivialUserMessage(value: string): boolean { + const normalized = normalizeWhitespace(value); + if (isImportantText(normalized) || isDurableConstraintText(normalized)) return false; + // Don't blanket-drop short messages — short user turns are often + // load-bearing constraints ("Use ssh2", "中文输出", "no logs", "more + // verbose") that the IMPORTANT/DURABLE regexes can't realistically + // enumerate. The trivial-phrase regex already catches actual filler + // ("ok", "yes", "thanks", "继续"). + return TRIVIAL_USER_MESSAGE_PATTERNS.some((pattern) => pattern.test(normalized)); +} + +function getDurableUserPriority(value: string): number { + const normalized = normalizeWhitespace(value); + if (isImportantText(normalized) || isDurableConstraintText(normalized)) return 2; + return 1; +} + +function isSubstantiveAssistantMessage(value: string): boolean { + const normalized = normalizeWhitespace(value); + if (!normalized) return false; + // Mirror the user-side loosening: don't blanket-drop short assistant + // messages just because they're under 40 chars or don't match the small + // English keyword list. Short but load-bearing decisions ("Use ssh2", + // "rebase instead", "中文输出") aren't realistically enumerable and + // they're the exact things a later "do what you suggested" references. + // TRIVIAL_ASSISTANT_MESSAGE_PATTERNS still catches the actual filler + // ("ok", "ack", "got it", "明白"). + return !TRIVIAL_ASSISTANT_MESSAGE_PATTERNS.some((pattern) => pattern.test(normalized)); +} + +function getDurableAssistantPriority(value: string): number { + const normalized = normalizeWhitespace(value); + if (isImportantText(normalized)) return 2; + return 1; +} + +function appendUniqueLine( + target: string[], + seen: Set, + line: string, + maxSectionChars: number, + sectionCharsRef: { value: number }, +): void { + const normalized = normalizeWhitespace(line); + if (!normalized || seen.has(normalized)) return; + const nextChars = sectionCharsRef.value + normalized.length; + if (nextChars > maxSectionChars) return; + seen.add(normalized); + target.push(normalized); + sectionCharsRef.value = nextChars; +} + +function summarizeToolMessage( + message: ChatMessage, + toolCallIndex: Map, +): string[] { + if (!message.toolResults?.length) return []; + return message.toolResults.map((result) => { + const prefix = result.isError ? "Tool error" : "Tool result"; + // Same provenance problem as the raw-window path: once a tool result + // lands in the compact section (older than the 6-item raw window), + // its paired assistant tool_call is almost always gone. Without the + // call label, multiple older results collapse into indistinguishable + // "Tool result (callN): ..." lines and follow-ups like "use the + // resolv.conf output" can't be resolved. Inline the name+args here + // the same way toRawHistoryMessage does. + const callInfo = lookupToolCallInfo(toolCallIndex, message.id, result.toolCallId); + const callLabel = callInfo + ? ` [from ${callInfo.name}(${truncateText(JSON.stringify(callInfo.arguments ?? {}), MAX_TOOL_CALL_LABEL_CHARS)})]` + : ""; + const replayContent = buildHistoricalToolResultReplayText(result, callInfo + ? { id: result.toolCallId, name: callInfo.name, arguments: callInfo.arguments as Record } + : undefined); + return `${prefix}${callLabel} (${result.toolCallId}): ${truncateText(normalizeWhitespace(replayContent), MAX_TOOL_SUMMARY_CHARS)}`; + }); +} + +function summarizeMessage( + message: ChatMessage, + toolCallIndex: Map, +): string[] { + if (message.role === "system") return []; + if (message.role === "tool") return summarizeToolMessage(message, toolCallIndex); + + const lines: string[] = []; + const content = getUserHistoryContent(message); + if (content && isImportantText(content)) { + const label = message.role === "user" ? "User" : "Assistant"; + lines.push(`${label}: ${truncateText(normalizeWhitespace(content), MAX_TOOL_SUMMARY_CHARS)}`); + } + + if (message.role === "assistant" && message.toolCalls?.length) { + for (const toolCall of message.toolCalls) { + const args = JSON.stringify(toolCall.arguments ?? {}); + const summary = `Tool call: ${toolCall.name}(${truncateText(args, 220)})`; + if (isImportantText(summary)) lines.push(summary); + } + } + + return lines; +} + +function summarizeDurableUserMessage(message: ChatMessage): string | null { + if (message.role !== "user") return null; + const content = getUserHistoryContent(message); + if (!content) return null; + if (isTrivialUserMessage(content)) return null; + return `User request: ${truncateText(normalizeWhitespace(content), MAX_DURABLE_USER_MESSAGE_CHARS)}`; +} + +function summarizeDurableAssistantMessage(message: ChatMessage): string | null { + if (message.role !== "assistant" || !message.content) return null; + if (!isSubstantiveAssistantMessage(message.content)) return null; + return `Assistant context: ${truncateText(normalizeWhitespace(message.content), MAX_DURABLE_ASSISTANT_MESSAGE_CHARS)}`; +} + +/** + * Build a per-tool-result provenance index. Keys are + * `${toolResultMessageId}:${toolCallId}` rather than the bare toolCall.id + * so that provider-reused ids (e.g. "call1" across unrelated turns) don't + * cause later calls to overwrite older ones in the lookup — each + * tool_result resolves to the most recent assistant tool_call that + * preceded it with matching id, which preserves historical correctness + * when rebuilding older compact summaries. + */ +function buildToolCallIndex(messages: ChatMessage[]): Map { + const provenance = new Map(); + // Rolling map of the latest tool_call seen (by id) up to the current + // point in the message stream. + const latestByCallId = new Map(); + for (const message of messages) { + if (message.role === "assistant" && message.toolCalls?.length) { + for (const toolCall of message.toolCalls) { + if (!toolCall.id) continue; + latestByCallId.set(toolCall.id, { name: toolCall.name, arguments: toolCall.arguments }); + } + continue; + } + if (message.role === "tool" && message.toolResults?.length) { + for (const result of message.toolResults) { + const info = latestByCallId.get(result.toolCallId); + if (info) { + provenance.set(`${message.id}:${result.toolCallId}`, info); + } + } + } + } + return provenance; +} + +function lookupToolCallInfo( + index: Map, + toolMessageId: string, + toolCallId: string, +): ToolCallInfo | undefined { + return index.get(`${toolMessageId}:${toolCallId}`); +} + +function toRawHistoryMessage( + message: ChatMessage, + toolCallIndex: Map, +): RawHistoryMessage[] { + if (message.role === "user") { + const content = getUserHistoryContent(message); + return content + ? [{ sourceId: message.id, role: "user", content: truncateText(content, MAX_RAW_MESSAGE_CHARS) }] + : []; + } + + if (message.role === "assistant") { + const parts: string[] = []; + if (message.content) parts.push(message.content); + if (message.toolCalls?.length) { + parts.push(...message.toolCalls.map((tc) => `Tool call: ${tc.name}(${JSON.stringify(tc.arguments ?? {})})`)); + } + return parts.length + ? [{ sourceId: message.id, role: "assistant", content: truncateText(parts.join("\n\n"), MAX_RAW_MESSAGE_CHARS) }] + : []; + } + + if (message.role === "tool" && message.toolResults?.length) { + // Keep recent tool results self-describing while replacing terminal + // output with placeholders, so stale-session recovery doesn't replay + // bulky command output on every follow-up. External agent replay only + // supports user/assistant roles, so we flatten to "assistant" — the + // tool results were produced during the assistant's turn. + // + // Inline the originating tool_call's name+args. Tool calls and their + // results live in separate messages; if the last six raw items start + // in the middle of a tool interaction, the preceding assistant tool + // call can be outside the window. Without the call label the result + // is opaque bytes and "use that output" becomes ambiguous. + const parts = message.toolResults.map((result) => { + const prefix = result.isError ? "Tool error" : "Tool result"; + const callInfo = lookupToolCallInfo(toolCallIndex, message.id, result.toolCallId); + const callLabel = callInfo + ? ` [from ${callInfo.name}(${truncateText(JSON.stringify(callInfo.arguments ?? {}), MAX_TOOL_CALL_LABEL_CHARS)})]` + : ""; + const replayContent = buildHistoricalToolResultReplayText(result, callInfo + ? { id: result.toolCallId, name: callInfo.name, arguments: callInfo.arguments as Record } + : undefined); + return `${prefix}${callLabel} (${result.toolCallId}): ${replayContent}`; + }); + return [{ + sourceId: message.id, + role: "assistant", + content: truncateText(parts.join("\n\n"), MAX_RAW_MESSAGE_CHARS), + }]; + } + + return []; +} + +function buildCompactContext( + messages: ChatMessage[], + durableScanStart: number, + recentRawSourceIds: Set, + toolCallIndex: Map, +): ExternalAgentHistoryMessage[] { + const scanned = messages.slice(-MAX_MESSAGES_TO_SCAN); + const summaryLines: string[] = []; + const durableUserCandidates: DurableUserLine[] = []; + const selectedDurableUserLines: DurableUserLine[] = []; + const durableAssistantCandidates: DurableUserLine[] = []; + const selectedDurableAssistantLines: DurableUserLine[] = []; + const seen = new Set(); + const durableChars = { value: 0 }; + const durableAssistantChars = { value: 0 }; + const summaryChars = { value: 0 }; + + for (let messageIndex = durableScanStart; messageIndex < messages.length; messageIndex += 1) { + const message = messages[messageIndex]; + if (recentRawSourceIds.has(message.id)) continue; + const durableUserLine = summarizeDurableUserMessage(message); + if (durableUserLine) { + durableUserCandidates.push({ + line: durableUserLine, + messageIndex, + priority: getDurableUserPriority(getUserHistoryContent(message)), + }); + } + const durableAssistantLine = summarizeDurableAssistantMessage(message); + if (durableAssistantLine) { + durableAssistantCandidates.push({ + line: durableAssistantLine, + messageIndex, + priority: getDurableAssistantPriority(message.content || ""), + }); + } + } + + durableUserCandidates + .sort((left, right) => right.priority - left.priority || right.messageIndex - left.messageIndex) + .forEach((candidate) => { + const normalized = normalizeWhitespace(candidate.line); + if (!normalized || seen.has(normalized)) return; + const nextChars = durableChars.value + normalized.length; + if (nextChars > MAX_DURABLE_USER_CONTEXT_CHARS) return; + seen.add(normalized); + selectedDurableUserLines.push(candidate); + durableChars.value = nextChars; + }); + + durableAssistantCandidates + .sort((left, right) => right.priority - left.priority || right.messageIndex - left.messageIndex) + .forEach((candidate) => { + const normalized = normalizeWhitespace(candidate.line); + if (!normalized || seen.has(normalized)) return; + const nextChars = durableAssistantChars.value + normalized.length; + if (nextChars > MAX_DURABLE_ASSISTANT_CONTEXT_CHARS) return; + seen.add(normalized); + selectedDurableAssistantLines.push(candidate); + durableAssistantChars.value = nextChars; + }); + + const durableUserLines = selectedDurableUserLines + .sort((left, right) => left.messageIndex - right.messageIndex) + .map((candidate) => candidate.line); + const durableAssistantLines = selectedDurableAssistantLines + .sort((left, right) => left.messageIndex - right.messageIndex) + .map((candidate) => candidate.line); + + for (const line of [...durableUserLines, ...durableAssistantLines]) { + seen.add(normalizeWhitespace(line)); + } + + // Skip messages that are already appended verbatim in the raw window — + // otherwise the same last-6 turns get summarized here AND re-sent as + // raw, doubling the budget cost of important user turns / large tool + // output and crowding out older durable context the replay is meant + // to preserve. Matches the recentRawSourceIds skip in the durable pass. + for (const message of scanned) { + if (recentRawSourceIds.has(message.id)) continue; + for (const line of summarizeMessage(message, toolCallIndex)) { + appendUniqueLine(summaryLines, seen, line, MAX_RECENT_SUMMARY_CONTEXT_CHARS, summaryChars); + } + } + + if (!durableUserLines.length && !durableAssistantLines.length && !summaryLines.length) return []; + + const contentLines = [ + "[Compact prior Netcatty UI context]", + "The external SDK agent may already have its own persisted session context. Use this compact Netcatty UI context only as fallback/background, and prefer the current user request when there is any conflict.", + ]; + if (durableUserLines.length) { + contentLines.push("Earlier user requests that may still apply:"); + contentLines.push(...durableUserLines.map((line) => `- ${line}`)); + } + if (durableAssistantLines.length) { + contentLines.push("Earlier assistant context that may still matter:"); + contentLines.push(...durableAssistantLines.map((line) => `- ${line}`)); + } + if (summaryLines.length) { + contentLines.push("Recent noteworthy context:"); + contentLines.push(...summaryLines.map((line) => `- ${line}`)); + } + + return [{ + role: "user", + content: truncateText( + contentLines.join("\n"), + MAX_COMPACT_CONTEXT_CHARS, + ), + }]; +} + +/** + * Find the index of the first message to include in the scan window, + * bounded by MAX_DURABLE_SCAN_TURNS user turns (not raw message count). + * Walking backwards stops at the target turn count, so the cost is + * bounded even when the transcript is huge. + */ +function computeDurableScanStart(messages: ChatMessage[]): number { + let userTurns = 0; + for (let i = messages.length - 1; i >= 0; i -= 1) { + if (messages[i].role === "user") { + userTurns += 1; + if (userTurns >= MAX_DURABLE_SCAN_TURNS) return i; + } + } + return 0; +} + +export function buildExternalBridgeContextMessages(messages: ChatMessage[]): ExternalBridgeHistoryMessage[] { + // Compute the scan start once, then do all subsequent work over the + // already-sliced tail. This avoids O(N) walks over the whole transcript + // on every send — previously buildToolCallIndex + the flatMap-to-take- + // last-6 raw history both traversed every message in the chat. + const durableScanStart = computeDurableScanStart(messages); + const scannedTail = messages.slice(durableScanStart); + + // The tool-call provenance index only needs entries for tool_results + // that might appear in our output. Building from the scanned tail is + // correct for any tool_result whose paired assistant tool_call is + // also within the window, which covers >99% of realistic patterns + // (tool_calls and tool_results are always adjacent or near-adjacent). + // If an ancient tool_call's result stays within the window while the + // call itself is outside, that single result loses its [from X(Y)] + // label — an acceptable trade for eliminating the per-send O(N) walk. + const toolCallIndex = buildToolCallIndex(scannedTail); + + const rawHistory = scannedTail + .flatMap((message) => toRawHistoryMessage(message, toolCallIndex)) + .slice(-MAX_RECENT_RAW_MESSAGES); + const compactContext = buildCompactContext( + messages, + durableScanStart, + new Set(rawHistory.map((message) => message.sourceId)), + toolCallIndex, + ); + const recentRaw = rawHistory.map(({ role, content }) => ({ role, content })); + + return [...compactContext, ...recentRaw]; +} \ No newline at end of file diff --git a/infrastructure/ai/harness/generated/cattyToolSpecs.json b/infrastructure/ai/harness/generated/cattyToolSpecs.json new file mode 100644 index 000000000..18d13e643 --- /dev/null +++ b/infrastructure/ai/harness/generated/cattyToolSpecs.json @@ -0,0 +1,1079 @@ +[ + { + "capabilityId": "session.environment", + "toolName": "get_environment", + "rpcMethod": "netcatty/getContext", + "localExecution": false, + "description": "List scoped terminal sessions available to the agent.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "attachment.list", + "toolName": "list_attachments", + "rpcMethod": "netcatty/listAttachments", + "localExecution": false, + "description": "List user-attached files in the current AI chat scope.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "attachment.read", + "toolName": "read_attachment", + "rpcMethod": "netcatty/readAttachment", + "localExecution": false, + "description": "Read a user-attached file from the current AI chat scope.", + "inputShape": { + "filePath": { + "type": "string", + "optional": true, + "description": "Exact local attachment path." + }, + "filename": { + "type": "string", + "optional": true, + "description": "Attachment filename from list_attachments." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "terminal.execute", + "toolName": "terminal_execute", + "rpcMethod": "netcatty/exec", + "localExecution": false, + "description": "Execute a short command in a terminal session and wait for completion. Use only for commands expected to finish within about 60 seconds. For long-running commands use terminal_start and terminal_poll.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "The terminal session ID to execute on." + }, + "command": { + "type": "string", + "optional": false, + "description": "The shell command to execute in the target session." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "terminal.start", + "toolName": "terminal_start", + "rpcMethod": "netcatty/jobStart", + "localExecution": false, + "description": "Start a long-running command in a terminal session. Prefer for builds, scans, log-following, or anything likely to exceed about 2 minutes.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "The terminal session ID to start a long-running command on." + }, + "command": { + "type": "string", + "optional": false, + "description": "The command to start in the target session." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "terminal.poll", + "toolName": "terminal_poll", + "rpcMethod": "netcatty/jobPoll", + "localExecution": false, + "description": "Poll incremental output from a long-running terminal job. Wait at least about 30 seconds between polls unless output justifies checking sooner.", + "inputShape": { + "jobId": { + "type": "string", + "optional": false, + "description": "The background job ID returned by terminal_start." + }, + "offset": { + "type": "number", + "optional": true, + "description": "Character offset from a previous poll (default 0)." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "terminal.stop", + "toolName": "terminal_stop", + "rpcMethod": "netcatty/jobStop", + "localExecution": false, + "description": "Stop a long-running terminal job.", + "inputShape": { + "jobId": { + "type": "string", + "optional": false, + "description": "The background job ID returned by terminal_start." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": true, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "sftp.list", + "toolName": "sftp_list", + "rpcMethod": "netcatty/sftp/list", + "localExecution": false, + "description": "List a remote directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote directory path." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "sftp.read", + "toolName": "sftp_read_file", + "rpcMethod": "netcatty/sftp/read", + "localExecution": false, + "description": "Read a remote file over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file path to read." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "sftp.write", + "toolName": "sftp_write_file", + "rpcMethod": "netcatty/sftp/write", + "localExecution": false, + "description": "Write a remote file over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file path to write." + }, + "content": { + "type": "string", + "optional": false, + "description": "File content to write." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "sftp.download", + "toolName": "sftp_download", + "rpcMethod": "netcatty/sftp/download", + "localExecution": false, + "description": "Download a remote file to a local path.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "remotePath": { + "type": "string", + "optional": false, + "description": "Remote file path to download." + }, + "localPath": { + "type": "string", + "optional": false, + "description": "Local destination path." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "sftp.upload", + "toolName": "sftp_upload", + "rpcMethod": "netcatty/sftp/upload", + "localExecution": false, + "description": "Upload a local file to a remote path.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "localPath": { + "type": "string", + "optional": false, + "description": "Local file path to upload." + }, + "remotePath": { + "type": "string", + "optional": false, + "description": "Remote destination path." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "sftp.stat", + "toolName": "sftp_stat", + "rpcMethod": "netcatty/sftp/stat", + "localExecution": false, + "description": "Get remote file metadata over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote path to stat." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "sftp.home", + "toolName": "sftp_home", + "rpcMethod": "netcatty/sftp/home", + "localExecution": false, + "description": "Get the remote home directory for a session.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "sftp.mkdir", + "toolName": "sftp_mkdir", + "rpcMethod": "netcatty/sftp/mkdir", + "localExecution": false, + "description": "Create a remote directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote directory path to create." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "sftp.delete", + "toolName": "sftp_delete", + "rpcMethod": "netcatty/sftp/delete", + "localExecution": false, + "description": "Delete a remote file or directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file or directory path to delete." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "sftp.rename", + "toolName": "sftp_rename", + "rpcMethod": "netcatty/sftp/rename", + "localExecution": false, + "description": "Rename a remote file or directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "oldPath": { + "type": "string", + "optional": false, + "description": "Current remote path." + }, + "newPath": { + "type": "string", + "optional": false, + "description": "New remote path." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "sftp.chmod", + "toolName": "sftp_chmod", + "rpcMethod": "netcatty/sftp/chmod", + "localExecution": false, + "description": "Change remote file permissions over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file path." + }, + "mode": { + "type": "string", + "optional": false, + "description": "Octal permission mode (e.g. 755)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "vault.host.get", + "toolName": "host_get", + "rpcMethod": "vault/host/get", + "localExecution": false, + "description": "Get host metadata from the vault.", + "inputShape": { + "hostId": { + "type": "string", + "optional": false, + "description": "Vault host ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "vault.host.list", + "toolName": "vault_hosts_list", + "rpcMethod": "vault/hosts/list", + "localExecution": false, + "description": "List saved hosts in the vault (metadata only — no passwords or keys).", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "vault.hosts.create", + "toolName": "vault_hosts_create", + "rpcMethod": "vault/hosts/create", + "localExecution": false, + "description": "Create vault hosts from structured host objects. Use when the user wants to add/create a host (Vault → Hosts). NOT for Vault → Notes sidebar documentation. Use when the user wants to add/create a host in Vault → Hosts (创建主机、SSH 连接凭据). NOT for Vault → Notes sidebar docs. Put SSH password in password field; long remarks/admin tables in host notes field. Never fall back to vault_notes_create if this fails.", + "inputShape": { + "hosts": { + "type": "string", + "optional": false, + "description": "JSON array of host objects you extracted from the user's text. Each object: hostname (required), label, port, username, password, group, tags (array or comma-separated string), notes (Host Details remarks — NOT Vault sidebar Notes), protocol (ssh|telnet|local)." + }, + "dryRun": { + "type": "string", + "optional": true, + "description": "Set to true to validate and preview without writing to the vault." + }, + "skipDuplicates": { + "type": "string", + "optional": true, + "description": "Set to false to create even when a matching host already exists (default true)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "vault.host.import", + "toolName": "vault_hosts_import", + "rpcMethod": "vault/hosts/import", + "localExecution": false, + "description": "Parse known host export file formats (PuTTY, MobaXterm, CSV, SecureCRT, ssh_config) into vault hosts. For arbitrary unstructured text, map to host objects and use vault_hosts_create instead. Only for text in known export formats (PuTTY reg, MobaXterm ini, CSV template, SecureCRT, ssh_config). For arbitrary unstructured host lists from the user, extract fields yourself and call vault_hosts_create.", + "inputShape": { + "format": { + "type": "string", + "optional": false, + "description": "Import format: csv, putty, mobaxterm, securecrt, ssh_config, or auto to detect from text." + }, + "text": { + "type": "string", + "optional": false, + "description": "Exported host data text to import." + }, + "dryRun": { + "type": "string", + "optional": true, + "description": "Set to true to parse and preview without writing to the vault." + }, + "skipDuplicates": { + "type": "string", + "optional": true, + "description": "Set to false to import even when a matching host already exists (default true)." + }, + "fileName": { + "type": "string", + "optional": true, + "description": "Optional source file name (helps SecureCRT .ini parsing)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "vault.host.notes.get", + "toolName": "host_notes_get", + "rpcMethod": "vault/host/notes/get", + "localExecution": false, + "description": "Read host metadata notes attached to a saved host (Host Details panel — not Vault sidebar Notes). Host metadata notes on a saved host — not Vault → Notes sidebar entries.", + "inputShape": { + "hostId": { + "type": "string", + "optional": false, + "description": "Vault host ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "vault.host.notes.set", + "toolName": "host_notes_set", + "rpcMethod": "vault/host/notes/set", + "localExecution": false, + "description": "Update host metadata notes on a saved host (Host Details panel — not Vault sidebar Notes). Host metadata notes on a saved host — not Vault → Notes sidebar entries. Prefer vault_notes_create/update when the user wants vault notes they can open in the Notes sidebar.", + "inputShape": { + "hostId": { + "type": "string", + "optional": false, + "description": "Vault host ID." + }, + "notes": { + "type": "string", + "optional": false, + "description": "Host metadata notes (Host Details — not Vault sidebar Notes)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "vault.note.list", + "toolName": "vault_notes_list", + "rpcMethod": "vault/notes/list", + "localExecution": false, + "description": "List notes in Vault → Notes (markdown notes visible in the vault sidebar).", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "vault.note.get", + "toolName": "vault_notes_get", + "rpcMethod": "vault/notes/get", + "localExecution": false, + "description": "Get a Vault → Notes entry by id (full markdown content).", + "inputShape": { + "noteId": { + "type": "string", + "optional": false, + "description": "Vault note ID from vault_notes_list." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "vault.note.create", + "toolName": "vault_notes_create", + "rpcMethod": "vault/notes/create", + "localExecution": false, + "description": "Create a note in Vault → Notes sidebar (markdown documentation). NOT for adding SSH hosts — use vault_hosts_create for that. Use ONLY when the user wants markdown documentation in Vault → Notes sidebar (保险箱笔记). Do NOT use when the user asked to create/add a host — use vault_hosts_create instead.", + "inputShape": { + "title": { + "type": "string", + "optional": false, + "description": "Note title shown in Vault → Notes." + }, + "content": { + "type": "string", + "optional": false, + "description": "Markdown note body." + }, + "group": { + "type": "string", + "optional": true, + "description": "Optional folder path (e.g. infra/prod)." + }, + "linkedHostIds": { + "type": "string", + "optional": true, + "description": "Optional JSON array of vault host IDs to link." + }, + "tags": { + "type": "string", + "optional": true, + "description": "Optional JSON array of tag strings." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "vault.note.update", + "toolName": "vault_notes_update", + "rpcMethod": "vault/notes/update", + "localExecution": false, + "description": "Update an existing Vault → Notes entry by id. Update an existing Vault → Notes entry (visible in the vault notes sidebar).", + "inputShape": { + "noteId": { + "type": "string", + "optional": false, + "description": "Vault note ID to update." + }, + "title": { + "type": "string", + "optional": true, + "description": "New title." + }, + "content": { + "type": "string", + "optional": true, + "description": "New markdown body." + }, + "group": { + "type": "string", + "optional": true, + "description": "New folder path." + }, + "linkedHostIds": { + "type": "string", + "optional": true, + "description": "Optional JSON array of vault host IDs to link." + }, + "tags": { + "type": "string", + "optional": true, + "description": "Optional JSON array of tag strings." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "vault.snippets.list", + "toolName": "snippets_list", + "rpcMethod": "vault/snippets/list", + "localExecution": false, + "description": "List code snippets stored in the vault.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "vault.snippets.get", + "toolName": "snippets_get", + "rpcMethod": "vault/snippets/get", + "localExecution": false, + "description": "Get a single code snippet from the vault.", + "inputShape": { + "snippetId": { + "type": "string", + "optional": false, + "description": "Snippet ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "vault.snippets.run", + "toolName": "snippets_run", + "rpcMethod": "vault/snippets/run", + "localExecution": false, + "description": "Resolve snippet variables and execute a snippet in a terminal session.", + "inputShape": { + "snippetId": { + "type": "string", + "optional": false, + "description": "Snippet ID to run." + }, + "sessionId": { + "type": "string", + "optional": false, + "description": "Terminal session ID to execute on." + }, + "variables": { + "type": "string", + "optional": true, + "description": "JSON object of snippet variable values." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "portforward.rules.list", + "toolName": "portforward_rules_list", + "rpcMethod": "portforward/rules/list", + "localExecution": false, + "description": "List persisted port forwarding rules.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "portforward.tunnels.list", + "toolName": "portforward_tunnels_list", + "rpcMethod": "portforward/tunnels/list", + "localExecution": false, + "description": "List active port forwarding tunnels.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "portforward.start", + "toolName": "portforward_start", + "rpcMethod": "portforward/start", + "localExecution": false, + "description": "Start a port forwarding tunnel for a rule.", + "inputShape": { + "ruleId": { + "type": "string", + "optional": false, + "description": "Port forwarding rule ID." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "portforward.stop", + "toolName": "portforward_stop", + "rpcMethod": "portforward/stop", + "localExecution": false, + "description": "Stop an active port forwarding tunnel.", + "inputShape": { + "ruleId": { + "type": "string", + "optional": false, + "description": "Port forwarding rule ID." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + } + }, + { + "capabilityId": "harness.tool_output.read", + "toolName": "tool_output_read", + "rpcMethod": null, + "localExecution": true, + "description": "Read stored tool output by handle id when a prior tool result was truncated.", + "inputShape": { + "handleId": { + "type": "string", + "optional": false, + "description": "Tool output handle id from a prior truncated result." + }, + "mode": { + "type": "string", + "optional": true, + "description": "Which portion to read: head, tail, or full." + }, + "maxChars": { + "type": "number", + "optional": true, + "description": "Maximum characters to return." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "harness.workspace.get_info", + "toolName": "workspace_get_info", + "rpcMethod": null, + "localExecution": true, + "description": "Get information about the current workspace, including all terminal sessions and their connection status.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "harness.workspace.get_session_info", + "toolName": "workspace_get_session_info", + "rpcMethod": null, + "localExecution": true, + "description": "Get detailed information about a specific terminal or SFTP session.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "The session ID to get information about." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "harness.web.search", + "toolName": "web_search", + "rpcMethod": null, + "localExecution": true, + "description": "Search the web for current information when configured in AI settings.", + "inputShape": { + "query": { + "type": "string", + "optional": false, + "description": "The search query to look up on the web." + }, + "maxResults": { + "type": "number", + "optional": true, + "description": "Maximum number of search results to return." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + }, + { + "capabilityId": "harness.url.fetch", + "toolName": "url_fetch", + "rpcMethod": null, + "localExecution": true, + "description": "Fetch and read the content of an HTTPS URL.", + "inputShape": { + "url": { + "type": "string", + "optional": false, + "description": "The HTTPS URL to fetch." + }, + "maxLength": { + "type": "number", + "optional": true, + "description": "Maximum characters to return (default 50000)." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + } + } +] diff --git a/infrastructure/ai/harness/generated/globalAgentToolSpecs.json b/infrastructure/ai/harness/generated/globalAgentToolSpecs.json new file mode 100644 index 000000000..a296b38bf --- /dev/null +++ b/infrastructure/ai/harness/generated/globalAgentToolSpecs.json @@ -0,0 +1,985 @@ +[ + { + "capabilityId": "session.environment", + "toolName": "get_environment", + "rpcMethod": "netcatty/getContext", + "localExecution": false, + "description": "List scoped terminal sessions available to the agent.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "attachment.list", + "toolName": "list_attachments", + "rpcMethod": "netcatty/listAttachments", + "localExecution": false, + "description": "List user-attached files in the current AI chat scope.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "attachment.read", + "toolName": "read_attachment", + "rpcMethod": "netcatty/readAttachment", + "localExecution": false, + "description": "Read a user-attached file from the current AI chat scope.", + "inputShape": { + "filePath": { + "type": "string", + "optional": true, + "description": "Exact local attachment path." + }, + "filename": { + "type": "string", + "optional": true, + "description": "Attachment filename from list_attachments." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "terminal.execute", + "toolName": "terminal_execute", + "rpcMethod": "netcatty/exec", + "localExecution": false, + "description": "Execute a short command in a terminal session and wait for completion. Use only for commands expected to finish within about 60 seconds. For long-running commands use terminal_start and terminal_poll.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "The terminal session ID to execute on." + }, + "command": { + "type": "string", + "optional": false, + "description": "The shell command to execute in the target session." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "terminal.start", + "toolName": "terminal_start", + "rpcMethod": "netcatty/jobStart", + "localExecution": false, + "description": "Start a long-running command in a terminal session. Prefer for builds, scans, log-following, or anything likely to exceed about 2 minutes.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "The terminal session ID to start a long-running command on." + }, + "command": { + "type": "string", + "optional": false, + "description": "The command to start in the target session." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "terminal.poll", + "toolName": "terminal_poll", + "rpcMethod": "netcatty/jobPoll", + "localExecution": false, + "description": "Poll incremental output from a long-running terminal job. Wait at least about 30 seconds between polls unless output justifies checking sooner.", + "inputShape": { + "jobId": { + "type": "string", + "optional": false, + "description": "The background job ID returned by terminal_start." + }, + "offset": { + "type": "number", + "optional": true, + "description": "Character offset from a previous poll (default 0)." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "terminal.stop", + "toolName": "terminal_stop", + "rpcMethod": "netcatty/jobStop", + "localExecution": false, + "description": "Stop a long-running terminal job.", + "inputShape": { + "jobId": { + "type": "string", + "optional": false, + "description": "The background job ID returned by terminal_start." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": true, + "bypassesObserverBlock": true, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.list", + "toolName": "sftp_list", + "rpcMethod": "netcatty/sftp/list", + "localExecution": false, + "description": "List a remote directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote directory path." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.read", + "toolName": "sftp_read_file", + "rpcMethod": "netcatty/sftp/read", + "localExecution": false, + "description": "Read a remote file over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file path to read." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.write", + "toolName": "sftp_write_file", + "rpcMethod": "netcatty/sftp/write", + "localExecution": false, + "description": "Write a remote file over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file path to write." + }, + "content": { + "type": "string", + "optional": false, + "description": "File content to write." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.download", + "toolName": "sftp_download", + "rpcMethod": "netcatty/sftp/download", + "localExecution": false, + "description": "Download a remote file to a local path.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "remotePath": { + "type": "string", + "optional": false, + "description": "Remote file path to download." + }, + "localPath": { + "type": "string", + "optional": false, + "description": "Local destination path." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.upload", + "toolName": "sftp_upload", + "rpcMethod": "netcatty/sftp/upload", + "localExecution": false, + "description": "Upload a local file to a remote path.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "localPath": { + "type": "string", + "optional": false, + "description": "Local file path to upload." + }, + "remotePath": { + "type": "string", + "optional": false, + "description": "Remote destination path." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.stat", + "toolName": "sftp_stat", + "rpcMethod": "netcatty/sftp/stat", + "localExecution": false, + "description": "Get remote file metadata over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote path to stat." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.home", + "toolName": "sftp_home", + "rpcMethod": "netcatty/sftp/home", + "localExecution": false, + "description": "Get the remote home directory for a session.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.mkdir", + "toolName": "sftp_mkdir", + "rpcMethod": "netcatty/sftp/mkdir", + "localExecution": false, + "description": "Create a remote directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote directory path to create." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.delete", + "toolName": "sftp_delete", + "rpcMethod": "netcatty/sftp/delete", + "localExecution": false, + "description": "Delete a remote file or directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file or directory path to delete." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.rename", + "toolName": "sftp_rename", + "rpcMethod": "netcatty/sftp/rename", + "localExecution": false, + "description": "Rename a remote file or directory over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "oldPath": { + "type": "string", + "optional": false, + "description": "Current remote path." + }, + "newPath": { + "type": "string", + "optional": false, + "description": "New remote path." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "sftp.chmod", + "toolName": "sftp_chmod", + "rpcMethod": "netcatty/sftp/chmod", + "localExecution": false, + "description": "Change remote file permissions over SFTP.", + "inputShape": { + "sessionId": { + "type": "string", + "optional": false, + "description": "SFTP session ID." + }, + "path": { + "type": "string", + "optional": false, + "description": "Remote file path." + }, + "mode": { + "type": "string", + "optional": false, + "description": "Octal permission mode (e.g. 755)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.host.get", + "toolName": "host_get", + "rpcMethod": "vault/host/get", + "localExecution": false, + "description": "Get host metadata from the vault.", + "inputShape": { + "hostId": { + "type": "string", + "optional": false, + "description": "Vault host ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.host.list", + "toolName": "vault_hosts_list", + "rpcMethod": "vault/hosts/list", + "localExecution": false, + "description": "List saved hosts in the vault (metadata only — no passwords or keys).", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.hosts.create", + "toolName": "vault_hosts_create", + "rpcMethod": "vault/hosts/create", + "localExecution": false, + "description": "Create vault hosts from structured host objects. Use when the user wants to add/create a host (Vault → Hosts). NOT for Vault → Notes sidebar documentation. Use when the user wants to add/create a host in Vault → Hosts (创建主机、SSH 连接凭据). NOT for Vault → Notes sidebar docs. Put SSH password in password field; long remarks/admin tables in host notes field. Never fall back to vault_notes_create if this fails.", + "inputShape": { + "hosts": { + "type": "string", + "optional": false, + "description": "JSON array of host objects you extracted from the user's text. Each object: hostname (required), label, port, username, password, group, tags (array or comma-separated string), notes (Host Details remarks — NOT Vault sidebar Notes), protocol (ssh|telnet|local)." + }, + "dryRun": { + "type": "string", + "optional": true, + "description": "Set to true to validate and preview without writing to the vault." + }, + "skipDuplicates": { + "type": "string", + "optional": true, + "description": "Set to false to create even when a matching host already exists (default true)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.host.import", + "toolName": "vault_hosts_import", + "rpcMethod": "vault/hosts/import", + "localExecution": false, + "description": "Parse known host export file formats (PuTTY, MobaXterm, CSV, SecureCRT, ssh_config) into vault hosts. For arbitrary unstructured text, map to host objects and use vault_hosts_create instead. Only for text in known export formats (PuTTY reg, MobaXterm ini, CSV template, SecureCRT, ssh_config). For arbitrary unstructured host lists from the user, extract fields yourself and call vault_hosts_create.", + "inputShape": { + "format": { + "type": "string", + "optional": false, + "description": "Import format: csv, putty, mobaxterm, securecrt, ssh_config, or auto to detect from text." + }, + "text": { + "type": "string", + "optional": false, + "description": "Exported host data text to import." + }, + "dryRun": { + "type": "string", + "optional": true, + "description": "Set to true to parse and preview without writing to the vault." + }, + "skipDuplicates": { + "type": "string", + "optional": true, + "description": "Set to false to import even when a matching host already exists (default true)." + }, + "fileName": { + "type": "string", + "optional": true, + "description": "Optional source file name (helps SecureCRT .ini parsing)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.host.notes.get", + "toolName": "host_notes_get", + "rpcMethod": "vault/host/notes/get", + "localExecution": false, + "description": "Read host metadata notes attached to a saved host (Host Details panel — not Vault sidebar Notes). Host metadata notes on a saved host — not Vault → Notes sidebar entries.", + "inputShape": { + "hostId": { + "type": "string", + "optional": false, + "description": "Vault host ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": true, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.host.notes.set", + "toolName": "host_notes_set", + "rpcMethod": "vault/host/notes/set", + "localExecution": false, + "description": "Update host metadata notes on a saved host (Host Details panel — not Vault sidebar Notes). Host metadata notes on a saved host — not Vault → Notes sidebar entries. Prefer vault_notes_create/update when the user wants vault notes they can open in the Notes sidebar.", + "inputShape": { + "hostId": { + "type": "string", + "optional": false, + "description": "Vault host ID." + }, + "notes": { + "type": "string", + "optional": false, + "description": "Host metadata notes (Host Details — not Vault sidebar Notes)." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.note.list", + "toolName": "vault_notes_list", + "rpcMethod": "vault/notes/list", + "localExecution": false, + "description": "List notes in Vault → Notes (markdown notes visible in the vault sidebar).", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.note.get", + "toolName": "vault_notes_get", + "rpcMethod": "vault/notes/get", + "localExecution": false, + "description": "Get a Vault → Notes entry by id (full markdown content).", + "inputShape": { + "noteId": { + "type": "string", + "optional": false, + "description": "Vault note ID from vault_notes_list." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.note.create", + "toolName": "vault_notes_create", + "rpcMethod": "vault/notes/create", + "localExecution": false, + "description": "Create a note in Vault → Notes sidebar (markdown documentation). NOT for adding SSH hosts — use vault_hosts_create for that. Use ONLY when the user wants markdown documentation in Vault → Notes sidebar (保险箱笔记). Do NOT use when the user asked to create/add a host — use vault_hosts_create instead.", + "inputShape": { + "title": { + "type": "string", + "optional": false, + "description": "Note title shown in Vault → Notes." + }, + "content": { + "type": "string", + "optional": false, + "description": "Markdown note body." + }, + "group": { + "type": "string", + "optional": true, + "description": "Optional folder path (e.g. infra/prod)." + }, + "linkedHostIds": { + "type": "string", + "optional": true, + "description": "Optional JSON array of vault host IDs to link." + }, + "tags": { + "type": "string", + "optional": true, + "description": "Optional JSON array of tag strings." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.note.update", + "toolName": "vault_notes_update", + "rpcMethod": "vault/notes/update", + "localExecution": false, + "description": "Update an existing Vault → Notes entry by id. Update an existing Vault → Notes entry (visible in the vault notes sidebar).", + "inputShape": { + "noteId": { + "type": "string", + "optional": false, + "description": "Vault note ID to update." + }, + "title": { + "type": "string", + "optional": true, + "description": "New title." + }, + "content": { + "type": "string", + "optional": true, + "description": "New markdown body." + }, + "group": { + "type": "string", + "optional": true, + "description": "New folder path." + }, + "linkedHostIds": { + "type": "string", + "optional": true, + "description": "Optional JSON array of vault host IDs to link." + }, + "tags": { + "type": "string", + "optional": true, + "description": "Optional JSON array of tag strings." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.snippets.list", + "toolName": "snippets_list", + "rpcMethod": "vault/snippets/list", + "localExecution": false, + "description": "List code snippets stored in the vault.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.snippets.get", + "toolName": "snippets_get", + "rpcMethod": "vault/snippets/get", + "localExecution": false, + "description": "Get a single code snippet from the vault.", + "inputShape": { + "snippetId": { + "type": "string", + "optional": false, + "description": "Snippet ID." + } + }, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "vault.snippets.run", + "toolName": "snippets_run", + "rpcMethod": "vault/snippets/run", + "localExecution": false, + "description": "Resolve snippet variables and execute a snippet in a terminal session.", + "inputShape": { + "snippetId": { + "type": "string", + "optional": false, + "description": "Snippet ID to run." + }, + "sessionId": { + "type": "string", + "optional": false, + "description": "Terminal session ID to execute on." + }, + "variables": { + "type": "string", + "optional": true, + "description": "JSON object of snippet variable values." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": true, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "portforward.rules.list", + "toolName": "portforward_rules_list", + "rpcMethod": "portforward/rules/list", + "localExecution": false, + "description": "List persisted port forwarding rules.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "portforward.tunnels.list", + "toolName": "portforward_tunnels_list", + "rpcMethod": "portforward/tunnels/list", + "localExecution": false, + "description": "List active port forwarding tunnels.", + "inputShape": {}, + "policy": { + "write": false, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": true, + "bypassesChatCancel": true + }, + "agentKind": "global" + }, + { + "capabilityId": "portforward.start", + "toolName": "portforward_start", + "rpcMethod": "portforward/start", + "localExecution": false, + "description": "Start a port forwarding tunnel for a rule.", + "inputShape": { + "ruleId": { + "type": "string", + "optional": false, + "description": "Port forwarding rule ID." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": true, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + }, + { + "capabilityId": "portforward.stop", + "toolName": "portforward_stop", + "rpcMethod": "portforward/stop", + "localExecution": false, + "description": "Stop an active port forwarding tunnel.", + "inputShape": { + "ruleId": { + "type": "string", + "optional": false, + "description": "Port forwarding rule ID." + } + }, + "policy": { + "write": true, + "sensitiveRead": false, + "longRunning": false, + "requiresChatSession": false, + "bypassesObserverBlock": false, + "bypassesApproval": false, + "bypassesChatCancel": false + }, + "agentKind": "global" + } +] diff --git a/infrastructure/ai/harness/globalAgentRuntime.ts b/infrastructure/ai/harness/globalAgentRuntime.ts new file mode 100644 index 000000000..88551f187 --- /dev/null +++ b/infrastructure/ai/harness/globalAgentRuntime.ts @@ -0,0 +1,11 @@ +import { AgentRuntime } from './agentRuntime'; +import { cattyTurnDriver } from './turnDrivers/cattyTurnDriver'; +import { externalSdkTurnDriver } from './turnDrivers/externalSdkTurnDriver'; + +export const globalAgentRuntime = new AgentRuntime({ + drivers: [cattyTurnDriver, externalSdkTurnDriver], +}); + +export function getAgentRuntime(): AgentRuntime { + return globalAgentRuntime; +} diff --git a/infrastructure/ai/harness/index.ts b/infrastructure/ai/harness/index.ts new file mode 100644 index 000000000..72247bd94 --- /dev/null +++ b/infrastructure/ai/harness/index.ts @@ -0,0 +1,99 @@ +export type { + AgentBackend, + AgentEvent, + AgentEventListener, + AgentEventType, + ApprovalOutcome, + CompactionEvent, + CompactionTrace, + ContextPrepareResult, + ContextPrepareTrigger, + ExternalBridgeHistoryMessage, + TokenEstimatorKind, + UsageEvent, +} from './types'; + +export { TraceStore, globalTraceStore } from './traceStore'; +export type { TraceExport } from './traceStore'; + +export { stopAgentTurn, clearChatSessionCancelled } from './agentStop'; +export type { AgentStopBridge, StopAgentTurnParams, StopAgentTurnReason } from './agentStop'; + +export { AgentRuntime } from './agentRuntime'; +export { globalAgentRuntime, getAgentRuntime } from './globalAgentRuntime'; + +export { + estimateModelMessagesTokens, + estimateModelMessagesTokensWithKind, +} from './tokenEstimator'; +export type { EstimateModelMessagesTokensInput, EstimateModelMessagesTokensResult } from './tokenEstimator'; + +export { ToolOutputStore, globalToolOutputStore } from './toolOutputStore'; +export type { ToolOutputHandle, StoreToolOutputInput, ReadToolOutputInput } from './toolOutputStore'; + +export { ToolResultDedup, hashScopeKey, previewToolResult } from './toolResultDedup'; +export type { ToolResultDedupEntry } from './toolResultDedup'; + +export { cattyTurnDriver } from './turnDrivers/cattyTurnDriver'; +export { externalSdkTurnDriver } from './turnDrivers/externalSdkTurnDriver'; +export type { + TurnInput, + TurnResult, + TurnDriver, + TurnDriverContext, + TurnUiCallbacks, + CattyTurnInput, + ExternalTurnInput, +} from './turnDrivers/types'; + +export { + prepareTurnContext, + prepareStepContext, + buildExternalBridgeContext, + extractLatestUserGoal, +} from './contextManager'; +export type { PrepareTurnContextInput, PostCompactReinjection } from './contextManager'; + +export { buildExternalBridgeContextMessages } from './externalBridgeContext'; + +export { + fitTerminalExecuteResultForModel, + MAX_LIVE_TERMINAL_STDOUT_CHARS, + MAX_LIVE_TERMINAL_STDERR_CHARS, +} from './terminalCompression'; +export type { TerminalExecuteResult, TerminalOutputHandle } from './terminalCompression'; + +export { + encodeSdkSessionIdentity, + parseSdkSessionIdentity, + SDK_SESSION_ID_PREFIX, +} from './sdkSessionIdentity'; +export type { SdkSessionIdentityPayload } from './sdkSessionIdentity'; + +export { + mapCattyStreamChunkToAgentEvents, + mapSdkStreamEventToAgentEvents, + createHarnessEventSink, +} from './agentEventAdapter'; +export type { StreamEventContext } from './agentEventAdapter'; + +export { + compactCattyMessages, + prepareCattyMessagesForStream, +} from './cattyRuntime'; +export type { CompactCattyMessagesInput, CompactCattyMessagesResult } from './cattyRuntime'; + +export { createCattyToolsFromCatalog } from './capabilityTools'; + +export { + PermissionGrantStore, + buildGrantFromApproval, + createPermissionGrantId, + getActivePermissionGrants, + matchPermissionGrant, + patternMatches, + resolveCapabilityId, + sanitizePermissionGrants, + setActivePermissionGrants, +} from './permissionGrants'; +export type { PermissionGrantMatchContext, PermissionGrantRule } from './permissionGrants'; diff --git a/infrastructure/ai/harness/permissionGrants.test.ts b/infrastructure/ai/harness/permissionGrants.test.ts new file mode 100644 index 000000000..0bb56716f --- /dev/null +++ b/infrastructure/ai/harness/permissionGrants.test.ts @@ -0,0 +1,87 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { + buildGrantFromApproval, + buildGrantsFromApproval, + listGrantableCapabilityIds, + matchPermissionGrant, + patternMatches, + type PermissionGrantRule, +} from './permissionGrants'; + +const baseRule = (overrides: Partial): PermissionGrantRule => ({ + id: 'grant-1', + capabilityId: 'terminal.execute', + sessionPattern: 'session-a', + createdAt: Date.now(), + ...overrides, +}); + +describe('permissionGrants', () => { + it('matches wildcard session and command patterns', () => { + const rules = [baseRule({ sessionPattern: '*', commandPattern: 'ls *' })]; + const matched = matchPermissionGrant(rules, { + capabilityId: 'terminal.execute', + sessionId: 'any-session', + args: { command: 'ls -la /tmp' }, + }); + assert.ok(matched); + }); + + it('ignores sessionPattern and matches globally by capability and command', () => { + const rules = [baseRule({ sessionPattern: 'old-session-uuid', commandPattern: 'ls *' })]; + const matched = matchPermissionGrant(rules, { + capabilityId: 'terminal.execute', + sessionId: 'different-session', + args: { command: 'ls -la /tmp' }, + }); + assert.ok(matched); + }); + + it('does not match a different capability', () => { + const rules = [baseRule({ sessionPattern: '*' })]; + const matched = matchPermissionGrant(rules, { + capabilityId: 'terminal.start', + sessionId: 'session-a', + args: { command: 'make' }, + }); + assert.equal(matched, null); + }); + + it('buildGrantFromApproval uses global scope and OpenCode-style command prefix patterns', () => { + const grant = buildGrantFromApproval('terminal.execute', { + sessionId: 'ssh-1', + command: 'systemctl status nginx', + }, 'chat-1'); + assert.equal(grant.sessionPattern, '*'); + assert.equal(grant.commandPattern, 'systemctl status *'); + }); + + it('buildGrantsFromApproval emits one rule per chained command segment', () => { + const grants = buildGrantsFromApproval('terminal.execute', { + sessionId: 'ssh-1', + command: 'cd /tmp && lscpu', + }, 'chat-1'); + assert.equal(grants.length, 1); + assert.equal(grants[0]?.commandPattern, 'lscpu *'); + }); + + it('OpenCode wildcard allows optional args after prefix', () => { + assert.equal(patternMatches('lscpu *', 'lscpu'), true); + assert.equal(patternMatches('lscpu *', 'lscpu -e'), true); + assert.equal(patternMatches('git checkout *', 'git checkout main'), true); + assert.equal(patternMatches('git checkout *', 'git commit'), false); + }); + + it('lists grantable capability ids from catalog policy', () => { + const ids = listGrantableCapabilityIds(); + assert.ok(ids.includes('terminal.execute')); + assert.ok(ids.includes('sftp.write')); + assert.ok(!ids.includes('terminal.poll')); + }); + + it('patternMatches supports regex literals', () => { + assert.equal(patternMatches('/^ls\\b/', 'ls -la'), true); + assert.equal(patternMatches('/^ls\\b/', 'cat file'), false); + }); +}); diff --git a/infrastructure/ai/harness/permissionGrants.ts b/infrastructure/ai/harness/permissionGrants.ts new file mode 100644 index 000000000..e7d623db8 --- /dev/null +++ b/infrastructure/ai/harness/permissionGrants.ts @@ -0,0 +1,286 @@ +import cattyToolSpecs from './generated/cattyToolSpecs.json'; +import { buildAlwaysAllowCommandPatterns } from '../shared/shellCommandGrant'; + +export interface PermissionGrantRule { + id: string; + capabilityId: string; + sessionPattern: string; + commandPattern?: string; + argsPattern?: Record; + createdAt: number; + note?: string; +} + +export interface PermissionGrantMatchContext { + capabilityId: string; + sessionId?: string; + chatSessionId?: string; + hostname?: string; + args?: Record; +} + +type CattyToolSpecRef = { + capabilityId: string; + toolName: string; + rpcMethod: string | null; + policy?: { + write?: boolean; + bypassesApproval?: boolean; + }; +}; + +const TOOL_NAME_TO_CAPABILITY = new Map(); +const RPC_METHOD_TO_CAPABILITY = new Map(); + +for (const spec of cattyToolSpecs as CattyToolSpecRef[]) { + TOOL_NAME_TO_CAPABILITY.set(spec.toolName, spec.capabilityId); + if (spec.rpcMethod) { + RPC_METHOD_TO_CAPABILITY.set(spec.rpcMethod, spec.capabilityId); + } +} + +export function resolveCapabilityId(toolOrRpcName: string): string { + return TOOL_NAME_TO_CAPABILITY.get(toolOrRpcName) + ?? RPC_METHOD_TO_CAPABILITY.get(toolOrRpcName) + ?? toolOrRpcName; +} + +export function patternMatches(pattern: string, value: string): boolean { + if (!pattern) return false; + if (pattern === '*') return true; + + if (pattern.startsWith('host:')) { + return globOrRegexMatch(pattern.slice('host:'.length), value); + } + + return globOrRegexMatch(pattern, value); +} + +function globOrRegexMatch(pattern: string, value: string): boolean { + if (pattern.startsWith('/') && pattern.lastIndexOf('/') > 0) { + const lastSlash = pattern.lastIndexOf('/'); + const body = pattern.slice(1, lastSlash); + const flags = pattern.slice(lastSlash + 1); + try { + return new RegExp(body, flags).test(value); + } catch { + return false; + } + } + + if (!pattern.includes('*') && !pattern.includes('?')) { + return value === pattern; + } + + // OpenCode Wildcard.match semantics (trailing " *" allows optional args). + let escaped = pattern + .replace(/[.+^${}()|[\]\\]/g, '\\$&') + .replace(/\*/g, '.*') + .replace(/\?/g, '.'); + if (escaped.endsWith(' .*')) { + escaped = `${escaped.slice(0, -3)}( .*)?`; + } + + return new RegExp(`^${escaped}$`, 's').test(value); +} + +function argsPatternMatches( + argsPattern: Record | undefined, + args: Record | undefined, +): boolean { + if (!argsPattern) return true; + if (!args) return false; + + for (const [key, pattern] of Object.entries(argsPattern)) { + const argValue = args[key]; + if (typeof argValue === 'undefined') return false; + if (!patternMatches(pattern, String(argValue))) return false; + } + return true; +} + +export function matchPermissionGrant( + rules: readonly PermissionGrantRule[], + ctx: PermissionGrantMatchContext, +): PermissionGrantRule | null { + if (rules.length === 0) return null; + + const args = ctx.args ?? {}; + + for (const rule of rules) { + if (rule.capabilityId !== ctx.capabilityId) continue; + + if (rule.commandPattern) { + const command = typeof args.command === 'string' ? args.command : ''; + if (!patternMatches(rule.commandPattern, command)) continue; + } + + if (!argsPatternMatches(rule.argsPattern, args)) continue; + + return rule; + } + + return null; +} + +export function sanitizePermissionGrants(raw: unknown): PermissionGrantRule[] { + if (!Array.isArray(raw)) return []; + + const result: PermissionGrantRule[] = []; + for (const entry of raw) { + if (!entry || typeof entry !== 'object') continue; + const record = entry as Record; + const capabilityId = typeof record.capabilityId === 'string' ? record.capabilityId.trim() : ''; + if (!capabilityId) continue; + + const rule: PermissionGrantRule = { + id: typeof record.id === 'string' && record.id.trim() + ? record.id.trim().slice(0, 64) + : createPermissionGrantId(), + capabilityId, + sessionPattern: typeof record.sessionPattern === 'string' && record.sessionPattern.trim() + ? record.sessionPattern.trim() + : '*', + createdAt: typeof record.createdAt === 'number' && Number.isFinite(record.createdAt) + ? record.createdAt + : Date.now(), + }; + + if (typeof record.commandPattern === 'string' && record.commandPattern.trim()) { + rule.commandPattern = record.commandPattern.trim(); + } + if (record.argsPattern && typeof record.argsPattern === 'object' && !Array.isArray(record.argsPattern)) { + const argsPattern: Record = {}; + for (const [key, value] of Object.entries(record.argsPattern as Record)) { + if (typeof value === 'string' && value.trim()) { + argsPattern[key] = value.trim(); + } + } + if (Object.keys(argsPattern).length > 0) { + rule.argsPattern = argsPattern; + } + } + if (typeof record.note === 'string' && record.note.trim()) { + rule.note = record.note.trim().slice(0, 240); + } + + result.push(rule); + } + + return result; +} + +export function createPermissionGrantId(): string { + return `grant_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`; +} + +const COMMAND_GRANT_CAPABILITIES = new Set([ + 'terminal.execute', + 'terminal.start', +]); + +const GRANTABLE_CAPABILITY_IDS: readonly string[] = Object.freeze( + [...new Set( + (cattyToolSpecs as CattyToolSpecRef[]) + .filter((spec) => spec.policy?.write && !spec.policy?.bypassesApproval) + .map((spec) => spec.capabilityId), + )].sort(), +); + +export function listGrantableCapabilityIds(): readonly string[] { + return GRANTABLE_CAPABILITY_IDS; +} + +export function capabilitySupportsCommandPatternGrant(capabilityId: string): boolean { + return COMMAND_GRANT_CAPABILITIES.has(capabilityId); +} + +function resolveCommandGrantPatterns( + capabilityId: string, + args: Record, +): string[] | undefined { + if (!COMMAND_GRANT_CAPABILITIES.has(capabilityId)) return undefined; + const command = typeof args.command === 'string' ? args.command.trim() : ''; + if (!command) return undefined; + return buildAlwaysAllowCommandPatterns(command); +} + +export function buildGrantsFromApproval( + capabilityId: string, + args: Record, + _chatSessionId?: string, +): PermissionGrantRule[] { + // OpenCode-style always-allow: global scope (not bound to a terminal session UUID). + const sessionPattern = '*'; + const commandPatterns = resolveCommandGrantPatterns(capabilityId, args); + const createdAt = Date.now(); + + if (!commandPatterns || commandPatterns.length === 0) { + return [{ + id: createPermissionGrantId(), + capabilityId, + sessionPattern, + createdAt, + }]; + } + + return commandPatterns.map((commandPattern) => ({ + id: createPermissionGrantId(), + capabilityId, + sessionPattern, + commandPattern, + createdAt, + })); +} + +export function buildGrantFromApproval( + capabilityId: string, + args: Record, + chatSessionId?: string, +): PermissionGrantRule { + return buildGrantsFromApproval(capabilityId, args, chatSessionId)[0]; +} + +let activeRules: PermissionGrantRule[] = []; + +export function setActivePermissionGrants(rules: PermissionGrantRule[]): void { + activeRules = [...rules]; +} + +export function getActivePermissionGrants(): readonly PermissionGrantRule[] { + return activeRules; +} + +export class PermissionGrantStore { + private rules: PermissionGrantRule[]; + + constructor(rules: PermissionGrantRule[] = []) { + this.rules = [...rules]; + } + + getRules(): readonly PermissionGrantRule[] { + return this.rules; + } + + setRules(rules: PermissionGrantRule[]): void { + this.rules = [...rules]; + } + + addRule(rule: PermissionGrantRule): void { + this.rules = [...this.rules, rule]; + } + + updateRule(id: string, updates: Partial>): void { + this.rules = this.rules.map((rule) => ( + rule.id === id ? { ...rule, ...updates } : rule + )); + } + + removeRule(id: string): void { + this.rules = this.rules.filter((rule) => rule.id !== id); + } + + match(ctx: PermissionGrantMatchContext): PermissionGrantRule | null { + return matchPermissionGrant(this.rules, ctx); + } +} diff --git a/infrastructure/ai/harness/sdkSessionIdentity.ts b/infrastructure/ai/harness/sdkSessionIdentity.ts new file mode 100644 index 000000000..bb2ddf437 --- /dev/null +++ b/infrastructure/ai/harness/sdkSessionIdentity.ts @@ -0,0 +1,35 @@ +export const SDK_SESSION_ID_PREFIX = 'netcatty-sdk-session:'; + +export interface SdkSessionIdentityPayload { + v: 1; + id: string; + backend: string; + binPath: string; +} + +export function encodeSdkSessionIdentity( + sessionId: string, + sdkBackend?: string, + binPath?: string, +): string { + if (!sessionId || !sdkBackend) return sessionId; + const payload: SdkSessionIdentityPayload = { + v: 1, + id: sessionId, + backend: sdkBackend, + binPath: binPath || '', + }; + return `${SDK_SESSION_ID_PREFIX}${encodeURIComponent(JSON.stringify(payload))}`; +} + +export function parseSdkSessionIdentity(value: string | undefined | null): SdkSessionIdentityPayload | null { + const raw = String(value || '').trim(); + if (!raw.startsWith(SDK_SESSION_ID_PREFIX)) return null; + try { + const parsed = JSON.parse(decodeURIComponent(raw.slice(SDK_SESSION_ID_PREFIX.length))) as SdkSessionIdentityPayload; + if (parsed?.v !== 1 || !parsed.id || !parsed.backend) return null; + return parsed; + } catch { + return null; + } +} diff --git a/infrastructure/ai/harness/terminalCompression.ts b/infrastructure/ai/harness/terminalCompression.ts new file mode 100644 index 000000000..c53f265d0 --- /dev/null +++ b/infrastructure/ai/harness/terminalCompression.ts @@ -0,0 +1,93 @@ +import { compressVerboseText, truncateTextWithHeadAndTail } from '../requestPayloadCompression'; +import type { ToolOutputStore } from './toolOutputStore'; + +export const MAX_LIVE_TERMINAL_STDOUT_CHARS = 24_000; +export const MAX_LIVE_TERMINAL_STDERR_CHARS = 12_000; + +export interface TerminalExecuteResult { + stdout: string; + stderr: string; + exitCode: number | null; + command?: string; + sessionId?: string; +} + +export interface TerminalOutputHandle { + kind: 'terminal-output'; + sessionId: string; + command?: string; + totalStdoutChars: number; + totalStderrChars: number; + handleId?: string; +} + +export interface FitTerminalExecuteResultOptions { + chatSessionId?: string; + toolOutputStore?: ToolOutputStore; +} + +export function fitTerminalExecuteResultForModel( + result: TerminalExecuteResult, + options?: FitTerminalExecuteResultOptions, +): TerminalExecuteResult { + const stdout = truncateTextWithHeadAndTail( + compressVerboseText(result.stdout), + MAX_LIVE_TERMINAL_STDOUT_CHARS, + ); + const stderr = truncateTextWithHeadAndTail( + compressVerboseText(result.stderr), + MAX_LIVE_TERMINAL_STDERR_CHARS, + ); + + const fitted: TerminalExecuteResult = { + ...result, + stdout, + stderr, + }; + + if ( + stdout.length < result.stdout.length + || stderr.length < result.stderr.length + ) { + const fullContent = [ + result.command ? `command: ${result.command}` : '', + result.stdout ? `stdout:\n${result.stdout}` : '', + result.stderr ? `stderr:\n${result.stderr}` : '', + ].filter(Boolean).join('\n\n'); + + let handleId: string | undefined; + if (options?.toolOutputStore && options.chatSessionId && fullContent) { + handleId = options.toolOutputStore.store({ + chatSessionId: options.chatSessionId, + capabilityId: 'terminal.execute', + sessionId: result.sessionId, + content: fullContent, + }).id; + } + + const handle: TerminalOutputHandle = { + kind: 'terminal-output', + sessionId: result.sessionId ?? 'unknown', + command: result.command, + totalStdoutChars: result.stdout.length, + totalStderrChars: result.stderr.length, + handleId, + }; + fitted.stdout = appendOutputHandleNotice(stdout, handle, 'stdout'); + if (result.stderr) { + fitted.stderr = appendOutputHandleNotice(stderr, handle, 'stderr'); + } + } + + return fitted; +} + +function appendOutputHandleNotice( + truncated: string, + handle: TerminalOutputHandle, + stream: 'stdout' | 'stderr', +): string { + const totalChars = stream === 'stdout' ? handle.totalStdoutChars : handle.totalStderrChars; + const handleSuffix = handle.handleId ? ` handleId=${handle.handleId}` : ''; + return `${truncated}\n\n[output handle: session=${handle.sessionId}${handle.command ? ` command=${handle.command}` : ''} ${stream}=${totalChars} chars truncated for model context${handleSuffix}]`; +} diff --git a/infrastructure/ai/harness/tokenEstimator.ts b/infrastructure/ai/harness/tokenEstimator.ts new file mode 100644 index 000000000..624baa488 --- /dev/null +++ b/infrastructure/ai/harness/tokenEstimator.ts @@ -0,0 +1,71 @@ +import type { ModelMessage } from 'ai'; +import type { TokenEstimatorKind } from './types'; + +const CHARS_PER_TOKEN_FALLBACK = 4; + +function estimateChars(value: unknown): number { + if (value == null) return 0; + if (typeof value === 'string') return value.length; + if (Array.isArray(value)) { + return value.reduce((sum, item) => sum + estimateChars(item), 0); + } + if (typeof value === 'object') { + try { + return JSON.stringify(value).length; + } catch { + return String(value).length; + } + } + return String(value).length; +} + +function resolveEstimatorKind(providerId?: string | null): TokenEstimatorKind { + const id = (providerId ?? '').toLowerCase(); + if (id.includes('openai') || id.includes('gpt')) return 'openai-heuristic'; + if (id.includes('anthropic') || id.includes('claude')) return 'anthropic-heuristic'; + if (id.includes('google') || id.includes('gemini')) return 'google-heuristic'; + return 'chars-div-4'; +} + +function applyHeuristicMultiplier(chars: number, kind: TokenEstimatorKind): number { + switch (kind) { + case 'openai-heuristic': + return Math.ceil(chars / 3.5); + case 'anthropic-heuristic': + return Math.ceil(chars / 3.2); + case 'google-heuristic': + return Math.ceil(chars / 3.8); + default: + return Math.ceil(chars / CHARS_PER_TOKEN_FALLBACK); + } +} + +export interface EstimateModelMessagesTokensInput { + messages: ModelMessage[]; + providerId?: string | null; +} + +export interface EstimateModelMessagesTokensResult { + tokens: number; + estimatorKind: TokenEstimatorKind; +} + +export function estimateModelMessagesTokensWithKind( + input: EstimateModelMessagesTokensInput, +): EstimateModelMessagesTokensResult { + const estimatorKind = resolveEstimatorKind(input.providerId); + const chars = input.messages.reduce((total, message) => { + return total + estimateChars(message.role) + estimateChars(message.content); + }, 0); + return { + tokens: applyHeuristicMultiplier(chars, estimatorKind), + estimatorKind, + }; +} + +export function estimateModelMessagesTokens( + messages: ModelMessage[], + providerId?: string | null, +): number { + return estimateModelMessagesTokensWithKind({ messages, providerId }).tokens; +} diff --git a/infrastructure/ai/harness/toolOutputStore.test.ts b/infrastructure/ai/harness/toolOutputStore.test.ts new file mode 100644 index 000000000..535c76e79 --- /dev/null +++ b/infrastructure/ai/harness/toolOutputStore.test.ts @@ -0,0 +1,26 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { ToolOutputStore } from './toolOutputStore'; + +test('ToolOutputStore stores and reads truncated output by handle', () => { + const store = new ToolOutputStore(); + const handle = store.store({ + chatSessionId: 'chat-1', + capabilityId: 'terminal.execute', + sessionId: 'sess-1', + content: 'A'.repeat(50_000), + }); + + assert.ok(handle.id.startsWith('tool-output-')); + assert.equal(handle.totalChars, 50_000); + + const head = store.read({ handleId: handle.id, mode: 'head', maxChars: 100 }, 'chat-1'); + assert.equal(head?.length, 100); + + const tail = store.read({ handleId: handle.id, mode: 'tail', maxChars: 50 }, 'chat-1'); + assert.equal(tail?.length, 50); + assert.equal(tail, 'A'.repeat(50)); + + store.prune('chat-1'); + assert.equal(store.read({ handleId: handle.id }, 'chat-1'), null); +}); diff --git a/infrastructure/ai/harness/toolOutputStore.ts b/infrastructure/ai/harness/toolOutputStore.ts new file mode 100644 index 000000000..38824ac50 --- /dev/null +++ b/infrastructure/ai/harness/toolOutputStore.ts @@ -0,0 +1,87 @@ +export interface ToolOutputHandle { + id: string; + capabilityId: string; + sessionId?: string; + totalChars: number; + preview: string; + storedAt: number; + fullContent: string; +} + +export interface StoreToolOutputInput { + chatSessionId: string; + capabilityId: string; + content: string; + sessionId?: string; + previewChars?: number; +} + +export interface ReadToolOutputInput { + handleId: string; + mode?: 'head' | 'tail' | 'full'; + maxChars?: number; +} + +let handleCounter = 0; + +function nextHandleId(): string { + handleCounter += 1; + return `tool-output-${Date.now()}-${handleCounter}`; +} + +export class ToolOutputStore { + private readonly bySession = new Map>(); + + store(input: StoreToolOutputInput): ToolOutputHandle { + const previewChars = input.previewChars ?? 240; + const handle: ToolOutputHandle = { + id: nextHandleId(), + capabilityId: input.capabilityId, + sessionId: input.sessionId, + totalChars: input.content.length, + preview: input.content.slice(0, previewChars), + storedAt: Date.now(), + fullContent: input.content, + }; + const sessionMap = this.bySession.get(input.chatSessionId) ?? new Map(); + sessionMap.set(handle.id, handle); + this.bySession.set(input.chatSessionId, sessionMap); + return handle; + } + + get(handleId: string, chatSessionId?: string): ToolOutputHandle | undefined { + if (chatSessionId) { + return this.bySession.get(chatSessionId)?.get(handleId); + } + for (const sessionMap of this.bySession.values()) { + const handle = sessionMap.get(handleId); + if (handle) return handle; + } + return undefined; + } + + listPendingHandles(chatSessionId: string): ToolOutputHandle[] { + return [...(this.bySession.get(chatSessionId)?.values() ?? [])]; + } + + read(input: ReadToolOutputInput, chatSessionId?: string): string | null { + const handle = this.get(input.handleId, chatSessionId); + if (!handle) return null; + const maxChars = input.maxChars ?? 12_000; + const mode = input.mode ?? 'head'; + const content = handle.fullContent; + if (mode === 'full') { + return content.length <= maxChars ? content : content.slice(0, maxChars); + } + if (mode === 'tail') { + return content.length <= maxChars ? content : content.slice(-maxChars); + } + return content.slice(0, maxChars); + } + + prune(chatSessionId: string): void { + this.bySession.delete(chatSessionId); + } +} + +export const globalToolOutputStore = new ToolOutputStore(); diff --git a/infrastructure/ai/harness/toolResultDedup.ts b/infrastructure/ai/harness/toolResultDedup.ts new file mode 100644 index 000000000..dd5d071d3 --- /dev/null +++ b/infrastructure/ai/harness/toolResultDedup.ts @@ -0,0 +1,54 @@ +export interface ToolResultDedupEntry { + fingerprint: string; + toolName: string; + turnNumber: number; + preview: string; +} + +export class ToolResultDedup { + private turnNumber = 0; + private readonly cache = new Map(); + + beginTurn(): void { + this.turnNumber += 1; + } + + reset(): void { + this.cache.clear(); + this.turnNumber = 0; + } + + fingerprintFor(toolName: string, key: string): string { + return `${toolName}:${key}`; + } + + check(fingerprint: string): ToolResultDedupEntry | undefined { + return this.cache.get(fingerprint); + } + + remember(toolName: string, fingerprint: string, preview: string): void { + this.cache.set(fingerprint, { + fingerprint, + toolName, + turnNumber: this.turnNumber, + preview: preview.slice(0, 160), + }); + } + + buildCachedNotice(entry: ToolResultDedupEntry): string { + return `[cached] same as turn ${entry.turnNumber} for ${entry.toolName}`; + } +} + +export function hashScopeKey(parts: Array): string { + return parts.filter(Boolean).join('|'); +} + +export function previewToolResult(result: unknown): string { + if (typeof result === 'string') return result.slice(0, 160); + try { + return JSON.stringify(result).slice(0, 160); + } catch { + return String(result).slice(0, 160); + } +} diff --git a/infrastructure/ai/harness/traceStore.ts b/infrastructure/ai/harness/traceStore.ts new file mode 100644 index 000000000..0c9d85f57 --- /dev/null +++ b/infrastructure/ai/harness/traceStore.ts @@ -0,0 +1,60 @@ +import type { AgentEvent, CompactionTrace } from './types'; + +const DEFAULT_MAX_EVENTS = 2_000; + +export interface TraceExport { + sessionId: string; + events: AgentEvent[]; + compactions: CompactionTrace[]; + exportedAt: number; +} + +export class TraceStore { + private readonly events = new Map(); + private readonly compactions = new Map(); + private readonly maxEvents: number; + + constructor(maxEvents = DEFAULT_MAX_EVENTS) { + this.maxEvents = maxEvents; + } + + append(event: AgentEvent): void { + const list = this.events.get(event.sessionId) ?? []; + list.push(event); + if (list.length > this.maxEvents) { + list.splice(0, list.length - this.maxEvents); + } + this.events.set(event.sessionId, list); + + if (event.type === 'compaction') { + const traces = this.compactions.get(event.sessionId) ?? []; + traces.push(event.trace); + this.compactions.set(event.sessionId, traces); + } + } + + getEvents(sessionId: string): readonly AgentEvent[] { + return this.events.get(sessionId) ?? []; + } + + getCompactions(sessionId: string): readonly CompactionTrace[] { + return this.compactions.get(sessionId) ?? []; + } + + exportTrace(sessionId: string): TraceExport { + return { + sessionId, + events: [...(this.events.get(sessionId) ?? [])], + compactions: [...(this.compactions.get(sessionId) ?? [])], + exportedAt: Date.now(), + }; + } + + clear(sessionId: string): void { + this.events.delete(sessionId); + this.compactions.delete(sessionId); + } +} + +/** Process-wide trace store for harness debugging. */ +export const globalTraceStore = new TraceStore(); diff --git a/infrastructure/ai/harness/turnDrivers/cattyMessageBuilder.ts b/infrastructure/ai/harness/turnDrivers/cattyMessageBuilder.ts new file mode 100644 index 000000000..9917401de --- /dev/null +++ b/infrastructure/ai/harness/turnDrivers/cattyMessageBuilder.ts @@ -0,0 +1,266 @@ +import type { ModelMessage } from 'ai'; +import type { ChatMessage, ChatMessageAttachment, ToolResult } from '../../types'; +import { + buildHistoricalToolReplayMaps, + buildHistoricalToolResultReplayText, + buildHistoricalUserReplayContent, +} from '../../../../components/ai/cattyHistoryReplay'; +import { + buildPromptWithTerminalSelectionAttachments, + isTerminalSelectionAttachment, +} from '../../../../application/state/terminalSelectionAttachment'; +import { + getOpenAIChatAssistantFieldsForHistoryMessage, + isProviderContinuationForSource, + type OpenAIChatAssistantFields, + type ProviderContinuation, +} from '../../providerContinuation'; +import { + toAssistantModelContent, + type AssistantContentPart, + type CattyProviderContinuationContext, +} from '../../../../components/ai/hooks/aiChatStreamingSupport'; + +const OPENAI_CHAT_ASSISTANT_FIELDS = Symbol('netcatty.openAIChatAssistantFields'); + +type ModelMessageWithOpenAIChatFields = ModelMessage & { + [OPENAI_CHAT_ASSISTANT_FIELDS]?: OpenAIChatAssistantFields; +}; + +function rememberOpenAIChatAssistantFields( + message: ModelMessage, + fields: OpenAIChatAssistantFields | undefined, + fieldsByMessage: Map, +): void { + fieldsByMessage.set(message, fields); + (message as ModelMessageWithOpenAIChatFields)[OPENAI_CHAT_ASSISTANT_FIELDS] = fields; +} + +function getRememberedOpenAIChatAssistantFields( + message: ModelMessage, + fieldsByMessage: Map, +): OpenAIChatAssistantFields | undefined { + if (fieldsByMessage.has(message)) return fieldsByMessage.get(message); + return (message as ModelMessageWithOpenAIChatFields)[OPENAI_CHAT_ASSISTANT_FIELDS]; +} + +function modelMessageHasToolCall(message: ModelMessage): boolean { + if (message.role !== 'assistant' || !Array.isArray(message.content)) return false; + return message.content.some((part) => part && typeof part === 'object' && (part as { type?: string }).type === 'tool-call'); +} + +export function collectOpenAIChatAssistantFieldsForMessages( + messages: ModelMessage[], + fieldsByMessage: Map, +): Array { + const fields: Array = []; + let previousMessageWasTool = false; + for (const message of messages) { + const needsContinuationFields = message.role === 'assistant' + && (modelMessageHasToolCall(message) || previousMessageWasTool); + if (needsContinuationFields) { + fields.push(getRememberedOpenAIChatAssistantFields(message, fieldsByMessage)); + } + previousMessageWasTool = message.role === 'tool'; + } + return fields; +} + +export interface BuildCattySdkMessagesInput { + allMessages: ChatMessage[]; + includeCurrentUserMessage: boolean; + trimmed: string; + attachments?: ChatMessageAttachment[]; + continuationContext: CattyProviderContinuationContext; + preserveTerminalToolResults?: ReadonlySet; + fieldsByMessage: Map; +} + +export function buildCattySdkMessages(input: BuildCattySdkMessagesInput): ModelMessage[] { + const { + allMessages, + includeCurrentUserMessage, + trimmed, + attachments, + continuationContext, + preserveTerminalToolResults = new Set(), + fieldsByMessage, + } = input; + + const { resolvedToolCallsByAssistant, toolCallByToolResult } = buildHistoricalToolReplayMaps(allMessages); + const nextFieldsByMessage = new Map(); + const sdkMessages: ModelMessage[] = []; + let previousHistoryMessageWasToolResult = false; + + for (const m of allMessages) { + const currentMessageFollowsToolResult = previousHistoryMessageWasToolResult; + if (m.role === 'user') { + const messageAttachments = m.attachments ?? m.images; + sdkMessages.push({ + role: 'user', + content: buildHistoricalUserReplayContent(m.content, messageAttachments ?? []), + }); + } else if (m.role === 'assistant') { + const activeContinuation = isProviderContinuationForSource( + m.providerContinuation, + continuationContext.source, + ) + ? m.providerContinuation + : undefined; + const openAIChatAssistantFields = getOpenAIChatAssistantFieldsForHistoryMessage( + m, + continuationContext.source, + ); + if (m.toolCalls?.length) { + const resolvedToolCalls = resolvedToolCallsByAssistant.get(m); + const resolvedCalls = resolvedToolCalls + ? m.toolCalls.filter(tc => resolvedToolCalls.has(tc)) + : []; + const contentParts: AssistantContentPart[] = []; + if (resolvedCalls.length > 0) { + for (const part of activeContinuation?.reasoningParts ?? []) { + if (!part.text && !part.providerOptions) continue; + contentParts.push({ + type: 'reasoning' as const, + text: part.text, + ...(part.providerOptions ? { providerOptions: part.providerOptions } : {}), + }); + } + } + if (m.content) { + contentParts.push({ + type: 'text' as const, + text: m.content, + ...(activeContinuation?.textProviderOptions ? { providerOptions: activeContinuation.textProviderOptions } : {}), + }); + } + for (const tc of resolvedCalls) { + const providerOptions = activeContinuation?.toolCallProviderOptionsById?.[tc.id]; + contentParts.push({ + type: 'tool-call' as const, + toolCallId: tc.id, + toolName: tc.name, + input: tc.arguments ?? {}, + ...(providerOptions ? { providerOptions } : {}), + }); + } + if (contentParts.length > 0) { + const message: ModelMessage = { role: 'assistant', content: toAssistantModelContent(contentParts) }; + sdkMessages.push(message); + if (resolvedCalls.length > 0) { + rememberOpenAIChatAssistantFields(message, openAIChatAssistantFields, nextFieldsByMessage); + } + } + } else if (m.content) { + const contentParts: AssistantContentPart[] = []; + for (const part of activeContinuation?.reasoningParts ?? []) { + if (!part.text && !part.providerOptions) continue; + contentParts.push({ + type: 'reasoning' as const, + text: part.text, + ...(part.providerOptions ? { providerOptions: part.providerOptions } : {}), + }); + } + contentParts.push({ + type: 'text' as const, + text: m.content, + ...(activeContinuation?.textProviderOptions ? { providerOptions: activeContinuation.textProviderOptions } : {}), + }); + const message: ModelMessage = { + role: 'assistant', + content: toAssistantModelContent(contentParts), + }; + sdkMessages.push(message); + if (currentMessageFollowsToolResult) { + rememberOpenAIChatAssistantFields(message, openAIChatAssistantFields, nextFieldsByMessage); + } + } + } else if (m.role === 'tool' && m.toolResults?.length) { + sdkMessages.push({ + role: 'tool', + content: m.toolResults.map(tr => { + const toolCall = toolCallByToolResult.get(tr); + return { + type: 'tool-result' as const, + toolCallId: tr.toolCallId, + toolName: toolCall?.name ?? 'unknown', + output: { + type: 'text' as const, + value: buildHistoricalToolResultReplayText(tr, toolCall, { + preserveTerminalOutput: preserveTerminalToolResults.has(tr), + }), + }, + }; + }), + }); + } + previousHistoryMessageWasToolResult = m.role === 'tool' && !!m.toolResults?.length; + } + + if (includeCurrentUserMessage) { + if (attachments?.length) { + const modelText = buildPromptWithTerminalSelectionAttachments(trimmed, attachments); + const modelAttachments = attachments.filter( + (attachment) => !isTerminalSelectionAttachment(attachment), + ); + if (!modelAttachments.length) { + sdkMessages.push({ role: 'user', content: modelText }); + } else { + const parts: Array<{ type: 'text'; text: string } | { type: 'image'; image: string; mediaType?: string } | { type: 'file'; data: string; mediaType: string; filename?: string }> = []; + parts.push({ type: 'text', text: modelText }); + for (const att of modelAttachments) { + if (att.mediaType.startsWith('image/')) { + parts.push({ type: 'image', image: att.base64Data, mediaType: att.mediaType }); + } else { + parts.push({ type: 'file', data: att.base64Data, mediaType: att.mediaType, filename: att.filename }); + } + } + sdkMessages.push({ role: 'user', content: parts }); + } + } else { + sdkMessages.push({ role: 'user', content: trimmed }); + } + } + + for (const [message, fields] of nextFieldsByMessage.entries()) { + fieldsByMessage.set(message, fields); + } + + return sdkMessages; +} + +export function collectToolResultsAfterMessage( + messages: ChatMessage[], + messageId: string, +): Set { + const results = new Set(); + let afterMessage = false; + for (const message of messages) { + if (message.id === messageId) { + afterMessage = true; + continue; + } + if (!afterMessage || message.role !== 'tool' || !message.toolResults?.length) continue; + for (const result of message.toolResults) { + results.add(result); + } + } + return results; +} + +export function createContinuationContext( + providerConfigId: string, + providerType: string, + modelId: string, +): CattyProviderContinuationContext { + return { + source: { + providerConfigId, + providerType, + modelId, + }, + openAIChatAssistantFields: [], + }; +} + +export type { CattyProviderContinuationContext, ProviderContinuation }; diff --git a/infrastructure/ai/harness/turnDrivers/cattyStreamProcessor.ts b/infrastructure/ai/harness/turnDrivers/cattyStreamProcessor.ts new file mode 100644 index 000000000..b4032c941 --- /dev/null +++ b/infrastructure/ai/harness/turnDrivers/cattyStreamProcessor.ts @@ -0,0 +1,358 @@ +import { streamText, stepCountIs, type ModelMessage } from 'ai'; +import { classifyError } from '../../errorClassifier'; +import { isRequestTooLargeError } from '../../errorClassifier'; +import { isSdkStreamStateError } from '../../shared/streamStateErrors'; +import { + createCattyRequestTooLargeRetryError, + hadToolProgressBeforeRequestTooLarge, +} from '../../cattyRequestTooLargeRetry'; +import { mapCattyStreamChunkToAgentEvents } from '../agentEventAdapter'; +import type { AgentEvent } from '../types'; +import type { ProviderAdvancedParams } from '../../types'; +import { createModelFromConfig } from '../../sdk/providers'; +import { createCattyToolsFromCatalog } from '../capabilityTools'; +import { + extractProviderContinuationFromRawChunk, + mergeProviderContinuation, + normalizeProviderContinuationOptions, + withProviderContinuationSource, + type ProviderContinuation, +} from '../../providerContinuation'; +import { + generateId, + isToolResultError, + type CattyProviderContinuationContext, + type ErrorChunk, + type RawChunk, + type ReasoningChunk, + type StreamChunk, + type TextDeltaChunk, + type ToolCallChunk, + type ToolResultChunk, +} from '../../../../components/ai/hooks/aiChatStreamingSupport'; +import type { ChatMessage } from '../../types'; + +export type CattyModel = ReturnType; +export type CattyTools = ReturnType; + +export interface CattyStreamUiSink { + addMessageToSession: (sessionId: string, message: ChatMessage) => void; + updateMessageById: (sessionId: string, messageId: string, updater: (msg: ChatMessage) => ChatMessage) => void; +} + +export interface ProcessCattyStreamInput { + streamSessionId: string; + model: CattyModel; + systemPrompt: string; + tools: CattyTools; + sdkMessages: ModelMessage[]; + signal: AbortSignal; + currentAssistantMsgId: string; + maxIterations: number; + advancedParams?: ProviderAdvancedParams; + continuationContext?: CattyProviderContinuationContext; + turnId?: string; + onAgentEvent?: (event: AgentEvent) => void; + prepareStep?: (args: { stepNumber: number; messages: ModelMessage[] }) => Promise<{ messages: ModelMessage[] } | undefined>; + ui: CattyStreamUiSink; +} + +export async function processCattyStream(input: ProcessCattyStreamInput): Promise<{ usage?: { promptTokens?: number; completionTokens?: number; totalTokens?: number } }> { + const { + streamSessionId, + model, + systemPrompt, + tools, + sdkMessages, + signal, + currentAssistantMsgId, + maxIterations, + advancedParams, + continuationContext, + turnId, + onAgentEvent, + prepareStep, + ui, + } = input; + + const result = streamText({ + model, + messages: sdkMessages, + system: systemPrompt, + tools, + stopWhen: stepCountIs(maxIterations), + abortSignal: signal, + includeRawChunks: true, + ...(prepareStep ? { + prepareStep: async ({ stepNumber, messages }) => { + const prepared = await prepareStep({ stepNumber, messages }); + return prepared ?? { messages }; + }, + } : {}), + ...(advancedParams?.maxTokens != null && { maxOutputTokens: advancedParams.maxTokens }), + ...(advancedParams?.temperature != null && { temperature: advancedParams.temperature }), + ...(advancedParams?.topP != null && { topP: advancedParams.topP }), + ...(advancedParams?.frequencyPenalty != null && { frequencyPenalty: advancedParams.frequencyPenalty }), + ...(advancedParams?.presencePenalty != null && { presencePenalty: advancedParams.presencePenalty }), + }); + + let activeMsgId = currentAssistantMsgId; + let lastAddedRole: 'assistant' | 'tool' = 'assistant'; + let hadToolProgress = false; + const reader = result.fullStream.getReader(); + + let pendingText = ''; + let rafId: number | null = null; + + const ensureAssistantMessage = (): string => { + if (lastAddedRole !== 'tool') return activeMsgId; + const newId = generateId(); + ui.addMessageToSession(streamSessionId, { + id: newId, + role: 'assistant', + content: '', + timestamp: Date.now(), + }); + activeMsgId = newId; + lastAddedRole = 'assistant'; + return activeMsgId; + }; + + const updateAssistantContinuation = ( + messageId: string, + continuation: ProviderContinuation | undefined, + thinkingText = '', + ) => { + if (!continuation && !thinkingText) return; + const sourcedContinuation = withProviderContinuationSource(continuation, continuationContext?.source); + ui.updateMessageById(streamSessionId, messageId, msg => { + const providerContinuation = mergeProviderContinuation(msg.providerContinuation, sourcedContinuation); + return { + ...msg, + ...(providerContinuation ? { providerContinuation } : {}), + ...(thinkingText ? { thinking: (msg.thinking || '') + thinkingText } : {}), + }; + }); + }; + + const getOpenAIReasoningText = (continuation: ProviderContinuation | undefined): string => { + const reasoningContent = continuation?.openAIChatAssistantFields?.reasoning_content; + return typeof reasoningContent === 'string' ? reasoningContent : ''; + }; + + const flushText = () => { + if (pendingText) { + const text = pendingText; + pendingText = ''; + if (lastAddedRole === 'tool') { + const newId = generateId(); + ui.addMessageToSession(streamSessionId, { + id: newId, + role: 'assistant', + content: text, + timestamp: Date.now(), + }); + activeMsgId = newId; + lastAddedRole = 'assistant'; + } else { + ui.updateMessageById(streamSessionId, activeMsgId, msg => ({ + ...msg, + content: msg.content + text, + })); + } + } + rafId = null; + }; + + const cancelPendingFlush = () => { + if (rafId !== null) { + cancelAnimationFrame(rafId); + rafId = null; + } + }; + + try { + while (true) { + let readResult: ReadableStreamReadResult; + try { + readResult = await reader.read(); + } catch (readErr) { + if (isRequestTooLargeError(readErr)) { + throw createCattyRequestTooLargeRetryError(readErr, hadToolProgress); + } + throw readErr; + } + const { done, value } = readResult; + if (done) break; + const chunk = value as StreamChunk; + for (const agentEvent of mapCattyStreamChunkToAgentEvents(chunk, { + sessionId: streamSessionId, + chatSessionId: streamSessionId, + turnId, + })) { + onAgentEvent?.(agentEvent); + } + switch (chunk.type) { + case 'text': + case 'text-delta': { + const typedChunk = chunk as TextDeltaChunk; + const text = typedChunk.text ?? typedChunk.textDelta; + const providerOptions = normalizeProviderContinuationOptions(typedChunk.providerMetadata); + if (providerOptions) { + const messageId = ensureAssistantMessage(); + updateAssistantContinuation(messageId, { textProviderOptions: providerOptions }); + } + if (text) { + pendingText += text; + if (rafId === null) { + rafId = requestAnimationFrame(flushText); + } + } + break; + } + case 'reasoning': + case 'reasoning-start': + case 'reasoning-delta': { + cancelPendingFlush(); + flushText(); + const typedChunk = chunk as ReasoningChunk; + const rText = typedChunk.text ?? typedChunk.textDelta ?? typedChunk.delta ?? ''; + const providerOptions = normalizeProviderContinuationOptions(typedChunk.providerMetadata); + const continuation = rText || providerOptions + ? { + reasoningParts: [{ + text: rText, + ...(providerOptions ? { providerOptions } : {}), + }], + } satisfies ProviderContinuation + : undefined; + if (continuation || rText) { + const messageId = ensureAssistantMessage(); + updateAssistantContinuation(messageId, continuation, rText); + } + break; + } + case 'raw': { + const typedChunk = chunk as RawChunk; + const continuation = extractProviderContinuationFromRawChunk(typedChunk.rawValue); + if (continuation) { + cancelPendingFlush(); + flushText(); + const messageId = ensureAssistantMessage(); + updateAssistantContinuation(messageId, continuation, getOpenAIReasoningText(continuation)); + } + break; + } + case 'reasoning-end': + case 'text-start': + case 'text-end': + case 'start': + case 'finish': + case 'start-step': + case 'finish-step': + break; + case 'tool-call': { + cancelPendingFlush(); + flushText(); + const typedChunk = chunk as ToolCallChunk; + hadToolProgress = true; + const messageId = ensureAssistantMessage(); + const providerOptions = normalizeProviderContinuationOptions(typedChunk.providerMetadata); + ui.updateMessageById(streamSessionId, messageId, msg => ({ + ...msg, + toolCalls: [...(msg.toolCalls || []), { + id: typedChunk.toolCallId, + name: typedChunk.toolName, + arguments: (typedChunk.input ?? typedChunk.args) as Record, + }], + executionStatus: 'running', + statusText: undefined, + })); + if (providerOptions) { + updateAssistantContinuation(messageId, { + toolCallProviderOptionsById: { + [typedChunk.toolCallId]: providerOptions, + }, + }); + } + break; + } + case 'tool-result': { + cancelPendingFlush(); + flushText(); + const typedChunk = chunk as ToolResultChunk; + hadToolProgress = true; + ui.updateMessageById(streamSessionId, activeMsgId, msg => + msg.role === 'assistant' && msg.executionStatus === 'running' + ? { ...msg, executionStatus: 'completed', statusText: undefined } : msg, + ); + const toolOutput = typedChunk.output ?? typedChunk.result; + const toolError = isToolResultError(toolOutput); + ui.addMessageToSession(streamSessionId, { + id: generateId(), + role: 'tool', + content: '', + toolResults: [{ + toolCallId: typedChunk.toolCallId, + content: typeof toolOutput === 'string' + ? toolOutput + : JSON.stringify(toolOutput), + isError: toolError, + }], + timestamp: Date.now(), + executionStatus: 'completed', + }); + lastAddedRole = 'tool'; + break; + } + case 'error': { + const typedChunk = chunk as ErrorChunk; + if (isSdkStreamStateError(typedChunk.error)) { + console.warn('[Catty] suppressed SDK stream state error:', typedChunk.error); + break; + } + if (isRequestTooLargeError(typedChunk.error)) { + cancelPendingFlush(); + flushText(); + throw createCattyRequestTooLargeRetryError( + typedChunk.error, + hadToolProgress, + ); + } + cancelPendingFlush(); + flushText(); + ui.updateMessageById(streamSessionId, activeMsgId, msg => ({ + ...msg, + statusText: '', + executionStatus: msg.executionStatus === 'running' ? 'failed' : msg.executionStatus, + })); + ui.addMessageToSession(streamSessionId, { + id: generateId(), + role: 'assistant', + content: '', + errorInfo: classifyError(typedChunk.error), + timestamp: Date.now(), + }); + break; + } + default: + break; + } + } + } finally { + cancelPendingFlush(); + flushText(); + reader.releaseLock(); + } + + const usage = await result.usage; + return { + usage: usage ? { + promptTokens: usage.inputTokens, + completionTokens: usage.outputTokens, + totalTokens: (usage.inputTokens ?? 0) + (usage.outputTokens ?? 0), + } : undefined, + }; +} + +export { hadToolProgressBeforeRequestTooLarge }; diff --git a/infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts b/infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts new file mode 100644 index 000000000..549b6f349 --- /dev/null +++ b/infrastructure/ai/harness/turnDrivers/cattyTurnDriver.ts @@ -0,0 +1,318 @@ +import type { ModelMessage } from 'ai'; +import type { OpenAIChatAssistantFields } from '../../providerContinuation'; +import { + DEFAULT_CONTEXT_WINDOW_TOKENS, + estimateUnknownTokens, + resolveContextWindow, +} from '../../contextCompaction'; +import { buildSystemPrompt } from '../../cattyAgent/systemPrompt'; +import { isWebSearchReady } from '../../types'; +import { createModelFromConfig } from '../../sdk/providers'; +import { createCattyToolsFromCatalog } from '../capabilityTools'; +import { + compactCattyMessages, + prepareCattyMessagesForStream, +} from '../cattyRuntime'; +import { clearChatSessionCancelled } from '../agentStop'; +import { prepareStepContext } from '../contextManager'; +import { isRequestTooLargeError } from '../../errorClassifier'; +import { getNetcattyBridge, generateId, resolveUserSkillsContext } from '../../../../components/ai/hooks/aiChatStreamingSupport'; +import { + buildCattySdkMessages, + collectOpenAIChatAssistantFieldsForMessages, + collectToolResultsAfterMessage, + createContinuationContext, +} from './cattyMessageBuilder'; +import { hadToolProgressBeforeRequestTooLarge, processCattyStream } from './cattyStreamProcessor'; +import type { CattyTurnInput, TurnDriver, TurnDriverContext } from './types'; + +export class CattyTurnDriver implements TurnDriver { + readonly backend = 'catty' as const; + + async run(input: import('./types').TurnInput, ctx: TurnDriverContext): Promise { + if (input.backend !== 'catty') { + throw new Error('CattyTurnDriver received non-catty input'); + } + await runCattyTurn(input, ctx); + } + + abort(): void { + // Abort is handled via AbortSignal on the turn input. + } +} + +async function runCattyTurn(input: CattyTurnInput, ctx: TurnDriverContext): Promise { + const { + chatSessionId: sessionId, + sendScopeKey, + userText: trimmed, + signal, + currentSession, + assistantMsgId, + context, + attachments, + maxIterations, + bridge, + ui, + } = input; + + const netcattyBridge = bridge ?? getNetcattyBridge(); + await clearChatSessionCancelled(sessionId, netcattyBridge); + if (netcattyBridge.aiMcpUpdateSessions) { + await netcattyBridge.aiMcpUpdateSessions(context.terminalSessions, sessionId); + } + const userSkillsContext = await resolveUserSkillsContext( + netcattyBridge, + trimmed, + context.selectedUserSkillSlugs, + ); + const getExecutorContext = context.getExecutorContext ?? (() => ({ + sessions: context.terminalSessions, + workspaceId: context.scopeType === 'workspace' ? context.scopeTargetId : undefined, + workspaceName: context.scopeType === 'workspace' ? context.scopeLabel : undefined, + })); + const tools = createCattyToolsFromCatalog( + netcattyBridge, + getExecutorContext, + context.commandBlocklist, + context.globalPermissionMode, + context.webSearchConfig ?? undefined, + sessionId, + ctx.toolOutputStore, + ctx.toolResultDedup, + ); + + const systemPrompt = buildSystemPrompt({ + scopeType: context.scopeType, + scopeLabel: context.scopeLabel, + hosts: context.terminalSessions, + permissionMode: context.globalPermissionMode, + webSearchEnabled: isWebSearchReady(context.webSearchConfig), + userSkillsContext, + }); + + if (!context.activeProvider) { + ui.reportStreamError(sessionId, signal, 'No AI provider configured. Please configure a provider in Settings → AI.'); + return; + } + + const activeModelId = context.activeModelId || context.activeProvider.defaultModel || ''; + const continuationContext = createContinuationContext( + context.activeProvider.id, + context.activeProvider.providerId, + activeModelId, + ); + + ui.setStreamingForScope(sessionId, true); + + try { + const openAIChatAssistantFieldsByMessage = new Map(); + + const buildSdkMessages = ( + allMessages: import('../../types').ChatMessage[], + includeCurrentUserMessage: boolean, + options: { preserveTerminalToolResults?: ReadonlySet } = {}, + ) => buildCattySdkMessages({ + allMessages, + includeCurrentUserMessage, + trimmed, + attachments: includeCurrentUserMessage ? attachments : undefined, + continuationContext, + preserveTerminalToolResults: options.preserveTerminalToolResults, + fieldsByMessage: openAIChatAssistantFieldsByMessage, + }); + + let model; + try { + model = createModelFromConfig( + { + ...context.activeProvider, + defaultModel: activeModelId, + }, + { + getOpenAIChatAssistantFields: () => continuationContext.openAIChatAssistantFields, + }, + ); + } catch (e) { + console.error('[Catty] Model creation failed:', e); + ui.reportStreamError(sessionId, signal, `Model creation failed: ${e instanceof Error ? e.message : String(e)}`); + return; + } + + const contextWindow = resolveContextWindow({ + provider: context.activeProvider, + modelId: activeModelId, + defaultContextWindow: DEFAULT_CONTEXT_WINDOW_TOKENS, + }); + const outputReserveTokens = Math.min(4096, Math.ceil(contextWindow * 0.05)); + const getRequestReserveTokens = () => outputReserveTokens + estimateUnknownTokens({ + systemPrompt, + toolNames: Object.keys(tools), + openAIChatAssistantFields: Array.from(openAIChatAssistantFieldsByMessage.values()), + }); + + const prepareMessagesForStream = (messages: ModelMessage[]): ModelMessage[] => { + const pruned = prepareCattyMessagesForStream(messages); + continuationContext.openAIChatAssistantFields = collectOpenAIChatAssistantFieldsForMessages( + pruned, + openAIChatAssistantFieldsByMessage, + ); + return pruned; + }; + + const compactMessages = async ( + messages: ModelMessage[], + options: { + force?: boolean; + statusText?: string; + compressForRequestTooLargeRetry?: boolean; + }, + ): Promise => { + const pendingHandles = ctx.toolOutputStore.listPendingHandles(sessionId); + const result = await compactCattyMessages({ + messages, + sessionId, + chatSessionId: sessionId, + provider: context.activeProvider, + modelId: activeModelId || context.activeProvider?.defaultModel, + reservedTokens: getRequestReserveTokens, + model, + abortSignal: signal, + trigger: options.force ? 'force' : options.compressForRequestTooLargeRetry ? '413-retry' : 'pre-turn', + force: options.force, + compressForRequestTooLargeRetry: options.compressForRequestTooLargeRetry, + onStatusText: (text) => { + ui.updateLastMessage(sessionId, msg => ({ ...msg, statusText: options.statusText || text })); + }, + onCompaction: (trace) => { + ctx.emit({ + id: `compaction-${Date.now()}`, + type: 'compaction', + trace, + } as import('../types').AgentEvent); + if (options.compressForRequestTooLargeRetry && trace.did413Fallback) { + console.warn('[Catty] Request content compressed after forced context compaction.'); + } + }, + reinjection: { + permissionMode: context.permissionMode ?? context.globalPermissionMode, + sessionScopeSummary: pendingHandles.length + ? `Pending tool output handles: ${pendingHandles.map(h => h.id).join(', ')}` + : undefined, + }, + }); + return result.messages; + }; + + let messagesForStream = buildSdkMessages(currentSession?.messages ?? [], true); + messagesForStream = await compactMessages(messagesForStream, {}); + messagesForStream = prepareMessagesForStream(messagesForStream); + + const runStream = async (streamMessages: ModelMessage[], streamAssistantMsgId: string) => { + const { usage } = await processCattyStream({ + streamSessionId: sessionId, + model, + systemPrompt, + tools, + sdkMessages: streamMessages, + signal, + currentAssistantMsgId: streamAssistantMsgId, + maxIterations, + advancedParams: context.activeProvider?.advancedParams, + continuationContext, + turnId: ctx.turnId, + onAgentEvent: (event) => ctx.emit(event), + prepareStep: async ({ stepNumber, messages }) => { + const prepared = await prepareStepContext({ + messages, + stepNumber, + sessionId, + chatSessionId: sessionId, + providerId: context.activeProvider?.providerId, + modelId: activeModelId, + toolOutputStore: ctx.toolOutputStore, + }); + return { messages: prepared.messages }; + }, + ui: { + addMessageToSession: ui.addMessageToSession, + updateMessageById: ui.updateMessageById, + }, + }); + + if (usage?.totalTokens) { + ctx.emit({ + id: `usage-${ctx.turnId}`, + type: 'usage', + promptTokens: usage.promptTokens ?? 0, + completionTokens: usage.completionTokens ?? 0, + totalTokens: usage.totalTokens, + estimated: false, + } as import('../types').AgentEvent); + } + }; + + try { + await runStream(messagesForStream, assistantMsgId); + } catch (streamErr) { + if (signal.aborted || !isRequestTooLargeError(streamErr)) { + throw streamErr; + } + + console.warn('[Catty] Request hit HTTP 413; forcing context compaction and retrying once.', streamErr); + const statusText = 'Request was too large. Compacting context and retrying...'; + const hadToolProgress = hadToolProgressBeforeRequestTooLarge(streamErr); + let retryBaseMessages = messagesForStream; + let retryAssistantMsgId = assistantMsgId; + if (hadToolProgress) { + const latestSession = ui.getLatestSession?.(sessionId); + if (latestSession) { + retryBaseMessages = buildSdkMessages(latestSession.messages, false, { + preserveTerminalToolResults: collectToolResultsAfterMessage( + latestSession.messages, + assistantMsgId, + ), + }); + } + retryAssistantMsgId = generateId(); + ui.addMessageToSession(sessionId, { + id: retryAssistantMsgId, + role: 'assistant', + content: '', + timestamp: Date.now(), + model: activeModelId || context.activeProvider?.defaultModel || '', + providerId: context.activeProvider?.providerId, + statusText, + }); + } else { + ui.updateMessageById(sessionId, assistantMsgId, msg => ({ + ...msg, + content: '', + thinking: undefined, + thinkingDurationMs: undefined, + providerContinuation: undefined, + toolCalls: undefined, + errorInfo: undefined, + executionStatus: undefined, + pendingApproval: undefined, + statusText, + })); + } + const retryMessages = prepareMessagesForStream(await compactMessages(retryBaseMessages, { + force: true, + statusText, + compressForRequestTooLargeRetry: true, + })); + await runStream(retryMessages, retryAssistantMsgId); + } + } catch (err) { + console.error('[Catty] streamText error:', err); + ui.reportStreamError(sessionId, signal, err); + } finally { + ui.updateLastMessage(sessionId, msg => msg.statusText ? { ...msg, statusText: '' } : msg); + ui.setStreamingForScope(sessionId, false); + context.autoTitleSession(sessionId, context.titleText ?? trimmed); + } +} + +export const cattyTurnDriver = new CattyTurnDriver(); diff --git a/infrastructure/ai/harness/turnDrivers/externalSdkTurnDriver.ts b/infrastructure/ai/harness/turnDrivers/externalSdkTurnDriver.ts new file mode 100644 index 000000000..66ac0a799 --- /dev/null +++ b/infrastructure/ai/harness/turnDrivers/externalSdkTurnDriver.ts @@ -0,0 +1,174 @@ +import { getExternalAgentSdkBackend } from '../../managedAgents'; +import { runSdkAgentTurn, type SdkAgentCallbacks } from '../../sdkAgentAdapter'; +import { getNetcattyBridge, generateId, resolveUserSkillsContext, isToolResultError } from '../../../../components/ai/hooks/aiChatStreamingSupport'; +import type { ExternalTurnInput, TurnDriver, TurnDriverContext } from './types'; + +export class ExternalSdkTurnDriver implements TurnDriver { + readonly backend = 'external-sdk' as const; + + async run(input: import('./types').TurnInput, ctx: TurnDriverContext): Promise { + if (input.backend !== 'external-sdk') { + throw new Error('ExternalSdkTurnDriver received non-external input'); + } + await runExternalTurn(input, ctx); + } + + abort(): void { + // Abort is handled via AbortSignal on the turn input. + } +} + +async function runExternalTurn(input: ExternalTurnInput, ctx: TurnDriverContext): Promise { + const { + chatSessionId: sessionId, + userText: trimmed, + signal, + agentConfig, + attachedImages, + context, + bridge, + ui, + } = input; + + const netcattyBridge = bridge ?? getNetcattyBridge(); + const sdkBackend = getExternalAgentSdkBackend(agentConfig); + + if (!sdkBackend || !netcattyBridge) { + ui.reportStreamError( + sessionId, + signal, + 'This agent has no SDK backend configured. Re-discover it in Settings -> AI.', + ); + ui.setStreamingForScope(sessionId, false); + return; + } + + const userSkillsContext = await resolveUserSkillsContext( + netcattyBridge, + trimmed, + context.selectedUserSkillSlugs, + ); + + const requestId = ctx.turnId; + ui.setStreamingForScope(sessionId, true); + + if (netcattyBridge.aiMcpUpdateSessions) { + await netcattyBridge.aiMcpUpdateSessions(context.terminalSessions, sessionId); + } + + let needsNewAssistantMsg = false; + const maybeCreateAssistantMsg = () => { + if (needsNewAssistantMsg) { + needsNewAssistantMsg = false; + ui.addMessageToSession(sessionId, { + id: generateId(), + role: 'assistant', + content: '', + timestamp: Date.now(), + model: agentConfig.name || 'external', + }); + } + }; + + const callbacks: SdkAgentCallbacks = { + onTextDelta: (text: string) => { + maybeCreateAssistantMsg(); + ui.updateLastMessage(sessionId, msg => ({ + ...msg, + content: msg.content + text, + statusText: undefined, + thinkingDurationMs: msg.thinking && !msg.thinkingDurationMs + ? Date.now() - msg.timestamp : msg.thinkingDurationMs, + })); + }, + onThinkingDelta: (text: string) => { + maybeCreateAssistantMsg(); + ui.updateLastMessage(sessionId, msg => ({ + ...msg, + thinking: (msg.thinking || '') + text, + })); + }, + onThinkingDone: () => { + ui.updateLastMessage(sessionId, msg => ({ + ...msg, + thinkingDurationMs: msg.thinkingDurationMs || (Date.now() - msg.timestamp), + })); + }, + onToolCall: (toolName: string, args: Record, toolCallId?: string) => { + maybeCreateAssistantMsg(); + ui.updateLastMessage(sessionId, msg => ({ + ...msg, + toolCalls: [...(msg.toolCalls || []), { id: toolCallId || `tc_${Date.now()}`, name: toolName, arguments: args }], + executionStatus: 'running', + statusText: undefined, + })); + }, + onToolResult: (toolCallId: string, result: string, toolName?: string) => { + ui.updateLastMessage(sessionId, msg => { + if (msg.role !== 'assistant' || msg.executionStatus !== 'running') return msg; + const updatedToolCalls = toolName && !toolName.includes('sdk_agent_dynamic_tool') && msg.toolCalls + ? msg.toolCalls.map(tc => tc.id === toolCallId && !tc.name ? { ...tc, name: toolName } : tc) + : msg.toolCalls; + return { ...msg, toolCalls: updatedToolCalls, executionStatus: 'completed', statusText: undefined }; + }); + ui.addMessageToSession(sessionId, { + id: generateId(), + role: 'tool', + content: '', + toolResults: [{ toolCallId, content: result, isError: isToolResultError(result) }], + timestamp: Date.now(), + executionStatus: 'completed', + }); + needsNewAssistantMsg = true; + }, + onStatus: (message: string) => { + maybeCreateAssistantMsg(); + ui.updateLastMessage(sessionId, msg => ({ ...msg, statusText: message })); + }, + onSessionId: (externalSessionId: string) => { + context.updateExternalSessionId?.(sessionId, externalSessionId); + }, + onError: (error: string) => { + ui.reportStreamError(sessionId, signal, error); + ui.setStreamingForScope(sessionId, false); + }, + onDone: () => {}, + }; + + try { + await runSdkAgentTurn( + netcattyBridge, + requestId, + sessionId, + agentConfig, + trimmed, + callbacks, + signal, + undefined, + context.selectedAgentModel, + context.existingSessionId, + context.historyMessages, + attachedImages.length > 0 ? attachedImages : undefined, + context.toolIntegrationMode, + context.defaultTargetSession, + userSkillsContext, + { + traceSink: (event) => ctx.emit(event), + skipHarnessTrace: true, + }, + ); + + ctx.emit({ + id: `usage-${ctx.turnId}`, + type: 'usage', + promptTokens: 0, + completionTokens: 0, + totalTokens: Math.ceil(trimmed.length / 4), + estimated: true, + } as import('../types').AgentEvent); + } finally { + ui.setStreamingForScope(sessionId, false); + } +} + +export const externalSdkTurnDriver = new ExternalSdkTurnDriver(); diff --git a/infrastructure/ai/harness/turnDrivers/types.ts b/infrastructure/ai/harness/turnDrivers/types.ts new file mode 100644 index 000000000..ba160c9b3 --- /dev/null +++ b/infrastructure/ai/harness/turnDrivers/types.ts @@ -0,0 +1,139 @@ +import type { ModelMessage } from 'ai'; +import type { + AIPermissionMode, + AIToolIntegrationMode, + AISession, + ChatMessage, + ChatMessageAttachment, + ExternalAgentConfig, + ProviderAdvancedParams, + ProviderConfig, + WebSearchConfig, +} from '../../types'; +import type { ExecutorContext } from '../../cattyAgent/executor'; +import type { AgentBackend, AgentEvent, AgentEventListener } from '../types'; +import type { ToolOutputStore } from '../toolOutputStore'; +import type { ToolResultDedup } from '../toolResultDedup'; +import type { DefaultTargetSessionHint } from '../../sdkAgentAdapter'; +import type { AgentStopBridge } from '../agentStop'; + +export interface TerminalSessionInfo { + sessionId: string; + hostId?: string; + hostname: string; + label: string; + os?: string; + username?: string; + protocol?: string; + shellType?: string; + deviceType?: string; + connected: boolean; + hostChain?: Array<{ hostId: string; label?: string; hostname?: string }>; + activePortForwards?: Array<{ + ruleId: string; + label?: string; + type?: string; + localPort?: number; + status?: string; + }>; +} + +export interface TurnUiCallbacks { + addMessageToSession: (sessionId: string, message: ChatMessage) => void; + updateLastMessage: (sessionId: string, updater: (msg: ChatMessage) => ChatMessage) => void; + updateMessageById: (sessionId: string, messageId: string, updater: (msg: ChatMessage) => ChatMessage) => void; + reportStreamError: (sessionId: string, abortSignal: AbortSignal, err: unknown) => void; + setStreamingForScope: (key: string, val: boolean) => void; + getLatestSession?: (sessionId: string) => AISession | undefined; +} + +export interface CattyTurnContext { + activeProvider: ProviderConfig | undefined; + activeModelId: string; + scopeType: 'terminal' | 'workspace'; + scopeTargetId?: string; + scopeLabel?: string; + globalPermissionMode: AIPermissionMode; + permissionMode?: AIPermissionMode; + commandBlocklist?: string[]; + terminalSessions: TerminalSessionInfo[]; + webSearchConfig?: WebSearchConfig | null; + getExecutorContext?: () => ExecutorContext; + autoTitleSession: (sessionId: string, text: string) => void; + titleText?: string; + selectedUserSkillSlugs?: string[]; +} + +export interface ExternalTurnContext { + existingSessionId?: string; + updateExternalSessionId?: (sessionId: string, externalSessionId: string | undefined) => void; + historyMessages?: Array<{ role: 'user' | 'assistant'; content: string }>; + terminalSessions: TerminalSessionInfo[]; + defaultTargetSession?: DefaultTargetSessionHint; + providers: ProviderConfig[]; + selectedAgentModel?: string; + toolIntegrationMode: AIToolIntegrationMode; + selectedUserSkillSlugs?: string[]; +} + +export interface CattyTurnInput { + backend: 'catty'; + chatSessionId: string; + sendScopeKey: string; + userText: string; + signal: AbortSignal; + currentSession: AISession | undefined; + assistantMsgId: string; + context: CattyTurnContext; + attachments?: ChatMessageAttachment[]; + maxIterations: number; + bridge?: AgentStopBridge | null; + ui: TurnUiCallbacks; +} + +export interface ExternalTurnInput { + backend: 'external-sdk'; + chatSessionId: string; + userText: string; + signal: AbortSignal; + agentConfig: ExternalAgentConfig; + attachedImages: Array<{ base64Data: string; mediaType: string; filename?: string; filePath?: string }>; + context: ExternalTurnContext; + bridge?: Record unknown> | null; + ui: TurnUiCallbacks; +} + +export type TurnInput = CattyTurnInput | ExternalTurnInput; + +export interface TurnResult { + turnId: string; + reason: 'completed' | 'aborted' | 'error'; +} + +export interface TurnDriverContext { + turnId: string; + chatSessionId: string; + sessionId: string; + backend: AgentBackend; + signal: AbortSignal; + emit: (event: Omit & Partial>) => void; + toolOutputStore: ToolOutputStore; + toolResultDedup: ToolResultDedup; + onEvent?: AgentEventListener; +} + +export interface TurnDriver { + readonly backend: AgentBackend; + run(input: TurnInput, ctx: TurnDriverContext): Promise; + abort?(chatSessionId: string): void; +} + +export interface PrepareStepContextInput { + messages: ModelMessage[]; + stepNumber: number; + sessionId: string; + chatSessionId?: string; + providerId?: string | null; + modelId?: string | null; + toolOutputStore?: ToolOutputStore; +} diff --git a/infrastructure/ai/harness/types.ts b/infrastructure/ai/harness/types.ts new file mode 100644 index 000000000..abedfad36 --- /dev/null +++ b/infrastructure/ai/harness/types.ts @@ -0,0 +1,145 @@ +/** + * Unified agent harness event protocol. + * Catty (Vercel AI SDK) and external SDK drivers emit the same shapes. + */ + +export type AgentEventType = + | 'turn_start' + | 'model_delta' + | 'reasoning_delta' + | 'tool_call' + | 'tool_result' + | 'approval_requested' + | 'approval_resolved' + | 'compaction' + | 'usage' + | 'error' + | 'turn_end'; + +export type AgentBackend = 'catty' | 'external-sdk'; + +export type ApprovalOutcome = 'approved' | 'denied' | 'timeout'; + +export type ContextPrepareTrigger = 'pre-turn' | '413-retry' | 'force'; + +export interface AgentEventBase { + id: string; + sessionId: string; + chatSessionId?: string; + backend: AgentBackend; + timestamp: number; + turnId?: string; +} + +export interface TurnStartEvent extends AgentEventBase { + type: 'turn_start'; + backendLabel?: string; +} + +export interface ModelDeltaEvent extends AgentEventBase { + type: 'model_delta'; + text: string; +} + +export interface ReasoningDeltaEvent extends AgentEventBase { + type: 'reasoning_delta'; + text: string; +} + +export interface ToolCallEvent extends AgentEventBase { + type: 'tool_call'; + toolCallId: string; + toolName: string; + args: Record; +} + +export interface ToolResultEvent extends AgentEventBase { + type: 'tool_result'; + toolCallId: string; + toolName?: string; + result: string; + isError?: boolean; +} + +export interface ApprovalRequestedEvent extends AgentEventBase { + type: 'approval_requested'; + toolCallId: string; + toolName: string; + args: Record; +} + +export interface ApprovalResolvedEvent extends AgentEventBase { + type: 'approval_resolved'; + toolCallId: string; + toolName: string; + outcome: ApprovalOutcome; + persistedGrantId?: string; +} + +export type TokenEstimatorKind = 'chars-div-4' | 'openai-heuristic' | 'anthropic-heuristic' | 'google-heuristic'; + +export interface CompactionTrace { + trigger: ContextPrepareTrigger; + estimatedTokensBefore: number; + estimatedTokensAfter: number; + messagesBefore: number; + messagesAfter: number; + compressedMessageCount: number; + retainedTailCount: number; + summaryLength?: number; + didTypedCompression: boolean; + didLlmSummarize: boolean; + did413Fallback: boolean; + estimatorKind?: TokenEstimatorKind; +} + +export interface CompactionEvent extends AgentEventBase { + type: 'compaction'; + trace: CompactionTrace; +} + +export interface ErrorEvent extends AgentEventBase { + type: 'error'; + message: string; + code?: string; + recoverable?: boolean; +} + +export interface UsageEvent extends AgentEventBase { + type: 'usage'; + promptTokens: number; + completionTokens: number; + totalTokens: number; + estimated?: boolean; +} + +export interface TurnEndEvent extends AgentEventBase { + type: 'turn_end'; + reason?: 'completed' | 'aborted' | 'error'; +} + +export type AgentEvent = + | TurnStartEvent + | ModelDeltaEvent + | ReasoningDeltaEvent + | ToolCallEvent + | ToolResultEvent + | ApprovalRequestedEvent + | ApprovalResolvedEvent + | CompactionEvent + | UsageEvent + | ErrorEvent + | TurnEndEvent; + +export type AgentEventListener = (event: AgentEvent) => void; + +export interface ContextPrepareResult { + messages: import('ai').ModelMessage[]; + didAdjust: boolean; + trace?: CompactionTrace; +} + +export interface ExternalBridgeHistoryMessage { + role: 'user' | 'assistant'; + content: string; +} diff --git a/infrastructure/ai/managedAgents.ts b/infrastructure/ai/managedAgents.ts index b0f78ad93..0dda10e84 100644 --- a/infrastructure/ai/managedAgents.ts +++ b/infrastructure/ai/managedAgents.ts @@ -1,4 +1,7 @@ import type { DiscoveredAgent, ExternalAgentConfig } from './types'; +import { getCommandBasename, isPathLikeCommand } from './shared/pathLikeCommand'; + +export { isPathLikeCommand, getCommandBasename }; export type ManagedAgentKey = 'codex' | 'claude' | 'copilot' | 'cursor' | 'codebuddy' | 'opencode'; @@ -11,18 +14,6 @@ const MANAGED_AGENT_META: Record | null | undefined, +): string | undefined { + const command = String(config?.command || '').trim(); + return config?.commandSource === 'manual' && command ? command : undefined; +} diff --git a/infrastructure/ai/quickMessages.ts b/infrastructure/ai/quickMessages.ts index 1c6153da0..10a6ba286 100644 --- a/infrastructure/ai/quickMessages.ts +++ b/infrastructure/ai/quickMessages.ts @@ -150,3 +150,16 @@ export function getSlashCommandItemId(item: SlashCommandItem): string { export function createQuickMessageId(): string { return `qm_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`; } + +/** Built-in slash commands — not stored in user quick messages. */ +export const SYSTEM_BUILTIN_SLASH_COMMANDS = { + stop: { + slug: 'stop', + descriptionKey: 'ai.chat.slashStopDesc', + }, +} as const; + +export function isSystemStopSlashCommand(input: string): boolean { + const trimmed = input.trim(); + return /^\/stop(?:\s|$)/i.test(trimmed) || trimmed.toLowerCase() === '/stop'; +} diff --git a/infrastructure/ai/sdk/tools.ts b/infrastructure/ai/sdk/tools.ts index 36d34c2cc..7e1172a2a 100644 --- a/infrastructure/ai/sdk/tools.ts +++ b/infrastructure/ai/sdk/tools.ts @@ -1,194 +1,6 @@ -import { tool } from 'ai'; -import { z } from 'zod'; -import type { NetcattyBridge } from '../cattyAgent/executor'; -import type { AIPermissionMode } from '../types'; -import type { WebSearchConfig } from '../types'; -import { isWebSearchReady } from '../types'; -import { - executeTerminalExecute, - executeWorkspaceGetInfo, - executeWorkspaceGetSessionInfo, - executeWebSearch, - executeUrlFetch, - type ToolDeps, - type ToolExecResult, -} from '../shared/toolExecutors'; -import { requestApproval } from '../shared/approvalGate'; -import { reserveSessionSlot } from '../shared/sessionExecutionQueue'; -import { truncateTextWithHeadAndTail } from '../requestPayloadCompression'; +import { createCattyToolsFromCatalog, fitTerminalExecuteResultForModel } from '../harness'; -const MAX_LIVE_TERMINAL_STDOUT_CHARS = 24_000; -const MAX_LIVE_TERMINAL_STDERR_CHARS = 12_000; +export { fitTerminalExecuteResultForModel }; -/** Unwrap a shared ToolExecResult into the shape expected by Vercel AI SDK tool results. */ -function unwrap(r: ToolExecResult): T | { error: string } { - if (r.ok === false) return { error: r.error }; - return r.data; -} - -export function fitTerminalExecuteResultForModel(result: { - stdout: string; - stderr: string; - exitCode: number | null; -}): { stdout: string; stderr: string; exitCode: number | null } { - return { - ...result, - stdout: truncateTextWithHeadAndTail(result.stdout, MAX_LIVE_TERMINAL_STDOUT_CHARS), - stderr: truncateTextWithHeadAndTail(result.stderr, MAX_LIVE_TERMINAL_STDERR_CHARS), - }; -} - -/** - * Create Catty Agent tools using the Vercel AI SDK `tool()` helper with zod schemas. - * - * @param bridge - The Electron IPC bridge for executing operations - * @param context - Workspace/session context available to the agent - * @param commandBlocklist - Optional command blocklist patterns for safety checks - * @param permissionMode - Permission mode for tool execution gating - */ -export function createCattyTools( - bridge: NetcattyBridge, - context: ToolDeps['context'], - commandBlocklist?: string[], - permissionMode: AIPermissionMode = 'confirm', - webSearchConfig?: WebSearchConfig, - chatSessionId?: string, -) { - const deps: ToolDeps = { bridge, context, commandBlocklist, permissionMode, webSearchConfig, chatSessionId }; - - return { - terminal_execute: tool({ - description: - 'Execute a shell command on the specified terminal session. ' + - "The command runs in the session's shell and output is returned when complete.", - inputSchema: z.object({ - sessionId: z.string().describe('The terminal session ID to execute the command on.'), - command: z.string().describe('The shell command to execute in the target session.'), - }), - // No needsApproval — approval is handled inside execute via the approval gate. - execute: async ({ sessionId, command }, { toolCallId, abortSignal }) => { - // Snap our place in the per-session execution queue *first*, - // synchronously, so the eventual command-run order matches the - // LLM's tool_use emission order regardless of how long each - // call's approval prompt takes to settle. Vercel AI SDK - // dispatches every tool_use block in a turn through - // `Promise.all(...)`, so the three executes for "A then B then - // C" all start at the same instant; if we deferred slot - // reservation until after approval, B's approval could land - // first and run B before A. Reserving up front fixes that. - // - // The bridge-side mutex (mcpServerBridge.reserveSessionExecution) - // stays as defense-in-depth for non-LLM IPC paths - // (terminal_start, MCP, etc.) — this queue just keeps the - // renderer-side LLM path from racing into it. - const queueKey = `${chatSessionId ?? 'global'}:${sessionId}`; - const slot = reserveSessionSlot(queueKey); - try { - // In confirm mode, await user approval. Approvals run *while* - // the slot is held but before the serialized work, so multiple - // parallel tool_use blocks each surface their own approval - // card immediately and the user can approve/deny in any - // order — the queue still drains in reservation order. - if (permissionMode === 'confirm') { - const approved = await requestApproval(toolCallId, 'terminal_execute', { sessionId, command }, chatSessionId); - if (!approved) { - return { error: 'User denied command execution.' }; - } - } - if (abortSignal?.aborted) { - return { error: 'Command cancelled before it could start.' }; - } - await slot.ready; - if (abortSignal?.aborted) { - return { error: 'Command cancelled before it could start.' }; - } - // There's a tiny race between this check and the main-process - // `mcpServerBridge.reserveSessionExecution` registering the new - // exec into the cancellation tracker: `handleStop` issues a - // cancel IPC and the user's abort signal fires, but if our - // `aiExec` IPC was already in transit, the cancel may run - // before the exec has registered — and find nothing to - // cancel. Re-issue the cancel from the abort listener so a - // duplicate `aiCattyCancelExec` lands once the registration - // is complete. The cancel is idempotent (it only acts on - // entries it finds in `activePtyExecs`), so issuing twice is - // harmless. - const cancelOnAbort = () => { - if (chatSessionId) { - void bridge.aiCattyCancelExec?.(chatSessionId); - } - }; - abortSignal?.addEventListener('abort', cancelOnAbort, { once: true }); - try { - const result = await executeTerminalExecute(deps, { sessionId, command }); - if (result.ok === false) return unwrap(result); - return fitTerminalExecuteResultForModel(result.data); - } finally { - abortSignal?.removeEventListener('abort', cancelOnAbort); - } - } finally { - slot.release(); - } - }, - }), - - workspace_get_info: tool({ - description: - 'Get information about the current workspace, including all terminal sessions ' + - 'and their connection status. No parameters required.', - inputSchema: z.object({}), - execute: async () => { - return unwrap(executeWorkspaceGetInfo(deps)); - }, - }), - - workspace_get_session_info: tool({ - description: - 'Get detailed information about a specific terminal or SFTP session, including ' + - 'its connection status, protocol, shell hints, and session metadata.', - inputSchema: z.object({ - sessionId: z.string().describe('The session ID to get information about.'), - }), - execute: async ({ sessionId }) => { - return unwrap(executeWorkspaceGetSessionInfo(deps, { sessionId })); - }, - }), - - // -- Web Search (conditional on fully configured webSearchConfig) -- - ...(isWebSearchReady(webSearchConfig) ? { - web_search: tool({ - description: - 'Search the web for current information. Use this when the user asks about recent events, ' + - 'news, or facts you are unsure about. Returns a list of search results with titles, URLs, and content snippets.', - inputSchema: z.object({ - query: z.string().describe('The search query to look up on the web.'), - maxResults: z - .number() - .optional() - .describe('Maximum number of search results to return. If omitted, uses the configured default.'), - }), - execute: async ({ query, maxResults }) => { - return unwrap(await executeWebSearch(deps, { query, maxResults })); - }, - }), - } : {}), - - // -- URL Fetch (always available, read-only like sftp_read_file) -- - url_fetch: tool({ - description: - 'Fetch and read the content of a web URL. Use this when the user provides a URL and wants ' + - 'you to read or summarize its content. Returns the page content as text.', - inputSchema: z.object({ - url: z.string().describe('The HTTPS URL to fetch. Must start with https://.'), - maxLength: z - .number() - .optional() - .default(50000) - .describe('Maximum number of characters to return. Defaults to 50000.'), - }), - execute: async ({ url, maxLength }) => { - return unwrap(await executeUrlFetch(deps, { url, maxLength })); - }, - }), - }; -} +/** @deprecated Use createCattyToolsFromCatalog from harness directly. */ +export const createCattyTools = createCattyToolsFromCatalog; diff --git a/infrastructure/ai/sdkAgentAdapter.ts b/infrastructure/ai/sdkAgentAdapter.ts index eaf93c20e..98c61f46f 100644 --- a/infrastructure/ai/sdkAgentAdapter.ts +++ b/infrastructure/ai/sdkAgentAdapter.ts @@ -6,7 +6,9 @@ */ import type { AIToolIntegrationMode, ExternalAgentConfig } from './types'; -import { getExternalAgentSdkBackend } from './managedAgents'; +import { getExternalAgentSdkBackend, getManualAgentCommand } from './managedAgents'; +import { encodeSdkSessionIdentity } from './harness/sdkSessionIdentity'; +import { globalTraceStore, mapSdkStreamEventToAgentEvents } from './harness'; import { decryptField } from '../persistence/secureFieldAdapter'; export interface DefaultTargetSessionHint { @@ -63,32 +65,6 @@ interface StreamEvent { [key: string]: unknown; } -const SDK_SESSION_ID_PREFIX = 'netcatty-sdk-session:'; - -function getManualAgentCommand(config: ExternalAgentConfig): string | undefined { - const command = String(config.command || '').trim(); - return config.commandSource === 'manual' && command ? command : undefined; -} - -function encodeSdkSessionIdentity( - sessionId: string, - sdkBackend?: string, - binPath?: string, -): string { - if (!sessionId || !sdkBackend) return sessionId; - return `${SDK_SESSION_ID_PREFIX}${encodeURIComponent(JSON.stringify({ - v: 1, - id: sessionId, - backend: sdkBackend, - binPath: binPath || '', - }))}`; -} - -/** - * Run one managed SDK agent turn. - * Sends the prompt to the main process and listens for streamed events. - * Stream events are forwarded back via IPC. - */ export interface FileAttachment { base64Data: string; mediaType: string; @@ -182,6 +158,10 @@ export async function runSdkAgentTurn( toolIntegrationMode?: AIToolIntegrationMode, defaultTargetSession?: DefaultTargetSessionHint, userSkillsContext?: string, + harnessOptions?: { + traceSink?: (event: import('./harness/types').AgentEvent) => void; + skipHarnessTrace?: boolean; + }, ): Promise { const sdkBridge = bridge as unknown as SdkAgentBridge; const sdkBackend = getExternalAgentSdkBackend(config); @@ -209,7 +189,36 @@ export async function runSdkAgentTurn( const agentCommand = getManualAgentCommand(config); // Set up event listeners before starting stream + if (!harnessOptions?.skipHarnessTrace) { + globalTraceStore.append({ + id: `turn-start-${requestId}`, + type: 'turn_start', + sessionId: chatSessionId, + chatSessionId, + backend: 'external-sdk', + timestamp: Date.now(), + backendLabel: sdkBackend, + }); + } + + const appendHarnessEvent = (event: import('./harness/types').AgentEvent) => { + if (harnessOptions?.traceSink) { + harnessOptions.traceSink(event); + return; + } + if (!harnessOptions?.skipHarnessTrace) { + globalTraceStore.append(event); + } + }; + const unsubEvent = sdkBridge.onAiSdkAgentEvent(requestId, (event: StreamEvent) => { + for (const agentEvent of mapSdkStreamEventToAgentEvents(event, { + sessionId: chatSessionId, + chatSessionId, + turnId: requestId, + })) { + appendHarnessEvent(agentEvent); + } const streamFailed = handleStreamEvent(event, callbacks); if (streamFailed) { settle(); diff --git a/infrastructure/ai/shared/approvalConstants.ts b/infrastructure/ai/shared/approvalConstants.ts new file mode 100644 index 000000000..769feb569 --- /dev/null +++ b/infrastructure/ai/shared/approvalConstants.ts @@ -0,0 +1,8 @@ +/** Renderer-side Catty tool approval timeout (5 minutes). */ +export const CATTY_APPROVAL_TIMEOUT_MS = 5 * 60 * 1000; + +/** + * MCP / external SDK approval timeout aligned with Codex MCP limits (~110s). + * Kept separate from Catty because external agents block on main-process IPC. + */ +export const MCP_APPROVAL_TIMEOUT_MS = 110 * 1000; diff --git a/infrastructure/ai/shared/approvalGate.ts b/infrastructure/ai/shared/approvalGate.ts index eb39b5fa7..ae54d3b5d 100644 --- a/infrastructure/ai/shared/approvalGate.ts +++ b/infrastructure/ai/shared/approvalGate.ts @@ -15,8 +15,18 @@ * interference when stopping or cancelling sessions. */ -/** Default timeout for unanswered approval prompts (5 minutes). */ -const APPROVAL_TIMEOUT_MS = 5 * 60 * 1000; +import { CATTY_APPROVAL_TIMEOUT_MS } from './approvalConstants'; +import { localStorageAdapter } from '../../persistence/localStorageAdapter'; +import { STORAGE_KEY_AI_PERMISSION_GRANTS } from '../../config/storageKeys'; +import { globalTraceStore } from '../harness/traceStore'; +import { + getActivePermissionGrants, + matchPermissionGrant, + resolveCapabilityId, + sanitizePermissionGrants, + setActivePermissionGrants, + type PermissionGrantRule, +} from '../harness/permissionGrants'; export interface ApprovalRequest { toolCallId: string; @@ -24,6 +34,53 @@ export interface ApprovalRequest { args: Record; /** Optional chat session scope — used to clear only relevant approvals on stop */ chatSessionId?: string; + capabilityId?: string; +} + +export interface ResolveApprovalOptions { + approved: boolean; + persistGrant?: PermissionGrantRule; + persistGrants?: PermissionGrantRule[]; +} + +export type GrantPersister = (rule: PermissionGrantRule) => void; + +let grantPersister: GrantPersister | null = null; +const grantPersisterStack: GrantPersister[] = []; +let grantsHydrated = false; + +function refreshPermissionGrantsFromStorage(): void { + if (typeof window === 'undefined') return; + setActivePermissionGrants( + sanitizePermissionGrants(localStorageAdapter.read(STORAGE_KEY_AI_PERMISSION_GRANTS)), + ); + grantsHydrated = true; +} + +function ensureGrantsHydrated(): void { + if (grantsHydrated) return; + refreshPermissionGrantsFromStorage(); +} + +export function setGrantPersister(persister: GrantPersister | null): void { + grantPersisterStack.length = 0; + if (persister) { + grantPersisterStack.push(persister); + } + grantPersister = persister; +} + +/** Register a grant persister; supports multiple mounted AI panels via a stack. */ +export function registerGrantPersister(persister: GrantPersister): () => void { + grantPersisterStack.push(persister); + grantPersister = persister; + return () => { + const idx = grantPersisterStack.lastIndexOf(persister); + if (idx >= 0) { + grantPersisterStack.splice(idx, 1); + } + grantPersister = grantPersisterStack[grantPersisterStack.length - 1] ?? null; + }; } // Pending approval entries keyed by toolCallId. @@ -42,6 +99,59 @@ const listeners = new Set(); type ApprovalClearedListener = (toolCallIds: string[]) => void; const clearedListeners = new Set(); +let approvalEventCounter = 0; + +function nextApprovalEventId(prefix: string): string { + approvalEventCounter += 1; + return `${prefix}-${Date.now()}-${approvalEventCounter}`; +} + +function emitApprovalEvent( + type: 'approval_requested' | 'approval_resolved', + request: ApprovalRequest, + extra?: { outcome?: 'approved' | 'denied' | 'timeout'; persistedGrantId?: string }, +): void { + const sessionId = request.chatSessionId ?? 'global'; + const capabilityId = request.capabilityId ?? resolveCapabilityId(request.toolName); + const base = { + sessionId, + chatSessionId: request.chatSessionId, + backend: 'catty' as const, + timestamp: Date.now(), + toolCallId: request.toolCallId, + toolName: request.toolName, + }; + + if (type === 'approval_requested') { + globalTraceStore.append({ + ...base, + id: nextApprovalEventId('approval-requested'), + type: 'approval_requested', + args: request.args, + }); + return; + } + + globalTraceStore.append({ + ...base, + id: nextApprovalEventId('approval-resolved'), + type: 'approval_resolved', + outcome: extra?.outcome ?? 'denied', + persistedGrantId: extra?.persistedGrantId, + }); +} + +function isGrantedByRules(request: ApprovalRequest): boolean { + refreshPermissionGrantsFromStorage(); + const capabilityId = request.capabilityId ?? resolveCapabilityId(request.toolName); + return matchPermissionGrant(getActivePermissionGrants(), { + capabilityId, + chatSessionId: request.chatSessionId, + sessionId: typeof request.args.sessionId === 'string' ? request.args.sessionId : undefined, + args: request.args, + }) !== null; +} + /** * Called from a tool's `execute` function when it needs user approval. * Returns a Promise that resolves to `true` (approved) or `false` (denied). @@ -55,9 +165,22 @@ export function requestApproval( toolName: string, args: Record, chatSessionId?: string, - timeoutMs: number = APPROVAL_TIMEOUT_MS, + timeoutMs: number = CATTY_APPROVAL_TIMEOUT_MS, + capabilityId?: string, ): Promise { - const request: ApprovalRequest = { toolCallId, toolName, args, chatSessionId }; + const request: ApprovalRequest = { + toolCallId, + toolName, + args, + chatSessionId, + capabilityId: capabilityId ?? resolveCapabilityId(toolName), + }; + + if (isGrantedByRules(request)) { + return Promise.resolve(true); + } + + emitApprovalEvent('approval_requested', request); return new Promise((resolve) => { let timerId: ReturnType | null = null; @@ -74,6 +197,7 @@ export function requestApproval( if (pendingApprovals.has(toolCallId)) { pendingApprovals.delete(toolCallId); wrappedResolve(false); + emitApprovalEvent('approval_resolved', request, { outcome: 'timeout' }); // Notify UI to remove the stale card for (const cl of clearedListeners) { try { cl([toolCallId]); } catch { /* ignore */ } @@ -92,14 +216,38 @@ export function requestApproval( * Called from the UI when the user approves or rejects a tool execution. * Handles both SDK tool calls (local Promise) and MCP tool calls (IPC to main process). */ -export function resolveApproval(toolCallId: string, approved: boolean): void { +export function resolveApproval( + toolCallId: string, + decision: boolean | ResolveApprovalOptions, +): void { + const approved = typeof decision === 'boolean' ? decision : decision.approved; + const persistGrant = typeof decision === 'boolean' ? undefined : decision.persistGrant; + const persistGrants = typeof decision === 'boolean' + ? undefined + : (decision.persistGrants ?? (persistGrant ? [persistGrant] : undefined)); + const entry = pendingApprovals.get(toolCallId); + const request = entry?.request; + if (entry) { pendingApprovals.delete(toolCallId); - // SDK tool calls have a real resolve; MCP tool calls have a no-op resolve entry.resolve(approved); } + if (request) { + let persistedGrantId: string | undefined; + if (approved && persistGrants?.length) { + for (const grant of persistGrants) { + grantPersister?.(grant); + persistedGrantId = grant.id; + } + } + emitApprovalEvent('approval_resolved', request, { + outcome: approved ? 'approved' : 'denied', + persistedGrantId, + }); + } + // MCP tool call: also forward response to main process via IPC if (toolCallId.startsWith('mcp_approval_')) { const bridge = (window as unknown as { netcatty?: { respondMcpApproval?: (id: string, approved: boolean) => Promise } }).netcatty; @@ -218,6 +366,7 @@ export function setupMcpApprovalBridge(): () => void { toolName: payload.toolName, args: payload.args, chatSessionId: payload.chatSessionId, + capabilityId: resolveCapabilityId(payload.toolName), }; // Store in pendingApprovals so it survives unmount/remount diff --git a/infrastructure/ai/shared/bashArity.test.ts b/infrastructure/ai/shared/bashArity.test.ts new file mode 100644 index 000000000..2a85fb242 --- /dev/null +++ b/infrastructure/ai/shared/bashArity.test.ts @@ -0,0 +1,29 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { bashArityPrefix } from './bashArity'; + +describe('bashArity (OpenCode parity)', () => { + it('unknown commands default to first token', () => { + assert.deepEqual(bashArityPrefix(['unknown', 'command', 'subcommand']), ['unknown']); + assert.deepEqual(bashArityPrefix(['touch', 'foo.txt']), ['touch']); + }); + + it('two token commands', () => { + assert.deepEqual(bashArityPrefix(['git', 'checkout', 'main']), ['git', 'checkout']); + assert.deepEqual(bashArityPrefix(['docker', 'run', 'nginx']), ['docker', 'run']); + }); + + it('three token commands', () => { + assert.deepEqual(bashArityPrefix(['aws', 's3', 'ls', 'my-bucket']), ['aws', 's3', 'ls']); + assert.deepEqual(bashArityPrefix(['npm', 'run', 'dev', 'script']), ['npm', 'run', 'dev']); + }); + + it('longest match wins', () => { + assert.deepEqual(bashArityPrefix(['docker', 'compose', 'up', 'service']), ['docker', 'compose', 'up']); + assert.deepEqual(bashArityPrefix(['consul', 'kv', 'get', 'config']), ['consul', 'kv', 'get']); + }); + + it('lscpu uses first token only', () => { + assert.deepEqual(bashArityPrefix(['lscpu']), ['lscpu']); + }); +}); diff --git a/infrastructure/ai/shared/bashArity.ts b/infrastructure/ai/shared/bashArity.ts new file mode 100644 index 000000000..bb336c702 --- /dev/null +++ b/infrastructure/ai/shared/bashArity.ts @@ -0,0 +1,163 @@ +export function prefix(tokens: string[]) { + for (let len = tokens.length; len > 0; len--) { + const prefix = tokens.slice(0, len).join(" ") + const arity = ARITY[prefix] + if (arity !== undefined) return tokens.slice(0, arity) + } + if (tokens.length === 0) return [] + return tokens.slice(0, 1) +} + +/* Generated with following prompt: +You are generating a dictionary of command-prefix arities for bash-style commands. +This dictionary is used to identify the "human-understandable command" from an input shell command.### **RULES (follow strictly)**1. Each entry maps a **command prefix string → number**, representing how many **tokens** define the command. +2. **Flags NEVER count as tokens**. Only subcommands count. +3. **Longest matching prefix wins**. +4. **Only include a longer prefix if its arity is different from what the shorter prefix already implies**. * Example: If `git` is 2, then do **not** include `git checkout`, `git commit`, etc. unless they require *different* arity. +5. The output must be a **single JSON object**. Each entry should have a comment with an example real world matching command. DO NOT MAKE ANY OTHER COMMENTS. Should be alphabetical +6. Include the **most commonly used commands** across many stacks and languages. More is better.### **Semantics examples*** `touch foo.txt` → `touch` (arity 1, explicitly listed) +* `git checkout main` → `git checkout` (because `git` has arity 2) +* `npm install` → `npm install` (because `npm` has arity 2) +* `npm run dev` → `npm run dev` (because `npm run` has arity 3) +* `python script.py` → `python script.py` (default: whole input, not in dictionary)### **Now generate the dictionary.** +*/ +const ARITY: Record = { + cat: 1, // cat file.txt + cd: 1, // cd /path/to/dir + chmod: 1, // chmod 755 script.sh + chown: 1, // chown user:group file.txt + cp: 1, // cp source.txt dest.txt + echo: 1, // echo "hello world" + env: 1, // env + export: 1, // export PATH=/usr/bin + grep: 1, // grep pattern file.txt + kill: 1, // kill 1234 + killall: 1, // killall process + ln: 1, // ln -s source target + ls: 1, // ls -la + mkdir: 1, // mkdir new-dir + mv: 1, // mv old.txt new.txt + ps: 1, // ps aux + pwd: 1, // pwd + rm: 1, // rm file.txt + rmdir: 1, // rmdir empty-dir + sleep: 1, // sleep 5 + source: 1, // source ~/.bashrc + tail: 1, // tail -f log.txt + touch: 1, // touch file.txt + unset: 1, // unset VAR + which: 1, // which node + aws: 3, // aws s3 ls + az: 3, // az storage blob list + bazel: 2, // bazel build + brew: 2, // brew install node + bun: 2, // bun install + "bun run": 3, // bun run dev + "bun x": 3, // bun x vite + cargo: 2, // cargo build + "cargo add": 3, // cargo add tokio + "cargo run": 3, // cargo run main + cdk: 2, // cdk deploy + cf: 2, // cf push app + cmake: 2, // cmake build + composer: 2, // composer require laravel + consul: 2, // consul members + "consul kv": 3, // consul kv get config/app + crictl: 2, // crictl ps + deno: 2, // deno run server.ts + "deno task": 3, // deno task dev + doctl: 3, // doctl kubernetes cluster list + docker: 2, // docker run nginx + "docker builder": 3, // docker builder prune + "docker compose": 3, // docker compose up + "docker container": 3, // docker container ls + "docker image": 3, // docker image prune + "docker network": 3, // docker network inspect + "docker volume": 3, // docker volume ls + eksctl: 2, // eksctl get clusters + "eksctl create": 3, // eksctl create cluster + firebase: 2, // firebase deploy + flyctl: 2, // flyctl deploy + gcloud: 3, // gcloud compute instances list + gh: 3, // gh pr list + git: 2, // git checkout main + "git config": 3, // git config user.name + "git remote": 3, // git remote add origin + "git stash": 3, // git stash pop + go: 2, // go build + gradle: 2, // gradle build + helm: 2, // helm install mychart + heroku: 2, // heroku logs + hugo: 2, // hugo new site blog + ip: 2, // ip link show + "ip addr": 3, // ip addr show + "ip link": 3, // ip link set eth0 up + "ip netns": 3, // ip netns exec foo bash + "ip route": 3, // ip route add default via 1.1.1.1 + kind: 2, // kind delete cluster + "kind create": 3, // kind create cluster + kubectl: 2, // kubectl get pods + "kubectl kustomize": 3, // kubectl kustomize overlays/dev + "kubectl rollout": 3, // kubectl rollout restart deploy/api + kustomize: 2, // kustomize build . + make: 2, // make build + mc: 2, // mc ls myminio + "mc admin": 3, // mc admin info myminio + minikube: 2, // minikube start + mongosh: 2, // mongosh test + mysql: 2, // mysql -u root + mvn: 2, // mvn compile + ng: 2, // ng generate component home + npm: 2, // npm install + "npm exec": 3, // npm exec vite + "npm init": 3, // npm init vue + "npm run": 3, // npm run dev + "npm view": 3, // npm view react version + nvm: 2, // nvm use 18 + nx: 2, // nx build + openssl: 2, // openssl genrsa 2048 + "openssl req": 3, // openssl req -new -key key.pem + "openssl x509": 3, // openssl x509 -in cert.pem + pip: 2, // pip install numpy + pipenv: 2, // pipenv install flask + pnpm: 2, // pnpm install + "pnpm dlx": 3, // pnpm dlx create-next-app + "pnpm exec": 3, // pnpm exec vite + "pnpm run": 3, // pnpm run dev + poetry: 2, // poetry add requests + podman: 2, // podman run alpine + "podman container": 3, // podman container ls + "podman image": 3, // podman image prune + psql: 2, // psql -d mydb + pulumi: 2, // pulumi up + "pulumi stack": 3, // pulumi stack output + pyenv: 2, // pyenv install 3.11 + python: 2, // python -m venv env + rake: 2, // rake db:migrate + rbenv: 2, // rbenv install 3.2.0 + "redis-cli": 2, // redis-cli ping + rustup: 2, // rustup update + serverless: 2, // serverless invoke + sfdx: 3, // sfdx force:org:list + skaffold: 2, // skaffold dev + sls: 2, // sls deploy + sst: 2, // sst deploy + swift: 2, // swift build + systemctl: 2, // systemctl restart nginx + terraform: 2, // terraform apply + "terraform workspace": 3, // terraform workspace select prod + tmux: 2, // tmux new -s dev + turbo: 2, // turbo run build + ufw: 2, // ufw allow 22 + vault: 2, // vault login + "vault auth": 3, // vault auth list + "vault kv": 3, // vault kv get secret/api + vercel: 2, // vercel deploy + volta: 2, // volta install node + wp: 2, // wp plugin install + yarn: 2, // yarn add react + "yarn dlx": 3, // yarn dlx create-react-app + "yarn run": 3, // yarn run dev +} + +export function bashArityPrefix(tokens: string[]): string[] { return prefix(tokens); } diff --git a/infrastructure/ai/shared/pathLikeCommand.ts b/infrastructure/ai/shared/pathLikeCommand.ts new file mode 100644 index 000000000..48420d394 --- /dev/null +++ b/infrastructure/ai/shared/pathLikeCommand.ts @@ -0,0 +1,12 @@ +/** True when the configured agent command looks like an explicit filesystem path. */ +export function isPathLikeCommand(command: string | undefined): boolean { + const normalized = String(command || '').trim(); + return normalized.includes('/') || normalized.includes('\\') || /^[a-z]:/i.test(normalized); +} + +export function getCommandBasename(command: string | undefined): string { + const normalized = String(command || '').trim(); + if (!normalized) return ''; + const parts = normalized.split(/[\\/]/); + return (parts.pop() || '').toLowerCase(); +} diff --git a/infrastructure/ai/shared/shellCommandGrant.test.ts b/infrastructure/ai/shared/shellCommandGrant.test.ts new file mode 100644 index 000000000..dfe657064 --- /dev/null +++ b/infrastructure/ai/shared/shellCommandGrant.test.ts @@ -0,0 +1,23 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { buildAlwaysAllowCommandPatterns } from './shellCommandGrant'; + +describe('shellCommandGrant (OpenCode always patterns)', () => { + it('builds prefix wildcard for simple commands', () => { + assert.deepEqual(buildAlwaysAllowCommandPatterns('lscpu'), ['lscpu *']); + assert.deepEqual(buildAlwaysAllowCommandPatterns('touch foo.txt'), ['touch *']); + }); + + it('builds subcommand-aware prefixes', () => { + assert.deepEqual(buildAlwaysAllowCommandPatterns('git checkout main'), ['git checkout *']); + assert.deepEqual(buildAlwaysAllowCommandPatterns('systemctl status nginx'), ['systemctl status *']); + assert.deepEqual(buildAlwaysAllowCommandPatterns('npm run dev'), ['npm run dev *']); + }); + + it('skips cd segments in chains but keeps others', () => { + assert.deepEqual( + buildAlwaysAllowCommandPatterns('cd /tmp && ls -la'), + ['ls *'], + ); + }); +}); diff --git a/infrastructure/ai/shared/shellCommandGrant.ts b/infrastructure/ai/shared/shellCommandGrant.ts new file mode 100644 index 000000000..2f7ecc41c --- /dev/null +++ b/infrastructure/ai/shared/shellCommandGrant.ts @@ -0,0 +1,65 @@ +import { bashArityPrefix } from './bashArity'; + +/** Commands whose always-allow patterns are skipped (OpenCode shell.ts CWD set). */ +const CWD_COMMANDS = new Set([ + 'cd', + 'chdir', + 'popd', + 'pushd', + 'push-location', + 'set-location', +]); + +export function unquoteShellToken(token: string): string { + if (token.length >= 2) { + const first = token[0]; + const last = token[token.length - 1]; + if ((first === '"' || first === "'") && first === last) { + return token.slice(1, -1); + } + } + return token; +} + +export function tokenizeShellCommand(command: string): string[] { + const matches = command.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) ?? []; + return matches.map(unquoteShellToken); +} + +export function splitShellCommandSegments(command: string): string[] { + return command + .split(/\s*(?:&&|\|\||;)\s*/) + .map((segment) => segment.trim()) + .filter(Boolean); +} + +/** + * OpenCode always-allow patterns: BashArity.prefix(tokens) + " *" + * @see packages/opencode/src/tool/shell.ts collect() + */ +export function buildAlwaysAllowCommandPatterns(command: string): string[] { + const trimmed = command.trim(); + if (!trimmed) return []; + + const patterns = new Set(); + const segments = splitShellCommandSegments(trimmed); + + for (const segment of segments) { + const tokens = tokenizeShellCommand(segment); + if (tokens.length === 0) continue; + const cmd = tokens[0]?.toLowerCase(); + if (cmd && CWD_COMMANDS.has(cmd)) continue; + const prefix = bashArityPrefix(tokens); + if (prefix.length === 0) continue; + patterns.add(`${prefix.join(' ')} *`); + } + + if (patterns.size === 0) { + const tokens = tokenizeShellCommand(trimmed); + if (tokens.length > 0) { + patterns.add(`${bashArityPrefix(tokens).join(' ')} *`); + } + } + + return [...patterns]; +} diff --git a/infrastructure/ai/vaultAgentBridgeClient.test.ts b/infrastructure/ai/vaultAgentBridgeClient.test.ts new file mode 100644 index 000000000..3f975890d --- /dev/null +++ b/infrastructure/ai/vaultAgentBridgeClient.test.ts @@ -0,0 +1,310 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import type { Host, VaultNote } from '../../domain/models'; +import { handleVaultAgentOp, runSerializedVaultAgentRequest, type VaultAgentApiDeps } from './vaultAgentBridgeClient'; + +type DepsSeed = { + hosts?: Host[]; + notes?: VaultNote[]; + customGroups?: string[]; +}; + +function createDeps( + overrides: Partial & DepsSeed = {}, +): VaultAgentApiDeps { + let hosts = overrides.hosts ?? []; + let notes = overrides.notes ?? []; + let customGroups = overrides.customGroups ?? []; + + const base: VaultAgentApiDeps = { + getHosts: () => hosts, + getNotes: () => notes, + getCustomGroups: () => customGroups, + snippets: [], + portForwardingRules: [], + keys: [], + identities: [], + resolveEffectiveHost: (host) => host, + updateHostNotes: () => {}, + updateCustomGroups: (groups) => { + customGroups = groups; + }, + updateHosts: (nextHosts) => { + hosts = nextHosts; + }, + updateNotes: (nextNotes) => { + notes = nextNotes; + }, + startTunnel: async () => ({ success: true }), + stopTunnel: async () => ({ success: true }), + }; + + return { + ...base, + ...overrides, + getHosts: overrides.getHosts ?? base.getHosts, + getNotes: overrides.getNotes ?? base.getNotes, + getCustomGroups: overrides.getCustomGroups ?? base.getCustomGroups, + updateHosts: (nextHosts) => { + base.updateHosts(nextHosts); + overrides.updateHosts?.(nextHosts); + }, + updateNotes: (nextNotes) => { + base.updateNotes(nextNotes); + overrides.updateNotes?.(nextNotes); + }, + updateCustomGroups: (groups) => { + base.updateCustomGroups(groups); + overrides.updateCustomGroups?.(groups); + }, + }; +} + +describe('handleVaultAgentOp vault notes', () => { + it('note.create persists to updateNotes and returns the new note', async () => { + const updated: VaultNote[][] = []; + const deps = createDeps({ + notes: [], + updateNotes: (nextNotes) => { + updated.push(nextNotes); + }, + }); + + const result = await handleVaultAgentOp( + 'note.create', + { title: 'Deploy runbook', content: '# Steps\n1. Connect' }, + deps, + ); + + assert.equal(result.ok, true); + assert.equal(updated.length, 1); + assert.equal(updated[0]?.length, 1); + assert.equal(updated[0]?.[0]?.title, 'Deploy runbook'); + assert.equal(updated[0]?.[0]?.content, '# Steps\n1. Connect'); + assert.equal((result as { note?: VaultNote }).note?.id, updated[0]?.[0]?.id); + }); + + it('note.list returns summaries without full content', async () => { + const note: VaultNote = { + id: 'note-1', + title: 'Existing', + content: 'secret body', + createdAt: 1, + updatedAt: 2, + }; + const result = await handleVaultAgentOp('note.list', {}, createDeps({ notes: [note] })); + + assert.equal(result.ok, true); + const listed = (result as { notes?: Array<{ id: string; contentLength: number; title: string }> }).notes; + assert.equal(listed?.length, 1); + assert.equal(listed?.[0]?.title, 'Existing'); + assert.equal(listed?.[0]?.contentLength, 'secret body'.length); + assert.equal('content' in (listed?.[0] ?? {}), false); + }); + + it('note.update replaces content and bumps updatedAt', async () => { + const existing: VaultNote = { + id: 'note-1', + title: 'Old title', + content: 'old', + createdAt: 100, + updatedAt: 100, + }; + const updated: VaultNote[][] = []; + const deps = createDeps({ + notes: [existing], + updateNotes: (nextNotes) => { + updated.push(nextNotes); + }, + }); + + const result = await handleVaultAgentOp( + 'note.update', + { noteId: 'note-1', title: 'New title', content: 'new body' }, + deps, + ); + + assert.equal(result.ok, true); + assert.equal(updated[0]?.[0]?.title, 'New title'); + assert.equal(updated[0]?.[0]?.content, 'new body'); + assert.ok((updated[0]?.[0]?.updatedAt ?? 0) >= 100); + }); + + it('sequential note.create calls accumulate instead of overwriting prior notes', async () => { + const deps = createDeps({ notes: [] }); + + const first = await handleVaultAgentOp( + 'note.create', + { title: 'First', content: 'one' }, + deps, + ); + const second = await handleVaultAgentOp( + 'note.create', + { title: 'Second', content: 'two' }, + deps, + ); + + assert.equal(first.ok, true); + assert.equal(second.ok, true); + assert.equal(deps.getNotes().length, 2); + assert.deepEqual(deps.getNotes().map((note) => note.title), ['First', 'Second']); + }); + + it('host.notes.set still updates host metadata separately from vault notes', async () => { + const host: Host = { + id: 'host-1', + label: 'prod', + hostname: '10.0.0.1', + username: 'root', + notes: '', + }; + let hostNotes = ''; + const deps = createDeps({ + hosts: [host], + updateHostNotes: (hostId, notes) => { + assert.equal(hostId, 'host-1'); + hostNotes = notes; + }, + }); + + const result = await handleVaultAgentOp( + 'host.notes.set', + { hostId: 'host-1', notes: 'host detail memo' }, + deps, + ); + + assert.equal(result.ok, true); + assert.equal(hostNotes, 'host detail memo'); + }); +}); + +describe('handleVaultAgentOp vault hosts', () => { + const csvText = [ + 'Label,Hostname,Port,Username,Groups', + 'web-1,10.0.0.10,22,deploy,prod/web', + 'db-1,10.0.0.20,22,root,prod/db', + ].join('\n'); + + it('host.list returns metadata without passwords', async () => { + const host: Host = { + id: 'host-1', + label: 'prod', + hostname: '10.0.0.1', + username: 'root', + password: 'secret', + port: 22, + }; + const result = await handleVaultAgentOp('host.list', {}, createDeps({ hosts: [host] })); + assert.equal(result.ok, true); + const hosts = (result as { hosts?: Array> }).hosts; + assert.equal(hosts?.length, 1); + assert.equal(hosts?.[0]?.hostname, '10.0.0.1'); + assert.equal('password' in (hosts?.[0] ?? {}), false); + }); + + it('hosts.create maps structured JSON from arbitrary text into vault hosts', async () => { + const updatedHosts: Host[][] = []; + const unstructuredMapped = JSON.stringify([ + { label: 'Prod API', hostname: 'api.example.com', username: 'deploy', port: 22, group: 'prod/api' }, + { label: 'Staging DB', hostname: '10.20.0.5', username: 'postgres', tags: ['db', 'staging'] }, + ]); + + const result = await handleVaultAgentOp( + 'hosts.create', + { hosts: unstructuredMapped }, + createDeps({ + updateHosts: (hosts) => { + updatedHosts.push(hosts); + }, + updateCustomGroups: () => {}, + }), + ); + + assert.equal(result.ok, true); + assert.equal(updatedHosts[0]?.length, 2); + assert.equal((result as { addedCount?: number }).addedCount, 2); + }); + + it('sequential hosts.create calls accumulate instead of dropping prior hosts', async () => { + const deps = createDeps({ hosts: [], customGroups: [] }); + + const first = await handleVaultAgentOp( + 'hosts.create', + { hosts: JSON.stringify([{ hostname: '10.0.0.1', username: 'root', label: 'first' }]) }, + deps, + ); + const second = await handleVaultAgentOp( + 'hosts.create', + { hosts: JSON.stringify([{ hostname: '10.0.0.2', username: 'root', label: 'second' }]) }, + deps, + ); + + assert.equal(first.ok, true); + assert.equal(second.ok, true); + assert.equal(deps.getHosts().length, 2); + assert.deepEqual(deps.getHosts().map((host) => host.hostname), ['10.0.0.1', '10.0.0.2']); + }); + + it('host.import dryRun previews parsed hosts without writing', async () => { + const updatedHosts: Host[][] = []; + const result = await handleVaultAgentOp( + 'host.import', + { format: 'csv', text: csvText, dryRun: 'true' }, + createDeps({ + updateHosts: (hosts) => { + updatedHosts.push(hosts); + }, + }), + ); + + assert.equal(result.ok, true); + assert.equal((result as { dryRun?: boolean }).dryRun, true); + assert.equal(updatedHosts.length, 0); + assert.equal((result as { previewHosts?: unknown[] }).previewHosts?.length, 2); + }); + + it('host.import applies hosts to the vault', async () => { + const updatedHosts: Host[][] = []; + const updatedGroups: string[][] = []; + const result = await handleVaultAgentOp( + 'host.import', + { format: 'auto', text: csvText }, + createDeps({ + updateHosts: (hosts) => { + updatedHosts.push(hosts); + }, + updateCustomGroups: (groups) => { + updatedGroups.push(groups); + }, + }), + ); + + assert.equal(result.ok, true); + assert.equal(updatedHosts[0]?.length, 2); + assert.ok(updatedGroups[0]?.includes('prod/web')); + assert.equal((result as { addedCount?: number }).addedCount, 2); + }); +}); + +describe('runSerializedVaultAgentRequest', () => { + it('runs concurrent tasks sequentially without losing updates', async () => { + let value = 0; + const results = await Promise.all([ + runSerializedVaultAgentRequest(async () => { + const snapshot = value; + await new Promise((resolve) => setTimeout(resolve, 5)); + value = snapshot + 1; + return value; + }), + runSerializedVaultAgentRequest(async () => { + const snapshot = value; + await new Promise((resolve) => setTimeout(resolve, 1)); + value = snapshot + 1; + return value; + }), + ]); + + assert.deepEqual(results, [1, 2]); + assert.equal(value, 2); + }); +}); diff --git a/infrastructure/ai/vaultAgentBridgeClient.ts b/infrastructure/ai/vaultAgentBridgeClient.ts new file mode 100644 index 000000000..8eb34e177 --- /dev/null +++ b/infrastructure/ai/vaultAgentBridgeClient.ts @@ -0,0 +1,582 @@ +import type { Host, Identity, PortForwardingRule, Snippet, SSHKey, TerminalSettings, VaultNote } from '../../domain/models'; +import { + normalizeVaultNotes, + sanitizeNoteTitle, + sanitizeVaultNote, +} from '../../domain/notes'; +import { getNextVaultOrder } from '../../domain/vaultOrder'; +import { + applyVaultHostCreates, + buildVaultHostsFromDrafts, + parseVaultHostDraftsInput, +} from '../../domain/vaultHostCreate'; +import { + applyVaultHostImport, + detectVaultImportFormat, + importVaultHostsFromText, + VAULT_IMPORT_FORMATS, + type VaultImportFormat, +} from '../../domain/vaultImport'; +import { resolveHostAuth } from '../../domain/sshAuth'; +import { netcattyBridge } from '../services/netcattyBridge'; + +const SENSITIVE_HOST_KEYS = new Set([ + 'password', + 'telnetPassword', + 'privateKey', + 'passphrase', +]); + +export function sanitizeHostForAgent(host: Host): Record { + const sanitized: Record = {}; + for (const [key, value] of Object.entries(host)) { + if (SENSITIVE_HOST_KEYS.has(key)) continue; + sanitized[key] = value; + } + return sanitized; +} + +function summarizeHostForList(host: Host) { + return { + id: host.id, + label: host.label, + hostname: host.hostname, + port: host.port, + username: host.username, + protocol: host.protocol, + group: host.group, + tags: host.tags, + os: host.os, + createdAt: host.createdAt, + }; +} + +function parseOptionalBoolean(value: unknown): boolean | undefined { + if (value === undefined || value === null) return undefined; + if (typeof value === 'boolean') return value; + if (typeof value !== 'string') return undefined; + const normalized = value.trim().toLowerCase(); + if (normalized === 'true' || normalized === '1' || normalized === 'yes') return true; + if (normalized === 'false' || normalized === '0' || normalized === 'no') return false; + return undefined; +} + +function resolveVaultImportFormat(raw: unknown): VaultImportFormat | 'auto' | { error: string } { + const format = typeof raw === 'string' ? raw.trim().toLowerCase() : ''; + if (!format) return { error: 'format is required.' }; + if (format === 'auto') return 'auto'; + if ((VAULT_IMPORT_FORMATS as readonly string[]).includes(format)) { + return format as VaultImportFormat; + } + return { + error: `Unsupported format "${format}". Use csv, putty, mobaxterm, securecrt, ssh_config, or auto.`, + }; +} + +export function sanitizePortForwardRuleForAgent(rule: PortForwardingRule): Record { + return { + id: rule.id, + label: rule.label, + type: rule.type, + localPort: rule.localPort, + bindAddress: rule.bindAddress, + remoteHost: rule.remoteHost, + remotePort: rule.remotePort, + hostId: rule.hostId, + autoStart: rule.autoStart, + status: rule.status, + error: rule.error, + createdAt: rule.createdAt, + lastUsedAt: rule.lastUsedAt, + }; +} + +function summarizeVaultNoteForList(note: VaultNote) { + return { + id: note.id, + title: note.title, + group: note.group, + tags: note.tags, + linkedHostIds: note.linkedHostIds, + updatedAt: note.updatedAt, + contentLength: note.content.length, + }; +} + +function serializeVaultNoteForAgent(note: VaultNote) { + return { + id: note.id, + title: note.title, + content: note.content, + group: note.group, + tags: note.tags, + linkedHostIds: note.linkedHostIds, + createdAt: note.createdAt, + updatedAt: note.updatedAt, + }; +} + +function parseSnippetVariableValues( + raw: unknown, +): Record | { error: string } { + if (raw === undefined || raw === null) return {}; + if (typeof raw === 'object' && !Array.isArray(raw)) { + return Object.fromEntries( + Object.entries(raw as Record).map(([key, value]) => [key, String(value ?? '')]), + ); + } + if (typeof raw !== 'string' || !raw.trim()) return {}; + try { + const parsed = JSON.parse(raw) as unknown; + if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { + return Object.fromEntries( + Object.entries(parsed as Record).map(([key, value]) => [key, String(value ?? '')]), + ); + } + return { error: 'variables must be a JSON object string.' }; + } catch { + return { error: 'variables must be a JSON object string.' }; + } +} + +function parseOptionalStringArray( + value: unknown, + fieldName: string, +): string[] | undefined | { error: string } { + if (value === undefined || value === null) return undefined; + if (Array.isArray(value)) { + return value.map((entry) => String(entry).trim()).filter(Boolean); + } + if (typeof value !== 'string') { + return { error: `${fieldName} must be a string or array.` }; + } + const trimmed = value.trim(); + if (!trimmed) return undefined; + if (trimmed.startsWith('[')) { + try { + const parsed = JSON.parse(trimmed) as unknown; + if (!Array.isArray(parsed)) { + return { error: `${fieldName} must be a JSON array.` }; + } + return parsed.map((entry) => String(entry).trim()).filter(Boolean); + } catch { + return { error: `${fieldName} must be valid JSON array.` }; + } + } + return trimmed.split(',').map((entry) => entry.trim()).filter(Boolean); +} + +export interface VaultAgentApiDeps { + getHosts: () => Host[]; + getNotes: () => VaultNote[]; + getCustomGroups: () => string[]; + snippets: Snippet[]; + portForwardingRules: PortForwardingRule[]; + keys: SSHKey[]; + identities: Identity[]; + terminalSettings?: Pick; + resolveEffectiveHost: (host: Host) => Host; + updateHostNotes: (hostId: string, notes: string) => void; + updateCustomGroups: (groups: string[]) => void; + updateHosts: (hosts: Host[]) => void; + updateNotes: (notes: VaultNote[]) => void; + startTunnel: ( + rule: PortForwardingRule, + host: Host, + hosts: Host[], + keys: SSHKey[], + identities: Identity[], + onStatusChange?: (status: PortForwardingRule['status'], error?: string) => void, + enableReconnect?: boolean, + terminalSettings?: Pick, + ) => Promise<{ success: boolean; error?: string }>; + stopTunnel: ( + ruleId: string, + onStatusChange?: (status: PortForwardingRule['status']) => void, + ) => Promise<{ success: boolean; error?: string }>; +} + +export async function handleVaultAgentOp( + op: string, + params: Record, + deps: VaultAgentApiDeps, +): Promise> { + switch (op) { + case 'host.get': { + const hostId = String(params.hostId || ''); + const host = deps.getHosts().find((entry) => entry.id === hostId); + if (!host) return { ok: false, error: `Host "${hostId}" was not found.` }; + return { ok: true, host: sanitizeHostForAgent(deps.resolveEffectiveHost(host)) }; + } + case 'host.list': { + return { + ok: true, + hosts: deps.getHosts().map((host) => summarizeHostForList(deps.resolveEffectiveHost(host))), + }; + } + case 'hosts.create': { + const parsedDrafts = parseVaultHostDraftsInput(params.hosts); + if (!parsedDrafts.ok) return { ok: false, error: parsedDrafts.error }; + + const dryRun = parseOptionalBoolean(params.dryRun) ?? false; + const skipDuplicates = parseOptionalBoolean(params.skipDuplicates) ?? true; + const { hosts: builtHosts, issues: buildIssues } = buildVaultHostsFromDrafts(parsedDrafts.drafts); + + if (builtHosts.length === 0) { + return { + ok: false, + error: buildIssues[0]?.error || 'No valid hosts to create.', + issues: buildIssues, + }; + } + + const previewHosts = builtHosts.map((host) => sanitizeHostForAgent(host)); + + if (dryRun) { + return { + ok: true, + dryRun: true, + parsedCount: parsedDrafts.drafts.length, + validCount: builtHosts.length, + issues: buildIssues, + previewHosts, + }; + } + + const merged = applyVaultHostCreates( + deps.getHosts(), + deps.getCustomGroups(), + builtHosts, + { skipDuplicates }, + ); + + if (merged.addedCount === 0) { + return { + ok: false, + error: 'No new hosts were added (all duplicates or invalid).', + issues: buildIssues, + skippedExistingCount: merged.skippedExistingCount, + previewHosts, + }; + } + + deps.updateHosts(merged.hosts); + deps.updateCustomGroups(merged.customGroups); + + return { + ok: true, + dryRun: false, + parsedCount: parsedDrafts.drafts.length, + validCount: builtHosts.length, + addedCount: merged.addedCount, + skippedExistingCount: merged.skippedExistingCount, + issues: buildIssues, + previewHosts: merged.addedHosts.map((host) => sanitizeHostForAgent(host)), + }; + } + case 'host.import': { + const text = typeof params.text === 'string' ? params.text : ''; + if (!text.trim()) return { ok: false, error: 'text is required.' }; + + const formatParam = resolveVaultImportFormat(params.format); + if (typeof formatParam === 'object' && 'error' in formatParam) { + return { ok: false, error: formatParam.error }; + } + + let resolvedFormat: VaultImportFormat; + if (formatParam === 'auto') { + const detected = detectVaultImportFormat(text); + if (!detected) { + return { + ok: false, + error: 'Could not detect import format. Specify csv, putty, mobaxterm, securecrt, or ssh_config.', + }; + } + resolvedFormat = detected; + } else { + resolvedFormat = formatParam; + } + + const dryRun = parseOptionalBoolean(params.dryRun) ?? false; + const skipDuplicates = parseOptionalBoolean(params.skipDuplicates) ?? true; + const fileName = typeof params.fileName === 'string' && params.fileName.trim() + ? params.fileName.trim() + : undefined; + + const importResult = importVaultHostsFromText(resolvedFormat, text, { fileName }); + const previewHosts = importResult.hosts.map((host) => sanitizeHostForAgent(host)); + + if (dryRun) { + return { + ok: true, + dryRun: true, + format: resolvedFormat, + stats: importResult.stats, + issues: importResult.issues, + groups: importResult.groups, + previewHosts, + }; + } + + const merged = applyVaultHostImport( + deps.getHosts(), + deps.getCustomGroups(), + importResult, + { skipDuplicates }, + ); + + if (merged.addedCount === 0 && importResult.stats.parsed === 0) { + return { + ok: false, + error: importResult.issues[0]?.message || 'No hosts were imported.', + format: resolvedFormat, + stats: importResult.stats, + issues: importResult.issues, + }; + } + + deps.updateHosts(merged.hosts); + deps.updateCustomGroups(merged.customGroups); + + return { + ok: true, + dryRun: false, + format: resolvedFormat, + stats: importResult.stats, + issues: importResult.issues, + addedCount: merged.addedCount, + skippedExistingCount: merged.skippedExistingCount, + previewHosts: previewHosts.slice(0, 20), + }; + } + case 'host.notes.get': { + const hostId = String(params.hostId || ''); + const host = deps.getHosts().find((entry) => entry.id === hostId); + if (!host) return { ok: false, error: `Host "${hostId}" was not found.` }; + return { ok: true, hostId, notes: host.notes || '' }; + } + case 'host.notes.set': { + const hostId = String(params.hostId || ''); + const notes = typeof params.notes === 'string' ? params.notes : ''; + const host = deps.getHosts().find((entry) => entry.id === hostId); + if (!host) return { ok: false, error: `Host "${hostId}" was not found.` }; + deps.updateHostNotes(hostId, notes); + return { ok: true, hostId }; + } + case 'note.list': { + return { + ok: true, + notes: deps.getNotes().map(summarizeVaultNoteForList), + }; + } + case 'note.get': { + const noteId = String(params.noteId || ''); + const note = deps.getNotes().find((entry) => entry.id === noteId); + if (!note) return { ok: false, error: `Vault note "${noteId}" was not found.` }; + return { ok: true, note: serializeVaultNoteForAgent(note) }; + } + case 'note.create': { + const title = sanitizeNoteTitle(params.title); + if (!title) return { ok: false, error: 'title is required.' }; + const content = typeof params.content === 'string' ? params.content : ''; + const linkedHostIds = parseOptionalStringArray(params.linkedHostIds, 'linkedHostIds'); + if (linkedHostIds && 'error' in linkedHostIds) return { ok: false, error: linkedHostIds.error }; + const tags = parseOptionalStringArray(params.tags, 'tags'); + if (tags && 'error' in tags) return { ok: false, error: tags.error }; + const note = sanitizeVaultNote({ + title, + content, + group: typeof params.group === 'string' && params.group.trim() ? params.group.trim() : undefined, + linkedHostIds, + tags, + order: getNextVaultOrder(deps.getNotes()), + }); + const nextNotes = normalizeVaultNotes([...deps.getNotes(), note]); + deps.updateNotes(nextNotes); + return { ok: true, note: serializeVaultNoteForAgent(note) }; + } + case 'note.update': { + const noteId = String(params.noteId || ''); + const existing = deps.getNotes().find((entry) => entry.id === noteId); + if (!existing) return { ok: false, error: `Vault note "${noteId}" was not found.` }; + const linkedHostIds = parseOptionalStringArray(params.linkedHostIds, 'linkedHostIds'); + if (linkedHostIds && 'error' in linkedHostIds) return { ok: false, error: linkedHostIds.error }; + const tags = parseOptionalStringArray(params.tags, 'tags'); + if (tags && 'error' in tags) return { ok: false, error: tags.error }; + const note = sanitizeVaultNote({ + ...existing, + title: params.title !== undefined ? sanitizeNoteTitle(params.title) : existing.title, + content: typeof params.content === 'string' ? params.content : existing.content, + group: params.group !== undefined + ? (typeof params.group === 'string' && params.group.trim() ? params.group.trim() : undefined) + : existing.group, + linkedHostIds: linkedHostIds ?? existing.linkedHostIds, + tags: tags ?? existing.tags, + updatedAt: Date.now(), + }); + if (!note.title) return { ok: false, error: 'title cannot be empty.' }; + const nextNotes = normalizeVaultNotes( + deps.getNotes().map((entry) => (entry.id === noteId ? note : entry)), + ); + deps.updateNotes(nextNotes); + return { ok: true, note: serializeVaultNoteForAgent(note) }; + } + case 'snippets.list': { + return { + ok: true, + snippets: deps.snippets.map((snippet) => ({ + id: snippet.id, + label: snippet.label, + tags: snippet.tags || [], + targets: snippet.targets || [], + package: snippet.package, + })), + }; + } + case 'snippets.get': { + const snippetId = String(params.snippetId || ''); + const snippet = deps.snippets.find((entry) => entry.id === snippetId); + if (!snippet) return { ok: false, error: `Snippet "${snippetId}" was not found.` }; + return { + ok: true, + snippet: { + id: snippet.id, + label: snippet.label, + command: snippet.command, + tags: snippet.tags || [], + targets: snippet.targets || [], + noAutoRun: snippet.noAutoRun, + }, + }; + } + case 'snippets.run': { + const snippetId = String(params.snippetId || ''); + const sessionId = String(params.sessionId || ''); + const snippet = deps.snippets.find((entry) => entry.id === snippetId); + if (!snippet) return { ok: false, error: `Snippet "${snippetId}" was not found.` }; + if (!sessionId) return { ok: false, error: 'sessionId is required.' }; + + let variableValues: Record = {}; + const parsedVariables = parseSnippetVariableValues(params.variables); + if ('error' in parsedVariables) { + return { ok: false, error: parsedVariables.error }; + } + variableValues = parsedVariables; + + const defs = parseSnippetVariables(snippet.command); + for (const def of defs) { + if (variableValues[def.name] === undefined && def.defaultValue !== undefined) { + variableValues[def.name] = def.defaultValue; + } + } + for (const def of defs) { + if ((variableValues[def.name] ?? '').trim() === '' && def.defaultValue === undefined) { + return { ok: false, error: `Missing snippet variable "${def.name}".` }; + } + } + + const command = applySnippetVariables(snippet.command, variableValues); + const bridge = netcattyBridge.get(); + if (!bridge?.aiExec) { + return { ok: false, error: 'Terminal execution bridge is unavailable.' }; + } + const chatSessionId = typeof params.chatSessionId === 'string' ? params.chatSessionId : undefined; + const result = await bridge.aiExec(sessionId, command, chatSessionId); + if (result && typeof result === 'object' && 'ok' in result && result.ok === false) { + return { ok: false, error: (result as { error?: string }).error || 'Snippet execution failed.' }; + } + return { ok: true, sessionId, snippetId, command, result }; + } + case 'portforward.rules.list': { + return { + ok: true, + rules: deps.portForwardingRules.map(sanitizePortForwardRuleForAgent), + }; + } + case 'portforward.start': { + const ruleId = String(params.ruleId || ''); + const rule = deps.portForwardingRules.find((entry) => entry.id === ruleId); + if (!rule) return { ok: false, error: `Port forwarding rule "${ruleId}" was not found.` }; + if (!rule.hostId) return { ok: false, error: 'Rule has no associated host.' }; + const rawHost = deps.getHosts().find((entry) => entry.id === rule.hostId); + if (!rawHost) return { ok: false, error: `Host "${rule.hostId}" was not found.` }; + const host = deps.resolveEffectiveHost(rawHost); + try { + resolveHostAuth({ host, keys: deps.keys, identities: deps.identities }); + } catch (err) { + return { ok: false, error: err instanceof Error ? err.message : String(err) }; + } + const effectiveHosts = deps.getHosts().map((entry) => deps.resolveEffectiveHost(entry)); + const result = await deps.startTunnel( + rule, + host, + effectiveHosts, + deps.keys, + deps.identities, + undefined, + false, + deps.terminalSettings, + ); + if (!result.success) { + return { ok: false, error: result.error || 'Failed to start port forwarding tunnel.' }; + } + return { ok: true, ruleId }; + } + case 'portforward.stop': { + const ruleId = String(params.ruleId || ''); + const result = await deps.stopTunnel(ruleId); + if (!result.success) { + return { ok: false, error: result.error || 'Failed to stop port forwarding tunnel.' }; + } + return { ok: true, ruleId }; + } + default: + return { ok: false, error: `Unknown vault agent operation "${op}".` }; + } +} + +export type VaultAgentHandler = (op: string, params: Record) => Promise>; + +let activeHandler: VaultAgentHandler | null = null; +let vaultAgentMutationChain: Promise = Promise.resolve(); + +/** Serialize vault agent IPC handlers so read-modify-write mutations cannot clobber each other. */ +export async function runSerializedVaultAgentRequest(task: () => Promise): Promise { + const run = () => task(); + const next = vaultAgentMutationChain.then(run, run); + vaultAgentMutationChain = next.then( + () => undefined, + () => undefined, + ); + return next; +} + +export function registerVaultAgentHandler(handler: VaultAgentHandler | null): void { + activeHandler = handler; +} + +export function setupVaultAgentBridge(): () => void { + const bridge = netcattyBridge.get(); + if (!bridge?.onVaultAgentRequest || !bridge.respondVaultAgent) { + return () => {}; + } + + const unsubscribe = bridge.onVaultAgentRequest(async (payload) => { + const { requestId, op, params } = payload; + await runSerializedVaultAgentRequest(async () => { + try { + const result = activeHandler + ? await activeHandler(op, params || {}) + : { ok: false, error: 'Vault agent bridge is not ready.' }; + await bridge.respondVaultAgent?.(requestId, result); + } catch (err) { + await bridge.respondVaultAgent?.(requestId, { + ok: false, + error: err instanceof Error ? err.message : String(err), + }); + } + }); + }); + + return unsubscribe; +} diff --git a/infrastructure/config/storageKeys.ts b/infrastructure/config/storageKeys.ts index 3b6a474e0..18097c490 100644 --- a/infrastructure/config/storageKeys.ts +++ b/infrastructure/config/storageKeys.ts @@ -156,6 +156,8 @@ export const STORAGE_KEY_AI_AGENT_MODEL_MAP = 'netcatty_ai_agent_model_map_v1'; export const STORAGE_KEY_AI_AGENT_PROVIDER_MAP = 'netcatty_ai_agent_provider_map_v1'; export const STORAGE_KEY_AI_WEB_SEARCH = 'netcatty_ai_web_search_v1'; export const STORAGE_KEY_AI_QUICK_MESSAGES = 'netcatty_ai_quick_messages_v1'; +/** Confirm-mode permission grant memory (capability + session/command patterns). */ +export const STORAGE_KEY_AI_PERMISSION_GRANTS = 'netcatty_ai_permission_grants_v1'; export const STORAGE_KEY_AI_SHOW_TERMINAL_SELECTION_ACTION = 'netcatty_ai_show_terminal_selection_action_v1'; // SFTP Transfer Concurrency diff --git a/package-lock.json b/package-lock.json index 378bdd3f3..a7f777ccb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -326,29 +326,6 @@ "win32" ] }, - "node_modules/@anthropic-ai/sdk": { - "version": "0.105.0", - "resolved": "https://registry.npmjs.org/@anthropic-ai/sdk/-/sdk-0.105.0.tgz", - "integrity": "sha512-sDyu+aM9cE6uZE+HgRjjHRb+qqb87GHZOx+8bE0YlWetdL1YcVLxn8h9ltxGOflyChTe6PMEo50kMQV4cw0hfg==", - "license": "MIT", - "optional": true, - "peer": true, - "dependencies": { - "json-schema-to-ts": "^3.1.1", - "standardwebhooks": "^1.0.0" - }, - "bin": { - "anthropic-ai-sdk": "bin/cli" - }, - "peerDependencies": { - "zod": "^3.25.0 || ^4.0.0" - }, - "peerDependenciesMeta": { - "zod": { - "optional": true - } - } - }, "node_modules/@aws-crypto/crc32": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/@aws-crypto/crc32/-/crc32-5.2.0.tgz", @@ -1278,6 +1255,7 @@ "integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.0", "@babel/generator": "^7.29.0", @@ -1558,14 +1536,6 @@ "node": ">=6.9.0" } }, - "node_modules/@bufbuild/protobuf": { - "version": "1.10.1", - "resolved": "https://registry.npmjs.org/@bufbuild/protobuf/-/protobuf-1.10.1.tgz", - "integrity": "sha512-wJ8ReQbHxsAfXhrf9ixl0aYbZorRuOWpBNzm8pL8ftmSxQx/wnJD5Eg861NwJU/czy2VXFIebCeZnZrI9rktIQ==", - "license": "(Apache-2.0 AND BSD-3-Clause)", - "optional": true, - "peer": true - }, "node_modules/@buttercup/fetch": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/@buttercup/fetch/-/fetch-0.2.1.tgz", @@ -1884,6 +1854,7 @@ "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.12.3.tgz", "integrity": "sha512-QwCZW6Tt1siP37Jet9Tb02Zs81TQt6qQrZR2H+eGMcFsL1zMrk2/b9CLC7/9ieP1fjIUMgviLWMmgiHoJrj+ZA==", "license": "MIT", + "peer": true, "dependencies": { "@codemirror/state": "^6.0.0", "@codemirror/view": "^6.23.0", @@ -1973,6 +1944,7 @@ "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.6.0.tgz", "integrity": "sha512-4nbvra5R5EtiCzr9BTHiTLc+MLXK2QGiAVYMyi8PkQd3SR+6ixar/Q/01Fa21TBIDOZXgeWV4WppsQolSreAPQ==", "license": "MIT", + "peer": true, "dependencies": { "@marijn/find-cluster-break": "^1.0.0" } @@ -1982,6 +1954,7 @@ "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.43.1.tgz", "integrity": "sha512-+BIjw/AG3tDQ4pJgTLPYdAW25eDE66YsvM4LKyVPgGzVgZ4a9Wj1SRX8kPVKgBDdPt8oHtZ15F0qx7p0oOHdHw==", "license": "MIT", + "peer": true, "dependencies": { "@codemirror/state": "^6.6.0", "crelt": "^1.0.6", @@ -1989,16 +1962,6 @@ "w3c-keyname": "^2.2.4" } }, - "node_modules/@connectrpc/connect": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/@connectrpc/connect/-/connect-1.7.0.tgz", - "integrity": "sha512-iNKdJRi69YP3mq6AePRT8F/HrxWCewrhxnLMNm0vpqXAR8biwzRtO6Hjx80C6UvtKJ5sFmffQT7I4Baecz389w==", - "license": "Apache-2.0", - "optional": true, - "peerDependencies": { - "@bufbuild/protobuf": "^1.10.0" - } - }, "node_modules/@connectrpc/connect-node": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/@connectrpc/connect-node/-/connect-node-1.7.0.tgz", @@ -2446,7 +2409,6 @@ "dev": true, "license": "BSD-2-Clause", "optional": true, - "peer": true, "dependencies": { "cross-dirname": "^0.1.0", "debug": "^4.3.4", @@ -2468,7 +2430,6 @@ "dev": true, "license": "MIT", "optional": true, - "peer": true, "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", @@ -3878,6 +3839,7 @@ "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.3.tgz", "integrity": "sha512-qXdH7UqTvGfdVBINrgKhDsVTJTxactNNxLk7+UMwZhU13lMHaOBlJe9Vqp907ya56Y3+ed2tlqzys7jDkTmW0g==", "license": "MIT", + "peer": true, "dependencies": { "@lezer/common": "^1.3.0" } @@ -4160,6 +4122,7 @@ "resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.29.0.tgz", "integrity": "sha512-zo37mZA9hJWpULgkRpowewez1y6ML5GsXJPY8FI0tBBCd77HEvza4jDqRKOXgHNn867PVGCyTdzqpz0izu5ZjQ==", "license": "MIT", + "peer": true, "dependencies": { "@hono/node-server": "^1.19.9", "ajv": "^8.17.1", @@ -7582,8 +7545,7 @@ "resolved": "https://registry.npmjs.org/@stablelib/base64/-/base64-1.0.1.tgz", "integrity": "sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ==", "license": "MIT", - "optional": true, - "peer": true + "optional": true }, "node_modules/@standard-schema/spec": { "version": "1.1.0", @@ -8193,6 +8155,7 @@ "integrity": "sha512-hAAP5io/7csFStuOmR782YmTthKBJ9ND3WVL60hcOjvtGFb+HJxH4O5huAcmcZ9v9G8P+JETiZ/G1B8MALnWZQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/regexpp": "^4.12.2", "@typescript-eslint/scope-manager": "8.54.0", @@ -8222,6 +8185,7 @@ "integrity": "sha512-BtE0k6cjwjLZoZixN0t5AKP0kSzlGu7FctRXYuPAm//aaiZhmfq1JwdYpYr1brzEspYyFeF+8XF5j2VK6oalrA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@typescript-eslint/scope-manager": "8.54.0", "@typescript-eslint/types": "8.54.0", @@ -8575,6 +8539,7 @@ "resolved": "https://registry.npmjs.org/@xterm/xterm/-/xterm-6.1.0-beta.220.tgz", "integrity": "sha512-rwEKE75wfwfNWeGnjl0iQRA1W50HigKoGcGMeF3o5CqDSdI9IjmwzpV9xHXqLH6CYj6s9N1g6bbKFVDou9dJ/A==", "license": "MIT", + "peer": true, "workspaces": [ "addons/*" ] @@ -8639,6 +8604,7 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", "integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==", "license": "MIT", + "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -9340,6 +9306,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -10165,8 +10132,7 @@ "integrity": "sha512-+R08/oI0nl3vfPcqftZRpytksBXDzOUveBq/NBVx0sUp1axwzPQrKinNx5yd5sxPu8j1wIy8AfnVQ+5eFdha6Q==", "dev": true, "license": "MIT", - "optional": true, - "peer": true + "optional": true }, "node_modules/cross-env": { "version": "10.1.0", @@ -10463,6 +10429,7 @@ "integrity": "sha512-fMkjRqKyPtsz4Kzu/qGP0BGjqzMCIgp+/7kw/u6YH6lvn/8hvL3c0TXhoFayBoYdpPCnEinnCHztd4bW7/jetA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "app-builder-lib": "26.15.2", "builder-util": "26.15.0", @@ -10744,7 +10711,6 @@ "dev": true, "hasInstallScript": true, "license": "MIT", - "peer": true, "dependencies": { "@electron/asar": "^3.2.1", "debug": "^4.1.1", @@ -10765,7 +10731,6 @@ "integrity": "sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "graceful-fs": "^4.1.2", "jsonfile": "^4.0.0", @@ -10781,7 +10746,6 @@ "integrity": "sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==", "dev": true, "license": "MIT", - "peer": true, "optionalDependencies": { "graceful-fs": "^4.1.6" } @@ -10792,7 +10756,6 @@ "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", "dev": true, "license": "MIT", - "peer": true, "engines": { "node": ">= 4.0.0" } @@ -11017,6 +10980,7 @@ "integrity": "sha512-LEyamqS7W5HB3ujJyvi0HQK/dtVINZvd5mAAp9eT5S/ujByGjiZLCzPcHVzuXbpJDJF/cxwHlfceVUDZ2lnSTw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.8.0", "@eslint-community/regexpp": "^4.12.1", @@ -11470,8 +11434,7 @@ "resolved": "https://registry.npmjs.org/fast-sha256/-/fast-sha256-1.3.0.tgz", "integrity": "sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ==", "license": "Unlicense", - "optional": true, - "peer": true + "optional": true }, "node_modules/fast-uri": { "version": "3.1.0", @@ -12408,6 +12371,7 @@ "resolved": "https://registry.npmjs.org/hono/-/hono-4.12.14.tgz", "integrity": "sha512-am5zfg3yu6sqn5yjKBNqhnTX7Cv+m00ox+7jbaKkrLMRJ4rAdldd1xPd/JzbBWspqaQv6RSTrgFN95EsfhC+7w==", "license": "MIT", + "peer": true, "engines": { "node": ">=16.9.0" } @@ -12864,7 +12828,6 @@ "resolved": "https://registry.npmjs.org/isomorphic.js/-/isomorphic.js-0.2.5.tgz", "integrity": "sha512-PIeMbHqMt4DnUP3MA/Flc0HElYjMXArsw1qwJZcm9sqR8mq3l8NYizFMty0pWwE/tzIGH3EKK5+jes5mAr85yw==", "license": "MIT", - "peer": true, "funding": { "type": "GitHub Sponsors ❤", "url": "https://github.com/sponsors/dmonad" @@ -12986,7 +12949,6 @@ "integrity": "sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g==", "license": "MIT", "optional": true, - "peer": true, "dependencies": { "@babel/runtime": "^7.18.3", "ts-algebra": "^2.0.0" @@ -13144,7 +13106,6 @@ "resolved": "https://registry.npmjs.org/lib0/-/lib0-0.2.117.tgz", "integrity": "sha512-DeXj9X5xDCjgKLU/7RR+/HQEVzuuEUiwldwOGsHK/sfAfELGWEyTcf0x+uOvCvK3O2zPmZePXWL85vtia6GyZw==", "license": "MIT", - "peer": true, "dependencies": { "isomorphic.js": "^0.2.4" }, @@ -14090,6 +14051,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "@types/debug": "^4.0.0", "debug": "^4.0.0", @@ -15210,7 +15172,6 @@ "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "minimist": "^1.2.6" }, @@ -15230,6 +15191,7 @@ "resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.55.1.tgz", "integrity": "sha512-jz4x+TJNFHwHtwuV9vA9rMujcZRb0CEilTEwG2rRSpe/A7Jdkuj8xPKttCgOh+v/lkHy7HsZ64oj+q3xoAFl9A==", "license": "MIT", + "peer": true, "dependencies": { "dompurify": "3.2.7", "marked": "14.0.0" @@ -15980,7 +15942,6 @@ "dev": true, "license": "MIT", "optional": true, - "peer": true, "dependencies": { "commander": "^9.4.0" }, @@ -15998,7 +15959,6 @@ "dev": true, "license": "MIT", "optional": true, - "peer": true, "engines": { "node": "^12.20.0 || >=14" } @@ -16303,6 +16263,7 @@ "resolved": "https://registry.npmjs.org/react/-/react-19.2.4.tgz", "integrity": "sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ==", "license": "MIT", + "peer": true, "engines": { "node": ">=0.10.0" } @@ -16312,6 +16273,7 @@ "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.4.tgz", "integrity": "sha512-AXJdLo8kgMbimY95O2aKQqsz2iWi9jMgKJhRBAxECE4IFxfcazB2LmzloIoibJI3C12IlY20+KFaLv+71bUJeQ==", "license": "MIT", + "peer": true, "dependencies": { "scheduler": "^0.27.0" }, @@ -17649,7 +17611,6 @@ "integrity": "sha512-BbHGOQK9olHPMvQNHWul6MYlrRTAOKn03rOe4A8O3CLWhNf4YHBqq2HJKKC+sfqpxiBY52pNeesD6jIiLDz8jg==", "license": "MIT", "optional": true, - "peer": true, "dependencies": { "@stablelib/base64": "^1.0.0", "fast-sha256": "^1.3.0" @@ -17955,7 +17916,6 @@ "integrity": "sha512-yYrrsWnrXMcdsnu/7YMYAofM1ktpL5By7vZhf15CrXijWWrEYZks5AXBudalfSWJLlnen/QUJUB5aoB0kqZUGA==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "mkdirp": "^0.5.1", "rimraf": "~2.6.2" @@ -17982,7 +17942,6 @@ "deprecated": "Rimraf versions prior to v4 are no longer supported", "dev": true, "license": "ISC", - "peer": true, "dependencies": { "glob": "^7.1.3" }, @@ -18057,6 +18016,7 @@ "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -18151,8 +18111,7 @@ "resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-2.0.0.tgz", "integrity": "sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==", "license": "MIT", - "optional": true, - "peer": true + "optional": true }, "node_modules/ts-api-utils": { "version": "2.4.0", @@ -18179,6 +18138,7 @@ "integrity": "sha512-5C1sg4USs1lfG0GFb2RLXsdpXqBSEhAaA/0kPL01wxzpMqLILNxIxIOKiILz+cdg/pLnOUxFYOR5yhHU666wbw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "esbuild": "~0.27.0", "get-tsconfig": "^4.7.5" @@ -18290,6 +18250,7 @@ "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", "dev": true, "license": "Apache-2.0", + "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -18330,6 +18291,7 @@ "resolved": "https://registry.npmjs.org/unified/-/unified-11.0.5.tgz", "integrity": "sha512-xKvGhPWw3k84Qjh8bI3ZeJjqnyadK+GEFtazSfZv/rKeTkTjOJho6mFqh2SM96iIcZokxiOpg78GazTSg8+KHA==", "license": "MIT", + "peer": true, "dependencies": { "@types/unist": "^3.0.0", "bail": "^2.0.0", @@ -18705,6 +18667,7 @@ "integrity": "sha512-Bby3NOsna2jsjfLVOHKes8sGwgl4TT0E6vvpYgnAYDIF/tie7MRaFthmKuHx1NSXjiTueXH3do80FMQgvEktRg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "esbuild": "^0.27.0", "fdir": "^6.5.0", @@ -18798,6 +18761,7 @@ "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -19085,6 +19049,7 @@ "resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz", "integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==", "license": "MIT", + "peer": true, "funding": { "url": "https://github.com/sponsors/colinhacks" } diff --git a/package.json b/package.json index 8ed8b167a..cab4c4062 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,10 @@ "postinstall": "electron-builder install-app-deps && patch-package && node scripts/patch-xterm-webgl-atlas.cjs", "rebuild": "electron-builder install-app-deps", "tool:cli": "node electron/cli/netcatty-tool-cli.cjs", + "generate:capability-tools": "node scripts/generate-capability-tools.cjs", "lint": "eslint .", "lint:fix": "eslint . --fix", - "test": "node --test --import tsx electron/*.test.cjs electron/capabilities/*.test.cjs electron/capabilities/*/*.test.cjs electron/cli/*.test.cjs electron/bridges/*.test.cjs electron/bridges/*/*.test.cjs electron/bridges/aiBridge/sdk/*.test.cjs scripts/*.test.cjs application/*.test.ts application/app/*.test.ts application/i18n/locales/*.test.ts application/state/*.test.ts application/state/*/*.test.ts components/*.test.ts components/*.test.tsx components/editor/*.test.ts components/editor/*.test.tsx components/notes/*.test.tsx components/port-forwarding/*.test.ts components/systemManager/*.test.ts components/terminalLayer/*.test.ts components/settings/*.test.tsx components/settings/tabs/*.test.ts components/settings/tabs/ai/*.test.ts components/settings/tabs/ai/*.test.tsx components/ai/*.test.ts components/ai/*.test.tsx components/ai-elements/*.test.tsx components/sftp/*.test.ts components/terminal/*.test.ts components/terminal/*.test.tsx components/terminal/runtime/*.test.ts components/vault/*.test.ts components/vault/*.test.tsx domain/*.test.ts domain/*/*.test.ts infrastructure/ai/*.test.ts infrastructure/ai/sdk/*.test.ts infrastructure/config/*.test.ts infrastructure/persistence/*.test.ts infrastructure/services/*/*.test.ts lib/*.test.ts" + "test": "node --test --import tsx electron/*.test.cjs electron/capabilities/*.test.cjs electron/capabilities/*/*.test.cjs electron/capabilities/*/*/*.test.cjs electron/cli/*.test.cjs electron/bridges/*.test.cjs electron/bridges/*/*.test.cjs electron/bridges/aiBridge/sdk/*.test.cjs scripts/*.test.cjs application/*.test.ts application/app/*.test.ts application/i18n/locales/*.test.ts application/state/*.test.ts application/state/*/*.test.ts components/*.test.ts components/*.test.tsx components/editor/*.test.ts components/editor/*.test.tsx components/notes/*.test.tsx components/port-forwarding/*.test.ts components/systemManager/*.test.ts components/terminalLayer/*.test.ts components/settings/*.test.tsx components/settings/tabs/*.test.ts components/settings/tabs/ai/*.test.ts components/settings/tabs/ai/*.test.tsx components/ai/*.test.ts components/ai/*.test.tsx components/ai-elements/*.test.tsx components/sftp/*.test.ts components/terminal/*.test.ts components/terminal/*.test.tsx components/terminal/runtime/*.test.ts components/vault/*.test.ts components/vault/*.test.tsx domain/*.test.ts domain/*/*.test.ts infrastructure/ai/*.test.ts infrastructure/ai/*/*.test.ts infrastructure/ai/sdk/*.test.ts infrastructure/config/*.test.ts infrastructure/persistence/*.test.ts infrastructure/services/*/*.test.ts lib/*.test.ts" }, "dependencies": { "@ai-sdk/anthropic": "^3.0.58", diff --git a/scripts/generate-capability-tools.cjs b/scripts/generate-capability-tools.cjs new file mode 100644 index 000000000..5a3aa9ec5 --- /dev/null +++ b/scripts/generate-capability-tools.cjs @@ -0,0 +1,27 @@ +#!/usr/bin/env node +"use strict"; + +const fs = require("node:fs"); +const path = require("node:path"); +const { AGENT_KINDS, listAgentToolSpecs, listCattyToolSpecs } = require("../electron/capabilities/codegen/toolSurfaces.cjs"); + +const generatedDir = path.join( + __dirname, + "../infrastructure/ai/harness/generated", +); + +function writeSpecs(filename, specs) { + const outputPath = path.join(generatedDir, filename); + fs.mkdirSync(path.dirname(outputPath), { recursive: true }); + fs.writeFileSync(outputPath, `${JSON.stringify(specs, null, 2)}\n`, "utf8"); + return { outputPath, count: specs.length }; +} + +const sidebar = writeSpecs("cattyToolSpecs.json", listCattyToolSpecs()); +const globalAgent = writeSpecs( + "globalAgentToolSpecs.json", + listAgentToolSpecs(AGENT_KINDS.GLOBAL), +); + +process.stdout.write(`Wrote ${sidebar.count} sidebar tool specs to ${sidebar.outputPath}\n`); +process.stdout.write(`Wrote ${globalAgent.count} global agent tool specs to ${globalAgent.outputPath}\n`); diff --git a/scripts/generate-capability-tools.test.cjs b/scripts/generate-capability-tools.test.cjs new file mode 100644 index 000000000..cb4e19a6b --- /dev/null +++ b/scripts/generate-capability-tools.test.cjs @@ -0,0 +1,33 @@ +"use strict"; + +const test = require("node:test"); +const assert = require("node:assert/strict"); +const fs = require("node:fs"); +const path = require("node:path"); + +const { AGENT_KINDS, listAgentToolSpecs, listCattyToolSpecs } = require("../electron/capabilities/codegen/toolSurfaces.cjs"); + +const GENERATED_DIR = path.join( + __dirname, + "..", + "infrastructure", + "ai", + "harness", + "generated", +); + +test("committed cattyToolSpecs.json matches listCattyToolSpecs()", () => { + const committed = JSON.parse( + fs.readFileSync(path.join(GENERATED_DIR, "cattyToolSpecs.json"), "utf8"), + ); + const fresh = listCattyToolSpecs(); + assert.deepEqual(committed, fresh); +}); + +test("committed globalAgentToolSpecs.json matches listAgentToolSpecs(global)", () => { + const committed = JSON.parse( + fs.readFileSync(path.join(GENERATED_DIR, "globalAgentToolSpecs.json"), "utf8"), + ); + const fresh = listAgentToolSpecs(AGENT_KINDS.GLOBAL); + assert.deepEqual(committed, fresh); +}); diff --git a/types/global/netcatty-bridge-ai.d.ts b/types/global/netcatty-bridge-ai.d.ts index dff132ef7..942285bc7 100644 --- a/types/global/netcatty-bridge-ai.d.ts +++ b/types/global/netcatty-bridge-ai.d.ts @@ -94,6 +94,7 @@ declare global { }>; aiMcpUpdateSessions?(sessions: Array<{ sessionId: string; + hostId?: string; hostname: string; label: string; os?: string; @@ -102,7 +103,11 @@ declare global { shellType?: string; deviceType?: string; connected: boolean; + hostChain?: Array<{ hostId: string; label?: string; hostname?: string }>; + activePortForwards?: Array<{ ruleId: string; label?: string; type?: string; localPort?: number; status?: string }>; }>, chatSessionId?: string): Promise<{ ok: boolean }>; + onVaultAgentRequest?(cb: (payload: { requestId: string; op: string; params: Record }) => void): () => void; + respondVaultAgent?(requestId: string, result: Record): Promise<{ ok: boolean; error?: string }>; aiMcpSetToolIntegrationMode?(mode: 'mcp' | 'skills'): Promise<{ ok: boolean; error?: string }>; aiUserSkillsGetStatus?(): Promise<{ ok: boolean; @@ -149,6 +154,9 @@ declare global { }>; aiSdkAgentStream?(requestId: string, chatSessionId: string, sdkBackend: string, prompt: string, cwd?: string, providerId?: string, model?: string, existingSessionId?: string, historyMessages?: Array<{ role: 'user' | 'assistant'; content: string }>, images?: Array<{ base64Data: string; mediaType: string; filename?: string; filePath?: string }>, toolIntegrationMode?: 'mcp' | 'skills', defaultTargetSession?: { sessionId: string; hostname: string; label: string; os?: string; username?: string; protocol?: string; shellType?: string; deviceType?: string; connected: boolean; source: 'scope-target' | 'only-connected-in-scope' }, userSkillsContext?: string, agentEnv?: Record, agentCommand?: string): Promise<{ ok: boolean; error?: string }>; aiSdkAgentListModels?(sdkBackend: string, cwd?: string, providerId?: string, chatSessionId?: string, agentEnv?: Record, agentCommand?: string): Promise<{ ok: boolean; models?: Array<{ id: string; name: string; description?: string; thinkingLevels?: string[] }>; currentModelId?: string | null; error?: string }>; + aiCattyCancelExec?(chatSessionId: string): Promise; + aiSetChatSessionCancelled?(chatSessionId: string, cancelled?: boolean): Promise<{ ok: boolean; error?: string }>; + aiMcpSyncPermissionGrants?(grants: Array>): Promise<{ ok: boolean; count?: number; error?: string }>; aiSdkAgentCancel?(requestId: string, chatSessionId?: string): Promise<{ ok: boolean; error?: string }>; aiSdkAgentCleanup?(chatSessionId: string): Promise<{ ok: boolean }>; onAiSdkAgentEvent?(requestId: string, cb: (event: Record) => void): () => void;