@octopi-agent/engine 0.102.10 → 0.121.10
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/dist/builtin-model-info.js +28 -7
- package/dist/config-admin/agent-presets.d.ts +32 -0
- package/dist/config-admin/agent-presets.js +133 -0
- package/dist/config-admin/discover.d.ts +33 -4
- package/dist/config-admin/discover.js +101 -6
- package/dist/config-admin/env.d.ts +11 -0
- package/dist/config-admin/env.js +41 -0
- package/dist/config-admin/error-copy.d.ts +106 -0
- package/dist/config-admin/error-copy.js +287 -0
- package/dist/config-admin/i18n.d.ts +21 -0
- package/dist/config-admin/i18n.js +19 -0
- package/dist/config-admin/index.d.ts +6 -2
- package/dist/config-admin/index.js +3 -1
- package/dist/config-admin/model-presets.d.ts +14 -0
- package/dist/config-admin/model-presets.js +595 -70
- package/dist/config-admin/onboarding-apply.d.ts +71 -0
- package/dist/config-admin/onboarding-apply.js +235 -0
- package/dist/config-admin/onboarding.d.ts +154 -0
- package/dist/config-admin/onboarding.js +270 -0
- package/dist/config-admin/redact.d.ts +42 -9
- package/dist/config-admin/redact.js +69 -5
- package/dist/config-schema/agent.d.ts +5 -1
- package/dist/config-schema/agent.js +20 -2
- package/dist/config-schema/browser.d.ts +62 -0
- package/dist/config-schema/browser.js +89 -0
- package/dist/config-schema/context.d.ts +7 -2
- package/dist/config-schema/context.js +6 -3
- package/dist/config-schema/engine.d.ts +86 -7
- package/dist/config-schema/engine.js +9 -2
- package/dist/config-schema/governance.d.ts +12 -1
- package/dist/config-schema/governance.js +10 -2
- package/dist/config-schema/models.d.ts +12 -8
- package/dist/config-schema/models.js +16 -1
- package/dist/config-schema/run.d.ts +4 -0
- package/dist/config-schema/run.js +10 -0
- package/dist/config-schema/shared.d.ts +1 -0
- package/dist/config-schema/shared.js +4 -2
- package/dist/config-schema/substrate.d.ts +5 -0
- package/dist/config-schema/substrate.js +8 -0
- package/dist/config.d.ts +116 -10
- package/dist/config.js +109 -13
- package/dist/file-mode.d.ts +67 -0
- package/dist/file-mode.js +182 -0
- package/dist/fork-child.d.ts +82 -0
- package/dist/fork-child.js +123 -0
- package/dist/harness/activation/delegated-run.d.ts +42 -0
- package/dist/harness/activation/delegated-run.js +71 -0
- package/dist/harness/activation/dispatcher.js +4 -0
- package/dist/harness/activation/index.d.ts +1 -0
- package/dist/harness/activation/run-backend.d.ts +92 -0
- package/dist/harness/activation/run-backend.js +16 -0
- package/dist/harness/activation/run-protocol.d.ts +392 -0
- package/dist/harness/activation/run-protocol.js +19 -0
- package/dist/harness/activation/runtime.d.ts +33 -0
- package/dist/harness/activation/runtime.js +119 -28
- package/dist/harness/activation/types.d.ts +19 -2
- package/dist/harness/agent/builder.d.ts +12 -0
- package/dist/harness/agent/builder.js +78 -22
- package/dist/harness/agent/config-bridge.d.ts +10 -1
- package/dist/harness/agent/config-bridge.js +15 -3
- package/dist/harness/agent/registered-tool-adapter.d.ts +27 -0
- package/dist/harness/agent/registered-tool-adapter.js +67 -0
- package/dist/harness/agent/wire-agent-builder.d.ts +2 -0
- package/dist/harness/agent/wire-agent-builder.js +9 -2
- package/dist/harness/capabilities/document/backends/plain.js +2 -1
- package/dist/harness/capabilities/document/legacy/factory.js +3 -2
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.d.ts +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.js +66 -36
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.d.ts +6 -2
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.js +27 -7
- package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.js +13 -0
- package/dist/harness/collaboration/autonomous-subsystem/loader.js +1 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +11 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.js +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.d.ts +13 -3
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +48 -39
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +3 -0
- package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +4 -0
- package/dist/harness/collaboration/delegation/child-run.d.ts +9 -0
- package/dist/harness/collaboration/delegation/child-run.js +34 -2
- package/dist/harness/collaboration/delegation/registry-types.d.ts +2 -0
- package/dist/harness/collaboration/delegation/service.d.ts +9 -4
- package/dist/harness/collaboration/delegation/service.js +33 -12
- package/dist/harness/collaboration/delegation/store.d.ts +2 -0
- package/dist/harness/collaboration/delegation/store.js +1 -0
- package/dist/harness/collaboration/delegation/types.d.ts +11 -0
- package/dist/harness/collaboration/delegation/wiring.d.ts +2 -0
- package/dist/harness/collaboration/delegation/wiring.js +1 -0
- package/dist/harness/context/capabilities/compact/engine.d.ts +44 -5
- package/dist/harness/context/capabilities/compact/engine.js +127 -39
- package/dist/harness/context/capabilities/compact/index.d.ts +2 -1
- package/dist/harness/context/capabilities/compact/index.js +1 -1
- package/dist/harness/context/capabilities/compact/types.d.ts +59 -9
- package/dist/harness/context/capabilities/summary/gate.d.ts +19 -2
- package/dist/harness/context/capabilities/summary/gate.js +53 -6
- package/dist/harness/context/capabilities/summary/registry.js +20 -0
- package/dist/harness/context/capabilities/summary/tool-binding.js +16 -12
- package/dist/harness/context/capabilities/summary/tool-bindings.d.ts +13 -0
- package/dist/harness/context/capabilities/summary/tool-bindings.js +22 -0
- package/dist/harness/context/capabilities/summary/types.d.ts +16 -0
- package/dist/harness/context/constitution/default-agents.md +85 -0
- package/dist/harness/context/context-size-ledger.js +8 -1
- package/dist/harness/context/default-context-engine.d.ts +100 -6
- package/dist/harness/context/default-context-engine.js +382 -89
- package/dist/harness/context/index.d.ts +2 -1
- package/dist/harness/context/index.js +1 -0
- package/dist/harness/context/layer-health.d.ts +4 -0
- package/dist/harness/context/layer-health.js +4 -0
- package/dist/harness/context/layer-types.d.ts +57 -0
- package/dist/harness/context/layer-types.js +73 -1
- package/dist/harness/context/layers.d.ts +3 -0
- package/dist/harness/context/layers.js +13 -6
- package/dist/harness/context/prefix-fingerprint.d.ts +46 -0
- package/dist/harness/context/prefix-fingerprint.js +52 -0
- package/dist/harness/context/summarize.js +18 -0
- package/dist/harness/context/system-prompt-assembler.d.ts +2 -0
- package/dist/harness/context/system-prompt-assembler.js +5 -2
- package/dist/harness/context/tool-result-aging.d.ts +104 -16
- package/dist/harness/context/tool-result-aging.js +211 -57
- package/dist/harness/context/types.d.ts +71 -4
- package/dist/harness/context/types.js +25 -1
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +4 -3
- package/dist/harness/extension/execution-environment/effect-policy.js +2 -2
- package/dist/harness/extension/execution-environment/privileged-exec.js +4 -1
- package/dist/harness/extension/execution-environment/workspace.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +27 -1
- package/dist/harness/extension/plugin-ecosystem/channel-types.js +11 -2
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +12 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +25 -0
- package/dist/harness/extension/plugin-ecosystem/commands/builtin.js +28 -1
- package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/api.d.ts +168 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +156 -15
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.d.ts +95 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.js +170 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.d.ts +178 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.js +314 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.d.ts +40 -5
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +45 -26
- package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +11 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +42 -76
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +30 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +46 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +9 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +19 -14
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +3 -1
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.d.ts +333 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.js +16 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.d.ts +64 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.js +692 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +0 -37
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +108 -4
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +18 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +34 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.d.ts +0 -6
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +57 -4
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +81 -13
- package/dist/harness/extension/plugin-ecosystem/tools/http.js +5 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +13 -7
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +126 -14
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +44 -5
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +10 -24
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +7 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +4 -0
- package/dist/harness/governance/credentials/db.d.ts +9 -0
- package/dist/harness/governance/credentials/db.js +122 -31
- package/dist/harness/governance/credentials/store.d.ts +68 -7
- package/dist/harness/governance/credentials/store.js +177 -64
- package/dist/harness/governance/credentials/types.d.ts +13 -2
- package/dist/harness/governance/index.d.ts +1 -0
- package/dist/harness/governance/index.js +1 -0
- package/dist/harness/governance/registry/db.d.ts +32 -0
- package/dist/harness/governance/registry/db.js +142 -0
- package/dist/harness/governance/registry/index.d.ts +9 -0
- package/dist/harness/governance/registry/index.js +9 -0
- package/dist/harness/governance/registry/service.d.ts +381 -0
- package/dist/harness/governance/registry/service.js +825 -0
- package/dist/harness/governance/registry/types.d.ts +77 -0
- package/dist/harness/governance/registry/types.js +7 -0
- package/dist/harness/governance/security/default-security-guard.d.ts +3 -0
- package/dist/harness/governance/security/default-security-guard.js +12 -1
- package/dist/harness/governance/security/risk-evaluator.d.ts +13 -3
- package/dist/harness/governance/security/risk-evaluator.js +307 -21
- package/dist/harness/governance/security/shell-parser-treesitter.js +12 -4
- package/dist/harness/governance/session-acl/tool-rights.d.ts +1 -0
- package/dist/harness/index.d.ts +5 -3
- package/dist/harness/index.js +2 -2
- package/dist/harness/knowledge/boot-config.d.ts +2 -0
- package/dist/harness/knowledge/boot-config.js +2 -0
- package/dist/harness/knowledge/client.d.ts +53 -7
- package/dist/harness/knowledge/client.js +77 -20
- package/dist/harness/knowledge/db.d.ts +16 -0
- package/dist/harness/knowledge/db.js +107 -41
- package/dist/harness/knowledge/engine-thread.js +5 -7
- package/dist/harness/knowledge/grounding.js +4 -1
- package/dist/harness/knowledge/http-app.d.ts +15 -0
- package/dist/harness/knowledge/http-app.js +82 -21
- package/dist/harness/knowledge/http-bridge.d.ts +15 -0
- package/dist/harness/knowledge/http-bridge.js +19 -0
- package/dist/harness/knowledge/knowledge-serve-child.js +2 -3
- package/dist/harness/knowledge/query-service.d.ts +12 -2
- package/dist/harness/knowledge/query-service.js +75 -41
- package/dist/harness/knowledge/query-worker-client.d.ts +5 -1
- package/dist/harness/knowledge/query-worker-client.js +4 -1
- package/dist/harness/knowledge/query-worker.js +5 -3
- package/dist/harness/knowledge/read-http.d.ts +2 -0
- package/dist/harness/knowledge/read-http.js +9 -2
- package/dist/harness/knowledge/retriever.d.ts +1 -1
- package/dist/harness/knowledge/retriever.js +1 -2
- package/dist/harness/knowledge/serve.d.ts +6 -1
- package/dist/harness/knowledge/serve.js +5 -6
- package/dist/harness/knowledge/source-store.d.ts +101 -5
- package/dist/harness/knowledge/source-store.js +297 -71
- package/dist/harness/knowledge/start-service-process.d.ts +27 -8
- package/dist/harness/knowledge/start-service-process.js +49 -90
- package/dist/harness/knowledge/tenant-gate.d.ts +81 -0
- package/dist/harness/knowledge/tenant-gate.js +167 -0
- package/dist/harness/knowledge/writer-service.d.ts +18 -2
- package/dist/harness/knowledge/writer-service.js +80 -16
- package/dist/harness/knowledge/writer-worker-client.d.ts +9 -2
- package/dist/harness/knowledge/writer-worker-client.js +20 -3
- package/dist/harness/knowledge/writer-worker.js +11 -4
- package/dist/harness/memory/backfill-trigger.d.ts +10 -3
- package/dist/harness/memory/backfill-trigger.js +19 -19
- package/dist/harness/memory/cognition-seeds.d.ts +45 -0
- package/dist/harness/memory/cognition-seeds.js +67 -0
- package/dist/harness/memory/cognition.d.ts +12 -3
- package/dist/harness/memory/cognition.js +55 -35
- package/dist/harness/memory/conceptualizer.d.ts +11 -1
- package/dist/harness/memory/conceptualizer.js +10 -1
- package/dist/harness/memory/context-slice.d.ts +42 -0
- package/dist/harness/memory/context-slice.js +197 -0
- package/dist/harness/memory/health-probe.d.ts +8 -2
- package/dist/harness/memory/health-probe.js +10 -13
- package/dist/harness/memory/index.d.ts +5 -1
- package/dist/harness/memory/index.js +3 -1
- package/dist/harness/memory/sqlite/agent-db.d.ts +1 -0
- package/dist/harness/memory/sqlite/agent-db.js +9 -8
- package/dist/harness/memory/sqlite/cognition-store.d.ts +26 -3
- package/dist/harness/memory/sqlite/cognition-store.js +103 -45
- package/dist/harness/memory/sqlite/memory-store.js +4 -0
- package/dist/harness/memory/sqlite/sqlite-vec.d.ts +24 -1
- package/dist/harness/memory/sqlite/sqlite-vec.js +37 -1
- package/dist/harness/memory/types.d.ts +18 -2
- package/dist/harness/observability/observer/hub.d.ts +10 -0
- package/dist/harness/observability/observer/hub.js +95 -2
- package/dist/harness/run/agent/agent.js +3 -2
- package/dist/harness/run/concurrency/tool-validator.js +4 -3
- package/dist/harness/run/reliability/run-agent.d.ts +22 -0
- package/dist/harness/run/reliability/run-agent.js +79 -10
- package/dist/harness/run/run-host.d.ts +302 -0
- package/dist/harness/run/run-host.js +978 -0
- package/dist/harness/run/run-scope.d.ts +11 -0
- package/dist/harness/run/run-scope.js +19 -1
- package/dist/harness/run/runner.d.ts +149 -22
- package/dist/harness/run/runner.js +352 -35
- package/dist/harness/session/in-memory-store.js +13 -1
- package/dist/harness/session/schedules/service.d.ts +10 -2
- package/dist/harness/session/schedules/service.js +13 -2
- package/dist/harness/session/tasks/tools.js +12 -0
- package/dist/harness/session/types.d.ts +10 -14
- package/dist/harness/shared/events/agent-event-map.d.ts +29 -0
- package/dist/harness/shared/types/agent-definition.d.ts +6 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/integration/agent-runtime/channel-message-source.d.ts +3 -1
- package/dist/integration/agent-runtime/channel-message-source.js +4 -3
- package/dist/integration/browser/camoufox.d.ts +40 -0
- package/dist/integration/browser/camoufox.js +80 -0
- package/dist/integration/browser/index.d.ts +91 -0
- package/dist/integration/browser/index.js +110 -0
- package/dist/integration/browser/install.d.ts +47 -0
- package/dist/integration/browser/install.js +214 -0
- package/dist/integration/browser/playwright.d.ts +224 -0
- package/dist/integration/browser/playwright.js +925 -0
- package/dist/integration/browser/service.d.ts +41 -0
- package/dist/integration/browser/service.js +181 -0
- package/dist/integration/browser/stealth.d.ts +42 -0
- package/dist/integration/browser/stealth.js +104 -0
- package/dist/integration/providers/anthropic-message-normalize.d.ts +20 -0
- package/dist/integration/providers/anthropic-message-normalize.js +48 -0
- package/dist/integration/providers/anthropic.js +27 -2
- package/dist/integration/providers/fetch-timeout.d.ts +17 -0
- package/dist/integration/providers/fetch-timeout.js +24 -0
- package/dist/integration/providers/openai.d.ts +8 -0
- package/dist/integration/providers/openai.js +41 -1
- package/dist/integration/providers/wire-dump-diff.js +2 -1
- package/dist/integration/providers/wire-dump.d.ts +56 -2
- package/dist/integration/providers/wire-dump.js +119 -6
- package/dist/integration/storage/archive-manager.d.ts +14 -2
- package/dist/integration/storage/archive-manager.js +24 -8
- package/dist/integration/storage/atomic-tmp.d.ts +12 -0
- package/dist/integration/storage/atomic-tmp.js +36 -0
- package/dist/integration/storage/jsonl.d.ts +9 -0
- package/dist/integration/storage/jsonl.js +137 -38
- package/dist/integration/storage/session-index.d.ts +5 -0
- package/dist/integration/storage/session-index.js +25 -4
- package/dist/integration/tools/assemble.d.ts +50 -0
- package/dist/integration/tools/assemble.js +137 -0
- package/dist/paths.d.ts +18 -0
- package/dist/paths.js +36 -0
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +3 -1
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +74 -5
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +3 -2
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +11 -2
- package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +6 -3
- package/dist/subsystems/cognition-steward/consolidate/handler.js +266 -28
- package/dist/subsystems/memory-steward/backfill/handler.js +3 -2
- package/dist/subsystems/memory-steward/govern/config.yaml +6 -1
- package/dist/subsystems/wisdom-steward/attribute/config.yaml +5 -1
- package/dist/subsystems/wisdom-steward/formation/config.yaml +3 -0
- package/dist/subsystems/wisdom-steward/govern/config.yaml +6 -1
- package/package.json +5 -3
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* browser_* 工具面 — 浏览器操作五件套(arch/browser-capability.md §3.4)
|
|
3
|
+
*
|
|
4
|
+
* 效应纪律(§3.6):
|
|
5
|
+
* - 浏览器 = 网络出口(外泄通道)+ 不可逆外部写。URL 判定与外部写闸门在**请求层**
|
|
6
|
+
* 收口(BrowserRequestPolicy 注入会话;重定向/子资源/页内导航全覆盖),DOM 层
|
|
7
|
+
* 敏感标记(passwordField/formSubmit)只是提前预判——两层都过才执行。
|
|
8
|
+
* - 确认闸门复用 HitlDecision(confirmEffect 缺失时敏感动作 fail-closed 拒绝)。
|
|
9
|
+
* - 导航/写尝试/拦截经 recordOpsEvent 进统一效应账本。
|
|
10
|
+
* - 截图落盘经 fileEffects(L3 undo 记账),返回**路径**——工具结果在 LLM 边界是
|
|
11
|
+
* 纯文本序列化(core normalizeMessagesForLlm),图像数据不进上下文。
|
|
12
|
+
*/
|
|
13
|
+
import { randomUUID } from 'node:crypto';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { domainToASCII } from 'node:url';
|
|
16
|
+
import { BROWSER_DEFAULT_TIMEOUT_MS } from './browser-types.js';
|
|
17
|
+
import { getRunScope, runInScope } from '../../../run/run-scope.js';
|
|
18
|
+
import { assertUrlAllowed } from '../../../knowledge/network-guard.js';
|
|
19
|
+
import { containmentOptions, resolveToolPath } from './platform.js';
|
|
20
|
+
// ── URL 策略(请求层单点;playwright 路由拦截与工具预检共用) ──
|
|
21
|
+
/** 域名归一:去尾点 + IDN→punycode(URL.hostname 已是 punycode,模式侧需对齐) */
|
|
22
|
+
function normalizeHost(host) {
|
|
23
|
+
return host.replace(/\.$/, '').toLowerCase();
|
|
24
|
+
}
|
|
25
|
+
function normalizePattern(pattern) {
|
|
26
|
+
const trimmed = pattern.trim().toLowerCase().replace(/\.$/, '');
|
|
27
|
+
const wildcard = trimmed.startsWith('*.');
|
|
28
|
+
const bare = wildcard ? trimmed.slice(2) : trimmed;
|
|
29
|
+
// IDN 模式(中文/Unicode 域名)转 punycode,与 URL.hostname 对齐
|
|
30
|
+
const ascii = domainToASCII(bare) || bare;
|
|
31
|
+
return { wildcard, suffix: ascii };
|
|
32
|
+
}
|
|
33
|
+
export function domainMatches(pattern, host) {
|
|
34
|
+
const { wildcard, suffix } = normalizePattern(pattern);
|
|
35
|
+
const h = normalizeHost(host);
|
|
36
|
+
if (!suffix)
|
|
37
|
+
return false;
|
|
38
|
+
if (!wildcard)
|
|
39
|
+
return h === suffix;
|
|
40
|
+
// `*.example.com` 匹配 example.com 及其子域(含裸域)
|
|
41
|
+
return h === suffix || (h.length > suffix.length && h.endsWith(`.${suffix}`));
|
|
42
|
+
}
|
|
43
|
+
/** 剥掉 userinfo/query/hash 再进错误与账本(凭据与 query 令牌不入错误/日志) */
|
|
44
|
+
function redactUrl(raw) {
|
|
45
|
+
return raw.replace(/\/\/[^@/]*@/g, '//***@').replace(/[?#].*$/, '');
|
|
46
|
+
}
|
|
47
|
+
export async function assertBrowserUrlAllowed(rawUrl, options) {
|
|
48
|
+
let url;
|
|
49
|
+
try {
|
|
50
|
+
url = new URL(rawUrl);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
throw new Error(`url_not_allowed: invalid url: ${redactUrl(rawUrl)}`);
|
|
54
|
+
}
|
|
55
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
56
|
+
throw new Error(`url_not_allowed: protocol not allowed: ${url.protocol.replace(':', '')}`);
|
|
57
|
+
}
|
|
58
|
+
const host = normalizeHost(url.hostname);
|
|
59
|
+
for (const deny of options.urlPolicy?.deny ?? []) {
|
|
60
|
+
if (domainMatches(deny, host)) {
|
|
61
|
+
throw new Error(`url_not_allowed: domain denied by policy: ${host}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const allow = options.urlPolicy?.allow ?? [];
|
|
65
|
+
if (allow.length > 0 && !allow.some((p) => domainMatches(p, host))) {
|
|
66
|
+
throw new Error(`url_not_allowed: domain not in allow list: ${host}`);
|
|
67
|
+
}
|
|
68
|
+
// 私网/环回门禁(SSRF 面);网络层出口限制另见 KNOWN-ISSUES
|
|
69
|
+
const guard = {
|
|
70
|
+
allowPrivateNetwork: options.allowPrivateNetwork ?? false,
|
|
71
|
+
};
|
|
72
|
+
await assertUrlAllowed(url.href, guard);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 决策归一化(fail-closed):`HitlDecision` 字面量或 `ConfirmOutcome` 对象
|
|
76
|
+
* (生产确认通道返回对象,见 gateway run-assembly 接线);`undefined`/未知
|
|
77
|
+
* 形态一律 deny——放行必须是明确的 approve 系决策。
|
|
78
|
+
*/
|
|
79
|
+
function normalizeDecision(raw) {
|
|
80
|
+
const decision = typeof raw === 'string' ? raw : raw?.decision;
|
|
81
|
+
return decision === 'approve' || decision === 'approve_session' || decision === 'approve_always'
|
|
82
|
+
? 'approve'
|
|
83
|
+
: 'deny';
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 确认闸三档 → 单闸开关的唯一映射(DOM 闸与请求层闸共用;勿在调用点手写档位判断)。
|
|
87
|
+
*
|
|
88
|
+
* @param confirm - 三档配置(缺省 `standard`)
|
|
89
|
+
* @param kind - 闸门种类(`xhr` = 页内 fetch/XHR 外部写)
|
|
90
|
+
* @returns 该闸在当前档位下是否启用
|
|
91
|
+
*/
|
|
92
|
+
export function confirmGateEnabled(confirm, kind) {
|
|
93
|
+
const level = confirm ?? 'standard';
|
|
94
|
+
if (level === 'off')
|
|
95
|
+
return false;
|
|
96
|
+
return kind === 'xhr' ? level === 'strict' : true;
|
|
97
|
+
}
|
|
98
|
+
async function gateSensitiveAction(ctx, confirm, intent, domain, gateTimeoutMs, onApproved) {
|
|
99
|
+
const enabled = confirmGateEnabled(confirm, intent.kind);
|
|
100
|
+
if (!enabled)
|
|
101
|
+
return;
|
|
102
|
+
const gate = ctx.confirmEffect;
|
|
103
|
+
if (!gate) {
|
|
104
|
+
// 缺确认通道时敏感效应拒绝执行(fail-closed,不静默放行)
|
|
105
|
+
throw new Error(`denied: ${intent.kind} requires confirmation but no confirmEffect channel`);
|
|
106
|
+
}
|
|
107
|
+
const label = intent.kind === 'formSubmit'
|
|
108
|
+
? 'form submit'
|
|
109
|
+
: intent.kind === 'fileUpload'
|
|
110
|
+
? 'file upload'
|
|
111
|
+
: 'password fill';
|
|
112
|
+
// 闸门预算必须 ≤ 动作预算(评审 F1/R1:迟到批准不得产生工具已报失败的效果;
|
|
113
|
+
// 宿主不守 timeoutMs 时本地 race 兜底,超时即拒)
|
|
114
|
+
const decision = await Promise.race([
|
|
115
|
+
gate({
|
|
116
|
+
kind: 'other',
|
|
117
|
+
description: `browser ${label}: ${intent.description}`,
|
|
118
|
+
domain,
|
|
119
|
+
timeoutMs: gateTimeoutMs,
|
|
120
|
+
}),
|
|
121
|
+
new Promise((resolve) => {
|
|
122
|
+
const t = setTimeout(() => resolve('gate-timeout'), gateTimeoutMs);
|
|
123
|
+
t.unref?.();
|
|
124
|
+
}),
|
|
125
|
+
]);
|
|
126
|
+
if (decision === 'gate-timeout' || normalizeDecision(decision) === 'deny') {
|
|
127
|
+
audit(ctx, 'browser.act.denied', { reason: intent.kind, description: intent.description });
|
|
128
|
+
throw new Error(`denied: browser ${intent.kind} not approved`);
|
|
129
|
+
}
|
|
130
|
+
onApproved?.();
|
|
131
|
+
}
|
|
132
|
+
function audit(ctx, event, data) {
|
|
133
|
+
ctx.recordOpsEvent?.({ event, data });
|
|
134
|
+
}
|
|
135
|
+
function hostOf(url) {
|
|
136
|
+
try {
|
|
137
|
+
return normalizeHost(new URL(url).hostname);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// about:blank 等无 host URL:确认意图的 domain 留空(如实缺省,不编造)
|
|
141
|
+
return '';
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** 审批缓存键用 origin(协议+主机+端口;评审 F4:hostname 不足以区分 origin) */
|
|
145
|
+
function originOf(url) {
|
|
146
|
+
try {
|
|
147
|
+
return new URL(url).origin;
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return hostOf(url);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// ── P3 拟人化节流(humanize) ──
|
|
154
|
+
/** humanize 缺省参数(可配全覆盖;此处仅缺省值,非隐藏 tunable) */
|
|
155
|
+
const DEFAULT_HUMANIZE = {
|
|
156
|
+
typingDelayMs: [20, 80],
|
|
157
|
+
actionJitterMs: [100, 600],
|
|
158
|
+
maxRetries: 2,
|
|
159
|
+
};
|
|
160
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
161
|
+
/** 区间 [min,max] 毫秒均匀采样(min>max 时按 min 处理,不抛不NaN) */
|
|
162
|
+
function sampleRange(range, fallback) {
|
|
163
|
+
const [min, max] = range ?? fallback;
|
|
164
|
+
const lo = Math.max(0, Math.min(min, max));
|
|
165
|
+
const hi = Math.max(lo, Math.max(min, max));
|
|
166
|
+
return lo + Math.random() * (hi - lo);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 生效 humanize 参数:显式配置优先;stealth 系引擎(stealth/rebrowser/camoufox)
|
|
170
|
+
* 缺省启用;其余引擎无配置则不启用。
|
|
171
|
+
*
|
|
172
|
+
* @param opts - 工具选项(engine + stealth.humanize)
|
|
173
|
+
* @returns 生效参数;undefined = 不启用拟人化
|
|
174
|
+
*/
|
|
175
|
+
export function effectiveHumanize(opts) {
|
|
176
|
+
const explicit = opts.stealth?.humanize;
|
|
177
|
+
if (explicit) {
|
|
178
|
+
return { ...DEFAULT_HUMANIZE, ...Object.fromEntries(Object.entries(explicit).filter(([, v]) => v !== undefined)) };
|
|
179
|
+
}
|
|
180
|
+
const engine = opts.engine;
|
|
181
|
+
if (engine === 'stealth' || engine === 'rebrowser' || engine === 'camoufox') {
|
|
182
|
+
return { ...DEFAULT_HUMANIZE };
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
// ── 会话解析(归属校验 + 按需创建) ──
|
|
187
|
+
async function resolveSession(service, explicit, ctx, launch, createIfMissing) {
|
|
188
|
+
if (typeof explicit === 'string' && explicit.trim()) {
|
|
189
|
+
const found = await service.byId(explicit.trim());
|
|
190
|
+
if (!found) {
|
|
191
|
+
throw new Error(`browser_session_not_found: ${explicit}`);
|
|
192
|
+
}
|
|
193
|
+
// 归属校验:显式引用也必须属于本会话(防跨会话操作别人的浏览器)
|
|
194
|
+
if (found.ownerKey && found.ownerKey !== ctx.sessionId) {
|
|
195
|
+
throw new Error(`denied: browser session ${explicit} belongs to another session`);
|
|
196
|
+
}
|
|
197
|
+
return found;
|
|
198
|
+
}
|
|
199
|
+
if (createIfMissing) {
|
|
200
|
+
return service.acquire(ctx.sessionId, launch);
|
|
201
|
+
}
|
|
202
|
+
const existing = await service.get(ctx.sessionId);
|
|
203
|
+
if (!existing) {
|
|
204
|
+
throw new Error('browser_session_not_found: no active browser session — call browser_navigate first');
|
|
205
|
+
}
|
|
206
|
+
return existing;
|
|
207
|
+
}
|
|
208
|
+
function buildRequestPolicy(ctx, opts, approvals, gateTimeoutMs) {
|
|
209
|
+
// N1:CDP Fetch 回调由 socket 驱动,不继承运行期 ALS——宿主 confirmEffect 按
|
|
210
|
+
// getRunScope() 定界会拿不到 scope(无卡全拒)。在 handler 执行期捕获 scope,
|
|
211
|
+
// 回调里重入;无 scope(单测直调)则原样执行。
|
|
212
|
+
const scope = getRunScope();
|
|
213
|
+
const inScope = (fn) => (scope ? runInScope(scope, fn) : fn());
|
|
214
|
+
return {
|
|
215
|
+
checkUrl: async (url) => {
|
|
216
|
+
await assertBrowserUrlAllowed(url, opts);
|
|
217
|
+
},
|
|
218
|
+
confirmWrite: async ({ method, url, documentLevel }) => {
|
|
219
|
+
const enabled = confirmGateEnabled(opts.confirm, documentLevel ? 'formSubmit' : 'xhr');
|
|
220
|
+
if (!enabled)
|
|
221
|
+
return true;
|
|
222
|
+
// 审批缓存键 = method+origin(与契约/文档一致);DOM 闸批准会预热同一键(避免同一提交问两次)
|
|
223
|
+
const cacheKey = `${method} ${originOf(url)}`;
|
|
224
|
+
if (approvals.has(cacheKey))
|
|
225
|
+
return true;
|
|
226
|
+
const gate = inScope(() => ctx.confirmEffect);
|
|
227
|
+
if (!gate)
|
|
228
|
+
return false;
|
|
229
|
+
// 闸门预算必须 ≤ 动作预算:超时即拒、请求 failRequest——迟到批准不得发出写(评审 F1)
|
|
230
|
+
const decision = await Promise.race([
|
|
231
|
+
inScope(() => gate({
|
|
232
|
+
kind: 'other',
|
|
233
|
+
description: `browser write ${method} ${redactUrl(url)}${documentLevel ? '' : ' (page script)'}`,
|
|
234
|
+
domain: hostOf(url),
|
|
235
|
+
timeoutMs: gateTimeoutMs,
|
|
236
|
+
})),
|
|
237
|
+
new Promise((resolve) => {
|
|
238
|
+
const t = setTimeout(() => resolve('gate-timeout'), gateTimeoutMs);
|
|
239
|
+
t.unref?.();
|
|
240
|
+
}),
|
|
241
|
+
]);
|
|
242
|
+
if (decision === 'gate-timeout')
|
|
243
|
+
return false;
|
|
244
|
+
const allowed = normalizeDecision(decision) !== 'deny';
|
|
245
|
+
if (allowed)
|
|
246
|
+
approvals.add(cacheKey);
|
|
247
|
+
return allowed;
|
|
248
|
+
},
|
|
249
|
+
record: (event) => {
|
|
250
|
+
inScope(() => {
|
|
251
|
+
audit(ctx, `browser.${event.kind}`, {
|
|
252
|
+
url: redactUrl(event.url),
|
|
253
|
+
...(event.method ? { method: event.method } : {}),
|
|
254
|
+
...(event.reason ? { reason: event.reason } : {}),
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
// ── 工具面 ──
|
|
261
|
+
export function createBrowserTools(service, options = {}) {
|
|
262
|
+
// 会话级 method+origin 审批缓存(DOM 闸与请求层闸共享;键随会话 key 归属)
|
|
263
|
+
const writeApprovals = new Map();
|
|
264
|
+
const approvalsFor = (ctx) => {
|
|
265
|
+
let set = writeApprovals.get(ctx.sessionId);
|
|
266
|
+
if (!set) {
|
|
267
|
+
set = new Set();
|
|
268
|
+
writeApprovals.set(ctx.sessionId, set);
|
|
269
|
+
}
|
|
270
|
+
return set;
|
|
271
|
+
};
|
|
272
|
+
// 闸门预算 ≤ 动作预算(评审 F1:迟到批准不得产生"工具已报失败"的写入)
|
|
273
|
+
const gateTimeoutMs = Math.max(500, (options.timeoutMs ?? BROWSER_DEFAULT_TIMEOUT_MS) - 2_000);
|
|
274
|
+
const policyFor = (ctx) => buildRequestPolicy(ctx, options, approvalsFor(ctx), gateTimeoutMs);
|
|
275
|
+
const launchFor = (ctx) => {
|
|
276
|
+
const profileId = options.profiles?.defaultProfile;
|
|
277
|
+
if (profileId && /[\\/]/.test(profileId)) {
|
|
278
|
+
// profileId 进文件名(storageState 路径拼接):路径字符直接拒(防越出 profile 目录)
|
|
279
|
+
throw new Error(`browser_profiles: defaultProfile must not contain path separators: ${profileId}`);
|
|
280
|
+
}
|
|
281
|
+
const humanize = effectiveHumanize(options);
|
|
282
|
+
return {
|
|
283
|
+
ownerKey: ctx.sessionId,
|
|
284
|
+
policy: policyFor(ctx),
|
|
285
|
+
...(profileId ? { profileId } : {}),
|
|
286
|
+
...(options.profileDir ? { profileDir: options.profileDir } : {}),
|
|
287
|
+
...(options.profiles?.persist ? { persistProfile: true } : {}),
|
|
288
|
+
...(humanize ? { humanize } : {}),
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
const snapshotArgs = {
|
|
292
|
+
session: {
|
|
293
|
+
type: 'string',
|
|
294
|
+
description: 'Browser session id from a previous browser_* result (default: this conversation session)',
|
|
295
|
+
},
|
|
296
|
+
interactiveOnly: {
|
|
297
|
+
type: 'boolean',
|
|
298
|
+
description: 'Only list interactive elements (cheaper)',
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
const navigateTool = {
|
|
302
|
+
definition: {
|
|
303
|
+
name: 'browser_navigate',
|
|
304
|
+
description: 'Open a URL in the agent browser and return the page snapshot (interactive elements with refs + condensed text). Launches the browser on first use. Refs are valid until the next snapshot/navigation. URL policy (domains/private-net) and write-confirmation gates apply.',
|
|
305
|
+
parameters: {
|
|
306
|
+
url: { type: 'string', description: 'http(s) URL to open' },
|
|
307
|
+
session: snapshotArgs.session,
|
|
308
|
+
},
|
|
309
|
+
capabilities: ['egress'],
|
|
310
|
+
timeoutMs: options.timeoutMs ?? BROWSER_DEFAULT_TIMEOUT_MS,
|
|
311
|
+
},
|
|
312
|
+
source: { kind: 'builtin', origin: 'browser', trustLevel: 'builtin' },
|
|
313
|
+
executionMode: 'sequential',
|
|
314
|
+
handler: async (args, ctx) => {
|
|
315
|
+
const rawUrl = String(args.url ?? '');
|
|
316
|
+
// 预检(fail-fast + 高质量错误);请求层拦截是权威兜底(重定向/子资源全覆盖)
|
|
317
|
+
await assertBrowserUrlAllowed(rawUrl, options);
|
|
318
|
+
// 用归一化 href 导航(与判定目标一致,避免同 URL 双形态)
|
|
319
|
+
const target = new URL(rawUrl).href;
|
|
320
|
+
const runOnce = async (s) => {
|
|
321
|
+
const result = await s.goto(target, {
|
|
322
|
+
waitUntil: 'load',
|
|
323
|
+
timeoutMs: options.timeoutMs,
|
|
324
|
+
});
|
|
325
|
+
audit(ctx, 'browser.navigate', { url: redactUrl(result.url), sessionId: s.id });
|
|
326
|
+
const snapshot = await s.snapshot({
|
|
327
|
+
maxChars: options.snapshotMaxChars,
|
|
328
|
+
});
|
|
329
|
+
const challenge = await s.detectChallenge();
|
|
330
|
+
return {
|
|
331
|
+
sessionId: s.id,
|
|
332
|
+
engine: s.engine,
|
|
333
|
+
...result,
|
|
334
|
+
...(challenge
|
|
335
|
+
? {
|
|
336
|
+
challenge,
|
|
337
|
+
hint: 'challenge page detected — ask the user for help via ask_user; in attach mode the user can solve it in the open browser, then call browser_snapshot to re-check',
|
|
338
|
+
}
|
|
339
|
+
: {}),
|
|
340
|
+
snapshot,
|
|
341
|
+
};
|
|
342
|
+
};
|
|
343
|
+
try {
|
|
344
|
+
const session = await resolveSession(service, args.session, ctx, launchFor(ctx), true);
|
|
345
|
+
const out = await runOnce(session);
|
|
346
|
+
// P3:挑战驱动的后端轮换(stealth 链)——换后端重试一次,仍命中走 HITL 出口
|
|
347
|
+
if (out.challenge && typeof service.rotateAfterChallenge === 'function') {
|
|
348
|
+
const rotated = await service.rotateAfterChallenge(ctx.sessionId);
|
|
349
|
+
if (rotated) {
|
|
350
|
+
audit(ctx, 'browser.challenge.rotate', {
|
|
351
|
+
url: redactUrl(target),
|
|
352
|
+
fromSessionId: session.id,
|
|
353
|
+
});
|
|
354
|
+
const retrySession = await resolveSession(service, args.session, ctx, launchFor(ctx), true);
|
|
355
|
+
return await runOnce(retrySession);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
360
|
+
catch (err) {
|
|
361
|
+
audit(ctx, 'browser.navigate.failed', {
|
|
362
|
+
url: redactUrl(rawUrl),
|
|
363
|
+
reason: err instanceof Error ? err.message : String(err),
|
|
364
|
+
});
|
|
365
|
+
throw err;
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
const snapshotTool = {
|
|
370
|
+
definition: {
|
|
371
|
+
name: 'browser_snapshot',
|
|
372
|
+
description: 'Get a fresh snapshot of the current page (interactive elements with refs + condensed text in document order). Call this after navigating or when refs went stale. Does not launch a browser — an active session is required.',
|
|
373
|
+
parameters: snapshotArgs,
|
|
374
|
+
capabilities: ['egress'],
|
|
375
|
+
timeoutMs: options.timeoutMs ?? BROWSER_DEFAULT_TIMEOUT_MS,
|
|
376
|
+
},
|
|
377
|
+
source: { kind: 'builtin', origin: 'browser', trustLevel: 'builtin' },
|
|
378
|
+
executionMode: 'sequential',
|
|
379
|
+
handler: async (args, ctx) => {
|
|
380
|
+
const session = await resolveSession(service, args.session, ctx, launchFor(ctx), false);
|
|
381
|
+
const snapshot = await session.snapshot({
|
|
382
|
+
interactiveOnly: Boolean(args.interactiveOnly),
|
|
383
|
+
maxChars: options.snapshotMaxChars,
|
|
384
|
+
});
|
|
385
|
+
// 挑战复检点(评审 F10):hint 承诺 browser_snapshot 可复检——这里兑现
|
|
386
|
+
const challenge = await session.detectChallenge();
|
|
387
|
+
return {
|
|
388
|
+
sessionId: session.id,
|
|
389
|
+
...snapshot,
|
|
390
|
+
...(challenge
|
|
391
|
+
? {
|
|
392
|
+
challenge,
|
|
393
|
+
hint: 'challenge page detected — ask the user for help via ask_user; in attach mode the user can solve it in the open browser, then call browser_snapshot to re-check',
|
|
394
|
+
}
|
|
395
|
+
: {}),
|
|
396
|
+
};
|
|
397
|
+
},
|
|
398
|
+
};
|
|
399
|
+
const actTool = {
|
|
400
|
+
definition: {
|
|
401
|
+
name: 'browser_act',
|
|
402
|
+
description: 'Act on the current page: action=click|fill|select|check|uncheck|hover|press|scroll|wait|upload. Use refs from the latest snapshot (stale refs error). Sensitive effects (password fills, form submits, file uploads) go through a human confirmation gate; submissions are also gated at the request layer.',
|
|
403
|
+
parameters: {
|
|
404
|
+
action: {
|
|
405
|
+
type: 'string',
|
|
406
|
+
description: 'click | fill | select | check | uncheck | hover | press | scroll | wait | upload',
|
|
407
|
+
},
|
|
408
|
+
ref: { type: 'string', description: 'element ref from the latest snapshot' },
|
|
409
|
+
value: { type: 'string', description: 'value for fill / select' },
|
|
410
|
+
key: { type: 'string', description: 'key name for press (e.g. Enter, Tab)' },
|
|
411
|
+
deltaY: { type: 'number', description: 'scroll distance for scroll (default 600)' },
|
|
412
|
+
ms: { type: 'number', description: 'wait duration in ms for wait (max 10000)' },
|
|
413
|
+
path: { type: 'string', description: 'file path to set for upload (workspace-relative or absolute)' },
|
|
414
|
+
session: snapshotArgs.session,
|
|
415
|
+
},
|
|
416
|
+
capabilities: ['egress'],
|
|
417
|
+
timeoutMs: options.timeoutMs ?? BROWSER_DEFAULT_TIMEOUT_MS,
|
|
418
|
+
},
|
|
419
|
+
source: { kind: 'builtin', origin: 'browser', trustLevel: 'builtin' },
|
|
420
|
+
executionMode: 'sequential',
|
|
421
|
+
handler: async (args, ctx) => {
|
|
422
|
+
const session = await resolveSession(service, args.session, ctx, launchFor(ctx), false);
|
|
423
|
+
const action = buildPageAction(args);
|
|
424
|
+
// upload:路径过包含原语(workspace/effectPolicy 根约束),外发文件进账本
|
|
425
|
+
if (action.kind === 'upload') {
|
|
426
|
+
const cwd = ctx.cwd ?? process.cwd();
|
|
427
|
+
action.path = resolveToolPath(action.path, cwd, containmentOptions(ctx));
|
|
428
|
+
// 意图级账本(尝试);成功后另有 browser.upload 结果条目(评审 F12)
|
|
429
|
+
audit(ctx, 'browser.upload.attempt', { path: action.path, sessionId: session.id });
|
|
430
|
+
}
|
|
431
|
+
const intent = await session.inspectAction(action);
|
|
432
|
+
const domain = hostOf(session.url);
|
|
433
|
+
if (intent.sensitive !== false) {
|
|
434
|
+
await gateSensitiveAction(ctx, options.confirm, intent.sensitive, domain, gateTimeoutMs, () => {
|
|
435
|
+
// 表单提交批准 → 预热请求层缓存(同一 POST 不再二次询问)
|
|
436
|
+
if (intent.sensitive !== false && intent.sensitive.kind === 'formSubmit') {
|
|
437
|
+
approvalsFor(ctx).add(`POST ${originOf(session.url)}`);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
audit(ctx, 'browser.act.sensitive', {
|
|
441
|
+
kind: intent.sensitive.kind,
|
|
442
|
+
target: intent.target,
|
|
443
|
+
domain,
|
|
444
|
+
sessionId: session.id,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
// P3 拟人化:动作前抖动(humanize 启用时)+ 有界重试(退避随次数放大)
|
|
448
|
+
const humanize = effectiveHumanize(options);
|
|
449
|
+
if (humanize) {
|
|
450
|
+
await sleep(sampleRange(humanize.actionJitterMs, DEFAULT_HUMANIZE.actionJitterMs));
|
|
451
|
+
}
|
|
452
|
+
const maxRetries = humanize?.maxRetries ?? 0;
|
|
453
|
+
let result;
|
|
454
|
+
for (let attempt = 0;; attempt += 1) {
|
|
455
|
+
try {
|
|
456
|
+
result = await session.act(action);
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
catch (err) {
|
|
460
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
461
|
+
// stale_ref(ref 过期)与策略拒绝不是瞬态:重试只会重复错误,直接抛
|
|
462
|
+
if (attempt >= maxRetries || /stale_ref|^denied:/.test(message))
|
|
463
|
+
throw err;
|
|
464
|
+
await sleep(sampleRange(humanize?.actionJitterMs, DEFAULT_HUMANIZE.actionJitterMs) * (attempt + 1));
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (action.kind === 'upload') {
|
|
468
|
+
audit(ctx, 'browser.upload', { path: action.path, sessionId: session.id });
|
|
469
|
+
}
|
|
470
|
+
return result;
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
const screenshotTool = {
|
|
474
|
+
definition: {
|
|
475
|
+
name: 'browser_screenshot',
|
|
476
|
+
description: 'Save a screenshot of the current page (or one element) to a PNG/JPEG file and return its path. The image is NOT inlined (tool results are text-only) — use send_file to show it to the user.',
|
|
477
|
+
parameters: {
|
|
478
|
+
fullPage: { type: 'boolean', description: 'capture the full scrollable page' },
|
|
479
|
+
ref: { type: 'string', description: 'element ref from the latest snapshot (element shot)' },
|
|
480
|
+
type: { type: 'string', description: 'png (default) | jpeg' },
|
|
481
|
+
session: snapshotArgs.session,
|
|
482
|
+
},
|
|
483
|
+
capabilities: ['egress'],
|
|
484
|
+
timeoutMs: options.timeoutMs ?? BROWSER_DEFAULT_TIMEOUT_MS,
|
|
485
|
+
},
|
|
486
|
+
source: { kind: 'builtin', origin: 'browser', trustLevel: 'builtin' },
|
|
487
|
+
executionMode: 'sequential',
|
|
488
|
+
handler: async (args, ctx) => {
|
|
489
|
+
const session = await resolveSession(service, args.session, ctx, launchFor(ctx), false);
|
|
490
|
+
const shot = await session.screenshot({
|
|
491
|
+
fullPage: Boolean(args.fullPage),
|
|
492
|
+
type: args.type === 'jpeg' ? 'jpeg' : 'png',
|
|
493
|
+
...(typeof args.ref === 'string' && args.ref.trim()
|
|
494
|
+
? { ref: args.ref.trim() }
|
|
495
|
+
: {}),
|
|
496
|
+
});
|
|
497
|
+
// 路径与其它文件工具同一包含原语(resolveToolPath + containmentOptions)
|
|
498
|
+
const cwd = ctx.cwd ?? process.cwd();
|
|
499
|
+
const ext = shot.mimeType === 'image/jpeg' ? 'jpg' : 'png';
|
|
500
|
+
const filename = `browser-${session.id}-${Date.now()}-${randomUUID().slice(0, 8)}.${ext}`;
|
|
501
|
+
const path = resolveToolPath(join('browser-screenshots', filename), cwd, containmentOptions(ctx));
|
|
502
|
+
// 与 file_write 同惯例:先建父目录再落盘
|
|
503
|
+
const { mkdir } = await import('node:fs/promises');
|
|
504
|
+
const { dirname } = await import('node:path');
|
|
505
|
+
await mkdir(dirname(path), { recursive: true });
|
|
506
|
+
await ctx.fileEffects.writeFile(path, shot.data);
|
|
507
|
+
audit(ctx, 'browser.screenshot', { path, sessionId: session.id });
|
|
508
|
+
return {
|
|
509
|
+
path,
|
|
510
|
+
mimeType: shot.mimeType,
|
|
511
|
+
bytes: shot.data.byteLength,
|
|
512
|
+
note: 'image saved to path; not inlined — use send_file to show the user',
|
|
513
|
+
};
|
|
514
|
+
},
|
|
515
|
+
};
|
|
516
|
+
const extractTool = {
|
|
517
|
+
definition: {
|
|
518
|
+
name: 'browser_extract',
|
|
519
|
+
description: 'Deterministic structured extraction from the current page: fields = { name: { selector, attribute?, multiple? } } (CSS selectors). Returns real DOM data; fields with no match are null. For semantic reading use browser_snapshot instead.',
|
|
520
|
+
parameters: {
|
|
521
|
+
fields: {
|
|
522
|
+
type: 'object',
|
|
523
|
+
description: 'map of field name → { selector: css, attribute?: attr name (default: text), multiple?: boolean }',
|
|
524
|
+
},
|
|
525
|
+
session: snapshotArgs.session,
|
|
526
|
+
},
|
|
527
|
+
capabilities: ['egress'],
|
|
528
|
+
timeoutMs: options.timeoutMs ?? BROWSER_DEFAULT_TIMEOUT_MS,
|
|
529
|
+
},
|
|
530
|
+
source: { kind: 'builtin', origin: 'browser', trustLevel: 'builtin' },
|
|
531
|
+
executionMode: 'sequential',
|
|
532
|
+
handler: async (args, ctx) => {
|
|
533
|
+
const session = await resolveSession(service, args.session, ctx, launchFor(ctx), false);
|
|
534
|
+
const spec = parseExtractSpec(args.fields);
|
|
535
|
+
const outcome = await session.extract(spec);
|
|
536
|
+
// 聚合字符预算(评审 N5):全字段共享 budget,截断如实标记;counts 透出命中总数
|
|
537
|
+
const budget = options.snapshotMaxChars ?? 8000;
|
|
538
|
+
let truncated = outcome.truncated;
|
|
539
|
+
let remaining = budget;
|
|
540
|
+
const trimmed = {};
|
|
541
|
+
for (const [key, value] of Object.entries(outcome.data)) {
|
|
542
|
+
if (typeof value === 'string') {
|
|
543
|
+
const take = Math.min(value.length, Math.max(remaining, 0));
|
|
544
|
+
if (take < value.length)
|
|
545
|
+
truncated = true;
|
|
546
|
+
trimmed[key] = value.slice(0, take);
|
|
547
|
+
remaining -= take;
|
|
548
|
+
}
|
|
549
|
+
else if (Array.isArray(value)) {
|
|
550
|
+
trimmed[key] = value.map((s) => {
|
|
551
|
+
if (typeof s !== 'string')
|
|
552
|
+
return s;
|
|
553
|
+
const take = Math.min(s.length, Math.max(remaining, 0));
|
|
554
|
+
if (take < s.length)
|
|
555
|
+
truncated = true;
|
|
556
|
+
remaining -= take;
|
|
557
|
+
return s.slice(0, take);
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
else {
|
|
561
|
+
trimmed[key] = value;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return {
|
|
565
|
+
sessionId: session.id,
|
|
566
|
+
url: session.url,
|
|
567
|
+
data: trimmed,
|
|
568
|
+
truncated,
|
|
569
|
+
...(outcome.counts ? { counts: outcome.counts } : {}),
|
|
570
|
+
};
|
|
571
|
+
},
|
|
572
|
+
};
|
|
573
|
+
const closeTool = {
|
|
574
|
+
definition: {
|
|
575
|
+
name: 'browser_close',
|
|
576
|
+
description: 'Close the browser session (this conversation session or an explicit session id). Idempotent.',
|
|
577
|
+
parameters: {
|
|
578
|
+
session: snapshotArgs.session,
|
|
579
|
+
},
|
|
580
|
+
capabilities: ['egress'],
|
|
581
|
+
timeoutMs: options.timeoutMs ?? BROWSER_DEFAULT_TIMEOUT_MS,
|
|
582
|
+
},
|
|
583
|
+
source: { kind: 'builtin', origin: 'browser', trustLevel: 'builtin' },
|
|
584
|
+
executionMode: 'sequential',
|
|
585
|
+
handler: async (args, ctx) => {
|
|
586
|
+
const explicit = typeof args.session === 'string' && args.session.trim() ? args.session.trim() : '';
|
|
587
|
+
if (explicit) {
|
|
588
|
+
// 归属校验:显式关闭同样不得跨会话(N5)
|
|
589
|
+
const found = await service.byId(explicit);
|
|
590
|
+
if (found && found.ownerKey && found.ownerKey !== ctx.sessionId) {
|
|
591
|
+
throw new Error(`denied: browser session ${explicit} belongs to another session`);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
const keyOrId = explicit || ctx.sessionId;
|
|
595
|
+
const closed = await service.close(keyOrId);
|
|
596
|
+
// 关会话即撤销写批准(KNOWN-ISSUES 语义兑现;重开会话不得沿用旧批准)
|
|
597
|
+
writeApprovals.delete(ctx.sessionId);
|
|
598
|
+
audit(ctx, 'browser.close', { keyOrId, closed });
|
|
599
|
+
return { ok: true, closed };
|
|
600
|
+
},
|
|
601
|
+
};
|
|
602
|
+
return [navigateTool, snapshotTool, actTool, screenshotTool, extractTool, closeTool];
|
|
603
|
+
}
|
|
604
|
+
function buildPageAction(args) {
|
|
605
|
+
const kind = String(args.action ?? '');
|
|
606
|
+
const ref = typeof args.ref === 'string' && args.ref.trim() ? args.ref.trim() : undefined;
|
|
607
|
+
switch (kind) {
|
|
608
|
+
case 'click':
|
|
609
|
+
requireRef(kind, ref);
|
|
610
|
+
return { kind: 'click', ref };
|
|
611
|
+
case 'fill': {
|
|
612
|
+
requireRef(kind, ref);
|
|
613
|
+
const value = args.value === undefined ? '' : String(args.value);
|
|
614
|
+
return { kind: 'fill', ref, value };
|
|
615
|
+
}
|
|
616
|
+
case 'select': {
|
|
617
|
+
requireRef(kind, ref);
|
|
618
|
+
if (args.value === undefined)
|
|
619
|
+
throw new Error('browser_act: select requires value');
|
|
620
|
+
return { kind: 'select', ref, value: String(args.value) };
|
|
621
|
+
}
|
|
622
|
+
case 'check':
|
|
623
|
+
requireRef(kind, ref);
|
|
624
|
+
return { kind: 'check', ref };
|
|
625
|
+
case 'uncheck':
|
|
626
|
+
requireRef(kind, ref);
|
|
627
|
+
return { kind: 'uncheck', ref };
|
|
628
|
+
case 'hover':
|
|
629
|
+
requireRef(kind, ref);
|
|
630
|
+
return { kind: 'hover', ref };
|
|
631
|
+
case 'press': {
|
|
632
|
+
if (!args.key)
|
|
633
|
+
throw new Error('browser_act: press requires key');
|
|
634
|
+
return { kind: 'press', key: String(args.key), ...(ref ? { ref } : {}) };
|
|
635
|
+
}
|
|
636
|
+
case 'scroll':
|
|
637
|
+
return {
|
|
638
|
+
kind: 'scroll',
|
|
639
|
+
deltaY: args.deltaY === undefined ? 600 : toFiniteNumber(args.deltaY, 'deltaY'),
|
|
640
|
+
};
|
|
641
|
+
case 'wait': {
|
|
642
|
+
const ms = args.ms === undefined ? 0 : toFiniteNumber(args.ms, 'ms');
|
|
643
|
+
return { kind: 'wait', ms };
|
|
644
|
+
}
|
|
645
|
+
case 'upload': {
|
|
646
|
+
requireRef('upload', ref);
|
|
647
|
+
const path = String(args.path ?? '').trim();
|
|
648
|
+
if (!path)
|
|
649
|
+
throw new Error('browser_act: upload requires path');
|
|
650
|
+
return { kind: 'upload', ref, path };
|
|
651
|
+
}
|
|
652
|
+
default:
|
|
653
|
+
throw new Error(`browser_act: unknown action: ${kind || '(missing)'}`);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function requireRef(kind, ref) {
|
|
657
|
+
if (!ref)
|
|
658
|
+
throw new Error(`browser_act: ${kind} requires ref`);
|
|
659
|
+
}
|
|
660
|
+
/** 数字参数校验:非有限数(NaN/Infinity)直接拒绝,不静默兜底 */
|
|
661
|
+
function toFiniteNumber(value, name) {
|
|
662
|
+
const n = Number(value);
|
|
663
|
+
if (!Number.isFinite(n)) {
|
|
664
|
+
throw new Error(`browser_act: ${name} must be a finite number`);
|
|
665
|
+
}
|
|
666
|
+
return n;
|
|
667
|
+
}
|
|
668
|
+
/** 抽取规格校验:结构不对就 fail-loud(不猜语义、不静默丢字段) */
|
|
669
|
+
function parseExtractSpec(raw) {
|
|
670
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
671
|
+
throw new Error('browser_extract: fields must be an object of name → { selector, ... }');
|
|
672
|
+
}
|
|
673
|
+
const spec = {};
|
|
674
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
675
|
+
const field = (value ?? {});
|
|
676
|
+
if (typeof field.selector !== 'string' || !field.selector.trim()) {
|
|
677
|
+
throw new Error(`browser_extract: fields.${key}.selector must be a non-empty string`);
|
|
678
|
+
}
|
|
679
|
+
if (field.maxItems !== undefined && (!Number.isInteger(field.maxItems) || field.maxItems < 1)) {
|
|
680
|
+
throw new Error(`browser_extract: fields.${key}.maxItems must be a positive integer`);
|
|
681
|
+
}
|
|
682
|
+
spec[key] = {
|
|
683
|
+
selector: field.selector.trim(),
|
|
684
|
+
...(typeof field.attribute === 'string' && field.attribute.trim()
|
|
685
|
+
? { attribute: field.attribute.trim() }
|
|
686
|
+
: {}),
|
|
687
|
+
...(field.multiple === true ? { multiple: true } : {}),
|
|
688
|
+
...(field.maxItems === undefined ? {} : { maxItems: field.maxItems }),
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
return spec;
|
|
692
|
+
}
|