mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-25 15:37:30 +08:00
* refactor(agent): derive Runner input media from one ordered list Every Runner input is now rendered once from the platform message chain by agent/runner/input_media.py, replacing the contents/message-chain cross matching, the descriptor merge from #2926 and the Box ref reconciliation: - contents place [attachment att-N: <type> <name>] at each media position; quoted messages are wrapped in [quoted message from X] ... [/quoted message] and their media marked source=quote. - attachments describe the same media one-to-one as metadata only and never carry inline content. Media added by user_message_alter stay model-only. - read_input_attachment(ref) serves bytes on demand (ExecutionAPI run scope, available_apis.input_attachments): base64, http(s) URL, adapter path, then adapter get_file_url for ID-only media; 10 MiB per attachment, 50 MiB per run, no caching or payload logging. - Event envelopes carry inline payloads Host-internally until prepare_input_files registers them by ref; steering items drop refs that would address the claimed run's own input. - The media cache defaults to a 1024 MB cap (oldest removed first). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> * docs(plugin): document input attachment refs and read_input_attachment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> * feat(plugins): read input attachments by ref in official Runners Dify, Coze, Tbox and ACP read attachment bytes with ctx.read_input_attachment when the SDK and Host support it, upload each media once under an att-N prefixed name, and skip unreadable ones; older SDKs or Hosts keep the previous inline/URL/contents logic. DeerFlow takes images from contents only on current Hosts, n8n forwards attachment metadata, and LocalAgent exposes refs to the model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> * chore(deps): pin langbot-plugin 0.7.23 from PyPI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> * docs(plugin): note the SDK version for read_input_attachment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> * fix(plugin): accept the SDK send_message timeout and route read_input_attachment SDK 0.7.23 sends a client-side send_message timeout that the Runtime strips; an older Runtime forwards it unchanged, so the Host now accepts and ignores it instead of rejecting the call. The routing integration test covers the new read_input_attachment operation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
137 lines
5.2 KiB
Python
137 lines
5.2 KiB
Python
"""Runners read input attachments by ref from Hosts that serve them."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import base64
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from tests.test_native_parity_external import ctx, load_runner
|
|
|
|
IMAGE = b"\x89PNG image bytes"
|
|
REPORT = b"%PDF report bytes"
|
|
|
|
|
|
class HostContext:
|
|
"""A RunnerContext from a Host with ``input_attachments`` and a newer SDK."""
|
|
|
|
def __init__(self, base, files):
|
|
self._base = base
|
|
self.files = files
|
|
self.reads = []
|
|
self.context = SimpleNamespace(available_apis=SimpleNamespace(input_attachments=True))
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(self._base, name)
|
|
|
|
async def read_input_attachment(self, attachment):
|
|
self.reads.append(attachment.ref)
|
|
if attachment.ref not in self.files:
|
|
raise RuntimeError("cannot be read")
|
|
name, mime_type, data = self.files[attachment.ref]
|
|
return SimpleNamespace(ref=attachment.ref, name=name, mime_type=mime_type, data=data)
|
|
|
|
|
|
def host_context(*, with_report=True):
|
|
from langbot_plugin.api.entities.builtin.provider.message import ContentElement
|
|
from langbot_plugin.api.entities.builtin.runner.input import InputAttachment
|
|
|
|
base = ctx(text="look [attachment att-1: image] and [attachment att-2: file report.pdf]")
|
|
image_data_url = "data:image/png;base64," + base64.b64encode(IMAGE).decode()
|
|
# Contents still carry the image for the model; attachments are metadata only.
|
|
base.input.contents = [ContentElement.from_text("look"), ContentElement.from_image_base64(image_data_url)]
|
|
base.input.attachments = [
|
|
InputAttachment(ref="att-1", type="image", source="message", mime_type="image/png"),
|
|
InputAttachment(ref="att-3", type="image", source="quote"),
|
|
]
|
|
files = {"att-1": (None, "image/png", IMAGE)}
|
|
if with_report:
|
|
base.input.attachments.insert(1, InputAttachment(ref="att-2", type="file", source="message", name="report.pdf"))
|
|
files["att-2"] = ("report.pdf", "application/pdf", REPORT)
|
|
return HostContext(base, files)
|
|
|
|
|
|
def test_dify_uploads_each_attachment_once_named_by_ref():
|
|
with load_runner("dify-agent") as (m, runner):
|
|
client = object.__new__(m.AsyncDifyClient)
|
|
uploads = []
|
|
|
|
async def upload(name, data, mime, user):
|
|
uploads.append((name, data, mime))
|
|
return {"id": f"upload-{len(uploads)}"}
|
|
|
|
client.upload_file = upload
|
|
context = host_context()
|
|
files = asyncio.run(runner._upload_input_files(context, client, "user"))
|
|
|
|
# The unreadable quoted image is skipped instead of failing the run.
|
|
assert context.reads == ["att-1", "att-2", "att-3"]
|
|
assert uploads == [
|
|
("att-1_image.png", IMAGE, "image/png"),
|
|
("att-2_report.pdf", REPORT, "application/pdf"),
|
|
]
|
|
assert [item["type"] for item in files] == ["image", "document"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"plugin,method,client_name",
|
|
[
|
|
("coze-agent", "_build_additional_messages", "AsyncCozeClient"),
|
|
("tbox-agent", "_upload_input_files", "AsyncTboxClient"),
|
|
],
|
|
)
|
|
def test_coze_and_tbox_upload_host_bytes_once(plugin, method, client_name):
|
|
with load_runner(plugin) as (m, runner):
|
|
client = object.__new__(getattr(m, client_name))
|
|
uploads = []
|
|
|
|
async def upload(data, name):
|
|
uploads.append((name, data))
|
|
return "provider-upload"
|
|
|
|
client.upload_file = upload
|
|
# Tbox accepts images only; keep the input to the image it supports.
|
|
context = host_context(with_report=plugin == "coze-agent")
|
|
asyncio.run(getattr(runner, method)(context, client))
|
|
|
|
expected = [("att-1_image.png", IMAGE)]
|
|
if plugin == "coze-agent":
|
|
expected.append(("att-2_report.pdf", REPORT))
|
|
assert uploads == expected
|
|
|
|
|
|
def test_deerflow_takes_images_from_contents_only():
|
|
with load_runner("deerflow-agent") as (_, runner):
|
|
context = host_context()
|
|
context._base.input.attachments[0].url = "https://example.invalid/image.png"
|
|
urls = runner._extract_image_urls(context)
|
|
|
|
assert urls == ["data:image/png;base64," + base64.b64encode(IMAGE).decode()]
|
|
|
|
|
|
def test_n8n_payload_describes_attachments_by_ref():
|
|
with load_runner("n8n-agent") as (_, runner):
|
|
context = host_context()
|
|
payload = runner._build_payload(context, context.input.to_text(), "conversation", "session")
|
|
|
|
assert payload["attachments"] == [
|
|
{"ref": "att-1", "type": "image", "source": "message", "mime_type": "image/png"},
|
|
{"ref": "att-2", "type": "file", "source": "message", "name": "report.pdf"},
|
|
{"ref": "att-3", "type": "image", "source": "quote"},
|
|
]
|
|
assert "[attachment att-2: file report.pdf]" in payload["chatInput"]
|
|
|
|
|
|
def test_acp_hydrates_attachment_content_from_host():
|
|
with load_runner("acp-agent-runner") as (m, _):
|
|
context = host_context()
|
|
asyncio.run(m._hydrate_input_attachments(context))
|
|
|
|
image, report, quoted = context.input.attachments
|
|
assert image.content == "data:image/png;base64," + base64.b64encode(IMAGE).decode()
|
|
assert report.content == "data:application/pdf;base64," + base64.b64encode(REPORT).decode()
|
|
assert report.mime_type == "application/pdf"
|
|
assert quoted.content is None
|