@harrylabsj/kiwi 0.12.27 → 0.12.28
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/README.md +9 -2
- package/contracts/kiwi-catalog/1.0/agent-record.schema.json +29 -0
- package/contracts/kiwi-contracts.lock.json +2 -2
- package/contracts/manifest.json +2 -2
- package/dist/a2a/admission.js +2 -1
- package/dist/a2a/admission.js.map +1 -1
- package/dist/a2a/node.d.ts +6 -0
- package/dist/a2a/node.js +70 -10
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/reception/agent-runtime.js +9 -3
- package/dist/a2a/reception/agent-runtime.js.map +1 -1
- package/dist/a2a/reception/reply-outbox.d.ts +6 -0
- package/dist/a2a/reception/reply-outbox.js +13 -0
- package/dist/a2a/reception/reply-outbox.js.map +1 -1
- package/dist/a2a/reception/retention.d.ts +1 -0
- package/dist/a2a/reception/retention.js +9 -1
- package/dist/a2a/reception/retention.js.map +1 -1
- package/dist/a2a/server/auth.js +22 -3
- package/dist/a2a/server/auth.js.map +1 -1
- package/dist/a2a/server/identity.d.ts +23 -0
- package/dist/a2a/server/identity.js +27 -0
- package/dist/a2a/server/identity.js.map +1 -0
- package/dist/a2a/server/merchant-handler.d.ts +0 -11
- package/dist/a2a/server/merchant-handler.js +13 -6
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/negotiation-advisor.js +3 -1
- package/dist/a2a/server/negotiation-advisor.js.map +1 -1
- package/dist/a2a/server/pipeline.d.ts +7 -1
- package/dist/a2a/server/pipeline.js +65 -35
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/server.d.ts +0 -17
- package/dist/a2a/server/server.js +21 -4
- package/dist/a2a/server/server.js.map +1 -1
- package/dist/a2a/server/task-registry.d.ts +3 -0
- package/dist/a2a/server/task-registry.js +33 -1
- package/dist/a2a/server/task-registry.js.map +1 -1
- package/dist/agent/buyer/buyer-tools.js +33 -19
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/buyer/ranker.js +32 -14
- package/dist/agent/buyer/ranker.js.map +1 -1
- package/dist/agent/buyer/scheduler.js +12 -3
- package/dist/agent/buyer/scheduler.js.map +1 -1
- package/dist/agent/buyer/types.d.ts +2 -1
- package/dist/agent/buyer/types.js.map +1 -1
- package/dist/agent/connector/types.d.ts +4 -2
- package/dist/agent/connector/types.js +8 -2
- package/dist/agent/connector/types.js.map +1 -1
- package/dist/agent/host/events.js +1 -1
- package/dist/agent/host/events.js.map +1 -1
- package/dist/agent/kernel.js +14 -7
- package/dist/agent/kernel.js.map +1 -1
- package/dist/agent/negotiation-chat.js +6 -4
- package/dist/agent/negotiation-chat.js.map +1 -1
- package/dist/agent/presentation/sanitize.js +1 -1
- package/dist/agent/presentation/sanitize.js.map +1 -1
- package/dist/agent/supplier/scheduler.js.map +1 -1
- package/dist/auth/merchant-pairing.js +5 -0
- package/dist/auth/merchant-pairing.js.map +1 -1
- package/dist/auth/merchant-sessions.js +19 -4
- package/dist/auth/merchant-sessions.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +7 -0
- package/dist/buyer-core/a2a-negotiator.js +17 -3
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +22 -0
- package/dist/buyer-core/a2a-quote-fetcher.js +71 -3
- package/dist/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/dist/buyer-core/build-service.d.ts +7 -0
- package/dist/buyer-core/build-service.js +249 -22
- package/dist/buyer-core/build-service.js.map +1 -1
- package/dist/buyer-core/buyer-route-hint-store.d.ts +21 -0
- package/dist/buyer-core/buyer-route-hint-store.js +119 -0
- package/dist/buyer-core/buyer-route-hint-store.js.map +1 -0
- package/dist/buyer-core/merchant-index.js +7 -2
- package/dist/buyer-core/merchant-index.js.map +1 -1
- package/dist/buyer-core/service.d.ts +15 -0
- package/dist/buyer-core/service.js +34 -9
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/buyer-core/store.d.ts +11 -0
- package/dist/buyer-core/store.js +68 -0
- package/dist/buyer-core/store.js.map +1 -1
- package/dist/cli.js +61 -8
- package/dist/cli.js.map +1 -1
- package/dist/cloud/access/access-mode.d.ts +56 -0
- package/dist/cloud/access/access-mode.js +129 -0
- package/dist/cloud/access/access-mode.js.map +1 -0
- package/dist/cloud/binding/enrollment-challenge.js +3 -2
- package/dist/cloud/binding/enrollment-challenge.js.map +1 -1
- package/dist/cloud/binding/runtime-challenge.js +3 -2
- package/dist/cloud/binding/runtime-challenge.js.map +1 -1
- package/dist/cloud/binding/store-lock.js +16 -7
- package/dist/cloud/binding/store-lock.js.map +1 -1
- package/dist/cloud/bootstrap.d.ts +1 -1
- package/dist/cloud/bootstrap.js +47 -7
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/catalog-client.d.ts +25 -1
- package/dist/cloud/catalog-client.js +163 -4
- package/dist/cloud/catalog-client.js.map +1 -1
- package/dist/cloud/connect-service.d.ts +69 -0
- package/dist/cloud/connect-service.js +418 -50
- package/dist/cloud/connect-service.js.map +1 -1
- package/dist/cloud/file-listing-publication.d.ts +3 -0
- package/dist/cloud/file-listing-publication.js +2 -2
- package/dist/cloud/file-listing-publication.js.map +1 -1
- package/dist/cloud/owned-runtime.js +21 -2
- package/dist/cloud/owned-runtime.js.map +1 -1
- package/dist/cloud/product-import-tabular.d.ts +6 -2
- package/dist/cloud/product-import-tabular.js +57 -41
- package/dist/cloud/product-import-tabular.js.map +1 -1
- package/dist/cloud/product-source.d.ts +6 -3
- package/dist/cloud/product-source.js +8 -8
- package/dist/cloud/product-source.js.map +1 -1
- package/dist/cloud/trade-platforms-client.d.ts +8 -0
- package/dist/cloud/trade-platforms-client.js +28 -0
- package/dist/cloud/trade-platforms-client.js.map +1 -0
- package/dist/commerce/types.d.ts +6 -2
- package/dist/commerce/types.js +10 -3
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/profile.d.ts +5 -0
- package/dist/config/profile.js +7 -2
- package/dist/config/profile.js.map +1 -1
- package/dist/counterparty/shopping-cli-hosted/index.js +20 -12
- package/dist/counterparty/shopping-cli-hosted/index.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-record.d.ts +23 -0
- package/dist/discovery/catalog-source/kiwi-record.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-source.js +4 -1
- package/dist/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/dist/discovery/merchant-subscriptions.js +9 -0
- package/dist/discovery/merchant-subscriptions.js.map +1 -1
- package/dist/handoff/channel.js +3 -1
- package/dist/handoff/channel.js.map +1 -1
- package/dist/handoff/idempotency.d.ts +6 -0
- package/dist/handoff/idempotency.js +71 -18
- package/dist/handoff/idempotency.js.map +1 -1
- package/dist/handoff/launch-marketplace-policy.d.ts +1 -1
- package/dist/handoff/launch-marketplace-policy.js +9 -1
- package/dist/handoff/launch-marketplace-policy.js.map +1 -1
- package/dist/handoff/ledger.d.ts +6 -1
- package/dist/handoff/ledger.js +13 -2
- package/dist/handoff/ledger.js.map +1 -1
- package/dist/handoff/marketplace-product-link.d.ts +17 -6
- package/dist/handoff/marketplace-product-link.js +27 -37
- package/dist/handoff/marketplace-product-link.js.map +1 -1
- package/dist/handoff/operator-approval.d.ts +3 -0
- package/dist/handoff/operator-approval.js +34 -1
- package/dist/handoff/operator-approval.js.map +1 -1
- package/dist/handoff/trade-platform-rules.d.ts +26 -0
- package/dist/handoff/trade-platform-rules.js +95 -0
- package/dist/handoff/trade-platform-rules.js.map +1 -0
- package/dist/handoff/transaction.d.ts +5 -0
- package/dist/handoff/transaction.js +7 -1
- package/dist/handoff/transaction.js.map +1 -1
- package/dist/handoff/ucp-checkout/channel.d.ts +14 -0
- package/dist/handoff/ucp-checkout/channel.js +87 -1
- package/dist/handoff/ucp-checkout/channel.js.map +1 -1
- package/dist/handoff/ucp-checkout/types.d.ts +1 -1
- package/dist/handoff/ucp-checkout/types.js +3 -0
- package/dist/handoff/ucp-checkout/types.js.map +1 -1
- package/dist/handoff/url-safety.js +8 -1
- package/dist/handoff/url-safety.js.map +1 -1
- package/dist/http/bounded-ip-rate-limit.js +16 -2
- package/dist/http/bounded-ip-rate-limit.js.map +1 -1
- package/dist/http/merchant-management/api.d.ts +20 -0
- package/dist/http/merchant-management/api.js +70 -5
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/page.js +200 -38
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/http/merchant-management/trusted-page.js +5 -3
- package/dist/http/merchant-management/trusted-page.js.map +1 -1
- package/dist/http/merchant-privacy-api.js +24 -0
- package/dist/http/merchant-privacy-api.js.map +1 -1
- package/dist/http/merchant-server.js +13 -4
- package/dist/http/merchant-server.js.map +1 -1
- package/dist/http/server.js +25 -5
- package/dist/http/server.js.map +1 -1
- package/dist/mcp/cli.d.ts +2 -0
- package/dist/mcp/cli.js +11 -0
- package/dist/mcp/cli.js.map +1 -1
- package/dist/mcp/merchant-server.js +8 -6
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/merchant/application/service.d.ts +2 -1
- package/dist/merchant/application/service.js.map +1 -1
- package/dist/merchant/decision-backend.js +24 -2
- package/dist/merchant/decision-backend.js.map +1 -1
- package/dist/merchant/negotiation-observer.js +12 -2
- package/dist/merchant/negotiation-observer.js.map +1 -1
- package/dist/merchant/watch/detect.js +14 -0
- package/dist/merchant/watch/detect.js.map +1 -1
- package/dist/merchant/watch/weixin.js +7 -1
- package/dist/merchant/watch/weixin.js.map +1 -1
- package/dist/merchant-core/commands.d.ts +5 -0
- package/dist/merchant-core/commands.js +19 -0
- package/dist/merchant-core/commands.js.map +1 -1
- package/dist/merchant-core/executor.js +3 -2
- package/dist/merchant-core/executor.js.map +1 -1
- package/dist/merchant-core/operating/service.js +9 -0
- package/dist/merchant-core/operating/service.js.map +1 -1
- package/dist/merchant-core/operations.js +18 -4
- package/dist/merchant-core/operations.js.map +1 -1
- package/dist/merchant-core/opportunity/service.js +9 -0
- package/dist/merchant-core/opportunity/service.js.map +1 -1
- package/dist/merchant-core/orders/repository.js +8 -0
- package/dist/merchant-core/orders/repository.js.map +1 -1
- package/dist/merchant-core/product-import.d.ts +9 -0
- package/dist/merchant-core/product-import.js +17 -3
- package/dist/merchant-core/product-import.js.map +1 -1
- package/dist/merchant-core/proposal/service.js +9 -0
- package/dist/merchant-core/proposal/service.js.map +1 -1
- package/dist/merchant-core/rfq/delivery.js +1 -1
- package/dist/merchant-core/rfq/delivery.js.map +1 -1
- package/dist/merchant-core/rfq/handoff.js +2 -3
- package/dist/merchant-core/rfq/handoff.js.map +1 -1
- package/dist/merchant-core/rfq/policy.d.ts +0 -8
- package/dist/merchant-core/rfq/policy.js +6 -1
- package/dist/merchant-core/rfq/policy.js.map +1 -1
- package/dist/merchant-core/rfq/repository.d.ts +2 -0
- package/dist/merchant-core/rfq/repository.js +10 -1
- package/dist/merchant-core/rfq/repository.js.map +1 -1
- package/dist/merchant-core/service.js +21 -3
- package/dist/merchant-core/service.js.map +1 -1
- package/dist/merchant-gateway/credential-vault.d.ts +3 -0
- package/dist/merchant-gateway/credential-vault.js +54 -10
- package/dist/merchant-gateway/credential-vault.js.map +1 -1
- package/dist/merchant-gateway/entry-server.js +13 -3
- package/dist/merchant-gateway/entry-server.js.map +1 -1
- package/dist/merchant-runtime/manager.d.ts +2 -0
- package/dist/merchant-runtime/manager.js +49 -1
- package/dist/merchant-runtime/manager.js.map +1 -1
- package/dist/negotiation/ledger/store.js +6 -3
- package/dist/negotiation/ledger/store.js.map +1 -1
- package/dist/net/client-ip.d.ts +29 -0
- package/dist/net/client-ip.js +39 -0
- package/dist/net/client-ip.js.map +1 -0
- package/dist/net/safe-http.d.ts +1 -1
- package/dist/net/safe-http.js +8 -0
- package/dist/net/safe-http.js.map +1 -1
- package/dist/operator/controller.js +3 -1
- package/dist/operator/controller.js.map +1 -1
- package/dist/operator/runner.js +1 -1
- package/dist/operator/runner.js.map +1 -1
- package/dist/operator/tui.js +10 -3
- package/dist/operator/tui.js.map +1 -1
- package/dist/product-buyer.js +1 -0
- package/dist/product-buyer.js.map +1 -1
- package/dist/product-cli.js +2 -1
- package/dist/product-cli.js.map +1 -1
- package/dist/protocol/legacy-shopping-negotiation/adapter.js +9 -0
- package/dist/protocol/legacy-shopping-negotiation/adapter.js.map +1 -1
- package/dist/runtime/bounded-memory.d.ts +25 -0
- package/dist/runtime/bounded-memory.js +60 -0
- package/dist/runtime/bounded-memory.js.map +1 -0
- package/dist/runtime/fake-model.js +7 -1
- package/dist/runtime/fake-model.js.map +1 -1
- package/dist/runtime/lock-owner.d.ts +36 -0
- package/dist/runtime/lock-owner.js +98 -0
- package/dist/runtime/lock-owner.js.map +1 -0
- package/dist/runtime/locks-cli.d.ts +21 -0
- package/dist/runtime/locks-cli.js +94 -0
- package/dist/runtime/locks-cli.js.map +1 -0
- package/dist/runtime/locks.d.ts +60 -0
- package/dist/runtime/locks.js +202 -0
- package/dist/runtime/locks.js.map +1 -0
- package/dist/runtime/negotiation-turn.js +1 -1
- package/dist/runtime/negotiation-turn.js.map +1 -1
- package/dist/trust/identity/auth-verifier.d.ts +23 -21
- package/dist/trust/identity/auth-verifier.js +65 -1
- package/dist/trust/identity/auth-verifier.js.map +1 -1
- package/dist/trust/identity/jwk.d.ts +1 -0
- package/dist/trust/identity/signature-base.js +3 -1
- package/dist/trust/identity/signature-base.js.map +1 -1
- package/dist/trust/records/store.d.ts +7 -1
- package/dist/trust/records/store.js +44 -14
- package/dist/trust/records/store.js.map +1 -1
- package/dist/weixin/channel.js +11 -1
- package/dist/weixin/channel.js.map +1 -1
- package/package.json +5 -3
|
@@ -61,6 +61,7 @@ import { validateAgentCard } from "../discovery/agent-card/validate.js";
|
|
|
61
61
|
import { isRedirectResponse, readJsonBody } from "../net/safe-http.js";
|
|
62
62
|
import { CatalogClient, CatalogClientError, normalizeOrigin, runtimePublicKey, } from "./catalog-client.js";
|
|
63
63
|
import { assertShareLinkMatchesRuntimeOrigin, buildWorkbuddyAccessConfig, buildWorkbuddyAccessRevocation, parseWorkbuddyShareLink, WORKBUDDY_CUSTODY_TERMS_VERSION, WorkbuddyShareLinkError, } from "./access/workbuddy-share-link.js";
|
|
64
|
+
import { readAccessMode, writeAccessMode, } from "./access/access-mode.js";
|
|
64
65
|
import { enrollmentStorePath, readEnrollmentStore as readEnrollmentStoreRaw, } from "./binding/enrollment-challenge.js";
|
|
65
66
|
const observedSession = Symbol("connect-observed-session");
|
|
66
67
|
const sessionDigest = (item) => createHash("sha256")
|
|
@@ -76,6 +77,24 @@ function readEnrollmentStore(dataDir) {
|
|
|
76
77
|
});
|
|
77
78
|
return store;
|
|
78
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* 确定性拒绝(4xx/本地校验失败)已证明**未产生远端副作用**,可以安全释放
|
|
82
|
+
* operation_claim;其余(网络/5xx/未归类)保持 unknown,交给显式恢复
|
|
83
|
+
* (review P1-7:此前所有失败都被打成 unknown,一次 grant 过期即可让会话
|
|
84
|
+
* 永久卡死)。
|
|
85
|
+
*/
|
|
86
|
+
export const DETERMINISTIC_CONNECT_RELEASE_CODES = new Set([
|
|
87
|
+
"BIND_REJECTED",
|
|
88
|
+
"BIND_CLAIM_INVALID",
|
|
89
|
+
"CARD_PUBLISH_REJECTED",
|
|
90
|
+
"CARD_PUBLISH_INVALID",
|
|
91
|
+
"PUBLICATION_RECEIPT_INVALID",
|
|
92
|
+
"ACTIVATION_RECEIPT_INVALID",
|
|
93
|
+
"STATE_CAS_CONFLICT",
|
|
94
|
+
]);
|
|
95
|
+
export function isDeterministicConnectRelease(error) {
|
|
96
|
+
return error instanceof MerchantConnectError && DETERMINISTIC_CONNECT_RELEASE_CODES.has(error.code);
|
|
97
|
+
}
|
|
79
98
|
export class MerchantConnectError extends Error {
|
|
80
99
|
code;
|
|
81
100
|
constructor(code, message) {
|
|
@@ -102,6 +121,8 @@ export const CONNECTION_PAIRING_SAFE_CODES = new Set([
|
|
|
102
121
|
"RUNTIME_UNREACHABLE",
|
|
103
122
|
"RUNTIME_NOT_READY",
|
|
104
123
|
"RUNTIME_IDENTITY_MISMATCH",
|
|
124
|
+
"ACCESS_MODE_UNCERTAIN",
|
|
125
|
+
"ACCESS_STATE_UNAVAILABLE",
|
|
105
126
|
"CATALOG_RUNTIME_NOT_READY",
|
|
106
127
|
"STATE_CAS_CONFLICT",
|
|
107
128
|
"CONNECTION_OPERATION_PENDING",
|
|
@@ -186,8 +207,20 @@ export function priorPublishedCatalogAgentId(sessions) {
|
|
|
186
207
|
return ids.values().next().value;
|
|
187
208
|
}
|
|
188
209
|
/** 会话里的脱敏投影 → 工作台视图;没有记录即"未配置"。 */
|
|
189
|
-
function accessSummaryOf(session) {
|
|
210
|
+
function accessSummaryOf(session, modeState, detection, syncError) {
|
|
190
211
|
const record = session?.runtime_access;
|
|
212
|
+
const bound = session !== undefined && session.binding_id !== undefined;
|
|
213
|
+
const catalog = modeState.catalog;
|
|
214
|
+
const catalogSynced = catalog !== undefined && catalog.mode === modeState.mode;
|
|
215
|
+
const catalogSync = !bound
|
|
216
|
+
? "unbound"
|
|
217
|
+
: syncError === "UNSUPPORTED"
|
|
218
|
+
? "unsupported"
|
|
219
|
+
: syncError !== null
|
|
220
|
+
? "failed"
|
|
221
|
+
: catalogSynced
|
|
222
|
+
? "synced"
|
|
223
|
+
: "pending";
|
|
191
224
|
return {
|
|
192
225
|
configured: record?.status === "active",
|
|
193
226
|
status: record?.status ?? "none",
|
|
@@ -202,8 +235,34 @@ function accessSummaryOf(session) {
|
|
|
202
235
|
statusReason: record?.status_reason ?? "",
|
|
203
236
|
recentFaults: record?.recent_faults ?? 0,
|
|
204
237
|
recentFaultsLastAt: record?.recent_faults_last_at ?? "",
|
|
238
|
+
mode: modeState.mode,
|
|
239
|
+
modePersisted: modeState.persisted,
|
|
240
|
+
modeInvalid: modeState.invalid,
|
|
241
|
+
detection,
|
|
242
|
+
catalogMode: catalog?.mode ?? null,
|
|
243
|
+
catalogRevision: catalog?.revision ?? null,
|
|
244
|
+
catalogSynced,
|
|
245
|
+
catalogSync,
|
|
246
|
+
catalogSyncError: syncError,
|
|
205
247
|
};
|
|
206
248
|
}
|
|
249
|
+
/** 接入方式同步失败 → 稳定码(不含 URL/分享码/远端正文)。 */
|
|
250
|
+
function accessSyncErrorCode(error) {
|
|
251
|
+
if (error instanceof CatalogClientError) {
|
|
252
|
+
if (error.code === "NOT_FOUND")
|
|
253
|
+
return "UNSUPPORTED";
|
|
254
|
+
if (error.code === "CATALOG_UNREACHABLE")
|
|
255
|
+
return "UNREACHABLE";
|
|
256
|
+
if (error.code === "CONFLICT")
|
|
257
|
+
return "CONFLICT";
|
|
258
|
+
if (error.code === "REQUEST_REJECTED")
|
|
259
|
+
return "REJECTED";
|
|
260
|
+
if (error.code === "RESPONSE_INVALID")
|
|
261
|
+
return "RESPONSE_INVALID";
|
|
262
|
+
return error.code;
|
|
263
|
+
}
|
|
264
|
+
return "SYNC_FAILED";
|
|
265
|
+
}
|
|
207
266
|
/**
|
|
208
267
|
* 访问配置写失败 → 工作台安全码。绝不携带分享码、完整带码 URL 或远端原文:
|
|
209
268
|
* 只保留自有码与一句可展示的说明。
|
|
@@ -233,8 +292,12 @@ function cardDigest(card) {
|
|
|
233
292
|
.update(canonicalize(card), "utf8")
|
|
234
293
|
.digest("hex")}`;
|
|
235
294
|
}
|
|
236
|
-
/**
|
|
237
|
-
|
|
295
|
+
/**
|
|
296
|
+
* 只对调用方给定的 origin 发一次 GET;固定 well-known 路径,`redirect: "manual"`
|
|
297
|
+
* 且不跟随 3xx,`credentials: "omit"`,响应体读取受同一 AbortController 与大小
|
|
298
|
+
* 上限保护。`shareSecret` 只会追加到本 origin 的固定路径,不参与任何重定向目标。
|
|
299
|
+
*/
|
|
300
|
+
async function requestPublicCard(origin, fetchImpl, shareSecret) {
|
|
238
301
|
const controller = new AbortController();
|
|
239
302
|
const timer = setTimeout(() => controller.abort(), 8_000);
|
|
240
303
|
const url = new URL(`${origin}/.well-known/agent-card.json`);
|
|
@@ -244,40 +307,115 @@ async function fetchPublicCard(origin, expectedEndpoint, identity, fetchImpl, sh
|
|
|
244
307
|
const response = await fetchImpl(url.href, {
|
|
245
308
|
method: "GET",
|
|
246
309
|
redirect: "manual",
|
|
310
|
+
credentials: "omit",
|
|
247
311
|
signal: controller.signal,
|
|
248
312
|
headers: { accept: "application/json" },
|
|
249
313
|
});
|
|
250
|
-
if (isRedirectResponse(response)
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
if (signatureScheme !== undefined &&
|
|
262
|
-
(signatureScheme.type !== "kiwi-http-message-signature" ||
|
|
263
|
-
signatureScheme.keyid !== identity.keyid ||
|
|
264
|
-
signatureScheme.publicKeyPem !== identity.publicKeyPem ||
|
|
265
|
-
signatureScheme.algorithm !== "ed25519")) {
|
|
266
|
-
throw new MerchantConnectError("RUNTIME_IDENTITY_MISMATCH", "公网服务的 A2A 签名声明与当前 Runtime 持久密钥不一致;未发布名片。请在实际承接请求的 Runtime 主机上运行连接命令。");
|
|
267
|
-
}
|
|
268
|
-
return card;
|
|
314
|
+
if (isRedirectResponse(response))
|
|
315
|
+
return { kind: "redirect", status: response.status };
|
|
316
|
+
if (!response.ok)
|
|
317
|
+
return { kind: "http", status: response.status };
|
|
318
|
+
try {
|
|
319
|
+
const card = validateAgentCard(await readJsonBody(response, { signal: controller.signal }));
|
|
320
|
+
return { kind: "ok", card };
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
return { kind: "invalid", code: "response" };
|
|
324
|
+
}
|
|
269
325
|
}
|
|
270
|
-
catch
|
|
271
|
-
|
|
272
|
-
throw err;
|
|
273
|
-
throw new MerchantConnectError("RUNTIME_UNREACHABLE", shareSecret === undefined
|
|
274
|
-
? `暂时无法从外部连接你的商家服务(${err instanceof Error ? err.message : String(err)})。请确认服务已启动、TLS 证书有效且允许公网访问,然后重试。`
|
|
275
|
-
: "暂时无法通过 WorkBuddy 分享链接读取商家名片。请核对分享链接与服务状态后重试。");
|
|
326
|
+
catch {
|
|
327
|
+
return { kind: "invalid", code: controller.signal.aborted ? "timeout" : "network" };
|
|
276
328
|
}
|
|
277
329
|
finally {
|
|
278
330
|
clearTimeout(timer);
|
|
279
331
|
}
|
|
280
332
|
}
|
|
333
|
+
/** 校验公开卡与当前 Runtime 地址/签名身份一致;不涉及平台加白/门禁结论。 */
|
|
334
|
+
function assertPublicCard(card, origin, expectedEndpoint, identity) {
|
|
335
|
+
const urls = card.supportedInterfaces.map((entry) => entry.url);
|
|
336
|
+
if (typeof card.url !== "string" ||
|
|
337
|
+
new URL(card.url).origin !== origin ||
|
|
338
|
+
!urls.includes(expectedEndpoint)) {
|
|
339
|
+
throw new MerchantConnectError("RUNTIME_NOT_READY", "公网入口返回的 Agent Card 与当前 Runtime 地址不一致;未发布名片。请检查反向代理与服务配置后重试。");
|
|
340
|
+
}
|
|
341
|
+
const signatureScheme = card.securitySchemes?.["kiwi-signature"];
|
|
342
|
+
if (signatureScheme !== undefined &&
|
|
343
|
+
(signatureScheme.type !== "kiwi-http-message-signature" ||
|
|
344
|
+
signatureScheme.keyid !== identity.keyid ||
|
|
345
|
+
signatureScheme.publicKeyPem !== identity.publicKeyPem ||
|
|
346
|
+
signatureScheme.algorithm !== "ed25519")) {
|
|
347
|
+
throw new MerchantConnectError("RUNTIME_IDENTITY_MISMATCH", "公网服务的 A2A 签名声明与当前 Runtime 持久密钥不一致;未发布名片。请在实际承接请求的 Runtime 主机上运行连接命令。");
|
|
348
|
+
}
|
|
349
|
+
return card;
|
|
350
|
+
}
|
|
351
|
+
/** 门禁拒绝/异常响应的统一安全提示:不推断“未加白”,只给可执行下一步。 */
|
|
352
|
+
function uncertainAccessError(mode, shareAttempted) {
|
|
353
|
+
return new MerchantConnectError("ACCESS_MODE_UNCERTAIN", `暂时无法确认本实例的接入方式:本次公开读取${shareAttempted ? "(含已暂存分享码)" : ""}未被接受。` +
|
|
354
|
+
"单次 401/403、网络错误或不完整响应都不能判定实例是否加白。" +
|
|
355
|
+
(mode === "share_code"
|
|
356
|
+
? "请核对分享码是否已在平台侧更换或失效,然后重新提交分享链接。"
|
|
357
|
+
: mode === "no_share_code"
|
|
358
|
+
? "当前选择「无分享码」;如平台门禁确实要求分享码,请提交本实例分享链接后切换为自动检测或使用分享码。"
|
|
359
|
+
: "请先确认服务已启动、公网入口与 TLS 正常;如平台门禁确实拦截,可由商家提交本实例分享链接后重试。") +
|
|
360
|
+
"目录里已保存的分享码不会被删除。");
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* 把一次探测失败收敛成稳定错误。只有 401/403 是门禁信号但仍不足以判定
|
|
364
|
+
* 加白,所以统一给 `ACCESS_MODE_UNCERTAIN`;其它错误沿用原有运行时就绪码。
|
|
365
|
+
*/
|
|
366
|
+
function probeFailure(probe, mode, shareAttempted) {
|
|
367
|
+
if (probe.kind === "http") {
|
|
368
|
+
if (probe.status === 401 || probe.status === 403)
|
|
369
|
+
return uncertainAccessError(mode, shareAttempted);
|
|
370
|
+
return new MerchantConnectError("RUNTIME_UNREACHABLE", `暂时无法从外部读取商家服务的 Agent Card(HTTP ${probe.status})。请确认服务已启动且公网入口可访问,然后重试。`);
|
|
371
|
+
}
|
|
372
|
+
if (probe.kind === "redirect") {
|
|
373
|
+
return new MerchantConnectError("RUNTIME_UNREACHABLE", "公网入口返回了重定向;为避免把请求或凭据带到未校验的目标,已停止。请检查反向代理配置后重试。");
|
|
374
|
+
}
|
|
375
|
+
if (probe.code === "response") {
|
|
376
|
+
return new MerchantConnectError("RUNTIME_NOT_READY", "公网入口返回的 Agent Card 不完整或不合法;未发布名片。该结果不能用于判断平台加白状态,请检查服务响应后重试。");
|
|
377
|
+
}
|
|
378
|
+
if (shareAttempted) {
|
|
379
|
+
return new MerchantConnectError("RUNTIME_UNREACHABLE", "暂时无法通过 WorkBuddy 分享链接读取商家名片。请核对分享链接与服务状态后重试。");
|
|
380
|
+
}
|
|
381
|
+
return new MerchantConnectError("RUNTIME_UNREACHABLE", probe.code === "timeout"
|
|
382
|
+
? "读取商家公开名片超时。请确认服务已启动、TLS 证书有效且允许公网访问,然后重试。"
|
|
383
|
+
: "暂时无法从外部连接你的商家服务。请确认服务已启动、TLS 证书有效且允许公网访问,然后重试。");
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* 缺省公开卡加载:与 CLI verifyPublicRuntimeReady 同口径(匿名读 + 签名声明
|
|
387
|
+
* 核对)。接入方式只影响“是否/何时追加分享码”,不放宽地址、身份或签名校验。
|
|
388
|
+
*
|
|
389
|
+
* - `share_code`:显式带码读取;没有码就在任何网络动作前失败。
|
|
390
|
+
* - `no_share_code`:只做无码读取;即使本进程有码也不发送。
|
|
391
|
+
* - `auto`:先无码读取;只有明确 401/403 且本进程有已授权分享码时才带码
|
|
392
|
+
* 重试一次。网络错误/5xx/重定向/坏响应不拿分享码去试探。
|
|
393
|
+
*/
|
|
394
|
+
async function fetchPublicCard(origin, expectedEndpoint, identity, fetchImpl, shareSecret, mode) {
|
|
395
|
+
const verify = (probe, shareAttempted) => {
|
|
396
|
+
if (probe.kind === "ok")
|
|
397
|
+
return assertPublicCard(probe.card, origin, expectedEndpoint, identity);
|
|
398
|
+
throw probeFailure(probe, mode, shareAttempted);
|
|
399
|
+
};
|
|
400
|
+
if (mode === "share_code") {
|
|
401
|
+
if (shareSecret === undefined) {
|
|
402
|
+
throw new MerchantConnectError("ACCESS_SHARE_LINK_REQUIRED", "已选择「使用分享码」,但本进程没有可用的分享码。请由商家重新提交本实例的分享链接;目录里已保存的分享码不会被删除。");
|
|
403
|
+
}
|
|
404
|
+
return verify(await requestPublicCard(origin, fetchImpl, shareSecret), true);
|
|
405
|
+
}
|
|
406
|
+
if (mode === "no_share_code") {
|
|
407
|
+
return verify(await requestPublicCard(origin, fetchImpl), false);
|
|
408
|
+
}
|
|
409
|
+
const direct = await requestPublicCard(origin, fetchImpl);
|
|
410
|
+
if (direct.kind === "ok")
|
|
411
|
+
return verify(direct, false);
|
|
412
|
+
if (direct.kind === "http" &&
|
|
413
|
+
(direct.status === 401 || direct.status === 403) &&
|
|
414
|
+
shareSecret !== undefined) {
|
|
415
|
+
return verify(await requestPublicCard(origin, fetchImpl, shareSecret), true);
|
|
416
|
+
}
|
|
417
|
+
return verify(direct, false);
|
|
418
|
+
}
|
|
281
419
|
class ConnectionServiceImpl {
|
|
282
420
|
dataDir;
|
|
283
421
|
origin;
|
|
@@ -289,6 +427,7 @@ class ConnectionServiceImpl {
|
|
|
289
427
|
identity;
|
|
290
428
|
keyThumbprint;
|
|
291
429
|
now;
|
|
430
|
+
fetchImpl;
|
|
292
431
|
loadPublicCard;
|
|
293
432
|
beforePublish;
|
|
294
433
|
/**
|
|
@@ -313,6 +452,10 @@ class ConnectionServiceImpl {
|
|
|
313
452
|
*/
|
|
314
453
|
publicationVerified = false;
|
|
315
454
|
availability = null;
|
|
455
|
+
/** 本进程最近一次显式检测;不落盘,避免把过时结论当成当前事实。 */
|
|
456
|
+
lastDetection = null;
|
|
457
|
+
/** 最近一次接入方式同步失败的稳定码(不落盘;重启后重新对账)。 */
|
|
458
|
+
lastAccessSyncError = null;
|
|
316
459
|
constructor(options) {
|
|
317
460
|
this.dataDir = options.dataDir;
|
|
318
461
|
this.generation = options.generation ?? 1;
|
|
@@ -392,21 +535,21 @@ class ConnectionServiceImpl {
|
|
|
392
535
|
};
|
|
393
536
|
this.keyThumbprint = runtimePublicKey(this.identity).keyThumbprint;
|
|
394
537
|
this.now = options.now ?? (() => new Date());
|
|
395
|
-
|
|
538
|
+
this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
396
539
|
this.loadPublicCard =
|
|
397
540
|
options.loadPublicCard ??
|
|
398
541
|
(async () => await fetchPublicCard(this.origin, this.a2aEndpoint, {
|
|
399
542
|
keyid: this.identityRaw.keyid,
|
|
400
543
|
publicKeyPem: this.identityRaw.publicKeyPem,
|
|
401
|
-
}, fetchImpl, this.firstBindAccessConfig?.secret_value));
|
|
544
|
+
}, this.fetchImpl, this.firstBindAccessConfig?.secret_value, readAccessMode(this.dataDir).mode));
|
|
402
545
|
this.beforePublish = options.beforePublish;
|
|
403
546
|
}
|
|
404
547
|
// ── 公开接口 ─────────────────────────────────────────────────────────
|
|
405
548
|
async begin() {
|
|
406
549
|
return await this.withLock(async () => {
|
|
407
|
-
if (
|
|
550
|
+
if (readAccessMode(this.dataDir).mode === "share_code" &&
|
|
408
551
|
this.firstBindAccessConfig === undefined) {
|
|
409
|
-
throw new MerchantConnectError("ACCESS_SHARE_LINK_REQUIRED", "
|
|
552
|
+
throw new MerchantConnectError("ACCESS_SHARE_LINK_REQUIRED", "已选择「使用分享码」,但本实例还没有可用的分享码。请先提交并授权本实例的 WorkBuddy 分享链接,再发起目录连接。");
|
|
410
553
|
}
|
|
411
554
|
const card = await this.loadPublicCard();
|
|
412
555
|
const currentDigest = cardDigest(card);
|
|
@@ -584,6 +727,8 @@ class ConnectionServiceImpl {
|
|
|
584
727
|
throw err;
|
|
585
728
|
}
|
|
586
729
|
if (current.status === "published") {
|
|
730
|
+
// 显式 reconcile(含用户操作)每次都核对 Catalog;后台轮询节流放在
|
|
731
|
+
// bootstrap 的 tick 层(published 会话 60s 一次,onboarding 仍 5s)。
|
|
587
732
|
try {
|
|
588
733
|
await this.assertPublicationActive(current);
|
|
589
734
|
}
|
|
@@ -734,7 +879,12 @@ class ConnectionServiceImpl {
|
|
|
734
879
|
// ── WorkBuddy 访问配置(M-02/M-03/M-04)────────────────────────────
|
|
735
880
|
getAccessSummary() {
|
|
736
881
|
const session = this.selectSession(readEnrollmentStore(this.dataDir));
|
|
737
|
-
const
|
|
882
|
+
const modeState = readAccessMode(this.dataDir);
|
|
883
|
+
// 检测结论只在同一模式内有意义;模式切换后不把旧结论当新选择的结果。
|
|
884
|
+
const detection = this.lastDetection !== null && this.lastDetection.mode === modeState.mode
|
|
885
|
+
? this.lastDetection
|
|
886
|
+
: null;
|
|
887
|
+
const summary = accessSummaryOf(session, modeState, detection, this.lastAccessSyncError);
|
|
738
888
|
if (this.localShareInvalid)
|
|
739
889
|
return { ...summary, configured: false, status: "invalid", statusReason: "local_vault_invalid" };
|
|
740
890
|
// 目录已有分享码、随绑凭据被按版本冲突跳过:不能显示成"已加密暂存,继续
|
|
@@ -745,24 +895,135 @@ class ConnectionServiceImpl {
|
|
|
745
895
|
return { ...summary, configured: false, status: "invalid", statusReason: "catalog_access_exists" };
|
|
746
896
|
}
|
|
747
897
|
if (this.firstBindAccessConfig !== undefined && summary.status === "none") {
|
|
898
|
+
if (modeState.mode === "no_share_code") {
|
|
899
|
+
// 本地仍保留已同意的分享码,但当前模式明确不使用:不能显示成
|
|
900
|
+
// “继续连接即会生效”,也不能让首次绑定悄悄把它提交给目录。
|
|
901
|
+
return { ...summary, status: "none", statusReason: "local_saved_not_used",
|
|
902
|
+
consentGranted: true,
|
|
903
|
+
consentTermsVersion: this.firstBindAccessConfig.consent.terms_version };
|
|
904
|
+
}
|
|
748
905
|
return { ...summary, status: "pending", statusReason: "local_staged", consentGranted: true,
|
|
749
906
|
consentTermsVersion: this.firstBindAccessConfig.consent.terms_version };
|
|
750
907
|
}
|
|
751
908
|
return summary;
|
|
752
909
|
}
|
|
910
|
+
getAccessMode() {
|
|
911
|
+
return readAccessMode(this.dataDir);
|
|
912
|
+
}
|
|
913
|
+
async setAccessMode(mode) {
|
|
914
|
+
return await this.withLock(async () => {
|
|
915
|
+
const previous = readAccessMode(this.dataDir);
|
|
916
|
+
try {
|
|
917
|
+
// 只写模式文件(保留已有目录 revision);绝不调用 Catalog 撤销/更新
|
|
918
|
+
// runtime-access,也不清 firstBindAccessConfig / shareVault。
|
|
919
|
+
writeAccessMode(this.dataDir, mode, previous.catalog);
|
|
920
|
+
}
|
|
921
|
+
catch {
|
|
922
|
+
throw new MerchantConnectError("ACCESS_STATE_UNAVAILABLE", "接入方式未能保存;请刷新后重试。已保存的分享码不会被删除。");
|
|
923
|
+
}
|
|
924
|
+
// 旧检测结论不再代表新选择,清掉以免误导。
|
|
925
|
+
this.lastDetection = null;
|
|
926
|
+
// 经授权把选择同步到目录的公开路由投影;失败不回滚本地选择,页面会
|
|
927
|
+
// 明确显示“目录尚未同步”,买家继续按旧状态路由。
|
|
928
|
+
await this.syncAccessModeUnlocked(mode, false);
|
|
929
|
+
return this.getAccessSummary();
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* 把本地接入方式对账到目录公开路由投影(必须已持有 withLock)。
|
|
934
|
+
*
|
|
935
|
+
* - `adoptRemote`:目录 revision 比本地已知更新且是另一台设备写入时,采用
|
|
936
|
+
* 目录状态(本地未同步的修改优先推送,不会被远端静默覆盖)。
|
|
937
|
+
* - 任何网络/权限失败都收敛为稳定码,不抛出、不影响本地已保存模式;绝不
|
|
938
|
+
* 触碰 runtime-access 或分享码。
|
|
939
|
+
*/
|
|
940
|
+
async syncAccessModeUnlocked(desiredMode, adoptRemote = false) {
|
|
941
|
+
let context;
|
|
942
|
+
try {
|
|
943
|
+
({ context } = this.requireAccessTarget());
|
|
944
|
+
}
|
|
945
|
+
catch {
|
|
946
|
+
this.lastAccessSyncError = "UNBOUND";
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
const modeState = readAccessMode(this.dataDir);
|
|
950
|
+
const mode = desiredMode ?? modeState.mode;
|
|
951
|
+
const localPending = modeState.catalog !== undefined && modeState.catalog.mode !== modeState.mode;
|
|
952
|
+
try {
|
|
953
|
+
const remote = await this.client.readBuyerAccess(context, this.identity);
|
|
954
|
+
if (remote.mode === mode) {
|
|
955
|
+
this.recordAccessSync(mode, remote.revision);
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
if (adoptRemote &&
|
|
959
|
+
!localPending &&
|
|
960
|
+
remote.mode !== null &&
|
|
961
|
+
(modeState.catalog === undefined || remote.revision > modeState.catalog.revision)) {
|
|
962
|
+
// 另一台设备在本机上次同步之后写入:以目录为准收敛本地,避免两台
|
|
963
|
+
// 设备各自反复覆盖。
|
|
964
|
+
this.recordAccessSync(remote.mode, remote.revision);
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
try {
|
|
968
|
+
const updated = await this.client.updateBuyerAccess(context, this.identity, mode, remote.revision);
|
|
969
|
+
this.recordAccessSync(mode, updated.revision);
|
|
970
|
+
}
|
|
971
|
+
catch (error) {
|
|
972
|
+
if (!(error instanceof CatalogClientError) || error.code !== "CONFLICT")
|
|
973
|
+
throw error;
|
|
974
|
+
// 并发更新:重读后只重试一次。若目录已经是目标模式则视为成功。
|
|
975
|
+
const head = await this.client.readBuyerAccess(context, this.identity);
|
|
976
|
+
if (head.mode === mode) {
|
|
977
|
+
this.recordAccessSync(mode, head.revision);
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
const updated = await this.client.updateBuyerAccess(context, this.identity, mode, head.revision);
|
|
981
|
+
this.recordAccessSync(mode, updated.revision);
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
catch (error) {
|
|
985
|
+
this.lastAccessSyncError = accessSyncErrorCode(error);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
recordAccessSync(mode, revision) {
|
|
989
|
+
try {
|
|
990
|
+
writeAccessMode(this.dataDir, mode, {
|
|
991
|
+
mode,
|
|
992
|
+
revision,
|
|
993
|
+
syncedAt: this.now().toISOString(),
|
|
994
|
+
});
|
|
995
|
+
this.lastAccessSyncError = null;
|
|
996
|
+
}
|
|
997
|
+
catch {
|
|
998
|
+
this.lastAccessSyncError = "LOCAL_WRITE_FAILED";
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
async detectAccess() {
|
|
1002
|
+
return await this.withLock(async () => {
|
|
1003
|
+
const result = await this.detectAccessUnlocked();
|
|
1004
|
+
this.lastDetection = result;
|
|
1005
|
+
return result;
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
753
1008
|
async refreshAccess() {
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
1009
|
+
// 与 setAccessMode / begin / reconcile 串行,避免一次刷新与一次保存交错
|
|
1010
|
+
// 后本地 mode 与目录 revision 指向不同版本。
|
|
1011
|
+
return await this.withLock(async () => {
|
|
1012
|
+
const current = this.selectSession(readEnrollmentStore(this.dataDir));
|
|
1013
|
+
if (current === undefined || (current.binding_id === undefined &&
|
|
1014
|
+
(current.status === "preparing" || current.status === "authorized")))
|
|
1015
|
+
return this.getAccessSummary();
|
|
1016
|
+
const { session, context } = this.requireAccessTarget();
|
|
1017
|
+
const { configVersion, runtimeAccess, custodyEnabled, consent, recentFaults } = await this.client.readRuntimeAccess(context, this.identity);
|
|
1018
|
+
this.recordAccess(session.enrollment_id, configVersion, runtimeAccess, {
|
|
1019
|
+
custodyEnabled,
|
|
1020
|
+
consent,
|
|
1021
|
+
recentFaults,
|
|
1022
|
+
});
|
|
1023
|
+
// 对账接入方式:目录更新则采纳,本地未同步则补推;失败只记状态。
|
|
1024
|
+
await this.syncAccessModeUnlocked(undefined, true);
|
|
757
1025
|
return this.getAccessSummary();
|
|
758
|
-
const { session, context } = this.requireAccessTarget();
|
|
759
|
-
const { configVersion, runtimeAccess, custodyEnabled, consent, recentFaults } = await this.client.readRuntimeAccess(context, this.identity);
|
|
760
|
-
this.recordAccess(session.enrollment_id, configVersion, runtimeAccess, {
|
|
761
|
-
custodyEnabled,
|
|
762
|
-
consent,
|
|
763
|
-
recentFaults,
|
|
764
1026
|
});
|
|
765
|
-
return this.getAccessSummary();
|
|
766
1027
|
}
|
|
767
1028
|
async submitAccessShareLink(rawLink, consentTermsVersion) {
|
|
768
1029
|
this.assertSignatureAuthMode();
|
|
@@ -850,6 +1111,13 @@ class ConnectionServiceImpl {
|
|
|
850
1111
|
this.localShareInvalid = false;
|
|
851
1112
|
return this.getAccessSummary();
|
|
852
1113
|
}
|
|
1114
|
+
async readBoundAccount() {
|
|
1115
|
+
const binding = await this.getVerifiedBinding();
|
|
1116
|
+
if (binding === null)
|
|
1117
|
+
throw new MerchantConnectError("STATE_INVALID", "尚未建立有效的 Catalog 绑定");
|
|
1118
|
+
return this.client.readBoundAccount({ agentId: binding.agentId, bindingId: binding.bindingId,
|
|
1119
|
+
keyId: this.identity.keyId, runtimeOrigin: this.origin, serviceEpoch: this.serviceEpoch }, this.identity);
|
|
1120
|
+
}
|
|
853
1121
|
async readPublicInquiryConsent() {
|
|
854
1122
|
return await this.withLock(async () => {
|
|
855
1123
|
const binding = await this.getVerifiedBinding();
|
|
@@ -883,6 +1151,76 @@ class ConnectionServiceImpl {
|
|
|
883
1151
|
return { ...remote, localEnabled: this.inquiryPolicy.isEnabled() };
|
|
884
1152
|
});
|
|
885
1153
|
}
|
|
1154
|
+
/**
|
|
1155
|
+
* 公开检测只面向构造时的已验证 publicOrigin;**不接受任何目标 URL**。
|
|
1156
|
+
* 每次只发 GET well-known,`redirect: "manual"`,分享码只在内存中已存在
|
|
1157
|
+
* 且明确观察到 401/403 时追加,绝不用探测去试探其它部署或绕过授权。
|
|
1158
|
+
*/
|
|
1159
|
+
async detectAccessUnlocked() {
|
|
1160
|
+
const modeState = readAccessMode(this.dataDir);
|
|
1161
|
+
const mode = modeState.mode;
|
|
1162
|
+
const shareCodeAvailable = this.firstBindAccessConfig !== undefined;
|
|
1163
|
+
const checkedAt = this.now().toISOString();
|
|
1164
|
+
const uncertainGuidance = "单次 401/403、网络错误、重定向或不完整响应都不能判定实例未加白。" +
|
|
1165
|
+
"请先确认服务已启动、公网入口与 TLS 正常;如仍被平台拦截,可提交本实例分享链接后重试,或稍后重新检测。" +
|
|
1166
|
+
"切换接入方式或重新检测都不会删除已保存的分享码。";
|
|
1167
|
+
const result = (status, code, httpStatus, detail, guidance) => ({
|
|
1168
|
+
status,
|
|
1169
|
+
code,
|
|
1170
|
+
checkedAt,
|
|
1171
|
+
mode,
|
|
1172
|
+
shareCodeAvailable,
|
|
1173
|
+
httpStatus,
|
|
1174
|
+
detail,
|
|
1175
|
+
guidance,
|
|
1176
|
+
});
|
|
1177
|
+
const cardMatches = (card) => {
|
|
1178
|
+
try {
|
|
1179
|
+
assertPublicCard(card, this.origin, this.a2aEndpoint, { keyid: this.identityRaw.keyid, publicKeyPem: this.identityRaw.publicKeyPem });
|
|
1180
|
+
return true;
|
|
1181
|
+
}
|
|
1182
|
+
catch {
|
|
1183
|
+
return false;
|
|
1184
|
+
}
|
|
1185
|
+
};
|
|
1186
|
+
const direct = await requestPublicCard(this.origin, this.fetchImpl);
|
|
1187
|
+
if (direct.kind === "ok") {
|
|
1188
|
+
if (!cardMatches(direct.card)) {
|
|
1189
|
+
return result("uncertain", "response_ambiguous", 200, "公开名片可读取,但与当前实例地址或签名身份不一致。", "该响应不足以判断平台加白状态;请检查反向代理、公开入口和签名密钥配置后重试。");
|
|
1190
|
+
}
|
|
1191
|
+
return result("direct_ok", "public_card_readable", 200, "本次无码读取公开名片成功。", "本次探测只证明无码可读取公开名片,不等于平台已加白,检测本身不改写已保存的接入方式或买家路由状态;如后续仍被门禁拦截,请重新检测或改用分享码。");
|
|
1192
|
+
}
|
|
1193
|
+
if (direct.kind === "http" && (direct.status === 401 || direct.status === 403)) {
|
|
1194
|
+
if (!shareCodeAvailable || mode === "no_share_code") {
|
|
1195
|
+
return result("uncertain", mode === "share_code" && !shareCodeAvailable ? "share_code_missing" : "gate_rejected", direct.status, `无码读取公开名片被拒绝(HTTP ${direct.status})。`, mode === "share_code"
|
|
1196
|
+
? "已选择「使用分享码」,但当前进程没有可用分享码。请由商家提交本实例分享链接后重试;目录里已保存的分享码不会被删除。"
|
|
1197
|
+
: mode === "no_share_code"
|
|
1198
|
+
? "当前选择「无分享码」,检测没有发送已保存的分享码。单次 401/403 不能判定实例未加白;如平台门禁确实要求分享码,请提交本实例分享链接后切换为自动检测或使用分享码。"
|
|
1199
|
+
: uncertainGuidance);
|
|
1200
|
+
}
|
|
1201
|
+
const withCode = await requestPublicCard(this.origin, this.fetchImpl, this.firstBindAccessConfig?.secret_value);
|
|
1202
|
+
if (withCode.kind === "ok") {
|
|
1203
|
+
if (!cardMatches(withCode.card)) {
|
|
1204
|
+
return result("uncertain", "response_ambiguous", 200, "带分享码读取到的公开名片与当前实例地址或签名身份不一致。", uncertainGuidance);
|
|
1205
|
+
}
|
|
1206
|
+
return result("share_code_ok", "sharecode_public_card_readable", 200, "无码读取被拒绝,但已暂存的分享码可以读取公开名片。", "本次探测只证明已保存的分享码可通过平台门禁;平台是否加白仍未确认。切换接入方式或重新检测都不会删除已保存的分享码。");
|
|
1207
|
+
}
|
|
1208
|
+
const codeStatus = withCode.kind === "http" ? withCode.status : null;
|
|
1209
|
+
return result("uncertain", "gate_rejected", codeStatus, `无码读取被拒绝(HTTP ${direct.status}),带分享码读取也未成功。`, "这可能是分享码已更换/失效、门禁形态变化或服务暂时异常,不能据此判定未加白。" +
|
|
1210
|
+
"请核对分享码后在下方重新提交,或稍后重新检测;已保存的分享码不会被删除。");
|
|
1211
|
+
}
|
|
1212
|
+
if (direct.kind === "redirect") {
|
|
1213
|
+
return result("uncertain", "unexpected_redirect", direct.status, `公开名片请求返回重定向(HTTP ${direct.status}),已停止且未跟随。`, "为避免把请求带到未校验目标,检测不会跟随重定向。请检查反向代理配置后重新检测。");
|
|
1214
|
+
}
|
|
1215
|
+
if (direct.kind === "invalid") {
|
|
1216
|
+
return result("uncertain", direct.code === "response" ? "response_ambiguous" : "network_error", null, direct.code === "timeout"
|
|
1217
|
+
? "公开名片请求超时。"
|
|
1218
|
+
: direct.code === "network"
|
|
1219
|
+
? "公开名片请求未能建立连接。"
|
|
1220
|
+
: "公开名片响应不完整或不合法。", uncertainGuidance);
|
|
1221
|
+
}
|
|
1222
|
+
return result("uncertain", "http_error", direct.status, `公开名片请求返回 HTTP ${direct.status}。`, uncertainGuidance);
|
|
1223
|
+
}
|
|
886
1224
|
/**
|
|
887
1225
|
* WorkBuddy 访问配置要求签名认证(G0 §1.1.3):平台会改写 Authorization,
|
|
888
1226
|
* bearer 身份不能当业务凭据。不静默降级,直接给出可执行的修复提示。
|
|
@@ -1021,6 +1359,10 @@ class ConnectionServiceImpl {
|
|
|
1021
1359
|
firstBindAccessForFreshBinding() {
|
|
1022
1360
|
if (this.firstBindAccessConfig === undefined)
|
|
1023
1361
|
return undefined;
|
|
1362
|
+
// 显式“无分享码”模式不得把已暂存的凭据随首次绑定提交给目录;凭据仍
|
|
1363
|
+
// 保留在本地,切换回 auto/share_code 后可继续使用,且不会被自动删除。
|
|
1364
|
+
if (readAccessMode(this.dataDir).mode === "no_share_code")
|
|
1365
|
+
return undefined;
|
|
1024
1366
|
const boundBefore = readEnrollmentStore(this.dataDir).sessions.some((item) => {
|
|
1025
1367
|
const previous = item;
|
|
1026
1368
|
return typeof previous.binding_id === "string" && previous.binding_id !== "" &&
|
|
@@ -1092,6 +1434,9 @@ class ConnectionServiceImpl {
|
|
|
1092
1434
|
: {}),
|
|
1093
1435
|
};
|
|
1094
1436
|
this.persist((fresh) => fresh.sessions.map((item) => (item.enrollment_id === session.enrollment_id ? bound : item)));
|
|
1437
|
+
// 绑定已持久化后补一次接入方式对账(旧目录 404 会记为 unsupported;
|
|
1438
|
+
// 失败绝不影响绑定/发布本身)。
|
|
1439
|
+
await this.syncAccessModeUnlocked(undefined, true);
|
|
1095
1440
|
// 绑定已持久化:发布交给 reconcile 的 bound 分支——失败阶段归类
|
|
1096
1441
|
// (CARD_PUBLISH_* + stage=publish)由该分支的阶段标记正确给出,
|
|
1097
1442
|
// 首配级联路径不再把发布失败误报成 BIND_REJECTED。
|
|
@@ -1458,16 +1803,22 @@ class ConnectionServiceImpl {
|
|
|
1458
1803
|
return session;
|
|
1459
1804
|
}));
|
|
1460
1805
|
let result;
|
|
1806
|
+
let deterministicRelease = false;
|
|
1807
|
+
let failure;
|
|
1461
1808
|
this.effectStarted = false;
|
|
1462
1809
|
try {
|
|
1463
1810
|
result = await step();
|
|
1464
|
-
return result;
|
|
1465
1811
|
}
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1812
|
+
catch (error) {
|
|
1813
|
+
failure = error;
|
|
1814
|
+
deterministicRelease = isDeterministicConnectRelease(error);
|
|
1815
|
+
}
|
|
1816
|
+
// Effect uncertainty is durable. Only read-only calls or known pre-effect
|
|
1817
|
+
// readiness failures release a claim automatically; no stale lease stealing.
|
|
1818
|
+
const safe = result !== undefined || readOnly || !this.effectStarted || deterministicRelease;
|
|
1819
|
+
this.preEffectReadinessFailure = false;
|
|
1820
|
+
let persistError;
|
|
1821
|
+
try {
|
|
1471
1822
|
this.persist((fresh) => fresh.sessions.map((item) => {
|
|
1472
1823
|
const live = item;
|
|
1473
1824
|
if (live.enrollment_id !== session.enrollment_id || live.operation_claim?.token !== token)
|
|
@@ -1477,7 +1828,7 @@ class ConnectionServiceImpl {
|
|
|
1477
1828
|
delete updated.operation_claim;
|
|
1478
1829
|
else
|
|
1479
1830
|
updated.operation_claim = { ...live.operation_claim, state: "unknown" };
|
|
1480
|
-
if (result) {
|
|
1831
|
+
if (result !== undefined) {
|
|
1481
1832
|
if (safe)
|
|
1482
1833
|
delete result.operation_claim;
|
|
1483
1834
|
Object.assign(result, updated);
|
|
@@ -1485,6 +1836,23 @@ class ConnectionServiceImpl {
|
|
|
1485
1836
|
return result ?? updated;
|
|
1486
1837
|
}));
|
|
1487
1838
|
}
|
|
1839
|
+
catch (error) {
|
|
1840
|
+
persistError = error;
|
|
1841
|
+
}
|
|
1842
|
+
// 审查 P2:清理写入抛错不得吞掉原始错误;没有原始错误时上抛(claim
|
|
1843
|
+
// 清理失败不能静默),有原始错误时保留原错误并把失败附在其上。
|
|
1844
|
+
if (failure !== undefined) {
|
|
1845
|
+
if (persistError !== undefined && failure !== null && typeof failure === "object") {
|
|
1846
|
+
Object.assign(failure, { persist_error: persistError });
|
|
1847
|
+
}
|
|
1848
|
+
throw failure;
|
|
1849
|
+
}
|
|
1850
|
+
if (persistError !== undefined)
|
|
1851
|
+
throw persistError;
|
|
1852
|
+
if (result === undefined) {
|
|
1853
|
+
throw new MerchantConnectError("CONNECTION_OPERATION_UNKNOWN", "connection step completed without a session");
|
|
1854
|
+
}
|
|
1855
|
+
return result;
|
|
1488
1856
|
}
|
|
1489
1857
|
preEffectReadinessFailure = false;
|
|
1490
1858
|
effectStarted = false;
|