@mupt-ai/dari-gateway 1.0.309 → 1.0.310

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.
@@ -2,7 +2,7 @@ import { HttpError } from "../http/errors.js";
2
2
  import { piMessageToInteractionOutput } from "./pi_output.js";
3
3
  import { captureOperationalError } from "../observability/errors.js";
4
4
  import { CustomerDisconnectedError, customerCanceledProviderMessage, } from "../runtime/customer_disconnect.js";
5
- import { isProviderFailureMessage, providerFailureFromCaught, providerFailureFromTerminal, providerFailureMessage, } from "../runtime/provider_stream_outcome.js";
5
+ import { credentialRejectionFromFailure, failureWithCredentialRejection, isProviderFailureMessage, providerFailureFromCaught, providerFailureFromTerminal, providerFailureMessage, } from "../runtime/provider_stream_outcome.js";
6
6
  import { reportedExecution, } from "../runtime/runtime_reporter.js";
7
7
  import { trackBackgroundTelemetry } from "../telemetry/background.js";
8
8
  import { gatewayPlatform } from "../platform/platform.js";
@@ -248,6 +248,13 @@ async function throwProviderFailure(args) {
248
248
  const codex = codexUsageLimitError(limit, accountsUrl);
249
249
  throw new HttpError(429, subscriptionUsageLimitMessage(limit, accountsUrl), "subscription_usage_limit", undefined, { "retry-after": String(retryAfterSeconds(limit)), ...codex?.headers }, codex?.body);
250
250
  }
251
+ const rejected = credentialRejectionFromFailure(result.failure);
252
+ if (rejected !== null) {
253
+ const accountsUrl = gatewayPlatform().accountsUrl;
254
+ throw new HttpError(502, `Your ${rejected.provider} API key was rejected (${rejected.status}): `
255
+ + `${providerFailureMessage(result.failure)} `
256
+ + (accountsUrl ? `Update it at ${accountsUrl}.` : "Update it with `dari accounts`."), "provider_credential_rejected");
257
+ }
251
258
  throw new HttpError(502, providerFailureMessage(result.failure), "provider_execution_failed");
252
259
  }
253
260
  export async function prepareStreamingExecution(args) {
@@ -418,9 +425,14 @@ async function providerFailedAttempt(message, latencyMs, execution, responseCapt
418
425
  failure: message,
419
426
  });
