feat(agent-runner): integrate AgentRunner Protocol v1 with plugin system

Phase 0 integration complete - verified minimal loop with local-agent stub runner.

Changes:
- Add AgentRunOrchestrator for plugin-based agent execution
- Add AgentResultNormalizer for Protocol v1 result conversion
- Add AgentRunnerDescriptor for runner ID parsing (plugin:author/name/runner)
- Update chat handler to use new orchestrator instead of direct runner lookup
- Add plugin handler methods for list_agent_runners and run_agent
- Add connector methods for AgentRunner protocol forwarding
- Update pipeline API to include runner options in metadata
- Add integration docs and implementation plan

Integration verified:
- Runner: plugin:langbot/local-agent/default
- Input: "你好"
- Output: [stub] Echo: 你好
- Date: 2026-05-10 10:09

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
huanghuoguoguo
2026-05-10 10:11:54 +08:00
parent b7dcda8b23
commit 5aaa422250
29 changed files with 3955 additions and 298 deletions

View File

@@ -0,0 +1,2 @@
"""Tests for agent runner subsystem."""
from __future__ import annotations

View File

@@ -0,0 +1,553 @@
"""Tests for ChatMessageHandler behavior with AgentRunOrchestrator.
Tests focus on:
- Streaming mode behavior (single resp_message_id, pop/append pattern)
- Non-streaming mode behavior (no pop)
- Orchestrator invocation
- Error handling for RunnerNotFoundError, RunnerExecutionError
Avoids circular imports by using proper import structure.
"""
from __future__ import annotations
import uuid
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from langbot.pkg.agent.runner.errors import (
RunnerNotFoundError,
RunnerExecutionError,
RunnerNotAuthorizedError,
)
from langbot.pkg.agent.runner.config_migration import ConfigMigration
# Define mock classes in dependency order (no forward references needed)
class MockLauncherType:
value = 'person'
class MockConversation:
uuid = 'conv-uuid'
messages = []
class MockMessage:
role = 'user'
content = 'Hello'
class MockAdapter:
is_stream = False
async def is_stream_output_supported(self):
return self.is_stream
async def create_message_card(self, resp_message_id, message_event):
pass
class MockSession:
launcher_type = MockLauncherType()
launcher_id = 'user123'
using_conversation = MockConversation()
class MockQuery:
"""Mock Query for testing."""
def __init__(self):
self.query_id = 1
self.launcher_type = MockLauncherType()
self.launcher_id = 'user123'
self.sender_id = 'user123'
self.bot_uuid = 'bot-uuid'
self.pipeline_uuid = 'pipeline-uuid'
self.pipeline_config = {
'ai': {
'runner': {
'id': 'plugin:langbot/local-agent/default',
},
'runner_config': {},
},
'output': {
'misc': {
'exception-handling': 'show-hint',
'failure-hint': 'Request failed.',
},
},
}
self.variables = {}
self.session = MockSession()
self.user_message = MockMessage()
self.messages = []
self.resp_messages = []
self.resp_message_chain = None
self.adapter = MockAdapter()
self.message_event = MagicMock()
self.message_chain = MagicMock()
class MockMessageChunk:
"""Mock MessageChunk for testing."""
def __init__(self, content, resp_message_id=None):
self.role = 'assistant'
self.content = content
self.resp_message_id = resp_message_id
self.is_final = False
def readable_str(self):
return self.content
class MockEventContext:
"""Mock event context for testing."""
def __init__(self, prevented=False, reply_message_chain=None, user_message_alter=None):
self._prevented = prevented
self.event = MagicMock()
self.event.reply_message_chain = reply_message_chain
self.event.user_message_alter = user_message_alter
def is_prevented_default(self):
return self._prevented
class MockAgentRunOrchestrator:
"""Mock AgentRunOrchestrator for testing."""
def __init__(self, chunks=None, error=None):
self._chunks = chunks or []
self._error = error
async def run_from_query(self, query):
"""Async generator that yields chunks or raises error."""
if self._error:
raise self._error
for chunk in self._chunks:
yield chunk
def resolve_runner_id_for_telemetry(self, query):
return 'plugin:langbot/local-agent/default'
class MockApplication:
"""Mock Application for testing."""
def __init__(self, orchestrator=None):
self.agent_run_orchestrator = orchestrator or MockAgentRunOrchestrator()
self.logger = MagicMock()
self.logger.info = MagicMock()
self.logger.debug = MagicMock()
self.logger.warning = MagicMock()
self.logger.error = MagicMock()
# Mock plugin_connector
self.plugin_connector = MagicMock()
self.plugin_connector.emit_event = AsyncMock(return_value=MockEventContext())
# Mock telemetry
self.telemetry = MagicMock()
self.telemetry.start_send_task = AsyncMock()
# Mock survey
self.survey = MagicMock()
self.survey.trigger_event = AsyncMock()
# Mock model_mgr
self.model_mgr = MagicMock()
self.model_mgr.get_model_by_uuid = AsyncMock(return_value=None)
class TestStreamingBehavior:
"""Tests for streaming mode behavior."""
def test_single_resp_message_id_for_streaming(self):
"""Streaming mode should use single resp_message_id for entire response."""
# Simulate the streaming logic: resp_message_id created outside loop
resp_message_id = uuid.uuid4()
chunks = ['Hello', ' World', '!']
resp_messages = []
for chunk in chunks:
result = MockMessageChunk(chunk)
result.resp_message_id = str(resp_message_id)
# Pop old chunk (streaming behavior)
if resp_messages:
resp_messages.pop()
resp_messages.append(result)
# All chunks should have same resp_message_id
assert len(resp_messages) == 1 # Only last chunk remains after pop/append
assert resp_messages[0].resp_message_id == str(resp_message_id)
def test_pop_before_append_in_streaming(self):
"""Streaming mode should pop old chunk before appending new."""
resp_message_id = uuid.uuid4()
resp_messages = []
# First chunk - no pop
chunk1 = MockMessageChunk('Hello')
chunk1.resp_message_id = str(resp_message_id)
resp_messages.append(chunk1)
assert len(resp_messages) == 1
# Second chunk - pop first, then append
if resp_messages:
resp_messages.pop()
chunk2 = MockMessageChunk('Hello World')
chunk2.resp_message_id = str(resp_message_id)
resp_messages.append(chunk2)
assert len(resp_messages) == 1
assert resp_messages[0].content == 'Hello World'
def test_non_streaming_no_pop(self):
"""Non-streaming mode should NOT pop previous responses."""
resp_messages = []
# First message
msg1 = MockMessageChunk('Response 1')
resp_messages.append(msg1)
assert len(resp_messages) == 1
# Second message - should NOT pop in non-streaming
msg2 = MockMessageChunk('Response 2')
resp_messages.append(msg2)
assert len(resp_messages) == 2
class TestConfigMigrationInChatHandler:
"""Tests for ConfigMigration usage in chat handler context."""
def test_resolve_runner_id_from_pipeline_config(self):
"""Chat handler should use ConfigMigration to resolve runner ID."""
pipeline_config = {
'ai': {
'runner': {
'id': 'plugin:langbot/local-agent/default',
},
},
}
runner_id = ConfigMigration.resolve_runner_id(pipeline_config)
assert runner_id == 'plugin:langbot/local-agent/default'
def test_resolve_runner_id_from_old_format(self):
"""ConfigMigration should handle old runner format."""
pipeline_config = {
'ai': {
'runner': {
'runner': 'local-agent',
},
},
}
runner_id = ConfigMigration.resolve_runner_id(pipeline_config)
assert runner_id == 'plugin:langbot/local-agent/default'
class TestErrorHandling:
"""Tests for orchestrator error handling."""
def test_runner_not_found_error_properties(self):
"""RunnerNotFoundError should have runner_id property."""
error = RunnerNotFoundError('plugin:notexist/unknown/default')
assert error.runner_id == 'plugin:notexist/unknown/default'
assert 'not found' in str(error)
def test_runner_execution_error_retryable(self):
"""RunnerExecutionError should have retryable property."""
error = RunnerExecutionError(
'plugin:langbot/local-agent/default',
'Upstream timeout',
retryable=True,
)
assert error.runner_id == 'plugin:langbot/local-agent/default'
assert error.retryable is True
assert 'timeout' in str(error)
def test_runner_execution_error_not_retryable(self):
"""RunnerExecutionError can be non-retryable."""
error = RunnerExecutionError(
'plugin:langbot/local-agent/default',
'Configuration error',
retryable=False,
)
assert error.retryable is False
def test_runner_not_authorized_error_properties(self):
"""RunnerNotAuthorizedError should have bound_plugins property."""
error = RunnerNotAuthorizedError(
'plugin:langbot/local-agent/default',
['langbot/dify-agent'],
)
assert error.runner_id == 'plugin:langbot/local-agent/default'
assert error.bound_plugins == ['langbot/dify-agent']
class TestChatHandlerImports:
"""Test that chat handler can be imported without circular import."""
def test_import_chat_handler_module(self):
"""Import chat handler module should work."""
# This test verifies the import works without circular dependency
from langbot.pkg.pipeline.process.handlers import chat
assert chat.ChatMessageHandler is not None
def test_chat_handler_class_exists(self):
"""ChatMessageHandler class should be defined."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
assert ChatMessageHandler.__name__ == 'ChatMessageHandler'
def test_chat_handler_has_handle_method(self):
"""ChatMessageHandler should have async generator handle method."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
assert hasattr(ChatMessageHandler, 'handle')
# handle returns AsyncGenerator, so check for async generator function
import inspect
assert inspect.isasyncgenfunction(ChatMessageHandler.handle)
class TestChatHandlerAsyncBehavior:
"""Real async tests for ChatMessageHandler.handle() with mocked orchestrator."""
@pytest.mark.asyncio
async def test_streaming_single_resp_message_id(self):
"""Streaming mode: all chunks should have same resp_message_id."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
from langbot.pkg.pipeline import entities
# Create chunks for streaming
chunks = [
MockMessageChunk('Hello'),
MockMessageChunk('Hello World'),
MockMessageChunk('Hello World!'),
]
orchestrator = MockAgentRunOrchestrator(chunks=chunks)
mock_ap = MockApplication(orchestrator=orchestrator)
# Mock event context to not prevent default
event_ctx = MockEventContext(prevented=False)
mock_ap.plugin_connector.emit_event = AsyncMock(return_value=event_ctx)
query = MockQuery()
query.adapter.is_stream = True # Enable streaming mode
handler = ChatMessageHandler(mock_ap)
# Mock event creation and StageProcessResult to bypass pydantic validation
mock_event = MagicMock()
mock_event.return_value = MagicMock()
def make_result(*args, **kwargs):
return MagicMock(result_type=kwargs.get('result_type', entities.ResultType.CONTINUE))
with patch('langbot.pkg.pipeline.process.handlers.chat.events') as mock_events_module, \
patch('langbot.pkg.pipeline.entities.StageProcessResult', side_effect=make_result):
mock_events_module.PersonNormalMessageReceived = mock_event
mock_events_module.GroupNormalMessageReceived = mock_event
results = []
async for result in handler.handle(query):
results.append(result)
# Verify single resp_message_id
resp_ids = [msg.resp_message_id for msg in query.resp_messages if hasattr(msg, 'resp_message_id')]
assert len(set(resp_ids)) == 1 # All same ID
# Verify pop/append pattern: only last chunk remains
assert len(query.resp_messages) == 1
assert query.resp_messages[0].content == 'Hello World!'
@pytest.mark.asyncio
async def test_non_streaming_no_pop(self):
"""Non-streaming mode: all chunks should remain."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
from langbot.pkg.pipeline import entities
chunks = [
MockMessageChunk('Response 1'),
MockMessageChunk('Response 2'),
]
orchestrator = MockAgentRunOrchestrator(chunks=chunks)
mock_ap = MockApplication(orchestrator=orchestrator)
mock_ap.plugin_connector.emit_event = AsyncMock(return_value=MockEventContext(prevented=False))
query = MockQuery()
query.adapter.is_stream = False # Disable streaming mode
handler = ChatMessageHandler(mock_ap)
mock_event = MagicMock()
mock_event.return_value = MagicMock()
def make_result(*args, **kwargs):
return MagicMock(result_type=kwargs.get('result_type', entities.ResultType.CONTINUE))
with patch('langbot.pkg.pipeline.process.handlers.chat.events') as mock_events_module, \
patch('langbot.pkg.pipeline.entities.StageProcessResult', side_effect=make_result):
mock_events_module.PersonNormalMessageReceived = mock_event
mock_events_module.GroupNormalMessageReceived = mock_event
results = []
async for result in handler.handle(query):
results.append(result)
# No pop: all chunks should remain
assert len(query.resp_messages) == 2
assert query.resp_messages[0].content == 'Response 1'
assert query.resp_messages[1].content == 'Response 2'
@pytest.mark.asyncio
async def test_runner_not_found_error(self):
"""Handler should catch RunnerNotFoundError and return INTERRUPT."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
from langbot.pkg.pipeline import entities
orchestrator = MockAgentRunOrchestrator(
error=RunnerNotFoundError('plugin:notexist/unknown/default')
)
mock_ap = MockApplication(orchestrator=orchestrator)
mock_ap.plugin_connector.emit_event = AsyncMock(return_value=MockEventContext(prevented=False))
query = MockQuery()
handler = ChatMessageHandler(mock_ap)
mock_event = MagicMock()
mock_event.return_value = MagicMock()
def make_result(*args, **kwargs):
return MagicMock(
result_type=kwargs.get('result_type'),
user_notice=kwargs.get('user_notice'),
)
with patch('langbot.pkg.pipeline.process.handlers.chat.events') as mock_events_module, \
patch('langbot.pkg.pipeline.entities.StageProcessResult', side_effect=make_result):
mock_events_module.PersonNormalMessageReceived = mock_event
mock_events_module.GroupNormalMessageReceived = mock_event
results = []
async for result in handler.handle(query):
results.append(result)
# Should return INTERRUPT with user_notice
assert len(results) == 1
assert results[0].result_type == entities.ResultType.INTERRUPT
assert 'not found' in results[0].user_notice
@pytest.mark.asyncio
async def test_runner_not_authorized_error(self):
"""Handler should catch RunnerNotAuthorizedError and return INTERRUPT."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
from langbot.pkg.pipeline import entities
orchestrator = MockAgentRunOrchestrator(
error=RunnerNotAuthorizedError('plugin:langbot/local-agent/default', ['other/plugin'])
)
mock_ap = MockApplication(orchestrator=orchestrator)
mock_ap.plugin_connector.emit_event = AsyncMock(return_value=MockEventContext(prevented=False))
query = MockQuery()
handler = ChatMessageHandler(mock_ap)
mock_event = MagicMock()
mock_event.return_value = MagicMock()
def make_result(*args, **kwargs):
return MagicMock(
result_type=kwargs.get('result_type'),
user_notice=kwargs.get('user_notice'),
)
with patch('langbot.pkg.pipeline.process.handlers.chat.events') as mock_events_module, \
patch('langbot.pkg.pipeline.entities.StageProcessResult', side_effect=make_result):
mock_events_module.PersonNormalMessageReceived = mock_event
mock_events_module.GroupNormalMessageReceived = mock_event
results = []
async for result in handler.handle(query):
results.append(result)
assert len(results) == 1
assert results[0].result_type == entities.ResultType.INTERRUPT
assert 'not authorized' in results[0].user_notice
@pytest.mark.asyncio
async def test_runner_execution_error_retryable(self):
"""Handler should catch retryable RunnerExecutionError."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
from langbot.pkg.pipeline import entities
orchestrator = MockAgentRunOrchestrator(
error=RunnerExecutionError('plugin:langbot/local-agent/default', 'timeout', retryable=True)
)
mock_ap = MockApplication(orchestrator=orchestrator)
mock_ap.plugin_connector.emit_event = AsyncMock(return_value=MockEventContext(prevented=False))
query = MockQuery()
handler = ChatMessageHandler(mock_ap)
mock_event = MagicMock()
mock_event.return_value = MagicMock()
def make_result(*args, **kwargs):
return MagicMock(
result_type=kwargs.get('result_type'),
user_notice=kwargs.get('user_notice'),
)
with patch('langbot.pkg.pipeline.process.handlers.chat.events') as mock_events_module, \
patch('langbot.pkg.pipeline.entities.StageProcessResult', side_effect=make_result):
mock_events_module.PersonNormalMessageReceived = mock_event
mock_events_module.GroupNormalMessageReceived = mock_event
results = []
async for result in handler.handle(query):
results.append(result)
assert len(results) == 1
assert results[0].result_type == entities.ResultType.INTERRUPT
assert 'temporarily unavailable' in results[0].user_notice
@pytest.mark.asyncio
async def test_prevented_default_with_reply(self):
"""When event prevented default with reply, use reply message."""
from langbot.pkg.pipeline.process.handlers.chat import ChatMessageHandler
from langbot.pkg.pipeline import entities
# Mock reply message chain
reply_chain = MockMessageChunk('Reply from plugin')
mock_ap = MockApplication()
mock_ap.plugin_connector.emit_event = AsyncMock(
return_value=MockEventContext(prevented=True, reply_message_chain=reply_chain)
)
query = MockQuery()
handler = ChatMessageHandler(mock_ap)
mock_event = MagicMock()
mock_event.return_value = MagicMock()
def make_result(*args, **kwargs):
return MagicMock(result_type=kwargs.get('result_type', entities.ResultType.CONTINUE))
with patch('langbot.pkg.pipeline.process.handlers.chat.events') as mock_events_module, \
patch('langbot.pkg.pipeline.entities.StageProcessResult', side_effect=make_result):
mock_events_module.PersonNormalMessageReceived = mock_event
mock_events_module.GroupNormalMessageReceived = mock_event
results = []
async for result in handler.handle(query):
results.append(result)
# Should return CONTINUE with reply message
assert len(results) == 1
assert results[0].result_type == entities.ResultType.CONTINUE
assert len(query.resp_messages) == 1

View File

@@ -0,0 +1,231 @@
"""Tests for agent runner config migration."""
from __future__ import annotations
from langbot.pkg.agent.runner.config_migration import (
ConfigMigration,
OLD_RUNNER_TO_PLUGIN_RUNNER_ID,
)
class TestOldRunnerMapping:
"""Tests for OLD_RUNNER_TO_PLUGIN_RUNNER_ID mapping."""
def test_local_agent_mapping(self):
"""Local-agent should map to official plugin."""
assert OLD_RUNNER_TO_PLUGIN_RUNNER_ID['local-agent'] == 'plugin:langbot/local-agent/default'
def test_dify_mapping(self):
"""Dify should map to official plugin."""
assert OLD_RUNNER_TO_PLUGIN_RUNNER_ID['dify-service-api'] == 'plugin:langbot/dify-agent/default'
def test_n8n_mapping(self):
"""n8n should map to official plugin."""
assert OLD_RUNNER_TO_PLUGIN_RUNNER_ID['n8n-service-api'] == 'plugin:langbot/n8n-agent/default'
def test_coze_mapping(self):
"""Coze should map to official plugin."""
assert OLD_RUNNER_TO_PLUGIN_RUNNER_ID['coze-api'] == 'plugin:langbot/coze-agent/default'
def test_all_runners_mapped(self):
"""All old runners should have mapping."""
expected_runners = [
'local-agent',
'dify-service-api',
'n8n-service-api',
'coze-api',
'dashscope-app-api',
'langflow-api',
'tbox-app-api',
]
for runner in expected_runners:
assert runner in OLD_RUNNER_TO_PLUGIN_RUNNER_ID
mapped = OLD_RUNNER_TO_PLUGIN_RUNNER_ID[runner]
assert mapped.startswith('plugin:langbot/')
assert mapped.endswith('/default')
class TestResolveRunnerId:
"""Tests for ConfigMigration.resolve_runner_id."""
def test_resolve_new_format_runner_id(self):
"""Resolve runner ID from new format."""
pipeline_config = {
'ai': {
'runner': {
'id': 'plugin:langbot/local-agent/default',
},
},
}
runner_id = ConfigMigration.resolve_runner_id(pipeline_config)
assert runner_id == 'plugin:langbot/local-agent/default'
def test_resolve_old_format_runner_name(self):
"""Resolve runner ID from old format."""
pipeline_config = {
'ai': {
'runner': {
'runner': 'local-agent',
},
},
}
runner_id = ConfigMigration.resolve_runner_id(pipeline_config)
assert runner_id == 'plugin:langbot/local-agent/default'
def test_resolve_old_format_plugin_runner(self):
"""Resolve already migrated plugin:* runner."""
pipeline_config = {
'ai': {
'runner': {
'runner': 'plugin:alice/my-agent/custom',
},
},
}
runner_id = ConfigMigration.resolve_runner_id(pipeline_config)
assert runner_id == 'plugin:alice/my-agent/custom'
def test_resolve_no_runner_config(self):
"""Resolve runner ID when not configured."""
pipeline_config = {}
runner_id = ConfigMigration.resolve_runner_id(pipeline_config)
assert runner_id is None
def test_resolve_priority_new_over_old(self):
"""New format takes priority over old format."""
pipeline_config = {
'ai': {
'runner': {
'id': 'plugin:langbot/local-agent/default',
'runner': 'dify-service-api', # This should be ignored
},
},
}
runner_id = ConfigMigration.resolve_runner_id(pipeline_config)
assert runner_id == 'plugin:langbot/local-agent/default'
class TestResolveRunnerConfig:
"""Tests for ConfigMigration.resolve_runner_config."""
def test_resolve_new_format_config(self):
"""Resolve runner config from new format."""
pipeline_config = {
'ai': {
'runner_config': {
'plugin:langbot/local-agent/default': {
'model': 'uuid-123',
'max_round': 10,
},
},
},
}
config = ConfigMigration.resolve_runner_config(
pipeline_config,
'plugin:langbot/local-agent/default',
)
assert config == {'model': 'uuid-123', 'max_round': 10}
def test_resolve_old_format_config(self):
"""Resolve runner config from old format."""
pipeline_config = {
'ai': {
'local-agent': {
'model': 'uuid-123',
'max_round': 10,
},
},
}
config = ConfigMigration.resolve_runner_config(
pipeline_config,
'plugin:langbot/local-agent/default',
)
assert config == {'model': 'uuid-123', 'max_round': 10}
def test_resolve_no_config(self):
"""Resolve runner config when not found."""
pipeline_config = {}
config = ConfigMigration.resolve_runner_config(
pipeline_config,
'plugin:langbot/local-agent/default',
)
assert config == {}
def test_resolve_priority_new_over_old(self):
"""New format config takes priority."""
pipeline_config = {
'ai': {
'runner_config': {
'plugin:langbot/local-agent/default': {
'model': 'new-uuid',
},
},
'local-agent': {
'model': 'old-uuid',
},
},
}
config = ConfigMigration.resolve_runner_config(
pipeline_config,
'plugin:langbot/local-agent/default',
)
assert config == {'model': 'new-uuid'}
class TestGetExpireTime:
"""Tests for ConfigMigration.get_expire_time."""
def test_get_expire_time_zero(self):
"""Get expire time when zero."""
pipeline_config = {
'ai': {
'runner': {
'expire-time': 0,
},
},
}
expire_time = ConfigMigration.get_expire_time(pipeline_config)
assert expire_time == 0
def test_get_expire_time_positive(self):
"""Get expire time when positive."""
pipeline_config = {
'ai': {
'runner': {
'expire-time': 3600,
},
},
}
expire_time = ConfigMigration.get_expire_time(pipeline_config)
assert expire_time == 3600
def test_get_expire_time_default(self):
"""Get expire time when not configured."""
pipeline_config = {}
expire_time = ConfigMigration.get_expire_time(pipeline_config)
assert expire_time == 0
class TestGetOldRunnerName:
"""Tests for ConfigMigration.get_old_runner_name."""
def test_get_old_runner_name_mapped(self):
"""Get old runner name for mapped runner ID."""
old_name = ConfigMigration.get_old_runner_name('plugin:langbot/local-agent/default')
assert old_name == 'local-agent'
def test_get_old_runner_name_not_mapped(self):
"""Get old runner name for unmapped runner ID."""
old_name = ConfigMigration.get_old_runner_name('plugin:alice/my-agent/custom')
assert old_name is None

View File

@@ -0,0 +1,137 @@
"""Tests for agent runner ID parsing and formatting."""
from __future__ import annotations
import pytest
from langbot.pkg.agent.runner.id import (
parse_runner_id,
format_runner_id,
RunnerIdParts,
is_plugin_runner_id,
)
class TestRunnerIdParsing:
"""Tests for parse_runner_id."""
def test_parse_plugin_runner_id(self):
"""Parse valid plugin runner ID."""
runner_id = 'plugin:langbot/local-agent/default'
parts = parse_runner_id(runner_id)
assert parts.source == 'plugin'
assert parts.plugin_author == 'langbot'
assert parts.plugin_name == 'local-agent'
assert parts.runner_name == 'default'
def test_parse_plugin_runner_id_complex_names(self):
"""Parse plugin runner ID with complex names."""
runner_id = 'plugin:alice/helpdesk-agent/ticket-handler'
parts = parse_runner_id(runner_id)
assert parts.source == 'plugin'
assert parts.plugin_author == 'alice'
assert parts.plugin_name == 'helpdesk-agent'
assert parts.runner_name == 'ticket-handler'
def test_parse_invalid_plugin_runner_id_missing_parts(self):
"""Parse invalid plugin runner ID with missing parts."""
runner_id = 'plugin:langbot/local-agent'
with pytest.raises(ValueError) as exc_info:
parse_runner_id(runner_id)
assert 'Invalid plugin runner ID format' in str(exc_info.value)
def test_parse_invalid_plugin_runner_id_empty_parts(self):
"""Parse invalid plugin runner ID with empty parts."""
runner_id = 'plugin://default'
with pytest.raises(ValueError) as exc_info:
parse_runner_id(runner_id)
assert 'non-empty' in str(exc_info.value)
def test_parse_invalid_runner_id_not_plugin(self):
"""Parse invalid runner ID without plugin prefix."""
runner_id = 'local-agent'
with pytest.raises(ValueError) as exc_info:
parse_runner_id(runner_id)
assert 'Invalid runner ID format' in str(exc_info.value)
def test_parse_invalid_runner_id_empty_string(self):
"""Parse empty runner ID."""
runner_id = ''
with pytest.raises(ValueError):
parse_runner_id(runner_id)
class TestRunnerIdFormatting:
"""Tests for format_runner_id."""
def test_format_plugin_runner_id(self):
"""Format plugin runner ID."""
runner_id = format_runner_id(
source='plugin',
plugin_author='langbot',
plugin_name='local-agent',
runner_name='default',
)
assert runner_id == 'plugin:langbot/local-agent/default'
def test_format_invalid_source(self):
"""Format runner ID with invalid source."""
with pytest.raises(ValueError) as exc_info:
format_runner_id(
source='builtin',
plugin_author='langbot',
plugin_name='local-agent',
runner_name='default',
)
assert 'Invalid runner source' in str(exc_info.value)
class TestRunnerIdParts:
"""Tests for RunnerIdParts dataclass."""
def test_get_plugin_id(self):
"""Get plugin ID from parts."""
parts = RunnerIdParts(
source='plugin',
plugin_author='langbot',
plugin_name='local-agent',
runner_name='default',
)
assert parts.to_plugin_id() == 'langbot/local-agent'
def test_frozen_dataclass(self):
"""RunnerIdParts should be immutable."""
parts = RunnerIdParts(
source='plugin',
plugin_author='langbot',
plugin_name='local-agent',
runner_name='default',
)
with pytest.raises(Exception): # FrozenInstanceError
parts.plugin_author = 'other'
class TestIsPluginRunnerId:
"""Tests for is_plugin_runner_id."""
def test_is_plugin_runner_id_true(self):
"""Check plugin runner ID returns True."""
assert is_plugin_runner_id('plugin:langbot/local-agent/default') is True
def test_is_plugin_runner_id_false(self):
"""Check non-plugin runner ID returns False."""
assert is_plugin_runner_id('local-agent') is False
assert is_plugin_runner_id('builtin:local-agent') is False
assert is_plugin_runner_id('') is False

View File

@@ -0,0 +1,278 @@
"""Tests for agent runner registry."""
from __future__ import annotations
import pytest
from langbot.pkg.agent.runner.registry import AgentRunnerRegistry
from langbot.pkg.agent.runner.descriptor import AgentRunnerDescriptor
from langbot.pkg.agent.runner.errors import RunnerNotFoundError, RunnerNotAuthorizedError
class FakeApplication:
"""Fake Application for testing."""
def __init__(self):
class FakeLogger:
def info(self, msg):
pass
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
pass
self.logger = FakeLogger()
class FakePluginConnector:
is_enable_plugin = True
async def list_agent_runners(self, bound_plugins=None):
# Return sample runner data
return [
{
'plugin_author': 'langbot',
'plugin_name': 'local-agent',
'runner_name': 'default',
'manifest': {
'kind': 'AgentRunner',
'metadata': {
'name': 'default',
'label': {'en_US': 'Local Agent'},
},
'spec': {
'protocol_version': '1',
'config': [],
'capabilities': {'streaming': True},
'permissions': {},
},
},
},
{
'plugin_author': 'alice',
'plugin_name': 'my-agent',
'runner_name': 'custom',
'manifest': {
'kind': 'AgentRunner',
'metadata': {
'name': 'custom',
'label': {'en_US': 'Custom Agent'},
},
'spec': {
'protocol_version': '1',
'config': [{'name': 'param1', 'type': 'string'}],
'capabilities': {},
'permissions': {},
},
},
},
# Invalid runner - wrong kind
{
'plugin_author': 'bad',
'plugin_name': 'wrong-kind',
'runner_name': 'default',
'manifest': {
'kind': 'Tool', # Wrong kind
'metadata': {},
'spec': {},
},
},
# Invalid runner - missing name
{
'plugin_author': 'bad',
'plugin_name': 'missing-name',
'runner_name': 'default',
'manifest': {
'kind': 'AgentRunner',
'metadata': {}, # No name
'spec': {},
},
},
]
self.plugin_connector = FakePluginConnector()
class TestRegistryDiscovery:
"""Tests for runner discovery."""
@pytest.mark.asyncio
async def test_discover_valid_runners(self):
"""Discover valid runners from plugin runtime."""
ap = FakeApplication()
registry = AgentRunnerRegistry(ap)
runners = await registry.list_runners(use_cache=False)
# Should find 2 valid runners (langbot/local-agent and alice/my-agent)
assert len(runners) == 2
ids = [r.id for r in runners]
assert 'plugin:langbot/local-agent/default' in ids
assert 'plugin:alice/my-agent/custom' in ids
@pytest.mark.asyncio
async def test_discover_caches_results(self):
"""Discovery should cache results."""
ap = FakeApplication()
registry = AgentRunnerRegistry(ap)
# First discovery
runners1 = await registry.list_runners(use_cache=True)
# Second call should use cache
runners2 = await registry.list_runners(use_cache=True)
assert registry._cache is not None
assert len(runners1) == len(runners2)
@pytest.mark.asyncio
async def test_discover_handles_plugin_disabled(self):
"""Discovery returns empty when plugin system disabled."""
ap = FakeApplication()
ap.plugin_connector.is_enable_plugin = False
registry = AgentRunnerRegistry(ap)
runners = await registry.list_runners(use_cache=False)
assert runners == []
@pytest.mark.asyncio
async def test_cache_not_polluted_by_bound_plugins(self):
"""Cache should contain ALL runners, not filtered by bound_plugins.
Regression test: get(bound_plugins=["a/b"]) should not pollute cache,
so subsequent list_runners(bound_plugins=None) should return all runners.
"""
ap = FakeApplication()
registry = AgentRunnerRegistry(ap)
# First: get with bound_plugins filter (should not pollute cache)
descriptor = await registry.get(
'plugin:langbot/local-agent/default',
bound_plugins=['langbot/local-agent'],
)
assert descriptor.id == 'plugin:langbot/local-agent/default'
# Cache should contain ALL runners (both langbot and alice)
assert registry._cache is not None
assert len(registry._cache) == 2 # Both runners in cache
assert 'plugin:langbot/local-agent/default' in registry._cache
assert 'plugin:alice/my-agent/custom' in registry._cache
# Second: list_runners without filter should return ALL runners
all_runners = await registry.list_runners(bound_plugins=None, use_cache=True)
assert len(all_runners) == 2 # Both runners returned
# Third: list_runners with different filter should work correctly
alice_runners = await registry.list_runners(bound_plugins=['alice/my-agent'], use_cache=True)
assert len(alice_runners) == 1
assert alice_runners[0].id == 'plugin:alice/my-agent/custom'
class TestRegistryGet:
"""Tests for getting specific runner."""
@pytest.mark.asyncio
async def test_get_existing_runner(self):
"""Get existing runner by ID."""
ap = FakeApplication()
registry = AgentRunnerRegistry(ap)
descriptor = await registry.get('plugin:langbot/local-agent/default')
assert descriptor.id == 'plugin:langbot/local-agent/default'
assert descriptor.plugin_author == 'langbot'
assert descriptor.plugin_name == 'local-agent'
assert descriptor.runner_name == 'default'
@pytest.mark.asyncio
async def test_get_nonexistent_runner(self):
"""Get nonexistent runner raises RunnerNotFoundError."""
ap = FakeApplication()
registry = AgentRunnerRegistry(ap)
with pytest.raises(RunnerNotFoundError) as exc_info:
await registry.get('plugin:notexist/unknown/default')
assert exc_info.value.runner_id == 'plugin:notexist/unknown/default'
@pytest.mark.asyncio
async def test_get_runner_with_bound_plugins_filter(self):
"""Get runner with bound plugins authorization."""
ap = FakeApplication()
registry = AgentRunnerRegistry(ap)
# Authorized - langbot plugin in bound list
descriptor = await registry.get(
'plugin:langbot/local-agent/default',
bound_plugins=['langbot/local-agent'],
)
assert descriptor is not None
# Not authorized - plugin not in bound list
with pytest.raises(RunnerNotAuthorizedError):
await registry.get(
'plugin:alice/my-agent/custom',
bound_plugins=['langbot/local-agent'],
)
class TestRegistryMetadataForPipeline:
"""Tests for get_runner_metadata_for_pipeline."""
@pytest.mark.asyncio
async def test_get_metadata_options_and_stages(self):
"""Get metadata options and stages for pipeline UI."""
ap = FakeApplication()
registry = AgentRunnerRegistry(ap)
options, stages = await registry.get_runner_metadata_for_pipeline()
# Should have options for each runner
assert len(options) == 2
option_ids = [o['name'] for o in options]
assert 'plugin:langbot/local-agent/default' in option_ids
assert 'plugin:alice/my-agent/custom' in option_ids
# Should have stages for runners with config
# Note: stages may be empty if config_schema is empty list
# In real scenarios, runners with config_schema will generate stages
# Only runners with non-empty config_schema generate stages
# mock data has config: [{'name': 'param1', 'type': 'string'}] for alice/my-agent
# but config is now taken from runner_data.get('config', [])
assert len(stages) >= 0 # Can be 0 if all runners have empty config
class TestDescriptorValidation:
"""Tests for descriptor validation."""
def test_validate_runner_descriptor(self):
"""Validate correctly built descriptor."""
descriptor = AgentRunnerDescriptor(
id='plugin:test/my-runner/default',
source='plugin',
label={'en_US': 'Test Runner'},
plugin_author='test',
plugin_name='my-runner',
runner_name='default',
)
assert descriptor.id == 'plugin:test/my-runner/default'
assert descriptor.get_plugin_id() == 'test/my-runner'
assert descriptor.protocol_version == '1'
def test_descriptor_capabilities(self):
"""Descriptor capability helper methods."""
descriptor = AgentRunnerDescriptor(
id='plugin:test/my-runner/default',
source='plugin',
label={'en_US': 'Test Runner'},
plugin_author='test',
plugin_name='my-runner',
runner_name='default',
capabilities={'streaming': True, 'tool_calling': False},
)
assert descriptor.supports_streaming() is True
assert descriptor.supports_tool_calling() is False
assert descriptor.supports_knowledge_retrieval() is False

View File

@@ -0,0 +1,343 @@
"""Tests for agent runner result normalizer."""
from __future__ import annotations
import pytest
from langbot.pkg.agent.runner.result_normalizer import AgentResultNormalizer
from langbot.pkg.agent.runner.descriptor import AgentRunnerDescriptor
from langbot.pkg.agent.runner.errors import RunnerExecutionError, RunnerProtocolError
from langbot_plugin.api.entities.builtin.provider import message as provider_message
class FakeApplication:
"""Fake Application for testing."""
def __init__(self):
class FakeLogger:
def info(self, msg):
pass
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
pass
self.logger = FakeLogger()
def make_descriptor():
"""Create a test descriptor."""
return AgentRunnerDescriptor(
id='plugin:langbot/local-agent/default',
source='plugin',
label={'en_US': 'Local Agent', 'zh_Hans': '内置 Agent'},
plugin_author='langbot',
plugin_name='local-agent',
runner_name='default',
protocol_version='1',
capabilities={'streaming': True},
)
class TestNormalizeMessageDelta:
"""Tests for normalizing message.delta results."""
@pytest.mark.asyncio
async def test_normalize_message_delta_text(self):
"""Normalize message.delta with text chunk."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'message.delta',
'data': {
'chunk': {
'role': 'assistant',
'content': 'Hello',
},
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is not None
assert isinstance(result, provider_message.MessageChunk)
assert result.role == 'assistant'
assert result.content == 'Hello'
@pytest.mark.asyncio
async def test_normalize_message_delta_missing_chunk(self):
"""Normalize message.delta without chunk data."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'message.delta',
'data': {},
}
with pytest.raises(RunnerProtocolError) as exc_info:
await normalizer.normalize(result_dict, descriptor)
assert 'missing chunk data' in str(exc_info.value)
class TestNormalizeMessageCompleted:
"""Tests for normalizing message.completed results."""
@pytest.mark.asyncio
async def test_normalize_message_completed(self):
"""Normalize message.completed with full message."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'message.completed',
'data': {
'message': {
'role': 'assistant',
'content': 'Complete response',
},
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is not None
assert isinstance(result, provider_message.Message)
assert result.role == 'assistant'
assert result.content == 'Complete response'
@pytest.mark.asyncio
async def test_normalize_message_completed_missing_message(self):
"""Normalize message.completed without message data."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'message.completed',
'data': {},
}
with pytest.raises(RunnerProtocolError) as exc_info:
await normalizer.normalize(result_dict, descriptor)
assert 'missing message data' in str(exc_info.value)
class TestNormalizeRunCompleted:
"""Tests for normalizing run.completed results."""
@pytest.mark.asyncio
async def test_normalize_run_completed_with_message(self):
"""Normalize run.completed with final message."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'run.completed',
'data': {
'message': {
'role': 'assistant',
'content': 'Final response',
},
'finish_reason': 'stop',
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is not None
assert isinstance(result, provider_message.Message)
@pytest.mark.asyncio
async def test_normalize_run_completed_without_message(self):
"""Normalize run.completed without message."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'run.completed',
'data': {
'finish_reason': 'stop',
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
class TestNormalizeRunFailed:
"""Tests for normalizing run.failed results."""
@pytest.mark.asyncio
async def test_normalize_run_failed(self):
"""Normalize run.failed raises RunnerExecutionError."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'run.failed',
'data': {
'error': 'Upstream timeout',
'code': 'upstream.timeout',
'retryable': True,
},
}
with pytest.raises(RunnerExecutionError) as exc_info:
await normalizer.normalize(result_dict, descriptor)
assert exc_info.value.runner_id == 'plugin:langbot/local-agent/default'
assert exc_info.value.retryable is True
assert 'timeout' in str(exc_info.value)
class TestNormalizeNonMessageResults:
"""Tests for normalizing non-message results."""
@pytest.mark.asyncio
async def test_normalize_tool_call_started(self):
"""Normalize tool.call.started returns None."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'tool.call.started',
'data': {
'tool_call_id': 'call_1',
'tool_name': 'weather',
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
@pytest.mark.asyncio
async def test_normalize_tool_call_completed(self):
"""Normalize tool.call.completed returns None."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'tool.call.completed',
'data': {
'tool_call_id': 'call_1',
'tool_name': 'weather',
'result': {'temp': 20},
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
@pytest.mark.asyncio
async def test_normalize_state_updated(self):
"""Normalize state.updated returns None."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'state.updated',
'data': {
'key': 'external_conversation_id',
'value': 'abc123',
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
@pytest.mark.asyncio
async def test_normalize_action_requested(self):
"""Normalize action.requested returns None (EBA reserved)."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'action.requested',
'data': {
'action': 'platform.message.edit',
'parameters': {},
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
class TestNormalizeInvalidResults:
"""Tests for handling invalid results."""
@pytest.mark.asyncio
async def test_normalize_missing_type(self):
"""Normalize result without type."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'data': {},
}
with pytest.raises(RunnerProtocolError) as exc_info:
await normalizer.normalize(result_dict, descriptor)
assert 'Missing result type' in str(exc_info.value)
@pytest.mark.asyncio
async def test_normalize_unknown_type(self):
"""Normalize unknown type returns None."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
result_dict = {
'type': 'unknown_type',
'data': {},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
@pytest.mark.asyncio
async def test_normalize_legacy_type_returns_none(self):
"""Legacy types (chunk, text, finish) are now treated as unknown."""
normalizer = AgentResultNormalizer(FakeApplication())
descriptor = make_descriptor()
# chunk is now unknown
result_dict = {
'type': 'chunk',
'data': {
'message_chunk': {
'role': 'assistant',
'content': 'Legacy chunk',
},
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
# text is now unknown
result_dict = {
'type': 'text',
'data': {
'content': 'Legacy text',
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None
# finish is now unknown
result_dict = {
'type': 'finish',
'data': {
'message': {
'role': 'assistant',
'content': 'Legacy finish',
},
},
}
result = await normalizer.normalize(result_dict, descriptor)
assert result is None