| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { ChatMessage, 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 | setToolGenerated, |
| 12 | streamUpdate, |
| 13 | } from "../slices/sessionSlice"; |
| 14 | import { ThunkApiType } from "../store"; |
| 15 | import { callCurrentTool } from "./callCurrentTool"; |
| 16 | |
| 17 | export 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 | if (state.session.hasReasoningEnabled) { |
| 49 | completionOptions = { |
| 50 | ...completionOptions, |
| 51 | reasoning: true, |
| 52 | reasoningBudgetTokens: completionOptions.reasoningBudgetTokens ?? 2048, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | // Send request |
| 57 | const gen = extra.ideMessenger.llmStreamChat( |
| 58 | { |
| 59 | completionOptions, |
| 60 | title: selectedChatModel.title, |
| 61 | messages, |
| 62 | legacySlashCommandData, |
| 63 | }, |
| 64 | streamAborter.signal, |
| 65 | ); |
| 66 | |
| 67 | // Stream response |
| 68 | let next = await gen.next(); |
| 69 | while (!next.done) { |
| 70 | if (!getState().session.isStreaming) { |
| 71 | dispatch(abortStream()); |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | dispatch(streamUpdate(next.value)); |
| 76 | next = await gen.next(); |
| 77 | } |
| 78 | |
| 79 | // Attach prompt log and end thinking for reasoning models |
| 80 | if (next.done && next.value) { |
| 81 | dispatch(addPromptCompletionPair([next.value])); |
| 82 | |
| 83 | try { |
| 84 | if (state.session.mode === "chat" || state.session.mode === "agent") { |
| 85 | extra.ideMessenger.post("devdata/log", { |
| 86 | name: "chatInteraction", |
| 87 | data: { |
| 88 | prompt: next.value.prompt, |
| 89 | completion: next.value.completion, |
| 90 | modelProvider: selectedChatModel.underlyingProviderName, |
| 91 | modelTitle: selectedChatModel.title, |
| 92 | sessionId: state.session.id, |
| 93 | ...(state.session.mode === "agent" && { |
| 94 | tools: activeTools.map((tool) => tool.function.name), |
| 95 | }), |
| 96 | }, |
| 97 | }); |
| 98 | } |
| 99 | // else if (state.session.mode === "edit") { |
| 100 | // extra.ideMessenger.post("devdata/log", { |
| 101 | // name: "editInteraction", |
| 102 | // data: { |
| 103 | // prompt: next.value.prompt, |
| 104 | // completion: next.value.completion, |
| 105 | // modelProvider: selectedChatModel.provider, |
| 106 | // modelTitle: selectedChatModel.title, |
| 107 | // }, |
| 108 | // }); |
| 109 | // } |
| 110 | } catch (e) { |
| 111 | console.error("Failed to send dev data interaction log", e); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // If it's a tool call that is automatically accepted, we should call it |
| 116 | const newState = getState(); |
| 117 | const toolSettings = newState.ui.toolSettings; |
| 118 | const toolCallState = selectCurrentToolCall(newState); |
| 119 | if (toolCallState) { |
| 120 | dispatch( |
| 121 | setToolGenerated({ |
| 122 | toolCallId: toolCallState.toolCallId, |
| 123 | }), |
| 124 | ); |
| 125 | |
| 126 | if ( |
| 127 | toolSettings[toolCallState.toolCall.function.name] === |
| 128 | "allowedWithoutPermission" |
| 129 | ) { |
| 130 | const response = await dispatch(callCurrentTool()); |
| 131 | unwrapResult(response); |
| 132 | } |
| 133 | } |
| 134 | }, |
| 135 | ); |