-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathstreamNormalInput.ts
More file actions
411 lines (378 loc) · 13.2 KB
/
streamNormalInput.ts
File metadata and controls
411 lines (378 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
import { LLMFullCompletionOptions, ModelDescription } from "core";
import { getRuleId } from "core/llm/rules/getSystemMessageWithRules";
import { ToCoreProtocol } from "core/protocol";
import { BUILT_IN_GROUP_NAME } from "core/tools/builtIn";
import { selectActiveTools } from "../selectors/selectActiveTools";
import { selectSelectedChatModel } from "../slices/configSlice";
import {
abortStream,
addPromptCompletionPair,
errorToolCall,
setActive,
setAppliedRulesAtIndex,
setContextPercentage,
setInactive,
setInlineErrorMessage,
setIsPruned,
setToolGenerated,
streamUpdate,
} from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
import { constructMessages } from "../util/constructMessages";
import { modelSupportsNativeTools } from "core/llm/toolSupport";
import { applyToolOverrides } from "core/tools/applyToolOverrides";
import { addSystemMessageToolsToSystemMessage } from "core/tools/systemMessageTools/buildToolsSystemMessage";
import { interceptSystemToolCalls } from "core/tools/systemMessageTools/interceptSystemToolCalls";
import { SystemMessageToolCodeblocksFramework } from "core/tools/systemMessageTools/toolCodeblocks";
import posthog from "posthog-js";
import {
selectCurrentToolCalls,
selectPendingToolCalls,
} from "../selectors/selectToolCalls";
import { getBaseSystemMessage } from "../util/getBaseSystemMessage";
import { callToolById } from "./callToolById";
import { evaluateToolPolicies } from "./evaluateToolPolicies";
import { preprocessToolCalls } from "./preprocessToolCallArgs";
import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
/**
* Builds completion options with reasoning configuration based on session state and model capabilities.
*
* @param baseOptions - Base completion options to extend
* @param hasReasoningEnabled - Whether reasoning is enabled in the session
* @param model - The selected model with provider and completion options
* @returns Completion options with reasoning configuration
*/
function buildReasoningCompletionOptions(
baseOptions: LLMFullCompletionOptions,
hasReasoningEnabled: boolean | undefined,
model: ModelDescription,
): LLMFullCompletionOptions {
if (model.completionOptions?.reasoning === false) {
return baseOptions;
}
if (hasReasoningEnabled === undefined) {
return baseOptions;
}
const reasoningOptions: LLMFullCompletionOptions = {
...baseOptions,
reasoning: !!hasReasoningEnabled,
};
// Add reasoning budget tokens if reasoning is enabled and provider supports it
if (hasReasoningEnabled && model.underlyingProviderName !== "ollama") {
// Ollama doesn't support limiting reasoning tokens at this point
reasoningOptions.reasoningBudgetTokens =
model.completionOptions?.reasoningBudgetTokens ?? 2048;
}
return reasoningOptions;
}
export const streamNormalInput = createAsyncThunk<
void,
{
legacySlashCommandData?: ToCoreProtocol["llm/streamChat"][0]["legacySlashCommandData"];
depth?: number;
},
ThunkApiType
>(
"chat/streamNormalInput",
async (
{ legacySlashCommandData, depth = 0 },
{ dispatch, extra, getState },
) => {
if (process.env.NODE_ENV === "test" && depth > 50) {
const message = `Max stream depth of ${50} reached in test`;
console.error(message, JSON.stringify(getState(), null, 2));
throw new Error(message);
}
const state = getState();
const selectedChatModel = selectSelectedChatModel(state);
if (!selectedChatModel) {
throw new Error("No chat model selected");
}
// Get tools and apply model-level overrides (disabled, description, etc.)
let activeTools = selectActiveTools(state);
if (selectedChatModel.toolOverrides?.length) {
const { tools: overriddenTools, errors } = applyToolOverrides(
activeTools,
selectedChatModel.toolOverrides,
);
activeTools = overriddenTools;
for (const error of errors) {
if (!error.fatal) {
console.warn(`Tool override warning: ${error.message}`);
}
}
}
// Use the centralized selector to determine if system message tools should be used
const useNativeTools = state.config.config.experimental
?.onlyUseSystemMessageTools
? false
: modelSupportsNativeTools(selectedChatModel);
const systemToolsFramework = !useNativeTools
? new SystemMessageToolCodeblocksFramework()
: undefined;
// Construct completion options
let completionOptions: LLMFullCompletionOptions = {};
if (useNativeTools && activeTools.length > 0) {
completionOptions = {
tools: activeTools,
};
}
completionOptions = buildReasoningCompletionOptions(
completionOptions,
state.session.hasReasoningEnabled,
selectedChatModel,
);
// Construct messages (excluding system message)
const baseSystemMessage = getBaseSystemMessage(
state.session.mode,
selectedChatModel,
activeTools,
);
const systemMessage = systemToolsFramework
? addSystemMessageToolsToSystemMessage(
systemToolsFramework,
baseSystemMessage,
activeTools,
)
: baseSystemMessage;
const withoutMessageIds = state.session.history.map((item) => {
const { id, ...messageWithoutId } = item.message;
return { ...item, message: messageWithoutId };
});
const { messages, appliedRules, appliedRuleIndex } = constructMessages(
withoutMessageIds,
systemMessage,
state.config.config.rules,
state.ui.ruleSettings,
systemToolsFramework,
);
// TODO parallel tool calls will cause issues with this
// because there will be multiple tool messages, so which one should have applied rules?
dispatch(
setAppliedRulesAtIndex({
index: appliedRuleIndex,
appliedRules: appliedRules,
}),
);
dispatch(setActive());
dispatch(setInlineErrorMessage(undefined));
const precompiledRes = await extra.ideMessenger.request("llm/compileChat", {
messages,
options: completionOptions,
});
if (precompiledRes.status === "error") {
if (precompiledRes.error.includes("Not enough context")) {
dispatch(setInlineErrorMessage("out-of-context"));
dispatch(setInactive());
return;
} else {
throw new Error(precompiledRes.error);
}
}
const { compiledChatMessages, didPrune, contextPercentage } =
precompiledRes.content;
dispatch(setIsPruned(didPrune));
dispatch(setContextPercentage(contextPercentage));
const start = Date.now();
const streamAborter = state.session.streamAborter;
try {
let gen = extra.ideMessenger.llmStreamChat(
{
completionOptions,
title: selectedChatModel.title,
messages: compiledChatMessages,
legacySlashCommandData,
messageOptions: { precompiled: true },
},
streamAborter.signal,
);
if (systemToolsFramework && activeTools.length > 0) {
gen = interceptSystemToolCalls(
gen,
streamAborter,
systemToolsFramework,
);
}
let next = await gen.next();
while (!next.done) {
if (!getState().session.isStreaming) {
dispatch(abortStream());
break;
}
dispatch(streamUpdate(next.value));
next = await gen.next();
}
// Attach prompt log and end thinking for reasoning models
if (next.done && next.value) {
dispatch(addPromptCompletionPair([next.value]));
try {
extra.ideMessenger.post("devdata/log", {
name: "chatInteraction",
data: {
prompt: next.value.prompt,
completion: next.value.completion,
modelProvider: selectedChatModel.underlyingProviderName,
modelName: selectedChatModel.title,
modelTitle: selectedChatModel.title,
sessionId: state.session.id,
...(!!activeTools.length && {
tools: activeTools.map((tool) => tool.function.name),
}),
...(appliedRules.length > 0 && {
rules: appliedRules.map((rule) => ({
id: getRuleId(rule),
slug: rule.slug,
})),
}),
},
});
} catch (e) {
console.error("Failed to send dev data interaction log", e);
}
}
} catch (e) {
const toolCallsToCancel = selectCurrentToolCalls(getState());
posthog.capture("stream_premature_close_error", {
duration: (Date.now() - start) / 1000,
model: selectedChatModel.model,
provider: selectedChatModel.underlyingProviderName,
context: legacySlashCommandData ? "slash_command" : "regular_chat",
...(legacySlashCommandData && {
command: legacySlashCommandData.command.name,
}),
});
if (
toolCallsToCancel.length > 0 &&
e instanceof Error &&
e.message.toLowerCase().includes("premature close")
) {
for (const tc of toolCallsToCancel) {
dispatch(
errorToolCall({
toolCallId: tc.toolCallId,
output: [
{
name: "Tool Call Error",
description: "Premature Close",
content: `"Premature Close" error: this tool call was aborted mid-stream because the arguments took too long to stream or there were network issues. Please re-attempt by breaking the operation into smaller chunks or trying something else`,
icon: "problems",
},
],
}),
);
}
} else {
throw e;
}
}
// Tool call sequence:
// 1. Mark generating tool calls as generated
const state1 = getState();
if (streamAborter.signal.aborted || !state1.session.isStreaming) {
return;
}
const originalToolCalls = selectCurrentToolCalls(state1);
const generatingCalls = originalToolCalls.filter(
(tc) => tc.status === "generating",
);
for (const { toolCallId } of generatingCalls) {
dispatch(
setToolGenerated({
toolCallId,
tools: state1.config.config.tools,
}),
);
}
// 2. Pre-process args to catch invalid args before checking policies
const state2 = getState();
if (streamAborter.signal.aborted || !state2.session.isStreaming) {
return;
}
const generatedCalls2 = selectPendingToolCalls(state2);
await preprocessToolCalls(dispatch, extra.ideMessenger, generatedCalls2);
// 3. Security check: evaluate updated policies based on args
const state3 = getState();
if (streamAborter.signal.aborted || !state3.session.isStreaming) {
return;
}
const generatedCalls3 = selectPendingToolCalls(state3);
const toolPolicies = state3.ui.toolSettings;
const policies = await evaluateToolPolicies(
dispatch,
extra.ideMessenger,
activeTools,
generatedCalls3,
toolPolicies,
);
const autoApprovedPolicies = policies.filter(
({ policy }) => policy === "allowedWithoutPermission",
);
const needsApprovalPolicies = policies.filter(
({ policy }) => policy === "allowedWithPermission",
);
// 4. Execute remaining tool calls
if (originalToolCalls.length === 0) {
dispatch(setInactive());
} else if (needsApprovalPolicies.length > 0) {
const builtInReadonlyAutoApproved = autoApprovedPolicies.filter(
({ toolCallState }) =>
toolCallState.tool?.group === BUILT_IN_GROUP_NAME &&
toolCallState.tool?.readonly,
);
if (builtInReadonlyAutoApproved.length > 0) {
const state4 = getState();
if (streamAborter.signal.aborted || !state4.session.isStreaming) {
return;
}
await Promise.all(
builtInReadonlyAutoApproved.map(async ({ toolCallState }) => {
unwrapResult(
await dispatch(
callToolById({
toolCallId: toolCallState.toolCallId,
isAutoApproved: true,
depth: depth + 1,
}),
),
);
}),
);
}
dispatch(setInactive());
} else {
// auto stream cases increase thunk depth by 1 for debugging
const state4 = getState();
const generatedCalls4 = selectPendingToolCalls(state4);
if (streamAborter.signal.aborted || !state4.session.isStreaming) {
return;
}
if (generatedCalls4.length > 0) {
await Promise.all(
generatedCalls4.map(async ({ toolCallId }) => {
unwrapResult(
await dispatch(
callToolById({
toolCallId,
isAutoApproved: true,
depth: depth + 1,
}),
),
);
}),
);
} else {
for (const { toolCallId } of originalToolCalls) {
unwrapResult(
await dispatch(
streamResponseAfterToolCall({
toolCallId,
depth: depth + 1,
}),
),
);
}
}
}
},
);