-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
126 lines (84 loc) · 3.11 KB
/
Copy pathstate.py
File metadata and controls
126 lines (84 loc) · 3.11 KB
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
"""
state.py — 运行时状态容器
==========================
维护插件运行时的全局状态:当前会话、消息列表、连接状态、待执行工具等。
模块级变量作为单例使用(Blender 插件中全局唯一)。
"""
from __future__ import annotations
from .schemas import (
ConnectionState,
Message,
RuntimeState,
Session,
ToolCall,
)
# ---------------------------------------------------------------------------
# 全局单例状态
# ---------------------------------------------------------------------------
_state = RuntimeState()
def get_state() -> RuntimeState:
"""获取当前运行时状态。"""
return _state
def reset_state() -> None:
"""重置所有运行时状态(用于新建会话 / 重新加载)。"""
global _state
_state = RuntimeState()
# ---------------------------------------------------------------------------
# 便捷访问器
# ---------------------------------------------------------------------------
def get_current_session() -> Session | None:
return _state.current_session
def set_current_session(session: Session) -> None:
_state.current_session = session
# 确保 session 在 sessions 列表中
if session not in _state.sessions:
_state.sessions.append(session)
def new_session() -> Session:
"""创建新会话并设为当前。"""
session = Session()
set_current_session(session)
return session
def add_message(msg: Message) -> None:
"""向当前会话添加消息。"""
if _state.current_session is None:
new_session()
assert _state.current_session is not None
_state.current_session.add_message(msg)
def get_messages() -> list[Message]:
"""获取当前会话所有消息。"""
if _state.current_session is None:
return []
return _state.current_session.messages
def set_connection_state(cs: ConnectionState) -> None:
_state.connection_state = cs
def is_processing() -> bool:
return _state.is_processing
def set_processing(flag: bool) -> None:
_state.is_processing = flag
def set_last_error(err: str) -> None:
_state.last_error = err
def add_recent_event(event_text: str, *, limit: int = 30) -> None:
"""记录一条最近操作事件,用于自动上下文同步。"""
text = event_text.strip()
if not text:
return
_state.recent_events.append(text)
if len(_state.recent_events) > limit:
_state.recent_events[:] = _state.recent_events[-limit:]
def get_recent_events(limit: int = 30) -> list[str]:
"""返回最近操作事件。"""
if limit <= 0:
return []
return _state.recent_events[-limit:]
def set_last_context_signature(signature: str) -> None:
_state.last_context_signature = signature
def get_last_context_signature() -> str:
return _state.last_context_signature
def add_pending_tool_call(tc: ToolCall) -> None:
_state.pending_tool_calls.append(tc)
def clear_pending_tools() -> None:
_state.pending_tool_calls.clear()
def add_attachment(att: dict) -> None:
_state.attachments.append(att)
def clear_attachments() -> None:
_state.attachments.clear()