-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path_langgraph_messages.py
More file actions
85 lines (73 loc) · 3.49 KB
/
Copy path_langgraph_messages.py
File metadata and controls
85 lines (73 loc) · 3.49 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
"""Emit finished LangGraph messages as Agentex task 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.
"""
from __future__ import annotations
from typing import Any
async def emit_langgraph_messages(messages: list[Any], task_id: str) -> str:
"""Create Agentex messages for a list of LangGraph messages.
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