Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat(chat): extend reasoning controls to OpenAI, Anthropic, and Google

Akarshan Biswas20140bbbdb84
diff --git a/web-app/src/containers/ChatInput.tsx b/web-app/src/containers/ChatInput.tsx
index a2f7b61e8..9e55ebfb7 100644
--- a/web-app/src/containers/ChatInput.tsx
+++ b/web-app/src/containers/ChatInput.tsx
@@ -2234,8 +2234,16 @@ const ChatInput = memo(function ChatInput({
)}
{!effectiveAgentMode &&
- selectedProvider === 'llamacpp' &&
+ (selectedProvider === 'llamacpp' ||
+ selectedProvider === 'google' ||
+ selectedProvider === 'gemini' ||
+ selectedProvider === 'anthropic' ||
+ selectedProvider === 'openai') &&
(() => {
+ // The token-budget submenu only applies to local llama.cpp
+ // (budget resolved against live n_ctx). Cloud providers size
+ // their own budget dynamically, so on/off/auto is enough.
+ const showThinkingBudget = selectedProvider === 'llamacpp'
const reasoningValue =
(selectedModel?.settings?.reasoning?.controller_props
?.value as 'auto' | 'on' | 'off' | undefined) ?? 'auto'
@@ -2282,6 +2290,114 @@ const ChatInput = memo(function ChatInput({
// chat transport both observe the new value.
selectModelProvider(selectedProvider, selectedModel.id)
}
+ const clearModelSetting = (settingKey: string) => {
+ if (!selectedProvider || !selectedModel) return
+ const providerObj = getProviderByName(selectedProvider)
+ if (!providerObj) return
+ const modelIndex = providerObj.models.findIndex(
+ (m) => m.id === selectedModel.id
+ )
+ if (modelIndex === -1) return
+ const nextSettings = { ...(selectedModel.settings ?? {}) }
+ delete nextSettings[settingKey]
+ const updatedModels = [...providerObj.models]
+ updatedModels[modelIndex] = {
+ ...selectedModel,
+ settings: nextSettings,
+ } as Model
+ updateProvider(selectedProvider, { models: updatedModels })
+ selectModelProvider(selectedProvider, selectedModel.id)
+ }
+
+ // OpenAI reasoning models expose a discrete effort (no
+ // on/off, no token budget). Reuse the thinking_budget_tokens
+ // level as the effort value; "Default" clears it so the
+ // model uses its own default effort.
+ if (selectedProvider === 'openai') {
+ const rawEffort =
+ selectedModel?.settings?.thinking_budget_tokens
+ ?.controller_props?.value
+ const currentEffort =
+ isThinkingBudgetLevelKey(rawEffort) &&
+ rawEffort !== 'unlimited'
+ ? rawEffort
+ : undefined
+ const EFFORTS: ThinkingBudgetLevelKey[] = [
+ 'low',
+ 'medium',
+ 'high',
+ 'xhigh',
+ ]
+ const effortLabel = currentEffort
+ ? THINKING_BUDGET_LEVELS.find(
+ (l) => l.key === currentEffort
+ )!.label
+ : 'Default'
+ return (
+ <DropdownMenu>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <DropdownMenuTrigger asChild>
+ <Button
+ variant="ghost"
+ size="icon-xs"
+ aria-label={`Reasoning effort: ${effortLabel}`}
+ >
+ <IconBrain
+ size={18}
+ className={cn(
+ 'text-muted-foreground',
+ currentEffort && 'text-primary'
+ )}
+ />
+ </Button>
+ </DropdownMenuTrigger>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>Reasoning effort: {effortLabel}</p>
+ </TooltipContent>
+ </Tooltip>
+ <DropdownMenuContent align="start">
+ <DropdownMenuItem
+ onClick={() =>
+ clearModelSetting('thinking_budget_tokens')
+ }
+ >
+ Default
+ {!currentEffort && (
+ <span className="ml-auto text-xs text-muted-foreground">
+ ✓
+ </span>
+ )}
+ </DropdownMenuItem>
+ {EFFORTS.map((key) => (
+ <DropdownMenuItem
+ key={key}
+ onClick={() =>
+ updateModelSetting(
+ 'thinking_budget_tokens',
+ 'Reasoning Effort',
+ 'dropdown',
+ key
+ )
+ }
+ >
+ {
+ THINKING_BUDGET_LEVELS.find(
+ (l) => l.key === key
+ )!.label
+ }
+ {currentEffort === key && (
+ <span className="ml-auto text-xs text-muted-foreground">
+ ✓
+ </span>
+ )}
+ </DropdownMenuItem>
+ ))}
+ </DropdownMenuContent>
+ </DropdownMenu>
+ )
+ }
const setReasoning = (value: 'auto' | 'on' | 'off') =>
updateModelSetting(
'reasoning',
@@ -2300,7 +2416,7 @@ const ChatInput = memo(function ChatInput({
? 'Reasoning forced on for every request.'
: reasoningValue === 'off'
? 'Reasoning disabled for every request.'
- : "Reasoning auto-detected from the model's chat template."
+ : "Reasoning uses the model's default."
// Stored as a symbolic level, not an absolute token count:
// the live context size (post auto-fit) is only known once
@@ -2380,44 +2496,53 @@ const ChatInput = memo(function ChatInput({
</span>
)}
</DropdownMenuItem>
- <DropdownMenuSeparator />
- <DropdownMenuSub>
- <DropdownMenuSubTrigger>
- <span className="flex-1">Thinking Budget</span>
- <span className="text-xs text-muted-foreground">
- {currentBudgetLabel}
- </span>
- </DropdownMenuSubTrigger>
- <DropdownMenuSubContent
- collisionPadding={{ bottom: 16 }}
- >
- {THINKING_BUDGET_LEVELS.map((level) => {
- const approxTokens = tokensForThinkingBudgetLevel(
- level.key,
- approxContextSize
- )
- return (
- <DropdownMenuItem
- key={level.key}
- onClick={() => setThinkingBudget(level.key)}
- className="gap-2"
- >
- <span className="flex-1">{level.label}</span>
- <span className="w-14 shrink-0 text-right text-xs text-muted-foreground tabular-nums">
- {approxTokens === -1
- ? ''
- : `~${approxTokens}`}
- </span>
- <span className="w-3 shrink-0 text-xs text-muted-foreground">
- {currentBudgetLevel === level.key
- ? '✓'
- : ''}
- </span>
- </DropdownMenuItem>
- )
- })}
- </DropdownMenuSubContent>
- </DropdownMenuSub>
+ {showThinkingBudget && (
+ <>
+ <DropdownMenuSeparator />
+ <DropdownMenuSub>
+ <DropdownMenuSubTrigger>
+ <span className="flex-1">Thinking Budget</span>
+ <span className="text-xs text-muted-foreground">
+ {currentBudgetLabel}
+ </span>
+ </DropdownMenuSubTrigger>
+ <DropdownMenuSubContent
+ collisionPadding={{ bottom: 16 }}
+ >
+ {THINKING_BUDGET_LEVELS.map((level) => {
+ const approxTokens =
+ tokensForThinkingBudgetLevel(
+ level.key,
+ approxContextSize
+ )
+ return (
+ <DropdownMenuItem
+ key={level.key}
+ onClick={() =>
+ setThinkingBudget(level.key)
+ }
+ className="gap-2"
+ >
+ <span className="flex-1">
+ {level.label}
+ </span>
+ <span className="w-14 shrink-0 text-right text-xs text-muted-foreground tabular-nums">
+ {approxTokens === -1
+ ? ''
+ : `~${approxTokens}`}
+ </span>
+ <span className="w-3 shrink-0 text-xs text-muted-foreground">
+ {currentBudgetLevel === level.key
+ ? '✓'
+ : ''}
+ </span>
+ </DropdownMenuItem>
+ )
+ })}
+ </DropdownMenuSubContent>
+ </DropdownMenuSub>
+ </>
+ )}
</DropdownMenuContent>
</DropdownMenu>
)
diff --git a/web-app/src/lib/__tests__/reasoningProviderOptions.test.ts b/web-app/src/lib/__tests__/reasoningProviderOptions.test.ts
new file mode 100644
index 000000000..821ac34b9
--- /dev/null
+++ b/web-app/src/lib/__tests__/reasoningProviderOptions.test.ts
@@ -0,0 +1,112 @@
+import { describe, expect, it } from 'vitest'
+import { buildReasoningProviderOptions } from '../reasoningProviderOptions'
+
+const modelWith = (settings: Record<string, unknown>) =>
+ ({ settings }) as unknown as Model
+
+const reasoning = (value: string) => ({
+ reasoning: { controller_props: { value } },
+})
+const budget = (value: string) => ({
+ thinking_budget_tokens: { controller_props: { value } },
+})
+
+describe('buildReasoningProviderOptions', () => {
+ it('returns undefined for unknown/local providers', () => {
+ expect(
+ buildReasoningProviderOptions('llamacpp', modelWith(reasoning('on')))
+ ).toBeUndefined()
+ expect(buildReasoningProviderOptions('mlx', null)).toBeUndefined()
+ })
+
+ describe('google', () => {
+ it('maps reasoning off to a zero thinking budget', () => {
+ expect(
+ buildReasoningProviderOptions('google', modelWith(reasoning('off')))
+ ).toEqual({ google: { thinkingConfig: { thinkingBudget: 0 } } })
+ })
+
+ it('maps reasoning on to a dynamic budget with thought summaries', () => {
+ expect(
+ buildReasoningProviderOptions('google', modelWith(reasoning('on')))
+ ).toEqual({
+ google: { thinkingConfig: { thinkingBudget: -1, includeThoughts: true } },
+ })
+ })
+
+ it('enables dynamic thinking when only a budget level is set', () => {
+ expect(
+ buildReasoningProviderOptions('google', modelWith(budget('high')))
+ ).toEqual({
+ google: { thinkingConfig: { thinkingBudget: -1, includeThoughts: true } },
+ })
+ })
+
+ it('returns undefined when reasoning is auto with no level', () => {
+ expect(
+ buildReasoningProviderOptions('google', modelWith(reasoning('auto')))
+ ).toBeUndefined()
+ })
+
+ it('matches the real gemini provider id too', () => {
+ expect(
+ buildReasoningProviderOptions('gemini', modelWith(reasoning('on')))
+ ).toEqual({
+ google: { thinkingConfig: { thinkingBudget: -1, includeThoughts: true } },
+ })
+ })
+ })
+
+ describe('anthropic', () => {
+ it('maps reasoning off to disabled thinking', () => {
+ expect(
+ buildReasoningProviderOptions('anthropic', modelWith(reasoning('off')))
+ ).toEqual({ anthropic: { thinking: { type: 'disabled' } } })
+ })
+
+ it('maps reasoning on to adaptive thinking (no token guess)', () => {
+ expect(
+ buildReasoningProviderOptions('anthropic', modelWith(reasoning('on')))
+ ).toEqual({
+ anthropic: { thinking: { type: 'adaptive', display: 'summarized' } },
+ })
+ })
+
+ it('returns undefined at provider default', () => {
+ expect(
+ buildReasoningProviderOptions('anthropic', modelWith({}))
+ ).toBeUndefined()
+ })
+ })
+
+ describe('openai', () => {
+ it('maps each concrete budget level to a reasoning effort', () => {
+ const cases: Array<[string, string]> = [
+ ['low', 'low'],
+ ['medium', 'medium'],
+ ['high', 'high'],
+ ['xhigh', 'xhigh'],
+ ]
+ for (const [level, effort] of cases) {
+ expect(
+ buildReasoningProviderOptions('openai', modelWith(budget(level)))
+ ).toEqual({
+ openai: { reasoningEffort: effort, reasoningSummary: 'auto' },
+ })
+ }
+ })
+
+ it('treats unlimited as no explicit effort (model default)', () => {
+ expect(
+ buildReasoningProviderOptions('openai', modelWith(budget('unlimited')))
+ ).toBeUndefined()
+ })
+
+ it('returns undefined when no level is chosen (reasoning off has no effort)', () => {
+ expect(
+ buildReasoningProviderOptions('openai', modelWith(reasoning('off')))
+ ).toBeUndefined()
+ expect(buildReasoningProviderOptions('openai', modelWith({}))).toBeUndefined()
+ })
+ })
+})
diff --git a/web-app/src/lib/custom-chat-transport.ts b/web-app/src/lib/custom-chat-transport.ts
index 25a59f2d2..9ec4c27da 100644
--- a/web-app/src/lib/custom-chat-transport.ts
+++ b/web-app/src/lib/custom-chat-transport.ts
@@ -28,6 +28,7 @@ import {
tokensForThinkingBudgetLevel,
isThinkingBudgetLevelKey,
} from '@/lib/thinkingBudget'
+import { buildReasoningProviderOptions } from '@/lib/reasoningProviderOptions'
import {
ExtensionTypeEnum,
VectorDBExtension,
@@ -1175,6 +1176,13 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
const modelSupportsTools = selectedModel?.capabilities?.includes('tools') ?? this.modelSupportsTools
const shouldEnableTools = hasTools && modelSupportsTools
+ // Cloud providers take reasoning via the AI SDK's per-request
+ // providerOptions (native thinking config), not the raw body.
+ const reasoningProviderOptions = buildReasoningProviderOptions(
+ providerId,
+ useModelProvider.getState().selectedModel
+ )
+
let streamStartTime: number | undefined
useAppState.getState().updatePromptProgress(undefined)
useAppState.getState().updateThreadPromptProgress(threadId, undefined)
@@ -1189,6 +1197,9 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
toolChoice: shouldEnableTools ? 'auto' : undefined,
system: effectiveSystem,
...(maxOutputTokens !== undefined ? { maxTokens: maxOutputTokens } : {}),
+ ...(reasoningProviderOptions
+ ? { providerOptions: reasoningProviderOptions }
+ : {}),
experimental_repairToolCall: async ({ toolCall, error }) => {
// Windows paths (`C:\Users\...`) contain invalid JSON escapes that make
// the SDK's argument parse fail. Re-escape lone backslashes and retry
diff --git a/web-app/src/lib/model-factory.ts b/web-app/src/lib/model-factory.ts
index 0b709a4d5..c92471d65 100644
--- a/web-app/src/lib/model-factory.ts
+++ b/web-app/src/lib/model-factory.ts
@@ -64,6 +64,7 @@ import { hasVideoSentinel, splitVideoSentinels } from './video-sentinel'
import { filterDefaultSseEvents } from './sseEventTypeFilter'
import { isPlatformTauri } from '@/lib/platform/utils'
import { providerRemoteApiKeyChain } from '@/lib/provider-api-keys'
+import { isThinkingBudgetLevelKey } from '@/lib/thinkingBudget'
import {
LLAMACPP_ONLY_PARAM_KEYS,
paramsSettings,
@@ -1085,7 +1086,17 @@ export class ModelFactory {
fetch: fetchImpl,
})
- return openai.chat(modelId)
+ // Reasoning summaries (reasoningSummary) are only returned by the Responses
+ // API, so switch to it when the user set a concrete effort level. Everyone
+ // else stays on Chat Completions to avoid breaking OpenAI-compatible
+ // proxies that only implement /chat/completions.
+ const effortLevel =
+ provider.models?.find((m) => m.id === modelId)?.settings
+ ?.thinking_budget_tokens?.controller_props?.value
+ const useResponses =
+ isThinkingBudgetLevelKey(effortLevel) && effortLevel !== 'unlimited'
+
+ return useResponses ? openai.responses(modelId) : openai.chat(modelId)
}
/**
diff --git a/web-app/src/lib/reasoningProviderOptions.ts b/web-app/src/lib/reasoningProviderOptions.ts
new file mode 100644
index 000000000..6d7083537
--- /dev/null
+++ b/web-app/src/lib/reasoningProviderOptions.ts
@@ -0,0 +1,90 @@
+import {
+ isThinkingBudgetLevelKey,
+ type ThinkingBudgetLevelKey,
+} from './thinkingBudget'
+
+type ReasoningChoice = 'auto' | 'on' | 'off' | undefined
+
+// OpenAI's reasoning_effort is a discrete level (no token budget). Our shared
+// thinking-budget levels map 1:1; 'unlimited' has no effort equivalent and is
+// treated as "no explicit effort" (model default), matching the UI's Default.
+const LEVEL_TO_OPENAI_EFFORT: Partial<Record<ThinkingBudgetLevelKey, string>> = {
+ low: 'low',
+ medium: 'medium',
+ high: 'high',
+ xhigh: 'xhigh',
+}
+
+function readReasoning(model: Model | null | undefined): ReasoningChoice {
+ const v = model?.settings?.reasoning?.controller_props?.value
+ return v === 'on' || v === 'off' || v === 'auto' ? v : undefined
+}
+
+function readBudgetLevel(
+ model: Model | null | undefined
+): ThinkingBudgetLevelKey | undefined {
+ const v = model?.settings?.thinking_budget_tokens?.controller_props?.value
+ return isThinkingBudgetLevelKey(v) ? v : undefined
+}
+
+/**
+ * Translate Jan's shared reasoning settings (reasoning on/off/auto + thinking
+ * budget level) into the per-request `providerOptions` the AI SDK expects for
+ * cloud providers, using each provider's NATIVE options rather than a
+ * context-derived token budget (which we can't know for cloud models):
+ *
+ * - Google Gemini: `thinkingConfig.thinkingBudget` -1 (dynamic, model-sized) /
+ * 0 (off), plus `includeThoughts` to surface thought summaries.
+ * - Anthropic: `thinking` adaptive (model-sized, no token guess) / disabled.
+ * - OpenAI: `reasoningEffort` discrete level (no budget).
+ *
+ * Returns undefined when the provider has no mapping or the user left reasoning
+ * at its provider default.
+ */
+export function buildReasoningProviderOptions(
+ providerId: string,
+ model: Model | null | undefined
+): Record<string, Record<string, unknown>> | undefined {
+ const reasoning = readReasoning(model)
+ const level = readBudgetLevel(model)
+
+ if (providerId === 'google' || providerId === 'gemini') {
+ if (reasoning === 'off') {
+ return { google: { thinkingConfig: { thinkingBudget: 0 } } }
+ }
+ if (reasoning === 'on' || level) {
+ return {
+ google: { thinkingConfig: { thinkingBudget: -1, includeThoughts: true } },
+ }
+ }
+ return undefined
+ }
+
+ if (providerId === 'anthropic') {
+ if (reasoning === 'off') {
+ return { anthropic: { thinking: { type: 'disabled' } } }
+ }
+ if (reasoning === 'on' || level) {
+ // Adaptive lets Claude size its own thinking budget; 'summarized' streams
+ // the thought summary we render in the reasoning trace.
+ return {
+ anthropic: { thinking: { type: 'adaptive', display: 'summarized' } },
+ }
+ }
+ return undefined
+ }
+
+ if (providerId === 'openai') {
+ // Reasoning models always reason, so 'off' has no universal equivalent;
+ // only a concrete effort level maps ('unlimited' = model default).
+ // reasoningSummary surfaces the (otherwise hidden) reasoning as summary
+ // parts — it requires the Responses API, which model-factory selects when
+ // an effort level is set.
+ const effort = level ? LEVEL_TO_OPENAI_EFFORT[level] : undefined
+ return effort
+ ? { openai: { reasoningEffort: effort, reasoningSummary: 'auto' } }
+ : undefined
+ }
+
+ return undefined
+}