@peopl-health/nexus 5.12.5 → 5.12.6

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.
@@ -208,7 +208,8 @@ class AnthropicProvider extends BaseLLMProvider {
208
208
  const turnContent = Array.isArray(lastUserTurn.content)
209
209
  ? lastUserTurn.content
210
210
  : [{ type: 'text', text: String(lastUserTurn.content ?? '') }];
211
- lastUserTurn.content = [...turnContent, { type: 'text', text: DELIVERY_NUDGE }];
211
+ const alreadyNudged = turnContent.some(part => part?.type === 'text' && part.text === DELIVERY_NUDGE);
212
+ if (!alreadyNudged) lastUserTurn.content = [...turnContent, { type: 'text', text: DELIVERY_NUDGE }];
212
213
  // The nudge is a model call like any other: gate thinking on it so the per-iteration counter stays honest.
213
214
  prepThinking();
214
215
  const { result: { result: nudged, retries: nudgeRetries }, durationMs: nudgeDurationMs } = await withTiming(() => makeAPICall(currentMessages));
@@ -16,7 +16,12 @@ const CONVERSATION_CONTINUATION = '(continuar)';
16
16
  const NEGATIVE_GUIDANCE_MARKER = '**When NOT to call';
17
17
  const TERMINAL_DELIVERY_TOOLS = new Set(['DeliverPatientMessage']);
18
18
  const DELIVERY_NUDGE = 'sistema: el turno no puede cerrar sin un mensaje para el paciente. Llama ahora a DeliverPatientMessage con message_text en español. No repitas herramientas clínicas ya ejecutadas en este turno.';
19
- const MAX_DELIVERY_NUDGES = 1;
19
+ const MAX_DELIVERY_NUDGES = 2;
20
+ const DELIVERY_FALLBACK_MESSAGES = [
21
+ 'Gracias por tu mensaje. Si necesitas apoyo, escríbeme cuando quieras.',
22
+ 'Gracias por escribirme. Aquí estoy cuando necesites algo.',
23
+ 'Recibí tu mensaje. Cuando quieras contarme más o pedir apoyo, escríbeme.',
24
+ ];
20
25
 
21
26
  function isSuperseded(shouldContinue) {
22
27
  return typeof shouldContinue === 'function' && !shouldContinue();
@@ -324,7 +329,13 @@ class BaseLLMProvider {
324
329
  const result = await this._executeConversation(config);
325
330
  // DeliverPatientMessage is the curated patient reply; prefer it over any model narration in the final response.
326
331
  const delivered = this._extractDeliveredMessage(config, result);
327
- const extractedOutput = delivered?.trim() ? delivered : this._extractMessageOutput(result);
332
+ let extractedOutput = delivered?.trim() ? delivered : this._extractMessageOutput(result);
333
+
334
+ if (!delivered?.trim() && extractedOutput?.trim() && this._deliveryExpected(result)) {
335
+ logger.warn('[runConversation] Delivery tool offered but never committed — substituting safe fallback for raw narration', { attempt, suppressedLength: extractedOutput.length });
336
+ config?.assistant?.toolRuntimeContext?.trace?.setSignals?.({ deliveryFallbackUsed: true });
337
+ extractedOutput = this._deliveryFallbackMessage(config);
338
+ }
328
339
 
329
340
  if (extractedOutput?.trim()) {
330
341
  result.output_text = extractedOutput;
@@ -371,6 +382,19 @@ class BaseLLMProvider {
371
382
  return (toolsExecuted || []).some(t => TERMINAL_DELIVERY_TOOLS.has(t?.tool_name) && this._toolSucceeded(t));
372
383
  }
373
384
 
385
+ _deliveryExpected(result) {
386
+ return (result?.tool_ids || []).some(id => TERMINAL_DELIVERY_TOOLS.has(id));
387
+ }
388
+
389
+ _deliveryFallbackMessage(config) {
390
+ const key = String(config?.assistant?.toolRuntimeContext?.turnId
391
+ ?? config?.assistant?.toolRuntimeContext?.patientCode
392
+ ?? config?.threadId ?? '');
393
+ let hash = 0;
394
+ for (let i = 0; i < key.length; i++) hash = (hash * 31 + key.charCodeAt(i)) | 0;
395
+ return DELIVERY_FALLBACK_MESSAGES[Math.abs(hash) % DELIVERY_FALLBACK_MESSAGES.length];
396
+ }
397
+
374
398
  _isTerminalOnly(toolNames) {
375
399
  return toolNames.length > 0 && toolNames.every(name => TERMINAL_DELIVERY_TOOLS.has(name));
376
400
  }
@@ -531,6 +555,7 @@ module.exports = {
531
555
  CONVERSATION_CONTINUATION,
532
556
  DELIVERY_NUDGE,
533
557
  MAX_DELIVERY_NUDGES,
558
+ DELIVERY_FALLBACK_MESSAGES,
534
559
  TERMINAL_DELIVERY_TOOLS,
535
560
  isSuperseded,
536
561
  };
@@ -2,7 +2,7 @@ const { requireGatewayProvider } = require('../config/llmConfig');
2
2
  const { renderMessage } = require('../services/composerService');
3
3
  const { isComposerEnabled, isAgentSelfRenderEnabled } = require('../flags/composerFlags');
4
4
 
5
- const EMPTY_OUTBOUND_FALLBACK = 'Estoy revisando tu mensaje con detalle, te respondo en un momento.';
5
+ const EMPTY_OUTBOUND_FALLBACK = 'Recibí tu mensaje. Si quieres contarme más o necesitas apoyo, escríbeme.';
6
6
 
7
7
  const definition = {
8
8
  name: 'DeliverPatientMessage',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.12.5",
3
+ "version": "5.12.6",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",