mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-09 12:40:59 +00:00
fix(agent): harden runner delivery validation
This commit is contained in:
@@ -7,6 +7,9 @@ from unittest.mock import AsyncMock, Mock
|
||||
import pytest
|
||||
|
||||
import langbot_plugin.api.entities.builtin.platform.events as platform_events
|
||||
import langbot_plugin.api.entities.builtin.platform.entities as platform_entities
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
import langbot_plugin.api.entities.builtin.provider.message as provider_message
|
||||
from langbot.pkg.platform.sources import websocket_adapter as websocket_adapter_module
|
||||
from langbot.pkg.platform.sources.websocket_adapter import WebSocketAdapter, WebSocketMessage, WebSocketSession
|
||||
from langbot.pkg.platform.sources.websocket_manager import WebSocketConnectionManager, is_valid_session_id
|
||||
@@ -164,6 +167,88 @@ async def test_stable_session_launcher_resolves_to_active_connection(monkeypatch
|
||||
assert await manager.get_connection_by_session_id(session_id, 'pipeline-1') is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dashboard_reply_uses_event_pipeline_after_connection_closes(monkeypatch):
|
||||
manager = WebSocketConnectionManager()
|
||||
connection = await manager.add_connection(
|
||||
websocket=Mock(),
|
||||
pipeline_uuid='pipeline-1',
|
||||
session_type='person',
|
||||
)
|
||||
monkeypatch.setattr(websocket_adapter_module, 'ws_connection_manager', manager)
|
||||
|
||||
app = Mock()
|
||||
app.platform_mgr.websocket_proxy_bot.bot_entity = Mock(spec=[])
|
||||
adapter = WebSocketAdapter.model_construct(ap=app, logger=AsyncMock())
|
||||
message_source = platform_events.FriendMessage(
|
||||
sender=platform_entities.Friend(
|
||||
id=f'websocket_{connection.connection_id}',
|
||||
nickname='User',
|
||||
remark='User',
|
||||
),
|
||||
message_chain=platform_message.MessageChain([platform_message.Plain(text='hello')]),
|
||||
time=1,
|
||||
)
|
||||
object.__setattr__(message_source, '_langbot_pipeline_uuid', 'pipeline-1')
|
||||
await manager.remove_connection(connection.connection_id)
|
||||
|
||||
assert await adapter._get_message_context(message_source) == ('pipeline-1', None)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_late_final_events_update_one_stream_message(monkeypatch):
|
||||
manager = WebSocketConnectionManager()
|
||||
connection = await manager.add_connection(
|
||||
websocket=Mock(),
|
||||
pipeline_uuid='pipeline-1',
|
||||
session_type='person',
|
||||
)
|
||||
monkeypatch.setattr(websocket_adapter_module, 'ws_connection_manager', manager)
|
||||
|
||||
adapter = WebSocketAdapter.model_construct(ap=Mock(), logger=AsyncMock())
|
||||
adapter.websocket_person_session = WebSocketSession(id='person')
|
||||
adapter.websocket_group_session = WebSocketSession(id='group')
|
||||
message_source = platform_events.FriendMessage(
|
||||
sender=platform_entities.Friend(
|
||||
id=f'websocket_{connection.connection_id}',
|
||||
nickname='User',
|
||||
remark='User',
|
||||
),
|
||||
message_chain=platform_message.MessageChain([platform_message.Plain(text='hello')]),
|
||||
time=1,
|
||||
)
|
||||
first = provider_message.MessageChunk(
|
||||
role='assistant',
|
||||
content='first final',
|
||||
is_final=True,
|
||||
resp_message_id='response-1',
|
||||
)
|
||||
second = provider_message.MessageChunk(
|
||||
role='assistant',
|
||||
content='corrected final',
|
||||
is_final=True,
|
||||
resp_message_id='response-1',
|
||||
)
|
||||
|
||||
await adapter.reply_message_chunk(
|
||||
message_source,
|
||||
first,
|
||||
platform_message.MessageChain([platform_message.Plain(text='first final')]),
|
||||
is_final=True,
|
||||
)
|
||||
await adapter.reply_message_chunk(
|
||||
message_source,
|
||||
second,
|
||||
platform_message.MessageChain([platform_message.Plain(text='corrected final')]),
|
||||
is_final=True,
|
||||
)
|
||||
|
||||
messages = adapter.get_websocket_messages('pipeline-1', 'person')
|
||||
assert len(messages) == 1
|
||||
assert messages[0]['content'] == 'corrected final'
|
||||
assert messages[0]['is_final'] is True
|
||||
|
||||
|
||||
def test_session_ids_must_be_canonical_random_uuids():
|
||||
assert is_valid_session_id('31c0f2e9-b115-4ee6-8f15-3e624d6456b1')
|
||||
assert not is_valid_session_id('session-a')
|
||||
|
||||
Reference in New Issue
Block a user