@allbluecn/web-app 0.27.16 → 0.27.18
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/package.json +43 -32
- package/src/__tests__/integration/harness-conformance.test.ts +164 -0
- package/src/__tests__/integration/harness-log-store.test.ts +436 -0
- package/src/__tests__/integration/harness-media.test.ts +195 -0
- package/src/__tests__/integration/harness-prisma-host.test.ts +219 -0
- package/src/__tests__/integration/harness-session-metadata.test.ts +244 -0
- package/src/__tests__/security/ai/destructive-tool-approval.test.ts +18 -10
- package/src/__tests__/security/ai/prompt-injection.test.ts +3 -2
- package/src/__tests__/security/ai/sandbox-tool-bridge.test.ts +0 -1
- package/src/__tests__/setup.ts +19 -0
- package/src/components/agents/__tests__/agent-bindings-tab.test.tsx +1 -1
- package/src/components/ai/__tests__/ai-prompt-input-attach.test.tsx +5 -3
- package/src/components/ai/__tests__/chat-thread-id-stability.test.tsx +3 -0
- package/src/components/ai/ai-prompt-input/index.tsx +2 -2
- package/src/components/ai/global-harness-sidebar/global-sidebar-connection.tsx +126 -0
- package/src/components/ai/global-harness-sidebar/global-sidebar-context.tsx +438 -0
- package/src/components/ai/global-harness-sidebar/index.tsx +65 -308
- package/src/components/billing/subscription-provider.tsx +3 -2
- package/src/components/design/canvas/document/types.ts +5 -1
- package/src/components/design/editor/collaboration/design-agent-panel.tsx +1 -0
- package/src/components/devtools/devtools-panel.tsx +7 -15
- package/src/components/devtools/unified-devtools.tsx +46 -0
- package/src/components/harness/__tests__/attachment-policy.test.ts +22 -9
- package/src/components/harness/__tests__/composer-draft.test.ts +78 -1
- package/src/components/harness/__tests__/composer-keymap.test.ts +228 -0
- package/src/components/harness/__tests__/composer-paste.test.tsx +109 -0
- package/src/components/harness/__tests__/composer-prefill.test.ts +2 -2
- package/src/components/harness/__tests__/composer-primary-button.test.tsx +205 -0
- package/src/components/harness/__tests__/composer-send.test.tsx +141 -22
- package/src/components/harness/__tests__/composer-takeover.test.tsx +221 -0
- package/src/components/harness/__tests__/gallery-behavior.test.tsx +1 -4
- package/src/components/harness/__tests__/harness-workbench-first-send.test.tsx +125 -10
- package/src/components/harness/__tests__/harness-workbench-mount.test.ts +4 -2
- package/src/components/harness/__tests__/mention-kbid.test.ts +94 -0
- package/src/components/harness/__tests__/submit-machine.test.ts +14 -2
- package/src/components/harness/__tests__/takeover.test.ts +74 -0
- package/src/components/harness/__tests__/use-attachments.test.tsx +185 -0
- package/src/components/harness/ai-chat-history/types.ts +2 -2
- package/src/components/harness/chat-ui.tsx +65 -2
- package/src/components/harness/composer/attachments/attachment-policy.ts +8 -18
- package/src/components/harness/composer/attachments/attachment-tray.tsx +72 -28
- package/src/components/harness/composer/attachments/use-attachments.ts +149 -96
- package/src/components/harness/composer/composer-editor.tsx +127 -25
- package/src/components/harness/composer/composer-keymap.ts +98 -0
- package/src/components/harness/composer/composer-primary-button.tsx +124 -0
- package/src/components/harness/composer/harness-composer.tsx +242 -112
- package/src/components/harness/composer/harness-engine-control.tsx +6 -6
- package/src/components/harness/composer/mention/mention-suggestion-menu.tsx +29 -6
- package/src/components/harness/composer/plus-menu.tsx +100 -0
- package/src/components/harness/composer/submit-machine.ts +12 -4
- package/src/components/harness/composer/takeover.ts +64 -0
- package/src/components/harness/harness-chat-slots.ts +2 -2
- package/src/components/harness/harness-workbench-page.tsx +150 -613
- package/src/components/harness/interrupt-approval.tsx +59 -4
- package/src/components/harness/local-agent/__tests__/agent-tool-card.test.tsx +2 -2
- package/src/components/harness/local-agent/agent-tool-card.tsx +1 -1
- package/src/components/harness/session/__tests__/client-session.test.tsx +154 -0
- package/src/components/harness/session/__tests__/use-harness-chat-adapter.test.ts +187 -0
- package/src/components/harness/session/client-tools-dispatcher.tsx +82 -0
- package/src/components/harness/session/harness-session-appchat.tsx +126 -0
- package/src/components/harness/session/use-harness-chat-adapter.ts +299 -0
- package/src/components/harness/session/use-harness-session.ts +96 -0
- package/src/components/harness/session/view-message-adapter.tsx +174 -0
- package/src/components/harness/workbench/workbench-connection.tsx +165 -0
- package/src/components/harness/workbench/workbench-context.tsx +847 -0
- package/src/components/settings/ai/local-agent-panel.tsx +3 -19
- package/src/components/shared-kit/list-kit/display/__tests__/dropdown.test.tsx +0 -1
- package/src/components/story/ai/__tests__/use-story-ai-model.test.tsx +9 -6
- package/src/components/story/ai/model-types.ts +4 -4
- package/src/components/story/ai/split-dialog.tsx +15 -42
- package/src/components/story/ai/story-ai-panel.tsx +18 -25
- package/src/components/story/ai/use-story-ai-model.ts +5 -26
- package/src/components/story/display/__tests__/display-dropdown.test.tsx +0 -1
- package/src/components/story/inline-editors/labels-editor.tsx +1 -0
- package/src/components/tiptap/__tests__/ai-extension.test.ts +182 -87
- package/src/components/tiptap/extensions/ai-extension.ts +94 -140
- package/src/components/tiptap/node/mention-extension.ts +4 -0
- package/src/components/tiptap/ui/ai-menu/ai-menu.tsx +70 -52
- package/src/design-engine/ai/__tests__/ai.test.ts +133 -0
- package/src/lib/__tests__/harness-gate.test.ts +47 -0
- package/src/lib/__tests__/provider-router.test.ts +0 -3
- package/src/lib/ai/__tests__/story-ai-stream.test.ts +317 -1
- package/src/lib/ai/__tests__/story-prompt.test.ts +2 -2
- package/src/lib/ai/__tests__/tiptap-stream.test.ts +287 -0
- package/src/lib/ai/story-ai-stream.ts +149 -16
- package/src/lib/ai/tiptap-stream.ts +212 -0
- package/src/lib/changelog/sdk-versions.ts +8 -8
- package/src/lib/harness/blob-url.ts +39 -0
- package/src/lib/harness-gate.ts +28 -0
- package/src/lib/queries/local-agents.ts +1 -1
- package/src/paraglide/messages/_index.js +186 -79
- package/src/paraglide/messages/en.js +60 -25
- package/src/paraglide/messages/zh.js +60 -25
- package/src/routeTree.gen.ts +21 -0
- package/src/routes/api/design/prototype/__tests__/route.test.ts +168 -297
- package/src/routes/api/design/prototype/route.ts +65 -259
- package/src/routes/api/generate/__tests__/media.test.ts +70 -85
- package/src/routes/api/generate/media.ts +49 -91
- package/src/routes/api/harness/$.ts +90 -0
- package/src/routes/api/mcp/bugs.ts +6 -5
- package/src/routes/server/__tests__/ai-stream.test.ts +206 -0
- package/src/routes/server/ai-stream.ts +68 -76
- package/src/server/__tests__/agent-crud.test.ts +31 -0
- package/src/server/__tests__/chat-agent-resolve.test.ts +18 -0
- package/src/server/__tests__/chat-list-messages.test.ts +30 -2
- package/src/server/__tests__/entitlements.test.ts +24 -21
- package/src/server/__tests__/sandbox-entitlement.test.ts +5 -6
- package/src/server/__tests__/task-executor.test.ts +33 -22
- package/src/server/agents/__tests__/agent-registry.test.ts +183 -0
- package/src/server/agents/__tests__/agent-stream.test.ts +163 -0
- package/src/server/agents/__tests__/design.server.test.ts +769 -0
- package/src/server/agents/__tests__/gates.server.test.ts +125 -0
- package/src/server/agents/__tests__/knowledge-index.server.test.ts +246 -0
- package/src/server/agents/__tests__/knowledge.server.test.ts +678 -0
- package/src/server/agents/__tests__/media.server.test.ts +309 -0
- package/src/server/agents/__tests__/registry-user-agent.test.ts +228 -0
- package/src/server/agents/__tests__/runtime-subagents.test.ts +225 -0
- package/src/server/agents/__tests__/story.server.test.ts +451 -0
- package/src/server/agents/__tests__/tiptap.server.test.ts +334 -0
- package/src/server/agents/bug-task.server.ts +115 -0
- package/src/server/agents/context.server.ts +31 -0
- package/src/server/agents/design-stream.server.ts +325 -0
- package/src/server/agents/design.server.ts +337 -0
- package/src/server/agents/gates.server.ts +78 -0
- package/src/server/agents/knowledge.server.ts +667 -0
- package/src/server/agents/label-suggest.server.ts +119 -0
- package/src/server/agents/media.server.ts +341 -0
- package/src/server/agents/registry.server.ts +234 -0
- package/src/server/agents/runtime.server.ts +389 -0
- package/src/server/agents/story.server.ts +414 -0
- package/src/server/agents/testcase-generate.server.ts +106 -0
- package/src/server/agents/testcase.server.ts +195 -0
- package/src/server/agents/tiptap.server.ts +198 -0
- package/src/server/agents/translation.server.ts +62 -0
- package/src/server/agents/types.server.ts +99 -0
- package/src/server/ai-gateway/__tests__/adapter-has-ai-key.test.ts +109 -0
- package/src/server/ai-gateway/__tests__/agent-runtime.test.ts +78 -87
- package/src/server/ai-gateway/__tests__/attachment-deref.test.ts +406 -0
- package/src/server/ai-gateway/__tests__/chat-handler-als-identity.test.ts +181 -0
- package/src/server/ai-gateway/__tests__/chat-handler-overrides-model.test.ts +93 -0
- package/src/server/ai-gateway/__tests__/chat-handler-prompt-key.test.ts +11 -7
- package/src/server/ai-gateway/__tests__/chat-prompt-key-drift.test.ts +5 -1
- package/src/server/ai-gateway/__tests__/chat-stream-builder-code-mode.test.ts +12 -0
- package/src/server/ai-gateway/__tests__/chat-stream-builder-memory.test.ts +12 -0
- package/src/server/ai-gateway/__tests__/chat-stream-builder-sandbox.test.ts +17 -2
- package/src/server/ai-gateway/__tests__/chat-stream-builder-skills.test.ts +14 -2
- package/src/server/ai-gateway/__tests__/chat-stream-builder-subagents.test.ts +326 -0
- package/src/server/ai-gateway/__tests__/code-mode-integration-smoke.test.ts +29 -8
- package/src/server/ai-gateway/__tests__/knowledge-scope.test.ts +47 -101
- package/src/server/ai-gateway/__tests__/knowledge-search-fence.test.ts +105 -47
- package/src/server/ai-gateway/__tests__/local-agent-adapter.test.ts +2 -2
- package/src/server/ai-gateway/__tests__/media-transcription-persist.test.ts +21 -4
- package/src/server/ai-gateway/__tests__/message-store.test.ts +12 -0
- package/src/server/ai-gateway/__tests__/rerank.test.ts +31 -5
- package/src/server/ai-gateway/__tests__/retry-duplication-regression.test.ts +224 -0
- package/src/server/ai-gateway/__tests__/runtime.test.ts +0 -3
- package/src/server/ai-gateway/__tests__/story-ai-local.test.ts +2 -2
- package/src/server/ai-gateway/__tests__/tool-error-sanitize.test.ts +7 -6
- package/src/server/ai-gateway/__tests__/tool-pool-sentinel.test.ts +104 -0
- package/src/server/ai-gateway/__tests__/tool-registry.test.ts +0 -1
- package/src/server/ai-gateway/__tests__/tools-authorization.test.ts +141 -67
- package/src/server/ai-gateway/__tests__/tools-select.test.ts +28 -29
- package/src/server/ai-gateway/adapter.ts +33 -6
- package/src/server/ai-gateway/chat-handler.ts +117 -119
- package/src/server/ai-gateway/chat-stream-builder.ts +32 -9
- package/src/server/ai-gateway/chat-subagents.server.ts +82 -0
- package/src/server/ai-gateway/durability/reaper-driver.ts +4 -1
- package/src/server/ai-gateway/embedding.ts +6 -2
- package/src/server/ai-gateway/index.ts +3 -4
- package/src/server/ai-gateway/kb/__tests__/provider-config.test.ts +276 -0
- package/src/server/ai-gateway/kb/provider-config.ts +196 -0
- package/src/server/ai-gateway/local-agent-adapter.ts +17 -18
- package/src/server/ai-gateway/media/__tests__/adapters.spec.ts +72 -0
- package/src/server/ai-gateway/media/__tests__/generation.spec.ts +9 -2
- package/src/server/ai-gateway/media/__tests__/metering.spec.ts +242 -0
- package/src/server/ai-gateway/media/__tests__/provider-config.spec.ts +99 -0
- package/src/server/ai-gateway/media/adapters.ts +15 -4
- package/src/server/ai-gateway/media/index.ts +202 -86
- package/src/server/ai-gateway/media/provider-config.ts +76 -0
- package/src/server/ai-gateway/middlewares/attachment-deref.ts +266 -0
- package/src/server/ai-gateway/middlewares/index.ts +12 -0
- package/src/server/ai-gateway/middlewares/usage-log.ts +7 -2
- package/src/server/ai-gateway/persistence/message-store.ts +30 -1
- package/src/server/ai-gateway/ports.ts +8 -17
- package/src/server/ai-gateway/remote-forward.ts +104 -0
- package/src/server/ai-gateway/rerank.ts +27 -3
- package/src/server/ai-gateway/runs/agent-runtime.ts +58 -43
- package/src/server/ai-gateway/runs/biz-keys.ts +19 -24
- package/src/server/ai-gateway/runs/resolve-overrides.ts +7 -0
- package/src/server/ai-gateway/runs/user-agent.ts +7 -5
- package/src/server/ai-gateway/sandbox/tool-bridge.ts +6 -6
- package/src/server/ai-gateway/story-handler.ts +52 -112
- package/src/server/ai-gateway/tools/__tests__/knowledge-search-tool.test.ts +95 -0
- package/src/server/ai-gateway/tools/definitions.ts +4 -1
- package/src/server/ai-gateway/tools/index.ts +2 -1
- package/src/server/ai-gateway/tools/keys.ts +0 -1
- package/src/server/ai-gateway/tools/knowledge-scope.ts +24 -12
- package/src/server/ai-gateway/tools/registry.ts +0 -2
- package/src/server/ai-gateway/tools/select.ts +16 -15
- package/src/server/ai-gateway/tools/server.ts +73 -145
- package/src/server/ai-gateway/tools.ts +1 -1
- package/src/server/ai-gateway/types.ts +3 -2
- package/src/server/ai-gateway/unified-router.ts +39 -9
- package/src/server/ai-ports.ts +3 -28
- package/src/server/ai-task/executor.ts +20 -12
- package/src/server/bug-report/__tests__/repro-runner.test.ts +41 -29
- package/src/server/bug-report/repro-runner.ts +15 -21
- package/src/server/collab/__tests__/reindex-endpoint.test.ts +11 -0
- package/src/server/collab/__tests__/write-guard-sdk.test.ts +4 -19
- package/src/server/collab/reindex-endpoint.ts +3 -2
- package/src/server/harness/__tests__/access-trusted-context.test.ts +189 -0
- package/src/server/harness/__tests__/agents-assembly.test.ts +65 -0
- package/src/server/harness/__tests__/compaction.server.test.ts +39 -0
- package/src/server/harness/__tests__/credentials.server.test.ts +92 -0
- package/src/server/harness/__tests__/engine-assembly.test.ts +107 -0
- package/src/server/harness/__tests__/gate.server.test.ts +52 -0
- package/src/server/harness/__tests__/git-probe.test.ts +89 -0
- package/src/server/harness/__tests__/workspace-permissions.test.ts +160 -0
- package/src/server/harness/access.server.ts +73 -0
- package/src/server/harness/compaction.server.ts +77 -0
- package/src/server/harness/credentials.server.ts +72 -0
- package/src/server/harness/define.server.ts +213 -0
- package/src/server/harness/gate.server.ts +35 -0
- package/src/server/harness/git-probe.server.ts +76 -0
- package/src/server/harness/host-user.types.ts +11 -0
- package/src/server/harness/host.server.ts +139 -0
- package/src/server/harness/model-catalog.server.ts +56 -0
- package/src/server/harness/permission-mode.server.ts +62 -0
- package/src/server/harness/persistence/artifact-store.server.ts +112 -0
- package/src/server/harness/persistence/blob-store.server.ts +225 -0
- package/src/server/harness/persistence/log-store.server.ts +588 -0
- package/src/server/harness/persistence/log-write-params.ts +15 -0
- package/src/server/harness/persistence/metadata-store.server.ts +80 -0
- package/src/server/harness/persistence/prisma-persistence.server.ts +56 -0
- package/src/server/harness/persistence/session-index-store.server.ts +237 -0
- package/src/server/harness/tool-identity.server.ts +44 -0
- package/src/server/harness/trusted-context.server.ts +86 -0
- package/src/server/harness/web-tools.server.ts +81 -0
- package/src/server/harness/workspace-binder.server.ts +53 -0
- package/src/server/knowledge/__tests__/knowledge-index-core.test.ts +438 -0
- package/src/server/knowledge/__tests__/reindex-guard.test.ts +99 -0
- package/src/server/knowledge/__tests__/search-scope.test.ts +98 -0
- package/src/server/knowledge/knowledge-helpers.ts +0 -7
- package/src/server/knowledge/knowledge-index-utils.ts +193 -69
- package/src/server/knowledge/reindex-guard.ts +24 -0
- package/src/server/knowledge/search-scope.ts +90 -0
- package/src/server/lib/rate-limit.ts +12 -0
- package/src/server/mcp/__tests__/mcp-server.test.ts +382 -0
- package/src/server/mcp/__tests__/tools.test.ts +65 -167
- package/src/server/mcp/mcp-server.ts +261 -0
- package/src/server/mcp/tool-errors.ts +12 -8
- package/src/server/mcp/tools.ts +113 -56
- package/src/server/serverFns/__tests__/knowledge-breadcrumb.test.ts +1 -1
- package/src/server/serverFns/__tests__/knowledge-delete.test.ts +2 -2
- package/src/server/serverFns/__tests__/knowledge-index.test.ts +98 -144
- package/src/server/serverFns/__tests__/knowledge-kb-projects.test.ts +0 -1
- package/src/server/serverFns/__tests__/knowledge-readable-kb.test.ts +1 -1
- package/src/server/serverFns/__tests__/knowledge-search.test.ts +146 -42
- package/src/server/serverFns/__tests__/ownership-merge.test.ts +11 -5
- package/src/server/serverFns/__tests__/update-document-guard.test.ts +18 -2
- package/src/server/serverFns/agents/agent-crud.ts +4 -2
- package/src/server/serverFns/blob-upload.ts +7 -1
- package/src/server/serverFns/chat/conversations.ts +29 -9
- package/src/server/serverFns/chat/list-messages.ts +31 -6
- package/src/server/serverFns/design/ai/fill-annotation.ts +9 -17
- package/src/server/serverFns/design/ai/local-agents.ts +7 -7
- package/src/server/serverFns/harness/__tests__/attach-to-kb.spec.ts +4 -4
- package/src/server/serverFns/harness/__tests__/gallery.spec.ts +73 -12
- package/src/server/serverFns/harness/__tests__/media-prototype.spec.ts +5 -4
- package/src/server/serverFns/harness/__tests__/media.spec.ts +37 -29
- package/src/server/serverFns/harness/attach-to-kb.ts +2 -1
- package/src/server/serverFns/harness/gallery.ts +26 -28
- package/src/server/serverFns/harness/media.ts +48 -105
- package/src/server/serverFns/knowledge-index.ts +22 -65
- package/src/server/serverFns/knowledge.ts +14 -136
- package/src/server/serverFns/story/__tests__/label-suggest-fn.test.ts +102 -37
- package/src/server/serverFns/story/story-crud/label-suggest.ts +12 -2
- package/src/server/serverFns/testcase/__tests__/automation.test.ts +45 -36
- package/src/server/serverFns/testcase/__tests__/generation.test.ts +213 -65
- package/src/server/serverFns/testcase/__tests__/testgen-output.test.ts +38 -21
- package/src/server/serverFns/testcase/automation.ts +17 -26
- package/src/server/serverFns/testcase/generation.ts +60 -83
- package/src/server/serverFns/translation.ts +8 -18
- package/src/server/testcase/__tests__/auto-bug.test.ts +10 -4
- package/src/server/testcase/__tests__/builtin-executor.test.ts +37 -25
- package/src/server/testcase/__tests__/exec-pool.test.ts +17 -37
- package/src/server/testcase/__tests__/triage.test.ts +46 -29
- package/src/server/testcase/builtin-executor.ts +56 -100
- package/src/server/testcase/triage.ts +21 -30
- package/src/shared/design/board.ts +26 -0
- package/src/components/harness/composer/use-composer-submit.ts +0 -82
- package/src/lib/ai/__tests__/story-jsonl.test.ts +0 -198
- package/src/lib/ai/story-jsonl.ts +0 -154
- package/src/server/__tests__/rag.test.ts +0 -139
- package/src/server/ai-gateway/__tests__/delegate-task.test.ts +0 -121
- package/src/server/ai-gateway/tools/delegate-task.ts +0 -111
- package/src/server/mcp/__tests__/json-rpc.test.ts +0 -496
- package/src/server/mcp/json-rpc.ts +0 -178
- package/src/server/mcp/protocol.ts +0 -364
- package/src/server/rag.ts +0 -58
- package/src/server/story/story-crud/label-suggest-core.ts +0 -98
- package/src/shared/design-ai/__tests__/ndjson.test.ts +0 -317
- package/src/shared/design-ai/ndjson.ts +0 -137
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* testcase 域 agent(Phase M1:triage / exec 两件;generate / automation 随
|
|
20
|
+
* 后续批次迁入)。AI 输出 schema 的**权威定义在本文件**(消费方 re-export
|
|
21
|
+
* 保持既有 import 路径兼容);run 内只做「组装提示词 → 结构化调用」,业务
|
|
22
|
+
* 编排(队列口径/幂等守卫/落账/沙箱动作裁决)留在消费方。
|
|
23
|
+
*
|
|
24
|
+
* 身份:triage 取批次创建人(agent 无登录用户,照 projectActivity 归属先例)、
|
|
25
|
+
* exec 取执行发起人——均由调用方经 invokeAgent 的 identity 显式传入。
|
|
26
|
+
*/
|
|
27
|
+
import { z } from "zod"
|
|
28
|
+
import { FAILURE_CATEGORY_VALUES } from "@/server/serverFns/testcase/constants"
|
|
29
|
+
import { assemblePrompts } from "@/server/ai-gateway/runs/agent-runtime"
|
|
30
|
+
import { runAgentStructured } from "./runtime.server"
|
|
31
|
+
import type { AllblueAgentDefinition } from "./types.server"
|
|
32
|
+
|
|
33
|
+
/* ── testcase.triage(AI 段自 triage.ts 下沉)── */
|
|
34
|
+
|
|
35
|
+
export const TriageOutputSchema = z
|
|
36
|
+
.object({
|
|
37
|
+
category: z.enum(FAILURE_CATEGORY_VALUES),
|
|
38
|
+
evidence: z.array(z.string().min(1)).max(20),
|
|
39
|
+
confidence: z.number().min(0).max(1),
|
|
40
|
+
})
|
|
41
|
+
.strict();
|
|
42
|
+
export type TriageOutput = z.infer<typeof TriageOutputSchema>;
|
|
43
|
+
|
|
44
|
+
export interface TriageAgentInput {
|
|
45
|
+
caseTitle: string
|
|
46
|
+
caseSteps: string
|
|
47
|
+
stepResults: string
|
|
48
|
+
execEvidence: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const triageAgent: AllblueAgentDefinition<TriageAgentInput, TriageOutput> = {
|
|
52
|
+
name: "testcase.triage",
|
|
53
|
+
description:
|
|
54
|
+
"对失败测试执行做归因分类(env_issue/product_bug/test_bug/suspected_flaky/unknown),返回证据清单与置信度",
|
|
55
|
+
inputSchema: z.object({
|
|
56
|
+
caseTitle: z.string().min(1).max(500),
|
|
57
|
+
caseSteps: z.string().max(20_000),
|
|
58
|
+
stepResults: z.string().max(50_000),
|
|
59
|
+
execEvidence: z.string().max(50_000),
|
|
60
|
+
}),
|
|
61
|
+
temperature: 0.1,
|
|
62
|
+
run: async (input, ctx) => {
|
|
63
|
+
const systemPrompts = await assemblePrompts("testcase.triage", null, {
|
|
64
|
+
userId: ctx.identity.userId,
|
|
65
|
+
promptVars: {
|
|
66
|
+
caseTitle: input.caseTitle,
|
|
67
|
+
caseSteps: input.caseSteps,
|
|
68
|
+
stepResults: input.stepResults,
|
|
69
|
+
execEvidence: input.execEvidence,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
const raw = await runAgentStructured({
|
|
73
|
+
agentName: "testcase.triage",
|
|
74
|
+
identity: ctx.identity,
|
|
75
|
+
messages: [{ role: "user", content: `对失败用例「${input.caseTitle}」做失败归因` }],
|
|
76
|
+
systemPrompts,
|
|
77
|
+
temperature: 0.1,
|
|
78
|
+
outputSchema: TriageOutputSchema,
|
|
79
|
+
abortSignal: ctx.abortSignal,
|
|
80
|
+
})
|
|
81
|
+
return TriageOutputSchema.parse(JSON.parse(raw))
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* ── testcase.exec(AI 段自 builtin-executor.ts 下沉)── */
|
|
86
|
+
|
|
87
|
+
/** 动作类型集(简报钉死六种之内) */
|
|
88
|
+
export const EXEC_ACTION_TYPES = [
|
|
89
|
+
"click",
|
|
90
|
+
"fill",
|
|
91
|
+
"assert",
|
|
92
|
+
"navigate",
|
|
93
|
+
"done",
|
|
94
|
+
"fail",
|
|
95
|
+
] as const;
|
|
96
|
+
export type ExecActionType = (typeof EXEC_ACTION_TYPES)[number];
|
|
97
|
+
|
|
98
|
+
/** 单步动作数上限(协议约束,防 LLM 失控循环) */
|
|
99
|
+
export const MAX_ACTIONS_PER_STEP = 8;
|
|
100
|
+
|
|
101
|
+
export type ExecVerdict = "pass" | "fail" | "uncertain";
|
|
102
|
+
|
|
103
|
+
// ── 动作协议 schema(strict:未知字段如 css/selector/xpath 直接拒绝)──
|
|
104
|
+
|
|
105
|
+
export const ExecActionSchema = z
|
|
106
|
+
.object({
|
|
107
|
+
type: z.enum(EXEC_ACTION_TYPES),
|
|
108
|
+
/** ARIA 角色(getByRole),如 button / textbox / link */
|
|
109
|
+
role: z.string().min(1).max(100).optional(),
|
|
110
|
+
/** 可访问名称(getByRole 的 name 选项,仅在 role 存在时有效) */
|
|
111
|
+
name: z.string().min(1).max(200).optional(),
|
|
112
|
+
/** 可见文本(getByText) */
|
|
113
|
+
text: z.string().min(1).max(500).optional(),
|
|
114
|
+
/** fill 输入值;assert 的期望文本子串 */
|
|
115
|
+
value: z.string().max(2000).optional(),
|
|
116
|
+
/** navigate 目标(缺省回落 targetEnv) */
|
|
117
|
+
url: z.string().max(2000).optional(),
|
|
118
|
+
/** done/fail 的原因说明(入 observed) */
|
|
119
|
+
reason: z.string().min(1).max(2000).optional(),
|
|
120
|
+
})
|
|
121
|
+
.strict()
|
|
122
|
+
.superRefine((action, ctx) => {
|
|
123
|
+
const needsLocator = action.type === "click" || action.type === "fill" || action.type === "assert";
|
|
124
|
+
if (needsLocator && !action.role && !action.text) {
|
|
125
|
+
ctx.addIssue({
|
|
126
|
+
code: "custom",
|
|
127
|
+
message: `${action.type} 动作需要 role 或 text 语义定位(禁止 CSS 选择器)`,
|
|
128
|
+
path: ["role"],
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (action.name && !action.role) {
|
|
132
|
+
ctx.addIssue({
|
|
133
|
+
code: "custom",
|
|
134
|
+
message: "name 仅在与 role 搭配时有效(语义定位)",
|
|
135
|
+
path: ["name"],
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (action.type === "fill" && (!action.value || action.value.length === 0)) {
|
|
139
|
+
ctx.addIssue({ code: "custom", message: "fill 动作需要 value", path: ["value"] });
|
|
140
|
+
}
|
|
141
|
+
if ((action.type === "done" || action.type === "fail") && !action.reason) {
|
|
142
|
+
ctx.addIssue({
|
|
143
|
+
code: "custom",
|
|
144
|
+
message: `${action.type} 动作需要 reason 说明`,
|
|
145
|
+
path: ["reason"],
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
export type ExecAction = z.infer<typeof ExecActionSchema>;
|
|
150
|
+
|
|
151
|
+
export const ExecStepPlanSchema = z
|
|
152
|
+
.object({ actions: z.array(ExecActionSchema).min(1).max(MAX_ACTIONS_PER_STEP) })
|
|
153
|
+
.strict();
|
|
154
|
+
export type ExecStepPlan = z.infer<typeof ExecStepPlanSchema>;
|
|
155
|
+
|
|
156
|
+
export interface ExecStepAgentInput {
|
|
157
|
+
targetEnv: string
|
|
158
|
+
stepAction: string
|
|
159
|
+
stepExpected: string
|
|
160
|
+
pageSnapshot: string
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export const execStepAgent: AllblueAgentDefinition<ExecStepAgentInput, ExecStepPlan> = {
|
|
164
|
+
name: "testcase.exec",
|
|
165
|
+
description:
|
|
166
|
+
"内置浏览器执行的单步动作规划:按页面 aria 快照与步骤预期,产出最多 8 个受控动作的序列 JSON(协议违规由消费方降级 uncertain,不执行任何动作)",
|
|
167
|
+
inputSchema: z.object({
|
|
168
|
+
targetEnv: z.string().max(2000),
|
|
169
|
+
stepAction: z.string().max(10_000),
|
|
170
|
+
stepExpected: z.string().max(10_000),
|
|
171
|
+
pageSnapshot: z.string().max(60_000),
|
|
172
|
+
}),
|
|
173
|
+
temperature: 0,
|
|
174
|
+
run: async (input, ctx) => {
|
|
175
|
+
const systemPrompts = await assemblePrompts("testcase.exec", null, {
|
|
176
|
+
userId: ctx.identity.userId,
|
|
177
|
+
promptVars: {
|
|
178
|
+
targetEnv: input.targetEnv,
|
|
179
|
+
stepAction: input.stepAction,
|
|
180
|
+
stepExpected: input.stepExpected,
|
|
181
|
+
pageSnapshot: input.pageSnapshot,
|
|
182
|
+
},
|
|
183
|
+
})
|
|
184
|
+
const raw = await runAgentStructured({
|
|
185
|
+
agentName: "testcase.exec",
|
|
186
|
+
identity: ctx.identity,
|
|
187
|
+
messages: [{ role: "user", content: "按动作协议返回当前步骤的动作序列 JSON" }],
|
|
188
|
+
systemPrompts,
|
|
189
|
+
temperature: 0,
|
|
190
|
+
outputSchema: ExecStepPlanSchema,
|
|
191
|
+
abortSignal: ctx.abortSignal,
|
|
192
|
+
})
|
|
193
|
+
return ExecStepPlanSchema.parse(JSON.parse(raw))
|
|
194
|
+
},
|
|
195
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* TipTap 编辑器 AI 双入口(Phase M3 T-M3 任务 2):
|
|
20
|
+
*
|
|
21
|
+
* - tiptap.improve(原 /server/ai-stream 路由内联的 resolveSystemPrompt +
|
|
22
|
+
* runChatStream 链):选区改写,动作指令经 TIPTAP_ACTION_INSTRUCTIONS 映射。
|
|
23
|
+
* - tiptap.generate(原 ai-menu.tsx 裸 useChat 直连 /api/chat):快捷菜单内容
|
|
24
|
+
* 生成。脱离主对话通道后不再建 ChatThread(K4:不污染会话列表),也不再吃
|
|
25
|
+
* /api/chat 的 webSearchEnabled 缺省 true(默认关联网预搜达成)。
|
|
26
|
+
*
|
|
27
|
+
* 两入口均为无 outputSchema 的纯文本面(runAgentText / streamAgentChat 无
|
|
28
|
+
* outputSchema);提示词模板(kernel tiptap.improve / tiptap.menu.generate)
|
|
29
|
+
* 正文不动。bizKey 处置:BizAgentKey 无 tiptap 键,且两个入口现网均无 persona
|
|
30
|
+
* 前置(improve 直调 resolveSystemPrompt;generate 走 /api/chat 亦无 agent
|
|
31
|
+
* 覆盖)——assemblePrompts 恒传 agent=null(等价「显式绑定才前置」的空集),
|
|
32
|
+
* 提示词来源经 promptKey 覆盖(design.prototype.stream / story.polish 同款
|
|
33
|
+
* 覆盖先例),bizKey 形参仅满足签名、不参与查找。
|
|
34
|
+
*/
|
|
35
|
+
import { z } from "zod"
|
|
36
|
+
import type { StreamChunk } from "@tanstack/ai"
|
|
37
|
+
import { assemblePrompts } from "@/server/ai-gateway/runs/agent-runtime"
|
|
38
|
+
import { TIPTAP_ACTION_INSTRUCTIONS } from "@/server/ai-gateway/prompts/builtin"
|
|
39
|
+
import { enforceGates } from "./gates.server"
|
|
40
|
+
import { runAgentText, streamAgentChat } from "./runtime.server"
|
|
41
|
+
import type { AllblueAgentDefinition } from "./types.server"
|
|
42
|
+
|
|
43
|
+
/* ── 提示词组装(run 与流式面共用)── */
|
|
44
|
+
|
|
45
|
+
/** 空分区过滤与现网一致:userInstruction/actionInstruction 传空串时模板渲染
|
|
46
|
+
* 为空元素,resolveSystemPrompt 不自行剔除(kernel builtin.ts 注释口径),
|
|
47
|
+
* 调用方需自行 filter(原 /server/ai-stream 路由的 .filter(Boolean) 等价)。 */
|
|
48
|
+
async function prepareTiptapImprovePrompts(input: {
|
|
49
|
+
instruction?: string
|
|
50
|
+
type?: string
|
|
51
|
+
}, userId: string): Promise<string[]> {
|
|
52
|
+
return (
|
|
53
|
+
await assemblePrompts("story.generate", null, {
|
|
54
|
+
userId,
|
|
55
|
+
promptKey: "tiptap.improve",
|
|
56
|
+
promptVars: {
|
|
57
|
+
actionInstruction: TIPTAP_ACTION_INSTRUCTIONS[input.type ?? ""] ?? "",
|
|
58
|
+
userInstruction: input.instruction ?? "",
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
).filter(Boolean)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** tiptap.menu.generate 模板无变量(kernel builtin.ts variables: []),空 vars */
|
|
65
|
+
async function prepareTiptapGeneratePrompts(userId: string): Promise<string[]> {
|
|
66
|
+
return (
|
|
67
|
+
await assemblePrompts("story.generate", null, {
|
|
68
|
+
userId,
|
|
69
|
+
promptKey: "tiptap.menu.generate",
|
|
70
|
+
})
|
|
71
|
+
).filter(Boolean)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* ── tiptap.improve(registry 14 号)── */
|
|
75
|
+
|
|
76
|
+
export interface TiptapImproveInput {
|
|
77
|
+
prompt: string
|
|
78
|
+
instruction?: string
|
|
79
|
+
type?: string
|
|
80
|
+
model?: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const tiptapImproveAgent: AllblueAgentDefinition<TiptapImproveInput, string> = {
|
|
84
|
+
name: "tiptap.improve",
|
|
85
|
+
description:
|
|
86
|
+
"TipTap 编辑器选区改写:按动作指令(TIPTAP_ACTION_INSTRUCTIONS)与用户自定义指令改写文本,纯文本输出",
|
|
87
|
+
inputSchema: z.object({
|
|
88
|
+
prompt: z.string().min(1).max(10_000),
|
|
89
|
+
instruction: z.string().max(2_000).optional(),
|
|
90
|
+
type: z.string().max(50).optional(),
|
|
91
|
+
model: z.string().max(100).optional(),
|
|
92
|
+
}),
|
|
93
|
+
gates: {
|
|
94
|
+
// 沿用现网 improve 端点的 chat 限流(AI_RATE_LIMIT.chat),不新增
|
|
95
|
+
rateLimit: { endpoint: "chat" },
|
|
96
|
+
requiresKey: true,
|
|
97
|
+
},
|
|
98
|
+
run: async (input, ctx) => {
|
|
99
|
+
const systemPrompts = await prepareTiptapImprovePrompts(input, ctx.identity.userId)
|
|
100
|
+
return runAgentText({
|
|
101
|
+
agentName: "tiptap.improve",
|
|
102
|
+
identity: ctx.identity,
|
|
103
|
+
messages: [{ role: "user", content: input.prompt }],
|
|
104
|
+
systemPrompts,
|
|
105
|
+
model: input.model,
|
|
106
|
+
abortSignal: ctx.abortSignal,
|
|
107
|
+
})
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* ── tiptap.generate(registry 15 号)── */
|
|
112
|
+
|
|
113
|
+
export interface TiptapGenerateInput {
|
|
114
|
+
prompt: string
|
|
115
|
+
model?: string
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const tiptapGenerateAgent: AllblueAgentDefinition<TiptapGenerateInput, string> = {
|
|
119
|
+
name: "tiptap.generate",
|
|
120
|
+
description:
|
|
121
|
+
"TipTap 快捷菜单内容生成:按用户指令续写或创作内容(只输出内容不解释),纯文本输出",
|
|
122
|
+
inputSchema: z.object({
|
|
123
|
+
prompt: z.string().min(1).max(10_000),
|
|
124
|
+
model: z.string().max(100).optional(),
|
|
125
|
+
}),
|
|
126
|
+
gates: {
|
|
127
|
+
rateLimit: { endpoint: "chat" },
|
|
128
|
+
requiresKey: true,
|
|
129
|
+
},
|
|
130
|
+
run: async (input, ctx) => {
|
|
131
|
+
const systemPrompts = await prepareTiptapGeneratePrompts(ctx.identity.userId)
|
|
132
|
+
return runAgentText({
|
|
133
|
+
agentName: "tiptap.generate",
|
|
134
|
+
identity: ctx.identity,
|
|
135
|
+
messages: [{ role: "user", content: input.prompt }],
|
|
136
|
+
systemPrompts,
|
|
137
|
+
model: input.model,
|
|
138
|
+
abortSignal: ctx.abortSignal,
|
|
139
|
+
})
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* ── SSE 流式面(薄壳 /server/ai-stream 消费;不走 def.run,显式门控)── */
|
|
144
|
+
|
|
145
|
+
const TIPTAP_MODE_TO_AGENT = {
|
|
146
|
+
improve: "tiptap.improve",
|
|
147
|
+
generate: "tiptap.generate",
|
|
148
|
+
} as const
|
|
149
|
+
|
|
150
|
+
export interface TiptapStreamInput {
|
|
151
|
+
mode: "improve" | "generate"
|
|
152
|
+
prompt: string
|
|
153
|
+
/** improve 专属:用户自定义改写指令;generate 模式忽略(zod strip) */
|
|
154
|
+
instruction?: string
|
|
155
|
+
/** improve 专属:动作名(TIPTAP_ACTION_INSTRUCTIONS 键);generate 模式忽略 */
|
|
156
|
+
type?: string
|
|
157
|
+
model?: string
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* TipTap 双入口的云端流式面:mode → agent 映射 → inputSchema.parse(generate
|
|
162
|
+
* 模式下 improve 专属字段 instruction/type 由 zod strip 静默忽略)→
|
|
163
|
+
* enforceGates(chat 限流 + requiresKey)→ 提示词组装(与 run 共用 prepare)→
|
|
164
|
+
* streamAgentChat(无 outputSchema 纯文本流,TEXT_MESSAGE_CONTENT 原样透传,
|
|
165
|
+
* 客户端就吃这个)。异常/缺 key 以 throw 传播(route 统一映射 RUN_ERROR /
|
|
166
|
+
* 429)。data.model 透传(model picker 行为不变)。
|
|
167
|
+
*/
|
|
168
|
+
export async function streamTiptap(
|
|
169
|
+
input: TiptapStreamInput,
|
|
170
|
+
userId: string,
|
|
171
|
+
abortSignal?: AbortSignal,
|
|
172
|
+
): Promise<AsyncIterable<StreamChunk>> {
|
|
173
|
+
const agentName = TIPTAP_MODE_TO_AGENT[input.mode]
|
|
174
|
+
// 动态 import 避免与 registry.server 的静态环(registry 静态登记本文件导出)
|
|
175
|
+
const { getAgent } = await import("./registry.server")
|
|
176
|
+
const def = getAgent(agentName)
|
|
177
|
+
if (!def || !def.inputSchema) {
|
|
178
|
+
throw new Error(`未知 agent:${agentName}`)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const parsedInput = def.inputSchema.parse(input)
|
|
182
|
+
await enforceGates(def.gates, { userId })
|
|
183
|
+
|
|
184
|
+
const systemPrompts =
|
|
185
|
+
input.mode === "improve"
|
|
186
|
+
? await prepareTiptapImprovePrompts(parsedInput as TiptapImproveInput, userId)
|
|
187
|
+
: await prepareTiptapGeneratePrompts(userId)
|
|
188
|
+
|
|
189
|
+
// 不传 max_tokens 类上限:与全体注册表 agent 同口径走 adapter 按模型缺省(E5)
|
|
190
|
+
return streamAgentChat({
|
|
191
|
+
agentName,
|
|
192
|
+
identity: { userId },
|
|
193
|
+
messages: [{ role: "user", content: (parsedInput as { prompt: string }).prompt }],
|
|
194
|
+
systemPrompts,
|
|
195
|
+
model: (parsedInput as { model?: string }).model,
|
|
196
|
+
abortSignal,
|
|
197
|
+
})
|
|
198
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `translation.run` agent(Phase A1 试点;原 serverFns/translation.ts 的
|
|
20
|
+
* runChatAsAgent 路径迁移)。行为等价:promptKey translation.run、
|
|
21
|
+
* temperature 0.3、user 消息=原文、返回 trim 后译文;门控
|
|
22
|
+
* (translation 限流 + hasAiKey)由 agent gates 承接。
|
|
23
|
+
*/
|
|
24
|
+
import { z } from "zod"
|
|
25
|
+
import { assemblePrompts } from "@/server/ai-gateway/runs/agent-runtime"
|
|
26
|
+
import { runAgentText } from "./runtime.server"
|
|
27
|
+
import type { AllblueAgentDefinition } from "./types.server"
|
|
28
|
+
|
|
29
|
+
export interface TranslationInput {
|
|
30
|
+
text: string
|
|
31
|
+
from: string
|
|
32
|
+
to: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const translationAgent: AllblueAgentDefinition<TranslationInput, string> = {
|
|
36
|
+
name: "translation.run",
|
|
37
|
+
description: "把一段文本在指定语言之间翻译,返回纯译文(无解释、无前后缀)",
|
|
38
|
+
inputSchema: z.object({
|
|
39
|
+
text: z.string().min(1).max(5000),
|
|
40
|
+
from: z.string().max(10),
|
|
41
|
+
to: z.string().max(10),
|
|
42
|
+
}),
|
|
43
|
+
gates: {
|
|
44
|
+
rateLimit: { endpoint: "translation" },
|
|
45
|
+
requiresKey: true,
|
|
46
|
+
},
|
|
47
|
+
temperature: 0.3,
|
|
48
|
+
run: async (input, ctx) => {
|
|
49
|
+
const systemPrompts = await assemblePrompts("translation.run", null, {
|
|
50
|
+
userId: ctx.identity.userId,
|
|
51
|
+
promptVars: { from: input.from, to: input.to },
|
|
52
|
+
})
|
|
53
|
+
return runAgentText({
|
|
54
|
+
agentName: "translation.run",
|
|
55
|
+
identity: ctx.identity,
|
|
56
|
+
messages: [{ role: "user", content: input.text }],
|
|
57
|
+
systemPrompts,
|
|
58
|
+
temperature: 0.3,
|
|
59
|
+
abortSignal: ctx.abortSignal,
|
|
60
|
+
})
|
|
61
|
+
},
|
|
62
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// This program is free software: you can redistribute it and/or modify
|
|
6
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
// (at your option) any later version.
|
|
9
|
+
//
|
|
10
|
+
// This program is distributed in the hope that it will be useful,
|
|
11
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
// GNU Affero General Public License for more details.
|
|
14
|
+
//
|
|
15
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
import type { z } from "zod"
|
|
19
|
+
import type { AI_RATE_LIMIT } from "@/server/lib/rate-limit"
|
|
20
|
+
import type { ResourceSnapshots } from "@allbluecn/kernel"
|
|
21
|
+
|
|
22
|
+
/** 请求级可信身份(access 层产出;三调用面同源) */
|
|
23
|
+
export interface TrustedIdentity {
|
|
24
|
+
userId: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 声明式门控(设计 §13.4):由 `withGates` 包装进 agent 本身,三条调用面
|
|
29
|
+
* (invokeAgent / 模型调度合成工具 / MCP)拿到的是同一个已包装对象——
|
|
30
|
+
* 门控不旁路。执行序:rateLimit → feature → resource → requiresKey。
|
|
31
|
+
*/
|
|
32
|
+
export interface AgentGates {
|
|
33
|
+
/** 进程内限流端点(checkAiRateLimit;端点须在 AI_RATE_LIMIT 表注册) */
|
|
34
|
+
rateLimit?: { endpoint: keyof typeof AI_RATE_LIMIT }
|
|
35
|
+
/** 功能门(requireFeature;Phase A0 D5 下全放开,保留恢复面) */
|
|
36
|
+
feature?: string
|
|
37
|
+
/**
|
|
38
|
+
* 资源配额预检(requireResourceForUser(userId, key),设计 §13.4 的
|
|
39
|
+
* `resource?: ResourceKey`;T-M4 mediaGenerationsMonthly 首个消费方)。
|
|
40
|
+
* ResourceLimitError 原样上抛(429 语义由入口映射)。
|
|
41
|
+
*/
|
|
42
|
+
resource?: keyof ResourceSnapshots
|
|
43
|
+
/** 缺 AI key 时 fail-closed(原各消费点散落的 hasAiKey 预检收口) */
|
|
44
|
+
requiresKey?: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** run 内可用的执行上下文(身份已解析,来自 ALS 或 harness RunContext) */
|
|
48
|
+
export interface AgentRunContext {
|
|
49
|
+
identity: TrustedIdentity
|
|
50
|
+
/** 一次性会话标识(不落 ChatThread,业务 agent 不建持久化线程) */
|
|
51
|
+
threadId: string
|
|
52
|
+
runId: string
|
|
53
|
+
abortSignal?: AbortSignal
|
|
54
|
+
/** 调用方模型覆盖(agent 定义未指定时使用) */
|
|
55
|
+
model?: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 注册表一等公民。`run` 返回的值经官方 spawn 语义落在
|
|
60
|
+
* `SUBAGENT_FINISHED.result`(M6 调度)或 invokeAgent 的 result(代码直调);
|
|
61
|
+
* 文本类 agent 用 `runAgentText` / 结构化类用 `runAgentStructured`(均经
|
|
62
|
+
* `chatWithStack` 公共装配,禁止在 run 内裸调 `chat`)。
|
|
63
|
+
*/
|
|
64
|
+
export interface AllblueAgentDefinition<TInput = unknown, TResult = unknown> {
|
|
65
|
+
/** agent 名(域.动作);与 bizKey 重命名后的绑定键同空间(T-M0) */
|
|
66
|
+
name: string
|
|
67
|
+
/** 给模型看的描述(M6 合成工具 / router 读取;1-2 句,写清输入与产物) */
|
|
68
|
+
description: string
|
|
69
|
+
/**
|
|
70
|
+
* 产物类型声明(官方 DefinedAgent.produces 语义;T-M4 媒体 agent 首批
|
|
71
|
+
* 消费:'image' | 'video' | 'speech' | 'transcription')。
|
|
72
|
+
* registry.findAgentsByProduces 按此查找(M6 模型调度按产物选 agent)。
|
|
73
|
+
*/
|
|
74
|
+
produces?: string
|
|
75
|
+
/** 入参 zod schema;invokeAgent 与合成工具入参都会按此校验 */
|
|
76
|
+
inputSchema?: z.ZodType<TInput>
|
|
77
|
+
gates?: AgentGates
|
|
78
|
+
/** 缺省温度(模型未覆盖时使用) */
|
|
79
|
+
temperature?: number
|
|
80
|
+
/** run:身份在 ctx.identity(已过门控);禁止读 input 中的身份字段 */
|
|
81
|
+
run: (input: TInput, ctx: AgentRunContext) => Promise<TResult> | TResult
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 泛型擦除视图(注册表/运行时的统一持有形状;注册表聚合异构 agent 用) */
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
|
+
export type AnyAgentDef = AllblueAgentDefinition<any, any>
|
|
87
|
+
|
|
88
|
+
/** invokeAgent 返回值(M1 结构化消费方同步拿 JSON) */
|
|
89
|
+
export interface AgentInvokeResult<TResult = unknown> {
|
|
90
|
+
result: TResult
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 门控失败(限流/无 key/无身份);message 面向用户可直接展示 */
|
|
94
|
+
export class AgentGateError extends Error {
|
|
95
|
+
constructor(message: string) {
|
|
96
|
+
super(message)
|
|
97
|
+
this.name = "AgentGateError"
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
|
+
// AllBlue - 理想之海
|
|
4
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
5
|
+
//
|
|
6
|
+
// This program is free software: you can redistribute it and/or modify
|
|
7
|
+
// it under the terms of the GNU Affero General Public License as published by
|
|
8
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
// (at your option) any later version.
|
|
10
|
+
//
|
|
11
|
+
// This program is distributed in the hope that it will be useful,
|
|
12
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
// GNU Affero General Public License for more details.
|
|
15
|
+
//
|
|
16
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
17
|
+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
|
|
19
|
+
// hasAiKey 门控口径回归:必须遍历用户**全部**启用服务商配置,任一解密后为有效
|
|
20
|
+
// key 即 true——与 resolveDefaultProviderConfig 的取 key 口径(遍历取首个有效)
|
|
21
|
+
// 及 auth session 的 hasAiKey(some(c => !!c.apiKey))对齐。E4 实测缺陷:旧实现
|
|
22
|
+
// findFirst 只看第一条启用配置,首条无 key(如 agnes)即使次条有效(sensenova)
|
|
23
|
+
// 也误判 false → /api/story-ai 等 requiresKey agent 直接 RUN_ERROR「AI 未配置」,
|
|
24
|
+
// 而会话 hasAiKey=true、实际调用解析能取到 key——三处口径不一致。
|
|
25
|
+
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
26
|
+
|
|
27
|
+
const mocks = vi.hoisted(() => ({
|
|
28
|
+
aiProviderConfigFindFirst: vi.fn(),
|
|
29
|
+
aiProviderConfigFindMany: vi.fn(),
|
|
30
|
+
userFindUnique: vi.fn(),
|
|
31
|
+
decrypt: vi.fn(),
|
|
32
|
+
}))
|
|
33
|
+
|
|
34
|
+
vi.mock("@allbluecn/database", () => ({
|
|
35
|
+
prisma: {
|
|
36
|
+
aiProviderConfig: {
|
|
37
|
+
findFirst: mocks.aiProviderConfigFindFirst,
|
|
38
|
+
findMany: mocks.aiProviderConfigFindMany,
|
|
39
|
+
},
|
|
40
|
+
user: { findUnique: mocks.userFindUnique },
|
|
41
|
+
},
|
|
42
|
+
}))
|
|
43
|
+
vi.mock("../runtime", () => ({
|
|
44
|
+
getPorts: () => ({
|
|
45
|
+
keyCipher: { decrypt: mocks.decrypt },
|
|
46
|
+
}),
|
|
47
|
+
}))
|
|
48
|
+
|
|
49
|
+
import { hasAiKey } from "../adapter"
|
|
50
|
+
|
|
51
|
+
const USER_ID = "u-e4"
|
|
52
|
+
|
|
53
|
+
/** 模拟 keyCipher.decrypt:enc:<key> → <key>;null 透传 */
|
|
54
|
+
mocks.decrypt.mockImplementation((v: string | null) => (v === null ? null : v.replace(/^enc:/, "")))
|
|
55
|
+
|
|
56
|
+
const enabledRows = (apiKeys: Array<string | null>) =>
|
|
57
|
+
apiKeys.map((apiKey) => ({ apiKey }))
|
|
58
|
+
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
vi.clearAllMocks()
|
|
61
|
+
delete process.env.OPENROUTER_API_KEY
|
|
62
|
+
delete process.env.OPENAI_API_KEY
|
|
63
|
+
mocks.userFindUnique.mockResolvedValue(null)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe("hasAiKey 遍历全部启用配置(E4 口径对齐)", () => {
|
|
67
|
+
it("首条启用配置无 key、次条有效 key → true(findMany 遍历,RED 缺陷修复点)", async () => {
|
|
68
|
+
mocks.aiProviderConfigFindMany.mockResolvedValue(enabledRows([null, "enc:sk-live-2"]))
|
|
69
|
+
await expect(hasAiKey(USER_ID)).resolves.toBe(true)
|
|
70
|
+
expect(mocks.aiProviderConfigFindMany).toHaveBeenCalledWith({
|
|
71
|
+
where: { userId: USER_ID, enabled: true },
|
|
72
|
+
select: { apiKey: true },
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it("首条即有效 key → true,无需看后续", async () => {
|
|
77
|
+
mocks.aiProviderConfigFindMany.mockResolvedValue(enabledRows(["enc:sk-live-1", null]))
|
|
78
|
+
await expect(hasAiKey(USER_ID)).resolves.toBe(true)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it("解密后为占位符 key 不算有效(isValidKey 口径)", async () => {
|
|
82
|
+
mocks.aiProviderConfigFindMany.mockResolvedValue(
|
|
83
|
+
enabledRows(["enc:sk-your-api-key-here", "enc:sk-or-your-api-key-here"]),
|
|
84
|
+
)
|
|
85
|
+
await expect(hasAiKey(USER_ID)).resolves.toBe(false)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it("全部启用配置无有效 key → 回落旧字段/env(本例两者皆无 → false)", async () => {
|
|
89
|
+
mocks.aiProviderConfigFindMany.mockResolvedValue(enabledRows([null, null]))
|
|
90
|
+
await expect(hasAiKey(USER_ID)).resolves.toBe(false)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it("DB 无启用配置但旧字段(User.aiApiKey)有效 → 回落命中 true", async () => {
|
|
94
|
+
mocks.aiProviderConfigFindMany.mockResolvedValue([])
|
|
95
|
+
mocks.userFindUnique.mockResolvedValue({ aiApiKey: "sk-legacy-field" })
|
|
96
|
+
await expect(hasAiKey(USER_ID)).resolves.toBe(true)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("DB 与旧字段皆无但 env key 有效 → env 兜底 true", async () => {
|
|
100
|
+
mocks.aiProviderConfigFindMany.mockResolvedValue([])
|
|
101
|
+
process.env.OPENROUTER_API_KEY = "sk-or-env"
|
|
102
|
+
await expect(hasAiKey(USER_ID)).resolves.toBe(true)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it("无 userId(未登录上下文)→ 跳过 DB 查询,仅旧字段/env 兜底", async () => {
|
|
106
|
+
await expect(hasAiKey(undefined)).resolves.toBe(false)
|
|
107
|
+
expect(mocks.aiProviderConfigFindMany).not.toHaveBeenCalled()
|
|
108
|
+
})
|
|
109
|
+
})
|