chore(merge): sync master into dev/4.11.x

This commit is contained in:
huanghuoguoguo
2026-07-31 19:29:38 +08:00
502 changed files with 77975 additions and 12729 deletions
+60 -141
View File
@@ -8,10 +8,6 @@ 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
@@ -20,36 +16,23 @@ from langbot_plugin.api.entities.builtin.provider.message import Message
from langbot_plugin.api.entities.builtin.provider.prompt import Prompt
from langbot_plugin.api.entities.builtin.provider.session import Conversation, LauncherTypes, Session
from langbot.pkg.agent.runner.descriptor import AgentRunnerDescriptor
from langbot.pkg.api.http.context import ExecutionContext
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
_RUNNER_ID = 'plugin:langbot-team/LocalAgent/default'
_CONTEXT = ExecutionContext(
instance_uuid='instance-a',
workspace_uuid='workspace-a',
placement_generation=1,
bot_uuid='bot-1',
pipeline_uuid='pipe-1',
)
def _make_query() -> Query:
message_chain = MessageChain([Plain(text='create a skill')])
return Query(
query = Query(
query_id=1,
launcher_type=LauncherTypes.PERSON,
launcher_id='launcher-1',
@@ -64,19 +47,21 @@ def _make_query() -> Query:
pipeline_uuid='pipe-1',
pipeline_config={
'ai': {
'runner': {'id': 'plugin:langbot-team/LocalAgent/default'},
'runner': {'id': _RUNNER_ID},
'runner_config': {
'plugin:langbot-team/LocalAgent/default': {
_RUNNER_ID: {
'model': {'primary': 'model-1', 'fallbacks': []},
'prompt': [],
'prompt': [{'role': 'system', 'content': 'system prompt'}],
'knowledge-bases': [],
},
}
},
},
'trigger': {'misc': {}},
},
variables={},
)
object.__setattr__(query, '_execution_context', _CONTEXT)
return query
def _make_conversation() -> Conversation:
@@ -89,20 +74,30 @@ 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()
model = SimpleNamespace(model_entity=SimpleNamespace(uuid='model-1', abilities={'func_call'}))
tool_mgr = SimpleNamespace(get_resolved_tool_catalog=AsyncMock(return_value=[]))
descriptor = AgentRunnerDescriptor(
id=_RUNNER_ID,
source='plugin',
label={'en_US': 'Local Agent'},
plugin_author='langbot-team',
plugin_name='LocalAgent',
runner_name='default',
config_schema=[
{'name': 'model', 'type': 'model-fallback-selector'},
{'name': 'prompt', 'type': 'prompt-editor'},
{'name': 'knowledge-bases', 'type': 'knowledge-base-multi-selector'},
],
capabilities={
'tool_calling': True,
'knowledge_retrieval': True,
'multimodal_input': True,
'skill_authoring': True,
},
)
return SimpleNamespace(
sess_mgr=SimpleNamespace(
@@ -110,6 +105,7 @@ def _make_app(*, skill_service) -> SimpleNamespace:
get_conversation=AsyncMock(return_value=conversation),
),
model_mgr=SimpleNamespace(get_model_by_uuid=AsyncMock(return_value=model)),
agent_runner_registry=SimpleNamespace(get=AsyncMock(return_value=descriptor)),
tool_mgr=tool_mgr,
plugin_connector=SimpleNamespace(
emit_event=AsyncMock(
@@ -124,8 +120,10 @@ 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(
ensure_loaded=AsyncMock(),
get_skills=Mock(return_value={}),
build_skill_aware_prompt_addition=Mock(return_value=''),
skills={},
),
skill_service=skill_service,
@@ -150,7 +148,7 @@ def _import_preproc_modules():
@pytest.mark.asyncio
async def test_preproc_loads_host_tools_for_runner():
async def test_preproc_resolves_host_tools_for_plugin_runner():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
@@ -160,6 +158,7 @@ async def test_preproc_loads_host_tools_for_runner():
assert result.result_type == entities_module.ResultType.CONTINUE
app.tool_mgr.get_resolved_tool_catalog.assert_awaited_once_with(
_CONTEXT,
None,
None,
include_skill_authoring=True,
@@ -168,45 +167,7 @@ async def test_preproc_loads_host_tools_for_runner():
@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_resolved_tool_catalog = AsyncMock(
return_value=[
{
'name': 'activate',
'source': 'skill',
'description': 'Activate a skill',
'parameters': {},
},
{
'name': 'register_skill',
'source': 'skill',
'description': 'Register a skill',
'parameters': {},
},
]
)
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_resolved_tool_catalog.assert_awaited_once_with(
None,
None,
include_skill_authoring=True,
include_mcp_resource_tools=True,
)
assert [tool.name for tool in query.use_funcs] == ['activate', 'register_skill']
@pytest.mark.asyncio
async def test_preproc_loads_host_tools_regardless_of_skill_service():
"""Skill tooling no longer gates on skill_service at the preproc layer."""
async def test_preproc_keeps_host_skill_tools_visible_when_skill_service_missing():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=None)
@@ -216,6 +177,7 @@ async def test_preproc_loads_host_tools_regardless_of_skill_service():
assert result.result_type == entities_module.ResultType.CONTINUE
app.tool_mgr.get_resolved_tool_catalog.assert_awaited_once_with(
_CONTEXT,
None,
None,
include_skill_authoring=True,
@@ -236,6 +198,7 @@ async def test_preproc_disables_mcp_resource_tools_when_agent_reading_is_disable
assert result.result_type == entities_module.ResultType.CONTINUE
app.tool_mgr.get_resolved_tool_catalog.assert_awaited_once_with(
_CONTEXT,
None,
None,
include_skill_authoring=True,
@@ -244,17 +207,19 @@ async def test_preproc_disables_mcp_resource_tools_when_agent_reading_is_disable
@pytest.mark.asyncio
async def test_preproc_records_all_visible_skills_without_prompt_injection():
async def test_preproc_leaves_skill_prompt_projection_to_agent_runner_resources():
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
app.skill_mgr.build_skill_aware_prompt_addition = Mock(
return_value='\n\nAvailable Skills:\n- demo (demo): Demo skill.\n\nCall activate ...'
)
query = _make_query()
result = await stage_process_capture(preproc_module, app, query)
assert result.result_type == entities_module.ResultType.CONTINUE
app.pipeline_service.get_pipeline.assert_awaited_once_with('pipe-1')
assert query.variables.get('_pipeline_bound_skills') is None
app.skill_mgr.build_skill_aware_prompt_addition.assert_not_called()
head = query.prompt.messages[0]
assert head.role == 'system'
assert head.content == 'system prompt'
@@ -262,6 +227,7 @@ async def test_preproc_records_all_visible_skills_without_prompt_injection():
@pytest.mark.asyncio
async def test_preproc_respects_pipeline_bound_skills_subset():
"""When all skills are disabled, the authorized subset is retained for resources."""
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
@@ -273,79 +239,32 @@ 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_not_called()
assert query.variables.get('_pipeline_bound_skills') == ['only-this']
assert query.prompt.messages[0].content == 'system prompt'
@pytest.mark.asyncio
async def test_preproc_does_not_load_skill_preferences_without_skill_mgr():
async def test_preproc_skips_injection_when_addendum_is_empty():
"""No visible skills → system prompt is left untouched (no
``Available Skills`` block appended)."""
preproc_module, entities_module = _import_preproc_modules()
app = _make_app(skill_service=SimpleNamespace())
app.skill_mgr = None # no skill manager -> skill tooling unavailable
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.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-team/LocalAgent/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
app.skill_mgr.build_skill_aware_prompt_addition.assert_not_called()
if query.prompt and query.prompt.messages:
assert 'Available Skills' not in (query.prompt.messages[0].content or '')
async def stage_process_capture(preproc_module, app, query):