4
4
*/
5
5
6
6
/*
7
- chat Editor Actions
7
+ Chat Editor Actions
8
8
*/
9
9
10
10
import {
11
11
Actions as CodeEditorActions ,
12
12
CodeEditorState ,
13
13
} from "../code-editor/actions" ;
14
14
import { FrameTree } from "../frame-tree/types" ;
15
- import { TaskActions } from "@cocalc/frontend/editors/task-editor/actions" ;
16
- import { TaskStore } from "@cocalc/frontend/editors/task-editor/store" ;
17
- import { redux_name } from "../../app-framework" ;
18
- import { aux_file , cmp } from "@cocalc/util/misc" ;
19
- import { Map } from "immutable" ;
15
+ import { ChatActions } from "@cocalc/frontend/chat/actions" ;
16
+ import { ChatStore } from "@cocalc/frontend/chat/store" ;
17
+ import { redux_name } from "@cocalc/frontend/app-framework" ;
18
+ import { aux_file } from "@cocalc/util/misc" ;
20
19
21
- interface TaskEditorState extends CodeEditorState {
20
+ interface ChatEditorState extends CodeEditorState {
22
21
// nothing yet
23
22
}
24
23
25
- export class Actions extends CodeEditorActions < TaskEditorState > {
24
+ export class Actions extends CodeEditorActions < ChatEditorState > {
26
25
protected doctype : string = "syncdb" ;
27
- protected primary_keys : string [ ] = [ "task_id" ] ;
28
- protected string_cols : string [ ] = [ "desc" ] ;
29
- protected searchEmbeddings = {
30
- primaryKey : "task_id" ,
31
- textColumn : "desc" ,
32
- metaColumns : [ "due_date" , "done" ] ,
33
- } ;
34
- taskActions : { [ frameId : string ] : TaskActions } = { } ;
35
- taskStore : TaskStore ;
26
+ protected primary_keys = [ "date" , "sender_id" , "event" ] ;
27
+ // used only for drafts, since store lots of versions as user types:
28
+ protected string_cols = [ "input" ] ;
29
+ chatActions : { [ frameId : string ] : ChatActions } = { } ;
30
+ chatStore : ChatStore ;
36
31
auxPath : string ;
37
32
38
33
_init2 ( ) : void {
39
34
this . auxPath = aux_file ( this . path , "tasks" ) ;
40
- this . taskStore = this . redux . createStore (
35
+ this . chatStore = this . redux . createStore (
41
36
redux_name ( this . project_id , this . auxPath ) ,
42
- TaskStore
37
+ ChatStore ,
43
38
) ;
44
- const syncdb = this . _syncstring ;
45
- syncdb . on ( "change" , this . syncdbChange ) ;
46
- syncdb . once ( "change" , this . ensurePositionsAreUnique ) ;
47
- }
48
-
49
- private syncdbChange ( changes ) {
50
- const syncdb = this . _syncstring ;
51
- const store = this . taskStore ;
52
- if ( syncdb == null || store == null ) {
53
- // may happen during close
54
- return ;
55
- }
56
- let tasks = store . get ( "tasks" ) ?? Map ( ) ;
57
- changes . forEach ( ( x ) => {
58
- const task_id = x . get ( "task_id" ) ;
59
- const t = syncdb . get_one ( x ) ;
60
- if ( t == null ) {
61
- // deleted
62
- tasks = tasks . delete ( task_id ) ;
63
- } else {
64
- // changed
65
- tasks = tasks . set ( task_id , t as any ) ;
66
- }
67
- } ) ;
68
-
69
- store . setState ( { tasks } ) ;
70
- for ( const id in this . taskActions ) {
71
- this . taskActions [ id ] . _update_visible ( ) ;
72
- }
73
- }
74
-
75
- private ensurePositionsAreUnique ( ) {
76
- let tasks = this . taskStore . get ( "tasks" ) ;
77
- if ( tasks == null ) {
78
- return ;
79
- }
80
- // iterate through tasks adding their (string) positions to a
81
- // "set" (using a map)
82
- const s = { } ;
83
- let unique = true ;
84
- tasks . forEach ( ( task , id ) => {
85
- if ( tasks == null ) return ; // won't happpen, but TS doesn't know that.
86
- let pos = task . get ( "position" ) ;
87
- if ( pos == null ) {
88
- // no position set at all -- just arbitrarily set it to 0; it'll get
89
- // fixed below, if this conflicts.
90
- pos = 0 ;
91
- tasks = tasks . set ( id , task . set ( "position" , 0 ) ) ;
92
- }
93
- if ( s [ pos ] ) {
94
- // already got this position -- so they can't be unique
95
- unique = false ;
96
- return false ;
97
- }
98
- s [ pos ] = true ;
99
- } ) ;
100
- if ( unique ) {
101
- // positions turned out to all be unique - done
102
- return ;
103
- }
104
- // positions are NOT unique - this could happen, e.g., due to merging
105
- // offline changes. We fix this by simply spreading them all out to be
106
- // 0 to n, arbitrarily breaking ties.
107
- const v : [ number , string ] [ ] = [ ] ;
108
- tasks . forEach ( ( task , id ) => {
109
- v . push ( [ task . get ( "position" ) ?? 0 , id ] ) ;
110
- } ) ;
111
- v . sort ( ( a , b ) => cmp ( a [ 0 ] , b [ 0 ] ) ) ;
112
- let position = 0 ;
113
- const actions = this . getTaskActions ( ) ;
114
- if ( actions == null ) return ;
115
- for ( let x of v ) {
116
- actions . set_task ( x [ 1 ] , { position } ) ;
117
- position += 1 ;
118
- }
119
39
}
120
40
121
- getTaskActions ( frameId ?) : TaskActions | undefined {
41
+ getChatActions ( frameId ?) : ChatActions | undefined {
122
42
if ( frameId == null ) {
123
- for ( const actions of Object . values ( this . taskActions ) ) {
43
+ for ( const actions of Object . values ( this . chatActions ) ) {
124
44
return actions ;
125
45
}
126
46
return undefined ;
127
47
}
128
- if ( this . taskActions [ frameId ] != null ) {
129
- return this . taskActions [ frameId ] ;
48
+ if ( this . chatActions [ frameId ] != null ) {
49
+ return this . chatActions [ frameId ] ;
130
50
}
51
+ const syncdb = this . _syncstring ;
131
52
const auxPath = this . auxPath + frameId ;
132
53
const reduxName = redux_name ( this . project_id , auxPath ) ;
133
- const actions = this . redux . createActions ( reduxName , TaskActions ) ;
134
- actions . _init (
135
- this . project_id ,
136
- this . auxPath ,
137
- this . _syncstring ,
138
- this . taskStore ,
139
- this . path
140
- ) ;
141
- actions . _init_frame ( frameId , this ) ;
142
- this . taskActions [ frameId ] = actions ;
54
+ const actions = this . redux . createActions ( reduxName , ChatActions ) ;
55
+
56
+ const init = ( ) => {
57
+ actions . set_syncdb ( syncdb , this . chatStore ) ;
58
+ actions . init_from_syncdb ( ) ;
59
+ syncdb . on ( "change" , actions . syncdb_change . bind ( actions ) ) ;
60
+ syncdb . on ( "has-uncommitted-changes" , ( val ) =>
61
+ actions . setState ( { has_uncommitted_changes : val } ) ,
62
+ ) ;
63
+ syncdb . on ( "has-unsaved-changes" , ( val ) =>
64
+ actions . setState ( { has_unsaved_changes : val } ) ,
65
+ ) ;
66
+ } ;
67
+ if ( syncdb . get_state ( ) != "ready" ) {
68
+ syncdb . once ( "ready" , init ) ;
69
+ } else {
70
+ init ( ) ;
71
+ }
72
+
73
+ this . chatActions [ frameId ] = actions ;
143
74
return actions ;
144
75
}
145
76
146
77
undo ( ) {
147
- this . getTaskActions ( ) ?. undo ( ) ;
78
+ this . getChatActions ( ) ?. undo ( ) ;
148
79
}
149
80
redo ( ) {
150
- this . getTaskActions ( ) ?. redo ( ) ;
81
+ this . getChatActions ( ) ?. redo ( ) ;
151
82
}
152
83
153
84
help ( ) {
154
- this . getTaskActions ( ) ?. help ( ) ;
85
+ this . getChatActions ( ) ?. help ( ) ;
155
86
}
156
87
157
88
close_frame ( frameId : string ) : void {
158
89
super . close_frame ( frameId ) ; // actually closes the frame itself
159
- // now clean up if it is a task frame:
160
-
161
- if ( this . taskActions [ frameId ] != null ) {
162
- this . closeTaskFrame ( frameId ) ;
90
+ // now clean up if it is a chat frame:
91
+ if ( this . chatActions [ frameId ] != null ) {
92
+ this . closeChatFrame ( frameId ) ;
163
93
}
164
94
}
165
95
166
- closeTaskFrame ( frameId : string ) : void {
167
- const actions = this . taskActions [ frameId ] ;
96
+ closeChatFrame ( frameId : string ) : void {
97
+ const actions = this . chatActions [ frameId ] ;
168
98
if ( actions == null ) {
169
99
return ;
170
100
}
171
- delete this . taskActions [ frameId ] ;
101
+ delete this . chatActions [ frameId ] ;
172
102
const name = actions . name ;
173
103
this . redux . removeActions ( name ) ;
174
104
actions . close ( ) ;
@@ -178,68 +108,22 @@ export class Actions extends CodeEditorActions<TaskEditorState> {
178
108
if ( this . _state == "closed" ) {
179
109
return ;
180
110
}
181
- for ( const frameId in this . taskActions ) {
182
- this . closeTaskFrame ( frameId ) ;
111
+ for ( const frameId in this . chatActions ) {
112
+ this . closeChatFrame ( frameId ) ;
183
113
}
184
- this . redux . removeStore ( this . taskStore . name ) ;
114
+ this . redux . removeStore ( this . chatStore . name ) ;
185
115
super . close ( ) ;
186
116
}
187
117
188
118
_raw_default_frame_tree ( ) : FrameTree {
189
- return { type : "tasks " } ;
119
+ return { type : "chatroom " } ;
190
120
}
191
121
192
122
async export_to_markdown ( ) : Promise < void > {
193
123
try {
194
- await this . getTaskActions ( ) ?. export_to_markdown ( ) ;
124
+ await this . getChatActions ( ) ?. export_to_markdown ( ) ;
195
125
} catch ( error ) {
196
126
this . set_error ( `${ error } ` ) ;
197
127
}
198
128
}
199
-
200
- public focus ( id ?: string ) : void {
201
- if ( id === undefined ) {
202
- id = this . _get_active_id ( ) ;
203
- }
204
- if ( this . _get_frame_type ( id ) == "tasks" ) {
205
- this . getTaskActions ( id ) ?. show ( ) ;
206
- return ;
207
- }
208
- super . focus ( id ) ;
209
- }
210
-
211
- public blur ( id ?: string ) : void {
212
- if ( id === undefined ) {
213
- id = this . _get_active_id ( ) ;
214
- }
215
- if ( this . _get_frame_type ( id ) == "tasks" ) {
216
- this . getTaskActions ( id ) ?. hide ( ) ;
217
- }
218
- }
219
-
220
- protected languageModelGetText ( frameId : string , scope ) : string {
221
- if ( this . _get_frame_type ( frameId ) == "tasks" ) {
222
- const node = this . _get_frame_node ( frameId ) ;
223
- return (
224
- this . getTaskActions ( frameId ) ?. chatgptGetText (
225
- scope ,
226
- node ?. get ( "data-current_task_id" )
227
- ) ?? ""
228
- ) ;
229
- }
230
- return super . languageModelGetText ( frameId , scope ) ;
231
- }
232
-
233
- languageModelGetScopes ( ) {
234
- return new Set < "cell" > ( [ "cell" ] ) ;
235
- }
236
-
237
- languageModelGetLanguage ( ) {
238
- return "md" ;
239
- }
240
-
241
- // async updateEmbeddings(): Promise<number> {
242
- // if (this._syncstring == null) return 0;
243
- // return (await this.getTaskActions()?.updateEmbeddings()) ?? 0;
244
- // }
245
129
}
0 commit comments