Elffuss BraidHub

kikocis/continue public

cd33d3613239 · 5.1 KiBRawPermalink
1import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
2import { LLMFullCompletionOptions } from "core";
3import { modelSupportsTools } from "core/llm/autodetect";
4import { ToCoreProtocol } from "core/protocol";
5import { selectActiveTools } from "../selectors/selectActiveTools";
6import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall";
7import { selectSelectedChatModel } from "../slices/configSlice";
8import {
9 abortStream,
10 addPromptCompletionPair,
11 setActive,
12 setAppliedRulesAtIndex,
13 setInactive,
14 setToolGenerated,
15 streamUpdate,
16} from "../slices/sessionSlice";
17import { ThunkApiType } from "../store";
18import {
19 constructMessages,
20 getBaseSystemMessage,
21} from "../util/constructMessages";
22import { callCurrentTool } from "./callCurrentTool";
23
24export const streamNormalInput = createAsyncThunk<
25 void,
26 {
27 legacySlashCommandData?: ToCoreProtocol["llm/streamChat"][0]["legacySlashCommandData"];
28 },
29 ThunkApiType
30>(
31 "chat/streamNormalInput",
32 async ({ legacySlashCommandData }, { dispatch, extra, getState }) => {
33 const state = getState();
34 const selectedChatModel = selectSelectedChatModel(state);
35
36 if (!selectedChatModel) {
37 throw new Error("No chat model selected");
38 }
39
40 // Get tools
41 const activeTools = selectActiveTools(state);
42 const toolsSupported = modelSupportsTools(selectedChatModel);
43
44 // Construct completion options
45 let completionOptions: LLMFullCompletionOptions = {};
46 if (toolsSupported && activeTools.length > 0) {
47 completionOptions = {
48 tools: activeTools,
49 };
50 }
51
52 if (state.session.hasReasoningEnabled) {
53 completionOptions = {
54 ...completionOptions,
55 reasoning: true,
56 reasoningBudgetTokens: completionOptions.reasoningBudgetTokens ?? 2048,
57 };
58 }
59
60 // Construct messages (excluding system message)
61 const baseSystemMessage = getBaseSystemMessage(
62 state.session.mode,
63 selectedChatModel,
64 );
65
66 const withoutMessageIds = state.session.history.map((item) => {
67 const { id, ...messageWithoutId } = item.message;
68 return { ...item, message: messageWithoutId };
69 });
70 const { messages, appliedRules, appliedRuleIndex } = constructMessages(
71 withoutMessageIds,
72 baseSystemMessage,
73 state.config.config.rules,
74 state.ui.ruleSettings,
75 );
76
77 // TODO parallel tool calls will cause issues with this
78 // because there will be multiple tool messages, so which one should have applied rules?
79 dispatch(
80 setAppliedRulesAtIndex({
81 index: appliedRuleIndex,
82 appliedRules: appliedRules,
83 }),
84 );
85
86 dispatch(setActive());
87
88 // Send request and stream response
89 const streamAborter = state.session.streamAborter;
90 const gen = extra.ideMessenger.llmStreamChat(
91 {
92 completionOptions,
93 title: selectedChatModel.title,
94 messages: messages,
95 legacySlashCommandData,
96 },
97 streamAborter.signal,
98 );
99
100 let next = await gen.next();
101 while (!next.done) {
102 if (!getState().session.isStreaming) {
103 dispatch(abortStream());
104 break;
105 }
106
107 dispatch(streamUpdate(next.value));
108 next = await gen.next();
109 }
110
111 dispatch(setInactive());
112
113 // Attach prompt log and end thinking for reasoning models
114 if (next.done && next.value) {
115 dispatch(addPromptCompletionPair([next.value]));
116
117 try {
118 if (state.session.mode === "chat" || state.session.mode === "agent") {
119 extra.ideMessenger.post("devdata/log", {
120 name: "chatInteraction",
121 data: {
122 prompt: next.value.prompt,
123 completion: next.value.completion,
124 modelProvider: selectedChatModel.underlyingProviderName,
125 modelTitle: selectedChatModel.title,
126 sessionId: state.session.id,
127 ...(state.session.mode === "agent" && {
128 tools: activeTools.map((tool) => tool.function.name),
129 }),
130 },
131 });
132 }
133 // else if (state.session.mode === "edit") {
134 // extra.ideMessenger.post("devdata/log", {
135 // name: "editInteraction",
136 // data: {
137 // prompt: next.value.prompt,
138 // completion: next.value.completion,
139 // modelProvider: selectedChatModel.provider,
140 // modelTitle: selectedChatModel.title,
141 // },
142 // });
143 // }
144 } catch (e) {
145 console.error("Failed to send dev data interaction log", e);
146 }
147 }
148
149 // If it's a tool call that is automatically accepted, we should call it
150 const newState = getState();
151 const toolSettings = newState.ui.toolSettings;
152 const toolCallState = selectCurrentToolCall(newState);
153 if (toolCallState) {
154 dispatch(
155 setToolGenerated({
156 toolCallId: toolCallState.toolCallId,
157 }),
158 );
159
160 if (
161 toolSettings[toolCallState.toolCall.function.name] ===
162 "allowedWithoutPermission"
163 ) {
164 const response = await dispatch(callCurrentTool());
165 unwrapResult(response);
166 }
167 }
168 },
169);