|
| 1 | +import { describe, test, expect, afterEach } from "bun:test"; |
| 2 | +import type { ConversationTurn, InferenceOptions } from "@intx/types/runtime"; |
| 3 | +import { SOURCE_MAX_TOKENS, type ProviderCatalogEntry } from "./index.js"; |
| 4 | +import { |
| 5 | + buildInferenceSourceForRef, |
| 6 | + type BuildSourceContext, |
| 7 | +} from "./inference-sources.js"; |
| 8 | +import type { Settings } from "./settings.js"; |
| 9 | +import { |
| 10 | + buildProviderContextWindowOverrides, |
| 11 | + contextWindowFor, |
| 12 | + setProviderContextWindowOverrides, |
| 13 | +} from "../provider/context-window.js"; |
| 14 | +import { createOpenAICompatibleAdapter } from "../provider/openai-compatible-adapter.js"; |
| 15 | + |
| 16 | +const WINDOW = 400_000; |
| 17 | + |
| 18 | +function catalog(): ProviderCatalogEntry[] { |
| 19 | + return [ |
| 20 | + { |
| 21 | + name: "fp", |
| 22 | + baseURL: "https://fp.example/v1", |
| 23 | + apiKey: "fp-key", |
| 24 | + models: ["fp-large"], |
| 25 | + }, |
| 26 | + ]; |
| 27 | +} |
| 28 | + |
| 29 | +function ctx(): BuildSourceContext { |
| 30 | + return { sessionId: "sess-1", catalog: catalog() }; |
| 31 | +} |
| 32 | + |
| 33 | +function settingsWithWindow(): Settings { |
| 34 | + return { |
| 35 | + providers: { |
| 36 | + fp: { |
| 37 | + baseURL: "https://fp.example/v1", |
| 38 | + apiKey: "fp-key", |
| 39 | + models: ["fp-large"], |
| 40 | + contextWindow: WINDOW, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +afterEach(() => { |
| 47 | + setProviderContextWindowOverrides(undefined); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("contextWindow / maxTokens split (CL-7784)", () => { |
| 51 | + test("setting contextWindow does not change the source output budget", () => { |
| 52 | + const source = buildInferenceSourceForRef( |
| 53 | + { provider: "fp", model: "fp-large" }, |
| 54 | + ctx(), |
| 55 | + settingsWithWindow(), |
| 56 | + ); |
| 57 | + expect(source?.defaults?.maxTokens).toBe(SOURCE_MAX_TOKENS); |
| 58 | + }); |
| 59 | + |
| 60 | + test("contextWindow 400000 does not reach the wire as max_tokens 400000", () => { |
| 61 | + const source = buildInferenceSourceForRef( |
| 62 | + { provider: "fp", model: "fp-large" }, |
| 63 | + ctx(), |
| 64 | + settingsWithWindow(), |
| 65 | + ); |
| 66 | + const adapter = createOpenAICompatibleAdapter( |
| 67 | + source as unknown as Parameters<typeof createOpenAICompatibleAdapter>[0], |
| 68 | + ); |
| 69 | + const messages = [ |
| 70 | + { role: "user", content: [{ type: "text", text: "hi" }] }, |
| 71 | + ] as unknown as ConversationTurn[]; |
| 72 | + const built = adapter.buildRequest(messages, "fp-large", { |
| 73 | + maxTokens: source?.defaults?.maxTokens, |
| 74 | + } as InferenceOptions); |
| 75 | + const body = JSON.parse(built.body) as Record<string, unknown>; |
| 76 | + expect(body["max_tokens"]).toBe(SOURCE_MAX_TOKENS); |
| 77 | + expect(body["max_tokens"]).not.toBe(WINDOW); |
| 78 | + }); |
| 79 | + |
| 80 | + test("setting contextWindow still changes contextWindowFor", () => { |
| 81 | + const settings = settingsWithWindow(); |
| 82 | + setProviderContextWindowOverrides( |
| 83 | + buildProviderContextWindowOverrides(settings.providers, "fp", "fp-large"), |
| 84 | + ); |
| 85 | + expect(contextWindowFor("fp:fp-large")).toBe(WINDOW); |
| 86 | + }); |
| 87 | +}); |
0 commit comments