@mupt-ai/dari-gateway 1.0.302 → 1.0.303
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 +6 -2
- package/dari-gateway.example.yaml +6 -0
- package/dist/ingress/anthropic_messages.js +2 -1
- package/dist/ingress/chat_completions.js +11 -2
- package/dist/ingress/client_harness.d.ts +13 -0
- package/dist/ingress/client_harness.js +27 -0
- package/dist/ingress/codex.js +5 -1
- package/dist/ingress/models.js +4 -10
- package/dist/self_host/config.d.ts +14 -1
- package/dist/self_host/config.js +37 -9
- package/dist/self_host/main.d.ts +3 -2
- package/dist/self_host/main.js +28 -31
- package/dist/self_host/resolve.d.ts +5 -2
- package/dist/self_host/resolve.js +13 -9
- package/dist/self_host/router_models.d.ts +41 -0
- package/dist/self_host/router_models.js +120 -0
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
- package/src/ingress/anthropic_messages.ts +2 -1
- package/src/ingress/chat_completions.ts +12 -2
- package/src/ingress/client_harness.ts +32 -0
- package/src/ingress/codex.ts +5 -1
- package/src/ingress/models.ts +4 -13
- package/src/self_host/config.ts +51 -10
- package/src/self_host/main.ts +28 -30
- package/src/self_host/resolve.ts +22 -10
- package/src/self_host/router_models.ts +149 -0
- package/src/types.ts +10 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model lists saved through the gateway's API, which `dari gateway models`
|
|
3
|
+
* calls: a router's own list and each coding agent's. They live in SQLite and
|
|
4
|
+
* are served in place of the config file's, so the file is never rewritten
|
|
5
|
+
* and a change needs no restart.
|
|
6
|
+
*/
|
|
7
|
+
import type { Database } from "bun:sqlite";
|
|
8
|
+
|
|
9
|
+
import { errorResponse, HttpError } from "../http/errors.js";
|
|
10
|
+
import { CLIENT_HARNESSES } from "../ingress/client_harness.js";
|
|
11
|
+
import { DEFAULT_ROUTER_ALIAS } from "../platform/routing_contract.js";
|
|
12
|
+
import type { ClientHarness } from "../types.js";
|
|
13
|
+
import {
|
|
14
|
+
DEFAULT_GATEWAY_CONFIG,
|
|
15
|
+
parseRouterModels,
|
|
16
|
+
type GatewayConfig,
|
|
17
|
+
type RouterConfig,
|
|
18
|
+
type RouterModelConfig,
|
|
19
|
+
} from "./config.js";
|
|
20
|
+
|
|
21
|
+
// A router's own list is saved under no agent.
|
|
22
|
+
const ROUTER_LIST = "";
|
|
23
|
+
|
|
24
|
+
export class RouterModelStore {
|
|
25
|
+
constructor(private readonly db: Database) {
|
|
26
|
+
db.run(`CREATE TABLE IF NOT EXISTS router_models (
|
|
27
|
+
router_id TEXT NOT NULL,
|
|
28
|
+
agent TEXT NOT NULL,
|
|
29
|
+
models TEXT NOT NULL,
|
|
30
|
+
PRIMARY KEY (router_id, agent)
|
|
31
|
+
)`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The router as served: each saved list in place of its config's. */
|
|
35
|
+
served(routerId: string, router: RouterConfig): RouterConfig {
|
|
36
|
+
const rows = this.db
|
|
37
|
+
.query<{ agent: string; models: string }, [string]>(
|
|
38
|
+
"SELECT agent, models FROM router_models WHERE router_id = ?",
|
|
39
|
+
)
|
|
40
|
+
.all(routerId);
|
|
41
|
+
if (rows.length === 0) return router;
|
|
42
|
+
const served = { ...router, agentModels: { ...router.agentModels } };
|
|
43
|
+
for (const row of rows) {
|
|
44
|
+
const models = JSON.parse(row.models) as RouterModelConfig[];
|
|
45
|
+
if (row.agent === ROUTER_LIST) served.models = models;
|
|
46
|
+
else served.agentModels[row.agent as ClientHarness] = models;
|
|
47
|
+
}
|
|
48
|
+
return served;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The routers with a saved list. */
|
|
52
|
+
routersWithSavedLists(): string[] {
|
|
53
|
+
return this.db
|
|
54
|
+
.query<{ router_id: string }, []>("SELECT DISTINCT router_id FROM router_models ORDER BY router_id")
|
|
55
|
+
.all()
|
|
56
|
+
.map((row) => row.router_id);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Whether the router's own list is a saved one. */
|
|
60
|
+
hasRouterList(routerId: string): boolean {
|
|
61
|
+
return this.db
|
|
62
|
+
.query("SELECT 1 FROM router_models WHERE router_id = ? AND agent = ?")
|
|
63
|
+
.get(routerId, ROUTER_LIST) !== null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Save the router's own list, or one coding agent's. */
|
|
67
|
+
save(routerId: string, agent: ClientHarness | undefined, models: RouterModelConfig[]): void {
|
|
68
|
+
this.db
|
|
69
|
+
.query(
|
|
70
|
+
`INSERT INTO router_models (router_id, agent, models) VALUES (?, ?, ?)
|
|
71
|
+
ON CONFLICT (router_id, agent) DO UPDATE SET models = excluded.models`,
|
|
72
|
+
)
|
|
73
|
+
.run(routerId, agent ?? ROUTER_LIST, JSON.stringify(models));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Forget a saved list, so the config's is served again. */
|
|
77
|
+
remove(routerId: string, agent: ClientHarness | undefined): void {
|
|
78
|
+
this.db
|
|
79
|
+
.query("DELETE FROM router_models WHERE router_id = ? AND agent = ?")
|
|
80
|
+
.run(routerId, agent ?? ROUTER_LIST);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** `/v1/router/models` for the default router, `/<router>/v1/router/models` for another; a coding agent's list is one segment below. */
|
|
85
|
+
export const ROUTER_MODELS_ROUTE = /^(?:\/([^/]+))?\/v1\/router\/models(?:\/([^/]+))?$/;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Serve a router's model lists. The caller authenticates the gateway key.
|
|
89
|
+
*
|
|
90
|
+
* - `GET /v1/router/models` returns the lists as served.
|
|
91
|
+
* - `PUT /v1/router/models` with `{ "models": [...] }` saves the router's own
|
|
92
|
+
* list, and `DELETE` goes back to the config file's.
|
|
93
|
+
* - `PUT` and `DELETE /v1/router/models/<agent>` do the same for one coding
|
|
94
|
+
* agent's list.
|
|
95
|
+
*
|
|
96
|
+
* Each answers with the lists as served after the change. Returns null for
|
|
97
|
+
* any other path.
|
|
98
|
+
*/
|
|
99
|
+
export async function handleRouterModelsRequest(
|
|
100
|
+
request: Request,
|
|
101
|
+
deps: { config: GatewayConfig; routerModels: RouterModelStore },
|
|
102
|
+
): Promise<Response | null> {
|
|
103
|
+
const route = ROUTER_MODELS_ROUTE.exec(new URL(request.url).pathname);
|
|
104
|
+
if (route === null) return null;
|
|
105
|
+
try {
|
|
106
|
+
const routerId = route[1] ?? DEFAULT_ROUTER_ALIAS;
|
|
107
|
+
const configured = deps.config.routers[routerId];
|
|
108
|
+
if (configured === undefined) {
|
|
109
|
+
throw new HttpError(404, `Router ${routerId} is not configured on this gateway.`, "router_not_found");
|
|
110
|
+
}
|
|
111
|
+
const agent = route[2] as ClientHarness | undefined;
|
|
112
|
+
if (agent !== undefined && !CLIENT_HARNESSES.includes(agent)) {
|
|
113
|
+
throw new HttpError(
|
|
114
|
+
404,
|
|
115
|
+
`Unknown coding agent '${agent}'; use ${CLIENT_HARNESSES.join(", ")}.`,
|
|
116
|
+
"coding_agent_not_found",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (request.method === "PUT") {
|
|
120
|
+
deps.routerModels.save(routerId, agent, await requestedModels(request, routerId));
|
|
121
|
+
} else if (request.method === "DELETE") {
|
|
122
|
+
deps.routerModels.remove(routerId, agent);
|
|
123
|
+
} else if (request.method !== "GET" || agent !== undefined) {
|
|
124
|
+
throw new HttpError(405, "Method not allowed.", "method_not_allowed");
|
|
125
|
+
}
|
|
126
|
+
const served = deps.routerModels.served(routerId, configured);
|
|
127
|
+
return Response.json(
|
|
128
|
+
{
|
|
129
|
+
models: served.models,
|
|
130
|
+
agent_models: served.agentModels,
|
|
131
|
+
// False while the router serves the gateway's built-in default
|
|
132
|
+
// models, which nobody chose.
|
|
133
|
+
chosen: deps.config !== DEFAULT_GATEWAY_CONFIG || deps.routerModels.hasRouterList(routerId),
|
|
134
|
+
},
|
|
135
|
+
{ headers: { "Cache-Control": "no-store" } },
|
|
136
|
+
);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
return errorResponse(error);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function requestedModels(request: Request, routerId: string): Promise<RouterModelConfig[]> {
|
|
143
|
+
const body: unknown = await request.json().catch(() => null);
|
|
144
|
+
try {
|
|
145
|
+
return parseRouterModels(routerId, "models", (body as { models?: unknown } | null)?.models);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
throw new HttpError(400, error instanceof Error ? error.message : String(error), "invalid_request_error", "models");
|
|
148
|
+
}
|
|
149
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -88,7 +88,16 @@ export type PublicEvalCatalogCard = Omit<RouterEval, "scores"> & {
|
|
|
88
88
|
// and Anthropic routing metadata.
|
|
89
89
|
export type ServedDecision = RoutingDecision & { sessionPinned?: boolean };
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
// The coding agent a request comes from. Claude Code and Muse Code are named
|
|
92
|
+
// only from their own request headers, since their plans serve only them.
|
|
93
|
+
export type ClientHarness =
|
|
94
|
+
| "claude_code"
|
|
95
|
+
| "codex"
|
|
96
|
+
| "muse_code"
|
|
97
|
+
| "opencode"
|
|
98
|
+
| "pi"
|
|
99
|
+
| "openclaw"
|
|
100
|
+
| "hermes";
|
|
92
101
|
|
|
93
102
|
export type ResolvedRouterModelConfig = {
|
|
94
103
|
provider: string;
|