1
1
mod schema;
2
2
3
3
use crate :: assistant:: { self , Assistant , Reply , Token } ;
4
- use crate :: chat:: schema:: Schema ;
5
4
use crate :: model;
6
5
use crate :: plan:: { self , Plan } ;
7
6
use crate :: Error ;
@@ -43,27 +42,11 @@ impl Chat {
43
42
}
44
43
45
44
pub async fn fetch ( id : Id ) -> Result < Self , Error > {
46
- let bytes = fs:: read ( Self :: path ( & id) . await ?) . await ?;
45
+ let json = fs:: read_to_string ( Self :: path ( & id) . await ?) . await ?;
47
46
48
47
let _ = LastOpened :: update ( id) . await ;
49
48
50
- task:: spawn_blocking ( move || {
51
- let schema: Schema = serde_json:: from_slice ( & bytes) ?;
52
-
53
- let history = schema
54
- . history
55
- . into_iter ( )
56
- . map ( schema:: Message :: to_data)
57
- . collect ( ) ;
58
-
59
- Ok ( Self {
60
- id,
61
- file : schema. file ,
62
- title : schema. title ,
63
- history,
64
- } )
65
- } )
66
- . await ?
49
+ task:: spawn_blocking ( move || schema:: decode ( & json) ) . await ?
67
50
}
68
51
69
52
pub async fn fetch_last_opened ( ) -> Result < Self , Error > {
@@ -78,7 +61,14 @@ impl Chat {
78
61
history : Vec < Item > ,
79
62
) -> Result < Self , Error > {
80
63
let id = Id ( Uuid :: new_v4 ( ) ) ;
81
- let chat = Self :: save ( id, file, title, history) . await ?;
64
+ let chat = Self {
65
+ id,
66
+ file,
67
+ title,
68
+ history,
69
+ }
70
+ . save ( )
71
+ . await ?;
82
72
83
73
LastOpened :: update ( chat. id ) . await ?;
84
74
@@ -92,48 +82,24 @@ impl Chat {
92
82
Ok ( chat)
93
83
}
94
84
95
- pub async fn save (
96
- id : Id ,
97
- file : model:: File ,
98
- title : Option < String > ,
99
- history : Vec < Item > ,
100
- ) -> Result < Self , Error > {
101
- if let Ok ( current) = Self :: fetch ( id) . await {
102
- if current. title != title {
85
+ pub async fn save ( self ) -> Result < Self , Error > {
86
+ if let Ok ( current) = Self :: fetch ( self . id ) . await {
87
+ if current. title != self . title {
103
88
let mut list = List :: fetch ( ) . await ?;
104
89
105
- if let Some ( entry) = list. entries . iter_mut ( ) . find ( |entry| entry. id == id) {
106
- entry. title = title. clone ( ) ;
90
+ if let Some ( entry) = list. entries . iter_mut ( ) . find ( |entry| entry. id == self . id ) {
91
+ entry. title = self . title . clone ( ) ;
107
92
}
108
93
109
94
list. save ( ) . await ?;
110
95
}
111
96
}
112
97
113
- let ( bytes, chat, history) = task:: spawn_blocking ( move || {
114
- let chat = Schema {
115
- id,
116
- file,
117
- title,
118
- history : history
119
- . iter ( )
120
- . cloned ( )
121
- . map ( schema:: Message :: from_data)
122
- . collect ( ) ,
123
- } ;
124
-
125
- ( serde_json:: to_vec_pretty ( & chat) , chat, history)
126
- } )
127
- . await ?;
98
+ let ( bytes, chat) = task:: spawn_blocking ( move || ( schema:: encode ( & self ) , self ) ) . await ?;
128
99
129
100
fs:: write ( Self :: path ( & chat. id ) . await ?, bytes?) . await ?;
130
101
131
- Ok ( Self {
132
- id : chat. id ,
133
- file : chat. file ,
134
- title : chat. title ,
135
- history,
136
- } )
102
+ Ok ( chat)
137
103
}
138
104
139
105
pub async fn delete ( id : Id ) -> Result < ( ) , Error > {
0 commit comments