diff options
Diffstat (limited to 'src/server/ApiManagers/AssistantManager.ts')
-rw-r--r-- | src/server/ApiManagers/AssistantManager.ts | 71 |
1 files changed, 38 insertions, 33 deletions
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts index 54d78cd15..2ffc99e58 100644 --- a/src/server/ApiManagers/AssistantManager.ts +++ b/src/server/ApiManagers/AssistantManager.ts @@ -204,23 +204,42 @@ export default class AssistantManager extends ApiManager { } ); - const jobId = response.data.job_id; + const jobId = response.data['job_id']; + console.log('Job ID:', jobId); - let progress; - let result; - while (!result) { - await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for 5 seconds - const progressResponse = await axios.get(`http://localhost:8080/getProgress/${jobId}`); - const progress = progressResponse.data; + res.send({ jobId }); + } catch (error: any) { + console.error('Error communicating with chatbot:', error); + res.status(500).send({ error: 'Failed to communicate with the chatbot', details: error.message }); + } + }, + }); - // Accessing the correct keys - console.log(`Current step: ${progress.step}, Progress within step: ${progress.progress}%`); + register({ + method: Method.GET, + subscription: '/getProgress/:jobId', + secureHandler: async ({ req, res }) => { + const { jobId } = req.params; + try { + const progressResponse = await axios.get(`http://localhost:8080/getProgress/${jobId}`); + console.log(`Current step: ${progressResponse.data.step}, Progress within step: ${progressResponse.data.progress}%`); + res.json(progressResponse.data); + } catch (error) { + console.error('Error getting progress:', error); + res.status(500).send({ error: 'Failed to get progress', details: JSON.parse(error as string).message }); + } + }, + }); - const resultResponse = await axios.get(`http://localhost:8080/getResult/${jobId}`); - if (resultResponse.status === 200) { - result = resultResponse.data; - } - } + register({ + method: Method.GET, + subscription: '/getResult/:jobId', + secureHandler: async ({ req, res }) => { + const { jobId } = req.params; + try { + const finalResponse = await axios.get(`http://localhost:8080/getResult/${jobId}`); + console.log('Result:', finalResponse.data); + const result = finalResponse.data; if (result.chunks && Array.isArray(result.chunks)) { for (const chunk of result.chunks) { @@ -249,29 +268,15 @@ export default class AssistantManager extends ApiManager { } } } + result['status'] = 'completed'; } else { - console.warn("Result does not contain an iterable 'chunks' property"); + console.warn('Not ready'); + result.status = 'pending'; } - - res.send({ document_json: result }); - } catch (error: any) { - console.error('Error communicating with chatbot:', error); - res.status(500).send({ error: 'Failed to communicate with the chatbot', details: error.message }); - } - }, - }); - - register({ - method: Method.GET, - subscription: '/getProgress/:jobId', - secureHandler: async ({ req, res }) => { - const { jobId } = req.params; - try { - const progressResponse = await axios.get(`http://localhost:8080/getProgress/${jobId}`); - res.json(progressResponse.data); + res.json(result); } catch (error) { console.error('Error getting progress:', error); - res.status(500).send({ error: 'Failed to get progress', details: JSON.parse(error as string).message }); + res.status(500).send({ error: 'Failed to get progress', details: error }); } }, }); |