-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnodes.py
More file actions
276 lines (218 loc) · 9.31 KB
/
nodes.py
File metadata and controls
276 lines (218 loc) · 9.31 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
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
"""Node functions for the minimal graph workflow."""
import json
from typing import Any
from typing import Dict
from trpc_agent_sdk.context import InvocationContext
from trpc_agent_sdk.dsl.graph import AsyncEventWriter
from trpc_agent_sdk.dsl.graph import STATE_KEY_LAST_RESPONSE
from trpc_agent_sdk.dsl.graph import STATE_KEY_LAST_TOOL_RESPONSE
from trpc_agent_sdk.dsl.graph import STATE_KEY_NODE_RESPONSES
from trpc_agent_sdk.dsl.graph import STATE_KEY_USER_INPUT
from trpc_agent_sdk.dsl.graph import State
from .state import DocumentState
ROUTE_PREVIEW = "preview"
ROUTE_SUMMARIZE = "summarize"
ROUTE_SUBGRAPH = "subgraph"
ROUTE_LLM_AGENT = "llm_agent"
ROUTE_TOOL = "tool"
ROUTE_CODE = "code"
ROUTE_MCP = "mcp"
ROUTE_KNOWLEDGE = "knowledge"
WORD_COUNT_SUMMARY_THRESHOLD = 40
def _normalize_text(text: str) -> str:
return text.strip() if text else ""
def _truncate_text(text: str, max_len: int = 80) -> str:
if len(text) <= max_len:
return text
return text[:max_len - 3] + "..."
def _format_value(value: Any, max_len: int = 80) -> str:
text = repr(value)
if len(text) <= max_len:
return text
return text[:max_len - 3] + "..."
def _log_node(node_name: str, message: str) -> None:
print(f"[node_execute:{node_name}] {message}")
async def extract_document(state: State) -> Dict[str, Any]:
"""Read user input and store it on the document state.
Uses the base State type to show that generic nodes work with any schema.
"""
user_text = _normalize_text(state.get(STATE_KEY_USER_INPUT, ""))
if not user_text:
raise ValueError("No user_input provided")
_log_node("extract", f"args.user_input={_truncate_text(user_text)}")
result = {
"document": user_text,
"word_count": len(user_text.split()),
}
_log_node("extract", f"return={_format_value(result)}")
return result
async def decide_route(state: DocumentState, ctx: InvocationContext) -> Dict[str, Any]:
"""Decide which branch to take based on input and context.
Demonstrates the InvocationContext parameter.
"""
document = _normalize_text(state.get("document", ""))
word_count = state.get("word_count", 0)
_log_node("decide", f"args.document={_truncate_text(document)} word_count={word_count}")
route = ROUTE_PREVIEW
if document.lower().startswith("subgraph:"):
route = ROUTE_SUBGRAPH
document = _normalize_text(document[len("subgraph:"):])
elif document.lower().startswith("llm_agent:"):
route = ROUTE_LLM_AGENT
document = _normalize_text(document[len("llm_agent:"):])
elif document.lower().startswith("tool:"):
route = ROUTE_TOOL
document = _normalize_text(document[len("tool:"):])
elif document.lower().startswith("code:"):
route = ROUTE_CODE
document = _normalize_text(document[len("code:"):])
elif document.lower().startswith("mcp:"):
route = ROUTE_MCP
document = _normalize_text(document[len("mcp:"):])
elif document.lower().startswith("knowledge:"):
route = ROUTE_KNOWLEDGE
document = _normalize_text(document[len("knowledge:"):])
elif word_count >= WORD_COUNT_SUMMARY_THRESHOLD:
route = ROUTE_SUMMARIZE
result = {
"route": route,
"document": document,
STATE_KEY_USER_INPUT: document,
"context_note": f"user={ctx.user_id} session={ctx.session_id}",
"word_count": len(document.split()) if document else word_count,
}
_log_node("decide", f"return={_format_value(result)}")
return result
def create_route_choice(available_routes: set[str]):
"""Create a routing function that falls back to preview for unavailable routes."""
def route_choice(state: DocumentState) -> str:
route = state.get("route", ROUTE_PREVIEW)
if route in available_routes:
return route
return ROUTE_PREVIEW
return route_choice
async def stream_preview(state: DocumentState, async_writer: AsyncEventWriter) -> Dict[str, Any]:
"""Stream a quick preview using AsyncEventWriter.
`async_writer` is awaitable and preserves write ordering by waiting for
stream consumption. Use `writer` for fire-and-forget/high-frequency text.
"""
document = _normalize_text(state.get("document", ""))
preview = document[:120]
_log_node("preview", f"args.document_len={len(document)}")
if preview:
await async_writer.write_text("[event_writer]: preview\n")
await async_writer.write_text(f"[event_writer]: {preview}\n")
result = {
"preview": preview,
}
_log_node("preview", f"return={_format_value(result)}")
return result
# ---------------------------------------------------------------------------
# MCP helper: prepare request arguments for the MCP node
# ---------------------------------------------------------------------------
async def prepare_mcp_request(state: DocumentState) -> Dict[str, Any]:
"""Parse the document text as MCP request arguments.
Expects JSON after the ``mcp:`` prefix (e.g. ``mcp: {"a": 3, "b": 5}``).
Falls back to ``{"input": <text>}`` if parsing fails.
"""
document = _normalize_text(state.get("document", ""))
_log_node("prepare_mcp_request", f"args.document={_truncate_text(document)}")
try:
args = json.loads(document)
if not isinstance(args, dict):
args = {"input": document}
except (json.JSONDecodeError, TypeError):
args = {"input": document}
_log_node("prepare_mcp_request", f"return.args={_format_value(args)}")
return {
STATE_KEY_NODE_RESPONSES: {
"prepare_mcp_request": args,
},
}
# ---------------------------------------------------------------------------
# Knowledge helper: resolve search query from state
# ---------------------------------------------------------------------------
def resolve_knowledge_query(state: DocumentState) -> str:
"""Return the document text as the knowledge search query."""
return _normalize_text(state.get("document", ""))
# ---------------------------------------------------------------------------
# Final output formatter
# ---------------------------------------------------------------------------
async def format_output(state: DocumentState) -> Dict[str, Any]:
"""Format the final output shown to the user."""
route = state.get("route", ROUTE_PREVIEW)
word_count = state.get("word_count", 0)
context_note = state.get("context_note", "")
node_responses = state.get(STATE_KEY_NODE_RESPONSES, {})
content = ""
if route == ROUTE_SUMMARIZE:
content = state.get(STATE_KEY_LAST_RESPONSE, "")
if not content:
content = node_responses.get("summarize", "")
elif route == ROUTE_TOOL:
tool_payload = state.get(STATE_KEY_LAST_TOOL_RESPONSE, "")
content = tool_payload or "(Tool did not return a result)"
elif route == ROUTE_SUBGRAPH:
content = state.get("subgraph_reply", "")
elif route == ROUTE_LLM_AGENT:
content = state.get("query_reply", "")
elif route == ROUTE_CODE:
content = node_responses.get("code_exec", "(Code execution did not return a result)")
elif route == ROUTE_MCP:
mcp_result = node_responses.get("mcp_call", "")
content = str(mcp_result) if mcp_result else "(MCP did not return a result)"
elif route == ROUTE_KNOWLEDGE:
knowledge_result = node_responses.get("knowledge_search", {})
if isinstance(knowledge_result, dict):
documents = knowledge_result.get("documents", [])
if documents:
doc_lines = []
for i, doc in enumerate(documents[:3], 1):
text = doc.get("text", "")[:120]
score = doc.get("score", 0.0)
doc_lines.append(f" [{i}] (score={score:.2f}) {text}")
content = f"Found {len(documents)} documents:\n" + "\n".join(doc_lines)
else:
content = "(No documents found)"
else:
content = str(knowledge_result)
else:
content = state.get("preview", "") or state.get("document", "")
if not content:
content = "(No content produced)"
execution_flow = _format_execution_flow(state.get("node_execution_history", []))
final_output = f"""
==============================
Graph Result
==============================
{content}
------------------------------
Processing
------------------------------
Route: {route}
Word Count: {word_count}
Context: {context_note}{execution_flow}
"""
result = {
STATE_KEY_LAST_RESPONSE: final_output.strip(),
}
_log_node(
"format_output",
f"return.last_response_len={len(result[STATE_KEY_LAST_RESPONSE])}",
)
return result
def _format_execution_flow(history: list[dict[str, Any]]) -> str:
if not history:
return ""
lines = ["Execution Flow:"]
for idx, entry in enumerate(history, start=1):
name = entry.get("node_name", entry.get("node_id", "unknown"))
node_type = entry.get("node_type", "")
duration = entry.get("execution_time", 0.0)
lines.append(f" {idx}. {name} ({node_type}) - {duration:.3f}s")
return "\n" + "\n".join(lines)