@harrylabsj/kiwi-merchant-cloud 0.12.31 → 0.12.32
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/app/a2a/node.d.ts +1 -0
- package/app/a2a/node.js +170 -2
- package/app/a2a/node.js.map +1 -1
- package/app/a2a/reception/agent-runtime.d.ts +32 -1
- package/app/a2a/reception/agent-runtime.js +420 -11
- package/app/a2a/reception/agent-runtime.js.map +1 -1
- package/app/a2a/reception/customer-memory-tools.d.ts +73 -0
- package/app/a2a/reception/customer-memory-tools.js +17 -0
- package/app/a2a/reception/customer-memory-tools.js.map +1 -0
- package/app/a2a/reception/engine.d.ts +12 -0
- package/app/a2a/reception/engine.js +42 -1
- package/app/a2a/reception/engine.js.map +1 -1
- package/app/a2a/reception/publish.d.ts +14 -1
- package/app/a2a/reception/publish.js +62 -1
- package/app/a2a/reception/publish.js.map +1 -1
- package/app/a2a/reception/question-spec.d.ts +91 -0
- package/app/a2a/reception/question-spec.js +260 -0
- package/app/a2a/reception/question-spec.js.map +1 -0
- package/app/a2a/reception/reply-outbox.d.ts +6 -0
- package/app/a2a/reception/reply-outbox.js +44 -0
- package/app/a2a/reception/reply-outbox.js.map +1 -1
- package/app/a2a/reception/responder.d.ts +13 -1
- package/app/a2a/reception/responder.js +26 -3
- package/app/a2a/reception/responder.js.map +1 -1
- package/app/a2a/reception/skill-catalog.d.ts +72 -0
- package/app/a2a/reception/skill-catalog.js +85 -0
- package/app/a2a/reception/skill-catalog.js.map +1 -0
- package/app/a2a/reception/skill-contract.d.ts +70 -0
- package/app/a2a/reception/skill-contract.js +183 -0
- package/app/a2a/reception/skill-contract.js.map +1 -0
- package/app/a2a/reception/types.d.ts +12 -0
- package/app/a2a/server/customer-identity-gateway.d.ts +111 -0
- package/app/a2a/server/customer-identity-gateway.js +441 -0
- package/app/a2a/server/customer-identity-gateway.js.map +1 -0
- package/app/a2a/server/merchant-handler.d.ts +34 -0
- package/app/a2a/server/merchant-handler.js +439 -19
- package/app/a2a/server/merchant-handler.js.map +1 -1
- package/app/a2a/server/pipeline.js +74 -29
- package/app/a2a/server/pipeline.js.map +1 -1
- package/app/a2a/server/types.d.ts +2 -0
- package/app/a2a/unit-alias.d.ts +3 -3
- package/app/a2a/unit-alias.js +9 -5
- package/app/a2a/unit-alias.js.map +1 -1
- package/app/auth/merchant-sessions.d.ts +3 -0
- package/app/auth/merchant-sessions.js +21 -5
- package/app/auth/merchant-sessions.js.map +1 -1
- package/app/buyer-core/a2a-knp.d.ts +4 -2
- package/app/buyer-core/a2a-knp.js +5 -2
- package/app/buyer-core/a2a-knp.js.map +1 -1
- package/app/buyer-core/a2a-negotiator.d.ts +1 -0
- package/app/buyer-core/a2a-negotiator.js +18 -11
- package/app/buyer-core/a2a-negotiator.js.map +1 -1
- package/app/buyer-core/a2a-quote-fetcher.d.ts +25 -0
- package/app/buyer-core/a2a-quote-fetcher.js +82 -4
- package/app/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/app/buyer-core/a2a-quote-history.d.ts +94 -0
- package/app/buyer-core/a2a-quote-history.js +149 -0
- package/app/buyer-core/a2a-quote-history.js.map +1 -0
- package/app/buyer-core/build-service.d.ts +19 -0
- package/app/buyer-core/build-service.js +30 -1
- package/app/buyer-core/build-service.js.map +1 -1
- package/app/buyer-core/errors.d.ts +1 -1
- package/app/buyer-core/errors.js +1 -0
- package/app/buyer-core/errors.js.map +1 -1
- package/app/buyer-core/open-inquiry-signer.d.ts +1 -0
- package/app/buyer-core/open-inquiry-signer.js +5 -2
- package/app/buyer-core/open-inquiry-signer.js.map +1 -1
- package/app/buyer-core/protocol-recovery.d.ts +2 -1
- package/app/buyer-core/protocol-recovery.js +14 -4
- package/app/buyer-core/protocol-recovery.js.map +1 -1
- package/app/buyer-core/service.d.ts +57 -5
- package/app/buyer-core/service.js +275 -50
- package/app/buyer-core/service.js.map +1 -1
- package/app/buyer-core/store.d.ts +8 -0
- package/app/buyer-core/store.js +37 -7
- package/app/buyer-core/store.js.map +1 -1
- package/app/cli.js +13 -2
- package/app/cli.js.map +1 -1
- package/app/cloud/bootstrap.js +141 -7
- package/app/cloud/bootstrap.js.map +1 -1
- package/app/cloud-sample/products.json +3 -3
- package/app/commerce/receipt/dispatcher.d.ts +42 -0
- package/app/commerce/receipt/dispatcher.js +68 -0
- package/app/commerce/receipt/dispatcher.js.map +1 -0
- package/app/commerce/receipt/receipts.d.ts +140 -0
- package/app/commerce/receipt/receipts.js +247 -0
- package/app/commerce/receipt/receipts.js.map +1 -0
- package/app/config/profile.d.ts +2 -2
- package/app/handoff/order-receipts.d.ts +62 -0
- package/app/handoff/order-receipts.js +71 -0
- package/app/handoff/order-receipts.js.map +1 -0
- package/app/handoff/ucp-checkout/cart-channel.d.ts +2 -1
- package/app/handoff/ucp-checkout/cart-channel.js +1 -0
- package/app/handoff/ucp-checkout/cart-channel.js.map +1 -1
- package/app/handoff/ucp-checkout/channel.d.ts +6 -0
- package/app/handoff/ucp-checkout/channel.js +44 -0
- package/app/handoff/ucp-checkout/channel.js.map +1 -1
- package/app/http/cli.js +6 -0
- package/app/http/cli.js.map +1 -1
- package/app/http/merchant-management/api.d.ts +10 -0
- package/app/http/merchant-management/api.js +443 -0
- package/app/http/merchant-management/api.js.map +1 -1
- package/app/http/merchant-management/draft-store.js +8 -6
- package/app/http/merchant-management/draft-store.js.map +1 -1
- package/app/http/merchant-management/page.js +2 -2
- package/app/mcp/cli.js +6 -0
- package/app/mcp/cli.js.map +1 -1
- package/app/mcp/merchant-runtime-assembly.d.ts +20 -0
- package/app/mcp/merchant-runtime-assembly.js +93 -3
- package/app/mcp/merchant-runtime-assembly.js.map +1 -1
- package/app/mcp/merchant-server.d.ts +5 -0
- package/app/mcp/merchant-server.js +12 -0
- package/app/mcp/merchant-server.js.map +1 -1
- package/app/mcp/tools.js +7 -1
- package/app/mcp/tools.js.map +1 -1
- package/app/merchant/application/actor.d.ts +1 -1
- package/app/merchant/application/actor.js +15 -0
- package/app/merchant/application/actor.js.map +1 -1
- package/app/merchant/workbench-server.d.ts +1 -0
- package/app/merchant/workbench-server.js +176 -2
- package/app/merchant/workbench-server.js.map +1 -1
- package/app/merchant-core/budget/background-budget.d.ts +132 -0
- package/app/merchant-core/budget/background-budget.js +308 -0
- package/app/merchant-core/budget/background-budget.js.map +1 -0
- package/app/merchant-core/context-governance/actor-context.d.ts +56 -0
- package/app/merchant-core/context-governance/actor-context.js +76 -0
- package/app/merchant-core/context-governance/actor-context.js.map +1 -0
- package/app/merchant-core/context-governance/authority.d.ts +39 -0
- package/app/merchant-core/context-governance/authority.js +28 -0
- package/app/merchant-core/context-governance/authority.js.map +1 -0
- package/app/merchant-core/context-governance/context-guard.d.ts +104 -0
- package/app/merchant-core/context-governance/context-guard.js +194 -0
- package/app/merchant-core/context-governance/context-guard.js.map +1 -0
- package/app/merchant-core/context-governance/data-use-grant.d.ts +91 -0
- package/app/merchant-core/context-governance/data-use-grant.js +211 -0
- package/app/merchant-core/context-governance/data-use-grant.js.map +1 -0
- package/app/merchant-core/context-governance/feature-flags.d.ts +32 -0
- package/app/merchant-core/context-governance/feature-flags.js +83 -0
- package/app/merchant-core/context-governance/feature-flags.js.map +1 -0
- package/app/merchant-core/context-governance/pilot-readiness.d.ts +39 -0
- package/app/merchant-core/context-governance/pilot-readiness.js +141 -0
- package/app/merchant-core/context-governance/pilot-readiness.js.map +1 -0
- package/app/merchant-core/context-governance/source-governance.d.ts +157 -0
- package/app/merchant-core/context-governance/source-governance.js +492 -0
- package/app/merchant-core/context-governance/source-governance.js.map +1 -0
- package/app/merchant-core/context-governance/types.d.ts +62 -0
- package/app/merchant-core/context-governance/types.js +92 -0
- package/app/merchant-core/context-governance/types.js.map +1 -0
- package/app/merchant-core/customer-delivery/binding.d.ts +55 -0
- package/app/merchant-core/customer-delivery/binding.js +121 -0
- package/app/merchant-core/customer-delivery/binding.js.map +1 -0
- package/app/merchant-core/customer-delivery/channel.d.ts +75 -0
- package/app/merchant-core/customer-delivery/channel.js +157 -0
- package/app/merchant-core/customer-delivery/channel.js.map +1 -0
- package/app/merchant-core/customer-delivery/consent-repository.d.ts +50 -0
- package/app/merchant-core/customer-delivery/consent-repository.js +160 -0
- package/app/merchant-core/customer-delivery/consent-repository.js.map +1 -0
- package/app/merchant-core/customer-delivery/dispatcher-loop.d.ts +68 -0
- package/app/merchant-core/customer-delivery/dispatcher-loop.js +132 -0
- package/app/merchant-core/customer-delivery/dispatcher-loop.js.map +1 -0
- package/app/merchant-core/customer-delivery/dispatcher.d.ts +118 -0
- package/app/merchant-core/customer-delivery/dispatcher.js +166 -0
- package/app/merchant-core/customer-delivery/dispatcher.js.map +1 -0
- package/app/merchant-core/customer-delivery/email-adapter.d.ts +31 -0
- package/app/merchant-core/customer-delivery/email-adapter.js +90 -0
- package/app/merchant-core/customer-delivery/email-adapter.js.map +1 -0
- package/app/merchant-core/customer-delivery/gate.d.ts +37 -0
- package/app/merchant-core/customer-delivery/gate.js +196 -0
- package/app/merchant-core/customer-delivery/gate.js.map +1 -0
- package/app/merchant-core/customer-delivery/reconciliation.d.ts +13 -0
- package/app/merchant-core/customer-delivery/reconciliation.js +70 -0
- package/app/merchant-core/customer-delivery/reconciliation.js.map +1 -0
- package/app/merchant-core/customer-delivery/repository.d.ts +55 -0
- package/app/merchant-core/customer-delivery/repository.js +235 -0
- package/app/merchant-core/customer-delivery/repository.js.map +1 -0
- package/app/merchant-core/customer-delivery/smtp-adapter.d.ts +30 -0
- package/app/merchant-core/customer-delivery/smtp-adapter.js +183 -0
- package/app/merchant-core/customer-delivery/smtp-adapter.js.map +1 -0
- package/app/merchant-core/customer-delivery/stream-gate.d.ts +45 -0
- package/app/merchant-core/customer-delivery/stream-gate.js +123 -0
- package/app/merchant-core/customer-delivery/stream-gate.js.map +1 -0
- package/app/merchant-core/customer-delivery/types.d.ts +103 -0
- package/app/merchant-core/customer-delivery/types.js +32 -0
- package/app/merchant-core/customer-delivery/types.js.map +1 -0
- package/app/merchant-core/customer-delivery/weixin-adapter.d.ts +35 -0
- package/app/merchant-core/customer-delivery/weixin-adapter.js +56 -0
- package/app/merchant-core/customer-delivery/weixin-adapter.js.map +1 -0
- package/app/merchant-core/customer-memory/auto-identity.d.ts +43 -0
- package/app/merchant-core/customer-memory/auto-identity.js +163 -0
- package/app/merchant-core/customer-memory/auto-identity.js.map +1 -0
- package/app/merchant-core/customer-memory/grants.d.ts +72 -0
- package/app/merchant-core/customer-memory/grants.js +224 -0
- package/app/merchant-core/customer-memory/grants.js.map +1 -0
- package/app/merchant-core/customer-memory/history.d.ts +51 -0
- package/app/merchant-core/customer-memory/history.js +252 -0
- package/app/merchant-core/customer-memory/history.js.map +1 -0
- package/app/merchant-core/customer-memory/identity.d.ts +66 -0
- package/app/merchant-core/customer-memory/identity.js +249 -0
- package/app/merchant-core/customer-memory/identity.js.map +1 -0
- package/app/merchant-core/customer-memory/maintenance.d.ts +51 -0
- package/app/merchant-core/customer-memory/maintenance.js +67 -0
- package/app/merchant-core/customer-memory/maintenance.js.map +1 -0
- package/app/merchant-core/customer-memory/memory.d.ts +119 -0
- package/app/merchant-core/customer-memory/memory.js +502 -0
- package/app/merchant-core/customer-memory/memory.js.map +1 -0
- package/app/merchant-core/customer-memory/private-transport.d.ts +10 -0
- package/app/merchant-core/customer-memory/private-transport.js +108 -0
- package/app/merchant-core/customer-memory/private-transport.js.map +1 -0
- package/app/merchant-core/customer-memory/quote-history.d.ts +160 -0
- package/app/merchant-core/customer-memory/quote-history.js +392 -0
- package/app/merchant-core/customer-memory/quote-history.js.map +1 -0
- package/app/merchant-core/customer-memory/reception-provider.d.ts +69 -0
- package/app/merchant-core/customer-memory/reception-provider.js +250 -0
- package/app/merchant-core/customer-memory/reception-provider.js.map +1 -0
- package/app/merchant-core/customer-memory/reception-writer.d.ts +19 -0
- package/app/merchant-core/customer-memory/reception-writer.js +32 -0
- package/app/merchant-core/customer-memory/reception-writer.js.map +1 -0
- package/app/merchant-core/customer-memory/types.d.ts +111 -0
- package/app/merchant-core/customer-memory/types.js +58 -0
- package/app/merchant-core/customer-memory/types.js.map +1 -0
- package/app/merchant-core/followup/repository.d.ts +2 -1
- package/app/merchant-core/followup/repository.js +29 -23
- package/app/merchant-core/followup/repository.js.map +1 -1
- package/app/merchant-core/learning/evaluation-fixtures.d.ts +38 -0
- package/app/merchant-core/learning/evaluation-fixtures.js +83 -0
- package/app/merchant-core/learning/evaluation-fixtures.js.map +1 -0
- package/app/merchant-core/learning/evaluation-suite-v1.d.ts +56 -0
- package/app/merchant-core/learning/evaluation-suite-v1.js +129 -0
- package/app/merchant-core/learning/evaluation-suite-v1.js.map +1 -0
- package/app/merchant-core/learning/evaluation.d.ts +84 -0
- package/app/merchant-core/learning/evaluation.js +581 -0
- package/app/merchant-core/learning/evaluation.js.map +1 -0
- package/app/merchant-core/learning/initial-skills.d.ts +36 -0
- package/app/merchant-core/learning/initial-skills.js +67 -0
- package/app/merchant-core/learning/initial-skills.js.map +1 -0
- package/app/merchant-core/learning/precheck.d.ts +33 -0
- package/app/merchant-core/learning/precheck.js +56 -0
- package/app/merchant-core/learning/precheck.js.map +1 -0
- package/app/merchant-core/learning/real-evaluation-runner.d.ts +52 -0
- package/app/merchant-core/learning/real-evaluation-runner.js +167 -0
- package/app/merchant-core/learning/real-evaluation-runner.js.map +1 -0
- package/app/merchant-core/learning/reception-skill-catalog.d.ts +39 -0
- package/app/merchant-core/learning/reception-skill-catalog.js +145 -0
- package/app/merchant-core/learning/reception-skill-catalog.js.map +1 -0
- package/app/merchant-core/learning/recovery-gateway.d.ts +27 -0
- package/app/merchant-core/learning/recovery-gateway.js +106 -0
- package/app/merchant-core/learning/recovery-gateway.js.map +1 -0
- package/app/merchant-core/learning/repository.d.ts +130 -0
- package/app/merchant-core/learning/repository.js +588 -0
- package/app/merchant-core/learning/repository.js.map +1 -0
- package/app/merchant-core/learning/service.d.ts +262 -0
- package/app/merchant-core/learning/service.js +1498 -0
- package/app/merchant-core/learning/service.js.map +1 -0
- package/app/merchant-core/learning/test-dispatch-gateway.d.ts +43 -0
- package/app/merchant-core/learning/test-dispatch-gateway.js +145 -0
- package/app/merchant-core/learning/test-dispatch-gateway.js.map +1 -0
- package/app/merchant-core/learning/types.d.ts +217 -0
- package/app/merchant-core/learning/types.js +52 -0
- package/app/merchant-core/learning/types.js.map +1 -0
- package/app/merchant-core/migrations/ledgers.d.ts +40 -0
- package/app/merchant-core/migrations/ledgers.js +824 -0
- package/app/merchant-core/migrations/ledgers.js.map +1 -0
- package/app/merchant-core/migrations/runner.d.ts +55 -0
- package/app/merchant-core/migrations/runner.js +114 -0
- package/app/merchant-core/migrations/runner.js.map +1 -0
- package/app/merchant-core/operations-jobs/advice-runner.d.ts +63 -0
- package/app/merchant-core/operations-jobs/advice-runner.js +199 -0
- package/app/merchant-core/operations-jobs/advice-runner.js.map +1 -0
- package/app/merchant-core/operations-jobs/pilot-evidence.d.ts +64 -0
- package/app/merchant-core/operations-jobs/pilot-evidence.js +109 -0
- package/app/merchant-core/operations-jobs/pilot-evidence.js.map +1 -0
- package/app/merchant-core/operations-jobs/pilot-metrics.d.ts +70 -0
- package/app/merchant-core/operations-jobs/pilot-metrics.js +98 -0
- package/app/merchant-core/operations-jobs/pilot-metrics.js.map +1 -0
- package/app/merchant-core/operations-jobs/projection.d.ts +41 -0
- package/app/merchant-core/operations-jobs/projection.js +158 -0
- package/app/merchant-core/operations-jobs/projection.js.map +1 -0
- package/app/merchant-core/operations-jobs/scheduler.d.ts +68 -0
- package/app/merchant-core/operations-jobs/scheduler.js +233 -0
- package/app/merchant-core/operations-jobs/scheduler.js.map +1 -0
- package/app/merchant-core/operations-jobs/service.d.ts +260 -0
- package/app/merchant-core/operations-jobs/service.js +922 -0
- package/app/merchant-core/operations-jobs/service.js.map +1 -0
- package/app/merchant-core/ops/facade.d.ts +10 -0
- package/app/merchant-core/ops/facade.js +52 -0
- package/app/merchant-core/ops/facade.js.map +1 -1
- package/app/merchant-core/ops/schema.d.ts +1 -1
- package/app/merchant-core/ops/schema.js +119 -2
- package/app/merchant-core/ops/schema.js.map +1 -1
- package/app/merchant-core/ops/surface.d.ts +37 -0
- package/app/merchant-core/ops/surface.js +240 -0
- package/app/merchant-core/ops/surface.js.map +1 -1
- package/app/merchant-core/ownership/instance-ownership.d.ts +122 -0
- package/app/merchant-core/ownership/instance-ownership.js +236 -0
- package/app/merchant-core/ownership/instance-ownership.js.map +1 -0
- package/app/merchant-core/ownership/runtime.d.ts +175 -0
- package/app/merchant-core/ownership/runtime.js +422 -0
- package/app/merchant-core/ownership/runtime.js.map +1 -0
- package/app/merchant-core/storage/transaction.d.ts +102 -7
- package/app/merchant-core/storage/transaction.js +274 -3
- package/app/merchant-core/storage/transaction.js.map +1 -1
- package/app/merchant-credential-rotation.d.ts +4 -1
- package/app/merchant-credential-rotation.js +12 -8
- package/app/merchant-credential-rotation.js.map +1 -1
- package/app/merchant-runtime/manager.d.ts +2 -0
- package/app/merchant-runtime/manager.js +5 -0
- package/app/merchant-runtime/manager.js.map +1 -1
- package/build-manifest.json +1202 -242
- package/deployment-lock.json +2 -2
- package/package.json +1 -1
- package/prepare.mjs +5 -1
- package/runtime-package.json +1 -1
- package/shipping-sbom.cdx.json +6 -6
- package/shipping-source.json +12 -12
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* 基于 state.sqlite 的接待客户记忆工具提供者(M3-6)。
|
|
18
|
+
*
|
|
19
|
+
* 懒打开数据库;无绑定、无有效 grant 或 scope 已封存时返回 undefined,接待
|
|
20
|
+
* 只使用当前会话。工具内部用服务主体执行,模型不能提交 subject/merchant。
|
|
21
|
+
*/
|
|
22
|
+
import { existsSync } from "node:fs";
|
|
23
|
+
import { DatabaseSync } from "node:sqlite";
|
|
24
|
+
import { createActorContext } from "../context-governance/actor-context.js";
|
|
25
|
+
import { DataUseGrantStore } from "../context-governance/data-use-grant.js";
|
|
26
|
+
import { SourceGovernanceService } from "../context-governance/source-governance.js";
|
|
27
|
+
import { CustomerIdentityService } from "./identity.js";
|
|
28
|
+
import { CustomerMemoryService } from "./memory.js";
|
|
29
|
+
import { HistorySearchService } from "./history.js";
|
|
30
|
+
import { requestMemoryProposal } from "./private-transport.js";
|
|
31
|
+
export class SqliteCustomerMemoryProvider {
|
|
32
|
+
dbPath;
|
|
33
|
+
now;
|
|
34
|
+
/** 接待进程是否允许写候选记忆;单写者部署下 a2a 只读。 */
|
|
35
|
+
allowWrites;
|
|
36
|
+
socketPath;
|
|
37
|
+
handle;
|
|
38
|
+
constructor(options) {
|
|
39
|
+
this.dbPath = options.dbPath;
|
|
40
|
+
this.now = options.now ?? (() => new Date().toISOString());
|
|
41
|
+
// P2-9:默认只读;直写路径必须由宿主显式开启(生产始终 false)。
|
|
42
|
+
this.allowWrites = options.allowWrites ?? false;
|
|
43
|
+
this.socketPath = options.socketPath;
|
|
44
|
+
}
|
|
45
|
+
db() {
|
|
46
|
+
if (this.handle !== undefined)
|
|
47
|
+
return this.handle;
|
|
48
|
+
if (!existsSync(this.dbPath))
|
|
49
|
+
return undefined;
|
|
50
|
+
try {
|
|
51
|
+
this.handle = new DatabaseSync(this.dbPath);
|
|
52
|
+
return this.handle;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
systemActor(merchantId) {
|
|
59
|
+
return createActorContext({
|
|
60
|
+
merchant_id: merchantId,
|
|
61
|
+
actor_id: "system:reception-memory",
|
|
62
|
+
auth_strength: "verified",
|
|
63
|
+
subject_binding: "organization",
|
|
64
|
+
capabilities: ["memory:read", "memory:write", "customer:self"],
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
services(db) {
|
|
68
|
+
const source = new SourceGovernanceService({ db, now: this.now });
|
|
69
|
+
return {
|
|
70
|
+
identity: new CustomerIdentityService({ db, now: this.now }),
|
|
71
|
+
grants: new DataUseGrantStore({ db, now: this.now }),
|
|
72
|
+
memory: new CustomerMemoryService({ db, now: this.now, sourceGovernance: source, identity: new CustomerIdentityService({ db, now: this.now }) }),
|
|
73
|
+
history: new HistorySearchService({ db, now: this.now }),
|
|
74
|
+
source,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
contextFor(merchantId, actorId) {
|
|
78
|
+
const db = this.db();
|
|
79
|
+
if (db === undefined)
|
|
80
|
+
return undefined;
|
|
81
|
+
try {
|
|
82
|
+
const { identity, source } = this.services(db);
|
|
83
|
+
const binding = identity.currentSubjectForActor(merchantId, actorId);
|
|
84
|
+
if (binding === undefined)
|
|
85
|
+
return undefined;
|
|
86
|
+
const stamp = this.now();
|
|
87
|
+
const grants = db
|
|
88
|
+
.prepare(`SELECT grant_id, grant_epoch, purpose, resource_scope_id
|
|
89
|
+
FROM data_grants
|
|
90
|
+
WHERE merchant_id = ? AND subject_id = ?
|
|
91
|
+
AND status = 'ACTIVE' AND expires_at > ?`)
|
|
92
|
+
.all(merchantId, binding.subject_id, stamp);
|
|
93
|
+
if (grants.length === 0)
|
|
94
|
+
return undefined;
|
|
95
|
+
const scope = grants.find((grant) => grant.purpose === "history_recall")?.resource_scope_id ??
|
|
96
|
+
grants.find((grant) => grant.purpose === "customer_memory")?.resource_scope_id;
|
|
97
|
+
if (scope === undefined)
|
|
98
|
+
return undefined;
|
|
99
|
+
const memoryScope = grants.find((grant) => grant.purpose === "customer_memory")?.resource_scope_id;
|
|
100
|
+
const memoryDataScope = memoryScope === undefined ? undefined : source.getDataScope(merchantId, memoryScope);
|
|
101
|
+
const dataScope = source.getDataScope(merchantId, scope);
|
|
102
|
+
if (dataScope === undefined || dataScope.state !== "ACTIVE")
|
|
103
|
+
return undefined;
|
|
104
|
+
const grantVersions = {};
|
|
105
|
+
for (const grant of grants)
|
|
106
|
+
grantVersions[grant.grant_id] = grant.grant_epoch;
|
|
107
|
+
const setVersion = this.services(db).memory.currentMemorySetVersion(merchantId, binding.subject_id) ?? 0;
|
|
108
|
+
return {
|
|
109
|
+
merchant_id: merchantId,
|
|
110
|
+
actor_id: actorId,
|
|
111
|
+
subject_id: binding.subject_id,
|
|
112
|
+
binding_epoch: binding.binding_epoch,
|
|
113
|
+
resource_scope_id: scope,
|
|
114
|
+
...(memoryScope !== undefined && memoryDataScope?.state === "ACTIVE" ? { memory_scope_id: memoryScope } : {}),
|
|
115
|
+
grant_versions: grantVersions,
|
|
116
|
+
memory_set_version: setVersion,
|
|
117
|
+
data_scope_epochs: { [scope]: dataScope.data_epoch, ...(memoryScope !== undefined && memoryDataScope?.state === "ACTIVE" ? { [memoryScope]: memoryDataScope.data_epoch } : {}) },
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
currentBindingEpoch(merchantId, actorId) {
|
|
125
|
+
const db = this.db();
|
|
126
|
+
if (db === undefined)
|
|
127
|
+
return undefined;
|
|
128
|
+
try {
|
|
129
|
+
return new CustomerIdentityService({ db, now: this.now }).currentBindingEpoch(merchantId, actorId);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
currentGrantEpoch(merchantId, grantId) {
|
|
136
|
+
const db = this.db();
|
|
137
|
+
if (db === undefined)
|
|
138
|
+
return undefined;
|
|
139
|
+
try {
|
|
140
|
+
return new DataUseGrantStore({ db, now: this.now }).currentGrantEpoch(merchantId, grantId);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
currentDataScopeEpoch(merchantId, scopeKey) {
|
|
147
|
+
const db = this.db();
|
|
148
|
+
if (db === undefined)
|
|
149
|
+
return undefined;
|
|
150
|
+
try {
|
|
151
|
+
return new SourceGovernanceService({ db, now: this.now }).currentDataScopeEpoch(merchantId, scopeKey);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
currentMemorySetVersion(merchantId, subjectId) {
|
|
158
|
+
const db = this.db();
|
|
159
|
+
if (db === undefined)
|
|
160
|
+
return undefined;
|
|
161
|
+
try {
|
|
162
|
+
return new CustomerMemoryService({ db, now: this.now }).currentMemorySetVersion(merchantId, subjectId);
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
recall(context) {
|
|
169
|
+
const db = this.db();
|
|
170
|
+
if (db === undefined)
|
|
171
|
+
return [];
|
|
172
|
+
try {
|
|
173
|
+
return this.services(db)
|
|
174
|
+
.memory.listActiveForRecall(context.merchant_id, context.subject_id)
|
|
175
|
+
.slice(0, 5)
|
|
176
|
+
.map((memory) => ({
|
|
177
|
+
memory_id: memory.memory_id,
|
|
178
|
+
namespace: memory.namespace,
|
|
179
|
+
key: memory.key,
|
|
180
|
+
value_ref: memory.value_ref,
|
|
181
|
+
value_type: memory.value_type,
|
|
182
|
+
version: memory.version,
|
|
183
|
+
confirmed_at: memory.confirmed_at,
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
search(context, input) {
|
|
191
|
+
const db = this.db();
|
|
192
|
+
if (db === undefined)
|
|
193
|
+
return { status: "unavailable", reason: "history_store_unavailable" };
|
|
194
|
+
try {
|
|
195
|
+
return this.services(db).history.search({
|
|
196
|
+
merchant_id: context.merchant_id,
|
|
197
|
+
actor: this.systemActor(context.merchant_id),
|
|
198
|
+
subject_id: context.subject_id,
|
|
199
|
+
resource_scope_id: context.resource_scope_id,
|
|
200
|
+
query: input.query,
|
|
201
|
+
...(input.cursor !== undefined ? { cursor: input.cursor } : {}),
|
|
202
|
+
...(input.limit !== undefined ? { limit: input.limit } : {}),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return { status: "unavailable", reason: "history_store_error" };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
canPropose(context) {
|
|
210
|
+
return context.memory_scope_id !== undefined && (this.allowWrites || this.socketPath !== undefined);
|
|
211
|
+
}
|
|
212
|
+
propose(context, input) {
|
|
213
|
+
if (!this.allowWrites && this.socketPath !== undefined)
|
|
214
|
+
return requestMemoryProposal(this.socketPath, context, input);
|
|
215
|
+
if (!this.allowWrites) {
|
|
216
|
+
throw new Error("customer memory writes are handled by the standalone workbench");
|
|
217
|
+
}
|
|
218
|
+
const db = this.db();
|
|
219
|
+
if (db === undefined) {
|
|
220
|
+
throw new Error("customer memory store unavailable");
|
|
221
|
+
}
|
|
222
|
+
const memory = this.services(db).memory.proposeMemory({
|
|
223
|
+
merchant_id: context.merchant_id,
|
|
224
|
+
actor: this.systemActor(context.merchant_id),
|
|
225
|
+
subject_id: context.subject_id,
|
|
226
|
+
// P2-8:与 canPropose 的判定一致——写记忆落在 memory scope,而不是
|
|
227
|
+
// history scope(否则 fail-closed 或归档到错误域)。
|
|
228
|
+
resource_scope_id: context.memory_scope_id ?? context.resource_scope_id,
|
|
229
|
+
namespace: input.namespace,
|
|
230
|
+
key: input.key,
|
|
231
|
+
value_ref: input.value_ref,
|
|
232
|
+
value_type: input.value_type,
|
|
233
|
+
source_kind: input.source_kind,
|
|
234
|
+
...(input.confidence !== undefined ? { confidence: input.confidence } : {}),
|
|
235
|
+
});
|
|
236
|
+
return { memory_id: memory.memory_id, state: memory.state };
|
|
237
|
+
}
|
|
238
|
+
close() {
|
|
239
|
+
if (this.handle !== undefined) {
|
|
240
|
+
try {
|
|
241
|
+
this.handle.close();
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
// 关闭失败不影响退出。
|
|
245
|
+
}
|
|
246
|
+
this.handle = undefined;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=reception-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reception-provider.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-memory/reception-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,wCAAwC,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAC5E,OAAO,EAAE,uBAAuB,EAAE,MAAM,4CAA4C,CAAC;AAUrF,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAE/D,MAAM,OAAO,4BAA4B;IACtB,MAAM,CAAS;IACf,GAAG,CAAe;IACnC,mCAAmC;IAClB,WAAW,CAAU;IACrB,UAAU,CAAqB;IACxC,MAAM,CAA2B;IAEzC,YAAY,OAA2F;QACrG,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAA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AAK;QACH,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAC9B,IAAI,CAAC;gBACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;YACtB,CAAC;YAAC,MAAM,CAAC;gBACP,aAAa;YACf,CAAC;YACD,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QAC1B,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
/** Workbench 在所有权事务内核验接待快照并写入候选。 */
|
|
8
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
9
|
+
import type { CustomerMemoryToolProvider } from "../../a2a/reception/customer-memory-tools.js";
|
|
10
|
+
export type ReceptionMemoryWriter = (context: Parameters<CustomerMemoryToolProvider["propose"]>[0], input: Parameters<CustomerMemoryToolProvider["propose"]>[1]) => {
|
|
11
|
+
memory_id: string;
|
|
12
|
+
state: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function createReceptionMemoryWriter(options: {
|
|
15
|
+
db: DatabaseSync;
|
|
16
|
+
merchantId: string;
|
|
17
|
+
now: () => string;
|
|
18
|
+
write: <T>(work: () => T) => T;
|
|
19
|
+
}): ReceptionMemoryWriter;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createActorContext } from "../context-governance/actor-context.js";
|
|
2
|
+
import { DataUseGrantStore } from "../context-governance/data-use-grant.js";
|
|
3
|
+
import { SourceGovernanceService } from "../context-governance/source-governance.js";
|
|
4
|
+
import { CustomerIdentityService } from "./identity.js";
|
|
5
|
+
import { CustomerMemoryService } from "./memory.js";
|
|
6
|
+
export function createReceptionMemoryWriter(options) {
|
|
7
|
+
const { db, merchantId, now, write } = options;
|
|
8
|
+
return (context, input) => write(() => {
|
|
9
|
+
if (!context || context.merchant_id !== merchantId || typeof context.actor_id !== "string" || !context.actor_id || typeof context.memory_scope_id !== "string")
|
|
10
|
+
throw new Error("invalid context");
|
|
11
|
+
const memoryScope = context.memory_scope_id;
|
|
12
|
+
const identity = new CustomerIdentityService({ db, now });
|
|
13
|
+
const binding = identity.currentSubjectForActor(merchantId, context.actor_id);
|
|
14
|
+
if (!binding || binding.subject_id !== context.subject_id || binding.binding_epoch !== context.binding_epoch)
|
|
15
|
+
throw new Error("binding changed");
|
|
16
|
+
const source = new SourceGovernanceService({ db, now });
|
|
17
|
+
const scope = source.getDataScope(merchantId, memoryScope);
|
|
18
|
+
if (!scope || scope.state !== "ACTIVE" || scope.data_epoch !== context.data_scope_epochs?.[memoryScope])
|
|
19
|
+
throw new Error("scope changed");
|
|
20
|
+
const grants = new DataUseGrantStore({ db, now });
|
|
21
|
+
const grant = grants.getActiveGrant({ merchant_id: merchantId, subject_id: context.subject_id, purpose: "customer_memory", resource_scope_id: memoryScope });
|
|
22
|
+
if (!grant || context.grant_versions?.[grant.grant_id] !== grant.grant_epoch)
|
|
23
|
+
throw new Error("grant changed");
|
|
24
|
+
const memory = new CustomerMemoryService({ db, now, sourceGovernance: source, identity });
|
|
25
|
+
if ((memory.currentMemorySetVersion(merchantId, context.subject_id) ?? 0) !== context.memory_set_version)
|
|
26
|
+
throw new Error("memory changed");
|
|
27
|
+
const actor = createActorContext({ merchant_id: merchantId, actor_id: "system:reception-memory", auth_strength: "verified", subject_binding: "organization", capabilities: ["memory:write"] });
|
|
28
|
+
const result = memory.proposeMemory({ merchant_id: merchantId, actor, subject_id: context.subject_id, resource_scope_id: memoryScope, namespace: input.namespace, key: input.key, value_ref: input.value_ref, value_type: input.value_type, source_kind: input.source_kind, ...(input.confidence !== undefined ? { confidence: input.confidence } : {}) });
|
|
29
|
+
return { memory_id: result.memory_id, state: result.state };
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=reception-writer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reception-writer.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-memory/reception-writer.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,kBAAkB,EAAE,MAAM,wCAAwC,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAC5E,OAAO,EAAE,uBAAuB,EAAE,MAAM,4CAA4C,CAAC;AACrF,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAIpD,MAAM,UAAU,2BAA2B,CAAC,OAE3C;IACC,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC;IAC/C,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE;QACpC,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,WAAW,KAAK,UAAU,IAAI,OAAO,OAAO,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,OAAO,CAAC,QAAQ,IAAI,OAAO,OAAO,CAAC,eAAe,KAAK,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;QACnM,MAAM,WAAW,GAAG,OAAO,CAAC,eAAe,CAAC;QAC5C,MAAM,QAAQ,GAAG,IAAI,uBAAuB,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QAC1D,MAAM,OAAO,GAAG,QAAQ,CAAC,sBAAsB,CAAC,UAAU,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC9E,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,UAAU,KAAK,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,aAAa,KAAK,OAAO,CAAC,aAAa;YAAE,MAAM,IAAI,KAAK,CAA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EAAE,cAAc,EAAE,YAAY,EAAE,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC;QAC/L,MAAM,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,EAAE,WAAW,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,iBAAiB,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,CAAC,UAAU,EAAE,WAAW,EAAE,KAAK,CAAC,WAAW,EAAE,GAAG,CAAC,KAAK,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QAC3V,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC;IAC9D,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* TOP2 客户记忆域类型(设计 v1.1 §9–§12 / M3)。
|
|
18
|
+
*
|
|
19
|
+
* P0 只启用 organization / agent_scope;person / verified_person 由服务层拒绝。
|
|
20
|
+
* 记忆、证据、事件与历史索引独立于 owner Principal Memory;所有时间 RFC3339
|
|
21
|
+
* UTC,所有读取带 merchant + subject 作用域。
|
|
22
|
+
*/
|
|
23
|
+
import type { ActorCapability } from "../context-governance/types.js";
|
|
24
|
+
export type CustomerMemoryErrorCode = "validation" | "access_denied" | "not_found" | "version_conflict" | "confirmation_invalid" | "confirmation_expired" | "confirmation_consumed" | "grant_unavailable" | "source_unavailable" | "schema_unavailable" | "unavailable";
|
|
25
|
+
export declare class CustomerMemoryError extends Error {
|
|
26
|
+
readonly code: CustomerMemoryErrorCode;
|
|
27
|
+
readonly reason: string;
|
|
28
|
+
constructor(code: CustomerMemoryErrorCode, reason: string, message: string);
|
|
29
|
+
}
|
|
30
|
+
export declare const CUSTOMER_SUBJECT_KINDS: readonly ["organization", "agent_scope", "person", "verified_person"];
|
|
31
|
+
export type CustomerSubjectKind = (typeof CUSTOMER_SUBJECT_KINDS)[number];
|
|
32
|
+
export declare const P0_SUBJECT_KINDS: ReadonlySet<CustomerSubjectKind>;
|
|
33
|
+
export declare const MEMORY_STATES: readonly ["candidate", "active", "needs_review", "superseded", "deleted", "expired"];
|
|
34
|
+
export type MemoryState = (typeof MEMORY_STATES)[number];
|
|
35
|
+
export declare const MEMORY_SOURCE_KINDS: readonly ["explicit", "observed", "inferred"];
|
|
36
|
+
export type MemorySourceKind = (typeof MEMORY_SOURCE_KINDS)[number];
|
|
37
|
+
export declare const MEMORY_NAMESPACES: readonly ["preference", "constraint", "requirement", "communication", "region"];
|
|
38
|
+
export type MemoryNamespace = (typeof MEMORY_NAMESPACES)[number];
|
|
39
|
+
export interface CustomerSubject {
|
|
40
|
+
readonly subject_id: string;
|
|
41
|
+
readonly merchant_id: string;
|
|
42
|
+
readonly subject_kind: CustomerSubjectKind;
|
|
43
|
+
readonly status: "ACTIVE" | "REVOKED";
|
|
44
|
+
readonly created_at: string;
|
|
45
|
+
readonly updated_at: string;
|
|
46
|
+
}
|
|
47
|
+
export interface SubjectBinding {
|
|
48
|
+
readonly binding_id: string;
|
|
49
|
+
readonly merchant_id: string;
|
|
50
|
+
readonly subject_id: string;
|
|
51
|
+
readonly actor_id: string;
|
|
52
|
+
readonly issuer: string;
|
|
53
|
+
readonly assurance_class: string;
|
|
54
|
+
readonly binding_epoch: number;
|
|
55
|
+
readonly status: "ACTIVE" | "REVOKED";
|
|
56
|
+
readonly expires_at: string;
|
|
57
|
+
readonly created_at: string;
|
|
58
|
+
readonly updated_at: string;
|
|
59
|
+
}
|
|
60
|
+
export interface CustomerMemory {
|
|
61
|
+
readonly memory_id: string;
|
|
62
|
+
readonly merchant_id: string;
|
|
63
|
+
readonly subject_id: string;
|
|
64
|
+
readonly resource_scope_id: string;
|
|
65
|
+
readonly namespace: MemoryNamespace;
|
|
66
|
+
readonly key: string;
|
|
67
|
+
readonly value_ref: string;
|
|
68
|
+
readonly value_type: string;
|
|
69
|
+
readonly state: MemoryState;
|
|
70
|
+
readonly source_kind: MemorySourceKind;
|
|
71
|
+
readonly source_refs: readonly string[];
|
|
72
|
+
readonly confidence: number | null;
|
|
73
|
+
readonly confirmed_by: string | null;
|
|
74
|
+
readonly confirmed_at: string | null;
|
|
75
|
+
readonly last_confirmed_at: string | null;
|
|
76
|
+
readonly valid_from: string;
|
|
77
|
+
readonly expires_at: string;
|
|
78
|
+
readonly policy_version: string;
|
|
79
|
+
readonly version: number;
|
|
80
|
+
readonly grant_id: string;
|
|
81
|
+
readonly grant_epoch: number;
|
|
82
|
+
readonly data_epoch: number;
|
|
83
|
+
readonly erase_scope_id: string;
|
|
84
|
+
readonly purge_at: string;
|
|
85
|
+
readonly superseded_by: string | null;
|
|
86
|
+
readonly created_at: string;
|
|
87
|
+
readonly updated_at: string;
|
|
88
|
+
}
|
|
89
|
+
export interface HistoryHit {
|
|
90
|
+
readonly document_ref: string;
|
|
91
|
+
readonly source_version: number;
|
|
92
|
+
readonly historical: true;
|
|
93
|
+
readonly is_current: false;
|
|
94
|
+
readonly kind: string;
|
|
95
|
+
readonly sku?: string;
|
|
96
|
+
readonly occurred_at: string;
|
|
97
|
+
readonly snippet: string;
|
|
98
|
+
}
|
|
99
|
+
export type HistorySearchResult = {
|
|
100
|
+
readonly status: "ok";
|
|
101
|
+
readonly hits: readonly HistoryHit[];
|
|
102
|
+
readonly next_cursor?: string;
|
|
103
|
+
} | {
|
|
104
|
+
readonly status: "no_match";
|
|
105
|
+
readonly hits: readonly [];
|
|
106
|
+
} | {
|
|
107
|
+
readonly status: "unavailable";
|
|
108
|
+
readonly reason: string;
|
|
109
|
+
};
|
|
110
|
+
/** 能力检查:customer:self 走客户自助;memory:write/read 走管理/worker。 */
|
|
111
|
+
export declare function requireCapability(actorCapabilities: readonly string[], capability: ActorCapability, reason?: string): void;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export class CustomerMemoryError extends Error {
|
|
17
|
+
code;
|
|
18
|
+
reason;
|
|
19
|
+
constructor(code, reason, message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "CustomerMemoryError";
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export const CUSTOMER_SUBJECT_KINDS = [
|
|
27
|
+
"organization",
|
|
28
|
+
"agent_scope",
|
|
29
|
+
"person",
|
|
30
|
+
"verified_person",
|
|
31
|
+
];
|
|
32
|
+
export const P0_SUBJECT_KINDS = new Set([
|
|
33
|
+
"organization",
|
|
34
|
+
"agent_scope",
|
|
35
|
+
]);
|
|
36
|
+
export const MEMORY_STATES = [
|
|
37
|
+
"candidate",
|
|
38
|
+
"active",
|
|
39
|
+
"needs_review",
|
|
40
|
+
"superseded",
|
|
41
|
+
"deleted",
|
|
42
|
+
"expired",
|
|
43
|
+
];
|
|
44
|
+
export const MEMORY_SOURCE_KINDS = ["explicit", "observed", "inferred"];
|
|
45
|
+
export const MEMORY_NAMESPACES = [
|
|
46
|
+
"preference",
|
|
47
|
+
"constraint",
|
|
48
|
+
"requirement",
|
|
49
|
+
"communication",
|
|
50
|
+
"region",
|
|
51
|
+
];
|
|
52
|
+
/** 能力检查:customer:self 走客户自助;memory:write/read 走管理/worker。 */
|
|
53
|
+
export function requireCapability(actorCapabilities, capability, reason = "capability_missing") {
|
|
54
|
+
if (!actorCapabilities.includes(capability)) {
|
|
55
|
+
throw new CustomerMemoryError("access_denied", reason, `actor 缺少 ${capability}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-memory/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAyBH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACnC,IAAI,CAA0B;IAC9B,MAAM,CAAS;IAExB,YAAY,IAA6B,EAAE,MAAc,EAAE,OAAe;QACxE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,cAAc;IACd,aAAa;IACb,QAAQ;IACR,iBAAiB;CACT,CAAC;AAEX,MAAM,CAAC,MAAM,gBAAgB,GAAqC,IAAI,GAAG,CAAC;IACxE,cAAc;IACd,aAAa;CACd,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,WAAW;IACX,QAAQ;IACR,cAAc;IACd,YAAY;IACZ,SAAS;IACT,SAAS;CACD,CAAC;AAGX,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,CAAU,CAAC;AAGjF,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,YAAY;IACZ,YAAY;IACZ,aAAa;IACb,eAAe;IACf,QAAQ;CACA,CAAC;AA4EX,6DAA6D;AAC7D,MAAM,UAAU,iBAAiB,CAC/B,iBAAoC,EACpC,UAA2B,EAC3B,MAAM,GAAG,oBAAoB;IAE7B,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,mBAAmB,CAAC,eAAe,EAAE,MAAM,EAAE,YAAY,UAAU,EAAE,CAAC,CAAC;IACnF,CAAC;AACH,CAAC"}
|
|
@@ -43,9 +43,10 @@ export interface FollowUpRepositoryOptions {
|
|
|
43
43
|
export declare class FollowUpRepository {
|
|
44
44
|
private readonly db;
|
|
45
45
|
private readonly now;
|
|
46
|
-
private inTx;
|
|
47
46
|
constructor(options: FollowUpRepositoryOptions);
|
|
48
47
|
transaction<T>(work: () => T): T;
|
|
48
|
+
/** 不缓存 schema:同一连接在构造后可能升级。旧表上的回退谓词在执行时再次核对列。 */
|
|
49
|
+
private legacyTaskPredicate;
|
|
49
50
|
createTask(input: {
|
|
50
51
|
merchantId: string;
|
|
51
52
|
opportunityId?: string;
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
*/
|
|
23
23
|
import { randomUUID } from "node:crypto";
|
|
24
24
|
import { contentDigest } from "../../negotiation/jcs.js";
|
|
25
|
+
import { inImmediateTransaction } from "../storage/transaction.js";
|
|
25
26
|
import { FollowUpError } from "./types.js";
|
|
26
27
|
function rowToTask(row) {
|
|
27
28
|
return {
|
|
@@ -49,43 +50,48 @@ function rowToTask(row) {
|
|
|
49
50
|
export class FollowUpRepository {
|
|
50
51
|
db;
|
|
51
52
|
now;
|
|
52
|
-
inTx = false;
|
|
53
53
|
constructor(options) {
|
|
54
54
|
this.db = options.db;
|
|
55
55
|
this.now = options.now;
|
|
56
56
|
}
|
|
57
57
|
transaction(work) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.db
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.inTx = false;
|
|
58
|
+
// 委托受控事务上下文:顶层 BEGIN IMMEDIATE/COMMIT/ROLLBACK 语义不变;
|
|
59
|
+
// 嵌套(栅栏写入等外层事务内调用)走 SAVEPOINT——内层抛错只回滚自己,
|
|
60
|
+
// "cannot start a transaction within a transaction" 不再出现。
|
|
61
|
+
return inImmediateTransaction(this.db, work);
|
|
62
|
+
}
|
|
63
|
+
/** 不缓存 schema:同一连接在构造后可能升级。旧表上的回退谓词在执行时再次核对列。 */
|
|
64
|
+
legacyTaskPredicate() {
|
|
65
|
+
const columns = this.db.prepare("PRAGMA table_info(followup_tasks)").all();
|
|
66
|
+
if (columns.some((column) => column.name === "rule_id"))
|
|
67
|
+
return "rule_id IS NULL";
|
|
68
|
+
const version = this.db.prepare("SELECT MAX(version) AS version FROM merchant_ops_schema_migrations")
|
|
69
|
+
.get();
|
|
70
|
+
if (version.version === null || version.version >= 3) {
|
|
71
|
+
throw new FollowUpError("invalid_transition", "跟进任务表缺少规则隔离列");
|
|
73
72
|
}
|
|
73
|
+
return "NOT EXISTS (SELECT 1 FROM pragma_table_info('followup_tasks') WHERE name = 'rule_id')";
|
|
74
74
|
}
|
|
75
75
|
createTask(input) {
|
|
76
76
|
if (input.idempotencyKey !== undefined) {
|
|
77
77
|
const existing = this.findByKey(input.merchantId, input.idempotencyKey);
|
|
78
78
|
if (existing !== undefined) {
|
|
79
|
+
// X07:幂等摘要覆盖全部业务含义字段(目标、due、重试预算、载荷),
|
|
80
|
+
// 不能只比较触发/动作,否则改时间或重试参数会静默复用旧任务。
|
|
79
81
|
const expectedDigest = contentDigest({
|
|
80
82
|
trigger_event: input.triggerEvent,
|
|
81
83
|
action_type: input.actionType,
|
|
82
84
|
target_version: input.targetVersion ?? null,
|
|
85
|
+
due_at: input.dueAt ?? null,
|
|
86
|
+
max_attempts: input.maxAttempts,
|
|
83
87
|
payload: input.payload,
|
|
84
88
|
});
|
|
85
89
|
const storedDigest = contentDigest({
|
|
86
90
|
trigger_event: existing.trigger_event,
|
|
87
91
|
action_type: existing.action_type,
|
|
88
92
|
target_version: existing.target_version,
|
|
93
|
+
due_at: existing.due_at ?? null,
|
|
94
|
+
max_attempts: existing.max_attempts,
|
|
89
95
|
payload: existing.payload,
|
|
90
96
|
});
|
|
91
97
|
if (expectedDigest !== storedDigest) {
|
|
@@ -107,13 +113,13 @@ export class FollowUpRepository {
|
|
|
107
113
|
}
|
|
108
114
|
findByKey(merchantId, key) {
|
|
109
115
|
const row = this.db
|
|
110
|
-
.prepare(
|
|
116
|
+
.prepare(`SELECT * FROM followup_tasks WHERE merchant_id = ? AND idempotency_key = ? AND ${this.legacyTaskPredicate()}`)
|
|
111
117
|
.get(merchantId, key);
|
|
112
118
|
return row === undefined ? undefined : rowToTask(row);
|
|
113
119
|
}
|
|
114
120
|
getTask(merchantId, taskId) {
|
|
115
121
|
const row = this.db
|
|
116
|
-
.prepare(
|
|
122
|
+
.prepare(`SELECT * FROM followup_tasks WHERE merchant_id = ? AND task_id = ? AND ${this.legacyTaskPredicate()}`)
|
|
117
123
|
.get(merchantId, taskId);
|
|
118
124
|
return row === undefined ? undefined : rowToTask(row);
|
|
119
125
|
}
|
|
@@ -136,7 +142,7 @@ export class FollowUpRepository {
|
|
|
136
142
|
listDue(merchantId, at, limit = 50) {
|
|
137
143
|
const rows = this.db
|
|
138
144
|
.prepare(`SELECT * FROM followup_tasks
|
|
139
|
-
WHERE merchant_id = ? AND status = 'PENDING'
|
|
145
|
+
WHERE merchant_id = ? AND status = 'PENDING' AND ${this.legacyTaskPredicate()}
|
|
140
146
|
AND due_at IS NOT NULL AND due_at <= ?
|
|
141
147
|
ORDER BY due_at ASC LIMIT ?`)
|
|
142
148
|
.all(merchantId, at, limit);
|
|
@@ -152,7 +158,7 @@ export class FollowUpRepository {
|
|
|
152
158
|
.prepare(`UPDATE followup_tasks
|
|
153
159
|
SET status = 'RUNNING', lease_owner = ?, lease_expires_at = ?,
|
|
154
160
|
fencing_token = fencing_token + 1, attempts = attempts + 1, updated_at = ?
|
|
155
|
-
WHERE merchant_id = ? AND task_id = ? AND status = 'READY'
|
|
161
|
+
WHERE merchant_id = ? AND task_id = ? AND status = 'READY' AND ${this.legacyTaskPredicate()}
|
|
156
162
|
AND (lease_expires_at IS NULL OR lease_expires_at <= ?)`)
|
|
157
163
|
.run(input.workerId, expires, input.now, input.merchantId, input.taskId, input.now);
|
|
158
164
|
return Number(result.changes) === 1 ? this.getTask(input.merchantId, input.taskId) : undefined;
|
|
@@ -161,7 +167,7 @@ export class FollowUpRepository {
|
|
|
161
167
|
const result = this.db
|
|
162
168
|
.prepare(`UPDATE followup_tasks
|
|
163
169
|
SET external_call_started = 1, updated_at = ?
|
|
164
|
-
WHERE merchant_id = ? AND task_id = ? AND status = 'RUNNING' AND fencing_token =
|
|
170
|
+
WHERE merchant_id = ? AND task_id = ? AND status = 'RUNNING' AND fencing_token = ? AND ${this.legacyTaskPredicate()}`)
|
|
165
171
|
.run(this.now(), merchantId, taskId, fencingToken);
|
|
166
172
|
if (Number(result.changes) !== 1) {
|
|
167
173
|
throw new FollowUpError("invalid_transition", "旧 fencing token 不能标记外部调用");
|
|
@@ -169,7 +175,7 @@ export class FollowUpRepository {
|
|
|
169
175
|
}
|
|
170
176
|
/** 状态迁移 CAS;FROM RUNNING 时必须提供当前 fencing token。 */
|
|
171
177
|
applyTransition(input) {
|
|
172
|
-
const conditions = ["merchant_id = ?", "task_id = ?", "status = ?"];
|
|
178
|
+
const conditions = ["merchant_id = ?", "task_id = ?", "status = ?", this.legacyTaskPredicate()];
|
|
173
179
|
const params = [input.merchantId, input.taskId, input.from];
|
|
174
180
|
if (input.from === "RUNNING") {
|
|
175
181
|
if (input.fencingToken === undefined) {
|
|
@@ -244,7 +250,7 @@ export class FollowUpRepository {
|
|
|
244
250
|
recoverExpiredLeases(merchantId, at) {
|
|
245
251
|
const rows = this.db
|
|
246
252
|
.prepare(`SELECT * FROM followup_tasks
|
|
247
|
-
WHERE merchant_id = ? AND status = 'RUNNING'
|
|
253
|
+
WHERE merchant_id = ? AND status = 'RUNNING' AND ${this.legacyTaskPredicate()}
|
|
248
254
|
AND lease_expires_at IS NOT NULL AND lease_expires_at <= ?`)
|
|
249
255
|
.all(merchantId, at);
|
|
250
256
|
const recovered = [];
|