seeds: make calendar.create_event the authoritative example
CODEGEN.md now points to seeds/calendar/create_event/ as the worked example, and the other seeds mirror it: next_event and next_message gain the configured-default contract var plus create_event's tau -> default -> weak-winner candidate resolution, and the shared docstring/banner/_now style. connect_link gets the style alignment only; create_event itself is unchanged.
This commit is contained in:
1 parent
63c5fc9903
commit
e81e6c3a6b
10 files changed
+294
-92
No files matched your search
@@ -1,14 +1,14 @@
|
||||
"""Show the SimpleX contact link others use to connect to this agent.
|
||||
|
||||
Real integration: the standalone SimpleX forwarding bridge
|
||||
(`simplex_bridge_url`). The bridge's own connection to the `simplex-chat`
|
||||
daemon is the only thing that can read (or create) the forwarding bot's *user
|
||||
contact address*; this body asks the bridge for it and reports the link
|
||||
honestly. It never opens a WebSocket to the daemon and never invents a link: if
|
||||
the bridge is not running or has no address, it says so.
|
||||
(`simplex_bridge_url`, `simplex_bridge_token`). The bridge's own connection to
|
||||
the `simplex-chat` daemon is the only thing that can read (or create) the
|
||||
forwarding bot's *user contact address*; this body asks the bridge for it and
|
||||
reports the link honestly. It never opens a WebSocket to the daemon and never
|
||||
invents a link: if the bridge is not running or has no address, it says so.
|
||||
|
||||
`GET /address` shows the existing link and creates one on first call, returning
|
||||
`{short_link, full_link, created}`.
|
||||
`act` health-checks the bridge, then `GET /address` shows the existing link and
|
||||
creates one on first call, returning `{short_link, full_link, created}`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -37,6 +37,9 @@ class BridgeError(Exception):
|
||||
"""The SimpleX forwarding bridge is unreachable or returned an error."""
|
||||
|
||||
|
||||
# ---- transport ----
|
||||
|
||||
|
||||
def _base(ctx):
|
||||
url = str(ctx.config.get("simplex_bridge_url", "") or "").strip().rstrip("/")
|
||||
if not url:
|
||||
@@ -63,6 +66,9 @@ def _get(ctx, path):
|
||||
raise BridgeError(f"GET {path}: {exc}") from exc
|
||||
|
||||
|
||||
# ---- skill phases ----
|
||||
|
||||
|
||||
def act(ctx, request):
|
||||
try:
|
||||
payload = _get(ctx, "/health")
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"simplex_bridge_url": "Base URL of the standalone SimpleX forwarding bridge, e.g. http://127.0.0.1:5227 (no trailing slash needed).",
|
||||
"simplex_bridge_token": "Shared secret for the bridge, if one is configured; sent as the X-Semif-Token header. Leave blank when the bridge requires no auth."
|
||||
"simplex_bridge_token": "Shared secret for the bridge, if one is configured; sent as the X-Semif-Token header. Leave blank when the bridge requires no auth.",
|
||||
"simplex_default_contact": "Default SimpleX contact (contact id or display name) used when the request does not strongly point at a conversation; leave blank to always use the strongest candidate."
|
||||
}
|
||||
@@ -3,9 +3,11 @@
|
||||
Real integration: the standalone SimpleX forwarding bridge
|
||||
(`simplex_bridge_url`) buffers every inbound DM and forwards outbound sends.
|
||||
`act` peeks that inbox, finds which conversations have unread messages, and
|
||||
resolves which one to read with a SemIf sub-decision when more than one does
|
||||
(honoring a configured default contact); it then pops and reports the oldest
|
||||
unread message for that conversation.
|
||||
resolves which one to read with a SemIf choice over them (state = the user
|
||||
query): a strong winner (probability >= `CONTACT_WINNER_TAU`) is used, otherwise
|
||||
the configured default contact is used; with no configured default (or a default
|
||||
with no unread message) the weak winner stands. It then pops and reports the
|
||||
oldest unread message for that conversation.
|
||||
|
||||
The bridge owns the read cursor, so this skill keeps no state of its own. It
|
||||
never opens a WebSocket to the `simplex-chat` daemon — the bridge is the only
|
||||
@@ -25,22 +27,32 @@ from semif_agent.skills import ActionResult
|
||||
INTEGRATION = {
|
||||
"service": "simplex",
|
||||
"transport": "http",
|
||||
"config_vars": ["simplex_bridge_url", "simplex_bridge_token"],
|
||||
"config_vars": [
|
||||
"simplex_bridge_url",
|
||||
"simplex_bridge_token",
|
||||
"simplex_default_contact",
|
||||
],
|
||||
}
|
||||
|
||||
CONTRACT = {
|
||||
"simplex_bridge_url": "Base URL of the standalone SimpleX forwarding bridge, e.g. http://127.0.0.1:5227 (no trailing slash needed).",
|
||||
"simplex_bridge_token": "Shared secret for the bridge, if one is configured; sent as the X-Semif-Token header. Leave blank when the bridge requires no auth.",
|
||||
"simplex_default_contact": "Default SimpleX contact (contact id or display name) used when the request does not strongly point at a conversation; leave blank to always use the strongest candidate.",
|
||||
}
|
||||
|
||||
|
||||
TIMEOUT_SECONDS = 20
|
||||
# A SemIf probability at or above this counts as a "strong winner" for the
|
||||
# conversation; below it the configured default contact is used instead.
|
||||
CONTACT_WINNER_TAU = 0.6
|
||||
|
||||
|
||||
class BridgeError(Exception):
|
||||
"""The SimpleX forwarding bridge is unreachable or returned an error."""
|
||||
|
||||
|
||||
# ---- transport ----
|
||||
|
||||
|
||||
def _base(ctx):
|
||||
url = str(ctx.config.get("simplex_bridge_url", "") or "").strip().rstrip("/")
|
||||
if not url:
|
||||
@@ -67,6 +79,9 @@ def _get(ctx, path):
|
||||
raise BridgeError(f"GET {path}: {exc}") from exc
|
||||
|
||||
|
||||
# ---- conversation resolution ----
|
||||
|
||||
|
||||
def _senders(messages):
|
||||
"""Distinct conversations with buffered messages, in arrival order."""
|
||||
senders = []
|
||||
@@ -82,16 +97,45 @@ def _senders(messages):
|
||||
return senders
|
||||
|
||||
|
||||
def _match_default(senders, default):
|
||||
default = str(default or "").strip()
|
||||
def _default_sender(ctx, senders):
|
||||
"""The configured default contact, if it has unread messages."""
|
||||
default = str(ctx.config.get("simplex_default_contact", "") or "").strip()
|
||||
if not default:
|
||||
return None
|
||||
for sender in senders:
|
||||
if sender["id"] == default or sender["display_name"] == default:
|
||||
return sender["id"]
|
||||
return sender
|
||||
return None
|
||||
|
||||
|
||||
def _resolve_contact(ctx, request, senders, decisions):
|
||||
"""Resolve which conversation to read, mirroring calendar.create_event.
|
||||
|
||||
A strong SemIf winner is used; a weak winner falls back to the configured
|
||||
default contact, and with no configured default the weak winner stands.
|
||||
"""
|
||||
default = _default_sender(ctx, senders)
|
||||
if len(senders) == 1:
|
||||
return senders[0]["id"]
|
||||
|
||||
decision = DecisionRequest(
|
||||
state=request.text,
|
||||
question="Which conversation should I read the next SimpleX message from?",
|
||||
options=[Option(sender["id"], sender["display_name"]) for sender in senders],
|
||||
)
|
||||
result = ctx.engine.call(decision)
|
||||
decisions.append((decision, result))
|
||||
winner = result.selected
|
||||
if result.prob(winner) >= CONTACT_WINNER_TAU:
|
||||
return winner
|
||||
if default is not None:
|
||||
return default["id"]
|
||||
return winner
|
||||
|
||||
|
||||
# ---- skill phases ----
|
||||
|
||||
|
||||
def act(ctx, request):
|
||||
try:
|
||||
payload = _get(ctx, "/inbox")
|
||||
@@ -104,18 +148,7 @@ def act(ctx, request):
|
||||
new_state="no unread SimpleX messages",
|
||||
)
|
||||
decisions = []
|
||||
contact = _match_default(senders, ctx.config.get("simplex_default_contact", ""))
|
||||
if contact is None and len(senders) > 1:
|
||||
decision = DecisionRequest(
|
||||
state=request.text,
|
||||
question="Which conversation should I read the next SimpleX message from?",
|
||||
options=[Option(sender["id"], sender["display_name"]) for sender in senders],
|
||||
)
|
||||
result = ctx.engine.call(decision)
|
||||
decisions.append((decision, result))
|
||||
contact = result.selected
|
||||
if contact is None:
|
||||
contact = senders[0]["id"]
|
||||
contact = _resolve_contact(ctx, request, senders, decisions)
|
||||
try:
|
||||
payload = _get(
|
||||
ctx, f"/inbox/next?contact={urllib.parse.quote(contact)}"
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
Run from this folder: `python skill.test.py`. No external network: a loopback
|
||||
http.server plays the standalone SimpleX forwarding bridge. This proves the body
|
||||
peeks the inbox, resolves the conversation through a SemIf sub-decision (or a
|
||||
configured default), pops the real `next` message, and fails honestly when the
|
||||
bridge is unreachable. It does NOT prove the live integration — only a real run
|
||||
against the bridge and a real contact does.
|
||||
peeks the inbox, resolves the conversation through a SemIf sub-decision (a
|
||||
strong winner is used, otherwise the configured default, otherwise the weak
|
||||
winner), pops the real `next` message, and fails honestly when the bridge is
|
||||
unreachable. It does NOT prove the live integration — only a real run against
|
||||
the bridge and a real contact does.
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -21,19 +22,28 @@ import skill
|
||||
|
||||
|
||||
class FakeEngine:
|
||||
"""Test-only engine: picks the requested option id, else the first."""
|
||||
"""Test-only engine.
|
||||
|
||||
def __init__(self, pick=None):
|
||||
By default it picks the requested option id (else the first) with full
|
||||
confidence. Pass `probs` (a mapping of option id -> probability) to script an
|
||||
arbitrary distribution, e.g. a near-tie that must fall back to the default.
|
||||
"""
|
||||
|
||||
def __init__(self, pick=None, probs=None):
|
||||
self.pick = pick
|
||||
self.probs = probs
|
||||
self.decisions = []
|
||||
|
||||
def call(self, decision):
|
||||
self.decisions.append(decision)
|
||||
option_ids = [option.id for option in decision.options]
|
||||
chosen = self.pick if self.pick in option_ids else option_ids[0]
|
||||
probabilities = [
|
||||
1.0 if option_id == chosen else 0.0 for option_id in option_ids
|
||||
]
|
||||
if self.probs is not None:
|
||||
probabilities = [float(self.probs.get(option_id, 0.0)) for option_id in option_ids]
|
||||
else:
|
||||
chosen = self.pick if self.pick in option_ids else option_ids[0]
|
||||
probabilities = [
|
||||
1.0 if option_id == chosen else 0.0 for option_id in option_ids
|
||||
]
|
||||
return DecisionResult(
|
||||
request=decision, option_ids=option_ids, probabilities=probabilities
|
||||
)
|
||||
@@ -129,7 +139,7 @@ def test_multiple_senders_uses_semif_decision():
|
||||
server.server_close()
|
||||
|
||||
|
||||
def test_default_contact_skips_decision():
|
||||
def test_strong_winner_overrides_default_contact():
|
||||
server, handler, url = start_server(
|
||||
[
|
||||
message("m1", "4", "Alice", "hello there"),
|
||||
@@ -137,13 +147,55 @@ def test_default_contact_skips_decision():
|
||||
]
|
||||
)
|
||||
try:
|
||||
engine = FakeEngine()
|
||||
engine = FakeEngine(probs={"4": 0.1, "7": 0.9})
|
||||
ctx = ActionContext(engine=engine, config=config(url, default="Alice"))
|
||||
request = Request("read my next simplex message from Bob")
|
||||
action = skill.act(ctx, request)
|
||||
assert len(engine.decisions) == 1, "the conversation is chosen with one SemIf decision"
|
||||
assert handler.requests[1] == "/inbox/next?contact=7", handler.requests
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
|
||||
|
||||
def test_weak_winner_falls_back_to_default_contact():
|
||||
server, handler, url = start_server(
|
||||
[
|
||||
message("m1", "4", "Alice", "hello there"),
|
||||
message("m2", "7", "Bob", "shipment arrived"),
|
||||
]
|
||||
)
|
||||
try:
|
||||
# Bob edges out Alice but stays below the strong-winner threshold, so the
|
||||
# configured default ("Alice") is used instead.
|
||||
engine = FakeEngine(probs={"4": 0.45, "7": 0.5})
|
||||
ctx = ActionContext(engine=engine, config=config(url, default="Alice"))
|
||||
request = Request("read my next simplex message")
|
||||
action = skill.act(ctx, request)
|
||||
assert engine.decisions == [], "a configured default needs no SemIf decision"
|
||||
assert "Alice" in action.action_log, action.action_log
|
||||
assert len(engine.decisions) == 1
|
||||
assert handler.requests[1] == "/inbox/next?contact=4", handler.requests
|
||||
assert "hello there" in action.action_log, action.action_log
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
|
||||
|
||||
def test_weak_winner_without_default_stands():
|
||||
server, handler, url = start_server(
|
||||
[
|
||||
message("m1", "4", "Alice", "hello there"),
|
||||
message("m2", "7", "Bob", "shipment arrived"),
|
||||
]
|
||||
)
|
||||
try:
|
||||
engine = FakeEngine(probs={"4": 0.45, "7": 0.5})
|
||||
ctx = ActionContext(engine=engine, config=config(url))
|
||||
request = Request("read my next simplex message")
|
||||
action = skill.act(ctx, request)
|
||||
assert len(engine.decisions) == 1
|
||||
assert handler.requests[1] == "/inbox/next?contact=7", handler.requests
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -207,7 +259,9 @@ def test_auth_token_is_sent_when_configured():
|
||||
|
||||
def main():
|
||||
test_multiple_senders_uses_semif_decision()
|
||||
test_default_contact_skips_decision()
|
||||
test_strong_winner_overrides_default_contact()
|
||||
test_weak_winner_falls_back_to_default_contact()
|
||||
test_weak_winner_without_default_stands()
|
||||
test_single_sender_needs_no_decision()
|
||||
test_empty_inbox_is_reported_honestly()
|
||||
test_unreachable_bridge_fails_honestly()
|
||||
|
||||
Reference in new issue
Block a user