File size: 3,622 Bytes
89d813f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
AgentState โ€” LangGraph ็Šถๆ€ๆœบ็š„ๅ…จๅฑ€็Šถๆ€ๅฎšไน‰

่ฎพ่ฎก่ฏดๆ˜Ž๏ผš
- thread_id ๅทฒ้ข„็•™๏ผŒๅฝ“ๅ‰ๅ‰็ซฏๆฏๆฌก่ฏทๆฑ‚ไผ ๅ…ฅๅ…จ้‡ conversation_path๏ผˆๆ— ็Šถๆ€ๆจกๅผ๏ผ‰
- ๆœชๆฅ่‹ฅ่ฆๆŽฅๅ…ฅ PostgreSQL/Redis Checkpointer๏ผŒๅช้œ€ๅœจ graph.compile() ๅค„
  ไผ ๅ…ฅ checkpointer ๅ‚ๆ•ฐ๏ผŒๅ‰็ซฏๅ’Œ FastAPI ่ทฏ็”ฑๆ— ้œ€ๆ”นๅŠจ
"""

from __future__ import annotations

from typing import Literal, NotRequired
from typing_extensions import TypedDict


class DocumentSection(TypedDict):
    id: str
    title: str
    level: int
    content: str


class LearningDocument(TypedDict):
    type: Literal["file", "topic"]
    title: str
    content: str
    structure: list[DocumentSection]


class BubbleNode(TypedDict):
    id: str
    parentId: str | None
    userQuery: str
    aiResponse: str
    anchorText: NotRequired[str | None]
    resolved: NotRequired[bool]


class QuoteRef(TypedDict):
    nodeId: str
    text: str


class AnchorLocation(TypedDict):
    anchor_text: str | None
    section_id: str | None
    confidence: float


class DecomposedQuestion(TypedDict):
    id: str
    query: str
    anchor: str | None
    reason: str | None
    order: int
    selected: bool
    mountNodeId: NotRequired[str | None]
    mountStrategy: NotRequired[str | None]
    mountReason: NotRequired[str | None]
    section_id: NotRequired[str | None]


class TreeMountDecision(TypedDict):
    questionId: str
    parentId: str
    strategy: str
    reason: str


class AgentState(TypedDict):
    # โ”€โ”€ ่ฏทๆฑ‚ๆ ‡่ฏ† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    # ้ข„็•™ thread_id๏ผš็›ฎๅ‰ๅ‰็ซฏๆฏๆฌกไผ ๅ…จ้‡ context๏ผˆๆ— ็Šถๆ€๏ผ‰
    # ๆœชๆฅๆŽฅๅ…ฅ Checkpointer ๆ—ถ๏ผŒๅŽ็ซฏๅฏ็”จๆญค ID ๆฃ€็ดขๅކๅฒ๏ผŒๅ‰็ซฏๆ— ้œ€ไปปไฝ•ๆ”นๅŠจ
    thread_id: str  # UUID๏ผŒ็”ฑๅ‰็ซฏ๏ผˆsessionId๏ผ‰็”Ÿๆˆๅนถ้šๆฏๆฌก่ฏทๆฑ‚ไผ ๅ…ฅ

    # โ”€โ”€ ่พ“ๅ…ฅ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    document: LearningDocument
    conversation_path: list[BubbleNode]  # ไปŽๆ นๅˆฐๅฝ“ๅ‰่Š‚็‚น็š„ๆฐ”ๆณก่ทฏๅพ„๏ผˆๅซๅฎŒๆ•ด aiResponse๏ผ‰
    user_query: str
    anchor_text: str | None
    language: Literal["en", "zh"]
    mode: Literal["overview", "followup", "decompose", "tree_writer"]
    skip_decomposition: bool

    # โ”€โ”€ ไธญ้—ดไบง็‰ฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    decomposed_questions: list[DecomposedQuestion]  # decomposer ไบงๅ‡บ
    needs_decomposition: bool                        # router ๅˆคๆ–ญ๏ผšๆ˜ฏๅฆ้œ€่ฆๆ‹†่งฃ
    located_anchor: NotRequired[AnchorLocation]      # anchor_locator ไบงๅ‡บ (ๅ•ๆ้—ฎๆจกๅผ)
    tree_nodes: NotRequired[list[BubbleNode]]
    tree_questions: NotRequired[list[DecomposedQuestion]]
    current_node_id: NotRequired[str | None]
    quote_refs: NotRequired[list[QuoteRef]]
    tree_mounts: NotRequired[list[TreeMountDecision]]

    # โ”€โ”€ ๆœ€็ปˆ่พ“ๅ‡บ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    answer: str           # ๅฎŒๆ•ดๅ›ž็ญ”ๆ–‡ๆœฌ๏ผˆๆตๅผๅœบๆ™ฏไธ‹็”ฑ tutor node ๆž„ๅปบ๏ผ‰
    is_off_topic: bool
    off_topic_hint: str | None
    document_summary: NotRequired[str | None]