@probemesh/sdk 0.1.0
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/LICENSE +22 -0
- package/README.md +160 -0
- package/dist/chunk-257Y7LN2.js +827 -0
- package/dist/chunk-257Y7LN2.js.map +1 -0
- package/dist/chunk-2ASMVLG4.js +56 -0
- package/dist/chunk-2ASMVLG4.js.map +1 -0
- package/dist/chunk-2KWGHJYP.js +285 -0
- package/dist/chunk-2KWGHJYP.js.map +1 -0
- package/dist/chunk-3H7UGVI6.js +1117 -0
- package/dist/chunk-3H7UGVI6.js.map +1 -0
- package/dist/chunk-5Q3PDYIA.js +657 -0
- package/dist/chunk-5Q3PDYIA.js.map +1 -0
- package/dist/chunk-CXZOO3U4.js +550 -0
- package/dist/chunk-CXZOO3U4.js.map +1 -0
- package/dist/chunk-FRFBK4SY.js +270 -0
- package/dist/chunk-FRFBK4SY.js.map +1 -0
- package/dist/chunk-HJK52QJF.js +994 -0
- package/dist/chunk-HJK52QJF.js.map +1 -0
- package/dist/chunk-HNBDX7IU.js +705 -0
- package/dist/chunk-HNBDX7IU.js.map +1 -0
- package/dist/chunk-NYTV263W.js +116 -0
- package/dist/chunk-NYTV263W.js.map +1 -0
- package/dist/chunk-PXG54XOG.js +595 -0
- package/dist/chunk-PXG54XOG.js.map +1 -0
- package/dist/chunk-TDOBAMYM.js +607 -0
- package/dist/chunk-TDOBAMYM.js.map +1 -0
- package/dist/chunk-TV42EZSI.js +2157 -0
- package/dist/chunk-TV42EZSI.js.map +1 -0
- package/dist/chunk-UU2ZG7P7.js +408 -0
- package/dist/chunk-UU2ZG7P7.js.map +1 -0
- package/dist/chunk-WKN7QOCA.js +977 -0
- package/dist/chunk-WKN7QOCA.js.map +1 -0
- package/dist/chunk-ZJOLPBJJ.js +1091 -0
- package/dist/chunk-ZJOLPBJJ.js.map +1 -0
- package/dist/cli/audit-trail-export.cjs +1193 -0
- package/dist/cli/audit-trail-export.cjs.map +1 -0
- package/dist/cli/audit-trail-export.d.cts +1 -0
- package/dist/cli/audit-trail-export.d.ts +1 -0
- package/dist/cli/audit-trail-export.js +24 -0
- package/dist/cli/audit-trail-export.js.map +1 -0
- package/dist/cli/catalog-check.cjs +2687 -0
- package/dist/cli/catalog-check.cjs.map +1 -0
- package/dist/cli/catalog-check.d.cts +1 -0
- package/dist/cli/catalog-check.d.ts +1 -0
- package/dist/cli/catalog-check.js +26 -0
- package/dist/cli/catalog-check.js.map +1 -0
- package/dist/cli/probemesh-init.cjs +1049 -0
- package/dist/cli/probemesh-init.cjs.map +1 -0
- package/dist/cli/probemesh-init.d.cts +1 -0
- package/dist/cli/probemesh-init.d.ts +1 -0
- package/dist/cli/probemesh-init.js +22 -0
- package/dist/cli/probemesh-init.js.map +1 -0
- package/dist/cli/provider-conformance.cjs +6180 -0
- package/dist/cli/provider-conformance.cjs.map +1 -0
- package/dist/cli/provider-conformance.d.cts +1 -0
- package/dist/cli/provider-conformance.d.ts +1 -0
- package/dist/cli/provider-conformance.js +29 -0
- package/dist/cli/provider-conformance.js.map +1 -0
- package/dist/cli/provider-dossier-check.cjs +2978 -0
- package/dist/cli/provider-dossier-check.cjs.map +1 -0
- package/dist/cli/provider-dossier-check.d.cts +1 -0
- package/dist/cli/provider-dossier-check.d.ts +1 -0
- package/dist/cli/provider-dossier-check.js +27 -0
- package/dist/cli/provider-dossier-check.js.map +1 -0
- package/dist/cli/provider-dossier.cjs +1753 -0
- package/dist/cli/provider-dossier.cjs.map +1 -0
- package/dist/cli/provider-dossier.d.cts +1 -0
- package/dist/cli/provider-dossier.d.ts +1 -0
- package/dist/cli/provider-dossier.js +26 -0
- package/dist/cli/provider-dossier.js.map +1 -0
- package/dist/cli/x402-accept.cjs +6009 -0
- package/dist/cli/x402-accept.cjs.map +1 -0
- package/dist/cli/x402-accept.d.cts +1 -0
- package/dist/cli/x402-accept.d.ts +1 -0
- package/dist/cli/x402-accept.js +28 -0
- package/dist/cli/x402-accept.js.map +1 -0
- package/dist/index.cjs +13671 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2026 -0
- package/dist/index.d.ts +2026 -0
- package/dist/index.js +2560 -0
- package/dist/index.js.map +1 -0
- package/package.json +111 -0
|
@@ -0,0 +1,1091 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertProviderCatalogArtifact,
|
|
3
|
+
mergePaymentPreferences,
|
|
4
|
+
resolveProviderPaymentOption,
|
|
5
|
+
validatePaymentPreferences,
|
|
6
|
+
validatePaymentStrategy,
|
|
7
|
+
validateProviderCatalogArtifact,
|
|
8
|
+
validateProviderPaymentOptions
|
|
9
|
+
} from "./chunk-3H7UGVI6.js";
|
|
10
|
+
import {
|
|
11
|
+
ProbeMeshError
|
|
12
|
+
} from "./chunk-2ASMVLG4.js";
|
|
13
|
+
|
|
14
|
+
// src/providerCatalog.ts
|
|
15
|
+
var PROVIDER_CATALOG_SCHEMA_VERSION = "probemesh.provider-catalog.index.v1";
|
|
16
|
+
function validateProviderCatalog(catalog) {
|
|
17
|
+
const errors = [];
|
|
18
|
+
if (!isRecord(catalog)) {
|
|
19
|
+
return {
|
|
20
|
+
valid: false,
|
|
21
|
+
errors: [
|
|
22
|
+
{
|
|
23
|
+
path: "$",
|
|
24
|
+
message: "Provider catalog must be an object."
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (catalog.schemaVersion !== PROVIDER_CATALOG_SCHEMA_VERSION) {
|
|
30
|
+
errors.push({
|
|
31
|
+
path: "schemaVersion",
|
|
32
|
+
message: `schemaVersion must be "${PROVIDER_CATALOG_SCHEMA_VERSION}".`
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
validateIsoTimestamp(catalog.generatedAt, "generatedAt", errors);
|
|
36
|
+
if (!Array.isArray(catalog.entries)) {
|
|
37
|
+
errors.push({
|
|
38
|
+
path: "entries",
|
|
39
|
+
message: "entries must be an array."
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
catalog.entries.forEach(
|
|
43
|
+
(entry, index) => validateProviderCatalogEntry(entry, `entries.${index}`, errors)
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
valid: errors.length === 0,
|
|
48
|
+
errors
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function assertProviderCatalog(catalog) {
|
|
52
|
+
const result = validateProviderCatalog(catalog);
|
|
53
|
+
if (result.valid) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
throw new ProbeMeshError({
|
|
57
|
+
code: "invalid_request",
|
|
58
|
+
message: `Invalid provider catalog: ${formatValidationErrors(result.errors)}`
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function parseProviderCatalogArtifactJson(json) {
|
|
62
|
+
if (typeof json !== "string") {
|
|
63
|
+
throw new ProbeMeshError({
|
|
64
|
+
code: "invalid_request",
|
|
65
|
+
message: "Provider catalog artifact JSON must be a string."
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
let parsed;
|
|
69
|
+
try {
|
|
70
|
+
parsed = JSON.parse(json);
|
|
71
|
+
} catch (cause) {
|
|
72
|
+
throw new ProbeMeshError({
|
|
73
|
+
code: "invalid_request",
|
|
74
|
+
message: "Provider catalog artifact JSON must be valid JSON.",
|
|
75
|
+
cause
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
assertProviderCatalogArtifact(parsed);
|
|
79
|
+
return parsed;
|
|
80
|
+
}
|
|
81
|
+
function createProviderCatalog(options) {
|
|
82
|
+
assertProviderCatalogOptions(options);
|
|
83
|
+
const entries = options.artifacts.map((artifact, index) => {
|
|
84
|
+
const validation = validateProviderCatalogArtifact(artifact);
|
|
85
|
+
if (!validation.valid) {
|
|
86
|
+
throw new ProbeMeshError({
|
|
87
|
+
code: "invalid_request",
|
|
88
|
+
message: `Invalid provider catalog artifact at index ${index}: ${formatValidationErrors(
|
|
89
|
+
validation.errors
|
|
90
|
+
)}`
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return createCatalogEntryFromArtifact(artifact);
|
|
94
|
+
});
|
|
95
|
+
const catalog = {
|
|
96
|
+
schemaVersion: PROVIDER_CATALOG_SCHEMA_VERSION,
|
|
97
|
+
generatedAt: options.generatedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
98
|
+
entries
|
|
99
|
+
};
|
|
100
|
+
assertProviderCatalog(catalog);
|
|
101
|
+
return catalog;
|
|
102
|
+
}
|
|
103
|
+
function findProviderCatalogMatches(catalog, query) {
|
|
104
|
+
assertProviderCatalog(catalog);
|
|
105
|
+
assertProviderCatalogQuery(query);
|
|
106
|
+
const requiredStatus = query.status ?? "ready";
|
|
107
|
+
const requiredReceiptTypes = query.requiredReceiptTypes ?? [];
|
|
108
|
+
const providerIds = query.providerIds;
|
|
109
|
+
const blockedProviderIds = new Set(query.blockedProviderIds ?? []);
|
|
110
|
+
return catalog.entries.flatMap((entry) => {
|
|
111
|
+
const providerId = entry.provider.id;
|
|
112
|
+
if (entry.status !== requiredStatus) {
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
if (!entry.provider.capabilities.includes(query.capability)) {
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
if (query.protocolMode !== void 0 && entry.provider.protocolMode !== query.protocolMode) {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
if (providerIds !== void 0 && !providerIds.includes(providerId)) {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
if (blockedProviderIds.has(providerId)) {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
const paymentPreferences = mergePaymentPreferences(
|
|
128
|
+
query.protocolMode ? {
|
|
129
|
+
allowedProtocols: [query.protocolMode]
|
|
130
|
+
} : void 0,
|
|
131
|
+
query.paymentPreferences
|
|
132
|
+
);
|
|
133
|
+
const paymentResolution = resolveProviderPaymentOption({
|
|
134
|
+
providerId,
|
|
135
|
+
providerMode: entry.provider.protocolMode,
|
|
136
|
+
capabilities: entry.provider.capabilities,
|
|
137
|
+
capability: query.capability,
|
|
138
|
+
pricing: entry.provider.pricing,
|
|
139
|
+
paymentOptions: entry.provider.paymentOptions,
|
|
140
|
+
paymentPreferences,
|
|
141
|
+
paymentStrategy: query.paymentStrategy,
|
|
142
|
+
defaultReceiptTypes: entry.acceptance.receipts.types
|
|
143
|
+
});
|
|
144
|
+
const cost = paymentSelectionCost(paymentResolution.selection) ?? resolveCatalogCost(entry, query.capability);
|
|
145
|
+
if (query.maxCostUsd !== void 0 && (cost === void 0 || cost.amountUsd > query.maxCostUsd)) {
|
|
146
|
+
return [];
|
|
147
|
+
}
|
|
148
|
+
if (!hasRequiredReceiptTypes(entry, requiredReceiptTypes)) {
|
|
149
|
+
return [];
|
|
150
|
+
}
|
|
151
|
+
if (!paymentResolution.selection) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
return [
|
|
155
|
+
{
|
|
156
|
+
entry,
|
|
157
|
+
providerId,
|
|
158
|
+
capability: query.capability,
|
|
159
|
+
status: entry.status,
|
|
160
|
+
cost,
|
|
161
|
+
paymentSelection: paymentResolution.selection,
|
|
162
|
+
reasons: createMatchReasons(
|
|
163
|
+
entry,
|
|
164
|
+
query,
|
|
165
|
+
cost,
|
|
166
|
+
paymentResolution.selection
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
];
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function summarizeProviderCatalog(catalog) {
|
|
173
|
+
assertProviderCatalog(catalog);
|
|
174
|
+
const providers = /* @__PURE__ */ new Set();
|
|
175
|
+
const capabilities = /* @__PURE__ */ new Set();
|
|
176
|
+
const protocolModes = /* @__PURE__ */ new Set();
|
|
177
|
+
let readyEntries = 0;
|
|
178
|
+
let rejectedEntries = 0;
|
|
179
|
+
for (const entry of catalog.entries) {
|
|
180
|
+
providers.add(entry.provider.id);
|
|
181
|
+
protocolModes.add(entry.provider.protocolMode);
|
|
182
|
+
for (const capability of entry.provider.capabilities) {
|
|
183
|
+
capabilities.add(capability);
|
|
184
|
+
}
|
|
185
|
+
if (entry.status === "ready") {
|
|
186
|
+
readyEntries += 1;
|
|
187
|
+
} else {
|
|
188
|
+
rejectedEntries += 1;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
schemaVersion: catalog.schemaVersion,
|
|
193
|
+
generatedAt: catalog.generatedAt,
|
|
194
|
+
totalEntries: catalog.entries.length,
|
|
195
|
+
readyEntries,
|
|
196
|
+
rejectedEntries,
|
|
197
|
+
providers: [...providers],
|
|
198
|
+
capabilities: [...capabilities],
|
|
199
|
+
protocolModes: [...protocolModes]
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function assertProviderCatalogOptions(options) {
|
|
203
|
+
if (!isRecord(options)) {
|
|
204
|
+
throw new ProbeMeshError({
|
|
205
|
+
code: "invalid_request",
|
|
206
|
+
message: "Provider catalog options must be an object."
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (!Array.isArray(options.artifacts)) {
|
|
210
|
+
throw new ProbeMeshError({
|
|
211
|
+
code: "invalid_request",
|
|
212
|
+
message: "Provider catalog options require an artifacts array."
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (options.generatedAt !== void 0 && (typeof options.generatedAt !== "string" || Number.isNaN(Date.parse(options.generatedAt)))) {
|
|
216
|
+
throw new ProbeMeshError({
|
|
217
|
+
code: "invalid_request",
|
|
218
|
+
message: "Provider catalog generatedAt must be an ISO timestamp."
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function assertProviderCatalogQuery(query) {
|
|
223
|
+
if (!isRecord(query)) {
|
|
224
|
+
throw new ProbeMeshError({
|
|
225
|
+
code: "invalid_request",
|
|
226
|
+
message: "Provider catalog query must be an object."
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (typeof query.capability !== "string" || query.capability.length === 0) {
|
|
230
|
+
throw new ProbeMeshError({
|
|
231
|
+
code: "invalid_request",
|
|
232
|
+
message: "Provider catalog query capability must be a non-empty string."
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
if (query.protocolMode !== void 0 && (typeof query.protocolMode !== "string" || query.protocolMode.length === 0)) {
|
|
236
|
+
throw new ProbeMeshError({
|
|
237
|
+
code: "invalid_request",
|
|
238
|
+
message: "Provider catalog query protocolMode must be a non-empty string."
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
validatePaymentPreferences(query.paymentPreferences, query.capability);
|
|
242
|
+
validatePaymentStrategy(query.paymentStrategy, query.capability);
|
|
243
|
+
if (query.maxCostUsd !== void 0 && (typeof query.maxCostUsd !== "number" || !Number.isFinite(query.maxCostUsd) || query.maxCostUsd < 0)) {
|
|
244
|
+
throw new ProbeMeshError({
|
|
245
|
+
code: "invalid_request",
|
|
246
|
+
message: "Provider catalog query maxCostUsd must be a non-negative finite number."
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
if (query.status !== void 0 && query.status !== "ready" && query.status !== "rejected") {
|
|
250
|
+
throw new ProbeMeshError({
|
|
251
|
+
code: "invalid_request",
|
|
252
|
+
message: 'Provider catalog query status must be "ready" or "rejected".'
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
validateQueryStringArray(query.providerIds, "providerIds");
|
|
256
|
+
validateQueryStringArray(query.blockedProviderIds, "blockedProviderIds");
|
|
257
|
+
if (query.requiredReceiptTypes !== void 0 && (!Array.isArray(query.requiredReceiptTypes) || query.requiredReceiptTypes.some((receiptType) => !isReceiptType(receiptType)))) {
|
|
258
|
+
throw new ProbeMeshError({
|
|
259
|
+
code: "invalid_request",
|
|
260
|
+
message: "Provider catalog query requiredReceiptTypes must contain known receipt types."
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function createCatalogEntryFromArtifact(artifact) {
|
|
265
|
+
return {
|
|
266
|
+
artifactId: artifact.artifactId,
|
|
267
|
+
generatedAt: artifact.generatedAt,
|
|
268
|
+
status: artifact.status,
|
|
269
|
+
provider: {
|
|
270
|
+
id: artifact.provider.id,
|
|
271
|
+
displayName: artifact.provider.displayName,
|
|
272
|
+
capabilities: [...artifact.provider.capabilities],
|
|
273
|
+
protocolMode: artifact.provider.protocolMode,
|
|
274
|
+
paymentOptions: cloneSerializable(artifact.provider.paymentOptions),
|
|
275
|
+
pricing: cloneSerializable(artifact.provider.pricing),
|
|
276
|
+
receipts: cloneSerializable(artifact.provider.receipts),
|
|
277
|
+
limits: cloneSerializable(artifact.provider.limits)
|
|
278
|
+
},
|
|
279
|
+
acceptance: {
|
|
280
|
+
gate: cloneSerializable(artifact.acceptance.gate),
|
|
281
|
+
failedChecks: [...artifact.acceptance.failedChecks],
|
|
282
|
+
receipts: cloneSerializable(artifact.acceptance.receipts),
|
|
283
|
+
safety: cloneSerializable(artifact.acceptance.safety),
|
|
284
|
+
retrySafety: cloneSerializable(artifact.acceptance.retrySafety),
|
|
285
|
+
route: cloneSerializable(artifact.acceptance.route),
|
|
286
|
+
timelineEventTypes: [...artifact.acceptance.timelineEventTypes],
|
|
287
|
+
telemetryEventTypes: [...artifact.acceptance.telemetryEventTypes],
|
|
288
|
+
errorCode: artifact.acceptance.errorCode
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function resolveCatalogCost(entry, capability) {
|
|
293
|
+
const pricing = entry.provider.pricing;
|
|
294
|
+
if (isProbeMeshPricing(pricing)) {
|
|
295
|
+
return pricingToCost(pricing);
|
|
296
|
+
}
|
|
297
|
+
const capabilityPricing = pricing[capability];
|
|
298
|
+
if (!isProbeMeshPricing(capabilityPricing)) {
|
|
299
|
+
return void 0;
|
|
300
|
+
}
|
|
301
|
+
return pricingToCost(capabilityPricing);
|
|
302
|
+
}
|
|
303
|
+
function pricingToCost(pricing) {
|
|
304
|
+
return {
|
|
305
|
+
amountUsd: pricing.amountUsd,
|
|
306
|
+
currency: pricing.currency,
|
|
307
|
+
unit: pricing.unit
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function hasRequiredReceiptTypes(entry, requiredReceiptTypes) {
|
|
311
|
+
const receiptTypes = new Set(entry.acceptance.receipts.types);
|
|
312
|
+
return requiredReceiptTypes.every(
|
|
313
|
+
(receiptType) => receiptTypes.has(receiptType)
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
function createMatchReasons(entry, query, cost, paymentSelection) {
|
|
317
|
+
const reasons = [
|
|
318
|
+
`status:${entry.status}`,
|
|
319
|
+
`capability:${query.capability}`,
|
|
320
|
+
`protocol:${entry.provider.protocolMode}`
|
|
321
|
+
];
|
|
322
|
+
if (query.paymentPreferences !== void 0) {
|
|
323
|
+
reasons.push("payment_preferences:matched");
|
|
324
|
+
}
|
|
325
|
+
if (query.paymentStrategy !== void 0) {
|
|
326
|
+
reasons.push(`payment_strategy:${query.paymentStrategy.mode}`);
|
|
327
|
+
}
|
|
328
|
+
if (paymentSelection?.score !== void 0) {
|
|
329
|
+
reasons.push(`payment_score:${paymentSelection.score}`);
|
|
330
|
+
}
|
|
331
|
+
for (const scoreReason of paymentSelection?.scoreReasons ?? []) {
|
|
332
|
+
reasons.push(`payment_score_reason:${scoreReason}`);
|
|
333
|
+
}
|
|
334
|
+
if (query.providerIds !== void 0) {
|
|
335
|
+
reasons.push("provider:allowed");
|
|
336
|
+
}
|
|
337
|
+
if (query.maxCostUsd !== void 0 && cost !== void 0) {
|
|
338
|
+
reasons.push(`cost:${cost.amountUsd}<=${query.maxCostUsd}`);
|
|
339
|
+
}
|
|
340
|
+
if ((query.requiredReceiptTypes ?? []).length > 0) {
|
|
341
|
+
reasons.push(`receipts:${query.requiredReceiptTypes?.join(",")}`);
|
|
342
|
+
}
|
|
343
|
+
return reasons;
|
|
344
|
+
}
|
|
345
|
+
function paymentSelectionCost(selection) {
|
|
346
|
+
return selection?.cost;
|
|
347
|
+
}
|
|
348
|
+
function validateQueryStringArray(value, name) {
|
|
349
|
+
if (value !== void 0 && (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0))) {
|
|
350
|
+
throw new ProbeMeshError({
|
|
351
|
+
code: "invalid_request",
|
|
352
|
+
message: `Provider catalog query ${name} must be an array of non-empty strings.`
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function isProbeMeshPricing(value) {
|
|
357
|
+
return isRecord(value) && typeof value.unit === "string" && typeof value.amountUsd === "number" && Number.isFinite(value.amountUsd) && value.amountUsd >= 0 && (value.currency === void 0 || typeof value.currency === "string");
|
|
358
|
+
}
|
|
359
|
+
function isReceiptType(value) {
|
|
360
|
+
return value === "payment_proof" || value === "settlement_confirmation" || value === "provider_delivery" || value === "provider_response_evidence" || value === "authorization_decision";
|
|
361
|
+
}
|
|
362
|
+
function cloneSerializable(value) {
|
|
363
|
+
if (value === void 0) {
|
|
364
|
+
return value;
|
|
365
|
+
}
|
|
366
|
+
return JSON.parse(JSON.stringify(value));
|
|
367
|
+
}
|
|
368
|
+
function validateProviderCatalogEntry(entry, path, errors) {
|
|
369
|
+
if (!isRecord(entry)) {
|
|
370
|
+
errors.push({
|
|
371
|
+
path,
|
|
372
|
+
message: "catalog entry must be an object."
|
|
373
|
+
});
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
validateNonEmptyString(entry.artifactId, `${path}.artifactId`, errors);
|
|
377
|
+
validateIsoTimestamp(entry.generatedAt, `${path}.generatedAt`, errors);
|
|
378
|
+
if (entry.status !== "ready" && entry.status !== "rejected") {
|
|
379
|
+
errors.push({
|
|
380
|
+
path: `${path}.status`,
|
|
381
|
+
message: 'status must be "ready" or "rejected".'
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
validateEntryProvider(entry.provider, `${path}.provider`, errors);
|
|
385
|
+
validateEntryAcceptance(entry.acceptance, `${path}.acceptance`, errors);
|
|
386
|
+
}
|
|
387
|
+
function validateEntryProvider(provider, path, errors) {
|
|
388
|
+
if (!isRecord(provider)) {
|
|
389
|
+
errors.push({
|
|
390
|
+
path,
|
|
391
|
+
message: "provider must be an object."
|
|
392
|
+
});
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
validateNonEmptyString(provider.id, `${path}.id`, errors);
|
|
396
|
+
validateNonEmptyString(provider.displayName, `${path}.displayName`, errors);
|
|
397
|
+
validateStringArray(provider.capabilities, `${path}.capabilities`, errors);
|
|
398
|
+
validateNonEmptyString(provider.protocolMode, `${path}.protocolMode`, errors);
|
|
399
|
+
validateProviderPaymentOptions(
|
|
400
|
+
provider.paymentOptions,
|
|
401
|
+
`${path}.paymentOptions`,
|
|
402
|
+
Array.isArray(provider.capabilities) ? provider.capabilities : [],
|
|
403
|
+
errors
|
|
404
|
+
);
|
|
405
|
+
validatePricingDeclaration(provider.pricing, `${path}.pricing`, errors);
|
|
406
|
+
validateReceiptSupport(provider.receipts, `${path}.receipts`, errors);
|
|
407
|
+
if (provider.limits !== void 0 && !isRecord(provider.limits)) {
|
|
408
|
+
errors.push({
|
|
409
|
+
path: `${path}.limits`,
|
|
410
|
+
message: "limits must be an object when provided."
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
function validateEntryAcceptance(acceptance, path, errors) {
|
|
415
|
+
if (!isRecord(acceptance)) {
|
|
416
|
+
errors.push({
|
|
417
|
+
path,
|
|
418
|
+
message: "acceptance must be an object."
|
|
419
|
+
});
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (!isRecord(acceptance.gate)) {
|
|
423
|
+
errors.push({
|
|
424
|
+
path: `${path}.gate`,
|
|
425
|
+
message: "gate must be an object."
|
|
426
|
+
});
|
|
427
|
+
} else {
|
|
428
|
+
if (typeof acceptance.gate.accepted !== "boolean") {
|
|
429
|
+
errors.push({
|
|
430
|
+
path: `${path}.gate.accepted`,
|
|
431
|
+
message: "gate.accepted must be a boolean."
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
if (acceptance.gate.status !== "accepted" && acceptance.gate.status !== "rejected") {
|
|
435
|
+
errors.push({
|
|
436
|
+
path: `${path}.gate.status`,
|
|
437
|
+
message: 'gate.status must be "accepted" or "rejected".'
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
validateStringArray(acceptance.failedChecks, `${path}.failedChecks`, errors);
|
|
442
|
+
validateReceiptSummary(acceptance.receipts, `${path}.receipts`, errors);
|
|
443
|
+
validateStringArray(
|
|
444
|
+
acceptance.timelineEventTypes,
|
|
445
|
+
`${path}.timelineEventTypes`,
|
|
446
|
+
errors
|
|
447
|
+
);
|
|
448
|
+
validateStringArray(
|
|
449
|
+
acceptance.telemetryEventTypes,
|
|
450
|
+
`${path}.telemetryEventTypes`,
|
|
451
|
+
errors
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
function validatePricingDeclaration(pricing, path, errors) {
|
|
455
|
+
if (!isRecord(pricing)) {
|
|
456
|
+
errors.push({
|
|
457
|
+
path,
|
|
458
|
+
message: "pricing must be an object."
|
|
459
|
+
});
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (isPricingObject(pricing)) {
|
|
463
|
+
validatePricingObject(pricing, path, errors);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
for (const [capability, capabilityPricing] of Object.entries(pricing)) {
|
|
467
|
+
validateNonEmptyString(capability, `${path}.${capability}`, errors);
|
|
468
|
+
validatePricingObject(capabilityPricing, `${path}.${capability}`, errors);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
function validatePricingObject(pricing, path, errors) {
|
|
472
|
+
if (!isRecord(pricing)) {
|
|
473
|
+
errors.push({
|
|
474
|
+
path,
|
|
475
|
+
message: "pricing entry must be an object."
|
|
476
|
+
});
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
validateNonEmptyString(pricing.unit, `${path}.unit`, errors);
|
|
480
|
+
if (typeof pricing.amountUsd !== "number" || !Number.isFinite(pricing.amountUsd) || pricing.amountUsd < 0) {
|
|
481
|
+
errors.push({
|
|
482
|
+
path: `${path}.amountUsd`,
|
|
483
|
+
message: "amountUsd must be a non-negative finite number."
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
if (pricing.currency !== void 0 && typeof pricing.currency !== "string") {
|
|
487
|
+
errors.push({
|
|
488
|
+
path: `${path}.currency`,
|
|
489
|
+
message: "currency must be a string when provided."
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function validateReceiptSupport(receipts, path, errors) {
|
|
494
|
+
if (!isRecord(receipts)) {
|
|
495
|
+
errors.push({
|
|
496
|
+
path,
|
|
497
|
+
message: "receipts must be an object."
|
|
498
|
+
});
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
for (const key of ["payment", "delivery", "responseEvidence"]) {
|
|
502
|
+
if (typeof receipts[key] !== "boolean") {
|
|
503
|
+
errors.push({
|
|
504
|
+
path: `${path}.${key}`,
|
|
505
|
+
message: `${key} must be a boolean.`
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
function validateReceiptSummary(receipts, path, errors) {
|
|
511
|
+
if (!isRecord(receipts)) {
|
|
512
|
+
errors.push({
|
|
513
|
+
path,
|
|
514
|
+
message: "receipts must be an object."
|
|
515
|
+
});
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (typeof receipts.total !== "number" || !Number.isFinite(receipts.total)) {
|
|
519
|
+
errors.push({
|
|
520
|
+
path: `${path}.total`,
|
|
521
|
+
message: "total must be a finite number."
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
validateStringArray(receipts.types, `${path}.types`, errors);
|
|
525
|
+
validateStringArray(receipts.providers, `${path}.providers`, errors);
|
|
526
|
+
for (const key of [
|
|
527
|
+
"hasPaymentProof",
|
|
528
|
+
"hasSettlementConfirmation",
|
|
529
|
+
"hasProviderDelivery"
|
|
530
|
+
]) {
|
|
531
|
+
if (typeof receipts[key] !== "boolean") {
|
|
532
|
+
errors.push({
|
|
533
|
+
path: `${path}.${key}`,
|
|
534
|
+
message: `${key} must be a boolean.`
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function isPricingObject(value) {
|
|
540
|
+
return "unit" in value || "amountUsd" in value || "currency" in value;
|
|
541
|
+
}
|
|
542
|
+
function validateStringArray(value, path, errors) {
|
|
543
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0)) {
|
|
544
|
+
errors.push({
|
|
545
|
+
path,
|
|
546
|
+
message: `${path} must be an array of non-empty strings.`
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function validateNonEmptyString(value, path, errors) {
|
|
551
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
552
|
+
errors.push({
|
|
553
|
+
path,
|
|
554
|
+
message: `${path} must be a non-empty string.`
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function validateIsoTimestamp(value, path, errors) {
|
|
559
|
+
if (typeof value !== "string" || Number.isNaN(Date.parse(value))) {
|
|
560
|
+
errors.push({
|
|
561
|
+
path,
|
|
562
|
+
message: `${path} must be an ISO timestamp string.`
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
function formatValidationErrors(errors) {
|
|
567
|
+
return errors.map((error) => `${error.path}: ${error.message}`).join("; ");
|
|
568
|
+
}
|
|
569
|
+
function isRecord(value) {
|
|
570
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// src/providerCatalogPolicy.ts
|
|
574
|
+
function evaluateProviderCatalogPolicy(options) {
|
|
575
|
+
assertEvaluationOptions(options);
|
|
576
|
+
const catalogPolicy = resolveCatalogPolicy(options.catalogPolicy);
|
|
577
|
+
const adapterInventory = options.adapterInventory;
|
|
578
|
+
const adapterIds = new Set(adapterInventory.map((adapter) => adapter.id));
|
|
579
|
+
const catalogSummary = summarizeProviderCatalog(options.catalog);
|
|
580
|
+
const checks = [
|
|
581
|
+
{
|
|
582
|
+
name: "catalog_valid",
|
|
583
|
+
status: "passed",
|
|
584
|
+
message: "Provider catalog is valid."
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
name: "adapter_inventory_valid",
|
|
588
|
+
status: "passed",
|
|
589
|
+
message: "Adapter inventory is valid."
|
|
590
|
+
}
|
|
591
|
+
];
|
|
592
|
+
const queryResults = options.queries.map(
|
|
593
|
+
(query) => evaluateQuery({
|
|
594
|
+
catalog: options.catalog,
|
|
595
|
+
catalogPolicy,
|
|
596
|
+
query,
|
|
597
|
+
adapterInventory,
|
|
598
|
+
adapterIds,
|
|
599
|
+
checks
|
|
600
|
+
})
|
|
601
|
+
);
|
|
602
|
+
const failedChecks = checks.filter((check) => check.status === "failed").length;
|
|
603
|
+
const routeableMatches = queryResults.reduce(
|
|
604
|
+
(total, result) => total + result.routeableMatches.length,
|
|
605
|
+
0
|
|
606
|
+
);
|
|
607
|
+
const missingAdapterMatches = queryResults.reduce(
|
|
608
|
+
(total, result) => total + result.missingAdapterMatches.length,
|
|
609
|
+
0
|
|
610
|
+
);
|
|
611
|
+
return {
|
|
612
|
+
status: failedChecks === 0 ? "passed" : "failed",
|
|
613
|
+
checks,
|
|
614
|
+
summary: {
|
|
615
|
+
...catalogSummary,
|
|
616
|
+
totalQueries: options.queries.length,
|
|
617
|
+
passedChecks: checks.length - failedChecks,
|
|
618
|
+
failedChecks,
|
|
619
|
+
routeableMatches,
|
|
620
|
+
missingAdapterMatches
|
|
621
|
+
},
|
|
622
|
+
queryResults
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
function createProviderCatalogPolicyReport(result, options = {}) {
|
|
626
|
+
const report = {
|
|
627
|
+
title: options.title ?? "ProbeMesh Catalog Policy Report",
|
|
628
|
+
generatedAt: options.generatedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
629
|
+
status: result.status,
|
|
630
|
+
summary: cloneSerializable2(result.summary),
|
|
631
|
+
checks: cloneSerializable2(result.checks),
|
|
632
|
+
queryResults: cloneSerializable2(result.queryResults)
|
|
633
|
+
};
|
|
634
|
+
return sanitizeReport(report, options.forbiddenOutputValues ?? []);
|
|
635
|
+
}
|
|
636
|
+
function formatProviderCatalogPolicyReport(report, options = {}) {
|
|
637
|
+
if ((options.format ?? "json") === "markdown") {
|
|
638
|
+
return formatMarkdownReport(report);
|
|
639
|
+
}
|
|
640
|
+
return JSON.stringify(report, null, 2);
|
|
641
|
+
}
|
|
642
|
+
function assertEvaluationOptions(options) {
|
|
643
|
+
if (!isRecord2(options)) {
|
|
644
|
+
throw new ProbeMeshError({
|
|
645
|
+
code: "invalid_request",
|
|
646
|
+
message: "Provider catalog policy evaluation options must be an object."
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
assertProviderCatalog(options.catalog);
|
|
650
|
+
assertAdapterInventory(options.adapterInventory);
|
|
651
|
+
if (!Array.isArray(options.queries) || options.queries.length === 0) {
|
|
652
|
+
throw new ProbeMeshError({
|
|
653
|
+
code: "invalid_request",
|
|
654
|
+
message: "Provider catalog policy evaluation requires at least one query."
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
options.queries.forEach(
|
|
658
|
+
(query, index) => assertExpectedQuery(query, `queries.${index}`)
|
|
659
|
+
);
|
|
660
|
+
assertCatalogPolicyOptions(options.catalogPolicy, "catalogPolicy");
|
|
661
|
+
}
|
|
662
|
+
function assertAdapterInventory(adapterInventory) {
|
|
663
|
+
if (!Array.isArray(adapterInventory)) {
|
|
664
|
+
throw new ProbeMeshError({
|
|
665
|
+
code: "invalid_request",
|
|
666
|
+
message: "Provider catalog policy adapterInventory must be an array."
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
const seen = /* @__PURE__ */ new Set();
|
|
670
|
+
adapterInventory.forEach((adapter, index) => {
|
|
671
|
+
const path = `adapterInventory.${index}`;
|
|
672
|
+
if (!isRecord2(adapter)) {
|
|
673
|
+
throw new ProbeMeshError({
|
|
674
|
+
code: "invalid_request",
|
|
675
|
+
message: `${path} must be an object.`
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
if (typeof adapter.id !== "string" || adapter.id.length === 0) {
|
|
679
|
+
throw new ProbeMeshError({
|
|
680
|
+
code: "invalid_request",
|
|
681
|
+
message: `${path}.id must be a non-empty string.`
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
if (seen.has(adapter.id)) {
|
|
685
|
+
throw new ProbeMeshError({
|
|
686
|
+
code: "invalid_request",
|
|
687
|
+
message: `${path}.id duplicates adapter "${adapter.id}".`
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
seen.add(adapter.id);
|
|
691
|
+
if (!Array.isArray(adapter.capabilities) || adapter.capabilities.some(
|
|
692
|
+
(capability) => typeof capability !== "string" || capability.length === 0
|
|
693
|
+
)) {
|
|
694
|
+
throw new ProbeMeshError({
|
|
695
|
+
code: "invalid_request",
|
|
696
|
+
message: `${path}.capabilities must be an array of non-empty strings.`
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
if (adapter.paymentOptions !== void 0 && (!Array.isArray(adapter.paymentOptions) || adapter.paymentOptions.some(
|
|
700
|
+
(option) => !isRecord2(option) || typeof option.id !== "string" || option.id.length === 0 || typeof option.protocolMode !== "string" || option.protocolMode.length === 0
|
|
701
|
+
))) {
|
|
702
|
+
throw new ProbeMeshError({
|
|
703
|
+
code: "invalid_request",
|
|
704
|
+
message: `${path}.paymentOptions must be an array of payment option objects when provided.`
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
function assertExpectedQuery(query, path) {
|
|
710
|
+
if (!isRecord2(query)) {
|
|
711
|
+
throw new ProbeMeshError({
|
|
712
|
+
code: "invalid_request",
|
|
713
|
+
message: `${path} must be an object.`
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
if (typeof query.capability !== "string" || query.capability.length === 0) {
|
|
717
|
+
throw new ProbeMeshError({
|
|
718
|
+
code: "invalid_request",
|
|
719
|
+
message: `${path}.capability must be a non-empty string.`
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
const minMatches = query.minMatches;
|
|
723
|
+
if (minMatches !== void 0 && (typeof minMatches !== "number" || !Number.isInteger(minMatches) || minMatches < 0)) {
|
|
724
|
+
throw new ProbeMeshError({
|
|
725
|
+
code: "invalid_request",
|
|
726
|
+
message: `${path}.minMatches must be a non-negative integer.`
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
assertCatalogPolicyOptions(query, path);
|
|
730
|
+
}
|
|
731
|
+
function assertCatalogPolicyOptions(policy, path) {
|
|
732
|
+
if (policy === void 0) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
if (!isRecord2(policy)) {
|
|
736
|
+
throw new ProbeMeshError({
|
|
737
|
+
code: "invalid_request",
|
|
738
|
+
message: `${path} must be an object when provided.`
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
if (policy.status !== void 0 && policy.status !== "ready" && policy.status !== "rejected") {
|
|
742
|
+
throw new ProbeMeshError({
|
|
743
|
+
code: "invalid_request",
|
|
744
|
+
message: `${path}.status must be "ready" or "rejected".`
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
if (policy.protocolMode !== void 0 && (typeof policy.protocolMode !== "string" || policy.protocolMode.length === 0)) {
|
|
748
|
+
throw new ProbeMeshError({
|
|
749
|
+
code: "invalid_request",
|
|
750
|
+
message: `${path}.protocolMode must be a non-empty string.`
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
validatePaymentPreferences(policy.paymentPreferences, `${path}.paymentPreferences`);
|
|
754
|
+
validatePaymentStrategy(policy.paymentStrategy, `${path}.paymentStrategy`);
|
|
755
|
+
if (policy.maxCostUsd !== void 0 && (typeof policy.maxCostUsd !== "number" || !Number.isFinite(policy.maxCostUsd) || policy.maxCostUsd < 0)) {
|
|
756
|
+
throw new ProbeMeshError({
|
|
757
|
+
code: "invalid_request",
|
|
758
|
+
message: `${path}.maxCostUsd must be a non-negative finite number.`
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
validateStringArray2(policy.providerIds, `${path}.providerIds`);
|
|
762
|
+
validateStringArray2(policy.blockedProviderIds, `${path}.blockedProviderIds`);
|
|
763
|
+
if (policy.requiredReceiptTypes !== void 0 && (!Array.isArray(policy.requiredReceiptTypes) || policy.requiredReceiptTypes.some((receiptType) => !isReceiptType2(receiptType)))) {
|
|
764
|
+
throw new ProbeMeshError({
|
|
765
|
+
code: "invalid_request",
|
|
766
|
+
message: `${path}.requiredReceiptTypes must contain known receipt types.`
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
function evaluateQuery(options) {
|
|
771
|
+
const resolvedQuery = resolveQuery(options.catalogPolicy, options.query);
|
|
772
|
+
const matches = findProviderCatalogMatches(options.catalog, resolvedQuery);
|
|
773
|
+
const routeableMatches = matches.filter(
|
|
774
|
+
(match) => hasRouteableAdapter(match, options.adapterInventory)
|
|
775
|
+
);
|
|
776
|
+
const missingAdapterMatches = matches.filter((match) => !options.adapterIds.has(match.providerId)).map((match) => ({
|
|
777
|
+
providerId: match.providerId,
|
|
778
|
+
capability: match.capability,
|
|
779
|
+
catalogArtifactId: match.entry.artifactId,
|
|
780
|
+
catalogStatus: match.entry.status,
|
|
781
|
+
reasons: [
|
|
782
|
+
...match.reasons,
|
|
783
|
+
`missing_adapter:${match.providerId}`
|
|
784
|
+
]
|
|
785
|
+
}));
|
|
786
|
+
const skippedProviders = createSkippedProviders(
|
|
787
|
+
options.catalog.entries,
|
|
788
|
+
options.query.capability,
|
|
789
|
+
matches,
|
|
790
|
+
resolvedQuery,
|
|
791
|
+
options.adapterInventory
|
|
792
|
+
);
|
|
793
|
+
const minMatches = options.query.minMatches ?? 1;
|
|
794
|
+
pushCheck(options.checks, {
|
|
795
|
+
name: "ready_matches",
|
|
796
|
+
status: matches.length >= minMatches ? "passed" : "failed",
|
|
797
|
+
capability: options.query.capability,
|
|
798
|
+
message: matches.length >= minMatches ? `Found ${matches.length} catalog match(es) for "${options.query.capability}".` : `Expected at least ${minMatches} catalog match(es) for "${options.query.capability}", found ${matches.length}.`
|
|
799
|
+
});
|
|
800
|
+
pushCheck(options.checks, {
|
|
801
|
+
name: "routeable_matches",
|
|
802
|
+
status: routeableMatches.length >= minMatches ? "passed" : "failed",
|
|
803
|
+
capability: options.query.capability,
|
|
804
|
+
message: routeableMatches.length >= minMatches ? `Found ${routeableMatches.length} routeable adapter-backed match(es) for "${options.query.capability}".` : `Expected at least ${minMatches} routeable adapter-backed match(es) for "${options.query.capability}", found ${routeableMatches.length}.`
|
|
805
|
+
});
|
|
806
|
+
for (const missingMatch of missingAdapterMatches) {
|
|
807
|
+
pushCheck(options.checks, {
|
|
808
|
+
name: "missing_adapter",
|
|
809
|
+
status: routeableMatches.length >= minMatches ? "passed" : "failed",
|
|
810
|
+
capability: missingMatch.capability,
|
|
811
|
+
providerId: missingMatch.providerId,
|
|
812
|
+
message: routeableMatches.length >= minMatches ? `Catalog provider "${missingMatch.providerId}" matches policy but is missing from adapter inventory; minimum routeable matches are still satisfied.` : `Catalog provider "${missingMatch.providerId}" matches policy but is missing from adapter inventory.`
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
return {
|
|
816
|
+
query: options.query,
|
|
817
|
+
matches,
|
|
818
|
+
routeableMatches,
|
|
819
|
+
missingAdapterMatches,
|
|
820
|
+
skippedProviders
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
function resolveCatalogPolicy(policy) {
|
|
824
|
+
return {
|
|
825
|
+
enabled: policy?.enabled ?? true,
|
|
826
|
+
status: policy?.status ?? "ready",
|
|
827
|
+
protocolMode: policy?.protocolMode,
|
|
828
|
+
paymentPreferences: policy?.paymentPreferences,
|
|
829
|
+
paymentStrategy: policy?.paymentStrategy,
|
|
830
|
+
maxCostUsd: policy?.maxCostUsd,
|
|
831
|
+
requiredReceiptTypes: policy?.requiredReceiptTypes,
|
|
832
|
+
providerIds: policy?.providerIds,
|
|
833
|
+
blockedProviderIds: policy?.blockedProviderIds
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
function resolveQuery(policy, query) {
|
|
837
|
+
const queryPolicy = resolveCatalogPolicy(query);
|
|
838
|
+
const activePolicy = policy.enabled ? policy : resolveCatalogPolicy(void 0);
|
|
839
|
+
return {
|
|
840
|
+
capability: query.capability,
|
|
841
|
+
protocolMode: queryPolicy.protocolMode ?? activePolicy.protocolMode,
|
|
842
|
+
paymentPreferences: mergePaymentPreferences(
|
|
843
|
+
activePolicy.paymentPreferences,
|
|
844
|
+
queryPolicy.paymentPreferences
|
|
845
|
+
),
|
|
846
|
+
paymentStrategy: queryPolicy.paymentStrategy ?? activePolicy.paymentStrategy,
|
|
847
|
+
maxCostUsd: strictestMaxCostUsd(
|
|
848
|
+
queryPolicy.maxCostUsd,
|
|
849
|
+
activePolicy.maxCostUsd
|
|
850
|
+
),
|
|
851
|
+
requiredReceiptTypes: unionProviderIds(
|
|
852
|
+
queryPolicy.requiredReceiptTypes,
|
|
853
|
+
activePolicy.requiredReceiptTypes
|
|
854
|
+
),
|
|
855
|
+
providerIds: intersectProviderIds(
|
|
856
|
+
queryPolicy.providerIds,
|
|
857
|
+
activePolicy.providerIds
|
|
858
|
+
),
|
|
859
|
+
blockedProviderIds: unionProviderIds(
|
|
860
|
+
queryPolicy.blockedProviderIds,
|
|
861
|
+
activePolicy.blockedProviderIds
|
|
862
|
+
),
|
|
863
|
+
status: queryPolicy.status ?? activePolicy.status
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
function hasRouteableAdapter(match, adapterInventory) {
|
|
867
|
+
return adapterInventory.some(
|
|
868
|
+
(adapter) => adapter.id === match.providerId && adapter.capabilities.includes(match.capability) && (adapter.paymentOptions === void 0 || match.paymentSelection === void 0 || adapter.paymentOptions.some(
|
|
869
|
+
(option) => option.id === match.paymentSelection?.optionId
|
|
870
|
+
))
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
function createSkippedProviders(entries, capability, matches, query, adapterInventory) {
|
|
874
|
+
const matchIds = new Set(matches.map((match) => match.providerId));
|
|
875
|
+
const adapterIds = new Set(adapterInventory.map((adapter) => adapter.id));
|
|
876
|
+
const catalogProviderIds = new Set(entries.map((entry) => entry.provider.id));
|
|
877
|
+
const skippedCatalogEntries = entries.filter((entry) => entry.provider.capabilities.includes(capability)).filter((entry) => !matchIds.has(entry.provider.id)).map((entry) => ({
|
|
878
|
+
providerId: entry.provider.id,
|
|
879
|
+
capability,
|
|
880
|
+
catalogArtifactId: entry.artifactId,
|
|
881
|
+
catalogStatus: entry.status,
|
|
882
|
+
reasons: explainSkippedEntry(entry, query, adapterIds)
|
|
883
|
+
}));
|
|
884
|
+
const inventoryOnlyAdapters = adapterInventory.filter((adapter) => adapter.capabilities.includes(capability)).filter((adapter) => !catalogProviderIds.has(adapter.id)).map((adapter) => ({
|
|
885
|
+
providerId: adapter.id,
|
|
886
|
+
capability,
|
|
887
|
+
inventoryOnly: true,
|
|
888
|
+
reasons: ["adapter:not_in_catalog"]
|
|
889
|
+
}));
|
|
890
|
+
return [...skippedCatalogEntries, ...inventoryOnlyAdapters];
|
|
891
|
+
}
|
|
892
|
+
function explainSkippedEntry(entry, query, adapterIds) {
|
|
893
|
+
const reasons = [];
|
|
894
|
+
const providerId = entry.provider.id;
|
|
895
|
+
if (entry.status !== (query.status ?? "ready")) {
|
|
896
|
+
reasons.push(`status:${entry.status}`);
|
|
897
|
+
}
|
|
898
|
+
if (query.protocolMode && entry.provider.protocolMode !== query.protocolMode) {
|
|
899
|
+
reasons.push(`protocol:${entry.provider.protocolMode}`);
|
|
900
|
+
}
|
|
901
|
+
if (query.paymentPreferences) {
|
|
902
|
+
const paymentMatch = findProviderCatalogMatches(
|
|
903
|
+
{
|
|
904
|
+
schemaVersion: "probemesh.provider-catalog.index.v1",
|
|
905
|
+
generatedAt: "1970-01-01T00:00:00.000Z",
|
|
906
|
+
entries: [entry]
|
|
907
|
+
},
|
|
908
|
+
{
|
|
909
|
+
capability: query.capability,
|
|
910
|
+
paymentPreferences: query.paymentPreferences,
|
|
911
|
+
status: entry.status
|
|
912
|
+
}
|
|
913
|
+
);
|
|
914
|
+
if (paymentMatch.length === 0) {
|
|
915
|
+
reasons.push("payment_preferences:not_matched");
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
if (query.providerIds && !query.providerIds.includes(providerId)) {
|
|
919
|
+
reasons.push("provider:not_allowed");
|
|
920
|
+
}
|
|
921
|
+
if (query.blockedProviderIds?.includes(providerId)) {
|
|
922
|
+
reasons.push("provider:blocked");
|
|
923
|
+
}
|
|
924
|
+
const cost = resolveCatalogCost2(entry, query.capability);
|
|
925
|
+
if (query.maxCostUsd !== void 0 && (cost === void 0 || cost.amountUsd > query.maxCostUsd)) {
|
|
926
|
+
reasons.push(`cost:${cost?.amountUsd ?? "unknown"}>${query.maxCostUsd}`);
|
|
927
|
+
}
|
|
928
|
+
const receiptTypes = new Set(entry.acceptance.receipts.types);
|
|
929
|
+
for (const receiptType of query.requiredReceiptTypes ?? []) {
|
|
930
|
+
if (!receiptTypes.has(receiptType)) {
|
|
931
|
+
reasons.push(`missing_receipt:${receiptType}`);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
if (!adapterIds.has(providerId)) {
|
|
935
|
+
reasons.push(`missing_adapter:${providerId}`);
|
|
936
|
+
}
|
|
937
|
+
return reasons.length > 0 ? reasons : ["not_matched"];
|
|
938
|
+
}
|
|
939
|
+
function resolveCatalogCost2(entry, capability) {
|
|
940
|
+
const pricing = entry.provider.pricing;
|
|
941
|
+
if (isPricing(pricing)) {
|
|
942
|
+
return pricing;
|
|
943
|
+
}
|
|
944
|
+
const capabilityPricing = pricing[capability];
|
|
945
|
+
return isPricing(capabilityPricing) ? capabilityPricing : void 0;
|
|
946
|
+
}
|
|
947
|
+
function pushCheck(checks, check) {
|
|
948
|
+
checks.push(check);
|
|
949
|
+
}
|
|
950
|
+
function strictestMaxCostUsd(first, second) {
|
|
951
|
+
if (first === void 0) {
|
|
952
|
+
return second;
|
|
953
|
+
}
|
|
954
|
+
if (second === void 0) {
|
|
955
|
+
return first;
|
|
956
|
+
}
|
|
957
|
+
return Math.min(first, second);
|
|
958
|
+
}
|
|
959
|
+
function intersectProviderIds(first, second) {
|
|
960
|
+
if (!first) {
|
|
961
|
+
return second;
|
|
962
|
+
}
|
|
963
|
+
if (!second) {
|
|
964
|
+
return first;
|
|
965
|
+
}
|
|
966
|
+
return first.filter((providerId) => second.includes(providerId));
|
|
967
|
+
}
|
|
968
|
+
function unionProviderIds(first, second) {
|
|
969
|
+
return [.../* @__PURE__ */ new Set([...first ?? [], ...second ?? []])];
|
|
970
|
+
}
|
|
971
|
+
function formatMarkdownReport(report) {
|
|
972
|
+
const lines = [
|
|
973
|
+
`# ${report.title}`,
|
|
974
|
+
"",
|
|
975
|
+
`Status: ${report.status}`,
|
|
976
|
+
`Generated: ${report.generatedAt}`,
|
|
977
|
+
"",
|
|
978
|
+
"## Summary",
|
|
979
|
+
`- Total entries: ${report.summary.totalEntries}`,
|
|
980
|
+
`- Ready entries: ${report.summary.readyEntries}`,
|
|
981
|
+
`- Rejected entries: ${report.summary.rejectedEntries}`,
|
|
982
|
+
`- Queries: ${report.summary.totalQueries}`,
|
|
983
|
+
`- Routeable matches: ${report.summary.routeableMatches}`,
|
|
984
|
+
`- Missing adapter matches: ${report.summary.missingAdapterMatches}`,
|
|
985
|
+
`- Failed checks: ${report.summary.failedChecks}`,
|
|
986
|
+
"",
|
|
987
|
+
"## Checks",
|
|
988
|
+
...report.checks.map(
|
|
989
|
+
(check) => `- ${check.status === "passed" ? "[x]" : "[ ]"} ${check.name}: ${check.message}`
|
|
990
|
+
),
|
|
991
|
+
"",
|
|
992
|
+
"## Queries",
|
|
993
|
+
...report.queryResults.flatMap((result) => [
|
|
994
|
+
`### ${result.query.capability}`,
|
|
995
|
+
`- Matches: ${formatProviderIds(result.matches.map((match) => match.providerId))}`,
|
|
996
|
+
`- Payment options: ${formatMatchPaymentOptions(result.routeableMatches)}`,
|
|
997
|
+
`- Routeable: ${formatProviderIds(
|
|
998
|
+
result.routeableMatches.map((match) => match.providerId)
|
|
999
|
+
)}`,
|
|
1000
|
+
`- Missing adapters: ${formatProviderIds(
|
|
1001
|
+
result.missingAdapterMatches.map((match) => match.providerId)
|
|
1002
|
+
)}`,
|
|
1003
|
+
`- Skipped: ${formatProviderIds(
|
|
1004
|
+
result.skippedProviders.map((provider) => provider.providerId)
|
|
1005
|
+
)}`,
|
|
1006
|
+
""
|
|
1007
|
+
])
|
|
1008
|
+
];
|
|
1009
|
+
return `${lines.join("\n")}
|
|
1010
|
+
`;
|
|
1011
|
+
}
|
|
1012
|
+
function formatProviderIds(providerIds) {
|
|
1013
|
+
return providerIds.length > 0 ? providerIds.join(", ") : "none";
|
|
1014
|
+
}
|
|
1015
|
+
function formatMatchPaymentOptions(matches) {
|
|
1016
|
+
if (matches.length === 0) {
|
|
1017
|
+
return "none";
|
|
1018
|
+
}
|
|
1019
|
+
return matches.map((match) => {
|
|
1020
|
+
const selection = match.paymentSelection;
|
|
1021
|
+
if (!selection) {
|
|
1022
|
+
return `${match.providerId}: none`;
|
|
1023
|
+
}
|
|
1024
|
+
const score = selection.score !== void 0 ? ` score=${selection.score}` : "";
|
|
1025
|
+
const reasons = selection.scoreReasons && selection.scoreReasons.length > 0 ? ` (${selection.scoreReasons.join("; ")})` : "";
|
|
1026
|
+
return `${match.providerId}: ${selection.optionId}/${selection.protocolMode}${score}${reasons}`;
|
|
1027
|
+
}).join(", ");
|
|
1028
|
+
}
|
|
1029
|
+
function sanitizeReport(value, forbiddenOutputValues) {
|
|
1030
|
+
return replaceForbiddenValues(
|
|
1031
|
+
cloneSerializable2(value),
|
|
1032
|
+
forbiddenOutputValues.filter((entry) => entry.length > 0)
|
|
1033
|
+
);
|
|
1034
|
+
}
|
|
1035
|
+
function replaceForbiddenValues(value, forbiddenValues) {
|
|
1036
|
+
if (typeof value === "string") {
|
|
1037
|
+
return forbiddenValues.reduce(
|
|
1038
|
+
(current, forbidden) => current.split(forbidden).join("[REDACTED]"),
|
|
1039
|
+
value
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
if (value === null || typeof value !== "object" || value instanceof Date) {
|
|
1043
|
+
return value;
|
|
1044
|
+
}
|
|
1045
|
+
if (Array.isArray(value)) {
|
|
1046
|
+
return value.map((entry) => replaceForbiddenValues(entry, forbiddenValues));
|
|
1047
|
+
}
|
|
1048
|
+
return Object.fromEntries(
|
|
1049
|
+
Object.entries(value).map(([key, entry]) => [
|
|
1050
|
+
key,
|
|
1051
|
+
replaceForbiddenValues(entry, forbiddenValues)
|
|
1052
|
+
])
|
|
1053
|
+
);
|
|
1054
|
+
}
|
|
1055
|
+
function cloneSerializable2(value) {
|
|
1056
|
+
if (value === void 0) {
|
|
1057
|
+
return value;
|
|
1058
|
+
}
|
|
1059
|
+
return JSON.parse(JSON.stringify(value));
|
|
1060
|
+
}
|
|
1061
|
+
function validateStringArray2(value, path) {
|
|
1062
|
+
if (value !== void 0 && (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0))) {
|
|
1063
|
+
throw new ProbeMeshError({
|
|
1064
|
+
code: "invalid_request",
|
|
1065
|
+
message: `${path} must be an array of non-empty strings.`
|
|
1066
|
+
});
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
function isPricing(value) {
|
|
1070
|
+
return isRecord2(value) && typeof value.unit === "string" && typeof value.amountUsd === "number" && Number.isFinite(value.amountUsd) && value.amountUsd >= 0 && (value.currency === void 0 || typeof value.currency === "string");
|
|
1071
|
+
}
|
|
1072
|
+
function isReceiptType2(value) {
|
|
1073
|
+
return value === "payment_proof" || value === "settlement_confirmation" || value === "provider_delivery" || value === "provider_response_evidence" || value === "authorization_decision";
|
|
1074
|
+
}
|
|
1075
|
+
function isRecord2(value) {
|
|
1076
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
export {
|
|
1080
|
+
PROVIDER_CATALOG_SCHEMA_VERSION,
|
|
1081
|
+
validateProviderCatalog,
|
|
1082
|
+
assertProviderCatalog,
|
|
1083
|
+
parseProviderCatalogArtifactJson,
|
|
1084
|
+
createProviderCatalog,
|
|
1085
|
+
findProviderCatalogMatches,
|
|
1086
|
+
summarizeProviderCatalog,
|
|
1087
|
+
evaluateProviderCatalogPolicy,
|
|
1088
|
+
createProviderCatalogPolicyReport,
|
|
1089
|
+
formatProviderCatalogPolicyReport
|
|
1090
|
+
};
|
|
1091
|
+
//# sourceMappingURL=chunk-ZJOLPBJJ.js.map
|