| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- // npx jest src/api/transform/__tests__/reasoning.test.ts
- import type { ModelInfo, ProviderSettings } from "@roo-code/types"
- import {
- getOpenRouterReasoning,
- getAnthropicReasoning,
- getOpenAiReasoning,
- GetModelReasoningOptions,
- OpenRouterReasoningParams,
- AnthropicReasoningParams,
- OpenAiReasoningParams,
- } from "../reasoning"
- describe("reasoning.ts", () => {
- const baseModel: ModelInfo = {
- contextWindow: 16000,
- supportsPromptCache: true,
- }
- const baseSettings: ProviderSettings = {}
- const baseOptions: GetModelReasoningOptions = {
- model: baseModel,
- reasoningBudget: 1000,
- reasoningEffort: "medium",
- settings: baseSettings,
- }
- describe("getOpenRouterReasoning", () => {
- it("should return reasoning budget params when model has requiredReasoningBudget", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const options = { ...baseOptions, model: modelWithRequired }
- const result = getOpenRouterReasoning(options)
- expect(result).toEqual({ max_tokens: 1000 })
- })
- it("should return reasoning budget params when model supports reasoning budget and setting is enabled", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningBudget: true,
- }
- const settingsWithEnabled: ProviderSettings = {
- enableReasoningEffort: true,
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: settingsWithEnabled,
- }
- const result = getOpenRouterReasoning(options)
- expect(result).toEqual({ max_tokens: 1000 })
- })
- it("should return reasoning effort params when model supports reasoning effort and has effort in settings", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningEffort: true,
- }
- const settingsWithEffort: ProviderSettings = {
- reasoningEffort: "high",
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: settingsWithEffort,
- reasoningEffort: "high" as const,
- }
- const result = getOpenRouterReasoning(options)
- expect(result).toEqual({ effort: "high" })
- })
- it("should return reasoning effort params when model has reasoningEffort property", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: "medium",
- }
- const options = { ...baseOptions, model: modelWithEffort }
- const result = getOpenRouterReasoning(options)
- expect(result).toEqual({ effort: "medium" })
- })
- it("should return undefined when model has no reasoning capabilities", () => {
- const result = getOpenRouterReasoning(baseOptions)
- expect(result).toBeUndefined()
- })
- it("should prioritize reasoning budget over reasoning effort", () => {
- const hybridModel: ModelInfo = {
- ...baseModel,
- supportsReasoningBudget: true,
- reasoningEffort: "high",
- }
- const settingsWithBoth: ProviderSettings = {
- enableReasoningEffort: true,
- reasoningEffort: "low",
- }
- const options = {
- ...baseOptions,
- model: hybridModel,
- settings: settingsWithBoth,
- }
- const result = getOpenRouterReasoning(options)
- expect(result).toEqual({ max_tokens: 1000 })
- })
- it("should handle undefined reasoningBudget", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const optionsWithoutBudget = {
- ...baseOptions,
- model: modelWithRequired,
- reasoningBudget: undefined,
- }
- const result = getOpenRouterReasoning(optionsWithoutBudget)
- expect(result).toEqual({ max_tokens: undefined })
- })
- it("should handle undefined reasoningEffort", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: "medium",
- }
- const optionsWithoutEffort = {
- ...baseOptions,
- model: modelWithEffort,
- reasoningEffort: undefined,
- }
- const result = getOpenRouterReasoning(optionsWithoutEffort)
- expect(result).toEqual({ effort: undefined })
- })
- it("should handle all reasoning effort values", () => {
- const efforts: Array<"low" | "medium" | "high"> = ["low", "medium", "high"]
- efforts.forEach((effort) => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: effort,
- }
- const options = { ...baseOptions, model: modelWithEffort, reasoningEffort: effort }
- const result = getOpenRouterReasoning(options)
- expect(result).toEqual({ effort })
- })
- })
- it("should handle zero reasoningBudget", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const optionsWithZeroBudget = {
- ...baseOptions,
- model: modelWithRequired,
- reasoningBudget: 0,
- }
- const result = getOpenRouterReasoning(optionsWithZeroBudget)
- expect(result).toEqual({ max_tokens: 0 })
- })
- it("should not use reasoning budget when supportsReasoningBudget is true but enableReasoningEffort is false", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningBudget: true,
- }
- const settingsWithDisabled: ProviderSettings = {
- enableReasoningEffort: false,
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: settingsWithDisabled,
- }
- const result = getOpenRouterReasoning(options)
- expect(result).toBeUndefined()
- })
- it("should not use reasoning effort when supportsReasoningEffort is true but no effort is specified", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningEffort: true,
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: {},
- reasoningEffort: undefined,
- }
- const result = getOpenRouterReasoning(options)
- expect(result).toBeUndefined()
- })
- })
- describe("getAnthropicReasoning", () => {
- it("should return reasoning budget params when model has requiredReasoningBudget", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const options = { ...baseOptions, model: modelWithRequired }
- const result = getAnthropicReasoning(options)
- expect(result).toEqual({
- type: "enabled",
- budget_tokens: 1000,
- })
- })
- it("should return reasoning budget params when model supports reasoning budget and setting is enabled", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningBudget: true,
- }
- const settingsWithEnabled: ProviderSettings = {
- enableReasoningEffort: true,
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: settingsWithEnabled,
- }
- const result = getAnthropicReasoning(options)
- expect(result).toEqual({
- type: "enabled",
- budget_tokens: 1000,
- })
- })
- it("should return undefined when model has no reasoning budget capability", () => {
- const result = getAnthropicReasoning(baseOptions)
- expect(result).toBeUndefined()
- })
- it("should return undefined when supportsReasoningBudget is true but enableReasoningEffort is false", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningBudget: true,
- }
- const settingsWithDisabled: ProviderSettings = {
- enableReasoningEffort: false,
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: settingsWithDisabled,
- }
- const result = getAnthropicReasoning(options)
- expect(result).toBeUndefined()
- })
- it("should handle undefined reasoningBudget with non-null assertion", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const optionsWithoutBudget = {
- ...baseOptions,
- model: modelWithRequired,
- reasoningBudget: undefined,
- }
- const result = getAnthropicReasoning(optionsWithoutBudget)
- expect(result).toEqual({
- type: "enabled",
- budget_tokens: undefined,
- })
- })
- it("should handle zero reasoningBudget", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const optionsWithZeroBudget = {
- ...baseOptions,
- model: modelWithRequired,
- reasoningBudget: 0,
- }
- const result = getAnthropicReasoning(optionsWithZeroBudget)
- expect(result).toEqual({
- type: "enabled",
- budget_tokens: 0,
- })
- })
- it("should handle large reasoningBudget values", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const optionsWithLargeBudget = {
- ...baseOptions,
- model: modelWithRequired,
- reasoningBudget: 100000,
- }
- const result = getAnthropicReasoning(optionsWithLargeBudget)
- expect(result).toEqual({
- type: "enabled",
- budget_tokens: 100000,
- })
- })
- it("should not be affected by reasoningEffort parameter", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const optionsWithEffort = {
- ...baseOptions,
- model: modelWithRequired,
- reasoningEffort: "high" as const,
- }
- const result = getAnthropicReasoning(optionsWithEffort)
- expect(result).toEqual({
- type: "enabled",
- budget_tokens: 1000,
- })
- })
- it("should ignore reasoning effort capabilities for Anthropic", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- supportsReasoningEffort: true,
- reasoningEffort: "high",
- }
- const settingsWithEffort: ProviderSettings = {
- reasoningEffort: "medium",
- }
- const options = {
- ...baseOptions,
- model: modelWithEffort,
- settings: settingsWithEffort,
- }
- const result = getAnthropicReasoning(options)
- expect(result).toBeUndefined()
- })
- })
- describe("getOpenAiReasoning", () => {
- it("should return reasoning effort params when model supports reasoning effort and has effort in settings", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningEffort: true,
- }
- const settingsWithEffort: ProviderSettings = {
- reasoningEffort: "high",
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: settingsWithEffort,
- reasoningEffort: "high" as const,
- }
- const result = getOpenAiReasoning(options)
- expect(result).toEqual({ reasoning_effort: "high" })
- })
- it("should return reasoning effort params when model has reasoningEffort property", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: "medium",
- }
- const options = { ...baseOptions, model: modelWithEffort }
- const result = getOpenAiReasoning(options)
- expect(result).toEqual({ reasoning_effort: "medium" })
- })
- it("should return undefined when model has no reasoning effort capability", () => {
- const result = getOpenAiReasoning(baseOptions)
- expect(result).toBeUndefined()
- })
- it("should return undefined when supportsReasoningEffort is true but no effort is specified", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningEffort: true,
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: {},
- reasoningEffort: undefined,
- }
- const result = getOpenAiReasoning(options)
- expect(result).toBeUndefined()
- })
- it("should handle undefined reasoningEffort", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: "medium",
- }
- const optionsWithoutEffort = {
- ...baseOptions,
- model: modelWithEffort,
- reasoningEffort: undefined,
- }
- const result = getOpenAiReasoning(optionsWithoutEffort)
- expect(result).toEqual({ reasoning_effort: undefined })
- })
- it("should handle all reasoning effort values", () => {
- const efforts: Array<"low" | "medium" | "high"> = ["low", "medium", "high"]
- efforts.forEach((effort) => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: effort,
- }
- const options = { ...baseOptions, model: modelWithEffort, reasoningEffort: effort }
- const result = getOpenAiReasoning(options)
- expect(result).toEqual({ reasoning_effort: effort })
- })
- })
- it("should not be affected by reasoningBudget parameter", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: "medium",
- }
- const optionsWithBudget = {
- ...baseOptions,
- model: modelWithEffort,
- reasoningBudget: 5000,
- }
- const result = getOpenAiReasoning(optionsWithBudget)
- expect(result).toEqual({ reasoning_effort: "medium" })
- })
- it("should ignore reasoning budget capabilities for OpenAI", () => {
- const modelWithBudget: ModelInfo = {
- ...baseModel,
- supportsReasoningBudget: true,
- requiredReasoningBudget: true,
- }
- const settingsWithEnabled: ProviderSettings = {
- enableReasoningEffort: true,
- }
- const options = {
- ...baseOptions,
- model: modelWithBudget,
- settings: settingsWithEnabled,
- }
- const result = getOpenAiReasoning(options)
- expect(result).toBeUndefined()
- })
- })
- describe("Integration scenarios", () => {
- it("should handle model with requiredReasoningBudget across all providers", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const options = {
- ...baseOptions,
- model: modelWithRequired,
- }
- const openRouterResult = getOpenRouterReasoning(options)
- const anthropicResult = getAnthropicReasoning(options)
- const openAiResult = getOpenAiReasoning(options)
- expect(openRouterResult).toEqual({ max_tokens: 1000 })
- expect(anthropicResult).toEqual({ type: "enabled", budget_tokens: 1000 })
- expect(openAiResult).toBeUndefined()
- })
- it("should handle model with supportsReasoningEffort across all providers", () => {
- const modelWithSupported: ModelInfo = {
- ...baseModel,
- supportsReasoningEffort: true,
- }
- const settingsWithEffort: ProviderSettings = {
- reasoningEffort: "high",
- }
- const options = {
- ...baseOptions,
- model: modelWithSupported,
- settings: settingsWithEffort,
- reasoningEffort: "high" as const,
- }
- const openRouterResult = getOpenRouterReasoning(options)
- const anthropicResult = getAnthropicReasoning(options)
- const openAiResult = getOpenAiReasoning(options)
- expect(openRouterResult).toEqual({ effort: "high" })
- expect(anthropicResult).toBeUndefined()
- expect(openAiResult).toEqual({ reasoning_effort: "high" })
- })
- it("should handle model with both reasoning capabilities - budget takes precedence", () => {
- const hybridModel: ModelInfo = {
- ...baseModel,
- supportsReasoningBudget: true,
- reasoningEffort: "medium",
- }
- const settingsWithBoth: ProviderSettings = {
- enableReasoningEffort: true,
- reasoningEffort: "high",
- }
- const options = {
- ...baseOptions,
- model: hybridModel,
- settings: settingsWithBoth,
- }
- const openRouterResult = getOpenRouterReasoning(options)
- const anthropicResult = getAnthropicReasoning(options)
- const openAiResult = getOpenAiReasoning(options)
- // Budget should take precedence for OpenRouter and Anthropic
- expect(openRouterResult).toEqual({ max_tokens: 1000 })
- expect(anthropicResult).toEqual({ type: "enabled", budget_tokens: 1000 })
- // OpenAI should still use effort since it doesn't support budget
- expect(openAiResult).toEqual({ reasoning_effort: "medium" })
- })
- it("should handle empty settings", () => {
- const options = {
- ...baseOptions,
- settings: {},
- }
- const openRouterResult = getOpenRouterReasoning(options)
- const anthropicResult = getAnthropicReasoning(options)
- const openAiResult = getOpenAiReasoning(options)
- expect(openRouterResult).toBeUndefined()
- expect(anthropicResult).toBeUndefined()
- expect(openAiResult).toBeUndefined()
- })
- it("should handle undefined settings", () => {
- const options = {
- ...baseOptions,
- settings: undefined as any,
- }
- const openRouterResult = getOpenRouterReasoning(options)
- const anthropicResult = getAnthropicReasoning(options)
- const openAiResult = getOpenAiReasoning(options)
- expect(openRouterResult).toBeUndefined()
- expect(anthropicResult).toBeUndefined()
- expect(openAiResult).toBeUndefined()
- })
- it("should handle model with reasoningEffort property", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: "low",
- }
- const options = {
- ...baseOptions,
- model: modelWithEffort,
- reasoningEffort: "low" as const, // Override the baseOptions reasoningEffort
- }
- const openRouterResult = getOpenRouterReasoning(options)
- const anthropicResult = getAnthropicReasoning(options)
- const openAiResult = getOpenAiReasoning(options)
- expect(openRouterResult).toEqual({ effort: "low" })
- expect(anthropicResult).toBeUndefined()
- expect(openAiResult).toEqual({ reasoning_effort: "low" })
- })
- })
- describe("Type safety", () => {
- it("should return correct types for OpenRouter reasoning params", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const options = { ...baseOptions, model: modelWithRequired }
- const result: OpenRouterReasoningParams | undefined = getOpenRouterReasoning(options)
- expect(result).toBeDefined()
- if (result) {
- expect(typeof result).toBe("object")
- expect("max_tokens" in result || "effort" in result || "exclude" in result).toBe(true)
- }
- })
- it("should return correct types for Anthropic reasoning params", () => {
- const modelWithRequired: ModelInfo = {
- ...baseModel,
- requiredReasoningBudget: true,
- }
- const options = { ...baseOptions, model: modelWithRequired }
- const result: AnthropicReasoningParams | undefined = getAnthropicReasoning(options)
- expect(result).toBeDefined()
- if (result) {
- expect(result).toHaveProperty("type", "enabled")
- expect(result).toHaveProperty("budget_tokens")
- }
- })
- it("should return correct types for OpenAI reasoning params", () => {
- const modelWithEffort: ModelInfo = {
- ...baseModel,
- reasoningEffort: "medium",
- }
- const options = { ...baseOptions, model: modelWithEffort }
- const result: OpenAiReasoningParams | undefined = getOpenAiReasoning(options)
- expect(result).toBeDefined()
- if (result) {
- expect(result).toHaveProperty("reasoning_effort")
- }
- })
- })
- })
|