mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-29 18:17:21 +08:00
* feat(agent): add cross-platform interactive card tool * fix(agent): create pipelines with selected runner defaults * fix(agent): enable reliable cross-platform card interactions * feat(dingtalk): deliver A2UI cards and restore agent replies * Fix DingTalk A2UI form submissions and agent replies * Restore LangBot branding on DingTalk A2UI cards * Use compatible Markdown link in DingTalk A2UI branding * Place LangBot branding above DingTalk card content * Stream DingTalk replies without a source message * Move DingTalk branding into compact card footer * fix(wecombot): handle native select card callbacks * fix(wecombot): stream replies after HITL cards * fix(dingtalk): show submitted HITL choices in cards * fix(lark): stream replies after HITL submissions * fix(plugins): refresh LocalAgent source hashes * fix(dify): merge HITL resume streaming into in-tree plugin * feat(interactions): resume runner in place after card submission * fix(interactions): respect live adapter APIs and preserve deadlines * fix(ci): lock blocking interaction SDK for dependent checks * test(runner): keep legacy SDK probe importable * fix(interactions): align adapter manifests and close timed-out streams * fix(agent): avoid duplicate replies and enable LocalAgent cards * chore(plugins): sync LocalAgent catalog version * refactor(agent): unify blocking interaction callbacks and adapters * chore(deps): pin blocking interaction SDK revision * test(agent): remove obsolete async fixture mark * test(interaction): update legacy admission assertions * fix(qqofficial): keep card callbacks responsive * fix(runner): keep blocking card interactions alive * ci(plugins): expect SDK 0.7.20 in Runner workflows Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> * ci(plugins): test coding Runners against SDK 0.7.20 Claude Code and Codex now call ctx.request_interaction, first released in 0.7.20. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> * fix(discord): deliver guild interaction cards to the originating channel Pipeline group conversations are keyed by guild id, so send_interaction targeted the guild and Discord returned Unknown Channel. Resolve the channel from the message being answered, edit cards in that channel, and report the card's conversation on callbacks so the Host scope check matches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: RockChinQ <rockchinq@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
728 lines
25 KiB
Python
728 lines
25 KiB
Python
"""Tests for AgentResourceBuilder."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, Mock
|
|
|
|
import pytest
|
|
from langbot_plugin.api.entities.builtin.runner import AgentInput, DeliveryContext
|
|
|
|
from langbot.pkg.agent.runner.descriptor import RunnerDescriptor
|
|
from langbot.pkg.agent.runner.binding_resolver import AgentBindingResolver
|
|
from langbot.pkg.agent.runner.query_entry_adapter import QueryEntryAdapter
|
|
from langbot.pkg.agent.runner.resource_builder import AgentResourceBuilder
|
|
from langbot.pkg.agent.runner.host_models import (
|
|
AgentBinding,
|
|
AgentEventEnvelope,
|
|
BindingScope,
|
|
DeliveryPolicy,
|
|
ResourcePolicy,
|
|
)
|
|
from langbot.pkg.api.http.context import ExecutionContext
|
|
from langbot.pkg.provider.tools.toolmgr import ToolManager
|
|
|
|
|
|
RUNNER_ID = 'plugin:test/runner/default'
|
|
TEST_CONTEXT = ExecutionContext(
|
|
instance_uuid='instance-test',
|
|
workspace_uuid='workspace-test',
|
|
placement_generation=1,
|
|
)
|
|
FULL_PERMISSIONS = {
|
|
'models': ['count_tokens', 'invoke', 'stream', 'rerank'],
|
|
'tools': ['detail', 'call'],
|
|
'knowledge_bases': ['list', 'retrieve'],
|
|
'history': ['page', 'search'],
|
|
'events': ['get', 'page'],
|
|
'storage': ['plugin', 'workspace'],
|
|
}
|
|
|
|
|
|
def make_descriptor(
|
|
*,
|
|
config_schema: list[dict] | None = None,
|
|
capabilities: dict | None = None,
|
|
permissions: dict | None = None,
|
|
) -> RunnerDescriptor:
|
|
return RunnerDescriptor(
|
|
usages=['agent'],
|
|
id=RUNNER_ID,
|
|
source='plugin',
|
|
label={'en_US': 'Test Runner'},
|
|
plugin_author='test',
|
|
plugin_name='runner',
|
|
runner_name='default',
|
|
capabilities=capabilities or {},
|
|
permissions=permissions if permissions is not None else FULL_PERMISSIONS,
|
|
config_schema=config_schema or [],
|
|
)
|
|
|
|
|
|
def make_model(model_type='llm', provider='test-provider'):
|
|
return SimpleNamespace(
|
|
model_entity=SimpleNamespace(model_type=model_type),
|
|
provider_entity=SimpleNamespace(name=provider),
|
|
)
|
|
|
|
|
|
def make_query(
|
|
runner_config: dict,
|
|
*,
|
|
variables: dict | None = None,
|
|
use_llm_model_uuid=None,
|
|
use_funcs: list | None = None,
|
|
):
|
|
query = SimpleNamespace(
|
|
query_id=1,
|
|
bot_uuid='bot_001',
|
|
launcher_type='person',
|
|
launcher_id='launcher_001',
|
|
sender_id='sender_001',
|
|
message_event=None,
|
|
message_chain=None,
|
|
user_message=None,
|
|
session=None,
|
|
pipeline_config={
|
|
'ai': {
|
|
'runner': {'id': RUNNER_ID},
|
|
'runner_config': {RUNNER_ID: runner_config},
|
|
},
|
|
},
|
|
variables=variables or {},
|
|
use_llm_model_uuid=use_llm_model_uuid,
|
|
use_funcs=use_funcs or [],
|
|
pipeline_uuid='pipeline_001',
|
|
)
|
|
query._execution_context = TEST_CONTEXT
|
|
return query
|
|
|
|
|
|
async def build_resources(app, query, descriptor):
|
|
event = QueryEntryAdapter.query_to_event(query)
|
|
agent_config = QueryEntryAdapter.config_to_agent_config(query, descriptor.id)
|
|
binding = AgentBindingResolver().resolve_one(event, [agent_config])
|
|
return await AgentResourceBuilder(app).build_resources_from_binding(
|
|
execution_context=TEST_CONTEXT,
|
|
event=event,
|
|
binding=binding,
|
|
descriptor=descriptor,
|
|
tool_catalog=getattr(query, '_host_tool_catalog', None),
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
mock_app = Mock()
|
|
mock_app.logger = Mock()
|
|
mock_app.model_mgr = Mock()
|
|
mock_app.rag_mgr = Mock()
|
|
mock_app.rag_mgr.get_knowledge_base_by_uuid = AsyncMock(return_value=None)
|
|
mock_app.skill_mgr = None
|
|
mock_app.tool_mgr = Mock()
|
|
mock_app.tool_mgr.get_tool_schema = AsyncMock(return_value=(None, None))
|
|
mock_app.tool_mgr.get_resolved_tool_catalog = AsyncMock(return_value=[])
|
|
return mock_app
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_models_authorizes_config_declared_llm_and_rerank_models(app):
|
|
"""DynamicForm model selectors should become run-scoped authorized models."""
|
|
llm_models = {
|
|
'primary': make_model(),
|
|
'fallback': make_model(),
|
|
'aux': make_model(provider='aux-provider'),
|
|
}
|
|
rerank_models = {
|
|
'rerank': make_model(model_type='rerank', provider='rerank-provider'),
|
|
}
|
|
|
|
async def get_model_by_uuid(context, model_uuid):
|
|
assert context == TEST_CONTEXT
|
|
return llm_models.get(model_uuid)
|
|
|
|
async def get_rerank_model_by_uuid(context, model_uuid):
|
|
assert context == TEST_CONTEXT
|
|
return rerank_models.get(model_uuid)
|
|
|
|
app.model_mgr.get_model_by_uuid = AsyncMock(side_effect=get_model_by_uuid)
|
|
app.model_mgr.get_rerank_model_by_uuid = AsyncMock(side_effect=get_rerank_model_by_uuid)
|
|
descriptor = make_descriptor(
|
|
config_schema=[
|
|
{'name': 'model', 'type': 'model-fallback-selector'},
|
|
{'name': 'aux-model', 'type': 'llm-model-selector'},
|
|
{'name': 'rerank-model', 'type': 'rerank-model-selector'},
|
|
],
|
|
)
|
|
query = make_query(
|
|
{
|
|
'model': {'primary': 'primary', 'fallbacks': ['fallback', 'primary']},
|
|
'aux-model': 'aux',
|
|
'rerank-model': 'rerank',
|
|
}
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['models'] == [
|
|
{
|
|
'model_id': 'primary',
|
|
'model_type': 'llm',
|
|
'provider': 'test-provider',
|
|
'operations': ['invoke', 'stream', 'count_tokens'],
|
|
},
|
|
{
|
|
'model_id': 'fallback',
|
|
'model_type': 'llm',
|
|
'provider': 'test-provider',
|
|
'operations': ['invoke', 'stream', 'count_tokens'],
|
|
},
|
|
{
|
|
'model_id': 'aux',
|
|
'model_type': 'llm',
|
|
'provider': 'aux-provider',
|
|
'operations': ['invoke', 'stream', 'count_tokens'],
|
|
},
|
|
{'model_id': 'rerank', 'model_type': 'rerank', 'provider': 'rerank-provider', 'operations': ['rerank']},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_models_from_config_without_manifest_acl(app):
|
|
"""Config-selected models are not projected without manifest model permissions."""
|
|
app.model_mgr.get_model_by_uuid = AsyncMock(return_value=make_model())
|
|
app.model_mgr.get_rerank_model_by_uuid = AsyncMock(return_value=make_model(model_type='rerank'))
|
|
descriptor = make_descriptor(
|
|
config_schema=[
|
|
{'name': 'model', 'type': 'model-fallback-selector'},
|
|
{'name': 'rerank-model', 'type': 'rerank-model-selector'},
|
|
],
|
|
permissions={},
|
|
)
|
|
query = make_query(
|
|
{
|
|
'model': {'primary': 'primary', 'fallbacks': ['fallback']},
|
|
'rerank-model': 'rerank',
|
|
}
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['models'] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_platform_tools_are_not_claimed_when_runner_disables_tool_calling(app):
|
|
event = AgentEventEnvelope(
|
|
event_id='event-platform-disabled',
|
|
event_type='message.received',
|
|
source='platform',
|
|
bot_id='bot-1',
|
|
input=AgentInput(text='hello'),
|
|
delivery=DeliveryContext(
|
|
surface='platform',
|
|
reply_target={'target_type': 'person', 'target_id': 'user-1'},
|
|
platform_capabilities={'supported_apis': ['send_message']},
|
|
),
|
|
)
|
|
binding = AgentBinding(
|
|
binding_id='binding-platform-disabled',
|
|
scope=BindingScope(scope_type='global'),
|
|
runner_id=RUNNER_ID,
|
|
resource_policy=ResourcePolicy(allowed_platform_tool_names=['event_reply']),
|
|
)
|
|
|
|
resources = await AgentResourceBuilder(app).build_resources_from_binding(
|
|
execution_context=TEST_CONTEXT,
|
|
event=event,
|
|
binding=binding,
|
|
descriptor=make_descriptor(capabilities={'tool_calling': False}),
|
|
)
|
|
|
|
assert resources['tools'] == []
|
|
assert resources['platform_capabilities']['authorized_tools'] == []
|
|
assert resources['platform_capabilities']['unavailable_tools'] == [
|
|
{'name': 'event_reply', 'reason': 'runner_call_permission_missing'}
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_receives_interactive_card_tool_for_supported_adapter(app):
|
|
query = make_query({})
|
|
query.adapter = SimpleNamespace(
|
|
get_supported_apis=lambda: ['send_interaction'],
|
|
get_interaction_capabilities=lambda: {
|
|
'field_types': ['select'],
|
|
'action_styles': ['default', 'primary', 'danger'],
|
|
'supports_updates': True,
|
|
'max_fields': 10,
|
|
},
|
|
)
|
|
|
|
resources = await build_resources(
|
|
app,
|
|
query,
|
|
make_descriptor(
|
|
capabilities={'tool_calling': True, 'interactions': True},
|
|
permissions={**FULL_PERMISSIONS, 'interactions': ['request']},
|
|
),
|
|
)
|
|
|
|
assert [tool['tool_name'] for tool in resources['tools']] == ['request_interaction']
|
|
assert resources['platform_capabilities']['authorized_tools'] == ['request_interaction']
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_event_processor_cannot_receive_interactive_card_tool(app):
|
|
event = AgentEventEnvelope(
|
|
event_id='event-processor-card',
|
|
event_type='message.received',
|
|
source='platform',
|
|
bot_id='bot-1',
|
|
input=AgentInput(text='hello'),
|
|
delivery=DeliveryContext(
|
|
surface='platform',
|
|
reply_target={'target_type': 'person', 'target_id': 'user-1'},
|
|
platform_capabilities={'supported_apis': ['send_interaction']},
|
|
),
|
|
)
|
|
binding = AgentBinding(
|
|
binding_id='event-processor-binding',
|
|
scope=BindingScope(scope_type='global'),
|
|
runner_id=RUNNER_ID,
|
|
processor_type='event_processor',
|
|
resource_policy=ResourcePolicy(allowed_platform_tool_names=['request_interaction']),
|
|
delivery_policy=DeliveryPolicy(enable_interactions=True),
|
|
)
|
|
|
|
resources = await AgentResourceBuilder(app).build_resources_from_binding(
|
|
execution_context=TEST_CONTEXT,
|
|
event=event,
|
|
binding=binding,
|
|
descriptor=make_descriptor(capabilities={'tool_calling': True}),
|
|
)
|
|
|
|
assert resources['tools'] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_models_authorizes_rerank_and_llm_refs_from_config(app):
|
|
"""Config-selected model references are projected regardless of method granularity."""
|
|
app.model_mgr.get_model_by_uuid = AsyncMock(return_value=make_model())
|
|
app.model_mgr.get_rerank_model_by_uuid = AsyncMock(
|
|
return_value=make_model(model_type='rerank', provider='rerank-provider')
|
|
)
|
|
descriptor = make_descriptor(
|
|
config_schema=[
|
|
{'name': 'model', 'type': 'llm-model-selector'},
|
|
{'name': 'rerank-model', 'type': 'rerank-model-selector'},
|
|
],
|
|
)
|
|
query = make_query(
|
|
{
|
|
'model': 'llm',
|
|
'rerank-model': 'rerank',
|
|
}
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['models'] == [
|
|
{
|
|
'model_id': 'llm',
|
|
'model_type': 'llm',
|
|
'provider': 'test-provider',
|
|
'operations': ['invoke', 'stream', 'count_tokens'],
|
|
},
|
|
{'model_id': 'rerank', 'model_type': 'rerank', 'provider': 'rerank-provider', 'operations': ['rerank']},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_resources_accepts_dynamic_form_type_aliases(app):
|
|
"""Frontend DynamicForm aliases should resolve to runtime resource grants."""
|
|
app.model_mgr.get_model_by_uuid = AsyncMock(return_value=make_model())
|
|
|
|
async def get_kb(context, kb_uuid):
|
|
assert context == TEST_CONTEXT
|
|
return SimpleNamespace(
|
|
uuid=kb_uuid,
|
|
get_name=lambda: f'name-{kb_uuid}',
|
|
knowledge_base_entity=SimpleNamespace(kb_type='default'),
|
|
)
|
|
|
|
app.rag_mgr.get_knowledge_base_by_uuid = AsyncMock(side_effect=get_kb)
|
|
descriptor = make_descriptor(
|
|
capabilities={'knowledge_retrieval': True},
|
|
config_schema=[
|
|
{'name': 'model', 'type': 'select-llm-model'},
|
|
{'name': 'knowledge-bases', 'type': 'select-knowledge-bases'},
|
|
],
|
|
)
|
|
query = make_query(
|
|
{
|
|
'model': 'llm_alias',
|
|
'knowledge-bases': ['kb_alias'],
|
|
}
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['models'] == [
|
|
{
|
|
'model_id': 'llm_alias',
|
|
'model_type': 'llm',
|
|
'provider': 'test-provider',
|
|
'operations': ['invoke', 'stream', 'count_tokens'],
|
|
},
|
|
]
|
|
assert resources['knowledge_bases'] == [
|
|
{'kb_id': 'kb_alias', 'kb_name': 'name-kb_alias', 'kb_type': 'default', 'operations': ['list', 'retrieve']},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_models_manifest_permission_narrows_binding(app):
|
|
"""Manifest model permissions narrower than binding should remove LLM grants."""
|
|
app.model_mgr.get_model_by_uuid = AsyncMock(return_value=make_model())
|
|
app.model_mgr.get_rerank_model_by_uuid = AsyncMock(
|
|
return_value=make_model(model_type='rerank', provider='rerank-provider')
|
|
)
|
|
descriptor = make_descriptor(
|
|
config_schema=[
|
|
{'name': 'model', 'type': 'llm-model-selector'},
|
|
{'name': 'rerank-model', 'type': 'rerank-model-selector'},
|
|
],
|
|
permissions={
|
|
**FULL_PERMISSIONS,
|
|
'models': ['rerank'],
|
|
},
|
|
)
|
|
query = make_query(
|
|
{
|
|
'model': 'llm',
|
|
'rerank-model': 'rerank',
|
|
}
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['models'] == [
|
|
{'model_id': 'rerank', 'model_type': 'rerank', 'provider': 'rerank-provider', 'operations': ['rerank']},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_models_deduplicates_query_and_config_models(app):
|
|
"""A model selected by both preproc and runner config should appear once."""
|
|
app.model_mgr.get_model_by_uuid = AsyncMock(return_value=make_model())
|
|
app.model_mgr.get_rerank_model_by_uuid = AsyncMock(return_value=None)
|
|
descriptor = make_descriptor(
|
|
config_schema=[
|
|
{'name': 'model', 'type': 'model-fallback-selector'},
|
|
],
|
|
)
|
|
query = make_query(
|
|
{'model': {'primary': 'primary', 'fallbacks': ['fallback']}},
|
|
variables={'_fallback_model_uuids': ['fallback']},
|
|
use_llm_model_uuid='primary',
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert [model['model_id'] for model in resources['models']] == ['primary', 'fallback']
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_tools_authorizes_query_declared_tools(app):
|
|
"""Tools discovered by Pipeline preprocessing become run-scoped authorized
|
|
resources, with full parameters schema prefilled by the host."""
|
|
app.tool_mgr.get_tool_schema = AsyncMock(
|
|
side_effect=lambda context, name, source_ref=None: {
|
|
'qa_plugin_echo': (
|
|
'Echo test tool',
|
|
{'type': 'object', 'properties': {'text': {'type': 'string'}}},
|
|
),
|
|
}.get(name, (None, None))
|
|
)
|
|
descriptor = make_descriptor(
|
|
capabilities={'tool_calling': True},
|
|
)
|
|
query = make_query(
|
|
{},
|
|
variables={
|
|
'_host_tool_source_refs': {
|
|
'qa_plugin_echo': {'source': 'plugin', 'source_id': 'test/plugin'},
|
|
'qa_mcp_echo': {'source': 'mcp', 'source_id': 'mcp-server'},
|
|
},
|
|
},
|
|
use_funcs=[
|
|
{'name': 'qa_plugin_echo', 'description': 'Echo test tool'},
|
|
SimpleNamespace(name='qa_mcp_echo'),
|
|
],
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['tools'] == [
|
|
{
|
|
'tool_name': 'qa_plugin_echo',
|
|
'tool_type': 'plugin',
|
|
'description': 'Echo test tool',
|
|
'operations': ['detail', 'call'],
|
|
'parameters': {'type': 'object', 'properties': {'text': {'type': 'string'}}},
|
|
'source': 'plugin',
|
|
'source_id': 'test/plugin',
|
|
},
|
|
{
|
|
'tool_name': 'qa_mcp_echo',
|
|
'tool_type': 'mcp',
|
|
'description': None,
|
|
'operations': ['detail', 'call'],
|
|
'parameters': None,
|
|
'source': 'mcp',
|
|
'source_id': 'mcp-server',
|
|
},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize('catalog_source', ['builtin', 'plugin'])
|
|
async def test_build_tools_reuses_only_matching_host_catalog_schema(app, catalog_source):
|
|
query = make_query({}, use_funcs=[{'name': 'exec'}])
|
|
ToolManager.bind_query_tool_sources(
|
|
query,
|
|
[
|
|
{
|
|
'name': 'exec',
|
|
'source': catalog_source,
|
|
'description': 'Catalog command',
|
|
'parameters': {'type': 'object', 'properties': {'command': {'type': 'string'}}},
|
|
}
|
|
],
|
|
)
|
|
query.variables['_host_tool_source_refs'] = {'exec': {'source': 'builtin', 'source_id': None}}
|
|
app.tool_mgr.get_tool_schema.return_value = ('Live command', {'type': 'object'})
|
|
|
|
resources = await build_resources(app, query, make_descriptor(capabilities={'tool_calling': True}))
|
|
tool = next(tool for tool in resources['tools'] if tool['tool_name'] == 'exec')
|
|
if catalog_source == 'builtin':
|
|
assert tool['description'] == 'Catalog command'
|
|
assert tool['parameters'] == {'type': 'object', 'properties': {'command': {'type': 'string'}}}
|
|
app.tool_mgr.get_tool_schema.assert_not_awaited()
|
|
else:
|
|
assert tool['description'] == 'Live command'
|
|
assert tool['parameters'] == {'type': 'object'}
|
|
app.tool_mgr.get_tool_schema.assert_awaited_once_with(
|
|
TEST_CONTEXT, 'exec', source_ref={'source': 'builtin', 'source_id': None}
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_tools_manifest_permission_denies_binding_tools(app):
|
|
"""Binding tool grants should be removed when manifest does not request tools."""
|
|
descriptor = make_descriptor(
|
|
capabilities={'tool_calling': True},
|
|
permissions={
|
|
**FULL_PERMISSIONS,
|
|
'tools': [],
|
|
},
|
|
)
|
|
query = make_query(
|
|
{},
|
|
use_funcs=[
|
|
{'name': 'qa_plugin_echo', 'description': 'Echo test tool'},
|
|
],
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['tools'] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_tools_materializes_independent_agent_all_tools_policy(app):
|
|
"""Independent Agents resolve an all-tools grant against the live Host catalog."""
|
|
app.tool_mgr.get_resolved_tool_catalog = AsyncMock(
|
|
return_value=[
|
|
{
|
|
'name': 'exec',
|
|
'source': 'builtin',
|
|
'description': 'Execute a command',
|
|
'parameters': {'type': 'object', 'properties': {'command': {'type': 'string'}}},
|
|
},
|
|
{
|
|
'name': 'plugin_tool',
|
|
'source': 'plugin',
|
|
'source_id': 'test/plugin',
|
|
'description': 'Plugin tool',
|
|
'parameters': {},
|
|
},
|
|
]
|
|
)
|
|
descriptor = make_descriptor(capabilities={'tool_calling': True})
|
|
binding = AgentBinding(
|
|
binding_id='agent-binding',
|
|
scope=BindingScope(scope_type='agent', scope_id='agent-1'),
|
|
runner_id=RUNNER_ID,
|
|
runner_config={},
|
|
resource_policy=ResourcePolicy(allow_all_tools=True),
|
|
)
|
|
|
|
resources = await AgentResourceBuilder(app).build_resources_from_binding(
|
|
execution_context=TEST_CONTEXT,
|
|
event=QueryEntryAdapter.query_to_event(make_query({})),
|
|
binding=binding,
|
|
descriptor=descriptor,
|
|
)
|
|
|
|
assert [tool['tool_name'] for tool in resources['tools']] == ['exec', 'plugin_tool']
|
|
assert resources['tools'][0]['description'] == 'Execute a command'
|
|
assert resources['tools'][0]['parameters'] == {'type': 'object', 'properties': {'command': {'type': 'string'}}}
|
|
assert resources['tools'][1]['parameters'] is None
|
|
app.tool_mgr.get_tool_schema.assert_not_awaited()
|
|
app.tool_mgr.get_resolved_tool_catalog.assert_awaited_once_with(
|
|
TEST_CONTEXT,
|
|
include_skill_authoring=True,
|
|
include_mcp_resource_tools=True,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_tools_denies_mcp_resource_tools_when_agent_reads_disabled(app):
|
|
app.tool_mgr.get_resolved_tool_catalog = AsyncMock(
|
|
return_value=[
|
|
{'name': 'exec', 'source': 'builtin'},
|
|
{'name': 'langbot_mcp_list_resources', 'source': 'mcp'},
|
|
{'name': 'langbot_mcp_read_resource', 'source': 'mcp'},
|
|
]
|
|
)
|
|
descriptor = make_descriptor(capabilities={'tool_calling': True})
|
|
binding = AgentBinding(
|
|
binding_id='agent-binding',
|
|
scope=BindingScope(scope_type='agent', scope_id='agent-1'),
|
|
runner_id=RUNNER_ID,
|
|
runner_config={'mcp-resource-agent-read-enabled': False},
|
|
resource_policy=ResourcePolicy(allow_all_tools=True),
|
|
)
|
|
|
|
resources = await AgentResourceBuilder(app).build_resources_from_binding(
|
|
execution_context=TEST_CONTEXT,
|
|
event=QueryEntryAdapter.query_to_event(make_query({})),
|
|
binding=binding,
|
|
descriptor=descriptor,
|
|
)
|
|
|
|
assert [tool['tool_name'] for tool in resources['tools']] == ['exec']
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_tools_keeps_plugin_using_synthetic_mcp_tool_name_when_reads_disabled(app):
|
|
app.tool_mgr.get_resolved_tool_catalog = AsyncMock(
|
|
return_value=[
|
|
{
|
|
'name': 'langbot_mcp_read_resource',
|
|
'source': 'plugin',
|
|
'source_id': 'test/resource-reader',
|
|
},
|
|
]
|
|
)
|
|
descriptor = make_descriptor(capabilities={'tool_calling': True})
|
|
binding = AgentBinding(
|
|
binding_id='agent-binding',
|
|
scope=BindingScope(scope_type='agent', scope_id='agent-1'),
|
|
runner_id=RUNNER_ID,
|
|
runner_config={'mcp-resource-agent-read-enabled': False},
|
|
resource_policy=ResourcePolicy(allow_all_tools=True),
|
|
)
|
|
|
|
resources = await AgentResourceBuilder(app).build_resources_from_binding(
|
|
execution_context=TEST_CONTEXT,
|
|
event=QueryEntryAdapter.query_to_event(make_query({})),
|
|
binding=binding,
|
|
descriptor=descriptor,
|
|
)
|
|
|
|
assert resources['tools'] == [
|
|
{
|
|
'tool_name': 'langbot_mcp_read_resource',
|
|
'tool_type': 'plugin',
|
|
'description': None,
|
|
'operations': ['detail', 'call'],
|
|
'parameters': None,
|
|
'source': 'plugin',
|
|
'source_id': 'test/resource-reader',
|
|
}
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_knowledge_bases_unions_config_and_policy_grants(app):
|
|
descriptor = make_descriptor(
|
|
capabilities={'knowledge_retrieval': True},
|
|
config_schema=[
|
|
{'name': 'knowledge-bases', 'type': 'knowledge-base-multi-selector'},
|
|
],
|
|
)
|
|
query = make_query(
|
|
{'knowledge-bases': ['kb_config']},
|
|
variables={'_knowledge_base_uuids': ['kb_policy']},
|
|
)
|
|
|
|
async def get_kb(context, kb_uuid):
|
|
assert context == TEST_CONTEXT
|
|
return SimpleNamespace(
|
|
uuid=kb_uuid,
|
|
get_name=lambda: f'name-{kb_uuid}',
|
|
knowledge_base_entity=SimpleNamespace(kb_type='default'),
|
|
)
|
|
|
|
app.rag_mgr.get_knowledge_base_by_uuid = AsyncMock(side_effect=get_kb)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['knowledge_bases'] == [
|
|
{'kb_id': 'kb_config', 'kb_name': 'name-kb_config', 'kb_type': 'default', 'operations': ['list', 'retrieve']},
|
|
{'kb_id': 'kb_policy', 'kb_name': 'name-kb_policy', 'kb_type': 'default', 'operations': ['list', 'retrieve']},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_knowledge_bases_manifest_permission_denies_binding_kbs(app):
|
|
descriptor = make_descriptor(
|
|
capabilities={'knowledge_retrieval': True},
|
|
permissions={
|
|
**FULL_PERMISSIONS,
|
|
'knowledge_bases': [],
|
|
},
|
|
config_schema=[
|
|
{'name': 'knowledge-bases', 'type': 'knowledge-base-multi-selector'},
|
|
],
|
|
)
|
|
query = make_query(
|
|
{'knowledge-bases': ['kb_config']},
|
|
variables={'_knowledge_base_uuids': ['kb_policy']},
|
|
)
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['knowledge_bases'] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_storage_intersects_manifest_and_binding_policy(app):
|
|
descriptor = make_descriptor(
|
|
permissions={
|
|
**FULL_PERMISSIONS,
|
|
'storage': ['plugin'],
|
|
},
|
|
)
|
|
query = make_query({})
|
|
|
|
resources = await build_resources(app, query, descriptor)
|
|
|
|
assert resources['storage'] == {
|
|
'plugin_storage': True,
|
|
'workspace_storage': False,
|
|
}
|