mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-22 05:27:07 +08:00
Copy was broken twice over. `langbot.api()` already resolves the unwrapped
payload (PageResponse.ok -> HTTP data -> resp.data.data -> the SDK's
resolve(data.data)), so `item.data.url` was always `undefined` and the page
copied an empty string. `writeText('')` resolves without throwing, so it also
never reached the fallback: the UI said "copied" while the clipboard stayed
empty.
The iframe is worse than assumed. It is a `blob:` document under
`sandbox="allow-scripts allow-forms"`, so its origin is opaque: `location.origin`
is "null", relative asset URLs do not load, and `navigator.clipboard` rejects
with NotAllowedError. `/list` returns relative URLs while `public_base_url` is
empty, so even a correct value would have pasted as an unusable path. URLs are
now made absolute against `location.ancestorOrigins[0]` before either the
clipboard or an `<img src>`, and copy reads `item.url` from the list cache
instead of making a redundant `/item` round trip.
Every other call had the same layer mistake: `res.error` is never set on a
resolved value, because failures reject the promise, so no failure ever
surfaced. All call sites now catch, and an empty URL toasts an error rather
than claiming success.
Verified in a real blob: iframe with the real sandbox: relative url -> absolute
-> clipboard read back byte for byte, and the same URL loads as the preview
(0 `/image` calls). Also passes with `navigator.clipboard` removed, proving the
execCommand tier is what carries it.
The host is untouched now, so this is plugin-only; the `allow-modals` flip is
dropped and the clear confirmation stays in-page.
Co-authored-by: multica-agent <github@multica.ai>
121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
"""Tool contract and the stable cross-plugin interface."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
|
|
from components.tools.add_sticker import AddSticker
|
|
from components.tools.delete_sticker import DeleteSticker
|
|
from components.tools.get_sticker_url import GetStickerUrl
|
|
from components.tools.list_stickers import ListStickers
|
|
|
|
PNG = b"\x89PNG\r\n\x1a\n" + b"\x00" * 16
|
|
JPEG = b"\xff\xd8\xff\xe0" + b"\x00" * 16
|
|
|
|
|
|
def run(coro):
|
|
return asyncio.run(coro)
|
|
|
|
|
|
def make_tool(plugin, tool):
|
|
tool.plugin = plugin
|
|
return tool
|
|
|
|
|
|
def data_url(payload: bytes) -> str:
|
|
import base64
|
|
|
|
return "data:image/png;base64," + base64.b64encode(payload).decode("ascii")
|
|
|
|
|
|
def test_add_sticker_tool_stores_image(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin()
|
|
tool = make_tool(plugin, AddSticker())
|
|
result = json.loads(
|
|
await tool.call({"name": "cat", "data_url": data_url(PNG)}, {}, 1)
|
|
)
|
|
assert result["ok"] is True
|
|
assert result["sticker"]["slug"] == "cat"
|
|
assert await plugin.get_sticker("cat") is not None
|
|
run(scenario())
|
|
|
|
|
|
def test_add_sticker_tool_requires_input(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin()
|
|
tool = make_tool(plugin, AddSticker())
|
|
assert json.loads(await tool.call({}, {}, 1))["ok"] is False
|
|
assert json.loads(await tool.call({"name": "cat"}, {}, 1))["ok"] is False
|
|
run(scenario())
|
|
|
|
|
|
def test_get_sticker_url_is_the_stable_contract(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin({"public_base_url": "http://localhost:5300"})
|
|
await plugin.add_sticker("cat", PNG)
|
|
tool = make_tool(plugin, GetStickerUrl())
|
|
url = await tool.call({"name": "cat"}, {}, 1)
|
|
assert url == (
|
|
"http://localhost:5300/api/v1/plugins/langbot-team/StickerHub/assets/stickers/"
|
|
+ (await plugin.get_sticker("cat"))["filename"]
|
|
)
|
|
# The same URL comes back from the plugin API and the Page endpoint, so
|
|
# HumanTakeover gets a stable value regardless of the caller.
|
|
page_url = (await plugin.get_sticker("cat"))["url"]
|
|
assert page_url == url
|
|
run(scenario())
|
|
|
|
|
|
def test_get_sticker_url_missing_is_error_string(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin()
|
|
tool = make_tool(plugin, GetStickerUrl())
|
|
url = await tool.call({"name": "nope"}, {}, 1)
|
|
assert url.startswith("error:")
|
|
assert "nope" in url
|
|
run(scenario())
|
|
|
|
|
|
def test_list_stickers_tool(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin()
|
|
await plugin.add_sticker("cat", PNG)
|
|
await plugin.add_sticker("dog", JPEG)
|
|
tool = make_tool(plugin, ListStickers())
|
|
result = json.loads(await tool.call({"limit": 5}, {}, 1))
|
|
assert result["ok"] is True
|
|
assert result["total"] == 2
|
|
assert {item["name"] for item in result["items"]} == {"cat", "dog"}
|
|
for item in result["items"]:
|
|
assert item["uri"].startswith("/api/v1/plugins/langbot-team/StickerHub/assets/stickers/")
|
|
run(scenario())
|
|
|
|
|
|
def test_delete_sticker_tool(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin()
|
|
await plugin.add_sticker("cat", PNG)
|
|
tool = make_tool(plugin, DeleteSticker())
|
|
assert json.loads(await tool.call({"name": "cat"}, {}, 1))["ok"] is True
|
|
assert json.loads(await tool.call({"name": "cat"}, {}, 1))["ok"] is False
|
|
run(scenario())
|
|
|
|
|
|
def test_add_then_get_round_trip_through_tools(make_plugin):
|
|
"""The AI path: store via the tool, then fetch the stable URL via the tool."""
|
|
|
|
async def scenario():
|
|
plugin, _ = make_plugin({"public_base_url": "https://bot.example.com"})
|
|
add = make_tool(plugin, AddSticker())
|
|
get = make_tool(plugin, GetStickerUrl())
|
|
|
|
stored = json.loads(
|
|
await add.call({"name": "问候", "data_url": data_url(PNG)}, {}, 1)
|
|
)
|
|
url = await get.call({"name": "问候"}, {}, 1)
|
|
assert url == stored["sticker"]["url"]
|
|
assert url.startswith("https://bot.example.com/api/v1/plugins/langbot-team/StickerHub/")
|
|
run(scenario())
|