| 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 | // 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: state.session.mode === 'chat' ? "chatInteraction" : "agentInteraction", |
| 79 | data: { |
| 80 | prompt: next.value.prompt, |
| 81 | completion: next.value.completion, |
| 82 | modelProvider: selectedChatModel.provider, |
| 83 | modelTitle: selectedChatModel.title, |
| 84 | sessionId: state.session.id, |
| 85 | }, |
| 86 | }); |
| 87 | } |
| 88 | // else if (state.session.mode === "edit") { |
| 89 | // extra.ideMessenger.post("devdata/log", { |
| 90 | // name: "editInteraction", |
| 91 | // data: { |
| 92 | // prompt: next.value.prompt, |
| 93 | // completion: next.value.completion, |
| 94 | // modelProvider: selectedChatModel.provider, |
| 95 | // modelTitle: selectedChatModel.title, |
| 96 | // }, |
| 97 | // }); |
| 98 | // } |
| 99 | } catch (e) { |
| 100 | console.error("Failed to send dev data interaction log", e); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // If it's a tool call that is automatically accepted, we should call it |
| 105 | const newState = getState(); |
| 106 | const toolSettings = newState.ui.toolSettings; |
| 107 | const toolCallState = selectCurrentToolCall(newState); |
| 108 | if (toolCallState) { |
| 109 | dispatch( |
| 110 | setToolGenerated({ |
| 111 | toolCallId: toolCallState.toolCallId, |
| 112 | }), |
| 113 | ); |
| 114 | |
| 115 | if ( |
| 116 | toolSettings[toolCallState.toolCall.function.name] === |
| 117 | "allowedWithoutPermission" |
| 118 | ) { |
| 119 | const response = await dispatch(callCurrentTool()); |
| 120 | unwrapResult(response); |
| 121 | } |
| 122 | } |
| 123 | }, |
| 124 | ); |