Elffuss BraidHub

kikocis/continue public

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