File size: 2,384 Bytes
c27eaf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pydantic models for request/response and internal session state."""

from __future__ import annotations

from enum import Enum
from typing import Optional

from pydantic import BaseModel, Field


# ---------------------------------------------------------------------------
# Consultation phases
# ---------------------------------------------------------------------------

class Phase(str, Enum):
    DISCOVERY = "discovery"
    EXPLORATION = "exploration"
    CONSTRAINTS = "constraints"
    SOLUTION = "solution"
    REFINEMENT = "refinement"


PHASE_ORDER: list[Phase] = [
    Phase.DISCOVERY,
    Phase.EXPLORATION,
    Phase.CONSTRAINTS,
    Phase.SOLUTION,
    Phase.REFINEMENT,
]

# Confidence thresholds that trigger phase transitions
PHASE_THRESHOLDS: dict[Phase, tuple[float, float]] = {
    Phase.DISCOVERY:    (0.0, 0.20),
    Phase.EXPLORATION:  (0.20, 0.40),
    Phase.CONSTRAINTS:  (0.40, 0.60),
    Phase.SOLUTION:     (0.60, 0.80),
    Phase.REFINEMENT:   (0.80, 1.00),
}


# ---------------------------------------------------------------------------
# API models
# ---------------------------------------------------------------------------

class ChatRequest(BaseModel):
    session_id: str = Field(..., description="Unique session identifier")
    message: str = Field(..., min_length=1, description="User message")


class ChatResponse(BaseModel):
    reply: str
    phase: Phase
    confidence: float = Field(ge=0.0, le=1.0)
    understanding: str = Field(default="", description="Extracted understanding so far")


class SessionInfo(BaseModel):
    session_id: str
    phase: Phase
    confidence: float
    message_count: int
    created_at: str
    updated_at: str


class NewSessionResponse(BaseModel):
    session_id: str


# ---------------------------------------------------------------------------
# Internal state (persisted in SQLite as JSON)
# ---------------------------------------------------------------------------

class Message(BaseModel):
    role: str  # "user" | "assistant"
    content: str


class SessionState(BaseModel):
    session_id: str = ""
    messages: list[Message] = Field(default_factory=list)
    understanding: str = ""
    confidence: float = 0.0
    phase: Phase = Phase.DISCOVERY
    topic_start_turn: int = 0  # Turn when current topic started (for phase forcing)
    created_at: str = ""
    updated_at: str = ""