420
427
  if (limit === null) {
428
+ const status = responseCapture?.last?.status;
429
+ // The customer's own key was refused: theirs to fix, not a gateway fault.
430
+ const rejected = execution.providerKeySource === "user" && (status === 401 || status === 403);
421
431
  return {
422
432
  kind: "provider_failed",
423
- message,
433
+ message: rejected
434
+ ? failureWithCredentialRejection(message, { provider: execution.provider, status })
435
+ : message,
424
436
  latencyMs,
425
437
  retryable: !isPromptPolicyRejection(message),
426
438
  };
@@ -95,9 +95,11 @@ export function startProviderCompletion(args) {
95
95
  return { promise, customerAbort, responseCapture };
96
96
  }
97
97
  function responseCaptureFor(execution) {
98
- return execution.providerKeySource === "subscription"
99
- ? createProviderResponseCapture()
100
- : undefined;
98
+ // A subscription's failures carry its usage limit; a customer key's say
99
+ // whether the provider refused the key itself.
100
+ return execution.providerKeySource === "managed"
101
+ ? undefined
102
+ : createProviderResponseCapture();
101
103
  }
102
104
  export async function startProviderStream(args) {
103
105
  const providerStartedMs = Date.now();
@@ -21,3 +21,10 @@ export declare function providerFailureFromCaught(error: unknown): Record<string
21
21
  export declare function providerFailureFromTerminal(error: unknown, terminalMessage: Record<string, unknown>): Record<string, unknown>;
22
22
  export declare function providerFailure(message: string): Record<string, unknown>;
23
23
  export declare function providerFailureMessage(message: Record<string, unknown>): string;
24
+ export type CredentialRejection = {
25
+ provider: string;
26
+ status: number;
27
+ };
28
+ /** Mark a failure where the provider refused the customer's own API key. */
29
+ export declare function failureWithCredentialRejection(failure: Record<string, unknown>, rejection: CredentialRejection): Record<string, unknown>;
30
+ export declare function credentialRejectionFromFailure(failure: Record<string, unknown>): CredentialRejection | null;
@@ -53,3 +53,12 @@ export function providerFailureMessage(message) {
53
53
  function stringValue(value) {
54
54
  return typeof value === "string" && value ? value : undefined;
55
55
  }
56
+ const CREDENTIAL_REJECTED = "provider_credential_rejected";
57
+ /** Mark a failure where the provider refused the customer's own API key. */
58
+ export function failureWithCredentialRejection(failure, rejection) {
59
+ return { ...failure, [CREDENTIAL_REJECTED]: rejection };
60
+ }
61
+ export function credentialRejectionFromFailure(failure) {
62
+ const marker = failure[CREDENTIAL_REJECTED];
63
+ return typeof marker === "object" && marker !== null ? marker : null;
64
+ }
@@ -18,6 +18,7 @@ import { extractInteractionObservability } from "./requests/interaction_observab
18
18
  import { costString, finiteNumber, isRecord, tokenCount, } from "./ingest_values.js";
19
19
  import { buildCompletedPrefixHit, leaseCountdownForPrefix, prefixEntryForIngest, reportPrefixState, } from "./prefix_state.js";
20
20
  import { subscriptionUsageLimitFromFailure } from "../providers/subscription_limits.js";
21
+ import { credentialRejectionFromFailure } from "../runtime/provider_stream_outcome.js";
21
22
  import { scrubSecrets } from "./scrub.js";
22
23
  import { encodeRetainedInteractionRequest } from "./requests/retained_interaction_request.js";
23
24
  import { toolResultObservations, toolSkillEvents } from "./tools/observations.js";
@@ -505,9 +506,11 @@ function reportOutcome(args) {
505
506
  if (args.status === "provider_error") {
506
507
  return {
507
508
  status: "provider_error",
508
- errorCode: subscriptionUsageLimitFromFailure(args.message) === null
509
- ? "provider_execution_failed"
510
- : "subscription_usage_limit",
509
+ errorCode: subscriptionUsageLimitFromFailure(args.message) !== null
510
+ ? "subscription_usage_limit"
511
+ : credentialRejectionFromFailure(args.message) !== null
512
+ ? "provider_credential_rejected"
513
+ : "provider_execution_failed",
511
514
  errorMessage: typeof args.message.errorMessage === "string"
512
515
  ? args.message.errorMessage
513
516
  : undefined,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mupt-ai/dari-gateway",
3
- "version": "1.0.309",
3
+ "version": "1.0.310",
4
4
  "description": "Self-hostable LLM gateway for coding agents and APIs: one endpoint for Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses, with subscription pooling, failover, and prompt-cache-aware routing.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "@aws-sdk/client-sts": "3.1063.0",
54
- "@mupt-ai/dari-router": "1.0.309",
54
+ "@mupt-ai/dari-router": "1.0.310",
55
55
  "@mupt-ai/pi-ai": "1.5.2-mupt.1"
56
56
  },
57
57
  "devDependencies": {
@@ -8,6 +8,8 @@ import {
8
8
  customerCanceledProviderMessage,
9
9
  } from "../runtime/customer_disconnect.js";
10
10
  import {
11
+ credentialRejectionFromFailure,
12
+ failureWithCredentialRejection,
11
13
  isProviderFailureMessage,
12
14
  providerFailureFromCaught,
13
15
  providerFailureFromTerminal,
@@ -450,6 +452,17 @@ async function throwProviderFailure(args: {
450
452
  codex?.body,
451
453
  );
452
454
  }
455
+ const rejected = credentialRejectionFromFailure(result.failure);
456
+ if (rejected !== null) {
457
+ const accountsUrl = gatewayPlatform().accountsUrl;
458
+ throw new HttpError(
459
+ 502,
460
+ `Your ${rejected.provider} API key was rejected (${rejected.status}): `
461
+ + `${providerFailureMessage(result.failure)} `
462
+ + (accountsUrl ? `Update it at ${accountsUrl}.` : "Update it with `dari accounts`."),
463
+ "provider_credential_rejected",
464
+ );
465
+ }
453
466
  throw new HttpError(
454
467
  502,
455
468
  providerFailureMessage(result.failure),
@@ -712,9 +725,14 @@ async function providerFailedAttempt(
712
725
  failure: message,
713
726
  });
714
727
  if (limit === null) {
728
+ const status = responseCapture?.last?.status;
729
+ // The customer's own key was refused: theirs to fix, not a gateway fault.
730
+ const rejected = execution.providerKeySource === "user" && (status === 401 || status === 403);
715
731
  return {
716
732
  kind: "provider_failed",
717
- message,
733
+ message: rejected
734
+ ? failureWithCredentialRejection(message, { provider: execution.provider, status })
735
+ : message,
718
736
  latencyMs,
719
737
  retryable: !isPromptPolicyRejection(message),
720
738
  };
@@ -206,9 +206,11 @@ export function startProviderCompletion(args: {
206
206
  }
207
207
 
208
208
  function responseCaptureFor(execution: ProviderExecution): ProviderResponseCapture | undefined {
209
- return execution.providerKeySource === "subscription"
210
- ? createProviderResponseCapture()
211
- : undefined;
209
+ // A subscription's failures carry its usage limit; a customer key's say
210
+ // whether the provider refused the key itself.
211
+ return execution.providerKeySource === "managed"
212
+ ? undefined
213
+ : createProviderResponseCapture();
212
214
  }
213
215
 
214
216
  export async function startProviderStream(args: {
@@ -81,3 +81,22 @@ export function providerFailureMessage(message: Record<string, unknown>): string
81
81
  function stringValue(value: unknown): string | undefined {
82
82
  return typeof value === "string" && value ? value : undefined;
83
83
  }
84
+
85
+ const CREDENTIAL_REJECTED = "provider_credential_rejected";
86
+
87
+ export type CredentialRejection = { provider: string; status: number };
88
+
89
+ /** Mark a failure where the provider refused the customer's own API key. */
90
+ export function failureWithCredentialRejection(
91
+ failure: Record<string, unknown>,
92
+ rejection: CredentialRejection,
93
+ ): Record<string, unknown> {
94
+ return { ...failure, [CREDENTIAL_REJECTED]: rejection };
95
+ }
96
+
97
+ export function credentialRejectionFromFailure(
98
+ failure: Record<string, unknown>,
99
+ ): CredentialRejection | null {
100
+ const marker = failure[CREDENTIAL_REJECTED];
101
+ return typeof marker === "object" && marker !== null ? marker as CredentialRejection : null;
102
+ }
@@ -66,6 +66,7 @@ import {
66
66
  reportPrefixState,
67
67
  } from "./prefix_state.js";
68
68
  import { subscriptionUsageLimitFromFailure } from "../providers/subscription_limits.js";
69
+ import { credentialRejectionFromFailure } from "../runtime/provider_stream_outcome.js";
69
70
  import { scrubSecrets } from "./scrub.js";
70
71
  import { encodeRetainedInteractionRequest } from "./requests/retained_interaction_request.js";
71
72
  import { toolResultObservations, toolSkillEvents } from "./tools/observations.js";
@@ -703,9 +704,11 @@ function reportOutcome(args: ReportableProviderOutcome): ReportOutcome {
703
704
  if (args.status === "provider_error") {
704
705
  return {
705
706
  status: "provider_error",
706
- errorCode: subscriptionUsageLimitFromFailure(args.message) === null
707
- ? "provider_execution_failed"
708
- : "subscription_usage_limit",
707
+ errorCode: subscriptionUsageLimitFromFailure(args.message) !== null
708
+ ? "subscription_usage_limit"
709
+ : credentialRejectionFromFailure(args.message) !== null
710
+ ? "provider_credential_rejected"
711
+ : "provider_execution_failed",
709
712
  errorMessage:
710
713
  typeof args.message.errorMessage === "string"
711
714
  ? args.message.errorMessage