@mupt-ai/dari-gateway 1.0.339 → 1.0.340
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -7
- package/dist/index.d.ts +1 -1
- package/dist/link/assets.js +7 -0
- package/dist/link/endpoints.js +26 -6
- package/dist/link/errors.js +3 -0
- package/dist/link/self_hosted.js +8 -2
- package/dist/models/catalog.js +1 -0
- package/dist/models/execution_provider.d.ts +6 -1
- package/dist/models/execution_provider.js +12 -0
- package/dist/models/provider_defaults.js +9 -0
- package/dist/models/provider_models.json +217 -0
- package/dist/models/subscriptions.d.ts +5 -0
- package/dist/models/subscriptions.js +21 -2
- package/dist/providers/execution.d.ts +5 -0
- package/dist/providers/execution.js +12 -2
- package/dist/providers/github_copilot.d.ts +8 -0
- package/dist/providers/github_copilot.js +21 -0
- package/dist/providers/pi.js +3 -0
- package/dist/providers/provider_attempt_runner.d.ts +2 -0
- package/dist/providers/provider_attempt_runner.js +9 -0
- package/dist/providers/provider_call.js +6 -2
- package/dist/providers/subscription_limits.d.ts +6 -5
- package/dist/providers/subscription_limits.js +20 -9
- package/dist/runtime/routed_runtime.js +68 -3
- package/dist/self_host/resolve.d.ts +11 -9
- package/dist/self_host/resolve.js +247 -181
- package/dist/self_host/subscriptions/index.d.ts +6 -5
- package/dist/self_host/subscriptions/index.js +6 -5
- package/dist/self_host/subscriptions/oauth.d.ts +7 -3
- package/dist/self_host/subscriptions/oauth.js +195 -5
- package/dist/self_host/subscriptions/store.js +3 -0
- package/dist/telemetry/prefix_state.d.ts +1 -0
- package/dist/telemetry/prefix_state.js +1 -1
- package/dist/telemetry/report.js +2 -3
- package/dist/types.d.ts +6 -2
- package/package.json +2 -2
- package/src/index.ts +1 -1
- package/src/link/assets.ts +7 -0
- package/src/link/endpoints.ts +27 -7
- package/src/link/errors.ts +3 -0
- package/src/link/self_hosted.ts +9 -1
- package/src/models/catalog.ts +1 -0
- package/src/models/execution_provider.ts +24 -1
- package/src/models/provider_defaults.ts +9 -0
- package/src/models/provider_models.json +217 -0
- package/src/models/subscriptions.ts +22 -2
- package/src/providers/execution.ts +22 -2
- package/src/providers/github_copilot.ts +24 -0
- package/src/providers/pi.ts +2 -0
- package/src/providers/provider_attempt_runner.ts +15 -0
- package/src/providers/provider_call.ts +6 -2
- package/src/providers/subscription_limits.ts +20 -10
- package/src/runtime/routed_runtime.ts +79 -3
- package/src/self_host/resolve.ts +253 -186
- package/src/self_host/subscriptions/index.ts +7 -4
- package/src/self_host/subscriptions/oauth.ts +264 -8
- package/src/self_host/subscriptions/store.ts +3 -0
- package/src/telemetry/prefix_state.ts +4 -1
- package/src/telemetry/report.ts +2 -3
- package/src/types.ts +27 -3
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
executeStreaming,
|
|
17
17
|
prepareStreamingExecution,
|
|
18
18
|
type CompletedNonStreamingExecution,
|
|
19
|
+
type NextProviderChoice,
|
|
19
20
|
type ProviderChoice,
|
|
20
21
|
} from "../providers/execution.js";
|
|
21
22
|
import {
|
|
@@ -29,12 +30,14 @@ import {
|
|
|
29
30
|
} from "../providers/provider_call.js";
|
|
30
31
|
import type { SelectorBackend } from "../strategies/routing.js";
|
|
31
32
|
import { routeDecision, type RoutedDecision } from "../strategies/route_decision.js";
|
|
32
|
-
import { executionProviderForModel } from "../models/execution_provider.js";
|
|
33
|
+
import { credentialsServedBy, executionProviderForModel } from "../models/execution_provider.js";
|
|
34
|
+
import { gatewayPlatform } from "../platform/platform.js";
|
|
35
|
+
import { captureOperationalError } from "../observability/errors.js";
|
|
33
36
|
import { trackBackgroundTelemetry } from "../telemetry/background.js";
|
|
34
37
|
import { estimateBenchmarkReplay } from "../telemetry/benchmark_replay.js";
|
|
35
38
|
import { buildCompletedPrefixHit } from "../telemetry/prefix_state.js";
|
|
36
39
|
import { scrubSecrets } from "../telemetry/scrub.js";
|
|
37
|
-
import type { ResolvedRouter } from "../types.js";
|
|
40
|
+
import type { ModelFallback, ProviderCredential, ResolvedRouter } from "../types.js";
|
|
38
41
|
import type { PreparedRoutedCompletion } from "./prepare_routed_completion.js";
|
|
39
42
|
import { auxiliaryDecision } from "./claude_code/auxiliary_routing.js";
|
|
40
43
|
import { explicitModelDecision, strictModelDecision } from "./explicit_model.js";
|
|
@@ -70,6 +73,7 @@ type SpeculativeExecution = {
|
|
|
70
73
|
type RuntimeExecutionPlan = {
|
|
71
74
|
primary: ProviderChoice;
|
|
72
75
|
primaryRetries: number;
|
|
76
|
+
nextChoice: NextProviderChoice;
|
|
73
77
|
reportBarrier?: Promise<unknown>;
|
|
74
78
|
startedCompletion?: StartedCompletion;
|
|
75
79
|
startedStream?: StartedStream;
|
|
@@ -137,6 +141,7 @@ export async function runPreparedRoutedCompletion(args: {
|
|
|
137
141
|
interaction: prepared.interaction,
|
|
138
142
|
primary: plan.primary,
|
|
139
143
|
primaryRetries: plan.primaryRetries,
|
|
144
|
+
nextChoice: plan.nextChoice,
|
|
140
145
|
reporter: prepared.reporter,
|
|
141
146
|
protocol: prepared.protocol,
|
|
142
147
|
startedCompletion: plan.startedCompletion,
|
|
@@ -149,6 +154,7 @@ export async function runPreparedRoutedCompletion(args: {
|
|
|
149
154
|
interaction: prepared.interaction,
|
|
150
155
|
primary: plan.primary,
|
|
151
156
|
primaryRetries: plan.primaryRetries,
|
|
157
|
+
nextChoice: plan.nextChoice,
|
|
152
158
|
reporter: prepared.reporter,
|
|
153
159
|
startedStream: plan.startedStream,
|
|
154
160
|
initialError: plan.initialStreamError,
|
|
@@ -189,6 +195,7 @@ export async function runPreparedRoutedInteraction(args: {
|
|
|
189
195
|
interaction: args.prepared.interaction,
|
|
190
196
|
primary: plan.primary,
|
|
191
197
|
primaryRetries: plan.primaryRetries,
|
|
198
|
+
nextChoice: plan.nextChoice,
|
|
192
199
|
reporter: args.prepared.reporter,
|
|
193
200
|
protocol: args.prepared.protocol,
|
|
194
201
|
startedCompletion: plan.startedCompletion,
|
|
@@ -399,6 +406,7 @@ async function prepareRuntimeExecution(args: {
|
|
|
399
406
|
primaryRetries: configuredPrimaryRetries(
|
|
400
407
|
prepared.credentials.primary_retries,
|
|
401
408
|
),
|
|
409
|
+
nextChoice: (choice) => nextProviderChoice(prepared, choice),
|
|
402
410
|
reportBarrier: selectionPromise,
|
|
403
411
|
startedCompletion:
|
|
404
412
|
speculative.transport === "completion" ? speculative.started : undefined,
|
|
@@ -420,6 +428,7 @@ function selectedExecutionPlan(
|
|
|
420
428
|
primaryRetries: configuredPrimaryRetries(
|
|
421
429
|
prepared.credentials.primary_retries,
|
|
422
430
|
),
|
|
431
|
+
nextChoice: (choice) => nextProviderChoice(prepared, choice),
|
|
423
432
|
};
|
|
424
433
|
}
|
|
425
434
|
|
|
@@ -606,13 +615,79 @@ function providerChoice(
|
|
|
606
615
|
};
|
|
607
616
|
}
|
|
608
617
|
|
|
618
|
+
/**
|
|
619
|
+
* The same decision served by the model's next fallback after `choice`: a plan
|
|
620
|
+
* the platform still has a token for, or the router's API credential while
|
|
621
|
+
* the organization has credits. A fallback that cannot be prepared is
|
|
622
|
+
* skipped. Undefined when none is left.
|
|
623
|
+
*/
|
|
624
|
+
async function nextProviderChoice(
|
|
625
|
+
prepared: PreparedRoutedCompletion,
|
|
626
|
+
choice: ProviderChoice,
|
|
627
|
+
): Promise<ProviderChoice | undefined> {
|
|
628
|
+
const { credentials } = prepared;
|
|
629
|
+
const model = choice.decision.selectedModel;
|
|
630
|
+
const fallbacks = credentials.model_fallbacks?.[model] ?? [];
|
|
631
|
+
for (let index = (choice.fallback ?? -1) + 1; index < fallbacks.length; index += 1) {
|
|
632
|
+
const fallback = fallbacks[index]!;
|
|
633
|
+
if (!fallback.subscription && credentials.credit_exhausted === true) continue;
|
|
634
|
+
const plan = fallback.subscription
|
|
635
|
+
? await fallbackPlan(credentials, fallback, model, prepared.routing.conversationId)
|
|
636
|
+
: undefined;
|
|
637
|
+
if (fallback.subscription && plan === undefined) continue;
|
|
638
|
+
try {
|
|
639
|
+
return {
|
|
640
|
+
decision: choice.decision,
|
|
641
|
+
fallback: index,
|
|
642
|
+
execution: providerExecution(prepared, choice.decision, credentialsServedBy(credentials, model, fallback.config, plan)),
|
|
643
|
+
};
|
|
644
|
+
} catch (error) {
|
|
645
|
+
console.error(
|
|
646
|
+
"model fallback setup failed",
|
|
647
|
+
scrubSecrets(error instanceof Error ? error.message : String(error)),
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return undefined;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// The user's plan of the fallback's kind that serves this model now, if any.
|
|
655
|
+
async function fallbackPlan(
|
|
656
|
+
credentials: ResolvedRouter,
|
|
657
|
+
fallback: ModelFallback,
|
|
658
|
+
model: string,
|
|
659
|
+
conversationId: string,
|
|
660
|
+
): Promise<ProviderCredential | undefined> {
|
|
661
|
+
const platform = gatewayPlatform();
|
|
662
|
+
const userId = credentials.attributed_user_id;
|
|
663
|
+
// A strict router serves only plans the platform supplied up front.
|
|
664
|
+
if (platform.nextSubscription === undefined || !userId || credentials.strict === true) return undefined;
|
|
665
|
+
try {
|
|
666
|
+
const next = await platform.nextSubscription({
|
|
667
|
+
userId,
|
|
668
|
+
provider: fallback.config.provider,
|
|
669
|
+
model,
|
|
670
|
+
conversationId,
|
|
671
|
+
...(credentials.link_grant_id ? { linkGrantId: credentials.link_grant_id } : {}),
|
|
672
|
+
});
|
|
673
|
+
return next === undefined
|
|
674
|
+
? undefined
|
|
675
|
+
: { apiKey: next.apiKey, subscription: fallback.subscription!, credentialId: next.credentialId };
|
|
676
|
+
} catch (error) {
|
|
677
|
+
console.error("fallback subscription lookup failed", error);
|
|
678
|
+
captureOperationalError(error, { code: "next_subscription_lookup_failed" });
|
|
679
|
+
return undefined;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
609
683
|
function providerExecution(
|
|
610
684
|
prepared: PreparedRoutedCompletion,
|
|
611
685
|
decision: RoutingDecision,
|
|
686
|
+
credentials: ResolvedRouter = prepared.credentials,
|
|
612
687
|
): ProviderExecution {
|
|
613
688
|
return prepareProviderExecution({
|
|
614
689
|
decision,
|
|
615
|
-
credentials
|
|
690
|
+
credentials,
|
|
616
691
|
billable: prepared.billable,
|
|
617
692
|
routerId: prepared.routerId,
|
|
618
693
|
pricing: prepared.routing.pricing,
|
|
@@ -674,6 +749,7 @@ function runtimeResult(
|
|
|
674
749
|
target,
|
|
675
750
|
conversationId: prepared.routing.conversationId,
|
|
676
751
|
leaseDepth: prepared.routing.leaseDepth,
|
|
752
|
+
provider: result.choice.execution.provider,
|
|
677
753
|
providerBlockDepth:
|
|
678
754
|
prepared.routing.providerBlockDepthFor(decision.selectedModel),
|
|
679
755
|
fingerprints: prepared.routing.prefix.fingerprints,
|