forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreasoning_agent.py
More file actions
314 lines (291 loc) · 12.6 KB
/
Copy pathreasoning_agent.py
File metadata and controls
314 lines (291 loc) · 12.6 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
"""Reasoning agent — emits AG-UI REASONING_MESSAGE_* events.
Shared by:
- agentic-chat-reasoning (custom amber ReasoningBlock slot on the frontend)
- reasoning-default-render (CopilotKit's built-in reasoning slot)
Approach:
The Anthropic Python SDK supports Claude's extended-thinking ("thinking
budget") parameter on `messages.stream`, which streams `thinking_delta`
content blocks separately from text. We map those onto AG-UI's
REASONING_MESSAGE_* events. Models without extended-thinking fall back
to an inline ``<reasoning>...</reasoning>`` system-prompt convention that
this agent parses out of the text stream.
"""
from __future__ import annotations
import json
import os
import traceback
from collections.abc import AsyncIterator
from textwrap import dedent
from typing import Any
import anthropic
from ag_ui.core import (
EventType,
RunAgentInput,
RunFinishedEvent,
RunStartedEvent,
TextMessageContentEvent,
TextMessageEndEvent,
TextMessageStartEvent,
)
from ag_ui.encoder import EventEncoder
REASONING_SYSTEM_PROMPT = dedent("""
You are a helpful assistant. For each user question, FIRST emit a
short step-by-step plan inside `<reasoning>...</reasoning>` tags
(one or two short sentences per step, plain text only — no
Markdown, no JSON), THEN follow with the final concise answer for
the user OUTSIDE the tags.
The reasoning block is shown to the user as a visible "thinking"
chain — keep it brief and readable.
Example:
<reasoning>
The user is asking about X. I should consider Y and Z.
Combining both gives W.
</reasoning>
Final answer: W.
""").strip()
async def run_reasoning_agent(
input_data: RunAgentInput,
*,
system_prompt: str | None = None,
) -> AsyncIterator[str]:
"""Stream a reasoning-enabled assistant response as AG-UI events.
Splits Claude's response into REASONING_MESSAGE_* (everything inside
`<reasoning>...</reasoning>` tags) and TEXT_MESSAGE_* (everything
outside). Tags themselves are stripped before forwarding.
"""
encoder = EventEncoder()
client = anthropic.AsyncAnthropic(api_key=os.getenv("ANTHROPIC_API_KEY", ""))
messages: list[dict[str, Any]] = []
for msg in input_data.messages or []:
role = msg.role.value if hasattr(msg.role, "value") else str(msg.role)
if role not in ("user", "assistant"):
continue
raw_content = getattr(msg, "content", None)
content = ""
if isinstance(raw_content, str):
content = raw_content
elif isinstance(raw_content, list):
parts: list[str] = []
for part in raw_content:
if hasattr(part, "text"):
parts.append(part.text)
elif isinstance(part, dict) and "text" in part:
parts.append(part["text"])
content = "".join(parts)
if content:
messages.append({"role": role, "content": content})
thread_id = input_data.thread_id or "default"
run_id = input_data.run_id or "run-1"
yield encoder.encode(
RunStartedEvent(type=EventType.RUN_STARTED, thread_id=thread_id, run_id=run_id)
)
system = system_prompt or REASONING_SYSTEM_PROMPT
text_msg_id = f"msg-{run_id}-text"
reasoning_msg_id = f"msg-{run_id}-reasoning"
# State machine for parsing <reasoning>...</reasoning>
REASONING_OPEN = "<reasoning>"
REASONING_CLOSE = "</reasoning>"
in_reasoning = False
reasoning_started = False
text_started = False
buffer = ""
# Import lazily so missing imports don't crash module import-time
# on older ag_ui versions.
from ag_ui.core import (
ReasoningMessageContentEvent,
ReasoningMessageEndEvent,
ReasoningMessageStartEvent,
)
try:
async with client.messages.stream(
model=os.getenv("ANTHROPIC_MODEL", "claude-opus-4-5"),
max_tokens=2048,
system=system,
messages=messages,
) as stream:
async for event in stream:
etype = type(event).__name__
if etype != "RawContentBlockDeltaEvent":
continue
delta = event.delta # type: ignore[attr-defined]
if delta.type != "text_delta":
continue
buffer += delta.text
# Drain the buffer, emitting either reasoning or text
# whenever we have enough to safely classify.
while True:
if in_reasoning:
close_idx = buffer.find(REASONING_CLOSE)
if close_idx == -1:
# Hold last few chars in case the close tag is split.
keep = max(0, len(buffer) - len(REASONING_CLOSE))
chunk = buffer[:keep]
buffer = buffer[keep:]
if chunk:
if not reasoning_started:
yield encoder.encode(
ReasoningMessageStartEvent(
type=EventType.REASONING_MESSAGE_START,
message_id=reasoning_msg_id,
role="reasoning",
)
)
reasoning_started = True
yield encoder.encode(
ReasoningMessageContentEvent(
type=EventType.REASONING_MESSAGE_CONTENT,
message_id=reasoning_msg_id,
delta=chunk,
)
)
break
# Found close tag — emit remaining reasoning, end it, switch mode.
chunk = buffer[:close_idx]
if chunk:
if not reasoning_started:
yield encoder.encode(
ReasoningMessageStartEvent(
type=EventType.REASONING_MESSAGE_START,
message_id=reasoning_msg_id,
role="reasoning",
)
)
reasoning_started = True
yield encoder.encode(
ReasoningMessageContentEvent(
type=EventType.REASONING_MESSAGE_CONTENT,
message_id=reasoning_msg_id,
delta=chunk,
)
)
if reasoning_started:
yield encoder.encode(
ReasoningMessageEndEvent(
type=EventType.REASONING_MESSAGE_END,
message_id=reasoning_msg_id,
)
)
buffer = buffer[close_idx + len(REASONING_CLOSE) :]
in_reasoning = False
continue
else:
open_idx = buffer.find(REASONING_OPEN)
if open_idx == -1:
# No open tag in buffer — emit as text but hold tail in case tag is split.
keep = max(0, len(buffer) - len(REASONING_OPEN))
chunk = buffer[:keep]
buffer = buffer[keep:]
if chunk:
# Skip leading whitespace-only fragments before any reasoning
# so the empty assistant message doesn't appear.
if not text_started:
yield encoder.encode(
TextMessageStartEvent(
type=EventType.TEXT_MESSAGE_START,
message_id=text_msg_id,
role="assistant",
)
)
text_started = True
yield encoder.encode(
TextMessageContentEvent(
type=EventType.TEXT_MESSAGE_CONTENT,
message_id=text_msg_id,
delta=chunk,
)
)
break
# Found open tag — flush text up to it, switch to reasoning mode.
chunk = buffer[:open_idx]
if chunk:
if not text_started:
yield encoder.encode(
TextMessageStartEvent(
type=EventType.TEXT_MESSAGE_START,
message_id=text_msg_id,
role="assistant",
)
)
text_started = True
yield encoder.encode(
TextMessageContentEvent(
type=EventType.TEXT_MESSAGE_CONTENT,
message_id=text_msg_id,
delta=chunk,
)
)
buffer = buffer[open_idx + len(REASONING_OPEN) :]
in_reasoning = True
continue
# Flush any remaining buffered content as text.
if buffer:
if in_reasoning:
if not reasoning_started:
yield encoder.encode(
ReasoningMessageStartEvent(
type=EventType.REASONING_MESSAGE_START,
message_id=reasoning_msg_id,
role="reasoning",
)
)
reasoning_started = True
yield encoder.encode(
ReasoningMessageContentEvent(
type=EventType.REASONING_MESSAGE_CONTENT,
message_id=reasoning_msg_id,
delta=buffer,
)
)
yield encoder.encode(
ReasoningMessageEndEvent(
type=EventType.REASONING_MESSAGE_END,
message_id=reasoning_msg_id,
)
)
else:
if not text_started:
yield encoder.encode(
TextMessageStartEvent(
type=EventType.TEXT_MESSAGE_START,
message_id=text_msg_id,
role="assistant",
)
)
text_started = True
yield encoder.encode(
TextMessageContentEvent(
type=EventType.TEXT_MESSAGE_CONTENT,
message_id=text_msg_id,
delta=buffer,
)
)
except Exception:
err_text = f"Agent error: {traceback.format_exc()}"
if not text_started:
yield encoder.encode(
TextMessageStartEvent(
type=EventType.TEXT_MESSAGE_START,
message_id=text_msg_id,
role="assistant",
)
)
text_started = True
yield encoder.encode(
TextMessageContentEvent(
type=EventType.TEXT_MESSAGE_CONTENT,
message_id=text_msg_id,
delta=err_text,
)
)
if text_started:
yield encoder.encode(
TextMessageEndEvent(
type=EventType.TEXT_MESSAGE_END,
message_id=text_msg_id,
)
)
yield encoder.encode(
RunFinishedEvent(
type=EventType.RUN_FINISHED, thread_id=thread_id, run_id=run_id
)
)