@tbox.cn/app-template-agent 1.0.0-alpha.1 → 1.0.0-alpha.6
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/._.gitignore +0 -0
- package/._.nvmrc +0 -0
- package/._AGENTS.md +0 -0
- package/._ENV.md +0 -0
- package/._LEGAL.md +0 -0
- package/._LICENSE +0 -0
- package/._README.md +0 -0
- package/._eslint.config.mjs +0 -0
- package/._package.json +0 -0
- package/._pnpm-lock.template.yaml +0 -0
- package/._pnpm-workspace.template.yaml +0 -0
- package/._tsconfig.base.json +0 -0
- package/.gitignore +7 -2
- package/.tbox/._app.json +0 -0
- package/.tbox/._default-modules.json +0 -0
- package/.tbox/._miniapp-containers.json +0 -0
- package/.tbox/app.json +1 -1
- package/.tbox/card-render/._demo.html +0 -0
- package/.tbox/card-render/._demo.png +0 -0
- package/.tbox/card-render/._scale-demo.audit.html +0 -0
- package/.tbox/card-render/._scale-demo.html +0 -0
- package/.tbox/card-render/baseline/._demo.png +0 -0
- package/.tbox/card-render/baseline/demo.png +0 -0
- package/.tbox/card-render/demo.html +342 -0
- package/.tbox/card-render/demo.png +0 -0
- package/.tbox/card-render/scale-demo.audit.html +984 -0
- package/.tbox/card-render/scale-demo.html +939 -0
- package/.tbox/default-modules.json +1 -1
- package/.tbox/evidence/._cards.txt +0 -0
- package/.tbox/evidence/._gate.json +0 -0
- package/.tbox/evidence/._last-gate.json +0 -0
- package/.tbox/evidence/._scenarios.json +0 -0
- package/.tbox/evidence/cards.txt +1 -0
- package/.tbox/evidence/gate.json +1 -0
- package/.tbox/evidence/last-gate.json +1 -0
- package/.tbox/evidence/scenarios.json +1 -0
- package/.tbox/miniapp-containers.json +1 -1
- package/AGENTS.md +59 -13
- package/ENV.md +4 -4
- package/README.md +22 -20
- package/apps/._AGENTS.md +0 -0
- package/apps/AGENTS.md +0 -4
- package/apps/client/._STYLE_GUIDE.md +0 -0
- package/apps/client/._VISUAL_RESTORE.md +0 -0
- package/apps/client/._index.html +0 -0
- package/apps/client/._package.json +0 -0
- package/apps/client/._tsconfig.json +0 -0
- package/apps/client/._vite.config.ts +0 -0
- package/apps/client/.tbox-gen/._manifest.json +0 -0
- package/apps/client/.tbox-gen/._registry.client.ts +0 -0
- package/apps/client/.tbox-gen/._registry.server.ts +0 -0
- package/apps/client/.tbox-gen/documents/._index.html +0 -0
- package/apps/client/.tbox-gen/documents/_tbox/._card-preview.html +0 -0
- package/apps/client/.tbox-gen/documents/_tbox/card-preview.html +38 -14
- package/apps/client/.tbox-gen/documents/index.html +38 -14
- package/apps/client/.tbox-gen/entries/._card-preview.tsx +0 -0
- package/apps/client/.tbox-gen/entries/._index.tsx +0 -0
- package/apps/client/.tbox-gen/entries/card-preview.tsx +3 -15
- package/apps/client/.tbox-gen/entries/index.tsx +8 -15
- package/apps/client/.tbox-gen/registry.server.ts +4 -4
- package/apps/client/STYLE_GUIDE.md +31 -64
- package/apps/client/index.html +38 -14
- package/apps/client/package.json +3 -3
- package/apps/client/scripts/._check-evidence.mjs +0 -0
- package/apps/client/scripts/._render-card.mjs +0 -0
- package/apps/client/scripts/check-evidence.mjs +688 -0
- package/apps/client/scripts/render-card.mjs +743 -0
- package/apps/client/src/._app.config.ts +0 -0
- package/apps/client/src/._vite-env.d.ts +0 -0
- package/apps/client/src/app.config.ts +40 -0
- package/apps/client/src/cards/._AGENTS.md +0 -0
- package/apps/client/src/cards/._registry.ts +0 -0
- package/apps/client/src/cards/AGENTS.md +11 -0
- package/apps/client/src/pages/._page.tsx +0 -0
- package/apps/client/src/pages/index/._index.tsx +0 -0
- package/apps/client/src/pages/index/index.tsx +1 -1
- package/apps/client/src/pages/page.tsx +2 -2
- package/apps/client/src/store/._chat.ts +0 -0
- package/apps/client/src/store/chat.ts +7 -3
- package/apps/client/src/styles/._brand.css +0 -0
- package/apps/client/src/styles/._tailwind.css +0 -0
- package/apps/client/src/styles/brand.css +51 -0
- package/apps/client/src/styles/tailwind.css +307 -0
- package/apps/client/src/test/._setup.ts +0 -0
- package/apps/client/src/vite-env.d.ts +0 -2
- package/apps/client/vite.config.ts +5 -22
- package/apps/server/._package.json +0 -0
- package/apps/server/._tsconfig.json +0 -0
- package/apps/server/._tsup.config.ts +0 -0
- package/apps/server/._vitest.config.ts +0 -0
- package/apps/server/scripts/._probe.mjs +0 -0
- package/apps/server/scripts/._scenario-lib.mjs +0 -0
- package/apps/server/scripts/._scenario.mjs +0 -0
- package/apps/server/scripts/probe.mjs +382 -0
- package/apps/server/scripts/scenario-lib.mjs +223 -0
- package/apps/server/scripts/scenario.mjs +33 -0
- package/apps/server/src/._app.ts +0 -0
- package/apps/server/src/._dev.ts +0 -0
- package/apps/server/src/._index.ts +0 -0
- package/apps/server/src/._intent-routes.ts +0 -0
- package/apps/server/src/._pre-tools.ts +0 -0
- package/apps/server/src/app.ts +10 -3
- package/apps/server/src/cards/._AGENTS.md +0 -0
- package/apps/server/src/cards/AGENTS.md +19 -0
- package/apps/server/src/cli/._collect-registry.ts +0 -0
- package/apps/server/src/cli/._plugin-doctor.ts +0 -0
- package/apps/server/src/cli/._plugin-inspect.ts +0 -0
- package/apps/server/src/config/._integration.ts +0 -0
- package/apps/server/src/dev.ts +2 -2
- package/apps/server/src/index.ts +2 -2
- package/apps/server/src/pre-tools.ts +7 -16
- package/apps/server/src/tools/._AGENTS.md +0 -0
- package/apps/server/src/tools/AGENTS.md +20 -0
- package/apps/server/tests/._agent-prompt.test.ts +0 -0
- package/apps/server/tests/._cards-registry.test.ts +0 -0
- package/apps/server/tests/._deployment-loader.test.ts +0 -0
- package/apps/server/tests/._intent-dispatcher.test.ts +0 -0
- package/apps/server/tests/._tool-runtime.test.ts +0 -0
- package/apps/server/tests/helpers/._register.ts +0 -0
- package/apps/server/tests/tool-runtime.test.ts +0 -27
- package/config/._AGENTS.md +0 -0
- package/config/._README.md +0 -0
- package/config/._chat.json +0 -0
- package/config/._integrations.json +0 -0
- package/config/._llm_provider.json +0 -0
- package/config/._plugins.json +0 -0
- package/config/AGENTS.md +13 -0
- package/config/README.md +4 -4
- package/config/chat.json +8 -2
- package/config/integrations.json +1 -1
- package/docs/agent/._agent-prompt.md +0 -0
- package/docs/agent/._card-action.md +0 -0
- package/docs/agent/._card.md +0 -0
- package/docs/agent/._conversation.md +0 -0
- package/docs/agent/._kit.md +0 -0
- package/docs/agent/._shell.md +0 -0
- package/docs/agent/._state.md +0 -0
- package/docs/agent/._style.md +0 -0
- package/docs/agent/._tool-emits-card.md +0 -0
- package/docs/agent/._tool.md +0 -0
- package/docs/agent/._verify.md +0 -0
- package/docs/agent/agent-prompt.md +38 -0
- package/docs/agent/card-action.md +96 -0
- package/docs/agent/card.md +116 -0
- package/docs/agent/conversation.md +52 -0
- package/docs/agent/kit.md +79 -0
- package/docs/agent/shell.md +76 -0
- package/docs/agent/state.md +85 -0
- package/docs/agent/style.md +95 -0
- package/docs/agent/tool-emits-card.md +57 -0
- package/docs/agent/tool.md +69 -0
- package/docs/agent/verify.md +129 -0
- package/eslint.config.mjs +18 -0
- package/modules/provider-custom/._AGENTS.md +0 -0
- package/modules/provider-custom/._INTEGRATION.md +0 -0
- package/modules/provider-custom/._LICENSE +0 -0
- package/modules/provider-custom/._README.md +0 -0
- package/modules/provider-custom/._package.json +0 -0
- package/modules/provider-custom/._tbox.module.json +0 -0
- package/modules/provider-custom/._tsconfig.json +0 -0
- package/modules/provider-custom/._tsup.config.ts +0 -0
- package/modules/provider-custom/AGENTS.md +5 -5
- package/modules/provider-custom/INTEGRATION.md +2 -2
- package/modules/provider-custom/src/._index.ts +0 -0
- package/modules/provider-custom/src/server/._example-adapter.ts +0 -0
- package/modules/provider-custom/src/server/._index.ts +0 -0
- package/modules/provider-custom/src/server/example-adapter.ts +2 -2
- package/modules/provider-custom/src/server/index.ts +1 -1
- package/modules/provider-custom/tests/._provider-custom.test.ts +0 -0
- package/package.json +13 -5
- package/packages/agent-ui-kit/._AGENTS.md +0 -0
- package/packages/agent-ui-kit/._LICENSE +0 -0
- package/packages/agent-ui-kit/._README.md +0 -0
- package/packages/agent-ui-kit/._catalog.json +0 -0
- package/packages/agent-ui-kit/._package.json +0 -0
- package/packages/agent-ui-kit/._tsconfig.json +0 -0
- package/packages/agent-ui-kit/._vitest.config.ts +0 -0
- package/packages/agent-ui-kit/AGENTS.md +8 -0
- package/packages/agent-ui-kit/README.md +4 -4
- package/packages/agent-ui-kit/assets/._keyboard-circle.svg +0 -0
- package/packages/agent-ui-kit/assets/._more-circle.svg +0 -0
- package/packages/agent-ui-kit/assets/._new-chat.svg +0 -0
- package/packages/agent-ui-kit/assets/._quick-command-star.svg +0 -0
- package/packages/agent-ui-kit/assets/._send-gradient.svg +0 -0
- package/packages/agent-ui-kit/assets/._stop@2x.png +0 -0
- package/packages/agent-ui-kit/assets/._voice-circle.svg +0 -0
- package/packages/agent-ui-kit/catalog.json +842 -0
- package/packages/agent-ui-kit/package.json +8 -5
- package/packages/agent-ui-kit/src/._ChatPage.tsx +0 -0
- package/packages/agent-ui-kit/src/._chat-config.test.ts +0 -0
- package/packages/agent-ui-kit/src/._chat-config.ts +0 -0
- package/packages/agent-ui-kit/src/._icons.ts +0 -0
- package/packages/agent-ui-kit/src/._index.ts +0 -0
- package/packages/agent-ui-kit/src/._shell.contract.json +0 -0
- package/packages/agent-ui-kit/src/._shell.test.ts +0 -0
- package/packages/agent-ui-kit/src/._shell.ts +0 -0
- package/packages/agent-ui-kit/src/._store-context.tsx +0 -0
- package/packages/agent-ui-kit/src/._types.ts +0 -0
- package/packages/agent-ui-kit/src/._vite-env.d.ts +0 -0
- package/packages/agent-ui-kit/src/ChatPage.tsx +34 -7
- package/packages/agent-ui-kit/src/card/._define-card.ts +0 -0
- package/packages/agent-ui-kit/src/card/._primitives.test.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._primitives.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._use-card-actions.ts +0 -0
- package/packages/agent-ui-kit/src/card/define-card.ts +34 -0
- package/packages/agent-ui-kit/src/card/primitives.test.tsx +147 -0
- package/packages/agent-ui-kit/src/card/primitives.tsx +627 -0
- package/packages/agent-ui-kit/src/card/use-card-actions.ts +86 -0
- package/packages/agent-ui-kit/src/chat-config.test.ts +35 -71
- package/packages/agent-ui-kit/src/chat-config.ts +18 -0
- package/packages/agent-ui-kit/src/components/._CardRenderer.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ActionBar/._DebugButton.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ActionBar/._DebugButton.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ActionBar/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ActionBar/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ActionBar/DebugButton.tsx +3 -3
- package/packages/agent-ui-kit/src/components/ActionBar/index.tsx +13 -14
- package/packages/agent-ui-kit/src/components/ChatHeader/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ChatHeader/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ChatHeader/index.tsx +19 -17
- package/packages/agent-ui-kit/src/components/ChatInput/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ChatInput/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ChatInput/index.tsx +32 -26
- package/packages/agent-ui-kit/src/components/ChatInput/voice/._VoicePressField.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ChatInput/voice/._VoiceRecordingOverlay.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ChatInput/voice/._machine.ts +0 -0
- package/packages/agent-ui-kit/src/components/ChatInput/voice/._useVoicePress.ts +0 -0
- package/packages/agent-ui-kit/src/components/ChatInput/voice/VoicePressField.tsx +12 -3
- package/packages/agent-ui-kit/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +9 -9
- package/packages/agent-ui-kit/src/components/HistoryEntry/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/HistoryEntry/index.tsx +3 -4
- package/packages/agent-ui-kit/src/components/MediaList/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/MediaList/index.tsx +42 -28
- package/packages/agent-ui-kit/src/components/MessageBubble/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/MessageBubble/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/MessageBubble/index.tsx +52 -27
- package/packages/agent-ui-kit/src/components/PullDownIndicator/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/PullDownIndicator/index.tsx +3 -4
- package/packages/agent-ui-kit/src/components/QuickCommands/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/QuickCommands/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/QuickCommands/index.tsx +6 -6
- package/packages/agent-ui-kit/src/components/ReasoningBlock/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ReasoningBlock/index.tsx +14 -9
- package/packages/agent-ui-kit/src/components/ScrollToBottomButton/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ScrollToBottomButton/index.tsx +2 -4
- package/packages/agent-ui-kit/src/components/ToolCallBlock/._detail.test.ts +0 -0
- package/packages/agent-ui-kit/src/components/ToolCallBlock/._detail.ts +0 -0
- package/packages/agent-ui-kit/src/components/ToolCallBlock/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ToolCallBlock/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/ToolCallBlock/._titles.ts +0 -0
- package/packages/agent-ui-kit/src/components/ToolCallBlock/index.tsx +54 -36
- package/packages/agent-ui-kit/src/hooks/._use-drag-gesture.test.ts +0 -0
- package/packages/agent-ui-kit/src/hooks/._use-drag-gesture.ts +0 -0
- package/packages/agent-ui-kit/src/hooks/._use-pull-down-history.ts +0 -0
- package/packages/agent-ui-kit/src/hooks/._use-pull-down-wheel-native.test.ts +0 -0
- package/packages/agent-ui-kit/src/hooks/._useMediaUpload.ts +0 -0
- package/packages/agent-ui-kit/src/hooks/._useTTSAutoPlay.ts +0 -0
- package/packages/agent-ui-kit/src/icons.ts +39 -0
- package/packages/agent-ui-kit/src/index.ts +55 -2
- package/packages/agent-ui-kit/src/shell.contract.json +59 -0
- package/packages/agent-ui-kit/src/shell.test.ts +61 -0
- package/packages/agent-ui-kit/src/shell.ts +45 -0
- package/packages/agent-ui-kit/src/test/._setup.ts +0 -0
- package/packages/agent-ui-kit/src/theme/._base.css +0 -0
- package/packages/agent-ui-kit/src/theme/._contract.json +0 -0
- package/packages/agent-ui-kit/src/theme/._motion.css +0 -0
- package/packages/agent-ui-kit/src/theme/._theme-contract.test.ts +0 -0
- package/packages/agent-ui-kit/src/theme/._theme.ts +0 -0
- package/packages/agent-ui-kit/src/theme/._tokens.css +0 -0
- package/packages/agent-ui-kit/src/theme/base.css +132 -0
- package/packages/agent-ui-kit/src/theme/contract.json +1 -1
- package/packages/agent-ui-kit/src/theme/theme-contract.test.ts +3 -3
- package/packages/agent-ui-kit/src/theme/{tokens.less → tokens.css} +14 -12
- package/packages/agent-ui-kit/src/utils/._copyText.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._haptics.test.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._haptics.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._imagePreview.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._pull-down-machine.test.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._pull-down-machine.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._session-error.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._toast.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._tool-call-display.test.ts +0 -0
- package/packages/agent-ui-kit/src/utils/._tool-call-display.ts +0 -0
- package/packages/contracts-mall/._package.json +0 -0
- package/packages/contracts-mall/._tsconfig.json +0 -0
- package/packages/contracts-mall/src/._index.ts +0 -0
- package/packages/contracts-mall/src/._runtime.ts +0 -0
- package/packages/contracts-mall/src/server/._index.ts +0 -0
- package/packages/shared/._AGENTS.md +0 -0
- package/packages/shared/._package.json +0 -0
- package/packages/shared/._tsconfig.json +0 -0
- package/packages/shared/AGENTS.md +4 -2
- package/packages/shared/package.json +7 -0
- package/packages/shared/src/._index.ts +0 -0
- package/pnpm-lock.template.yaml +679 -464
- package/pnpm-workspace.template.yaml +16 -11
- package/profiles/._mall.json +0 -0
- package/profiles/mall/._TEMPLATE.md +0 -0
- package/profiles/mall/config/._agent.jsonc +0 -0
- package/profiles/mall/config/._chat.json +0 -0
- package/profiles/mall/config/._knowledge.json +0 -0
- package/profiles/mall/config/._llm_provider.json +0 -0
- package/profiles/mall/config/._plugins.json +0 -0
- package/scenarios/._README.md +0 -0
- package/scenarios/README.md +7 -0
- package/scripts/._build.sh +0 -0
- package/scripts/._check-css-target.mjs +0 -0
- package/scripts/._check-fast.mjs +0 -0
- package/scripts/._check-workspace.mjs +0 -0
- package/scripts/._checks.json +0 -0
- package/scripts/._compat-probe.mjs +0 -0
- package/scripts/._dev-alipay.mjs +0 -0
- package/scripts/._dev.sh +0 -0
- package/scripts/._gen-tailwind-sources.mjs +0 -0
- package/scripts/._generate.mjs +0 -0
- package/scripts/._prepare.sh +0 -0
- package/scripts/._smoke.mjs +0 -0
- package/scripts/._start.sh +0 -0
- package/scripts/._stop.sh +0 -0
- package/scripts/._verify-generated.mjs +0 -0
- package/scripts/check-css-target.mjs +69 -0
- package/scripts/check-fast.mjs +113 -0
- package/scripts/check-workspace.mjs +68 -19
- package/scripts/checks.json +205 -0
- package/scripts/compat-probe.mjs +108 -0
- package/scripts/dev-alipay.mjs +1 -1
- package/scripts/gen-tailwind-sources.mjs +55 -0
- package/tsconfig.base.json +1 -0
- package/apps/client/.tbox-gen/app-kind.ts +0 -1
- package/apps/client/src/platform/AgentChatApp.tsx +0 -6
- package/apps/client/src/platform/AppShell.tsx +0 -42
- package/apps/client/src/platform/agent-runtime.ts +0 -16
- package/apps/client/src/platform/bootstrap.ts +0 -55
- package/apps/client/src/platform/card-preview-page.tsx +0 -10
- package/apps/client/src/platform/mount-page.tsx +0 -20
- package/apps/client/src/platform/state/createPersistedStore.ts +0 -19
- package/apps/server/src/tools/clock.tool.ts +0 -24
- package/apps/server/src/tools/translate.tool.ts +0 -47
- package/packages/agent-ui-kit/src/ChatPage.less +0 -62
- package/packages/agent-ui-kit/src/components/ActionBar/index.less +0 -123
- package/packages/agent-ui-kit/src/components/ChatHeader/index.less +0 -148
- package/packages/agent-ui-kit/src/components/ChatInput/index.less +0 -394
- package/packages/agent-ui-kit/src/components/HistoryEntry/index.less +0 -49
- package/packages/agent-ui-kit/src/components/MediaList/index.less +0 -241
- package/packages/agent-ui-kit/src/components/MessageBubble/index.less +0 -325
- package/packages/agent-ui-kit/src/components/PullDownIndicator/index.less +0 -32
- package/packages/agent-ui-kit/src/components/QuickCommands/index.less +0 -63
- package/packages/agent-ui-kit/src/components/ReasoningBlock/index.less +0 -90
- package/packages/agent-ui-kit/src/components/ScrollToBottomButton/index.less +0 -32
- package/packages/agent-ui-kit/src/components/ToolCallBlock/index.less +0 -312
- package/packages/agent-ui-kit/src/theme/global.less +0 -99
- /package/packages/agent-ui-kit/src/theme/{motion.less → motion.css} +0 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* probe — 应用原语探针(F8)的命令行入口。
|
|
4
|
+
*
|
|
5
|
+
* 两个子模式,都在进程内直调应用代码(零端口、零模型、零浏览器):
|
|
6
|
+
* --tool <id> --input '<json>' [--user u1] [--json]
|
|
7
|
+
* 经 SDK testing 的 createToolHarness 调工具,打印出参、出卡(类型 + schema)
|
|
8
|
+
* 与该用户状态的前后差异;
|
|
9
|
+
* --record <名称> --steps "tool-id{} action:action-id{}" [--expect key1,key2] [--dry-run]
|
|
10
|
+
* 顺序执行各步,按 scenario-lib.mjs 的 recordScenario 生成 scenarios/<名称>.json。
|
|
11
|
+
*
|
|
12
|
+
* registry 从 apps/client/.tbox-gen/registry.server.ts 读(与 pnpm scenario 同源)。
|
|
13
|
+
* 用户状态由构造时登记(`createUserState` → `userStates()`)发现,不依赖文件路径。
|
|
14
|
+
*
|
|
15
|
+
* 用法:pnpm exec tbox-app tool run <id> … / pnpm exec tbox-app scenario record <名称> …
|
|
16
|
+
* 直接运行:pnpm --filter ./apps/server exec node --import tsx scripts/probe.mjs …
|
|
17
|
+
*/
|
|
18
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import process from 'node:process';
|
|
21
|
+
import { pathToFileURL } from 'node:url';
|
|
22
|
+
import { recordScenario, resolveScenarioFiles, runScenarios } from './scenario-lib.mjs';
|
|
23
|
+
|
|
24
|
+
const appDir = path.resolve(import.meta.dirname, '..', '..', '..');
|
|
25
|
+
|
|
26
|
+
// 探针输出是给人和 agent 读的:SDK 的 [TOOL] 观测行默认静音(需要时 TBOX_LOG_LEVEL=info 打开)。
|
|
27
|
+
process.env.TBOX_LOG_LEVEL ??= 'silent';
|
|
28
|
+
// 探针不读写应用根的真实状态文件:登记照常,落盘关闭。
|
|
29
|
+
process.env.TBOX_STATE_MEMORY ??= '1';
|
|
30
|
+
|
|
31
|
+
const USAGE = `用法:
|
|
32
|
+
probe.mjs --tool <id> [--input '<json>'] [--user <id>] [--json]
|
|
33
|
+
probe.mjs --run [文件或目录…]
|
|
34
|
+
probe.mjs --record <名称> --steps "tool-id{} action:action-id{}" [--expect key1,key2] [--dry-run] [--json]
|
|
35
|
+
|
|
36
|
+
说明:
|
|
37
|
+
--input 工具入参 JSON(缺省 {})
|
|
38
|
+
--user 调用用户 id(缺省 local_harness)
|
|
39
|
+
--run 跑 scenarios/*.json(或指定文件/目录),与 pnpm scenario 同源
|
|
40
|
+
--steps 空格分隔的步骤;工具写 <id>{<json>},动作写 action:<id>{<json>};
|
|
41
|
+
可在 { 前加 @<user> 覆盖该步用户,如 query-points@u2{}
|
|
42
|
+
--expect 只把这些顶层键写进期望(逗号分隔;缺省全部)
|
|
43
|
+
--dry-run 只打印场景 JSON,不写 scenarios/<名称>.json
|
|
44
|
+
--json 机器可读输出`;
|
|
45
|
+
|
|
46
|
+
function readArgs(argv) {
|
|
47
|
+
const out = { _: [] };
|
|
48
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
49
|
+
let arg = argv[i];
|
|
50
|
+
if (arg === '--help' || arg === '-h') {
|
|
51
|
+
out.help = true;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (arg === '--dry-run') {
|
|
55
|
+
out.dryRun = true;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (arg === '--run') {
|
|
59
|
+
out.run = true;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (arg === '--json') {
|
|
63
|
+
out.json = true;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
let value;
|
|
67
|
+
const eq = arg.indexOf('=');
|
|
68
|
+
if (arg.startsWith('--') && eq > 2) {
|
|
69
|
+
value = arg.slice(eq + 1);
|
|
70
|
+
arg = arg.slice(0, eq);
|
|
71
|
+
}
|
|
72
|
+
const take = () => {
|
|
73
|
+
if (value !== undefined) return value;
|
|
74
|
+
i += 1;
|
|
75
|
+
if (i >= argv.length) throw new Error(`${arg} 缺少取值`);
|
|
76
|
+
return argv[i];
|
|
77
|
+
};
|
|
78
|
+
if (arg === '--tool') out.tool = take();
|
|
79
|
+
else if (arg === '--input') out.input = take();
|
|
80
|
+
else if (arg === '--user') out.user = take();
|
|
81
|
+
else if (arg === '--record') out.record = take();
|
|
82
|
+
else if (arg === '--steps') out.steps = take();
|
|
83
|
+
else if (arg === '--expect') out.expect = take();
|
|
84
|
+
else if (arg.startsWith('-')) throw new Error(`未知参数:${arg}`);
|
|
85
|
+
else out._.push(arg);
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function parseJsonInput(raw, label) {
|
|
91
|
+
if (raw === undefined) return {};
|
|
92
|
+
if (raw.trim() === '') return {};
|
|
93
|
+
try {
|
|
94
|
+
return JSON.parse(raw);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
throw new Error(`${label} 不是合法 JSON:${error.message}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function loadRegistry() {
|
|
101
|
+
const file = path.join(appDir, 'apps/client/.tbox-gen/registry.server.ts');
|
|
102
|
+
if (!existsSync(file)) {
|
|
103
|
+
throw new Error('缺少 apps/client/.tbox-gen/registry.server.ts——先跑 pnpm generate 生成 registry');
|
|
104
|
+
}
|
|
105
|
+
const mod = await import(pathToFileURL(file).href);
|
|
106
|
+
if (!mod.registry) throw new Error(`${path.relative(appDir, file)} 没有导出 registry——先跑 pnpm generate`);
|
|
107
|
+
return mod.registry;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 已登记的用户状态(构造即登记;不扫文件路径)。 */
|
|
111
|
+
async function loadUserStates(warnings) {
|
|
112
|
+
try {
|
|
113
|
+
const { userStates } = await import('@tbox.cn/app-agent-sdk-server/testing');
|
|
114
|
+
return userStates().map(({ name, state }) => ({ source: name, handle: state }));
|
|
115
|
+
} catch (error) {
|
|
116
|
+
warnings.push(`读取状态登记失败:${error?.message ?? error}`);
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function snapshotStates(states, user, warnings) {
|
|
122
|
+
const snapshot = [];
|
|
123
|
+
for (const state of states) {
|
|
124
|
+
try {
|
|
125
|
+
snapshot.push({ ...state, value: state.handle.get(user) });
|
|
126
|
+
} catch (error) {
|
|
127
|
+
warnings.push(`读取状态失败 ${state.source}:${error?.message ?? error}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return snapshot;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function stateChanges(before, after) {
|
|
134
|
+
const changes = [];
|
|
135
|
+
for (const [index, entry] of before.entries()) {
|
|
136
|
+
const next = after[index];
|
|
137
|
+
if (!next) continue;
|
|
138
|
+
if (JSON.stringify(entry.value) === JSON.stringify(next.value)) continue;
|
|
139
|
+
changes.push({ source: entry.source, before: entry.value, after: next.value });
|
|
140
|
+
}
|
|
141
|
+
return changes;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function printToolResult(result) {
|
|
145
|
+
console.log(`工具 ${result.tool}(user=${result.user},${result.durationMs}ms)`);
|
|
146
|
+
console.log('出参:');
|
|
147
|
+
console.log(JSON.stringify(result.output, null, 2) ?? 'undefined');
|
|
148
|
+
if (result.cards.length === 0) {
|
|
149
|
+
console.log('出卡 0 张(工具未返回 cards,或卡片数据未过 schema 被丢弃)');
|
|
150
|
+
} else {
|
|
151
|
+
console.log(`出卡 ${result.cards.length} 张:`);
|
|
152
|
+
for (const card of result.cards) {
|
|
153
|
+
console.log(` ${card.valid ? '✓' : '✗'} ${card.cardType}${card.valid ? ':schema 通过' : `:${card.issues.join(';')}`}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (result.state.changes.length === 0) {
|
|
157
|
+
const suffix = result.state.modules === 0 ? '(未发现已登记状态:应用里没有 createUserState)' : '';
|
|
158
|
+
console.log(`用户状态:无变化${suffix}`);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
console.log(`用户状态:${result.state.changes.length} 个模块有变化`);
|
|
162
|
+
for (const change of result.state.changes) {
|
|
163
|
+
console.log(` ${change.source}`);
|
|
164
|
+
console.log(` - ${JSON.stringify(change.before)}`);
|
|
165
|
+
console.log(` + ${JSON.stringify(change.after)}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function runTool(args) {
|
|
170
|
+
if (!args.tool) throw new Error('缺少 --tool <id>(或改用 --record <名称>)');
|
|
171
|
+
const input = parseJsonInput(args.input, '--input');
|
|
172
|
+
const user = args.user ?? 'local_harness';
|
|
173
|
+
const warnings = [];
|
|
174
|
+
|
|
175
|
+
const registry = await loadRegistry();
|
|
176
|
+
const states = await loadUserStates(warnings);
|
|
177
|
+
const before = await snapshotStates(states, user, warnings);
|
|
178
|
+
|
|
179
|
+
const { createToolHarness } = await import('@tbox.cn/app-agent-sdk-server/testing');
|
|
180
|
+
const harness = createToolHarness({ registry });
|
|
181
|
+
let call;
|
|
182
|
+
try {
|
|
183
|
+
call = await harness.call(args.tool, input, args.user ? { user: args.user } : undefined);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
throw new Error(`工具调用失败:${error?.message ?? error}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const after = await snapshotStates(states, user, warnings);
|
|
189
|
+
const result = {
|
|
190
|
+
ok: true,
|
|
191
|
+
tool: args.tool,
|
|
192
|
+
user,
|
|
193
|
+
durationMs: call.durationMs,
|
|
194
|
+
output: call.output,
|
|
195
|
+
cards: call.cards,
|
|
196
|
+
state: { user, modules: states.length, changes: stateChanges(before, after), warnings },
|
|
197
|
+
registry: { tools: harness.toolIds(), cards: harness.cardTypes() },
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
if (args.json) console.log(JSON.stringify(result, null, 2));
|
|
201
|
+
else {
|
|
202
|
+
for (const warning of warnings) console.error(`⚠️ ${warning}`);
|
|
203
|
+
printToolResult(result);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 按顶层空白切步骤(花括号内与引号内的空白不切)。 */
|
|
208
|
+
function splitSteps(source) {
|
|
209
|
+
const steps = [];
|
|
210
|
+
let current = '';
|
|
211
|
+
let depth = 0;
|
|
212
|
+
let quote = '';
|
|
213
|
+
for (const ch of source) {
|
|
214
|
+
if (quote) {
|
|
215
|
+
current += ch;
|
|
216
|
+
if (ch === quote) quote = '';
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (ch === '"' || ch === "'") {
|
|
220
|
+
quote = ch;
|
|
221
|
+
current += ch;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (ch === '{') depth += 1;
|
|
225
|
+
if (ch === '}') {
|
|
226
|
+
depth -= 1;
|
|
227
|
+
if (depth < 0) throw new Error('--steps 花括号不配对');
|
|
228
|
+
}
|
|
229
|
+
if (/\s/.test(ch) && depth === 0) {
|
|
230
|
+
if (current.trim()) steps.push(current.trim());
|
|
231
|
+
current = '';
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
current += ch;
|
|
235
|
+
}
|
|
236
|
+
if (quote) throw new Error('--steps 引号不配对');
|
|
237
|
+
if (depth !== 0) throw new Error('--steps 花括号不配对');
|
|
238
|
+
if (current.trim()) steps.push(current.trim());
|
|
239
|
+
return steps;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 解析一步:tool-id{} / action:action-id{} / tool-id@u2{}。 */
|
|
243
|
+
export function parseStep(spec, index) {
|
|
244
|
+
const brace = spec.indexOf('{');
|
|
245
|
+
const head = (brace === -1 ? spec : spec.slice(0, brace)).trim();
|
|
246
|
+
const body = brace === -1 ? '' : spec.slice(brace).trim();
|
|
247
|
+
const match = /^(?:action:)?[^\s@]+(?:@[^\s@]+)?$/.test(head);
|
|
248
|
+
if (!head || !match) throw new Error(`第 ${index + 1} 步格式非法:${spec}(写法:tool-id{} 或 action:action-id{})`);
|
|
249
|
+
const suffix = head.lastIndexOf('@');
|
|
250
|
+
const user = suffix > 0 ? head.slice(suffix + 1) : undefined;
|
|
251
|
+
const core = suffix > 0 ? head.slice(0, suffix) : head;
|
|
252
|
+
const action = core.startsWith('action:');
|
|
253
|
+
const id = action ? core.slice('action:'.length) : core;
|
|
254
|
+
if (!id) throw new Error(`第 ${index + 1} 步缺少 id:${spec}`);
|
|
255
|
+
const input = body ? parseJsonInput(body, `--steps 第 ${index + 1} 步入参`) : undefined;
|
|
256
|
+
return {
|
|
257
|
+
...(action ? { action: id } : { tool: id }),
|
|
258
|
+
...(input !== undefined ? { input } : {}),
|
|
259
|
+
...(user ? { user } : {}),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function assertScenarioName(name) {
|
|
264
|
+
if (!/^[\p{L}\p{N}._-]+$/u.test(name) || name === '.' || name === '..') {
|
|
265
|
+
throw new Error(`场景名非法 "${name}":只允许字母、数字、中文、点、下划线、中划线`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function describeRecordStep(step, index) {
|
|
270
|
+
const kind = step.action ? 'action' : 'tool';
|
|
271
|
+
const id = step.action ?? step.tool;
|
|
272
|
+
const expect = step.expect ?? {};
|
|
273
|
+
const payload = kind === 'action' ? expect.result : expect.output;
|
|
274
|
+
const keys = payload !== null && typeof payload === 'object' ? Object.keys(payload).length : 0;
|
|
275
|
+
const cards = Array.isArray(expect.cards) ? ` · 出卡 ${expect.cards.join('、')}` : '';
|
|
276
|
+
const summary = kind === 'action' ? `返回 ${keys} 键` : `出参 ${keys} 键`;
|
|
277
|
+
return ` ✓ ${index + 1}. ${kind} ${id} → ${summary}${cards}`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** `--run`:与 pnpm scenario 同一执行器(scenario-lib.runScenarios),输出格式也保持一致。 */
|
|
281
|
+
async function runScenarioFiles(files) {
|
|
282
|
+
const targets = resolveScenarioFiles(appDir, files);
|
|
283
|
+
if (targets.length === 0) {
|
|
284
|
+
console.log(JSON.stringify({ ok: true, scenarios: 0, note: '没有场景文件(scenarios/*.json)' }));
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const registry = await loadRegistry();
|
|
288
|
+
const result = await runScenarios({ appDir, registry, files: targets });
|
|
289
|
+
for (const item of result.results) {
|
|
290
|
+
if (item.ok) continue;
|
|
291
|
+
console.error(`✗ ${item.scenario}(${item.steps} 步)`);
|
|
292
|
+
for (const line of item.failures) console.error(` - ${line}`);
|
|
293
|
+
}
|
|
294
|
+
console.log(JSON.stringify({ ok: result.ok, scenarios: result.scenarios, failed: result.failed, tools: result.tools, cards: result.cards }));
|
|
295
|
+
if (!result.ok) process.exitCode = 1;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function runRecord(args) {
|
|
299
|
+
if (!args.record) throw new Error('缺少 --record <名称>(或改用 --tool <id>)');
|
|
300
|
+
assertScenarioName(args.record);
|
|
301
|
+
if (!args.steps || !args.steps.trim()) {
|
|
302
|
+
throw new Error('缺少 --steps——例:--steps "query-points{} action:pay-parking{}"');
|
|
303
|
+
}
|
|
304
|
+
const specs = splitSteps(args.steps);
|
|
305
|
+
if (specs.length === 0) throw new Error('--steps 为空');
|
|
306
|
+
const steps = specs.map((spec, index) => parseStep(spec, index));
|
|
307
|
+
if (args.user) for (const step of steps) if (!step.user) step.user = args.user;
|
|
308
|
+
const keys = (args.expect ?? '')
|
|
309
|
+
.split(',')
|
|
310
|
+
.map((key) => key.trim())
|
|
311
|
+
.filter(Boolean);
|
|
312
|
+
for (const step of steps) if (keys.length > 0) step.keys = keys;
|
|
313
|
+
|
|
314
|
+
const registry = await loadRegistry();
|
|
315
|
+
const { createToolHarness } = await import('@tbox.cn/app-agent-sdk-server/testing');
|
|
316
|
+
const harness = createToolHarness({ registry });
|
|
317
|
+
const knownTools = new Set(harness.toolIds());
|
|
318
|
+
const knownActions = new Set(Object.keys(registry.actions ?? {}));
|
|
319
|
+
for (const [index, step] of steps.entries()) {
|
|
320
|
+
if (step.tool && !knownTools.has(step.tool)) {
|
|
321
|
+
throw new Error(`第 ${index + 1} 步工具未注册:${step.tool}(可用:${[...knownTools].join('、') || '无'};先 pnpm generate)`);
|
|
322
|
+
}
|
|
323
|
+
if (step.action && !knownActions.has(step.action)) {
|
|
324
|
+
throw new Error(`第 ${index + 1} 步动作未注册:${step.action}(可用:${[...knownActions].join('、') || '无'})`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
let scenario;
|
|
329
|
+
try {
|
|
330
|
+
scenario = await recordScenario({ registry, name: args.record, steps });
|
|
331
|
+
} catch (error) {
|
|
332
|
+
throw new Error(`录制执行失败:${error?.message ?? error}`);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const file = path.join(appDir, 'scenarios', `${args.record}.json`);
|
|
336
|
+
if (!args.dryRun) {
|
|
337
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
338
|
+
writeFileSync(file, `${JSON.stringify(scenario, null, 2)}\n`, 'utf8');
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const result = { ok: true, name: args.record, dryRun: Boolean(args.dryRun), ...(args.dryRun ? {} : { file: path.relative(appDir, file) }), scenario };
|
|
342
|
+
if (args.json || args.dryRun) {
|
|
343
|
+
console.log(JSON.stringify(args.dryRun ? scenario : result, null, 2));
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
console.log(`录制 ${args.record}(${steps.length} 步)`);
|
|
347
|
+
scenario.steps.forEach((step, index) => console.log(describeRecordStep(step, index)));
|
|
348
|
+
console.log(`✅ ${path.relative(appDir, file)}`);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function main() {
|
|
352
|
+
const args = readArgs(process.argv.slice(2));
|
|
353
|
+
if (args.help) {
|
|
354
|
+
console.log(USAGE);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (args.run) {
|
|
358
|
+
await runScenarioFiles(args._);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (args.record !== undefined) {
|
|
362
|
+
await runRecord(args);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (args.tool !== undefined) {
|
|
366
|
+
await runTool(args);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
console.log(USAGE);
|
|
370
|
+
throw new Error('需要 --tool <id>、--run 或 --record <名称>');
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
try {
|
|
374
|
+
await main();
|
|
375
|
+
} catch (error) {
|
|
376
|
+
if (process.argv.includes('--json')) {
|
|
377
|
+
console.log(JSON.stringify({ ok: false, error: error?.message ?? String(error) }, null, 2));
|
|
378
|
+
} else {
|
|
379
|
+
console.error(`❌ ${error?.message ?? error}`);
|
|
380
|
+
}
|
|
381
|
+
process.exitCode = 1;
|
|
382
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* scenario-lib — 进程内场景执行库(T1)。
|
|
3
|
+
*
|
|
4
|
+
* 与 `scenario.mjs`(CLI 包装)分离,供三处复用:
|
|
5
|
+
* - `pnpm scenario`(CLI);
|
|
6
|
+
* - `check:fast` 的证据包(样式 → 原语 → 渲染 → 场景,同一进程);
|
|
7
|
+
* - `pnpm exec tbox-app scenario record`(把真实执行记录写成场景期望)。
|
|
8
|
+
*
|
|
9
|
+
* 场景文件格式:
|
|
10
|
+
* {
|
|
11
|
+
* "name": "示例",
|
|
12
|
+
* "steps": [
|
|
13
|
+
* { "tool": "order-status", "input": {}, "user": "u1",
|
|
14
|
+
* "expect": { "output": { "title": "Order Status" }, "cards": ["order-status"] } },
|
|
15
|
+
* { "action": "order-status-pay", "user": "u1", "expect": { "result": { "text": "已处理" } } }
|
|
16
|
+
* ]
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* 断言语义:`output` 为子集匹配(数组要求等长逐项匹配);`cards` 要求按顺序出现且全部通过 schema 校验。
|
|
20
|
+
* 零端口、零模型、零浏览器。
|
|
21
|
+
*/
|
|
22
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
|
|
25
|
+
// 场景与探针一律用内存状态:登记照常,落盘关闭(不触碰应用根 .tbox/state)。
|
|
26
|
+
process.env.TBOX_STATE_MEMORY ??= '1';
|
|
27
|
+
|
|
28
|
+
/** 子集匹配:对象按键递归,数组等长逐项。 */
|
|
29
|
+
export function matches(actual, expected) {
|
|
30
|
+
if (Array.isArray(expected)) {
|
|
31
|
+
if (!Array.isArray(actual) || actual.length !== expected.length) return false;
|
|
32
|
+
return expected.every((item, index) => matches(actual[index], item));
|
|
33
|
+
}
|
|
34
|
+
if (expected !== null && typeof expected === 'object') {
|
|
35
|
+
if (actual === null || typeof actual !== 'object') return false;
|
|
36
|
+
return Object.entries(expected).every(([key, value]) => matches(actual[key], value));
|
|
37
|
+
}
|
|
38
|
+
return actual === expected;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 首个不符路径(给失败信息定位用)。 */
|
|
42
|
+
export function diffPath(actual, expected, prefix = '') {
|
|
43
|
+
if (Array.isArray(expected)) {
|
|
44
|
+
if (!Array.isArray(actual)) return `${prefix}: 期望数组,实际 ${typeof actual}`;
|
|
45
|
+
if (actual.length !== expected.length) return `${prefix}: 数组长度 ${actual.length} ≠ ${expected.length}`;
|
|
46
|
+
for (let i = 0; i < expected.length; i += 1) {
|
|
47
|
+
const found = diffPath(actual[i], expected[i], `${prefix}[${i}]`);
|
|
48
|
+
if (found) return found;
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
if (expected !== null && typeof expected === 'object') {
|
|
53
|
+
if (actual === null || typeof actual !== 'object') return `${prefix}: 期望对象,实际 ${JSON.stringify(actual)}`;
|
|
54
|
+
for (const [key, value] of Object.entries(expected)) {
|
|
55
|
+
const found = diffPath(actual[key], value, prefix ? `${prefix}.${key}` : key);
|
|
56
|
+
if (found) return found;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return Object.is(actual, expected) ? null : `${prefix}: 期望 ${JSON.stringify(expected)},实际 ${JSON.stringify(actual)}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 解析目标:显式文件/目录 → 目录下 *.json;无参数 → `<appDir>/scenarios`。 */
|
|
64
|
+
export function resolveScenarioFiles(appDir, args = []) {
|
|
65
|
+
const targets = [];
|
|
66
|
+
for (const arg of args) {
|
|
67
|
+
if (String(arg).startsWith('-')) continue;
|
|
68
|
+
const resolved = path.resolve(appDir, arg);
|
|
69
|
+
if (!existsSync(resolved)) continue;
|
|
70
|
+
if (statSync(resolved).isDirectory()) {
|
|
71
|
+
for (const name of readdirSync(resolved).filter((f) => f.endsWith('.json')).sort()) targets.push(path.join(resolved, name));
|
|
72
|
+
} else {
|
|
73
|
+
targets.push(resolved);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const scenarioRoot = path.join(appDir, 'scenarios');
|
|
77
|
+
if (targets.length === 0 && existsSync(scenarioRoot)) {
|
|
78
|
+
for (const name of readdirSync(scenarioRoot).filter((f) => f.endsWith('.json')).sort()) targets.push(path.join(scenarioRoot, name));
|
|
79
|
+
}
|
|
80
|
+
return targets;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function mergedState(harness, user) {
|
|
84
|
+
const merged = {};
|
|
85
|
+
for (const entry of harness.states(user)) {
|
|
86
|
+
if (entry && typeof entry.value === 'object' && entry.value !== null) Object.assign(merged, entry.value);
|
|
87
|
+
}
|
|
88
|
+
return merged;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function stateDiff(harness, user, expected) {
|
|
92
|
+
return diffPath(mergedState(harness, user), expected);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 顺序执行场景。
|
|
97
|
+
* @param {object} options
|
|
98
|
+
* @param {string} options.appDir 应用根
|
|
99
|
+
* @param {object} options.registry 服务端 registry(由调用方加载,避免重复实例)
|
|
100
|
+
* @param {string[]} options.files 场景文件绝对路径
|
|
101
|
+
*/
|
|
102
|
+
export async function runScenarios({ appDir, registry, files }) {
|
|
103
|
+
const { createToolHarness } = await import('@tbox.cn/app-agent-sdk-server/testing');
|
|
104
|
+
const harness = createToolHarness({ registry });
|
|
105
|
+
const results = [];
|
|
106
|
+
const instances = [];
|
|
107
|
+
let failed = 0;
|
|
108
|
+
|
|
109
|
+
for (const file of files) {
|
|
110
|
+
const scenario = JSON.parse(readFileSync(file, 'utf8'));
|
|
111
|
+
const rel = path.relative(appDir, file);
|
|
112
|
+
const steps = Array.isArray(scenario.steps) ? scenario.steps : [];
|
|
113
|
+
const scenarioUser = typeof scenario.user === 'string' ? scenario.user : `scenario:${rel}`;
|
|
114
|
+
const failures = [];
|
|
115
|
+
for (const [index, step] of steps.entries()) {
|
|
116
|
+
const kind = step.action ? 'action' : 'tool';
|
|
117
|
+
const id = step.action ?? step.tool;
|
|
118
|
+
const label = `${rel} #${index + 1} ${kind}:${id}`;
|
|
119
|
+
try {
|
|
120
|
+
const user = step.user ?? scenarioUser;
|
|
121
|
+
if (kind === 'action') {
|
|
122
|
+
const result = await harness.act(id, step.input ?? {}, { user });
|
|
123
|
+
if (result && typeof result === 'object' && result.card?.cardType) {
|
|
124
|
+
instances.push({ cardType: result.card.cardType, data: result.card.data, file: rel, step: index + 1 });
|
|
125
|
+
}
|
|
126
|
+
if (step.expect?.result !== undefined) {
|
|
127
|
+
const diff = diffPath(result, step.expect.result);
|
|
128
|
+
if (diff) failures.push(`${label}: 返回不符 → ${diff}`);
|
|
129
|
+
}
|
|
130
|
+
if (step.expect?.state !== undefined && stateDiff(harness, user, step.expect.state)) {
|
|
131
|
+
failures.push(`${label}: 状态不符 → ${stateDiff(harness, user, step.expect.state)}`);
|
|
132
|
+
}
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const result = await harness.call(id, step.input ?? {}, { user });
|
|
136
|
+
for (const card of result.cards ?? []) {
|
|
137
|
+
if (card.valid && card.data !== undefined) instances.push({ cardType: card.cardType, data: card.data, file: rel, step: index + 1 });
|
|
138
|
+
}
|
|
139
|
+
const invalid = result.cards.filter((card) => !card.valid);
|
|
140
|
+
if (invalid.length > 0) {
|
|
141
|
+
failures.push(`${label}: 卡片数据不合法 ${invalid.map((c) => `${c.cardType}(${c.issues.join('; ')})`).join('、')}`);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (step.expect?.output !== undefined) {
|
|
145
|
+
const diff = diffPath(result.output, step.expect.output);
|
|
146
|
+
if (diff) {
|
|
147
|
+
failures.push(`${label}: 出参不符 → ${diff}`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (Array.isArray(step.expect?.cards)) {
|
|
152
|
+
const produced = result.cards.map((card) => card.cardType);
|
|
153
|
+
const ok = step.expect.cards.every((type, i) => produced[i] === type);
|
|
154
|
+
if (!ok) failures.push(`${label}: 卡片序列不符 → 期望 ${JSON.stringify(step.expect.cards)},实际 ${JSON.stringify(produced)}`);
|
|
155
|
+
}
|
|
156
|
+
if (step.expect?.state !== undefined && stateDiff(harness, user, step.expect.state)) {
|
|
157
|
+
failures.push(`${label}: 状态不符 → ${stateDiff(harness, user, step.expect.state)}`);
|
|
158
|
+
}
|
|
159
|
+
} catch (error) {
|
|
160
|
+
failures.push(`${label}: 抛错 → ${error?.message ?? error}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (failures.length > 0) failed += 1;
|
|
164
|
+
results.push({
|
|
165
|
+
scenario: scenario.name ?? rel,
|
|
166
|
+
file: rel,
|
|
167
|
+
steps: steps.length,
|
|
168
|
+
ok: failures.length === 0,
|
|
169
|
+
failures,
|
|
170
|
+
...(Array.isArray(scenario.covers) ? { covers: scenario.covers } : {}),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
ok: failed === 0,
|
|
176
|
+
scenarios: results.length,
|
|
177
|
+
failed,
|
|
178
|
+
results,
|
|
179
|
+
instances,
|
|
180
|
+
tools: harness.toolIds(),
|
|
181
|
+
cards: harness.cardTypes(),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 录制:顺序执行每一步并把实际出参写成期望(只保留 `keys` 指定的键,缺省全部)。
|
|
187
|
+
* 返回可直接写入 `scenarios/<name>.json` 的对象。
|
|
188
|
+
*/
|
|
189
|
+
export async function recordScenario({ registry, name, steps, user }) {
|
|
190
|
+
const { createToolHarness } = await import('@tbox.cn/app-agent-sdk-server/testing');
|
|
191
|
+
const harness = createToolHarness({ registry });
|
|
192
|
+
const scenarioUser = typeof user === 'string' ? user : `scenario:${name}`;
|
|
193
|
+
const out = { name, user: scenarioUser, steps: [] };
|
|
194
|
+
for (const step of steps) {
|
|
195
|
+
const kind = step.action ? 'action' : 'tool';
|
|
196
|
+
const id = step.action ?? step.tool;
|
|
197
|
+
const ctx = { user: step.user ?? scenarioUser };
|
|
198
|
+
if (kind === 'action') {
|
|
199
|
+
const result = await harness.act(id, step.input ?? {}, ctx);
|
|
200
|
+
out.steps.push({ action: id, ...(step.input ? { input: step.input } : {}), ...(step.user ? { user: step.user } : {}), expect: { result: pick(result, step.keys) } });
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const result = await harness.call(id, step.input ?? {}, ctx);
|
|
204
|
+
out.steps.push({
|
|
205
|
+
tool: id,
|
|
206
|
+
...(step.input ? { input: step.input } : {}),
|
|
207
|
+
...(step.user ? { user: step.user } : {}),
|
|
208
|
+
expect: {
|
|
209
|
+
output: pick(result.output, step.keys),
|
|
210
|
+
...(result.cards.length > 0 ? { cards: result.cards.map((card) => card.cardType) } : {}),
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function pick(value, keys) {
|
|
218
|
+
if (!Array.isArray(keys) || keys.length === 0) return value;
|
|
219
|
+
if (value === null || typeof value !== 'object') return value;
|
|
220
|
+
const out = {};
|
|
221
|
+
for (const key of keys) if (key in value) out[key] = value[key];
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* scenario — 进程内场景执行器(T1)的命令行包装。
|
|
4
|
+
*
|
|
5
|
+
* 读 `scenarios/*.json`,逐步直调应用工具并断言出参与卡片。零端口、零模型、零浏览器:
|
|
6
|
+
* 一次运行就能回答「这条联动链路还通不通」。执行逻辑在 `scenario-lib.mjs`(与 `check:fast` 共用)。
|
|
7
|
+
*
|
|
8
|
+
* 运行:`pnpm scenario [文件或目录…]`(根脚本转发到本文件;SDK 从 apps/server 依赖解析)。
|
|
9
|
+
*/
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import process from 'node:process';
|
|
12
|
+
import { resolveScenarioFiles, runScenarios } from './scenario-lib.mjs';
|
|
13
|
+
|
|
14
|
+
const appDir = path.resolve(import.meta.dirname, '..', '..', '..');
|
|
15
|
+
const files = resolveScenarioFiles(appDir, process.argv.slice(2));
|
|
16
|
+
|
|
17
|
+
if (files.length === 0) {
|
|
18
|
+
console.log(JSON.stringify({ ok: true, scenarios: 0, note: '没有场景文件(scenarios/*.json)' }));
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { registry } = await import(path.join(appDir, 'apps/client/.tbox-gen/registry.server.ts'));
|
|
23
|
+
const result = await runScenarios({ appDir, registry, files });
|
|
24
|
+
|
|
25
|
+
for (const item of result.results) {
|
|
26
|
+
if (item.ok) continue;
|
|
27
|
+
console.error(`✗ ${item.scenario}(${item.steps} 步)`);
|
|
28
|
+
for (const line of item.failures) console.error(` - ${line}`);
|
|
29
|
+
}
|
|
30
|
+
console.log(
|
|
31
|
+
JSON.stringify({ ok: result.ok, scenarios: result.scenarios, failed: result.failed, tools: result.tools, cards: result.cards }),
|
|
32
|
+
);
|
|
33
|
+
if (!result.ok) process.exitCode = 1;
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|