mirror of
https://github.com/voideditor/void
synced 2026-05-24 09:58:23 +00:00
add chat context
This commit is contained in:
parent
22d32aca3a
commit
3a98d1edb1
2 changed files with 92 additions and 11 deletions
77
extensions/void/src/sidebar/context.tsx
Normal file
77
extensions/void/src/sidebar/context.tsx
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import React, {
|
||||
ReactNode,
|
||||
createContext,
|
||||
useContext,
|
||||
useEffect,
|
||||
useState,
|
||||
} from "react"
|
||||
import * as vscode from "vscode"
|
||||
import { ChatMessage, ChatThread, Selection } from "../shared_types"
|
||||
import { getVSCodeAPI } from "./getVscodeApi"
|
||||
|
||||
interface IChatProviderProps {
|
||||
chatMessageHistory: ChatMessage[]
|
||||
addMessageToHistory: (message: ChatMessage) => void
|
||||
setPreviousThreads: (threads: any) => void
|
||||
}
|
||||
|
||||
const defaults = {
|
||||
chatMessageHistory: [],
|
||||
addMessageToHistory: () => {},
|
||||
setPreviousThreads: () => {},
|
||||
thread: {
|
||||
id: "",
|
||||
createdAt: "",
|
||||
messages: [],
|
||||
},
|
||||
}
|
||||
|
||||
const ChatContext = createContext<IChatProviderProps>(defaults)
|
||||
|
||||
function ChatProvider({ children }: { children: ReactNode }) {
|
||||
const [previousThreads, setPreviousThreads] = useState<ChatThread[]>([])
|
||||
const [thread, setThread] = useState<ChatThread>(defaults.thread)
|
||||
|
||||
useEffect(() => {
|
||||
getVSCodeAPI().postMessage({ type: "getThreadHistory" })
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (thread.messages.length) {
|
||||
getVSCodeAPI().postMessage({ type: "updateThread", thread })
|
||||
}
|
||||
}, [thread])
|
||||
|
||||
const addMessageToHistory = (message: ChatMessage) => {
|
||||
setThread((prev) => ({
|
||||
...prev,
|
||||
...(!thread.id && {
|
||||
id: new Date().getTime().toString(),
|
||||
createdAt: new Date().toISOString(),
|
||||
}),
|
||||
messages: [...prev.messages, message],
|
||||
}))
|
||||
}
|
||||
|
||||
return (
|
||||
<ChatContext.Provider
|
||||
value={{
|
||||
chatMessageHistory: thread.messages,
|
||||
addMessageToHistory,
|
||||
setPreviousThreads,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</ChatContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
function useChat(): IChatProviderProps {
|
||||
const context = useContext<IChatProviderProps>(ChatContext)
|
||||
if (context === undefined) {
|
||||
throw new Error("useChat must be used within a ChatProvider")
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
export { ChatProvider, useChat }
|
||||
|
|
@ -1,16 +1,20 @@
|
|||
import * as React from 'react'
|
||||
import * as ReactDOM from 'react-dom/client'
|
||||
import Sidebar from './Sidebar'
|
||||
import * as React from "react"
|
||||
import * as ReactDOM from "react-dom/client"
|
||||
import Sidebar from "./Sidebar"
|
||||
import { ChatProvider } from "./context"
|
||||
|
||||
// mount the sidebar on the id="root" element
|
||||
if (typeof document === 'undefined') {
|
||||
console.log('index.tsx error: document was undefined')
|
||||
if (typeof document === "undefined") {
|
||||
console.log("index.tsx error: document was undefined")
|
||||
}
|
||||
|
||||
const rootElement = document.getElementById('root')!
|
||||
console.log('root Element', rootElement)
|
||||
const rootElement = document.getElementById("root")!
|
||||
console.log("root Element", rootElement)
|
||||
|
||||
const extension = (
|
||||
<ChatProvider>
|
||||
<Sidebar />
|
||||
</ChatProvider>
|
||||
)
|
||||
const root = ReactDOM.createRoot(rootElement)
|
||||
root.render(<Sidebar />)
|
||||
|
||||
|
||||
|
||||
root.render(extension)
|
||||
|
|
|
|||
Loading…
Reference in a new issue