-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path_langgraph_sync.py
More file actions
352 lines (302 loc) · 15.9 KB
/
Copy path_langgraph_sync.py
File metadata and controls
352 lines (302 loc) · 15.9 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""Sync LangGraph streaming helper for Agentex.
Converts LangGraph graph.astream() events into Agentex TaskMessageUpdate
events that are yielded back over the HTTP response. For use with sync ACP
agents that stream via HTTP yields rather than Redis.
Unified sync path
-----------------
Prefer using ``LangGraphTurn`` with ``UnifiedEmitter.yield_turn`` for new
agents, which adds usage capture and optional tracing via the shared harness
surface::
from agentex.lib.core.harness.emitter import UnifiedEmitter
from agentex.lib.adk._modules._langgraph_turn import LangGraphTurn
turn = LangGraphTurn(stream)
emitter = UnifiedEmitter(task_id=task_id, trace_id=trace_id, parent_span_id=span_id)
async for event in emitter.yield_turn(turn):
yield event
``convert_langgraph_to_agentex_events`` remains available as a lower-level
primitive (e.g. for callers that need the raw event stream without the
harness envelope).
"""
from __future__ import annotations
from typing import Any, Callable, Optional
from collections.abc import AsyncGenerator
async def convert_langgraph_to_agentex_events(
stream: Any,
on_final_ai_message: Optional[Callable[..., None]] = None,
) -> AsyncGenerator[Any, None]:
"""Convert LangGraph streaming events to Agentex TaskMessageUpdate events.
Expects the stream from graph.astream() called with
stream_mode=["messages", "updates"]. This produces two event types:
("messages", (message_chunk, metadata)) — token-by-token LLM output
("updates", {node_name: state_update}) — complete node outputs
Text tokens are streamed as Start/Delta/Done sequences.
Reasoning tokens are streamed as Start/Delta/Done sequences with ReasoningContentDelta.
Tool calls and tool results are emitted as Full messages.
Supports both regular models (chunk.content is a str) and reasoning models
like gpt-5/o1/o3 (chunk.content is a list of typed content blocks).
LangGraph emits tool requests as ``StreamTaskMessageFull`` (from "updates"
events), NOT Start+Delta+Done like pydantic-ai. No coalesce_tool_requests
option is needed for LangGraph.
Args:
stream: Async iterator from graph.astream(..., stream_mode=["messages", "updates"])
on_final_ai_message: Optional callback ``(msg: AIMessage) -> None`` called for
each ``AIMessage`` in an "agent" node update. Use this to capture
``usage_metadata`` for token accounting without re-traversing the stream.
The callback fires *after* all events for that message are yielded.
No-op when ``None`` (default).
Yields:
TaskMessageUpdate events (Start, Delta, Done, Full)
"""
# Lazy imports so langgraph/langchain aren't required at module load time
from langchain_core.messages import ToolMessage, AIMessageChunk
from agentex.types.text_content import TextContent
from agentex.types.reasoning_content import ReasoningContent
from agentex.types.task_message_delta import TextDelta
from agentex.types.task_message_update import (
StreamTaskMessageDone,
StreamTaskMessageFull,
StreamTaskMessageDelta,
StreamTaskMessageStart,
)
from agentex.types.tool_request_content import ToolRequestContent
from agentex.types.tool_response_content import ToolResponseContent
from agentex.types.reasoning_content_delta import ReasoningContentDelta
from agentex.types.reasoning_summary_delta import ReasoningSummaryDelta
message_index = 0
text_streaming = False
reasoning_streaming = False
reasoning_content_index = 0
async for event_type, event_data in stream:
if event_type == "messages":
chunk, metadata = event_data
if not isinstance(chunk, AIMessageChunk) or not chunk.content:
continue
# ----------------------------------------------------------
# Case 1: content is a plain string (regular models)
# ----------------------------------------------------------
if isinstance(chunk.content, str):
# Close reasoning stream if we're transitioning to text
if reasoning_streaming:
yield StreamTaskMessageDone(type="done", index=message_index)
reasoning_streaming = False
message_index += 1
if not text_streaming:
yield StreamTaskMessageStart(
type="start",
index=message_index,
content=TextContent(type="text", author="agent", content=""),
)
text_streaming = True
yield StreamTaskMessageDelta(
type="delta",
index=message_index,
delta=TextDelta(type="text", text_delta=chunk.content),
)
# ----------------------------------------------------------
# Case 2: content is a list of typed blocks (reasoning models)
# Responses API (responses/v1) format:
# {"type": "reasoning", "summary": [{"type": "summary_text", "text": "..."}]}
# {"type": "text", "text": "..."}
# ----------------------------------------------------------
elif isinstance(chunk.content, list):
for block in chunk.content:
if not isinstance(block, dict):
continue
block_type = block.get("type")
if block_type == "reasoning":
# Responses API: reasoning text is inside summary list
reasoning_text = ""
summaries = block.get("summary", [])
for s in summaries:
if isinstance(s, dict) and s.get("type") == "summary_text":
reasoning_text += s.get("text", "")
if not reasoning_text:
continue
# Close text stream if transitioning to reasoning
if text_streaming:
yield StreamTaskMessageDone(type="done", index=message_index)
text_streaming = False
message_index += 1
if not reasoning_streaming:
yield StreamTaskMessageStart(
type="start",
index=message_index,
content=ReasoningContent(
type="reasoning", author="agent", summary=[], content=[], style="active"
),
)
reasoning_streaming = True
reasoning_content_index = 0
yield StreamTaskMessageDelta(
type="delta",
index=message_index,
delta=ReasoningContentDelta(
type="reasoning_content",
content_index=reasoning_content_index,
content_delta=reasoning_text,
),
)
elif block_type == "text":
text_delta = block.get("text", "")
if not text_delta:
continue
# Close reasoning stream if transitioning to text
if reasoning_streaming:
yield StreamTaskMessageDone(type="done", index=message_index)
reasoning_streaming = False
reasoning_content_index += 1
message_index += 1
if not text_streaming:
yield StreamTaskMessageStart(
type="start",
index=message_index,
content=TextContent(type="text", author="agent", content=""),
)
text_streaming = True
yield StreamTaskMessageDelta(
type="delta",
index=message_index,
delta=TextDelta(type="text", text_delta=text_delta),
)
# ----------------------------------------------------------
# Reasoning summaries via additional_kwargs (OpenAI v0.3 format)
# ----------------------------------------------------------
additional_kwargs = getattr(chunk, "additional_kwargs", {})
reasoning_kw = additional_kwargs.get("reasoning")
if isinstance(reasoning_kw, dict):
summaries = reasoning_kw.get("summary", [])
for si, summary_item in enumerate(summaries):
if isinstance(summary_item, dict) and summary_item.get("type") == "summary_text":
summary_text = summary_item.get("text", "")
if summary_text:
yield StreamTaskMessageDelta(
type="delta",
index=message_index,
delta=ReasoningSummaryDelta(
type="reasoning_summary",
summary_index=si,
summary_delta=summary_text,
),
)
elif event_type == "updates":
for node_name, state_update in event_data.items():
if node_name == "agent":
messages = state_update.get("messages", [])
for msg in messages:
# Close any open streams
if text_streaming:
yield StreamTaskMessageDone(type="done", index=message_index)
text_streaming = False
message_index += 1
if reasoning_streaming:
yield StreamTaskMessageDone(type="done", index=message_index)
reasoning_streaming = False
message_index += 1
# Emit tool requests if the agent decided to call tools
if hasattr(msg, "tool_calls") and msg.tool_calls:
for tc in msg.tool_calls:
yield StreamTaskMessageFull(
type="full",
index=message_index,
content=ToolRequestContent(
tool_call_id=tc["id"],
name=tc["name"],
arguments=tc["args"],
author="agent",
),
)
message_index += 1
# Notify caller of the final AIMessage (e.g. for usage capture)
if on_final_ai_message is not None:
from langchain_core.messages import AIMessage as _AIMessage
if isinstance(msg, _AIMessage):
on_final_ai_message(msg)
elif node_name == "tools":
messages = state_update.get("messages", [])
for msg in messages:
if isinstance(msg, ToolMessage):
yield StreamTaskMessageFull(
type="full",
index=message_index,
content=ToolResponseContent(
tool_call_id=msg.tool_call_id,
name=msg.name or "unknown",
content=msg.content if isinstance(msg.content, str) else str(msg.content),
author="agent",
),
)
message_index += 1
# Close any remaining open streams
if text_streaming:
yield StreamTaskMessageDone(type="done", index=message_index)
if reasoning_streaming:
yield StreamTaskMessageDone(type="done", index=message_index)
async def emit_langgraph_messages(messages: list[Any], task_id: str) -> str:
"""Create Agentex messages for a list of LangGraph messages.
This is the non-streaming counterpart to ``stream_langgraph_events``. Use it
when you run a LangGraph graph with ``ainvoke`` (for example a Temporal-backed
agent using the LangGraph plugin, where streaming deltas aren't available) and
want to surface the resulting messages to the Agentex UI after the fact.
It maps LangGraph/LangChain message objects to Agentex content types:
- ``AIMessage`` tool calls -> ``ToolRequestContent`` (one per call)
- ``AIMessage`` text content -> ``TextContent``
- ``ToolMessage`` -> ``ToolResponseContent``
Pass only the messages produced this turn (e.g. ``messages[already_emitted:]``)
so each message is surfaced exactly once across a multi-turn conversation.
Args:
messages: LangGraph/LangChain message objects to surface — typically
the new messages a turn produced.
task_id: The Agentex task to create messages on.
Returns:
The last assistant text emitted (useful as a span/turn output), or "".
"""
# Lazy imports so langchain isn't required at module load time.
from langchain_core.messages import AIMessage, ToolMessage
from agentex.lib import adk
from agentex.types.text_content import TextContent
from agentex.types.tool_request_content import ToolRequestContent
from agentex.types.tool_response_content import ToolResponseContent
final_text = ""
for message in messages:
if isinstance(message, AIMessage):
for tool_call in message.tool_calls or []:
await adk.messages.create(
task_id=task_id,
content=ToolRequestContent(
author="agent",
tool_call_id=tool_call["id"],
name=tool_call["name"],
arguments=tool_call["args"],
),
)
# ``content`` may be a plain string (OpenAI) or a list of content
# blocks (Anthropic/Claude via LangChain, e.g.
# ``[{"type": "text", "text": "..."}]``). Extract and join the text
# so the response is visible regardless of the underlying model.
if isinstance(message.content, str):
text = message.content
else:
text = "".join(
block.get("text", "") if isinstance(block, dict) else str(block)
for block in message.content
if not isinstance(block, dict) or block.get("type") == "text"
)
if text:
final_text = text
await adk.messages.create(
task_id=task_id,
content=TextContent(author="agent", content=text, format="markdown"),
)
elif isinstance(message, ToolMessage):
await adk.messages.create(
task_id=task_id,
content=ToolResponseContent(
author="agent",
tool_call_id=message.tool_call_id,
name=message.name or "unknown",
content=message.content
if isinstance(message.content, str)
else str(message.content),
),
)
return final_text