From 6a44aecc245e38cefeb4438540b8392866b1fcd7 Mon Sep 17 00:00:00 2001 From: Danila Poyarkov Date: Thu, 13 Aug 2026 21:18:10 +0300 Subject: [PATCH] refactor(ai): isolate reasoning options --- src/app/ai/chat/reasoning.ts | 18 ++++++++++++++++++ src/app/ai/chat/transports.ts | 18 +----------------- src/app/ai/tools/vision.ts | 2 +- tests/engine/app/ai/model-roles.test.ts | 2 +- 4 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 src/app/ai/chat/reasoning.ts diff --git a/src/app/ai/chat/reasoning.ts b/src/app/ai/chat/reasoning.ts new file mode 100644 index 000000000..ada3c38cf --- /dev/null +++ b/src/app/ai/chat/reasoning.ts @@ -0,0 +1,18 @@ +import type { AIProviderID } from '@open-pencil/core/constants' + +type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue } +export type AIProviderOptions = Record + +export function buildReasoningProviderOptions( + providerID: AIProviderID, + reasoningEffort: string +): AIProviderOptions | undefined { + if (!reasoningEffort) return undefined + if (providerID === 'openrouter') { + return { openrouter: { reasoning: { effort: reasoningEffort } } } + } + if (providerID === 'openai' || providerID === 'openai-compatible') { + return { openai: { reasoningEffort } } + } + return undefined +} diff --git a/src/app/ai/chat/transports.ts b/src/app/ai/chat/transports.ts index aa840e570..0d5201460 100644 --- a/src/app/ai/chat/transports.ts +++ b/src/app/ai/chat/transports.ts @@ -13,6 +13,7 @@ import { type AIChatFailure } from '@/app/ai/chat/failure' import { resolveLanguageModelID } from '@/app/ai/chat/model' +import { buildReasoningProviderOptions, type AIProviderOptions } from '@/app/ai/chat/reasoning' import SYSTEM_PROMPT from '@/app/ai/chat/system-prompt.md?raw' import { createAIModelRuntime } from '@/app/ai/models' import { MAX_AGENT_STEPS, createAITools, recordStepUsage, resetRunSteps } from '@/app/ai/tools' @@ -49,23 +50,6 @@ function supportsAnthropicCaching(providerID: AIProviderID, modelID: string): bo ) } -type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue } -type AIProviderOptions = Record - -export function buildReasoningProviderOptions( - providerID: AIProviderID, - reasoningEffort: string -): AIProviderOptions | undefined { - if (!reasoningEffort) return undefined - if (providerID === 'openrouter') { - return { openrouter: { reasoning: { effort: reasoningEffort } } } - } - if (providerID === 'openai' || providerID === 'openai-compatible') { - return { openai: { reasoningEffort } } - } - return undefined -} - function mergeProviderOptions( cacheOptions: typeof ANTHROPIC_CACHE_CONTROL | undefined, reasoningOptions: AIProviderOptions | undefined diff --git a/src/app/ai/tools/vision.ts b/src/app/ai/tools/vision.ts index e7198666e..fbbbc5767 100644 --- a/src/app/ai/tools/vision.ts +++ b/src/app/ai/tools/vision.ts @@ -4,7 +4,7 @@ import * as v from 'valibot' import { computeContentBounds } from '@open-pencil/core/io' -import { buildReasoningProviderOptions } from '@/app/ai/chat/transports' +import { buildReasoningProviderOptions } from '@/app/ai/chat/reasoning' import { createAIModelRuntime } from '@/app/ai/models' import type { EditorStore } from '@/app/editor/active-store' diff --git a/tests/engine/app/ai/model-roles.test.ts b/tests/engine/app/ai/model-roles.test.ts index c2263f59d..1ebf9723d 100644 --- a/tests/engine/app/ai/model-roles.test.ts +++ b/tests/engine/app/ai/model-roles.test.ts @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test' -import { buildReasoningProviderOptions } from '@/app/ai/chat/transports' +import { buildReasoningProviderOptions } from '@/app/ai/chat/reasoning' import { aiModelSettings, createAIModelRuntime,