diff --git a/core/core.ts b/core/core.ts index 4c14fff02..415b621bd 100644 --- a/core/core.ts +++ b/core/core.ts @@ -421,7 +421,6 @@ export class Core { const chunk = next.value; - // @ts-ignore yield { content: chunk }; next = await gen.next(); } diff --git a/core/protocol/core.ts b/core/protocol/core.ts index 3849f2702..19f1942fe 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -23,10 +23,26 @@ import type { } from "../"; import { ConfigResult } from "../config/load"; -export type ProtocolGeneratorType = AsyncGenerator<{ +export type ProtocolGeneratorYield = { done?: boolean; content: T; -}>; +}; +export type ProtocolGeneratorType = AsyncGenerator< + ProtocolGeneratorYield +>; + +export type AsyncGeneratorYieldType = + T extends AsyncGenerator + ? Y extends ProtocolGeneratorYield + ? PR + : never + : never; +// export type AsyncGeneratorReturnType = +// T extends AsyncGenerator +// ? R extends ProtocolGeneratorYield +// ? PR +// : never +// : never; export type OnboardingModes = | "Local" diff --git a/core/protocol/messenger/index.ts b/core/protocol/messenger/index.ts index 8b7245224..dd5519b7f 100644 --- a/core/protocol/messenger/index.ts +++ b/core/protocol/messenger/index.ts @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from "uuid"; import type { IProtocol } from "../index"; -export interface Message { +export interface Message { messageType: string; messageId: string; data: T; diff --git a/gui/src/components/modelSelection/ModelSelectionListbox.tsx b/gui/src/components/modelSelection/ModelSelectionListbox.tsx index 0fc8e7dee..aadf4319c 100644 --- a/gui/src/components/modelSelection/ModelSelectionListbox.tsx +++ b/gui/src/components/modelSelection/ModelSelectionListbox.tsx @@ -103,10 +103,8 @@ export const StyledListboxOption = styled(Listbox.Option)<{ `; interface ModelSelectionListboxProps { - selectedProvider: DisplayInfo | ProviderInfo | ModelPackage; - setSelectedProvider: Dispatch< - SetStateAction - >; + selectedProvider: DisplayInfo; + setSelectedProvider: (val: DisplayInfo) => void; topOptions?: DisplayInfo[]; otherOptions?: DisplayInfo[]; } diff --git a/gui/src/context/IdeMessenger.ts b/gui/src/context/IdeMessenger.ts index 76881e9a7..1955f4f4c 100644 --- a/gui/src/context/IdeMessenger.ts +++ b/gui/src/context/IdeMessenger.ts @@ -7,6 +7,10 @@ import { createContext } from "react"; import { v4 as uuidv4 } from "uuid"; import "vscode-webview"; import { isJetBrains } from "../util"; +import { + AsyncGeneratorYieldType, + ProtocolGeneratorYield, +} from "core/protocol/core"; interface vscode { postMessage(message: any): vscode; @@ -164,24 +168,28 @@ export class IdeMessenger implements IIdeMessenger { messageType: T, data: FromWebviewProtocol[T][0], cancelToken?: AbortSignal, - ): AsyncGenerator { - // ): FromWebviewProtocol[T][1] { + ): AsyncGenerator[], any> { + type GenYield = AsyncGeneratorYieldType; + // type GenReturn = AsyncGeneratorReturnType; + const messageId = uuidv4(); this.post(messageType, data, messageId); - const buffer: any[] = []; + const buffer: GenYield[] = []; let index = 0; let done = false; let returnVal = undefined; - const handler = (event: { data: Message }) => { + const handler = (event: { + data: Message>; + }) => { if (event.data.messageId === messageId) { const responseData = event.data.data; if (responseData.done) { window.removeEventListener("message", handler); done = true; - returnVal = responseData; + returnVal = responseData.content; } else { buffer.push(responseData.content); } @@ -239,8 +247,8 @@ export class IdeMessenger implements IIdeMessenger { next = await gen.next(); } - if (next.value.error) { - throw new Error(next.value.error); + if (!next.done) { + throw new Error(); } return { diff --git a/gui/src/forms/AddModelForm.tsx b/gui/src/forms/AddModelForm.tsx index 7b25e5997..e96349f1b 100644 --- a/gui/src/forms/AddModelForm.tsx +++ b/gui/src/forms/AddModelForm.tsx @@ -14,6 +14,7 @@ import { import { FREE_TRIAL_LIMIT_REQUESTS, hasPassedFTL } from "../util/freeTrial"; import { completionParamsInputs } from "../pages/AddNewModel/configs/completionParamsInputs"; import { setDefaultModel } from "../redux/slices/configSlice"; +import { DisplayInfo } from "../pages/AddNewModel/configs/models"; interface QuickModelSetupProps { onDone: () => void; @@ -149,7 +150,14 @@ function AddModelForm({ { + const match = [...popularProviders, ...otherProviders].find( + (provider) => provider.title === val.title, + ); + if (match) { + setSelectedProvider(match); + } + }} topOptions={popularProviders} otherOptions={otherProviders} /> @@ -186,7 +194,19 @@ function AddModelForm({ { + const options = + Object.entries(providers).find( + ([, provider]) => + provider?.title === selectedProvider.title, + )?.[1]?.packages ?? []; + const match = options.find( + (option) => option.title === val.title, + ); + if (match) { + setSelectedModel(match); + } + }} otherOptions={ Object.entries(providers).find( ([, provider]) => diff --git a/gui/src/hooks/useWebviewListener.ts b/gui/src/hooks/useWebviewListener.ts index 4bdd9f5dc..797df3e61 100644 --- a/gui/src/hooks/useWebviewListener.ts +++ b/gui/src/hooks/useWebviewListener.ts @@ -13,7 +13,7 @@ export function useWebviewListener( useEffect( () => { - let listener: (event: { data: Message }) => Promise; + let listener: (event: { data: Message}) => Promise; if (!skip) { listener = async (event) => { diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 0c8e5b696..99c61a71c 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -407,7 +407,7 @@ export function Chat() { return (
diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 04b4ed9f4..6fcb0a622 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -288,7 +288,9 @@ export const sessionSlice = createSlice({ message.role && (lastMessage.role !== message.role || // This is when a tool call comes after assistant text - (lastMessage.content !== "" && message.role === "assistant")) + (lastMessage.content !== "" && + message.role === "assistant" && + message.toolCalls?.length)) ) { // Create a new message const historyItem: ChatHistoryItemWithMessageId = { @@ -302,12 +304,12 @@ export const sessionSlice = createSlice({ const toolCalls = message.toolCalls?.[0]; if (toolCalls) { const [_, parsedArgs] = incrementalParseJson( - message.toolCalls[0].function.arguments, + message.toolCalls[0].function?.arguments ?? "{}", ); historyItem.toolCallState = { status: "generating", - toolCall: message.toolCalls[0], - toolCallId: message.toolCalls[0].id, + toolCall: message.toolCalls[0] as ToolCall, + toolCallId: message.toolCalls[0].id as ToolCall["id"], parsedArgs, }; } @@ -330,16 +332,28 @@ export const sessionSlice = createSlice({ if (lastMessage.toolCalls!.length <= i) { lastMessage.toolCalls!.push(toolCall); } else { - lastMessage.toolCalls[i].function.arguments += - toolCall.function.arguments; + if ( + toolCall?.function?.arguments && + lastMessage?.toolCalls?.[i]?.function?.arguments && + lastItem.toolCallState + ) { + lastMessage.toolCalls[i].function!.arguments += + toolCall.function.arguments; - const [_, parsedArgs] = incrementalParseJson( - lastMessage.toolCalls[i].function.arguments, - ); + const [_, parsedArgs] = incrementalParseJson( + lastMessage.toolCalls[i].function!.arguments!, + ); - lastItem.toolCallState.parsedArgs = parsedArgs; - lastItem.toolCallState.toolCall.function.arguments += - toolCall.function.arguments; + lastItem.toolCallState.parsedArgs = parsedArgs; + lastItem.toolCallState.toolCall.function.arguments += + toolCall.function.arguments; + } else { + console.error( + "Unexpected tool call format received - this message added during gui strict null checks", + message, + lastMessage, + ); + } } }); } diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index d37575ce2..d09a59fa2 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -55,15 +55,14 @@ export const streamNormalInput = createAsyncThunk< break; } - const updates = next.value as ChatMessage[]; + const updates = next.value; dispatch(streamUpdate(updates)); next = await gen.next(); } // Attach prompt log - let returnVal = next.value as PromptLog; - if (returnVal) { - dispatch(addPromptCompletionPair([returnVal])); + if (next.done) { + dispatch(addPromptCompletionPair([next.value])); } // If it's a tool call that is automatically accepted, we should call it