@harrylabsj/kiwi-merchant-cloud 0.12.30 → 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 +56 -1
- package/app/a2a/server/merchant-handler.js +659 -44
- 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/agent/buyer/buyer-tools.js +1 -2
- package/app/agent/buyer/buyer-tools.js.map +1 -1
- package/app/agent/buyer/search-loop.js +0 -1
- package/app/agent/buyer/search-loop.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/merchant-index.js +0 -1
- package/app/buyer-core/merchant-index.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 +58 -5
- package/app/buyer-core/service.js +279 -53
- 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 +136 -34
- package/app/cli.js.map +1 -1
- package/app/cloud/bootstrap.d.ts +1 -1
- package/app/cloud/bootstrap.js +162 -35
- package/app/cloud/bootstrap.js.map +1 -1
- package/app/cloud/product-import-tabular.js +13 -5
- package/app/cloud/product-import-tabular.js.map +1 -1
- package/app/cloud/trade-platforms-client.d.ts +30 -0
- package/app/cloud/trade-platforms-client.js +34 -1
- package/app/cloud/trade-platforms-client.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/trade-platform-rules.js +5 -2
- package/app/handoff/trade-platform-rules.js.map +1 -1
- 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 +11 -1
- package/app/http/merchant-management/api.js +471 -3
- 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 +83 -22
- package/app/http/merchant-management/page.js.map +1 -1
- 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 +12 -3
- 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 +38 -0
- package/app/merchant/workbench-server.js +428 -0
- package/app/merchant/workbench-server.js.map +1 -0
- 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-gateway/catalog-tools.js +4 -2
- package/app/merchant-gateway/catalog-tools.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/app/product-buyer.d.ts +1 -0
- package/app/product-buyer.js +0 -1
- package/app/product-buyer.js.map +1 -1
- package/app/product-cli.js +11 -1
- package/app/product-cli.js.map +1 -1
- package/build-manifest.json +1255 -280
- 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,308 @@
|
|
|
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
|
+
import { inImmediateTransaction } from "../storage/transaction.js";
|
|
17
|
+
/** D-M0-09 冻结参数。 */
|
|
18
|
+
export const BACKGROUND_BUDGET_LIMITS = Object.freeze({
|
|
19
|
+
daily_runs: 20,
|
|
20
|
+
daily_input_tokens: 100_000,
|
|
21
|
+
daily_output_tokens: 12_000,
|
|
22
|
+
run_input_reserve: 5_000,
|
|
23
|
+
run_output_reserve: 600,
|
|
24
|
+
run_input_hard_cap: 24_576,
|
|
25
|
+
run_output_hard_cap: 3_072,
|
|
26
|
+
call_input_cap: 8_192,
|
|
27
|
+
call_output_cap: 1_024,
|
|
28
|
+
max_concurrency: 1,
|
|
29
|
+
});
|
|
30
|
+
export class BackgroundBudgetError extends Error {
|
|
31
|
+
code;
|
|
32
|
+
/** 稳定原因码(如 daily_runs / daily_input_tokens),进日志与工作台。 */
|
|
33
|
+
reason;
|
|
34
|
+
constructor(code, reason, message) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "BackgroundBudgetError";
|
|
37
|
+
this.code = code;
|
|
38
|
+
this.reason = reason;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function dayToUsage(row) {
|
|
42
|
+
return {
|
|
43
|
+
merchant_id: row.merchant_id,
|
|
44
|
+
day_key: row.day_key,
|
|
45
|
+
runs_used: row.runs_used,
|
|
46
|
+
input_used: row.input_used,
|
|
47
|
+
output_used: row.output_used,
|
|
48
|
+
updated_at: row.updated_at,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function runToRecord(row) {
|
|
52
|
+
return {
|
|
53
|
+
run_id: row.run_id,
|
|
54
|
+
merchant_id: row.merchant_id,
|
|
55
|
+
day_key: row.day_key,
|
|
56
|
+
template: row.template,
|
|
57
|
+
input_reserved: row.input_reserved,
|
|
58
|
+
output_reserved: row.output_reserved,
|
|
59
|
+
input_actual: row.input_actual,
|
|
60
|
+
output_actual: row.output_actual,
|
|
61
|
+
usage_state: row.usage_state,
|
|
62
|
+
state: row.state,
|
|
63
|
+
created_at: row.created_at,
|
|
64
|
+
updated_at: row.updated_at,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function requireId(value, field) {
|
|
68
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 128) {
|
|
69
|
+
throw new BackgroundBudgetError("validation", `${field}_invalid`, `${field} 必须是 1..128 字符`);
|
|
70
|
+
}
|
|
71
|
+
// eslint-disable-next-line no-control-regex
|
|
72
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) {
|
|
73
|
+
throw new BackgroundBudgetError("validation", `${field}_invalid`, `${field} 含控制字符`);
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
function requireTokenCount(value, field) {
|
|
78
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
79
|
+
throw new BackgroundBudgetError("validation", `${field}_invalid`, `${field} 必须是非负整数`);
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
const DAY_KEY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
84
|
+
function requireDayKey(value) {
|
|
85
|
+
if (typeof value !== "string" || !DAY_KEY_PATTERN.test(value)) {
|
|
86
|
+
throw new BackgroundBudgetError("validation", "day_key_invalid", "day_key 必须是 YYYY-MM-DD");
|
|
87
|
+
}
|
|
88
|
+
const ms = Date.parse(`${value}T00:00:00.000Z`);
|
|
89
|
+
if (!Number.isFinite(ms) || new Date(ms).toISOString().slice(0, 10) !== value) {
|
|
90
|
+
throw new BackgroundBudgetError("validation", "day_key_invalid", "day_key 不是有效日期");
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 商家记账日(宿主时区):`Intl.DateTimeFormat` 的 en-CA 日历格式为
|
|
96
|
+
* YYYY-MM-DD。非法时区/时间即 validation 拒绝(启动配置错误,不静默回落)。
|
|
97
|
+
*/
|
|
98
|
+
export function backgroundBudgetDayKey(now, timeZone) {
|
|
99
|
+
const ms = Date.parse(now);
|
|
100
|
+
if (!Number.isFinite(ms)) {
|
|
101
|
+
throw new BackgroundBudgetError("validation", "now_invalid", "now 必须是可解析的 RFC3339 时间");
|
|
102
|
+
}
|
|
103
|
+
let formatter;
|
|
104
|
+
try {
|
|
105
|
+
formatter = new Intl.DateTimeFormat("en-CA", {
|
|
106
|
+
timeZone,
|
|
107
|
+
year: "numeric",
|
|
108
|
+
month: "2-digit",
|
|
109
|
+
day: "2-digit",
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
throw new BackgroundBudgetError("validation", "timezone_invalid", `无法识别的时区 ${timeZone}`);
|
|
114
|
+
}
|
|
115
|
+
const parts = formatter.formatToParts(new Date(ms));
|
|
116
|
+
const pick = (type) => parts.find((part) => part.type === type)?.value ?? "";
|
|
117
|
+
const dayKey = `${pick("year")}-${pick("month")}-${pick("day")}`;
|
|
118
|
+
if (!DAY_KEY_PATTERN.test(dayKey)) {
|
|
119
|
+
throw new BackgroundBudgetError("validation", "day_key_invalid", "时区换算未得到 YYYY-MM-DD");
|
|
120
|
+
}
|
|
121
|
+
return dayKey;
|
|
122
|
+
}
|
|
123
|
+
export class BackgroundBudgetStore {
|
|
124
|
+
db;
|
|
125
|
+
now;
|
|
126
|
+
constructor(options) {
|
|
127
|
+
this.db = options.db;
|
|
128
|
+
this.now = options.now;
|
|
129
|
+
}
|
|
130
|
+
/** 调用方须使用同一连接,才能将预算与任务状态置于同一事务。 */
|
|
131
|
+
usesDatabase(db) {
|
|
132
|
+
return this.db === db;
|
|
133
|
+
}
|
|
134
|
+
nowUtc() {
|
|
135
|
+
const ms = Date.parse(this.now());
|
|
136
|
+
if (!Number.isFinite(ms)) {
|
|
137
|
+
throw new BackgroundBudgetError("validation", "now_invalid", "时钟输出必须是可解析的 RFC3339 时间");
|
|
138
|
+
}
|
|
139
|
+
return new Date(ms).toISOString();
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 准入预留:原子检查并发、日 run 数、日 input/output token 并占额。
|
|
143
|
+
* 同 run_id 重复预留:内容一致幂等返回;不一致 replay_conflict。
|
|
144
|
+
*/
|
|
145
|
+
reserveRun(input) {
|
|
146
|
+
const merchantId = requireId(input.merchant_id, "merchant_id");
|
|
147
|
+
const runId = requireId(input.run_id, "run_id");
|
|
148
|
+
const dayKey = requireDayKey(input.day_key);
|
|
149
|
+
const template = input.template === undefined ? null : requireId(input.template, "template");
|
|
150
|
+
return inImmediateTransaction(this.db, () => {
|
|
151
|
+
const existing = this.readRunRow(runId);
|
|
152
|
+
if (existing !== undefined) {
|
|
153
|
+
if (existing.merchant_id !== merchantId || existing.day_key !== dayKey) {
|
|
154
|
+
throw new BackgroundBudgetError("replay_conflict", "run_replay_conflict", `run ${runId} 已用于其他商家/记账日;不得复用`);
|
|
155
|
+
}
|
|
156
|
+
return runToRecord(existing);
|
|
157
|
+
}
|
|
158
|
+
const now = this.nowUtc();
|
|
159
|
+
this.ensureDayRow(merchantId, dayKey, now);
|
|
160
|
+
const day = this.readDayRow(merchantId, dayKey);
|
|
161
|
+
const active = this.db
|
|
162
|
+
.prepare(`SELECT COUNT(*) AS n FROM background_budget_reservations
|
|
163
|
+
WHERE merchant_id = ? AND state = 'RESERVED'`)
|
|
164
|
+
.get(merchantId);
|
|
165
|
+
if (Number(active.n) >= BACKGROUND_BUDGET_LIMITS.max_concurrency) {
|
|
166
|
+
throw new BackgroundBudgetError("concurrency_limit", "background_concurrency", `商家 ${merchantId} 已有后台 run 在途(并发上限 ${BACKGROUND_BUDGET_LIMITS.max_concurrency})`);
|
|
167
|
+
}
|
|
168
|
+
if (day.runs_used + 1 > BACKGROUND_BUDGET_LIMITS.daily_runs) {
|
|
169
|
+
throw new BackgroundBudgetError("budget_exhausted", "daily_runs", `商家 ${merchantId} 当日 run 已达上限 ${BACKGROUND_BUDGET_LIMITS.daily_runs}`);
|
|
170
|
+
}
|
|
171
|
+
if (day.input_used + BACKGROUND_BUDGET_LIMITS.run_input_reserve >
|
|
172
|
+
BACKGROUND_BUDGET_LIMITS.daily_input_tokens) {
|
|
173
|
+
throw new BackgroundBudgetError("budget_exhausted", "daily_input_tokens", `商家 ${merchantId} 当日 input token 不足(预留 ${BACKGROUND_BUDGET_LIMITS.run_input_reserve})`);
|
|
174
|
+
}
|
|
175
|
+
if (day.output_used + BACKGROUND_BUDGET_LIMITS.run_output_reserve >
|
|
176
|
+
BACKGROUND_BUDGET_LIMITS.daily_output_tokens) {
|
|
177
|
+
throw new BackgroundBudgetError("budget_exhausted", "daily_output_tokens", `商家 ${merchantId} 当日 output token 不足(预留 ${BACKGROUND_BUDGET_LIMITS.run_output_reserve})`);
|
|
178
|
+
}
|
|
179
|
+
this.db
|
|
180
|
+
.prepare(`INSERT INTO background_budget_reservations
|
|
181
|
+
(run_id, merchant_id, day_key, template, input_reserved, output_reserved,
|
|
182
|
+
input_actual, output_actual, usage_state, state, created_at, updated_at)
|
|
183
|
+
VALUES (?, ?, ?, ?, ?, ?, NULL, NULL, NULL, 'RESERVED', ?, ?)`)
|
|
184
|
+
.run(runId, merchantId, dayKey, template, BACKGROUND_BUDGET_LIMITS.run_input_reserve, BACKGROUND_BUDGET_LIMITS.run_output_reserve, now, now);
|
|
185
|
+
this.db
|
|
186
|
+
.prepare(`UPDATE background_budget_days
|
|
187
|
+
SET runs_used = runs_used + 1,
|
|
188
|
+
input_used = input_used + ?,
|
|
189
|
+
output_used = output_used + ?,
|
|
190
|
+
updated_at = ?
|
|
191
|
+
WHERE merchant_id = ? AND day_key = ?`)
|
|
192
|
+
.run(BACKGROUND_BUDGET_LIMITS.run_input_reserve, BACKGROUND_BUDGET_LIMITS.run_output_reserve, now, merchantId, dayKey);
|
|
193
|
+
return runToRecord(this.readRunRow(runId));
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* 结算:预留换成实际用量。同 run 重复结算为 no-op(返回当前记录)。
|
|
198
|
+
* `usage_state='unknown'`(provider 未返回用量)按预留值保守占额。
|
|
199
|
+
*/
|
|
200
|
+
settleRun(input) {
|
|
201
|
+
const merchantId = requireId(input.merchant_id, "merchant_id");
|
|
202
|
+
const runId = requireId(input.run_id, "run_id");
|
|
203
|
+
const usageState = input.usage_state ?? "known";
|
|
204
|
+
if (usageState !== "known" && usageState !== "unknown") {
|
|
205
|
+
throw new BackgroundBudgetError("validation", "usage_state_invalid", "usage_state 只能是 known/unknown");
|
|
206
|
+
}
|
|
207
|
+
return inImmediateTransaction(this.db, () => {
|
|
208
|
+
const row = this.readRunRow(runId);
|
|
209
|
+
if (row === undefined || row.merchant_id !== merchantId) {
|
|
210
|
+
throw new BackgroundBudgetError("not_found", "run_not_found", `run ${runId} 不存在或不可见`);
|
|
211
|
+
}
|
|
212
|
+
if (row.state !== "RESERVED")
|
|
213
|
+
return runToRecord(row);
|
|
214
|
+
const inputActual = usageState === "unknown"
|
|
215
|
+
? row.input_reserved
|
|
216
|
+
: requireTokenCount(input.input_tokens, "input_tokens");
|
|
217
|
+
const outputActual = usageState === "unknown"
|
|
218
|
+
? row.output_reserved
|
|
219
|
+
: requireTokenCount(input.output_tokens, "output_tokens");
|
|
220
|
+
const now = this.nowUtc();
|
|
221
|
+
this.db
|
|
222
|
+
.prepare(`UPDATE background_budget_reservations
|
|
223
|
+
SET state = 'SETTLED', input_actual = ?, output_actual = ?,
|
|
224
|
+
usage_state = ?, updated_at = ?
|
|
225
|
+
WHERE run_id = ? AND state = 'RESERVED'`)
|
|
226
|
+
.run(inputActual, outputActual, usageState, now, runId);
|
|
227
|
+
this.db
|
|
228
|
+
.prepare(`UPDATE background_budget_days
|
|
229
|
+
SET input_used = input_used - ? + ?,
|
|
230
|
+
output_used = output_used - ? + ?,
|
|
231
|
+
updated_at = ?
|
|
232
|
+
WHERE merchant_id = ? AND day_key = ?`)
|
|
233
|
+
.run(row.input_reserved, inputActual, row.output_reserved, outputActual, now, row.merchant_id, row.day_key);
|
|
234
|
+
return runToRecord(this.readRunRow(runId));
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/** 释放未发起的预留(幂等):归还 token 与 run 计数。 */
|
|
238
|
+
releaseRun(input) {
|
|
239
|
+
const merchantId = requireId(input.merchant_id, "merchant_id");
|
|
240
|
+
const runId = requireId(input.run_id, "run_id");
|
|
241
|
+
return inImmediateTransaction(this.db, () => {
|
|
242
|
+
const row = this.readRunRow(runId);
|
|
243
|
+
if (row === undefined || row.merchant_id !== merchantId) {
|
|
244
|
+
throw new BackgroundBudgetError("not_found", "run_not_found", `run ${runId} 不存在或不可见`);
|
|
245
|
+
}
|
|
246
|
+
if (row.state !== "RESERVED")
|
|
247
|
+
return runToRecord(row);
|
|
248
|
+
const now = this.nowUtc();
|
|
249
|
+
this.db
|
|
250
|
+
.prepare(`UPDATE background_budget_reservations
|
|
251
|
+
SET state = 'RELEASED', updated_at = ?
|
|
252
|
+
WHERE run_id = ? AND state = 'RESERVED'`)
|
|
253
|
+
.run(now, runId);
|
|
254
|
+
this.db
|
|
255
|
+
.prepare(`UPDATE background_budget_days
|
|
256
|
+
SET runs_used = runs_used - 1,
|
|
257
|
+
input_used = input_used - ?,
|
|
258
|
+
output_used = output_used - ?,
|
|
259
|
+
updated_at = ?
|
|
260
|
+
WHERE merchant_id = ? AND day_key = ?`)
|
|
261
|
+
.run(row.input_reserved, row.output_reserved, now, row.merchant_id, row.day_key);
|
|
262
|
+
return runToRecord(this.readRunRow(runId));
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
getRun(merchantId, runId) {
|
|
266
|
+
const row = this.readRunRow(requireId(runId, "run_id"));
|
|
267
|
+
if (row === undefined || row.merchant_id !== requireId(merchantId, "merchant_id")) {
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
return runToRecord(row);
|
|
271
|
+
}
|
|
272
|
+
/** 当日已用量(含在途预留);行不存在时返回零值(不写库)。 */
|
|
273
|
+
getDayUsage(merchantId, dayKey) {
|
|
274
|
+
const merchant = requireId(merchantId, "merchant_id");
|
|
275
|
+
const day = requireDayKey(dayKey);
|
|
276
|
+
const row = this.readDayRow(merchant, day);
|
|
277
|
+
if (row === undefined) {
|
|
278
|
+
return {
|
|
279
|
+
merchant_id: merchant,
|
|
280
|
+
day_key: day,
|
|
281
|
+
runs_used: 0,
|
|
282
|
+
input_used: 0,
|
|
283
|
+
output_used: 0,
|
|
284
|
+
updated_at: "",
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
return dayToUsage(row);
|
|
288
|
+
}
|
|
289
|
+
ensureDayRow(merchantId, dayKey, now) {
|
|
290
|
+
this.db
|
|
291
|
+
.prepare(`INSERT INTO background_budget_days
|
|
292
|
+
(merchant_id, day_key, runs_used, input_used, output_used, updated_at)
|
|
293
|
+
VALUES (?, ?, 0, 0, 0, ?)
|
|
294
|
+
ON CONFLICT(merchant_id, day_key) DO NOTHING`)
|
|
295
|
+
.run(merchantId, dayKey, now);
|
|
296
|
+
}
|
|
297
|
+
readDayRow(merchantId, dayKey) {
|
|
298
|
+
return this.db
|
|
299
|
+
.prepare("SELECT * FROM background_budget_days WHERE merchant_id = ? AND day_key = ?")
|
|
300
|
+
.get(merchantId, dayKey);
|
|
301
|
+
}
|
|
302
|
+
readRunRow(runId) {
|
|
303
|
+
return this.db
|
|
304
|
+
.prepare("SELECT * FROM background_budget_reservations WHERE run_id = ?")
|
|
305
|
+
.get(runId);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
//# sourceMappingURL=background-budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"background-budget.js","sourceRoot":"","sources":["../../../src/merchant-core/budget/background-budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AA4BH,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAEnE,oBAAoB;AACpB,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC,MAAM,CAAC;IACpD,UAAU,EAAE,EAAE;IACd,kBAAkB,EAAE,OAAO;IAC3B,mBAAmB,EAAE,MAAM;IAC3B,iBAAiB,EAAE,KAAK;IACxB,kBAAkB,EAAE,GAAG;IACvB,kBAAkB,EAAE,MAAM;IAC1B,mBAAmB,EAAE,KAAK;IAC1B,cAAc,EAAE,KAAK;IACrB,eAAe,EAAE,KAAK;IACtB,eAAe,EAAE,CAAC;CACnB,CAAC,CAAC;AASH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IACrC,IAAI,CAA4B;IACzC,wDAAwD;IAC/C,MAAM,CAAS;IAExB,YAAY,IAA+B,EAAE,MAAc,EAAE,OAAe;QAC1E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAqDD,SAAS,UAAU,CAAC,GAAW;IAC7B,O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AAC;QAC3C,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,OAAO;gBACL,WAAW,EAAE,QAAQ;gBACrB,OAAO,EAAE,GAAG;gBACZ,SAAS,EAAE,CAAC;gBACZ,UAAU,EAAE,CAAC;gBACb,WAAW,EAAE,CAAC;gBACd,UAAU,EAAE,EAAE;aACf,CAAC;QACJ,CAAC;QACD,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAEO,YAAY,CAAC,UAAkB,EAAE,MAAc,EAAE,GAAW;QAClE,IAAI,CAAC,EAAE;aACJ,OAAO,CACN;;;sDAG8C,CAC/C;aACA,GAAG,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;IAClC,CAAC;IAEO,UAAU,CAAC,UAAkB,EAAE,MAAc;QACnD,OAAO,IAAI,CAAC,EAAE;aACX,OAAO,CAAC,4EAA4E,CAAC;aACrF,GAAG,CAAC,UAAU,EAAE,MAAM,CAAkC,CAAC;IAC9D,CAAC;IAEO,UAAU,CAAC,KAAa;QAC9B,OAAO,IAAI,CAAC,EAAE;aACX,OAAO,CAAC,+DAA+D,CAAC;aACxE,GAAG,CAAC,KAAK,CAAkC,CAAC;IACjD,CAAC;CACF"}
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
* ActorContext(TOP1-3 设计 v1.1 §4.1 / §4.2)。
|
|
18
|
+
*
|
|
19
|
+
* 由管理鉴权、可信 A2A 身份或现有访问会话**在服务端创建**;模型与客户端
|
|
20
|
+
* 只提交业务参数,不能提交可信的 merchant_id、subject_id、权限集合或身份
|
|
21
|
+
* 验证结论。本文件是唯一构造点:入参形状来自服务端调用方(facade 注入的
|
|
22
|
+
* 认证上下文),严格校验失败即抛类型化错误。
|
|
23
|
+
*
|
|
24
|
+
* D-M0-01:`subject_binding` 为 `person` / `verified_person` 时构造失败
|
|
25
|
+
* (P0 个人主体关闭);上层可捕获 `subject_binding_rejected` 降级为匿名。
|
|
26
|
+
*/
|
|
27
|
+
import { type ActorCapability, type AuthStrength, type SubjectBindingKind } from "./types.js";
|
|
28
|
+
export interface ActorContext {
|
|
29
|
+
/** 商家域,来自认证上下文;接待/管理调用不接受正文自报。 */
|
|
30
|
+
readonly merchant_id: string;
|
|
31
|
+
/** 服务端身份(管理员、后台服务主体或客户主体引用),不是模型会话 ID。 */
|
|
32
|
+
readonly actor_id: string;
|
|
33
|
+
/** 服务端判定的认证强度。 */
|
|
34
|
+
readonly auth_strength: AuthStrength;
|
|
35
|
+
/** 主体绑定范围;P0 拒绝 person / verified_person(D-M0-01)。 */
|
|
36
|
+
readonly subject_binding: SubjectBindingKind;
|
|
37
|
+
/** 能力集合,来自固定注册表;空集合 = 只有匿名接待级权限。 */
|
|
38
|
+
readonly capabilities: readonly ActorCapability[];
|
|
39
|
+
/** 构造时的 policy_version(m0-pilot-1),用于 trace 与守卫比对。 */
|
|
40
|
+
readonly policy_version: string;
|
|
41
|
+
}
|
|
42
|
+
export interface CreateActorContextInput {
|
|
43
|
+
merchant_id: string;
|
|
44
|
+
actor_id: string;
|
|
45
|
+
auth_strength: AuthStrength;
|
|
46
|
+
subject_binding: SubjectBindingKind;
|
|
47
|
+
capabilities?: readonly string[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 服务端创建 ActorContext。任何形状问题都 fail-closed;个人主体绑定在此
|
|
51
|
+
* 拒绝(原因码 `subject_binding_rejected`),不是静默降级。
|
|
52
|
+
*/
|
|
53
|
+
export declare function createActorContext(input: CreateActorContextInput): ActorContext;
|
|
54
|
+
export declare function hasCapability(actor: ActorContext, capability: ActorCapability): boolean;
|
|
55
|
+
/** 要求具备某能力,否则 access_denied(不披露其他主体资源是否存在)。 */
|
|
56
|
+
export declare function requireCapability(actor: ActorContext, capability: ActorCapability): void;
|
|
@@ -0,0 +1,76 @@
|
|
|
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
|
+
* ActorContext(TOP1-3 设计 v1.1 §4.1 / §4.2)。
|
|
18
|
+
*
|
|
19
|
+
* 由管理鉴权、可信 A2A 身份或现有访问会话**在服务端创建**;模型与客户端
|
|
20
|
+
* 只提交业务参数,不能提交可信的 merchant_id、subject_id、权限集合或身份
|
|
21
|
+
* 验证结论。本文件是唯一构造点:入参形状来自服务端调用方(facade 注入的
|
|
22
|
+
* 认证上下文),严格校验失败即抛类型化错误。
|
|
23
|
+
*
|
|
24
|
+
* D-M0-01:`subject_binding` 为 `person` / `verified_person` 时构造失败
|
|
25
|
+
* (P0 个人主体关闭);上层可捕获 `subject_binding_rejected` 降级为匿名。
|
|
26
|
+
*/
|
|
27
|
+
import { ACTOR_CAPABILITIES, AUTH_STRENGTHS, ContextGovernanceError, GOVERNANCE_POLICY_VERSION, PERSONAL_SUBJECT_BINDINGS, SUBJECT_BINDING_KINDS, assertOpaqueId, } from "./types.js";
|
|
28
|
+
function parseCapabilities(raw) {
|
|
29
|
+
if (raw === undefined)
|
|
30
|
+
return [];
|
|
31
|
+
const seen = new Set();
|
|
32
|
+
for (const item of raw) {
|
|
33
|
+
if (!ACTOR_CAPABILITIES.includes(item)) {
|
|
34
|
+
throw new ContextGovernanceError("validation", "capability_unknown", `能力 "${item}" 不在固定注册表中,枚举不可由调用方扩展`);
|
|
35
|
+
}
|
|
36
|
+
seen.add(item);
|
|
37
|
+
}
|
|
38
|
+
// 输出稳定排序,保证派生摘要与日志确定性。
|
|
39
|
+
return ACTOR_CAPABILITIES.filter((cap) => seen.has(cap));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 服务端创建 ActorContext。任何形状问题都 fail-closed;个人主体绑定在此
|
|
43
|
+
* 拒绝(原因码 `subject_binding_rejected`),不是静默降级。
|
|
44
|
+
*/
|
|
45
|
+
export function createActorContext(input) {
|
|
46
|
+
if (!AUTH_STRENGTHS.includes(input.auth_strength)) {
|
|
47
|
+
throw new ContextGovernanceError("validation", "auth_strength_unknown", `auth_strength "${input.auth_strength}" 不在固定注册表中`);
|
|
48
|
+
}
|
|
49
|
+
if (!SUBJECT_BINDING_KINDS.includes(input.subject_binding)) {
|
|
50
|
+
throw new ContextGovernanceError("validation", "subject_binding_unknown", `subject_binding "${input.subject_binding}" 不在固定注册表中`);
|
|
51
|
+
}
|
|
52
|
+
if (PERSONAL_SUBJECT_BINDINGS.has(input.subject_binding)) {
|
|
53
|
+
throw new ContextGovernanceError("unsupported_capability", "subject_binding_rejected", `P0 不启用个人主体(D-M0-01):${input.subject_binding} 一律拒绝,保留接入点`);
|
|
54
|
+
}
|
|
55
|
+
if (input.subject_binding !== "none" && input.auth_strength === "anonymous") {
|
|
56
|
+
throw new ContextGovernanceError("validation", "subject_binding_without_auth", "匿名认证不能携带主体绑定");
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
merchant_id: assertOpaqueId(input.merchant_id, "merchant_id"),
|
|
60
|
+
actor_id: assertOpaqueId(input.actor_id, "actor_id"),
|
|
61
|
+
auth_strength: input.auth_strength,
|
|
62
|
+
subject_binding: input.subject_binding,
|
|
63
|
+
capabilities: parseCapabilities(input.capabilities),
|
|
64
|
+
policy_version: GOVERNANCE_POLICY_VERSION,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export function hasCapability(actor, capability) {
|
|
68
|
+
return actor.capabilities.includes(capability);
|
|
69
|
+
}
|
|
70
|
+
/** 要求具备某能力,否则 access_denied(不披露其他主体资源是否存在)。 */
|
|
71
|
+
export function requireCapability(actor, capability) {
|
|
72
|
+
if (!hasCapability(actor, capability)) {
|
|
73
|
+
throw new ContextGovernanceError("access_denied", "capability_missing", `actor 缺少能力 ${capability}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=actor-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actor-context.js","sourceRoot":"","sources":["../../../src/merchant-core/context-governance/actor-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;GAUG;AAEH,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,sBAAsB,EACtB,yBAAyB,EACzB,yBAAyB,EACzB,qBAAqB,EACrB,cAAc,GAIf,MAAM,YAAY,CAAC;AAyBpB,SAAS,iBAAiB,CAAC,GAAkC;IAC3D,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC;IACjC,MAAM,IAAI,GAAG,IAAI,GAAG,EAAmB,CAAC;IACxC,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;QACvB,IAAI,CAAE,kBAAwC,CAAC,QAAQ,CAAC,I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AC7D,QAAQ,EAAE,cAAc,CAAC,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC;QACpD,aAAa,EAAE,KAAK,CAAC,aAAa;QAClC,eAAe,EAAE,KAAK,CAAC,eAAe;QACtC,YAAY,EAAE,iBAAiB,CAAC,KAAK,CAAC,YAAY,CAAC;QACnD,cAAc,EAAE,yBAAyB;KAC1C,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAmB,EAAE,UAA2B;IAC5E,OAAO,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;AACjD,CAAC;AAED,+CAA+C;AAC/C,MAAM,UAAU,iBAAiB,CAAC,KAAmB,EAAE,UAA2B;IAChF,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,sBAAsB,CAC9B,eAAe,EACf,oBAAoB,EACpB,cAAc,UAAU,EAAE,CAC3B,CAAC;IACJ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
* grant 支撑的默认 ContextVersionAuthority(M1 共性控制)。
|
|
18
|
+
*
|
|
19
|
+
* grant 域直接读 `DataUseGrantStore`(撤销/过期即 `undefined`);其余域
|
|
20
|
+
* (data scope、技能、记忆集合、会话、配置版本、来源可用性)在后续里程碑
|
|
21
|
+
* 接入各自存储,通过可选回调注入。**未注入的域按失效处理**:快照一旦绑定
|
|
22
|
+
* 该域,守卫一律 stale_context——不允许"查不到就当没变"。
|
|
23
|
+
*/
|
|
24
|
+
import type { DataUseGrantStore } from "./data-use-grant.js";
|
|
25
|
+
import type { ContextVersionAuthority, CurrentSkillBinding, SourceRefCheck } from "./context-guard.js";
|
|
26
|
+
export interface GrantBackedAuthorityOptions {
|
|
27
|
+
readonly grants: DataUseGrantStore;
|
|
28
|
+
/** 主体绑定当前 epoch(§9.2);undefined = 已撤销/不存在。 */
|
|
29
|
+
readonly bindingEpoch?: (merchantId: string, actorId: string) => number | undefined;
|
|
30
|
+
/** 数据域当前 epoch;undefined = 已封存/删除。 */
|
|
31
|
+
readonly dataScopeEpoch?: (merchantId: string, scopeKey: string) => number | undefined;
|
|
32
|
+
readonly skillBinding?: (merchantId: string, skillKey: string) => CurrentSkillBinding | undefined;
|
|
33
|
+
readonly memorySetVersion?: (merchantId: string) => number | undefined;
|
|
34
|
+
readonly conversationRevision?: (merchantId: string) => number | undefined;
|
|
35
|
+
/** 配置版本;undefined = 无法核实(绑定非空 config_version 的快照将被拒绝)。 */
|
|
36
|
+
readonly configVersion?: () => string | undefined;
|
|
37
|
+
readonly sourceAvailable?: (merchantId: string, ref: SourceRefCheck) => boolean;
|
|
38
|
+
}
|
|
39
|
+
export declare function createGrantBackedAuthority(options: GrantBackedAuthorityOptions): ContextVersionAuthority;
|
|
@@ -0,0 +1,28 @@
|
|
|
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 function createGrantBackedAuthority(options) {
|
|
17
|
+
return {
|
|
18
|
+
currentGrantEpoch: (merchantId, grantId) => options.grants.currentGrantEpoch(merchantId, grantId),
|
|
19
|
+
currentBindingEpoch: options.bindingEpoch ?? ((_merchantId, _actorId) => undefined),
|
|
20
|
+
currentDataScopeEpoch: options.dataScopeEpoch ?? ((_merchantId, _scopeKey) => undefined),
|
|
21
|
+
currentSkillBinding: options.skillBinding ?? ((_merchantId, _skillKey) => undefined),
|
|
22
|
+
currentMemorySetVersion: options.memorySetVersion ?? ((_merchantId) => undefined),
|
|
23
|
+
currentConversationRevision: options.conversationRevision ?? ((_merchantId) => undefined),
|
|
24
|
+
currentConfigVersion: options.configVersion ?? (() => undefined),
|
|
25
|
+
sourceAvailable: options.sourceAvailable ?? ((_merchantId, _ref) => false),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=authority.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authority.js","sourceRoot":"","sources":["../../../src/merchant-core/context-governance/authority.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAmCH,MAAM,UAAU,0BAA0B,CACxC,OAAoC;IAEpC,OAAO;QACL,iBAAiB,EAAE,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,CACzC,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,UAAU,EAAE,OAAO,CAAC;QACvD,mBAAmB,EAAE,OAAO,CAAC,YAAY,IAAI,CAAC,CAAC,WAAW,EAAE,QAAQ,EAAE,EAAE,CAAC,SAAS,CAAC;QACnF,qBAAqB,EACnB,OAAO,CAAC,cAAc,IAAI,CAAC,CAAC,WAAW,EAAE,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC;QACnE,mBAAmB,EAAE,OAAO,CAAC,YAAY,IAAI,CAAC,CAAC,WAAW,EAAE,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC;QACpF,uBAAuB,EAAE,OAAO,CAAC,gBAAgB,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,SAAS,CAAC;QACjF,2BAA2B,EAAE,OAAO,CAAC,oBAAoB,IAAI,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,SAAS,CAAC;QACzF,oBAAoB,EAAE,OAAO,CAAC,aAAa,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;QAChE,eAAe,EAAE,OAAO,CAAC,eAAe,IAAI,CAAC,CAAC,WAAW,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC;KAC3E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
import { type ActorContext } from "./actor-context.js";
|
|
17
|
+
import type { ActorCapability } from "./types.js";
|
|
18
|
+
export type ContextCheckpoint = "read" | "tool_call" | "provider_attempt" | "publish";
|
|
19
|
+
/** 运行开始时固定的技能绑定(§7.1);M1 只定义形状与校验。 */
|
|
20
|
+
export interface SkillBindingRef {
|
|
21
|
+
readonly skill_key: string;
|
|
22
|
+
readonly release_id: string;
|
|
23
|
+
readonly compiled_artifact_hash: string;
|
|
24
|
+
readonly generation: number;
|
|
25
|
+
readonly revocation_epoch: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 服务端签发的运行快照(§4.2)。版本号 0 / 空集合表示本次运行不使用该域;
|
|
29
|
+
* 使用中的域必须在每个检查点与当前版本一致。
|
|
30
|
+
*/
|
|
31
|
+
export interface ContextSnapshot {
|
|
32
|
+
readonly snapshot_id: string;
|
|
33
|
+
readonly issued_at: string;
|
|
34
|
+
readonly binding_epoch: number;
|
|
35
|
+
/** grant_id → 签发时的 grant_epoch。 */
|
|
36
|
+
readonly grant_versions: Readonly<Record<string, number>>;
|
|
37
|
+
/** 数据域(erase_scope)→ 签发时的 data_scope_epoch。 */
|
|
38
|
+
readonly data_scope_epochs: Readonly<Record<string, number>>;
|
|
39
|
+
readonly memory_set_version: number;
|
|
40
|
+
readonly conversation_revision: number;
|
|
41
|
+
readonly config_version: string;
|
|
42
|
+
readonly skill_bindings: readonly SkillBindingRef[];
|
|
43
|
+
}
|
|
44
|
+
/** 来源可用性检查输入;不携带正文或秘密(§4.2 SourceRef)。 */
|
|
45
|
+
export interface SourceRefCheck {
|
|
46
|
+
readonly object_id: string;
|
|
47
|
+
readonly version: number;
|
|
48
|
+
readonly expires_at?: string;
|
|
49
|
+
readonly erased?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** 技能当前绑定(权威值);undefined = 已撤回/未绑定/已删除。 */
|
|
52
|
+
export interface CurrentSkillBinding {
|
|
53
|
+
readonly release_id: string;
|
|
54
|
+
/** 权威编译产物摘要(发布时冻结);快照绑定必须与之逐值一致(§7.1)。 */
|
|
55
|
+
readonly compiled_artifact_hash: string;
|
|
56
|
+
readonly generation: number;
|
|
57
|
+
readonly revocation_epoch: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* "当前版本"权威。实现方返回 `undefined` 表示对象已不存在(撤权、封存、
|
|
61
|
+
* 接管后旧对象失效);guard 把一切与快照的不一致折叠为 stale_context。
|
|
62
|
+
*/
|
|
63
|
+
export interface ContextVersionAuthority {
|
|
64
|
+
currentGrantEpoch(merchantId: string, grantId: string): number | undefined;
|
|
65
|
+
/** 主体绑定当前 epoch(§9.2);undefined = 绑定已撤销/不存在。 */
|
|
66
|
+
currentBindingEpoch(merchantId: string, actorId: string): number | undefined;
|
|
67
|
+
currentDataScopeEpoch(merchantId: string, scopeKey: string): number | undefined;
|
|
68
|
+
currentSkillBinding(merchantId: string, skillKey: string): CurrentSkillBinding | undefined;
|
|
69
|
+
currentMemorySetVersion(merchantId: string): number | undefined;
|
|
70
|
+
currentConversationRevision(merchantId: string): number | undefined;
|
|
71
|
+
currentConfigVersion(): string | undefined;
|
|
72
|
+
sourceAvailable(merchantId: string, ref: SourceRefCheck): boolean;
|
|
73
|
+
}
|
|
74
|
+
export interface ContextGuardCheckInput {
|
|
75
|
+
readonly actor: ActorContext;
|
|
76
|
+
readonly snapshot: ContextSnapshot;
|
|
77
|
+
/**
|
|
78
|
+
* 本检查点行使的能力。§4.3:四个检查点"分别检查权限"——全部必填,
|
|
79
|
+
* 不提供无权限旁路(缺省即 validation 失败)。
|
|
80
|
+
*/
|
|
81
|
+
readonly capability: ActorCapability;
|
|
82
|
+
/** tool_call 的诊断名,进 trace 原因码,不参与判定。 */
|
|
83
|
+
readonly tool?: string;
|
|
84
|
+
/** 本次读取/调用要用的来源引用;每个都必须可用。 */
|
|
85
|
+
readonly source_refs?: readonly SourceRefCheck[];
|
|
86
|
+
}
|
|
87
|
+
export interface ContextGuardOptions {
|
|
88
|
+
readonly authority: ContextVersionAuthority;
|
|
89
|
+
readonly now: () => string;
|
|
90
|
+
}
|
|
91
|
+
export declare class ContextGuard {
|
|
92
|
+
private readonly authority;
|
|
93
|
+
private readonly now;
|
|
94
|
+
constructor(options: ContextGuardOptions);
|
|
95
|
+
/** 读取前检查:能力(必填)+ 主体绑定 + grant/来源/版本一致性。 */
|
|
96
|
+
checkRead(input: ContextGuardCheckInput): void;
|
|
97
|
+
/** 工具调用前检查:同 read;工具名进 trace。 */
|
|
98
|
+
checkToolCall(input: ContextGuardCheckInput): void;
|
|
99
|
+
/** provider attempt 派发前检查:重试/压缩/摘要分支同样要走(§4.3)。 */
|
|
100
|
+
checkProviderAttempt(input: ContextGuardCheckInput): void;
|
|
101
|
+
/** 发布前检查(发布临界区的版本半边):全部域必须仍一致。 */
|
|
102
|
+
checkPublish(input: ContextGuardCheckInput): void;
|
|
103
|
+
private check;
|
|
104
|
+
}
|