1
1
import { getDefaultStore } from 'jotai'
2
- import {
3
- Settings ,
4
- createMessage ,
5
- Message ,
6
- Session ,
7
- } from '../../shared/types'
2
+ import { Settings , createMessage , Message , Session , SessionsDump } from '../../shared/types'
8
3
import * as atoms from './atoms'
9
4
import * as promptFormat from '../packages/prompts'
10
5
import * as Sentry from '@sentry/react'
@@ -18,35 +13,41 @@ import { throttle } from 'lodash'
18
13
import { countWord } from '@/packages/word-count'
19
14
import { estimateTokensFromMessages } from '@/packages/token'
20
15
import * as settingActions from './settingActions'
16
+ import { scheduleSaveSessionsToBackend } from '@/packages/sync-sessions'
21
17
22
18
export function create ( newSession : Session ) {
23
19
const store = getDefaultStore ( )
24
- store . set ( atoms . sessionsAtom , ( sessions ) => [ ...sessions , newSession ] )
20
+ store . set ( atoms . sessionsAtom , ( { sessions } ) => ( {
21
+ ts : Date . now ( ) ,
22
+ sessions : [ ...sessions , newSession ] ,
23
+ } ) )
25
24
switchCurrentSession ( newSession . id )
26
25
}
27
26
28
27
export function modify ( update : Session ) {
29
28
const store = getDefaultStore ( )
30
- store . set ( atoms . sessionsAtom , ( sessions ) =>
31
- sessions . map ( ( s ) => {
29
+ store . set ( atoms . sessionsAtom , ( { sessions } ) => ( {
30
+ ts : Date . now ( ) ,
31
+ sessions : sessions . map ( ( s ) => {
32
32
if ( s . id === update . id ) {
33
33
return update
34
34
}
35
35
return s
36
- } )
37
- )
36
+ } ) ,
37
+ } ) )
38
38
}
39
39
40
40
export function modifyName ( sessionId : string , name : string ) {
41
41
const store = getDefaultStore ( )
42
- store . set ( atoms . sessionsAtom , ( sessions ) =>
43
- sessions . map ( ( s ) => {
42
+ store . set ( atoms . sessionsAtom , ( { sessions } ) => ( {
43
+ ts : Date . now ( ) ,
44
+ sessions : sessions . map ( ( s ) => {
44
45
if ( s . id === sessionId ) {
45
46
return { ...s , name, threadName : name }
46
47
}
47
48
return s
48
- } )
49
- )
49
+ } ) ,
50
+ } ) )
50
51
}
51
52
52
53
export function createEmpty ( type : 'chat' ) {
@@ -66,7 +67,10 @@ export function switchCurrentSession(sessionId: string) {
66
67
67
68
export function remove ( session : Session ) {
68
69
const store = getDefaultStore ( )
69
- store . set ( atoms . sessionsAtom , ( sessions ) => sessions . filter ( ( s ) => s . id !== session . id ) )
70
+ store . set ( atoms . sessionsAtom , ( { sessions } ) => ( {
71
+ ts : Date . now ( ) ,
72
+ sessions : sessions . filter ( ( s ) => s . id !== session . id ) ,
73
+ } ) )
70
74
}
71
75
72
76
export function clear ( sessionId : string ) {
@@ -84,29 +88,30 @@ export async function copy(source: Session) {
84
88
const store = getDefaultStore ( )
85
89
const newSession = { ...source }
86
90
newSession . id = uuidv4 ( )
87
- store . set ( atoms . sessionsAtom , ( sessions ) => {
91
+ store . set ( atoms . sessionsAtom , ( { sessions } ) => {
88
92
let originIndex = sessions . findIndex ( ( s ) => s . id === source . id )
89
93
if ( originIndex < 0 ) {
90
94
originIndex = 0
91
95
}
92
96
const newSessions = [ ...sessions ]
93
97
newSessions . splice ( originIndex + 1 , 0 , newSession )
94
- return newSessions
98
+ return { ts : Date . now ( ) , sessions : newSessions }
95
99
} )
96
100
}
97
101
98
102
export function getSession ( sessionId : string ) {
99
103
const store = getDefaultStore ( )
100
- const sessions = store . get ( atoms . sessionsAtom )
104
+ const { sessions } = store . get ( atoms . sessionsAtom )
101
105
return sessions . find ( ( s ) => s . id === sessionId )
102
106
}
103
107
104
108
export function insertMessage ( sessionId : string , msg : Message ) {
105
109
const store = getDefaultStore ( )
106
110
msg . wordCount = countWord ( msg . content )
107
111
msg . tokenCount = estimateTokensFromMessages ( [ msg ] )
108
- store . set ( atoms . sessionsAtom , ( sessions ) =>
109
- sessions . map ( ( s ) => {
112
+ store . set ( atoms . sessionsAtom , ( { sessions } ) => ( {
113
+ ts : Date . now ( ) ,
114
+ sessions : sessions . map ( ( s ) => {
110
115
if ( s . id === sessionId ) {
111
116
const newMessages = [ ...s . messages ]
112
117
newMessages . push ( msg )
@@ -116,8 +121,8 @@ export function insertMessage(sessionId: string, msg: Message) {
116
121
}
117
122
}
118
123
return s
119
- } )
120
- )
124
+ } ) ,
125
+ } ) )
121
126
}
122
127
123
128
export function modifyMessage ( sessionId : string , updated : Message , refreshCounting ?: boolean ) {
@@ -139,15 +144,16 @@ export function modifyMessage(sessionId: string, updated: Message, refreshCounti
139
144
return m
140
145
} )
141
146
}
142
- store . set ( atoms . sessionsAtom , ( sessions ) =>
143
- sessions . map ( ( s ) => {
147
+ store . set ( atoms . sessionsAtom , ( { sessions } ) => ( {
148
+ ts : Date . now ( ) ,
149
+ sessions : sessions . map ( ( s ) => {
144
150
if ( s . id !== sessionId ) {
145
151
return s
146
152
}
147
153
s . messages = handle ( s . messages )
148
154
return { ...s }
149
- } )
150
- )
155
+ } ) ,
156
+ } ) )
151
157
}
152
158
153
159
export async function submitNewUserMessage ( params : {
@@ -329,11 +335,27 @@ export function initEmptyChatSession(): Session {
329
335
}
330
336
}
331
337
332
- export function getSessions ( ) {
338
+ export function replaceSessionsFromBackend ( newDump : SessionsDump ) {
339
+ const store = getDefaultStore ( )
340
+ const currentDump = store . get ( atoms . sessionsAtom )
341
+
342
+ if ( newDump . ts > currentDump . ts ) {
343
+ store . set ( atoms . sessionsAtom , newDump )
344
+ } else if ( newDump . ts < currentDump . ts ) {
345
+ scheduleSaveSessionsToBackend ( currentDump )
346
+ }
347
+ }
348
+
349
+ export function getSessionsDump ( ) {
333
350
const store = getDefaultStore ( )
334
351
return store . get ( atoms . sessionsAtom )
335
352
}
336
353
354
+ export function getSessions ( ) {
355
+ const store = getDefaultStore ( )
356
+ return store . get ( atoms . sessionsAtom ) . sessions
357
+ }
358
+
337
359
export function getSortedSessions ( ) {
338
360
const store = getDefaultStore ( )
339
361
return store . get ( atoms . sortedSessionsAtom )
0 commit comments