@valiron/sdk 1.0.1 → 1.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/README.md +15 -2
- package/dist/clients.d.ts +40 -1
- package/dist/clients.js +69 -6
- package/dist/index.d.ts +50 -1
- package/dist/index.js +51 -0
- package/dist/middleware.d.ts +9 -1
- package/dist/middleware.js +228 -9
- package/dist/operatorSdk.d.ts +11 -1
- package/dist/operatorSdk.js +152 -3
- package/dist/telemetry.js +1 -1
- package/dist/types.d.ts +101 -2
- package/docs/AGENT-READY-APIS.md +417 -0
- package/docs/API-REFERENCE.md +208 -26
- package/docs/DASHBOARD.md +107 -28
- package/docs/ERRORS.md +12 -2
- package/docs/IDENTITY.md +67 -14
- package/docs/QUICKSTART.md +67 -5
- package/docs/README.md +20 -4
- package/docs/SDK-REFERENCE.md +58 -12
- package/docs/TRUST-MODEL.md +38 -4
- package/docs/llms.txt +119 -7
- package/package.json +2 -2
package/dist/middleware.js
CHANGED
|
@@ -138,6 +138,49 @@ function getGateCheckCache(sdk) {
|
|
|
138
138
|
}
|
|
139
139
|
return cache;
|
|
140
140
|
}
|
|
141
|
+
function normalizeTrustSignals(trustSignals) {
|
|
142
|
+
return trustSignals?.length ? [...trustSignals].sort() : undefined;
|
|
143
|
+
}
|
|
144
|
+
function cacheKeyFor(agentId, trustSignals) {
|
|
145
|
+
const normalized = normalizeTrustSignals(trustSignals);
|
|
146
|
+
return normalized ? `${agentId}:${normalized.join(",")}` : agentId;
|
|
147
|
+
}
|
|
148
|
+
function bucketScore(score) {
|
|
149
|
+
if (score === undefined || !Number.isFinite(score))
|
|
150
|
+
return "unknown";
|
|
151
|
+
if (score <= 40)
|
|
152
|
+
return "0-40";
|
|
153
|
+
if (score <= 70)
|
|
154
|
+
return "41-70";
|
|
155
|
+
if (score <= 90)
|
|
156
|
+
return "71-90";
|
|
157
|
+
return "91+";
|
|
158
|
+
}
|
|
159
|
+
function emitGateTelemetry(sdk, payload) {
|
|
160
|
+
if (typeof sdk.emitTelemetry !== "function")
|
|
161
|
+
return;
|
|
162
|
+
const signals = normalizeTrustSignals(payload.trustSignals);
|
|
163
|
+
sdk.emitTelemetry("gate_decision", {
|
|
164
|
+
framework: payload.framework,
|
|
165
|
+
agent_type: payload.agentType,
|
|
166
|
+
chain: payload.chain || "unknown",
|
|
167
|
+
allow: payload.allow,
|
|
168
|
+
route: payload.route,
|
|
169
|
+
tier: payload.tier,
|
|
170
|
+
risk_level: payload.riskLevel,
|
|
171
|
+
score_bucket: bucketScore(payload.score),
|
|
172
|
+
min_score_bucket: bucketScore(payload.minScore),
|
|
173
|
+
cached: payload.cached,
|
|
174
|
+
pending_evaluation: payload.pendingEvaluation || false,
|
|
175
|
+
trust_signals: signals?.join(","),
|
|
176
|
+
trust_signal_count: signals?.length,
|
|
177
|
+
world_included: signals?.includes("world"),
|
|
178
|
+
icebreaker_included: signals?.includes("icebreaker"),
|
|
179
|
+
self_included: signals?.includes("self"),
|
|
180
|
+
status: payload.status || "ok",
|
|
181
|
+
latency_ms: payload.latencyMs,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
141
184
|
async function runGateCheck(config, req, cache) {
|
|
142
185
|
const minScore = config.minScore ?? 65;
|
|
143
186
|
const cacheTtl = config.cacheTtlMs ?? DEFAULT_CACHE_TTL_MS;
|
|
@@ -145,9 +188,10 @@ async function runGateCheck(config, req, cache) {
|
|
|
145
188
|
const extractAgentId = config.extractAgentId || defaultExtractAgentId;
|
|
146
189
|
const agentId = extractAgentId(req);
|
|
147
190
|
if (agentId) {
|
|
191
|
+
const agentCacheKey = cacheKeyFor(agentId, config.trustSignals);
|
|
148
192
|
// Check client-side cache first
|
|
149
193
|
if (cacheTtl > 0) {
|
|
150
|
-
const cached = cache.getCached(
|
|
194
|
+
const cached = cache.getCached(agentCacheKey, cacheTtl);
|
|
151
195
|
if (cached)
|
|
152
196
|
return { agentId, result: cached.result, pendingEvaluation: cached.pendingEvaluation };
|
|
153
197
|
}
|
|
@@ -155,7 +199,9 @@ async function runGateCheck(config, req, cache) {
|
|
|
155
199
|
// Use getAgentProfile() which hits the PUBLIC /operator/agent/ endpoint.
|
|
156
200
|
// The old gate() call used POST /operator/gate/ which is protected by
|
|
157
201
|
// agent challenge-response auth and fails from server contexts.
|
|
158
|
-
const profile = await config.sdk.getAgentProfile(agentId
|
|
202
|
+
const profile = await config.sdk.getAgentProfile(agentId, {
|
|
203
|
+
trustSignals: normalizeTrustSignals(config.trustSignals),
|
|
204
|
+
});
|
|
159
205
|
const result = profileToGateResult(profile, minScore);
|
|
160
206
|
// Detect brand-new agent: on-chain identity exists but zero reputation.
|
|
161
207
|
// Trigger a sandbox evaluation so the agent can build a score.
|
|
@@ -167,7 +213,7 @@ async function runGateCheck(config, req, cache) {
|
|
|
167
213
|
config.sdk.triggerSandboxTest(agentId).catch(() => { });
|
|
168
214
|
}
|
|
169
215
|
if (cacheTtl > 0)
|
|
170
|
-
cache.setCache(
|
|
216
|
+
cache.setCache(agentCacheKey, result, isNewAgent || undefined);
|
|
171
217
|
return { agentId, result, pendingEvaluation: isNewAgent || undefined };
|
|
172
218
|
}
|
|
173
219
|
catch (error) {
|
|
@@ -178,9 +224,10 @@ async function runGateCheck(config, req, cache) {
|
|
|
178
224
|
const extractAddress = config.extractAgentAddress || defaultExtractAgentAddress;
|
|
179
225
|
const agentAddr = extractAddress(req);
|
|
180
226
|
if (agentAddr) {
|
|
227
|
+
const agentCacheKey = cacheKeyFor(agentAddr, config.trustSignals);
|
|
181
228
|
// Check client-side cache first
|
|
182
229
|
if (cacheTtl > 0) {
|
|
183
|
-
const cached = cache.getCached(
|
|
230
|
+
const cached = cache.getCached(agentCacheKey, cacheTtl);
|
|
184
231
|
if (cached)
|
|
185
232
|
return { agentId: agentAddr, result: cached.result, pendingEvaluation: cached.pendingEvaluation };
|
|
186
233
|
}
|
|
@@ -194,7 +241,7 @@ async function runGateCheck(config, req, cache) {
|
|
|
194
241
|
config.sdk.triggerKeyAgentSandbox(agentAddr).catch(() => { });
|
|
195
242
|
}
|
|
196
243
|
if (cacheTtl > 0)
|
|
197
|
-
cache.setCache(
|
|
244
|
+
cache.setCache(agentCacheKey, result, isNewKeyAgent || undefined);
|
|
198
245
|
return { agentId: agentAddr, result, pendingEvaluation: isNewKeyAgent || undefined };
|
|
199
246
|
}
|
|
200
247
|
catch (error) {
|
|
@@ -244,8 +291,22 @@ function createValironGate(config) {
|
|
|
244
291
|
const frozen = { ...config };
|
|
245
292
|
const cache = createProfileCache();
|
|
246
293
|
async function valironGateHandler(req, res, next) {
|
|
294
|
+
const startedAt = Date.now();
|
|
247
295
|
const { agentId, result, error, pendingEvaluation } = await runGateCheck(frozen, req, cache);
|
|
296
|
+
const latencyMs = Date.now() - startedAt;
|
|
297
|
+
const minScore = frozen.minScore ?? 65;
|
|
298
|
+
const agentType = agentId
|
|
299
|
+
? (defaultExtractAgentAddress(req) === agentId ? "key" : "onchain")
|
|
300
|
+
: "missing";
|
|
248
301
|
if (!agentId) {
|
|
302
|
+
emitGateTelemetry(frozen.sdk, {
|
|
303
|
+
framework: "express",
|
|
304
|
+
agentType: "missing",
|
|
305
|
+
minScore,
|
|
306
|
+
trustSignals: frozen.trustSignals,
|
|
307
|
+
status: "missing_identity",
|
|
308
|
+
latencyMs,
|
|
309
|
+
});
|
|
249
310
|
return res.status(401).json({
|
|
250
311
|
error: "Missing agent identity",
|
|
251
312
|
message: "Provide an x-agent-id or x-agent-address header. If you don't have an identity yet, see the Valiron Identity Skill.",
|
|
@@ -253,10 +314,33 @@ function createValironGate(config) {
|
|
|
253
314
|
});
|
|
254
315
|
}
|
|
255
316
|
if (error) {
|
|
317
|
+
emitGateTelemetry(frozen.sdk, {
|
|
318
|
+
framework: "express",
|
|
319
|
+
agentType,
|
|
320
|
+
minScore,
|
|
321
|
+
trustSignals: frozen.trustSignals,
|
|
322
|
+
status: "error",
|
|
323
|
+
latencyMs,
|
|
324
|
+
});
|
|
256
325
|
return res.status(503).json({
|
|
257
326
|
error: "Trust gate unavailable \u2014 request denied (fail-closed)",
|
|
258
327
|
});
|
|
259
328
|
}
|
|
329
|
+
emitGateTelemetry(frozen.sdk, {
|
|
330
|
+
framework: "express",
|
|
331
|
+
agentType,
|
|
332
|
+
chain: result.chain?.network,
|
|
333
|
+
allow: result.allow,
|
|
334
|
+
route: result.route,
|
|
335
|
+
tier: result.tier,
|
|
336
|
+
riskLevel: result.riskLevel,
|
|
337
|
+
score: result.score,
|
|
338
|
+
minScore,
|
|
339
|
+
cached: result.cached,
|
|
340
|
+
pendingEvaluation,
|
|
341
|
+
trustSignals: frozen.trustSignals,
|
|
342
|
+
latencyMs,
|
|
343
|
+
});
|
|
260
344
|
if (result.allow) {
|
|
261
345
|
req.valiron = result;
|
|
262
346
|
if (config.onAllow)
|
|
@@ -341,8 +425,22 @@ async function valironFastifyPlugin(fastify, opts) {
|
|
|
341
425
|
if (pathname !== prefix && !pathname.startsWith(prefix + "/"))
|
|
342
426
|
return;
|
|
343
427
|
}
|
|
428
|
+
const startedAt = Date.now();
|
|
344
429
|
const { agentId, result, error, pendingEvaluation } = await runGateCheck(gateConfig, request, cache);
|
|
430
|
+
const latencyMs = Date.now() - startedAt;
|
|
431
|
+
const minScore = opts.minScore ?? 65;
|
|
432
|
+
const agentType = agentId
|
|
433
|
+
? (defaultExtractAgentAddress(request) === agentId ? "key" : "onchain")
|
|
434
|
+
: "missing";
|
|
345
435
|
if (!agentId) {
|
|
436
|
+
emitGateTelemetry(opts.sdk, {
|
|
437
|
+
framework: "fastify",
|
|
438
|
+
agentType: "missing",
|
|
439
|
+
minScore,
|
|
440
|
+
trustSignals: opts.trustSignals,
|
|
441
|
+
status: "missing_identity",
|
|
442
|
+
latencyMs,
|
|
443
|
+
});
|
|
346
444
|
return reply.status(401).send({
|
|
347
445
|
error: "Missing agent identity",
|
|
348
446
|
message: "Provide an x-agent-id or x-agent-address header. If you don't have an identity yet, see the Valiron Identity Skill.",
|
|
@@ -350,10 +448,33 @@ async function valironFastifyPlugin(fastify, opts) {
|
|
|
350
448
|
});
|
|
351
449
|
}
|
|
352
450
|
if (error) {
|
|
451
|
+
emitGateTelemetry(opts.sdk, {
|
|
452
|
+
framework: "fastify",
|
|
453
|
+
agentType,
|
|
454
|
+
minScore,
|
|
455
|
+
trustSignals: opts.trustSignals,
|
|
456
|
+
status: "error",
|
|
457
|
+
latencyMs,
|
|
458
|
+
});
|
|
353
459
|
return reply.status(503).send({
|
|
354
460
|
error: "Trust gate unavailable \u2014 request denied (fail-closed)",
|
|
355
461
|
});
|
|
356
462
|
}
|
|
463
|
+
emitGateTelemetry(opts.sdk, {
|
|
464
|
+
framework: "fastify",
|
|
465
|
+
agentType,
|
|
466
|
+
chain: result.chain?.network,
|
|
467
|
+
allow: result.allow,
|
|
468
|
+
route: result.route,
|
|
469
|
+
tier: result.tier,
|
|
470
|
+
riskLevel: result.riskLevel,
|
|
471
|
+
score: result.score,
|
|
472
|
+
minScore,
|
|
473
|
+
cached: result.cached,
|
|
474
|
+
pendingEvaluation,
|
|
475
|
+
trustSignals: opts.trustSignals,
|
|
476
|
+
latencyMs,
|
|
477
|
+
});
|
|
357
478
|
if (result.allow) {
|
|
358
479
|
request.valiron = result;
|
|
359
480
|
if (opts.onAllow)
|
|
@@ -406,13 +527,14 @@ async function valironFastifyPlugin(fastify, opts) {
|
|
|
406
527
|
* ```
|
|
407
528
|
*/
|
|
408
529
|
function createValironNextMiddleware(config) {
|
|
409
|
-
const { sdk, minScore, paths } = config;
|
|
530
|
+
const { sdk, minScore, paths, trustSignals } = config;
|
|
410
531
|
const cache = createProfileCache();
|
|
411
532
|
const extractAgentId = config.extractAgentId ||
|
|
412
533
|
((req) => req.headers.get("x-agent-id"));
|
|
413
534
|
const extractAddress = config.extractAgentAddress ||
|
|
414
535
|
((req) => req.headers.get("x-agent-address") || req.headers.get("x-agent-key-hash"));
|
|
415
536
|
return async function valironNextGate(request) {
|
|
537
|
+
const startedAt = Date.now();
|
|
416
538
|
// Skip paths not in scope
|
|
417
539
|
if (paths && paths.length > 0) {
|
|
418
540
|
const url = new URL(request.url);
|
|
@@ -424,6 +546,14 @@ function createValironNextMiddleware(config) {
|
|
|
424
546
|
const agentId = extractAgentId(request);
|
|
425
547
|
const agentAddr = agentId ? null : extractAddress(request);
|
|
426
548
|
if (!agentId && !agentAddr) {
|
|
549
|
+
emitGateTelemetry(sdk, {
|
|
550
|
+
framework: "next",
|
|
551
|
+
agentType: "missing",
|
|
552
|
+
minScore: minScore ?? 65,
|
|
553
|
+
trustSignals,
|
|
554
|
+
status: "missing_identity",
|
|
555
|
+
latencyMs: Date.now() - startedAt,
|
|
556
|
+
});
|
|
427
557
|
return Response.json({
|
|
428
558
|
error: "Missing agent identity",
|
|
429
559
|
message: "Provide an x-agent-id or x-agent-address header. If you don't have an identity yet, see the Valiron Identity Skill.",
|
|
@@ -440,13 +570,30 @@ function createValironNextMiddleware(config) {
|
|
|
440
570
|
if (cacheTtl > 0) {
|
|
441
571
|
const cached = cache.getCached(identifier, cacheTtl);
|
|
442
572
|
if (cached) {
|
|
573
|
+
emitGateTelemetry(sdk, {
|
|
574
|
+
framework: "next",
|
|
575
|
+
agentType: agentId ? "onchain" : "key",
|
|
576
|
+
chain: cached.result.chain?.network,
|
|
577
|
+
allow: cached.result.allow,
|
|
578
|
+
route: cached.result.route,
|
|
579
|
+
tier: cached.result.tier,
|
|
580
|
+
riskLevel: cached.result.riskLevel,
|
|
581
|
+
score: cached.result.score,
|
|
582
|
+
minScore: effectiveMinScore,
|
|
583
|
+
cached: true,
|
|
584
|
+
pendingEvaluation: cached.pendingEvaluation,
|
|
585
|
+
trustSignals,
|
|
586
|
+
latencyMs: Date.now() - startedAt,
|
|
587
|
+
});
|
|
443
588
|
if (cached.result.allow)
|
|
444
589
|
return undefined;
|
|
445
590
|
return Response.json({ error: "Agent blocked by Valiron trust gate", ...cached.result }, { status: 403 });
|
|
446
591
|
}
|
|
447
592
|
}
|
|
448
593
|
if (agentId) {
|
|
449
|
-
const profile = await sdk.getAgentProfile(agentId
|
|
594
|
+
const profile = await sdk.getAgentProfile(agentId, {
|
|
595
|
+
trustSignals: normalizeTrustSignals(trustSignals),
|
|
596
|
+
});
|
|
450
597
|
result = profileToGateResult(profile, effectiveMinScore);
|
|
451
598
|
// Detect brand-new agent: on-chain identity exists but zero reputation.
|
|
452
599
|
if (!result.allow && (profile.onchainReputation?.totalFeedback ?? 0) === 0) {
|
|
@@ -467,6 +614,21 @@ function createValironNextMiddleware(config) {
|
|
|
467
614
|
}
|
|
468
615
|
if (cacheTtl > 0)
|
|
469
616
|
cache.setCache(identifier, result, pendingEvaluation || undefined);
|
|
617
|
+
emitGateTelemetry(sdk, {
|
|
618
|
+
framework: "next",
|
|
619
|
+
agentType: agentId ? "onchain" : "key",
|
|
620
|
+
chain: result.chain?.network,
|
|
621
|
+
allow: result.allow,
|
|
622
|
+
route: result.route,
|
|
623
|
+
tier: result.tier,
|
|
624
|
+
riskLevel: result.riskLevel,
|
|
625
|
+
score: result.score,
|
|
626
|
+
minScore: effectiveMinScore,
|
|
627
|
+
cached: result.cached,
|
|
628
|
+
pendingEvaluation,
|
|
629
|
+
trustSignals,
|
|
630
|
+
latencyMs: Date.now() - startedAt,
|
|
631
|
+
});
|
|
470
632
|
if (result.allow) {
|
|
471
633
|
return undefined; // continue to route handler
|
|
472
634
|
}
|
|
@@ -481,6 +643,14 @@ function createValironNextMiddleware(config) {
|
|
|
481
643
|
return Response.json({ error: "Agent blocked by Valiron trust gate", ...result }, { status: 403 });
|
|
482
644
|
}
|
|
483
645
|
catch {
|
|
646
|
+
emitGateTelemetry(sdk, {
|
|
647
|
+
framework: "next",
|
|
648
|
+
agentType: agentId ? "onchain" : "key",
|
|
649
|
+
minScore: effectiveMinScore,
|
|
650
|
+
trustSignals,
|
|
651
|
+
status: "error",
|
|
652
|
+
latencyMs: Date.now() - startedAt,
|
|
653
|
+
});
|
|
484
654
|
return Response.json({ error: "Trust gate unavailable \u2014 request denied (fail-closed)" }, { status: 503 });
|
|
485
655
|
}
|
|
486
656
|
};
|
|
@@ -528,9 +698,18 @@ function createValironNextMiddleware(config) {
|
|
|
528
698
|
* ```
|
|
529
699
|
*/
|
|
530
700
|
async function valironGateCheck(sdk, opts) {
|
|
531
|
-
const { agentId, agentAddress, minScore } = opts;
|
|
701
|
+
const { agentId, agentAddress, minScore, trustSignals } = opts;
|
|
702
|
+
const startedAt = Date.now();
|
|
532
703
|
const identifier = agentId || agentAddress || null;
|
|
533
704
|
if (!identifier) {
|
|
705
|
+
emitGateTelemetry(sdk, {
|
|
706
|
+
framework: "generic",
|
|
707
|
+
agentType: "missing",
|
|
708
|
+
minScore: minScore ?? 65,
|
|
709
|
+
trustSignals,
|
|
710
|
+
status: "missing_identity",
|
|
711
|
+
latencyMs: Date.now() - startedAt,
|
|
712
|
+
});
|
|
534
713
|
return {
|
|
535
714
|
hasAgent: false,
|
|
536
715
|
agentId: null,
|
|
@@ -551,6 +730,21 @@ async function valironGateCheck(sdk, opts) {
|
|
|
551
730
|
if (cacheTtl > 0) {
|
|
552
731
|
const cached = cache.getCached(identifier, cacheTtl);
|
|
553
732
|
if (cached) {
|
|
733
|
+
emitGateTelemetry(sdk, {
|
|
734
|
+
framework: "generic",
|
|
735
|
+
agentType: agentId ? "onchain" : "key",
|
|
736
|
+
chain: cached.result.chain?.network,
|
|
737
|
+
allow: cached.result.allow,
|
|
738
|
+
route: cached.result.route,
|
|
739
|
+
tier: cached.result.tier,
|
|
740
|
+
riskLevel: cached.result.riskLevel,
|
|
741
|
+
score: cached.result.score,
|
|
742
|
+
minScore: effectiveMinScore,
|
|
743
|
+
cached: true,
|
|
744
|
+
pendingEvaluation: cached.pendingEvaluation,
|
|
745
|
+
trustSignals,
|
|
746
|
+
latencyMs: Date.now() - startedAt,
|
|
747
|
+
});
|
|
554
748
|
if (cached.result.allow) {
|
|
555
749
|
return { hasAgent: true, agentId: identifier, ok: true, allow: true, result: cached.result, status: 200 };
|
|
556
750
|
}
|
|
@@ -570,7 +764,9 @@ async function valironGateCheck(sdk, opts) {
|
|
|
570
764
|
let result;
|
|
571
765
|
let pendingEvaluation = false;
|
|
572
766
|
if (agentId) {
|
|
573
|
-
const profile = await sdk.getAgentProfile(agentId
|
|
767
|
+
const profile = await sdk.getAgentProfile(agentId, {
|
|
768
|
+
trustSignals: normalizeTrustSignals(trustSignals),
|
|
769
|
+
});
|
|
574
770
|
result = profileToGateResult(profile, effectiveMinScore);
|
|
575
771
|
// Detect brand-new agent: on-chain identity exists but zero reputation.
|
|
576
772
|
if (!result.allow && (profile.onchainReputation?.totalFeedback ?? 0) === 0) {
|
|
@@ -591,6 +787,21 @@ async function valironGateCheck(sdk, opts) {
|
|
|
591
787
|
}
|
|
592
788
|
if (cacheTtl > 0)
|
|
593
789
|
cache.setCache(identifier, result, pendingEvaluation || undefined);
|
|
790
|
+
emitGateTelemetry(sdk, {
|
|
791
|
+
framework: "generic",
|
|
792
|
+
agentType: agentId ? "onchain" : "key",
|
|
793
|
+
chain: result.chain?.network,
|
|
794
|
+
allow: result.allow,
|
|
795
|
+
route: result.route,
|
|
796
|
+
tier: result.tier,
|
|
797
|
+
riskLevel: result.riskLevel,
|
|
798
|
+
score: result.score,
|
|
799
|
+
minScore: effectiveMinScore,
|
|
800
|
+
cached: result.cached,
|
|
801
|
+
pendingEvaluation,
|
|
802
|
+
trustSignals,
|
|
803
|
+
latencyMs: Date.now() - startedAt,
|
|
804
|
+
});
|
|
594
805
|
if (result.allow) {
|
|
595
806
|
return { hasAgent: true, agentId: identifier, ok: true, allow: true, result, status: 200 };
|
|
596
807
|
}
|
|
@@ -622,6 +833,14 @@ async function valironGateCheck(sdk, opts) {
|
|
|
622
833
|
};
|
|
623
834
|
}
|
|
624
835
|
catch {
|
|
836
|
+
emitGateTelemetry(sdk, {
|
|
837
|
+
framework: "generic",
|
|
838
|
+
agentType: agentId ? "onchain" : "key",
|
|
839
|
+
minScore: effectiveMinScore,
|
|
840
|
+
trustSignals,
|
|
841
|
+
status: "error",
|
|
842
|
+
latencyMs: Date.now() - startedAt,
|
|
843
|
+
});
|
|
625
844
|
return {
|
|
626
845
|
hasAgent: true,
|
|
627
846
|
agentId: identifier,
|
package/dist/operatorSdk.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ValironSDK } from "./index.js";
|
|
2
|
-
import type { SupportedChain } from "./types.js";
|
|
2
|
+
import type { SupportedChain, TrustSignal } from "./types.js";
|
|
3
3
|
/**
|
|
4
4
|
* Configuration for the Valiron Operator SDK.
|
|
5
5
|
*/
|
|
@@ -23,6 +23,8 @@ export interface PaywallConfig {
|
|
|
23
23
|
pricePerCall: number;
|
|
24
24
|
/** Minimum trust score to allow (default: 45). */
|
|
25
25
|
minTrustScore?: number;
|
|
26
|
+
/** Customer-selected scoring signals. Free plans support 8004+sandbox; Pro adds world+self+icebreaker. */
|
|
27
|
+
trustSignals?: TrustSignal[];
|
|
26
28
|
/**
|
|
27
29
|
* Server-side gate result TTL in ms (default: 86400000 = 24h).
|
|
28
30
|
* @deprecated Use `cacheTtlMs` for client-side cache control.
|
|
@@ -66,7 +68,15 @@ export declare class ValironOperator {
|
|
|
66
68
|
private sdk;
|
|
67
69
|
private apiKey;
|
|
68
70
|
private debug;
|
|
71
|
+
private endpoint;
|
|
69
72
|
constructor(config: ValironOperatorConfig);
|
|
73
|
+
private buildAnalyticsUrl;
|
|
74
|
+
private recordUsage;
|
|
75
|
+
private extractHeader;
|
|
76
|
+
private expressPath;
|
|
77
|
+
private expressIp;
|
|
78
|
+
private fastifyPath;
|
|
79
|
+
private normalizeTrustSignals;
|
|
70
80
|
/**
|
|
71
81
|
* Create Express/Connect middleware that gates access + tracks usage.
|
|
72
82
|
*
|
package/dist/operatorSdk.js
CHANGED
|
@@ -31,6 +31,7 @@ class ValironOperator {
|
|
|
31
31
|
}
|
|
32
32
|
this.apiKey = config.apiKey;
|
|
33
33
|
this.debug = config.debug || false;
|
|
34
|
+
this.endpoint = config.endpoint || "https://valiron-edge-proxy.onrender.com";
|
|
34
35
|
this.sdk = new index_js_1.ValironSDK({
|
|
35
36
|
apiKey: config.apiKey,
|
|
36
37
|
chain: config.chain,
|
|
@@ -39,6 +40,66 @@ class ValironOperator {
|
|
|
39
40
|
debug: config.debug,
|
|
40
41
|
});
|
|
41
42
|
}
|
|
43
|
+
buildAnalyticsUrl() {
|
|
44
|
+
return new URL("/operator/analytics/ingest", this.endpoint).toString();
|
|
45
|
+
}
|
|
46
|
+
recordUsage(entry) {
|
|
47
|
+
const trustSignals = this.normalizeTrustSignals(entry.trustSignals);
|
|
48
|
+
const body = {
|
|
49
|
+
agent_id: entry.agentId || undefined,
|
|
50
|
+
agent_label: entry.agentLabel || undefined,
|
|
51
|
+
session_id: entry.sessionId || undefined,
|
|
52
|
+
chain: entry.chain || undefined,
|
|
53
|
+
path: entry.path,
|
|
54
|
+
method: entry.method,
|
|
55
|
+
status_code: entry.statusCode,
|
|
56
|
+
price_charged: entry.priceCharged,
|
|
57
|
+
response_ms: entry.responseMs,
|
|
58
|
+
user_agent: entry.userAgent || undefined,
|
|
59
|
+
ip_address: entry.ipAddress || undefined,
|
|
60
|
+
error_message: entry.errorMessage || undefined,
|
|
61
|
+
trust_signals: trustSignals,
|
|
62
|
+
trust_signal_count: trustSignals.length,
|
|
63
|
+
scoring_mode: trustSignals.length ? trustSignals.join("+") : undefined,
|
|
64
|
+
erc8004_included: trustSignals.includes("8004"),
|
|
65
|
+
sandbox_included: trustSignals.includes("sandbox"),
|
|
66
|
+
world_included: trustSignals.includes("world"),
|
|
67
|
+
icebreaker_included: trustSignals.includes("icebreaker"),
|
|
68
|
+
self_included: trustSignals.includes("self"),
|
|
69
|
+
};
|
|
70
|
+
void fetch(this.buildAnalyticsUrl(), {
|
|
71
|
+
method: "POST",
|
|
72
|
+
headers: {
|
|
73
|
+
"Content-Type": "application/json",
|
|
74
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
75
|
+
},
|
|
76
|
+
body: JSON.stringify(body),
|
|
77
|
+
})
|
|
78
|
+
.then((response) => response.arrayBuffer().catch(() => response.body?.cancel()))
|
|
79
|
+
.catch(() => {
|
|
80
|
+
// Best-effort usage analytics: never affect seller request handling.
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
extractHeader(source, name) {
|
|
84
|
+
const headers = source?.headers ?? {};
|
|
85
|
+
if (typeof headers.get === "function")
|
|
86
|
+
return headers.get(name);
|
|
87
|
+
const direct = headers[name] ?? headers[name.toLowerCase()];
|
|
88
|
+
return Array.isArray(direct) ? direct[0] : direct ?? null;
|
|
89
|
+
}
|
|
90
|
+
expressPath(req) {
|
|
91
|
+
return req.originalUrl || req.url || req.path || "/";
|
|
92
|
+
}
|
|
93
|
+
expressIp(req) {
|
|
94
|
+
const forwarded = this.extractHeader(req, "x-forwarded-for")?.split(",")[0]?.trim();
|
|
95
|
+
return forwarded || this.extractHeader(req, "x-real-ip") || req.ip || req.socket?.remoteAddress || null;
|
|
96
|
+
}
|
|
97
|
+
fastifyPath(req) {
|
|
98
|
+
return req.url || req.routerPath || "/";
|
|
99
|
+
}
|
|
100
|
+
normalizeTrustSignals(signals) {
|
|
101
|
+
return signals?.length ? [...signals].sort() : [];
|
|
102
|
+
}
|
|
42
103
|
/**
|
|
43
104
|
* Create Express/Connect middleware that gates access + tracks usage.
|
|
44
105
|
*
|
|
@@ -63,6 +124,7 @@ class ValironOperator {
|
|
|
63
124
|
const gateConfig = {
|
|
64
125
|
sdk: this.sdk,
|
|
65
126
|
minScore: config.minTrustScore ?? 45,
|
|
127
|
+
trustSignals: config.trustSignals,
|
|
66
128
|
cacheTtlMs: config.cacheTtlMs ?? config.ttlMs,
|
|
67
129
|
onDeny: config.onDeny,
|
|
68
130
|
onAllow: (req, result) => {
|
|
@@ -74,13 +136,55 @@ class ValironOperator {
|
|
|
74
136
|
},
|
|
75
137
|
};
|
|
76
138
|
const middleware = (0, middleware_js_1.createValironGate)(gateConfig);
|
|
139
|
+
const withUsageLogging = (req, res, next) => {
|
|
140
|
+
const startedAt = Date.now();
|
|
141
|
+
let logged = false;
|
|
142
|
+
const logOnce = () => {
|
|
143
|
+
if (logged)
|
|
144
|
+
return;
|
|
145
|
+
logged = true;
|
|
146
|
+
const gate = req.valiron;
|
|
147
|
+
const statusCode = res.statusCode;
|
|
148
|
+
const allowed = Boolean(gate?.allow);
|
|
149
|
+
const chargeable = allowed && statusCode >= 200 && statusCode < 400;
|
|
150
|
+
const errorMessage = !allowed
|
|
151
|
+
? "gate_denied"
|
|
152
|
+
: statusCode >= 400
|
|
153
|
+
? `downstream_${statusCode}`
|
|
154
|
+
: undefined;
|
|
155
|
+
const agentId = gate?.agentId ||
|
|
156
|
+
this.extractHeader(req, "x-agent-id") ||
|
|
157
|
+
this.extractHeader(req, "x-agent-address") ||
|
|
158
|
+
this.extractHeader(req, "x-agent-key-hash");
|
|
159
|
+
this.recordUsage({
|
|
160
|
+
agentId,
|
|
161
|
+
agentLabel: gate?.tier ? `${gate.tier}:${gate.route}` : undefined,
|
|
162
|
+
sessionId: this.extractHeader(req, "x-valiron-session-id") || this.extractHeader(req, "x-request-id"),
|
|
163
|
+
chain: gate?.chain?.network || this.extractHeader(req, "x-chain"),
|
|
164
|
+
path: this.expressPath(req).split("?")[0] || "/",
|
|
165
|
+
method: req.method || "GET",
|
|
166
|
+
statusCode,
|
|
167
|
+
priceCharged: chargeable ? config.pricePerCall : 0,
|
|
168
|
+
responseMs: Date.now() - startedAt,
|
|
169
|
+
userAgent: this.extractHeader(req, "user-agent"),
|
|
170
|
+
ipAddress: this.expressIp(req),
|
|
171
|
+
errorMessage,
|
|
172
|
+
trustSignals: config.trustSignals,
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
if (typeof res.once === "function") {
|
|
176
|
+
res.once("finish", logOnce);
|
|
177
|
+
res.once("close", logOnce);
|
|
178
|
+
}
|
|
179
|
+
return middleware(req, res, next);
|
|
180
|
+
};
|
|
77
181
|
if (this.debug) {
|
|
78
182
|
return async (req, res, next) => {
|
|
79
183
|
console.log(`[ValironOperator] Gate check for ${req.headers?.["x-agent-id"] || "unknown"} @ $${config.pricePerCall}/call`);
|
|
80
|
-
return
|
|
184
|
+
return withUsageLogging(req, res, next);
|
|
81
185
|
};
|
|
82
186
|
}
|
|
83
|
-
return
|
|
187
|
+
return withUsageLogging;
|
|
84
188
|
}
|
|
85
189
|
/**
|
|
86
190
|
* Create a Fastify plugin that gates access + tracks usage.
|
|
@@ -98,6 +202,7 @@ class ValironOperator {
|
|
|
98
202
|
const pluginConfig = {
|
|
99
203
|
sdk: this.sdk,
|
|
100
204
|
minScore: config.minTrustScore ?? 45,
|
|
205
|
+
trustSignals: config.trustSignals,
|
|
101
206
|
cacheTtlMs: config.cacheTtlMs ?? config.ttlMs,
|
|
102
207
|
prefix: config.prefix,
|
|
103
208
|
onDeny: config.onDeny,
|
|
@@ -109,7 +214,51 @@ class ValironOperator {
|
|
|
109
214
|
},
|
|
110
215
|
};
|
|
111
216
|
return (fastify, opts, done) => {
|
|
112
|
-
|
|
217
|
+
fastify.addHook?.("onRequest", async (request) => {
|
|
218
|
+
request.valironUsageStartedAt = Date.now();
|
|
219
|
+
});
|
|
220
|
+
(0, middleware_js_1.valironFastifyPlugin)(fastify, { ...pluginConfig, ...opts }).then(() => {
|
|
221
|
+
fastify.addHook?.("onResponse", async (request, reply) => {
|
|
222
|
+
const rawUrl = request.url || "";
|
|
223
|
+
if (config.prefix) {
|
|
224
|
+
const pathname = rawUrl.split("?")[0];
|
|
225
|
+
const prefix = config.prefix.endsWith("/") && config.prefix !== "/"
|
|
226
|
+
? config.prefix.slice(0, -1)
|
|
227
|
+
: config.prefix;
|
|
228
|
+
if (pathname !== prefix && !pathname.startsWith(prefix + "/"))
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const gate = request.valiron;
|
|
232
|
+
const statusCode = reply.statusCode;
|
|
233
|
+
const allowed = Boolean(gate?.allow);
|
|
234
|
+
const chargeable = allowed && statusCode >= 200 && statusCode < 400;
|
|
235
|
+
const errorMessage = !allowed
|
|
236
|
+
? "gate_denied"
|
|
237
|
+
: statusCode >= 400
|
|
238
|
+
? `downstream_${statusCode}`
|
|
239
|
+
: undefined;
|
|
240
|
+
const agentId = gate?.agentId ||
|
|
241
|
+
this.extractHeader(request, "x-agent-id") ||
|
|
242
|
+
this.extractHeader(request, "x-agent-address") ||
|
|
243
|
+
this.extractHeader(request, "x-agent-key-hash");
|
|
244
|
+
this.recordUsage({
|
|
245
|
+
agentId,
|
|
246
|
+
agentLabel: gate?.tier ? `${gate.tier}:${gate.route}` : undefined,
|
|
247
|
+
sessionId: this.extractHeader(request, "x-valiron-session-id") || this.extractHeader(request, "x-request-id"),
|
|
248
|
+
chain: gate?.chain?.network || this.extractHeader(request, "x-chain"),
|
|
249
|
+
path: this.fastifyPath(request).split("?")[0] || "/",
|
|
250
|
+
method: request.method || "GET",
|
|
251
|
+
statusCode,
|
|
252
|
+
priceCharged: chargeable ? config.pricePerCall : 0,
|
|
253
|
+
responseMs: Date.now() - (request.valironUsageStartedAt || Date.now()),
|
|
254
|
+
userAgent: this.extractHeader(request, "user-agent"),
|
|
255
|
+
ipAddress: request.ip || this.extractHeader(request, "x-forwarded-for")?.split(",")[0]?.trim() || null,
|
|
256
|
+
errorMessage,
|
|
257
|
+
trustSignals: config.trustSignals,
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
done();
|
|
261
|
+
}).catch(done);
|
|
113
262
|
};
|
|
114
263
|
}
|
|
115
264
|
/**
|
package/dist/telemetry.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TelemetryClient = void 0;
|
|
4
|
-
const SDK_VERSION = "0.
|
|
4
|
+
const SDK_VERSION = "1.0.2";
|
|
5
5
|
function detectRuntime() {
|
|
6
6
|
return typeof globalThis.window === "undefined" ? "node" : "browser";
|
|
7
7
|
}
|