feat(agent-runner): add plugin runner host integration

This commit is contained in:
huanghuoguoguo
2026-06-20 10:18:52 +08:00
parent d22fa82d7c
commit cede35b31b
129 changed files with 26980 additions and 6209 deletions
+129 -25
View File
@@ -8,6 +8,10 @@ from unittest.mock import AsyncMock, Mock
import pytest
from langbot_plugin.api.entities.builtin.agent_runner.manifest import (
AgentRunnerCapabilities,
AgentRunnerPermissions,
)
from langbot_plugin.api.entities.builtin.pipeline.query import Query
from langbot_plugin.api.entities.builtin.platform.entities import Friend
from langbot_plugin.api.entities.builtin.platform.events import FriendMessage
@@ -17,6 +21,32 @@ from langbot_plugin.api.entities.builtin.provider.prompt import Prompt
from langbot_plugin.api.entities.builtin.provider.session import Conversation, LauncherTypes, Session
class _FakeRunnerDescriptor:
config_schema = [
{'name': 'model', 'type': 'model-fallback-selector'},
{'name': 'prompt', 'type': 'prompt-editor', 'default': []},
{'name': 'knowledge-bases', 'type': 'knowledge-base-multi-selector', 'default': []},
]
permissions = {
'models': ['invoke', 'stream'],
'tools': ['detail', 'call'],
'knowledge_bases': ['list', 'retrieve'],
}
permissions = AgentRunnerPermissions.model_validate(permissions)
capabilities = AgentRunnerCapabilities(
tool_calling=True,
knowledge_retrieval=True,
multimodal_input=True,
skill_authoring=True,
)
def supports_tool_calling(self):
return self.capabilities.tool_calling
def supports_knowledge_retrieval(self):
return self.capabilities.knowledge_retrieval
def _make_query() -> Query:
message_chain = MessageChain([Plain(text='create a skill')])
return Query(
@@ -34,11 +64,13 @@ def _make_query() -> Query:
pipeline_uuid='pipe-1',
pipeline_config={
'ai': {
'runner': {'runner': 'local-agent'},
'local-agent': {
'model': {'primary': 'model-1', 'fallbacks': []},
'prompt': 'default',
'knowledge-bases': [],
'runner': {'id': 'plugin:langbot/local-agent/default'},
'runner_config': {
'plugin:langbot/local-agent/default': {
'model': {'primary': 'model-1', 'fallbacks': []},
'prompt': [],
'knowledge-bases': [],
},
},
},
'trigger': {'misc': {}},
@@ -57,6 +89,15 @@ def _make_conversation() -> Conversation:
)
async def _passthrough_preproc_event(event, bound_plugins):
return SimpleNamespace(
event=SimpleNamespace(
default_prompt=event.default_prompt,
prompt=event.prompt,
)
)
def _make_app(*, skill_service) -> SimpleNamespace:
session = Session(launcher_type=LauncherTypes.PERSON, launcher_id='launcher-1', sender_id='sender-1')
conversation = _make_conversation()
@@ -83,8 +124,8 @@ def _make_app(*, skill_service) -> SimpleNamespace:
pipeline_service=SimpleNamespace(
get_pipeline=AsyncMock(return_value={'extensions_preferences': {'enable_all_skills': True}})
),
agent_runner_registry=SimpleNamespace(get=AsyncMock(return_value=_FakeRunnerDescriptor())),
skill_mgr=SimpleNamespace(
build_skill_aware_prompt_addition=Mock(return_value=''),
skills={},
),
skill_service=skill_service,
@@ -121,6 +162,28 @@ async def test_preproc_enables_skill_authoring_tools_when_skill_service_availabl
app.tool_mgr.get_all_tools.assert_awaited_once_with(None, None, include_skill_authoring=True)
@pytest.mark.asyncio
async def test_preproc_puts_host_skill_tools_into_query_scope():
"""AgentRunner resource authorization consumes the tools discovered by preproc."""
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
app.tool_mgr.get_all_tools = AsyncMock(
return_value=[
SimpleNamespace(name='activate'),
SimpleNamespace(name='register_skill'),
]
)
query = _make_query()
stage = preproc_module.PreProcessor(app)
result = await stage.process(query, 'PreProcessor')
assert result.result_type == entities_module.ResultType.CONTINUE
app.tool_mgr.get_all_tools.assert_awaited_once_with(None, None, include_skill_authoring=True)
assert [tool.name for tool in query.use_funcs] == ['activate', 'register_skill']
@pytest.mark.asyncio
async def test_preproc_disables_skill_authoring_tools_when_skill_service_missing():
preproc_module, entities_module = _import_preproc_modules()
@@ -135,30 +198,24 @@ async def test_preproc_disables_skill_authoring_tools_when_skill_service_missing
@pytest.mark.asyncio
async def test_preproc_injects_skill_index_into_system_prompt():
"""The Tool Call activation pattern still needs the LLM to know which
skills exist. PreProcessor must append the SkillManager's index
addendum to the first system message."""
async def test_preproc_records_all_visible_skills_without_prompt_injection():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
addendum = '\n\nAvailable Skills:\n- demo (demo): Demo skill.\n\nCall activate ...'
app.skill_mgr.build_skill_aware_prompt_addition = Mock(return_value=addendum)
query = _make_query()
result = await stage_process_capture(preproc_module, app, query)
assert result.result_type == entities_module.ResultType.CONTINUE
app.skill_mgr.build_skill_aware_prompt_addition.assert_called_once_with(bound_skills=None)
app.pipeline_service.get_pipeline.assert_awaited_once_with('pipe-1')
assert query.variables.get('_pipeline_bound_skills') is None
head = query.prompt.messages[0]
assert head.role == 'system'
assert head.content.endswith(addendum)
assert head.content == 'system prompt'
@pytest.mark.asyncio
async def test_preproc_respects_pipeline_bound_skills_subset():
"""When ``enable_all_skills`` is false the bound list is passed through
so the addendum only mentions skills allowed for this pipeline."""
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
@@ -170,31 +227,78 @@ async def test_preproc_respects_pipeline_bound_skills_subset():
}
}
)
app.skill_mgr.build_skill_aware_prompt_addition = Mock(return_value='')
query = _make_query()
result = await stage_process_capture(preproc_module, app, query)
assert result.result_type == entities_module.ResultType.CONTINUE
app.skill_mgr.build_skill_aware_prompt_addition.assert_called_once_with(bound_skills=['only-this'])
assert query.variables.get('_pipeline_bound_skills') == ['only-this']
assert query.prompt.messages[0].content == 'system prompt'
@pytest.mark.asyncio
async def test_preproc_skips_injection_when_addendum_is_empty():
"""No visible skills → system prompt is left untouched (no
``Available Skills`` block appended)."""
async def test_preproc_does_not_load_skill_preferences_without_skill_authoring_service():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
app.skill_mgr.build_skill_aware_prompt_addition = Mock(return_value='')
app = _make_app(skill_service=None)
query = _make_query()
result = await stage_process_capture(preproc_module, app, query)
assert result.result_type == entities_module.ResultType.CONTINUE
if query.prompt and query.prompt.messages:
assert 'Available Skills' not in (query.prompt.messages[0].content or '')
app.pipeline_service.get_pipeline.assert_not_awaited()
assert '_pipeline_bound_skills' not in query.variables
assert query.prompt.messages[0].content == 'system prompt'
@pytest.mark.asyncio
async def test_preproc_uses_transcript_history_view_when_available():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
conversation = app.sess_mgr.get_conversation.return_value
conversation.messages = [Message(role='user', content='legacy history')]
app.plugin_connector.emit_event = AsyncMock(side_effect=_passthrough_preproc_event)
transcript_messages = [
Message(role='user', content='from transcript user'),
Message(role='assistant', content='from transcript assistant'),
]
stage = preproc_module.PreProcessor(app)
stage._load_agent_runner_history_messages = AsyncMock(return_value=transcript_messages)
query = _make_query()
result = await stage.process(query, 'PreProcessor')
assert result.result_type == entities_module.ResultType.CONTINUE
assert query.messages == transcript_messages
stage._load_agent_runner_history_messages.assert_awaited_once_with(
'plugin:langbot/local-agent/default',
'conv-1',
bot_id='bot-1',
workspace_id=None,
thread_id=None,
)
@pytest.mark.asyncio
async def test_preproc_falls_back_to_conversation_messages_when_transcript_empty():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
legacy_messages = [Message(role='user', content='legacy history')]
app.sess_mgr.get_conversation.return_value.messages = legacy_messages
app.plugin_connector.emit_event = AsyncMock(side_effect=_passthrough_preproc_event)
stage = preproc_module.PreProcessor(app)
stage._load_agent_runner_history_messages = AsyncMock(return_value=None)
query = _make_query()
result = await stage.process(query, 'PreProcessor')
assert result.result_type == entities_module.ResultType.CONTINUE
assert query.messages == legacy_messages
async def stage_process_capture(preproc_module, app, query):