@@ -125,10 +125,8 @@ export async function openAssistantForm({
125
125
lastTask = task
126
126
view . selectedTaskId = lastTask ?. id
127
127
view . expectedRuntime = ( lastTask ?. completionExpectedAt - lastTask ?. scheduledAt ) || null
128
- const setProgress = ( progress ) => {
129
- view . progress = progress
130
- }
131
- pollTask ( task . id , setProgress ) . then ( finishedTask => {
128
+
129
+ pollTask ( task . id , view ) . then ( finishedTask => {
132
130
console . debug ( 'pollTask.then' , finishedTask )
133
131
if ( finishedTask . status === TASK_STATUS_STRING . successful ) {
134
132
if ( closeOnResult ) {
@@ -139,7 +137,7 @@ export async function openAssistantForm({
139
137
} else if ( finishedTask . status === TASK_STATUS_STRING . failed ) {
140
138
showError (
141
139
t ( 'assistant' , 'The server failed to process your task with ID {id}' , { id : finishedTask . id } )
142
- + '. ' + t ( 'assistant' , 'Please inform the server administrators of this issue.' ) ,
140
+ + '. ' + t ( 'assistant' , 'Please inform the server administrators of this issue.' ) ,
143
141
)
144
142
console . error ( '[assistant] Task failed' , finishedTask )
145
143
view . outputs = null
@@ -215,18 +213,15 @@ export async function openAssistantForm({
215
213
view . progress = null
216
214
view . expectedRuntime = ( updatedTask ?. completionExpectedAt - updatedTask ?. scheduledAt ) || null
217
215
218
- const setProgress = ( progress ) => {
219
- view . progress = progress
220
- }
221
- pollTask ( updatedTask . id , setProgress ) . then ( finishedTask => {
216
+ pollTask ( updatedTask . id , view ) . then ( finishedTask => {
222
217
console . debug ( 'pollTask.then' , finishedTask )
223
218
if ( finishedTask . status === TASK_STATUS_STRING . successful ) {
224
219
view . outputs = finishedTask ?. output
225
220
view . selectedTaskId = finishedTask ?. id
226
221
} else if ( finishedTask . status === TASK_STATUS_STRING . failed ) {
227
222
showError (
228
223
t ( 'assistant' , 'The server failed to process your task with ID {id}' , { id : finishedTask . id } )
229
- + '. ' + t ( 'assistant' , 'Please inform the server administrators of this issue.' ) ,
224
+ + '. ' + t ( 'assistant' , 'Please inform the server administrators of this issue.' ) ,
230
225
)
231
226
console . error ( '[assistant] Task failed' , finishedTask )
232
227
view . outputs = null
@@ -287,7 +282,15 @@ export async function openAssistantForm({
287
282
} )
288
283
}
289
284
290
- export async function pollTask ( taskId , setProgress ) {
285
+ function updateTask ( task , object ) {
286
+ if ( task ?. status === TASK_STATUS_STRING . running ) {
287
+ object . progress = task ?. progress * 100
288
+ }
289
+ object . taskStatus = task ?. status
290
+ object . scheduledAt = task ?. scheduledAt
291
+ }
292
+
293
+ export async function pollTask ( taskId , obj , callback = updateTask ) {
291
294
return new Promise ( ( resolve , reject ) => {
292
295
window . assistantPollTimerId = setInterval ( ( ) => {
293
296
getTask ( taskId ) . then ( response => {
@@ -296,8 +299,8 @@ export async function pollTask(taskId, setProgress) {
296
299
reject ( new Error ( 'pollTask cancelled' ) )
297
300
return
298
301
}
299
- if ( task ?. status === TASK_STATUS_STRING . running ) {
300
- setProgress ( task ?. progress * 100 )
302
+ if ( obj ) {
303
+ callback ( task , obj )
301
304
}
302
305
if ( ! [ TASK_STATUS_STRING . scheduled , TASK_STATUS_STRING . running ] . includes ( task ?. status ) ) {
303
306
// stop polling
@@ -548,7 +551,7 @@ export async function openAssistantTask(
548
551
lastTask = task
549
552
view . selectedTaskId = lastTask ?. id
550
553
view . expectedRuntime = ( lastTask ?. completionExpectedAt - lastTask ?. scheduledAt ) || null
551
- pollTask ( task . id ) . then ( finishedTask => {
554
+ pollTask ( task . id , view ) . then ( finishedTask => {
552
555
if ( finishedTask . status === TASK_STATUS_STRING . successful ) {
553
556
view . outputs = finishedTask ?. output
554
557
} else if ( finishedTask . status === TASK_STATUS_STRING . failed ) {
@@ -627,10 +630,7 @@ export async function openAssistantTask(
627
630
view . progress = null
628
631
view . expectedRuntime = ( updatedTask ?. completionExpectedAt - updatedTask ?. scheduledAt ) || null
629
632
630
- const setProgress = ( progress ) => {
631
- view . progress = progress
632
- }
633
- pollTask ( updatedTask . id , setProgress ) . then ( finishedTask => {
633
+ pollTask ( updatedTask . id , view ) . then ( finishedTask => {
634
634
console . debug ( 'pollTask.then' , finishedTask )
635
635
if ( finishedTask . status === TASK_STATUS_STRING . successful ) {
636
636
view . outputs = finishedTask ?. output
0 commit comments