diff options
| author | Adam Malczewski <[email protected]> | 2026-06-02 13:48:15 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-02 13:48:15 +0900 |
| commit | d635b7e95e7a0432d9e246f5f3f0eb1335c6adc2 (patch) | |
| tree | 6b2457405f126ed476e7aafd0e2e0296056eaaf6 /packages/api | |
| parent | 48c120e5cd400b2e2b8afae0afcc7c8bc4d2ccb4 (diff) | |
| parent | d9b42227fa309dc0f15999dafe944cb6dd560b02 (diff) | |
| download | dispatch-d635b7e95e7a0432d9e246f5f3f0eb1335c6adc2.tar.gz dispatch-d635b7e95e7a0432d9e246f5f3f0eb1335c6adc2.zip | |
Merge branch 'dev' into u1/usage-persistence
Diffstat (limited to 'packages/api')
| -rw-r--r-- | packages/api/src/routes/models.ts | 16 | ||||
| -rw-r--r-- | packages/api/tests/routes.test.ts | 32 |
2 files changed, 48 insertions, 0 deletions
diff --git a/packages/api/src/routes/models.ts b/packages/api/src/routes/models.ts index 03c079a..6a0f5dc 100644 --- a/packages/api/src/routes/models.ts +++ b/packages/api/src/routes/models.ts @@ -17,6 +17,7 @@ import { listStoredCredentials, refreshAccountCredentialsAsync, resolveApiKey, + resolveContextLimit, setApiKey, validateAccountCredentials, } from "@dispatch/core"; @@ -161,6 +162,21 @@ modelsRoutes.get("/available", async (c) => { return c.json({ models }); }); +// Resolve a model's MAXIMUM context window (in tokens) from the models.dev +// catalog. Returns `{ contextLimit: number | null }`; `null` means the model's +// limit is unknown (unsupported provider, unknown model, or catalog offline), +// which the frontend renders without a denominator/percentage. +modelsRoutes.get("/context-limit", async (c) => { + const provider = c.req.query("provider"); + const modelId = c.req.query("modelId"); + if (!provider || !modelId) { + return c.json({ error: "provider and modelId query parameters are required" }, 400); + } + + const contextLimit = await resolveContextLimit(provider, modelId); + return c.json({ contextLimit }); +}); + // List available Claude accounts with validated credentials modelsRoutes.get("/claude-accounts", async (c) => { const candidates = resolveClaudeAccounts(); diff --git a/packages/api/tests/routes.test.ts b/packages/api/tests/routes.test.ts index ad6d5b1..feed217 100644 --- a/packages/api/tests/routes.test.ts +++ b/packages/api/tests/routes.test.ts @@ -289,6 +289,13 @@ vi.mock("@dispatch/core", () => ({ execute: async () => "mock", }; }, + // ── models.dev context-limit stub ───────────────────────────── + resolveContextLimit(provider: string, modelId: string) { + if (provider === "anthropic" && modelId === "claude-sonnet-4-5") { + return Promise.resolve(200000); + } + return Promise.resolve(null); + }, // ── ntfy notifications stubs ────────────────────────────────── NotificationDispatcher: class MockNotificationDispatcher { attachToAgentManager() { @@ -831,3 +838,28 @@ describe("Wake schedule routes", () => { expect(body.schedule["13"]).toBeUndefined(); }); }); + +describe("GET /models/context-limit", () => { + it("returns the resolved context limit for a known model", async () => { + const res = await app.request( + "/models/context-limit?provider=anthropic&modelId=claude-sonnet-4-5", + ); + expect(res.status).toBe(200); + const body = (await res.json()) as { contextLimit: number | null }; + expect(body.contextLimit).toBe(200000); + }); + + it("returns null contextLimit for an unknown model", async () => { + const res = await app.request("/models/context-limit?provider=anthropic&modelId=mystery"); + expect(res.status).toBe(200); + const body = (await res.json()) as { contextLimit: number | null }; + expect(body.contextLimit).toBeNull(); + }); + + it("400s when provider or modelId is missing", async () => { + const res1 = await app.request("/models/context-limit?provider=anthropic"); + expect(res1.status).toBe(400); + const res2 = await app.request("/models/context-limit?modelId=claude-sonnet-4-5"); + expect(res2.status).toBe(400); + }); +}); |
