From 8de8419028212d8db21c93dc729020a8ba48e566 Mon Sep 17 00:00:00 2001 From: whekin Date: Sat, 14 Mar 2026 14:54:49 +0400 Subject: [PATCH] feat(bot): enhance topic processor logging for diagnosis Added comprehensive logging to topic-processor.ts to capture API errors, parsing failures, and silent decisions. Also added result logging to purchase and payment ingestion handlers to trace the processor's output. --- apps/bot/src/payment-topic-ingestion.ts | 5 +++ apps/bot/src/purchase-topic-ingestion.ts | 5 +++ apps/bot/src/topic-processor.ts | 42 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/apps/bot/src/payment-topic-ingestion.ts b/apps/bot/src/payment-topic-ingestion.ts index 85170a4..515e9da 100644 --- a/apps/bot/src/payment-topic-ingestion.ts +++ b/apps/bot/src/payment-topic-ingestion.ts @@ -635,6 +635,11 @@ export function registerConfiguredPaymentTopicIngestion( engagementAssessment: conversationContext.engagement }) + options.logger?.info( + { event: 'payment.topic_processor_result', result: processorResult }, + 'Topic processor finished' + ) + // Handle processor failure - only if explicitly mentioned if (!processorResult) { if (conversationContext.explicitMention) { diff --git a/apps/bot/src/purchase-topic-ingestion.ts b/apps/bot/src/purchase-topic-ingestion.ts index 9d54d75..b1f3dba 100644 --- a/apps/bot/src/purchase-topic-ingestion.ts +++ b/apps/bot/src/purchase-topic-ingestion.ts @@ -2476,6 +2476,11 @@ export function registerConfiguredPurchaseTopicIngestion( engagementAssessment: conversationContext.engagement }) + options.logger?.info( + { event: 'purchase.topic_processor_result', result: processorResult }, + 'Topic processor finished' + ) + // Handle processor failure - fun "bot sleeps" message only if explicitly mentioned if (!processorResult) { if (conversationContext.explicitMention) { diff --git a/apps/bot/src/topic-processor.ts b/apps/bot/src/topic-processor.ts index 3b2df9f..0f4e58a 100644 --- a/apps/bot/src/topic-processor.ts +++ b/apps/bot/src/topic-processor.ts @@ -390,6 +390,14 @@ If user dismisses ("не, забей", "cancel"), use dismiss_workflow.` }) if (!response.ok) { + logger?.error( + { + event: 'topic_processor.api_error', + status: response.status, + text: await response.text() + }, + 'Topic processor API error' + ) return null } @@ -398,6 +406,10 @@ If user dismisses ("не, забей", "cancel"), use dismiss_workflow.` const parsed = parseJsonFromResponseText(text ?? '') if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + logger?.error( + { event: 'topic_processor.parse_error', text }, + 'Topic processor failed to parse response' + ) return null } @@ -409,6 +421,10 @@ If user dismisses ("не, забей", "cancel"), use dismiss_workflow.` switch (route) { case 'silent': + logger?.error( + { event: 'topic_processor.silent', reason }, + 'Topic processor decided silent' + ) return { route, reason } case 'chat_reply': { @@ -417,6 +433,10 @@ If user dismisses ("не, забей", "cancel"), use dismiss_workflow.` ? parsed.replyText.trim() : null if (!replyText) { + logger?.error( + { event: 'topic_processor.empty_chat_reply', reason }, + 'Topic processor returned empty chat reply' + ) return { route: 'silent', reason: 'empty_chat_reply' } } return { route, replyText, reason } @@ -431,6 +451,15 @@ If user dismisses ("не, забей", "cancel"), use dismiss_workflow.` : null if (!amountMinor || !currency || !itemDescription) { + logger?.error( + { + event: 'topic_processor.missing_purchase_fields', + amountMinor: parsed.amountMinor, + currency: parsed.currency, + itemDescription: parsed.itemDescription + }, + 'Topic processor missing purchase fields' + ) return { route: 'purchase_clarification', clarificationQuestion: 'Could you clarify the purchase details?', @@ -476,6 +505,15 @@ If user dismisses ("не, забей", "cancel"), use dismiss_workflow.` const kind = parsed.kind === 'rent' || parsed.kind === 'utilities' ? parsed.kind : null if (!amountMinor || !currency || !kind) { + logger?.error( + { + event: 'topic_processor.missing_payment_fields', + amountMinor: parsed.amountMinor, + currency: parsed.currency, + kind: parsed.kind + }, + 'Topic processor missing payment fields' + ) return { route: 'payment_clarification', clarificationQuestion: 'Could you clarify the payment details?', @@ -505,6 +543,10 @@ If user dismisses ("не, забей", "cancel"), use dismiss_workflow.` } default: + logger?.error( + { event: 'topic_processor.unknown_route', route: parsed.route }, + 'Topic processor returned unknown route' + ) return { route: 'silent', reason: 'unknown_route' } } } catch (error) {