@peopl-health/nexus 2.0.14 → 2.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,18 +47,20 @@ const addMsgAssistantController = async (req, res) => {
47
47
  };
48
48
 
49
49
  const createAssistantController = async (req, res) => {
50
- const { assistant_id, codes, instrucciones=[], messages=[], force=false } = req.body;
50
+ let { assistant_id, codes, instrucciones=[], messages=[], force=false } = req.body;
51
51
  if (!Array.isArray(codes) || codes.length === 0) {
52
52
  return res.status(400).send({ error: 'codes must be a non-empty array' });
53
53
  }
54
+ instrucciones = Array.isArray(instrucciones) ? instrucciones : [];
55
+ messages = Array.isArray(messages) ? messages : [];
54
56
 
55
57
  try {
56
- console.log('codes', codes);
58
+ console.log('[createAssistantController] codes', codes);
57
59
  for (const code of codes) {
58
60
  await createAssistant(code, assistant_id, [...instrucciones, ...messages], force);
59
- console.log('messages', messages);
61
+ console.log('[createAssistantController] messages', messages);
60
62
  for (const message of messages) {
61
- console.log('message', message);
63
+ console.log('[createAssistantController] message', message);
62
64
  await sendMessage({code, body: message, fileType: 'text'});
63
65
  }
64
66
  }
@@ -194,13 +194,15 @@ const createAssistant = async (code, assistant_id, messages=[], force=false) =>
194
194
  // If thread already exists, update it
195
195
  const findThread = await Thread.findOne({ code: code });
196
196
  const variant = process.env.VARIANT || 'assistants';
197
+ console.log('[createAssistant] findThread', findThread);
197
198
  if ((findThread?.conversation_id && variant === 'responses') || (findThread?.thread_id && variant === 'assistants')) {
199
+ console.log('[createAssistant] Thread already exists');
198
200
  await Thread.updateOne({ code: code }, { $set: { active: true, stopped: false, assistant_id: assistant_id } });
199
201
  return findThread;
200
202
  }
201
203
 
202
204
  const curRow = await getCurRow(Historial_Clinico_ID, code);
203
- console.log('curRow', curRow[0]);
205
+ console.log('[createAssistant] curRow', curRow[0]);
204
206
  const nombre = curRow?.[0]?.['name'] || null;
205
207
  const patientId = curRow?.[0]?.['record_id'] || null;
206
208
 
@@ -217,7 +219,7 @@ const createAssistant = async (code, assistant_id, messages=[], force=false) =>
217
219
  });
218
220
  }
219
221
 
220
- console.log('initialThread', initialThread);
222
+ console.log('[createAssistant] initialThread', initialThread);
221
223
  // Define new thread data
222
224
  const thread = {
223
225
  code: code,
@@ -233,11 +235,12 @@ const createAssistant = async (code, assistant_id, messages=[], force=false) =>
233
235
  const updateData = Object.fromEntries(
234
236
  Object.entries(thread).filter(([_, v]) => v != null)
235
237
  );
238
+ console.log('[createAssistant] updateData', updateData);
236
239
 
237
- const condition = { $or: [{ thread_id: findThread?.thread_id }, { conversation_id: findThread?.conversation_id }] };
240
+ const condition = { $or: [{ code: code }] };
238
241
  const options = { new: true, upsert: true };
239
242
  const updatedThread = await Thread.findOneAndUpdate(condition, {run_id: null, ...updateData}, options);
240
- console.log('Updated thread:', updatedThread);
243
+ console.log('[createAssistant] Updated thread:', updatedThread);
241
244
 
242
245
  // Delete previous thread
243
246
  if (force) {
@@ -265,11 +268,10 @@ const addMsgAssistant = async (code, inMessages, reply = false) => {
265
268
 
266
269
  if (!reply) return null;
267
270
 
268
- const assistant = getAssistantById(thread?.prompt_id || thread?.assistant_id, thread);
269
271
  let output, completed;
270
272
  let retries = 0;
271
273
  const maxRetries = 10;
272
-
274
+ const assistant = getAssistantById(thread?.prompt_id || thread?.assistant_id, thread);
273
275
  do {
274
276
  ({ output, completed } = await runAssistantAndWait({ thread, assistant }));
275
277
  console.log(`Attempt ${retries + 1}: completed=${completed}, output=${output || '(empty)'}`);
@@ -293,17 +295,27 @@ const addInsAssistant = async (code, instruction) => {
293
295
  console.log(thread);
294
296
  if (thread === null) return null;
295
297
 
298
+ let output, completed;
299
+ let retries = 0;
300
+ const maxRetries = 10;
296
301
  const assistant = getAssistantById(thread?.prompt_id || thread?.assistant_id, thread);
297
- const { output } = await runAssistantAndWait({
298
- thread,
299
- assistant,
300
- runConfig: {
301
- additionalInstructions: instruction,
302
- additionalMessages: [
303
- { role: 'user', content: instruction }
304
- ]
305
- }
306
- });
302
+ do {
303
+ ({ output, completed } = await runAssistantAndWait({
304
+ thread,
305
+ assistant,
306
+ runConfig: {
307
+ additionalInstructions: instruction,
308
+ additionalMessages: [
309
+ { role: 'user', content: instruction }
310
+ ]
311
+ }
312
+ }));
313
+ console.log(`Attempt ${retries + 1}: completed=${completed}, output=${output || '(empty)'}`);
314
+
315
+ if (completed && output) break;
316
+ if (retries < maxRetries) await new Promise(resolve => setTimeout(resolve, 2000));
317
+ retries++;
318
+ } while (retries <= maxRetries && (!completed || !output));
307
319
  console.log('RUN RESPONSE', output);
308
320
 
309
321
  return output;
@@ -410,11 +422,22 @@ const replyAssistant = async function (code, message_ = null, thread_ = null, ru
410
422
  const assistant = getAssistantById(process.env.VARIANT === 'responses' ? thread?.prompt_id : thread?.assistant_id, thread);
411
423
  assistant.setReplies(patientReply);
412
424
 
413
- const { run, output, completed } = await runAssistantAndWait({
414
- thread,
415
- assistant,
416
- runConfig: runOptions
417
- });
425
+ let run, output, completed;
426
+ let retries = 0;
427
+ const maxRetries = 10;
428
+ do {
429
+ ({ run, output, completed } = await runAssistantAndWait({
430
+ thread,
431
+ assistant,
432
+ runConfig: runOptions
433
+ }));
434
+ console.log(`Attempt ${retries + 1}: completed=${completed}, output=${output || '(empty)'}`);
435
+
436
+ if (completed && output) break;
437
+ if (retries < maxRetries) await new Promise(resolve => setTimeout(resolve, 2000));
438
+ retries++;
439
+ } while (retries <= maxRetries && (!completed || !output));
440
+
418
441
  console.log('RUN LAST ERROR:', run?.last_error);
419
442
  console.log('RUN STATUS', completed);
420
443
  console.log(output);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "2.0.14",
3
+ "version": "2.0.16",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",