feat(webui): stream live file edit events
This commit is contained in:
@@ -309,6 +309,100 @@ class TestToolEventProgress:
|
||||
await invoke_file_edit_progress(telegram_progress, edit_events)
|
||||
assert bus.outbound_size == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_goal_turn_keeps_live_file_edit_progress_for_webui(self, tmp_path: Path) -> None:
|
||||
"""The /goal command rewrites the prompt but must not bypass WebUI file-edit progress."""
|
||||
bus = MessageBus()
|
||||
provider = MagicMock()
|
||||
provider.supports_progress_deltas = True
|
||||
provider.get_default_model.return_value = "test-model"
|
||||
call_count = 0
|
||||
target = tmp_path / "goal.txt"
|
||||
|
||||
async def chat_stream_with_retry(*, on_tool_call_delta=None, **kwargs):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
assert on_tool_call_delta is not None
|
||||
await on_tool_call_delta({
|
||||
"index": 0,
|
||||
"call_id": "call-goal-write",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"goal.txt","content":"',
|
||||
})
|
||||
await on_tool_call_delta({
|
||||
"index": 0,
|
||||
"arguments_delta": "one\\ntwo\\nthree\\n",
|
||||
})
|
||||
await on_tool_call_delta({"index": 0, "arguments_delta": '"}'})
|
||||
return LLMResponse(
|
||||
content=None,
|
||||
tool_calls=[
|
||||
ToolCallRequest(
|
||||
id="call-goal-write",
|
||||
name="write_file",
|
||||
arguments={
|
||||
"path": "goal.txt",
|
||||
"content": "one\ntwo\nthree\n",
|
||||
},
|
||||
)
|
||||
],
|
||||
usage={},
|
||||
)
|
||||
return LLMResponse(content="Done", tool_calls=[], usage={})
|
||||
|
||||
async def execute(name: str, params: dict) -> str:
|
||||
assert name == "write_file"
|
||||
target.write_text(params["content"], encoding="utf-8")
|
||||
return "ok"
|
||||
|
||||
provider.chat_stream_with_retry = chat_stream_with_retry
|
||||
provider.chat_with_retry = AsyncMock()
|
||||
loop = AgentLoop(bus=bus, provider=provider, workspace=tmp_path, model="test-model")
|
||||
loop.tools.get_definitions = MagicMock(return_value=[
|
||||
{"type": "function", "function": {"name": "write_file"}},
|
||||
])
|
||||
loop.tools.prepare_call = MagicMock(
|
||||
return_value=(
|
||||
None,
|
||||
{"path": "goal.txt", "content": "one\ntwo\nthree\n"},
|
||||
None,
|
||||
),
|
||||
)
|
||||
loop.tools.execute = AsyncMock(side_effect=execute)
|
||||
loop.consolidator.maybe_consolidate_by_tokens = AsyncMock(return_value=False) # type: ignore[method-assign]
|
||||
|
||||
await loop._dispatch(InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="u1",
|
||||
chat_id="chat1",
|
||||
content="/goal create goal file",
|
||||
metadata={"_wants_stream": True},
|
||||
))
|
||||
|
||||
outbound = []
|
||||
while bus.outbound_size > 0:
|
||||
outbound.append(await bus.consume_outbound())
|
||||
|
||||
edit_events = [
|
||||
event
|
||||
for msg in outbound
|
||||
for event in msg.metadata.get("_file_edit_events", [])
|
||||
]
|
||||
assert any(
|
||||
event["status"] == "editing"
|
||||
and event["approximate"]
|
||||
and event["added"] == 3
|
||||
for event in edit_events
|
||||
)
|
||||
assert any(
|
||||
event["status"] == "done"
|
||||
and not event["approximate"]
|
||||
and event["added"] == 3
|
||||
for event in edit_events
|
||||
)
|
||||
provider.chat_with_retry.assert_not_awaited()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_streaming_channel_does_not_publish_codex_progress_deltas(
|
||||
self,
|
||||
|
||||
@@ -6,7 +6,7 @@ import pytest
|
||||
|
||||
from nanobot.agent.runner import AgentRunner, AgentRunSpec
|
||||
from nanobot.config.schema import AgentDefaults
|
||||
from nanobot.providers.base import LLMResponse
|
||||
from nanobot.providers.base import LLMResponse, ToolCallRequest
|
||||
|
||||
_MAX_TOOL_RESULT_CHARS = AgentDefaults().max_tool_result_chars
|
||||
|
||||
@@ -77,3 +77,220 @@ async def test_runner_streams_provider_progress_deltas_by_default():
|
||||
assert result.final_content == "hello"
|
||||
assert [call.args[0] for call in progress_cb.await_args_list] == ["he", "llo"]
|
||||
provider.chat_with_retry.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_runner_streams_live_write_file_activity_from_tool_argument_deltas(tmp_path):
|
||||
provider = MagicMock()
|
||||
provider.supports_progress_deltas = True
|
||||
call_count = 0
|
||||
progress_events: list[dict] = []
|
||||
|
||||
async def progress_cb(content, *, file_edit_events=None, **kwargs):
|
||||
if file_edit_events:
|
||||
progress_events.extend(file_edit_events)
|
||||
|
||||
class Tools:
|
||||
def get_definitions(self):
|
||||
return [{"type": "function", "function": {"name": "write_file"}}]
|
||||
|
||||
def get(self, name):
|
||||
return None
|
||||
|
||||
async def execute(self, name, params):
|
||||
assert name == "write_file"
|
||||
assert any(event["approximate"] and event["added"] == 24 for event in progress_events)
|
||||
target = tmp_path / params["path"]
|
||||
target.write_text(params["content"], encoding="utf-8")
|
||||
return "ok"
|
||||
|
||||
async def chat_stream_with_retry(*, on_tool_call_delta=None, **kwargs):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
assert on_tool_call_delta is not None
|
||||
await on_tool_call_delta({
|
||||
"index": 0,
|
||||
"call_id": "call-write",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"big.txt","content":"',
|
||||
})
|
||||
await on_tool_call_delta({"index": 0, "arguments_delta": "line\\n" * 24})
|
||||
return LLMResponse(
|
||||
content=None,
|
||||
tool_calls=[
|
||||
ToolCallRequest(
|
||||
id="call-write",
|
||||
name="write_file",
|
||||
arguments={"path": "big.txt", "content": "line\n" * 24},
|
||||
)
|
||||
],
|
||||
usage={},
|
||||
)
|
||||
return LLMResponse(content="done", tool_calls=[], usage={})
|
||||
|
||||
provider.chat_stream_with_retry = chat_stream_with_retry
|
||||
provider.chat_with_retry = AsyncMock()
|
||||
|
||||
runner = AgentRunner(provider)
|
||||
result = await runner.run(AgentRunSpec(
|
||||
initial_messages=[{"role": "user", "content": "write a large file"}],
|
||||
tools=Tools(),
|
||||
model="test-model",
|
||||
max_iterations=2,
|
||||
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
|
||||
progress_callback=progress_cb,
|
||||
workspace=tmp_path,
|
||||
))
|
||||
|
||||
assert result.final_content == "done"
|
||||
assert any(event["approximate"] and event["added"] == 24 for event in progress_events)
|
||||
assert any(
|
||||
not event["approximate"] and event["phase"] == "end" and event["added"] == 24
|
||||
for event in progress_events
|
||||
)
|
||||
provider.chat_with_retry.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_runner_streams_live_edit_file_activity_from_tool_argument_deltas(tmp_path):
|
||||
provider = MagicMock()
|
||||
provider.supports_progress_deltas = True
|
||||
call_count = 0
|
||||
progress_events: list[dict] = []
|
||||
target = tmp_path / "notes.txt"
|
||||
target.write_text("old\nkeep\n", encoding="utf-8")
|
||||
|
||||
async def progress_cb(content, *, file_edit_events=None, **kwargs):
|
||||
if file_edit_events:
|
||||
progress_events.extend(file_edit_events)
|
||||
|
||||
class Tools:
|
||||
def get_definitions(self):
|
||||
return [{"type": "function", "function": {"name": "edit_file"}}]
|
||||
|
||||
def get(self, name):
|
||||
return None
|
||||
|
||||
async def execute(self, name, params):
|
||||
assert name == "edit_file"
|
||||
assert any(
|
||||
event["tool"] == "edit_file"
|
||||
and event["approximate"]
|
||||
and event["added"] == 3
|
||||
and event["deleted"] == 2
|
||||
for event in progress_events
|
||||
)
|
||||
target.write_text(params["new_text"], encoding="utf-8")
|
||||
return "ok"
|
||||
|
||||
async def chat_stream_with_retry(*, on_tool_call_delta=None, **kwargs):
|
||||
nonlocal call_count
|
||||
call_count += 1
|
||||
if call_count == 1:
|
||||
assert on_tool_call_delta is not None
|
||||
await on_tool_call_delta({
|
||||
"index": 0,
|
||||
"call_id": "call-edit",
|
||||
"name": "edit_file",
|
||||
"arguments_delta": (
|
||||
'{"path":"notes.txt","old_text":"old\\nkeep\\n","new_text":"'
|
||||
),
|
||||
})
|
||||
await on_tool_call_delta({
|
||||
"index": 0,
|
||||
"arguments_delta": "new\\nkeep\\nextra\\n",
|
||||
})
|
||||
await on_tool_call_delta({"index": 0, "arguments_delta": '"}'})
|
||||
return LLMResponse(
|
||||
content=None,
|
||||
tool_calls=[
|
||||
ToolCallRequest(
|
||||
id="call-edit",
|
||||
name="edit_file",
|
||||
arguments={
|
||||
"path": "notes.txt",
|
||||
"old_text": "old\nkeep\n",
|
||||
"new_text": "new\nkeep\nextra\n",
|
||||
},
|
||||
)
|
||||
],
|
||||
usage={},
|
||||
)
|
||||
return LLMResponse(content="done", tool_calls=[], usage={})
|
||||
|
||||
provider.chat_stream_with_retry = chat_stream_with_retry
|
||||
provider.chat_with_retry = AsyncMock()
|
||||
|
||||
runner = AgentRunner(provider)
|
||||
result = await runner.run(AgentRunSpec(
|
||||
initial_messages=[{"role": "user", "content": "edit a file"}],
|
||||
tools=Tools(),
|
||||
model="test-model",
|
||||
max_iterations=2,
|
||||
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
|
||||
progress_callback=progress_cb,
|
||||
workspace=tmp_path,
|
||||
))
|
||||
|
||||
assert result.final_content == "done"
|
||||
assert any(
|
||||
event["tool"] == "edit_file"
|
||||
and event["approximate"]
|
||||
and event["added"] == 3
|
||||
and event["deleted"] == 2
|
||||
for event in progress_events
|
||||
)
|
||||
assert any(
|
||||
event["tool"] == "edit_file"
|
||||
and not event["approximate"]
|
||||
and event["phase"] == "end"
|
||||
and event["added"] == 2
|
||||
and event["deleted"] == 1
|
||||
for event in progress_events
|
||||
)
|
||||
provider.chat_with_retry.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_runner_marks_unfinished_live_write_file_activity_failed(tmp_path):
|
||||
provider = MagicMock()
|
||||
provider.supports_progress_deltas = True
|
||||
progress_events: list[dict] = []
|
||||
|
||||
async def progress_cb(content, *, file_edit_events=None, **kwargs):
|
||||
if file_edit_events:
|
||||
progress_events.extend(file_edit_events)
|
||||
|
||||
async def chat_stream_with_retry(*, on_tool_call_delta=None, **kwargs):
|
||||
assert on_tool_call_delta is not None
|
||||
await on_tool_call_delta({
|
||||
"index": 0,
|
||||
"call_id": "call-write",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"aborted.txt","content":"partial\\n',
|
||||
})
|
||||
return LLMResponse(content="stopped", tool_calls=[], finish_reason="stop", usage={})
|
||||
|
||||
provider.chat_stream_with_retry = chat_stream_with_retry
|
||||
provider.chat_with_retry = AsyncMock()
|
||||
tools = MagicMock()
|
||||
tools.get_definitions.return_value = [{"type": "function", "function": {"name": "write_file"}}]
|
||||
tools.get.return_value = None
|
||||
|
||||
runner = AgentRunner(provider)
|
||||
result = await runner.run(AgentRunSpec(
|
||||
initial_messages=[{"role": "user", "content": "write a large file"}],
|
||||
tools=tools,
|
||||
model="test-model",
|
||||
max_iterations=1,
|
||||
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
|
||||
progress_callback=progress_cb,
|
||||
workspace=tmp_path,
|
||||
))
|
||||
|
||||
assert result.final_content == "stopped"
|
||||
assert progress_events[-1]["path"] == "aborted.txt"
|
||||
assert progress_events[-1]["phase"] == "error"
|
||||
assert progress_events[-1]["status"] == "error"
|
||||
provider.chat_with_retry.assert_not_awaited()
|
||||
|
||||
@@ -129,6 +129,74 @@ async def test_chat_stream_invokes_on_thinking_delta_for_thinking_delta() -> Non
|
||||
assert text_parts == ["X"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_chat_stream_invokes_tool_call_delta_for_input_json_delta() -> None:
|
||||
provider = AnthropicProvider(api_key="sk-test")
|
||||
provider._client = MagicMock()
|
||||
|
||||
chunks = [
|
||||
SimpleNamespace(
|
||||
type="content_block_start",
|
||||
index=1,
|
||||
content_block=SimpleNamespace(
|
||||
type="tool_use",
|
||||
id="toolu_1",
|
||||
name="write_file",
|
||||
),
|
||||
),
|
||||
SimpleNamespace(
|
||||
type="content_block_delta",
|
||||
index=1,
|
||||
delta=SimpleNamespace(
|
||||
type="input_json_delta",
|
||||
partial_json='{"path":"notes.md","content":"',
|
||||
),
|
||||
),
|
||||
SimpleNamespace(
|
||||
type="content_block_delta",
|
||||
index=1,
|
||||
delta=SimpleNamespace(type="input_json_delta", partial_json="line\\n"),
|
||||
),
|
||||
]
|
||||
fake = _FakeAsyncStream(chunks)
|
||||
stream_cm = MagicMock()
|
||||
stream_cm.__aenter__ = AsyncMock(return_value=fake)
|
||||
stream_cm.__aexit__ = AsyncMock(return_value=None)
|
||||
provider._client.messages.stream = MagicMock(return_value=stream_cm)
|
||||
|
||||
deltas: list[dict] = []
|
||||
|
||||
async def on_tool_delta(delta: dict) -> None:
|
||||
deltas.append(delta)
|
||||
|
||||
await provider.chat_stream(
|
||||
messages=[{"role": "user", "content": "write"}],
|
||||
on_tool_call_delta=on_tool_delta,
|
||||
)
|
||||
|
||||
assert deltas == [
|
||||
{
|
||||
"index": 1,
|
||||
"call_id": "toolu_1",
|
||||
"name": "write_file",
|
||||
"arguments_delta": "",
|
||||
},
|
||||
{
|
||||
"index": 1,
|
||||
"call_id": "toolu_1",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"notes.md","content":"',
|
||||
},
|
||||
{
|
||||
"index": 1,
|
||||
"call_id": "toolu_1",
|
||||
"name": "write_file",
|
||||
"arguments_delta": "line\\n",
|
||||
},
|
||||
]
|
||||
fake.get_final_message.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_chat_stream_without_callback_still_finalizes() -> None:
|
||||
provider = AnthropicProvider(api_key="sk-test")
|
||||
|
||||
@@ -164,6 +164,130 @@ def _fake_chat_stream_reasoning_chunks():
|
||||
return _stream()
|
||||
|
||||
|
||||
def _fake_chat_stream_tool_call_chunks():
|
||||
"""Mimic OpenAI-compatible streaming tool-call argument deltas."""
|
||||
|
||||
async def _stream():
|
||||
yield SimpleNamespace(
|
||||
choices=[
|
||||
SimpleNamespace(
|
||||
finish_reason=None,
|
||||
delta=SimpleNamespace(
|
||||
content=None,
|
||||
reasoning_content=None,
|
||||
reasoning=None,
|
||||
tool_calls=[
|
||||
SimpleNamespace(
|
||||
index=0,
|
||||
id="call_write",
|
||||
function=SimpleNamespace(
|
||||
name="write_file",
|
||||
arguments='{"path":"notes.md","content":"',
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
usage=None,
|
||||
)
|
||||
yield SimpleNamespace(
|
||||
choices=[
|
||||
SimpleNamespace(
|
||||
finish_reason=None,
|
||||
delta=SimpleNamespace(
|
||||
content=None,
|
||||
reasoning_content=None,
|
||||
reasoning=None,
|
||||
tool_calls=[
|
||||
SimpleNamespace(
|
||||
index=0,
|
||||
id=None,
|
||||
function=SimpleNamespace(name=None, arguments='line\\n"}'),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
usage=None,
|
||||
)
|
||||
yield SimpleNamespace(
|
||||
choices=[
|
||||
SimpleNamespace(
|
||||
finish_reason="tool_calls",
|
||||
delta=SimpleNamespace(
|
||||
content=None,
|
||||
reasoning_content=None,
|
||||
reasoning=None,
|
||||
tool_calls=None,
|
||||
),
|
||||
),
|
||||
],
|
||||
usage=SimpleNamespace(prompt_tokens=10, completion_tokens=5, total_tokens=15),
|
||||
)
|
||||
|
||||
return _stream()
|
||||
|
||||
|
||||
def _fake_chat_stream_legacy_function_call_chunks():
|
||||
"""Mimic older OpenAI-compatible ``delta.function_call`` chunks."""
|
||||
|
||||
async def _stream():
|
||||
yield SimpleNamespace(
|
||||
choices=[
|
||||
SimpleNamespace(
|
||||
finish_reason=None,
|
||||
delta=SimpleNamespace(
|
||||
content=None,
|
||||
reasoning_content=None,
|
||||
reasoning=None,
|
||||
tool_calls=None,
|
||||
function_call=SimpleNamespace(
|
||||
name="write_file",
|
||||
arguments='{"path":"notes.md","content":"',
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
usage=None,
|
||||
)
|
||||
yield SimpleNamespace(
|
||||
choices=[
|
||||
SimpleNamespace(
|
||||
finish_reason=None,
|
||||
delta=SimpleNamespace(
|
||||
content=None,
|
||||
reasoning_content=None,
|
||||
reasoning=None,
|
||||
tool_calls=None,
|
||||
function_call=SimpleNamespace(
|
||||
name=None,
|
||||
arguments='line\\n"}',
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
usage=None,
|
||||
)
|
||||
yield SimpleNamespace(
|
||||
choices=[
|
||||
SimpleNamespace(
|
||||
finish_reason="function_call",
|
||||
delta=SimpleNamespace(
|
||||
content=None,
|
||||
reasoning_content=None,
|
||||
reasoning=None,
|
||||
tool_calls=None,
|
||||
function_call=None,
|
||||
),
|
||||
),
|
||||
],
|
||||
usage=SimpleNamespace(prompt_tokens=10, completion_tokens=5, total_tokens=15),
|
||||
)
|
||||
|
||||
return _stream()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_openai_compat_stream_forwards_reasoning_deltas_deepseek_style() -> None:
|
||||
"""Regression: DeepSeek-V4 / reasoner expose ``delta.reasoning_content`` during streaming."""
|
||||
@@ -202,6 +326,98 @@ async def test_openai_compat_stream_forwards_reasoning_deltas_deepseek_style() -
|
||||
mock_chat.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("provider_name", "model"),
|
||||
[
|
||||
("openai", "gpt-4o"),
|
||||
("deepseek", "deepseek-chat"),
|
||||
("minimax", "MiniMax-M2.7"),
|
||||
("zhipu", "glm-4.6"),
|
||||
],
|
||||
)
|
||||
async def test_openai_compat_stream_forwards_tool_call_argument_deltas(
|
||||
provider_name: str,
|
||||
model: str,
|
||||
) -> None:
|
||||
mock_chat = AsyncMock(return_value=_fake_chat_stream_tool_call_chunks())
|
||||
spec = find_by_name(provider_name)
|
||||
deltas: list[dict] = []
|
||||
|
||||
async def on_tool_delta(delta: dict) -> None:
|
||||
deltas.append(delta)
|
||||
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI") as mock_openai:
|
||||
client_instance = mock_openai.return_value
|
||||
client_instance.chat.completions.create = mock_chat
|
||||
|
||||
provider = OpenAICompatProvider(
|
||||
api_key="sk-test",
|
||||
default_model=model,
|
||||
spec=spec,
|
||||
)
|
||||
result = await provider.chat_stream(
|
||||
messages=[{"role": "user", "content": "write"}],
|
||||
tools=[{"type": "function", "function": {"name": "write_file"}}],
|
||||
model=model,
|
||||
on_tool_call_delta=on_tool_delta,
|
||||
)
|
||||
|
||||
assert deltas == [
|
||||
{
|
||||
"index": 0,
|
||||
"call_id": "call_write",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"notes.md","content":"',
|
||||
},
|
||||
{"index": 0, "call_id": "", "name": "", "arguments_delta": 'line\\n"}'},
|
||||
]
|
||||
assert result.tool_calls[0].name == "write_file"
|
||||
assert result.tool_calls[0].arguments == {"path": "notes.md", "content": "line\n"}
|
||||
kwargs = mock_chat.await_args.kwargs
|
||||
if provider_name == "zhipu":
|
||||
assert kwargs["extra_body"]["tool_stream"] is True
|
||||
else:
|
||||
assert kwargs.get("extra_body", {}).get("tool_stream") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_openai_compat_stream_forwards_legacy_function_call_argument_deltas() -> None:
|
||||
mock_chat = AsyncMock(return_value=_fake_chat_stream_legacy_function_call_chunks())
|
||||
deltas: list[dict] = []
|
||||
|
||||
async def on_tool_delta(delta: dict) -> None:
|
||||
deltas.append(delta)
|
||||
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI") as mock_openai:
|
||||
client_instance = mock_openai.return_value
|
||||
client_instance.chat.completions.create = mock_chat
|
||||
|
||||
provider = OpenAICompatProvider(
|
||||
api_key="sk-test",
|
||||
default_model="deepseek-chat",
|
||||
spec=find_by_name("deepseek"),
|
||||
)
|
||||
result = await provider.chat_stream(
|
||||
messages=[{"role": "user", "content": "write"}],
|
||||
tools=[{"type": "function", "function": {"name": "write_file"}}],
|
||||
model="deepseek-chat",
|
||||
on_tool_call_delta=on_tool_delta,
|
||||
)
|
||||
|
||||
assert deltas == [
|
||||
{
|
||||
"index": 0,
|
||||
"call_id": "",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"notes.md","content":"',
|
||||
},
|
||||
{"index": 0, "call_id": "", "name": "", "arguments_delta": 'line\\n"}'},
|
||||
]
|
||||
assert result.tool_calls[0].name == "write_file"
|
||||
assert result.tool_calls[0].arguments == {"path": "notes.md", "content": "line\n"}
|
||||
|
||||
|
||||
class _FakeResponsesError(Exception):
|
||||
def __init__(self, status_code: int, text: str):
|
||||
super().__init__(text)
|
||||
|
||||
@@ -44,9 +44,15 @@ class TestShouldExecuteTools:
|
||||
resp = _response("stop")
|
||||
assert resp.should_execute_tools is True
|
||||
|
||||
def test_legacy_function_call_reason_executes(self) -> None:
|
||||
# Older OpenAI-compatible streaming APIs can still use the singular
|
||||
# function_call finish reason while carrying a tool-call-shaped payload.
|
||||
resp = _response("function_call")
|
||||
assert resp.should_execute_tools is True
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"anomalous_reason",
|
||||
["refusal", "content_filter", "error", "length", "function_call", ""],
|
||||
["refusal", "content_filter", "error", "length", ""],
|
||||
)
|
||||
def test_tool_calls_under_anomalous_reason_blocked(self, anomalous_reason: str) -> None:
|
||||
# This is the #3220 bug: gateways injecting tool_calls under any of these
|
||||
|
||||
@@ -453,6 +453,56 @@ class TestConsumeSdkStream:
|
||||
assert tool_calls[0].name == "get_weather"
|
||||
assert tool_calls[0].arguments == {"city": "SF"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_tool_call_argument_delta_callback(self):
|
||||
item_added = MagicMock(type="function_call", call_id="c1", id="fc1", arguments="")
|
||||
item_added.name = "write_file"
|
||||
ev1 = MagicMock(type="response.output_item.added", item=item_added)
|
||||
ev2 = MagicMock(
|
||||
type="response.function_call_arguments.delta",
|
||||
call_id="c1",
|
||||
delta='{"path":"a.txt","content":"',
|
||||
)
|
||||
ev3 = MagicMock(
|
||||
type="response.function_call_arguments.delta",
|
||||
call_id="c1",
|
||||
delta='hello\\n',
|
||||
)
|
||||
ev4 = MagicMock(
|
||||
type="response.function_call_arguments.done",
|
||||
call_id="c1",
|
||||
arguments='{"path":"a.txt","content":"hello\\n"}',
|
||||
)
|
||||
item_done = MagicMock(
|
||||
type="function_call",
|
||||
call_id="c1",
|
||||
id="fc1",
|
||||
arguments='{"path":"a.txt","content":"hello\\n"}',
|
||||
)
|
||||
item_done.name = "write_file"
|
||||
ev5 = MagicMock(type="response.output_item.done", item=item_done)
|
||||
resp_obj = MagicMock(status="completed", usage=None, output=[])
|
||||
ev6 = MagicMock(type="response.completed", response=resp_obj)
|
||||
deltas: list[dict] = []
|
||||
|
||||
async def cb(delta: dict) -> None:
|
||||
deltas.append(delta)
|
||||
|
||||
async def stream():
|
||||
for e in [ev1, ev2, ev3, ev4, ev5, ev6]:
|
||||
yield e
|
||||
|
||||
await consume_sdk_stream(stream(), on_tool_call_delta=cb)
|
||||
assert deltas == [
|
||||
{"call_id": "c1", "name": "write_file", "arguments_delta": ""},
|
||||
{
|
||||
"call_id": "c1",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"a.txt","content":"',
|
||||
},
|
||||
{"call_id": "c1", "name": "write_file", "arguments_delta": "hello\\n"},
|
||||
]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_usage_extracted(self):
|
||||
usage_obj = MagicMock(input_tokens=10, output_tokens=5, total_tokens=15)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
from nanobot.utils.file_edit_events import (
|
||||
build_file_edit_end_event,
|
||||
@@ -8,6 +10,7 @@ from nanobot.utils.file_edit_events import (
|
||||
line_diff_stats,
|
||||
prepare_file_edit_tracker,
|
||||
read_file_snapshot,
|
||||
StreamingFileEditTracker,
|
||||
)
|
||||
|
||||
|
||||
@@ -20,6 +23,10 @@ def test_line_diff_stats_normalizes_crlf() -> None:
|
||||
assert line_diff_stats("a\r\nb\r\n", "a\nb\nc\n") == (1, 0)
|
||||
|
||||
|
||||
def test_line_diff_stats_counts_new_file_crlf_lines_once() -> None:
|
||||
assert line_diff_stats("", "a\r\nb\r\n") == (2, 0)
|
||||
|
||||
|
||||
def test_write_file_start_predicts_and_end_calibrates_exact_diff(tmp_path: Path) -> None:
|
||||
target = tmp_path / "notes.txt"
|
||||
target.write_text("old\nkeep\n", encoding="utf-8")
|
||||
@@ -39,6 +46,7 @@ def test_write_file_start_predicts_and_end_calibrates_exact_diff(tmp_path: Path)
|
||||
"call_id": "call-write",
|
||||
"tool": "write_file",
|
||||
"path": "notes.txt",
|
||||
"absolute_path": (tmp_path / "notes.txt").as_posix(),
|
||||
"phase": "start",
|
||||
"added": 2,
|
||||
"deleted": 1,
|
||||
@@ -73,6 +81,307 @@ def test_binary_file_is_reported_but_not_counted(tmp_path: Path) -> None:
|
||||
assert (event["added"], event["deleted"]) == (0, 0)
|
||||
|
||||
|
||||
def test_oversized_write_file_end_uses_known_content_for_exact_count(tmp_path: Path) -> None:
|
||||
target = tmp_path / "large.txt"
|
||||
params = {"path": "large.txt", "content": "x" * (2 * 1024 * 1024 + 1)}
|
||||
tracker = prepare_file_edit_tracker(
|
||||
call_id="call-large",
|
||||
tool_name="write_file",
|
||||
tool=None,
|
||||
workspace=tmp_path,
|
||||
params=params,
|
||||
)
|
||||
|
||||
assert tracker is not None
|
||||
target.write_text(params["content"], encoding="utf-8")
|
||||
event = build_file_edit_end_event(tracker, params)
|
||||
assert event.get("binary") is not True
|
||||
assert event["added"] == 1
|
||||
assert event["deleted"] == 0
|
||||
|
||||
|
||||
def test_streaming_write_file_tracker_emits_live_line_counts(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-live",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"notes.md","content":"',
|
||||
})
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"arguments_delta": "line\\n" * 24,
|
||||
})
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
assert events[0] == {
|
||||
"version": 1,
|
||||
"call_id": "call-live",
|
||||
"tool": "write_file",
|
||||
"path": "notes.md",
|
||||
"absolute_path": (tmp_path / "notes.md").as_posix(),
|
||||
"phase": "start",
|
||||
"added": 0,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
}
|
||||
assert events[-1]["path"] == "notes.md"
|
||||
assert events[-1]["status"] == "editing"
|
||||
assert events[-1]["approximate"] is True
|
||||
assert events[-1]["added"] == 24
|
||||
assert events[-1]["deleted"] == 0
|
||||
|
||||
|
||||
def test_streaming_write_file_tracker_emits_pending_before_path(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-live",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"content":"line\\n',
|
||||
})
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"arguments_delta": 'more\\n","path":"late.md"',
|
||||
})
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
assert events[0] == {
|
||||
"version": 1,
|
||||
"call_id": "call-live",
|
||||
"tool": "write_file",
|
||||
"path": "",
|
||||
"phase": "start",
|
||||
"added": 1,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
"pending": True,
|
||||
}
|
||||
assert events[-1]["path"] == "late.md"
|
||||
assert events[-1].get("pending") is not True
|
||||
assert events[-1]["added"] == 2
|
||||
|
||||
|
||||
def test_streaming_write_file_tracker_flushes_small_pending_count(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-live",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"small.md","content":"one\\n',
|
||||
})
|
||||
await tracker.flush()
|
||||
|
||||
asyncio.run(run())
|
||||
assert events
|
||||
assert events[-1]["path"] == "small.md"
|
||||
assert events[-1]["added"] == 1
|
||||
|
||||
|
||||
def test_streaming_write_file_tracker_normalizes_crlf_line_counts(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-live",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"windows.txt","content":"one\\r\\ntwo\\r\\n',
|
||||
})
|
||||
await tracker.flush()
|
||||
|
||||
asyncio.run(run())
|
||||
assert events[-1]["path"] == "windows.txt"
|
||||
assert events[-1]["added"] == 2
|
||||
|
||||
|
||||
def test_streaming_write_file_tracker_counts_unicode_escaped_newlines(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-live",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"unicode.txt","content":"one\\u000atwo',
|
||||
})
|
||||
await tracker.flush()
|
||||
|
||||
asyncio.run(run())
|
||||
assert events[-1]["path"] == "unicode.txt"
|
||||
assert events[-1]["added"] == 2
|
||||
|
||||
|
||||
def test_streaming_edit_file_tracker_emits_live_line_counts(tmp_path: Path) -> None:
|
||||
target = tmp_path / "notes.md"
|
||||
target.write_text("old\nkeep\n", encoding="utf-8")
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-edit",
|
||||
"name": "edit_file",
|
||||
"arguments_delta": '{"path":"notes.md","old_text":"old\\nkeep","new_text":"',
|
||||
})
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"arguments_delta": "new\\nkeep\\nextra\\n" * 8,
|
||||
})
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
assert events[0] == {
|
||||
"version": 1,
|
||||
"call_id": "call-edit",
|
||||
"tool": "edit_file",
|
||||
"path": "notes.md",
|
||||
"absolute_path": (tmp_path / "notes.md").as_posix(),
|
||||
"phase": "start",
|
||||
"added": 0,
|
||||
"deleted": 2,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
}
|
||||
assert events[-1]["path"] == "notes.md"
|
||||
assert events[-1]["status"] == "editing"
|
||||
assert events[-1]["approximate"] is True
|
||||
assert events[-1]["added"] == 24
|
||||
assert events[-1]["deleted"] == 2
|
||||
|
||||
|
||||
def test_streaming_tracker_applies_canonical_call_id_to_final_tool(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"matched.md","content":"one\\n',
|
||||
})
|
||||
final = SimpleNamespace(
|
||||
id="provider-final-id",
|
||||
name="write_file",
|
||||
arguments={"path": "matched.md", "content": "one\n"},
|
||||
)
|
||||
tracker.apply_final_call_ids([final])
|
||||
assert final.id == "idx:0"
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_streaming_edit_file_tracker_flushes_small_pending_count(tmp_path: Path) -> None:
|
||||
target = tmp_path / "small.py"
|
||||
target.write_text("old\n", encoding="utf-8")
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-edit",
|
||||
"name": "edit_file",
|
||||
"arguments_delta": '{"path":"small.py","old_text":"old\\n","new_text":"new\\nextra',
|
||||
})
|
||||
await tracker.flush()
|
||||
|
||||
asyncio.run(run())
|
||||
assert events
|
||||
assert events[-1]["path"] == "small.py"
|
||||
assert events[-1]["added"] == 2
|
||||
assert events[-1]["deleted"] == 1
|
||||
|
||||
|
||||
def test_streaming_write_file_tracker_errors_unmatched_live_edits(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "call-live",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"aborted.md","content":"one\\n',
|
||||
})
|
||||
await tracker.error_unmatched([], "Tool call did not complete.")
|
||||
|
||||
asyncio.run(run())
|
||||
assert events[-1]["path"] == "aborted.md"
|
||||
assert events[-1]["phase"] == "error"
|
||||
assert events[-1]["status"] == "error"
|
||||
|
||||
|
||||
def test_streaming_write_file_tracker_keeps_matched_final_tool_call(tmp_path: Path) -> None:
|
||||
events: list[dict] = []
|
||||
|
||||
async def emit(batch: list[dict]) -> None:
|
||||
events.extend(batch)
|
||||
|
||||
async def run() -> None:
|
||||
tracker = StreamingFileEditTracker(workspace=tmp_path, tools={}, emit=emit)
|
||||
await tracker.update({
|
||||
"index": 0,
|
||||
"call_id": "idx-only",
|
||||
"name": "write_file",
|
||||
"arguments_delta": '{"path":"matched.md","content":"one\\n',
|
||||
})
|
||||
await tracker.error_unmatched([
|
||||
SimpleNamespace(
|
||||
id="final-call",
|
||||
name="write_file",
|
||||
arguments={"path": "matched.md", "content": "one\n"},
|
||||
)
|
||||
], "Tool call did not complete.")
|
||||
|
||||
asyncio.run(run())
|
||||
assert events
|
||||
assert all(event["status"] == "editing" for event in events)
|
||||
|
||||
|
||||
def test_untracked_tools_do_not_prepare_file_edit_tracker(tmp_path: Path) -> None:
|
||||
assert prepare_file_edit_tracker(
|
||||
call_id="call-exec",
|
||||
|
||||
@@ -98,6 +98,201 @@ def test_replay_file_edit_event_creates_file_activity(tmp_path, monkeypatch) ->
|
||||
assert msgs[2]["activitySegmentId"] != msgs[1]["activitySegmentId"]
|
||||
|
||||
|
||||
def test_replay_file_edit_progress_merges_after_interleaved_activity(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
|
||||
key = "websocket:t-file-progress"
|
||||
for ev in (
|
||||
{"event": "user", "chat_id": "t-file-progress", "text": "edit"},
|
||||
{
|
||||
"event": "message",
|
||||
"chat_id": "t-file-progress",
|
||||
"text": 'write_file({"path":"foo.txt"})',
|
||||
"kind": "tool_hint",
|
||||
},
|
||||
{
|
||||
"event": "file_edit",
|
||||
"chat_id": "t-file-progress",
|
||||
"edits": [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-write",
|
||||
"tool": "write_file",
|
||||
"path": "foo.txt",
|
||||
"phase": "start",
|
||||
"added": 12,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"event": "message",
|
||||
"chat_id": "t-file-progress",
|
||||
"text": "still working",
|
||||
"kind": "progress",
|
||||
},
|
||||
{
|
||||
"event": "file_edit",
|
||||
"chat_id": "t-file-progress",
|
||||
"edits": [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-write",
|
||||
"tool": "write_file",
|
||||
"path": "foo.txt",
|
||||
"phase": "end",
|
||||
"added": 30,
|
||||
"deleted": 0,
|
||||
"approximate": False,
|
||||
"status": "done",
|
||||
},
|
||||
],
|
||||
},
|
||||
):
|
||||
append_transcript_object(key, ev)
|
||||
|
||||
msgs = replay_transcript_to_ui_messages(read_transcript_lines(key))
|
||||
file_edit_messages = [msg for msg in msgs if msg.get("fileEdits")]
|
||||
|
||||
assert len(file_edit_messages) == 1
|
||||
assert file_edit_messages[0]["fileEdits"] == [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-write",
|
||||
"tool": "write_file",
|
||||
"path": "foo.txt",
|
||||
"phase": "end",
|
||||
"added": 30,
|
||||
"deleted": 0,
|
||||
"approximate": False,
|
||||
"status": "done",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def test_replay_file_edit_pending_placeholder_upgrades_to_path(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
|
||||
key = "websocket:t-file-pending"
|
||||
for ev in (
|
||||
{"event": "user", "chat_id": "t-file-pending", "text": "write"},
|
||||
{
|
||||
"event": "file_edit",
|
||||
"chat_id": "t-file-pending",
|
||||
"edits": [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-write",
|
||||
"tool": "write_file",
|
||||
"path": "",
|
||||
"phase": "start",
|
||||
"added": 1,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
"pending": True,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"event": "file_edit",
|
||||
"chat_id": "t-file-pending",
|
||||
"edits": [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-write",
|
||||
"tool": "write_file",
|
||||
"path": "foo.txt",
|
||||
"phase": "start",
|
||||
"added": 12,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
},
|
||||
],
|
||||
},
|
||||
):
|
||||
append_transcript_object(key, ev)
|
||||
|
||||
msgs = replay_transcript_to_ui_messages(read_transcript_lines(key))
|
||||
file_edit_messages = [msg for msg in msgs if msg.get("fileEdits")]
|
||||
|
||||
assert len(file_edit_messages) == 1
|
||||
assert file_edit_messages[0]["fileEdits"] == [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-write",
|
||||
"tool": "write_file",
|
||||
"path": "foo.txt",
|
||||
"phase": "start",
|
||||
"added": 12,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def test_replay_keeps_new_file_edit_after_reasoning_in_order(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
|
||||
key = "websocket:t-file-order"
|
||||
for ev in (
|
||||
{"event": "user", "chat_id": "t-file-order", "text": "edit"},
|
||||
{
|
||||
"event": "file_edit",
|
||||
"chat_id": "t-file-order",
|
||||
"edits": [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-one",
|
||||
"tool": "write_file",
|
||||
"path": "one.txt",
|
||||
"phase": "start",
|
||||
"added": 10,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
},
|
||||
],
|
||||
},
|
||||
{"event": "reasoning_delta", "chat_id": "t-file-order", "text": "Check next."},
|
||||
{"event": "reasoning_end", "chat_id": "t-file-order"},
|
||||
{
|
||||
"event": "file_edit",
|
||||
"chat_id": "t-file-order",
|
||||
"edits": [
|
||||
{
|
||||
"version": 1,
|
||||
"call_id": "call-two",
|
||||
"tool": "write_file",
|
||||
"path": "two.txt",
|
||||
"phase": "start",
|
||||
"added": 20,
|
||||
"deleted": 0,
|
||||
"approximate": True,
|
||||
"status": "editing",
|
||||
},
|
||||
],
|
||||
},
|
||||
):
|
||||
append_transcript_object(key, ev)
|
||||
|
||||
msgs = replay_transcript_to_ui_messages(read_transcript_lines(key))
|
||||
|
||||
assert [msg.get("fileEdits", [{}])[0].get("path") if msg.get("fileEdits") else msg.get("reasoning") for msg in msgs[1:]] == [
|
||||
"one.txt",
|
||||
"Check next.",
|
||||
"two.txt",
|
||||
]
|
||||
file_edit_segments = [
|
||||
msg.get("activitySegmentId")
|
||||
for msg in msgs
|
||||
if msg.get("fileEdits")
|
||||
]
|
||||
assert len(file_edit_segments) == 2
|
||||
assert file_edit_segments[0] != file_edit_segments[1]
|
||||
|
||||
|
||||
def test_build_response_schema(monkeypatch, tmp_path) -> None:
|
||||
from nanobot.utils.webui_transcript import build_webui_thread_response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user