latentlearn-agent / state.py
howcloudy's picture
Deploy LatentLearn Python Agent Backend to Hugging Face Space
89d813f
Raw History Blame Contribute Delete
3.62 kB
"""
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]