@causyne/agent 1.20.122 → 1.20.124
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/causyne.js +38 -15
- package/docs/SECURITY_REVIEW.md +42 -6
- package/package.json +5 -5
- package/src/acp/agentCatalog.js +74 -50
- package/src/acp/agentCatalog.ts +82 -49
- package/src/acp/client.js +309 -86
- package/src/acp/client.ts +291 -84
- package/src/acp/protocol.js +147 -3
- package/src/acp/protocol.ts +162 -3
- package/src/acp/server.js +138 -39
- package/src/acp/server.ts +122 -41
- package/src/agent/errorClassifier.js +42 -6
- package/src/agent/errorClassifier.ts +44 -5
- package/src/agent/providerFailure.js +1 -1
- package/src/agent/providerFailure.ts +7 -1
- package/src/agent/toolArgValidator.js +38 -4
- package/src/agent/toolArgValidator.ts +39 -4
- package/src/agent/toolTurnIntegrity.js +28 -7
- package/src/agent/toolTurnIntegrity.ts +25 -6
- package/src/agent/workspaceBoundary.js +30 -4
- package/src/agent/workspaceBoundary.ts +32 -4
- package/src/agentLoop.js +598 -217
- package/src/agentLoop.ts +621 -215
- package/src/approvalPolicy.js +28 -1
- package/src/approvalPolicy.ts +28 -1
- package/src/auditLog.js +641 -48
- package/src/auditLog.ts +661 -0
- package/src/autoUpdate.js +32 -5
- package/src/autoUpdate.ts +27 -5
- package/src/backgroundTasks.js +328 -0
- package/src/backgroundTasks.ts +339 -0
- package/src/benchmarks/codingEval.js +558 -0
- package/src/benchmarks/codingEval.ts +628 -0
- package/src/benchmarks/planCache.js +115 -134
- package/src/benchmarks/planCache.ts +163 -0
- package/src/browser/browserRuntime.js +52 -0
- package/src/browser/browserRuntime.ts +55 -0
- package/src/browser/causyneBrowser.js +884 -10
- package/src/browser/causyneBrowser.ts +1042 -32
- package/src/browser/searchProviders.js +94 -6
- package/src/browser/searchProviders.ts +89 -6
- package/src/browser/urlPolicy.js +8 -1
- package/src/browser/urlPolicy.ts +7 -1
- package/src/browser/workspacePathSafety.js +6 -4
- package/src/browser/workspacePathSafety.ts +7 -4
- package/src/capabilities.js +18 -17
- package/src/capabilities.ts +38 -0
- package/src/checkpoints.js +1241 -1127
- package/src/checkpoints.ts +1377 -0
- package/src/cliMode.js +707 -755
- package/src/cliMode.ts +780 -0
- package/src/codebase/repoMap.js +656 -0
- package/src/codebase/repoMap.ts +664 -0
- package/src/commands.js +3651 -2514
- package/src/commands.ts +3985 -0
- package/src/compiledSkills.js +8052 -8054
- package/src/compiledSkills.ts +8078 -0
- package/src/config.js +103 -1
- package/src/config.ts +110 -1
- package/src/configVault.js +29 -2
- package/src/configVault.ts +26 -2
- package/src/contextManager.js +473 -557
- package/src/contextManager.ts +599 -0
- package/src/continuum/adapters/antigravity.js +8 -3
- package/src/continuum/adapters/antigravity.ts +8 -3
- package/src/continuum/sanitizer.js +23 -1
- package/src/continuum/sanitizer.ts +23 -1
- package/src/continuum/ui/continuumPicker.js +0 -2
- package/src/continuum/ui/continuumPicker.ts +0 -2
- package/src/continuum/ui/continuumPickerView.js +0 -2
- package/src/continuum/ui/continuumPickerView.ts +4 -20
- package/src/core/contextCompactor.js +88 -7
- package/src/core/contextCompactor.ts +87 -8
- package/src/creative/liveBridge.js +3 -2
- package/src/creative/liveBridge.ts +4 -2
- package/src/creative/studioScripts.js +1 -1
- package/src/creative/studioScripts.ts +1 -1
- package/src/decideTools.js +7 -2
- package/src/decideTools.ts +7 -2
- package/src/decisionPolicy.js +10 -2
- package/src/decisionPolicy.ts +10 -2
- package/src/designPalettes.js +72 -84
- package/src/designPalettes.ts +111 -0
- package/src/devMode.js +2 -2
- package/src/devMode.ts +6 -0
- package/src/diffReview.js +370 -0
- package/src/diffReview.ts +407 -0
- package/src/dynamicCatalogue.js +311 -11
- package/src/dynamicCatalogue.ts +331 -11
- package/src/embodimentSession.js +143 -154
- package/src/embodimentSession.ts +173 -0
- package/src/engines/installer.js +60 -29
- package/src/engines/installer.ts +73 -22
- package/src/enterprisePolicy.js +660 -0
- package/src/enterprisePolicy.ts +728 -0
- package/src/entrypoint.js +514 -268
- package/src/entrypoint.ts +607 -0
- package/src/gitSnapshot.js +417 -312
- package/src/gitSnapshot.ts +500 -0
- package/src/headlessCli.js +324 -13
- package/src/headlessCli.ts +321 -12
- package/src/hooks.js +247 -26
- package/src/hooks.ts +236 -26
- package/src/ignoreRules.js +297 -0
- package/src/ignoreRules.ts +308 -0
- package/src/implementationPlan.js +7 -6
- package/src/implementationPlan.ts +8 -6
- package/src/infra/atomicWriter.js +92 -19
- package/src/infra/atomicWriter.ts +108 -19
- package/src/launchIntent.js +13 -2
- package/src/launchIntent.ts +13 -2
- package/src/licensing/entitlements.js +553 -237
- package/src/licensing/entitlements.ts +590 -0
- package/src/licensing/policy.js +17 -1
- package/src/licensing/policy.ts +31 -0
- package/src/lsp/lspCatalog.js +398 -0
- package/src/lsp/lspCatalog.ts +452 -0
- package/src/lsp/lspClient.js +699 -0
- package/src/lsp/lspClient.ts +799 -0
- package/src/lsp/lspProtocol.js +121 -0
- package/src/lsp/lspProtocol.ts +140 -0
- package/src/mcp/mcpClient.js +69 -8
- package/src/mcp/mcpClient.ts +66 -9
- package/src/mcp/mcpRegistry.js +180 -7
- package/src/mcp/mcpRegistry.ts +171 -7
- package/src/mcp/mcpSecurity.js +6 -7
- package/src/mcp/mcpSecurity.ts +7 -6
- package/src/memory/episodicStore.js +2 -5
- package/src/memory/episodicStore.ts +2 -5
- package/src/memory.js +80 -32
- package/src/memory.ts +111 -0
- package/src/memoryManager.js +151 -143
- package/src/memoryManager.ts +182 -0
- package/src/modelCatalogue.js +172 -195
- package/src/modelCatalogue.ts +201 -0
- package/src/modelIds.js +8 -27
- package/src/modelIds.ts +33 -0
- package/src/modelVariants.js +600 -0
- package/src/modelVariants.ts +663 -0
- package/src/observer/evidence.js +5 -1
- package/src/observer/evidence.ts +5 -1
- package/src/osKeychain.js +17 -9
- package/src/osKeychain.ts +14 -9
- package/src/outputStore.js +210 -156
- package/src/outputStore.ts +321 -0
- package/src/planCache.js +3 -6
- package/src/planCache.ts +3 -6
- package/src/plugins/pluginManager.js +689 -461
- package/src/plugins/pluginManager.ts +807 -0
- package/src/plugins/pluginRunner.js +82 -37
- package/src/plugins/pluginRunner.ts +98 -0
- package/src/plugins/pluginSkills.js +47 -9
- package/src/plugins/pluginSkills.ts +46 -8
- package/src/pluginsFactory/discovery.js +0 -1
- package/src/pluginsFactory/discovery.ts +0 -1
- package/src/pluginsFactory/templates.js +17 -4
- package/src/pluginsFactory/templates.ts +17 -4
- package/src/prWorkflow.js +337 -0
- package/src/prWorkflow.ts +376 -0
- package/src/procSignal.js +1 -2
- package/src/procSignal.ts +1 -3
- package/src/projectInstructions.js +319 -0
- package/src/projectInstructions.ts +361 -0
- package/src/providers/acp.js +234 -49
- package/src/providers/acp.ts +240 -50
- package/src/providers/anthropic.js +152 -47
- package/src/providers/anthropic.ts +152 -45
- package/src/providers/antigravity.js +565 -354
- package/src/providers/antigravity.ts +555 -355
- package/src/providers/base.js +9 -2
- package/src/providers/base.ts +10 -2
- package/src/providers/byokHttp.js +127 -17
- package/src/providers/byokHttp.ts +175 -18
- package/src/providers/byokStream.js +162 -36
- package/src/providers/byokStream.ts +162 -55
- package/src/providers/dashscope-byok.js +29 -3
- package/src/providers/dashscope-byok.ts +30 -2
- package/src/providers/deepseek.js +12 -3
- package/src/providers/deepseek.ts +12 -3
- package/src/providers/factory.js +41 -0
- package/src/providers/factory.ts +42 -0
- package/src/providers/gemini.js +74 -22
- package/src/providers/gemini.ts +78 -26
- package/src/providers/moonshot.js +36 -3
- package/src/providers/moonshot.ts +32 -3
- package/src/providers/ollama.js +267 -21
- package/src/providers/ollama.ts +302 -29
- package/src/providers/openai-compatible.js +67 -11
- package/src/providers/openai-compatible.ts +78 -17
- package/src/providers/openai.js +25 -2
- package/src/providers/openai.ts +24 -2
- package/src/providers/openrouter.js +233 -66
- package/src/providers/openrouter.ts +226 -70
- package/src/providers/qwen-byok.js +29 -3
- package/src/providers/qwen-byok.ts +30 -2
- package/src/providers/wireNormalizer.js +66 -0
- package/src/providers/wireNormalizer.ts +64 -0
- package/src/providers/xai.js +10 -3
- package/src/providers/xai.ts +9 -3
- package/src/provisioning/capabilityConsent.js +29 -11
- package/src/provisioning/capabilityConsent.ts +42 -0
- package/src/provisioning/capabilityProvisioner.js +309 -197
- package/src/provisioning/capabilityProvisioner.ts +413 -0
- package/src/provisioning/capabilityTools.js +31 -17
- package/src/provisioning/capabilityTools.ts +43 -0
- package/src/provisioning/npmInstall.js +33 -5
- package/src/provisioning/npmInstall.ts +33 -5
- package/src/reporting.js +328 -326
- package/src/reporting.ts +482 -0
- package/src/sandbox.js +37 -0
- package/src/sandbox.ts +38 -0
- package/src/sessionJournal.js +24 -3
- package/src/sessionJournal.ts +18 -3
- package/src/sessionStore.js +1 -2
- package/src/sessionStore.ts +2 -0
- package/src/slopDetector.js +202 -265
- package/src/slopDetector.ts +279 -0
- package/src/smartToolsUse.js +10 -4
- package/src/smartToolsUse.ts +10 -4
- package/src/stallGuard.js +10 -3
- package/src/stallGuard.ts +9 -3
- package/src/taskBuffer.js +91 -40
- package/src/taskBuffer.ts +94 -0
- package/src/tokenTracker.js +78 -11
- package/src/tokenTracker.ts +69 -11
- package/src/toolPacks.js +5 -0
- package/src/toolPacks.ts +5 -0
- package/src/tools/applyPatch.js +610 -0
- package/src/tools/applyPatch.ts +661 -0
- package/src/tools/archive.js +135 -21
- package/src/tools/archive.ts +136 -21
- package/src/tools/astryxOracle.js +77 -72
- package/src/tools/astryxOracle.ts +95 -0
- package/src/tools/axSnapshot.js +127 -147
- package/src/tools/axSnapshot.ts +174 -0
- package/src/tools/backgroundTask.js +86 -0
- package/src/tools/backgroundTask.ts +121 -0
- package/src/tools/blenderAutomator.js +434 -390
- package/src/tools/blenderAutomator.ts +623 -0
- package/src/tools/checkSystemSpecs.js +54 -61
- package/src/tools/checkSystemSpecs.ts +73 -0
- package/src/tools/clipboard.js +42 -31
- package/src/tools/clipboard.ts +61 -0
- package/src/tools/codeSearchAst.js +359 -91
- package/src/tools/codeSearchAst.ts +399 -0
- package/src/tools/codeSearcher.js +179 -37
- package/src/tools/codeSearcher.ts +188 -33
- package/src/tools/codeTools.js +208 -245
- package/src/tools/codeTools.ts +271 -0
- package/src/tools/codebaseSearch.js +5 -0
- package/src/tools/codebaseSearch.ts +5 -0
- package/src/tools/desktopAct.js +185 -197
- package/src/tools/desktopAct.ts +257 -0
- package/src/tools/dynamicLoader.js +4 -40
- package/src/tools/dynamicLoader.ts +13 -0
- package/src/tools/executeProceduralMedia.js +117 -86
- package/src/tools/executeProceduralMedia.ts +135 -0
- package/src/tools/fileTracker.js +74 -0
- package/src/tools/fileTracker.ts +94 -0
- package/src/tools/fsDelete.js +57 -47
- package/src/tools/fsDelete.ts +80 -0
- package/src/tools/fsEdit.js +192 -74
- package/src/tools/fsEdit.ts +201 -72
- package/src/tools/fsMove.js +79 -52
- package/src/tools/fsMove.ts +108 -0
- package/src/tools/fsRead.js +54 -15
- package/src/tools/fsRead.ts +55 -15
- package/src/tools/fsSearch.js +5 -0
- package/src/tools/fsSearch.ts +5 -0
- package/src/tools/fsWrite.js +14 -6
- package/src/tools/fsWrite.ts +14 -6
- package/src/tools/gameAiBehaviorTree.js +74 -91
- package/src/tools/gameAiBehaviorTree.ts +219 -0
- package/src/tools/gameAnimationRig.js +128 -147
- package/src/tools/gameAnimationRig.ts +215 -0
- package/src/tools/gameAssetFetcher.js +403 -441
- package/src/tools/gameAssetFetcher.ts +477 -0
- package/src/tools/gameAssetHarvester.js +171 -200
- package/src/tools/gameAssetHarvester.ts +261 -0
- package/src/tools/gameAudioSynth.js +223 -259
- package/src/tools/gameAudioSynth.ts +255 -0
- package/src/tools/gameBuilder.js +100 -91
- package/src/tools/gameBuilder.ts +913 -0
- package/src/tools/gameCanvasAudit.js +138 -96
- package/src/tools/gameCanvasAudit.ts +157 -0
- package/src/tools/gameCinematicSequencer.js +93 -111
- package/src/tools/gameCinematicSequencer.ts +165 -0
- package/src/tools/gameDataForge.js +115 -118
- package/src/tools/gameDataForge.ts +151 -0
- package/src/tools/gameDialogueTreeGen.js +125 -156
- package/src/tools/gameDialogueTreeGen.ts +159 -0
- package/src/tools/gameEconomyBalancer.js +150 -153
- package/src/tools/gameEconomyBalancer.ts +186 -0
- package/src/tools/gameEntitySpawner.js +91 -108
- package/src/tools/gameEntitySpawner.ts +209 -0
- package/src/tools/gameLoreForge.js +125 -147
- package/src/tools/gameLoreForge.ts +162 -0
- package/src/tools/gameQuestArcBuilder.js +105 -144
- package/src/tools/gameQuestArcBuilder.ts +147 -0
- package/src/tools/gameSceneBuilder.js +143 -149
- package/src/tools/gameSceneBuilder.ts +196 -0
- package/src/tools/gameShaderForge.js +83 -101
- package/src/tools/gameShaderForge.ts +181 -0
- package/src/tools/gameTilemapGenerator.js +99 -129
- package/src/tools/gameTilemapGenerator.ts +158 -0
- package/src/tools/gameUiHudBuilder.js +109 -129
- package/src/tools/gameUiHudBuilder.ts +300 -0
- package/src/tools/gameVfxEmitterGen.js +86 -89
- package/src/tools/gameVfxEmitterGen.ts +133 -0
- package/src/tools/gameWebRuntimeInspector.js +153 -86
- package/src/tools/gameWebRuntimeInspector.ts +167 -0
- package/src/tools/generateMediaViaApi.js +53 -12
- package/src/tools/generateMediaViaApi.ts +52 -12
- package/src/tools/gitExec.js +133 -86
- package/src/tools/gitExec.ts +146 -0
- package/src/tools/gitWorktree.js +115 -0
- package/src/tools/gitWorktree.ts +150 -0
- package/src/tools/godotAutomator.js +417 -394
- package/src/tools/godotAutomator.ts +530 -0
- package/src/tools/lspCodeIntel.js +217 -0
- package/src/tools/lspCodeIntel.ts +260 -0
- package/src/tools/manageLocalAiDependencies.js +242 -206
- package/src/tools/manageLocalAiDependencies.ts +288 -0
- package/src/tools/multiEdit.js +16 -76
- package/src/tools/multiEdit.ts +20 -77
- package/src/tools/osSandbox.js +152 -15
- package/src/tools/osSandbox.ts +148 -15
- package/src/tools/palmierAutomator.js +147 -95
- package/src/tools/palmierAutomator.ts +171 -0
- package/src/tools/pdfReader.js +95 -104
- package/src/tools/pdfReader.ts +125 -0
- package/src/tools/persistentShell.js +267 -144
- package/src/tools/persistentShell.ts +314 -0
- package/src/tools/pluginManager.js +84 -72
- package/src/tools/pluginManager.ts +109 -0
- package/src/tools/printer.js +278 -296
- package/src/tools/printer.ts +326 -0
- package/src/tools/procTerm.js +96 -83
- package/src/tools/procTerm.ts +127 -0
- package/src/tools/processList.js +45 -48
- package/src/tools/processList.ts +73 -0
- package/src/tools/registry.js +298 -286
- package/src/tools/registry.ts +486 -0
- package/src/tools/repoMap.js +46 -0
- package/src/tools/repoMap.ts +66 -0
- package/src/tools/subagentOrchestrator.js +404 -348
- package/src/tools/subagentOrchestrator.ts +457 -0
- package/src/tools/sysRun.js +83 -26
- package/src/tools/sysRun.ts +80 -26
- package/src/tools/toolForge.js +84 -67
- package/src/tools/toolForge.ts +102 -0
- package/src/tools/toolPathSafety.js +262 -0
- package/src/tools/toolPathSafety.ts +308 -0
- package/src/tools/toolScanner.js +28 -0
- package/src/tools/toolScanner.ts +29 -0
- package/src/tools/toolSelfRepair.js +21 -6
- package/src/tools/toolSelfRepair.ts +26 -6
- package/src/tools/walkerSafety.ts +1 -0
- package/src/tools/webFetch.js +5 -2
- package/src/tools/webFetch.ts +5 -2
- package/src/toolsList.js +5 -0
- package/src/toolsList.ts +5 -0
- package/src/tui/ask.js +159 -168
- package/src/tui/ask.ts +259 -0
- package/src/tui/block.js +0 -1
- package/src/tui/block.ts +0 -1
- package/src/tui/chatRenderer.js +1193 -1441
- package/src/tui/chatRenderer.ts +1336 -0
- package/src/tui/clipboardImage.js +36 -12
- package/src/tui/clipboardImage.ts +39 -13
- package/src/tui/clipboardSync.js +34 -9
- package/src/tui/clipboardSync.ts +38 -9
- package/src/tui/codeProgress.js +0 -1
- package/src/tui/codeProgress.ts +0 -1
- package/src/tui/completion.js +395 -371
- package/src/tui/completion.ts +574 -0
- package/src/tui/composerControls.js +113 -19
- package/src/tui/composerControls.ts +124 -19
- package/src/tui/consoleGuard.js +27 -7
- package/src/tui/consoleGuard.ts +29 -7
- package/src/tui/dialogs.js +864 -717
- package/src/tui/dialogs.ts +984 -0
- package/src/tui/diff.js +205 -195
- package/src/tui/diff.ts +336 -0
- package/src/tui/history.js +143 -108
- package/src/tui/history.ts +167 -0
- package/src/tui/home.js +165 -161
- package/src/tui/home.ts +251 -0
- package/src/tui/invocation.js +58 -65
- package/src/tui/invocation.ts +117 -0
- package/src/tui/keys.js +437 -420
- package/src/tui/keys.ts +597 -0
- package/src/tui/lineEditor.js +477 -432
- package/src/tui/lineEditor.ts +542 -0
- package/src/tui/messageActionPlacement.js +0 -1
- package/src/tui/messageActionPlacement.ts +0 -1
- package/src/tui/messageInteraction.js +0 -2
- package/src/tui/messageInteraction.ts +0 -2
- package/src/tui/modelPicker.js +0 -3
- package/src/tui/modelPicker.ts +3 -6
- package/src/tui/observerPicker.js +0 -2
- package/src/tui/observerPicker.ts +0 -2
- package/src/tui/overlay.js +0 -1
- package/src/tui/overlay.ts +0 -1
- package/src/tui/prompt.js +1816 -2151
- package/src/tui/prompt.ts +1870 -0
- package/src/tui/runningLight.js +0 -1
- package/src/tui/runningLight.ts +0 -1
- package/src/tui/selectList.js +365 -337
- package/src/tui/selectList.ts +441 -0
- package/src/tui/sessionPicker.js +0 -1
- package/src/tui/sessionPicker.ts +0 -1
- package/src/tui/sessionPickerView.js +0 -2
- package/src/tui/sessionPickerView.ts +4 -21
- package/src/tui/sessionSidebar.js +3 -1
- package/src/tui/sessionSidebar.ts +4 -1
- package/src/tui/setup.js +17 -5
- package/src/tui/setup.ts +19 -5
- package/src/tui/skillAuthoring.js +0 -2
- package/src/tui/skillAuthoring.ts +0 -3
- package/src/tui/stageSurface.js +829 -909
- package/src/tui/stageSurface.ts +1003 -0
- package/src/tui/statusLine.js +7 -4
- package/src/tui/statusLine.ts +7 -3
- package/src/tui/terminalInputFramer.js +82 -69
- package/src/tui/terminalInputFramer.ts +116 -0
- package/src/tui/text.js +25 -10
- package/src/tui/text.ts +24 -10
- package/src/tui/textLayout.js +0 -1
- package/src/tui/textLayout.ts +0 -1
- package/src/tui/theme.js +245 -250
- package/src/tui/theme.ts +441 -0
- package/src/tui/thinkingBox.js +0 -1
- package/src/tui/thinkingBox.ts +0 -1
- package/src/tui/transcript.js +239 -216
- package/src/tui/transcript.ts +324 -0
- package/src/tui/turnRail.js +53 -54
- package/src/tui/turnRail.ts +51 -49
- package/src/tui/usageCard.js +0 -2
- package/src/tui/usageCard.ts +0 -2
- package/src/tui/workspaceSwitch.js +4 -2
- package/src/tui/workspaceSwitch.ts +5 -2
- package/src/types/contracts.ts +4 -1
- package/src/utils/browserOpen.js +107 -0
- package/src/utils/browserOpen.ts +129 -0
- package/src/utils/cli_ui.js +0 -0
- package/src/utils/cli_ui.ts +158 -0
- package/src/utils/winSpawn.js +248 -0
- package/src/utils/winSpawn.ts +275 -0
- package/src/verification/boundedExplorer.js +138 -107
- package/src/verification/boundedExplorer.ts +154 -0
- package/src/verification/byokSmokeProbe.js +193 -0
- package/src/verification/byokSmokeProbe.ts +234 -0
- package/src/verification/cliProbe.js +144 -110
- package/src/verification/cliProbe.ts +170 -0
- package/src/verification/featureMap.js +105 -75
- package/src/verification/featureMap.ts +124 -0
- package/src/verification/interactivePty.js +195 -100
- package/src/verification/interactivePty.ts +333 -0
- package/src/verification/propertyProbe.js +109 -101
- package/src/verification/propertyProbe.ts +159 -0
- package/src/verification/ptyProbe.js +168 -73
- package/src/verification/ptyProbe.ts +254 -0
- package/src/verification/skillCatalog.js +25 -25
- package/src/verification/skillCatalog.ts +66 -0
- package/src/verification/statechart.js +140 -127
- package/src/verification/statechart.ts +196 -0
- package/src/verification/uiRuntime.js +80 -49
- package/src/verification/uiRuntime.ts +88 -0
- package/src/version.js +37 -32
- package/src/version.ts +45 -0
- package/src/web/guiLauncher.js +89 -0
- package/src/web/guiLauncher.ts +119 -0
- package/src/web/skybridgeBrowserBridge.js +256 -0
- package/src/web/skybridgeBrowserBridge.ts +285 -0
- package/src/web/skybridgeBrowserMcp.js +730 -0
- package/src/web/skybridgeBrowserMcp.ts +813 -0
- package/src/web/skybridgeClientScript.js +3951 -0
- package/src/web/skybridgeClientScript.ts +3952 -0
- package/src/web/skybridgeServer.js +2622 -0
- package/src/web/skybridgeServer.ts +2787 -0
- package/src/web/skybridgeStyles.js +827 -0
- package/src/web/skybridgeStyles.ts +828 -0
- package/src/web/skybridgeUi.js +472 -0
- package/src/web/skybridgeUi.ts +485 -0
- package/src/worktreeManager.js +511 -0
- package/src/worktreeManager.ts +546 -0
- package/src/zeroCostFailover.js +168 -0
- package/src/zeroCostFailover.ts +198 -0
- package/src/zeroCostRouting.js +391 -0
- package/src/zeroCostRouting.ts +437 -0
- package/src/agentLoop.d.ts +0 -65
- package/src/auditLog.d.ts +0 -3
- package/src/checkpoints.d.ts +0 -4
- package/src/compiledSkills.d.ts +0 -5
- package/src/contextManager.d.ts +0 -14
- package/src/designPalettes.d.ts +0 -2
- package/src/devMode.d.ts +0 -1
- package/src/embodimentSession.d.ts +0 -2
- package/src/licensing/entitlements.d.ts +0 -14
- package/src/memory.d.ts +0 -13
- package/src/memoryManager.d.ts +0 -1
- package/src/modelCatalogue.d.ts +0 -6
- package/src/modelIds.d.ts +0 -2
- package/src/outputStore.d.ts +0 -4
- package/src/plugins/pluginManager.d.ts +0 -7
- package/src/providers/antigravity.d.ts +0 -19
- package/src/provisioning/capabilityProvisioner.d.ts +0 -12
- package/src/slopDetector.d.ts +0 -3
- package/src/taskBuffer.d.ts +0 -1
- package/src/tools/desktopAct.d.ts +0 -10
- package/src/tools/registry.d.ts +0 -5
- package/src/tui/chatRenderer.d.ts +0 -27
- package/src/tui/diff.d.ts +0 -3
- package/src/utils/cli_ui.d.ts +0 -19
- package/src/utils/paths.d.ts +0 -3
- package/src/verification/skillCatalog.d.ts +0 -1
- package/src/version.d.ts +0 -1
package/src/tui/prompt.js
CHANGED
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
// never be the reason chat does not start.
|
|
17
17
|
//
|
|
18
18
|
// Pure Node. No dependency, no shell.
|
|
19
|
-
|
|
20
19
|
import { KeyDecoder, bracketedPaste } from './keys.js';
|
|
21
20
|
import { TerminalInputFramer, INPUT_PREFIX_TIMEOUT_MS, shouldTreatAsAtomicPaste } from './terminalInputFramer.js';
|
|
22
21
|
import { LineEditor } from './lineEditor.js';
|
|
@@ -26,15 +25,14 @@ import { removeInlineSlashToken, isInlineSkillInvocation } from './inlineSlash.j
|
|
|
26
25
|
import { paint, dim, bold, GLYPHS, terminalWidth, HAS_COLOR } from './theme.js';
|
|
27
26
|
import { barBlock, GUTTER } from './block.js';
|
|
28
27
|
import { composerGeometry } from './composerGeometry.js';
|
|
29
|
-
import { visibleWidth, truncate, wrapBlock, shortenHome, safeDisplay
|
|
28
|
+
import { visibleWidth, truncate, wrapBlock, shortenHome, safeDisplay } from './text.js';
|
|
30
29
|
import { pickExample, fitHints } from './banner.js';
|
|
31
30
|
import { panelRows } from './overlay.js';
|
|
32
31
|
import { renderUsageCardRows, renderStatusCardRows, collectUsageCardData } from './usageCard.js';
|
|
33
32
|
import { buildStatusText } from '../reporting.js';
|
|
34
33
|
import { commandPalette, skillPicker, ApprovalLayer, DecisionLayer, ConfirmLayer, HomeLayer, TextLayer } from './dialogs.js';
|
|
35
34
|
import { describeTool } from './toolDisplay.js';
|
|
36
|
-
import {
|
|
37
|
-
import { readClipboardImageSync, formatByteSize, loadLocalImageAttachment } from './clipboardImage.js';
|
|
35
|
+
import { writeClipboardSync } from './clipboardSync.js';
|
|
38
36
|
import { pasteIntoChatbox } from './chatboxClipboard.js';
|
|
39
37
|
import { handleViewportWheel, handleScrollbarMouse } from './scrollInteraction.js';
|
|
40
38
|
import { centerRows, sessionRows, dialogWidth, dialogHeight } from './layout.js';
|
|
@@ -44,2191 +42,1858 @@ import { isStartupDialog, startupDialogView } from './homeView.js';
|
|
|
44
42
|
import { handleMessageMouse, handleMessageActionMenuMouse, isMouseMotion } from './messageInteraction.js';
|
|
45
43
|
import { handleTurnRailMouse } from './turnRail.js';
|
|
46
44
|
import { runWithStageWrite } from './consoleGuard.js';
|
|
47
|
-
import {
|
|
48
|
-
resetTurnInterruptState,
|
|
49
|
-
shouldAcceptStatusUpdate,
|
|
50
|
-
triggerTurnInterrupt,
|
|
51
|
-
handlePromptCtrlC,
|
|
52
|
-
handlePromptEscape
|
|
53
|
-
} from './composerControls.js';
|
|
54
|
-
|
|
45
|
+
import { resetTurnInterruptState, shouldAcceptStatusUpdate, triggerTurnInterrupt, handlePromptCtrlC, handlePromptEscape } from './composerControls.js';
|
|
55
46
|
const CTRL_C_WINDOW_MS = 2000;
|
|
56
|
-
|
|
57
47
|
// Rows the editor may occupy before it starts scrolling internally.
|
|
58
|
-
//
|
|
59
|
-
// Without a cap, pasting a 200-line file draws 200 rows and erase() then walks
|
|
60
|
-
// 200 rows back up every keystroke. The live region has to stay small enough
|
|
61
|
-
// that the conversation above it is never pushed off the screen.
|
|
62
48
|
const MAX_PROMPT_ROWS = 8;
|
|
63
|
-
|
|
64
49
|
// How long to wait for a resize gesture to settle before repainting.
|
|
65
|
-
//
|
|
66
|
-
// A window drag delivers a resize event per step — dozens per second. Painting
|
|
67
|
-
// the live region on each one is what filled the screen with overlapping copies
|
|
68
|
-
// of the input row. One repaint per gesture is both correct and invisible to the
|
|
69
|
-
// user at this delay.
|
|
70
50
|
const RESIZE_SETTLE_MS = 60;
|
|
71
|
-
|
|
72
|
-
// Air. The two rows that were missing.
|
|
73
|
-
//
|
|
74
|
-
// opencode's prompt box carries paddingTop={1} INSIDE its filled surface and a
|
|
75
|
-
// one-row bordered spacer between the box and the footer
|
|
76
|
-
// (component/prompt/index.tsx:1361-1363, :1487-1497). Causyne had neither: the
|
|
77
|
-
// caret sat on the very first row of the fill and the footer was welded to the
|
|
78
|
-
// bottom of the box, which is what made the whole thing read as jammed into the
|
|
79
|
-
// last four rows of the terminal.
|
|
80
|
-
//
|
|
81
|
-
// They are constants rather than magic numbers in the layout because the caret
|
|
82
|
-
// row is computed from them — get that wrong and the cursor parks on the wrong
|
|
83
|
-
// line, which is the class of bug erase() exists to prevent.
|
|
84
51
|
const PROMPT_PAD_TOP = 1;
|
|
85
52
|
const PROMPT_PAD_BOTTOM = 1;
|
|
86
53
|
const FOOTER_GAP = 1;
|
|
87
|
-
|
|
88
|
-
// Air is what a roomy terminal can afford, and the first thing a cramped one
|
|
89
|
-
// gives up.
|
|
90
|
-
//
|
|
91
|
-
// opencode's spacers carry `minHeight={0} flexShrink={1}` (routes/home.tsx:73-80)
|
|
92
|
-
// precisely so they collapse before anything with content does. Causyne has no
|
|
93
|
-
// flex engine, so the same idea is arithmetic — and it is not decoration: on a
|
|
94
|
-
// 17-row terminal the extra row made the banner plus the band come to exactly 17,
|
|
95
|
-
// which costs the top of the wordmark. UX-03's sweep is what caught it.
|
|
96
54
|
const ROOMY_TERMINAL_ROWS = 20;
|
|
97
55
|
const CRAMPED_TERMINAL_ROWS = 18;
|
|
98
|
-
|
|
99
56
|
/** Can this process own the keyboard? */
|
|
100
57
|
export function isInteractive() {
|
|
101
|
-
|
|
102
|
-
|
|
58
|
+
if (process.env.CAUSYNE_SIMPLE_INPUT === '1')
|
|
59
|
+
return false;
|
|
60
|
+
return Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
103
61
|
}
|
|
104
|
-
|
|
105
62
|
export class ChatPrompt {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
this.completion = null;
|
|
186
|
-
this.selected = 0;
|
|
187
|
-
this.lastCtrlC = 0;
|
|
188
|
-
this.interruptedTurn = false;
|
|
189
|
-
this.notice = '';
|
|
190
|
-
this.redrawScheduled = false;
|
|
191
|
-
this.resizeTimer = null;
|
|
192
|
-
this.suspendedSurface = null;
|
|
193
|
-
this.pendingAttachments = [];
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// -------------------------------------------------------------------------
|
|
197
|
-
// Lifecycle
|
|
198
|
-
// -------------------------------------------------------------------------
|
|
199
|
-
|
|
200
|
-
writeControl(sequence) {
|
|
201
|
-
if (!sequence) return;
|
|
202
|
-
runWithStageWrite(() => {
|
|
203
|
-
this.out.write(sequence);
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
start() {
|
|
208
|
-
if (this.active) return;
|
|
209
|
-
this.active = true;
|
|
210
|
-
this.inputFramer.clear();
|
|
211
|
-
this.input.setRawMode(true);
|
|
212
|
-
this.input.resume();
|
|
213
|
-
this.input.on('data', this.handleData);
|
|
214
|
-
this.writeControl(bracketedPaste(true));
|
|
215
|
-
// Node's normal exit does not guarantee ChatPrompt.stop() is called.
|
|
216
|
-
// Always restore bracketed paste and raw stdin when the process exits.
|
|
217
|
-
this.exitHook = () => {
|
|
218
|
-
try { this.writeControl(bracketedPaste(false)); } catch { /* terminal already closed */ }
|
|
219
|
-
try { this.input.setRawMode(false); } catch { /* stdin already closed */ }
|
|
220
|
-
};
|
|
221
|
-
process.once('exit', this.exitHook);
|
|
222
|
-
// Rows are laid out against the terminal width; without this they stay stale
|
|
223
|
-
// until the next keystroke, and a narrowed terminal wraps rows that erase()
|
|
224
|
-
// is still counting as one.
|
|
225
|
-
if (typeof this.out.on === 'function') this.out.on('resize', this.handleResize);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
stop() {
|
|
229
|
-
if (!this.active) return;
|
|
230
|
-
this.active = false;
|
|
231
|
-
if (this.layer) {
|
|
232
|
-
const layer = this.layer;
|
|
233
|
-
this.layer = null;
|
|
234
|
-
if (typeof layer.answer === 'function') layer.answer(false);
|
|
235
|
-
}
|
|
236
|
-
this.stopTick();
|
|
237
|
-
if (this.inputFrameTimer) { clearTimeout(this.inputFrameTimer); this.inputFrameTimer = null; }
|
|
238
|
-
if (this.decoderRecoveryTimer) { clearTimeout(this.decoderRecoveryTimer); this.decoderRecoveryTimer = null; }
|
|
239
|
-
this.inputFramer.clear();
|
|
240
|
-
this.decoder.pending = Buffer.alloc(0);
|
|
241
|
-
if (this.resizeTimer) { clearTimeout(this.resizeTimer); this.resizeTimer = null; }
|
|
242
|
-
this.erase();
|
|
243
|
-
// The exit contract (UX-01 rev 2 clause d): the band collapses and the whole
|
|
244
|
-
// session is flushed into real terminal scrollback, so leaving Causyne leaves
|
|
245
|
-
// a transcript behind exactly as it always has.
|
|
246
|
-
if (this.surface) this.surface.close();
|
|
247
|
-
this.writeControl(bracketedPaste(false));
|
|
248
|
-
if (typeof this.out.removeListener === 'function') this.out.removeListener('resize', this.handleResize);
|
|
249
|
-
this.input.off('data', this.handleData);
|
|
250
|
-
try { this.input.setRawMode(false); } catch (e) { /* already closed */ }
|
|
251
|
-
this.input.pause();
|
|
252
|
-
if (this.exitHook) {
|
|
253
|
-
process.removeListener('exit', this.exitHook);
|
|
254
|
-
this.exitHook = null;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* The terminal changed size. Take the live region down before redrawing it.
|
|
260
|
-
*
|
|
261
|
-
* This used to zero drawnRows and cursorRow and then schedule a draw. Zeroing
|
|
262
|
-
* the counters is what erase() reads to decide how far to walk back up — so
|
|
263
|
-
* with them at zero it returned IMMEDIATELY and removed nothing, while the
|
|
264
|
-
* caret was still parked mid-block at column 2 from the previous draw. The
|
|
265
|
-
* redraw then wrote a fresh copy starting at that column, on top of a row that
|
|
266
|
-
* already had content.
|
|
267
|
-
*
|
|
268
|
-
* One stale copy per event. Dragging a window emits hundreds, and the result
|
|
269
|
-
* was a screen filled edge to edge with overlapping copies of the input row.
|
|
270
|
-
*
|
|
271
|
-
* Erasing with the pre-resize geometry is approximate — the terminal has
|
|
272
|
-
* already reflowed what we drew — but approximate removal is categorically
|
|
273
|
-
* better than none, and the debounce below means it happens once per gesture
|
|
274
|
-
* instead of once per pixel.
|
|
275
|
-
*/
|
|
276
|
-
handleResize = () => {
|
|
277
|
-
if (!this.resizeTimer) this.erase();
|
|
278
|
-
// The Stage owns its own geometry and cannot repaint a band whose height it
|
|
279
|
-
// no longer knows. Resizing it here, before the settle timer, would do it
|
|
280
|
-
// once per pixel of a drag; it is done in the settled callback below.
|
|
281
|
-
// A drag delivers a continuous stream of these. Redrawing on every one is
|
|
282
|
-
// both the corruption above and a huge amount of pointless output; settle
|
|
283
|
-
// first, then draw once.
|
|
284
|
-
if (this.resizeTimer) clearTimeout(this.resizeTimer);
|
|
285
|
-
this.resizeTimer = setTimeout(() => {
|
|
286
|
-
this.resizeTimer = null;
|
|
287
|
-
// Geometry from before the reflow is meaningless now. Drop it AFTER the
|
|
288
|
-
// erase above has used it, not before.
|
|
289
|
-
this.drawnRows = 0;
|
|
290
|
-
this.cursorRow = 0;
|
|
291
|
-
// A stage that can no longer fit gives the band back and hands the
|
|
292
|
-
// session to the scrollback renderer rather than painting into a region
|
|
293
|
-
// too short to hold a viewport.
|
|
294
|
-
const settled = settleStageResize(this.surface, this.suspendedSurface);
|
|
295
|
-
this.surface = settled.current;
|
|
296
|
-
this.suspendedSurface = settled.suspended;
|
|
297
|
-
if (settled.restored) this.attachSurface(settled.current);
|
|
298
|
-
this.draw();
|
|
299
|
-
}, RESIZE_SETTLE_MS);
|
|
300
|
-
if (this.resizeTimer.unref) this.resizeTimer.unref();
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
handleData = (chunk) => {
|
|
304
|
-
// KeyDecoder expects complete escape prefixes. A PTY may split ESC from
|
|
305
|
-
// `[<button;x;yM`; framing these bytes first prevents mouse coordinates
|
|
306
|
-
// from becoming chat content. Do not decode bulk paste before this step.
|
|
307
|
-
const frames = this.inputFramer.push(chunk);
|
|
308
|
-
for (const frame of frames) this.handleFramedData(frame);
|
|
309
|
-
if (this.inputFrameTimer) { clearTimeout(this.inputFrameTimer); this.inputFrameTimer = null; }
|
|
310
|
-
if (this.inputFramer.hasPending) {
|
|
311
|
-
this.inputFrameTimer = setTimeout(() => {
|
|
63
|
+
out;
|
|
64
|
+
input;
|
|
65
|
+
root;
|
|
66
|
+
focusDir;
|
|
67
|
+
onInterrupt;
|
|
68
|
+
surface;
|
|
69
|
+
onExit;
|
|
70
|
+
onExpand;
|
|
71
|
+
onOpenMode;
|
|
72
|
+
onCycleMode;
|
|
73
|
+
onOpenModel;
|
|
74
|
+
onOpenVariant;
|
|
75
|
+
onOpenVariantList;
|
|
76
|
+
messageActionLoop;
|
|
77
|
+
getMode;
|
|
78
|
+
editor;
|
|
79
|
+
history;
|
|
80
|
+
index;
|
|
81
|
+
_unsubscribeFileIndex;
|
|
82
|
+
decoder;
|
|
83
|
+
inputFramer;
|
|
84
|
+
inputFrameTimer;
|
|
85
|
+
decoderRecoveryTimer;
|
|
86
|
+
busy;
|
|
87
|
+
active;
|
|
88
|
+
exitHook;
|
|
89
|
+
drawnRows;
|
|
90
|
+
usageCard;
|
|
91
|
+
usageCardLoading;
|
|
92
|
+
statusCard;
|
|
93
|
+
statusCardUsage;
|
|
94
|
+
infoPanelVersion;
|
|
95
|
+
usageRefreshPending;
|
|
96
|
+
cursorRow;
|
|
97
|
+
renderer;
|
|
98
|
+
statusMessage;
|
|
99
|
+
tick;
|
|
100
|
+
tickTimer;
|
|
101
|
+
example;
|
|
102
|
+
commands;
|
|
103
|
+
isCommand;
|
|
104
|
+
layer;
|
|
105
|
+
queue;
|
|
106
|
+
waiter;
|
|
107
|
+
currentSubmissionAttachments;
|
|
108
|
+
completion;
|
|
109
|
+
selected;
|
|
110
|
+
lastCtrlC;
|
|
111
|
+
interruptedTurn;
|
|
112
|
+
notice;
|
|
113
|
+
redrawScheduled;
|
|
114
|
+
resizeTimer;
|
|
115
|
+
suspendedSurface;
|
|
116
|
+
pendingAttachments;
|
|
117
|
+
pickerLayout;
|
|
118
|
+
constructor(options = {}) {
|
|
119
|
+
const opts = options && typeof options === 'object' ? options : {};
|
|
120
|
+
this.out = opts.out || process.stdout;
|
|
121
|
+
this.input = opts.input || process.stdin;
|
|
122
|
+
this.root = opts.root || process.cwd();
|
|
123
|
+
this.focusDir = opts.focusDir || null;
|
|
124
|
+
this.onInterrupt = opts.onInterrupt || (() => { });
|
|
125
|
+
this.surface = opts.surface || null;
|
|
126
|
+
this.onExit = opts.onExit || (() => process.exit(0));
|
|
127
|
+
this.onExpand = opts.onExpand || (() => { });
|
|
128
|
+
this.onOpenMode = opts.onOpenMode || null;
|
|
129
|
+
this.onCycleMode = opts.onCycleMode || null;
|
|
130
|
+
this.onOpenModel = opts.onOpenModel || null;
|
|
131
|
+
this.onOpenVariant = opts.onOpenVariant || null;
|
|
132
|
+
this.onOpenVariantList = opts.onOpenVariantList || null;
|
|
133
|
+
this.messageActionLoop = opts.messageActionLoop || null;
|
|
134
|
+
this.getMode = opts.getMode || (() => 'B');
|
|
135
|
+
this.editor = new LineEditor();
|
|
136
|
+
this.history = opts.history || new PromptHistory();
|
|
137
|
+
this.index = new FileIndex(this.root);
|
|
138
|
+
this._unsubscribeFileIndex = null;
|
|
139
|
+
this.bindFileIndex(this.index);
|
|
140
|
+
this.decoder = new KeyDecoder();
|
|
141
|
+
this.inputFramer = new TerminalInputFramer();
|
|
312
142
|
this.inputFrameTimer = null;
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
143
|
+
this.decoderRecoveryTimer = null;
|
|
144
|
+
this.busy = false;
|
|
145
|
+
this.active = false;
|
|
146
|
+
this.exitHook = null;
|
|
147
|
+
this.drawnRows = 0;
|
|
148
|
+
this.usageCard = null;
|
|
149
|
+
this.usageCardLoading = false;
|
|
150
|
+
this.statusCard = null;
|
|
151
|
+
this.statusCardUsage = null;
|
|
152
|
+
this.infoPanelVersion = 0;
|
|
153
|
+
this.usageRefreshPending = false;
|
|
154
|
+
this.cursorRow = 0;
|
|
155
|
+
this.renderer = opts.renderer || null;
|
|
156
|
+
this.statusMessage = '';
|
|
157
|
+
this.tick = 0;
|
|
158
|
+
this.tickTimer = null;
|
|
159
|
+
this.example = pickExample();
|
|
160
|
+
this.commands = opts.commands || [];
|
|
161
|
+
this.isCommand = typeof opts.isCommand === 'function' ? opts.isCommand : () => false;
|
|
162
|
+
this.layer = null;
|
|
163
|
+
this.queue = [];
|
|
164
|
+
this.waiter = null;
|
|
165
|
+
this.currentSubmissionAttachments = null;
|
|
166
|
+
this.completion = null;
|
|
167
|
+
this.selected = 0;
|
|
168
|
+
this.lastCtrlC = 0;
|
|
169
|
+
this.interruptedTurn = false;
|
|
335
170
|
this.notice = '';
|
|
336
|
-
|
|
171
|
+
this.redrawScheduled = false;
|
|
172
|
+
this.resizeTimer = null;
|
|
173
|
+
this.suspendedSurface = null;
|
|
174
|
+
this.pendingAttachments = [];
|
|
175
|
+
this.pickerLayout = null;
|
|
176
|
+
}
|
|
177
|
+
// -------------------------------------------------------------------------
|
|
178
|
+
// Lifecycle
|
|
179
|
+
// -------------------------------------------------------------------------
|
|
180
|
+
writeControl(sequence) {
|
|
181
|
+
if (!sequence)
|
|
182
|
+
return;
|
|
183
|
+
runWithStageWrite(() => {
|
|
184
|
+
this.out.write(sequence);
|
|
185
|
+
});
|
|
337
186
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
this.
|
|
342
|
-
this.
|
|
343
|
-
this.
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
this.
|
|
398
|
-
this.
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
this.statusMessage = message;
|
|
438
|
-
this.scheduleDraw();
|
|
187
|
+
start() {
|
|
188
|
+
if (this.active)
|
|
189
|
+
return;
|
|
190
|
+
this.active = true;
|
|
191
|
+
this.inputFramer.clear();
|
|
192
|
+
this.input.setRawMode(true);
|
|
193
|
+
this.input.resume();
|
|
194
|
+
this.input.on('data', this.handleData);
|
|
195
|
+
this.writeControl(bracketedPaste(true));
|
|
196
|
+
this.exitHook = () => {
|
|
197
|
+
try {
|
|
198
|
+
this.writeControl(bracketedPaste(false));
|
|
199
|
+
}
|
|
200
|
+
catch { /* terminal already closed */ }
|
|
201
|
+
try {
|
|
202
|
+
this.input.setRawMode(false);
|
|
203
|
+
}
|
|
204
|
+
catch { /* stdin already closed */ }
|
|
205
|
+
};
|
|
206
|
+
process.once('exit', this.exitHook);
|
|
207
|
+
if (typeof this.out.on === 'function')
|
|
208
|
+
this.out.on('resize', this.handleResize);
|
|
209
|
+
}
|
|
210
|
+
stop() {
|
|
211
|
+
if (!this.active) {
|
|
212
|
+
if (this.waiter) {
|
|
213
|
+
const resolve = this.waiter;
|
|
214
|
+
this.waiter = null;
|
|
215
|
+
resolve(null);
|
|
216
|
+
}
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this.active = false;
|
|
220
|
+
if (this.layer) {
|
|
221
|
+
const layer = this.layer;
|
|
222
|
+
this.layer = null;
|
|
223
|
+
if (typeof layer.answer === 'function')
|
|
224
|
+
layer.answer(false);
|
|
225
|
+
}
|
|
226
|
+
if (this.waiter) {
|
|
227
|
+
const resolve = this.waiter;
|
|
228
|
+
this.waiter = null;
|
|
229
|
+
resolve(null);
|
|
230
|
+
}
|
|
231
|
+
if (typeof this._unsubscribeFileIndex === 'function') {
|
|
232
|
+
this._unsubscribeFileIndex();
|
|
233
|
+
this._unsubscribeFileIndex = null;
|
|
234
|
+
}
|
|
235
|
+
this.index?.dispose();
|
|
236
|
+
this.stopTick();
|
|
237
|
+
if (this.inputFrameTimer) {
|
|
238
|
+
clearTimeout(this.inputFrameTimer);
|
|
239
|
+
this.inputFrameTimer = null;
|
|
240
|
+
}
|
|
241
|
+
if (this.decoderRecoveryTimer) {
|
|
242
|
+
clearTimeout(this.decoderRecoveryTimer);
|
|
243
|
+
this.decoderRecoveryTimer = null;
|
|
244
|
+
}
|
|
245
|
+
this.inputFramer.clear();
|
|
246
|
+
this.decoder.pending = Buffer.alloc(0);
|
|
247
|
+
if (this.resizeTimer) {
|
|
248
|
+
clearTimeout(this.resizeTimer);
|
|
249
|
+
this.resizeTimer = null;
|
|
250
|
+
}
|
|
251
|
+
this.erase();
|
|
252
|
+
if (this.surface)
|
|
253
|
+
this.surface.close();
|
|
254
|
+
this.writeControl(bracketedPaste(false));
|
|
255
|
+
if (typeof this.out.removeListener === 'function')
|
|
256
|
+
this.out.removeListener('resize', this.handleResize);
|
|
257
|
+
this.input.off('data', this.handleData);
|
|
258
|
+
try {
|
|
259
|
+
this.input.setRawMode(false);
|
|
260
|
+
}
|
|
261
|
+
catch { /* already closed */ }
|
|
262
|
+
this.input.pause();
|
|
263
|
+
if (this.exitHook) {
|
|
264
|
+
process.removeListener('exit', this.exitHook);
|
|
265
|
+
this.exitHook = null;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
handleResize = () => {
|
|
269
|
+
if (!this.resizeTimer)
|
|
270
|
+
this.erase();
|
|
271
|
+
if (this.resizeTimer)
|
|
272
|
+
clearTimeout(this.resizeTimer);
|
|
273
|
+
this.resizeTimer = setTimeout(() => {
|
|
274
|
+
this.resizeTimer = null;
|
|
275
|
+
this.drawnRows = 0;
|
|
276
|
+
this.cursorRow = 0;
|
|
277
|
+
const settled = settleStageResize(this.surface, this.suspendedSurface);
|
|
278
|
+
this.surface = settled.current;
|
|
279
|
+
this.suspendedSurface = settled.suspended;
|
|
280
|
+
if (settled.restored)
|
|
281
|
+
this.attachSurface(settled.current);
|
|
282
|
+
this.draw();
|
|
283
|
+
}, RESIZE_SETTLE_MS);
|
|
284
|
+
if (this.resizeTimer.unref)
|
|
285
|
+
this.resizeTimer.unref();
|
|
439
286
|
};
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
287
|
+
handleData = (chunk) => {
|
|
288
|
+
const frames = this.inputFramer.push(chunk);
|
|
289
|
+
for (const frame of frames)
|
|
290
|
+
this.handleFramedData(frame);
|
|
291
|
+
if (this.inputFrameTimer) {
|
|
292
|
+
clearTimeout(this.inputFrameTimer);
|
|
293
|
+
this.inputFrameTimer = null;
|
|
294
|
+
}
|
|
295
|
+
if (this.inputFramer.hasPending) {
|
|
296
|
+
this.inputFrameTimer = setTimeout(() => {
|
|
297
|
+
this.inputFrameTimer = null;
|
|
298
|
+
for (const frame of this.inputFramer.flush())
|
|
299
|
+
this.handleFramedData(frame);
|
|
300
|
+
}, INPUT_PREFIX_TIMEOUT_MS);
|
|
301
|
+
this.inputFrameTimer.unref?.();
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
flushCharBatch(charBatch) {
|
|
305
|
+
if (!charBatch)
|
|
306
|
+
return '';
|
|
307
|
+
this.prepareKeyboardState(true);
|
|
308
|
+
this.editor.insert(charBatch);
|
|
309
|
+
if (charBatch.length < 60 && !charBatch.includes('\n')) {
|
|
310
|
+
this.refreshCompletion();
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
this.completion = null;
|
|
314
|
+
}
|
|
315
|
+
return '';
|
|
316
|
+
}
|
|
317
|
+
prepareKeyboardState(clearNotice) {
|
|
318
|
+
if (clearNotice && this.notice && !this.layer)
|
|
319
|
+
this.notice = '';
|
|
320
|
+
if (this.surface && typeof this.surface.captureMouse === 'function') {
|
|
321
|
+
if (this.surface.captureMouse() && typeof this.notice === 'string' && this.notice.startsWith('top of this view')) {
|
|
322
|
+
this.notice = '';
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (this.surface) {
|
|
326
|
+
this.surface.turnRailHoverSuppressed = false;
|
|
327
|
+
if (this.surface.hoveredTurnIndex !== null && this.surface.hoveredTurnIndex !== undefined) {
|
|
328
|
+
this.surface.hoveredTurnIndex = null;
|
|
329
|
+
this.surface.hoveredTurnAnchorY = null;
|
|
330
|
+
this.scheduleDraw();
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
handleFramedData = (chunk) => {
|
|
335
|
+
let motionOnly = true;
|
|
336
|
+
const str = typeof chunk === 'string' ? chunk : Buffer.isBuffer(chunk) ? chunk.toString('utf8') : '';
|
|
337
|
+
if (!this.layer && shouldTreatAsAtomicPaste(chunk, this.decoder)) {
|
|
338
|
+
try {
|
|
339
|
+
this.handleKey({ name: 'paste', text: str, sequence: '' });
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
this.notice = `paste failed: ${this.sanitizeSingleLine(error instanceof Error ? error.message : String(error))}`;
|
|
343
|
+
this.scheduleDraw();
|
|
344
|
+
}
|
|
345
|
+
this.draw();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const keys = this.decoder.decode(chunk);
|
|
349
|
+
if (this.decoderRecoveryTimer)
|
|
350
|
+
clearTimeout(this.decoderRecoveryTimer);
|
|
351
|
+
this.decoderRecoveryTimer = null;
|
|
352
|
+
if (!this.decoder.inPaste && this.decoder.pending.length > 0) {
|
|
353
|
+
this.decoderRecoveryTimer = setTimeout(() => {
|
|
354
|
+
this.decoderRecoveryTimer = null;
|
|
355
|
+
const recovered = this.decoder.drainIncomplete();
|
|
356
|
+
for (const event of recovered)
|
|
357
|
+
this.handleKey(event);
|
|
358
|
+
if (recovered.length)
|
|
359
|
+
this.scheduleDraw();
|
|
360
|
+
}, 180);
|
|
361
|
+
this.decoderRecoveryTimer.unref?.();
|
|
362
|
+
}
|
|
363
|
+
let charBatch = '';
|
|
364
|
+
for (let i = 0; i < keys.length; i++) {
|
|
365
|
+
const key = keys[i];
|
|
366
|
+
if (!key)
|
|
367
|
+
continue;
|
|
368
|
+
if (!this.layer && key.name === 'char' && typeof key.text === 'string' && !key.alt && !key.ctrl) {
|
|
369
|
+
charBatch += key.text;
|
|
370
|
+
motionOnly = false;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (charBatch.length > 0) {
|
|
374
|
+
charBatch = this.flushCharBatch(charBatch);
|
|
375
|
+
}
|
|
376
|
+
if (key.name !== 'mouse' || !isMouseMotion(key.sequence))
|
|
377
|
+
motionOnly = false;
|
|
378
|
+
try {
|
|
379
|
+
this.handleKey(key);
|
|
380
|
+
}
|
|
381
|
+
catch (e) {
|
|
382
|
+
this.notice = `input error: ${this.sanitizeSingleLine(e instanceof Error ? e.message : String(e))}`;
|
|
383
|
+
this.scheduleDraw();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (charBatch.length > 0) {
|
|
387
|
+
charBatch = this.flushCharBatch(charBatch);
|
|
388
|
+
}
|
|
389
|
+
if (!motionOnly)
|
|
390
|
+
this.draw();
|
|
444
391
|
};
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
// is torn down without one would otherwise leave its promise pending forever
|
|
579
|
-
// and hang the turn.
|
|
580
|
-
if (typeof layer.answer === 'function') layer.answer(false);
|
|
581
|
-
if (this.surface?.stage) this.surface.stage.invalidate();
|
|
582
|
-
this.draw();
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
/** Every command in the app, fuzzy-searchable. */
|
|
586
|
-
openPalette() {
|
|
587
|
-
if (this.commands.length === 0) {
|
|
588
|
-
this.notice = 'no commands available';
|
|
589
|
-
return;
|
|
590
|
-
}
|
|
591
|
-
this.openLayer(commandPalette(this.commands, (command) => {
|
|
592
|
-
// Run it the way the user would have typed it, so there is one execution
|
|
593
|
-
// path for a command however it was reached.
|
|
594
|
-
this.editor.setText(`/${command.name}`);
|
|
595
|
-
this.layer = null;
|
|
596
|
-
this.submit();
|
|
597
|
-
}));
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
openHome(items, onPick) {
|
|
602
|
-
if (!Array.isArray(items) || items.length === 0) {
|
|
603
|
-
this.notice = 'no actions available';
|
|
604
|
-
this.scheduleDraw();
|
|
605
|
-
return null;
|
|
606
|
-
}
|
|
607
|
-
return this.openLayer(new HomeLayer({
|
|
608
|
-
items,
|
|
609
|
-
onPick: (item) => {
|
|
392
|
+
attach(renderer) {
|
|
393
|
+
renderer.onBeforeWrite = () => this.erase();
|
|
394
|
+
renderer.onAfterWrite = () => this.scheduleDraw();
|
|
395
|
+
this.renderer = renderer;
|
|
396
|
+
if (this.surface)
|
|
397
|
+
this.surface.sidebarStatus = renderer.status;
|
|
398
|
+
renderer.quietStatus = true;
|
|
399
|
+
renderer.onStatusChange = (message) => {
|
|
400
|
+
if (!shouldAcceptStatusUpdate(this))
|
|
401
|
+
return;
|
|
402
|
+
this.statusMessage = message;
|
|
403
|
+
this.scheduleDraw();
|
|
404
|
+
};
|
|
405
|
+
renderer.onPlanChange = (plan) => {
|
|
406
|
+
if (this.surface)
|
|
407
|
+
this.surface.sidebarPlan = plan;
|
|
408
|
+
this.scheduleDraw();
|
|
409
|
+
return this.surface && sidebarVisible(this.surface.width, false, this.surface.env) ? 'sidebar' : undefined;
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
statusRow(width) {
|
|
413
|
+
if (!this.renderer || !this.renderer.status)
|
|
414
|
+
return [];
|
|
415
|
+
const status = this.renderer.status;
|
|
416
|
+
if (!this.busy) {
|
|
417
|
+
return [status.renderIdle({
|
|
418
|
+
width: Math.max(10, width),
|
|
419
|
+
hint: this.commands.length > 0 ? 'ctrl+p commands' : ''
|
|
420
|
+
})];
|
|
421
|
+
}
|
|
422
|
+
const frame = GLYPHS.spinner[this.tick % GLYPHS.spinner.length] ?? '*';
|
|
423
|
+
const since = this.renderer.turnStartedAt;
|
|
424
|
+
return [status.render({
|
|
425
|
+
message: this.statusMessage || 'working',
|
|
426
|
+
elapsedMs: since ? Date.now() - since : 0,
|
|
427
|
+
steps: this.renderer.stepCount || 0,
|
|
428
|
+
width: Math.max(10, width - 1),
|
|
429
|
+
spinnerFrame: frame,
|
|
430
|
+
tick: this.tick
|
|
431
|
+
})];
|
|
432
|
+
}
|
|
433
|
+
agentRow(width) {
|
|
434
|
+
const status = this.renderer && this.renderer.status;
|
|
435
|
+
if (!status)
|
|
436
|
+
return null;
|
|
437
|
+
const mode = `${this.busy ? 'Working' : 'Build'} ${this.getMode()}`;
|
|
438
|
+
const parts = [bold(paint('accent', mode))];
|
|
439
|
+
if (status.model)
|
|
440
|
+
parts.push(bold(paint('ink', status.model)));
|
|
441
|
+
if (status.variant)
|
|
442
|
+
parts.push(paint('accent', `[${status.variant}]`));
|
|
443
|
+
const line = parts.join(dim(` ${GLYPHS.dot} `));
|
|
444
|
+
const trailing = [status.provider, status.tier].filter(Boolean).join(' ');
|
|
445
|
+
const base = trailing ? `${line} ${dim(trailing)}` : line;
|
|
446
|
+
const details = [];
|
|
447
|
+
if (this.queue.length)
|
|
448
|
+
details.push(`${this.queue.length} queued`);
|
|
449
|
+
if (this.editor.lines.length > 1)
|
|
450
|
+
details.push(`${this.editor.lines.length} lines`);
|
|
451
|
+
const extra = details.length ? dim(` ${GLYPHS.dot} ${details.join(' · ')}`) : '';
|
|
452
|
+
const withMeta = visibleWidth(base) + visibleWidth(extra) <= width ? base + extra : base;
|
|
453
|
+
const hints = dim(' Ctrl+G/⇧Tab mode · Alt+M model · Ctrl+T variant');
|
|
454
|
+
const full = visibleWidth(withMeta) + visibleWidth(hints) <= width ? withMeta + hints : withMeta;
|
|
455
|
+
return visibleWidth(full) > width ? truncate(full, width, GLYPHS.ellipsis) : full;
|
|
456
|
+
}
|
|
457
|
+
planRows(width) {
|
|
458
|
+
if (this.surface && sidebarVisible(this.surface.width, false, this.surface.env))
|
|
459
|
+
return [];
|
|
460
|
+
const plan = this.renderer && Array.isArray(this.renderer.latestPlan) ? this.renderer.latestPlan : [];
|
|
461
|
+
if (plan.length === 0)
|
|
462
|
+
return [];
|
|
463
|
+
const completed = plan.filter(item => item.status === 'completed').length;
|
|
464
|
+
const stageMax = this.surface?.stage?.maxHeight || this.out?.rows || 24;
|
|
465
|
+
if (stageMax < 22) {
|
|
466
|
+
const activeStep = plan.find(item => item.status === 'in_progress') || plan.find(item => item.status !== 'completed') || plan[0];
|
|
467
|
+
const stepText = activeStep ? (activeStep.text || activeStep.step || '') : '';
|
|
468
|
+
const summary = `${paint('accent', GLYPHS.skill || '✦')} ${bold('plan')} ${dim(`(${completed}/${plan.length})`)} ${dim('·')} ${truncate(safeDisplay(stepText), Math.max(8, width - 24), GLYPHS.ellipsis)}`;
|
|
469
|
+
return [summary, ''];
|
|
470
|
+
}
|
|
471
|
+
const rows = [`${bold(paint('accent', 'implementation plan'))} ${dim(`${completed}/${plan.length}`)}`];
|
|
472
|
+
const limit = Math.min(plan.length, 5);
|
|
473
|
+
const active = plan.findIndex(item => item.status === 'in_progress');
|
|
474
|
+
const pending = plan.findIndex(item => item.status !== 'completed');
|
|
475
|
+
const focus = active >= 0 ? active : pending >= 0 ? pending : plan.length - 1;
|
|
476
|
+
const start = Math.max(0, Math.min(focus - 1, plan.length - limit));
|
|
477
|
+
if (start > 0)
|
|
478
|
+
rows.push(` ${dim(`${GLYPHS.ellipsis} ${start} earlier`)}`);
|
|
479
|
+
for (const item of plan.slice(start, start + limit)) {
|
|
480
|
+
const mark = item.status === 'completed'
|
|
481
|
+
? paint('success', GLYPHS.check)
|
|
482
|
+
: item.status === 'in_progress'
|
|
483
|
+
? paint('accent', GLYPHS.arrow)
|
|
484
|
+
: paint('muted', GLYPHS.dot);
|
|
485
|
+
rows.push(` ${mark} ${truncate(safeDisplay(item.text || item.step || ''), Math.max(8, width - 5), GLYPHS.ellipsis)}`);
|
|
486
|
+
}
|
|
487
|
+
const remaining = plan.length - start - limit;
|
|
488
|
+
if (remaining > 0)
|
|
489
|
+
rows.push(` ${dim(`${GLYPHS.ellipsis} ${remaining} later`)}`);
|
|
490
|
+
rows.push('');
|
|
491
|
+
return rows;
|
|
492
|
+
}
|
|
493
|
+
startTick() {
|
|
494
|
+
if (this.tickTimer)
|
|
495
|
+
return;
|
|
496
|
+
this.tickTimer = setInterval(() => {
|
|
497
|
+
this.tick++;
|
|
498
|
+
if (this.busy)
|
|
499
|
+
this.scheduleDraw();
|
|
500
|
+
if (this.busy && (this.usageCard || this.statusCard !== null) && this.tick % 32 === 0) {
|
|
501
|
+
this.refreshPinnedPanels();
|
|
502
|
+
}
|
|
503
|
+
}, 125);
|
|
504
|
+
if (this.tickTimer.unref)
|
|
505
|
+
this.tickTimer.unref();
|
|
506
|
+
}
|
|
507
|
+
stopTick() {
|
|
508
|
+
if (!this.tickTimer)
|
|
509
|
+
return;
|
|
510
|
+
clearInterval(this.tickTimer);
|
|
511
|
+
this.tickTimer = null;
|
|
512
|
+
}
|
|
513
|
+
openLayer(layer) {
|
|
514
|
+
this.closeLayer();
|
|
515
|
+
this.layer = layer;
|
|
516
|
+
if (this.surface?.stage)
|
|
517
|
+
this.surface.stage.invalidate();
|
|
518
|
+
this.draw();
|
|
519
|
+
return layer;
|
|
520
|
+
}
|
|
521
|
+
closeLayer() {
|
|
522
|
+
const layer = this.layer;
|
|
523
|
+
if (!layer)
|
|
524
|
+
return;
|
|
610
525
|
this.layer = null;
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
* at five. The picker is the same SelectLayer that already backs ctrl+p, so
|
|
646
|
-
* fuzzy search, grouping and scrolling come for free, and every row is
|
|
647
|
-
* truncated to the panel rather than to the terminal.
|
|
648
|
-
*/
|
|
649
|
-
browseSkills(skills, onOpen) {
|
|
650
|
-
if (!Array.isArray(skills) || skills.length === 0) {
|
|
651
|
-
this.notice = 'no skills are compiled into this build';
|
|
652
|
-
this.scheduleDraw();
|
|
653
|
-
return;
|
|
654
|
-
}
|
|
655
|
-
this.openLayer(skillPicker(skills, (skill) => {
|
|
656
|
-
this.layer = null;
|
|
657
|
-
if (onOpen) onOpen(skill);
|
|
658
|
-
}));
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
/** Show local command output inside the live region, never in chat scrollback. */
|
|
662
|
-
showText(title, text, options = {}) {
|
|
663
|
-
const panelWidth = dialogWidth(this.width(), options.width || 76);
|
|
664
|
-
const contentWidth = Math.max(12, panelWidth - 4);
|
|
665
|
-
let lines = wrapBlock(safeDisplay(String(text == null ? '' : text)), contentWidth, { hang: 0 });
|
|
666
|
-
if (lines.length > 0 && lines[0].trim().toLowerCase() === String(title || '').trim().toLowerCase()) {
|
|
667
|
-
lines = lines.slice(1);
|
|
668
|
-
if (lines.length > 0 && /^[-─═_]+$/.test(lines[0].trim())) {
|
|
669
|
-
lines = lines.slice(1);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
return this.openLayer(new TextLayer({
|
|
673
|
-
title: String(title || 'Details'),
|
|
674
|
-
lines: options.danger ? lines.map((line) => paint('danger', line)) : lines,
|
|
675
|
-
width: panelWidth,
|
|
676
|
-
height: options.height || 20
|
|
677
|
-
}));
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
/** Usage and Status are non-modal panels: ordinary chat must not close them. */
|
|
681
|
-
async toggleUsageCard(loop = null) {
|
|
682
|
-
if (this.usageCard || this.usageCardLoading) {
|
|
683
|
-
this.closeUsageCard();
|
|
684
|
-
return false;
|
|
685
|
-
}
|
|
686
|
-
const version = ++this.infoPanelVersion;
|
|
687
|
-
this.statusCard = null;
|
|
688
|
-
this.statusCardUsage = null;
|
|
689
|
-
this.usageCardLoading = true;
|
|
690
|
-
try {
|
|
691
|
-
const data = await collectUsageCardData(loop || this.messageActionLoop);
|
|
692
|
-
if (version !== this.infoPanelVersion || !this.usageCardLoading) return false;
|
|
693
|
-
this.usageCard = data;
|
|
694
|
-
this.usageCardLoading = false;
|
|
695
|
-
this.scheduleDraw();
|
|
696
|
-
return true;
|
|
697
|
-
} catch (error) {
|
|
698
|
-
if (version === this.infoPanelVersion) {
|
|
699
|
-
this.usageCardLoading = false;
|
|
700
|
-
this.notice = this.sanitizeSingleLine(error instanceof Error ? error.message : String(error));
|
|
526
|
+
if (typeof layer.answer === 'function')
|
|
527
|
+
layer.answer(false);
|
|
528
|
+
if (this.surface?.stage)
|
|
529
|
+
this.surface.stage.invalidate();
|
|
530
|
+
this.draw();
|
|
531
|
+
}
|
|
532
|
+
openPalette() {
|
|
533
|
+
if (this.commands.length === 0) {
|
|
534
|
+
this.notice = 'no commands available';
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
this.openLayer(commandPalette(this.commands, (command) => {
|
|
538
|
+
this.editor.setText(`/${command.name}`);
|
|
539
|
+
this.layer = null;
|
|
540
|
+
this.submit();
|
|
541
|
+
}));
|
|
542
|
+
}
|
|
543
|
+
openHome(items, onPick) {
|
|
544
|
+
if (!Array.isArray(items) || items.length === 0) {
|
|
545
|
+
this.notice = 'no actions available';
|
|
546
|
+
this.scheduleDraw();
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
return this.openLayer(new HomeLayer({
|
|
550
|
+
items,
|
|
551
|
+
onPick: (item) => {
|
|
552
|
+
this.layer = null;
|
|
553
|
+
return onPick ? onPick(item) : null;
|
|
554
|
+
}
|
|
555
|
+
}));
|
|
556
|
+
}
|
|
557
|
+
setText(text) {
|
|
558
|
+
this.editor.setText(String(text == null ? '' : text));
|
|
559
|
+
this.completion = null;
|
|
701
560
|
this.scheduleDraw();
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
async refreshUsageCard(loop = null) {
|
|
721
|
-
if (!this.usageCard || this.usageRefreshPending) return false;
|
|
722
|
-
const version = this.infoPanelVersion;
|
|
723
|
-
this.usageRefreshPending = true;
|
|
724
|
-
try {
|
|
725
|
-
const data = await collectUsageCardData(loop || this.messageActionLoop);
|
|
726
|
-
if (version !== this.infoPanelVersion || !this.usageCard) return false;
|
|
727
|
-
this.usageCard = data;
|
|
728
|
-
this.scheduleDraw();
|
|
729
|
-
return true;
|
|
730
|
-
} catch {
|
|
731
|
-
// A transient ledger read failure must not close an already visible panel.
|
|
732
|
-
return false;
|
|
733
|
-
} finally {
|
|
734
|
-
this.usageRefreshPending = false;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
toggleStatusCard(loop = null) {
|
|
739
|
-
if (this.statusCard !== null) {
|
|
740
|
-
this.closeStatusCard();
|
|
741
|
-
return false;
|
|
742
|
-
}
|
|
743
|
-
const targetLoop = loop || this.messageActionLoop;
|
|
744
|
-
++this.infoPanelVersion; // A pending usage read must not reopen a replaced panel.
|
|
745
|
-
this.usageCard = null;
|
|
746
|
-
this.usageCardLoading = false;
|
|
747
|
-
this.statusCardUsage = null;
|
|
748
|
-
this.statusCard = buildStatusText({ loop: targetLoop });
|
|
749
|
-
this.scheduleDraw();
|
|
750
|
-
void this.refreshStatusCardMeta(targetLoop);
|
|
751
|
-
return true;
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
closeStatusCard() {
|
|
755
|
-
if (this.statusCard === null) return false;
|
|
756
|
-
++this.infoPanelVersion;
|
|
757
|
-
this.statusCard = null;
|
|
758
|
-
this.statusCardUsage = null;
|
|
759
|
-
this.scheduleDraw();
|
|
760
|
-
return true;
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
async refreshStatusCardMeta(loop = null) {
|
|
764
|
-
if (this.statusCard === null) return false;
|
|
765
|
-
const version = this.infoPanelVersion;
|
|
766
|
-
try {
|
|
767
|
-
const data = await collectUsageCardData(loop || this.messageActionLoop);
|
|
768
|
-
if (version !== this.infoPanelVersion || this.statusCard === null) return false;
|
|
769
|
-
this.statusCardUsage = data;
|
|
770
|
-
this.scheduleDraw();
|
|
771
|
-
return true;
|
|
772
|
-
} catch {
|
|
773
|
-
return false;
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
refreshPinnedPanels(loop = null) {
|
|
778
|
-
const targetLoop = loop || this.messageActionLoop;
|
|
779
|
-
if (this.usageCard) void this.refreshUsageCard(targetLoop);
|
|
780
|
-
if (this.statusCard !== null) {
|
|
781
|
-
this.statusCard = buildStatusText({ loop: targetLoop });
|
|
782
|
-
this.scheduleDraw();
|
|
783
|
-
void this.refreshStatusCardMeta(targetLoop);
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
statusCardRows(width) {
|
|
788
|
-
if (this.statusCard === null) return [];
|
|
789
|
-
return renderStatusCardRows(this.statusCard, this.statusCardUsage, width);
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
/**
|
|
793
|
-
* Ask for approval of a tool call: risk, arguments and a scrollable diff.
|
|
794
|
-
*
|
|
795
|
-
* Replaces an inquirer.prompt() that took stdin while this prompt was already
|
|
796
|
-
* holding it in raw mode — the two contended, and the confirm could arrive on
|
|
797
|
-
* top of a half-drawn prompt.
|
|
798
|
-
*/
|
|
799
|
-
approve(request = {}) {
|
|
800
|
-
return new Promise((resolve) => {
|
|
801
|
-
// Only the curated verb — "run", "overwrite", "delete". The layer already
|
|
802
|
-
// renders the arguments (or a diff) underneath, so passing a rendered
|
|
803
|
-
// argument summary as `detail` printed the same line twice.
|
|
804
|
-
const { verb } = describeTool(request.tool, request.args || {});
|
|
805
|
-
// Resolve with WHICH option was chosen, not just yes/no: 'always' has to
|
|
806
|
-
// reach the caller so it can record a standing approval. A bare boolean
|
|
807
|
-
// would have silently downgraded it to 'once'.
|
|
808
|
-
this.openLayer(new ApprovalLayer({
|
|
809
|
-
request,
|
|
810
|
-
verb,
|
|
811
|
-
onAnswer: (approved, scope) => resolve({ approved, scope: scope || (approved ? 'once' : 'reject') })
|
|
812
|
-
}));
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
/** Ask for a durable implementation choice, with a free-form option. */
|
|
817
|
-
decide(request = {}) {
|
|
818
|
-
return new Promise((resolve) => {
|
|
819
|
-
this.openLayer(new DecisionLayer({ request, onAnswer: resolve }));
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
/** A plain yes/no, for the questions that are not tool approvals. */
|
|
824
|
-
confirm(message, options = {}) {
|
|
825
|
-
return new Promise((resolve) => {
|
|
826
|
-
let settled = false;
|
|
827
|
-
const settle = (answer) => { if (!settled) { settled = true; resolve(Boolean(answer)); } };
|
|
828
|
-
const layer = new ConfirmLayer({
|
|
829
|
-
message,
|
|
830
|
-
defaultAnswer: options.defaultAnswer === true,
|
|
831
|
-
onAnswer: settle
|
|
832
|
-
});
|
|
833
|
-
this.openLayer(layer);
|
|
834
|
-
});
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
/**
|
|
838
|
-
* Columns available on the stream this prompt actually writes to.
|
|
839
|
-
*
|
|
840
|
-
* draw() used to call the global terminalWidth(), which reads
|
|
841
|
-
* process.stdout.columns. That is right in production — `out` IS stdout — but
|
|
842
|
-
* it means the widget cannot be measured against anything else, and a panel
|
|
843
|
-
* sized from the wrong width overflows and wraps, which corrupts erase()'s row
|
|
844
|
-
* count and starts eating the conversation above.
|
|
845
|
-
*/
|
|
846
|
-
width() {
|
|
847
|
-
// The Stage keeps the composer at one readable measure through the session.
|
|
848
|
-
if (this.surface && typeof this.surface.composeWidth === 'function') {
|
|
849
|
-
const asked = this.surface.composeWidth();
|
|
850
|
-
if (Number.isFinite(asked) && asked > 0) return Math.max(20, asked);
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
const columns = this.out && this.out.columns;
|
|
854
|
-
// No ceiling. The prompt is chrome: the box, its fill and the footer are the
|
|
855
|
-
// surface the user is typing into, and they follow the window. A clamp at
|
|
856
|
-
// 120 left a stretched terminal two-thirds covered and one-third empty.
|
|
857
|
-
return Number.isFinite(columns) && columns > 0
|
|
858
|
-
? Math.max(20, columns)
|
|
859
|
-
: terminalWidth();
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
scheduleDraw() {
|
|
863
|
-
if (this.redrawScheduled || !this.active) return;
|
|
864
|
-
this.redrawScheduled = true;
|
|
865
|
-
setImmediate(() => {
|
|
866
|
-
this.redrawScheduled = false;
|
|
867
|
-
this.draw();
|
|
868
|
-
});
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
// -------------------------------------------------------------------------
|
|
872
|
-
// Reading
|
|
873
|
-
// -------------------------------------------------------------------------
|
|
874
|
-
|
|
875
|
-
/**
|
|
876
|
-
* Render submitted user message into the chat transcript.
|
|
877
|
-
*/
|
|
878
|
-
renderSubmittedUserMessage(text) {
|
|
879
|
-
if (this.surface?.homeInfo) this.surface.leaveHome();
|
|
880
|
-
if (this.renderer?.renderUserMessage) this.renderer.renderUserMessage(text);
|
|
881
|
-
else this.commit('user', (width) => {
|
|
882
|
-
const blockWidth = Math.max(8, width);
|
|
883
|
-
return barBlock(
|
|
884
|
-
wrapBlock(safeDisplay(text), Math.max(8, blockWidth - visibleWidth(GLYPHS.barHeavy || GLYPHS.bar) - GUTTER), { hang: 0 }),
|
|
885
|
-
{ width: blockWidth, role: 'user', filled: true, surface: 'surfaceHi' }
|
|
886
|
-
);
|
|
887
|
-
});
|
|
888
|
-
}
|
|
889
|
-
|
|
890
|
-
/** Restore attachments if an earlier submission was handled as a local command. */
|
|
891
|
-
restoreUnconsumedAttachments() {
|
|
892
|
-
if (Array.isArray(this.currentSubmissionAttachments) && this.currentSubmissionAttachments.length) {
|
|
893
|
-
this.pendingAttachments = this.currentSubmissionAttachments.concat(this.pendingAttachments);
|
|
894
|
-
}
|
|
895
|
-
this.currentSubmissionAttachments = null;
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
takeSubmissionAttachments() {
|
|
899
|
-
const attachments = Array.isArray(this.pendingAttachments) ? this.pendingAttachments : [];
|
|
900
|
-
this.pendingAttachments = [];
|
|
901
|
-
return attachments;
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
/** Queue text with its attachments. Local command values retain their existing type. */
|
|
905
|
-
enqueueSubmission(text, { rendered = false } = {}) {
|
|
906
|
-
if (!Array.isArray(this.queue)) this.queue = [];
|
|
907
|
-
if (this.isCommand(text)) {
|
|
908
|
-
this.queue.push(text);
|
|
909
|
-
} else {
|
|
910
|
-
this.queue.push({ kind: 'chat-submission', text, attachments: this.takeSubmissionAttachments(), rendered });
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
/** Read in FIFO order and display a submitted message only once dispatched. */
|
|
915
|
-
read() {
|
|
916
|
-
this.restoreUnconsumedAttachments();
|
|
917
|
-
if (this.queue.length > 0) {
|
|
918
|
-
const entry = this.queue.shift();
|
|
919
|
-
const submission = entry && typeof entry === 'object' && entry.kind === 'chat-submission';
|
|
920
|
-
const text = submission ? entry.text : entry;
|
|
921
|
-
if (submission) this.currentSubmissionAttachments = entry.attachments;
|
|
922
|
-
if (typeof text === 'string' && !this.isCommand(text) && !(submission && entry.rendered)) {
|
|
923
|
-
this.renderSubmittedUserMessage(text);
|
|
924
|
-
}
|
|
925
|
-
this.scheduleDraw();
|
|
926
|
-
return Promise.resolve(text);
|
|
927
|
-
}
|
|
928
|
-
return new Promise((resolve) => { this.waiter = resolve; });
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
/**
|
|
932
|
-
* Resolve a pending read() with a value nobody typed.
|
|
933
|
-
*
|
|
934
|
-
* The home menu needs this: picking an action must satisfy the same read()
|
|
935
|
-
* the keyboard would have satisfied, or the caller sits waiting for a line
|
|
936
|
-
* that is never coming. It deliberately does NOT echo, add to history or
|
|
937
|
-
* touch the editor — an action is not a message, and showing `start` in the
|
|
938
|
-
* transcript as though the user had said it would be a lie about what
|
|
939
|
-
* happened. If no read is outstanding the value queues, exactly as a typed
|
|
940
|
-
* line would while a turn is running.
|
|
941
|
-
*/
|
|
942
|
-
deliver(value) {
|
|
943
|
-
if (this.waiter) {
|
|
944
|
-
const resolve = this.waiter;
|
|
945
|
-
this.waiter = null;
|
|
946
|
-
resolve(value);
|
|
947
|
-
return true;
|
|
948
|
-
}
|
|
949
|
-
this.queue.push(value);
|
|
950
|
-
return false;
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
/**
|
|
954
|
-
* Consume and return any pending multimodal attachments (e.g. pasted screenshots).
|
|
955
|
-
*/
|
|
956
|
-
consumeAttachments() {
|
|
957
|
-
// An empty submission snapshot still takes precedence over a future draft.
|
|
958
|
-
if (this.currentSubmissionAttachments !== null) {
|
|
959
|
-
const attachments = this.currentSubmissionAttachments;
|
|
960
|
-
this.currentSubmissionAttachments = null;
|
|
961
|
-
return Array.isArray(attachments) ? attachments : [];
|
|
962
|
-
}
|
|
963
|
-
return this.takeSubmissionAttachments();
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
/**
|
|
967
|
-
* Paste clipboard content: seamlessly supports text and screenshot images.
|
|
968
|
-
*/
|
|
969
|
-
pasteClipboard() {
|
|
970
|
-
pasteIntoChatbox(this);
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
setBusy(busy) {
|
|
974
|
-
this.busy = busy;
|
|
975
|
-
resetTurnInterruptState(this, busy);
|
|
976
|
-
if (busy) this.startTick(); else this.stopTick();
|
|
977
|
-
if (!busy && this.usageCard) void this.refreshUsageCard();
|
|
978
|
-
if (this.statusCard !== null) {
|
|
979
|
-
this.statusCard = buildStatusText({ loop: this.messageActionLoop });
|
|
980
|
-
if (!busy) void this.refreshStatusCardMeta(this.messageActionLoop);
|
|
981
|
-
}
|
|
982
|
-
if (this.surface?.stage) this.surface.stage.invalidate();
|
|
983
|
-
this.draw();
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
submit() {
|
|
987
|
-
const text = this.editor.getText();
|
|
988
|
-
if (!text.trim()) return;
|
|
989
|
-
if (!Array.isArray(this.queue)) this.queue = [];
|
|
990
|
-
// busy=false does not imply read() is awaiting: command dispatch has gaps.
|
|
991
|
-
if ((this.busy || !this.waiter) && this.queue.length >= 32
|
|
992
|
-
&& !/^\/(?:stop|interrupt)$/i.test(text.trim())) {
|
|
993
|
-
this.notice = '32 queued messages: wait for a turn to complete before adding another';
|
|
994
|
-
this.scheduleDraw();
|
|
995
|
-
return;
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
this.erase();
|
|
999
|
-
this.history.add(text);
|
|
1000
|
-
this.editor.clear();
|
|
1001
|
-
this.completion = null;
|
|
1002
|
-
// Do not dismiss a pinned information panel on any chat submission,
|
|
1003
|
-
// queued prompt, or agent run. Only Esc or the same command closes it.
|
|
1004
|
-
// Instant slash command shortcuts that operate directly on the prompt surface
|
|
1005
|
-
if ((text.trim().toLowerCase() === '/usage' || text.trim().toLowerCase() === '/cost')
|
|
1006
|
-
&& typeof this.toggleUsageCard === 'function') {
|
|
1007
|
-
void this.toggleUsageCard();
|
|
1008
|
-
return;
|
|
1009
|
-
}
|
|
1010
|
-
if (text.trim().toLowerCase() === '/status' && typeof this.toggleStatusCard === 'function') {
|
|
1011
|
-
this.toggleStatusCard();
|
|
1012
|
-
return;
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
if (text === '/observer' || text === '/observer status') {
|
|
1016
|
-
import('./observerPicker.js').then(({ runInteractiveObserverPicker }) => {
|
|
1017
|
-
void runInteractiveObserverPicker(this);
|
|
1018
|
-
}).catch((err) => {
|
|
1019
|
-
this.notice = err instanceof Error ? err.message : String(err);
|
|
561
|
+
}
|
|
562
|
+
updateModel(provider, model, variant) {
|
|
563
|
+
const nextProvider = provider ? String(provider) : '';
|
|
564
|
+
const nextModel = model ? String(model) : '';
|
|
565
|
+
const sessionUpdate = {
|
|
566
|
+
provider: nextProvider,
|
|
567
|
+
model: nextModel
|
|
568
|
+
};
|
|
569
|
+
if (variant !== undefined) {
|
|
570
|
+
sessionUpdate.variant = variant ? String(variant) : '';
|
|
571
|
+
}
|
|
572
|
+
if (this.renderer && this.renderer.status) {
|
|
573
|
+
this.renderer.status.setSession(sessionUpdate);
|
|
574
|
+
}
|
|
575
|
+
if (this.surface && typeof this.surface.setSessionInfo === 'function') {
|
|
576
|
+
this.surface.setSessionInfo(sessionUpdate);
|
|
577
|
+
}
|
|
1020
578
|
this.scheduleDraw();
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
this.renderSubmittedUserMessage(text);
|
|
1089
|
-
this.enqueueSubmission(text, { rendered: true });
|
|
1090
|
-
this.notice = `queued (${this.queue.length}) — sends when ready`;
|
|
1091
|
-
this.scheduleDraw?.();
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
// -------------------------------------------------------------------------
|
|
1096
|
-
// Keys
|
|
1097
|
-
// -------------------------------------------------------------------------
|
|
1098
|
-
|
|
1099
|
-
handleKey(key) {
|
|
1100
|
-
const clearNotice = key.name === 'char' || key.name === 'paste' || key.name === 'backspace' || key.name === 'delete';
|
|
1101
|
-
if (clearNotice && this.notice && !this.layer) {
|
|
1102
|
-
this.notice = '';
|
|
1103
|
-
}
|
|
1104
|
-
|
|
1105
|
-
// The wheel was handed to the terminal at the top of the band; the moment
|
|
1106
|
-
// the user types again they are driving this prompt, so take it back.
|
|
1107
|
-
if (key.name !== 'wheel' && key.name !== 'mouse') {
|
|
1108
|
-
this.prepareKeyboardState(false);
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
if (key.ctrl && key.name === 'c') {
|
|
1112
|
-
if (this.layer) { this.closeLayer(); return; }
|
|
1113
|
-
if (this.editor.hasSelection) {
|
|
1114
|
-
const text = this.editor.getSelectedText();
|
|
1115
|
-
writeClipboardSync(text);
|
|
1116
|
-
this.editor.killRing = text;
|
|
1117
|
-
this.notice = `copied selection (${text.length} chars) to clipboard`;
|
|
579
|
+
}
|
|
580
|
+
browseSkills(skills, onOpen) {
|
|
581
|
+
if (!Array.isArray(skills) || skills.length === 0) {
|
|
582
|
+
this.notice = 'no skills are compiled into this build';
|
|
583
|
+
this.scheduleDraw();
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
this.openLayer(skillPicker(skills, (skill) => {
|
|
587
|
+
this.layer = null;
|
|
588
|
+
if (onOpen)
|
|
589
|
+
onOpen(skill);
|
|
590
|
+
}));
|
|
591
|
+
}
|
|
592
|
+
showText(title, text, options = {}) {
|
|
593
|
+
const opts = options && typeof options === 'object' ? options : {};
|
|
594
|
+
const panelWidth = dialogWidth(this.width(), opts.width || 76);
|
|
595
|
+
const contentWidth = Math.max(12, panelWidth - 4);
|
|
596
|
+
let lines = wrapBlock(safeDisplay(String(text == null ? '' : text)), contentWidth, { hang: 0 });
|
|
597
|
+
if (lines.length > 0 && (lines[0] ?? '').trim().toLowerCase() === String(title || '').trim().toLowerCase()) {
|
|
598
|
+
lines = lines.slice(1);
|
|
599
|
+
if (lines.length > 0 && /^[-─═_]+$/.test((lines[0] ?? '').trim())) {
|
|
600
|
+
lines = lines.slice(1);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
return this.openLayer(new TextLayer({
|
|
604
|
+
title: String(title || 'Details'),
|
|
605
|
+
lines: opts.danger ? lines.map((line) => paint('danger', line)) : lines,
|
|
606
|
+
width: panelWidth,
|
|
607
|
+
height: opts.height || 20
|
|
608
|
+
}));
|
|
609
|
+
}
|
|
610
|
+
async toggleUsageCard(loop = null) {
|
|
611
|
+
if (this.usageCard || this.usageCardLoading) {
|
|
612
|
+
this.closeUsageCard();
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
const version = ++this.infoPanelVersion;
|
|
616
|
+
this.statusCard = null;
|
|
617
|
+
this.statusCardUsage = null;
|
|
618
|
+
this.usageCardLoading = true;
|
|
619
|
+
try {
|
|
620
|
+
const data = await collectUsageCardData(loop || this.messageActionLoop);
|
|
621
|
+
if (version !== this.infoPanelVersion || !this.usageCardLoading)
|
|
622
|
+
return false;
|
|
623
|
+
this.usageCard = data;
|
|
624
|
+
this.usageCardLoading = false;
|
|
625
|
+
this.scheduleDraw();
|
|
626
|
+
return true;
|
|
627
|
+
}
|
|
628
|
+
catch (error) {
|
|
629
|
+
if (version === this.infoPanelVersion) {
|
|
630
|
+
this.usageCardLoading = false;
|
|
631
|
+
this.notice = this.sanitizeSingleLine(error instanceof Error ? error.message : String(error));
|
|
632
|
+
this.scheduleDraw();
|
|
633
|
+
}
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
showUsageCard(loop = null) {
|
|
638
|
+
return this.toggleUsageCard(loop);
|
|
639
|
+
}
|
|
640
|
+
closeUsageCard() {
|
|
641
|
+
if (!this.usageCard && !this.usageCardLoading)
|
|
642
|
+
return false;
|
|
643
|
+
++this.infoPanelVersion;
|
|
644
|
+
this.usageCard = null;
|
|
645
|
+
this.usageCardLoading = false;
|
|
1118
646
|
this.scheduleDraw();
|
|
1119
|
-
return;
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
if (this.completion && this.handleCompletionKey(key)) return;
|
|
1163
|
-
if (this.handleScrollKey(key)) return;
|
|
1164
|
-
|
|
1165
|
-
const isModeControl = Boolean(key.ctrl && key.name === 'g');
|
|
1166
|
-
const control = isModeControl ? this.onOpenMode
|
|
1167
|
-
: key.alt && key.name === 'm' ? this.onOpenModel
|
|
1168
|
-
: key.ctrl && key.name === 't' ? this.onOpenVariant : null;
|
|
1169
|
-
if (control) {
|
|
1170
|
-
if (this.busy && !isModeControl) {
|
|
1171
|
-
this.notice = 'Finish or interrupt the current turn before switching controls';
|
|
647
|
+
return true;
|
|
648
|
+
}
|
|
649
|
+
async refreshUsageCard(loop = null) {
|
|
650
|
+
if (!this.usageCard || this.usageRefreshPending)
|
|
651
|
+
return false;
|
|
652
|
+
const version = this.infoPanelVersion;
|
|
653
|
+
this.usageRefreshPending = true;
|
|
654
|
+
try {
|
|
655
|
+
const data = await collectUsageCardData(loop || this.messageActionLoop);
|
|
656
|
+
if (version !== this.infoPanelVersion || !this.usageCard)
|
|
657
|
+
return false;
|
|
658
|
+
this.usageCard = data;
|
|
659
|
+
this.scheduleDraw();
|
|
660
|
+
return true;
|
|
661
|
+
}
|
|
662
|
+
catch {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
finally {
|
|
666
|
+
this.usageRefreshPending = false;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
toggleStatusCard(loop = null) {
|
|
670
|
+
if (this.statusCard !== null) {
|
|
671
|
+
this.closeStatusCard();
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
const targetLoop = loop || this.messageActionLoop;
|
|
675
|
+
++this.infoPanelVersion;
|
|
676
|
+
this.usageCard = null;
|
|
677
|
+
this.usageCardLoading = false;
|
|
678
|
+
this.statusCardUsage = null;
|
|
679
|
+
this.statusCard = buildStatusText({ loop: targetLoop });
|
|
680
|
+
this.scheduleDraw();
|
|
681
|
+
void this.refreshStatusCardMeta(targetLoop);
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
closeStatusCard() {
|
|
685
|
+
if (this.statusCard === null)
|
|
686
|
+
return false;
|
|
687
|
+
++this.infoPanelVersion;
|
|
688
|
+
this.statusCard = null;
|
|
689
|
+
this.statusCardUsage = null;
|
|
1172
690
|
this.scheduleDraw();
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
691
|
+
return true;
|
|
692
|
+
}
|
|
693
|
+
async refreshStatusCardMeta(loop = null) {
|
|
694
|
+
if (this.statusCard === null)
|
|
695
|
+
return false;
|
|
696
|
+
const version = this.infoPanelVersion;
|
|
697
|
+
try {
|
|
698
|
+
const data = await collectUsageCardData(loop || this.messageActionLoop);
|
|
699
|
+
if (version !== this.infoPanelVersion || this.statusCard === null)
|
|
700
|
+
return false;
|
|
701
|
+
this.statusCardUsage = data;
|
|
702
|
+
this.scheduleDraw();
|
|
703
|
+
return true;
|
|
704
|
+
}
|
|
705
|
+
catch {
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
refreshPinnedPanels(loop = null) {
|
|
710
|
+
const targetLoop = loop || this.messageActionLoop;
|
|
711
|
+
if (this.usageCard)
|
|
712
|
+
void this.refreshUsageCard(targetLoop);
|
|
713
|
+
if (this.statusCard !== null) {
|
|
714
|
+
this.statusCard = buildStatusText({ loop: targetLoop });
|
|
715
|
+
this.scheduleDraw();
|
|
716
|
+
void this.refreshStatusCardMeta(targetLoop);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
statusCardRows(width) {
|
|
720
|
+
if (this.statusCard === null)
|
|
721
|
+
return [];
|
|
722
|
+
return renderStatusCardRows(this.statusCard, this.statusCardUsage, width);
|
|
723
|
+
}
|
|
724
|
+
approve(request = {}) {
|
|
725
|
+
return new Promise((resolve) => {
|
|
726
|
+
const { verb } = describeTool(request.tool, request.args || {});
|
|
727
|
+
this.openLayer(new ApprovalLayer({
|
|
728
|
+
request,
|
|
729
|
+
verb,
|
|
730
|
+
onAnswer: (approved, scope) => resolve({ approved, scope: scope || (approved ? 'once' : 'reject') })
|
|
731
|
+
}));
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
decide(request = {}) {
|
|
735
|
+
return new Promise((resolve) => {
|
|
736
|
+
this.openLayer(new DecisionLayer({ request, onAnswer: resolve }));
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
confirm(message, options = {}) {
|
|
740
|
+
const opts = options && typeof options === 'object' ? options : {};
|
|
741
|
+
return new Promise((resolve) => {
|
|
742
|
+
let settled = false;
|
|
743
|
+
const settle = (answer) => { if (!settled) {
|
|
744
|
+
settled = true;
|
|
745
|
+
resolve(Boolean(answer));
|
|
746
|
+
} };
|
|
747
|
+
const layer = new ConfirmLayer({
|
|
748
|
+
message,
|
|
749
|
+
defaultAnswer: opts.defaultAnswer === true,
|
|
750
|
+
onAnswer: settle
|
|
751
|
+
});
|
|
752
|
+
this.openLayer(layer);
|
|
1177
753
|
});
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
return
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
if (
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
if (
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
754
|
+
}
|
|
755
|
+
width() {
|
|
756
|
+
if (this.surface && typeof this.surface.composeWidth === 'function') {
|
|
757
|
+
const asked = this.surface.composeWidth();
|
|
758
|
+
if (Number.isFinite(asked) && asked > 0)
|
|
759
|
+
return Math.max(20, asked);
|
|
760
|
+
}
|
|
761
|
+
const columns = this.out && this.out.columns;
|
|
762
|
+
return Number.isFinite(columns) && columns > 0
|
|
763
|
+
? Math.max(20, columns)
|
|
764
|
+
: terminalWidth();
|
|
765
|
+
}
|
|
766
|
+
scheduleDraw() {
|
|
767
|
+
if (this.redrawScheduled || !this.active)
|
|
768
|
+
return;
|
|
769
|
+
this.redrawScheduled = true;
|
|
770
|
+
setImmediate(() => {
|
|
771
|
+
this.redrawScheduled = false;
|
|
772
|
+
this.draw();
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
renderSubmittedUserMessage(text) {
|
|
776
|
+
if (this.surface?.homeInfo)
|
|
777
|
+
this.surface.leaveHome();
|
|
778
|
+
if (this.renderer?.renderUserMessage)
|
|
779
|
+
this.renderer.renderUserMessage(text);
|
|
780
|
+
else
|
|
781
|
+
this.commit('user', (width) => {
|
|
782
|
+
const blockWidth = Math.max(8, width);
|
|
783
|
+
return barBlock(wrapBlock(safeDisplay(text), Math.max(8, blockWidth - visibleWidth(GLYPHS.barHeavy || GLYPHS.bar) - GUTTER), { hang: 0 }), { width: blockWidth, role: 'user', filled: true, surface: 'surfaceHi' });
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
restoreUnconsumedAttachments() {
|
|
787
|
+
if (Array.isArray(this.currentSubmissionAttachments) && this.currentSubmissionAttachments.length) {
|
|
788
|
+
this.pendingAttachments = this.currentSubmissionAttachments.concat(this.pendingAttachments);
|
|
789
|
+
}
|
|
790
|
+
this.currentSubmissionAttachments = null;
|
|
791
|
+
}
|
|
792
|
+
takeSubmissionAttachments() {
|
|
793
|
+
const attachments = Array.isArray(this.pendingAttachments) ? this.pendingAttachments : [];
|
|
794
|
+
this.pendingAttachments = [];
|
|
795
|
+
return attachments;
|
|
796
|
+
}
|
|
797
|
+
enqueueSubmission(text, { rendered = false } = {}) {
|
|
798
|
+
if (!Array.isArray(this.queue))
|
|
799
|
+
this.queue = [];
|
|
800
|
+
if (this.isCommand(text)) {
|
|
801
|
+
this.queue.push(text);
|
|
802
|
+
}
|
|
803
|
+
else {
|
|
804
|
+
this.queue.push({ kind: 'chat-submission', text, attachments: this.takeSubmissionAttachments(), rendered });
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
read() {
|
|
808
|
+
this.restoreUnconsumedAttachments();
|
|
809
|
+
if (this.queue.length > 0) {
|
|
810
|
+
const entry = this.queue.shift();
|
|
811
|
+
const submission = Boolean(entry && typeof entry === 'object' && entry.kind === 'chat-submission');
|
|
812
|
+
const text = submission ? entry.text : entry;
|
|
813
|
+
if (submission)
|
|
814
|
+
this.currentSubmissionAttachments = entry.attachments;
|
|
815
|
+
if (typeof text === 'string' && !this.isCommand(text) && !(submission && entry.rendered)) {
|
|
816
|
+
this.renderSubmittedUserMessage(text);
|
|
817
|
+
}
|
|
818
|
+
this.scheduleDraw();
|
|
819
|
+
return Promise.resolve(text);
|
|
820
|
+
}
|
|
821
|
+
return new Promise((resolve) => { this.waiter = resolve; });
|
|
822
|
+
}
|
|
823
|
+
deliver(value) {
|
|
824
|
+
if (this.waiter) {
|
|
825
|
+
const resolve = this.waiter;
|
|
826
|
+
this.waiter = null;
|
|
827
|
+
resolve(value);
|
|
828
|
+
return true;
|
|
829
|
+
}
|
|
830
|
+
this.queue.push(value);
|
|
831
|
+
return false;
|
|
832
|
+
}
|
|
833
|
+
consumeAttachments() {
|
|
834
|
+
if (this.currentSubmissionAttachments !== null) {
|
|
835
|
+
const attachments = this.currentSubmissionAttachments;
|
|
836
|
+
this.currentSubmissionAttachments = null;
|
|
837
|
+
return Array.isArray(attachments) ? attachments : [];
|
|
838
|
+
}
|
|
839
|
+
return this.takeSubmissionAttachments();
|
|
840
|
+
}
|
|
841
|
+
pasteClipboard() {
|
|
842
|
+
pasteIntoChatbox(this);
|
|
843
|
+
}
|
|
844
|
+
setBusy(busy) {
|
|
845
|
+
this.busy = busy;
|
|
846
|
+
resetTurnInterruptState(this, busy);
|
|
847
|
+
if (busy)
|
|
848
|
+
this.startTick();
|
|
849
|
+
else
|
|
850
|
+
this.stopTick();
|
|
851
|
+
if (!busy && this.usageCard)
|
|
852
|
+
void this.refreshUsageCard();
|
|
853
|
+
if (this.statusCard !== null) {
|
|
854
|
+
this.statusCard = buildStatusText({ loop: this.messageActionLoop });
|
|
855
|
+
if (!busy)
|
|
856
|
+
void this.refreshStatusCardMeta(this.messageActionLoop);
|
|
857
|
+
}
|
|
858
|
+
if (this.surface?.stage)
|
|
859
|
+
this.surface.stage.invalidate();
|
|
860
|
+
this.draw();
|
|
861
|
+
}
|
|
862
|
+
submit() {
|
|
863
|
+
const text = this.editor.getText();
|
|
864
|
+
if (!text.trim())
|
|
865
|
+
return;
|
|
866
|
+
if (!Array.isArray(this.queue))
|
|
867
|
+
this.queue = [];
|
|
868
|
+
if ((this.busy || !this.waiter) && this.queue.length >= 32
|
|
869
|
+
&& !/^\/(?:stop|interrupt)$/i.test(text.trim())) {
|
|
870
|
+
this.notice = '32 queued messages: wait for a turn to complete before adding another';
|
|
871
|
+
this.scheduleDraw();
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
this.erase();
|
|
875
|
+
this.history.add(text);
|
|
876
|
+
this.editor.clear();
|
|
877
|
+
this.completion = null;
|
|
878
|
+
const trimmedLower = text.trim().toLowerCase();
|
|
879
|
+
if ((trimmedLower === '/usage' || trimmedLower === '/cost')
|
|
880
|
+
&& typeof this.toggleUsageCard === 'function') {
|
|
881
|
+
void this.toggleUsageCard();
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
if (trimmedLower === '/status' && typeof this.toggleStatusCard === 'function') {
|
|
885
|
+
this.toggleStatusCard();
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
if (trimmedLower === '/observer' || trimmedLower === '/observer status') {
|
|
889
|
+
import('./observerPicker.js').then(({ runInteractiveObserverPicker }) => {
|
|
890
|
+
void runInteractiveObserverPicker(this);
|
|
891
|
+
}).catch((err) => {
|
|
892
|
+
this.notice = this.sanitizeSingleLine(err instanceof Error ? err.message : String(err));
|
|
893
|
+
this.scheduleDraw();
|
|
894
|
+
});
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
const commandToken = (trimmedLower.split(/\s+/, 1)[0] ?? '').toLowerCase();
|
|
898
|
+
const isControlCommand = this.isCommand(text) && new Set([
|
|
899
|
+
'/stop', '/interrupt', '/new', '/model', '/models',
|
|
900
|
+
'/variant', '/effort', '/reasoning', '/cortex',
|
|
901
|
+
'/gui', '/web', '/studio',
|
|
902
|
+
'/session', '/sessions', '/continuum', '/clear'
|
|
903
|
+
]).has(commandToken);
|
|
904
|
+
if ((trimmedLower === '/stop' || trimmedLower === '/interrupt') && this.busy) {
|
|
905
|
+
this.interrupt();
|
|
906
|
+
this.notice = 'Interrupted';
|
|
907
|
+
this.scheduleDraw();
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
if (isControlCommand && this.busy) {
|
|
911
|
+
this.interrupt();
|
|
912
|
+
}
|
|
913
|
+
if (this.busy) {
|
|
914
|
+
if (this.isCommand(text)) {
|
|
915
|
+
this.notice = `Command ${this.sanitizeSingleLine(text)} queued — will execute when current turn completes`;
|
|
916
|
+
}
|
|
917
|
+
else {
|
|
918
|
+
this.notice = `queued (${this.queue.length + 1}) — sends when this turn ends`;
|
|
919
|
+
}
|
|
920
|
+
this.enqueueSubmission(text);
|
|
921
|
+
this.scheduleDraw();
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
if (this.isCommand(text)) {
|
|
925
|
+
const settle = this.waiter;
|
|
926
|
+
this.waiter = null;
|
|
927
|
+
if (settle)
|
|
928
|
+
settle(text);
|
|
929
|
+
else {
|
|
930
|
+
this.enqueueSubmission(text);
|
|
931
|
+
this.notice = `queued (${this.queue.length}) — sends when ready`;
|
|
932
|
+
this.scheduleDraw?.();
|
|
933
|
+
}
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
const resolve = this.waiter;
|
|
937
|
+
this.waiter = null;
|
|
938
|
+
if (resolve) {
|
|
939
|
+
this.restoreUnconsumedAttachments();
|
|
940
|
+
this.currentSubmissionAttachments = this.takeSubmissionAttachments();
|
|
941
|
+
this.renderSubmittedUserMessage(text);
|
|
942
|
+
resolve(text);
|
|
943
|
+
}
|
|
944
|
+
else {
|
|
945
|
+
this.renderSubmittedUserMessage(text);
|
|
946
|
+
this.enqueueSubmission(text, { rendered: true });
|
|
947
|
+
this.notice = `queued (${this.queue.length}) — sends when ready`;
|
|
948
|
+
this.scheduleDraw?.();
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
// -------------------------------------------------------------------------
|
|
952
|
+
// Keys
|
|
953
|
+
// -------------------------------------------------------------------------
|
|
954
|
+
handleKey(key) {
|
|
955
|
+
const clearNotice = key.name === 'char' || key.name === 'paste' || key.name === 'backspace' || key.name === 'delete';
|
|
956
|
+
if (clearNotice && this.notice && !this.layer) {
|
|
957
|
+
this.notice = '';
|
|
958
|
+
}
|
|
959
|
+
if (key.name !== 'wheel' && key.name !== 'mouse') {
|
|
960
|
+
this.prepareKeyboardState(false);
|
|
961
|
+
}
|
|
962
|
+
if (key.ctrl && key.name === 'c') {
|
|
963
|
+
if (this.layer) {
|
|
964
|
+
this.closeLayer();
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
if (this.editor.hasSelection) {
|
|
968
|
+
const text = this.editor.getSelectedText();
|
|
969
|
+
try {
|
|
970
|
+
writeClipboardSync(text);
|
|
971
|
+
}
|
|
972
|
+
catch { /* clipboard unavailable */ }
|
|
973
|
+
this.editor.killRing = text;
|
|
974
|
+
this.notice = `copied selection (${text.length} chars) to clipboard`;
|
|
975
|
+
this.scheduleDraw();
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
return this.handleCtrlC();
|
|
979
|
+
}
|
|
980
|
+
if (key.ctrl && key.name === 'd' && this.editor.isEmpty)
|
|
981
|
+
return this.onExit();
|
|
982
|
+
if (this.layer) {
|
|
983
|
+
const previousSelection = this.layer.messageActions ? this.layer.list?.selected : null;
|
|
984
|
+
const action = key.name === 'mouse' && this.layer.messageActions
|
|
985
|
+
? handleMessageActionMenuMouse(this.layer, key)
|
|
986
|
+
: this.layer.key(key);
|
|
987
|
+
if (this.layer?.messageActions && isMouseMotion(key.sequence)
|
|
988
|
+
&& previousSelection !== this.layer.list?.selected)
|
|
989
|
+
this.scheduleDraw();
|
|
990
|
+
if (action === 'close') {
|
|
991
|
+
this.closeLayer();
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
if (action === 'dismiss')
|
|
995
|
+
this.closeLayer();
|
|
996
|
+
else if (action !== 'passthrough')
|
|
997
|
+
return;
|
|
998
|
+
}
|
|
999
|
+
if (key.name === 'paste-too-large') {
|
|
1000
|
+
this.notice = 'Paste exceeds 2 MiB; input unchanged. Attach a file or reduce the selection.';
|
|
1001
|
+
this.scheduleDraw();
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
if (key.name === 'paste') {
|
|
1005
|
+
if (this.surface && typeof this.surface.captureMouse === 'function') {
|
|
1006
|
+
this.surface.captureMouse();
|
|
1007
|
+
}
|
|
1008
|
+
pasteIntoChatbox(this, key.text);
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
if (this.completion && this.handleCompletionKey(key))
|
|
1012
|
+
return;
|
|
1013
|
+
if (this.handleScrollKey(key))
|
|
1014
|
+
return;
|
|
1015
|
+
const isModeControl = Boolean((key.ctrl && key.name === 'g') || (key.shift && key.name === 'tab' && this.onCycleMode));
|
|
1016
|
+
const control = (key.ctrl && key.name === 'g') ? this.onOpenMode
|
|
1017
|
+
: (key.shift && key.name === 'tab' && this.onCycleMode) ? this.onCycleMode
|
|
1018
|
+
: key.alt && key.name === 'm' ? this.onOpenModel
|
|
1019
|
+
: key.ctrl && key.name === 't' ? this.onOpenVariant
|
|
1020
|
+
: key.alt && key.name === 't' ? (this.onOpenVariantList || this.onOpenVariant) : null;
|
|
1021
|
+
if (control) {
|
|
1022
|
+
if (this.busy && !isModeControl) {
|
|
1023
|
+
this.notice = 'Finish or interrupt the current turn before switching controls';
|
|
1024
|
+
this.scheduleDraw();
|
|
1025
|
+
}
|
|
1026
|
+
else {
|
|
1027
|
+
Promise.resolve(control()).catch((error) => {
|
|
1028
|
+
this.notice = this.sanitizeSingleLine(error instanceof Error ? error.message : String(error));
|
|
1029
|
+
this.scheduleDraw();
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
switch (key.name) {
|
|
1035
|
+
case 'escape': return this.handleEscape();
|
|
1036
|
+
case 'enter':
|
|
1037
|
+
if (key.alt || key.shift) {
|
|
1038
|
+
this.editor.newline();
|
|
1039
|
+
return this.refreshCompletion();
|
|
1040
|
+
}
|
|
1041
|
+
return this.submit();
|
|
1042
|
+
case 'tab':
|
|
1043
|
+
if (key.shift)
|
|
1044
|
+
return;
|
|
1045
|
+
return this.openCompletion(true);
|
|
1046
|
+
case 'backspace':
|
|
1047
|
+
if (key.alt)
|
|
1048
|
+
this.editor.killWordLeft();
|
|
1049
|
+
else
|
|
1050
|
+
this.editor.backspace();
|
|
1051
|
+
return this.refreshCompletion();
|
|
1052
|
+
case 'delete':
|
|
1053
|
+
this.editor.deleteForward();
|
|
1054
|
+
return this.refreshCompletion();
|
|
1055
|
+
case 'left': return (key.ctrl || key.alt) ? this.editor.moveWordLeft(Boolean(key.shift)) : this.editor.moveLeft(Boolean(key.shift));
|
|
1056
|
+
case 'right': return (key.ctrl || key.alt) ? this.editor.moveWordRight(Boolean(key.shift)) : this.editor.moveRight(Boolean(key.shift));
|
|
1057
|
+
case 'home': return this.editor.moveHome(Boolean(key.shift));
|
|
1058
|
+
case 'end': return this.editor.moveEnd(Boolean(key.shift));
|
|
1059
|
+
case 'up':
|
|
1060
|
+
if (key.shift)
|
|
1061
|
+
return this.editor.moveUp(true);
|
|
1062
|
+
return this.historyOrMove(-1);
|
|
1063
|
+
case 'down':
|
|
1064
|
+
if (key.shift)
|
|
1065
|
+
return this.editor.moveDown(true);
|
|
1066
|
+
return this.historyOrMove(1);
|
|
1067
|
+
case 'pageup':
|
|
1068
|
+
if (key.shift) {
|
|
1069
|
+
this.editor.moveHome(true);
|
|
1070
|
+
this.editor.line = 0;
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
break;
|
|
1074
|
+
case 'pagedown':
|
|
1075
|
+
if (key.shift) {
|
|
1076
|
+
this.editor.moveEnd(true);
|
|
1077
|
+
this.editor.line = this.editor.lines.length - 1;
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
break;
|
|
1081
|
+
case 'char':
|
|
1082
|
+
this.editor.insert(key.text);
|
|
1083
|
+
return this.refreshCompletion();
|
|
1084
|
+
default: break;
|
|
1085
|
+
}
|
|
1086
|
+
if (key.ctrl) {
|
|
1087
|
+
switch (key.name) {
|
|
1088
|
+
case 'a': return this.editor.moveHome();
|
|
1089
|
+
case 'e': return this.editor.moveEnd();
|
|
1090
|
+
case 'k':
|
|
1091
|
+
this.editor.killToEnd();
|
|
1092
|
+
return this.refreshCompletion();
|
|
1093
|
+
case 'u':
|
|
1094
|
+
this.editor.killToStart();
|
|
1095
|
+
return this.refreshCompletion();
|
|
1096
|
+
case 'w':
|
|
1097
|
+
this.editor.killWordLeft();
|
|
1098
|
+
return this.refreshCompletion();
|
|
1099
|
+
case 'y':
|
|
1100
|
+
this.editor.yank();
|
|
1101
|
+
return this.refreshCompletion();
|
|
1102
|
+
case 'x': {
|
|
1103
|
+
if (this.cutSelection())
|
|
1104
|
+
return;
|
|
1105
|
+
break;
|
|
1106
|
+
}
|
|
1107
|
+
case 'p': return this.openPalette();
|
|
1108
|
+
case 'l': return this.clearScreen();
|
|
1109
|
+
case 'o': return this.expand();
|
|
1110
|
+
case 'v': return this.pasteClipboard();
|
|
1111
|
+
default: break;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
if (key.alt && key.name === 'a') {
|
|
1115
|
+
this.editor.selectAll();
|
|
1116
|
+
this.notice = 'selected all input text';
|
|
1117
|
+
this.scheduleDraw();
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
if (key.alt && key.name === 'x') {
|
|
1121
|
+
if (this.cutSelection())
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
if (key.alt && (key.name === 'c' || key.name === 'w')) {
|
|
1125
|
+
const text = this.editor.hasSelection
|
|
1126
|
+
? this.editor.getSelectedText()
|
|
1127
|
+
: this.editor.getText();
|
|
1128
|
+
if (text) {
|
|
1129
|
+
writeClipboardSync(text);
|
|
1130
|
+
this.editor.killRing = text;
|
|
1131
|
+
this.notice = this.editor.hasSelection
|
|
1132
|
+
? `copied selection (${text.length} chars) to clipboard`
|
|
1133
|
+
: 'copied input to clipboard';
|
|
1134
|
+
}
|
|
1135
|
+
else {
|
|
1136
|
+
this.notice = 'input is empty (nothing to copy)';
|
|
1137
|
+
}
|
|
1138
|
+
this.scheduleDraw();
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
if (key.alt && key.name === 'v')
|
|
1142
|
+
return this.pasteClipboard();
|
|
1143
|
+
if (key.alt && key.name === 'b')
|
|
1144
|
+
return this.editor.moveWordLeft();
|
|
1145
|
+
if (key.alt && key.name === 'f')
|
|
1146
|
+
return this.editor.moveWordRight();
|
|
1147
|
+
}
|
|
1148
|
+
handleScrollKey(key) {
|
|
1149
|
+
const viewport = this.surface && this.surface.viewport;
|
|
1150
|
+
if (key.name === 'mouse') {
|
|
1151
|
+
if (handleTurnRailMouse(key, this.surface, this))
|
|
1152
|
+
return true;
|
|
1153
|
+
if (this.messageActionLoop && isMouseMotion(key.sequence)) {
|
|
1154
|
+
return handleMessageMouse(this, this.messageActionLoop, key);
|
|
1155
|
+
}
|
|
1156
|
+
if (handleScrollbarMouse(key, viewport, this.surface, this))
|
|
1157
|
+
return true;
|
|
1158
|
+
if (!this.layer && this.messageActionLoop)
|
|
1159
|
+
return handleMessageMouse(this, this.messageActionLoop, key);
|
|
1160
|
+
return true;
|
|
1161
|
+
}
|
|
1162
|
+
if (key.name === 'wheel') {
|
|
1163
|
+
if (!viewport || !key.wheel)
|
|
1164
|
+
return true;
|
|
1165
|
+
const layout = this.surface?.turnRailLayout;
|
|
1166
|
+
const card = layout?.cardRect;
|
|
1167
|
+
const pointerX = key.wheel.x;
|
|
1168
|
+
const pointerY = key.wheel.y;
|
|
1169
|
+
const relativeY = pointerY - (Number.isFinite(layout?.stageTop) ? layout.stageTop : 0);
|
|
1170
|
+
if (card && Array.isArray(card.groupTurns) &&
|
|
1171
|
+
Number.isFinite(pointerX) && Number.isFinite(pointerY) &&
|
|
1172
|
+
pointerX >= card.x && pointerX < card.x + card.width &&
|
|
1173
|
+
relativeY >= card.y && relativeY < card.y + card.height &&
|
|
1174
|
+
(key.wheel.direction === 'up' || key.wheel.direction === 'down')) {
|
|
1175
|
+
const previous = Number.isFinite(this.surface.turnRailCardOffset)
|
|
1176
|
+
? this.surface.turnRailCardOffset : 0;
|
|
1177
|
+
const maximum = Number.isFinite(card.maxOffset) ? Math.max(0, card.maxOffset) : 0;
|
|
1178
|
+
const change = key.wheel.direction === 'up' ? -1 : 1;
|
|
1179
|
+
const current = Math.max(0, Math.min(maximum, previous + change));
|
|
1180
|
+
if (current !== previous) {
|
|
1181
|
+
this.surface.turnRailCardOffset = current;
|
|
1182
|
+
this.scheduleDraw();
|
|
1183
|
+
}
|
|
1184
|
+
return true;
|
|
1185
|
+
}
|
|
1186
|
+
if (this.renderer?.scrollCodePanel?.(key, this.surface)) {
|
|
1187
|
+
if (typeof this.scheduleDraw === 'function')
|
|
1188
|
+
this.scheduleDraw();
|
|
1189
|
+
else
|
|
1190
|
+
this.draw();
|
|
1191
|
+
return true;
|
|
1192
|
+
}
|
|
1193
|
+
if (this.renderer?.scrollThinkingPanel?.(key, this.surface)) {
|
|
1194
|
+
if (typeof this.scheduleDraw === 'function')
|
|
1195
|
+
this.scheduleDraw();
|
|
1196
|
+
else
|
|
1197
|
+
this.draw();
|
|
1198
|
+
return true;
|
|
1199
|
+
}
|
|
1200
|
+
return handleViewportWheel(key, viewport, this.surface, this);
|
|
1201
|
+
}
|
|
1202
|
+
if (!viewport)
|
|
1203
|
+
return false;
|
|
1204
|
+
const clearTurnRailFocus = () => {
|
|
1205
|
+
if (!this.surface)
|
|
1206
|
+
return;
|
|
1207
|
+
this.surface.activeTurnIndex = null;
|
|
1208
|
+
this.surface.hoveredTurnIndex = null;
|
|
1209
|
+
this.surface.hoveredTurnAnchorY = null;
|
|
1210
|
+
};
|
|
1211
|
+
if (key.name === 'pageup') {
|
|
1212
|
+
clearTurnRailFocus();
|
|
1213
|
+
if (key.shift)
|
|
1214
|
+
viewport.toTop();
|
|
1215
|
+
else
|
|
1216
|
+
viewport.pageUp();
|
|
1217
|
+
return true;
|
|
1218
|
+
}
|
|
1219
|
+
if (key.name === 'pagedown') {
|
|
1220
|
+
clearTurnRailFocus();
|
|
1221
|
+
if (key.shift)
|
|
1222
|
+
viewport.toBottom();
|
|
1223
|
+
else
|
|
1224
|
+
viewport.pageDown();
|
|
1225
|
+
return true;
|
|
1226
|
+
}
|
|
1227
|
+
if (key.shift && key.name === 'up') {
|
|
1228
|
+
if (!this.editor.isEmpty)
|
|
1229
|
+
return false;
|
|
1230
|
+
clearTurnRailFocus();
|
|
1231
|
+
viewport.lineUp();
|
|
1232
|
+
return true;
|
|
1233
|
+
}
|
|
1234
|
+
if (key.shift && key.name === 'down') {
|
|
1235
|
+
if (!this.editor.isEmpty)
|
|
1236
|
+
return false;
|
|
1237
|
+
clearTurnRailFocus();
|
|
1238
|
+
viewport.lineDown();
|
|
1239
|
+
return true;
|
|
1240
|
+
}
|
|
1241
|
+
if (key.alt && key.ctrl && key.name === 'u') {
|
|
1242
|
+
clearTurnRailFocus();
|
|
1243
|
+
viewport.halfPageUp();
|
|
1244
|
+
return true;
|
|
1245
|
+
}
|
|
1246
|
+
if (key.alt && key.ctrl && key.name === 'd') {
|
|
1247
|
+
clearTurnRailFocus();
|
|
1248
|
+
viewport.halfPageDown();
|
|
1249
|
+
return true;
|
|
1250
|
+
}
|
|
1251
|
+
const jumpViewport = (direction, kinds) => {
|
|
1252
|
+
clearTurnRailFocus();
|
|
1253
|
+
viewport.jump(direction, kinds);
|
|
1254
|
+
return true;
|
|
1255
|
+
};
|
|
1256
|
+
if (key.alt && key.name === ',')
|
|
1257
|
+
return jumpViewport(-1, ['step']);
|
|
1258
|
+
if (key.alt && key.name === '.')
|
|
1259
|
+
return jumpViewport(1, ['step']);
|
|
1260
|
+
if ((key.alt && key.name === '[') || (key.alt && key.name === '<'))
|
|
1261
|
+
return jumpViewport(-1, ['user']);
|
|
1262
|
+
if ((key.alt && key.name === ']') || (key.alt && key.name === '>'))
|
|
1263
|
+
return jumpViewport(1, ['user']);
|
|
1264
|
+
if (key.alt && key.name === 'u') {
|
|
1265
|
+
this.offerUndo();
|
|
1266
|
+
return true;
|
|
1267
|
+
}
|
|
1268
|
+
return false;
|
|
1269
|
+
}
|
|
1270
|
+
offerUndo() {
|
|
1271
|
+
const surface = this.surface;
|
|
1272
|
+
if (!surface)
|
|
1273
|
+
return;
|
|
1274
|
+
const width = surface.viewport.width;
|
|
1275
|
+
const marks = surface.transcript.markers(width).filter((m) => m.kind === 'checkpoint');
|
|
1276
|
+
if (marks.length === 0) {
|
|
1277
|
+
this.notice = 'no restore points in this session yet';
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
const top = surface.viewport.scrollTop;
|
|
1281
|
+
const bottom = top + surface.viewport.height;
|
|
1282
|
+
const onScreen = marks.filter((m) => m.row >= top && m.row < bottom);
|
|
1283
|
+
const above = marks.filter((m) => m.row < top);
|
|
1284
|
+
const mark = onScreen.length ? onScreen[onScreen.length - 1] : (above.pop() || marks[0]);
|
|
1285
|
+
const meta = mark?.meta || {};
|
|
1286
|
+
const where = meta.path ? this.sanitizeSingleLine(shortenHome(meta.path)) : 'files changed this task';
|
|
1287
|
+
const rawStep = meta.step == null ? '' : this.sanitizeSingleLine(meta.step);
|
|
1288
|
+
const step = rawStep ? ` at step ${rawStep}` : '';
|
|
1289
|
+
if (this.editor.isEmpty) {
|
|
1290
|
+
this.editor.setText('/undo');
|
|
1291
|
+
this.notice = `restore point${step} ${GLYPHS.dot} ${where} ${GLYPHS.dot} enter previews, --apply writes`;
|
|
1292
|
+
}
|
|
1293
|
+
else {
|
|
1294
|
+
this.notice = `restore point${step} ${GLYPHS.dot} ${where} ${GLYPHS.dot} run /undo to preview`;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
sanitizeSingleLine(value) {
|
|
1298
|
+
return value == null ? '' : safeDisplay(String(value)).replace(/\s+/g, ' ').trim();
|
|
1299
|
+
}
|
|
1300
|
+
cutSelection() {
|
|
1301
|
+
if (!this.editor.hasSelection)
|
|
1302
|
+
return false;
|
|
1303
|
+
const text = this.editor.getSelectedText();
|
|
1247
1304
|
writeClipboardSync(text);
|
|
1248
1305
|
this.editor.killRing = text;
|
|
1249
|
-
this.
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
} else {
|
|
1253
|
-
this.notice = 'input is empty (nothing to copy)';
|
|
1254
|
-
}
|
|
1255
|
-
this.scheduleDraw();
|
|
1256
|
-
return;
|
|
1257
|
-
}
|
|
1258
|
-
if (key.alt && key.name === 'v') return this.pasteClipboard();
|
|
1259
|
-
if (key.alt && key.name === 'b') return this.editor.moveWordLeft();
|
|
1260
|
-
if (key.alt && key.name === 'f') return this.editor.moveWordRight();
|
|
1261
|
-
}
|
|
1262
|
-
|
|
1263
|
-
/**
|
|
1264
|
-
* Scroll the transcript viewport. Returns true when the key was ours.
|
|
1265
|
-
*
|
|
1266
|
-
* Only bound when a Stage is running: without one there is no viewport, the
|
|
1267
|
-
* terminal's own scrollback is the viewport, and stealing pageup from it
|
|
1268
|
-
* would be worse than useless.
|
|
1269
|
-
*
|
|
1270
|
-
* The choices, and why they are not opencode's:
|
|
1271
|
-
*
|
|
1272
|
-
* * pageup/pagedown move a full viewport MINUS ONE ROW of overlap, which is
|
|
1273
|
-
* what every pager does and what lets the eye carry across the jump.
|
|
1274
|
-
* opencode calls height/2 a "page" and height/4 a "half page" — both are
|
|
1275
|
-
* half of what their names claim, and there is no reason to inherit that.
|
|
1276
|
-
* * ctrl+u / ctrl+d are NOT scroll keys. They are kill-to-start and
|
|
1277
|
-
* delete/exit, tests/chatSurface.test.js asserts it, and a chat where the
|
|
1278
|
-
* key that deletes your line also scrolls is a trap. Half-page is on the
|
|
1279
|
-
* alt-modified pair instead.
|
|
1280
|
-
* * home/end stay with the editor, so top/bottom take the shifted pageup and
|
|
1281
|
-
* pagedown rather than fighting for them.
|
|
1282
|
-
*/
|
|
1283
|
-
handleScrollKey(key) {
|
|
1284
|
-
const viewport = this.surface && this.surface.viewport;
|
|
1285
|
-
|
|
1286
|
-
// A mouse report must be swallowed whether or not there is a viewport to
|
|
1287
|
-
// act on. The bytes are already consumed by the decoder; letting the event
|
|
1288
|
-
// fall through would put `mouse` or `wheel` into the switch below, where a
|
|
1289
|
-
// future case could act on it by accident — and, worse, a terminal that
|
|
1290
|
-
// kept reporting after a failed teardown would type into the user's line.
|
|
1291
|
-
if (key.name === 'mouse') {
|
|
1292
|
-
if (handleTurnRailMouse(key, this.surface, this)) return true;
|
|
1293
|
-
if (this.messageActionLoop && isMouseMotion(key.sequence)) {
|
|
1294
|
-
return handleMessageMouse(this, this.messageActionLoop, key);
|
|
1295
|
-
}
|
|
1296
|
-
if (handleScrollbarMouse(key, viewport, this.surface, this)) return true;
|
|
1297
|
-
if (!this.layer && this.messageActionLoop) return handleMessageMouse(this, this.messageActionLoop, key);
|
|
1298
|
-
return true;
|
|
1299
|
-
}
|
|
1300
|
-
if (key.name === 'wheel') {
|
|
1301
|
-
if (!viewport || !key.wheel) return true;
|
|
1302
|
-
// The reference timeline popover is itself scrollable when a dot
|
|
1303
|
-
// represents more turns than can fit. Wheel input over the card must
|
|
1304
|
-
// not move the chat viewport behind it.
|
|
1305
|
-
const layout = this.surface?.turnRailLayout;
|
|
1306
|
-
const card = layout?.cardRect;
|
|
1307
|
-
const pointerX = key.wheel.x;
|
|
1308
|
-
const pointerY = key.wheel.y;
|
|
1309
|
-
const relativeY = pointerY - (Number.isFinite(layout?.stageTop) ? layout.stageTop : 0);
|
|
1310
|
-
if (card && Array.isArray(card.groupTurns) &&
|
|
1311
|
-
Number.isFinite(pointerX) && Number.isFinite(pointerY) &&
|
|
1312
|
-
pointerX >= card.x && pointerX < card.x + card.width &&
|
|
1313
|
-
relativeY >= card.y && relativeY < card.y + card.height &&
|
|
1314
|
-
(key.wheel.direction === 'up' || key.wheel.direction === 'down')) {
|
|
1315
|
-
const previous = Number.isFinite(this.surface.turnRailCardOffset)
|
|
1316
|
-
? this.surface.turnRailCardOffset : 0;
|
|
1317
|
-
const maximum = Number.isFinite(card.maxOffset) ? Math.max(0, card.maxOffset) : 0;
|
|
1318
|
-
const change = key.wheel.direction === 'up' ? -1 : 1;
|
|
1319
|
-
const current = Math.max(0, Math.min(maximum, previous + change));
|
|
1320
|
-
if (current !== previous) {
|
|
1321
|
-
this.surface.turnRailCardOffset = current;
|
|
1322
|
-
this.scheduleDraw();
|
|
1323
|
-
}
|
|
1306
|
+
this.editor.deleteSelection();
|
|
1307
|
+
this.notice = `cut selection (${text.length} chars) to clipboard`;
|
|
1308
|
+
this.refreshCompletion();
|
|
1324
1309
|
return true;
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
}
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
* part-way through typing.
|
|
1418
|
-
*
|
|
1419
|
-
* `/undo` is preview-by-default and needs `--apply` to write, so even the
|
|
1420
|
-
* accidental path here is a read. That is deliberate: a single keystroke may
|
|
1421
|
-
* not be able to overwrite a day's work.
|
|
1422
|
-
*
|
|
1423
|
-
* WHAT THIS IS NOT: undo for *exactly* that point. `/undo` takes a task, not a
|
|
1424
|
-
* checkpoint id, so there is no way to ask for "the state at this marker"
|
|
1425
|
-
* without changing restore semantics — which is not a terminal-UI change and
|
|
1426
|
-
* is not being smuggled into one. The notice says which step the marker is, so
|
|
1427
|
-
* the choice is at least informed.
|
|
1428
|
-
*/
|
|
1429
|
-
offerUndo() {
|
|
1430
|
-
const surface = this.surface;
|
|
1431
|
-
if (!surface) return;
|
|
1432
|
-
const width = surface.viewport.width;
|
|
1433
|
-
const marks = surface.transcript.markers(width).filter((m) => m.kind === 'checkpoint');
|
|
1434
|
-
if (marks.length === 0) {
|
|
1435
|
-
this.notice = 'no restore points in this session yet';
|
|
1436
|
-
return;
|
|
1437
|
-
}
|
|
1438
|
-
|
|
1439
|
-
const top = surface.viewport.scrollTop;
|
|
1440
|
-
const bottom = top + surface.viewport.height;
|
|
1441
|
-
const onScreen = marks.filter((m) => m.row >= top && m.row < bottom);
|
|
1442
|
-
const above = marks.filter((m) => m.row < top);
|
|
1443
|
-
const mark = onScreen.length ? onScreen[onScreen.length - 1] : (above.pop() || marks[0]);
|
|
1444
|
-
const meta = mark.meta || {};
|
|
1445
|
-
|
|
1446
|
-
const where = meta.path ? this.sanitizeSingleLine(shortenHome(meta.path)) : 'files changed this task';
|
|
1447
|
-
const rawStep = meta.step == null ? '' : this.sanitizeSingleLine(meta.step);
|
|
1448
|
-
const step = rawStep ? ` at step ${rawStep}` : '';
|
|
1449
|
-
|
|
1450
|
-
if (this.editor.isEmpty) {
|
|
1451
|
-
this.editor.setText('/undo');
|
|
1452
|
-
this.notice = `restore point${step} ${GLYPHS.dot} ${where} ${GLYPHS.dot} enter previews, --apply writes`;
|
|
1453
|
-
} else {
|
|
1454
|
-
// Their draft is theirs. Say what is there and leave the line alone.
|
|
1455
|
-
this.notice = `restore point${step} ${GLYPHS.dot} ${where} ${GLYPHS.dot} run /undo to preview`;
|
|
1456
|
-
}
|
|
1457
|
-
}
|
|
1458
|
-
|
|
1459
|
-
sanitizeSingleLine(value) {
|
|
1460
|
-
return value == null ? '' : safeDisplay(String(value)).replace(/\s+/g, ' ').trim();
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
|
-
cutSelection() {
|
|
1464
|
-
if (!this.editor.hasSelection) return false;
|
|
1465
|
-
const text = this.editor.getSelectedText();
|
|
1466
|
-
writeClipboardSync(text);
|
|
1467
|
-
this.editor.killRing = text;
|
|
1468
|
-
this.editor.deleteSelection();
|
|
1469
|
-
this.notice = `cut selection (${text.length} chars) to clipboard`;
|
|
1470
|
-
this.refreshCompletion();
|
|
1471
|
-
return true;
|
|
1472
|
-
}
|
|
1473
|
-
|
|
1474
|
-
handleCtrlC() {
|
|
1475
|
-
handlePromptCtrlC(
|
|
1476
|
-
this,
|
|
1477
|
-
() => [`${paint('warn', GLYPHS.cross)} ${dim('interrupted')}`],
|
|
1478
|
-
CTRL_C_WINDOW_MS
|
|
1479
|
-
);
|
|
1480
|
-
}
|
|
1481
|
-
|
|
1482
|
-
handleEscape() {
|
|
1483
|
-
handlePromptEscape(
|
|
1484
|
-
this,
|
|
1485
|
-
() => [`${paint('warn', GLYPHS.cross)} ${dim('interrupted')}`]
|
|
1486
|
-
);
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
interrupt() {
|
|
1490
|
-
triggerTurnInterrupt(
|
|
1491
|
-
this,
|
|
1492
|
-
() => [`${paint('warn', GLYPHS.cross)} ${dim('interrupted')}`]
|
|
1493
|
-
);
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
/** Show the last tool output in full, above the prompt. */
|
|
1497
|
-
expand() {
|
|
1498
|
-
this.erase();
|
|
1499
|
-
this.onExpand();
|
|
1500
|
-
this.notice = '';
|
|
1501
|
-
}
|
|
1502
|
-
|
|
1503
|
-
clearScreen() {
|
|
1504
|
-
// On the Stage, ctrl+L means what it means in a terminal: the screen goes
|
|
1505
|
-
// blank and your history is still behind you. Clearing the transcript alone
|
|
1506
|
-
// would destroy the session record, so the surface prints it through into
|
|
1507
|
-
// real scrollback first and then starts the band clean. It is also the
|
|
1508
|
-
// escape hatch for getting a transcript out of the band without exiting.
|
|
1509
|
-
if (this.surface) { this.surface.flushAndClear(); return; }
|
|
1510
|
-
|
|
1511
|
-
this.erase();
|
|
1512
|
-
// The one clear Causyne may perform: the user pressed ctrl+L and asked for it.
|
|
1513
|
-
// UX-01 forbids clearing the terminal on the app's own initiative.
|
|
1514
|
-
this.writeControl('\x1b[2J\x1b[H');
|
|
1515
|
-
this.drawnRows = 0;
|
|
1516
|
-
this.cursorRow = 0;
|
|
1517
|
-
}
|
|
1518
|
-
|
|
1519
|
-
historyOrMove(direction) {
|
|
1520
|
-
// Inside a multi-line buffer the arrows move the cursor; history is only
|
|
1521
|
-
// reachable from the edge, which is what every shell does.
|
|
1522
|
-
if (direction === -1 && this.editor.line > 0) return this.editor.moveUp();
|
|
1523
|
-
if (direction === 1 && this.editor.line < this.editor.lines.length - 1) return this.editor.moveDown();
|
|
1524
|
-
|
|
1525
|
-
const text = direction === -1
|
|
1526
|
-
? this.history.previous(this.editor.getText())
|
|
1527
|
-
: this.history.next();
|
|
1528
|
-
if (text === null || text === undefined) return;
|
|
1529
|
-
this.editor.setText(text);
|
|
1530
|
-
}
|
|
1531
|
-
|
|
1532
|
-
// -------------------------------------------------------------------------
|
|
1533
|
-
// Completion
|
|
1534
|
-
// -------------------------------------------------------------------------
|
|
1535
|
-
|
|
1536
|
-
/** A completed scan must refresh the currently displayed @ picker. */
|
|
1537
|
-
bindFileIndex(index) {
|
|
1538
|
-
if (typeof this._unsubscribeFileIndex === 'function') this._unsubscribeFileIndex();
|
|
1539
|
-
this._unsubscribeFileIndex = index.subscribe(() => {
|
|
1540
|
-
// Ignore a late callback from a previous workspace or a dismissed prompt.
|
|
1541
|
-
if (this.index !== index || !this.active) return;
|
|
1542
|
-
const trigger = detectTrigger(this.editor.getText(), this.cursorOffset());
|
|
1543
|
-
if (!trigger || trigger.kind !== 'file') return;
|
|
1544
|
-
this.openCompletion(false);
|
|
1545
|
-
if (index.truncated) this.notice = 'Workspace file index reached its safety limit; narrow the project root.';
|
|
1546
|
-
else if (this.notice === 'Indexing workspace files…') this.notice = '';
|
|
1547
|
-
this.scheduleDraw();
|
|
1548
|
-
});
|
|
1549
|
-
void index.ensure();
|
|
1550
|
-
}
|
|
1551
|
-
|
|
1552
|
-
/** Dynamically re-target the prompt root and optional focused subproject. */
|
|
1553
|
-
setWorkingRoot(newRoot, focusDir = null) {
|
|
1554
|
-
if (typeof this._unsubscribeFileIndex === 'function') this._unsubscribeFileIndex();
|
|
1555
|
-
this.index?.dispose();
|
|
1556
|
-
this.root = newRoot;
|
|
1557
|
-
this.focusDir = focusDir;
|
|
1558
|
-
this.index = new FileIndex(newRoot);
|
|
1559
|
-
this.bindFileIndex(this.index);
|
|
1560
|
-
const trigger = detectTrigger(this.editor.getText(), this.cursorOffset());
|
|
1561
|
-
if (trigger?.kind === 'file') this.openCompletion(false);
|
|
1562
|
-
}
|
|
1563
|
-
|
|
1564
|
-
cursorOffset() {
|
|
1565
|
-
// The editor stores Unicode code points; String.slice() in the completion
|
|
1566
|
-
// provider expects UTF-16 code units. Count units at this API boundary.
|
|
1567
|
-
const before = this.editor.lines.slice(0, this.editor.line)
|
|
1568
|
-
.reduce((total, line) => total + line.join('').length + 1, 0);
|
|
1569
|
-
return before + this.editor.lines[this.editor.line]
|
|
1570
|
-
.slice(0, this.editor.col).join('').length;
|
|
1571
|
-
}
|
|
1572
|
-
|
|
1573
|
-
openCompletion(force = false) {
|
|
1574
|
-
const text = this.editor.getText();
|
|
1575
|
-
const result = suggest(text, this.cursorOffset(), {
|
|
1576
|
-
root: this.root,
|
|
1577
|
-
index: this.index,
|
|
1578
|
-
focusDir: this.focusDir
|
|
1579
|
-
});
|
|
1580
|
-
if (!result) {
|
|
1581
|
-
if (force) this.notice = 'nothing to complete here';
|
|
1582
|
-
// Close a picker whose trigger has gone.
|
|
1583
|
-
this.completion = null;
|
|
1584
|
-
return;
|
|
1585
|
-
}
|
|
1586
|
-
this.completion = result;
|
|
1587
|
-
this.selected = 0;
|
|
1588
|
-
}
|
|
1589
|
-
|
|
1590
|
-
/** Re-run completion after an edit; close it when the trigger is gone. */
|
|
1591
|
-
refreshCompletion() {
|
|
1592
|
-
if (!this.completion) {
|
|
1593
|
-
const text = this.editor.getText();
|
|
1594
|
-
const offset = this.cursorOffset();
|
|
1595
|
-
const opens = Boolean(detectTrigger(text, offset));
|
|
1596
|
-
if (opens) this.openCompletion(false);
|
|
1597
|
-
return;
|
|
1598
|
-
}
|
|
1599
|
-
this.openCompletion(false);
|
|
1600
|
-
if (this.completion) this.selected = Math.min(this.selected, this.completion.items.length - 1);
|
|
1601
|
-
}
|
|
1602
|
-
|
|
1603
|
-
/**
|
|
1604
|
-
* Is the only suggestion the thing that is already typed?
|
|
1605
|
-
*
|
|
1606
|
-
* `/skill mine` offers exactly one completion — `mine` — which is what the
|
|
1607
|
-
* user has already written. Accepting it changes nothing, refreshCompletion()
|
|
1608
|
-
* immediately reopens the picker on the same text, and Enter becomes a key
|
|
1609
|
-
* that can never submit. The command was unreachable.
|
|
1610
|
-
*
|
|
1611
|
-
* openclaw solves this with shouldSubmitExactArgumentCompletion
|
|
1612
|
-
* (src/tui/commands.ts): when the sole completion equals the argument text,
|
|
1613
|
-
* submit instead of completing. Same rule here, extended to a whole command
|
|
1614
|
-
* so `/undo` submits on the first Enter rather than the second.
|
|
1615
|
-
*
|
|
1616
|
-
* Deliberately NOT applied to file completion: there, accepting a directory
|
|
1617
|
-
* that matches exactly is how you drill into it.
|
|
1618
|
-
*/
|
|
1619
|
-
completionIsExact() {
|
|
1620
|
-
if (!this.completion || this.completion.items.length !== 1) return false;
|
|
1621
|
-
const [item] = this.completion.items;
|
|
1622
|
-
if (this.completion.kind === 'command-argument') return item.value === this.completion.query;
|
|
1623
|
-
if (this.completion.kind === 'command') return item.value === this.editor.getText();
|
|
1624
|
-
return false;
|
|
1625
|
-
}
|
|
1626
|
-
|
|
1627
|
-
handleCompletionKey(key) {
|
|
1628
|
-
if (key.name === 'up') { this.moveSelection(-1); return true; }
|
|
1629
|
-
if (key.name === 'down') { this.moveSelection(1); return true; }
|
|
1630
|
-
// The wheel, PgUp and PgDn belong to the open picker. Letting them fall
|
|
1631
|
-
// through scrolled the transcript behind a frozen menu.
|
|
1632
|
-
if (key.name === 'wheel') {
|
|
1633
|
-
const direction = key.wheel && key.wheel.direction;
|
|
1634
|
-
if (direction === 'up') this.moveSelectionClamped(-1);
|
|
1635
|
-
else if (direction === 'down') this.moveSelectionClamped(1);
|
|
1636
|
-
this.scheduleDraw();
|
|
1637
|
-
return true;
|
|
1638
|
-
}
|
|
1639
|
-
if (key.name === 'pageup' && !key.shift) {
|
|
1640
|
-
this.moveSelectionClamped(-MAX_VISIBLE);
|
|
1641
|
-
this.scheduleDraw();
|
|
1642
|
-
return true;
|
|
1643
|
-
}
|
|
1644
|
-
if (key.name === 'pagedown' && !key.shift) {
|
|
1645
|
-
this.moveSelectionClamped(MAX_VISIBLE);
|
|
1646
|
-
this.scheduleDraw();
|
|
1647
|
-
return true;
|
|
1648
|
-
}
|
|
1649
|
-
if (key.name === 'mouse') {
|
|
1650
|
-
if (key.mouse && key.mouse.pressed && key.mouse.button === 0 && !isMouseMotion(key.sequence)) {
|
|
1651
|
-
let item = null;
|
|
1652
|
-
const hasZeroY = Number.isFinite(key.mouse.y);
|
|
1653
|
-
const rawMouseRow = hasZeroY
|
|
1654
|
-
? Math.floor(key.mouse.y)
|
|
1655
|
-
: (Number.isFinite(key.mouse.row) ? Math.floor(key.mouse.row) : NaN);
|
|
1656
|
-
if (this.pickerLayout && Number.isFinite(rawMouseRow)) {
|
|
1657
|
-
const stageHeight = this.surface?.stage?.height || this.out?.rows || process.stdout.rows || 24;
|
|
1658
|
-
const termRows = Number.isFinite(this.out?.rows) && this.out.rows > 0
|
|
1659
|
-
? Math.floor(this.out.rows)
|
|
1660
|
-
: stageHeight;
|
|
1661
|
-
// KeyDecoder emits 0-based x/y screen coordinates; legacy unit tests pass 1-based row/col.
|
|
1662
|
-
const promptTop = hasZeroY
|
|
1663
|
-
? Math.max(0, termRows - this.pickerLayout.totalPromptRows)
|
|
1664
|
-
: Math.max(1, stageHeight - this.pickerLayout.totalPromptRows + 1);
|
|
1665
|
-
const pickerTop = promptTop + this.pickerLayout.pickerStart;
|
|
1666
|
-
const pickerBottom = pickerTop + this.pickerLayout.pickerCount - 1;
|
|
1667
|
-
const mouseX = Number.isFinite(key.mouse.x) ? Math.floor(key.mouse.x) : NaN;
|
|
1668
|
-
const mainW = this.surface
|
|
1669
|
-
? sessionMainWidth(this.surface.width, Boolean(this.surface.homeInfo), this.surface.env)
|
|
1670
|
-
: Infinity;
|
|
1671
|
-
const inMainColumn = !Number.isFinite(mouseX) || (mouseX >= 0 && mouseX < mainW);
|
|
1672
|
-
|
|
1673
|
-
if (inMainColumn && rawMouseRow >= pickerTop && rawMouseRow <= pickerBottom) {
|
|
1674
|
-
// The picker renders a scrolled window (renderPicker); a click maps
|
|
1675
|
-
// to the item under the cursor, not to the first page of items.
|
|
1676
|
-
const items = Array.isArray(this.completion.items) ? this.completion.items : [];
|
|
1677
|
-
const total = items.length;
|
|
1678
|
-
const windowStart = Math.max(0, Math.min(this.selected - MAX_VISIBLE + 1, total - MAX_VISIBLE));
|
|
1679
|
-
const index = windowStart + (rawMouseRow - pickerTop);
|
|
1680
|
-
if (index >= 0 && index < total) {
|
|
1681
|
-
this.selected = index;
|
|
1682
|
-
item = items[index];
|
|
1683
|
-
}
|
|
1684
|
-
} else {
|
|
1685
|
-
// Click outside picker closes it
|
|
1310
|
+
}
|
|
1311
|
+
handleCtrlC() {
|
|
1312
|
+
handlePromptCtrlC(this, () => [`${paint('warn', GLYPHS.cross)} ${dim('interrupted')}`], CTRL_C_WINDOW_MS);
|
|
1313
|
+
}
|
|
1314
|
+
handleEscape() {
|
|
1315
|
+
handlePromptEscape(this, () => [`${paint('warn', GLYPHS.cross)} ${dim('interrupted')}`]);
|
|
1316
|
+
}
|
|
1317
|
+
interrupt() {
|
|
1318
|
+
triggerTurnInterrupt(this, () => [`${paint('warn', GLYPHS.cross)} ${dim('interrupted')}`]);
|
|
1319
|
+
}
|
|
1320
|
+
expand() {
|
|
1321
|
+
this.erase();
|
|
1322
|
+
this.onExpand();
|
|
1323
|
+
this.notice = '';
|
|
1324
|
+
}
|
|
1325
|
+
clearScreen() {
|
|
1326
|
+
if (this.surface) {
|
|
1327
|
+
this.surface.flushAndClear();
|
|
1328
|
+
return;
|
|
1329
|
+
}
|
|
1330
|
+
this.erase();
|
|
1331
|
+
// The one clear Causyne may perform: the user pressed ctrl+L and asked for it.
|
|
1332
|
+
// UX-01 forbids clearing the terminal on the app's own initiative.
|
|
1333
|
+
this.writeControl('\x1b[2J\x1b[H');
|
|
1334
|
+
this.drawnRows = 0;
|
|
1335
|
+
this.cursorRow = 0;
|
|
1336
|
+
}
|
|
1337
|
+
historyOrMove(direction) {
|
|
1338
|
+
if (direction === -1 && this.editor.line > 0) {
|
|
1339
|
+
this.editor.moveUp();
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
if (direction === 1 && this.editor.line < this.editor.lines.length - 1) {
|
|
1343
|
+
this.editor.moveDown();
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
const text = direction === -1
|
|
1347
|
+
? this.history.previous(this.editor.getText())
|
|
1348
|
+
: this.history.next();
|
|
1349
|
+
if (text === null || text === undefined)
|
|
1350
|
+
return;
|
|
1351
|
+
this.editor.setText(text);
|
|
1352
|
+
}
|
|
1353
|
+
// -------------------------------------------------------------------------
|
|
1354
|
+
// Completion
|
|
1355
|
+
// -------------------------------------------------------------------------
|
|
1356
|
+
bindFileIndex(index) {
|
|
1357
|
+
if (typeof this._unsubscribeFileIndex === 'function')
|
|
1358
|
+
this._unsubscribeFileIndex();
|
|
1359
|
+
this._unsubscribeFileIndex = index.subscribe(() => {
|
|
1360
|
+
if (this.index !== index || !this.active)
|
|
1361
|
+
return;
|
|
1362
|
+
const trigger = detectTrigger(this.editor.getText(), this.cursorOffset());
|
|
1363
|
+
if (!trigger || trigger.kind !== 'file')
|
|
1364
|
+
return;
|
|
1365
|
+
this.openCompletion(false);
|
|
1366
|
+
if (index.truncated)
|
|
1367
|
+
this.notice = 'Workspace file index reached its safety limit; narrow the project root.';
|
|
1368
|
+
else if (this.notice === 'Indexing workspace files…')
|
|
1369
|
+
this.notice = '';
|
|
1370
|
+
this.scheduleDraw();
|
|
1371
|
+
});
|
|
1372
|
+
void index.ensure();
|
|
1373
|
+
}
|
|
1374
|
+
setWorkingRoot(newRoot, focusDir = null) {
|
|
1375
|
+
if (typeof this._unsubscribeFileIndex === 'function')
|
|
1376
|
+
this._unsubscribeFileIndex();
|
|
1377
|
+
this.index?.dispose();
|
|
1378
|
+
this.root = newRoot;
|
|
1379
|
+
this.focusDir = focusDir;
|
|
1380
|
+
this.index = new FileIndex(newRoot);
|
|
1381
|
+
this.bindFileIndex(this.index);
|
|
1382
|
+
const trigger = detectTrigger(this.editor.getText(), this.cursorOffset());
|
|
1383
|
+
if (trigger?.kind === 'file')
|
|
1384
|
+
this.openCompletion(false);
|
|
1385
|
+
}
|
|
1386
|
+
cursorOffset() {
|
|
1387
|
+
const before = this.editor.lines.slice(0, this.editor.line)
|
|
1388
|
+
.reduce((total, line) => total + line.join('').length + 1, 0);
|
|
1389
|
+
return before + (this.editor.lines[this.editor.line] ?? [])
|
|
1390
|
+
.slice(0, this.editor.col).join('').length;
|
|
1391
|
+
}
|
|
1392
|
+
openCompletion(force = false) {
|
|
1393
|
+
const text = this.editor.getText();
|
|
1394
|
+
const result = suggest(text, this.cursorOffset(), {
|
|
1395
|
+
root: this.root,
|
|
1396
|
+
index: this.index,
|
|
1397
|
+
focusDir: this.focusDir
|
|
1398
|
+
});
|
|
1399
|
+
if (!result) {
|
|
1400
|
+
if (force)
|
|
1401
|
+
this.notice = 'nothing to complete here';
|
|
1686
1402
|
this.completion = null;
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1405
|
+
this.completion = result;
|
|
1406
|
+
this.selected = 0;
|
|
1407
|
+
}
|
|
1408
|
+
refreshCompletion() {
|
|
1409
|
+
if (!this.completion) {
|
|
1410
|
+
const text = this.editor.getText();
|
|
1411
|
+
const offset = this.cursorOffset();
|
|
1412
|
+
const opens = Boolean(detectTrigger(text, offset));
|
|
1413
|
+
if (opens)
|
|
1414
|
+
this.openCompletion(false);
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1417
|
+
this.openCompletion(false);
|
|
1418
|
+
if (this.completion)
|
|
1419
|
+
this.selected = Math.min(this.selected, this.completion.items.length - 1);
|
|
1420
|
+
}
|
|
1421
|
+
completionIsExact() {
|
|
1422
|
+
if (!this.completion || this.completion.items.length !== 1)
|
|
1423
|
+
return false;
|
|
1424
|
+
const item = this.completion.items[0];
|
|
1425
|
+
if (!item)
|
|
1426
|
+
return false;
|
|
1427
|
+
if (this.completion.kind === 'command-argument')
|
|
1428
|
+
return item.value === this.completion.query;
|
|
1429
|
+
if (this.completion.kind === 'command')
|
|
1430
|
+
return item.value === this.editor.getText();
|
|
1431
|
+
return false;
|
|
1432
|
+
}
|
|
1433
|
+
handleCompletionKey(key) {
|
|
1434
|
+
if (!this.completion)
|
|
1435
|
+
return false;
|
|
1436
|
+
if (key.name === 'up') {
|
|
1437
|
+
this.moveSelection(-1);
|
|
1438
|
+
return true;
|
|
1439
|
+
}
|
|
1440
|
+
if (key.name === 'down') {
|
|
1441
|
+
this.moveSelection(1);
|
|
1442
|
+
return true;
|
|
1443
|
+
}
|
|
1444
|
+
if (key.name === 'wheel') {
|
|
1445
|
+
const direction = key.wheel && key.wheel.direction;
|
|
1446
|
+
if (direction === 'up')
|
|
1447
|
+
this.moveSelectionClamped(-1);
|
|
1448
|
+
else if (direction === 'down')
|
|
1449
|
+
this.moveSelectionClamped(1);
|
|
1687
1450
|
this.scheduleDraw();
|
|
1688
1451
|
return true;
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
}
|
|
1694
|
-
|
|
1695
|
-
if (item) {
|
|
1696
|
-
if (this.completion.inline && (this.completion.kind === 'command' || this.completion.kind === 'command-argument')) {
|
|
1697
|
-
const selectedCommand = this.completion.kind === 'command'
|
|
1698
|
-
? item.value : `/${this.completion.command} ${item.value}`;
|
|
1699
|
-
this.runInlineSlashSelection(selectedCommand);
|
|
1452
|
+
}
|
|
1453
|
+
if (key.name === 'pageup' && !key.shift) {
|
|
1454
|
+
this.moveSelectionClamped(-MAX_VISIBLE);
|
|
1455
|
+
this.scheduleDraw();
|
|
1700
1456
|
return true;
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1457
|
+
}
|
|
1458
|
+
if (key.name === 'pagedown' && !key.shift) {
|
|
1459
|
+
this.moveSelectionClamped(MAX_VISIBLE);
|
|
1460
|
+
this.scheduleDraw();
|
|
1461
|
+
return true;
|
|
1462
|
+
}
|
|
1463
|
+
if (key.name === 'mouse') {
|
|
1464
|
+
if (key.mouse && key.mouse.pressed && key.mouse.button === 0 && !isMouseMotion(key.sequence)) {
|
|
1465
|
+
let item = null;
|
|
1466
|
+
const hasZeroY = Number.isFinite(key.mouse.y);
|
|
1467
|
+
const rawMouseRow = hasZeroY
|
|
1468
|
+
? Math.floor(key.mouse.y)
|
|
1469
|
+
: (Number.isFinite(key.mouse.row) ? Math.floor(key.mouse.row) : Number.NaN);
|
|
1470
|
+
if (this.pickerLayout && Number.isFinite(rawMouseRow)) {
|
|
1471
|
+
const stageHeight = this.surface?.stage?.height || this.out?.rows || process.stdout.rows || 24;
|
|
1472
|
+
const termRows = Number.isFinite(this.out?.rows) && this.out.rows > 0
|
|
1473
|
+
? Math.floor(this.out.rows)
|
|
1474
|
+
: stageHeight;
|
|
1475
|
+
const promptTop = hasZeroY
|
|
1476
|
+
? Math.max(0, termRows - this.pickerLayout.totalPromptRows)
|
|
1477
|
+
: Math.max(1, stageHeight - this.pickerLayout.totalPromptRows + 1);
|
|
1478
|
+
const pickerTop = promptTop + this.pickerLayout.pickerStart;
|
|
1479
|
+
const pickerBottom = pickerTop + this.pickerLayout.pickerCount - 1;
|
|
1480
|
+
const mouseX = Number.isFinite(key.mouse.x) ? Math.floor(key.mouse.x) : Number.NaN;
|
|
1481
|
+
const mainW = this.surface
|
|
1482
|
+
? sessionMainWidth(this.surface.width, Boolean(this.surface.homeInfo), this.surface.env)
|
|
1483
|
+
: Infinity;
|
|
1484
|
+
const inMainColumn = !Number.isFinite(mouseX) || (mouseX >= 0 && mouseX < mainW);
|
|
1485
|
+
if (inMainColumn && rawMouseRow >= pickerTop && rawMouseRow <= pickerBottom) {
|
|
1486
|
+
const items = Array.isArray(this.completion.items) ? this.completion.items : [];
|
|
1487
|
+
const total = items.length;
|
|
1488
|
+
const windowStart = Math.max(0, Math.min(this.selected - MAX_VISIBLE + 1, total - MAX_VISIBLE));
|
|
1489
|
+
const index = windowStart + (rawMouseRow - pickerTop);
|
|
1490
|
+
if (index >= 0 && index < total) {
|
|
1491
|
+
this.selected = index;
|
|
1492
|
+
item = items[index];
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
else {
|
|
1496
|
+
this.completion = null;
|
|
1497
|
+
this.scheduleDraw();
|
|
1498
|
+
return true;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
else {
|
|
1502
|
+
const items = Array.isArray(this.completion.items) ? this.completion.items : [];
|
|
1503
|
+
item = items[this.selected];
|
|
1504
|
+
}
|
|
1505
|
+
if (item) {
|
|
1506
|
+
if (this.completion.kind !== 'file' && this.completion.inline) {
|
|
1507
|
+
const selectedCommand = this.completion.kind === 'command'
|
|
1508
|
+
? item.value : `/${this.completion.command} ${item.value}`;
|
|
1509
|
+
this.runInlineSlashSelection(selectedCommand);
|
|
1510
|
+
return true;
|
|
1511
|
+
}
|
|
1512
|
+
if (this.completion.kind === 'command') {
|
|
1513
|
+
if (typeof item.value === 'string' && (item.value.startsWith('/skills ') || item.value.startsWith('/skill '))) {
|
|
1514
|
+
this.acceptCompletion();
|
|
1515
|
+
return true;
|
|
1516
|
+
}
|
|
1517
|
+
this.submitPickedCommand(item.value);
|
|
1518
|
+
return true;
|
|
1519
|
+
}
|
|
1520
|
+
if (this.completion.kind === 'command-argument') {
|
|
1521
|
+
this.submitPickedCommand(`/${this.completion.command} ${item.value}`);
|
|
1522
|
+
return true;
|
|
1523
|
+
}
|
|
1524
|
+
this.acceptCompletion();
|
|
1525
|
+
return true;
|
|
1526
|
+
}
|
|
1708
1527
|
}
|
|
1709
|
-
this.submitPickedCommand(item.value);
|
|
1710
1528
|
return true;
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
this.
|
|
1529
|
+
}
|
|
1530
|
+
if (key.name === 'enter' && !key.alt && !key.shift) {
|
|
1531
|
+
const item = this.completion.items && this.completion.items[this.selected];
|
|
1532
|
+
if (item && this.completion.kind !== 'file' && this.completion.inline) {
|
|
1533
|
+
const selectedCommand = this.completion.kind === 'command'
|
|
1534
|
+
? item.value : `/${this.completion.command} ${item.value}`;
|
|
1535
|
+
this.runInlineSlashSelection(selectedCommand);
|
|
1536
|
+
return true;
|
|
1537
|
+
}
|
|
1538
|
+
if (this.completion.kind === 'command' && item) {
|
|
1539
|
+
if (typeof item.value === 'string' && (item.value.startsWith('/skills ') || item.value.startsWith('/skill '))) {
|
|
1540
|
+
this.acceptCompletion();
|
|
1541
|
+
return true;
|
|
1542
|
+
}
|
|
1543
|
+
this.submitPickedCommand(item.value);
|
|
1544
|
+
return true;
|
|
1545
|
+
}
|
|
1546
|
+
if (this.completionIsExact()) {
|
|
1547
|
+
this.completion = null;
|
|
1548
|
+
return false;
|
|
1549
|
+
}
|
|
1550
|
+
this.acceptCompletion();
|
|
1714
1551
|
return true;
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
}
|
|
1735
|
-
// Instant execution on single Enter for any pure slash command
|
|
1736
|
-
this.submitPickedCommand(item.value);
|
|
1737
|
-
return true;
|
|
1738
|
-
}
|
|
1739
|
-
if (this.completionIsExact()) {
|
|
1740
|
-
// Nothing left to complete. Close the picker and let the key through to
|
|
1741
|
-
// submit, rather than re-offering what is already on the line.
|
|
1742
|
-
this.completion = null;
|
|
1552
|
+
}
|
|
1553
|
+
if (key.name === 'tab') {
|
|
1554
|
+
if (this.completion.kind !== 'file' && this.completion.inline) {
|
|
1555
|
+
this.notice = 'Press Enter to run the selected command; your unsent draft is preserved.';
|
|
1556
|
+
this.scheduleDraw();
|
|
1557
|
+
return true;
|
|
1558
|
+
}
|
|
1559
|
+
this.acceptCompletion();
|
|
1560
|
+
return true;
|
|
1561
|
+
}
|
|
1562
|
+
if (key.name === 'escape') {
|
|
1563
|
+
const dismissed = this.completion;
|
|
1564
|
+
this.completion = null;
|
|
1565
|
+
if (!(dismissed.kind !== 'file' && dismissed.inline) && /^\s*\/[a-z0-9_?-]*$/i.test(this.editor.getText())) {
|
|
1566
|
+
this.editor.clear();
|
|
1567
|
+
}
|
|
1568
|
+
this.scheduleDraw();
|
|
1569
|
+
return true;
|
|
1570
|
+
}
|
|
1743
1571
|
return false;
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
this.
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
}
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
* East Asian Ambiguous (see text.js), so the measurement can legitimately
|
|
1912
|
-
* disagree with the terminal by a column. Cutting to width - 1 makes a wrap
|
|
1913
|
-
* impossible whatever the measurement says, and leaves the last column free so
|
|
1914
|
-
* a row that lands exactly on the edge cannot trip the terminal's deferred-wrap
|
|
1915
|
-
* state either.
|
|
1916
|
-
*
|
|
1917
|
-
* truncate() is escape-aware and closes an open SGR at the cut, so a clamped
|
|
1918
|
-
* row cannot leak colour into the row below.
|
|
1919
|
-
*/
|
|
1920
|
-
clampRows(rows, width) {
|
|
1921
|
-
const limit = Math.max(1, width - 1);
|
|
1922
|
-
return rows.map((row) => (visibleWidth(row) <= limit ? row : truncate(row, limit, '')));
|
|
1923
|
-
}
|
|
1924
|
-
|
|
1925
|
-
/** Remove everything this prompt drew, leaving the cursor where it started. */
|
|
1926
|
-
erase() {
|
|
1927
|
-
// Nothing of ours is on the terminal in stage mode — the band is painted by
|
|
1928
|
-
// a diff and the previous frame is a row array, not screen state. Walking
|
|
1929
|
-
// up and clearing here would erase rows the Stage owns and then paint over
|
|
1930
|
-
// them, which is the collision this method exists to prevent.
|
|
1931
|
-
if (this.surface) return;
|
|
1932
|
-
if (this.drawnRows === 0) {
|
|
1933
|
-
// Nothing of OURS is on screen — but the caret may still be sitting at a
|
|
1934
|
-
// non-zero column, because whoever wrote last left it there. Writing the
|
|
1935
|
-
// block from there puts its first row at that offset, which is how a
|
|
1936
|
-
// second `▏` appeared beside the first on the same row. Returning to
|
|
1937
|
-
// column 0 costs one byte and removes the whole class.
|
|
1938
|
-
this.out.write('\r');
|
|
1939
|
-
return;
|
|
1940
|
-
}
|
|
1941
|
-
if (this.cursorRow > 0) this.out.write(`\x1b[${this.cursorRow}A`);
|
|
1942
|
-
this.out.write('\r\x1b[J');
|
|
1943
|
-
this.drawnRows = 0;
|
|
1944
|
-
this.cursorRow = 0;
|
|
1945
|
-
}
|
|
1946
|
-
|
|
1947
|
-
draw() {
|
|
1948
|
-
if (this.resizeTimer) return;
|
|
1949
|
-
if (!this.active) return;
|
|
1950
|
-
this.erase();
|
|
1951
|
-
|
|
1952
|
-
const width = this.width();
|
|
1953
|
-
if (this.layer) return this.drawLayer(width);
|
|
1954
|
-
|
|
1955
|
-
// The input is a BLOCK, not a line.
|
|
1956
|
-
//
|
|
1957
|
-
// It used to be a bare `▸ ` prefix, so the place the user types looked like
|
|
1958
|
-
// one more row of shell output among all the others. opencode gives its
|
|
1959
|
-
// prompt a left border in the agent's colour and a filled surface
|
|
1960
|
-
// (component/prompt/index.tsx:1353), which is what makes the input read as a
|
|
1961
|
-
// surface you are writing INTO. Every row of the block is inset by the same
|
|
1962
|
-
// gutter, so the caret column is a constant rather than a special case for
|
|
1963
|
-
// row 0 — which is also one fewer thing for erase() to get wrong.
|
|
1964
|
-
// Reserve one editor cell for the caret. The left bar and gutter are
|
|
1965
|
-
// consumed by barBlock; the remaining full-width body is not all editable.
|
|
1966
|
-
// This prevents a full sentence from placing the cursor beyond the fill.
|
|
1967
|
-
const { boxWidth, inset, editorWidth } = composerGeometry(
|
|
1968
|
-
width, visibleWidth(GLYPHS.barHeavy || GLYPHS.bar), GUTTER
|
|
1969
|
-
);
|
|
1970
|
-
const inner = editorWidth;
|
|
1971
|
-
const layout = this.editor.render(editorWidth, '');
|
|
1972
|
-
|
|
1973
|
-
// Scroll the editor internally rather than growing without bound: keep a
|
|
1974
|
-
// window of MAX_PROMPT_ROWS that always contains the caret.
|
|
1975
|
-
const start = Math.max(0, Math.min(layout.cursorRow - MAX_PROMPT_ROWS + 1,
|
|
1976
|
-
layout.rows.length - MAX_PROMPT_ROWS));
|
|
1977
|
-
const top = Math.max(0, start);
|
|
1978
|
-
const window = layout.rows.slice(top, top + MAX_PROMPT_ROWS);
|
|
1979
|
-
const caretRow = layout.cursorRow - top;
|
|
1980
|
-
let body = window.slice();
|
|
1981
|
-
|
|
1982
|
-
if (!this.editor.isEmpty) {
|
|
1983
|
-
body = body.map((line) => {
|
|
1984
|
-
if (typeof line !== 'string' || !line.includes('[Image ')) return line;
|
|
1985
|
-
return line.replace(/\[Image (\d+)\]/g, (_m, num) => {
|
|
1986
|
-
return paint('warn', bold(`[Image ${num}]`));
|
|
1572
|
+
}
|
|
1573
|
+
moveSelection(direction) {
|
|
1574
|
+
const count = Array.isArray(this.completion?.items) ? this.completion.items.length : 0;
|
|
1575
|
+
if (count === 0)
|
|
1576
|
+
return;
|
|
1577
|
+
this.selected = (((this.selected + direction) % count) + count) % count;
|
|
1578
|
+
}
|
|
1579
|
+
moveSelectionClamped(delta) {
|
|
1580
|
+
const count = Array.isArray(this.completion?.items) ? this.completion.items.length : 0;
|
|
1581
|
+
if (count === 0)
|
|
1582
|
+
return;
|
|
1583
|
+
this.selected = Math.max(0, Math.min(count - 1, this.selected + delta));
|
|
1584
|
+
}
|
|
1585
|
+
submitPickedCommand(value) {
|
|
1586
|
+
this.completion = null;
|
|
1587
|
+
this.editor.setText(value);
|
|
1588
|
+
this.submit();
|
|
1589
|
+
}
|
|
1590
|
+
runInlineSlashSelection(value) {
|
|
1591
|
+
const trigger = this.completion;
|
|
1592
|
+
if (!trigger || trigger.kind === 'file' || !trigger.inline) {
|
|
1593
|
+
this.submitPickedCommand(value);
|
|
1594
|
+
return;
|
|
1595
|
+
}
|
|
1596
|
+
const currentText = this.editor.getText();
|
|
1597
|
+
const currentCursor = this.cursorOffset();
|
|
1598
|
+
let remaining;
|
|
1599
|
+
try {
|
|
1600
|
+
remaining = removeInlineSlashToken(currentText, currentCursor, trigger);
|
|
1601
|
+
}
|
|
1602
|
+
catch {
|
|
1603
|
+
this.notice = 'The command text changed. Reopen the / menu and select again.';
|
|
1604
|
+
this.completion = null;
|
|
1605
|
+
this.scheduleDraw();
|
|
1606
|
+
return;
|
|
1607
|
+
}
|
|
1608
|
+
const consumeDraft = isInlineSkillInvocation(value) && remaining.text.trim().length > 0;
|
|
1609
|
+
const commandText = consumeDraft ? `${value} ${remaining.text.trim()}` : value;
|
|
1610
|
+
if (this.busy && this.queue.length >= 32 && !/^\/(?:stop|interrupt)$/i.test(commandText)) {
|
|
1611
|
+
this.notice = '32 queued messages: command not submitted; your draft is unchanged.';
|
|
1612
|
+
this.scheduleDraw();
|
|
1613
|
+
return;
|
|
1614
|
+
}
|
|
1615
|
+
this.completion = null;
|
|
1616
|
+
try {
|
|
1617
|
+
this.editor.setText(commandText);
|
|
1618
|
+
this.submit();
|
|
1619
|
+
}
|
|
1620
|
+
finally {
|
|
1621
|
+
this.editor.setText(consumeDraft ? '' : remaining.text);
|
|
1622
|
+
if (!consumeDraft)
|
|
1623
|
+
this.setCursorOffset(remaining.cursor);
|
|
1624
|
+
this.completion = null;
|
|
1625
|
+
this.scheduleDraw();
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
acceptCompletion() {
|
|
1629
|
+
if (!this.completion)
|
|
1630
|
+
return;
|
|
1631
|
+
const item = this.completion.items[this.selected];
|
|
1632
|
+
if (!item) {
|
|
1633
|
+
this.completion = null;
|
|
1634
|
+
return;
|
|
1635
|
+
}
|
|
1636
|
+
const applied = applyCompletion(this.editor.getText(), this.cursorOffset(), this.completion, item);
|
|
1637
|
+
this.editor.setText(applied.text);
|
|
1638
|
+
this.setCursorOffset(applied.cursor);
|
|
1639
|
+
this.completion = null;
|
|
1640
|
+
this.refreshCompletion();
|
|
1641
|
+
}
|
|
1642
|
+
setCursorOffset(offset) {
|
|
1643
|
+
let remaining = Math.max(0, Number.isFinite(offset) ? offset : 0);
|
|
1644
|
+
for (let index = 0; index < this.editor.lines.length; index++) {
|
|
1645
|
+
const line = this.editor.lines[index] ?? [];
|
|
1646
|
+
const units = line.join('').length;
|
|
1647
|
+
if (remaining <= units || index === this.editor.lines.length - 1) {
|
|
1648
|
+
let column = 0;
|
|
1649
|
+
let consumed = 0;
|
|
1650
|
+
while (column < line.length && consumed + (line[column]?.length ?? 0) <= remaining) {
|
|
1651
|
+
consumed += line[column]?.length ?? 0;
|
|
1652
|
+
column++;
|
|
1653
|
+
}
|
|
1654
|
+
this.editor.line = index;
|
|
1655
|
+
this.editor.col = column;
|
|
1656
|
+
return;
|
|
1657
|
+
}
|
|
1658
|
+
remaining -= units + 1;
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
// -------------------------------------------------------------------------
|
|
1662
|
+
// Drawing
|
|
1663
|
+
// -------------------------------------------------------------------------
|
|
1664
|
+
queuedPreviewRows(width) {
|
|
1665
|
+
if (this.queue.length === 0)
|
|
1666
|
+
return [];
|
|
1667
|
+
const last = this.queue[this.queue.length - 1];
|
|
1668
|
+
const text = typeof last === 'string' ? last
|
|
1669
|
+
: last && typeof last.text === 'string' ? last.text : 'pending action';
|
|
1670
|
+
const prefix = ` Queued (${this.queue.length}): `;
|
|
1671
|
+
const available = Math.max(0, width - visibleWidth(prefix) - 2);
|
|
1672
|
+
const preview = safeDisplay(text.slice(0, 160).replace(/\s+/g, ' ').trim());
|
|
1673
|
+
return [dim(prefix + truncate(preview, available, GLYPHS.ellipsis))];
|
|
1674
|
+
}
|
|
1675
|
+
clampRows(rows, width) {
|
|
1676
|
+
const limit = Math.max(1, width - 1);
|
|
1677
|
+
return rows.map((row) => (visibleWidth(row) <= limit ? row : truncate(row, limit, '')));
|
|
1678
|
+
}
|
|
1679
|
+
erase() {
|
|
1680
|
+
if (this.surface)
|
|
1681
|
+
return;
|
|
1682
|
+
if (this.drawnRows === 0) {
|
|
1683
|
+
this.out.write('\r');
|
|
1684
|
+
return;
|
|
1685
|
+
}
|
|
1686
|
+
if (this.cursorRow > 0)
|
|
1687
|
+
this.out.write(`\x1b[${this.cursorRow}A`);
|
|
1688
|
+
this.out.write('\r\x1b[J');
|
|
1689
|
+
this.drawnRows = 0;
|
|
1690
|
+
this.cursorRow = 0;
|
|
1691
|
+
}
|
|
1692
|
+
draw() {
|
|
1693
|
+
if (this.resizeTimer)
|
|
1694
|
+
return;
|
|
1695
|
+
if (!this.active)
|
|
1696
|
+
return;
|
|
1697
|
+
this.erase();
|
|
1698
|
+
const width = this.width();
|
|
1699
|
+
if (this.layer) {
|
|
1700
|
+
this.drawLayer(width);
|
|
1701
|
+
return;
|
|
1702
|
+
}
|
|
1703
|
+
const { boxWidth, inset, editorWidth } = composerGeometry(width, visibleWidth(GLYPHS.barHeavy || GLYPHS.bar), GUTTER);
|
|
1704
|
+
const inner = editorWidth;
|
|
1705
|
+
const layout = this.editor.render(editorWidth, '');
|
|
1706
|
+
const start = Math.max(0, Math.min(layout.cursorRow - MAX_PROMPT_ROWS + 1, layout.rows.length - MAX_PROMPT_ROWS));
|
|
1707
|
+
const top = Math.max(0, start);
|
|
1708
|
+
const windowRows = layout.rows.slice(top, top + MAX_PROMPT_ROWS);
|
|
1709
|
+
const caretRow = layout.cursorRow - top;
|
|
1710
|
+
let body = windowRows.slice();
|
|
1711
|
+
if (!this.editor.isEmpty) {
|
|
1712
|
+
body = body.map((line) => {
|
|
1713
|
+
if (typeof line !== 'string' || !line.includes('[Image '))
|
|
1714
|
+
return line;
|
|
1715
|
+
return line.replace(/\[Image (\d+)\]/g, (_m, num) => {
|
|
1716
|
+
return paint('warn', bold(`[Image ${num}]`));
|
|
1717
|
+
});
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
if (this.editor.isEmpty && !this.completion) {
|
|
1721
|
+
const idle = fitHints([`try "${this.example}"`, '/ commands', '@ files', '^V image'], Math.max(8, inner), 'ask anything');
|
|
1722
|
+
body = [dim(this.busy ? 'type to queue a message…' : idle)];
|
|
1723
|
+
}
|
|
1724
|
+
const agent = this.completion ? null : this.agentRow(inner);
|
|
1725
|
+
if (agent)
|
|
1726
|
+
body = body.concat(['', agent]);
|
|
1727
|
+
const rows = Number(this.out && this.out.rows);
|
|
1728
|
+
const roomy = !Number.isFinite(rows) || rows >= ROOMY_TERMINAL_ROWS;
|
|
1729
|
+
const padTop = (!Number.isFinite(rows) || rows >= CRAMPED_TERMINAL_ROWS) ? PROMPT_PAD_TOP : 0;
|
|
1730
|
+
const padBottom = roomy ? PROMPT_PAD_BOTTOM : 0;
|
|
1731
|
+
const padded = new Array(padTop).fill('')
|
|
1732
|
+
.concat(body, new Array(padBottom).fill(''));
|
|
1733
|
+
const boxRows = barBlock(padded, {
|
|
1734
|
+
width: boxWidth,
|
|
1735
|
+
role: this.busy ? 'accent' : 'user',
|
|
1736
|
+
filled: true,
|
|
1737
|
+
surface: 'surface',
|
|
1738
|
+
punctuate: false
|
|
1987
1739
|
});
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
const availableWidth = this.surface.composeAvailableWidth
|
|
2134
|
-
? this.surface.composeAvailableWidth()
|
|
2135
|
-
: sessionMainWidth(this.surface.width, false, this.surface.env);
|
|
2136
|
-
const requestedWidth = typeof this.layer.preferredWidth === 'function'
|
|
2137
|
-
? this.layer.preferredWidth(Math.max(8, availableWidth - 3))
|
|
2138
|
-
: (this.layer.width || 64);
|
|
2139
|
-
const panelWidth = dialogWidth(Math.max(8, availableWidth - 3), requestedWidth);
|
|
2140
|
-
const panel = panelRows(this.layer, panelWidth, preferredHeight);
|
|
2141
|
-
this.surface.presentMessageActions(panel, this.layer);
|
|
2142
|
-
return;
|
|
2143
|
-
}
|
|
2144
|
-
if (isStartupDialog(this.surface)) {
|
|
2145
|
-
const view = startupDialogView(this.layer, this.surface.width, this.surface.stage.maxHeight);
|
|
2146
|
-
return this.present(view.rows, view.caretRow, view.caretCol);
|
|
2147
|
-
}
|
|
2148
|
-
const inline = this.surface && !this.surface.homeInfo;
|
|
2149
|
-
const availableWidth = inline
|
|
2150
|
-
? (this.surface.composeAvailableWidth
|
|
2151
|
-
? this.surface.composeAvailableWidth()
|
|
2152
|
-
: sessionMainWidth(this.surface.width, false, this.surface.env))
|
|
2153
|
-
: width;
|
|
2154
|
-
const panelWidth = dialogWidth(availableWidth, this.layer.width || 64);
|
|
2155
|
-
const stageMax = this.surface?.stage?.maxHeight || this.out?.rows || 24;
|
|
2156
|
-
const statusReserve = 2; // status row + margin
|
|
2157
|
-
const availableDialogRows = Math.max(4, stageMax - statusReserve);
|
|
2158
|
-
const maxHeight = dialogHeight(availableDialogRows, preferredHeight);
|
|
2159
|
-
const panel = panelRows(this.layer, panelWidth, maxHeight);
|
|
2160
|
-
const status = this.statusRow(width);
|
|
2161
|
-
const all = this.clampRows(panel.concat(status), width);
|
|
2162
|
-
|
|
2163
|
-
// panelRows() called body(), which is where a layer publishes its cursor.
|
|
2164
|
-
const cursor = this.layer.cursor;
|
|
2165
|
-
const centered = this.surface ? centerRows(all, availableWidth) : { rows: all, inset: 0 };
|
|
2166
|
-
const stageHeight = this.surface?.stage?.height || this.out?.rows || all.length;
|
|
2167
|
-
const outRows = Number.isFinite(this.out?.rows) && this.out.rows > 0
|
|
2168
|
-
? Math.floor(this.out.rows)
|
|
2169
|
-
: stageHeight;
|
|
2170
|
-
this.layer.panelGeometry = {
|
|
2171
|
-
inset: centered.inset,
|
|
2172
|
-
// Anchor at the panel's last body row above the bottom border and status rows (0-based screen row).
|
|
2173
|
-
optionScreenRow: Math.max(0, outRows - status.length - 2)
|
|
2174
|
-
};
|
|
2175
|
-
this.present(centered.rows, cursor ? 1 + cursor.y : 0,
|
|
2176
|
-
(cursor ? 2 + cursor.x : 0) + centered.inset);
|
|
2177
|
-
}
|
|
2178
|
-
|
|
2179
|
-
pickerRows(width) {
|
|
2180
|
-
const rows = renderPicker(this.completion.items, this.selected, width);
|
|
2181
|
-
const labelWidth = Math.min(
|
|
2182
|
-
36,
|
|
2183
|
-
rows.reduce((max, row) => Math.max(max, visibleWidth(row.label)), 0)
|
|
2184
|
-
);
|
|
2185
|
-
|
|
2186
|
-
const drawn = rows.map((row) => {
|
|
2187
|
-
const marker = row.selected ? paint('accent', GLYPHS.arrow) : ' ';
|
|
2188
|
-
const label = truncate(row.label, labelWidth, GLYPHS.ellipsis);
|
|
2189
|
-
const padded = label + ' '.repeat(Math.max(0, labelWidth - visibleWidth(label)));
|
|
2190
|
-
const name = row.selected ? bold(paint('ink', padded)) : paint('ink', padded);
|
|
2191
|
-
const description = row.description
|
|
2192
|
-
? dim(' ' + truncate(row.description, Math.max(0, width - labelWidth - 8), GLYPHS.ellipsis))
|
|
2193
|
-
: '';
|
|
2194
|
-
return ` ${marker} ${name}${description}`;
|
|
2195
|
-
});
|
|
2196
|
-
|
|
2197
|
-
// Count against everything that MATCHED, not against the capped array the
|
|
2198
|
-
// picker happens to hold. Reporting the capped figure told the user there
|
|
2199
|
-
// were 42 more entries when 107 were reachable by scrolling.
|
|
2200
|
-
const matched = this.completion.total || this.completion.items.length;
|
|
2201
|
-
const hidden = matched - drawn.length;
|
|
2202
|
-
if (hidden > 0) drawn.push(dim(` ${GLYPHS.ellipsis} ${hidden} more`));
|
|
2203
|
-
if (this.completion?.inline && (this.completion.kind === 'command' || this.completion.kind === 'command-argument')) {
|
|
2204
|
-
drawn.push(dim(' ' + truncate('Enter run · Esc dismiss · draft protected', Math.max(0, width - 5))));
|
|
2205
|
-
}
|
|
2206
|
-
return drawn;
|
|
2207
|
-
}
|
|
1740
|
+
const usageCardRows = this.usageCard ? renderUsageCardRows(this.usageCard, boxWidth)
|
|
1741
|
+
: this.statusCard !== null ? this.statusCardRows(boxWidth) : [];
|
|
1742
|
+
const planRows = this.planRows(width);
|
|
1743
|
+
const pickerRows = this.completion ? this.pickerRows(width) : [];
|
|
1744
|
+
this.pickerLayout = this.completion ? {
|
|
1745
|
+
pickerStart: usageCardRows.length + planRows.length + boxRows.length,
|
|
1746
|
+
pickerCount: pickerRows.length,
|
|
1747
|
+
totalPromptRows: 0
|
|
1748
|
+
} : null;
|
|
1749
|
+
const queuedRows = this.queuedPreviewRows(width);
|
|
1750
|
+
const cleanNotice = this.notice ? this.sanitizeSingleLine(this.notice) : '';
|
|
1751
|
+
const noticeRows = cleanNotice ? [dim(` ${cleanNotice}`)] : [];
|
|
1752
|
+
const status = this.statusRow(width);
|
|
1753
|
+
const gap = status.length > 0 ? new Array(FOOTER_GAP).fill('') : [];
|
|
1754
|
+
const all = this.clampRows(usageCardRows.concat(planRows, boxRows, pickerRows, queuedRows, noticeRows, gap, status), width);
|
|
1755
|
+
if (this.pickerLayout)
|
|
1756
|
+
this.pickerLayout.totalPromptRows = all.length;
|
|
1757
|
+
const availableWidth = this.surface?.composeAvailableWidth
|
|
1758
|
+
? this.surface.composeAvailableWidth()
|
|
1759
|
+
: sessionMainWidth(this.surface?.width || terminalWidth(), false, this.surface?.env);
|
|
1760
|
+
const positioned = this.surface
|
|
1761
|
+
? (this.surface.homeInfo ? centerRows(all, this.surface.width) : sessionRows(all, availableWidth))
|
|
1762
|
+
: { rows: all, inset: 0 };
|
|
1763
|
+
this.present(positioned.rows, usageCardRows.length + planRows.length + caretRow + padTop, layout.cursorCol + inset + positioned.inset);
|
|
1764
|
+
}
|
|
1765
|
+
present(all, caretRow, caretCol) {
|
|
1766
|
+
const rows = Array.isArray(all) ? all : [];
|
|
1767
|
+
const maxRow = Math.max(0, rows.length - 1);
|
|
1768
|
+
const safeRow = Number.isFinite(caretRow) ? Math.max(0, Math.min(maxRow, Math.floor(caretRow))) : 0;
|
|
1769
|
+
const safeCol = Number.isFinite(caretCol) ? Math.max(0, Math.floor(caretCol)) : 0;
|
|
1770
|
+
const hideCursor = Boolean(this.busy && this.editor.isEmpty);
|
|
1771
|
+
if (this.surface) {
|
|
1772
|
+
this.drawnRows = 0;
|
|
1773
|
+
this.cursorRow = 0;
|
|
1774
|
+
this.surface.present(rows, {
|
|
1775
|
+
row: safeRow,
|
|
1776
|
+
col: safeCol,
|
|
1777
|
+
hideCursor,
|
|
1778
|
+
modal: Boolean(this.layer),
|
|
1779
|
+
inline: Boolean(this.layer && !isStartupDialog(this.surface))
|
|
1780
|
+
});
|
|
1781
|
+
return;
|
|
1782
|
+
}
|
|
1783
|
+
this.out.write(rows.join('\n'));
|
|
1784
|
+
this.drawnRows = rows.length;
|
|
1785
|
+
const below = rows.length - 1 - safeRow;
|
|
1786
|
+
if (below > 0)
|
|
1787
|
+
this.out.write(`\x1b[${below}A`);
|
|
1788
|
+
this.cursorRow = safeRow;
|
|
1789
|
+
this.out.write('\r');
|
|
1790
|
+
if (safeCol > 0)
|
|
1791
|
+
this.out.write(`\x1b[${safeCol}C`);
|
|
1792
|
+
if (hideCursor) {
|
|
1793
|
+
this.out.write('\x1b[?25l');
|
|
1794
|
+
}
|
|
1795
|
+
else {
|
|
1796
|
+
this.out.write('\x1b[?25h');
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
attachSurface(surface) {
|
|
1800
|
+
this.surface = surface || null;
|
|
1801
|
+
if (!surface)
|
|
1802
|
+
return;
|
|
1803
|
+
surface.onDirty = () => this.scheduleDraw();
|
|
1804
|
+
}
|
|
1805
|
+
commit(kind, produce) {
|
|
1806
|
+
const clamped = (width) => this.clampRows(produce(width), width);
|
|
1807
|
+
if (this.surface) {
|
|
1808
|
+
this.surface.commit(kind, clamped);
|
|
1809
|
+
return;
|
|
1810
|
+
}
|
|
1811
|
+
this.erase();
|
|
1812
|
+
this.out.write(clamped(this.width()).join('\n') + '\n');
|
|
1813
|
+
}
|
|
1814
|
+
drawLayer(width) {
|
|
1815
|
+
const preferredHeight = typeof this.layer.preferredHeight === 'function'
|
|
1816
|
+
? this.layer.preferredHeight()
|
|
1817
|
+
: 14;
|
|
1818
|
+
if (this.layer.messageActions && this.surface?.presentMessageActions) {
|
|
1819
|
+
const availableWidth = this.surface.composeAvailableWidth
|
|
1820
|
+
? this.surface.composeAvailableWidth()
|
|
1821
|
+
: sessionMainWidth(this.surface.width, false, this.surface.env);
|
|
1822
|
+
const requestedWidth = typeof this.layer.preferredWidth === 'function'
|
|
1823
|
+
? this.layer.preferredWidth(Math.max(8, availableWidth - 3))
|
|
1824
|
+
: (this.layer.width || 64);
|
|
1825
|
+
const panelWidth = dialogWidth(Math.max(8, availableWidth - 3), requestedWidth);
|
|
1826
|
+
const panel = panelRows(this.layer, panelWidth, preferredHeight);
|
|
1827
|
+
this.surface.presentMessageActions(panel, this.layer);
|
|
1828
|
+
return;
|
|
1829
|
+
}
|
|
1830
|
+
if (isStartupDialog(this.surface)) {
|
|
1831
|
+
const view = startupDialogView(this.layer, this.surface.width, this.surface.stage.maxHeight);
|
|
1832
|
+
this.present(view.rows, view.caretRow, view.caretCol);
|
|
1833
|
+
return;
|
|
1834
|
+
}
|
|
1835
|
+
const inline = Boolean(this.surface && !this.surface.homeInfo);
|
|
1836
|
+
const availableWidth = inline
|
|
1837
|
+
? (this.surface.composeAvailableWidth
|
|
1838
|
+
? this.surface.composeAvailableWidth()
|
|
1839
|
+
: sessionMainWidth(this.surface.width, false, this.surface.env))
|
|
1840
|
+
: width;
|
|
1841
|
+
const panelWidth = dialogWidth(availableWidth, this.layer.width || 64);
|
|
1842
|
+
const stageMax = this.surface?.stage?.maxHeight || this.out?.rows || 24;
|
|
1843
|
+
const statusReserve = 2;
|
|
1844
|
+
const availableDialogRows = Math.max(4, stageMax - statusReserve);
|
|
1845
|
+
const maxHeight = dialogHeight(availableDialogRows, preferredHeight);
|
|
1846
|
+
const panel = panelRows(this.layer, panelWidth, maxHeight);
|
|
1847
|
+
const status = this.statusRow(width);
|
|
1848
|
+
const all = this.clampRows(panel.concat(status), width);
|
|
1849
|
+
const cursor = this.layer.cursor;
|
|
1850
|
+
const centered = this.surface ? centerRows(all, availableWidth) : { rows: all, inset: 0 };
|
|
1851
|
+
const stageHeight = this.surface?.stage?.height || this.out?.rows || all.length;
|
|
1852
|
+
const outRows = Number.isFinite(this.out?.rows) && this.out.rows > 0
|
|
1853
|
+
? Math.floor(this.out.rows)
|
|
1854
|
+
: stageHeight;
|
|
1855
|
+
this.layer.panelGeometry = {
|
|
1856
|
+
inset: centered.inset,
|
|
1857
|
+
optionScreenRow: Math.max(0, outRows - status.length - 2)
|
|
1858
|
+
};
|
|
1859
|
+
this.present(centered.rows, cursor ? 1 + cursor.y : 0, (cursor ? 2 + cursor.x : 0) + centered.inset);
|
|
1860
|
+
}
|
|
1861
|
+
pickerRows(width) {
|
|
1862
|
+
if (!this.completion)
|
|
1863
|
+
return [];
|
|
1864
|
+
const rows = renderPicker(this.completion.items, this.selected, width);
|
|
1865
|
+
const labelWidth = Math.min(36, rows.reduce((max, row) => Math.max(max, visibleWidth(row.label)), 0));
|
|
1866
|
+
const drawn = rows.map((row) => {
|
|
1867
|
+
const marker = row.selected ? paint('accent', GLYPHS.arrow) : ' ';
|
|
1868
|
+
const label = truncate(row.label, labelWidth, GLYPHS.ellipsis);
|
|
1869
|
+
const padded = label + ' '.repeat(Math.max(0, labelWidth - visibleWidth(label)));
|
|
1870
|
+
const name = row.selected ? bold(paint('ink', padded)) : paint('ink', padded);
|
|
1871
|
+
const description = row.description
|
|
1872
|
+
? dim(' ' + truncate(row.description, Math.max(0, width - labelWidth - 8), GLYPHS.ellipsis))
|
|
1873
|
+
: '';
|
|
1874
|
+
return ` ${marker} ${name}${description}`;
|
|
1875
|
+
});
|
|
1876
|
+
const matched = this.completion.total || this.completion.items.length;
|
|
1877
|
+
const hidden = matched - drawn.length;
|
|
1878
|
+
if (hidden > 0)
|
|
1879
|
+
drawn.push(dim(` ${GLYPHS.ellipsis} ${hidden} more`));
|
|
1880
|
+
if (this.completion.kind !== 'file' && this.completion.inline) {
|
|
1881
|
+
drawn.push(dim(' ' + truncate('Enter run · Esc dismiss · draft protected', Math.max(0, width - 5))));
|
|
1882
|
+
}
|
|
1883
|
+
return drawn;
|
|
1884
|
+
}
|
|
2208
1885
|
}
|
|
2209
|
-
|
|
2210
|
-
/**
|
|
2211
|
-
* Hint line for the session header — the keys that are not discoverable.
|
|
2212
|
-
*
|
|
2213
|
-
* FITTED to the terminal. The full list is 140 columns; printed unconditionally
|
|
2214
|
-
* it wrapped on any terminal narrower than that, which is how the very first
|
|
2215
|
-
* thing a user sees ended up broken across two rows mid-word. Fragments are
|
|
2216
|
-
* dropped from the right — least important first — rather than the line being
|
|
2217
|
-
* truncated, because a hint clipped mid-key is worse than a missing one.
|
|
2218
|
-
*/
|
|
2219
1886
|
export function keyHints(width = terminalWidth()) {
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
: kept.join(separator);
|
|
1887
|
+
const parts = [
|
|
1888
|
+
'enter send', 'alt+enter newline', 'ctrl+p palette', '/ commands', '@ files',
|
|
1889
|
+
'↑ history', 'tab complete', 'esc interrupt', 'ctrl+o expand'
|
|
1890
|
+
];
|
|
1891
|
+
const separator = ` ${GLYPHS.dot} `;
|
|
1892
|
+
const kept = parts.slice();
|
|
1893
|
+
while (kept.length > 1 && visibleWidth(kept.join(separator)) > width - 1)
|
|
1894
|
+
kept.pop();
|
|
1895
|
+
return HAS_COLOR
|
|
1896
|
+
? kept.map((part) => dim(part)).join(dim(separator))
|
|
1897
|
+
: kept.join(separator);
|
|
2232
1898
|
}
|
|
2233
|
-
|
|
2234
1899
|
export { MAX_VISIBLE };
|