Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
Feat/vikingbot steer interrupt (#6115)
高凌霄4c976135a3f4
diff --git a/bot/tests/test_agent_loop_outcome.py b/bot/tests/test_agent_loop_outcome.pyindex cfe509c84..4df293e4f 100644--- a/bot/tests/test_agent_loop_outcome.py+++ b/bot/tests/test_agent_loop_outcome.py@@ -1,3 +1,4 @@+import asyncioimport copyfrom datetime import datetimefrom pathlib import Path@@ -68,6 +69,19 @@ class _FakeOVClient:return self.context_payload+class _BlockingFirstProvider(_RecordingProvider):+ def __init__(self):+ super().__init__()+ self.started = asyncio.Event()++ async def chat(self, *args, **kwargs):+ self.calls.append((args, kwargs))+ if len(self.calls) == 1:+ self.started.set()+ await asyncio.Future()+ return LLMResponse(content="ok")++@pytest.fixturedef make_loop(temp_dir, monkeypatch):"""Construct a real loop with external integrations stubbed for these tests."""@@ -144,6 +158,80 @@ class _ImageRegistry:)+class _SteerProvider(LLMProvider):+ def __init__(self):+ super().__init__()+ self.calls = []++ async def chat(self, messages, tools=None, **kwargs):+ self.calls.append(copy.deepcopy(messages))+ if len(self.calls) == 1:+ return LLMResponse(+ content="checking",+ tool_calls=[+ ToolCallRequest(+ id="call-1",+ name="wait_for_steer",+ arguments={},+ tokens=1,+ )+ ],+ )+ return LLMResponse(content="steered final")++ def get_default_model(self) -> str:+ return "fake-model"+++class _BlockingSteerRegistry:+ def __init__(self):+ self.started = asyncio.Event()+ self.release = asyncio.Event()++ def get_definitions(self, **kwargs):+ return [+ {+ "type": "function",+ "function": {+ "name": "wait_for_steer",+ "description": "Wait until the test releases the tool.",+ "parameters": {"type": "object", "properties": {}},+ },+ }+ ]++ async def execute(self, tool_name, arguments, **kwargs):+ self.started.set()+ await self.release.wait()+ return "tool result"+++async def _consume_outbound_event(+ bus: MessageBus, event_type: OutboundEventType, timeout: float = 2+) -> object:+ deadline = asyncio.get_running_loop().time() + timeout+ while True:+ remaining = deadline - asyncio.get_running_loop().time()+ if remaining <= 0:+ raise TimeoutError(f"Timed out waiting for outbound event {event_type}")+ outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=remaining)+ if outbound.event_type == event_type:+ return outbound+++async def _consume_outbound_until_response(bus: MessageBus, timeout: float = 2) -> list[object]:+ events = []+ deadline = asyncio.get_running_loop().time() + timeout+ while True:+ remaining = deadline - asyncio.get_running_loop().time()+ if remaining <= 0:+ raise TimeoutError("Timed out waiting for response event")+ outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=remaining)+ events.append(outbound)+ if outbound.event_type == OutboundEventType.RESPONSE:+ return events++def test_context_keeps_multimodal_tool_result_on_tool_message(temp_dir: Path):context = ContextBuilder(workspace=temp_dir / "workspace")content = [@@ -170,6 +258,26 @@ def test_context_keeps_multimodal_tool_result_on_tool_message(temp_dir: Path):]+async def test_context_build_messages_can_skip_system_prompt(temp_dir: Path):+ context = ContextBuilder(workspace=temp_dir / "workspace", eval=True)++ messages = await context.build_messages(+ history=[],+ current_message="steer with image",+ media=[{"type": "image_url", "image_url": {"url": "https://example.com/image.png"}}],+ session_key=SessionKey(type="cli", channel_id="default", chat_id="steer-context"),+ ov_tools_enable=False,+ include_system=False,+ )++ assert [message["role"] for message in messages] == ["user", "user"]+ assert messages[-1]["content"] == [+ {"type": "image_url", "image_url": {"url": "https://example.com/image.png"}},+ {"type": "text", "text": "steer with image"},+ ]++@pytest.mark.asyncio@pytest.mark.parametrize("supports_media", [False, True])async def test_agent_loop_gates_multimodal_result_by_provider(make_loop, supports_media: bool):@@ -301,6 +409,470 @@ async def test_agent_loop_passes_configured_temperature_to_provider(make_loop, tassert loop.subagents.kwargs["temperature"] == 0.2+async def test_agent_loop_interrupt_does_not_cancel_final_session_save(+ make_loop, temp_dir: Path, monkeypatch+):+ bus = MessageBus()+ config = Config(storage_workspace=str(temp_dir))+ loop = make_loop(bus=bus, config=config)+ session_key = SessionKey(type="cli", channel_id="default", chat_id="final-save")+ msg = InboundMessage(session_key=session_key, sender_id="user-1", content="finish cleanly")+ save_started = asyncio.Event()+ release_save = asyncio.Event()+ save_calls = 0+ original_save = loop.sessions.save++ async def fake_run_agent_loop(self, **kwargs):+ return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1++ async def blocking_save(session):+ nonlocal save_calls+ save_calls += 1+ if len(session.messages) >= 2 and session.messages[-1].get("role") == "assistant":+ save_started.set()+ await release_save.wait()+ await original_save(session)++ fake_langfuse = _FakeLangfuseClient()+ monkeypatch.setattr(AgentLoop, "_run_agent_loop", fake_run_agent_loop)+ monkeypatch.setattr(loop.sessions, "save", blocking_save)+ monkeypatch.setattr(+ "vikingbot.agent.loop.LangfuseClient.get_instance",+ staticmethod(lambda: fake_langfuse),+ )++ task = asyncio.create_task(loop._process_message_as_active_turn(msg))+ try:+ await asyncio.wait_for(save_started.wait(), timeout=2)++ assert await loop.interrupt(session_key) is False++ release_save.set()+ response = await asyncio.wait_for(task, timeout=2)++ assert response is not None+ assert response.content == "final answer"+ session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)+ assert [message["role"] for message in session.messages] == ["user", "assistant"]+ assert [message["content"] for message in session.messages] == [+ "finish cleanly",+ "final answer",+ ]+ assert save_calls == 1+ finally:+ release_save.set()+ if not task.done():+ task.cancel()+ await asyncio.gather(task, return_exceptions=True)+++async def test_agent_loop_interrupt_marks_active_and_current_queued_messages(make_loop):+ bus = MessageBus()+ provider = _BlockingFirstProvider()+ loop = make_loop(bus=bus, provider=provider)+ session_key = SessionKey(type="cli", channel_id="default", chat_id="interrupt-count")++ run_task = asyncio.create_task(loop.run())+ try:+ await bus.publish_inbound(+ InboundMessage(session_key=session_key, sender_id="user-1", content="active")+ )+ await asyncio.wait_for(provider.started.wait(), timeout=2)+ for content in ("first queued", "second queued"):+ await bus.publish_inbound(+ InboundMessage(session_key=session_key, sender_id="user-1", content=content)+ )++ assert await loop.interrupt(session_key) is True+ await bus.publish_inbound(+ InboundMessage(session_key=session_key, sender_id="user-1", content="after interrupt")+ )++ for _ in range(3):+ await _consume_outbound_event(bus, OutboundEventType.INTERRUPT)+ response = await _consume_outbound_event(bus, OutboundEventType.RESPONSE)++ session = loop.sessions.get_or_create(session_key)+ assert response.content == "ok"+ assert [message["content"] for message in session.messages] == [+ "active",+ "first queued",+ "second queued",+ "after interrupt",+ "ok",+ ]+ assert len(provider.calls) == 2+ finally:+ run_task.cancel()+ await asyncio.gather(run_task, return_exceptions=True)+++async def test_agent_loop_steers_same_session_input_into_next_tool_turn(make_loop, temp_dir: Path):+ bus = MessageBus()+ provider = _SteerProvider()+ tools = _BlockingSteerRegistry()+ loop = make_loop(+ bus=bus,+ provider=provider,+ config=Config(storage_workspace=str(temp_dir)),+ max_iterations=3,+ )+ loop.tools = tools+ session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-session")++ run_task = asyncio.create_task(loop.run())+ try:+ await bus.publish_inbound(+ InboundMessage(session_key=session_key, sender_id="user-1", content="first request")+ )+ await asyncio.wait_for(tools.started.wait(), timeout=2)+ assert (+ loop.steer(+ InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")+ )+ is True+ )++ tools.release.set()++ events = await _consume_outbound_until_response(bus)+ assert [event.event_type for event in events[:4]] == [+ OutboundEventType.ITERATION,+ OutboundEventType.TOOL_CALL,+ OutboundEventType.TOOL_RESULT,+ OutboundEventType.ITERATION,+ ]+ response = events[-1]++ assert response.content == "steered final"+ assert len(provider.calls) == 2+ second_call_user_text = "\n".join(+ str(message.get("content"))+ for message in provider.calls[1]+ if message.get("role") == "user"+ )+ assert "first request" in second_call_user_text+ assert "steer update" in second_call_user_text++ session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)+ assert [message["role"] for message in session.messages] == [+ "user",+ "user",+ "assistant",+ ]+ assert [message["content"] for message in session.messages] == [+ "first request",+ "steer update",+ "steered final",+ ]+ assert bus.inbound_size == 0+ finally:+ tools.release.set()+ run_task.cancel()+ await asyncio.gather(run_task, return_exceptions=True)+++async def test_agent_loop_steers_same_session_input_before_final_response(+ make_loop, temp_dir: Path, monkeypatch+):+ bus = MessageBus()+ session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-final-boundary")+ provider_calls = []++ class _LateSteerProvider(_FakeProvider):+ async def chat(self, messages, tools=None, **kwargs):+ provider_calls.append(copy.deepcopy(messages))+ if len(provider_calls) == 1:+ assert (+ loop.steer(+ InboundMessage(+ session_key=session_key,+ sender_id="user-1",+ content="late steer",+ )+ )+ is True+ )+ return LLMResponse(content="first final")+ assert (+ loop.steer(+ InboundMessage(+ session_key=session_key,+ sender_id="user-1",+ content="too late",+ )+ )+ is False+ )+ return LLMResponse(content="steered final")++ loop = make_loop(+ bus=bus,+ provider=_LateSteerProvider(),+ config=Config(storage_workspace=str(temp_dir)),+ max_iterations=2,+ )++ async def prepare_steer(steer_msg):+ return loop_module._PreparedSteer(+ msg=steer_msg,+ prompt_messages=[{"role": "user", "content": steer_msg.content}],+ newly_recalled_exp_uris=[],+ newly_recalled_exp_content="",+ auto_memory_tool={+ "tool_name": "auto_memory_search",+ "result": "recalled memory",+ },+ )++ monkeypatch.setattr(loop, "_prepare_steer_message", prepare_steer)++ run_task = asyncio.create_task(loop.run())+ try:+ await bus.publish_inbound(+ InboundMessage(session_key=session_key, sender_id="user-1", content="first request")+ )++ events = await _consume_outbound_until_response(bus)+ assert [event.event_type for event in events[:4]] == [+ OutboundEventType.ITERATION,+ OutboundEventType.ITERATION,+ OutboundEventType.TOOL_CALL,+ OutboundEventType.TOOL_RESULT,+ ]+ response = events[-1]++ assert response.content == "steered final"+ assert len(provider_calls) == 2+ final_call_user_text = "\n".join(+ str(message.get("content"))+ for message in provider_calls[1]+ if message.get("role") == "user"+ )+ assert "first request" in final_call_user_text+ assert "late steer" in final_call_user_text++ session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)+ assert [message["content"] for message in session.messages] == [+ "first request",+ "first final",+ "late steer",+ "steered final",+ ]+ assert bus.inbound_size == 0+ finally:+ run_task.cancel()+ await asyncio.gather(run_task, return_exceptions=True)+++async def test_agent_loop_does_not_share_steers_with_concurrent_internal_loop(+ make_loop,+):+ provider = _BlockingFirstProvider()+ loop = make_loop(provider=provider, max_iterations=2)+ session_key = SessionKey(type="cli", channel_id="default", chat_id="concurrent-loop")+ active_task = asyncio.create_task(+ loop._process_message_as_active_turn(+ InboundMessage(session_key=session_key, sender_id="user-1", content="first request")+ )+ )+ try:+ await asyncio.wait_for(provider.started.wait(), timeout=2)+ assert loop.steer(+ InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")+ )++ assert await loop.process_direct("internal task", session_key=session_key) == "ok"++ active = loop._active_turn+ assert active is not None+ assert active.accepting_steer is True+ assert [steer.msg.content for steer in active.accepted_steers] == ["steer update"]+ finally:+ active_task.cancel()+ await asyncio.gather(active_task, return_exceptions=True)+++async def test_agent_loop_interrupt_preserves_steers_during_batched_prepare(make_loop, monkeypatch):+ loop = make_loop()+ session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-batch-interrupt")+ first_started = asyncio.Event()+ first_finished = asyncio.Event()+ release_first = asyncio.Event()+ second_started = asyncio.Event()++ async def prepare(steer_msg):+ if steer_msg.content == "first steer":+ first_started.set()+ await release_first.wait()+ first_finished.set()+ else:+ second_started.set()+ await asyncio.Future()+ return loop_module._PreparedSteer(+ msg=steer_msg,+ prompt_messages=[{"role": "user", "content": steer_msg.content}],+ newly_recalled_exp_uris=[],+ newly_recalled_exp_content="",+ )++ monkeypatch.setattr(loop, "_prepare_steer_message", prepare)++ async def process(_msg, active_turn=None):+ assert loop.steer(+ InboundMessage(session_key=session_key, sender_id="user-1", content="first steer")+ )++ async def add_second_steer():+ await first_started.wait()+ assert loop.steer(+ InboundMessage(session_key=session_key, sender_id="user-1", content="second steer")+ )+ release_first.set()++ asyncio.create_task(add_second_steer())+ await loop._take_accepted_steers(active_turn)++ monkeypatch.setattr(loop, "_process_message", process)+ task = asyncio.create_task(+ loop._process_message_as_active_turn(+ InboundMessage(session_key=session_key, sender_id="user-1", content="first request")+ )+ )+ try:+ await asyncio.wait_for(second_started.wait(), timeout=2)+ await asyncio.wait_for(first_finished.wait(), timeout=2)+ await asyncio.sleep(0)+ assert await loop.interrupt(session_key)+ response = await task+ assert response is not None+ assert response.event_type == OutboundEventType.INTERRUPT+ session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)+ assert [message["content"] for message in session.messages] == [+ "first request",+ "first steer",+ "second steer",+ ]+ finally:+ release_first.set()+ if not task.done():+ task.cancel()+ await asyncio.gather(task, return_exceptions=True)+++async def test_agent_loop_interrupt_preserves_active_steer_input(+ make_loop, temp_dir: Path, monkeypatch+):+ bus = MessageBus()+ provider = _SteerProvider()+ tools = _BlockingSteerRegistry()+ loop = make_loop(+ bus=bus,+ provider=provider,+ config=Config(storage_workspace=str(temp_dir)),+ max_iterations=3,+ )+ loop.tools = tools+ session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-interrupt-early")+ original_build_messages = ContextBuilder.build_messages++ async def recall_experience(self, *args, **kwargs):+ messages = await original_build_messages(self, *args, **kwargs)+ self.latest_recalled_exp_uris = ["viking://agent/experience/interrupted"]+ self.latest_recalled_exp_content = "interrupted experience"+ return messages++ monkeypatch.setattr(ContextBuilder, "build_messages", recall_experience)++ run_task = asyncio.create_task(loop.run())+ try:+ await bus.publish_inbound(+ InboundMessage(session_key=session_key, sender_id="user-1", content="first request")+ )+ await asyncio.wait_for(tools.started.wait(), timeout=2)+ assert (+ loop.steer(+ InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")+ )+ is True+ )+ active = loop._active_turn+ assert active is not None+ steer_task = active.accepted_steers[0].task++ assert await loop.interrupt(session_key) is True++ interrupted = await _consume_outbound_event(bus, OutboundEventType.INTERRUPT)++ assert interrupted.content == ""+ session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)+ assert [message["role"] for message in session.messages] == ["user", "user"]+ assert [message["content"] for message in session.messages] == [+ "first request",+ "steer update",+ ]+ assert "recalled_exp_uris" not in session.metadata+ assert bus.inbound_size == 0+ assert active.accepted_steers == []+ assert steer_task.done()+ finally:+ tools.release.set()+ run_task.cancel()+ await asyncio.gather(run_task, return_exceptions=True)+++async def test_agent_loop_error_preserves_accepted_steer_input(make_loop, monkeypatch):+ loop = make_loop()+ session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-error")+ started = asyncio.Event()+ release = asyncio.Event()++ async def fail_message(_msg, active_turn=None):+ started.set()+ await release.wait()+ raise RuntimeError("processing failed")++ monkeypatch.setattr(loop, "_process_message", fail_message)+ task = asyncio.create_task(+ loop._process_message_as_active_turn(+ InboundMessage(session_key=session_key, sender_id="user-1", content="first request")+ )+ )+ try:+ await asyncio.wait_for(started.wait(), timeout=2)+ assert loop.steer(+ InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")+ )+ active = loop._active_turn+ assert active is not None+ steer_task = active.accepted_steers[0].task++ release.set()+ with pytest.raises(RuntimeError, match="processing failed"):+ await task++ session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)+ assert [message["content"] for message in session.messages] == [+ "first request",+ "steer update",+ ]+ assert active.accepted_steers == []+ assert steer_task.done()+ finally:+ release.set()+ if not task.done():+ task.cancel()+ await asyncio.gather(task, return_exceptions=True)++@pytest.mark.asyncioasync def test_agent_loop_makes_final_no_tool_call_when_iteration_limit_reached(make_loop, temp_dir: Pathdiff --git a/bot/tests/test_openapi_auth.py b/bot/tests/test_openapi_auth.pyindex ebd1e166a..3a001b939 100644--- a/bot/tests/test_openapi_auth.py+++ b/bot/tests/test_openapi_auth.py@@ -5,6 +5,7 @@import asyncioimport jsonimport tempfile+from datetime import datetimefrom pathlib import Pathfrom types import SimpleNamespace@@ -263,6 +264,84 @@ class TestOpenAPIAuth:assert response.status_code == 409assert message_bus.inbound_size == 0+ def test_steer_requires_existing_in_flight_request(self, message_bus, temp_workspace):+ channel = OpenAPIChannel(OpenAPIChannelConfig(), message_bus, temp_workspace)++ response = _make_client(channel).post(+ "/bot/v1/sessions/same-session/steer",+ json={"message": "follow up"},+ )++ assert response.status_code == 409+ assert message_bus.inbound_size == 0++ def test_steer_attaches_message_to_active_turn(self, message_bus, temp_workspace):+ class FakeAgentLoop:+ def __init__(self):+ self.msg = None++ def steer(self, msg):+ self.msg = msg+ return True++ agent_loop = FakeAgentLoop()+ channel = OpenAPIChannel(+ OpenAPIChannelConfig(),+ message_bus,+ temp_workspace,+ agent_loop=agent_loop,+ )+ scope = channel._principal_scope("standalone")+ storage_key = channel._scoped_session_id(scope, "same-session")+ channel._pending[storage_key] = PendingResponse()++ response = _make_client(channel).post(+ "/bot/v1/sessions/same-session/steer",+ json={"message": "follow up", "user_id": "user-1"},+ )++ assert response.status_code == 200+ assert response.json() == {"accepted": True}+ assert agent_loop.msg.session_key == SessionKey(+ type="cli", channel_id="default", chat_id=storage_key+ )+ assert agent_loop.msg.content == "follow up"+ assert agent_loop.msg.sender_id == "user-1"+ assert message_bus.inbound_size == 0++ def test_steer_rejects_pending_session_without_active_turn(+ self, message_bus, temp_workspace+ ):+ class FakeAgentLoop:+ def steer(self, msg):+ return False++ channel = OpenAPIChannel(+ OpenAPIChannelConfig(),+ message_bus,+ temp_workspace,+ agent_loop=FakeAgentLoop(),+ )+ scope = channel._principal_scope("standalone")+ storage_key = channel._scoped_session_id(scope, "same-session")+ channel._pending[storage_key] = PendingResponse()+ channel._sessions[storage_key] = {+ "last_active": datetime.fromisoformat("2026-01-01T00:00:00"),+ "message_count": 1,+ }++ response = _make_client(channel).post(+ "/bot/v1/sessions/same-session/steer",+ json={"message": "follow up", "user_id": "user-1"},+ )++ assert response.status_code == 409+ assert channel._sessions[storage_key] == {+ "last_active": datetime.fromisoformat("2026-01-01T00:00:00"),+ "message_count": 1,+ }+ assert message_bus.inbound_size == 0+def test_scoped_session_id_stays_logical_while_storage_path_is_portable(self, message_bus, temp_workspace):@@ -408,6 +487,63 @@ class TestOpenAPIAuth:assert client.get(f"/bot/v1/sessions/{session_id}", headers=bob_headers).status_code == 404assert client.get("/bot/v1/sessions", headers=bob_headers).json()["total"] == 0+ @pytest.mark.asyncio+ async def test_openapi_interrupt_outbound_closes_pending_response(+ self, message_bus, temp_workspace+ ):+ channel = OpenAPIChannel(OpenAPIChannelConfig(), message_bus, temp_workspace)+ session_key = SessionKey(type="cli", channel_id="default", chat_id="local:session-1")+ pending = PendingResponse()+ channel._pending[session_key.chat_id] = pending++ await channel.send(+ OutboundMessage(+ session_key=session_key,+ content="",+ event_type=OutboundEventType.INTERRUPT,+ metadata={"reason": "user_interrupt"},+ )+ )++ assert pending.final_content == ""+ assert pending.event.is_set()+ assert pending.events[0]["type"] == "interrupt"+ assert pending.events[0]["data"] == {"reason": "user_interrupt"}+ stream_event = await pending.stream_queue.get()+ assert stream_event.event.value == "interrupt"+ assert await pending.stream_queue.get() is None++ def test_openapi_interrupt_route_targets_scoped_session(+ self, message_bus, temp_workspace, monkeypatch+ ):+ class FakeAgentLoop:+ def __init__(self):+ self.session_key = None++ async def interrupt(self, session_key):+ self.session_key = session_key+ return True++ agent_loop = FakeAgentLoop()+ channel = OpenAPIChannel(+ OpenAPIChannelConfig(),+ message_bus,+ temp_workspace,+ agent_loop=agent_loop,+ )+ client = _make_client(channel)++ response = client.post("/bot/v1/sessions/session-1/interrupt")++ assert response.status_code == 200+ assert response.json() == {"interrupted": True}+ expected_scope = channel._principal_scope("standalone")+ assert agent_loop.session_key == SessionKey(+ type="cli",+ channel_id="default",+ chat_id=channel._scoped_session_id(expected_scope, "session-1"),+ )+def test_chat_accepts_requests_when_api_key_not_configured(self, message_bus, temp_workspace, monkeypatch):diff --git a/bot/vikingbot/agent/context.py b/bot/vikingbot/agent/context.pyindex 13db88e9c..def5de36b 100644--- a/bot/vikingbot/agent/context.py+++ b/bot/vikingbot/agent/context.py@@ -385,6 +385,7 @@ IMPORTANT:experience_recall_enable: bool | None = None,exp_exclude_uris: list[str] | None = None,experience_case_lookup: dict[str, Any] | None = None,+ include_system: bool = True,) -> list[dict[str, Any]]:"""Build the complete message list for an LLM call.@@ -402,6 +403,8 @@ IMPORTANT:from exposing OpenViking tools. Defaults to ov_tools_enable.exp_exclude_uris: Optional list of experience URIs that have already been recalledin this session and should be skipped (deduplication).+ include_system: Whether to prepend the system prompt. Steer inputs reuse the active+ turn's existing system prompt and set this to False.Returns:List of messages including system prompt.@@ -409,14 +412,15 @@ IMPORTANT:messages = []# System prompt- system_prompt = await self.build_system_prompt(- session_key,- ov_tools_enable=ov_tools_enable,- profile_user_list=profile_user_list,- memory_peer_ids=memory_peer_ids,- memory_owner_user_ids=memory_owner_user_ids,- )- messages.append({"role": "system", "content": system_prompt})+ if include_system:+ system_prompt = await self.build_system_prompt(+ session_key,+ ov_tools_enable=ov_tools_enable,+ profile_user_list=profile_user_list,+ memory_peer_ids=memory_peer_ids,+ memory_owner_user_ids=memory_owner_user_ids,+ )+ messages.append({"role": "system", "content": system_prompt})# Historyif not self._eval:diff --git a/bot/vikingbot/agent/loop.py b/bot/vikingbot/agent/loop.pyindex 36ff9398a..b6c49371a 100644--- a/bot/vikingbot/agent/loop.py+++ b/bot/vikingbot/agent/loop.py@@ -9,7 +9,7 @@ import timeimport uuidfrom collections.abc import Callablefrom contextlib import AsyncExitStack, aclosing-from dataclasses import dataclass+from dataclasses import dataclass, fieldfrom datetime import datetimefrom pathlib import Pathfrom typing import TYPE_CHECKING, Any@@ -264,6 +264,46 @@ class _PlainTextFinal:content: str | None = None+@dataclass(slots=True)+class _PreparedSteer:+ """Preprocessed same-session input to merge into an active agent loop."""++ msg: InboundMessage+ prompt_messages: list[dict[str, Any]]+ newly_recalled_exp_uris: list[str]+ newly_recalled_exp_content: str+ auto_memory_tool: dict[str, Any] | None = None+++@dataclass(slots=True)+class _AcceptedSteer:+ """A steer accepted by the active turn and its in-flight preprocessing."""++ msg: InboundMessage+ task: asyncio.Task[_PreparedSteer]+++@dataclass(slots=True)+class _AssistantBoundary:+ """A plain-text reply emitted before later steer input continued the turn."""++ content: str+ reasoning_content: str | None = None+++@dataclass(slots=True)+class _ActiveTurn:+ """The single turn currently running in the serial AgentLoop."""++ session_key: SessionKey+ task: asyncio.Task+ interruptible: bool = True+ interrupted: bool = False+ accepting_steer: bool = True+ accepted_steers: list[_AcceptedSteer] = field(default_factory=list)+ history_items: list[_PreparedSteer | _AssistantBoundary] = field(default_factory=list)++class AgentIterationLimitExceeded(RuntimeError):"""A structured task used every available AgentLoop iteration without submitting."""@@ -410,6 +450,8 @@ class AgentLoop:self._mcp_connected = Falseself._mcp_connecting = Falseself._ov_clients: dict[str, Any] = {}+ self._active_turn: _ActiveTurn | None = None+ self._queued_interrupt_counts: dict[SessionKey, int] = {}self._register_default_tools()async def _connect_mcp(self) -> None:@@ -453,23 +495,26 @@ class AgentLoop:session_key: SessionKey,query: str,result: str,+ *,+ publish_events: bool = True,) -> dict[str, Any]:"""Expose automatic OpenViking memory lookup using the existing tool event stream."""args_str = json.dumps({"query": query}, ensure_ascii=False)- await self.bus.publish_outbound(- OutboundMessage(- session_key=session_key,- content=f"auto_memory_search({args_str})",- event_type=OutboundEventType.TOOL_CALL,+ if publish_events:+ await self.bus.publish_outbound(+ OutboundMessage(+ session_key=session_key,+ content=f"auto_memory_search({args_str})",+ event_type=OutboundEventType.TOOL_CALL,+ ))- )- await self.bus.publish_outbound(- OutboundMessage(- session_key=session_key,- content=result,- event_type=OutboundEventType.TOOL_RESULT,+ await self.bus.publish_outbound(+ OutboundMessage(+ session_key=session_key,+ content=result,+ event_type=OutboundEventType.TOOL_RESULT,+ ))- )return {"tool_name": "auto_memory_search","args": args_str,@@ -1182,13 +1227,23 @@ class AgentLoop:while self._running:try:# Wait for next message- msg = await asyncio.wait_for(self.bus.consume_inbound(), timeout=1.0)+ msg = await self.bus.consume_inbound()# Process ittry:- response = await self._process_message(msg)+ queued_interrupts = self._queued_interrupt_counts.get(msg.session_key, 0)+ if queued_interrupts > 0:+ if queued_interrupts == 1:+ self._queued_interrupt_counts.pop(msg.session_key, None)+ else:+ self._queued_interrupt_counts[msg.session_key] = queued_interrupts - 1+ response = await self._complete_interrupted_message(msg)+ else:+ response = await self._process_message_as_active_turn(msg)if response:await self.bus.publish_outbound(response)+ except asyncio.CancelledError:+ raiseexcept Exception as e:logger.exception(f"Error processing message: {e}")# Send error response@@ -1202,11 +1257,262 @@ class AgentLoop:except asyncio.TimeoutError:continue+ async def interrupt(self, session_key: SessionKey) -> bool:+ """Interrupt the active or already queued turn for a session."""+ active = self._active_turn+ if (+ active+ and active.session_key == session_key+ and active.interruptible+ and not active.task.done()+ ):+ pending_count = self.bus.count_pending_inbound_session(session_key)+ if pending_count:+ self._queued_interrupt_counts[session_key] = pending_count+ active.interrupted = True+ active.task.cancel()+ return True+ pending_count = self.bus.count_pending_inbound_session(session_key)+ queued_interrupts = self._queued_interrupt_counts.get(session_key, 0)+ if queued_interrupts < pending_count:+ self._queued_interrupt_counts[session_key] = pending_count+ return True+ return False++ def steer(self, msg: InboundMessage) -> bool:+ """Attach same-session input directly to the active turn."""+ active = self._active_turn+ if (+ active is None+ or active.session_key != msg.session_key+ or not active.accepting_steer+ or active.interrupted+ or active.task.done()+ or not msg.need_reply+ or msg.content.strip().startswith("/")+ ):+ return False+ active.accepted_steers.append(+ _AcceptedSteer(+ msg=msg,+ task=asyncio.create_task(self._prepare_steer_message(msg)),+ )+ )+ return True++ async def _process_message_as_active_turn(self, msg: InboundMessage) -> OutboundMessage | None:+ active: _ActiveTurn++ async def run_active_message() -> OutboundMessage | None:+ return await self._process_message(msg, active_turn=active)++ task = asyncio.create_task(run_active_message())+ is_agent_turn = bool(+ msg.need_reply+ and msg.session_key.type != "system"+ and self.config.mode != BotMode.DEBUG+ and not msg.content.strip().startswith("/")+ )+ active = _ActiveTurn(+ msg.session_key,+ task,+ interruptible=is_agent_turn,+ accepting_steer=is_agent_turn,+ )+ self._active_turn = active+ try:+ response = await task+ if active.accepted_steers:+ raise RuntimeError("Active turn completed with unconsumed steer input")+ return response+ except asyncio.CancelledError:+ if active.interrupted:+ logger.info(f"Interrupted message from {msg.session_key}")+ return await self._complete_interrupted_message(msg)+ raise+ except Exception:+ if self._active_turn is active and (active.history_items or active.accepted_steers):+ await self._complete_interrupted_message(msg)+ raise+ finally:+ if self._active_turn is active:+ # Clear the active pointer before cleanup so later interrupt calls+ # cannot cancel this turn while its local teardown is finishing.+ self._active_turn = None++ async def _prepare_steer_message(self, msg: InboundMessage) -> _PreparedSteer:+ try:+ session_key = msg.session_key+ session = self.sessions.get_or_create(+ session_key,+ skip_heartbeat=session_key.type == "cli",+ )+ ov_tools_enable = self._get_ov_tools_enable(session_key)+ openviking_connection = (+ msg.openviking_connection if isinstance(msg.openviking_connection, dict) else None+ )+ actor_peer_id = msg.actor_peer_id or msg.sender_id+ memory_peer_ids = self._metadata_memory_peer_ids(msg.metadata)+ memory_owner_user_ids = self._metadata_memory_owner_user_ids(msg.metadata)+ channel_config = self._get_channel_config(session_key)+ profile_user_list = []+ if channel_config and ov_tools_enable:+ profile_user_list = getattr(channel_config, "profile_user_list", [])+ if not memory_peer_ids:+ memory_peer_ids = self._channel_memory_peer_ids(channel_config)+ if not memory_owner_user_ids:+ memory_owner_user_ids = self._channel_memory_owner_user_ids(channel_config)++ message_workspace = (+ self.sandbox_manager.get_workspace_path(session_key)+ if self.sandbox_manager+ else self.workspace+ )+ message_context = ContextBuilder(+ message_workspace,+ sandbox_manager=self.sandbox_manager,+ sender_id=msg.sender_id,+ actor_peer_id=actor_peer_id,+ sender_name=msg.sender_name,+ is_group_chat=msg.metadata.get("chat_type") == "group",+ eval=self._eval,+ openviking_connection=openviking_connection,+ enable_subagents=self._subagents_enabled(),+ config=self.config,+ )+ exp_exclude_uris = session.metadata.get("recalled_exp_uris", [])+ if not isinstance(exp_exclude_uris, list):+ exp_exclude_uris = []+ prepared_messages = await message_context.build_messages(+ history=[],+ current_message=msg.content,+ media=msg.media if msg.media else None,+ session_key=msg.session_key,+ ov_tools_enable=ov_tools_enable,+ profile_user_list=profile_user_list,+ memory_peer_ids=memory_peer_ids,+ memory_owner_user_ids=memory_owner_user_ids,+ exp_exclude_uris=exp_exclude_uris,+ include_system=False,+ )+ auto_memory_tool = None+ if ov_tools_enable and message_context.latest_relevant_memories:+ auto_memory_tool = await self._publish_auto_memory_context(+ session_key=session_key,+ query=msg.content,+ result=message_context.latest_relevant_memories,+ publish_events=False,+ )+ return _PreparedSteer(+ msg=msg,+ prompt_messages=prepared_messages,+ newly_recalled_exp_uris=list(message_context.latest_recalled_exp_uris),+ newly_recalled_exp_content=message_context.latest_recalled_exp_content,+ auto_memory_tool=auto_memory_tool,+ )+ except Exception:+ logger.exception("Failed to prepare steer context; using raw input")+ return _PreparedSteer(+ msg=msg,+ prompt_messages=[+ {+ "role": "user",+ "content": self.context._build_user_content(msg.content, msg.media),+ }+ ],+ newly_recalled_exp_uris=[],+ newly_recalled_exp_content="",+ )++ async def _take_accepted_steers(+ self,+ active: _ActiveTurn | None,+ ) -> list[_PreparedSteer]:+ if active is None:+ return []+ prepared: list[_PreparedSteer] = []+ prepared_count = 0+ # Keep ownership on the active turn until every observed batch is ready,+ # so interrupt can still persist all accepted raw inputs at any await.+ while prepared_count < len(active.accepted_steers):+ accepted = active.accepted_steers[prepared_count:]+ batch = await asyncio.gather(*(steer.task for steer in accepted))+ prepared.extend(batch)+ prepared_count += len(accepted)+ del active.accepted_steers[:prepared_count]+ return prepared++ @staticmethod+ def _append_active_turn_history(+ session: Session,+ items: list[_PreparedSteer | _AssistantBoundary],+ ) -> None:+ for item in items:+ if isinstance(item, _AssistantBoundary):+ session.add_message(+ "assistant",+ item.content,+ reasoning_content=item.reasoning_content,+ )+ else:+ if item.newly_recalled_exp_content:+ session.add_message(+ "user",+ "[Experience Reminder]\n"+ f"## Relevant Agent Experience\n{item.newly_recalled_exp_content}",+ exp_uris=item.newly_recalled_exp_uris,+ experience_reminder=True,+ )+ if item.newly_recalled_exp_uris:+ recalled_uris = session.metadata.setdefault("recalled_exp_uris", [])+ for uri in item.newly_recalled_exp_uris:+ if uri not in recalled_uris:+ recalled_uris.append(uri)+ session.add_message(+ "user",+ item.msg.content,+ sender_id=item.msg.sender_id,+ )+def stop(self) -> None:"""Stop the agent loop."""self._running = Falselogger.info("Agent loop stopping")+ async def _complete_interrupted_message(self, msg: InboundMessage) -> OutboundMessage:+ session_key = msg.session_key+ skip_heartbeat = session_key.type == "cli"+ session = self.sessions.get_or_create(session_key, skip_heartbeat=skip_heartbeat)+ session.add_message("user", msg.content, sender_id=msg.sender_id)+ active = self._active_turn+ accepted_steers: list[_AcceptedSteer] = []+ if active is not None and active.session_key == session_key:+ accepted_steers = active.accepted_steers+ consumed = (+ item.msg for item in active.history_items if isinstance(item, _PreparedSteer)+ )+ pending = (steer.msg for steer in accepted_steers)+ for steer_msg in (*consumed, *pending):+ session.add_message(+ "user",+ steer_msg.content,+ sender_id=steer_msg.sender_id,+ )+ await self.sessions.save(session)+ for steer in accepted_steers:+ steer.task.cancel()+ await asyncio.gather(+ *(steer.task for steer in accepted_steers),+ return_exceptions=True,+ )+ accepted_steers.clear()+ return OutboundMessage(+ session_key=msg.session_key,+ content="",+ event_type=OutboundEventType.INTERRUPT,+ metadata={**(msg.metadata or {}), "reason": "interrupted"},+ )+async def _compact_tool_loop(self,messages: list[dict],@@ -1447,6 +1753,7 @@ class AgentLoop:status_note_provider: Any | None = None,should_stop: Callable[[], bool] | None = None,skill_runtime: Any | None = None,+ active_turn: _ActiveTurn | None = None,agent_id: str = "main",max_iterations: int | None = None,) -> tuple[str | None, str | None, list[dict], dict[str, int], int]:@@ -1525,6 +1832,7 @@ class AgentLoop:compact_pending = Falsecompact_threshold_chars = (context_compact_budget or 0) * 0.85compact_trigger_chars = compact_threshold_chars+ steer_memory_events: list[_PreparedSteer] = []def accumulate_token_usage(response: Any) -> None:if not response.usage:@@ -1540,6 +1848,15 @@ class AgentLoop:breakiteration += 1+ if active_turn is not None and iteration >= iteration_limit:+ active_turn.accepting_steer = False+ accepted_steers = await self._take_accepted_steers(active_turn)+ for steer in accepted_steers:+ messages.extend(steer.prompt_messages)+ if accepted_steers:+ active_turn.history_items.extend(accepted_steers)+ steer_memory_events.extend(accepted_steers)+if context_compact_budget is not None:current_chars = _compact_msg_chars(messages)if compact_pending or current_chars > context_compact_budget:@@ -1567,6 +1884,14 @@ class AgentLoop:event_type=OutboundEventType.ITERATION,))+ for steer in steer_memory_events:+ if steer.auto_memory_tool:+ await self._publish_auto_memory_context(+ session_key=session_key,+ query=steer.msg.content,+ result=steer.auto_memory_tool["result"],+ )+ steer_memory_events.clear()if status_note_provider is not None:note = await status_note_provider(iteration)@@ -1970,6 +2295,29 @@ class AgentLoop:})continue+ final_steers = await self._take_accepted_steers(active_turn)+ if final_steers:+ messages = self.context.add_assistant_message(+ messages,+ response.content,+ [],+ reasoning_content=response.reasoning_content,+ )+ active_turn.history_items.append(+ _AssistantBoundary(+ content=response.content or "",+ reasoning_content=response.reasoning_content,+ )+ )+ active_turn.history_items.extend(final_steers)+ for steer in final_steers:+ messages.extend(steer.prompt_messages)+ steer_memory_events.extend(final_steers)+ continue+ if active_turn is not None:+ # No await separates this close from the empty queue check,+ # so a successful steer cannot slip past the final checkpoint.+ active_turn.accepting_steer = Falsefinal_content = response.contentfinal_reasoning_content = response.reasoning_contentif routed:@@ -2105,7 +2453,11 @@ class AgentLoop:extract_session_id=lambda msg: msg.session_key.safe_name(),extract_user_id=lambda msg: msg.sender_id,)- async def _process_message(self, msg: InboundMessage) -> OutboundMessage | None:+ async def _process_message(+ self,+ msg: InboundMessage,+ active_turn: _ActiveTurn | None = None,+ ) -> OutboundMessage | None:"""Process a single inbound message.@@ -2402,13 +2754,6 @@ class AgentLoop:# Track newly recalled experience URIs for deduplicationnewly_recalled_exp_uris = getattr(message_context, "latest_recalled_exp_uris", [])newly_recalled_exp_content = getattr(message_context, "latest_recalled_exp_content", "")- if newly_recalled_exp_uris:- existing_uris = set(exp_exclude_uris)- for uri in newly_recalled_exp_uris:- if uri not in existing_uris:- existing_uris.add(uri)- exp_exclude_uris.append(uri)- session.metadata["recalled_exp_uris"] = exp_exclude_uris# Run agent loop within a stable response identity for tracing/tool spans.response_id = uuid.uuid4().hex@@ -2434,10 +2779,15 @@ class AgentLoop:channel_metadata=msg.metadata,captured_turns=agent_turns,skill_runtime=skill_runtime,+ active_turn=active_turn,)if auto_memory_tool:tools_used = [auto_memory_tool, *(tools_used or [])]+ active_turn_history = list(active_turn.history_items) if active_turn is not None else []+ for item in active_turn_history:+ if isinstance(item, _PreparedSteer) and item.auto_memory_tool:+ tools_used.append(item.auto_memory_tool)# Log response previewpreview = final_content[:300] + "..." if len(final_content) > 300 else final_content@@ -2454,8 +2804,17 @@ class AgentLoop:tools_used=tools_used,)+ if active_turn is not None:+ # Detach before persistence so a later interrupt cannot cancel final session writes.+ self._active_turn = None+is_heartbeat = bool(msg.metadata.get(HEARTBEAT_METADATA_KEY))if not (is_heartbeat and is_heartbeat_noop_response(final_content)):+ if newly_recalled_exp_uris:+ recalled_uris = session.metadata.setdefault("recalled_exp_uris", [])+ for uri in newly_recalled_exp_uris:+ if uri not in recalled_uris:+ recalled_uris.append(uri)# Write experience reminder into session history so it persists# across turns and survives bot restarts (in JSONL).if newly_recalled_exp_content:@@ -2466,6 +2825,7 @@ class AgentLoop:experience_reminder=True,)session.add_message("user", msg.content, sender_id=msg.sender_id)+ self._append_active_turn_history(session, active_turn_history)session.add_message("assistant",final_content,diff --git a/bot/vikingbot/bus/events.py b/bot/vikingbot/bus/events.pyindex 9492634d5..ea378ccc0 100644--- a/bot/vikingbot/bus/events.py+++ b/bot/vikingbot/bus/events.py@@ -12,6 +12,7 @@ class OutboundEventType(str, Enum):"""Type of outbound message/event."""RESPONSE = "response" # Normal response message+ INTERRUPT = "interrupt" # Turn was interrupted before producing a final responseTOOL_CALL = "tool_call" # Tool being calledTOOL_RESULT = "tool_result" # Result from tool executionREASONING = "reasoning" # Reasoning contentdiff --git a/bot/vikingbot/bus/queue.py b/bot/vikingbot/bus/queue.pyindex a74c89500..5fcbf3e42 100644--- a/bot/vikingbot/bus/queue.py+++ b/bot/vikingbot/bus/queue.py@@ -33,6 +33,10 @@ class MessageBus:"""Consume the next outbound message (blocks until available)."""return await self.outbound.get()+ def count_pending_inbound_session(self, session_key) -> int:+ """Return the number of inbound messages waiting for the session."""+ return sum(1 for msg in list(self.inbound._queue) if msg.session_key == session_key)+@propertydef inbound_size(self) -> int:"""Number of pending inbound messages."""diff --git a/bot/vikingbot/channels/openapi.py b/bot/vikingbot/channels/openapi.pyindex a01c80e62..7d2ec2ec9 100644--- a/bot/vikingbot/channels/openapi.py+++ b/bot/vikingbot/channels/openapi.py@@ -168,11 +168,13 @@ class OpenAPIChannel(BaseChannel):app: "FastAPI | None" = None,global_config: Config | None = None,compile_service: Any | None = None,+ agent_loop: Any | None = None,):super().__init__(config, bus, workspace_path)self.config = configself._global_config = global_configself._compile_service = compile_service+ self._agent_loop = agent_loop# Regular OpenAPI pending and sessionsself._pending: Dict[str, PendingResponse] = {}self._sessions: Dict[str, Dict[str, Any]] = {}@@ -282,6 +284,11 @@ class OpenAPIChannel(BaseChannel):)pending.set_final(msg.content or "")await pending.close_stream()+ elif msg.event_type == OutboundEventType.INTERRUPT:+ pending.set_response_id(msg.response_id)+ await pending.add_event("interrupt", msg.metadata or {})+ pending.set_final(msg.content or "")+ await pending.close_stream()elif msg.event_type == OutboundEventType.REASONING:await pending.add_event("reasoning", msg.content)elif msg.event_type == OutboundEventType.CONTENT_DELTA:@@ -315,6 +322,11 @@ class OpenAPIChannel(BaseChannel):)pending.set_final(msg.content or "")await pending.close_stream()+ elif msg.event_type == OutboundEventType.INTERRUPT:+ pending.set_response_id(msg.response_id)+ await pending.add_event("interrupt", msg.metadata or {})+ pending.set_final(msg.content or "")+ await pending.close_stream()elif msg.event_type == OutboundEventType.REASONING:await pending.add_event("reasoning", msg.content)elif msg.event_type == OutboundEventType.CONTENT_DELTA:@@ -532,6 +544,37 @@ class OpenAPIChannel(BaseChannel):del channel._sessions[storage_key]return {"deleted": True}+ @router.post("/sessions/{session_id}/interrupt")+ async def interrupt_session(+ session_id: str,+ http_request: Request,+ auth: GatewayRequestAuth = Depends(verify_gateway_request),+ ):+ """Interrupt the active or queued turn for a session."""+ if channel._agent_loop is None:+ raise HTTPException(status_code=503, detail="Agent loop is not available")+ scope = await channel._resolve_request_principal(http_request, auth)+ storage_key = channel._scoped_session_id(scope, session_id)+ session_key = SessionKey(+ type="cli",+ channel_id=channel.config.channel_id(),+ chat_id=storage_key,+ )+ interrupted = await channel._agent_loop.interrupt(session_key)+ return {"interrupted": interrupted}++ @router.post("/sessions/{session_id}/steer")+ async def steer_session(+ session_id: str,+ request: ChatRequest,+ http_request: Request,+ auth: GatewayRequestAuth = Depends(verify_gateway_request),+ ):+ """Append user input to an active streaming turn."""+ await channel._prepare_chat_request(http_request, request, auth)+ request.session_id = session_id+ return await channel._handle_chat_steer(request)+# ========== Bot Channel Routes ==========@router.post("/chat/channel", response_model=ChatResponse)@@ -1567,6 +1610,44 @@ class OpenAPIChannel(BaseChannel):},)+ async def _handle_chat_steer(self, request: ChatRequest) -> dict[str, bool]:+ """Append input to an existing streaming chat request."""+ session_id = request.session_id+ if not session_id:+ raise HTTPException(status_code=400, detail="session_id is required")++ storage_key = self._scoped_session_id(request._principal_scope, session_id)+ if storage_key not in self._pending:+ raise HTTPException(+ status_code=409,+ detail="Session has no active request to steer",+ )++ user_id = self._request_user_id(request)+ session_key = SessionKey(+ type="cli",+ channel_id=self.config.channel_id(),+ chat_id=storage_key,+ )+ msg = InboundMessage(+ session_key=session_key,+ sender_id=user_id,+ actor_peer_id=self._request_actor_peer_id(request, user_id),+ content=self._request_content(request),+ media=self._request_media(request),+ metadata=self._request_metadata(request),+ openviking_connection=self._request_openviking_connection(request),+ )+ if self._agent_loop is None or not self._agent_loop.steer(msg):+ raise HTTPException(+ status_code=409,+ detail="Session has no active turn to steer",+ )+ if storage_key in self._sessions:+ self._sessions[storage_key]["last_active"] = datetime.now()+ self._sessions[storage_key]["message_count"] += 1+ return {"accepted": True}+async def _handle_bot_chat(self, channel_id: str, request: ChatRequest) -> ChatResponse:"""Handle a BotChannel chat request."""session_id = request.session_id or str(uuid.uuid4())diff --git a/bot/vikingbot/channels/openapi_models.py b/bot/vikingbot/channels/openapi_models.pyindex 072760edd..db7cd7881 100644--- a/bot/vikingbot/channels/openapi_models.py+++ b/bot/vikingbot/channels/openapi_models.py@@ -88,6 +88,7 @@ class EventType(str, Enum):"""Event type enumeration."""RESPONSE = "response"+ INTERRUPT = "interrupt"TOOL_CALL = "tool_call"TOOL_RESULT = "tool_result"REASONING = "reasoning"diff --git a/bot/vikingbot/cli/commands.py b/bot/vikingbot/cli/commands.pyindex c4f62cc38..f97f9bf41 100644--- a/bot/vikingbot/cli/commands.py+++ b/bot/vikingbot/cli/commands.py@@ -584,6 +584,7 @@ def gateway(enable_openapi=True,openapi_port=effective_port,compile_service=compile_service,+ agent_loop=agent_loop,)heartbeat = prepare_heartbeat(config, agent_loop, session_manager)@@ -793,6 +794,7 @@ def prepare_channel(enable_openapi: bool = False,openapi_port: int = 18790,compile_service=None,+ agent_loop=None,):"""Prepare channels for the bot.@@ -819,6 +821,7 @@ def prepare_channel(app=fastapi_app, # Pass the external FastAPI appglobal_config=config,compile_service=compile_service,+ agent_loop=agent_loop,)from vikingbot.studio.service import StudioServicediff --git a/openviking/server/routers/bot.py b/openviking/server/routers/bot.pyindex ddcaaaf69..a7cc4519c 100644--- a/openviking/server/routers/bot.py+++ b/openviking/server/routers/bot.py@@ -6,6 +6,7 @@ Vikingbot OpenAPIChannel when the --with-bot option is enabled.import jsonfrom typing import AsyncGenerator, Optional+from urllib.parse import quoteimport httpxfrom fastapi import APIRouter, Depends, HTTPException, Request, status@@ -315,6 +316,103 @@ async def feedback()[email protected]("/sessions/{session_id}/interrupt")+async def interrupt_session(+ session_id: str,+ request: Request,+ _ctx: RequestContext = Depends(get_request_context),+):+ """Interrupt an active or queued bot session via the VikingBot gateway."""+ bot_url = get_bot_url()+ headers = {+ "X-OpenViking-Account": _ctx.user.account_id,+ "X-OpenViking-User": _ctx.user.user_id,+ }+ if _ctx.api_key:+ headers["X-API-Key"] = _ctx.api_key+ if _ctx.actor_peer_id:+ headers["X-OpenViking-Actor-Peer"] = _ctx.actor_peer_id+ if BOT_API_KEY:+ headers["X-Gateway-Token"] = BOT_API_KEY++ try:+ async with _create_bot_proxy_client() as client:+ response = await client.post(+ f"{bot_url}/bot/v1/sessions/{quote(session_id, safe='')}/interrupt",+ headers=headers,+ timeout=30.0,+ )+ response.raise_for_status()+ return response.json()+ except httpx.RequestError as e:+ logger.error(f"Failed to connect to bot service: {e}")+ raise HTTPException(+ status_code=status.HTTP_502_BAD_GATEWAY,+ detail=f"Bot service unavailable: {str(e)}",+ )+ except httpx.HTTPStatusError as e:+ logger.error(f"Bot service returned error: {e}")+ if e.response.status_code < 500:+ raise HTTPException(+ status_code=e.response.status_code,+ detail=e.response.text,+ )+ raise HTTPException(+ status_code=status.HTTP_502_BAD_GATEWAY,+ detail=f"Bot service error: {e.response.text}",+ )++[email protected]("/sessions/{session_id}/steer")+async def steer_session(+ session_id: str,+ request: Request,+ _ctx: RequestContext = Depends(get_request_context),+):+ """Append user input to an active streaming bot session."""+ bot_url = get_bot_url()++ try:+ body = await request.json()+ except json.JSONDecodeError:+ raise HTTPException(+ status_code=status.HTTP_400_BAD_REQUEST,+ detail="Invalid JSON in request body",+ )++ try:+ async with _create_bot_proxy_client() as client:+ headers = {"Content-Type": "application/json"}+ if BOT_API_KEY:+ headers["X-Gateway-Token"] = BOT_API_KEY++ response = await client.post(+ f"{bot_url}/bot/v1/sessions/{quote(session_id, safe='')}/steer",+ json=_attach_openviking_connection(body, request, _ctx),+ headers=headers,+ timeout=30.0,+ )+ response.raise_for_status()+ return response.json()+ except httpx.RequestError as e:+ logger.error(f"Failed to connect to bot service: {e}")+ raise HTTPException(+ status_code=status.HTTP_502_BAD_GATEWAY,+ detail=f"Bot service unavailable: {str(e)}",+ )+ except httpx.HTTPStatusError as e:+ logger.error(f"Bot service returned error: {e}")+ if e.response.status_code < 500:+ raise HTTPException(+ status_code=e.response.status_code,+ detail=e.response.text,+ )+ raise HTTPException(+ status_code=status.HTTP_502_BAD_GATEWAY,+ detail=f"Bot service error: {e.response.text}",+ )++@router.post("/chat/stream")async def chat_stream(request: Request,diff --git a/tests/server/test_bot_proxy_auth.py b/tests/server/test_bot_proxy_auth.pyindex 7e909871c..d2d4f7228 100644--- a/tests/server/test_bot_proxy_auth.py+++ b/tests/server/test_bot_proxy_auth.py@@ -103,6 +103,117 @@ async def test_feedback_proxy_forwards_request(monkeypatch):assert forwarded["timeout"] == 30.0+async def test_interrupt_proxy_forwards_session_interrupt(monkeypatch):+ forwarded = {}++ class FakeResponse:+ status_code = 200+ text = '{"interrupted": true}'++ def raise_for_status(self):+ return None++ def json(self):+ return {"interrupted": True}++ class FakeClient:+ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, tb):+ return None++ async def post(self, url, headers, timeout):+ forwarded["url"] = url+ forwarded["headers"] = headers+ forwarded["timeout"] = timeout+ return FakeResponse()++ monkeypatch.setattr(bot_router_module, "BOT_API_URL", "http://127.0.0.1:18790")+ monkeypatch.setattr(bot_router_module, "BOT_API_KEY", "gateway-secret")+ monkeypatch.setattr(bot_router_module, "_create_bot_proxy_client", lambda: FakeClient())++ app = FastAPI()+ app.state.config = SimpleNamespace(get_effective_auth_mode=lambda: AuthMode.DEV)+ app.state.auth_plugin = DevAuthPlugin()+ app.include_router(bot_router_module.router, prefix="/bot/v1")+ transport = httpx.ASGITransport(app=app)++ async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:+ response = await client.post("/bot/v1/sessions/session%201/interrupt")++ assert response.status_code == 200+ assert response.json() == {"interrupted": True}+ assert forwarded["url"] == "http://127.0.0.1:18790/bot/v1/sessions/session%201/interrupt"+ assert forwarded["headers"]["X-Gateway-Token"] == "gateway-secret"+ assert forwarded["timeout"] == 30.0+++async def test_steer_proxy_forwards_message_body(monkeypatch):+ forwarded = {}++ class FakeResponse:+ status_code = 200+ text = '{"accepted": true}'++ def raise_for_status(self):+ return None++ def json(self):+ return {"accepted": True}++ class FakeClient:+ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, tb):+ return None++ async def post(self, url, json, headers, timeout):+ forwarded["url"] = url+ forwarded["json"] = json+ forwarded["headers"] = headers+ forwarded["timeout"] = timeout+ return FakeResponse()++ monkeypatch.setattr(bot_router_module, "BOT_API_URL", "http://127.0.0.1:18790")+ monkeypatch.setattr(bot_router_module, "BOT_API_KEY", "gateway-secret")+ monkeypatch.setattr(bot_router_module, "_create_bot_proxy_client", lambda: FakeClient())++ app = FastAPI()+ app.state.config = ServerConfig(+ auth_mode="trusted",+ host="127.0.0.1",+ port=1944,+ root_api_key="active-user-key",+ )+ app.state.auth_plugin = TrustedAuthPlugin()+ app.include_router(bot_router_module.router, prefix="/bot/v1")+ transport = httpx.ASGITransport(app=app)++ async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:+ response = await client.post(+ "/bot/v1/sessions/session%201/steer",+ headers={+ "X-API-Key": "active-user-key",+ "X-OpenViking-Account": "acct",+ "X-OpenViking-User": "alice",+ },+ json={"message": "follow up", "user_id": "ignored"},+ )++ assert response.status_code == 200+ assert response.json() == {"accepted": True}+ assert forwarded["url"] == "http://127.0.0.1:18790/bot/v1/sessions/session%201/steer"+ assert forwarded["json"]["message"] == "follow up"+ assert forwarded["json"]["openviking_connection"]["api_key"] == "active-user-key"+ assert forwarded["json"]["openviking_connection"]["user_id"] == "alice"+ assert forwarded["headers"]["X-Gateway-Token"] == "gateway-secret"+ assert forwarded["timeout"] == 30.0++@pytest.mark.asyncioasync def test_chat_proxy_attaches_authenticated_openviking_connection(monkeypatch):forwarded = {}diff --git a/tests/unit/test_vikingbot_agent_loop_interrupt.py b/tests/unit/test_vikingbot_agent_loop_interrupt.pynew file mode 100644index 000000000..4aa67bd5f--- /dev/null+++ b/tests/unit/test_vikingbot_agent_loop_interrupt.py@@ -0,0 +1,55 @@+import asyncio+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest+from vikingbot.agent.loop import AgentLoop+from vikingbot.bus.events import InboundMessage, OutboundMessage+from vikingbot.bus.queue import MessageBus+from vikingbot.config.schema import BotMode, SessionKey+++async def test_dequeued_message_is_active_before_interrupt_runs(monkeypatch):+ session_key = SessionKey(type="openapi", channel_id="test", chat_id="session")+ message = InboundMessage(sender_id="user", content="hello", session_key=session_key)+ original_wait_for = asyncio.wait_for++ async def task_wrapping_wait_for(awaitable, timeout=None):+ return await asyncio.create_task(awaitable)++ monkeypatch.setattr(asyncio, "wait_for", task_wrapping_wait_for)++ class InterruptingBus(MessageBus):+ interrupt_task: asyncio.Task[bool]++ async def consume_inbound(self) -> InboundMessage:+ msg = await super().consume_inbound()+ self.interrupt_task = asyncio.create_task(agent.interrupt(msg.session_key))+ return msg++ bus = InterruptingBus()+ agent = object.__new__(AgentLoop)+ agent.bus = bus+ agent.config = SimpleNamespace(mode=BotMode.NORMAL)+ agent._running = False+ agent._active_turn = None+ agent._queued_interrupt_counts = {}+ agent._connect_mcp = AsyncMock()++ async def process_message(*_args, **_kwargs):+ agent._running = False+ return OutboundMessage(session_key=session_key, content="completed")++ async def complete_interrupted(msg: InboundMessage):+ agent._running = False+ return OutboundMessage(session_key=msg.session_key, content="")++ agent._process_message = AsyncMock(side_effect=process_message)+ agent._complete_interrupted_message = AsyncMock(side_effect=complete_interrupted)++ await bus.publish_inbound(message)+ await original_wait_for(agent.run(), timeout=1)++ assert await bus.interrupt_task is True+ agent._complete_interrupted_message.assert_awaited_once_with(message)diff --git a/web-studio/src/lib/sessions/api.test.ts b/web-studio/src/lib/sessions/api.test.tsindex 073a037c8..b3531fcaf 100644--- a/web-studio/src/lib/sessions/api.test.ts+++ b/web-studio/src/lib/sessions/api.test.ts@@ -1,10 +1,12 @@import { beforeEach, describe, expect, it, vi } from 'vitest'-import { OvClientError } from '#/lib/ov-client'+import { OvClientError, ovClient } from '#/lib/ov-client'import {fetchSessionFirstTitle,fetchSessionMessages,+ interruptChatSession,+ steerChatSession,serializeParts,} from './api'@@ -408,3 +410,86 @@ describe('fetchSessionFirstTitle', () => {expect(getSessionBySessionIdMock).toHaveBeenCalledTimes(12)})})++describe('interruptChatSession', () => {+ beforeEach(() => {+ ovClient.setOptions({ baseUrl: 'http://localhost:1933' })+ ovClient.setConnection({+ accountId: 'account-1',+ apiKey: 'api-key-1',+ identityHeaders: true,+ userId: 'user-1',+ })+ })++ it('posts to the scoped bot interrupt endpoint with auth headers', async () => {+ const fetchMock = vi.fn().mockResolvedValue(+ new Response(JSON.stringify({ interrupted: true }), {+ headers: { 'Content-Type': 'application/json' },+ status: 200,+ }),+ )+ vi.stubGlobal('fetch', fetchMock)++ await expect(interruptChatSession('session 1')).resolves.toEqual({+ interrupted: true,+ })++ expect(fetchMock).toHaveBeenCalledWith(+ 'http://localhost:1933/bot/v1/sessions/session%201/interrupt',+ {+ headers: {+ 'Content-Type': 'application/json',+ 'X-API-Key': 'api-key-1',+ 'X-OpenViking-Account': 'account-1',+ 'X-OpenViking-User': 'user-1',+ },+ method: 'POST',+ },+ )+ })+})++describe('steerChatSession', () => {+ beforeEach(() => {+ ovClient.setOptions({ baseUrl: 'http://localhost:1933' })+ ovClient.setConnection({+ accountId: 'account-1',+ apiKey: 'api-key-1',+ identityHeaders: true,+ userId: 'user-1',+ })+ })++ it('posts the message to the scoped bot steer endpoint with auth headers', async () => {+ const fetchMock = vi.fn().mockResolvedValue(+ new Response(JSON.stringify({ accepted: true }), {+ headers: { 'Content-Type': 'application/json' },+ status: 200,+ }),+ )+ vi.stubGlobal('fetch', fetchMock)++ await expect(+ steerChatSession('session 1', { message: 'next input' }),+ ).resolves.toEqual({ accepted: true })++ expect(fetchMock).toHaveBeenCalledWith(+ 'http://localhost:1933/bot/v1/sessions/session%201/steer',+ {+ body: JSON.stringify({+ message: 'next input',+ session_id: 'session 1',+ user_id: 'user-1',+ }),+ headers: {+ 'Content-Type': 'application/json',+ 'X-API-Key': 'api-key-1',+ 'X-OpenViking-Account': 'account-1',+ 'X-OpenViking-User': 'user-1',+ },+ method: 'POST',+ },+ )+ })+})diff --git a/web-studio/src/lib/sessions/api.ts b/web-studio/src/lib/sessions/api.tsindex b423d3468..ae6d588f0 100644--- a/web-studio/src/lib/sessions/api.ts+++ b/web-studio/src/lib/sessions/api.ts@@ -503,6 +503,77 @@ export async function sendChat(return response.data as BotChatResponse}+export type InterruptChatSessionResult = {+ interrupted: boolean+}++export type SteerChatSessionResult = {+ accepted: boolean+}++export async function steerChatSession(+ sessionId: string,+ request: BotChatRequest,+): Promise<SteerChatSessionResult> {+ const baseUrl = ovClient.getOptions().baseUrl+ const conn = ovClient.getConnection()+ const response = await fetch(+ `${baseUrl}/bot/v1/sessions/${encodeURIComponent(sessionId)}/steer`,+ {+ method: 'POST',+ headers: buildFetchHeaders(),+ body: JSON.stringify({+ ...request,+ session_id: sessionId,+ user_id: request.user_id || conn.userId || undefined,+ }),+ },+ )++ if (!response.ok) {+ const text = await response.text().catch(() => '')+ throw new OvClientError({+ code: 'BOT_STEER_FAILED',+ message: extractErrorMessage(+ text,+ `Bot steer failed (${response.status})`,+ ),+ responseBody: text,+ statusCode: response.status,+ })+ }++ return response.json().catch(() => ({ accepted: false }))+}++export async function interruptChatSession(+ sessionId: string,+): Promise<InterruptChatSessionResult> {+ const baseUrl = ovClient.getOptions().baseUrl+ const response = await fetch(+ `${baseUrl}/bot/v1/sessions/${encodeURIComponent(sessionId)}/interrupt`,+ {+ method: 'POST',+ headers: buildFetchHeaders(),+ },+ )++ if (!response.ok) {+ const text = await response.text().catch(() => '')+ throw new OvClientError({+ code: 'BOT_INTERRUPT_FAILED',+ message: extractErrorMessage(+ text,+ `Bot interrupt failed (${response.status})`,+ ),+ responseBody: text,+ statusCode: response.status,+ })+ }++ return response.json().catch(() => ({ interrupted: false }))+}+// ---------------------------------------------------------------------------// Part serialization helpers (Message → API request format)// ---------------------------------------------------------------------------diff --git a/web-studio/src/lib/sessions/sse.ts b/web-studio/src/lib/sessions/sse.tsindex 56da258e5..65741da25 100644--- a/web-studio/src/lib/sessions/sse.ts+++ b/web-studio/src/lib/sessions/sse.ts@@ -6,6 +6,7 @@ import type { SseMessage } from '#/lib/sse'const VALID_EVENT_TYPES = new Set<string>(['response',+ 'interrupt','tool_call','tool_result','reasoning',diff --git a/web-studio/src/lib/sessions/use-chat-connection.test.tsx b/web-studio/src/lib/sessions/use-chat-connection.test.tsxindex dbea54445..4edb02308 100644--- a/web-studio/src/lib/sessions/use-chat-connection.test.tsx+++ b/web-studio/src/lib/sessions/use-chat-connection.test.tsx@@ -1,17 +1,21 @@// @vitest-environment jsdom-import { act, renderHook } from '@testing-library/react'+import { act, renderHook, waitFor } from '@testing-library/react'import { afterEach, expect, it, vi } from 'vitest'import { useChat } from './use-chat'import { ovClient } from '#/lib/ov-client'-const mocks = vi.hoisted(() => ({ stream: vi.fn(), postMessage: vi.fn() }))+const mocks = vi.hoisted(() => ({+ stream: vi.fn(),+ steer: vi.fn(),+ addMessage: vi.fn(),+ interrupt: vi.fn().mockResolvedValue({ interrupted: false }),+}))vi.mock('./api', async (original) => ({...(await original<Record<string, unknown>>()),+ addMessage: mocks.addMessage,+ interruptChatSession: mocks.interrupt,sendChatStream: mocks.stream,-}))-vi.mock('#/gen/ov-client/sdk.gen', async (original) => ({- ...(await original<Record<string, unknown>>()),- postSessionIdMessages: mocks.postMessage,+ steerChatSession: mocks.steer,}))vi.mock('./use-session-titles', () => ({ setSessionTitle: vi.fn() }))const originalOptions = ovClient.getOptions()@@ -40,9 +44,9 @@ it('does not persist a completed stream after its owner unmounts and the connect})()})const writes: string[] = []- mocks.postMessage.mockImplementation(async () => {+ mocks.addMessage.mockImplementation(async () => {writes.push(ovClient.getOptions().baseUrl)- return { data: { status: 'ok', result: {} } }+ return {}})ovClient.setOptions({ baseUrl: 'http://source.example' })const view = renderHook(() =>@@ -73,10 +77,10 @@ it('does not send the assistant message to a changed connection after the user w})(),)const writes: string[] = []- mocks.postMessage.mockImplementation(async () => {+ mocks.addMessage.mockImplementation(async () => {writes.push(ovClient.getOptions().baseUrl)ovClient.setOptions({ baseUrl: 'http://other.example' })- return { data: { status: 'ok', result: {} } }+ return {}})ovClient.setOptions({ baseUrl: 'http://source.example' })const view = renderHook(() =>@@ -88,3 +92,149 @@ it('does not send the assistant message to a changed connection after the user wexpect(writes).toEqual(['http://source.example'])view.unmount()})++it('does not interrupt a changed connection', async () => {+ let finish!: () => void+ const pause = new Promise<void>((resolve) => {+ finish = resolve+ })+ mocks.stream.mockResolvedValue(+ (async function* () {+ await pause+ yield* []+ })(),+ )+ mocks.interrupt.mockResolvedValue({ interrupted: false })+ ovClient.setOptions({ baseUrl: 'http://source.example' })+ const view = renderHook(() =>+ useChat({ sessionId: 'same-session-id', identityScopeKey: 'source-scope' }),+ )++ let sending!: Promise<void>+ await act(async () => {+ sending = view.result.current.send('synthetic source message')+ })+ ovClient.setOptions({ baseUrl: 'http://other.example' })+ act(() => {+ view.result.current.abort()+ })+ finish()+ await act(async () => {+ await sending+ })++ expect(mocks.interrupt).not.toHaveBeenCalled()+ view.unmount()+})++it('rejects steer input after the active stream connection changes', async () => {+ let finish!: () => void+ const pause = new Promise<void>((resolve) => {+ finish = resolve+ })+ mocks.stream.mockResolvedValue(+ (async function* () {+ await pause+ yield* []+ })(),+ )+ mocks.steer.mockResolvedValue({ accepted: true })+ ovClient.setOptions({ baseUrl: 'http://source.example' })+ const view = renderHook(() =>+ useChat({ sessionId: 'same-session-id', identityScopeKey: 'source-scope' }),+ )++ let sending!: Promise<void>+ await act(async () => {+ sending = view.result.current.send('synthetic source message')+ })+ await waitFor(() => expect(view.result.current.status).toBe('streaming'))++ ovClient.setOptions({ baseUrl: 'http://other.example' })+ await expect(view.result.current.send('synthetic follow-up')).rejects.toThrow(+ 'Active request belongs to a different connection',+ )++ expect(mocks.steer).not.toHaveBeenCalled()+ expect(view.result.current.messages).toHaveLength(1)++ finish()+ await act(async () => {+ await sending+ })+ view.unmount()+})++it('stops persisting the timeline when the connection changes between writes', async () => {+ let finishStream!: () => void+ const streamPaused = new Promise<void>((resolve) => {+ finishStream = resolve+ })+ let finishAssistantWrite!: () => void+ const assistantWritePaused = new Promise<void>((resolve) => {+ finishAssistantWrite = resolve+ })+ let assistantWriteStarted!: () => void+ const assistantWritePending = new Promise<void>((resolve) => {+ assistantWriteStarted = resolve+ })+ mocks.stream.mockResolvedValue(+ (async function* () {+ yield {+ data: JSON.stringify({+ event: 'content_delta',+ data: 'first segment',+ }),+ }+ await streamPaused+ yield {+ data: JSON.stringify({+ event: 'iteration',+ data: 'Iteration 2/10',+ }),+ }+ yield {+ data: JSON.stringify({+ event: 'response',+ data: 'final segment',+ }),+ }+ })(),+ )+ mocks.steer.mockResolvedValue({ accepted: true })+ const writes: string[] = []+ mocks.addMessage.mockImplementation(async () => {+ writes.push(ovClient.getOptions().baseUrl)+ if (writes.length === 2) {+ assistantWriteStarted()+ await assistantWritePaused+ }+ return {}+ })+ ovClient.setOptions({ baseUrl: 'http://source.example' })+ const view = renderHook(() =>+ useChat({ sessionId: 'same-session-id', identityScopeKey: 'source-scope' }),+ )++ let sending!: Promise<void>+ await act(async () => {+ sending = view.result.current.send('initial message')+ })+ await waitFor(() =>+ expect(view.result.current.streamingContent).toBe('first segment'),+ )+ await act(async () => {+ await view.result.current.send('first follow up')+ })+ await waitFor(() => expect(writes).toHaveLength(1))+ finishStream()+ await assistantWritePending+ ovClient.setOptions({ baseUrl: 'http://other.example' })+ finishAssistantWrite()+ await act(async () => {+ await sending+ })++ expect(writes).toEqual(['http://source.example', 'http://source.example'])+ view.unmount()+})diff --git a/web-studio/src/lib/sessions/use-chat-timeline.test.tsx b/web-studio/src/lib/sessions/use-chat-timeline.test.tsxindex ae17f28cc..a4fbc6dcb 100644--- a/web-studio/src/lib/sessions/use-chat-timeline.test.tsx+++ b/web-studio/src/lib/sessions/use-chat-timeline.test.tsx@@ -1,18 +1,28 @@// @vitest-environment jsdom-import { act, renderHook } from '@testing-library/react'+import { act, renderHook, waitFor } from '@testing-library/react'import { beforeEach, describe, expect, it, vi } from 'vitest'import { fetchSse } from '#/lib/sse'import { useChat } from './use-chat'-const { sendChatStreamMock } = vi.hoisted(() => ({+const {+ addMessageMock,+ interruptChatSessionMock,+ sendChatStreamMock,+ steerChatSessionMock,+} = vi.hoisted(() => ({+ addMessageMock: vi.fn(),+ interruptChatSessionMock: vi.fn(),sendChatStreamMock: vi.fn(),+ steerChatSessionMock: vi.fn(),}))vi.mock('./api', () => ({- addMessage: vi.fn(),+ addMessage: addMessageMock,+ interruptChatSession: interruptChatSessionMock,sendChatStream: sendChatStreamMock,+ steerChatSession: steerChatSessionMock,serializeParts: vi.fn(),}))@@ -41,9 +51,24 @@ function streamMessages(events: Array<{ event: string; data: unknown }>) {})}+function textMessage(id: string, role: 'user' | 'assistant', text: string) {+ return {+ id,+ role,+ parts: [{ type: 'text' as const, text }],+ created_at: '2026-01-01T00:00:00.000Z',+ }+}+describe('useChat event timeline', () => {beforeEach(() => {+ addMessageMock.mockReset()+ addMessageMock.mockResolvedValue({})+ interruptChatSessionMock.mockReset()+ interruptChatSessionMock.mockResolvedValue({ interrupted: false })sendChatStreamMock.mockReset()+ steerChatSessionMock.mockReset()+ steerChatSessionMock.mockResolvedValue({ accepted: true })vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {callback(0)return 1@@ -105,6 +130,62 @@ describe('useChat event timeline', () => {).toEqual([false, false])})+ it('starts a new request after a terminal response while persistence is pending', async () => {+ let releasePersistence!: () => void+ addMessageMock+ .mockResolvedValueOnce({})+ .mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releasePersistence = () => resolve({})+ }),+ )+ .mockResolvedValue({})+ sendChatStreamMock+ .mockResolvedValueOnce(+ streamMessages([{ event: 'response', data: 'first answer' }]),+ )+ .mockResolvedValueOnce(+ streamMessages([{ event: 'response', data: 'second answer' }]),+ )++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: true,+ sessionId: 'session-1',+ }),+ )++ let firstSend!: Promise<void>+ await act(async () => {+ firstSend = result.current.send('first question')+ })+ await waitFor(() => expect(result.current.status).toBe('idle'))++ let secondSend!: Promise<void>+ await act(async () => {+ secondSend = result.current.send('second question')+ await new Promise((resolve) => setTimeout(resolve, 0))+ })++ expect(steerChatSessionMock).not.toHaveBeenCalled()+ expect(sendChatStreamMock).toHaveBeenCalledTimes(2)+ expect(addMessageMock.mock.calls.map((call) => call[2])).toEqual([+ 'first question',+ undefined,+ ])++ releasePersistence()+ await Promise.all([firstSend, secondSend])+ expect(addMessageMock.mock.calls.map((call) => call[2])).toEqual([+ 'first question',+ undefined,+ 'second question',+ undefined,+ ])+ })+it('keeps only the latest reasoning block running', async () => {let controller!: ReadableStreamDefaultController<Uint8Array>const encoder = new TextEncoder()@@ -249,7 +330,646 @@ describe('useChat event timeline', () => {expect(streamReleased).toBe(true)})- it('keeps reasoning and tool timeline when aborted before content arrives', async () => {+ it('requests a backend interrupt before falling back to local abort', async () => {+ sendChatStreamMock.mockImplementation((_request, signal: AbortSignal) =>+ fetchSse('/stream', {+ signal,+ fetch: vi.fn().mockResolvedValue(+ new Response(new ReadableStream<Uint8Array>(), {+ headers: { 'Content-Type': 'text/event-stream' },+ }),+ ),+ }),+ )++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: false,+ sessionId: 'session-1',+ }),+ )++ let sendPromise!: Promise<void>+ await act(async () => {+ sendPromise = result.current.send('hello')+ await new Promise((resolve) => setTimeout(resolve, 0))+ result.current.abort()+ await sendPromise+ })++ expect(interruptChatSessionMock).toHaveBeenCalledWith('session-1')+ expect(result.current.status).toBe('idle')+ })++ it('stops streaming and persists only the user message when interrupted', async () => {+ let streamReleased = false+ async function* stream() {+ try {+ yield {+ data: JSON.stringify({+ event: 'interrupt',+ data: { reason: 'user' },+ }),+ event: '',+ id: '',+ }+ await new Promise(() => {})+ } finally {+ streamReleased = true+ }+ }+ sendChatStreamMock.mockResolvedValue(stream())++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: true,+ sessionId: 'session-1',+ }),+ )++ await act(async () => {+ await result.current.send('hello')+ })++ expect(result.current.status).toBe('idle')+ expect(result.current.streamingParts).toEqual([])+ expect(result.current.messages).toHaveLength(1)+ expect(result.current.messages[0]?.role).toBe('user')+ expect(addMessageMock).toHaveBeenCalledTimes(1)+ expect(addMessageMock).toHaveBeenCalledWith('session-1', 'user', 'hello')+ expect(streamReleased).toBe(true)+ })++ it('keeps accepted steer inputs visible when an acknowledged interrupt closes the stream', async () => {+ let streamController!: ReadableStreamDefaultController<Uint8Array>+ const encoder = new TextEncoder()+ interruptChatSessionMock.mockResolvedValueOnce({ interrupted: true })+ sendChatStreamMock.mockResolvedValue(+ fetchSse('/stream', {+ fetch: vi.fn().mockResolvedValue(+ new Response(+ new ReadableStream<Uint8Array>({+ start(controller) {+ streamController = controller+ },+ }),+ { headers: { 'Content-Type': 'text/event-stream' } },+ ),+ ),+ }),+ )++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: false,+ sessionId: 'session-1',+ }),+ )++ let sendPromise!: Promise<void>+ await act(async () => {+ sendPromise = result.current.send('A')+ })+ await waitFor(() => expect(result.current.status).toBe('streaming'))++ await act(async () => {+ streamController.enqueue(+ encoder.encode(+ `data: ${JSON.stringify({+ event: 'content_delta',+ data: 'partial',+ })}\n\n`,+ ),+ )+ await result.current.send('B')+ await result.current.send('D')+ })++ await act(async () => {+ result.current.abort()+ await new Promise((resolve) => setTimeout(resolve, 0))+ streamController.close()+ await sendPromise+ })++ expect(result.current.status).toBe('idle')+ expect(result.current.streamingInsertIndex).toBeUndefined()+ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['A', 'B', 'D'])+ })++ it('removes committed assistant segments when an acknowledged interrupt closes the stream', async () => {+ let streamController!: ReadableStreamDefaultController<Uint8Array>+ const encoder = new TextEncoder()+ interruptChatSessionMock.mockResolvedValueOnce({ interrupted: true })+ sendChatStreamMock.mockResolvedValue(+ fetchSse('/stream', {+ fetch: vi.fn().mockResolvedValue(+ new Response(+ new ReadableStream<Uint8Array>({+ start(controller) {+ streamController = controller+ },+ }),+ { headers: { 'Content-Type': 'text/event-stream' } },+ ),+ ),+ }),+ )++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: true,+ sessionId: 'session-1',+ }),+ )++ let sendPromise!: Promise<void>+ await act(async () => {+ sendPromise = result.current.send('A')+ })+ await waitFor(() => expect(result.current.status).toBe('streaming'))++ await act(async () => {+ streamController.enqueue(+ encoder.encode(+ `data: ${JSON.stringify({+ event: 'content_delta',+ data: 'partial',+ })}\n\n`,+ ),+ )+ await result.current.send('B')+ await result.current.send('D')+ })++ await act(async () => {+ streamController.enqueue(+ encoder.encode(+ `data: ${JSON.stringify({+ event: 'iteration',+ data: 'Iteration 2/10',+ })}\n\n`,+ ),+ )+ await new Promise((resolve) => setTimeout(resolve, 0))+ })++ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['A', 'partial', 'B', 'D'])++ await act(async () => {+ result.current.abort()+ await new Promise((resolve) => setTimeout(resolve, 0))+ streamController.close()+ await sendPromise+ })++ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['A', 'B', 'D'])+ expect(addMessageMock.mock.calls.map((call) => call[1])).toEqual([+ 'user',+ 'user',+ 'user',+ ])+ expect(addMessageMock.mock.calls.map((call) => call[2])).toEqual([+ 'A',+ 'B',+ 'D',+ ])+ })++ it('does not hide local steer inputs when a stale history snapshot arrives after interrupt', async () => {+ let streamController!: ReadableStreamDefaultController<Uint8Array>+ interruptChatSessionMock.mockResolvedValueOnce({ interrupted: true })+ sendChatStreamMock.mockResolvedValue(+ fetchSse('/stream', {+ fetch: vi.fn().mockResolvedValue(+ new Response(+ new ReadableStream<Uint8Array>({+ start(controller) {+ streamController = controller+ },+ }),+ { headers: { 'Content-Type': 'text/event-stream' } },+ ),+ ),+ }),+ )++ const { result, rerender } = renderHook(+ ({ initialMessages }) =>+ useChat({+ identityScopeKey: 'identity',+ initialMessages,+ persistMessages: false,+ sessionId: 'session-1',+ }),+ { initialProps: { initialMessages: [] } },+ )++ let sendPromise!: Promise<void>+ await act(async () => {+ sendPromise = result.current.send('A')+ })+ await waitFor(() => expect(result.current.status).toBe('streaming'))++ await act(async () => {+ await result.current.send('B')+ await result.current.send('D')+ result.current.abort()+ await new Promise((resolve) => setTimeout(resolve, 0))+ streamController.close()+ await sendPromise+ })++ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['A', 'B', 'D'])++ rerender({+ initialMessages: [+ textMessage('history-a', 'user', 'A'),+ textMessage('history-b', 'user', 'B'),+ ],+ })++ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['A', 'B', 'D'])+ })++ it('merges late initial history with local messages sent before history loads', async () => {+ sendChatStreamMock.mockResolvedValue(+ streamMessages([{ event: 'response', data: 'new answer' }]),+ )++ const { result, rerender } = renderHook(+ ({ initialMessages }) =>+ useChat({+ identityScopeKey: 'identity',+ initialMessages,+ persistMessages: false,+ sessionId: 'session-1',+ }),+ { initialProps: { initialMessages: undefined as Message[] | undefined } },+ )++ await act(async () => {+ await result.current.send('new question')+ })+ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['new question', 'new answer'])++ rerender({+ initialMessages: [+ textMessage('history-a', 'user', 'old question'),+ textMessage('history-b', 'assistant', 'old answer'),+ ],+ })++ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['old question', 'old answer', 'new question', 'new answer'])+ })++ it('keeps the steer boundary when the next iteration arrives before its ACK', async () => {+ let streamController!: ReadableStreamDefaultController<Uint8Array>+ let acceptSteer!: (result: { accepted: boolean }) => void+ const encoder = new TextEncoder()+ steerChatSessionMock.mockReturnValueOnce(+ new Promise((resolve) => {+ acceptSteer = resolve+ }),+ )+ sendChatStreamMock.mockResolvedValue(+ fetchSse('/stream', {+ fetch: vi.fn().mockResolvedValue(+ new Response(+ new ReadableStream<Uint8Array>({+ start(controller) {+ streamController = controller+ },+ }),+ { headers: { 'Content-Type': 'text/event-stream' } },+ ),+ ),+ }),+ )++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: true,+ sessionId: 'session-1',+ }),+ )++ let sendPromise!: Promise<void>+ let steerPromise!: Promise<void>+ await act(async () => {+ sendPromise = result.current.send('hello')+ })+ await waitFor(() => expect(result.current.status).toBe('streaming'))+ await act(async () => {+ streamController.enqueue(+ encoder.encode(+ `data: ${JSON.stringify({+ event: 'content_delta',+ data: 'before steer',+ })}\n\n`,+ ),+ )+ })+ await waitFor(() =>+ expect(result.current.streamingContent).toBe('before steer'),+ )+ await act(async () => {+ steerPromise = result.current.send('follow up')+ await new Promise((resolve) => setTimeout(resolve, 0))+ })++ expect(addMessageMock.mock.calls.map((call) => call[1])).toEqual(['user'])++ await act(async () => {+ streamController.enqueue(+ encoder.encode(+ [+ { event: 'iteration', data: 'Iteration 2/10' },+ { event: 'content_delta', data: 'after steer' },+ ]+ .map((event) => `data: ${JSON.stringify(event)}\n\n`)+ .join(''),+ ),+ )+ await new Promise((resolve) => setTimeout(resolve, 0))+ })++ await act(async () => {+ acceptSteer({ accepted: true })+ await steerPromise+ })++ await waitFor(() =>+ expect(+ result.current.messages.slice(1).map((message) => message.parts),+ ).toEqual([+ [{ type: 'text', text: 'before steer' }],+ [{ type: 'text', text: 'follow up' }],+ ]),+ )+ expect(result.current.streamingContent).toBe('after steer')++ await act(async () => {+ streamController.enqueue(+ encoder.encode(+ [{ event: 'response', data: 'answer' }]+ .map((event) => `data: ${JSON.stringify(event)}\n\n`)+ .join(''),+ ),+ )+ streamController.close()+ await sendPromise+ })++ expect(sendChatStreamMock).toHaveBeenCalledTimes(1)+ expect(steerChatSessionMock).toHaveBeenCalledWith('session-1', {+ message: 'follow up',+ })+ expect(addMessageMock.mock.calls.map((call) => call[1])).toEqual([+ 'user',+ 'assistant',+ 'user',+ 'assistant',+ ])+ expect(+ result.current.messages.slice(1).map((message) => message.parts),+ ).toEqual([+ [{ type: 'text', text: 'before steer' }],+ [{ type: 'text', text: 'follow up' }],+ [+ { type: 'iteration', iteration: 2 },+ { type: 'text', text: 'answer' },+ ],+ ])+ })++ it('keeps rejected steer input out of the timeline', async () => {+ let controller!: ReadableStreamDefaultController<Uint8Array>+ const encoder = new TextEncoder()+ sendChatStreamMock.mockResolvedValue(+ fetchSse('/stream', {+ fetch: vi.fn().mockResolvedValue(+ new Response(+ new ReadableStream<Uint8Array>({+ start(streamController) {+ controller = streamController+ },+ }),+ { headers: { 'Content-Type': 'text/event-stream' } },+ ),+ ),+ }),+ )+ steerChatSessionMock.mockRejectedValueOnce(new Error('turn closed'))++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: false,+ sessionId: 'session-1',+ }),+ )++ await act(async () => {+ void result.current.send('hello')+ controller.enqueue(+ encoder.encode(+ `data: ${JSON.stringify({+ event: 'content_delta',+ data: 'still streaming',+ })}\n\n`,+ ),+ )+ await new Promise((resolve) => setTimeout(resolve, 0))+ })+ await act(async () => {+ await expect(result.current.send('follow up')).rejects.toThrow(+ 'turn closed',+ )+ })++ expect(result.current.messages.map((message) => message.role)).toEqual([+ 'user',+ ])+ expect(result.current.streamingContent).toBe('still streaming')+ expect(result.current.streamingParts).toEqual([+ { type: 'text', text: 'still streaming' },+ ])+ expect(result.current.error).toBe('turn closed')++ await act(async () => {+ controller.close()+ })+ })++ it('ignores an aborted stream after switching sessions', async () => {+ let rejectOldStream!: () => void+ let newController!: ReadableStreamDefaultController<Uint8Array>+ let acceptSteer!: (result: { accepted: boolean }) => void+ const encoder = new TextEncoder()+ steerChatSessionMock.mockReturnValueOnce(+ new Promise((resolve) => {+ acceptSteer = resolve+ }),+ )+ async function* oldStream() {+ await new Promise<void>((resolve) => {+ rejectOldStream = resolve+ })+ yield* []+ }+ sendChatStreamMock.mockResolvedValueOnce(oldStream()).mockResolvedValueOnce(+ fetchSse('/stream', {+ fetch: vi.fn().mockResolvedValue(+ new Response(+ new ReadableStream<Uint8Array>({+ start(controller) {+ newController = controller+ },+ }),+ { headers: { 'Content-Type': 'text/event-stream' } },+ ),+ ),+ }),+ )++ const { result, rerender } = renderHook(+ ({ sessionId }) =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: true,+ sessionId,+ }),+ { initialProps: { sessionId: 'session-1' } },+ )++ let oldSend!: Promise<void>+ let steerSend!: Promise<void>+ await act(async () => {+ oldSend = result.current.send('old request')+ })+ await waitFor(() => expect(result.current.status).toBe('streaming'))+ await act(async () => {+ steerSend = result.current.send('follow up')+ await new Promise((resolve) => setTimeout(resolve, 0))+ })++ rerender({ sessionId: 'session-2' })+ let newSend!: Promise<void>+ await act(async () => {+ newSend = result.current.send('new request')+ newController.enqueue(+ encoder.encode(+ `data: ${JSON.stringify({+ event: 'content_delta',+ data: 'new answer',+ })}\n\n`,+ ),+ )+ })+ await waitFor(() =>+ expect(result.current.streamingContent).toBe('new answer'),+ )++ await act(async () => {+ acceptSteer({ accepted: true })+ rejectOldStream()+ await steerSend+ await oldSend+ })++ expect(result.current.status).toBe('streaming')+ expect(result.current.streamingContent).toBe('new answer')+ expect(result.current.error).toBeUndefined()+ expect(addMessageMock).toHaveBeenCalledWith(+ 'session-1',+ 'user',+ 'follow up',+ )++ await act(async () => {+ newController.close()+ await newSend+ })+ })++ it('ignores an aborted stream after resetting the same session', async () => {+ let releaseStream!: () => void+ async function* stream() {+ await new Promise<void>((resolve) => {+ releaseStream = resolve+ })+ yield* []+ }+ sendChatStreamMock.mockResolvedValue(stream())++ const { result } = renderHook(() =>+ useChat({+ identityScopeKey: 'identity',+ persistMessages: false,+ sessionId: 'session-1',+ }),+ )++ let sendPromise!: Promise<void>+ await act(async () => {+ sendPromise = result.current.send('first request')+ })+ await waitFor(() => expect(result.current.status).toBe('streaming'))+ await act(async () => {+ await result.current.send('follow up')+ })+ expect(+ result.current.messages.map(+ (message) => message.parts[0]?.type === 'text' && message.parts[0].text,+ ),+ ).toEqual(['first request', 'follow up'])++ await act(async () => {+ result.current.reset()+ releaseStream()+ await sendPromise+ })++ expect(result.current.status).toBe('idle')+ expect(result.current.messages).toEqual([])+ expect(result.current.streamingInsertIndex).toBeUndefined()+ })++ it('drops the unfinished assistant segment when aborted before content arrives', async () => {let controller!: ReadableStreamDefaultController<Uint8Array>const encoder = new TextEncoder()sendChatStreamMock.mockImplementation((_request, signal: AbortSignal) =>@@ -296,17 +1016,9 @@ describe('useChat event timeline', () => {expect(result.current.status).toBe('idle')expect(result.current.streamingParts).toEqual([])- expect(result.current.messages[1]?.parts).toMatchObject([- {- is_running: false,- reasoning: 'checking',- type: 'reasoning',- },- {- tool_name: 'search',- tool_status: 'running',- type: 'tool',- },+ expect(result.current.messages).toHaveLength(1)+ expect(result.current.messages[0]?.parts).toEqual([+ { type: 'text', text: 'hello' },])})})diff --git a/web-studio/src/lib/sessions/use-chat.ts b/web-studio/src/lib/sessions/use-chat.tsindex 00441fe13..e96f9e26e 100644--- a/web-studio/src/lib/sessions/use-chat.ts+++ b/web-studio/src/lib/sessions/use-chat.ts@@ -11,7 +11,13 @@ import type {ToolPart,ToolResultPart,} from './types/message'-import { addMessage, sendChatStream, serializeParts } from './api'+import {+ addMessage,+ interruptChatSession,+ sendChatStream,+ steerChatSession,+ serializeParts,+} from './api'import { parseSseStream, streamEventDataToText } from './sse'import { setSessionTitle } from './use-session-titles'import { createBrowserId } from '../browser-crypto'@@ -88,6 +94,18 @@ type SendOptions = {displayMessage?: string}+type AbortOptions = {+ discardPartial?: boolean+ closeLocalStream?: boolean+}++function isSupersededRequest(+ current: AbortController | null,+ expected: AbortController,+) {+ return current !== null && current !== expected+}+function buildAssistantMessage(content: string,toolCalls: StreamToolCall[],@@ -156,9 +174,10 @@ export interface UseChatReturn {streamingToolCalls: StreamToolCall[]streamingReasoning: stringstreamingParts: MessagePart[]+ streamingInsertIndex: number | undefinediteration: numbersend: (message: string, options?: SendOptions) => Promise<void>- abort: () => void+ abort: (options?: AbortOptions) => voidreset: () => voidsetMessages: React.Dispatch<React.SetStateAction<Message[]>>}@@ -180,13 +199,75 @@ export function useChat(options: UseChatOptions): UseChatReturn {>([])const [streamingReasoning, setStreamingReasoning] = useState('')const [streamingParts, setStreamingParts] = useState<MessagePart[]>([])+ const [streamingInsertIndex, setStreamingInsertIndex] = useState<+ number | undefined+ >(undefined)const [iteration, setIteration] = useState(0)const abortRef = useRef<AbortController | null>(null)+ const activeSessionRef = useRef<string | null>(null)+ const activeConnectionRef = useRef<string | null>(null)+ const sessionIdRef = useRef(sessionId)+ const appendInputRef = useRef<+ ((message: Message, accepted: Promise<boolean>) => Promise<boolean>) | null+ >(null)+ const persistenceTailRef = useRef<Promise<void>>(Promise.resolve())+ const discardPartialControllersRef = useRef(new WeakSet<AbortController>())+ const interruptedControllersRef = useRef(new WeakSet<AbortController>())const messagesRef = useRef<Message[]>(messages)+ const historyLoadedRef = useRef(Boolean(initialMessages))const lastSyncedInitialMessagesRef = useRef<Message[] | undefined>(undefined)const pendingInitialMessagesRef = useRef<Message[] | undefined>(undefined)messagesRef.current = messages+ sessionIdRef.current = sessionId++ const abort = useCallback((abortOptions?: AbortOptions) => {+ const controller = abortRef.current+ const activeSessionId = activeSessionRef.current+ if (!controller || !activeSessionId) return++ if (abortOptions?.discardPartial) {+ discardPartialControllersRef.current.add(controller)+ }++ const closeLocalStream = () => {+ controller.abort()+ if (abortRef.current === controller) {+ abortRef.current = null+ activeSessionRef.current = null+ activeConnectionRef.current = null+ appendInputRef.current = null+ }+ }++ const currentConnection = JSON.stringify([+ ovClient.getOptions(),+ ovClient.getConnection(),+ ])+ if (activeConnectionRef.current !== currentConnection) {+ closeLocalStream()+ return+ }++ void interruptChatSession(activeSessionId)+ .then((result) => {+ if (result.interrupted && abortRef.current === controller) {+ interruptedControllersRef.current.add(controller)+ }+ if (!result.interrupted && abortRef.current === controller) {+ closeLocalStream()+ }+ })+ .catch(() => {+ if (abortRef.current === controller) {+ closeLocalStream()+ }+ })++ if (abortOptions?.closeLocalStream) {+ closeLocalStream()+ }+ }, [])const ownerRef = useRef(0)@@ -201,8 +282,8 @@ export function useChat(options: UseChatOptions): UseChatReturn {// Reset state when sessionId changesuseEffect(() => {- abortRef.current?.abort()- abortRef.current = null+ abort({ closeLocalStream: true, discardPartial: true })+ historyLoadedRef.current = falselastSyncedInitialMessagesRef.current = undefinedpendingInitialMessagesRef.current = undefinedsetMessages([])@@ -212,8 +293,9 @@ export function useChat(options: UseChatOptions): UseChatReturn {setStreamingToolCalls([])setStreamingReasoning('')setStreamingParts([])+ setStreamingInsertIndex(undefined)setIteration(0)- }, [identityScopeKey, sessionId])+ }, [abort, identityScopeKey, sessionId])// Sync initialMessages into state when they load or when switching sessions.useEffect(() => {@@ -232,16 +314,15 @@ export function useChat(options: UseChatOptions): UseChatReturn {if (lastSyncedInitialMessagesRef.current === nextInitialMessages) returnlastSyncedInitialMessagesRef.current = nextInitialMessages- setMessages(nextInitialMessages)+ if (!historyLoadedRef.current) {+ setMessages((current) => [...nextInitialMessages, ...current])+ historyLoadedRef.current = true+ }}, [initialMessages, sessionId, status])- const abort = useCallback(() => {- abortRef.current?.abort()- abortRef.current = null- }, [])-const reset = useCallback(() => {- abort()+ abort({ closeLocalStream: true, discardPartial: true })+ historyLoadedRef.current = Boolean(initialMessages)setMessages(initialMessages ?? [])setStatus('idle')setError(undefined)@@ -249,17 +330,73 @@ export function useChat(options: UseChatOptions): UseChatReturn {setStreamingToolCalls([])setStreamingReasoning('')setStreamingParts([])+ setStreamingInsertIndex(undefined)setIteration(0)}, [abort, initialMessages])+ const queuePersistence = useCallback((operation: () => Promise<unknown>) => {+ const queued = persistenceTailRef.current.then(async () => {+ try {+ await operation()+ } catch {+ // Session persistence is best-effort and must not fail the chat stream.+ }+ })+ persistenceTailRef.current = queued+ return queued+ }, [])++ useEffect(+ () => () => {+ abort({ closeLocalStream: true, discardPartial: true })+ },+ [abort],+ )+const send = useCallback(async (message: string, sendOptions?: SendOptions) => {- if (status === 'streaming') return-const isFirstExchange = messagesRef.current.length === 0const displayMessage = sendOptions?.displayMessage ?? messageconst userMsg = createUserMessage(displayMessage)+ const connectionSnapshot = JSON.stringify([+ ovClient.getOptions(),+ ovClient.getConnection(),+ ])++ if (abortRef.current) {+ const activeController = abortRef.current+ if (activeConnectionRef.current !== connectionSnapshot) {+ throw new Error('Active request belongs to a different connection')+ }+ setError(undefined)+ const appendInput = appendInputRef.current+ if (!appendInput) {+ throw new Error('Session has no active request to steer')+ }+ let resolveAccepted!: (accepted: boolean) => void+ const accepted = new Promise<boolean>((resolve) => {+ resolveAccepted = resolve+ })+ const appended = appendInput(userMsg, accepted)+ try {+ const result = await steerChatSession(sessionId, { message })+ if (!result.accepted) throw new Error('Bot did not accept the input')+ resolveAccepted(true)+ await appended+ } catch (err) {+ resolveAccepted(false)+ if (+ abortRef.current === activeController &&+ sessionIdRef.current === sessionId+ ) {+ setError(err instanceof Error ? err.message : String(err))+ }+ throw err+ }+ return+ }+setMessages((prev) => [...prev, userMsg])setStatus('streaming')setError(undefined)@@ -267,27 +404,32 @@ export function useChat(options: UseChatOptions): UseChatReturn {setStreamingToolCalls([])setStreamingReasoning('')setStreamingParts([])+ setStreamingInsertIndex(messagesRef.current.length + 1)setIteration(0)const controller = new AbortController()- abortRef.current = controllerconst owner = ownerRef.current- const connectionSnapshot = JSON.stringify([- ovClient.getOptions(),- ovClient.getConnection(),- ])+ abortRef.current = controller+ activeSessionRef.current = sessionId+ activeConnectionRef.current = connectionSnapshot+ let streamingInsertIndexValue = messagesRef.current.length + 1const isCurrent = () =>owner === ownerRef.current &&connectionSnapshot ===JSON.stringify([ovClient.getOptions(), ovClient.getConnection()])- const canPersist = () => isCurrent() && !controller.signal.aborted+ const isSameConnection = () =>+ connectionSnapshot ===+ JSON.stringify([ovClient.getOptions(), ovClient.getConnection()])// Accumulators (mutable for performance during streaming)let accContent = ''let accReasoning = ''const accToolCalls: StreamToolCall[] = []const accParts: MessagePart[] = []- let lastToolCall: StreamToolCall | null = null+ const completedStreamMessages: Message[] = []+ const activeAssistantMessages = new Set<Message>()+ let pendingUserMessages: Message[] = []+ let flushedStreamMessageCount = 0let currentReasoningPart: ReasoningPart | null = nulllet currentReasoningHasDelta = falselet currentTextPart: TextPart | null = null@@ -295,6 +437,27 @@ export function useChat(options: UseChatOptions): UseChatReturn {let lastPaintAt = 0let publishScheduled = falselet publishFrameId: number | null = null+ let interrupted = false+ let timelineTail: Promise<void> = Promise.resolve()+ let streamStarted = false+ let initialUserPersistenceQueued = false+ let persistedInitialUserMessage = false+ const queueInitialUserPersistence = () => {+ if (+ !persistMessages ||+ !streamStarted ||+ initialUserPersistenceQueued ||+ persistedInitialUserMessage+ ) {+ return+ }+ initialUserPersistenceQueued = true+ void queuePersistence(async () => {+ if (!isCurrent() || controller.signal.aborted) return+ await addMessage(sessionId, 'user', displayMessage)+ persistedInitialUserMessage = true+ })+ }const publishStreamingPartsNow = () => {if (publishFrameId !== null && typeof window !== 'undefined') {@@ -315,6 +478,32 @@ export function useChat(options: UseChatOptions): UseChatReturn {publishFrameId = window.requestAnimationFrame(publishStreamingPartsNow)}+ const clearStreamingSegmentView = () => {+ if (publishFrameId !== null && typeof window !== 'undefined') {+ window.cancelAnimationFrame(publishFrameId)+ }+ publishFrameId = null+ publishScheduled = false+ setStreamingContent('')+ setStreamingToolCalls([])+ setStreamingReasoning('')+ setStreamingParts([])+ }++ const clearStreamingState = () => {+ clearStreamingSegmentView()+ setStreamingInsertIndex(undefined)+ }++ const finishActiveRequest = () => {+ appendInputRef.current = null+ if (abortRef.current === controller) {+ abortRef.current = null+ activeSessionRef.current = null+ activeConnectionRef.current = null+ }+ }+const yieldToRenderer = async () => {const now =typeof performance !== 'undefined' ? performance.now() : Date.now()@@ -367,6 +556,140 @@ export function useChat(options: UseChatOptions): UseChatReturn {publishStreamingPartsNow()}+ const resetAccumulators = () => {+ accContent = ''+ accReasoning = ''+ accToolCalls.length = 0+ accParts.length = 0+ currentReasoningPart = null+ currentReasoningHasDelta = false+ currentTextPart = null+ }++ const hasAssistantSegment = () =>+ accContent.length > 0 || accParts.length > 0 || accToolCalls.length > 0++ const finalizeAssistantSegment = () => {+ finishCurrentReasoning()+ if (!hasAssistantSegment()) return null+ const completedMessage = buildAssistantMessage(+ accContent,+ dedupeToolCalls(accToolCalls),+ accParts,+ )+ completedStreamMessages.push(completedMessage)+ resetAccumulators()+ return completedMessage+ }++ const flushCompletedStreamMessages = () => {+ const pendingMessages = completedStreamMessages.slice(+ flushedStreamMessageCount,+ )+ if (pendingMessages.length === 0) return+ flushedStreamMessageCount = completedStreamMessages.length+ const pendingAssistantMessages = pendingMessages.filter(+ (message) => message.role === 'assistant',+ )+ if (pendingAssistantMessages.length === 0) {+ streamingInsertIndexValue += pendingMessages.length+ setStreamingInsertIndex(streamingInsertIndexValue)+ return+ }+ for (const message of pendingAssistantMessages) {+ activeAssistantMessages.add(message)+ }+ setMessages((prev) => {+ const insertAt = Math.min(streamingInsertIndexValue, prev.length)+ const next = [+ ...prev.slice(0, insertAt),+ ...pendingAssistantMessages,+ ...prev.slice(insertAt),+ ]+ streamingInsertIndexValue = insertAt + pendingMessages.length+ setStreamingInsertIndex(streamingInsertIndexValue)+ return next+ })+ }++ const appendPendingUserMessagesToTimeline = () => {+ if (pendingUserMessages.length === 0) return+ completedStreamMessages.push(...pendingUserMessages)+ pendingUserMessages = []+ }++ const commitAssistantSegment = () => {+ finalizeAssistantSegment()+ appendPendingUserMessagesToTimeline()+ flushCompletedStreamMessages()+ clearStreamingSegmentView()+ }++ const dropStreamingSegment = () => {+ appendPendingUserMessagesToTimeline()+ resetAccumulators()+ clearStreamingState()+ if (activeAssistantMessages.size > 0) {+ setMessages((prev) =>+ prev.filter((message) => !activeAssistantMessages.has(message)),+ )+ }+ }++ const persistTimeline = (userOnly: boolean) => {+ if (!persistMessages) return Promise.resolve()+ const timeline = userOnly+ ? completedStreamMessages.filter((message) => message.role === 'user')+ : [...completedStreamMessages]+ return queuePersistence(async () => {+ if (+ !persistedInitialUserMessage &&+ !initialUserPersistenceQueued &&+ streamStarted+ ) {+ if (!isCurrent() || controller.signal.aborted) return+ await addMessage(sessionId, 'user', displayMessage)+ persistedInitialUserMessage = true+ }+ if (!isSameConnection()) return+ for (const completedMessage of timeline) {+ if (!isSameConnection()) return+ if (completedMessage.role === 'user') {+ await addMessage(+ sessionId,+ 'user',+ completedMessage.parts+ .filter((part): part is TextPart => part.type === 'text')+ .map((part) => part.text)+ .join('\n'),+ )+ } else if (!userOnly) {+ if (!isCurrent() || controller.signal.aborted) return+ await addMessage(+ sessionId,+ 'assistant',+ undefined,+ serializeParts(completedMessage.parts),+ )+ }+ }+ })+ }++ appendInputRef.current = (inputMessage, accepted) => {+ const decision = timelineTail.then(async () => {+ const wasAccepted = await accepted+ if (!wasAccepted) return false+ pendingUserMessages.push(inputMessage)+ if (abortRef.current === controller) {+ setMessages((prev) => [...prev, inputMessage])+ }+ return true+ })+ timelineTail = decision.then(() => undefined)+ return decision+ }+try {const response = await sendChatStream({ message, session_id: sessionId },@@ -375,9 +698,15 @@ export function useChat(options: UseChatOptions): UseChatReturn {stream: for await (const event of parseSseStream(response)) {if (controller.signal.aborted) break+ streamStarted = true+ queueInitialUserPersistence()switch (event.event) {case 'iteration': {+ await timelineTail+ if (pendingUserMessages.length > 0) {+ commitAssistantSegment()+ }const data = streamEventDataToText(event.data)const match = data.match(/(\d+)/)if (match) {@@ -448,16 +777,15 @@ export function useChat(options: UseChatOptions): UseChatReturn {tc.result === undefined,)if (duplicate) {- lastToolCall = duplicatesetStreamingToolCalls(dedupeToolCalls(accToolCalls))break}- lastToolCall = {+ const toolCall = {name,arguments: args,iteration: currentIteration,}- accToolCalls.push(lastToolCall)+ accToolCalls.push(toolCall)const toolPart: ToolPart = {type: 'tool',tool_id: createBrowserId('tool'),@@ -517,7 +845,17 @@ export function useChat(options: UseChatOptions): UseChatReturn {break}+ case 'interrupt': {+ appendInputRef.current = null+ await timelineTail+ dropStreamingSegment()+ interrupted = true+ break stream+ }+case 'response': {+ appendInputRef.current = null+ await timelineTail// Final complete response — overrides accumulated deltasaccContent = streamEventDataToText(event.data)setStreamingContent(accContent)@@ -527,40 +865,40 @@ export function useChat(options: UseChatOptions): UseChatReturn {}}+ // Build assistant message and finalize+ await timelineTail+ if (+ discardPartialControllersRef.current.has(controller) ||+ sessionIdRef.current !== sessionId ||+ isSupersededRequest(abortRef.current, controller)+ ) {+ appendPendingUserMessagesToTimeline()+ await persistTimeline(true)+ return+ }if (!isCurrent()) returnif (controller.signal.aborted)throw new DOMException('Aborted', 'AbortError')-- // Build assistant message and finalize+ appendInputRef.current = null+ if (interruptedControllersRef.current.has(controller)) {+ dropStreamingSegment()+ interrupted = true+ }finishCurrentReasoning()- const assistantMsg = buildAssistantMessage(- accContent,- dedupeToolCalls(accToolCalls),- accParts,- )- setStreamingContent('')- setStreamingToolCalls([])- setStreamingReasoning('')- setStreamingParts([])+ if (interrupted) {+ const persistence = persistTimeline(true)+ finishActiveRequest()+ setStatus('idle')+ await persistence+ return+ }+ commitAssistantSegment()+ const persistence = persistTimeline(false)+ finishActiveRequest()setStatus('idle')- setMessages((prev) => [...prev, assistantMsg])+ clearStreamingState()- // Persist to openviking session (bot doesn't do this automatically)- if (persistMessages && canPersist()) {- try {- // Sequential: user message must precede assistant message- await addMessage(sessionId, 'user', displayMessage)- if (!canPersist()) return- await addMessage(- sessionId,- 'assistant',- undefined,- serializeParts(assistantMsg.parts),- )- } catch {- // Persistence failure is non-blocking- }- }+ await persistence// Generate session title on first exchangeif (isCurrent() && sessionId && isFirstExchange) {@@ -572,38 +910,41 @@ export function useChat(options: UseChatOptions): UseChatReturn {)}} catch (err) {+ if (+ discardPartialControllersRef.current.has(controller) ||+ sessionIdRef.current !== sessionId ||+ isSupersededRequest(abortRef.current, controller)+ ) {+ await timelineTail+ appendPendingUserMessagesToTimeline()+ await persistTimeline(true)+ return+ }if (!isCurrent()) return+ appendInputRef.current = null+ await timelineTail+ dropStreamingSegment()if (controller.signal.aborted) {- // Aborted intentionally — still finalize any partial content- if (accContent || accParts.length > 0) {- finishCurrentReasoning()- const partialMsg = buildAssistantMessage(- accContent,- dedupeToolCalls(accToolCalls),- accParts,- )- setStreamingContent('')- setStreamingToolCalls([])- setStreamingReasoning('')- setStreamingParts([])- setMessages((prev) => [...prev, partialMsg])- } else {- setStreamingContent('')- setStreamingToolCalls([])- setStreamingReasoning('')- setStreamingParts([])- }setStatus('idle')+ await persistTimeline(true)} else {const msg = err instanceof Error ? err.message : String(err)setError(msg)setStatus('error')+ await persistTimeline(true)}} finally {- if (abortRef.current === controller) abortRef.current = null+ discardPartialControllersRef.current.delete(controller)+ interruptedControllersRef.current.delete(controller)+ if (abortRef.current === controller) {+ abortRef.current = null+ activeSessionRef.current = null+ activeConnectionRef.current = null+ appendInputRef.current = null+ }}},- [identityScopeKey, persistMessages, sessionId, status],+ [identityScopeKey, persistMessages, queuePersistence, sessionId],)return {@@ -614,6 +955,7 @@ export function useChat(options: UseChatOptions): UseChatReturn {streamingToolCalls,streamingReasoning,streamingParts,+ streamingInsertIndex,iteration,send,abort,diff --git a/web-studio/src/routes/filesystem/-components/agent-panel.tsx b/web-studio/src/routes/filesystem/-components/agent-panel.tsxindex 731b8ecb9..e6ce5a22b 100644--- a/web-studio/src/routes/filesystem/-components/agent-panel.tsx+++ b/web-studio/src/routes/filesystem/-components/agent-panel.tsx@@ -100,7 +100,7 @@ export function AgentPanel({const handleNewSession = useCallback(() => {if (isCreatingSession) returncreationGenerationRef.current += 1- chat.abort()+ chat.abort({ closeLocalStream: true, discardPartial: true })chat.setMessages([])creationStartedRef.current = falsesetSessionError(null)@@ -114,7 +114,7 @@ export function AgentPanel({const handleSwitchSession = useCallback((nextSessionId: string) => {creationGenerationRef.current += 1- chat.abort()+ chat.abort({ closeLocalStream: true, discardPartial: true })creationStartedRef.current = falsesetSessionError(null)setIsCreatingSession(false)@@ -146,6 +146,7 @@ export function AgentPanel({chat.streamingContent,chat.streamingReasoning,chat.streamingToolCalls,+ chat.streamingInsertIndex,])const send = useCallback(@@ -173,7 +174,10 @@ export function AgentPanel({}}}- void chat.send(message)+ const sending = chat.send(message)+ if (chat.status === 'streaming') {+ await sending+ }return true},[chat, createSession, onSessionChange, persistedSessionId, sessionId],@@ -244,6 +248,7 @@ export function AgentPanel({? {iteration: chat.iteration,parts: chat.streamingParts,+ insertIndex: chat.streamingInsertIndex,}: undefined}@@ -331,7 +336,10 @@ export function AgentPanel({)if (session.session_id === sessionId) {creationGenerationRef.current += 1- chat.abort()+ chat.abort({+ closeLocalStream: true,+ discardPartial: true,+ })chat.setMessages([])creationStartedRef.current = falsesetIsCreatingSession(false)diff --git a/web-studio/src/routes/sessions/-components/composer.test.tsx b/web-studio/src/routes/sessions/-components/composer.test.tsxnew file mode 100644index 000000000..3785c1353--- /dev/null+++ b/web-studio/src/routes/sessions/-components/composer.test.tsx@@ -0,0 +1,29 @@+// @vitest-environment jsdom++import { fireEvent, render, screen } from '@testing-library/react'+import { describe, expect, it, vi } from 'vitest'+import { Composer } from './composer'++vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}))++describe('Composer', () => {+ it('sends text while streaming and keeps the cancel control available', () => {+ const onSend = vi.fn()+ const onCancel = vi.fn()++ render(<Composer onSend={onSend} onCancel={onCancel} isStreaming />)++ fireEvent.change(screen.getByLabelText('chat.placeholder'), {+ target: { value: 'follow up' },+ })+ fireEvent.keyDown(screen.getByLabelText('chat.placeholder'), {+ key: 'Enter',+ shiftKey: false,+ })++ expect(onSend).toHaveBeenCalledWith('follow up')+ expect(screen.getByRole('button', { name: 'chat.cancel' })).toBeTruthy()+ })+})diff --git a/web-studio/src/routes/sessions/-components/composer.tsx b/web-studio/src/routes/sessions/-components/composer.tsxindex 6a25bb879..3a8efcd78 100644--- a/web-studio/src/routes/sessions/-components/composer.tsx+++ b/web-studio/src/routes/sessions/-components/composer.tsx@@ -33,7 +33,7 @@ export function Composer({const handleSend = useCallback(async () => {const trimmed = value.trim()- if (!trimmed || submittingRef.current || isStreaming) return+ if (!trimmed || submittingRef.current) returnsubmittingRef.current = truesetIsSubmitting(true)try {@@ -47,7 +47,7 @@ export function Composer({submittingRef.current = falsesetIsSubmitting(false)}- }, [value, onSend, isStreaming])+ }, [value, onSend])const handleKeyDown = useCallback((e: React.KeyboardEvent) => {@@ -56,10 +56,10 @@ export function Composer({if (e.key === 'Enter' && !e.shiftKey) {e.preventDefault()- if (!isStreaming) handleSend()+ void handleSend()}},- [isStreaming, handleSend],+ [handleSend],)return (@@ -96,11 +96,11 @@ export function Composer({/><divclassName={cn(- 'flex items-center justify-end',+ 'flex items-center justify-end gap-2',isCompact ? 'px-2.5 pb-2' : 'px-3 pb-2.5',)}>- {isStreaming ? (+ {isStreaming && (<buttontype="button"aria-label={t('chat.cancel')}@@ -113,7 +113,8 @@ export function Composer({><SquareIcon className="size-3.5" /></button>- ) : (+ )}+ {(!isStreaming || value.trim()) && (<buttontype="button"aria-label={t('chat.send')}diff --git a/web-studio/src/routes/sessions/-components/message-list.test.tsx b/web-studio/src/routes/sessions/-components/message-list.test.tsxindex 55dff0210..2295203b8 100644--- a/web-studio/src/routes/sessions/-components/message-list.test.tsx+++ b/web-studio/src/routes/sessions/-components/message-list.test.tsx@@ -133,6 +133,49 @@ describe('MessageList reasoning', () => {})})+describe('MessageList streaming insert position', () => {+ it('renders streaming assistant before later user messages', () => {+ const { container } = render(+ <MessageList+ messages={[+ {+ id: 'main-input',+ role: 'user',+ parts: [{ type: 'text', text: 'A' }],+ created_at: '2026-09-13T00:00:00Z',+ },+ {+ id: 'accepted-input-b',+ role: 'user',+ parts: [{ type: 'text', text: 'B' }],+ created_at: '2026-09-13T00:00:01Z',+ },+ {+ id: 'accepted-input-c',+ role: 'user',+ parts: [{ type: 'text', text: 'C' }],+ created_at: '2026-09-13T00:00:02Z',+ },+ ]}+ streaming={{+ iteration: 1,+ parts: [{ type: 'text', text: 'answer still streaming' }],+ insertIndex: 1,+ }}+ />,+ )++ const text = container.textContent+ expect(text.indexOf('A')).toBeLessThan(+ text.indexOf('answer still streaming'),+ )+ expect(text.indexOf('answer still streaming')).toBeLessThan(+ text.indexOf('B'),+ )+ expect(text.indexOf('B')).toBeLessThan(text.indexOf('C'))+ })+})+describe('MessageList images', () => {it('renders an image using the server message format', () => {const { container } = render(diff --git a/web-studio/src/routes/sessions/-components/message-list.tsx b/web-studio/src/routes/sessions/-components/message-list.tsxindex 79d8228f1..f7b3882de 100644--- a/web-studio/src/routes/sessions/-components/message-list.tsx+++ b/web-studio/src/routes/sessions/-components/message-list.tsx@@ -131,6 +131,7 @@ interface MessageListProps {messages: Message[]streaming?: {parts?: MessagePart[]+ insertIndex?: numberiteration: number}onResourceClick?: (uri: string) => void@@ -143,55 +144,55 @@ export function MessageList({streaming,}: MessageListProps) {const isExpanded = layout === 'expanded'- const safeMessages = groupMessages([- ...(Array.isArray(messages) ? messages : []),- ...(streaming- ? [- {- id: '__streaming__',- role: 'assistant' as const,- parts: streaming.parts ?? [],- created_at: new Date().toISOString(),- },- ]- : []),- ])- const streamingGroup =- streaming && safeMessages.at(-1)?.role === 'assistant'- ? safeMessages.pop()- : undefined+ const baseMessages = Array.isArray(messages) ? messages : []+ const insertIndex = streaming+ ? Math.min(+ streaming.insertIndex ?? baseMessages.length,+ baseMessages.length,+ )+ : baseMessages.length+ const beforeMessages = groupMessages(baseMessages.slice(0, insertIndex))+ const afterMessages = streaming+ ? groupMessages(baseMessages.slice(insertIndex))+ : []+ const safeMessages = streaming ? beforeMessages : groupMessages(baseMessages)++ const renderMessages = (items: Message[]) =>+ items.map((msg, idx) => {+ const prev = idx > 0 ? items[idx - 1] : null+ const sameRole =+ prev?.role === msg.role &&+ (!prev.turn_id || !msg.turn_id || prev.turn_id === msg.turn_id)+ return msg.role === 'user' ? (+ <UserMessage+ key={msg.id}+ message={msg}+ compact={sameRole}+ expanded={isExpanded}+ />+ ) : (+ <AssistantMessage+ key={msg.id}+ message={msg}+ compact={sameRole}+ expanded={isExpanded}+ onResourceClick={onResourceClick}+ />+ )+ })+return (<>- {safeMessages.map((msg, idx) => {- const prev = idx > 0 ? safeMessages[idx - 1] : null- const sameRole =- prev?.role === msg.role &&- (!prev.turn_id || !msg.turn_id || prev.turn_id === msg.turn_id)- return msg.role === 'user' ? (- <UserMessage- key={msg.id}- message={msg}- compact={sameRole}- expanded={isExpanded}- />- ) : (- <AssistantMessage- key={msg.id}- message={msg}- compact={sameRole}- expanded={isExpanded}- onResourceClick={onResourceClick}- />- )- })}+ {renderMessages(safeMessages)}{streaming && (<StreamingAssistantMessage{...streaming}- parts={streamingGroup?.parts ?? streaming.parts}+ parts={streaming.parts}expanded={isExpanded}onResourceClick={onResourceClick}/>)}+ {streaming && renderMessages(afterMessages)}</>)}diff --git a/web-studio/src/routes/sessions/-components/thread-draft.test.tsx b/web-studio/src/routes/sessions/-components/thread-draft.test.tsxindex 291c9c867..c3ed5a3ce 100644--- a/web-studio/src/routes/sessions/-components/thread-draft.test.tsx+++ b/web-studio/src/routes/sessions/-components/thread-draft.test.tsx@@ -29,7 +29,13 @@ vi.mock('#/lib/sessions/use-sessions', () => ({useSessionMessages: m.history,}))vi.mock('#/lib/sessions/use-chat', () => ({- useChat: () => ({ messages: [], status: 'idle', send: m.send }),+ useChat: () => ({+ messages: [],+ status: 'idle',+ streamingParts: [],+ streamingInsertIndex: undefined,+ send: m.send,+ }),}))vi.mock('./memory-impact', () => ({ MemoryImpact: () => null }))vi.mock('./composer', () => ({@@ -133,6 +139,14 @@ it('loads persisted history and continues after switching tabs', async () => {expect(m.send).toHaveBeenLastCalledWith('second')})+it('releases the composer after starting an existing session stream', async () => {+ m.send.mockReturnValue(new Promise<void>(() => {}))+ render(<Thread sessionId="existing" />)++ await expect(m.onSend!('first')).resolves.toBe(true)+ expect(m.send).toHaveBeenCalledWith('first')+})+it('preserves hosted session history without exposing a Bot composer or draft send', () => {m.provider = 'volcengine'm.history.mockReturnValue({ isPending: false, data: [] })diff --git a/web-studio/src/routes/sessions/-components/thread.tsx b/web-studio/src/routes/sessions/-components/thread.tsxindex 807c01852..4f9522b73 100644--- a/web-studio/src/routes/sessions/-components/thread.tsx+++ b/web-studio/src/routes/sessions/-components/thread.tsx@@ -88,7 +88,10 @@ function SessionThread({ sessionId, draft = false, onPersisted }: ThreadProps) {creating.current = false}}- void chat.send(message)+ const sending = chat.send(message)+ if (chat.status === 'streaming') {+ await sending+ }return true},[canChat, chat, createSession, sessionId, onPersisted],@@ -113,7 +116,7 @@ function SessionThread({ sessionId, draft = false, onPersisted }: ThreadProps) {scrollRafRef.current = requestAnimationFrame(() => {bottomRef.current?.scrollIntoView({ behavior: 'smooth' })})- }, [chat.messages.length, chat.streamingParts])+ }, [chat.messages.length, chat.streamingParts, chat.streamingInsertIndex])const [showBackground, setShowBackground] = useState(false)@@ -181,6 +184,7 @@ function SessionThread({ sessionId, draft = false, onPersisted }: ThreadProps) {isStreaming? {parts: chat.streamingParts,+ insertIndex: chat.streamingInsertIndex,iteration: chat.iteration,}: undefineddiff --git a/web-studio/types/ov-server/bot/v1/chat.ts b/web-studio/types/ov-server/bot/v1/chat.tsindex 8ac81bd5c..74b5489e7 100644--- a/web-studio/types/ov-server/bot/v1/chat.ts+++ b/web-studio/types/ov-server/bot/v1/chat.ts@@ -1,5 +1,6 @@export type ChatStreamEventType =| 'response'+ | 'interrupt'| 'tool_call'| 'tool_result'| 'reasoning'