-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathChatView.tsx
More file actions
162 lines (145 loc) · 5.03 KB
/
ChatView.tsx
File metadata and controls
162 lines (145 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ItemView, TFile, WorkspaceLeaf } from 'obsidian'
import React from 'react'
import { Root, createRoot } from 'react-dom/client'
import Chat, { ChatProps, ChatRef } from './components/chat-view/Chat'
import { CHAT_VIEW_TYPE } from './constants'
import { AppProvider } from './contexts/app-context'
import { ChatViewProvider } from './contexts/chat-view-context'
import { DarkModeProvider } from './contexts/dark-mode-context'
import { DatabaseProvider } from './contexts/database-context'
import { DialogContainerProvider } from './contexts/dialog-container-context'
import { McpProvider } from './contexts/mcp-context'
import { PluginProvider } from './contexts/plugin-context'
import { RAGProvider } from './contexts/rag-context'
import { SettingsProvider } from './contexts/settings-context'
import SmartComposerPlugin from './main'
import { MentionableBlockData } from './types/mentionable'
export class ChatView extends ItemView {
private root: Root | null = null
private initialChatProps?: ChatProps
private chatRef: React.RefObject<ChatRef> = React.createRef()
constructor(
leaf: WorkspaceLeaf,
private plugin: SmartComposerPlugin,
) {
super(leaf)
this.initialChatProps = plugin.initialChatProps
// Store reference to this view in the plugin
plugin.chatView = this
}
getViewType() {
return CHAT_VIEW_TYPE
}
getIcon() {
return 'wand-sparkles'
}
getDisplayText() {
return 'Smart composer chat'
}
async onOpen() {
await this.render()
// Consume chatProps
this.initialChatProps = undefined
}
async onClose() {
if (this.plugin.chatView === this) {
this.plugin.chatView = null
}
this.root?.unmount()
}
async render() {
if (!this.root) {
this.root = createRoot(this.containerEl.children[1])
}
const queryClient = new QueryClient({
defaultOptions: {
queries: {
gcTime: 0, // Immediately garbage collect queries. It prevents memory leak on ChatView close.
},
mutations: {
gcTime: 0, // Immediately garbage collect mutations. It prevents memory leak on ChatView close.
},
},
})
this.root.render(
<ChatViewProvider chatView={this}>
<PluginProvider plugin={this.plugin}>
<AppProvider app={this.app}>
<SettingsProvider
settings={this.plugin.settings}
setSettings={(newSettings) =>
this.plugin.setSettings(newSettings)
}
addSettingsChangeListener={(listener) =>
this.plugin.addSettingsChangeListener(listener)
}
>
<DarkModeProvider>
<DatabaseProvider
getDatabaseManager={() => this.plugin.getDbManager()}
>
<RAGProvider getRAGEngine={() => this.plugin.getRAGEngine()}>
<McpProvider
getMcpManager={() => this.plugin.getMcpManager()}
>
<QueryClientProvider client={queryClient}>
<React.StrictMode>
<DialogContainerProvider
container={
this.containerEl.children[1] as HTMLElement
}
>
<Chat
ref={this.chatRef}
{...this.initialChatProps}
/>
</DialogContainerProvider>
</React.StrictMode>
</QueryClientProvider>
</McpProvider>
</RAGProvider>
</DatabaseProvider>
</DarkModeProvider>
</SettingsProvider>
</AppProvider>
</PluginProvider>
</ChatViewProvider>,
)
}
openNewChat(selectedBlock?: MentionableBlockData) {
this.chatRef.current?.openNewChat(selectedBlock)
}
addSelectionToChat(selectedBlock: MentionableBlockData) {
this.chatRef.current?.addSelectionToChat(selectedBlock)
}
focusMessage() {
this.chatRef.current?.focusMessage()
}
/**
* Loads a specific conversation by ID
*/
loadConversation(conversationId: string) {
return this.chatRef.current?.loadConversation(conversationId)
}
/**
* Creates a new chat with block data and automatically submits it
* @param blockData The text block to add to the chat
* @returns Promise that resolves to the conversation ID
*/
async createAndSubmitChatFromBlock(blockData: {
file: TFile
text: string
startLine: number
endLine: number
}): Promise<string | undefined> {
// Convert to MentionableBlockData format
const mentionableBlock: MentionableBlockData = {
file: blockData.file,
content: blockData.text,
startLine: blockData.startLine,
endLine: blockData.endLine,
}
return this.chatRef.current?.createAndSubmitChat(mentionableBlock)
}
}