mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-22 18:27:12 +00:00
fix(wecombot): deliver sandbox outbox media through full pipeline chain (#2328)
* fix(wecombot): align media upload protocol * fix(wecombot): deliver outbox media in reply and fix tool call recording - Integrate _send_media into reply_message and reply_message_chunk so sandbox outbox images/voices/files are uploaded and sent instead of being silently dropped. - Add missing import base64 that caused _send_media to fail with a NameError swallowed by its except clause. - Change yiri2target to return component dicts (text/image/voice/file) so callers can distinguish text from media. - Fix _get_message_for_tool_context using result.first()/row[0] which returned a raw string instead of the ORM object, causing "'str' object has no attribute 'pipeline_id'" in tool call recording. Use result.scalars().first() per SQLAlchemy 2.0 convention. * fix(pipeline): collect outbox attachments on final chunk with empty content When the last streaming chunk has is_final=True but empty content (e.g. the LLM sends all text in earlier chunks), the 'if result.content' branch is skipped entirely, so _append_outbound_attachments never runs and sandbox outbox images are silently dropped. Add an elif branch for _is_final_assistant_message that creates an empty MessageChain and still collects outbox attachments, so images are delivered even when the final chunk carries no text. * fix(box): bypass stdout truncation when reading outbox via exec _read_outbox_via_exec used execute_tool which returns _serialize_result where stdout is truncated to output_limit_chars (4000). A 7KB JPEG encodes to ~9400 base64 chars, so the JSON payload was truncated and json.loads failed silently, returning an empty list. Call client.execute directly to get the raw BoxExecutionResult with untruncated stdout, so base64 file data is preserved. * fix(tests): adapt box and wrapper tests for client.execute and strict is_final check - wrapper.py: restrict outbox collection on empty-content chunks to actual MessageChunk instances with is_final=True, not generic Mock objects that happen to have role='assistant' - test_box_service.py: update _read_outbox_via_exec tests to mock client.execute (returning BoxExecutionResult) instead of execute_tool, matching the implementation change * chore(wecombot): remove temporary upload log * test(box): preserve direct outbox read and cleanup coverage --------- Co-authored-by: fdc310 <2213070223@qq.com> Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
This commit is contained in:
@@ -2163,25 +2163,38 @@ class TestInboundOutboundRoundTrip:
|
||||
|
||||
calls = []
|
||||
|
||||
async def fake_execute_tool(parameters, q):
|
||||
calls.append(parameters['command'])
|
||||
if 'os.scandir' in parameters['command']:
|
||||
return {
|
||||
'ok': True,
|
||||
'stdout': '[{"name": "out.png", "b64": "QUJD"}]',
|
||||
'stderr': '',
|
||||
}
|
||||
async def fake_client_execute(spec):
|
||||
cmd = spec.cmd
|
||||
calls.append(cmd)
|
||||
if 'os.scandir' in cmd:
|
||||
return BoxExecutionResult(
|
||||
session_id='s',
|
||||
backend_name='test',
|
||||
status=BoxExecutionStatus.COMPLETED,
|
||||
exit_code=0,
|
||||
stdout='[{"name": "out.png", "b64": "QUJD"}]',
|
||||
duration_ms=10,
|
||||
)
|
||||
# the rm -rf cleanup call
|
||||
return {'ok': True, 'stdout': '', 'stderr': ''}
|
||||
return BoxExecutionResult(
|
||||
session_id='s',
|
||||
backend_name='test',
|
||||
status=BoxExecutionStatus.COMPLETED,
|
||||
exit_code=0,
|
||||
stdout='',
|
||||
duration_ms=10,
|
||||
)
|
||||
|
||||
service.execute_tool = AsyncMock(side_effect=fake_execute_tool)
|
||||
service.client.execute = AsyncMock(side_effect=fake_client_execute)
|
||||
service.execute_tool = AsyncMock(return_value={'ok': True, 'stdout': '', 'stderr': ''})
|
||||
|
||||
attachments = await service.collect_outbound_attachments(query)
|
||||
assert len(attachments) == 1
|
||||
assert attachments[0]['type'] == 'Image'
|
||||
assert attachments[0]['name'] == 'out.png'
|
||||
# cleanup (rm -rf) must have been issued after a successful collection
|
||||
assert any('rm -rf' in c for c in calls)
|
||||
service.execute_tool.assert_awaited_once()
|
||||
assert 'rm -rf' in service.execute_tool.await_args.args[0]['command']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_collect_outbound_empty_still_clears(self):
|
||||
@@ -2193,16 +2206,33 @@ class TestInboundOutboundRoundTrip:
|
||||
|
||||
calls = []
|
||||
|
||||
async def fake_execute_tool(parameters, q):
|
||||
calls.append(parameters['command'])
|
||||
if 'os.scandir' in parameters['command']:
|
||||
return {'ok': True, 'stdout': '[]', 'stderr': ''}
|
||||
return {'ok': True, 'stdout': '', 'stderr': ''}
|
||||
async def fake_client_execute(spec):
|
||||
cmd = spec.cmd
|
||||
calls.append(cmd)
|
||||
if 'os.scandir' in cmd:
|
||||
return BoxExecutionResult(
|
||||
session_id='s',
|
||||
backend_name='test',
|
||||
status=BoxExecutionStatus.COMPLETED,
|
||||
exit_code=0,
|
||||
stdout='[]',
|
||||
duration_ms=10,
|
||||
)
|
||||
return BoxExecutionResult(
|
||||
session_id='s',
|
||||
backend_name='test',
|
||||
status=BoxExecutionStatus.COMPLETED,
|
||||
exit_code=0,
|
||||
stdout='',
|
||||
duration_ms=10,
|
||||
)
|
||||
|
||||
service.execute_tool = AsyncMock(side_effect=fake_execute_tool)
|
||||
service.client.execute = AsyncMock(side_effect=fake_client_execute)
|
||||
service.execute_tool = AsyncMock(return_value={'ok': True, 'stdout': '', 'stderr': ''})
|
||||
assert await service.collect_outbound_attachments(query) == []
|
||||
# cleanup (rm -rf) is issued unconditionally now
|
||||
assert any('rm -rf' in c for c in calls)
|
||||
service.execute_tool.assert_awaited_once()
|
||||
assert 'rm -rf' in service.execute_tool.await_args.args[0]['command']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_passthrough_noop_when_unavailable(self):
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import base64
|
||||
|
||||
import pytest
|
||||
|
||||
import langbot.pkg.core.app # noqa: F401
|
||||
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||
from langbot.libs.wecom_ai_bot_api.ws_client import _UPLOAD_CHUNK_SIZE, WecomBotWsClient
|
||||
from langbot.pkg.platform.sources.wecombot import WecomBotAdapter, WecomBotMessageConverter
|
||||
|
||||
|
||||
class Logger:
|
||||
def __init__(self):
|
||||
self.warnings = []
|
||||
self.errors = []
|
||||
|
||||
async def warning(self, message):
|
||||
self.warnings.append(message)
|
||||
|
||||
async def error(self, message):
|
||||
self.errors.append(message)
|
||||
|
||||
async def info(self, message):
|
||||
return None
|
||||
|
||||
|
||||
class UploadClient(WecomBotWsClient):
|
||||
def __init__(self):
|
||||
super().__init__(bot_id='bot', secret='secret', logger=Logger())
|
||||
self.frames = []
|
||||
|
||||
async def _send_reply(self, req_id: str, body: dict, cmd: str = 'aibot_respond_msg'):
|
||||
self.frames.append((cmd, body))
|
||||
if cmd == 'aibot_upload_media_init':
|
||||
return {'errcode': 0, 'body': {'upload_id': 'upload-1'}}
|
||||
if cmd == 'aibot_upload_media_finish':
|
||||
return {'errcode': 0, 'body': {'media_id': 'media-1'}}
|
||||
return {'errcode': 0}
|
||||
|
||||
|
||||
class Bot:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
|
||||
async def upload_media(self, data, filename='attachment', media_type='file'):
|
||||
self.calls.append(('upload_media', media_type, filename, data))
|
||||
return {'media_id': 'media-1'}
|
||||
|
||||
async def reply_text(self, req_id, content):
|
||||
self.calls.append(('reply_text', req_id, content))
|
||||
|
||||
async def reply_image(self, req_id, media_id):
|
||||
self.calls.append(('reply_image', req_id, media_id))
|
||||
|
||||
async def send_message(self, target_id, content):
|
||||
self.calls.append(('send_message', target_id, content))
|
||||
|
||||
|
||||
def make_adapter(bot):
|
||||
return WecomBotAdapter.model_construct(
|
||||
bot=bot,
|
||||
config={'enable-webhook': False},
|
||||
logger=Logger(),
|
||||
message_converter=WecomBotMessageConverter(),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ws_client_upload_media_uses_chunk_protocol():
|
||||
client = UploadClient()
|
||||
data = b'a' * (_UPLOAD_CHUNK_SIZE + 1)
|
||||
|
||||
upload_result = await client.upload_media(data, 'image.png', media_type='image')
|
||||
|
||||
assert upload_result['media_id'] == 'media-1'
|
||||
assert [cmd for cmd, _ in client.frames] == [
|
||||
'aibot_upload_media_init',
|
||||
'aibot_upload_media_chunk',
|
||||
'aibot_upload_media_chunk',
|
||||
'aibot_upload_media_finish',
|
||||
]
|
||||
init_body = client.frames[0][1]
|
||||
assert init_body['type'] == 'image'
|
||||
assert init_body['filename'] == 'image.png'
|
||||
assert init_body['total_size'] == len(data)
|
||||
assert init_body['total_chunks'] == 2
|
||||
assert client.frames[1][1]['chunk_index'] == 0
|
||||
assert base64.b64decode(client.frames[1][1]['base64_data']) == b'a' * _UPLOAD_CHUNK_SIZE
|
||||
assert client.frames[2][1]['chunk_index'] == 1
|
||||
assert base64.b64decode(client.frames[2][1]['base64_data']) == b'a'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reply_message_uploads_and_replies_image_media():
|
||||
bot = Bot()
|
||||
adapter = make_adapter(bot)
|
||||
png_data = b'\x89PNG\r\n\x1a\nimage'
|
||||
image_b64 = base64.b64encode(png_data).decode('utf-8')
|
||||
chain = platform_message.MessageChain([platform_message.Image(base64=f'data:image/png;base64,{image_b64}')])
|
||||
|
||||
items = await WecomBotMessageConverter.yiri2target(chain)
|
||||
await adapter._send_media(bot, 'req-1', items[0])
|
||||
|
||||
assert bot.calls == [
|
||||
('upload_media', 'image', 'attachment.image', png_data),
|
||||
('reply_image', 'req-1', 'media-1'),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_sends_text_and_skips_proactive_image():
|
||||
bot = Bot()
|
||||
adapter = make_adapter(bot)
|
||||
jpg_data = b'\xff\xd8\xffimage'
|
||||
image_b64 = base64.b64encode(jpg_data).decode('utf-8')
|
||||
chain = platform_message.MessageChain(
|
||||
[
|
||||
platform_message.Plain(text='before'),
|
||||
platform_message.Image(base64=f'data:image/jpeg;base64,{image_b64}'),
|
||||
platform_message.Plain(text='after'),
|
||||
]
|
||||
)
|
||||
|
||||
await adapter.send_message('group', 'chat-1', chain)
|
||||
|
||||
assert bot.calls == [
|
||||
('send_message', 'chat-1', 'beforeafter'),
|
||||
]
|
||||
Reference in New Issue
Block a user