@@ -10,22 +10,36 @@ import {
10
10
} from "./terminalHelpers.js" ;
11
11
import Anthropic from '@anthropic-ai/sdk' ;
12
12
import cliProgress from 'cli-progress' ;
13
- import { spawn } from 'child_process' ; ``
13
+ import { spawn } from 'child_process' ; ``
14
14
15
15
let throttleTime = 20 ;
16
16
let lastCallTime = 0 ;
17
17
18
18
export class conversation {
19
- constructor ( ) {
19
+ constructor ( id = null , targetFile = null ) {
20
20
this . messages = [ ] ;
21
+ this . title = '' ;
22
+ this . targetFile = targetFile ;
23
+
24
+ if ( id ) {
25
+ this . id = id ;
26
+ this . loadConversation ( id ) ;
27
+ } else {
28
+ //generate a unique id for the conversation based on the current time in the format
29
+ // of yyyy-mm-dd-hh-mm-ss
30
+ this . id = new Date ( ) . toISOString ( ) . replace ( / [ - : . ] / g, '' ) . replace ( 'T' , '_' ) . split ( '.' ) [ 0 ] ;
31
+ }
32
+
21
33
}
22
34
23
35
async addMessage ( role , content ) {
24
36
this . messages . push ( { role, content } ) ;
37
+ this . storeConversation ( ) ;
25
38
}
26
39
27
40
async addFileMessage ( role , filePath , description = '' ) {
28
41
this . messages . push ( { role, content : filePath , filePath, description } ) ;
42
+ this . storeConversation ( ) ;
29
43
}
30
44
31
45
async lastMessage ( ) {
@@ -42,9 +56,70 @@ export class conversation {
42
56
async getMessages ( ) {
43
57
return this . messages ;
44
58
}
59
+
60
+ async getConversation ( ) {
61
+ return {
62
+ messages : this . messages ,
63
+ title : this . title ,
64
+ id : this . id ,
65
+ targetFile : this . targetFile
66
+ }
67
+ }
68
+
69
+ async clearMessages ( ) {
70
+ this . messages = [ ] ;
71
+ this . storeConversation ( ) ;
72
+ }
73
+
74
+ async storeConversation ( id = this . id ) {
75
+ // write the conversation to a file
76
+ const conversationObject = {
77
+ messages : this . messages ,
78
+ title : this . title ,
79
+ id : this . id ,
80
+ targetFile : this . targetFile
81
+ } ;
82
+ const conversationJSON = JSON . stringify ( conversationObject ) ;
83
+ const filePath = `./.aiCoder/conversations/${ id } .json` ;
84
+ await writeFile ( filePath , conversationJSON ) ;
85
+
86
+ }
87
+
88
+ async loadConversation ( id = this . id ) {
89
+ // load the conversation from a file
90
+ const filePath = `./.aiCoder/conversations/${ id } .json` ;
91
+
92
+ const conversationJSON = await readFile ( filePath ) ;
93
+ const conversationObject = JSON . parse ( conversationJSON ) ;
94
+ this . messages = conversationObject . messages ;
95
+ this . title = conversationObject . title ;
96
+ this . id = conversationObject . id ;
97
+ this . targetFile = conversationObject . targetFile ;
98
+ }
45
99
}
46
100
47
101
102
+
103
+ export async function listConversations ( ) {
104
+ // load all the conversations from the conversation folder
105
+ const conversationFolder = './.aiCoder/conversations' ;
106
+ if ( ! fs . existsSync
107
+ ( conversationFolder ) ) {
108
+ fs . mkdirSync ( conversationFolder ) ;
109
+ }
110
+
111
+ const conversationFiles = fs . readdirSync ( conversationFolder ) ;
112
+ for ( const file of conversationFiles ) {
113
+ const conversationId = file . split ( '.' ) [ 0 ] ;
114
+ const newConversation = new conversation ( conversationId ) ;
115
+ this . conversations . push ( newConversation ) ;
116
+ }
117
+
118
+ return this . conversations ;
119
+ }
120
+
121
+
122
+
48
123
async function throttle ( ) {
49
124
// check if the current time is greater than the last call time + throttle time and if not wait until it is
50
125
// if it needs use the printAndPause function to show a message to the user and wait the remaining time
@@ -185,14 +260,14 @@ export async function selectModel(overwrite = false) {
185
260
const llmModelFileName = `./.aiCoder/${ await selectAIservice ( ) } -model.txt` ;
186
261
if ( readFile ( llmModelFileName ) && ! overwrite ) {
187
262
return readFile ( llmModelFileName ) ;
188
- }
263
+ }
189
264
}
190
265
191
266
192
267
export async function selectAIservice ( overwrite = false ) {
193
268
if ( fs . existsSync ( './.aiCoder/ai-service.txt' ) && ! overwrite ) {
194
269
return fs . readFileSync ( './.aiCoder/ai-service.txt' , 'utf8' ) ;
195
- }
270
+ }
196
271
}
197
272
198
273
// ollama related functions -----------------------------------------------------------------------------------------------
0 commit comments