@@ -50,7 +50,7 @@ function App() {
50
50
51
51
const createThreadAndRun = async ( ) : Promise < string | null > => {
52
52
if ( ! apiKey || ! assistantId ) return null ;
53
-
53
+
54
54
setIsLoading ( true ) ; // Start loading
55
55
try {
56
56
const response = await axios . post (
@@ -69,16 +69,22 @@ function App() {
69
69
} ,
70
70
}
71
71
) ;
72
-
72
+
73
73
console . log ( "Response from thread creation:" , response . data ) ; // Debugging log
74
-
75
74
const newThreadId = response . data . thread_id ;
76
75
const newRunId = response . data . id ;
77
-
76
+
78
77
if ( newThreadId && newRunId ) {
79
- setThreadId ( newThreadId ) ;
80
- setRunId ( newRunId ) ;
81
- checkRunStatus ( newRunId ) ; // Check the status without awaiting it
78
+ // Utiliser la valeur immédiatement dans les logs ou autres fonctions
79
+ console . log ( "new threadId:" , newThreadId ) ;
80
+ console . log ( "new runId:" , newRunId ) ;
81
+
82
+ setThreadId ( newThreadId ) ; // Ceci est toujours asynchrone
83
+ setRunId ( newRunId ) ; // Ceci est toujours asynchrone
84
+
85
+ // Passer directement newThreadId ici, au lieu de threadId qui n'est pas encore à jour
86
+ checkRunStatus ( newRunId , newThreadId ) ;
87
+
82
88
return newThreadId ;
83
89
} else {
84
90
console . error ( "Thread ID or Run ID is missing in the response" ) ;
@@ -90,52 +96,62 @@ function App() {
90
96
error
91
97
) ;
92
98
return null ;
99
+ } finally {
100
+ setIsLoading ( false ) ; // Stop loading
93
101
}
94
102
} ;
103
+
95
104
96
105
const handleFilesUpdate = (
97
106
files : { id : string ; created_at : number ; vector_store_id : string } [ ]
98
107
) => {
99
108
console . log ( "Files updated:" , files ) ;
100
109
} ;
101
110
102
- const checkRunStatus = async ( runId : string ) => {
103
- if ( ! apiKey || ! threadId ) return ;
104
-
111
+ const checkRunStatus = async ( runId : string , passedThreadId ?: string ) => {
112
+ const currentThreadId = passedThreadId || threadId ; // Si threadId n'est pas encore mis à jour, utilisez passedThreadId
113
+ console . log ( "Checking run status..." ) ;
114
+ console . log ( "threadId:" , currentThreadId ) ; // Cela devrait maintenant afficher la bonne valeur
115
+
116
+ if ( ! apiKey || ! currentThreadId ) return ;
117
+
105
118
try {
106
119
const response = await axios . get (
107
- `https://api.openai.com/v1/threads/${ threadId } /runs/${ runId } ` ,
120
+ `https://api.openai.com/v1/threads/${ currentThreadId } /runs/${ runId } ` ,
108
121
{
109
122
headers : {
110
123
"Content-Type" : "application/json" ,
111
124
"OpenAI-Beta" : "assistants=v2" ,
112
125
Authorization : `Bearer ${ apiKey } ` ,
113
- } ,
126
+ }
114
127
}
115
128
) ;
116
-
129
+
117
130
const runStatus = response . data . status ;
118
-
131
+ console . log ( "Run status:" , runStatus ) ;
132
+
119
133
if ( runStatus === "completed" ) {
120
- await fetchMessages ( ) ; // Fetch messages only once when completed
121
- } else if ( runStatus === "failed" || runStatus === "expired" ) {
122
- console . error ( "Le run a échoué ou a expiré." ) ;
123
- } else {
124
- setTimeout ( ( ) => checkRunStatus ( runId ) , 1000 ) ; // Retry checking every second
134
+ console . log ( "The run has completed successfully." ) ;
135
+ await fetchMessages ( currentThreadId ) ; // Utiliser le threadId correct
136
+ } else if ( runStatus === "failed" ) {
137
+ console . error ( "The run has failed" ) ;
138
+ } else if ( runStatus === "queued" || runStatus === "in_progress" ) {
139
+ console . log ( "Retrying to check run status..." ) ;
140
+ setTimeout ( ( ) => checkRunStatus ( runId , currentThreadId ) , 50000 ) ; // Réessayez en passant le bon threadId
125
141
}
126
142
} catch ( error ) {
127
- console . error ( "Erreur lors de la vérification du statut du run:" , error ) ;
128
- } finally {
129
- setIsLoading ( false ) ; // Always stop loading after check
143
+ console . error ( "Error while checking run status:" , error ) ;
130
144
}
131
145
} ;
146
+
147
+
132
148
133
149
const handleSubmitConfig = async ( ) => {
134
150
setFormVisible ( false ) ;
135
151
const newThreadId = await createThreadAndRun ( ) ; // Await and receive newThreadId
136
152
if ( newThreadId !== null ) {
137
153
setIsLoading ( true ) ;
138
- await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ;
154
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ;
139
155
console . log ( "Thread ID créé:" , newThreadId ) ;
140
156
setThreadId ( newThreadId ) ;
141
157
fetchMessages ( newThreadId ) ;
@@ -160,6 +176,7 @@ function App() {
160
176
}
161
177
}
162
178
) ;
179
+ console . log ( "Messages fetched:" , response . data . data ) ; // Add a log here
163
180
164
181
const filteredMessages = response . data . data . filter (
165
182
( message : { role : string } ) =>
@@ -168,12 +185,13 @@ function App() {
168
185
169
186
setMessages ( filteredMessages . reverse ( ) ) ;
170
187
} catch ( error ) {
171
- console . error ( "Erreur lors de la récupération des messages:" , error ) ;
188
+ console . error ( "Error while fetching messages:" , error ) ;
172
189
} finally {
173
190
setIsLoading ( false ) ;
174
191
setIsAssistantTyping ( false ) ; // Hide typing indicator once message is received
175
192
}
176
193
} ;
194
+
177
195
178
196
//afficher les messages
179
197
const formatMessageContent = ( content : string ) => {
@@ -450,8 +468,9 @@ function App() {
450
468
} }
451
469
>
452
470
< NorthIcon />
453
- </ Button >
471
+ </ Button >
454
472
</ Box >
473
+
455
474
) }
456
475
</ >
457
476
) }
0 commit comments