diff --git a/extensions/void/src/SidebarWebviewProvider.ts b/extensions/void/src/SidebarWebviewProvider.ts index e628586f..a750a21c 100644 --- a/extensions/void/src/SidebarWebviewProvider.ts +++ b/extensions/void/src/SidebarWebviewProvider.ts @@ -20,6 +20,7 @@ export class SidebarWebviewProvider implements vscode.WebviewViewProvider { private readonly _extensionUri: vscode.Uri private _webviewView?: vscode.WebviewView; // only used inside onDidChangeConfiguration + private _webviewDeps: string[] = []; constructor(context: vscode.ExtensionContext) { // const extensionPath = context.extensionPath // the directory where the extension is installed, might be useful later... was included in webviewProvider code @@ -30,8 +31,10 @@ export class SidebarWebviewProvider implements vscode.WebviewViewProvider { if (!temp_res) throw new Error("sidebar provider: resolver was undefined") this._res = temp_res + // if it affects one of the config items webview depends on, update the webview + // TODO should be able to move this entirely to React - make updateWebviewHTML mount once, and then send updates via postMessage from then on vscode.workspace.onDidChangeConfiguration(event => { - if (event.affectsConfiguration('void.ollama.endpoint')) { + if (this._webviewDeps.map(dep => event.affectsConfiguration(dep)).some(v => !!v)) { if (this._webviewView) { this.updateWebviewHTML(this._webviewView.webview); } @@ -39,12 +42,18 @@ export class SidebarWebviewProvider implements vscode.WebviewViewProvider { }); } + // this is updated private updateWebviewHTML(webview: vscode.Webview) { const allowed_urls = ['https://api.anthropic.com', 'https://api.openai.com', 'https://api.greptile.com']; + this._webviewDeps = [] + const ollamaEndpoint: string | undefined = vscode.workspace.getConfiguration('void.ollama').get('endpoint'); + this._webviewDeps.push('void.ollama.endpoint'); if (ollamaEndpoint) allowed_urls.push(ollamaEndpoint); + const openAICompatibleEndpoint: string | undefined = vscode.workspace.getConfiguration('void.openAICompatible').get('endpoint'); + this._webviewDeps.push('void.openAICompatible.endpoint'); if (openAICompatibleEndpoint) allowed_urls.push(openAICompatibleEndpoint); diff --git a/extensions/void/src/extension.ts b/extensions/void/src/extension.ts index 175bbf02..843f056d 100644 --- a/extensions/void/src/extension.ts +++ b/extensions/void/src/extension.ts @@ -143,13 +143,8 @@ export function activate(context: vscode.ExtensionContext) { await approvalCodeLensProvider.addNewApprovals(editor, suggestedEdits) } else if (m.type === 'getApiConfig') { - context.workspaceState.update('allThreads', {}) - const apiConfig = getApiConfig() - console.log('Api config:', apiConfig) - webview.postMessage({ type: 'apiConfig', apiConfig } satisfies WebviewMessage) - } else if (m.type === 'getAllThreads') { const threads: ChatThreads = context.workspaceState.get('allThreads') ?? {} diff --git a/extensions/void/src/sidebar/Sidebar.tsx b/extensions/void/src/sidebar/Sidebar.tsx index 67492f36..4092749f 100644 --- a/extensions/void/src/sidebar/Sidebar.tsx +++ b/extensions/void/src/sidebar/Sidebar.tsx @@ -9,7 +9,7 @@ import BlockCode from "./markdown/BlockCode"; import * as vscode from 'vscode' import { SelectedFiles } from "./components/SelectedFiles"; -import { useChat } from "./chatContext"; +import { useThreads } from "./threadsContext"; const filesStr = (fullFiles: File[]) => { @@ -69,7 +69,7 @@ const ChatBubble = ({ chatMessage }: { chatMessage: ChatMessage }) => { } const ThreadSelector = ({ onClose }: { onClose: () => void }) => { - const { allThreads, currentThread, switchToThread } = useChat() + const { allThreads, currentThread, switchToThread } = useThreads() return (
@@ -109,7 +109,7 @@ const ThreadSelector = ({ onClose }: { onClose: () => void }) => { const Sidebar = () => { - const { allThreads, currentThread, addMessageToHistory, startNewThread, } = useChat() + const { allThreads, currentThread, addMessageToHistory, startNewThread, } = useThreads() // state of current message const [selection, setSelection] = useState(null) // the code the user is selecting diff --git a/extensions/void/src/sidebar/index.tsx b/extensions/void/src/sidebar/index.tsx index 028ca2b3..7b01c5db 100644 --- a/extensions/void/src/sidebar/index.tsx +++ b/extensions/void/src/sidebar/index.tsx @@ -1,7 +1,7 @@ import * as React from "react" import * as ReactDOM from "react-dom/client" import Sidebar from "./Sidebar" -import { ChatProvider } from "./chatContext" +import { ThreadsProvider } from "./threadsContext" // mount the sidebar on the id="root" element if (typeof document === "undefined") { @@ -9,12 +9,12 @@ if (typeof document === "undefined") { } const rootElement = document.getElementById("root")! -console.log("root Element", rootElement) +console.log("Void root Element:", rootElement) const extension = ( - + - + ) const root = ReactDOM.createRoot(rootElement) root.render(extension) diff --git a/extensions/void/src/sidebar/chatContext.tsx b/extensions/void/src/sidebar/threadsContext.tsx similarity index 83% rename from extensions/void/src/sidebar/chatContext.tsx rename to extensions/void/src/sidebar/threadsContext.tsx index d6397392..5ade37f4 100644 --- a/extensions/void/src/sidebar/chatContext.tsx +++ b/extensions/void/src/sidebar/threadsContext.tsx @@ -3,7 +3,7 @@ import { ChatMessage, ChatThreads } from "../shared_types" import { awaitVSCodeResponse, getVSCodeAPI } from "./getVscodeApi" -type ChatContextValue = { +type ThreadsContextValue = { readonly allThreads: ChatThreads | null, readonly currentThread: ChatThreads[string] | null; addMessageToHistory: (message: ChatMessage) => void; @@ -11,7 +11,7 @@ type ChatContextValue = { startNewThread: () => void; } -const ChatContext = createContext({} as ChatContextValue) +const ThreadsContext = createContext(undefined as unknown as ThreadsContextValue) const createNewThread = () => ({ id: new Date().getTime().toString(), @@ -33,7 +33,7 @@ const useInstantState = (initVal: T) => { } -function ChatProvider({ children }: { children: ReactNode }) { +export function ThreadsProvider({ children }: { children: ReactNode }) { const [allThreads, setAllThreads] = useInstantState({}) const [currentThreadId, setCurrentThreadId] = useInstantState(null) @@ -41,12 +41,14 @@ function ChatProvider({ children }: { children: ReactNode }) { useEffect(() => { getVSCodeAPI().postMessage({ type: "getAllThreads" }) awaitVSCodeResponse('allThreads') - .then(response => { setAllThreads(response.threads) }) + .then(response => { + setAllThreads(response.threads) + }) }, [setAllThreads]) return ( - {children} - + ) } -function useChat(): ChatContextValue { - const context = useContext(ChatContext) +export function useThreads(): ThreadsContextValue { + const context = useContext(ThreadsContext) if (context === undefined) { - throw new Error("useChat must be used within a ChatProvider") + throw new Error("useThreads must be used within a ThreadsProvider") } return context } -export { ChatProvider, useChat }