|
1 | 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
|
2 |
| -import { LLMFullCompletionOptions, Tool } from "core"; |
| 2 | +import { LLMFullCompletionOptions, ModelDescription, Tool } from "core"; |
3 | 3 | import { getRuleId } from "core/llm/rules/getSystemMessageWithRules";
|
4 | 4 | import { ToCoreProtocol } from "core/protocol";
|
5 | 5 | import { selectActiveTools } from "../selectors/selectActiveTools";
|
@@ -85,6 +85,38 @@ async function handleToolCallExecution(
|
85 | 85 | }
|
86 | 86 | }
|
87 | 87 |
|
| 88 | +/** |
| 89 | + * Builds completion options with reasoning configuration based on session state and model capabilities. |
| 90 | + * |
| 91 | + * @param baseOptions - Base completion options to extend |
| 92 | + * @param hasReasoningEnabled - Whether reasoning is enabled in the session |
| 93 | + * @param model - The selected model with provider and completion options |
| 94 | + * @returns Completion options with reasoning configuration |
| 95 | + */ |
| 96 | +function buildReasoningCompletionOptions( |
| 97 | + baseOptions: LLMFullCompletionOptions, |
| 98 | + hasReasoningEnabled: boolean | undefined, |
| 99 | + model: ModelDescription, |
| 100 | +): LLMFullCompletionOptions { |
| 101 | + if (hasReasoningEnabled === undefined) { |
| 102 | + return baseOptions; |
| 103 | + } |
| 104 | + |
| 105 | + const reasoningOptions: LLMFullCompletionOptions = { |
| 106 | + ...baseOptions, |
| 107 | + reasoning: !!hasReasoningEnabled, |
| 108 | + }; |
| 109 | + |
| 110 | + // Add reasoning budget tokens if reasoning is enabled and provider supports it |
| 111 | + if (hasReasoningEnabled && model.underlyingProviderName !== "ollama") { |
| 112 | + // Ollama doesn't support limiting reasoning tokens at this point |
| 113 | + reasoningOptions.reasoningBudgetTokens = |
| 114 | + model.completionOptions?.reasoningBudgetTokens ?? 2048; |
| 115 | + } |
| 116 | + |
| 117 | + return reasoningOptions; |
| 118 | +} |
| 119 | + |
88 | 120 | export const streamNormalInput = createAsyncThunk<
|
89 | 121 | void,
|
90 | 122 | {
|
@@ -121,19 +153,11 @@ export const streamNormalInput = createAsyncThunk<
|
121 | 153 | };
|
122 | 154 | }
|
123 | 155 |
|
124 |
| - if (state.session.hasReasoningEnabled !== undefined) { |
125 |
| - completionOptions = { |
126 |
| - ...completionOptions, |
127 |
| - reasoning: !!state.session.hasReasoningEnabled, |
128 |
| - ...(state.session.hasReasoningEnabled && |
129 |
| - selectedChatModel.underlyingProviderName !== "ollama" && { |
130 |
| - // Ollama doesn't support limiting reasoning tokens at this point |
131 |
| - reasoningBudgetTokens: |
132 |
| - selectedChatModel.completionOptions?.reasoningBudgetTokens ?? |
133 |
| - 2048, |
134 |
| - }), |
135 |
| - }; |
136 |
| - } |
| 156 | + completionOptions = buildReasoningCompletionOptions( |
| 157 | + completionOptions, |
| 158 | + state.session.hasReasoningEnabled, |
| 159 | + selectedChatModel, |
| 160 | + ); |
137 | 161 |
|
138 | 162 | // Construct messages (excluding system message)
|
139 | 163 | const baseSystemMessage = getBaseSystemMessage(
|
|
0 commit comments