agents-over-telegram 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +141 -0
  3. package/docs/BOT_EXCHANGES.md +121 -0
  4. package/docs/COMPATIBILITY.md +110 -0
  5. package/docs/DIRECT_DISPATCH.md +155 -0
  6. package/docs/HOSTS.md +244 -0
  7. package/docs/IMPLEMENTATION_STATUS.md +205 -0
  8. package/docs/LIVE_VALIDATION.md +35 -0
  9. package/docs/MACOS_SERVICE.md +89 -0
  10. package/docs/OPERATIONS.md +568 -0
  11. package/docs/REACTIONS.md +51 -0
  12. package/docs/RELAY.md +115 -0
  13. package/docs/RELEASING.md +45 -0
  14. package/docs/schemas/mcp.schema.json +120 -0
  15. package/docs/schemas/sources.json +9 -0
  16. package/integrations/pi/agents-over-telegram.ts +154 -0
  17. package/package.json +41 -0
  18. package/pyproject.toml +37 -0
  19. package/requirements/build.txt +18 -0
  20. package/requirements/runtime.txt +532 -0
  21. package/scripts/bootstrap_runtime.py +402 -0
  22. package/skills/telegram-coordinate/SKILL.md +67 -0
  23. package/skills/telegram-setup/SKILL.md +81 -0
  24. package/src/agents_over_telegram/__init__.py +2 -0
  25. package/src/agents_over_telegram/__main__.py +2 -0
  26. package/src/agents_over_telegram/appserver_client.py +400 -0
  27. package/src/agents_over_telegram/cli.py +851 -0
  28. package/src/agents_over_telegram/codex_trust.py +206 -0
  29. package/src/agents_over_telegram/compat.py +207 -0
  30. package/src/agents_over_telegram/core.py +4253 -0
  31. package/src/agents_over_telegram/credentials.py +108 -0
  32. package/src/agents_over_telegram/demo.py +85 -0
  33. package/src/agents_over_telegram/desktop_bridge.py +537 -0
  34. package/src/agents_over_telegram/desktop_dispatcher.py +680 -0
  35. package/src/agents_over_telegram/dispatcher.py +260 -0
  36. package/src/agents_over_telegram/host_config.py +125 -0
  37. package/src/agents_over_telegram/hosts.py +590 -0
  38. package/src/agents_over_telegram/judge.py +230 -0
  39. package/src/agents_over_telegram/macos_process.py +133 -0
  40. package/src/agents_over_telegram/markdown.py +127 -0
  41. package/src/agents_over_telegram/mcp_server.py +652 -0
  42. package/src/agents_over_telegram/net.py +130 -0
  43. package/src/agents_over_telegram/onboarding.py +719 -0
  44. package/src/agents_over_telegram/push.py +421 -0
  45. package/src/agents_over_telegram/relay.py +380 -0
  46. package/src/agents_over_telegram/relay_api.py +591 -0
  47. package/src/agents_over_telegram/relay_watch.py +171 -0
  48. package/src/agents_over_telegram/serve.py +626 -0
  49. package/src/agents_over_telegram/settings_panel.py +216 -0
  50. package/src/agents_over_telegram/transport.py +720 -0
  51. package/src/agents_over_telegram/webhook.py +219 -0
  52. package/uv.lock +1011 -0
