Files
WeKnora/pluginsdk/python/tests/fixture.py
T
lyingbug 728007ada9 feat(plugin): remote runtime, Python SDK and python host plugins (P2 batch 3) (#3724)
* feat(plugin): remote runtime
* feat(plugin): Python SDK and python host plugins
* fix(pluginsdk): skip reverse DNS when a Python plugin binds
2026-09-26 14:46:03 +08:00

79 lines
2.4 KiB
Python

"""A plugin exercising every contribution kind: the SDK's tests and the Go
conformance suite run it."""
from __future__ import annotations
import os
import sys
import time
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src"))
from weknora_plugin import ( # noqa: E402
Cursor,
ErrorCode,
FetchedItem,
ParsedImage,
ParseOutput,
Plugin,
PluginError,
Resource,
SearchResult,
invalid_config,
)
plugin = Plugin("acme.fixture", "1.0.0")
@plugin.config_validator
def validate(call):
if call.tenant.get("region") == "mars":
raise invalid_config("unknown region", {"region": "no such region"})
@plugin.web_search("echo")
def search(call, q):
if q.query == "down":
raise PluginError(ErrorCode.UNAVAILABLE, "upstream is down")
if q.query == "crash":
raise RuntimeError("boom")
if q.query == "slow":
time.sleep(0.5)
return [SearchResult(title=q.query, url=f"https://example.com/?tenant={call.tenant_id}", snippet=call.locale)]
@plugin.parser("upper")
def parse(call, doc):
text = doc.content.decode("utf-8", "replace")
return ParseOutput(
markdown=text.upper() + "\n\n![](img/dot.png)",
images=[ParsedImage(original_ref="img/dot.png", data=b"\x89PNG", mime_type="image/png")],
metadata={"fileType": doc.file_type},
)
@plugin.connector("notes")
class Notes:
def validate(self, call, cfg):
if cfg.credentials.get("token") == "bad":
raise PluginError(ErrorCode.UNAUTHORIZED, "token rejected")
def list_resources(self, call, cfg, parent_id):
return [Resource(external_id="inbox", name="Inbox", has_children=not parent_id)]
def fetch(self, call, cfg, inp, stream):
start = int((inp.cursor.state or {}).get("after", 0)) if inp.cursor else 0
if cfg.settings.get("fail") == "before":
raise PluginError(ErrorCode.UNAUTHORIZED, "token rejected")
for i in range(start, start + 3):
stream.item(FetchedItem(external_id=f"n{i}", title=f"Note {i}", content=f"# Note {i}"))
if cfg.settings.get("fail") == "during":
raise PluginError(ErrorCode.RATE_LIMITED, "slow down", retry_after=7)
stream.checkpoint(Cursor(state={"after": start + 3}))
stream.progress("done")
return Cursor(state={"after": start + 3})
if __name__ == "__main__":
plugin.serve()