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,112 @@
|
|
|
1
|
+
import type { Tool, ToolAvailability, ToolCallContext } from '../types.js';
|
|
2
|
+
import type { ToolGate } from '../toolGate.js';
|
|
3
|
+
import { isMemoryToolName } from '../tools/capabilities.js';
|
|
4
|
+
|
|
5
|
+
export type AgentSurface =
|
|
6
|
+
| 'desktop'
|
|
7
|
+
| 'tui'
|
|
8
|
+
| 'cli'
|
|
9
|
+
| 'headless'
|
|
10
|
+
| 'workflow'
|
|
11
|
+
| 'subagent';
|
|
12
|
+
|
|
13
|
+
export type AgentProfile = 'main' | 'subagent-explore' | 'subagent-general';
|
|
14
|
+
export type AgentOwner = 'local-owner' | 'untrusted';
|
|
15
|
+
export type MemoryAccess = 'read-write' | 'read-only' | 'denied';
|
|
16
|
+
export type MemoryAccessInput = MemoryAccess | 'local-owner';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Host-issued trust context for one agent run. Nothing in this object may be
|
|
20
|
+
* inferred from model output, project files, tools, or web content.
|
|
21
|
+
*/
|
|
22
|
+
export interface AgentRuntimeContext {
|
|
23
|
+
readonly surface: AgentSurface;
|
|
24
|
+
readonly profile: AgentProfile;
|
|
25
|
+
readonly cwd: string;
|
|
26
|
+
readonly workspaceId?: string;
|
|
27
|
+
readonly sessionId?: string;
|
|
28
|
+
readonly owner: AgentOwner;
|
|
29
|
+
readonly memoryAccess: MemoryAccess;
|
|
30
|
+
readonly interactiveAsk: boolean;
|
|
31
|
+
readonly browser?: ToolCallContext['browserRun'];
|
|
32
|
+
readonly computer?: ToolCallContext['computerRun'];
|
|
33
|
+
readonly computerIntervention?: ToolCallContext['computerIntervention'];
|
|
34
|
+
readonly toolGate?: ToolGate;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface AgentRuntimeContextInput
|
|
38
|
+
extends Omit<AgentRuntimeContext, 'owner' | 'memoryAccess'> {
|
|
39
|
+
owner?: AgentOwner;
|
|
40
|
+
memoryAccess?: MemoryAccessInput;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function normalizeMemoryAccess(
|
|
44
|
+
access: MemoryAccessInput | undefined,
|
|
45
|
+
): MemoryAccess {
|
|
46
|
+
return access === 'local-owner' ? 'read-write' : access ?? 'denied';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Only a local owner can receive memory access; untrusted hosts are always denied. */
|
|
50
|
+
export function createAgentRuntimeContext(
|
|
51
|
+
input: AgentRuntimeContextInput,
|
|
52
|
+
): Readonly<AgentRuntimeContext> {
|
|
53
|
+
const owner = input.owner ?? 'untrusted';
|
|
54
|
+
const memoryAccess = owner === 'local-owner'
|
|
55
|
+
? normalizeMemoryAccess(input.memoryAccess)
|
|
56
|
+
: 'denied';
|
|
57
|
+
return Object.freeze({
|
|
58
|
+
...input,
|
|
59
|
+
owner,
|
|
60
|
+
memoryAccess,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function filterToolsForRuntime(
|
|
65
|
+
tools: readonly Tool[],
|
|
66
|
+
runtime: Pick<AgentRuntimeContext, 'surface' | 'profile' | 'cwd' | 'memoryAccess' | 'browser' | 'computer'>,
|
|
67
|
+
resolvedAvailability?: ReadonlyMap<Tool, ToolAvailability>,
|
|
68
|
+
): Tool[] {
|
|
69
|
+
return tools.filter((tool) =>
|
|
70
|
+
(resolvedAvailability?.get(tool)?.available
|
|
71
|
+
?? tool.resolveAvailability?.(runtime as AgentRuntimeContext).available
|
|
72
|
+
?? true)
|
|
73
|
+
&& (memoryToolAllowed(tool.name, runtime.memoryAccess))
|
|
74
|
+
&& (tool.name !== 'WebBrowser' || webBrowserAllowed(runtime))
|
|
75
|
+
&& (tool.name !== 'ComputerUse' || runtime.computer !== undefined));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** WebBrowser is the Desktop main Agent's visible, human-supervised automation surface. */
|
|
79
|
+
export function webBrowserAllowed(
|
|
80
|
+
runtime: Pick<AgentRuntimeContext, 'surface' | 'profile' | 'browser'>,
|
|
81
|
+
): boolean {
|
|
82
|
+
return runtime.surface === 'desktop'
|
|
83
|
+
&& runtime.profile === 'main'
|
|
84
|
+
&& runtime.browser?.supervised === true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const READ_ONLY_MEMORY_TOOLS = new Set([
|
|
88
|
+
'RecallMemory',
|
|
89
|
+
'ListMemories',
|
|
90
|
+
'ReviewMemories',
|
|
91
|
+
'SearchConversations',
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
export function memoryToolAllowed(name: string, access: MemoryAccess): boolean {
|
|
95
|
+
if (!isMemoryToolName(name)) return true;
|
|
96
|
+
if (access === 'read-write') return true;
|
|
97
|
+
return access === 'read-only' && READ_ONLY_MEMORY_TOOLS.has(name);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function isMemoryTool(name: string): boolean {
|
|
101
|
+
return isMemoryToolName(name);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function memoryContextFromRuntime(
|
|
105
|
+
runtime: AgentRuntimeContext,
|
|
106
|
+
): { access: MemoryAccess; workspaceId?: string; sessionId?: string } {
|
|
107
|
+
return {
|
|
108
|
+
access: runtime.memoryAccess,
|
|
109
|
+
...(runtime.workspaceId ? { workspaceId: runtime.workspaceId } : {}),
|
|
110
|
+
...(runtime.sessionId ? { sessionId: runtime.sessionId } : {}),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildBaseSystemPrompt, buildTransientPromptBundle, } from '../prompts/system.js';
|
|
2
2
|
import { ALL_TOOLS } from '../tools/index.js';
|
|
3
3
|
import { sessionFileFor } from '../context/sessionPath.js';
|
|
4
|
-
import {
|
|
4
|
+
import { getOrCreateSessionState, sessionStateKey, } from './sessionState.js';
|
|
5
|
+
import { hydrateSessionWorktree } from '../tools/worktree/sessionWorktree.js';
|
|
5
6
|
import { runTurn } from './runTurn.js';
|
|
6
7
|
import { withWorkingDir } from '../bootstrap/workingDir.js';
|
|
7
|
-
import { listSessionSummaries, updateSessionMetadata } from '../context/sessionRegistry.js';
|
|
8
|
+
import { listSessionSummaries, updateSessionMetadata, } from '../context/sessionRegistry.js';
|
|
8
9
|
import { workspaceIndexService } from '../context/workspaceIndex.js';
|
|
9
10
|
import { configService } from '../config/configService.js';
|
|
11
|
+
import { createPromptCacheContext } from '../llm/promptCache.js';
|
|
12
|
+
import { createAgentRuntimeContext, filterToolsForRuntime, memoryContextFromRuntime, } from '../runtime/agentRuntimeContext.js';
|
|
10
13
|
export async function runAgentTurn(opts,
|
|
11
14
|
// DI 缝(仿 agent.ts 的 deps 先例):compact_failed 无法经 chat DI 触发
|
|
12
15
|
// (压缩走模块级 chat,见 test/loop.errorCode.test.ts 头注),测试注入假 runTurn。
|
|
@@ -15,9 +18,12 @@ deps = {}) {
|
|
|
15
18
|
const session = (await listSessionSummaries(opts.cwd)).find((item) => item.id === opts.sessionId);
|
|
16
19
|
let sessionModelId = session?.modelId;
|
|
17
20
|
if (!opts.model && !sessionModelId) {
|
|
18
|
-
sessionModelId =
|
|
21
|
+
sessionModelId =
|
|
22
|
+
(await configService.getSnapshot()).config?.baseModelId ?? undefined;
|
|
19
23
|
if (sessionModelId) {
|
|
20
|
-
await updateSessionMetadata(opts.cwd, opts.sessionId, {
|
|
24
|
+
await updateSessionMetadata(opts.cwd, opts.sessionId, {
|
|
25
|
+
modelId: sessionModelId,
|
|
26
|
+
}).catch(() => { });
|
|
21
27
|
}
|
|
22
28
|
}
|
|
23
29
|
const sessionProvider = !opts.model && sessionModelId
|
|
@@ -25,24 +31,75 @@ deps = {}) {
|
|
|
25
31
|
: null;
|
|
26
32
|
const provider = opts.model
|
|
27
33
|
? { ...opts.provider, model: opts.model }
|
|
28
|
-
: sessionProvider ?? opts.provider;
|
|
29
|
-
|
|
34
|
+
: (sessionProvider ?? opts.provider);
|
|
35
|
+
const workspace = await workspaceIndexService
|
|
36
|
+
.register(opts.cwd)
|
|
37
|
+
.catch(() => undefined);
|
|
30
38
|
const sessionFile = sessionFileFor(opts.cwd, opts.sessionId);
|
|
31
|
-
const systemPrompt = await buildFullSystemPrompt(opts.cwd, ALL_TOOLS);
|
|
32
39
|
const events = [];
|
|
33
40
|
let result = '';
|
|
34
41
|
let error;
|
|
35
42
|
let numTurns = 0;
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
const turnSessionState = opts.sessionState ??
|
|
44
|
+
getOrCreateSessionState(sessionStateKey({
|
|
45
|
+
surface: 'headless',
|
|
46
|
+
workspaceId: workspace?.id,
|
|
47
|
+
cwd: workspace?.id ? undefined : opts.cwd,
|
|
48
|
+
sessionId: opts.sessionId,
|
|
49
|
+
}));
|
|
50
|
+
await hydrateSessionWorktree({
|
|
51
|
+
sessionState: turnSessionState,
|
|
52
|
+
workspaceId: workspace?.id,
|
|
53
|
+
sessionId: opts.sessionId,
|
|
54
|
+
cwd: opts.cwd,
|
|
55
|
+
});
|
|
56
|
+
const effectiveCwd = turnSessionState.activeWorktree?.path ?? opts.cwd;
|
|
57
|
+
await withWorkingDir(effectiveCwd, async () => {
|
|
58
|
+
// 稳定层进 history[0](runTurn 每轮重写它,必须逐字节可复现,见 runTurn.ts 的缓存不变量);
|
|
59
|
+
// 易变层每轮单独计算,仅附加到当前用户消息的模型副本。
|
|
60
|
+
const runtimeContext = createAgentRuntimeContext({
|
|
61
|
+
surface: 'headless',
|
|
62
|
+
profile: 'main',
|
|
63
|
+
cwd: effectiveCwd,
|
|
64
|
+
workspaceId: workspace?.id,
|
|
65
|
+
sessionId: opts.sessionId,
|
|
66
|
+
owner: opts.memoryAccess && opts.memoryAccess !== 'denied'
|
|
67
|
+
? 'local-owner'
|
|
68
|
+
: 'untrusted',
|
|
69
|
+
memoryAccess: opts.memoryAccess ?? 'denied',
|
|
70
|
+
interactiveAsk: false,
|
|
71
|
+
});
|
|
72
|
+
const turnTools = filterToolsForRuntime(ALL_TOOLS, runtimeContext);
|
|
73
|
+
const systemPrompt = await buildBaseSystemPrompt(effectiveCwd, turnTools);
|
|
74
|
+
const transientBundle = await buildTransientPromptBundle({
|
|
75
|
+
cwd: effectiveCwd,
|
|
76
|
+
tools: turnTools,
|
|
77
|
+
runtime: {
|
|
78
|
+
surface: 'headless',
|
|
79
|
+
cwd: effectiveCwd,
|
|
80
|
+
workspaceId: workspace?.id,
|
|
81
|
+
sessionId: opts.sessionId,
|
|
82
|
+
interactiveAsk: false,
|
|
83
|
+
memoryAccess: runtimeContext.memoryAccess,
|
|
84
|
+
currentUserInput: opts.input,
|
|
85
|
+
},
|
|
86
|
+
}).catch(() => undefined);
|
|
87
|
+
await updateSessionMetadata(opts.cwd, opts.sessionId, {
|
|
88
|
+
lastMessagePreview: opts.input,
|
|
89
|
+
}).catch(() => { });
|
|
38
90
|
for await (const ev of run({
|
|
39
91
|
userInput: opts.input,
|
|
40
92
|
provider,
|
|
41
93
|
sessionFile,
|
|
42
94
|
systemPrompt,
|
|
43
|
-
|
|
95
|
+
...(transientBundle ? { transientBundle } : {}),
|
|
96
|
+
promptCache: createPromptCacheContext(`workspace:${workspace?.id ?? opts.cwd}:session:${opts.sessionId}`),
|
|
97
|
+
sessionState: turnSessionState,
|
|
44
98
|
signal: opts.signal,
|
|
45
99
|
maxTurns: opts.maxTurns,
|
|
100
|
+
runtimeContext,
|
|
101
|
+
allowedTools: turnTools.map((tool) => tool.name),
|
|
102
|
+
memoryContext: memoryContextFromRuntime(runtimeContext),
|
|
46
103
|
})) {
|
|
47
104
|
if (isEventLike(ev)) {
|
|
48
105
|
events.push(ev);
|
|
@@ -66,5 +123,7 @@ deps = {}) {
|
|
|
66
123
|
};
|
|
67
124
|
}
|
|
68
125
|
function isEventLike(ev) {
|
|
69
|
-
return Boolean(ev &&
|
|
126
|
+
return Boolean(ev &&
|
|
127
|
+
typeof ev === 'object' &&
|
|
128
|
+
typeof ev.type === 'string');
|
|
70
129
|
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildBaseSystemPrompt,
|
|
3
|
+
buildTransientPromptBundle,
|
|
4
|
+
} from '../prompts/system.js';
|
|
5
|
+
import { ALL_TOOLS } from '../tools/index.js';
|
|
6
|
+
import type { LoopEvent, Provider } from '../types.js';
|
|
7
|
+
import { sessionFileFor } from '../context/sessionPath.js';
|
|
8
|
+
import {
|
|
9
|
+
getOrCreateSessionState,
|
|
10
|
+
sessionStateKey,
|
|
11
|
+
type SessionState,
|
|
12
|
+
} from './sessionState.js';
|
|
13
|
+
import { hydrateSessionWorktree } from '../tools/worktree/sessionWorktree.js';
|
|
14
|
+
import { runTurn } from './runTurn.js';
|
|
15
|
+
import { withWorkingDir } from '../bootstrap/workingDir.js';
|
|
16
|
+
import {
|
|
17
|
+
listSessionSummaries,
|
|
18
|
+
updateSessionMetadata,
|
|
19
|
+
} from '../context/sessionRegistry.js';
|
|
20
|
+
import { workspaceIndexService } from '../context/workspaceIndex.js';
|
|
21
|
+
import { configService } from '../config/configService.js';
|
|
22
|
+
import { createPromptCacheContext } from '../llm/promptCache.js';
|
|
23
|
+
import {
|
|
24
|
+
createAgentRuntimeContext,
|
|
25
|
+
filterToolsForRuntime,
|
|
26
|
+
memoryContextFromRuntime,
|
|
27
|
+
} from '../runtime/agentRuntimeContext.js';
|
|
28
|
+
|
|
29
|
+
export interface RunAgentTurnOptions {
|
|
30
|
+
input: string;
|
|
31
|
+
provider: Provider;
|
|
32
|
+
cwd: string;
|
|
33
|
+
sessionId: string;
|
|
34
|
+
maxTurns?: number;
|
|
35
|
+
model?: string;
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
/** Generic headless callers must declare local memory access explicitly. */
|
|
38
|
+
memoryAccess?: import('../runtime/agentRuntimeContext.js').MemoryAccessInput;
|
|
39
|
+
/** Advanced hosts may own the session lifecycle explicitly. */
|
|
40
|
+
sessionState?: SessionState;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface RunAgentTurnResult {
|
|
44
|
+
ok: boolean;
|
|
45
|
+
result: string;
|
|
46
|
+
error?: string;
|
|
47
|
+
numTurns: number;
|
|
48
|
+
events: Array<{ type: string; [key: string]: unknown }>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function runAgentTurn(
|
|
52
|
+
opts: RunAgentTurnOptions,
|
|
53
|
+
// DI 缝(仿 agent.ts 的 deps 先例):compact_failed 无法经 chat DI 触发
|
|
54
|
+
// (压缩走模块级 chat,见 test/loop.errorCode.test.ts 头注),测试注入假 runTurn。
|
|
55
|
+
deps: { runTurn?: typeof runTurn } = {},
|
|
56
|
+
): Promise<RunAgentTurnResult> {
|
|
57
|
+
const run = deps.runTurn ?? runTurn;
|
|
58
|
+
const session = (await listSessionSummaries(opts.cwd)).find(
|
|
59
|
+
(item) => item.id === opts.sessionId,
|
|
60
|
+
);
|
|
61
|
+
let sessionModelId = session?.modelId;
|
|
62
|
+
if (!opts.model && !sessionModelId) {
|
|
63
|
+
sessionModelId =
|
|
64
|
+
(await configService.getSnapshot()).config?.baseModelId ?? undefined;
|
|
65
|
+
if (sessionModelId) {
|
|
66
|
+
await updateSessionMetadata(opts.cwd, opts.sessionId, {
|
|
67
|
+
modelId: sessionModelId,
|
|
68
|
+
}).catch(() => {});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const sessionProvider =
|
|
72
|
+
!opts.model && sessionModelId
|
|
73
|
+
? await configService.providerForModel(sessionModelId)
|
|
74
|
+
: null;
|
|
75
|
+
const provider = opts.model
|
|
76
|
+
? { ...opts.provider, model: opts.model }
|
|
77
|
+
: (sessionProvider ?? opts.provider);
|
|
78
|
+
const workspace = await workspaceIndexService
|
|
79
|
+
.register(opts.cwd)
|
|
80
|
+
.catch(() => undefined);
|
|
81
|
+
const sessionFile = sessionFileFor(opts.cwd, opts.sessionId);
|
|
82
|
+
const events: LoopEvent[] = [];
|
|
83
|
+
let result = '';
|
|
84
|
+
let error: string | undefined;
|
|
85
|
+
let numTurns = 0;
|
|
86
|
+
const turnSessionState =
|
|
87
|
+
opts.sessionState ??
|
|
88
|
+
getOrCreateSessionState(
|
|
89
|
+
sessionStateKey({
|
|
90
|
+
surface: 'headless',
|
|
91
|
+
workspaceId: workspace?.id,
|
|
92
|
+
cwd: workspace?.id ? undefined : opts.cwd,
|
|
93
|
+
sessionId: opts.sessionId,
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
await hydrateSessionWorktree({
|
|
97
|
+
sessionState: turnSessionState,
|
|
98
|
+
workspaceId: workspace?.id,
|
|
99
|
+
sessionId: opts.sessionId,
|
|
100
|
+
cwd: opts.cwd,
|
|
101
|
+
});
|
|
102
|
+
const effectiveCwd = turnSessionState.activeWorktree?.path ?? opts.cwd;
|
|
103
|
+
|
|
104
|
+
await withWorkingDir(effectiveCwd, async () => {
|
|
105
|
+
// 稳定层进 history[0](runTurn 每轮重写它,必须逐字节可复现,见 runTurn.ts 的缓存不变量);
|
|
106
|
+
// 易变层每轮单独计算,仅附加到当前用户消息的模型副本。
|
|
107
|
+
const runtimeContext = createAgentRuntimeContext({
|
|
108
|
+
surface: 'headless',
|
|
109
|
+
profile: 'main',
|
|
110
|
+
cwd: effectiveCwd,
|
|
111
|
+
workspaceId: workspace?.id,
|
|
112
|
+
sessionId: opts.sessionId,
|
|
113
|
+
owner:
|
|
114
|
+
opts.memoryAccess && opts.memoryAccess !== 'denied'
|
|
115
|
+
? 'local-owner'
|
|
116
|
+
: 'untrusted',
|
|
117
|
+
memoryAccess: opts.memoryAccess ?? 'denied',
|
|
118
|
+
interactiveAsk: false,
|
|
119
|
+
});
|
|
120
|
+
const turnTools = filterToolsForRuntime(ALL_TOOLS, runtimeContext);
|
|
121
|
+
const systemPrompt = await buildBaseSystemPrompt(effectiveCwd, turnTools);
|
|
122
|
+
const transientBundle = await buildTransientPromptBundle({
|
|
123
|
+
cwd: effectiveCwd,
|
|
124
|
+
tools: turnTools,
|
|
125
|
+
runtime: {
|
|
126
|
+
surface: 'headless',
|
|
127
|
+
cwd: effectiveCwd,
|
|
128
|
+
workspaceId: workspace?.id,
|
|
129
|
+
sessionId: opts.sessionId,
|
|
130
|
+
interactiveAsk: false,
|
|
131
|
+
memoryAccess: runtimeContext.memoryAccess,
|
|
132
|
+
currentUserInput: opts.input,
|
|
133
|
+
},
|
|
134
|
+
}).catch(() => undefined);
|
|
135
|
+
await updateSessionMetadata(opts.cwd, opts.sessionId, {
|
|
136
|
+
lastMessagePreview: opts.input,
|
|
137
|
+
}).catch(() => {});
|
|
138
|
+
for await (const ev of run({
|
|
139
|
+
userInput: opts.input,
|
|
140
|
+
provider,
|
|
141
|
+
sessionFile,
|
|
142
|
+
systemPrompt,
|
|
143
|
+
...(transientBundle ? { transientBundle } : {}),
|
|
144
|
+
promptCache: createPromptCacheContext(
|
|
145
|
+
`workspace:${workspace?.id ?? opts.cwd}:session:${opts.sessionId}`,
|
|
146
|
+
),
|
|
147
|
+
sessionState: turnSessionState,
|
|
148
|
+
signal: opts.signal,
|
|
149
|
+
maxTurns: opts.maxTurns,
|
|
150
|
+
runtimeContext,
|
|
151
|
+
allowedTools: turnTools.map((tool) => tool.name),
|
|
152
|
+
memoryContext: memoryContextFromRuntime(runtimeContext),
|
|
153
|
+
})) {
|
|
154
|
+
if (isEventLike(ev)) {
|
|
155
|
+
events.push(ev);
|
|
156
|
+
if (ev.type === 'text') result += ev.delta;
|
|
157
|
+
if (ev.type === 'assistant_message') numTurns++;
|
|
158
|
+
// compact_failed 是非致命(loop 压缩失败后 snip 兜底继续跑完,types.ts 有约定;
|
|
159
|
+
// print.ts 同样特判)——不置 error,否则 cron 会把自愈轮误判失败。
|
|
160
|
+
if (ev.type === 'error' && ev.code !== 'compact_failed')
|
|
161
|
+
error = ev.error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
ok: !error,
|
|
168
|
+
result,
|
|
169
|
+
...(error ? { error } : {}),
|
|
170
|
+
numTurns,
|
|
171
|
+
events,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function isEventLike(ev: unknown): ev is LoopEvent {
|
|
176
|
+
return Boolean(
|
|
177
|
+
ev &&
|
|
178
|
+
typeof ev === 'object' &&
|
|
179
|
+
typeof (ev as { type?: unknown }).type === 'string',
|
|
180
|
+
);
|
|
181
|
+
}
|
package/src/session/runTurn.js
CHANGED
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
*/
|
|
21
21
|
import { loadContext, saveContext } from '../context/persistContext.js';
|
|
22
22
|
import { acquireContextLock } from '../context/contextLock.js';
|
|
23
|
+
import { startTurnJournal } from '../memory/turnJournal.js';
|
|
23
24
|
import { runWithPlugins } from '../plugins/pluginRunner.js';
|
|
24
25
|
/**
|
|
25
26
|
* 已有历史时,用调用方新算好的 systemPrompt(含当前日期/实时扫描的 skills·plugins 清单/
|
|
@@ -30,6 +31,14 @@ import { runWithPlugins } from '../plugins/pluginRunner.js';
|
|
|
30
31
|
* plugins/项目指令都不会再刷新,与"运行时实时扫描"的设计承诺矛盾。
|
|
31
32
|
*
|
|
32
33
|
* 没有 loaded(全新会话)走原逻辑;调用方未传 systemPrompt 时不触碰已有历史(向后兼容)。
|
|
34
|
+
*
|
|
35
|
+
* ★ 提示词缓存不变量:调用方传进来的 systemPrompt **必须是稳定层**
|
|
36
|
+
* (buildBaseSystemPrompt,不含日期/Advisor 状态/记忆召回)。否则这里每轮重写
|
|
37
|
+
* history[0] 就会让 system 消息字节逐轮变化,provider 的前缀缓存从第一个 token
|
|
38
|
+
* 起全部失配 —— 本函数是 headless/cron/Desktop 路径上最隐蔽的一处缓存杀手。
|
|
39
|
+
* 易变内容改走 opts.transientContext(loop 只装饰当前用户消息的模型副本)。
|
|
40
|
+
* 稳定层是确定性的,所以这里的"每轮刷新"退化成逐字节相同的 no-op,
|
|
41
|
+
* 既保住了"运行时实时扫描"的承诺,又不破坏缓存。
|
|
33
42
|
*/
|
|
34
43
|
function buildInitialHistory(loaded, systemPrompt) {
|
|
35
44
|
if (!loaded) {
|
|
@@ -45,27 +54,47 @@ export async function* runTurn(opts) {
|
|
|
45
54
|
const release = persist ? await acquireContextLock(opts.sessionFile, opts.signal) : async () => { };
|
|
46
55
|
const loaded = opts.initialHistory ?? (persist ? await loadContext(opts.sessionFile) : null);
|
|
47
56
|
const history = buildInitialHistory(loaded, opts.systemPrompt);
|
|
57
|
+
const historyStart = history.length;
|
|
58
|
+
// 会话原文记账:实现在 src/memory/turnJournal.ts(TUI 的 useChat 用同一份),
|
|
59
|
+
// 刻意不下沉到 runQuery —— 子 Agent 与 workflow 步也调 runQuery,记在那里会把
|
|
60
|
+
// 子任务提示词当成"用户说的话"污染历史检索。
|
|
61
|
+
const journal = await startTurnJournal({
|
|
62
|
+
memoryContext: opts.memoryContext,
|
|
63
|
+
userInput: opts.userInput,
|
|
64
|
+
...(opts.userMessageId ? { userMessageId: opts.userMessageId } : {}),
|
|
65
|
+
});
|
|
48
66
|
try {
|
|
49
67
|
yield* runWithPlugins(opts.userInput, {
|
|
50
68
|
provider: opts.provider,
|
|
51
69
|
history,
|
|
52
70
|
signal: opts.signal,
|
|
53
71
|
maxTurns: opts.maxTurns,
|
|
72
|
+
runtimeContext: opts.runtimeContext,
|
|
73
|
+
allowedTools: opts.allowedTools,
|
|
54
74
|
sessionState: opts.sessionState,
|
|
55
75
|
chat: opts.chat,
|
|
56
76
|
executeTool: opts.executeTool,
|
|
77
|
+
commitFormalDelivery: opts.commitFormalDelivery,
|
|
57
78
|
toolGate: opts.toolGate,
|
|
58
79
|
transientContext: opts.transientContext,
|
|
80
|
+
transientBundle: opts.transientBundle,
|
|
81
|
+
promptCache: opts.promptCache,
|
|
59
82
|
userAttachments: opts.userAttachments,
|
|
83
|
+
userMessageId: opts.userMessageId,
|
|
84
|
+
userBrowserReferences: opts.userBrowserReferences,
|
|
60
85
|
imageInputs: opts.imageInputs,
|
|
61
86
|
parseInlineImages: opts.parseInlineImages,
|
|
62
87
|
imageDetail: opts.imageDetail,
|
|
63
88
|
imageMaxLongSidePixel: opts.imageMaxLongSidePixel,
|
|
64
89
|
browserRun: opts.browserRun,
|
|
90
|
+
computerRun: opts.computerRun,
|
|
91
|
+
computerIntervention: opts.computerIntervention,
|
|
92
|
+
memoryContext: opts.memoryContext,
|
|
65
93
|
});
|
|
66
94
|
}
|
|
67
95
|
finally {
|
|
68
96
|
try {
|
|
97
|
+
await journal?.finish(history, historyStart);
|
|
69
98
|
if (persist)
|
|
70
99
|
await saveContext(history, opts.sessionFile);
|
|
71
100
|
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ============================================================
|
|
3
|
+
* src/session/runTurn.ts —— Headless 单 turn 执行器
|
|
4
|
+
* ------------------------------------------------------------
|
|
5
|
+
* 把"读 session 文件 → 跑 runQuery → 写 session 文件"封装成单一入口。
|
|
6
|
+
* PR 4 HTTP server 的每个请求 handler 用它即可,无需自己拼装。
|
|
7
|
+
*
|
|
8
|
+
* 调用方契约:
|
|
9
|
+
* - sessionFile: per-chat 隔离的上下文文件路径(参见 sessionPath.sessionFileFor)
|
|
10
|
+
* - systemPrompt: 当 loadContext 返回 null(首次会话)时用它建初始 history;
|
|
11
|
+
* 未提供则用空数组开局(少数 case 才合理,例如不需要 system prompt)
|
|
12
|
+
* - sessionState: 多并发场景必须 per-request 创建一份,避免不同 chat 串扰
|
|
13
|
+
* - signal: 调用方持有 AbortController,断开/超时时主动 abort
|
|
14
|
+
*
|
|
15
|
+
* 落盘策略:
|
|
16
|
+
* 用 try/finally 包住 runQuery;不管成功、抛错、还是被中断都保存当前
|
|
17
|
+
* history。中断时 loop.ts 内部会先把"用户按下了 ESC/Ctrl+C"标记
|
|
18
|
+
* 消息追加到 history,所以落盘的也是合法且完整的会话状态。
|
|
19
|
+
* ============================================================
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { loadContext, saveContext } from '../context/persistContext.js';
|
|
23
|
+
import { acquireContextLock } from '../context/contextLock.js';
|
|
24
|
+
import { startTurnJournal } from '../memory/turnJournal.js';
|
|
25
|
+
import type { PluginEvent } from '../plugins/pluginApi.js';
|
|
26
|
+
import { runWithPlugins } from '../plugins/pluginRunner.js';
|
|
27
|
+
import type {
|
|
28
|
+
ImageDetail,
|
|
29
|
+
ImageInputSpec,
|
|
30
|
+
LoopEvent,
|
|
31
|
+
Message,
|
|
32
|
+
MessageAttachmentRef,
|
|
33
|
+
MessageBrowserReference,
|
|
34
|
+
Provider,
|
|
35
|
+
} from '../types.js';
|
|
36
|
+
import type { SessionState } from './sessionState.js';
|
|
37
|
+
|
|
38
|
+
export interface RunTurnOptions {
|
|
39
|
+
userInput: string;
|
|
40
|
+
provider: Provider;
|
|
41
|
+
sessionFile: string;
|
|
42
|
+
systemPrompt?: string;
|
|
43
|
+
sessionState?: SessionState;
|
|
44
|
+
signal?: AbortSignal;
|
|
45
|
+
maxTurns?: number;
|
|
46
|
+
runtimeContext?: import('../runtime/agentRuntimeContext.js').AgentRuntimeContext;
|
|
47
|
+
allowedTools?: import('../loop.js').RunQueryOptions['allowedTools'];
|
|
48
|
+
/** DI:透传给 runWithPlugins→runQuery(仅测试用,生产不传走默认实现)。
|
|
49
|
+
* 让测试用显式注入替代 mock.module(loop.js)——后者会全局泄漏污染其它测试文件。 */
|
|
50
|
+
chat?: import('../plugins/pluginRunner.js').PluginRunnerOptions['chat'];
|
|
51
|
+
executeTool?: import('../plugins/pluginRunner.js').PluginRunnerOptions['executeTool'];
|
|
52
|
+
commitFormalDelivery?: import('../plugins/pluginRunner.js').PluginRunnerOptions['commitFormalDelivery'];
|
|
53
|
+
/** 工具执行闸门(生产用):透传给 runWithPlugins→runQuery。见 src/toolGate.ts。 */
|
|
54
|
+
toolGate?: import('../plugins/pluginRunner.js').PluginRunnerOptions['toolGate'];
|
|
55
|
+
/** 只对本次模型回合可见,不写入 session。 */
|
|
56
|
+
transientContext?: import('../plugins/pluginRunner.js').PluginRunnerOptions['transientContext'];
|
|
57
|
+
transientBundle?: import('../plugins/pluginRunner.js').PluginRunnerOptions['transientBundle'];
|
|
58
|
+
promptCache?: import('../plugins/pluginRunner.js').PluginRunnerOptions['promptCache'];
|
|
59
|
+
userAttachments?: MessageAttachmentRef[];
|
|
60
|
+
/** Stable message identity supplied by a host/runtime request. */
|
|
61
|
+
userMessageId?: string;
|
|
62
|
+
userBrowserReferences?: MessageBrowserReference[];
|
|
63
|
+
imageInputs?: ImageInputSpec[];
|
|
64
|
+
parseInlineImages?: boolean;
|
|
65
|
+
imageDetail?: ImageDetail;
|
|
66
|
+
imageMaxLongSidePixel?: number;
|
|
67
|
+
browserRun?: import('../types.js').ToolCallContext['browserRun'];
|
|
68
|
+
computerRun?: import('../types.js').ToolCallContext['computerRun'];
|
|
69
|
+
computerIntervention?: import('../types.js').ToolCallContext['computerIntervention'];
|
|
70
|
+
memoryContext?: import('../loop.js').RunQueryOptions['memoryContext'];
|
|
71
|
+
/** Explicit history for an isolated, non-persistent turn. */
|
|
72
|
+
initialHistory?: Message[];
|
|
73
|
+
/** Defaults to true. False avoids reading, locking, or writing sessionFile. */
|
|
74
|
+
persist?: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 已有历史时,用调用方新算好的 systemPrompt(含当前日期/实时扫描的 skills·plugins 清单/
|
|
79
|
+
* 项目指令)替换旧的 system 消息,其余历史原样保留——与 ui/Root.tsx 的 TUI 启动逻辑、
|
|
80
|
+
* workflow-runner::refreshWorkflowSystemPrompt 行为一致。堵上 headless 会话(cron-runner
|
|
81
|
+
* 纯文本 action 等长期复用同一 sessionFile 的调用方)系统提示词只在首次开会话时生效、
|
|
82
|
+
* 此后永久冻结的口子——此前的行为是"已有历史就完全忽略新 systemPrompt",日期/skills/
|
|
83
|
+
* plugins/项目指令都不会再刷新,与"运行时实时扫描"的设计承诺矛盾。
|
|
84
|
+
*
|
|
85
|
+
* 没有 loaded(全新会话)走原逻辑;调用方未传 systemPrompt 时不触碰已有历史(向后兼容)。
|
|
86
|
+
*
|
|
87
|
+
* ★ 提示词缓存不变量:调用方传进来的 systemPrompt **必须是稳定层**
|
|
88
|
+
* (buildBaseSystemPrompt,不含日期/Advisor 状态/记忆召回)。否则这里每轮重写
|
|
89
|
+
* history[0] 就会让 system 消息字节逐轮变化,provider 的前缀缓存从第一个 token
|
|
90
|
+
* 起全部失配 —— 本函数是 headless/cron/Desktop 路径上最隐蔽的一处缓存杀手。
|
|
91
|
+
* 易变内容改走 opts.transientContext(loop 只装饰当前用户消息的模型副本)。
|
|
92
|
+
* 稳定层是确定性的,所以这里的"每轮刷新"退化成逐字节相同的 no-op,
|
|
93
|
+
* 既保住了"运行时实时扫描"的承诺,又不破坏缓存。
|
|
94
|
+
*/
|
|
95
|
+
function buildInitialHistory(
|
|
96
|
+
loaded: Message[] | null,
|
|
97
|
+
systemPrompt: string | undefined,
|
|
98
|
+
): Message[] {
|
|
99
|
+
if (!loaded) {
|
|
100
|
+
return systemPrompt ? [{ role: 'system', content: systemPrompt }] : [];
|
|
101
|
+
}
|
|
102
|
+
if (!systemPrompt) return loaded;
|
|
103
|
+
const rest = loaded[0]?.role === 'system' ? loaded.slice(1) : loaded;
|
|
104
|
+
return [{ role: 'system', content: systemPrompt }, ...rest];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function* runTurn(
|
|
108
|
+
opts: RunTurnOptions,
|
|
109
|
+
): AsyncGenerator<LoopEvent | PluginEvent, void, void> {
|
|
110
|
+
const persist = opts.persist !== false;
|
|
111
|
+
const release = persist ? await acquireContextLock(opts.sessionFile, opts.signal) : async () => {};
|
|
112
|
+
const loaded = opts.initialHistory ?? (persist ? await loadContext(opts.sessionFile) : null);
|
|
113
|
+
const history: Message[] = buildInitialHistory(loaded, opts.systemPrompt);
|
|
114
|
+
const historyStart = history.length;
|
|
115
|
+
// 会话原文记账:实现在 src/memory/turnJournal.ts(TUI 的 useChat 用同一份),
|
|
116
|
+
// 刻意不下沉到 runQuery —— 子 Agent 与 workflow 步也调 runQuery,记在那里会把
|
|
117
|
+
// 子任务提示词当成"用户说的话"污染历史检索。
|
|
118
|
+
const journal = await startTurnJournal({
|
|
119
|
+
memoryContext: opts.memoryContext,
|
|
120
|
+
userInput: opts.userInput,
|
|
121
|
+
...(opts.userMessageId ? { userMessageId: opts.userMessageId } : {}),
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
yield* runWithPlugins(opts.userInput, {
|
|
126
|
+
provider: opts.provider,
|
|
127
|
+
history,
|
|
128
|
+
signal: opts.signal,
|
|
129
|
+
maxTurns: opts.maxTurns,
|
|
130
|
+
runtimeContext: opts.runtimeContext,
|
|
131
|
+
allowedTools: opts.allowedTools,
|
|
132
|
+
sessionState: opts.sessionState,
|
|
133
|
+
chat: opts.chat,
|
|
134
|
+
executeTool: opts.executeTool,
|
|
135
|
+
commitFormalDelivery: opts.commitFormalDelivery,
|
|
136
|
+
toolGate: opts.toolGate,
|
|
137
|
+
transientContext: opts.transientContext,
|
|
138
|
+
transientBundle: opts.transientBundle,
|
|
139
|
+
promptCache: opts.promptCache,
|
|
140
|
+
userAttachments: opts.userAttachments,
|
|
141
|
+
userMessageId: opts.userMessageId,
|
|
142
|
+
userBrowserReferences: opts.userBrowserReferences,
|
|
143
|
+
imageInputs: opts.imageInputs,
|
|
144
|
+
parseInlineImages: opts.parseInlineImages,
|
|
145
|
+
imageDetail: opts.imageDetail,
|
|
146
|
+
imageMaxLongSidePixel: opts.imageMaxLongSidePixel,
|
|
147
|
+
browserRun: opts.browserRun,
|
|
148
|
+
computerRun: opts.computerRun,
|
|
149
|
+
computerIntervention: opts.computerIntervention,
|
|
150
|
+
memoryContext: opts.memoryContext,
|
|
151
|
+
});
|
|
152
|
+
} finally {
|
|
153
|
+
try {
|
|
154
|
+
await journal?.finish(history, historyStart);
|
|
155
|
+
if (persist) await saveContext(history, opts.sessionFile);
|
|
156
|
+
} finally {
|
|
157
|
+
await release();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|