minimal-agent 0.10.0 → 0.12.0
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/README.md +11 -3
- package/package.json +52 -5
- package/plugins/computer_use/.claude-plugin/plugin.json +6 -0
- package/plugins/computer_use/README.md +94 -0
- package/plugins/computer_use/app/_template.md +142 -0
- package/plugins/computer_use/app/chrome.md +165 -0
- package/plugins/computer_use/app/finder.md +76 -0
- package/plugins/computer_use/app/macos-system-settings.md +81 -0
- package/plugins/computer_use/app/microsoft-excel.md +80 -0
- package/plugins/computer_use/app/microsoft-powerpoint.md +80 -0
- package/plugins/computer_use/app/microsoft-word.md +79 -0
- package/plugins/computer_use/app/wechat.md +413 -0
- package/plugins/computer_use/app/weread.md +319 -0
- package/plugins/computer_use/commands/computeruse.md +25 -0
- package/plugins/computer_use/commands/cu.md +9 -0
- package/plugins/computer_use/plugin.js +26 -0
- package/plugins/computer_use/plugin.ts +30 -0
- package/plugins/computer_use/src/capture.js +90 -0
- package/plugins/computer_use/src/capture.ts +104 -0
- package/plugins/computer_use/src/commands.js +91 -0
- package/plugins/computer_use/src/commands.ts +103 -0
- package/plugins/computer_use/src/daemon.js +134 -0
- package/plugins/computer_use/src/daemon.ts +161 -0
- package/plugins/computer_use/src/manuals.js +135 -0
- package/plugins/computer_use/src/manuals.ts +159 -0
- package/plugins/computer_use/src/verify.js +168 -0
- package/plugins/computer_use/src/verify.ts +219 -0
- package/plugins/computer_use/test/commands.test.ts +73 -0
- package/plugins/cron-runner/plugin.ts +38 -0
- package/plugins/cron-runner/src/commands.ts +373 -0
- package/plugins/cron-runner/src/cronTime.ts +155 -0
- package/plugins/cron-runner/src/lock.ts +79 -0
- package/plugins/cron-runner/src/runs.ts +99 -0
- package/plugins/cron-runner/src/scheduleEngine.ts +76 -0
- package/plugins/cron-runner/src/scheduler.ts +376 -0
- package/plugins/cron-runner/src/schema.ts +139 -0
- package/plugins/cron-runner/src/service.ts +193 -0
- package/plugins/cron-runner/src/store.ts +333 -0
- package/plugins/cron-runner/src/types.ts +141 -0
- package/plugins/cron-runner/test/cronTime.test.ts +41 -0
- package/plugins/cron-runner/test/plugin-integration.test.ts +47 -0
- package/plugins/cron-runner/test/scheduleEngine.test.ts +41 -0
- package/plugins/cron-runner/test/service.test.ts +76 -0
- package/plugins/cron-runner/test/store-and-scheduler.test.ts +501 -0
- package/plugins/workflow-runner/plugin.js +56 -2
- package/plugins/workflow-runner/plugin.ts +113 -0
- package/plugins/workflow-runner/src/agentCommon.js +43 -4
- package/plugins/workflow-runner/src/agentCommon.ts +435 -0
- package/plugins/workflow-runner/src/controlFlow/branch.js +40 -0
- package/plugins/workflow-runner/src/controlFlow/branch.ts +46 -0
- package/plugins/workflow-runner/src/controlFlow/loop.js +211 -0
- package/plugins/workflow-runner/src/controlFlow/loop.ts +248 -0
- package/plugins/workflow-runner/src/controlFlow/pause.js +129 -0
- package/plugins/workflow-runner/src/controlFlow/pause.ts +141 -0
- package/plugins/workflow-runner/src/controlFlow/signal.js +21 -0
- package/plugins/workflow-runner/src/controlFlow/signal.ts +29 -0
- package/plugins/workflow-runner/src/controlFlow/types.js +18 -0
- package/plugins/workflow-runner/src/controlFlow/types.ts +63 -0
- package/plugins/workflow-runner/src/dslManifest.ts +127 -0
- package/plugins/workflow-runner/src/expressions.ts +622 -0
- package/plugins/workflow-runner/src/index.js +13 -4
- package/plugins/workflow-runner/src/index.ts +338 -0
- package/plugins/workflow-runner/src/jsonPayload.ts +84 -0
- package/plugins/workflow-runner/src/loader.js +23 -0
- package/plugins/workflow-runner/src/loader.ts +721 -0
- package/plugins/workflow-runner/src/multimodal.js +2 -1
- package/plugins/workflow-runner/src/multimodal.ts +64 -0
- package/plugins/workflow-runner/src/recovery.ts +149 -0
- package/plugins/workflow-runner/src/runner.js +75 -659
- package/plugins/workflow-runner/src/runner.ts +604 -0
- package/plugins/workflow-runner/src/stepExecutors/agent.ts +234 -0
- package/plugins/workflow-runner/src/stepExecutors/assert.ts +50 -0
- package/plugins/workflow-runner/src/stepExecutors/echo.js +31 -0
- package/plugins/workflow-runner/src/stepExecutors/echo.ts +38 -0
- package/plugins/workflow-runner/src/stepExecutors/llm.ts +133 -0
- package/plugins/workflow-runner/src/stepExecutors/set.js +49 -0
- package/plugins/workflow-runner/src/stepExecutors/set.ts +56 -0
- package/plugins/workflow-runner/src/stepExecutors/skill.js +5 -6
- package/plugins/workflow-runner/src/stepExecutors/skill.ts +140 -0
- package/plugins/workflow-runner/src/stepExecutors/tool.js +80 -8
- package/plugins/workflow-runner/src/stepExecutors/tool.ts +256 -0
- package/plugins/workflow-runner/src/stepExecutors/verify.ts +68 -0
- package/plugins/workflow-runner/src/stepPipeline.js +277 -0
- package/plugins/workflow-runner/src/stepPipeline.ts +366 -0
- package/plugins/workflow-runner/src/taskIdentity.ts +405 -0
- package/plugins/workflow-runner/src/toolRuntime.js +48 -0
- package/plugins/workflow-runner/src/toolRuntime.ts +64 -0
- package/plugins/workflow-runner/src/types.js +37 -1
- package/plugins/workflow-runner/src/types.ts +602 -0
- package/plugins/workflow-runner/src/workflowState.ts +733 -0
- package/plugins/workflow-runner/test/agent.test.ts +587 -0
- package/plugins/workflow-runner/test/agentLlmError.test.ts +373 -0
- package/plugins/workflow-runner/test/bashBlacklist.sweep.test.ts +65 -0
- package/plugins/workflow-runner/test/captureMisbind.test.ts +99 -0
- package/plugins/workflow-runner/test/cli.e2e.test.ts +147 -0
- package/plugins/workflow-runner/test/context-files.test.ts +320 -0
- package/plugins/workflow-runner/test/e2e.test.ts +370 -0
- package/plugins/workflow-runner/test/engineRobustness.test.ts +226 -0
- package/plugins/workflow-runner/test/expressions.test.ts +397 -0
- package/plugins/workflow-runner/test/foundation.test.ts +126 -0
- package/plugins/workflow-runner/test/graceful.test.ts +139 -0
- package/plugins/workflow-runner/test/greedyInputs.load.test.ts +47 -0
- package/plugins/workflow-runner/test/hostCapability.test.ts +108 -0
- package/plugins/workflow-runner/test/illustrate.load.test.ts +183 -0
- package/plugins/workflow-runner/test/jsonPayload.test.ts +83 -0
- package/plugins/workflow-runner/test/loader.test.ts +617 -0
- package/plugins/workflow-runner/test/maxTurnsRetryDelay.test.ts +33 -0
- package/plugins/workflow-runner/test/multimodal.test.ts +342 -0
- package/plugins/workflow-runner/test/output-schema.test.ts +213 -0
- package/plugins/workflow-runner/test/parseWorkflowArgs.test.ts +87 -0
- package/plugins/workflow-runner/test/pluginRunner.isolation.test.ts +269 -0
- package/plugins/workflow-runner/test/ppt.load.test.ts +1220 -0
- package/plugins/workflow-runner/test/ralphLoop.e2e.test.ts +220 -0
- package/plugins/workflow-runner/test/recovery.test.ts +265 -0
- package/plugins/workflow-runner/test/runner.test.ts +911 -0
- package/plugins/workflow-runner/test/runnerPreflight.test.ts +379 -0
- package/plugins/workflow-runner/test/skillTurnDone.test.ts +71 -0
- package/plugins/workflow-runner/test/stateLifecycle.test.ts +150 -0
- package/plugins/workflow-runner/test/stepPipeline.test.ts +114 -0
- package/plugins/workflow-runner/test/taskArbitration.test.ts +338 -0
- package/plugins/workflow-runner/test/taskIdentity.test.ts +237 -0
- package/plugins/workflow-runner/test/taskLifecycle.test.ts +270 -0
- package/plugins/workflow-runner/test/toolArtifactOverwrite.test.ts +221 -0
- package/plugins/workflow-runner/test/toolCapabilityBoundary.test.ts +29 -0
- package/plugins/workflow-runner/test/toolRuntimeIntegration.test.ts +168 -0
- package/plugins/workflow-runner/test/webppt.load.test.ts +250 -0
- package/plugins/workflow-runner/test/word.load.test.ts +208 -0
- package/plugins/workflow-runner/test/workflows.smoke.test.ts +76 -0
- package/plugins/workflow-runner/test/yamlGuardConditions.test.ts +207 -0
- package/skills/browser-automation/SKILL.md +9 -19
- package/skills/web-crawler/SKILL.md +9 -11
- package/src/askBridge.ts +249 -0
- package/src/bootstrap/cwdArg.js +10 -3
- package/src/bootstrap/cwdArg.ts +31 -0
- package/src/bootstrap/workingDir.ts +48 -0
- package/src/cli/args.js +70 -7
- package/src/cli/args.ts +146 -0
- package/src/cli/computerUse.js +185 -0
- package/src/cli/computerUse.ts +221 -0
- package/src/cli/configMenu.js +274 -202
- package/src/cli/configMenu.tsx +521 -0
- package/src/cli/memoryAccess.js +45 -0
- package/src/cli/memoryAccess.ts +53 -0
- package/src/cli/print.js +119 -5
- package/src/cli/print.ts +597 -0
- package/src/cli/quickModelSetup.js +60 -0
- package/src/cli/quickModelSetup.tsx +96 -0
- package/src/cli/streamJson.js +80 -4
- package/src/cli/streamJson.ts +433 -0
- package/src/cli/wizards/advisorWizard.js +1 -0
- package/src/cli/wizards/advisorWizard.tsx +195 -0
- package/src/cli/wizards/agentRuntimeWizard.js +84 -0
- package/src/cli/wizards/agentRuntimeWizard.tsx +134 -0
- package/src/cli/wizards/browserWizard.js +55 -0
- package/src/cli/wizards/browserWizard.tsx +77 -0
- package/src/cli/wizards/contextAdvisorWizard.js +84 -0
- package/src/cli/wizards/contextAdvisorWizard.tsx +124 -0
- package/src/cli/wizards/fieldForm.js +7 -4
- package/src/cli/wizards/fieldForm.tsx +250 -0
- package/src/cli/wizards/imageWizard.js +4 -5
- package/src/cli/wizards/imageWizard.tsx +96 -0
- package/src/cli/wizards/openRouterWizard.tsx +48 -0
- package/src/cli/wizards/providerPresets.js +1 -8
- package/src/cli/wizards/providerPresets.ts +106 -0
- package/src/cli/wizards/providerWizard.js +152 -205
- package/src/cli/wizards/providerWizard.tsx +266 -0
- package/src/cli/wizards/searchWizard.tsx +47 -0
- package/src/cli/wizards/ttsWizard.js +4 -3
- package/src/cli/wizards/ttsWizard.tsx +61 -0
- package/src/config/configDefaults.js +25 -0
- package/src/config/configDefaults.ts +33 -0
- package/src/config/configDiagnostics.js +117 -0
- package/src/config/configDiagnostics.ts +166 -0
- package/src/config/configFile.js +52 -32
- package/src/config/configFile.ts +633 -0
- package/src/config/configManagement.js +126 -0
- package/src/config/configManagement.ts +159 -0
- package/src/config/configService.js +66 -11
- package/src/config/configService.ts +554 -0
- package/src/config/numericLimits.js +61 -0
- package/src/config/numericLimits.ts +80 -0
- package/src/config.ts +109 -0
- package/src/context/agentHome.ts +11 -0
- package/src/context/archive.ts +144 -0
- package/src/context/budgetPlan.js +54 -0
- package/src/context/budgetPlan.ts +88 -0
- package/src/context/compact.js +179 -106
- package/src/context/compact.ts +584 -0
- package/src/context/contextBundle.js +119 -0
- package/src/context/contextBundle.ts +176 -0
- package/src/context/contextLock.ts +84 -0
- package/src/context/microCompactLite.ts +218 -0
- package/src/context/persistContext.ts +177 -0
- package/src/context/reactiveCompact.js +19 -4
- package/src/context/reactiveCompact.ts +240 -0
- package/src/context/recentDirs.ts +42 -0
- package/src/context/sessionPath.ts +206 -0
- package/src/context/sessionRegistry.js +8 -2
- package/src/context/sessionRegistry.ts +414 -0
- package/src/context/snipCompact.ts +163 -0
- package/src/context/tokenCounter.ts +106 -0
- package/src/context/workspaceIndex.js +2 -2
- package/src/context/workspaceIndex.ts +751 -0
- package/src/entry.compiled.tsx +20 -0
- package/src/harness/claimValidation.js +9 -0
- package/src/harness/claimValidation.ts +22 -0
- package/src/harness/evidenceLedger.js +92 -0
- package/src/harness/evidenceLedger.ts +96 -0
- package/src/harness/stallGuard.js +60 -0
- package/src/harness/stallGuard.ts +81 -0
- package/src/humanIntervention.js +129 -0
- package/src/humanIntervention.ts +216 -0
- package/src/infra/verification.ts +295 -0
- package/src/irreversible/rules.js +216 -0
- package/src/irreversible/rules.ts +261 -0
- package/src/llm/client.js +40 -3
- package/src/llm/client.ts +621 -0
- package/src/llm/promptCache.js +27 -0
- package/src/llm/promptCache.ts +41 -0
- package/src/llm/providers/anthropicFormat.js +41 -5
- package/src/llm/providers/anthropicFormat.ts +290 -0
- package/src/llm/providers/openaiFormat.js +186 -8
- package/src/llm/providers/openaiFormat.ts +428 -0
- package/src/loop.js +614 -63
- package/src/loop.ts +1454 -0
- package/src/main.js +67 -10
- package/src/main.tsx +344 -0
- package/src/memory/codec.js +204 -0
- package/src/memory/codec.ts +249 -0
- package/src/memory/constants.js +34 -0
- package/src/memory/constants.ts +50 -0
- package/src/memory/episodicMemory.js +684 -0
- package/src/memory/episodicMemory.ts +815 -0
- package/src/memory/fileStore.js +148 -0
- package/src/memory/fileStore.ts +184 -0
- package/src/memory/memoryService.js +570 -0
- package/src/memory/memoryService.ts +859 -0
- package/src/memory/migration.js +128 -0
- package/src/memory/migration.ts +160 -0
- package/src/memory/repository.js +789 -0
- package/src/memory/repository.ts +893 -0
- package/src/memory/retrieval.js +108 -0
- package/src/memory/retrieval.ts +145 -0
- package/src/memory/searchIndex.js +107 -0
- package/src/memory/searchIndex.ts +153 -0
- package/src/memory/topicView.js +82 -0
- package/src/memory/topicView.ts +95 -0
- package/src/memory/turnJournal.js +95 -0
- package/src/memory/turnJournal.ts +120 -0
- package/src/memory/types.js +2 -0
- package/src/memory/types.ts +186 -0
- package/src/messageQueue.ts +123 -0
- package/src/multimodal.js +128 -46
- package/src/multimodal.ts +349 -0
- package/src/plugin-sdk.js +21 -1
- package/src/plugin-sdk.ts +134 -0
- package/src/plugins/builtinPluginIds.js +47 -0
- package/src/plugins/builtinPluginIds.ts +51 -0
- package/src/plugins/builtinRegistry.ts +43 -0
- package/src/plugins/commandRouter.js +40 -26
- package/src/plugins/commandRouter.ts +261 -0
- package/src/plugins/hookEngine.ts +341 -0
- package/src/plugins/pluginApi.ts +133 -0
- package/src/plugins/pluginLifecycle.ts +34 -0
- package/src/plugins/pluginLoader.ts +147 -0
- package/src/plugins/pluginManifest.js +127 -0
- package/src/plugins/pluginManifest.ts +152 -0
- package/src/plugins/pluginRunner.js +47 -2
- package/src/plugins/pluginRunner.ts +207 -0
- package/src/plugins/transcript.ts +208 -0
- package/src/prompts/lint.js +169 -0
- package/src/prompts/lint.ts +220 -0
- package/src/prompts/pluginList.js +32 -16
- package/src/prompts/pluginList.ts +86 -0
- package/src/prompts/policyRegistry.js +318 -0
- package/src/prompts/policyRegistry.ts +358 -0
- package/src/prompts/projectInstructions.js +24 -0
- package/src/prompts/projectInstructions.ts +81 -0
- package/src/prompts/promptAssembly.js +311 -202
- package/src/prompts/promptAssembly.ts +510 -0
- package/src/prompts/skillList.js +106 -45
- package/src/prompts/skillList.ts +198 -0
- package/src/prompts/system.js +31 -6
- package/src/prompts/system.ts +81 -0
- package/src/runtime/agentRuntimeContext.js +53 -0
- package/src/runtime/agentRuntimeContext.ts +112 -0
- package/src/session/runAgentTurn.js +71 -12
- package/src/session/runAgentTurn.ts +181 -0
- package/src/session/runTurn.js +29 -0
- package/src/session/runTurn.ts +160 -0
- package/src/session/runtimeStore.js +149 -0
- package/src/session/runtimeStore.ts +197 -0
- package/src/session/sessionState.js +79 -3
- package/src/session/sessionState.ts +134 -0
- package/src/toolGate.ts +50 -0
- package/src/tools/advisor/advisor.js +76 -15
- package/src/tools/advisor/advisor.ts +512 -0
- package/src/tools/agent/agent.js +245 -29
- package/src/tools/agent/agent.ts +837 -0
- package/src/tools/agent/agentTaskService.js +60 -0
- package/src/tools/agent/agentTaskService.ts +113 -0
- package/src/tools/agent/agentTypes.js +20 -8
- package/src/tools/agent/agentTypes.ts +94 -0
- package/src/tools/agent/coordinator.ts +111 -0
- package/src/tools/agent/worktree.ts +296 -0
- package/src/tools/ask/ask.js +17 -0
- package/src/tools/ask/ask.ts +122 -0
- package/src/tools/bash/attachImage.js +223 -0
- package/src/tools/bash/attachImage.ts +281 -0
- package/src/tools/bash/bash.js +132 -14
- package/src/tools/bash/bash.ts +642 -0
- package/src/tools/bash/semantics.ts +103 -0
- package/src/tools/bash/warnings.js +17 -11
- package/src/tools/bash/warnings.ts +117 -0
- package/src/tools/capabilities.js +42 -0
- package/src/tools/capabilities.ts +69 -0
- package/src/tools/computeruse/accessibilityPermission.js +261 -0
- package/src/tools/computeruse/accessibilityPermission.ts +294 -0
- package/src/tools/computeruse/axSearch.js +96 -0
- package/src/tools/computeruse/axSearch.ts +120 -0
- package/src/tools/computeruse/axSnap.js +272 -0
- package/src/tools/computeruse/axSnap.ts +349 -0
- package/src/tools/computeruse/capabilityLabels.js +95 -0
- package/src/tools/computeruse/capabilityLabels.ts +115 -0
- package/src/tools/computeruse/computerUseService.js +2235 -0
- package/src/tools/computeruse/computerUseService.ts +2686 -0
- package/src/tools/computeruse/computeruse.js +2742 -0
- package/src/tools/computeruse/computeruse.ts +3265 -0
- package/src/tools/computeruse/diagnosticTrace.js +154 -0
- package/src/tools/computeruse/diagnosticTrace.ts +195 -0
- package/src/tools/computeruse/diagnostics.js +369 -0
- package/src/tools/computeruse/diagnostics.ts +453 -0
- package/src/tools/computeruse/grants.js +53 -0
- package/src/tools/computeruse/grants.ts +70 -0
- package/src/tools/computeruse/humanBoundary.js +159 -0
- package/src/tools/computeruse/humanBoundary.ts +199 -0
- package/src/tools/computeruse/intervention.js +77 -0
- package/src/tools/computeruse/intervention.ts +110 -0
- package/src/tools/computeruse/manualSchema.js +709 -0
- package/src/tools/computeruse/manualSchema.ts +916 -0
- package/src/tools/computeruse/observationImages.js +705 -0
- package/src/tools/computeruse/observationImages.ts +889 -0
- package/src/tools/computeruse/protocol.js +60 -0
- package/src/tools/computeruse/protocol.ts +515 -0
- package/src/tools/computeruse/telemetryLog.js +57 -0
- package/src/tools/computeruse/telemetryLog.ts +67 -0
- package/src/tools/computeruse/vision/appGuide.js +325 -0
- package/src/tools/computeruse/vision/appGuide.ts +482 -0
- package/src/tools/computeruse/vision/focus.js +291 -0
- package/src/tools/computeruse/vision/focus.ts +382 -0
- package/src/tools/computeruse/vision/frames.js +248 -0
- package/src/tools/computeruse/vision/frames.ts +329 -0
- package/src/tools/computeruse/vision/geometry.js +131 -0
- package/src/tools/computeruse/vision/geometry.ts +163 -0
- package/src/tools/computeruse/vision/imaging.js +201 -0
- package/src/tools/computeruse/vision/imaging.ts +281 -0
- package/src/tools/computeruse/vision/png.js +274 -0
- package/src/tools/computeruse/vision/png.ts +317 -0
- package/src/tools/computeruse/vision/raster.js +320 -0
- package/src/tools/computeruse/vision/raster.ts +408 -0
- package/src/tools/computeruse/vision/settle.js +131 -0
- package/src/tools/computeruse/vision/settle.ts +220 -0
- package/src/tools/computeruse/vision/telemetry.js +196 -0
- package/src/tools/computeruse/vision/telemetry.ts +241 -0
- package/src/tools/computeruse/visionOcr.js +115 -0
- package/src/tools/computeruse/visionOcr.ts +128 -0
- package/src/tools/computeruse/waitFor.js +237 -0
- package/src/tools/computeruse/waitFor.ts +291 -0
- package/src/tools/download/download.js +10 -6
- package/src/tools/download/download.ts +187 -0
- package/src/tools/edit/edit.js +30 -18
- package/src/tools/edit/edit.ts +349 -0
- package/src/tools/edit/multi-edit.js +25 -14
- package/src/tools/edit/multi-edit.ts +223 -0
- package/src/tools/effectiveToolSet.js +73 -0
- package/src/tools/effectiveToolSet.ts +91 -0
- package/src/tools/genimage/genimage.ts +257 -0
- package/src/tools/genspeech/genspeech.ts +83 -0
- package/src/tools/glob/glob.js +176 -29
- package/src/tools/glob/glob.ts +283 -0
- package/src/tools/grep/grep.js +61 -19
- package/src/tools/grep/grep.ts +239 -0
- package/src/tools/grep/rgPath.ts +206 -0
- package/src/tools/index.js +99 -5
- package/src/tools/index.ts +293 -0
- package/src/tools/lsp/config.js +113 -0
- package/src/tools/lsp/config.ts +113 -0
- package/src/tools/lsp/lsp.js +256 -0
- package/src/tools/lsp/lsp.ts +274 -0
- package/src/tools/lsp/service.js +303 -0
- package/src/tools/lsp/service.ts +296 -0
- package/src/tools/memory/memory.js +520 -0
- package/src/tools/memory/memory.ts +625 -0
- package/src/tools/memory/policy.js +53 -0
- package/src/tools/memory/policy.ts +72 -0
- package/src/tools/notebook/notebookEdit.js +114 -0
- package/src/tools/notebook/notebookEdit.ts +126 -0
- package/src/tools/powershell/powershell.js +210 -0
- package/src/tools/powershell/powershell.ts +239 -0
- package/src/tools/presentresult/presentresult.js +145 -0
- package/src/tools/presentresult/presentresult.ts +156 -0
- package/src/tools/read/read.js +371 -163
- package/src/tools/read/read.ts +558 -0
- package/src/tools/shared/diffSummary.js +44 -0
- package/src/tools/shared/diffSummary.ts +63 -0
- package/src/tools/shared/fileActivity.js +51 -0
- package/src/tools/shared/fileActivity.ts +66 -0
- package/src/tools/shared/fileMutationBus.js +13 -0
- package/src/tools/shared/fileMutationBus.ts +14 -0
- package/src/tools/shared/fileState.js +73 -65
- package/src/tools/shared/fileState.ts +145 -0
- package/src/tools/shared/fileUtils.js +81 -4
- package/src/tools/shared/fileUtils.ts +504 -0
- package/src/tools/shared/killTree.ts +50 -0
- package/src/tools/shared/permanentDeleteGuard.js +26 -0
- package/src/tools/shared/permanentDeleteGuard.ts +34 -0
- package/src/tools/shared/schemas.ts +20 -0
- package/src/tools/shared/toolOutputStore.js +97 -0
- package/src/tools/shared/toolOutputStore.ts +110 -0
- package/src/tools/skill/skill.js +86 -0
- package/src/tools/skill/skill.ts +94 -0
- package/src/tools/snip/snip.js +67 -0
- package/src/tools/snip/snip.ts +80 -0
- package/src/tools/tasks/backgroundTaskService.js +275 -0
- package/src/tools/tasks/backgroundTaskService.ts +369 -0
- package/src/tools/tasks/processEvidence.js +24 -0
- package/src/tools/tasks/processEvidence.ts +34 -0
- package/src/tools/tasks/taskManagement.js +163 -0
- package/src/tools/tasks/taskManagement.ts +180 -0
- package/src/tools/tasks/taskOutput.js +113 -0
- package/src/tools/tasks/taskOutput.ts +145 -0
- package/src/tools/tasks/taskStop.js +74 -0
- package/src/tools/tasks/taskStop.ts +99 -0
- package/src/tools/tasks/taskStore.js +46 -0
- package/src/tools/tasks/taskStore.ts +77 -0
- package/src/tools/terminal/terminalCapture.js +167 -0
- package/src/tools/terminal/terminalCapture.ts +179 -0
- package/src/tools/trash/trash.js +233 -0
- package/src/tools/trash/trash.ts +283 -0
- package/src/tools/types.js +1 -1
- package/src/tools/types.ts +24 -0
- package/src/tools/webbrowser/agentBrowserPath.ts +286 -0
- package/src/tools/webbrowser/browser.js +214 -11
- package/src/tools/webbrowser/browser.ts +293 -0
- package/src/tools/webbrowser/browserControl.js +153 -48
- package/src/tools/webbrowser/browserControl.ts +262 -0
- package/src/tools/webbrowser/browserDaemon.js +147 -39
- package/src/tools/webbrowser/browserDaemon.ts +270 -0
- package/src/tools/webbrowser/browserOutcome.js +273 -0
- package/src/tools/webbrowser/browserOutcome.ts +306 -0
- package/src/tools/webbrowser/browserRunRegistry.js +150 -0
- package/src/tools/webbrowser/browserRunRegistry.ts +179 -0
- package/src/tools/webbrowser/browserService.js +20 -6
- package/src/tools/webbrowser/browserService.ts +56 -0
- package/src/tools/webbrowser/browserStream.js +30 -1
- package/src/tools/webbrowser/browserStream.ts +216 -0
- package/src/tools/webbrowser/browserTabs.js +143 -0
- package/src/tools/webbrowser/browserTabs.ts +210 -0
- package/src/tools/webbrowser/webbrowser.js +831 -150
- package/src/tools/webbrowser/webbrowser.ts +1537 -0
- package/src/tools/webfetch/preapproved.ts +274 -0
- package/src/tools/webfetch/webfetch.js +24 -23
- package/src/tools/webfetch/webfetch.ts +383 -0
- package/src/tools/websearch/websearch.js +12 -11
- package/src/tools/websearch/websearch.ts +292 -0
- package/src/tools/worktree/sessionWorktree.js +128 -0
- package/src/tools/worktree/sessionWorktree.ts +158 -0
- package/src/tools/write/write.js +27 -44
- package/src/tools/write/write.ts +136 -0
- package/src/types.ts +960 -0
- package/src/ui/App.js +216 -12
- package/src/ui/App.tsx +879 -0
- package/src/ui/AskQuestion.tsx +300 -0
- package/src/ui/InputBox.js +77 -1
- package/src/ui/InputBox.tsx +567 -0
- package/src/ui/LiveArea.tsx +344 -0
- package/src/ui/MessageList.js +27 -6
- package/src/ui/MessageList.tsx +559 -0
- package/src/ui/ModelPicker.tsx +107 -0
- package/src/ui/QueuedMessagesBanner.tsx +51 -0
- package/src/ui/Root.js +6 -8
- package/src/ui/Root.tsx +100 -0
- package/src/ui/SessionPicker.tsx +169 -0
- package/src/ui/StatusLine.js +22 -1
- package/src/ui/StatusLine.tsx +145 -0
- package/src/ui/WorkspacePicker.tsx +84 -0
- package/src/ui/advisorCommand.js +54 -0
- package/src/ui/advisorCommand.ts +63 -0
- package/src/ui/computerCommand.js +19 -0
- package/src/ui/computerCommand.ts +35 -0
- package/src/ui/figures.ts +45 -0
- package/src/ui/hooks/askSlot.ts +112 -0
- package/src/ui/hooks/useChat.js +271 -7
- package/src/ui/hooks/useChat.ts +1578 -0
- package/src/ui/hooks/useInputHistory.ts +235 -0
- package/src/ui/hooks/usePasteHandler.ts +220 -0
- package/src/ui/hooks/useTerminalSize.ts +41 -0
- package/src/ui/hooks/useTextBuffer.ts +108 -0
- package/src/ui/hooks/useTokenUsage.ts +44 -0
- package/src/ui/liveViewport.ts +189 -0
- package/src/ui/markdown.ts +230 -0
- package/src/ui/textBuffer.ts +239 -0
- package/src/ui/theme.ts +35 -0
- package/src/utils/clipboardImage.js +358 -0
- package/src/utils/clipboardImage.ts +420 -0
- package/src/utils/debugLog.ts +50 -0
- package/src/utils/greenRoot.ts +35 -0
- package/src/utils/packageRoot.ts +40 -0
- package/src/utils/pathContains.js +28 -0
- package/src/utils/pathContains.ts +31 -0
- package/src/utils/resourcePaths.ts +71 -0
- package/src/utils/stripThink.ts +49 -0
- package/src/utils/zodToJson.ts +73 -0
- package/src/web/browserBridge.js +365 -66
- package/src/web/browserBridge.ts +672 -0
- package/src/web/protocol.ts +1804 -0
- package/vendor/computer-use/arm64-darwin/cu-helperd +0 -0
- package/workflows/ppt.yaml +13 -2
- package/workflows/wechat-reading-shots.yaml +315 -0
- package/workflows/word.yaml +11 -1
- package/skills/docx/scripts/office/helpers/__pycache__/__init__.cpython-314.pyc +0 -0
- package/skills/docx/scripts/office/helpers/__pycache__/merge_runs.cpython-314.pyc +0 -0
- package/skills/docx/scripts/office/helpers/__pycache__/simplify_redlines.cpython-314.pyc +0 -0
- package/skills/docx/scripts/office/validators/__pycache__/__init__.cpython-314.pyc +0 -0
- package/skills/docx/scripts/office/validators/__pycache__/base.cpython-314.pyc +0 -0
- package/skills/docx/scripts/office/validators/__pycache__/docx.cpython-314.pyc +0 -0
- package/skills/docx/scripts/office/validators/__pycache__/pptx.cpython-314.pyc +0 -0
- package/skills/docx/scripts/office/validators/__pycache__/redlining.cpython-314.pyc +0 -0
- package/skills/image-stats/scripts/__pycache__/image_stats.cpython-314.pyc +0 -0
- package/skills/image-stats/scripts/__pycache__/image_stats_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/assemble_material.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/assemble_material_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/chart_sanity_check.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/check_language_policy.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/check_ui_artifact.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/check_ui_artifact_pptx_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/deck_pages_check.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/deck_pages_check_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/deck_visual_style_check.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/deck_visual_style_check_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/design_budget_check.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/design_budget_check_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/design_repair_hints.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/design_repair_hints_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/design_style_check.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/design_style_check_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/draft_coverage_check.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/extract_source_assets.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/extract_source_assets_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/generated_image_review_collect.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/generated_image_review_collect_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/image_plan_lint.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/image_plan_lint_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/image_prompt_rewrite.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/image_prompt_rewrite_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/layout_balance.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/layout_balance_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/normalize_design_json.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/normalize_design_json_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/plan_source_layout.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/plan_source_layout_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/resolve_source_hint.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/resolve_source_hint_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/resolve_source_input.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/resolve_source_input_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/scaffold_ui_artifact.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/source_usage_check.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/source_usage_check_test.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/text_readability_pixel.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/visual_quality_gate.cpython-314.pyc +0 -0
- package/skills/ui-design/scripts/__pycache__/visual_quality_gate_test.cpython-314.pyc +0 -0
- /package/skills/ai-image/scripts/{generate_test.mjs → generate.selftest.mjs} +0 -0
- /package/skills/ai-image/scripts/{generate_from_plan_test.mjs → generate_from_plan.selftest.mjs} +0 -0
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { rm } from 'node:fs/promises';
|
|
3
|
+
import { configService } from '../config/configService.js';
|
|
4
|
+
import { resolveAgentBrowserPath } from '../tools/webbrowser/agentBrowserPath.js';
|
|
5
|
+
import { browserProfilePath, normalizeUrl, sessionArgs, spawnRunner } from '../tools/webbrowser/browser.js';
|
|
6
|
+
import { ensureDaemon, stopDaemon } from '../tools/webbrowser/browserDaemon.js';
|
|
7
|
+
import { browserService } from '../tools/webbrowser/browserService.js';
|
|
8
|
+
import { browserRunRegistry, type BrowserRunContext } from '../tools/webbrowser/browserRunRegistry.js';
|
|
9
|
+
import {
|
|
10
|
+
createConversationTab,
|
|
11
|
+
ensureConversationTab,
|
|
12
|
+
listBrowserTabs,
|
|
13
|
+
} from '../tools/webbrowser/browserTabs.js';
|
|
14
|
+
import {
|
|
15
|
+
getBrowserStream,
|
|
16
|
+
type BrowserStreamEvent,
|
|
17
|
+
type PickedBrowserElement,
|
|
18
|
+
} from '../tools/webbrowser/browserStream.js';
|
|
19
|
+
import type { ServerFrame } from './protocol.js';
|
|
20
|
+
import type { HumanInterventionEvent } from '../humanIntervention.js';
|
|
21
|
+
|
|
22
|
+
type BrowserClientFrame = {
|
|
23
|
+
t: string;
|
|
24
|
+
requestId?: string;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type BrowserBridge = {
|
|
29
|
+
handle(frame: BrowserClientFrame): Promise<void>;
|
|
30
|
+
dispose(): void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function requireAllowedDeveloperOrigin(url: string, allowedOrigins: string[]): string {
|
|
34
|
+
let origin: string;
|
|
35
|
+
try { origin = new URL(url).origin; } catch { throw new Error('当前页面没有可授权的 HTTP(S) 站点'); }
|
|
36
|
+
if (!/^https?:\/\//i.test(origin) || !allowedOrigins.includes(origin)) {
|
|
37
|
+
throw new Error(`Developer Mode 尚未授权当前站点:${origin}。请在“设置 → 浏览器”添加该站点。`);
|
|
38
|
+
}
|
|
39
|
+
return origin;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parseJsonOutput(text: string): unknown {
|
|
43
|
+
const trimmed = text.trim();
|
|
44
|
+
if (!trimmed) return undefined;
|
|
45
|
+
try {
|
|
46
|
+
const value = JSON.parse(trimmed) as unknown;
|
|
47
|
+
if (typeof value === 'string') {
|
|
48
|
+
try { return JSON.parse(value) as unknown; } catch { return value; }
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
} catch { return trimmed; }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function pickScript(x: number, y: number): string {
|
|
55
|
+
return `(() => {
|
|
56
|
+
const el = document.elementFromPoint(${Math.round(x)}, ${Math.round(y)});
|
|
57
|
+
if (!el) return null;
|
|
58
|
+
const r = el.getBoundingClientRect();
|
|
59
|
+
const attrs = {};
|
|
60
|
+
for (const a of Array.from(el.attributes || []).slice(0, 16)) attrs[a.name] = String(a.value).slice(0, 300);
|
|
61
|
+
const escape = (v) => CSS.escape(String(v));
|
|
62
|
+
let selector = el.id ? '#' + escape(el.id) : el.tagName.toLowerCase();
|
|
63
|
+
if (!el.id && el.classList && el.classList.length) selector += '.' + Array.from(el.classList).slice(0, 2).map(escape).join('.');
|
|
64
|
+
if (!el.id) {
|
|
65
|
+
const same = Array.from(document.querySelectorAll(selector));
|
|
66
|
+
if (same.length > 1) selector += ':nth-of-type(' + (Array.from(el.parentElement?.children || []).filter((n) => n.tagName === el.tagName).indexOf(el) + 1) + ')';
|
|
67
|
+
}
|
|
68
|
+
const role = el.getAttribute('role') || ({A:'link',BUTTON:'button',INPUT:'textbox',SELECT:'combobox',TEXTAREA:'textbox',IMG:'img'}[el.tagName] || undefined);
|
|
69
|
+
const accessibleName = el.getAttribute('aria-label') || el.getAttribute('alt') || el.getAttribute('title') || String(el.innerText || el.textContent || '').trim().slice(0, 160);
|
|
70
|
+
return { ref: selector, tag: el.tagName.toLowerCase(), role, accessibleName, text: String(el.innerText || el.textContent || '').trim().slice(0, 1000), boundingBox: { x: r.x, y: r.y, width: r.width, height: r.height }, attributes: attrs };
|
|
71
|
+
})()`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function mapStreamEvent(event: BrowserStreamEvent): ServerFrame | null {
|
|
75
|
+
switch (event.type) {
|
|
76
|
+
case 'frame':
|
|
77
|
+
return { t: 'browser/frame', data: String(event.data ?? ''), metadata: event.metadata as Record<string, unknown> | undefined,
|
|
78
|
+
frameWidth: Number(event.frameWidth ?? event.width ?? (event.metadata as Record<string, unknown> | undefined)?.width ?? 0) || undefined,
|
|
79
|
+
frameHeight: Number(event.frameHeight ?? event.height ?? (event.metadata as Record<string, unknown> | undefined)?.height ?? 0) || undefined,
|
|
80
|
+
viewportWidth: Number(event.viewportWidth ?? 0) || undefined,
|
|
81
|
+
viewportHeight: Number(event.viewportHeight ?? 0) || undefined,
|
|
82
|
+
deviceScaleFactor: Number(event.deviceScaleFactor ?? 1) || 1 };
|
|
83
|
+
case 'status':
|
|
84
|
+
return {
|
|
85
|
+
t: 'browser/status',
|
|
86
|
+
connected: Boolean(event.connected),
|
|
87
|
+
screencasting: Boolean(event.screencasting ?? event.screencast),
|
|
88
|
+
viewportWidth: Number(event.viewportWidth ?? event.width ?? 1280),
|
|
89
|
+
viewportHeight: Number(event.viewportHeight ?? event.height ?? 720),
|
|
90
|
+
engine: 'agent-browser',
|
|
91
|
+
};
|
|
92
|
+
case 'tabs': {
|
|
93
|
+
const tabs = Array.isArray(event.tabs) ? event.tabs : [];
|
|
94
|
+
return { t: 'browser/tabs', tabs: tabs.map((tab) => {
|
|
95
|
+
const item = tab as Record<string, unknown>;
|
|
96
|
+
return { id: String(item.tabId ?? item.id ?? ''), url: String(item.url ?? ''), title: typeof item.title === 'string' ? item.title : undefined, active: Boolean(item.active), favicon: typeof item.favicon === 'string' ? item.favicon : undefined };
|
|
97
|
+
}) };
|
|
98
|
+
}
|
|
99
|
+
case 'agent_candidates': {
|
|
100
|
+
const list = Array.isArray(event.candidates) ? event.candidates : [];
|
|
101
|
+
return {
|
|
102
|
+
t: 'browser/agent/candidates',
|
|
103
|
+
candidates: list.map((item) => {
|
|
104
|
+
const c = item as Record<string, unknown>;
|
|
105
|
+
return { ref: String(c.ref ?? ''), line: String(c.line ?? '') };
|
|
106
|
+
}),
|
|
107
|
+
...(typeof event.selector === 'string' ? { selector: event.selector } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
case 'agent_outcome': return {
|
|
111
|
+
t: 'browser/agent/outcome',
|
|
112
|
+
action: String(event.action ?? ''),
|
|
113
|
+
effect: event.effect === 'unknown' ? 'unknown' : 'unchanged',
|
|
114
|
+
...(typeof event.selector === 'string' ? { selector: event.selector } : {}),
|
|
115
|
+
...(typeof event.maskedLines === 'number' ? { maskedLines: event.maskedLines } : {}),
|
|
116
|
+
};
|
|
117
|
+
case 'url': return { t: 'browser/url', url: String(event.url ?? ''), tabId: typeof event.tabId === 'string' ? event.tabId : undefined };
|
|
118
|
+
case 'title': return { t: 'browser/title', title: String(event.title ?? ''), tabId: typeof event.tabId === 'string' ? event.tabId : undefined };
|
|
119
|
+
case 'navigation': return { t: 'browser/navigation', url: String(event.url ?? ''), state: (event.state === 'start' || event.state === 'commit' || event.state === 'domcontentloaded') ? event.state : 'load' };
|
|
120
|
+
case 'console': return { t: 'browser/console', level: typeof event.level === 'string' ? event.level : undefined, text: String(event.text ?? '') };
|
|
121
|
+
case 'page_error': return { t: 'browser/page_error', text: String(event.text ?? event.message ?? '') };
|
|
122
|
+
case 'dialog': return { t: 'browser/dialog', type: (event.dialogType ?? event.kind ?? 'alert') as 'alert' | 'confirm' | 'prompt' | 'beforeunload', message: String(event.message ?? ''), defaultValue: typeof event.defaultValue === 'string' ? event.defaultValue : undefined };
|
|
123
|
+
case 'error': return { t: 'browser/error', message: String(event.message ?? '浏览器流错误') };
|
|
124
|
+
case 'mode': return { t: 'browser/mode', mode: event.mode as 'browse' | 'assist' | 'auto' };
|
|
125
|
+
case 'marked_elements': return { t: 'browser/marked/elements', elements: (event.elements ?? []) as never };
|
|
126
|
+
case 'agent_acting': return { t: 'browser/agent/acting', acting: Boolean(event.acting), action: typeof event.action === 'string' ? event.action : undefined, target: typeof event.target === 'string' ? event.target : undefined };
|
|
127
|
+
case 'agent_pointer': return { t: 'browser/agent/pointer', x: Number(event.x ?? 0), y: Number(event.y ?? 0), visible: Boolean(event.visible), action: (typeof event.action === 'string' ? event.action : undefined) as 'move' | 'click' | 'type' | 'scroll' | 'idle' | 'hover' | undefined, elementRef: typeof event.elementRef === 'string' ? event.elementRef : undefined };
|
|
128
|
+
case 'control_state': return { t: 'browser/control/changed', state: event.state as never };
|
|
129
|
+
default: return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function mapHumanInterventionEvent(event: HumanInterventionEvent): Record<string, unknown> {
|
|
134
|
+
if (event.type === 'started') {
|
|
135
|
+
return {
|
|
136
|
+
t: 'human-intervention/started',
|
|
137
|
+
intervention: event.intervention,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
t: 'human-intervention/resolved',
|
|
142
|
+
resolution: event.resolution,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// —— 用户指针输入的双通路路由 ————————————————————————————————————————————
|
|
147
|
+
// agent-browser daemon 有两条鼠标输入通路(子模块只读,二进制来自 GitHub releases):
|
|
148
|
+
// · stream `input_mouse`(native/stream/websocket.rs)——裸转发 CDP dispatchMouseEvent、
|
|
149
|
+
// **无状态、且不带 `buttons`**(当前按住的键位掩码)。够 hover / 单击 / 滚轮;但「按住拖拽」
|
|
150
|
+
// 必须靠 mouseMoved 上的 buttons=1 表明左键仍按住,缺了它 Chrome 只当悬停 → 拖不动
|
|
151
|
+
// (滑块/旋转类验证码就卡在这,用户「鼠标动不了」的真因)。
|
|
152
|
+
// · 命令 `mouse move|down|up`(native/actions.rs handle_mouse*)——走 build_mouse_event_params
|
|
153
|
+
// (&mut mouse_state),**有状态**、按已按下键位正确带 `buttons`,能真正拖拽。
|
|
154
|
+
// 故本桥接层(可写侧,符合「agent-browser 只读、修复落 minimal-agent」)做通路分流:
|
|
155
|
+
// hover / 单击 / 滚轮仍走快的 input_mouse;一旦识别为拖拽手势(按下后发生移动),整段
|
|
156
|
+
// down→move→up 改走命令通路让 buttons 被 daemon 跟踪。命令通路要 spawn CLI、逐帧太慢,故
|
|
157
|
+
// 拖拽中间帧按节流下发、并用串行队列保序,末帧落到抬起前的真实坐标。
|
|
158
|
+
export interface PointerRouteState {
|
|
159
|
+
dragging: boolean;
|
|
160
|
+
dragButton: string;
|
|
161
|
+
/** 已按下但尚未判定单击/拖拽的按下帧(移动到来=拖拽;直接抬起=单击)。 */
|
|
162
|
+
pendingPress: BrowserClientFrame | null;
|
|
163
|
+
lastDragMoveAt: number;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function createPointerRouteState(): PointerRouteState {
|
|
167
|
+
return { dragging: false, dragButton: 'left', pendingPress: null, lastDragMoveAt: 0 };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface PointerRoutePlan {
|
|
171
|
+
/** 走快通路 input_mouse 立即下发的帧。 */
|
|
172
|
+
stream: Array<Record<string, unknown>>;
|
|
173
|
+
/** 走 daemon 有状态命令通路、按序 enqueue 的 `mouse ...` argv。 */
|
|
174
|
+
commands: string[][];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 纯函数:按持久路由状态决定一个用户鼠标帧怎么走。就地更新 state,返回待下发的快通路帧
|
|
179
|
+
* 与待入队的命令通路 argv。抽成纯函数便于单测拖拽判定/单击缓冲/节流,不碰真实 spawn。
|
|
180
|
+
*/
|
|
181
|
+
export function routeUserMouse(
|
|
182
|
+
state: PointerRouteState,
|
|
183
|
+
frame: BrowserClientFrame,
|
|
184
|
+
now: number,
|
|
185
|
+
dragMoveThrottleMs = 40,
|
|
186
|
+
): PointerRoutePlan {
|
|
187
|
+
const plan: PointerRoutePlan = { stream: [], commands: [] };
|
|
188
|
+
const eventType = String(frame.eventType ?? '');
|
|
189
|
+
const x = Math.round(Number(frame.x ?? 0));
|
|
190
|
+
const y = Math.round(Number(frame.y ?? 0));
|
|
191
|
+
const fast = (f: BrowserClientFrame = frame) => plan.stream.push({ type: 'input_mouse', ...f, t: undefined });
|
|
192
|
+
|
|
193
|
+
if (eventType === 'mousePressed') {
|
|
194
|
+
// 缓冲按下:还不知道是单击还是拖拽——先不派发,等下一帧揭晓。
|
|
195
|
+
state.pendingPress = frame;
|
|
196
|
+
state.dragging = false;
|
|
197
|
+
return plan;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (eventType === 'mouseMoved') {
|
|
201
|
+
if (state.pendingPress && !state.dragging) {
|
|
202
|
+
// 按下后首次移动 = 拖拽:在命令通路上建立「按住」状态(mouse down 令 daemon 记住 buttons)。
|
|
203
|
+
const px = Math.round(Number(state.pendingPress.x ?? x));
|
|
204
|
+
const py = Math.round(Number(state.pendingPress.y ?? y));
|
|
205
|
+
state.dragButton = String(state.pendingPress.button ?? 'left') || 'left';
|
|
206
|
+
state.dragging = true;
|
|
207
|
+
state.pendingPress = null;
|
|
208
|
+
state.lastDragMoveAt = now;
|
|
209
|
+
plan.commands.push(['move', String(px), String(py)], ['down', state.dragButton], ['move', String(x), String(y)]);
|
|
210
|
+
return plan;
|
|
211
|
+
}
|
|
212
|
+
if (state.dragging) {
|
|
213
|
+
// 拖拽中间帧:节流下发命令通路(spawn 昂贵),不再走 input_mouse 避免双通路重复派发。
|
|
214
|
+
if (now - state.lastDragMoveAt >= dragMoveThrottleMs) {
|
|
215
|
+
state.lastDragMoveAt = now;
|
|
216
|
+
plan.commands.push(['move', String(x), String(y)]);
|
|
217
|
+
}
|
|
218
|
+
return plan;
|
|
219
|
+
}
|
|
220
|
+
fast(); // 普通悬停 → 快通路
|
|
221
|
+
return plan;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (eventType === 'mouseReleased') {
|
|
225
|
+
if (state.dragging) {
|
|
226
|
+
// 收尾:先补末帧到真实抬起坐标(节流可能丢了它),再抬起清 buttons。
|
|
227
|
+
plan.commands.push(['move', String(x), String(y)], ['up', state.dragButton]);
|
|
228
|
+
state.dragging = false;
|
|
229
|
+
return plan;
|
|
230
|
+
}
|
|
231
|
+
if (state.pendingPress) {
|
|
232
|
+
// 纯单击(按下后无移动):快通路补发缓冲的按下 + 本次抬起,语义仍是一次点击。
|
|
233
|
+
fast(state.pendingPress);
|
|
234
|
+
fast();
|
|
235
|
+
state.pendingPress = null;
|
|
236
|
+
return plan;
|
|
237
|
+
}
|
|
238
|
+
fast();
|
|
239
|
+
return plan;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// mouseWheel / 其它鼠标事件 → 快通路(不涉及按住拖拽)。
|
|
243
|
+
fast();
|
|
244
|
+
return plan;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function createBrowserBridge(send: (frame: ServerFrame) => void, sessionName = 'minimal-agent'): BrowserBridge {
|
|
248
|
+
let disposed = false;
|
|
249
|
+
let activeTabId = '';
|
|
250
|
+
let pageRevision = randomUUID();
|
|
251
|
+
let runContext: BrowserRunContext | null = null;
|
|
252
|
+
const stream = getBrowserStream(sessionName);
|
|
253
|
+
const unsubscribe = stream.subscribe((event) => {
|
|
254
|
+
if (disposed) return;
|
|
255
|
+
if (event.type === 'control_state') {
|
|
256
|
+
const control = event.state as import('../tools/webbrowser/browserControl.js').BrowserControlState | undefined;
|
|
257
|
+
if (control && control.status !== 'idle' && control.conversationKey) {
|
|
258
|
+
runContext = browserRunRegistry.find(control.conversationKey, control.runId)
|
|
259
|
+
?? browserRunRegistry.ensure({
|
|
260
|
+
conversationKey: control.conversationKey,
|
|
261
|
+
runId: control.runId,
|
|
262
|
+
profileId: 'default',
|
|
263
|
+
grants: ['observe', 'interact'],
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (event.type === 'human_intervention_started' || event.type === 'human_intervention_resolved') {
|
|
268
|
+
const interventionEvent = event.event as HumanInterventionEvent | undefined;
|
|
269
|
+
if (interventionEvent) send(mapHumanInterventionEvent(interventionEvent) as unknown as ServerFrame);
|
|
270
|
+
}
|
|
271
|
+
if (event.type === 'url' || event.type === 'navigation') pageRevision = randomUUID();
|
|
272
|
+
if (event.type === 'tabs' && Array.isArray(event.tabs)) {
|
|
273
|
+
const active = event.tabs.find((tab) => Boolean((tab as Record<string, unknown>).active)) as Record<string, unknown> | undefined;
|
|
274
|
+
activeTabId = String(active?.tabId ?? active?.id ?? activeTabId);
|
|
275
|
+
}
|
|
276
|
+
let mapped = mapStreamEvent(event);
|
|
277
|
+
const activeOwner = activeTabId ? browserRunRegistry.ownerOf(activeTabId) : undefined;
|
|
278
|
+
if (runContext && activeTabId && activeOwner !== runContext.conversationKey
|
|
279
|
+
&& mapped && ['browser/frame', 'browser/url', 'browser/title', 'browser/navigation'].includes(mapped.t)) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (runContext && mapped?.t === 'browser/tabs') {
|
|
283
|
+
const claimed = new Set(browserRunRegistry.claimedTabs(runContext.conversationKey).map(String));
|
|
284
|
+
mapped = { ...mapped, tabs: mapped.tabs.filter((tab) => claimed.has(tab.id)) };
|
|
285
|
+
}
|
|
286
|
+
if (mapped?.t === 'browser/frame') send({
|
|
287
|
+
...mapped,
|
|
288
|
+
tabId: activeTabId || mapped.tabId,
|
|
289
|
+
pageRevision,
|
|
290
|
+
...(runContext ? { conversationKey: runContext.conversationKey, runId: runContext.runId } : {}),
|
|
291
|
+
});
|
|
292
|
+
else if (mapped) send(mapped);
|
|
293
|
+
if (event.type === 'control_state') {
|
|
294
|
+
const state = event.state as import('../tools/webbrowser/browserControl.js').BrowserControlState | undefined;
|
|
295
|
+
// 控制权移交给用户(手动接管或验证挑战)时淡出 AI 指针,避免“谁在操作”的混淆。
|
|
296
|
+
if (state?.owner === 'user') stream.setAgentPointer({ x: 0, y: 0, visible: false });
|
|
297
|
+
if (state?.status === 'waiting_human' && state.reason !== 'manual') {
|
|
298
|
+
const summary = state.reason === 'captcha' ? '页面需要人工完成安全验证'
|
|
299
|
+
: state.reason === 'otp' ? '页面需要人工输入一次性验证码'
|
|
300
|
+
: state.reason === 'password' ? '页面需要人工输入密码'
|
|
301
|
+
: state.reason === 'qr_login' ? '页面需要人工扫码登录'
|
|
302
|
+
: state.reason === 'login' ? '页面需要人工登录'
|
|
303
|
+
: state.reason === 'permission' ? '页面需要人工确认权限'
|
|
304
|
+
: state.reason === 'security_warning' ? '页面需要人工处理安全警告'
|
|
305
|
+
: '网站已暂停或拦截当前访问';
|
|
306
|
+
send({
|
|
307
|
+
t: 'browser/challenge/detected',
|
|
308
|
+
reason: state.reason,
|
|
309
|
+
summary,
|
|
310
|
+
resumeToken: state.resumeToken,
|
|
311
|
+
runId: state.runId,
|
|
312
|
+
...(state.conversationKey ? { conversationKey: state.conversationKey } : {}),
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
const run = async (args: string[]) => {
|
|
319
|
+
const bin = await resolveAgentBrowserPath();
|
|
320
|
+
if (!bin) throw new Error('未找到内置 agent-browser');
|
|
321
|
+
await ensureDaemon(sessionName);
|
|
322
|
+
const result = await browserService.run({ sessionName, bin, args: [...sessionArgs(sessionName), ...args], runner: spawnRunner });
|
|
323
|
+
if (result.code !== 0) throw new Error((result.stderr || result.stdout || `exit ${result.code}`).trim());
|
|
324
|
+
return result.stdout.trim();
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const requireContext = (): BrowserRunContext => {
|
|
328
|
+
if (!runContext) throw new Error('浏览器尚未绑定当前会话,请重新打开工作台');
|
|
329
|
+
return runContext;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const claimContextTab = async (): Promise<string> => {
|
|
333
|
+
const context = requireContext();
|
|
334
|
+
const bin = await resolveAgentBrowserPath();
|
|
335
|
+
if (!bin) throw new Error('未找到内置 agent-browser');
|
|
336
|
+
await ensureDaemon(sessionName);
|
|
337
|
+
const tabId = await ensureConversationTab({
|
|
338
|
+
context,
|
|
339
|
+
sessionName,
|
|
340
|
+
bin,
|
|
341
|
+
globals: sessionArgs(sessionName),
|
|
342
|
+
runner: spawnRunner,
|
|
343
|
+
});
|
|
344
|
+
activeTabId = tabId;
|
|
345
|
+
return tabId;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const createContextTab = async (url = 'about:blank'): Promise<string> => {
|
|
349
|
+
const context = requireContext();
|
|
350
|
+
const bin = await resolveAgentBrowserPath();
|
|
351
|
+
if (!bin) throw new Error('未找到内置 agent-browser');
|
|
352
|
+
await ensureDaemon(sessionName);
|
|
353
|
+
const tabId = await createConversationTab({
|
|
354
|
+
context,
|
|
355
|
+
sessionName,
|
|
356
|
+
bin,
|
|
357
|
+
globals: sessionArgs(sessionName),
|
|
358
|
+
runner: spawnRunner,
|
|
359
|
+
}, url);
|
|
360
|
+
activeTabId = tabId;
|
|
361
|
+
return tabId;
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const runOnContextTab = async (args: string[]): Promise<string> => {
|
|
365
|
+
const tabId = await claimContextTab();
|
|
366
|
+
const bin = await resolveAgentBrowserPath();
|
|
367
|
+
if (!bin) throw new Error('未找到内置 agent-browser');
|
|
368
|
+
const output = await browserService.exclusive({
|
|
369
|
+
sessionName,
|
|
370
|
+
bin,
|
|
371
|
+
runner: spawnRunner,
|
|
372
|
+
}, async (execute) => {
|
|
373
|
+
const switched = await execute([...sessionArgs(sessionName), 'tab', tabId]);
|
|
374
|
+
if (switched.code !== 0) return switched;
|
|
375
|
+
return execute([...sessionArgs(sessionName), ...args]);
|
|
376
|
+
});
|
|
377
|
+
if (output.code !== 0) {
|
|
378
|
+
throw new Error((output.stderr || output.stdout || `exit ${output.code}`).trim());
|
|
379
|
+
}
|
|
380
|
+
return output.stdout.trim();
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const ensureStarted = async () => {
|
|
384
|
+
const config = await configService.browser();
|
|
385
|
+
if (!config.enabled) throw new Error('内置浏览器已在设置中关闭');
|
|
386
|
+
const info = await ensureDaemon(sessionName);
|
|
387
|
+
await stream.connect();
|
|
388
|
+
return info;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
// 改视口后重连查看者,让下一帧按新尺寸编码(screencast 尺寸绑定在查看者连接时刻)。
|
|
392
|
+
// 注意:标签切换/新建/关闭/导航不需要在这里重连——agent-browser daemon 在这些命令后会自己
|
|
393
|
+
// set_cdp_session_id + notify、让 screencast 重定向到新活动标签(见 actions.rs / stream/cdp_loop.rs)。
|
|
394
|
+
// 真正的坑是「链接点击开的新标签」不在 daemon 的同步清单里,那条路由前端「活动标签变化 → 补发 tab/switch」兜住(useBrowser.ts)。
|
|
395
|
+
const reconnectViewer = async () => {
|
|
396
|
+
if (stream.isConnected()) {
|
|
397
|
+
stream.disconnect();
|
|
398
|
+
await stream.connect();
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const applyViewport = async (widthValue: unknown, heightValue: unknown) => {
|
|
403
|
+
const width = Math.max(320, Math.round(Number(widthValue)));
|
|
404
|
+
const height = Math.max(240, Math.round(Number(heightValue)));
|
|
405
|
+
if (!Number.isFinite(width) || !Number.isFinite(height)) throw new Error('无效的浏览器视口尺寸');
|
|
406
|
+
await runOnContextTab(['set', 'viewport', String(width), String(height)]);
|
|
407
|
+
await reconnectViewer();
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const pick = async (x: number, y: number): Promise<PickedBrowserElement | null> => {
|
|
411
|
+
const value = parseJsonOutput(await runOnContextTab(['eval', pickScript(x, y)]));
|
|
412
|
+
if (!value || typeof value !== 'object') return null;
|
|
413
|
+
return value as PickedBrowserElement;
|
|
414
|
+
};
|
|
415
|
+
stream.setPicker(pick);
|
|
416
|
+
|
|
417
|
+
// 用户指针输入的双通路路由状态 + 命令通路串行队列(见 routeUserMouse 头注)。
|
|
418
|
+
const pointerRoute = createPointerRouteState();
|
|
419
|
+
let mouseCmdChain: Promise<unknown> = Promise.resolve();
|
|
420
|
+
const enqueueMouseCmd = (args: string[]): void => {
|
|
421
|
+
// 串行保序:逐条 await 上一条再跑下一条;失败吞掉(拖拽单帧丢失不该中断整段手势)。
|
|
422
|
+
mouseCmdChain = mouseCmdChain.then(() => runOnContextTab(['mouse', ...args])).catch(() => {});
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
const result = (frame: BrowserClientFrame, ok: boolean, data?: unknown, error?: string) => send({
|
|
426
|
+
t: 'browser/result', requestId: frame.requestId, action: frame.t, ok,
|
|
427
|
+
...(runContext ? { conversationKey: runContext.conversationKey, runId: runContext.runId, tabId: activeTabId || undefined } : {}),
|
|
428
|
+
...(typeof data === 'string' ? { content: data } : data === undefined ? {} : { data }),
|
|
429
|
+
...(error ? { error } : {}),
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
return {
|
|
433
|
+
async handle(frame: BrowserClientFrame) {
|
|
434
|
+
if (disposed) return;
|
|
435
|
+
try {
|
|
436
|
+
switch (frame.t) {
|
|
437
|
+
case 'browser/context/set': {
|
|
438
|
+
const conversationKey = String(frame.conversationKey ?? '').trim();
|
|
439
|
+
if (!conversationKey) throw new Error('缺少浏览器会话标识');
|
|
440
|
+
runContext = browserRunRegistry.ensure({
|
|
441
|
+
conversationKey,
|
|
442
|
+
runId: typeof frame.runId === 'string' ? frame.runId : 'workbench',
|
|
443
|
+
profileId: 'default',
|
|
444
|
+
grants: ['observe', 'interact'],
|
|
445
|
+
});
|
|
446
|
+
const tabId = await claimContextTab();
|
|
447
|
+
send({
|
|
448
|
+
t: 'browser/context/changed',
|
|
449
|
+
requestId: frame.requestId,
|
|
450
|
+
conversationKey: runContext.conversationKey,
|
|
451
|
+
runId: runContext.runId,
|
|
452
|
+
tabId,
|
|
453
|
+
});
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
case 'browser/start': {
|
|
457
|
+
requireContext();
|
|
458
|
+
const info = await ensureStarted();
|
|
459
|
+
await claimContextTab();
|
|
460
|
+
const viewport = frame.viewport as { width?: number; height?: number } | undefined;
|
|
461
|
+
if (viewport?.width && viewport?.height) await applyViewport(viewport.width, viewport.height);
|
|
462
|
+
send({
|
|
463
|
+
t: 'browser/started',
|
|
464
|
+
requestId: frame.requestId,
|
|
465
|
+
streamPort: info.streamPort,
|
|
466
|
+
pid: info.pid,
|
|
467
|
+
sessionId: sessionName,
|
|
468
|
+
conversationKey: runContext?.conversationKey,
|
|
469
|
+
runId: runContext?.runId,
|
|
470
|
+
});
|
|
471
|
+
result(frame, true, info);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
case 'browser/stop':
|
|
475
|
+
stream.disconnect(); await stopDaemon(sessionName); send({ t: 'browser/stopped', requestId: frame.requestId }); return;
|
|
476
|
+
case 'browser/profile/clear':
|
|
477
|
+
stream.disconnect(); await stopDaemon(sessionName); await rm(browserProfilePath('default'), { recursive: true, force: true });
|
|
478
|
+
send({ t: 'browser/stopped', requestId: frame.requestId }); result(frame, true); return;
|
|
479
|
+
case 'browser/mode':
|
|
480
|
+
stream.setMode(frame.mode as 'browse' | 'assist' | 'auto'); result(frame, true); return;
|
|
481
|
+
case 'browser/control/get':
|
|
482
|
+
requireContext();
|
|
483
|
+
send({ t: 'browser/control/changed', state: stream.getControlState() }); return;
|
|
484
|
+
case 'browser/control/takeover':
|
|
485
|
+
stream.requestHuman('manual', {
|
|
486
|
+
runId: runContext?.runId,
|
|
487
|
+
conversationKey: runContext?.conversationKey,
|
|
488
|
+
ownerId: 'local-owner',
|
|
489
|
+
});
|
|
490
|
+
result(frame, true, stream.getControlState()); return;
|
|
491
|
+
case 'browser/control/resume': {
|
|
492
|
+
const ok = stream.resumeControl(typeof frame.resumeToken === 'string' ? frame.resumeToken : undefined);
|
|
493
|
+
result(frame, ok, stream.getControlState(), ok ? undefined : '浏览器接管请求已失效'); return;
|
|
494
|
+
}
|
|
495
|
+
case 'browser/control/stop': {
|
|
496
|
+
const ok = stream.stopControl(typeof frame.resumeToken === 'string' ? frame.resumeToken : undefined);
|
|
497
|
+
result(frame, ok, stream.getControlState(), ok ? undefined : '浏览器接管请求已失效'); return;
|
|
498
|
+
}
|
|
499
|
+
case 'human-intervention/resume': {
|
|
500
|
+
const ok = stream.resumeIntervention({
|
|
501
|
+
id: String(frame.interventionId ?? ''),
|
|
502
|
+
resumeToken: String(frame.resumeToken ?? ''),
|
|
503
|
+
ownerId: String(frame.ownerId ?? ''),
|
|
504
|
+
conversationKey: String(frame.conversationKey ?? ''),
|
|
505
|
+
runId: String(frame.runId ?? ''),
|
|
506
|
+
});
|
|
507
|
+
result(frame, ok, stream.getControlState(), ok ? undefined : '浏览器人工介入请求已失效'); return;
|
|
508
|
+
}
|
|
509
|
+
case 'human-intervention/cancel': {
|
|
510
|
+
const ok = stream.cancelIntervention({
|
|
511
|
+
id: String(frame.interventionId ?? ''),
|
|
512
|
+
resumeToken: String(frame.resumeToken ?? ''),
|
|
513
|
+
ownerId: String(frame.ownerId ?? ''),
|
|
514
|
+
conversationKey: String(frame.conversationKey ?? ''),
|
|
515
|
+
runId: String(frame.runId ?? ''),
|
|
516
|
+
});
|
|
517
|
+
result(frame, ok, stream.getControlState(), ok ? undefined : '浏览器人工介入请求已失效'); return;
|
|
518
|
+
}
|
|
519
|
+
case 'browser/user/input': {
|
|
520
|
+
requireContext();
|
|
521
|
+
await claimContextTab();
|
|
522
|
+
const type = String(frame.eventType ?? '');
|
|
523
|
+
if (!type.startsWith('mouse')) { stream.send({ type: 'input_keyboard', ...frame, t: undefined }); return; }
|
|
524
|
+
// 鼠标:hover/单击/滚轮走快通路 input_mouse;拖拽手势改走 daemon 带 buttons 的命令通路。
|
|
525
|
+
const plan = routeUserMouse(pointerRoute, frame, Date.now());
|
|
526
|
+
for (const f of plan.stream) stream.send(f);
|
|
527
|
+
for (const args of plan.commands) enqueueMouseCmd(args);
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
case 'browser/navigate': {
|
|
531
|
+
requireContext();
|
|
532
|
+
await ensureStarted();
|
|
533
|
+
const url = normalizeUrl(String(frame.url ?? ''));
|
|
534
|
+
if (frame.newTab) await createContextTab(url);
|
|
535
|
+
else {
|
|
536
|
+
await claimContextTab();
|
|
537
|
+
await run(['open', url]);
|
|
538
|
+
}
|
|
539
|
+
stream.setCurrentUrl(url); result(frame, true); return;
|
|
540
|
+
}
|
|
541
|
+
case 'browser/back': await runOnContextTab(['back']); result(frame, true); return;
|
|
542
|
+
case 'browser/forward': await runOnContextTab(['forward']); result(frame, true); return;
|
|
543
|
+
case 'browser/reload': await runOnContextTab(['reload']); result(frame, true); return;
|
|
544
|
+
case 'browser/tab/new': {
|
|
545
|
+
const tabId = await createContextTab(
|
|
546
|
+
frame.url ? normalizeUrl(String(frame.url)) : 'about:blank',
|
|
547
|
+
);
|
|
548
|
+
result(frame, true, { tabId });
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
case 'browser/tab/close': {
|
|
552
|
+
const context = requireContext();
|
|
553
|
+
const tabId = String(frame.tabId ?? context.activeTabId ?? '');
|
|
554
|
+
if (!tabId) throw new Error('无效的标签标识');
|
|
555
|
+
browserRunRegistry.assertTab(context, tabId);
|
|
556
|
+
await run(['tab', 'close', tabId]);
|
|
557
|
+
browserRunRegistry.releaseTab(context.conversationKey, tabId);
|
|
558
|
+
activeTabId = context.activeTabId ?? '';
|
|
559
|
+
result(frame, true);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
case 'browser/tab/switch': {
|
|
563
|
+
const context = requireContext();
|
|
564
|
+
const tabId = String(frame.tabId ?? '');
|
|
565
|
+
if (!tabId) throw new Error('无效的标签标识');
|
|
566
|
+
browserRunRegistry.assertTab(context, tabId);
|
|
567
|
+
await run(['tab', tabId]);
|
|
568
|
+
context.activeTabId = tabId;
|
|
569
|
+
activeTabId = tabId;
|
|
570
|
+
result(frame, true);
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
case 'browser/tabs': {
|
|
574
|
+
const context = requireContext();
|
|
575
|
+
const bin = await resolveAgentBrowserPath();
|
|
576
|
+
if (!bin) throw new Error('未找到内置 agent-browser');
|
|
577
|
+
const tabs = await listBrowserTabs({
|
|
578
|
+
sessionName,
|
|
579
|
+
bin,
|
|
580
|
+
globals: sessionArgs(sessionName),
|
|
581
|
+
runner: spawnRunner,
|
|
582
|
+
});
|
|
583
|
+
const claimed = new Set(browserRunRegistry.claimedTabs(context.conversationKey));
|
|
584
|
+
result(frame, true, tabs.filter((tab) => claimed.has(tab.id)));
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
case 'browser/pick': {
|
|
588
|
+
const element = await pick(Number(frame.x), Number(frame.y));
|
|
589
|
+
if (!element) { send({ t: 'browser/picked', requestId: frame.requestId, error: '该位置没有可选元素' }); return; }
|
|
590
|
+
stream.markElement(element);
|
|
591
|
+
const item = element as PickedBrowserElement & { role?: string; accessibleName?: string };
|
|
592
|
+
send({ t: 'browser/picked', requestId: frame.requestId, reference: {
|
|
593
|
+
id: randomUUID(), url: stream.getCurrentUrl(), tabId: activeTabId, selector: item.ref,
|
|
594
|
+
tag: item.tag, role: item.role, accessibleName: item.accessibleName, text: item.text,
|
|
595
|
+
attributes: item.attributes ?? {}, boundingBox: item.boundingBox, pageRevision, capturedAt: Date.now(),
|
|
596
|
+
}, ...element }); return;
|
|
597
|
+
}
|
|
598
|
+
case 'browser/mark/element': {
|
|
599
|
+
const raw = parseJsonOutput(await runOnContextTab(['eval', `(() => { const el=document.querySelector(${JSON.stringify(String(frame.selector))}); if(!el)return null; const r=el.getBoundingClientRect(); return {ref:${JSON.stringify(String(frame.selector))},tag:el.tagName.toLowerCase(),text:String(el.innerText||el.textContent||'').trim().slice(0,1000),boundingBox:{x:r.x,y:r.y,width:r.width,height:r.height}} })()`]));
|
|
600
|
+
if (!raw || typeof raw !== 'object') throw new Error('未找到该元素');
|
|
601
|
+
stream.markElement({ ...(raw as PickedBrowserElement), label: typeof frame.label === 'string' ? frame.label : undefined, color: typeof frame.color === 'string' ? frame.color : undefined });
|
|
602
|
+
result(frame, true); return;
|
|
603
|
+
}
|
|
604
|
+
case 'browser/mark/clear': stream.clearMarkedElements(); result(frame, true); return;
|
|
605
|
+
case 'browser/viewport': await ensureStarted(); await applyViewport(frame.width, frame.height); result(frame, true); return;
|
|
606
|
+
case 'browser/dialog/accept': await runOnContextTab(frame.text ? ['dialog', 'accept', '--text', String(frame.text)] : ['dialog', 'accept']); result(frame, true); return;
|
|
607
|
+
case 'browser/dialog/dismiss': await runOnContextTab(['dialog', 'dismiss']); result(frame, true); return;
|
|
608
|
+
case 'browser/snapshot': {
|
|
609
|
+
const tree = await runOnContextTab(['snapshot']); send({ t: 'browser/snapshot', requestId: frame.requestId, tree }); return;
|
|
610
|
+
}
|
|
611
|
+
case 'browser/getContent': {
|
|
612
|
+
const text = await runOnContextTab(['get', 'text', String(frame.selector ?? 'body')]); send({ t: 'browser/content', requestId: frame.requestId, text }); return;
|
|
613
|
+
}
|
|
614
|
+
case 'browser/click': await runOnContextTab(['click', String(frame.selector)]); result(frame, true); return;
|
|
615
|
+
case 'browser/fill': await runOnContextTab(['fill', String(frame.selector), String(frame.value ?? '')]); result(frame, true); return;
|
|
616
|
+
case 'browser/type': await runOnContextTab(['type', String(frame.selector), String(frame.value ?? '')]); result(frame, true); return;
|
|
617
|
+
case 'browser/hover': await runOnContextTab(['hover', String(frame.selector)]); result(frame, true); return;
|
|
618
|
+
case 'browser/scroll': {
|
|
619
|
+
const args = frame.selector ? ['scrollintoview', String(frame.selector)] : ['scroll', String(frame.direction ?? 'down'), String(Math.abs(Number(frame.y ?? 600)))];
|
|
620
|
+
await runOnContextTab(args); result(frame, true); return;
|
|
621
|
+
}
|
|
622
|
+
case 'browser/select': await runOnContextTab(['select', String(frame.selector), ...(Array.isArray(frame.value) ? frame.value.map(String) : [String(frame.value ?? '')])]); result(frame, true); return;
|
|
623
|
+
case 'browser/press': await runOnContextTab(['press', String(frame.key)]); result(frame, true); return;
|
|
624
|
+
case 'browser/wait': await runOnContextTab(frame.selector ? ['wait', String(frame.selector)] : frame.text ? ['wait', '--text', String(frame.text)] : ['wait', String(frame.ms ?? 500)]); result(frame, true); return;
|
|
625
|
+
case 'browser/eval': result(frame, true, parseJsonOutput(await runOnContextTab(['eval', String(frame.js ?? '')]))); return;
|
|
626
|
+
case 'browser/upload': await runOnContextTab(['upload', String(frame.selector), ...(Array.isArray(frame.files) ? frame.files.map(String) : [])]); result(frame, true); return;
|
|
627
|
+
case 'browser/drag': await runOnContextTab(['drag', String(frame.fromSelector), String(frame.toSelector)]); result(frame, true); return;
|
|
628
|
+
case 'browser/screenshot': {
|
|
629
|
+
const args = ['screenshot', ...(frame.path ? [String(frame.path)] : []), ...(frame.annotate ? ['--annotate'] : []), ...(frame.fullPage ? ['--full'] : [])];
|
|
630
|
+
const output = await runOnContextTab(args); send({ t: 'browser/screenshot', requestId: frame.requestId, path: typeof frame.path === 'string' ? frame.path : undefined, data: output }); return;
|
|
631
|
+
}
|
|
632
|
+
case 'browser/pdf': { const output = await runOnContextTab(['pdf', String(frame.path ?? '')]); result(frame, true, output); return; }
|
|
633
|
+
case 'browser/cookies/get': send({ t: 'browser/cookies', requestId: frame.requestId, cookies: (parseJsonOutput(await runOnContextTab(['cookies', 'get'])) as never[]) ?? [] }); return;
|
|
634
|
+
case 'browser/cookies/clear': await runOnContextTab(['cookies', 'clear']); result(frame, true); return;
|
|
635
|
+
case 'browser/device': await runOnContextTab(frame.preset ? ['set', 'device', String(frame.preset)] : ['set', 'viewport', String(frame.width), String(frame.height)]); result(frame, true); return;
|
|
636
|
+
case 'browser/media': await runOnContextTab(['set', 'media', String(frame.scheme ?? 'no-preference')]); result(frame, true); return;
|
|
637
|
+
case 'browser/record/start': await runOnContextTab(['record', 'start', String(frame.path ?? '')]); result(frame, true); return;
|
|
638
|
+
case 'browser/record/stop': result(frame, true, await runOnContextTab(['record', 'stop'])); return;
|
|
639
|
+
case 'browser/debug/console':
|
|
640
|
+
case 'browser/debug/errors':
|
|
641
|
+
case 'browser/debug/network':
|
|
642
|
+
case 'browser/debug/styles':
|
|
643
|
+
case 'browser/debug/vitals':
|
|
644
|
+
case 'browser/debug/trace/start':
|
|
645
|
+
case 'browser/debug/trace/stop': {
|
|
646
|
+
await claimContextTab();
|
|
647
|
+
const browserConfig = await configService.browser();
|
|
648
|
+
if (!browserConfig.developerMode) throw new Error('请先在“设置 → 浏览器”开启完整 CDP Developer Mode');
|
|
649
|
+
requireAllowedDeveloperOrigin(stream.getCurrentUrl(), browserConfig.allowedCdpOrigins);
|
|
650
|
+
if (frame.t === 'browser/debug/console') result(frame, true, await run(['console']));
|
|
651
|
+
else if (frame.t === 'browser/debug/errors') result(frame, true, await run(['errors']));
|
|
652
|
+
else if (frame.t === 'browser/debug/network') result(frame, true, parseJsonOutput(await run(['network', 'requests', '--json'])));
|
|
653
|
+
else if (frame.t === 'browser/debug/styles') result(frame, true, parseJsonOutput(await run(['get', 'styles', String(frame.selector ?? 'body'), '--json'])));
|
|
654
|
+
else if (frame.t === 'browser/debug/vitals') result(frame, true, parseJsonOutput(await run(['vitals', ...(frame.url ? [String(frame.url)] : []), '--json'])));
|
|
655
|
+
else if (frame.t === 'browser/debug/trace/start') { await run(['trace', 'start']); result(frame, true); }
|
|
656
|
+
else result(frame, true, await run(['trace', 'stop', ...(frame.path ? [String(frame.path)] : [])]));
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
default:
|
|
660
|
+
result(frame, false, undefined, `暂不支持浏览器操作:${frame.t}`);
|
|
661
|
+
}
|
|
662
|
+
} catch (error) {
|
|
663
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
664
|
+
result(frame, false, undefined, message);
|
|
665
|
+
send({ t: 'browser/error', message });
|
|
666
|
+
}
|
|
667
|
+
},
|
|
668
|
+
dispose() { disposed = true; unsubscribe(); },
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
export type { BrowserClientFrame };
|