-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_models.py
More file actions
293 lines (260 loc) · 9.27 KB
/
Copy pathread_models.py
File metadata and controls
293 lines (260 loc) · 9.27 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import annotations
import json
from typing import Any, Mapping, Optional
from ..core.schema import Node, SessionData
from .dto import (
FactReadModel,
InteractionReadModel,
LlmUsageStatsResponse,
NodeDetailResponse,
NodePathEntry,
NodeStateReadModel,
ReportLlmStatsResponse,
ReportResponse,
ReportStatisticsResponse,
SessionReadModel,
TreeEdgeReadModel,
TreeNodePayload,
TreeNodeReadModel,
TreeResponse,
)
def parse_display_answer(answer: str) -> str:
if not answer:
return ""
try:
if answer.strip().startswith("[") or answer.strip().startswith("{"):
data = json.loads(answer)
if isinstance(data, list):
contents = [
item.get("content", "") for item in data if isinstance(item, dict)
]
normalized = "\n".join(filter(None, contents))
return normalized or answer
if isinstance(data, dict):
return str(data.get("content", answer))
except json.JSONDecodeError:
pass
return answer
def build_session_read_model(
session: SessionData,
*,
is_active: bool,
report_available: bool,
) -> SessionReadModel:
return SessionReadModel(
session_id=session.session_id,
root_node_id=session.root_node_id,
global_goal=session.global_goal,
total_simulations=session.total_simulations,
total_tokens_used=session.total_tokens_used,
is_legacy_token_accounting=session.is_legacy_token_accounting,
created_at=session.created_at,
updated_at=session.updated_at,
status=(
session.status.value
if hasattr(session.status, "value")
else str(session.status)
),
error_message=session.error_message,
total_nodes=session.get_total_nodes(),
total_facts=len(session.global_facts),
report_available=report_available,
is_active=is_active,
)
def build_tree_response(
session: SessionData,
*,
statistics: dict[str, Any],
) -> TreeResponse:
nodes: list[TreeNodeReadModel] = []
edges: list[TreeEdgeReadModel] = []
for node in session.nodes.values():
label = "Start"
question = "Root"
answer = ""
if node.interaction:
question = node.interaction.question
label = question[:30] + ("..." if len(question) > 30 else "")
answer = parse_display_answer(node.interaction.answer)
nodes.append(
TreeNodeReadModel(
id=node.id,
position={"x": 0.0, "y": 0.0},
data=TreeNodePayload(
label=label,
full_question=question,
visits=node.state.visit_count,
value=node.state.average_value,
depth=node.depth,
isPruned=node.is_pruned,
isTerminal=node.is_terminal,
isProcessing=node.is_processing,
factsCount=len(node.new_facts),
answer=answer,
),
)
)
for child_id in node.children_ids:
edges.append(
TreeEdgeReadModel(
id=f"{node.id}-{child_id}",
source=node.id,
target=child_id,
)
)
return TreeResponse(
session_id=session.session_id,
session_revision=session.session_revision,
nodes=nodes,
edges=edges,
statistics=statistics,
)
def build_node_detail_response(
session: SessionData,
node: Node,
) -> NodeDetailResponse:
path: list[NodePathEntry] = []
current_id: Optional[str] = node.id
while current_id:
path_node = session.nodes[current_id]
path.append(
NodePathEntry(
id=path_node.id,
depth=path_node.depth,
question=(
path_node.interaction.question if path_node.interaction else None
),
visits=path_node.state.visit_count,
value=path_node.state.average_value,
)
)
current_id = path_node.parent_id
interaction = None
if node.interaction:
interaction = InteractionReadModel(
question=node.interaction.question,
answer=parse_display_answer(node.interaction.answer),
summary=node.interaction.summary,
tokens_used=node.interaction.tokens_used,
model_used=node.interaction.model_used,
created_at=node.interaction.created_at,
)
return NodeDetailResponse(
id=node.id,
parent_id=node.parent_id,
depth=node.depth,
state=NodeStateReadModel(
visit_count=node.state.visit_count,
value_sum=node.state.value_sum,
average_value=node.state.average_value,
),
interaction=interaction,
new_facts=[
FactReadModel(
id=fact.id,
content=fact.content,
confidence=fact.confidence,
created_at=fact.created_at,
)
for fact in node.new_facts
],
is_terminal=node.is_terminal,
is_pruned=node.is_pruned,
prune_reason=node.prune_reason,
created_at=node.created_at,
updated_at=node.updated_at,
path=list(reversed(path)),
)
def build_report_response(
session: SessionData,
report_data: Mapping[str, Any] | None,
) -> ReportResponse:
payload = dict(report_data or {})
partial_data = payload.get("partial_data")
partial = partial_data if isinstance(partial_data, Mapping) else {}
statistics_payload = payload.get("statistics")
statistics = statistics_payload if isinstance(statistics_payload, Mapping) else {}
total_nodes = _safe_int(
statistics.get("total_nodes"),
_safe_int(partial.get("nodes_count"), session.get_total_nodes()),
)
total_simulations = _safe_int(
statistics.get("total_simulations"),
_safe_int(partial.get("simulations"), session.total_simulations),
)
tree_depth = _safe_int(statistics.get("tree_depth"), session.get_tree_depth())
total_facts = _safe_int(
statistics.get("total_facts"),
_safe_int(partial.get("facts_count"), len(session.global_facts)),
)
llm_stats_payload = payload.get("llm_stats")
llm_stats = llm_stats_payload if isinstance(llm_stats_payload, Mapping) else {}
usage_by_model_payload = llm_stats.get("usage_by_model")
if isinstance(usage_by_model_payload, Mapping):
usage_by_model: Mapping[str, Any] = usage_by_model_payload
else:
fallback_usage = session.llm_usage.to_report_payload().get(
"usage_by_model",
{},
)
usage_by_model = fallback_usage if isinstance(fallback_usage, Mapping) else {}
return ReportResponse(
session_id=str(payload.get("session_id") or session.session_id),
goal=str(payload.get("goal") or session.global_goal),
executive_summary=_safe_str(payload.get("executive_summary")),
full_report=_safe_str(payload.get("full_report")),
key_insights=_safe_str_list(payload.get("key_insights")),
pruned_insights=_safe_str_list(payload.get("pruned_insights")),
statistics=ReportStatisticsResponse(
total_nodes=total_nodes,
total_simulations=total_simulations,
tree_depth=tree_depth,
total_facts=total_facts,
active_nodes=_safe_int(
statistics.get("active_nodes"),
len(session.get_active_nodes()),
),
pruned_nodes=_safe_int(
statistics.get("pruned_nodes"),
sum(1 for node in session.nodes.values() if node.is_pruned),
),
),
llm_stats=ReportLlmStatsResponse(
total_calls=_safe_int(
llm_stats.get("total_calls"),
session.llm_usage.total_calls,
),
total_tokens=_safe_int(
llm_stats.get("total_tokens"),
session.llm_usage.total_tokens,
),
usage_by_model={
str(model): LlmUsageStatsResponse(
calls=_safe_int(data.get("calls"), 0),
tokens=_safe_int(data.get("tokens"), 0),
)
for model, data in usage_by_model.items()
if isinstance(data, Mapping)
},
),
suggestions=_safe_str_list(payload.get("suggestions")),
generated_at=str(payload.get("generated_at") or session.updated_at.isoformat()),
error_message=_safe_optional_str(
payload.get("error_message") or payload.get("error")
),
)
def _safe_int(value: Any, default: int) -> int:
try:
return int(value)
except (TypeError, ValueError):
return default
def _safe_str(value: Any) -> str:
return value if isinstance(value, str) else ""
def _safe_optional_str(value: Any) -> Optional[str]:
if value is None:
return None
return str(value)
def _safe_str_list(value: Any) -> list[str]:
if not isinstance(value, list):
return []
return [str(item) for item in value]