@@ -28,11 +28,25 @@ export class WaveAIModel {
2828
2929 widgetAccess : jotai . PrimitiveAtom < boolean > = jotai . atom ( true ) ;
3030 droppedFiles : jotai . PrimitiveAtom < DroppedFile [ ] > = jotai . atom ( [ ] ) ;
31- chatId : jotai . PrimitiveAtom < string > = jotai . atom ( crypto . randomUUID ( ) ) ;
31+ chatId ! : jotai . PrimitiveAtom < string > ;
3232 errorMessage : jotai . PrimitiveAtom < string > = jotai . atom ( null ) as jotai . PrimitiveAtom < string > ;
3333 modelAtom ! : jotai . Atom < string > ;
3434
3535 private constructor ( ) {
36+ const tabId = globalStore . get ( atoms . staticTabId ) ;
37+ const chatIdMetaAtom = getTabMetaKeyAtom ( tabId , "waveai:chatid" ) ;
38+ let chatIdValue = globalStore . get ( chatIdMetaAtom ) ;
39+
40+ if ( chatIdValue == null ) {
41+ chatIdValue = crypto . randomUUID ( ) ;
42+ RpcApi . SetMetaCommand ( TabRpcClient , {
43+ oref : WOS . makeORef ( "tab" , tabId ) ,
44+ meta : { "waveai:chatid" : chatIdValue } ,
45+ } ) ;
46+ }
47+
48+ this . chatId = jotai . atom ( chatIdValue ) ;
49+
3650 this . modelAtom = jotai . atom ( ( get ) => {
3751 const tabId = get ( atoms . staticTabId ) ;
3852 const modelMetaAtom = getTabMetaKeyAtom ( tabId , "waveai:model" ) ;
@@ -98,7 +112,14 @@ export class WaveAIModel {
98112
99113 clearChat ( ) {
100114 this . clearFiles ( ) ;
101- globalStore . set ( this . chatId , crypto . randomUUID ( ) ) ;
115+ const newChatId = crypto . randomUUID ( ) ;
116+ globalStore . set ( this . chatId , newChatId ) ;
117+
118+ const tabId = globalStore . get ( atoms . staticTabId ) ;
119+ RpcApi . SetMetaCommand ( TabRpcClient , {
120+ oref : WOS . makeORef ( "tab" , tabId ) ,
121+ meta : { "waveai:chatid" : newChatId } ,
122+ } ) ;
102123 }
103124
104125 setError ( message : string ) {
@@ -129,7 +150,26 @@ export class WaveAIModel {
129150 meta : { "waveai:model" : model } ,
130151 } ) ;
131152 }
132- }
133153
134- // Export singleton instance for easy access
135- export const waveAIModel = WaveAIModel . getInstance ( ) ;
154+ async loadChat ( ) : Promise < UIMessage [ ] > {
155+ const chatId = globalStore . get ( this . chatId ) ;
156+ try {
157+ const chatData = await RpcApi . GetWaveAIChatCommand ( TabRpcClient , { chatid : chatId } ) ;
158+ return chatData ?. messages ?? [ ] ;
159+ } catch ( error ) {
160+ console . error ( "Failed to load chat:" , error ) ;
161+ this . setError ( "Failed to load chat. Starting new chat..." ) ;
162+
163+ const newChatId = crypto . randomUUID ( ) ;
164+ globalStore . set ( this . chatId , newChatId ) ;
165+
166+ const tabId = globalStore . get ( atoms . staticTabId ) ;
167+ RpcApi . SetMetaCommand ( TabRpcClient , {
168+ oref : WOS . makeORef ( "tab" , tabId ) ,
169+ meta : { "waveai:chatid" : newChatId } ,
170+ } ) ;
171+
172+ return [ ] ;
173+ }
174+ }
175+ }
0 commit comments