@mupt-ai/dari-gateway 1.0.340 → 1.0.342
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/dist/activity/tool_exchanges.d.ts +0 -1
- package/dist/activity/values.d.ts +0 -1
- package/dist/activity/values.js +0 -4
- package/dist/ingress/codex_models.d.ts +2 -2
- package/dist/ingress/codex_models.js +34 -7
- package/dist/link/budget.d.ts +20 -7
- package/dist/link/budget.js +68 -18
- package/dist/link/endpoints.js +38 -39
- package/dist/link/errors.js +1 -1
- package/dist/link/grant_access.js +9 -4
- package/dist/link/grants.d.ts +0 -1
- package/dist/link/self_hosted.d.ts +3 -1
- package/dist/link/self_hosted.js +17 -7
- package/dist/link/service.d.ts +4 -4
- package/dist/link/service.js +41 -22
- package/dist/platform/platform.d.ts +13 -7
- package/dist/providers/subscription_limits.d.ts +6 -0
- package/dist/providers/subscription_limits.js +14 -0
- package/dist/self_host/resolve.js +2 -1
- package/dist/self_host/subscriptions/index.d.ts +3 -3
- package/dist/self_host/subscriptions/index.js +3 -3
- package/dist/self_host/subscriptions/usage.d.ts +1 -1
- package/dist/self_host/subscriptions/usage.js +28 -11
- package/dist/strategies/auto_routing_selector.js +7 -18
- package/dist/telemetry/tools/git_events.d.ts +0 -2
- package/dist/telemetry/tools/git_events.js +0 -4
- package/package.json +2 -2
- package/src/activity/tool_exchanges.ts +0 -1
- package/src/activity/values.ts +0 -5
- package/src/ingress/codex_models.ts +43 -10
- package/src/link/budget.ts +64 -21
- package/src/link/endpoints.ts +39 -38
- package/src/link/errors.ts +1 -1
- package/src/link/grant_access.ts +8 -3
- package/src/link/grants.ts +0 -2
- package/src/link/self_hosted.ts +17 -7
- package/src/link/service.ts +45 -23
- package/src/platform/platform.ts +16 -8
- package/src/providers/subscription_limits.ts +18 -0
- package/src/self_host/resolve.ts +2 -1
- package/src/self_host/subscriptions/index.ts +3 -3
- package/src/self_host/subscriptions/usage.ts +32 -10
- package/src/strategies/auto_routing_selector.ts +7 -22
- package/src/telemetry/tools/git_events.ts +0 -5
|
@@ -1,32 +1,34 @@
|
|
|
1
|
-
import { openCodeGoUsageFromReport } from "../../providers/subscription_limits.js";
|
|
1
|
+
import { openCodeGoUsageFromReport, tokenPassUsageFromCredits } from "../../providers/subscription_limits.js";
|
|
2
2
|
import { refreshedAccessToken } from "./access.js";
|
|
3
3
|
import type { OAuthFetch } from "./oauth.js";
|
|
4
4
|
import type { SubscriptionCredential, SubscriptionStore } from "./store.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Claude Code, ChatGPT, and Muse Code report usage on the responses the
|
|
8
|
-
* gateway serves. OpenCode Go
|
|
9
|
-
* that takes the plan's workspace,
|
|
10
|
-
*
|
|
8
|
+
* gateway serves. OpenCode Go and Vercel Token Pass do not: Go reports it from
|
|
9
|
+
* a usage endpoint that takes the plan's workspace, and Token Pass its
|
|
10
|
+
* remaining credit from AI Gateway's credits endpoint. The accounts view reads
|
|
11
|
+
* these again when the last reading is stale.
|
|
11
12
|
*/
|
|
12
13
|
// A read, failed or not, is not repeated within this interval.
|
|
13
14
|
const READ_INTERVAL_MS = 60_000;
|
|
14
15
|
const READ_TIMEOUT_MS = 8_000;
|
|
15
16
|
const OPENCODE_GO_USAGE_URL = "https://opencode.ai/zen/go/v1/usage";
|
|
17
|
+
const TOKEN_PASS_CREDITS_URL = "https://ai-gateway.vercel.sh/v1/credits";
|
|
16
18
|
|
|
17
19
|
// Reads that recorded nothing (failed, or no running window), so such a plan is
|
|
18
20
|
// not re-read on every listing. A recorded reading stamps the credential
|
|
19
21
|
// itself, which a reconnect clears.
|
|
20
22
|
const unrecordedAt = new Map<string, number>();
|
|
21
23
|
|
|
22
|
-
/** Read every stale OpenCode Go plan again; a failed read keeps the last one. */
|
|
24
|
+
/** Read every stale OpenCode Go and Token Pass plan again; a failed read keeps the last one. */
|
|
23
25
|
export async function refreshStaleUsage(
|
|
24
26
|
store: SubscriptionStore,
|
|
25
27
|
options: { fetch: OAuthFetch; now?: Date },
|
|
26
28
|
): Promise<void> {
|
|
27
29
|
const now = options.now ?? new Date();
|
|
28
30
|
const due = (at: number | undefined) => now.getTime() - (at ?? 0) >= READ_INTERVAL_MS;
|
|
29
|
-
const stale = store.list("opencode_go").filter((credential) =>
|
|
31
|
+
const stale = [...store.list("opencode_go"), ...store.list("vercel_token_pass")].filter((credential) =>
|
|
30
32
|
due(credential.usage?.observedAt.getTime()) && due(unrecordedAt.get(credential.id))
|
|
31
33
|
);
|
|
32
34
|
await Promise.all(stale.map(async (credential) => {
|
|
@@ -45,6 +47,19 @@ async function readUsage(
|
|
|
45
47
|
options: { fetch: OAuthFetch; now: Date },
|
|
46
48
|
): Promise<void> {
|
|
47
49
|
const token = await refreshedAccessToken(store, credential, options);
|
|
50
|
+
const usage = credential.provider === "vercel_token_pass"
|
|
51
|
+
? await readTokenPassUsage(token, options)
|
|
52
|
+
: await readOpenCodeGoUsage(credential, token, options);
|
|
53
|
+
if (usage === null) return;
|
|
54
|
+
unrecordedAt.delete(credential.id);
|
|
55
|
+
store.recordUsage(credential.id, usage, options.now);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function readOpenCodeGoUsage(
|
|
59
|
+
credential: SubscriptionCredential,
|
|
60
|
+
token: string,
|
|
61
|
+
options: { fetch: OAuthFetch; now: Date },
|
|
62
|
+
) {
|
|
48
63
|
const response = await options.fetch(OPENCODE_GO_USAGE_URL, {
|
|
49
64
|
method: "GET",
|
|
50
65
|
headers: {
|
|
@@ -57,8 +72,15 @@ async function readUsage(
|
|
|
57
72
|
});
|
|
58
73
|
// Go answers 403 for a workspace without a Go plan.
|
|
59
74
|
if (!response.ok) throw new Error(`OpenCode Go usage read failed with status ${response.status}.`);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
75
|
+
return openCodeGoUsageFromReport(await response.json(), options.now);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function readTokenPassUsage(token: string, options: { fetch: OAuthFetch; now: Date }) {
|
|
79
|
+
const response = await options.fetch(TOKEN_PASS_CREDITS_URL, {
|
|
80
|
+
method: "GET",
|
|
81
|
+
headers: { Accept: "application/json", Authorization: `Bearer ${token}`, "User-Agent": "dari-gateway" },
|
|
82
|
+
signal: AbortSignal.timeout(READ_TIMEOUT_MS),
|
|
83
|
+
});
|
|
84
|
+
if (!response.ok) throw new Error(`Token Pass usage read failed with status ${response.status}.`);
|
|
85
|
+
return tokenPassUsageFromCredits(await response.json(), options.now);
|
|
64
86
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { requestAutoRouterSelection } from "@mupt-ai/dari-router";
|
|
1
2
|
import { parseSelectorDecision, SELECTOR_LEASE_TURNS } from "@mupt-ai/dari-router/policy-engine";
|
|
2
3
|
import { HttpError } from "../http/errors.js";
|
|
3
4
|
import type { SelectorBackend } from "./routing.js";
|
|
@@ -7,7 +8,6 @@ export const DARI_AUTO_ROUTING_ENDPOINT = "https://routing.dari.dev/v1/auto-rout
|
|
|
7
8
|
// @mupt-ai/dari-router, which calls the same API.
|
|
8
9
|
const AUTO_ROUTER_MODEL = "dari/auto-router";
|
|
9
10
|
const AUTO_ROUTER_CONTEXT_WINDOW_CHARS = 32_000;
|
|
10
|
-
const AUTO_ROUTER_TIMEOUT_MS = 30_000;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Choose among a router's models with Dari Auto Routing, the hosted routing
|
|
@@ -23,10 +23,6 @@ export function createAutoRoutingSelectorBackend(config: {
|
|
|
23
23
|
if (!apiKey) {
|
|
24
24
|
throw new Error("Dari Auto Routing needs an API key.");
|
|
25
25
|
}
|
|
26
|
-
const endpoint = (config.endpoint ?? DARI_AUTO_ROUTING_ENDPOINT)
|
|
27
|
-
.replace(/\/+$/, "")
|
|
28
|
-
.replace(/\/select$/, "");
|
|
29
|
-
const fetchImpl = config.fetch ?? fetch;
|
|
30
26
|
return {
|
|
31
27
|
selectorModel: AUTO_ROUTER_MODEL,
|
|
32
28
|
contextWindowChars: async () => AUTO_ROUTER_CONTEXT_WINDOW_CHARS,
|
|
@@ -34,28 +30,17 @@ export function createAutoRoutingSelectorBackend(config: {
|
|
|
34
30
|
// the policy rather than the runtime's stateless hold.
|
|
35
31
|
negotiatedLeaseTurns: async () => SELECTOR_LEASE_TURNS,
|
|
36
32
|
select: async (args) => {
|
|
37
|
-
let
|
|
33
|
+
let outputText: string;
|
|
38
34
|
try {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
},
|
|
45
|
-
body: JSON.stringify(args.selectorRequest),
|
|
46
|
-
signal: AbortSignal.timeout(AUTO_ROUTER_TIMEOUT_MS),
|
|
35
|
+
outputText = await requestAutoRouterSelection({
|
|
36
|
+
apiKey,
|
|
37
|
+
endpoint: config.endpoint ?? DARI_AUTO_ROUTING_ENDPOINT,
|
|
38
|
+
request: args.selectorRequest,
|
|
39
|
+
fetch: config.fetch,
|
|
47
40
|
});
|
|
48
41
|
} catch {
|
|
49
42
|
throw new HttpError(502, "Dari Auto Routing request failed.", "selector_execution_failed");
|
|
50
43
|
}
|
|
51
|
-
if (!response.ok) {
|
|
52
|
-
throw new HttpError(
|
|
53
|
-
502,
|
|
54
|
-
`Dari Auto Routing returned HTTP ${response.status}.`,
|
|
55
|
-
"selector_execution_failed",
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
const outputText = await response.text();
|
|
59
44
|
return {
|
|
60
45
|
decision: parseSelectorDecision(outputText).decision,
|
|
61
46
|
selectorModel: args.selectorModel,
|
|
@@ -47,11 +47,6 @@ export function gitEvents(request: InteractionRequest, zdr: boolean): GitEventRe
|
|
|
47
47
|
return gitEventsIn(request.items, newlySubmittedToolResults(request).results.map(({ ordinal }) => ordinal));
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
/** The GitHub pull requests and pushes in every tool result of a request, as a stored request is read. */
|
|
51
|
-
export function allGitEvents(request: InteractionRequest): GitEventReport[] {
|
|
52
|
-
return gitEventsIn(request.items, request.items.flatMap((item, ordinal) => (item.type === "tool_result" ? [ordinal] : [])));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
50
|
function gitEventsIn(items: readonly InteractionItem[], resultOrdinals: readonly number[]): GitEventReport[] {
|
|
56
51
|
const calls = new Map<string, string>();
|
|
57
52
|
for (const item of items) {
|