File size: 5,808 Bytes
582520a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Core chat orchestration service."""

from __future__ import annotations

from typing import Any, Optional

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

from app.models import (
    Conversation,
    Lead,
    Message,
    UnansweredQuestion,
)
from app.services import calcom, handoff, leads, llm
from app.services.language import detect_language
from app.services.rag import get_knowledge_store

BOOKING_HINTS = (
    "book",
    "appointment",
    "schedule",
    "meeting",
    "slot",
    "rendez-vous",
    "rdv",
    "créneau",
    "запис",
    "встреч",
    "слот",
)


GREETINGS = {
    "en": (
        "Hello! I am ReplyFlow for Nordic Paper Solutions. "
        "Ask about products, pricing, delivery, or book a call."
    ),
    "fr": (
        "Bonjour ! Je suis ReplyFlow pour Nordic Paper "
        "Solutions. Posez vos questions sur les produits, "
        "tarifs, livraison, ou réservez un appel."
    ),
    "ru": (
        "Здравствуйте! Я ReplyFlow для Nordic Paper "
        "Solutions. Спросите о продуктах, ценах, доставке "
        "или запишитесь на звонок."
    ),
}


async def start_conversation(
    db: AsyncSession,
    *,
    channel: str = "web",
    ui_language: str = "en",
    external_id: Optional[str] = None,
) -> tuple[Conversation, str]:
    """Create conversation + empty lead + greeting."""
    lang = ui_language if ui_language in GREETINGS else "en"
    conv = Conversation(
        channel=channel,
        language=lang,
        external_id=external_id,
    )
    db.add(conv)
    await db.flush()
    db.add(Lead(conversation_id=conv.id))
    greeting = GREETINGS[lang]
    db.add(
        Message(
            conversation_id=conv.id,
            role="assistant",
            content=greeting,
            language=lang,
            grounded=True,
        )
    )
    await db.commit()
    await db.refresh(conv)
    return conv, greeting


async def handle_user_message(
    db: AsyncSession,
    *,
    conversation_id: int,
    message: str,
    ui_language: str = "en",
) -> dict[str, Any]:
    """Process one user turn end-to-end."""
    result = await db.execute(
        select(Conversation)
        .where(Conversation.id == conversation_id)
        .options(
            selectinload(Conversation.messages),
            selectinload(Conversation.lead),
        )
    )
    conv = result.scalar_one()
    language = detect_language(message, default=ui_language)
    conv.language = language

    previous_bot = None
    for msg in reversed(conv.messages):
        if msg.role == "assistant":
            previous_bot = msg.content
            break

    lead = conv.lead or Lead(conversation_id=conv.id)
    if conv.lead is None:
        db.add(lead)
        await db.flush()
    leads.update_lead_from_message(lead, message, previous_bot)

    db.add(
        Message(
            conversation_id=conv.id,
            role="user",
            content=message,
            language=language,
        )
    )

    wants_booking = any(
        h in message.lower() for h in BOOKING_HINTS
    )
    store = get_knowledge_store()
    chunks = await store.search(message, top_k=4)
    grounded = bool(chunks)

    if not grounded:
        conv.failure_streak += 1
        db.add(
            UnansweredQuestion(
                conversation_id=conv.id,
                question=message,
            )
        )
    else:
        conv.failure_streak = 0

    history = [
        {"role": m.role, "content": m.content}
        for m in conv.messages
        if m.role in {"user", "assistant"}
    ]
    lead_state = leads.lead_to_dict(lead)

    do_handoff, reason = handoff.should_handoff(
        message=message,
        failure_streak=conv.failure_streak,
        grounded=grounded,
    )

    suggested_slots = None
    reply: str
    if do_handoff:
        conv.handed_off = True
        conv.status = "handed_off"
        reply = handoff.handoff_message(language, reason)
        await handoff.notify_handoff(
            conversation_id=conv.id,
            reason=reason,
            history=history + [{"role": "user", "content": message}],
            lead=lead_state,
        )
    else:
        reply, grounded = await llm.generate_reply(
            question=message,
            language=language,
            context_chunks=chunks,
            history=history,
            lead_state=lead_state,
        )
        if wants_booking or (
            lead.complete and "book" in reply.lower()
        ):
            suggested_slots = await calcom.get_available_slots(3)
            offer = {
                "en": "Here are 3 available slots:",
                "fr": "Voici 3 créneaux disponibles :",
                "ru": "Вот 3 свободных слота:",
            }
            reply += "\n\n" + offer.get(language, offer["en"])
            for i, slot in enumerate(suggested_slots, start=1):
                reply += f"\n{i}. {slot['label']}"

        if grounded and not conv.handed_off:
            conv.resolved_without_human = True

    db.add(
        Message(
            conversation_id=conv.id,
            role="assistant",
            content=reply,
            language=language,
            grounded=grounded if not do_handoff else None,
        )
    )
    conv.updated_at = conv.updated_at
    await db.commit()

    return {
        "conversation_id": conv.id,
        "reply": reply,
        "language": language,
        "grounded": grounded and not do_handoff,
        "lead": leads.lead_to_dict(lead),
        "suggested_slots": suggested_slots,
        "handed_off": conv.handed_off,
        "appointment_booked": conv.appointment_booked,
    }