| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { LLMFullCompletionOptions } from "core"; |
| 3 | import { modelSupportsTools } from "core/llm/autodetect"; |
| 4 | import { ToCoreProtocol } from "core/protocol"; |
| 5 | import { selectActiveTools } from "../selectors/selectActiveTools"; |
| 6 | import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; |
| 7 | import { selectSelectedChatModel } from "../slices/configSlice"; |
| 8 | import { |
| 9 | abortStream, |
| 10 | addPromptCompletionPair, |
| 11 | setActive, |
| 12 | setAppliedRulesAtIndex, |
| 13 | setInactive, |
| 14 | setToolGenerated, |
| 15 | streamUpdate, |
| 16 | } from "../slices/sessionSlice"; |
| 17 | import { ThunkApiType } from "../store"; |
| 18 | import { |
| 19 | constructMessages, |
| 20 | getBaseSystemMessage, |
| 21 | } from "../util/constructMessages"; |
| 22 | import { callCurrentTool } from "./callCurrentTool"; |
| 23 | |
| 24 | export 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 | extra.ideMessenger.post("devdata/log", { |
| 119 | name: "chatInteraction", |
| 120 | data: { |
| 121 | prompt: next.value.prompt, |
| 122 | completion: next.value.completion, |
| 123 | modelProvider: selectedChatModel.underlyingProviderName, |
| 124 | modelTitle: selectedChatModel.title, |
| 125 | sessionId: state.session.id, |
| 126 | ...(!!activeTools.length && { |
| 127 | tools: activeTools.map((tool) => tool.function.name), |
| 128 | }), |
| 129 | }, |
| 130 | }); |
| 131 | // else if (state.session.mode === "edit") { |
| 132 | // extra.ideMessenger.post("devdata/log", { |
| 133 | // name: "editInteraction", |
| 134 | // data: { |
| 135 | // prompt: next.value.prompt, |
| 136 | // completion: next.value.completion, |
| 137 | // modelProvider: selectedChatModel.provider, |
| 138 | // modelTitle: selectedChatModel.title, |
| 139 | // }, |
| 140 | // }); |
| 141 | // } |
| 142 | } catch (e) { |
| 143 | console.error("Failed to send dev data interaction log", e); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // If it's a tool call that is automatically accepted, we should call it |
| 148 | const newState = getState(); |
| 149 | const toolSettings = newState.ui.toolSettings; |
| 150 | const toolCallState = selectCurrentToolCall(newState); |
| 151 | if (toolCallState) { |
| 152 | dispatch( |
| 153 | setToolGenerated({ |
| 154 | toolCallId: toolCallState.toolCallId, |
| 155 | tools: state.config.config.tools, |
| 156 | }), |
| 157 | ); |
| 158 | |
| 159 | if ( |
| 160 | toolSettings[toolCallState.toolCall.function.name] === |
| 161 | "allowedWithoutPermission" |
| 162 | ) { |
| 163 | const response = await dispatch(callCurrentTool()); |
| 164 | unwrapResult(response); |
| 165 | } |
| 166 | } |
| 167 | }, |
| 168 | ); |