Generic hooks the plugin builds on: the conversations.drawer, composer.send and app.modal slot places (openModal / modal.setResult / modal.close, ?modal= links), runs.start taking the composer's whole payload (auto routing, account, projects, cwd, meta.scheduled) and hub/runs/resolve, a turn's knobs on conversations.send, push buttons answered as the notify.action event, hub/changed (SSE plugin-data), hub/title (conversations.title), the runs.schedule provider behind /api/scheduled, the week's budget and the stopped-run card, providers in /api/plugins/ui, carryover.scheduled. backend/scheduled/ and frontend/src/business/scheduled/ are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
5.7 KiB
Python
122 lines
5.7 KiB
Python
"""The ``complete.run`` / ``conversations.label`` bridges and the
|
|
``conversation.title`` dispatch (plugins/hubcomplete.py, labels_bridge.py):
|
|
the body a plugin may send, the spend booked per engine, the labels written,
|
|
and what the core does with no labelling plugin installed."""
|
|
|
|
import types
|
|
import unittest
|
|
|
|
from plugins import hubcomplete, labels_bridge, manifest
|
|
|
|
USAGE = {"input_tokens": 400, "output_tokens": 20, "cache_read_input_tokens": 0,
|
|
"cache_creation_input_tokens": 0}
|
|
|
|
|
|
class Ledger:
|
|
def __init__(self):
|
|
self.rows = []
|
|
|
|
def add_side_call(self, kind, ref, account, model, cost, units, usage, t=None):
|
|
self.rows.append((kind, ref, account, model, cost, units, usage))
|
|
|
|
|
|
class NormComplete(unittest.TestCase):
|
|
def test_clamps_and_keeps_known_fields(self):
|
|
b = hubcomplete.norm_complete({"prompt": " hi ", "system": "s", "model": "haiku",
|
|
"harness": "claude", "account": "work",
|
|
"fallbackModel": "gemini-3.8-flash",
|
|
"fallbackHarness": "agy", "timeout": 999,
|
|
"bogus": 1})
|
|
self.assertEqual(b, {"system": "s", "prompt": "hi", "wakeLocal": False, "timeout": 120,
|
|
"model": "haiku", "harness": "claude", "account": "work",
|
|
"fallbackModel": "gemini-3.8-flash", "fallbackHarness": "agy"})
|
|
|
|
def test_refuses(self):
|
|
for bad in ({}, {"prompt": "x", "model": "a b"}, {"prompt": "x", "harness": "../x"},
|
|
{"prompt": "x", "account": "A;rm"}):
|
|
with self.assertRaises(ValueError):
|
|
hubcomplete.norm_complete(bad)
|
|
|
|
|
|
class Book(unittest.TestCase):
|
|
def setUp(self):
|
|
self.deps = types.SimpleNamespace(usage_store=Ledger())
|
|
|
|
def test_claude_is_priced_from_its_tokens(self):
|
|
from conversations import pricing
|
|
model = "claude-haiku-5-5-20261001"
|
|
out = hubcomplete.book(self.deps, {"harness": "claude", "model": model, "usage": USAGE,
|
|
"account": "work", "costUsd": 99}, "short-title", "c1")
|
|
[(kind, ref, account, m, cost, units, usage)] = self.deps.usage_store.rows
|
|
u = pricing.norm_usage(USAGE)
|
|
self.assertEqual((kind, ref, account, m, usage), ("short-title", "c1", "work", model, USAGE))
|
|
self.assertAlmostEqual(cost, pricing.usage_cost(u, model))
|
|
self.assertAlmostEqual(units, pricing.plan_units(u, model))
|
|
self.assertAlmostEqual(out["costUsd"], round(cost, 6))
|
|
|
|
def test_direct_engines_carry_their_own_figure(self):
|
|
hubcomplete.book(self.deps, {"harness": "openrouter", "model": "qwen/qwen3.8-27b",
|
|
"usage": USAGE, "account": "openrouter", "costUsd": 0.0012},
|
|
"look", "s")
|
|
hubcomplete.book(self.deps, {"harness": "evox2", "model": "inkling-2-mini",
|
|
"usage": USAGE, "account": "evox2", "costUsd": 0.0},
|
|
"look", "s")
|
|
self.assertEqual([(r[2], r[4], r[5]) for r in self.deps.usage_store.rows],
|
|
[("openrouter", 0.0012, 0.0), ("evox2", 0.0, 0.0)])
|
|
|
|
|
|
class WriteLook(unittest.TestCase):
|
|
def setUp(self):
|
|
self.metas, self.events = {}, []
|
|
self.deps = types.SimpleNamespace(
|
|
meta_store=types.SimpleNamespace(update=lambda sid, p: self.metas.setdefault(sid, p)),
|
|
hub=types.SimpleNamespace(publish=self.events.append))
|
|
|
|
def test_known_action_kept_unknown_stored_as_none(self):
|
|
self.assertEqual(hubcomplete.write_look(self.deps, "s1", " Fix ", "haiku", "plugin:x"),
|
|
"fix")
|
|
self.assertIsNone(hubcomplete.write_look(self.deps, "s2", "dance", "haiku"))
|
|
self.assertEqual(self.metas["s1"]["lookAuto"]["action"], "fix")
|
|
self.assertEqual(self.metas["s1"]["lookAuto"]["source"], "plugin:x")
|
|
self.assertIsNone(self.metas["s2"]["lookAuto"]["action"])
|
|
self.assertEqual(self.events, [{"type": "meta", "id": "s1"}, {"type": "meta", "id": "s2"}])
|
|
|
|
|
|
class NoPlugin(unittest.TestCase):
|
|
"""No ``conversation.title`` provider: nothing is asked (the
|
|
scheduled-conversations plugin then titles a prompt by its first words)."""
|
|
|
|
def test_falls_back(self):
|
|
deps = types.SimpleNamespace(plugin_store=None)
|
|
self.assertFalse(labels_bridge.available(deps))
|
|
self.assertIsNone(labels_bridge.title(deps, "Audit every compose file", "scheduled-title"))
|
|
|
|
def test_a_provider_answer_is_cleaned(self):
|
|
orig = labels_bridge.run
|
|
seen = []
|
|
labels_bridge.run = lambda deps, key, payload: seen.append((key, payload)) or {
|
|
"title": ' "Nightly audit" ', "summary": "Checks."}
|
|
try:
|
|
ans = labels_bridge.title(object(), "x" * 9000, "short-title", "c", "work")
|
|
finally:
|
|
labels_bridge.run = orig
|
|
self.assertEqual(ans, {"title": "Nightly audit", "summary": "Checks."})
|
|
[(key, payload)] = seen
|
|
self.assertEqual(key, "conversation.title")
|
|
self.assertEqual((len(payload["text"]), payload["account"], payload["isolated"]),
|
|
(6000, "work", True))
|
|
|
|
|
|
class Manifest(unittest.TestCase):
|
|
def test_new_capabilities_and_provides_are_operator_only(self):
|
|
for cap in ("complete.run", "conversations.label"):
|
|
self.assertIn(cap, manifest._CAPABILITIES)
|
|
self.assertIn(cap, manifest._OPERATOR_CAPS)
|
|
self.assertIn(cap, manifest._CAP_LABELS)
|
|
for key in ("conversation.title", "conversation.look"):
|
|
self.assertIn(key, manifest._PROVIDES)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|