feat(reasoning): add inline think tag extraction and Anthropic thinking_blocks support

Add extract_think() and emit_incremental_think() helpers to extract thinking content from inline <think> and <thought> tags in the content field. This handles models served via Ollama, self-hosted vLLM, or other compatible endpoints that embed reasoning as inline tags instead of using the dedicated reasoning_content API field.

Also adds Anthropic thinking_blocks support for extended thinking via the thinking content blocks array.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
Flinn XieandSisyphus committed 2026-05-12 23:02:59 +08:00
1 parent 3a27af0018
commit 3a851f8f8d
5 files changed
+283 -4

No files matched your search

+8 -1
View File
@@ -101,17 +101,23 @@ class _LoopHook(AgentHook):
self._metadata = metadata or {}
self._session_key = session_key
self._stream_buf = ""
self._emitted_thinking = ""
def wants_streaming(self) -> bool:
return self._on_stream is not None
async def on_stream(self, context: AgentHookContext, delta: str) -> None:
from nanobot.utils.helpers import strip_think
from nanobot.utils.helpers import emit_incremental_think, strip_think
prev_clean = strip_think(self._stream_buf)
self._stream_buf += delta
new_clean = strip_think(self._stream_buf)
incremental = new_clean[len(prev_clean) :]
self._emitted_thinking = await emit_incremental_think(
self._stream_buf, self._emitted_thinking, self.emit_reasoning,
)
if incremental and self._on_stream:
await self._on_stream(incremental)
@@ -119,6 +125,7 @@ class _LoopHook(AgentHook):
if self._on_stream_end:
await self._on_stream_end(resuming=resuming)
self._stream_buf = ""
self._emitted_thinking = ""
async def before_iteration(self, context: AgentHookContext) -> None:
self._loop._current_iteration = context.iteration