feat: Enhance conversation history retrieval and logging for negotiation responses

This commit is contained in:
2025-06-08 02:39:53 -03:00
parent 311ddbe5bd
commit 304b08f6bc
3 changed files with 120 additions and 22 deletions

View File

@@ -544,20 +544,26 @@ async function processNegotiation(
counterOfferContext,
);
// Create conversation message for the AI-generated response
const messageType = counterOfferContext
? "counter_offer"
: "negotiation_sent";
await client.from("conversation_messages").insert({
debt_id: record.id,
message_type: messageType,
direction: "outbound",
subject: emailResult.subject,
body: emailResult.body,
from_email: record.metadata?.toEmail || "user@example.com",
to_email: record.metadata?.fromEmail || record.vendor,
message_id: `ai-generated-${Date.now()}`,
});
// Create conversation message only for auto-responses (counter-offers)
// Regular negotiation generation doesn't create messages since they're not sent yet
if (counterOfferContext) {
await client.from("conversation_messages").insert({
debt_id: record.id,
message_type: "counter_offer",
direction: "outbound",
subject: emailResult.subject,
body: emailResult.body,
from_email: record.metadata?.toEmail || "user@example.com",
to_email: record.metadata?.fromEmail || record.vendor,
message_id: `auto-counter-${Date.now()}`,
ai_analysis: {
strategy: emailResult.strategy,
confidence: emailResult.confidenceLevel,
projectedSavings: emailResult.projectedSavings,
isAutoGenerated: true,
},
});
}
// Update debt record with AI-generated content - using provided client
const { error: updateError } = await client