summaryrefslogtreecommitdiffhomepage
path: root/packages/api/tests
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-02 13:25:23 +0900
committerAdam Malczewski <[email protected]>2026-06-02 13:25:23 +0900
commit6433cc42de1ceca7210e2b64ad3b98b3a5ce7d02 (patch)
tree78b30dedd471ab76177b3631a956ab160615e303 /packages/api/tests
parent3f629a8469fe483243671e1ca15582a111e96541 (diff)
downloaddispatch-6433cc42de1ceca7210e2b64ad3b98b3a5ce7d02.tar.gz
dispatch-6433cc42de1ceca7210e2b64ad3b98b3a5ce7d02.zip
feat(context-window): show current/max context usage per tab/model
Add a 'Context Window' sidebar view showing the live context occupancy (latest request's input+output) against the model's maximum context window, resolved dynamically from the models.dev catalog. - core: models.dev catalog module (resolveContextLimit) with disk cache, TTL, stale-fallback + offline penalty memo; null for unknown models. - api: GET /models/context-limit?provider=&modelId=. - frontend: ContextWindowPanel + computeContextUsage helper; App resolves + caches the active model's max (anthropic/opencode-anthropic only); percent shown to 2 decimals; degrades to bare token count when max unknown. - tests: core catalog (13), api route (3), frontend helper (6).
Diffstat (limited to 'packages/api/tests')
-rw-r--r--packages/api/tests/routes.test.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/packages/api/tests/routes.test.ts b/packages/api/tests/routes.test.ts
index c768cee..e4b8f0f 100644
--- a/packages/api/tests/routes.test.ts
+++ b/packages/api/tests/routes.test.ts
@@ -268,6 +268,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() {
@@ -751,3 +758,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);
+ });
+});