@open-mercato/ai-assistant 0.5.1-develop.3036.f02c281f23 → 0.5.1-develop.3045.b4b3320cc2
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/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +361 -0
- package/README.md +5 -0
- package/dist/index.js +154 -0
- package/dist/index.js.map +2 -2
- package/dist/modules/ai_assistant/__integration__/TC-AI-002-agent-policy.spec.js +73 -0
- package/dist/modules/ai_assistant/__integration__/TC-AI-002-agent-policy.spec.js.map +7 -0
- package/dist/modules/ai_assistant/__integration__/TC-AI-AGENT-SETTINGS-005-settings-page.spec.js +484 -0
- package/dist/modules/ai_assistant/__integration__/TC-AI-AGENT-SETTINGS-005-settings-page.spec.js.map +7 -0
- package/dist/modules/ai_assistant/__integration__/TC-AI-PLAYGROUND-004-playground.spec.js +251 -0
- package/dist/modules/ai_assistant/__integration__/TC-AI-PLAYGROUND-004-playground.spec.js.map +7 -0
- package/dist/modules/ai_assistant/__integration__/TC-INT-AI-TOOLS.spec.js +91 -0
- package/dist/modules/ai_assistant/__integration__/TC-INT-AI-TOOLS.spec.js.map +7 -0
- package/dist/modules/ai_assistant/ai-tools/attachments-pack.js +202 -0
- package/dist/modules/ai_assistant/ai-tools/attachments-pack.js.map +7 -0
- package/dist/modules/ai_assistant/ai-tools/meta-pack.js +121 -0
- package/dist/modules/ai_assistant/ai-tools/meta-pack.js.map +7 -0
- package/dist/modules/ai_assistant/ai-tools/search-pack.js +94 -0
- package/dist/modules/ai_assistant/ai-tools/search-pack.js.map +7 -0
- package/dist/modules/ai_assistant/ai-tools.js +14 -0
- package/dist/modules/ai_assistant/ai-tools.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/actions/[id]/cancel/route.js +175 -0
- package/dist/modules/ai_assistant/api/ai/actions/[id]/cancel/route.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/actions/[id]/confirm/route.js +174 -0
- package/dist/modules/ai_assistant/api/ai/actions/[id]/confirm/route.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/actions/[id]/route.js +101 -0
- package/dist/modules/ai_assistant/api/ai/actions/[id]/route.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/mutation-policy/route.js +311 -0
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/mutation-policy/route.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/prompt-override/route.js +246 -0
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/prompt-override/route.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/agents/route.js +94 -0
- package/dist/modules/ai_assistant/api/ai/agents/route.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/chat/route.js +173 -0
- package/dist/modules/ai_assistant/api/ai/chat/route.js.map +7 -0
- package/dist/modules/ai_assistant/api/ai/run-object/route.js +167 -0
- package/dist/modules/ai_assistant/api/ai/run-object/route.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/AiAgentSettingsPageClient.js +1111 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/AiAgentSettingsPageClient.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/page.js +10 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/page.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/page.meta.js +28 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/page.meta.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/legacy/page.js +10 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/legacy/page.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/legacy/page.meta.js +30 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/legacy/page.meta.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/page.js +4 -6
- package/dist/modules/ai_assistant/backend/config/ai-assistant/page.js.map +2 -2
- package/dist/modules/ai_assistant/backend/config/ai-assistant/page.meta.js +1 -21
- package/dist/modules/ai_assistant/backend/config/ai-assistant/page.meta.js.map +2 -2
- package/dist/modules/ai_assistant/backend/config/ai-assistant/playground/AiPlaygroundPageClient.js +462 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/playground/AiPlaygroundPageClient.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/playground/page.js +10 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/playground/page.js.map +7 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/playground/page.meta.js +28 -0
- package/dist/modules/ai_assistant/backend/config/ai-assistant/playground/page.meta.js.map +7 -0
- package/dist/modules/ai_assistant/cli.js +78 -12
- package/dist/modules/ai_assistant/cli.js.map +2 -2
- package/dist/modules/ai_assistant/data/entities/AiAgentMutationPolicyOverride.js +5 -0
- package/dist/modules/ai_assistant/data/entities/AiAgentMutationPolicyOverride.js.map +7 -0
- package/dist/modules/ai_assistant/data/entities/AiAgentPromptOverride.js +5 -0
- package/dist/modules/ai_assistant/data/entities/AiAgentPromptOverride.js.map +7 -0
- package/dist/modules/ai_assistant/data/entities/AiPendingAction.js +5 -0
- package/dist/modules/ai_assistant/data/entities/AiPendingAction.js.map +7 -0
- package/dist/modules/ai_assistant/data/entities.js +228 -0
- package/dist/modules/ai_assistant/data/entities.js.map +7 -0
- package/dist/modules/ai_assistant/data/repositories/AiAgentMutationPolicyOverrideRepository.js +95 -0
- package/dist/modules/ai_assistant/data/repositories/AiAgentMutationPolicyOverrideRepository.js.map +7 -0
- package/dist/modules/ai_assistant/data/repositories/AiAgentPromptOverrideRepository.js +95 -0
- package/dist/modules/ai_assistant/data/repositories/AiAgentPromptOverrideRepository.js.map +7 -0
- package/dist/modules/ai_assistant/data/repositories/AiPendingActionRepository.js +223 -0
- package/dist/modules/ai_assistant/data/repositories/AiPendingActionRepository.js.map +7 -0
- package/dist/modules/ai_assistant/events.js +33 -0
- package/dist/modules/ai_assistant/events.js.map +7 -0
- package/dist/modules/ai_assistant/i18n/de.json +252 -0
- package/dist/modules/ai_assistant/i18n/en.json +252 -0
- package/dist/modules/ai_assistant/i18n/es.json +252 -0
- package/dist/modules/ai_assistant/i18n/pl.json +252 -0
- package/dist/modules/ai_assistant/lib/agent-policy.js +168 -0
- package/dist/modules/ai_assistant/lib/agent-policy.js.map +7 -0
- package/dist/modules/ai_assistant/lib/agent-registry.js +195 -0
- package/dist/modules/ai_assistant/lib/agent-registry.js.map +7 -0
- package/dist/modules/ai_assistant/lib/agent-runtime.js +451 -0
- package/dist/modules/ai_assistant/lib/agent-runtime.js.map +7 -0
- package/dist/modules/ai_assistant/lib/agent-tools.js +223 -0
- package/dist/modules/ai_assistant/lib/agent-tools.js.map +7 -0
- package/dist/modules/ai_assistant/lib/agent-transport.js +25 -0
- package/dist/modules/ai_assistant/lib/agent-transport.js.map +7 -0
- package/dist/modules/ai_assistant/lib/ai-agent-definition.js +11 -0
- package/dist/modules/ai_assistant/lib/ai-agent-definition.js.map +7 -0
- package/dist/modules/ai_assistant/lib/ai-agents-generated.d.js +1 -0
- package/dist/modules/ai_assistant/lib/ai-agents-generated.d.js.map +7 -0
- package/dist/modules/ai_assistant/lib/ai-api-operation-runner.js +239 -0
- package/dist/modules/ai_assistant/lib/ai-api-operation-runner.js.map +7 -0
- package/dist/modules/ai_assistant/lib/ai-overrides.js +189 -0
- package/dist/modules/ai_assistant/lib/ai-overrides.js.map +7 -0
- package/dist/modules/ai_assistant/lib/ai-tool-definition.js +7 -0
- package/dist/modules/ai_assistant/lib/ai-tool-definition.js.map +7 -0
- package/dist/modules/ai_assistant/lib/ai-tools-generated.d.js +1 -0
- package/dist/modules/ai_assistant/lib/ai-tools-generated.d.js.map +7 -0
- package/dist/modules/ai_assistant/lib/api-backed-tool.js +48 -0
- package/dist/modules/ai_assistant/lib/api-backed-tool.js.map +7 -0
- package/dist/modules/ai_assistant/lib/attachment-bridge-types.js +1 -0
- package/dist/modules/ai_assistant/lib/attachment-bridge-types.js.map +7 -0
- package/dist/modules/ai_assistant/lib/attachment-parts.js +276 -0
- package/dist/modules/ai_assistant/lib/attachment-parts.js.map +7 -0
- package/dist/modules/ai_assistant/lib/model-factory.js +68 -0
- package/dist/modules/ai_assistant/lib/model-factory.js.map +7 -0
- package/dist/modules/ai_assistant/lib/pending-action-cancel.js +86 -0
- package/dist/modules/ai_assistant/lib/pending-action-cancel.js.map +7 -0
- package/dist/modules/ai_assistant/lib/pending-action-client.js +35 -0
- package/dist/modules/ai_assistant/lib/pending-action-client.js.map +7 -0
- package/dist/modules/ai_assistant/lib/pending-action-executor.js +243 -0
- package/dist/modules/ai_assistant/lib/pending-action-executor.js.map +7 -0
- package/dist/modules/ai_assistant/lib/pending-action-recheck.js +246 -0
- package/dist/modules/ai_assistant/lib/pending-action-recheck.js.map +7 -0
- package/dist/modules/ai_assistant/lib/pending-action-types.js +70 -0
- package/dist/modules/ai_assistant/lib/pending-action-types.js.map +7 -0
- package/dist/modules/ai_assistant/lib/prepare-mutation.js +315 -0
- package/dist/modules/ai_assistant/lib/prepare-mutation.js.map +7 -0
- package/dist/modules/ai_assistant/lib/prompt-composition-types.js +7 -0
- package/dist/modules/ai_assistant/lib/prompt-composition-types.js.map +7 -0
- package/dist/modules/ai_assistant/lib/prompt-override-merge.js +175 -0
- package/dist/modules/ai_assistant/lib/prompt-override-merge.js.map +7 -0
- package/dist/modules/ai_assistant/lib/schema-utils.js +5 -1
- package/dist/modules/ai_assistant/lib/schema-utils.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-executor.js +13 -2
- package/dist/modules/ai_assistant/lib/tool-executor.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-loader.js +86 -11
- package/dist/modules/ai_assistant/lib/tool-loader.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-test-fixtures.js +120 -0
- package/dist/modules/ai_assistant/lib/tool-test-fixtures.js.map +7 -0
- package/dist/modules/ai_assistant/lib/tool-test-runner.js +418 -0
- package/dist/modules/ai_assistant/lib/tool-test-runner.js.map +7 -0
- package/dist/modules/ai_assistant/migrations/Migration20260419100521.js +17 -0
- package/dist/modules/ai_assistant/migrations/Migration20260419100521.js.map +7 -0
- package/dist/modules/ai_assistant/migrations/Migration20260419132948.js +16 -0
- package/dist/modules/ai_assistant/migrations/Migration20260419132948.js.map +7 -0
- package/dist/modules/ai_assistant/migrations/Migration20260419134235.js +17 -0
- package/dist/modules/ai_assistant/migrations/Migration20260419134235.js.map +7 -0
- package/dist/modules/ai_assistant/setup.js +36 -0
- package/dist/modules/ai_assistant/setup.js.map +2 -2
- package/dist/modules/ai_assistant/workers/ai-pending-action-cleanup.js +161 -0
- package/dist/modules/ai_assistant/workers/ai-pending-action-cleanup.js.map +7 -0
- package/generated/entities/ai_agent_mutation_policy_override/index.ts +9 -0
- package/generated/entities/ai_agent_prompt_override/index.ts +10 -0
- package/generated/entities/ai_pending_action/index.ts +24 -0
- package/generated/entities.ids.generated.ts +13 -0
- package/generated/entity-fields-registry.ts +57 -0
- package/jest.config.cjs +7 -0
- package/package.json +4 -4
- package/src/index.ts +215 -0
- package/src/modules/ai_assistant/__integration__/README.md +5 -0
- package/src/modules/ai_assistant/__integration__/TC-AI-002-agent-policy.spec.ts +115 -0
- package/src/modules/ai_assistant/__integration__/TC-AI-AGENT-SETTINGS-005-settings-page.spec.ts +574 -0
- package/src/modules/ai_assistant/__integration__/TC-AI-PLAYGROUND-004-playground.spec.ts +333 -0
- package/src/modules/ai_assistant/__integration__/TC-INT-AI-TOOLS.spec.ts +135 -0
- package/src/modules/ai_assistant/__tests__/events.test.ts +145 -0
- package/src/modules/ai_assistant/__tests__/integration/pending-action-contract.test.ts +1015 -0
- package/src/modules/ai_assistant/__tests__/integration/ws-c-attachment-bridge.test.ts +235 -0
- package/src/modules/ai_assistant/__tests__/integration/ws-c-policy-and-tools.test.ts +330 -0
- package/src/modules/ai_assistant/__tests__/integration/ws-c-tool-pack-coverage.test.ts +285 -0
- package/src/modules/ai_assistant/ai-tools/__tests__/attachments-pack.test.ts +322 -0
- package/src/modules/ai_assistant/ai-tools/__tests__/meta-pack.test.ts +218 -0
- package/src/modules/ai_assistant/ai-tools/__tests__/search-pack.test.ts +192 -0
- package/src/modules/ai_assistant/ai-tools/attachments-pack.ts +269 -0
- package/src/modules/ai_assistant/ai-tools/meta-pack.ts +140 -0
- package/src/modules/ai_assistant/ai-tools/search-pack.ts +122 -0
- package/src/modules/ai_assistant/ai-tools.ts +21 -0
- package/src/modules/ai_assistant/api/ai/actions/[id]/__tests__/route.test.ts +222 -0
- package/src/modules/ai_assistant/api/ai/actions/[id]/cancel/__tests__/route.test.ts +286 -0
- package/src/modules/ai_assistant/api/ai/actions/[id]/cancel/route.ts +237 -0
- package/src/modules/ai_assistant/api/ai/actions/[id]/confirm/__tests__/route.test.ts +339 -0
- package/src/modules/ai_assistant/api/ai/actions/[id]/confirm/route.ts +229 -0
- package/src/modules/ai_assistant/api/ai/actions/[id]/route.ts +142 -0
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/mutation-policy/__tests__/route.test.ts +367 -0
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/mutation-policy/route.ts +380 -0
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/prompt-override/__tests__/route.test.ts +333 -0
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/prompt-override/route.ts +307 -0
- package/src/modules/ai_assistant/api/ai/agents/route.ts +107 -0
- package/src/modules/ai_assistant/api/ai/chat/__tests__/route.test.ts +282 -0
- package/src/modules/ai_assistant/api/ai/chat/route.ts +207 -0
- package/src/modules/ai_assistant/api/ai/run-object/__tests__/route.test.ts +282 -0
- package/src/modules/ai_assistant/api/ai/run-object/route.ts +204 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/agents/AiAgentSettingsPageClient.tsx +1419 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/agents/page.meta.ts +26 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/agents/page.tsx +12 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/legacy/page.meta.ts +28 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/legacy/page.tsx +12 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/page.meta.ts +8 -23
- package/src/modules/ai_assistant/backend/config/ai-assistant/page.tsx +15 -10
- package/src/modules/ai_assistant/backend/config/ai-assistant/playground/AiPlaygroundPageClient.tsx +604 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/playground/page.meta.ts +26 -0
- package/src/modules/ai_assistant/backend/config/ai-assistant/playground/page.tsx +12 -0
- package/src/modules/ai_assistant/cli.ts +99 -24
- package/src/modules/ai_assistant/data/__tests__/schema-unique-indexes.test.ts +69 -0
- package/src/modules/ai_assistant/data/entities/AiAgentMutationPolicyOverride.ts +7 -0
- package/src/modules/ai_assistant/data/entities/AiAgentPromptOverride.ts +7 -0
- package/src/modules/ai_assistant/data/entities/AiPendingAction.ts +7 -0
- package/src/modules/ai_assistant/data/entities.ts +270 -0
- package/src/modules/ai_assistant/data/repositories/AiAgentMutationPolicyOverrideRepository.ts +129 -0
- package/src/modules/ai_assistant/data/repositories/AiAgentPromptOverrideRepository.ts +132 -0
- package/src/modules/ai_assistant/data/repositories/AiPendingActionRepository.ts +334 -0
- package/src/modules/ai_assistant/data/repositories/__tests__/AiAgentMutationPolicyOverrideRepository.test.ts +195 -0
- package/src/modules/ai_assistant/data/repositories/__tests__/AiAgentPromptOverrideRepository.test.ts +197 -0
- package/src/modules/ai_assistant/data/repositories/__tests__/AiPendingActionRepository.test.ts +357 -0
- package/src/modules/ai_assistant/events.ts +112 -0
- package/src/modules/ai_assistant/i18n/de.json +252 -0
- package/src/modules/ai_assistant/i18n/en.json +252 -0
- package/src/modules/ai_assistant/i18n/es.json +252 -0
- package/src/modules/ai_assistant/i18n/pl.json +252 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-policy.mutation-override.test.ts +203 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-policy.test.ts +385 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-registry.test.ts +217 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-runtime-object.test.ts +329 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-runtime-parity.test.ts +573 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-runtime.test.ts +291 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-tools.test.ts +172 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-transport.test.ts +41 -0
- package/src/modules/ai_assistant/lib/__tests__/ai-agent-definition.test.ts +183 -0
- package/src/modules/ai_assistant/lib/__tests__/ai-api-operation-runner.test.ts +432 -0
- package/src/modules/ai_assistant/lib/__tests__/ai-overrides.test.ts +308 -0
- package/src/modules/ai_assistant/lib/__tests__/api-backed-tool.test.ts +302 -0
- package/src/modules/ai_assistant/lib/__tests__/attachment-bridge-and-prompt-types.test.ts +188 -0
- package/src/modules/ai_assistant/lib/__tests__/attachment-parts.test.ts +531 -0
- package/src/modules/ai_assistant/lib/__tests__/max-steps-budget.integration.test.ts +263 -0
- package/src/modules/ai_assistant/lib/__tests__/model-factory.integration.test.ts +183 -0
- package/src/modules/ai_assistant/lib/__tests__/model-factory.test.ts +168 -0
- package/src/modules/ai_assistant/lib/__tests__/pending-action-cancel.test.ts +235 -0
- package/src/modules/ai_assistant/lib/__tests__/pending-action-client.test.ts +148 -0
- package/src/modules/ai_assistant/lib/__tests__/pending-action-executor.test.ts +348 -0
- package/src/modules/ai_assistant/lib/__tests__/pending-action-recheck.test.ts +378 -0
- package/src/modules/ai_assistant/lib/__tests__/phase-0-additive-contract.test.ts +299 -0
- package/src/modules/ai_assistant/lib/__tests__/prepare-mutation.test.ts +610 -0
- package/src/modules/ai_assistant/lib/__tests__/prompt-override-merge.test.ts +136 -0
- package/src/modules/ai_assistant/lib/__tests__/tool-loader.test.ts +125 -0
- package/src/modules/ai_assistant/lib/agent-policy.ts +270 -0
- package/src/modules/ai_assistant/lib/agent-registry.ts +277 -0
- package/src/modules/ai_assistant/lib/agent-runtime.ts +751 -0
- package/src/modules/ai_assistant/lib/agent-tools.ts +396 -0
- package/src/modules/ai_assistant/lib/agent-transport.ts +51 -0
- package/src/modules/ai_assistant/lib/ai-agent-definition.ts +86 -0
- package/src/modules/ai_assistant/lib/ai-agents-generated.d.ts +18 -0
- package/src/modules/ai_assistant/lib/ai-api-operation-runner.ts +333 -0
- package/src/modules/ai_assistant/lib/ai-overrides.ts +389 -0
- package/src/modules/ai_assistant/lib/ai-tool-definition.ts +7 -0
- package/src/modules/ai_assistant/lib/ai-tools-generated.d.ts +7 -0
- package/src/modules/ai_assistant/lib/api-backed-tool.ts +85 -0
- package/src/modules/ai_assistant/lib/attachment-bridge-types.ts +24 -0
- package/src/modules/ai_assistant/lib/attachment-parts.ts +433 -0
- package/src/modules/ai_assistant/lib/model-factory.ts +212 -0
- package/src/modules/ai_assistant/lib/pending-action-cancel.ts +179 -0
- package/src/modules/ai_assistant/lib/pending-action-client.ts +126 -0
- package/src/modules/ai_assistant/lib/pending-action-executor.ts +424 -0
- package/src/modules/ai_assistant/lib/pending-action-recheck.ts +410 -0
- package/src/modules/ai_assistant/lib/pending-action-types.ts +194 -0
- package/src/modules/ai_assistant/lib/prepare-mutation.ts +448 -0
- package/src/modules/ai_assistant/lib/prompt-composition-types.ts +24 -0
- package/src/modules/ai_assistant/lib/prompt-override-merge.ts +253 -0
- package/src/modules/ai_assistant/lib/schema-utils.ts +14 -2
- package/src/modules/ai_assistant/lib/tool-executor.ts +25 -3
- package/src/modules/ai_assistant/lib/tool-loader.ts +159 -13
- package/src/modules/ai_assistant/lib/tool-test-fixtures.ts +160 -0
- package/src/modules/ai_assistant/lib/tool-test-runner.ts +596 -0
- package/src/modules/ai_assistant/lib/types.ts +105 -2
- package/src/modules/ai_assistant/migrations/.snapshot-open-mercato.json +871 -0
- package/src/modules/ai_assistant/migrations/Migration20260419100521.ts +17 -0
- package/src/modules/ai_assistant/migrations/Migration20260419132948.ts +16 -0
- package/src/modules/ai_assistant/migrations/Migration20260419134235.ts +17 -0
- package/src/modules/ai_assistant/setup.ts +53 -0
- package/src/modules/ai_assistant/workers/__tests__/ai-pending-action-cleanup.test.ts +333 -0
- package/src/modules/ai_assistant/workers/ai-pending-action-cleanup.ts +269 -0
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
// The real tool-execute path lives in `tool-executor` and expects a bootstrapped
|
|
2
|
+
// DI container. In this unit test we only care that the adapted wrapper
|
|
3
|
+
// invokes the tool's registered `handler` — so stub both
|
|
4
|
+
// `createRequestContainer` and `tool-executor.executeTool` so the handler is
|
|
5
|
+
// called directly with the test's mock context. Must be declared before any
|
|
6
|
+
// import that pulls in `agent-tools.ts`.
|
|
7
|
+
jest.mock('@open-mercato/shared/lib/di/container', () => ({
|
|
8
|
+
createRequestContainer: jest.fn(async () => ({
|
|
9
|
+
resolve: () => null,
|
|
10
|
+
})),
|
|
11
|
+
}))
|
|
12
|
+
|
|
13
|
+
jest.mock('../tool-executor', () => ({
|
|
14
|
+
executeTool: jest.fn(async (name: string, args: unknown, ctx: any) => {
|
|
15
|
+
const { toolRegistry: registry } = await import('../tool-registry')
|
|
16
|
+
const tool = registry.getTool(name)
|
|
17
|
+
if (!tool) {
|
|
18
|
+
return { success: false, error: `Tool "${name}" not found` }
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const result = await tool.handler(args as never, ctx)
|
|
22
|
+
return { success: true, result }
|
|
23
|
+
} catch (error) {
|
|
24
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
25
|
+
return { success: false, error: message }
|
|
26
|
+
}
|
|
27
|
+
}),
|
|
28
|
+
}))
|
|
29
|
+
|
|
30
|
+
import { z } from 'zod'
|
|
31
|
+
import type { AwilixContainer } from 'awilix'
|
|
32
|
+
import {
|
|
33
|
+
AiMutationPreparationError,
|
|
34
|
+
computeMutationIdempotencyKey,
|
|
35
|
+
prepareMutation,
|
|
36
|
+
} from '../prepare-mutation'
|
|
37
|
+
import { resolveAiAgentTools } from '../agent-tools'
|
|
38
|
+
import type { AiAgentDefinition } from '../ai-agent-definition'
|
|
39
|
+
import type { AiToolDefinition } from '../types'
|
|
40
|
+
import {
|
|
41
|
+
resetAgentRegistryForTests,
|
|
42
|
+
seedAgentRegistryForTests,
|
|
43
|
+
} from '../agent-registry'
|
|
44
|
+
import { registerMcpTool, toolRegistry } from '../tool-registry'
|
|
45
|
+
import type {
|
|
46
|
+
AiPendingActionStatus,
|
|
47
|
+
AiPendingActionQueueMode,
|
|
48
|
+
} from '../pending-action-types'
|
|
49
|
+
|
|
50
|
+
type Row = {
|
|
51
|
+
id: string
|
|
52
|
+
tenantId: string
|
|
53
|
+
organizationId: string | null
|
|
54
|
+
agentId: string
|
|
55
|
+
toolName: string
|
|
56
|
+
conversationId: string | null
|
|
57
|
+
targetEntityType: string | null
|
|
58
|
+
targetRecordId: string | null
|
|
59
|
+
normalizedInput: Record<string, unknown>
|
|
60
|
+
fieldDiff: Array<{ field: string; before: unknown; after: unknown }>
|
|
61
|
+
records: Array<Record<string, unknown>> | null
|
|
62
|
+
failedRecords: Array<Record<string, unknown>> | null
|
|
63
|
+
sideEffectsSummary: string | null
|
|
64
|
+
recordVersion: string | null
|
|
65
|
+
attachmentIds: string[]
|
|
66
|
+
idempotencyKey: string
|
|
67
|
+
createdByUserId: string
|
|
68
|
+
status: AiPendingActionStatus
|
|
69
|
+
queueMode: AiPendingActionQueueMode
|
|
70
|
+
executionResult: Record<string, unknown> | null
|
|
71
|
+
createdAt: Date
|
|
72
|
+
expiresAt: Date
|
|
73
|
+
resolvedAt: Date | null
|
|
74
|
+
resolvedByUserId: string | null
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let idCounter = 0
|
|
78
|
+
|
|
79
|
+
function rowMatchesWhere(row: Row, where: any): boolean {
|
|
80
|
+
if (!where) return true
|
|
81
|
+
if (where.id && row.id !== where.id) return false
|
|
82
|
+
if (where.tenantId && row.tenantId !== where.tenantId) return false
|
|
83
|
+
if ('organizationId' in where) {
|
|
84
|
+
const expected = where.organizationId ?? null
|
|
85
|
+
if ((row.organizationId ?? null) !== expected) return false
|
|
86
|
+
}
|
|
87
|
+
if (where.idempotencyKey && row.idempotencyKey !== where.idempotencyKey) {
|
|
88
|
+
return false
|
|
89
|
+
}
|
|
90
|
+
if (where.status && row.status !== where.status) return false
|
|
91
|
+
return true
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function mockEm() {
|
|
95
|
+
const store: Row[] = []
|
|
96
|
+
|
|
97
|
+
const find = async (_entity: unknown, where: any, options?: any): Promise<Row[]> => {
|
|
98
|
+
let rows = store.filter((row) => rowMatchesWhere(row, where))
|
|
99
|
+
if (options?.orderBy?.createdAt === 'desc') {
|
|
100
|
+
rows = [...rows].sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())
|
|
101
|
+
}
|
|
102
|
+
if (typeof options?.limit === 'number') rows = rows.slice(0, options.limit)
|
|
103
|
+
return rows
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const em: any = {
|
|
107
|
+
find,
|
|
108
|
+
findOne: async (_entity: unknown, where: any, options?: any) => {
|
|
109
|
+
const rows = await find(_entity, where, options)
|
|
110
|
+
return rows[0] ?? null
|
|
111
|
+
},
|
|
112
|
+
create: (_entity: unknown, data: any) => {
|
|
113
|
+
idCounter += 1
|
|
114
|
+
const row: Row = {
|
|
115
|
+
id: `row-${idCounter}`,
|
|
116
|
+
tenantId: data.tenantId,
|
|
117
|
+
organizationId: data.organizationId ?? null,
|
|
118
|
+
agentId: data.agentId,
|
|
119
|
+
toolName: data.toolName,
|
|
120
|
+
conversationId: data.conversationId ?? null,
|
|
121
|
+
targetEntityType: data.targetEntityType ?? null,
|
|
122
|
+
targetRecordId: data.targetRecordId ?? null,
|
|
123
|
+
normalizedInput: data.normalizedInput ?? {},
|
|
124
|
+
fieldDiff: Array.isArray(data.fieldDiff) ? data.fieldDiff : [],
|
|
125
|
+
records: data.records ?? null,
|
|
126
|
+
failedRecords: data.failedRecords ?? null,
|
|
127
|
+
sideEffectsSummary: data.sideEffectsSummary ?? null,
|
|
128
|
+
recordVersion: data.recordVersion ?? null,
|
|
129
|
+
attachmentIds: Array.isArray(data.attachmentIds) ? data.attachmentIds : [],
|
|
130
|
+
idempotencyKey: data.idempotencyKey,
|
|
131
|
+
createdByUserId: data.createdByUserId,
|
|
132
|
+
status: (data.status ?? 'pending') as AiPendingActionStatus,
|
|
133
|
+
queueMode: (data.queueMode ?? 'inline') as AiPendingActionQueueMode,
|
|
134
|
+
executionResult: data.executionResult ?? null,
|
|
135
|
+
createdAt: data.createdAt instanceof Date ? data.createdAt : new Date(),
|
|
136
|
+
expiresAt: data.expiresAt instanceof Date ? data.expiresAt : new Date(),
|
|
137
|
+
resolvedAt: data.resolvedAt ?? null,
|
|
138
|
+
resolvedByUserId: data.resolvedByUserId ?? null,
|
|
139
|
+
}
|
|
140
|
+
return row
|
|
141
|
+
},
|
|
142
|
+
persist: (row: Row) => {
|
|
143
|
+
em.__pendingPersist = row
|
|
144
|
+
return em
|
|
145
|
+
},
|
|
146
|
+
flush: async () => {
|
|
147
|
+
if (em.__pendingPersist) {
|
|
148
|
+
const row = em.__pendingPersist as Row
|
|
149
|
+
const idx = store.findIndex((candidate) => candidate.id === row.id)
|
|
150
|
+
if (idx >= 0) store[idx] = row
|
|
151
|
+
else store.push(row)
|
|
152
|
+
em.__pendingPersist = null
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
transactional: async (fn: (tx: any) => Promise<unknown>) => fn(em),
|
|
156
|
+
__pendingPersist: null as Row | null,
|
|
157
|
+
__store: store,
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return em
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function makeContainer(em: any): AwilixContainer {
|
|
164
|
+
return {
|
|
165
|
+
resolve: (name: string) => {
|
|
166
|
+
if (name === 'em') return em
|
|
167
|
+
throw new Error(`unknown dependency ${name}`)
|
|
168
|
+
},
|
|
169
|
+
} as unknown as AwilixContainer
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function makeAgent(
|
|
173
|
+
overrides: Partial<AiAgentDefinition> & Pick<AiAgentDefinition, 'id'>,
|
|
174
|
+
): AiAgentDefinition {
|
|
175
|
+
return {
|
|
176
|
+
id: overrides.id,
|
|
177
|
+
moduleId: 'catalog',
|
|
178
|
+
label: `${overrides.id} label`,
|
|
179
|
+
description: `${overrides.id} description`,
|
|
180
|
+
systemPrompt: 'You are a test agent.',
|
|
181
|
+
allowedTools: [],
|
|
182
|
+
readOnly: false,
|
|
183
|
+
mutationPolicy: 'confirm-required',
|
|
184
|
+
...overrides,
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function makeTool(
|
|
189
|
+
overrides: Partial<AiToolDefinition> & Pick<AiToolDefinition, 'name'>,
|
|
190
|
+
): AiToolDefinition {
|
|
191
|
+
return {
|
|
192
|
+
description: `${overrides.name} description`,
|
|
193
|
+
inputSchema: z.object({}).passthrough(),
|
|
194
|
+
handler: async () => ({ ok: true }),
|
|
195
|
+
...overrides,
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const baseCtx = {
|
|
200
|
+
tenantId: 't-alpha',
|
|
201
|
+
organizationId: 'org-alpha',
|
|
202
|
+
userId: 'u-1',
|
|
203
|
+
features: ['*'],
|
|
204
|
+
isSuperAdmin: true,
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
describe('prepareMutation', () => {
|
|
208
|
+
beforeEach(() => {
|
|
209
|
+
idCounter = 0
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
it('computeMutationIdempotencyKey is stable under object key reordering', () => {
|
|
213
|
+
const a = computeMutationIdempotencyKey({
|
|
214
|
+
tenantId: 't-alpha',
|
|
215
|
+
organizationId: 'org-alpha',
|
|
216
|
+
agentId: 'catalog.merch',
|
|
217
|
+
conversationId: 'conv-1',
|
|
218
|
+
toolName: 'catalog.products.update',
|
|
219
|
+
normalizedInput: { productId: 'p-1', patch: { name: 'new', sku: 'sku-1' } },
|
|
220
|
+
})
|
|
221
|
+
const b = computeMutationIdempotencyKey({
|
|
222
|
+
tenantId: 't-alpha',
|
|
223
|
+
organizationId: 'org-alpha',
|
|
224
|
+
agentId: 'catalog.merch',
|
|
225
|
+
conversationId: 'conv-1',
|
|
226
|
+
toolName: 'catalog.products.update',
|
|
227
|
+
normalizedInput: { patch: { sku: 'sku-1', name: 'new' }, productId: 'p-1' },
|
|
228
|
+
})
|
|
229
|
+
expect(a).toBe(b)
|
|
230
|
+
expect(a).toHaveLength(64)
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('single-record happy path: emits mutation-preview-card with pendingActionId and a computed fieldDiff', async () => {
|
|
234
|
+
const em = mockEm()
|
|
235
|
+
const container = makeContainer(em)
|
|
236
|
+
const tool = makeTool({
|
|
237
|
+
name: 'catalog.products.update',
|
|
238
|
+
isMutation: true,
|
|
239
|
+
loadBeforeRecord: async () => ({
|
|
240
|
+
recordId: 'p-1',
|
|
241
|
+
entityType: 'catalog.product',
|
|
242
|
+
recordVersion: 'v-1',
|
|
243
|
+
before: { name: 'old', sku: 'sku-1' },
|
|
244
|
+
}),
|
|
245
|
+
})
|
|
246
|
+
const agent = makeAgent({ id: 'catalog.merch' })
|
|
247
|
+
|
|
248
|
+
const { uiPart, pendingAction } = await prepareMutation(
|
|
249
|
+
{
|
|
250
|
+
agent,
|
|
251
|
+
tool,
|
|
252
|
+
toolCallArgs: { productId: 'p-1', patch: { name: 'new', sku: 'sku-1' } },
|
|
253
|
+
conversationId: 'conv-1',
|
|
254
|
+
},
|
|
255
|
+
{ ...baseCtx, container },
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
expect(uiPart.componentId).toBe('mutation-preview-card')
|
|
259
|
+
expect(uiPart.props.pendingActionId).toBe(pendingAction.id)
|
|
260
|
+
expect(uiPart.props.fieldDiff).toEqual([
|
|
261
|
+
{ field: 'name', before: 'old', after: 'new' },
|
|
262
|
+
])
|
|
263
|
+
expect(pendingAction.targetEntityType).toBe('catalog.product')
|
|
264
|
+
expect(pendingAction.targetRecordId).toBe('p-1')
|
|
265
|
+
expect(pendingAction.recordVersion).toBe('v-1')
|
|
266
|
+
expect(pendingAction.tenantId).toBe('t-alpha')
|
|
267
|
+
expect(pendingAction.organizationId).toBe('org-alpha')
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it('batch happy path: populates records[] with per-record diffs (fieldDiff stays []) when isBulk=true', async () => {
|
|
271
|
+
const em = mockEm()
|
|
272
|
+
const container = makeContainer(em)
|
|
273
|
+
const tool = makeTool({
|
|
274
|
+
name: 'catalog.products.bulk_update',
|
|
275
|
+
isMutation: true,
|
|
276
|
+
isBulk: true,
|
|
277
|
+
loadBeforeRecords: async () => [
|
|
278
|
+
{
|
|
279
|
+
recordId: 'p-1',
|
|
280
|
+
entityType: 'catalog.product',
|
|
281
|
+
label: 'Widget',
|
|
282
|
+
recordVersion: 'v-1',
|
|
283
|
+
before: { name: 'old', sku: 'sku-1' },
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
recordId: 'p-2',
|
|
287
|
+
entityType: 'catalog.product',
|
|
288
|
+
label: 'Gadget',
|
|
289
|
+
recordVersion: 'v-2',
|
|
290
|
+
before: { name: 'gadget-old', sku: 'sku-2' },
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
})
|
|
294
|
+
const agent = makeAgent({ id: 'catalog.merch' })
|
|
295
|
+
|
|
296
|
+
const { uiPart, pendingAction } = await prepareMutation(
|
|
297
|
+
{
|
|
298
|
+
agent,
|
|
299
|
+
tool,
|
|
300
|
+
toolCallArgs: {
|
|
301
|
+
records: [
|
|
302
|
+
{ recordId: 'p-1', patch: { name: 'new-1' } },
|
|
303
|
+
{ recordId: 'p-2', patch: { name: 'new-2', sku: 'sku-2-new' } },
|
|
304
|
+
],
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
{ ...baseCtx, container },
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
expect(uiPart.componentId).toBe('mutation-preview-card')
|
|
311
|
+
expect(uiPart.props.records).toBeDefined()
|
|
312
|
+
expect(uiPart.props.fieldDiff).toBeUndefined()
|
|
313
|
+
const records = pendingAction.records as Array<{
|
|
314
|
+
recordId: string
|
|
315
|
+
label: string
|
|
316
|
+
fieldDiff: Array<{ field: string; before: unknown; after: unknown }>
|
|
317
|
+
}>
|
|
318
|
+
expect(records).toHaveLength(2)
|
|
319
|
+
expect(records[0].recordId).toBe('p-1')
|
|
320
|
+
expect(records[0].label).toBe('Widget')
|
|
321
|
+
// Patch only carries `name` — keys present in `before` but not in `after`
|
|
322
|
+
// still surface as diff entries with after=undefined (they are "cleared").
|
|
323
|
+
expect(records[0].fieldDiff).toEqual(
|
|
324
|
+
expect.arrayContaining([{ field: 'name', before: 'old', after: 'new-1' }]),
|
|
325
|
+
)
|
|
326
|
+
expect(records[1].fieldDiff).toEqual(
|
|
327
|
+
expect.arrayContaining([
|
|
328
|
+
{ field: 'name', before: 'gadget-old', after: 'new-2' },
|
|
329
|
+
{ field: 'sku', before: 'sku-2', after: 'sku-2-new' },
|
|
330
|
+
]),
|
|
331
|
+
)
|
|
332
|
+
expect(pendingAction.fieldDiff).toEqual([])
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it('missing loadBeforeRecord: ships fieldDiff=[] + sideEffectsSummary warning + still creates the pending row', async () => {
|
|
336
|
+
const em = mockEm()
|
|
337
|
+
const container = makeContainer(em)
|
|
338
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
339
|
+
const tool = makeTool({
|
|
340
|
+
name: 'catalog.products.update',
|
|
341
|
+
isMutation: true,
|
|
342
|
+
})
|
|
343
|
+
const agent = makeAgent({ id: 'catalog.merch' })
|
|
344
|
+
|
|
345
|
+
const { uiPart, pendingAction } = await prepareMutation(
|
|
346
|
+
{
|
|
347
|
+
agent,
|
|
348
|
+
tool,
|
|
349
|
+
toolCallArgs: { productId: 'p-1', patch: { name: 'new' } },
|
|
350
|
+
},
|
|
351
|
+
{ ...baseCtx, container },
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
expect(uiPart.props.fieldDiff).toEqual([])
|
|
355
|
+
expect(uiPart.props.sideEffectsSummary).toMatch(/did not declare a field-diff resolver/)
|
|
356
|
+
expect(pendingAction.sideEffectsSummary).toMatch(/did not declare a field-diff resolver/)
|
|
357
|
+
expect(pendingAction.status).toBe('pending')
|
|
358
|
+
expect(warnSpy).toHaveBeenCalled()
|
|
359
|
+
warnSpy.mockRestore()
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
it('fails closed with read_only_agent when effective mutationPolicy is read-only', async () => {
|
|
363
|
+
const em = mockEm()
|
|
364
|
+
const container = makeContainer(em)
|
|
365
|
+
const tool = makeTool({ name: 'catalog.products.update', isMutation: true })
|
|
366
|
+
const agent = makeAgent({
|
|
367
|
+
id: 'catalog.merch',
|
|
368
|
+
readOnly: true,
|
|
369
|
+
mutationPolicy: 'read-only',
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
await expect(
|
|
373
|
+
prepareMutation(
|
|
374
|
+
{
|
|
375
|
+
agent,
|
|
376
|
+
tool,
|
|
377
|
+
toolCallArgs: { productId: 'p-1' },
|
|
378
|
+
},
|
|
379
|
+
{ ...baseCtx, container },
|
|
380
|
+
),
|
|
381
|
+
).rejects.toMatchObject({
|
|
382
|
+
name: 'AiMutationPreparationError',
|
|
383
|
+
code: 'read_only_agent',
|
|
384
|
+
})
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
it('fails closed with not_a_mutation_tool when the tool is not marked isMutation', async () => {
|
|
388
|
+
const em = mockEm()
|
|
389
|
+
const container = makeContainer(em)
|
|
390
|
+
const tool = makeTool({ name: 'catalog.products.list' })
|
|
391
|
+
const agent = makeAgent({ id: 'catalog.merch' })
|
|
392
|
+
|
|
393
|
+
await expect(
|
|
394
|
+
prepareMutation(
|
|
395
|
+
{ agent, tool, toolCallArgs: {} },
|
|
396
|
+
{ ...baseCtx, container },
|
|
397
|
+
),
|
|
398
|
+
).rejects.toBeInstanceOf(AiMutationPreparationError)
|
|
399
|
+
await expect(
|
|
400
|
+
prepareMutation(
|
|
401
|
+
{ agent, tool, toolCallArgs: {} },
|
|
402
|
+
{ ...baseCtx, container },
|
|
403
|
+
),
|
|
404
|
+
).rejects.toMatchObject({ code: 'not_a_mutation_tool' })
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
it('is idempotent: same (agent, tool, args, conversationId) returns the same pendingActionId', async () => {
|
|
408
|
+
const em = mockEm()
|
|
409
|
+
const container = makeContainer(em)
|
|
410
|
+
const tool = makeTool({
|
|
411
|
+
name: 'catalog.products.update',
|
|
412
|
+
isMutation: true,
|
|
413
|
+
loadBeforeRecord: async () => ({
|
|
414
|
+
recordId: 'p-1',
|
|
415
|
+
entityType: 'catalog.product',
|
|
416
|
+
recordVersion: 'v-1',
|
|
417
|
+
before: { name: 'old' },
|
|
418
|
+
}),
|
|
419
|
+
})
|
|
420
|
+
const agent = makeAgent({ id: 'catalog.merch' })
|
|
421
|
+
const args = { productId: 'p-1', patch: { name: 'new' } }
|
|
422
|
+
|
|
423
|
+
const first = await prepareMutation(
|
|
424
|
+
{ agent, tool, toolCallArgs: args, conversationId: 'conv-1' },
|
|
425
|
+
{ ...baseCtx, container },
|
|
426
|
+
)
|
|
427
|
+
const second = await prepareMutation(
|
|
428
|
+
{ agent, tool, toolCallArgs: args, conversationId: 'conv-1' },
|
|
429
|
+
{ ...baseCtx, container },
|
|
430
|
+
)
|
|
431
|
+
expect(second.pendingAction.id).toBe(first.pendingAction.id)
|
|
432
|
+
expect(em.__store.length).toBe(1)
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
it('enforces tenant scoping: persisted row carries ctx.tenantId + ctx.organizationId', async () => {
|
|
436
|
+
const em = mockEm()
|
|
437
|
+
const container = makeContainer(em)
|
|
438
|
+
const tool = makeTool({
|
|
439
|
+
name: 'catalog.products.update',
|
|
440
|
+
isMutation: true,
|
|
441
|
+
loadBeforeRecord: async () => ({
|
|
442
|
+
recordId: 'p-1',
|
|
443
|
+
entityType: 'catalog.product',
|
|
444
|
+
recordVersion: null,
|
|
445
|
+
before: {},
|
|
446
|
+
}),
|
|
447
|
+
})
|
|
448
|
+
const agent = makeAgent({ id: 'catalog.merch' })
|
|
449
|
+
|
|
450
|
+
const { pendingAction } = await prepareMutation(
|
|
451
|
+
{ agent, tool, toolCallArgs: {} },
|
|
452
|
+
{
|
|
453
|
+
tenantId: 't-beta',
|
|
454
|
+
organizationId: 'org-beta',
|
|
455
|
+
userId: 'u-2',
|
|
456
|
+
features: ['*'],
|
|
457
|
+
isSuperAdmin: false,
|
|
458
|
+
container,
|
|
459
|
+
},
|
|
460
|
+
)
|
|
461
|
+
expect(pendingAction.tenantId).toBe('t-beta')
|
|
462
|
+
expect(pendingAction.organizationId).toBe('org-beta')
|
|
463
|
+
expect(pendingAction.createdByUserId).toBe('u-2')
|
|
464
|
+
})
|
|
465
|
+
|
|
466
|
+
it('passes attachmentIds through from toolCallArgs into the pending action row', async () => {
|
|
467
|
+
const em = mockEm()
|
|
468
|
+
const container = makeContainer(em)
|
|
469
|
+
const tool = makeTool({
|
|
470
|
+
name: 'catalog.products.update',
|
|
471
|
+
isMutation: true,
|
|
472
|
+
loadBeforeRecord: async () => ({
|
|
473
|
+
recordId: 'p-1',
|
|
474
|
+
entityType: 'catalog.product',
|
|
475
|
+
recordVersion: null,
|
|
476
|
+
before: {},
|
|
477
|
+
}),
|
|
478
|
+
})
|
|
479
|
+
const agent = makeAgent({ id: 'catalog.merch' })
|
|
480
|
+
|
|
481
|
+
const { pendingAction } = await prepareMutation(
|
|
482
|
+
{
|
|
483
|
+
agent,
|
|
484
|
+
tool,
|
|
485
|
+
toolCallArgs: {
|
|
486
|
+
productId: 'p-1',
|
|
487
|
+
patch: {},
|
|
488
|
+
attachmentIds: ['att-1', 'att-2'],
|
|
489
|
+
},
|
|
490
|
+
},
|
|
491
|
+
{ ...baseCtx, container },
|
|
492
|
+
)
|
|
493
|
+
expect(pendingAction.attachmentIds).toEqual(['att-1', 'att-2'])
|
|
494
|
+
})
|
|
495
|
+
})
|
|
496
|
+
|
|
497
|
+
describe('resolveAiAgentTools mutation interception (Step 5.6)', () => {
|
|
498
|
+
let warnSpy: jest.SpyInstance
|
|
499
|
+
|
|
500
|
+
beforeEach(() => {
|
|
501
|
+
resetAgentRegistryForTests()
|
|
502
|
+
toolRegistry.clear()
|
|
503
|
+
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
504
|
+
})
|
|
505
|
+
|
|
506
|
+
afterEach(() => {
|
|
507
|
+
warnSpy.mockRestore()
|
|
508
|
+
})
|
|
509
|
+
|
|
510
|
+
afterAll(() => {
|
|
511
|
+
resetAgentRegistryForTests()
|
|
512
|
+
toolRegistry.clear()
|
|
513
|
+
})
|
|
514
|
+
|
|
515
|
+
it('replaces the mutation-tool handler with a wrapper that creates a pending action and enqueues a UI part (original handler NEVER invoked)', async () => {
|
|
516
|
+
const em = mockEm()
|
|
517
|
+
const container = makeContainer(em)
|
|
518
|
+
const handlerSpy = jest.fn(async () => ({ shouldNeverRun: true }))
|
|
519
|
+
const tool: AiToolDefinition = {
|
|
520
|
+
name: 'catalog.products.update',
|
|
521
|
+
description: 'update',
|
|
522
|
+
inputSchema: z.object({}).passthrough(),
|
|
523
|
+
handler: handlerSpy,
|
|
524
|
+
isMutation: true,
|
|
525
|
+
loadBeforeRecord: async () => ({
|
|
526
|
+
recordId: 'p-1',
|
|
527
|
+
entityType: 'catalog.product',
|
|
528
|
+
recordVersion: null,
|
|
529
|
+
before: { name: 'old' },
|
|
530
|
+
}),
|
|
531
|
+
}
|
|
532
|
+
registerMcpTool(tool, { moduleId: 'catalog' })
|
|
533
|
+
seedAgentRegistryForTests([
|
|
534
|
+
makeAgent({
|
|
535
|
+
id: 'catalog.merch',
|
|
536
|
+
moduleId: 'catalog',
|
|
537
|
+
allowedTools: ['catalog.products.update'],
|
|
538
|
+
readOnly: false,
|
|
539
|
+
mutationPolicy: 'confirm-required',
|
|
540
|
+
}),
|
|
541
|
+
])
|
|
542
|
+
|
|
543
|
+
const resolved = await resolveAiAgentTools({
|
|
544
|
+
agentId: 'catalog.merch',
|
|
545
|
+
authContext: {
|
|
546
|
+
tenantId: 't-alpha',
|
|
547
|
+
organizationId: 'org-alpha',
|
|
548
|
+
userId: 'u-1',
|
|
549
|
+
features: ['*'],
|
|
550
|
+
isSuperAdmin: true,
|
|
551
|
+
},
|
|
552
|
+
container,
|
|
553
|
+
conversationId: 'conv-1',
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
expect(Object.keys(resolved.tools)).toContain('catalog__products__update')
|
|
557
|
+
const adapted = resolved.tools['catalog__products__update'] as unknown as {
|
|
558
|
+
execute: (args: unknown) => Promise<unknown>
|
|
559
|
+
}
|
|
560
|
+
const outcome = await adapted.execute({ productId: 'p-1', patch: { name: 'new' } })
|
|
561
|
+
expect(handlerSpy).not.toHaveBeenCalled()
|
|
562
|
+
expect(em.__store).toHaveLength(1)
|
|
563
|
+
expect(em.__store[0].status).toBe('pending')
|
|
564
|
+
expect(resolved.uiPartQueue.size()).toBe(1)
|
|
565
|
+
const drained = resolved.uiPartQueue.drain()
|
|
566
|
+
expect(drained[0].componentId).toBe('mutation-preview-card')
|
|
567
|
+
expect(String(outcome)).toMatch(/pending-confirmation/)
|
|
568
|
+
})
|
|
569
|
+
|
|
570
|
+
it('does NOT intercept non-mutation tools even when the agent is mutation-capable', async () => {
|
|
571
|
+
const em = mockEm()
|
|
572
|
+
const container = makeContainer(em)
|
|
573
|
+
const handlerSpy = jest.fn(async () => ({ ok: true, items: [] }))
|
|
574
|
+
const tool: AiToolDefinition = {
|
|
575
|
+
name: 'catalog.products.list',
|
|
576
|
+
description: 'list',
|
|
577
|
+
inputSchema: z.object({}).passthrough(),
|
|
578
|
+
handler: handlerSpy,
|
|
579
|
+
}
|
|
580
|
+
registerMcpTool(tool, { moduleId: 'catalog' })
|
|
581
|
+
seedAgentRegistryForTests([
|
|
582
|
+
makeAgent({
|
|
583
|
+
id: 'catalog.merch',
|
|
584
|
+
moduleId: 'catalog',
|
|
585
|
+
allowedTools: ['catalog.products.list'],
|
|
586
|
+
readOnly: false,
|
|
587
|
+
mutationPolicy: 'confirm-required',
|
|
588
|
+
}),
|
|
589
|
+
])
|
|
590
|
+
|
|
591
|
+
const resolved = await resolveAiAgentTools({
|
|
592
|
+
agentId: 'catalog.merch',
|
|
593
|
+
authContext: {
|
|
594
|
+
tenantId: 't-alpha',
|
|
595
|
+
organizationId: 'org-alpha',
|
|
596
|
+
userId: 'u-1',
|
|
597
|
+
features: ['*'],
|
|
598
|
+
isSuperAdmin: true,
|
|
599
|
+
},
|
|
600
|
+
container,
|
|
601
|
+
})
|
|
602
|
+
const adapted = resolved.tools['catalog__products__list'] as unknown as {
|
|
603
|
+
execute: (args: unknown) => Promise<unknown>
|
|
604
|
+
}
|
|
605
|
+
await adapted.execute({})
|
|
606
|
+
expect(handlerSpy).toHaveBeenCalledTimes(1)
|
|
607
|
+
expect(em.__store).toHaveLength(0)
|
|
608
|
+
expect(resolved.uiPartQueue.size()).toBe(0)
|
|
609
|
+
})
|
|
610
|
+
})
|