@@ -13,6 +13,7 @@ import {
1313 setMessages ,
1414 duplicateChat ,
1515 createChatFromMessages ,
16+ type IChatMetadata ,
1617} from './db' ;
1718
1819export interface ChatHistoryItem {
@@ -21,6 +22,7 @@ export interface ChatHistoryItem {
2122 description ?: string ;
2223 messages : Message [ ] ;
2324 timestamp : string ;
25+ metadata ?: IChatMetadata ;
2426}
2527
2628const persistenceEnabled = ! import . meta. env . VITE_DISABLE_PERSISTENCE ;
@@ -29,7 +31,7 @@ export const db = persistenceEnabled ? await openDatabase() : undefined;
2931
3032export const chatId = atom < string | undefined > ( undefined ) ;
3133export const description = atom < string | undefined > ( undefined ) ;
32-
34+ export const chatMetadata = atom < IChatMetadata | undefined > ( undefined ) ;
3335export function useChatHistory ( ) {
3436 const navigate = useNavigate ( ) ;
3537 const { id : mixedId } = useLoaderData < { id ?: string } > ( ) ;
@@ -65,6 +67,7 @@ export function useChatHistory() {
6567 setUrlId ( storedMessages . urlId ) ;
6668 description . set ( storedMessages . description ) ;
6769 chatId . set ( storedMessages . id ) ;
70+ chatMetadata . set ( storedMessages . metadata ) ;
6871 } else {
6972 navigate ( '/' , { replace : true } ) ;
7073 }
@@ -81,6 +84,21 @@ export function useChatHistory() {
8184 return {
8285 ready : ! mixedId || ready ,
8386 initialMessages,
87+ updateChatMestaData : async ( metadata : IChatMetadata ) => {
88+ const id = chatId . get ( ) ;
89+
90+ if ( ! db || ! id ) {
91+ return ;
92+ }
93+
94+ try {
95+ await setMessages ( db , id , initialMessages , urlId , description . get ( ) , undefined , metadata ) ;
96+ chatMetadata . set ( metadata ) ;
97+ } catch ( error ) {
98+ toast . error ( 'Failed to update chat metadata' ) ;
99+ console . error ( error ) ;
100+ }
101+ } ,
84102 storeMessageHistory : async ( messages : Message [ ] ) => {
85103 if ( ! db || messages . length === 0 ) {
86104 return ;
@@ -109,7 +127,7 @@ export function useChatHistory() {
109127 }
110128 }
111129
112- await setMessages ( db , chatId . get ( ) as string , messages , urlId , description . get ( ) ) ;
130+ await setMessages ( db , chatId . get ( ) as string , messages , urlId , description . get ( ) , undefined , chatMetadata . get ( ) ) ;
113131 } ,
114132 duplicateCurrentChat : async ( listItemId : string ) => {
115133 if ( ! db || ( ! mixedId && ! listItemId ) ) {
@@ -125,13 +143,13 @@ export function useChatHistory() {
125143 console . log ( error ) ;
126144 }
127145 } ,
128- importChat : async ( description : string , messages : Message [ ] ) => {
146+ importChat : async ( description : string , messages : Message [ ] , metadata ?: IChatMetadata ) => {
129147 if ( ! db ) {
130148 return ;
131149 }
132150
133151 try {
134- const newId = await createChatFromMessages ( db , description , messages ) ;
152+ const newId = await createChatFromMessages ( db , description , messages , metadata ) ;
135153 window . location . href = `/chat/${ newId } ` ;
136154 toast . success ( 'Chat imported successfully' ) ;
137155 } catch ( error ) {
0 commit comments