@lucern/sdk 1.0.1 → 1.0.2
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/CHANGELOG.md +4 -0
- package/dist/accessControl.d.ts +1 -0
- package/dist/accessControl.js +213 -26
- package/dist/accessControl.js.map +1 -1
- package/dist/adminClient.d.ts +1 -0
- package/dist/adminClient.js +213 -26
- package/dist/adminClient.js.map +1 -1
- package/dist/answersClient.d.ts +1 -0
- package/dist/answersClient.js +213 -26
- package/dist/answersClient.js.map +1 -1
- package/dist/audiencesClient.d.ts +1 -0
- package/dist/audiencesClient.js +213 -26
- package/dist/audiencesClient.js.map +1 -1
- package/dist/auditClient.d.ts +1 -0
- package/dist/auditClient.js +213 -26
- package/dist/auditClient.js.map +1 -1
- package/dist/authDeviceClient.d.ts +1 -0
- package/dist/beliefs/index.d.ts +1 -0
- package/dist/beliefs/index.js +214 -27
- package/dist/beliefs/index.js.map +1 -1
- package/dist/beliefsClient.d.ts +1 -0
- package/dist/beliefsClient.js +213 -26
- package/dist/beliefsClient.js.map +1 -1
- package/dist/client.d.ts +1 -0
- package/dist/client.js +214 -27
- package/dist/client.js.map +1 -1
- package/dist/clientConfig.d.ts +1 -0
- package/dist/clientEvidenceCompat.d.ts +1 -0
- package/dist/clientKnowledgeNamespaces.d.ts +1 -0
- package/dist/clientLocalHelpers.d.ts +1 -0
- package/dist/clientLocalHelpers.js +2 -0
- package/dist/clientLocalHelpers.js.map +1 -1
- package/dist/clientPlatformNamespaces.d.ts +1 -0
- package/dist/clientRuntime.d.ts +1 -0
- package/dist/clientWorkflowNamespaces.d.ts +1 -0
- package/dist/contextClient.d.ts +1 -0
- package/dist/contextClient.js +213 -26
- package/dist/contextClient.js.map +1 -1
- package/dist/contradictions/index.d.ts +1 -0
- package/dist/contradictions/index.js +214 -27
- package/dist/contradictions/index.js.map +1 -1
- package/dist/control-plane.d.ts +1 -0
- package/dist/control-plane.js +213 -26
- package/dist/control-plane.js.map +1 -1
- package/dist/coreClient.d.ts +25 -1
- package/dist/coreClient.js +217 -27
- package/dist/coreClient.js.map +1 -1
- package/dist/decisions/index.d.ts +1 -0
- package/dist/decisions/index.js +214 -27
- package/dist/decisions/index.js.map +1 -1
- package/dist/decisionsClient.d.ts +1 -0
- package/dist/decisionsClient.js +213 -26
- package/dist/decisionsClient.js.map +1 -1
- package/dist/edges/index.d.ts +1 -0
- package/dist/edges/index.js +214 -27
- package/dist/edges/index.js.map +1 -1
- package/dist/embeddingsClient.d.ts +1 -0
- package/dist/embeddingsClient.js +213 -26
- package/dist/embeddingsClient.js.map +1 -1
- package/dist/eventingClient.d.ts +1 -0
- package/dist/eventingClient.js +213 -26
- package/dist/eventingClient.js.map +1 -1
- package/dist/eventsCore.d.ts +1 -0
- package/dist/eventsCore.js +213 -26
- package/dist/eventsCore.js.map +1 -1
- package/dist/evidence/index.d.ts +1 -0
- package/dist/evidence/index.js +214 -27
- package/dist/evidence/index.js.map +1 -1
- package/dist/evidenceClient.d.ts +1 -0
- package/dist/evidenceClient.js +213 -26
- package/dist/evidenceClient.js.map +1 -1
- package/dist/functionSurface.d.ts +1 -0
- package/dist/functionSurface.js +213 -26
- package/dist/functionSurface.js.map +1 -1
- package/dist/functionSurfaceClient.d.ts +1 -0
- package/dist/functionSurfaceClient.js +213 -26
- package/dist/functionSurfaceClient.js.map +1 -1
- package/dist/gatewayFacades.d.ts +1 -0
- package/dist/gatewayFacades.factories.d.ts +1 -0
- package/dist/gatewayFacades.factories.js +213 -26
- package/dist/gatewayFacades.factories.js.map +1 -1
- package/dist/gatewayFacades.js +213 -26
- package/dist/gatewayFacades.js.map +1 -1
- package/dist/graphAnalysisClient.d.ts +1 -0
- package/dist/graphAnalysisClient.js +213 -26
- package/dist/graphAnalysisClient.js.map +1 -1
- package/dist/graphClient.d.ts +1 -0
- package/dist/graphClient.js +213 -26
- package/dist/graphClient.js.map +1 -1
- package/dist/graphRecommendationsClient.d.ts +1 -0
- package/dist/graphRecommendationsClient.js +213 -26
- package/dist/graphRecommendationsClient.js.map +1 -1
- package/dist/graphStateClassifierClient.d.ts +1 -0
- package/dist/graphStateClassifierClient.js +213 -26
- package/dist/graphStateClassifierClient.js.map +1 -1
- package/dist/harnessClient.d.ts +1 -0
- package/dist/harnessClient.js +213 -26
- package/dist/harnessClient.js.map +1 -1
- package/dist/identityClient.d.ts +1 -0
- package/dist/identityClient.js +213 -26
- package/dist/identityClient.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +237 -28
- package/dist/index.js.map +1 -1
- package/dist/jobsClient.d.ts +1 -0
- package/dist/jobsClient.js +213 -26
- package/dist/jobsClient.js.map +1 -1
- package/dist/learningClient.d.ts +1 -0
- package/dist/learningClient.js +213 -26
- package/dist/learningClient.js.map +1 -1
- package/dist/lenses/index.d.ts +1 -0
- package/dist/lenses/index.js +214 -27
- package/dist/lenses/index.js.map +1 -1
- package/dist/mcpClient.d.ts +1 -0
- package/dist/mcpClient.js +213 -26
- package/dist/mcpClient.js.map +1 -1
- package/dist/modelRuntimeClient.d.ts +1 -0
- package/dist/modelRuntimeClient.js +213 -26
- package/dist/modelRuntimeClient.js.map +1 -1
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.js +214 -27
- package/dist/nodes/index.js.map +1 -1
- package/dist/ontologies/index.d.ts +1 -0
- package/dist/ontologies/index.js +214 -27
- package/dist/ontologies/index.js.map +1 -1
- package/dist/ontologyClient.d.ts +1 -0
- package/dist/ontologyClient.js +213 -26
- package/dist/ontologyClient.js.map +1 -1
- package/dist/ontologyLinksClient.d.ts +1 -0
- package/dist/ontologyLinksClient.js +213 -26
- package/dist/ontologyLinksClient.js.map +1 -1
- package/dist/orgGraphSearchClient.d.ts +1 -0
- package/dist/orgGraphSearchClient.js +213 -26
- package/dist/orgGraphSearchClient.js.map +1 -1
- package/dist/packsClient.d.ts +1 -0
- package/dist/packsClient.js +213 -26
- package/dist/packsClient.js.map +1 -1
- package/dist/policyClient.d.ts +1 -0
- package/dist/policyClient.js +213 -26
- package/dist/policyClient.js.map +1 -1
- package/dist/questions/index.d.ts +1 -0
- package/dist/questions/index.js +214 -27
- package/dist/questions/index.js.map +1 -1
- package/dist/reportsClient.d.ts +1 -0
- package/dist/reportsClient.js +213 -26
- package/dist/reportsClient.js.map +1 -1
- package/dist/schemaClient.d.ts +1 -0
- package/dist/schemaClient.js +213 -26
- package/dist/schemaClient.js.map +1 -1
- package/dist/sdkSurface.d.ts +1 -0
- package/dist/sourcesClient.d.ts +1 -0
- package/dist/sourcesClient.js +213 -26
- package/dist/sourcesClient.js.map +1 -1
- package/dist/telemetryClient.d.ts +1 -0
- package/dist/telemetryClient.js +213 -26
- package/dist/telemetryClient.js.map +1 -1
- package/dist/toolRegistryClient.d.ts +1 -0
- package/dist/toolRegistryClient.js +213 -26
- package/dist/toolRegistryClient.js.map +1 -1
- package/dist/topics/index.d.ts +1 -0
- package/dist/topics/index.js +214 -27
- package/dist/topics/index.js.map +1 -1
- package/dist/topicsClient.d.ts +1 -0
- package/dist/topicsClient.js +213 -26
- package/dist/topicsClient.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/dist/workflowClient.d.ts +1 -0
- package/dist/workflowClient.js +213 -26
- package/dist/workflowClient.js.map +1 -1
- package/dist/worktrees/index.d.ts +1 -0
- package/dist/worktrees/index.js +214 -27
- package/dist/worktrees/index.js.map +1 -1
- package/package.json +6 -5
package/dist/learningClient.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { createTelemetryExporterFromEnv, emitTelemetrySignal } from '@lucern/transport-core';
|
|
1
2
|
import { redactDiagnosticValue } from '@lucern/transport-core/redaction';
|
|
2
3
|
import { classifyRetry } from '@lucern/transport-core/transport';
|
|
4
|
+
import { Effect, Exit, Cause } from 'effect';
|
|
3
5
|
|
|
4
6
|
// src/coreClient.ts
|
|
5
7
|
|
|
@@ -171,6 +173,33 @@ function createCanonicalAuthHeaders(authContext) {
|
|
|
171
173
|
}
|
|
172
174
|
|
|
173
175
|
// src/coreClient.ts
|
|
176
|
+
var DEFAULT_GATEWAY_TIMEOUT_MS = 15e3;
|
|
177
|
+
var DEFAULT_GATEWAY_MAX_RETRIES = 2;
|
|
178
|
+
var DEFAULT_ENV_TIMEOUT_MS = "LUCERN_REQUEST_TIMEOUT_MS";
|
|
179
|
+
var DEFAULT_ENV_MAX_RETRIES = "LUCERN_GATEWAY_MAX_RETRIES";
|
|
180
|
+
var ENV_TIMEOUT_BY_METHOD_PREFIX = "LUCERN_REQUEST_TIMEOUT_MS_";
|
|
181
|
+
var GatewayTimeoutError = class extends Error {
|
|
182
|
+
retryable = true;
|
|
183
|
+
timeoutMs;
|
|
184
|
+
constructor(timeoutMs) {
|
|
185
|
+
super(`Request timed out after ${timeoutMs}ms`);
|
|
186
|
+
this.name = "AbortError";
|
|
187
|
+
this.timeoutMs = timeoutMs;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
var GatewayTransportError = class extends Error {
|
|
191
|
+
retryable;
|
|
192
|
+
cause;
|
|
193
|
+
constructor(message, options) {
|
|
194
|
+
super(message);
|
|
195
|
+
this.name = "GatewayTransportError";
|
|
196
|
+
this.retryable = options?.retryable ?? true;
|
|
197
|
+
this.cause = options?.cause;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
function isGatewayRetryableError(error) {
|
|
201
|
+
return error instanceof GatewayTimeoutError && error.retryable || error instanceof GatewayTransportError && error.retryable || false;
|
|
202
|
+
}
|
|
174
203
|
var LucernApiError = class extends Error {
|
|
175
204
|
code;
|
|
176
205
|
status;
|
|
@@ -237,6 +266,99 @@ function generatePortableRequestId() {
|
|
|
237
266
|
8
|
|
238
267
|
).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10).join("")}`;
|
|
239
268
|
}
|
|
269
|
+
function resolveEnvironment() {
|
|
270
|
+
const processEnv = typeof globalThis === "object" && globalThis !== null && "process" in globalThis ? globalThis.process : void 0;
|
|
271
|
+
const env = processEnv !== void 0 && typeof processEnv === "object" && processEnv !== null && typeof processEnv.env === "object" ? processEnv.env : void 0;
|
|
272
|
+
return {
|
|
273
|
+
get: (name) => {
|
|
274
|
+
const value = env?.[name];
|
|
275
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
function telemetryEnvironmentRecord(environment) {
|
|
280
|
+
const names = [
|
|
281
|
+
"LUCERN_TELEMETRY_ENABLED",
|
|
282
|
+
"AXIOM_TELEMETRY_ENABLED",
|
|
283
|
+
"LUCERN_AXIOM_TOKEN",
|
|
284
|
+
"AXIOM_TOKEN",
|
|
285
|
+
"LUCERN_AXIOM_EVENTS_DATASET",
|
|
286
|
+
"LUCERN_AXIOM_DATASET",
|
|
287
|
+
"AXIOM_EVENTS_DATASET",
|
|
288
|
+
"AXIOM_DATASET",
|
|
289
|
+
"LUCERN_AXIOM_API_URL",
|
|
290
|
+
"AXIOM_URL",
|
|
291
|
+
"LUCERN_ENVIRONMENT",
|
|
292
|
+
"NODE_ENV",
|
|
293
|
+
"LUCERN_RELEASE",
|
|
294
|
+
"SENTRY_RELEASE",
|
|
295
|
+
"VERCEL_GIT_COMMIT_SHA"
|
|
296
|
+
];
|
|
297
|
+
return Object.fromEntries(
|
|
298
|
+
names.map((name) => [name, environment.get(name)])
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
function resolveRequestProfile(config, environment) {
|
|
302
|
+
const requestIdFactory = config.requestIdFactory ?? (() => generatePortableRequestId());
|
|
303
|
+
const parsedMaxRetries = parseIntegerFromString(
|
|
304
|
+
config.maxRetries,
|
|
305
|
+
environment.get(DEFAULT_ENV_MAX_RETRIES)
|
|
306
|
+
);
|
|
307
|
+
const parsedTimeoutMs = parseIntegerFromString(
|
|
308
|
+
config.timeoutMs,
|
|
309
|
+
environment.get(DEFAULT_ENV_TIMEOUT_MS)
|
|
310
|
+
);
|
|
311
|
+
const methodTimeouts = {
|
|
312
|
+
...config.timeoutMsByMethod
|
|
313
|
+
};
|
|
314
|
+
for (const method of ["GET", "POST", "PUT", "PATCH", "DELETE"]) {
|
|
315
|
+
const envKey = `${ENV_TIMEOUT_BY_METHOD_PREFIX}${method}`;
|
|
316
|
+
const raw = environment.get(envKey);
|
|
317
|
+
if (!raw || methodTimeouts[method] !== void 0) {
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
const parsed = parseIntegerFromString(void 0, raw);
|
|
321
|
+
if (typeof parsed === "number") {
|
|
322
|
+
methodTimeouts[method] = parsed;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return {
|
|
326
|
+
maxRetries: parsedMaxRetries ?? DEFAULT_GATEWAY_MAX_RETRIES,
|
|
327
|
+
timeoutMs: parsedTimeoutMs ?? DEFAULT_GATEWAY_TIMEOUT_MS,
|
|
328
|
+
timeoutMsByMethod: methodTimeouts,
|
|
329
|
+
requestIdFactory
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
function createGatewayRuntime(config, environment) {
|
|
333
|
+
return {
|
|
334
|
+
fetch: config.fetchImpl ?? fetch,
|
|
335
|
+
now: () => Date.now(),
|
|
336
|
+
sleep: (ms) => delay(ms),
|
|
337
|
+
env: environment,
|
|
338
|
+
redaction: resolveRequestRedactionValue,
|
|
339
|
+
profile: resolveRequestProfile(config, environment)
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
function parseIntegerFromString(value, rawValue) {
|
|
343
|
+
if (typeof value === "number" && Number.isInteger(value) && value >= 0) {
|
|
344
|
+
return value;
|
|
345
|
+
}
|
|
346
|
+
if (typeof rawValue !== "string" || !rawValue.trim()) {
|
|
347
|
+
return void 0;
|
|
348
|
+
}
|
|
349
|
+
const parsed = Number.parseInt(rawValue, 10);
|
|
350
|
+
return Number.isInteger(parsed) && parsed >= 0 ? parsed : void 0;
|
|
351
|
+
}
|
|
352
|
+
function resolveRequestRedactionValue(value) {
|
|
353
|
+
return redactDiagnosticValue(value);
|
|
354
|
+
}
|
|
355
|
+
function resolveGatewayBaseUrl(configBaseUrl, environment) {
|
|
356
|
+
const envBaseUrl = environment.get("LUCERN_API_URL") ?? environment.get("LUCERN_BASE_URL") ?? environment.get("LUCERN_GATEWAY_BASE_URL");
|
|
357
|
+
return (configBaseUrl ?? envBaseUrl ?? "").replace(/\/+$/, "");
|
|
358
|
+
}
|
|
359
|
+
function normalizeGatewayEnvironment(value) {
|
|
360
|
+
return value === "sandbox" || value === "production" ? value : void 0;
|
|
361
|
+
}
|
|
240
362
|
function fallbackErrorCode(status) {
|
|
241
363
|
if (status === 401) {
|
|
242
364
|
return "AUTHENTICATION_REQUIRED";
|
|
@@ -275,10 +397,8 @@ function computeRetryDelayMs(args) {
|
|
|
275
397
|
const jitterWindow = Math.max(250, Math.round(baseDelay * 0.25));
|
|
276
398
|
return baseDelay + Math.round(Math.random() * jitterWindow);
|
|
277
399
|
}
|
|
278
|
-
function
|
|
279
|
-
|
|
280
|
-
error.name = "AbortError";
|
|
281
|
-
return error;
|
|
400
|
+
function classifyGatewayErrorForRetry(error) {
|
|
401
|
+
return isGatewayRetryableError(error) || classifyRetry({ error }).retryable;
|
|
282
402
|
}
|
|
283
403
|
function isRecord(value) {
|
|
284
404
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
@@ -333,10 +453,18 @@ function cleanHeaderValue(value) {
|
|
|
333
453
|
return normalized ? normalized : void 0;
|
|
334
454
|
}
|
|
335
455
|
function createGatewayRequestClient(config = {}) {
|
|
336
|
-
const
|
|
337
|
-
const
|
|
338
|
-
const
|
|
339
|
-
const
|
|
456
|
+
const env = resolveEnvironment();
|
|
457
|
+
const runtime = createGatewayRuntime(config, env);
|
|
458
|
+
const baseUrl = resolveGatewayBaseUrl(config.baseUrl, env);
|
|
459
|
+
const maxRetries = runtime.profile.maxRetries;
|
|
460
|
+
const requestIdFactory = runtime.profile.requestIdFactory;
|
|
461
|
+
const requestTimeoutByMethod = runtime.profile.timeoutMsByMethod;
|
|
462
|
+
const defaultRequestTimeoutMs = runtime.profile.timeoutMs;
|
|
463
|
+
const normalizedEnvironment = normalizeGatewayEnvironment(config.environment);
|
|
464
|
+
const telemetryExporter = config.telemetryEnabled === false ? null : config.telemetryExporter ?? createTelemetryExporterFromEnv(telemetryEnvironmentRecord(env), {
|
|
465
|
+
service: "lucern-sdk",
|
|
466
|
+
environment: normalizedEnvironment
|
|
467
|
+
});
|
|
340
468
|
async function resolveAuthHeaders() {
|
|
341
469
|
const provided = config.getAuthHeaders ? await config.getAuthHeaders() : {};
|
|
342
470
|
const headers = new Headers(provided);
|
|
@@ -348,7 +476,7 @@ function createGatewayRequestClient(config = {}) {
|
|
|
348
476
|
};
|
|
349
477
|
setIfAbsent("x-lucern-key", config.apiKey);
|
|
350
478
|
setIfAbsent("x-lucern-session-token", config.userToken);
|
|
351
|
-
setIfAbsent("x-lucern-environment",
|
|
479
|
+
setIfAbsent("x-lucern-environment", normalizedEnvironment);
|
|
352
480
|
setIfAbsent("x-lucern-clerk-id", config.clerkId);
|
|
353
481
|
setIfAbsent("x-lucern-user-id", config.userId ?? config.clerkId);
|
|
354
482
|
setIfAbsent("x-lucern-deployment-host", config.deploymentHost);
|
|
@@ -363,19 +491,73 @@ function createGatewayRequestClient(config = {}) {
|
|
|
363
491
|
return mergeHeaderRecord(base, createCanonicalAuthHeaders(authContext));
|
|
364
492
|
}
|
|
365
493
|
async function fetchWithTimeout(url, init, timeoutMs) {
|
|
494
|
+
const normalizeTransportError = (error, isTimeout) => {
|
|
495
|
+
if (isTimeout) {
|
|
496
|
+
return new GatewayTimeoutError(timeoutMs);
|
|
497
|
+
}
|
|
498
|
+
return error instanceof GatewayTimeoutError || error instanceof GatewayTransportError ? error : new GatewayTransportError(
|
|
499
|
+
error instanceof Error ? error.message : "Gateway transport error",
|
|
500
|
+
{
|
|
501
|
+
cause: error,
|
|
502
|
+
retryable: classifyGatewayErrorForRetry(error)
|
|
503
|
+
}
|
|
504
|
+
);
|
|
505
|
+
};
|
|
366
506
|
const controller = new AbortController();
|
|
367
507
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
508
|
+
const requestEffect = Effect.tryPromise({
|
|
509
|
+
try: () => runtime.fetch(url, { ...init, signal: controller.signal }),
|
|
510
|
+
catch: (error) => normalizeTransportError(error, controller.signal.aborted)
|
|
511
|
+
});
|
|
368
512
|
try {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
throw timeoutError(timeoutMs);
|
|
513
|
+
const exit = await Effect.runPromiseExit(requestEffect);
|
|
514
|
+
if (Exit.isSuccess(exit)) {
|
|
515
|
+
return exit.value;
|
|
373
516
|
}
|
|
374
|
-
|
|
517
|
+
const failure = Array.from(Cause.failures(exit.cause))[0];
|
|
518
|
+
if (failure !== void 0) {
|
|
519
|
+
throw failure;
|
|
520
|
+
}
|
|
521
|
+
throw Cause.squash(exit.cause);
|
|
375
522
|
} finally {
|
|
376
523
|
clearTimeout(timer);
|
|
377
524
|
}
|
|
378
525
|
}
|
|
526
|
+
async function emitSdkResponseTelemetry(context) {
|
|
527
|
+
const retry = classifyRetry({
|
|
528
|
+
status: context.status,
|
|
529
|
+
error: context.error,
|
|
530
|
+
retryAfter: context.retryAfterMs !== null && context.retryAfterMs !== void 0 ? String(context.retryAfterMs / 1e3) : void 0
|
|
531
|
+
});
|
|
532
|
+
await emitTelemetrySignal(telemetryExporter, {
|
|
533
|
+
signalType: "trace",
|
|
534
|
+
surface: "sdk-retry",
|
|
535
|
+
eventName: context.willRetry ? "sdk.retry" : context.error ? "sdk.request.error" : "sdk.request.complete",
|
|
536
|
+
severity: context.error ? context.willRetry ? "warn" : "error" : "info",
|
|
537
|
+
durationMs: context.durationMs,
|
|
538
|
+
metricName: "sdk.request.duration_ms",
|
|
539
|
+
metricValue: context.durationMs,
|
|
540
|
+
correlationId: context.correlationId ?? context.requestId,
|
|
541
|
+
policyTraceId: context.policyTraceId ?? null,
|
|
542
|
+
tenantId: context.headers.get("x-lucern-tenant-id") ?? context.headers.get("x-lucern-tenant") ?? void 0,
|
|
543
|
+
workspaceId: context.headers.get("x-lucern-workspace-id") ?? context.headers.get("x-lucern-workspace") ?? void 0,
|
|
544
|
+
attributes: {
|
|
545
|
+
service: "lucern-sdk",
|
|
546
|
+
operation: "gateway.request",
|
|
547
|
+
path: context.path,
|
|
548
|
+
httpMethod: context.method,
|
|
549
|
+
httpStatus: context.status,
|
|
550
|
+
attempt: context.attempt,
|
|
551
|
+
maxRetries: context.maxRetries,
|
|
552
|
+
retryReason: retry.reason,
|
|
553
|
+
retryAfterMs: context.retryAfterMs ?? retry.retryAfterMs,
|
|
554
|
+
willRetry: context.willRetry,
|
|
555
|
+
retryable: retry.retryable,
|
|
556
|
+
errorName: context.error instanceof Error ? context.error.name : void 0,
|
|
557
|
+
errorMessage: context.error instanceof Error ? context.error.message : void 0
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
}
|
|
379
561
|
async function parsePayload(response) {
|
|
380
562
|
const text = await response.text();
|
|
381
563
|
if (!text) {
|
|
@@ -391,11 +573,11 @@ function createGatewayRequestClient(config = {}) {
|
|
|
391
573
|
if (typeof requestTimeoutMs === "number") {
|
|
392
574
|
return requestTimeoutMs;
|
|
393
575
|
}
|
|
394
|
-
const methodTimeoutMs =
|
|
576
|
+
const methodTimeoutMs = requestTimeoutByMethod?.[method];
|
|
395
577
|
if (typeof methodTimeoutMs === "number") {
|
|
396
578
|
return methodTimeoutMs;
|
|
397
579
|
}
|
|
398
|
-
return
|
|
580
|
+
return defaultRequestTimeoutMs;
|
|
399
581
|
}
|
|
400
582
|
function tryParseGatewayEnvelopeJson(text) {
|
|
401
583
|
const trimmed = text.trim();
|
|
@@ -416,8 +598,8 @@ function createGatewayRequestClient(config = {}) {
|
|
|
416
598
|
const legacyError = failure && isRecord(failure.error) ? failure.error : failure?.legacyError;
|
|
417
599
|
const correlationId = failure?.correlationId ?? args.response.headers.get("x-lucern-correlation-id")?.trim() ?? args.requestId;
|
|
418
600
|
const policyTraceId = failure?.policyTraceId ?? args.response.headers.get("x-lucern-policy-trace-id")?.trim() ?? null;
|
|
419
|
-
const details =
|
|
420
|
-
failure?.details ?? legacyError?.details
|
|
601
|
+
const details = runtime.redaction(
|
|
602
|
+
redactJsonDiagnosticValue(failure?.details ?? legacyError?.details)
|
|
421
603
|
);
|
|
422
604
|
const policySummary = readPolicySummaryFromDetails(details);
|
|
423
605
|
const failureMessage = typeof failure?.error === "string" ? failure.error : legacyError?.message;
|
|
@@ -487,7 +669,7 @@ function createGatewayRequestClient(config = {}) {
|
|
|
487
669
|
failure
|
|
488
670
|
});
|
|
489
671
|
const willRetry = attempt < maxRetries && retry.retryable;
|
|
490
|
-
|
|
672
|
+
const responseContext2 = {
|
|
491
673
|
...hookRequestContext,
|
|
492
674
|
durationMs: Date.now() - startedAt,
|
|
493
675
|
status: response.status,
|
|
@@ -497,7 +679,9 @@ function createGatewayRequestClient(config = {}) {
|
|
|
497
679
|
policyTraceId: apiError.policyTraceId ?? null,
|
|
498
680
|
retryAfterMs,
|
|
499
681
|
willRetry
|
|
500
|
-
}
|
|
682
|
+
};
|
|
683
|
+
await config.onResponse?.(responseContext2);
|
|
684
|
+
await emitSdkResponseTelemetry(responseContext2);
|
|
501
685
|
if (willRetry) {
|
|
502
686
|
lastError = apiError;
|
|
503
687
|
await delay(
|
|
@@ -512,7 +696,7 @@ function createGatewayRequestClient(config = {}) {
|
|
|
512
696
|
throw apiError;
|
|
513
697
|
}
|
|
514
698
|
const successPayload = payload;
|
|
515
|
-
|
|
699
|
+
const responseContext = {
|
|
516
700
|
...hookRequestContext,
|
|
517
701
|
durationMs: Date.now() - startedAt,
|
|
518
702
|
status: response.status,
|
|
@@ -522,22 +706,25 @@ function createGatewayRequestClient(config = {}) {
|
|
|
522
706
|
idempotentReplay: successPayload.idempotentReplay,
|
|
523
707
|
retryAfterMs,
|
|
524
708
|
willRetry: false
|
|
525
|
-
}
|
|
709
|
+
};
|
|
710
|
+
await config.onResponse?.(responseContext);
|
|
711
|
+
await emitSdkResponseTelemetry(responseContext);
|
|
526
712
|
return successPayload;
|
|
527
713
|
} catch (fetchError) {
|
|
528
714
|
if (fetchError instanceof LucernApiError) {
|
|
529
715
|
throw fetchError;
|
|
530
716
|
}
|
|
531
|
-
const
|
|
532
|
-
const
|
|
533
|
-
await config.onResponse?.({
|
|
717
|
+
const willRetry = attempt < maxRetries && classifyGatewayErrorForRetry(fetchError);
|
|
718
|
+
const responseContext = {
|
|
534
719
|
...hookRequestContext,
|
|
535
720
|
durationMs: Date.now() - startedAt,
|
|
536
721
|
error: fetchError,
|
|
537
722
|
correlationId: requestId,
|
|
538
723
|
policyTraceId: null,
|
|
539
724
|
willRetry
|
|
540
|
-
}
|
|
725
|
+
};
|
|
726
|
+
await config.onResponse?.(responseContext);
|
|
727
|
+
await emitSdkResponseTelemetry(responseContext);
|
|
541
728
|
lastError = fetchError;
|
|
542
729
|
if (willRetry) {
|
|
543
730
|
await delay(computeRetryDelayMs({ attempt }));
|