@peopl-health/nexus 3.8.7 → 3.8.9

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.
@@ -35,6 +35,7 @@ class EvalProvider {
35
35
  // or { type: 'function', name: 'toolName' } to force a specific tool
36
36
  this.toolChoice = config.toolChoice || 'auto';
37
37
  this.promptVersions = config.promptVersions || {};
38
+ this.presetId = config.presetId || null;
38
39
  this.label = options.label || config.label || `nexus:${this.model}`;
39
40
 
40
41
  this.client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
@@ -65,9 +66,9 @@ class EvalProvider {
65
66
 
66
67
  const { messages, promptVariables, lastUserMessage } = await this._buildContext(numero, beforeCheckpoint);
67
68
 
68
- const { devContent, assistant, toolSchemas } = await this._resolvePrompt(prompt, assistantId, thread, promptVariables);
69
+ const { devContent, assistant, toolSchemas, resolvedPromptId } = await this._resolvePrompt(prompt, assistantId, thread, promptVariables);
69
70
 
70
- const apiConfig = this._buildApiConfig(devContent, messages, assistantId, promptVariables, toolSchemas);
71
+ const apiConfig = this._buildApiConfig(devContent, messages, resolvedPromptId || assistantId, promptVariables, toolSchemas);
71
72
 
72
73
  const startTime = Date.now();
73
74
  const { finalResponse, toolCallsRequested, allToolsExecuted, accumulatedUsage } =
@@ -107,12 +108,20 @@ class EvalProvider {
107
108
 
108
109
  async _resolvePrompt(fallbackPrompt, assistantId, thread, promptVariables) {
109
110
  let devContent;
110
- if (this.promptSource === 'airtable' && assistantId) {
111
- const { resolvedPrompt } = await composePrompt({
111
+ let resolvedPromptId = assistantId;
112
+ let presetToolIds = null;
113
+
114
+ const presetId = this.presetId || thread?.preset_id || null;
115
+
116
+ if (this.promptSource === 'airtable' && (presetId || assistantId)) {
117
+ const { resolvedPrompt, promptId, presetToolIds: toolIds } = await composePrompt({
118
+ presetId,
112
119
  promptId: assistantId,
113
120
  variables: promptVariables,
114
121
  });
115
122
  devContent = resolvedPrompt;
123
+ resolvedPromptId = promptId || assistantId;
124
+ presetToolIds = toolIds;
116
125
  } else {
117
126
  devContent = fallbackPrompt;
118
127
  devContent = devContent.replace(/\{\{(\w+)\}\}/g, (_, key) => promptVariables[key] ?? '');
@@ -131,7 +140,11 @@ class EvalProvider {
131
140
  toolSchemas = Array.from(schemasByName.values());
132
141
 
133
142
  if (toolSchemas.length > 0) {
134
- const { toolIds, filtered } = await resolveTools({ promptId: assistantId, assistant });
143
+ const { toolIds, filtered } = await resolveTools({
144
+ promptId: resolvedPromptId,
145
+ assistant,
146
+ presetToolIds,
147
+ });
135
148
  const activeToolSchemas = filtered && toolIds.length > 0
136
149
  ? toolSchemas.filter(s => toolIds.includes(s.function?.name))
137
150
  : toolSchemas;
@@ -145,13 +158,13 @@ class EvalProvider {
145
158
  }
146
159
  }
147
160
 
148
- return { devContent, assistant, toolSchemas };
161
+ return { devContent, assistant, toolSchemas, resolvedPromptId };
149
162
  }
150
163
 
151
164
  _buildApiConfig(devContent, messages, assistantId, promptVariables, toolSchemas) {
152
165
  const convertedMessages = this.provider._convertItemsToApiFormat(messages);
153
166
  const input = [{ role: 'developer', content: devContent }, ...convertedMessages];
154
- const apiConfig = { input, instructions: '' };
167
+ const apiConfig = { input, instructions: devContent || '' };
155
168
 
156
169
  if (assistantId) {
157
170
  apiConfig.prompt = { id: assistantId, variables: promptVariables };
@@ -5,7 +5,7 @@ const { Monitoreo_ID } = require('../config/airtableConfig');
5
5
 
6
6
  const { logger } = require('../utils/logger');
7
7
 
8
- const { getRecordByFilter } = require('../services/airtableService');
8
+ const { getRecordByFilter, updateRecordByFilter } = require('../services/airtableService');
9
9
 
10
10
  const messageSchema = new mongoose.Schema({
11
11
  raw: { type: Object, default: null },
@@ -173,6 +173,11 @@ async function insertMessage(values) {
173
173
  { upsert: true, new: true, setDefaultsOnInsert: true }
174
174
  );
175
175
 
176
+ updateRecordByFilter(Monitoreo_ID, 'message_monitor', `{whatsapp_id} = "${values.numero}"`, {
177
+ ...(values.from_me ? { last_message_bot: values.body } : { last_message_patient: values.body }),
178
+ ...(values.from_me ? { last_message_bot_time: values.timestamp } : { last_message_patient_time: values.timestamp })
179
+ }).catch(err => logger.error('[MongoStorage] Failed to update message_monitor table', { numero: values.numero, error: err.message }));
180
+
176
181
  logger.info('[MongoStorage] Message inserted or updated successfully');
177
182
  } catch (err) {
178
183
  logger.error('[MongoStorage] Error inserting message', { error: err.message, stack: err.stack });
@@ -354,7 +354,7 @@ class OpenAIResponsesProvider {
354
354
 
355
355
  const apiCallConfig = {
356
356
  prompt: promptConfig,
357
- instructions: instructions || additionalInstructions || '',
357
+ instructions: instructions || additionalInstructions || devContent || '',
358
358
  metadata,
359
359
  tool_choice: toolChoice
360
360
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "3.8.7",
3
+ "version": "3.8.9",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",