Elffuss BraidHub

kikocis/continue public

755feced0982 · 3.9 KiBRawPermalink
1import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit";
2import { ChatMessage, 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 setToolGenerated,
12 streamUpdate,
13} from "../slices/sessionSlice";
14import { ThunkApiType } from "../store";
15import { callCurrentTool } from "./callCurrentTool";
16
17export const streamNormalInput = createAsyncThunk<
18 void,
19 {
20 messages: ChatMessage[];
21 legacySlashCommandData?: ToCoreProtocol["llm/streamChat"][0]["legacySlashCommandData"];
22 },
23 ThunkApiType
24>(
25 "chat/streamNormalInput",
26 async (
27 { messages, legacySlashCommandData },
28 { dispatch, extra, getState },
29 ) => {
30 // Gather state
31 const state = getState();
32 const selectedChatModel = selectSelectedChatModel(state);
33
34 const streamAborter = state.session.streamAborter;
35 if (!selectedChatModel) {
36 throw new Error("Default model not defined");
37 }
38
39 let completionOptions: LLMFullCompletionOptions = {};
40 const activeTools = selectActiveTools(state);
41 const toolsSupported = modelSupportsTools(selectedChatModel);
42 if (toolsSupported && activeTools.length > 0) {
43 completionOptions = {
44 tools: activeTools,
45 };
46 }
47
48 // Send request
49 const gen = extra.ideMessenger.llmStreamChat(
50 {
51 completionOptions,
52 title: selectedChatModel.title,
53 messages,
54 legacySlashCommandData,
55 },
56 streamAborter.signal,
57 );
58
59 // Stream response
60 let next = await gen.next();
61 while (!next.done) {
62 if (!getState().session.isStreaming) {
63 dispatch(abortStream());
64 break;
65 }
66
67 dispatch(streamUpdate(next.value));
68 next = await gen.next();
69 }
70
71 // Attach prompt log and end thinking for reasoning models
72 if (next.done && next.value) {
73 dispatch(addPromptCompletionPair([next.value]));
74
75 try {
76 if (state.session.mode === "chat" || state.session.mode === "agent") {
77 extra.ideMessenger.post("devdata/log", {
78 name: "chatInteraction",
79 data: {
80 prompt: next.value.prompt,
81 completion: next.value.completion,
82 modelProvider: selectedChatModel.underlyingProviderName,
83 modelTitle: selectedChatModel.title,
84 sessionId: state.session.id,
85 ...(state.session.mode === "agent" && {
86 tools: activeTools.map((tool) => tool.function.name),
87 }),
88 },
89 });
90 }
91 // else if (state.session.mode === "edit") {
92 // extra.ideMessenger.post("devdata/log", {
93 // name: "editInteraction",
94 // data: {
95 // prompt: next.value.prompt,
96 // completion: next.value.completion,
97 // modelProvider: selectedChatModel.provider,
98 // modelTitle: selectedChatModel.title,
99 // },
100 // });
101 // }
102 } catch (e) {
103 console.error("Failed to send dev data interaction log", e);
104 }
105 }
106
107 // If it's a tool call that is automatically accepted, we should call it
108 const newState = getState();
109 const toolSettings = newState.ui.toolSettings;
110 const toolCallState = selectCurrentToolCall(newState);
111 if (toolCallState) {
112 dispatch(
113 setToolGenerated({
114 toolCallId: toolCallState.toolCallId,
115 }),
116 );
117
118 if (
119 toolSettings[toolCallState.toolCall.function.name] ===
120 "allowedWithoutPermission"
121 ) {
122 const response = await dispatch(callCurrentTool());
123 unwrapResult(response);
124 }
125 }
126 },
127);