mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-25 07:31:32 +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>
359 lines
14 KiB
Python
359 lines
14 KiB
Python
"""Explicit streaming delivery across SDK, Host lifecycle, and adapter boundaries."""
|
|
|
|
import asyncio
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from langbot_plugin.api.entities.builtin.platform import events, entities, message
|
|
from langbot_plugin.api.entities.builtin.runner.context_access import ContextAPICapabilities
|
|
from langbot_plugin.api.proxies.runner import RunnerAPIProxy
|
|
from langbot_plugin.api.proxies.runner.common import PermissionDeniedError
|
|
from langbot_plugin.entities.io.actions.enums import PluginToRuntimeAction
|
|
|
|
from langbot.pkg.agent.runner.reply_stream import ReplyStreamRequest, ReplyStreamSession
|
|
|
|
|
|
def make_session(*, native=True, source=True, mock=False):
|
|
event = SimpleNamespace(
|
|
delivery=SimpleNamespace(
|
|
surface='webui' if mock else 'platform',
|
|
platform_capabilities={'debug_mock': mock},
|
|
reply_target={'target_type': 'person', 'target_id': 'user-1'},
|
|
)
|
|
)
|
|
incoming = (
|
|
events.MessageReceivedEvent(
|
|
message_id='source-1',
|
|
sender=entities.User(id='user-1'),
|
|
chat_id='user-1',
|
|
chat_type=entities.ChatType.PRIVATE,
|
|
message_chain=message.MessageChain([message.Plain(text='hello')]),
|
|
source_platform_object=object(),
|
|
)
|
|
if source
|
|
else None
|
|
)
|
|
adapter = SimpleNamespace(
|
|
is_stream_output_supported=AsyncMock(return_value=native),
|
|
create_message_card=AsyncMock(return_value=True),
|
|
reply_message_chunk=AsyncMock(),
|
|
send_message=AsyncMock(),
|
|
reply_message=AsyncMock(),
|
|
)
|
|
return ReplyStreamSession(event, adapter, incoming), adapter, incoming
|
|
|
|
|
|
def request(key, operation='update', text='hello'):
|
|
return ReplyStreamRequest(stream_id=key, operation=operation, text=text)
|
|
|
|
|
|
def proxy_for(session, *, allowed=True, advertised=True):
|
|
if not hasattr(RunnerAPIProxy, 'reply_stream'):
|
|
pytest.skip('SDK does not provide the optional streaming reply API')
|
|
context = SimpleNamespace(
|
|
run_id='run-1',
|
|
runtime=SimpleNamespace(deadline_at=None),
|
|
context=SimpleNamespace(available_apis=ContextAPICapabilities(reply_stream=advertised)),
|
|
resources=SimpleNamespace(
|
|
models=[],
|
|
knowledge_bases=[],
|
|
tools=[SimpleNamespace(tool_name='event_reply', operations=['call'])] if allowed else [],
|
|
),
|
|
)
|
|
|
|
async def action(action, data, timeout, *, cancel_peer_on_cancel=False):
|
|
# The 0.7.19 SDK adapter always forwards cancel_peer_on_cancel to the
|
|
# runtime handler; this double stands in for that handler.
|
|
assert cancel_peer_on_cancel is False
|
|
assert action == PluginToRuntimeAction.REPLY_STREAM
|
|
assert data['run_id'] == 'run-1'
|
|
return {
|
|
'result': await session.apply(
|
|
ReplyStreamRequest.model_validate({k: v for k, v in data.items() if k != 'run_id'})
|
|
)
|
|
}
|
|
|
|
transport = SimpleNamespace(call_action=AsyncMock(side_effect=action))
|
|
return RunnerAPIProxy(context, transport), transport
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'native,source,mock',
|
|
[
|
|
(True, True, False),
|
|
(False, True, False),
|
|
(True, False, False),
|
|
(True, True, True),
|
|
],
|
|
)
|
|
async def test_sdk_stream_reuses_adapter_or_sends_one_final_message(native, source, mock):
|
|
session, adapter, incoming = make_session(native=native, source=source, mock=mock)
|
|
api, transport = proxy_for(session)
|
|
async with api.reply_stream() as stream:
|
|
await stream.update('hello')
|
|
await stream.update('hello world')
|
|
adapter.send_message.assert_not_awaited()
|
|
adapter.reply_message.assert_not_awaited()
|
|
assert stream.result['status'] == 'completed'
|
|
assert stream.result['text'] == 'hello world'
|
|
assert transport.call_action.await_count == 3
|
|
if mock:
|
|
adapter.create_message_card.assert_not_awaited()
|
|
adapter.reply_message_chunk.assert_not_awaited()
|
|
adapter.send_message.assert_not_awaited()
|
|
assert stream.result['mock'] is True
|
|
elif native and source:
|
|
adapter.create_message_card.assert_awaited_once()
|
|
delivered_source = adapter.create_message_card.await_args.args[1]
|
|
assert delivered_source.source_platform_object is incoming.source_platform_object
|
|
chunks = adapter.reply_message_chunk.await_args_list
|
|
assert [c.kwargs['bot_message'].all_content for c in chunks] == ['hello', 'hello world', 'hello world']
|
|
assert [c.kwargs['is_final'] for c in chunks] == [False, False, True]
|
|
assert chunks[-1].kwargs['bot_message'].tool_calls is None
|
|
else:
|
|
adapter.reply_message_chunk.assert_not_awaited()
|
|
if source:
|
|
adapter.reply_message.assert_awaited_once()
|
|
assert adapter.reply_message.await_args.kwargs['message'][0].text == 'hello world'
|
|
else:
|
|
adapter.send_message.assert_awaited_once()
|
|
assert adapter.send_message.await_args.args[2][0].text == 'hello world'
|
|
await session.close()
|
|
|
|
|
|
async def test_target_streaming_adapter_updates_card_without_source_message():
|
|
session, adapter, _ = make_session(source=False)
|
|
adapter.create_message_card_for_target = AsyncMock(return_value=True)
|
|
adapter.reply_message_chunk_for_target = AsyncMock()
|
|
key = uuid4()
|
|
|
|
await session.apply(request(key, text='hello'))
|
|
result = await session.apply(request(key, 'finish', 'hello world'))
|
|
|
|
assert result['delivery'] == 'streaming'
|
|
adapter.create_message_card_for_target.assert_awaited_once_with(
|
|
adapter.reply_message_chunk_for_target.await_args.kwargs['message_id'], 'person', 'user-1'
|
|
)
|
|
assert [call.kwargs['content'] for call in adapter.reply_message_chunk_for_target.await_args_list] == [
|
|
'hello',
|
|
'hello world',
|
|
]
|
|
assert [call.kwargs['is_final'] for call in adapter.reply_message_chunk_for_target.await_args_list] == [False, True]
|
|
adapter.send_message.assert_not_awaited()
|
|
await session.close()
|
|
|
|
|
|
async def test_non_message_event_buffers_one_final_reply():
|
|
event = SimpleNamespace(
|
|
event_type='platform.specific',
|
|
delivery=SimpleNamespace(
|
|
surface='platform',
|
|
platform_capabilities={},
|
|
reply_target={'target_type': 'person', 'target_id': 'user-1'},
|
|
),
|
|
)
|
|
source = events.PlatformSpecificEvent(
|
|
type='platform.specific',
|
|
adapter_name='wecombot-omni',
|
|
action='test.event',
|
|
data={'callback_req_id': 'callback-1'},
|
|
)
|
|
adapter = SimpleNamespace(
|
|
is_stream_output_supported=AsyncMock(return_value=True),
|
|
send_message=AsyncMock(),
|
|
)
|
|
session = ReplyStreamSession(event, adapter, source)
|
|
key = uuid4()
|
|
await session.apply(request(key, text='partial'))
|
|
result = await session.apply(request(key, 'finish', 'complete'))
|
|
|
|
assert result['delivery'] == 'buffered'
|
|
adapter.send_message.assert_awaited_once()
|
|
assert adapter.send_message.await_args.args[2][0].text == 'complete'
|
|
await session.close()
|
|
|
|
|
|
async def test_target_reply_is_sent_as_new_message():
|
|
event = SimpleNamespace(
|
|
event_type='message.received',
|
|
delivery=SimpleNamespace(
|
|
surface='platform',
|
|
platform_capabilities={},
|
|
reply_target={'target_type': 'person', 'target_id': 'user-1', 'message_id': 'original-1'},
|
|
),
|
|
)
|
|
adapter = SimpleNamespace(
|
|
is_stream_output_supported=AsyncMock(return_value=True),
|
|
send_message=AsyncMock(),
|
|
)
|
|
session = ReplyStreamSession(event, adapter, source=None)
|
|
key = uuid4()
|
|
await session.apply(request(key, text='partial'))
|
|
result = await session.apply(request(key, 'finish', 'complete'))
|
|
|
|
assert result['delivery'] == 'buffered'
|
|
adapter.send_message.assert_awaited_once()
|
|
assert adapter.send_message.await_args.args[2][0].text == 'complete'
|
|
await session.close()
|
|
|
|
|
|
@pytest.mark.parametrize('native', [True, False])
|
|
@pytest.mark.parametrize('error', [RuntimeError, asyncio.CancelledError])
|
|
async def test_exception_finalizes_visible_card_without_sending_buffered_partial(native, error):
|
|
session, adapter, _ = make_session(native=native)
|
|
api, _ = proxy_for(session)
|
|
with pytest.raises(error):
|
|
async with api.reply_stream() as stream:
|
|
await stream.update('partial')
|
|
raise error()
|
|
if native:
|
|
assert adapter.reply_message_chunk.await_args.kwargs['is_final'] is True
|
|
adapter.reply_message.assert_not_awaited()
|
|
adapter.send_message.assert_not_awaited()
|
|
count = adapter.reply_message_chunk.await_count
|
|
await session.close()
|
|
assert adapter.reply_message_chunk.await_count == count
|
|
|
|
|
|
@pytest.mark.parametrize('allowed,advertised', [(False, True), (True, False)])
|
|
async def test_missing_permission_or_old_host_fails_before_delivery(allowed, advertised):
|
|
session, _, _ = make_session()
|
|
api, transport = proxy_for(session, allowed=allowed, advertised=advertised)
|
|
with pytest.raises(PermissionDeniedError):
|
|
async with api.reply_stream():
|
|
pytest.fail('Not authorized')
|
|
transport.call_action.assert_not_awaited()
|
|
|
|
|
|
async def test_empty_stream_and_duplicate_finish_do_not_send_twice():
|
|
session, adapter, _ = make_session(native=False)
|
|
empty = uuid4()
|
|
await session.apply(request(empty, 'finish', ''))
|
|
adapter.reply_message.assert_not_awaited()
|
|
key = uuid4()
|
|
await session.apply(request(key))
|
|
first = await session.apply(request(key, 'finish'))
|
|
assert await session.apply(request(key, 'finish')) == first
|
|
adapter.reply_message.assert_awaited_once()
|
|
with pytest.raises(ValueError, match='closed'):
|
|
await session.apply(request(key))
|
|
await session.close()
|
|
with pytest.raises(ValueError, match='ended'):
|
|
await session.apply(request(uuid4()))
|
|
|
|
|
|
async def test_host_cleanup_closes_stream_when_plugin_disappears():
|
|
session, adapter, _ = make_session()
|
|
await session.apply(request(uuid4()))
|
|
await session.close()
|
|
assert adapter.reply_message_chunk.await_args.kwargs['is_final'] is True
|
|
|
|
|
|
async def test_uncertain_final_send_is_not_retried_by_finish_or_cleanup():
|
|
session, adapter, _ = make_session(native=False)
|
|
adapter.reply_message.side_effect = TimeoutError('Response lost')
|
|
key = uuid4()
|
|
with pytest.raises(TimeoutError):
|
|
await session.apply(request(key, 'finish'))
|
|
with pytest.raises(ValueError, match='closed'):
|
|
await session.apply(request(key, 'finish'))
|
|
await session.close()
|
|
adapter.reply_message.assert_awaited_once()
|
|
|
|
|
|
async def test_failed_update_closes_existing_card_during_run_cleanup():
|
|
session, adapter, _ = make_session()
|
|
adapter.reply_message_chunk.side_effect = [RuntimeError('update failed'), None]
|
|
with pytest.raises(RuntimeError):
|
|
await session.apply(request(uuid4()))
|
|
await session.close()
|
|
assert adapter.reply_message_chunk.await_args.kwargs['is_final'] is True
|
|
|
|
|
|
async def test_streams_are_isolated_by_run_and_bounded():
|
|
first, a, _ = make_session(native=False)
|
|
second, b, _ = make_session(native=False)
|
|
key = uuid4()
|
|
await first.apply(request(key, 'update', 'first'))
|
|
await second.apply(request(key, 'finish', 'second'))
|
|
assert b.reply_message.await_args.kwargs['message'][0].text == 'second'
|
|
a.reply_message.assert_not_awaited()
|
|
for _ in range(15):
|
|
await first.apply(request(uuid4(), 'finish', ''))
|
|
with pytest.raises(ValueError, match='at most'):
|
|
await first.apply(request(uuid4()))
|
|
await first.close()
|
|
a.reply_message.assert_not_awaited()
|
|
|
|
|
|
async def test_event_processor_uses_shared_sdk_api_and_emits_one_trace_for_the_stream():
|
|
from unittest.mock import Mock
|
|
from langbot_plugin.api.definition.components.runner import Runner, RunnerContext
|
|
|
|
session, adapter, incoming = make_session()
|
|
api, _ = proxy_for(session)
|
|
processor = Runner()
|
|
processor.get_run_api = Mock(return_value=api)
|
|
|
|
@processor.handler(events.MessageReceivedEvent)
|
|
async def handle(ctx):
|
|
async with ctx.reply_stream() as stream:
|
|
await stream.update('one')
|
|
await stream.update('one two')
|
|
|
|
context = RunnerContext.model_validate(
|
|
{
|
|
'run_id': 'run-1',
|
|
'trigger': {'type': incoming.type},
|
|
'event': {
|
|
'event_id': 'one',
|
|
'event_type': incoming.type,
|
|
'source': 'test',
|
|
'data': incoming.model_dump(mode='json', exclude={'source_platform_object', 'legacy_event'}),
|
|
},
|
|
'input': {},
|
|
'delivery': {'surface': 'test'},
|
|
'resources': {},
|
|
'runtime': {},
|
|
}
|
|
)
|
|
results = [result async for result in processor.invoke(context)]
|
|
assert [r.type for r in results] == ['tool.call.started', 'tool.call.completed', 'run.completed']
|
|
assert results[1].data['result']['text'] == 'one two'
|
|
assert adapter.reply_message_chunk.await_count == 3
|
|
await session.close()
|
|
|
|
|
|
async def test_shared_adapter_uses_host_ids_to_isolate_identical_plugin_stream_ids():
|
|
first, adapter, _ = make_session()
|
|
second, _, _ = make_session()
|
|
second.adapter = adapter
|
|
key = uuid4()
|
|
await first.apply(request(key, text='first'))
|
|
await second.apply(request(key, text='second'))
|
|
ids = [c.args[0] for c in adapter.create_message_card.await_args_list]
|
|
assert len(set(ids)) == 2
|
|
assert str(key) not in ids
|
|
await first.close()
|
|
await second.close()
|
|
|
|
|
|
async def test_run_cleanup_cancels_inflight_update_and_finalizes_card():
|
|
session, adapter, _ = make_session()
|
|
started = asyncio.Event()
|
|
|
|
async def update(**kwargs):
|
|
if not kwargs['is_final']:
|
|
started.set()
|
|
await asyncio.Event().wait()
|
|
|
|
adapter.reply_message_chunk.side_effect = update
|
|
task = asyncio.create_task(session.apply(request(uuid4())))
|
|
await asyncio.wait_for(started.wait(), 1)
|
|
await asyncio.wait_for(session.close(), 1)
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await task
|
|
assert adapter.reply_message_chunk.await_args.kwargs['is_final'] is True
|