@@ -0,0 +1,108 @@
1
+ """Protected-file fallback. Secrets never enter MCP arguments or diagnostics."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import os
6
+ from pathlib import Path
7
+ import re
8
+ import stat
9
+ import tempfile
10
+
11
+ from . import compat
12
+
13
+
14
+ class CredentialError(Exception):
15
+ def __init__(self, code="unsafe_credentials"):
16
+ self.code = code
17
+ super().__init__(code)
18
+
19
+
20
+ def private_directory(path):
21
+ path = Path(path).absolute()
22
+ try:
23
+ created = not path.exists()
24
+ path.mkdir(mode=0o700, parents=True, exist_ok=True)
25
+ if created and not compat.restrict_directory(path):
26
+ # Windows: a new private folder that keeps its parent's ACL may be
27
+ # readable by others. Remove it, so the next call tries again
28
+ # instead of finding it and using it.
29
+ try:
30
+ path.rmdir()
31
+ except OSError:
32
+ pass
33
+ raise CredentialError("state_directory_acl_failed")
34
+ # A junction or other reparse point could send private files elsewhere.
35
+ compat.refuse_link(path)
36
+ info = path.lstat()
37
+ if not stat.S_ISDIR(info.st_mode) or not compat.owned(info) or not compat.private(info, 0o077):
38
+ raise CredentialError("unsafe_state_directory")
39
+ except OSError:
40
+ raise CredentialError("state_directory_unavailable") from None
41
+ return path
42
+
43
+
44
+ def read_private_json(path):
45
+ try:
46
+ compat.refuse_link(path)
47
+ fd = os.open(path, os.O_RDONLY | compat.O_NOFOLLOW | compat.O_BINARY | compat.O_CLOEXEC)
48
+ with os.fdopen(fd, "r", encoding="utf-8") as stream:
49
+ info = os.fstat(stream.fileno())
50
+ if (not stat.S_ISREG(info.st_mode) or not compat.owned(info)
51
+ or not compat.private(info, 0o077) or info.st_nlink != 1):
52
+ raise CredentialError()
53
+ raw = stream.read(65537)
54
+ if len(raw) > 65536:
55
+ raise CredentialError("invalid_credentials")
56
+ value = json.loads(raw)
57
+ if not isinstance(value, dict):
58
+ raise CredentialError("invalid_credentials")
59
+ return value
60
+ except (OSError, ValueError, UnicodeError):
61
+ raise CredentialError("credentials_unavailable") from None
62
+
63
+
64
+ def write_private_json(path, value, *, overwrite=False):
65
+ path = Path(path).absolute()
66
+ private_directory(path.parent)
67
+ if path.exists() or path.is_symlink():
68
+ if not overwrite:
69
+ raise CredentialError("credentials_already_exist")
70
+ read_private_json(path)
71
+ temp = None
72
+ try:
73
+ fd, temp = tempfile.mkstemp(prefix=".aot-", dir=path.parent)
74
+ with os.fdopen(fd, "w", encoding="utf-8") as stream:
75
+ json.dump(value, stream, separators=(",", ":"))
76
+ stream.write("\n")
77
+ stream.flush()
78
+ os.fsync(stream.fileno())
79
+ if overwrite:
80
+ os.replace(temp, path)
81
+ else:
82
+ # link is an atomic no-replace publication; no race overwrites a secret.
83
+ os.link(temp, path)
84
+ os.unlink(temp)
85
+ temp = None
86
+ if not compat.WINDOWS:
87
+ # Windows cannot open a directory; NTFS journals the rename itself.
88
+ dfd = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY)
89
+ try:
90
+ os.fsync(dfd)
91
+ finally:
92
+ os.close(dfd)
93
+ except OSError:
94
+ raise CredentialError("credential_write_failed") from None
95
+ finally:
96
+ if temp is not None:
97
+ os.unlink(temp)
98
+
99
+
100
+ def validate_token(token):
101
+ if not isinstance(token, str) or not re.fullmatch(r"[0-9]{1,20}:[A-Za-z0-9_-]{20,200}", token):
102
+ raise CredentialError("invalid_token_format")
103
+ return token
104
+
105
+
106
+ def load_token(state_dir):
107
+ private_directory(state_dir)
108
+ return validate_token(read_private_json(Path(state_dir) / "credentials.json").get("token"))
@@ -0,0 +1,85 @@
1
+ """Repeatable acceptance example with no Telegram credentials or network."""
2
+ from __future__ import annotations
3
+ import tempfile
4
+ from .core import Core, CoreError
5
+ from .transport import FakeTransport
6
+
7
+
8
+ def _update(update_id, text, thread_id, *, sender_id=101, chat_id=-100123):
9
+ return {"update_id": update_id, "message": {"message_id": update_id,
10
+ "chat": {"id": chat_id, "type": "supergroup", "title": "Isolated offline example", "is_forum": True},
11
+ "message_thread_id": thread_id, "is_topic_message": True,
12
+ "from": {"id": sender_id, "is_bot": False}, "text": text}}
13
+
14
+
15
+ def run_demo():
16
+ now = [1000.0]
17
+ transport = FakeTransport()
18
+ with tempfile.TemporaryDirectory(prefix="aot-offline-demo-") as state:
19
+ core = Core(state, transport, clock=lambda: now[0])
20
+ bot = transport.get_me()
21
+ core.register_account(bot["id"], bot["username"])
22
+ alpha, beta = core.create_client("Alpha"), core.create_client("Beta")
23
+ a, b = alpha["client_id"], beta["client_id"]
24
+ pair_a, pair_b = core.connection_begin(a), core.connection_begin(b)
25
+ transport.updates.extend([_update(1, pair_a["command"], 11), _update(2, pair_b["command"], 22)])
26
+ core.collect()
27
+ core.confirm_pairing(pair_a["pairing_id"])
28
+ core.confirm_pairing(pair_b["pairing_id"])
29
+ transport.updates.extend([_update(3, "Alpha request", 11), _update(4, "Beta request", 22),
30
+ _update(5, "Unknown sender never reaches either agent", 11, sender_id=999)])
31
+ intake = core.collect()
32
+ page_a, page_b = core.poll(a), core.poll(b)
33
+ assert [m["text"] for m in page_a["deliveries"]] == ["Alpha request"]
34
+ assert [m["text"] for m in page_b["deliveries"]] == ["Beta request"]
35
+ cross_ack_blocked = False
36
+ try:
37
+ core.ack(a, [page_b["deliveries"][0]["delivery_id"]], page_b["lease_token"])
38
+ except CoreError:
39
+ cross_ack_blocked = True
40
+ assert cross_ack_blocked
41
+ for client, page in ((a,page_a), (b,page_b)):
42
+ core.ack(client, [m["delivery_id"] for m in page["deliveries"]], page["lease_token"])
43
+ core.grant_peer(a, b)
44
+ published = core.send(a, "Broker handoff", "handoff-1", recipient=b)
45
+ handoff = core.poll(b)
46
+ assert len(handoff["deliveries"]) == 1
47
+ assert handoff["deliveries"][0]["origin"] == "broker"
48
+ assert len(transport.updates) == 5 # Telegram never echoed outbound messages.
49
+ repeated = core.send(a, "Broker handoff", "handoff-1", recipient=b)
50
+ assert repeated["outbox_id"] == published["outbox_id"]
51
+ assert len(transport.sent) == 1
52
+ now[0] += 2
53
+ transport.outcomes.append("unknown")
54
+ uncertain = core.send(a, "Accepted remotely but response lost", "unknown-1")
55
+ assert uncertain["status"] == "unknown"
56
+ restarted = Core(state, transport, clock=lambda: now[0])
57
+ restarted.flush(a)
58
+ assert restarted.delivery_status(a, uncertain["outbox_id"])["status"] == "unknown"
59
+ assert len(transport.sent) == 2
60
+ restarted.pause(a)
61
+ transport.updates.extend([_update(6, "Retained during pause", 11), _update(7, "Beta still active", 22)])
62
+ restarted.collect()
63
+ assert restarted.poll(a)["deliveries"] == []
64
+ assert any(m["text"] == "Beta still active" for m in restarted.poll(b)["deliveries"])
65
+ restarted.pause(a, False)
66
+ assert any(m["text"] == "Retained during pause" for m in restarted.poll(a)["deliveries"])
67
+ history_page = restarted.history(a, limit=1)
68
+ history = list(history_page["messages"])
69
+ while history_page["next_cursor"]:
70
+ history_page = restarted.history(a, limit=1, cursor=history_page["next_cursor"])
71
+ history.extend(history_page["messages"])
72
+ assert any(m["text"] == "Alpha request" and m["status"] == "acknowledged" for m in history)
73
+ assert any(m["direction"] == "outgoing" and m["status"] == "unknown" for m in history)
74
+ assert not any(m["text"] == "Beta request" for m in history)
75
+ assert all(m["context_only"] for m in history)
76
+ assert len(transport.sent) == 2
77
+ now[0] += 86401
78
+ assert restarted.history(a)["messages"] == []
79
+ return {"result": "PASS", "network": "none", "credentials": "none", "bot_accounts": 1,
80
+ "agents": 2, "dedicated_topics": [11,22], "shared_cursor_after_first_intake": intake["cursor"],
81
+ "cross_agent_ack_blocked": cross_ack_blocked, "same_bot_delivery_origin": "broker",
82
+ "duplicate_send_suppressed": True, "unknown_send_survived_restart_without_retry": True,
83
+ "pause_retained_work_and_other_agent_continued": True, "temporary_state_removed": True,
84
+ "history_24h_read_only_context": True, "acknowledged_history_available": True,
85
+ "history_pagination_isolated": True, "history_cutoff_verified": True}