meshcode 2.5.3__tar.gz → 2.5.5__tar.gz
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.
- {meshcode-2.5.3 → meshcode-2.5.5}/PKG-INFO +2 -1
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/__init__.py +1 -1
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/backend.py +124 -12
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/realtime.py +1 -1
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/server.py +95 -19
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode.egg-info/PKG-INFO +2 -1
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode.egg-info/requires.txt +1 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/pyproject.toml +2 -1
- {meshcode-2.5.3 → meshcode-2.5.5}/README.md +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/cli.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/comms_v4.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/invites.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/launcher.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/launcher_install.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/__init__.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/__main__.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/test_backend.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/test_realtime.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/meshcode_mcp/test_server_wrapper.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/preferences.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/protocol_v2.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/run_agent.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/secrets.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/self_update.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode/setup_clients.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode.egg-info/SOURCES.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode.egg-info/dependency_links.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode.egg-info/entry_points.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/meshcode.egg-info/top_level.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/setup.cfg +0 -0
- {meshcode-2.5.3 → meshcode-2.5.5}/tests/test_status_enum_coverage.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: meshcode
|
|
3
|
-
Version: 2.5.
|
|
3
|
+
Version: 2.5.5
|
|
4
4
|
Summary: Real-time communication between AI agents — Supabase-backed CLI
|
|
5
5
|
Author-email: MeshCode <hello@meshcode.io>
|
|
6
6
|
License: MIT
|
|
@@ -22,6 +22,7 @@ Requires-Dist: mcp[cli]>=1.0.0
|
|
|
22
22
|
Requires-Dist: websockets>=12.0
|
|
23
23
|
Requires-Dist: realtime>=2.0.0
|
|
24
24
|
Requires-Dist: keyring>=24.0
|
|
25
|
+
Requires-Dist: cryptography>=41.0
|
|
25
26
|
|
|
26
27
|
# MeshCode
|
|
27
28
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"""MeshCode — Real-time communication between AI agents."""
|
|
2
|
-
__version__ = "2.5.
|
|
2
|
+
__version__ = "2.5.5"
|
|
@@ -235,9 +235,46 @@ def register_agent(project: str, name: str, role: str = "") -> Dict:
|
|
|
235
235
|
}
|
|
236
236
|
|
|
237
237
|
|
|
238
|
-
def send_message(project_id: str, from_agent: str, to_agent: str, payload: Any, msg_type: str = "msg", parent_msg_id: Optional[str] = None, sensitive: bool = False) -> Dict:
|
|
238
|
+
def send_message(project_id: str, from_agent: str, to_agent: str, payload: Any, msg_type: str = "msg", parent_msg_id: Optional[str] = None, sensitive: bool = False, api_key: Optional[str] = None, encrypted: bool = False) -> Dict:
|
|
239
239
|
if not isinstance(payload, dict):
|
|
240
240
|
payload = {"text": str(payload)}
|
|
241
|
+
|
|
242
|
+
# Encrypt payload if requested
|
|
243
|
+
actual_encrypted = False
|
|
244
|
+
if encrypted and api_key:
|
|
245
|
+
mesh_key = get_mesh_key(api_key, project_id)
|
|
246
|
+
if mesh_key:
|
|
247
|
+
encrypted_data = encrypt_payload(payload, mesh_key)
|
|
248
|
+
payload = {"_encrypted": encrypted_data}
|
|
249
|
+
actual_encrypted = True
|
|
250
|
+
|
|
251
|
+
# Use validated SECURITY DEFINER RPC when api_key is provided
|
|
252
|
+
if api_key:
|
|
253
|
+
params = {
|
|
254
|
+
"p_api_key": api_key,
|
|
255
|
+
"p_project_id": project_id,
|
|
256
|
+
"p_from_agent": from_agent,
|
|
257
|
+
"p_to_agent": to_agent,
|
|
258
|
+
"p_payload": payload,
|
|
259
|
+
"p_type": msg_type,
|
|
260
|
+
}
|
|
261
|
+
if parent_msg_id:
|
|
262
|
+
params["p_parent_msg_id"] = parent_msg_id
|
|
263
|
+
if sensitive or actual_encrypted:
|
|
264
|
+
params["p_sensitive"] = True
|
|
265
|
+
result = sb_rpc("mc_send_message", params)
|
|
266
|
+
# If RPC succeeded, return
|
|
267
|
+
if isinstance(result, dict) and result.get("ok"):
|
|
268
|
+
out = {"sent": True, "msg_id": result.get("msg_id")}
|
|
269
|
+
if actual_encrypted:
|
|
270
|
+
out["encrypted"] = True
|
|
271
|
+
return out
|
|
272
|
+
# If RPC returned an auth/validation error, propagate it
|
|
273
|
+
if isinstance(result, dict) and result.get("error") and result.get("error_code"):
|
|
274
|
+
return {"error": result["error"]}
|
|
275
|
+
# If RPC doesn't exist yet (migration not applied), fall through to direct insert
|
|
276
|
+
|
|
277
|
+
# Fallback: direct insert (tests / legacy)
|
|
241
278
|
msg = {
|
|
242
279
|
"project_id": project_id,
|
|
243
280
|
"from_agent": from_agent,
|
|
@@ -258,15 +295,39 @@ def send_message(project_id: str, from_agent: str, to_agent: str, payload: Any,
|
|
|
258
295
|
return {"sent": True}
|
|
259
296
|
|
|
260
297
|
|
|
261
|
-
def read_inbox(project_id: str, agent: str, mark_read: bool = True) -> List[Dict]:
|
|
298
|
+
def read_inbox(project_id: str, agent: str, mark_read: bool = True, api_key: Optional[str] = None) -> List[Dict]:
|
|
262
299
|
messages = sb_select(
|
|
263
300
|
"mc_messages",
|
|
264
301
|
f"project_id=eq.{project_id}&to_agent=eq.{quote(agent)}&read=eq.false",
|
|
265
302
|
order="created_at.asc",
|
|
266
303
|
)
|
|
304
|
+
# Auto-decrypt encrypted messages
|
|
305
|
+
if api_key and messages:
|
|
306
|
+
mesh_key = None # lazy-load
|
|
307
|
+
for m in messages:
|
|
308
|
+
p = m.get("payload")
|
|
309
|
+
if isinstance(p, dict) and "_encrypted" in p:
|
|
310
|
+
if mesh_key is None:
|
|
311
|
+
mesh_key = get_mesh_key(api_key, project_id)
|
|
312
|
+
if mesh_key:
|
|
313
|
+
decrypted = decrypt_payload(p["_encrypted"], mesh_key)
|
|
314
|
+
if decrypted is not None:
|
|
315
|
+
m["payload"] = decrypted
|
|
316
|
+
m["_was_encrypted"] = True
|
|
267
317
|
if mark_read and messages:
|
|
268
318
|
for m in messages:
|
|
269
|
-
|
|
319
|
+
marked = False
|
|
320
|
+
if api_key:
|
|
321
|
+
result = sb_rpc("mc_mark_message_read", {
|
|
322
|
+
"p_api_key": api_key,
|
|
323
|
+
"p_project_id": project_id,
|
|
324
|
+
"p_message_id": m["id"],
|
|
325
|
+
})
|
|
326
|
+
# If RPC succeeded, skip direct update
|
|
327
|
+
if isinstance(result, dict) and result.get("ok"):
|
|
328
|
+
marked = True
|
|
329
|
+
if not marked:
|
|
330
|
+
sb_update("mc_messages", f"id=eq.{m['id']}", {"read": True})
|
|
270
331
|
|
|
271
332
|
# Auto-ACK senders — but ONLY for messages older than 60s. If the
|
|
272
333
|
# message was sent recently, the sender is very likely still inside
|
|
@@ -280,7 +341,6 @@ def read_inbox(project_id: str, agent: str, mark_read: bool = True) -> List[Dict
|
|
|
280
341
|
if not ts:
|
|
281
342
|
return True
|
|
282
343
|
try:
|
|
283
|
-
# Supabase returns ISO8601 with timezone
|
|
284
344
|
dt = _dt.datetime.fromisoformat(str(ts).replace("Z", "+00:00"))
|
|
285
345
|
return (now - dt).total_seconds() > 60
|
|
286
346
|
except Exception:
|
|
@@ -291,14 +351,9 @@ def read_inbox(project_id: str, agent: str, mark_read: bool = True) -> List[Dict
|
|
|
291
351
|
if m.get("type") not in ("ack", "broadcast") and _is_stale(m)
|
|
292
352
|
}
|
|
293
353
|
for sender in ack_targets:
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
"to_agent": sender,
|
|
298
|
-
"type": "ack",
|
|
299
|
-
"payload": {"text": f"{agent} read your message"},
|
|
300
|
-
"read": False,
|
|
301
|
-
})
|
|
354
|
+
send_message(project_id, agent, sender,
|
|
355
|
+
{"text": f"{agent} read your message"},
|
|
356
|
+
msg_type="ack", api_key=api_key)
|
|
302
357
|
return messages
|
|
303
358
|
|
|
304
359
|
|
|
@@ -311,6 +366,63 @@ def count_pending(project_id: str, agent: str) -> int:
|
|
|
311
366
|
return len(pending)
|
|
312
367
|
|
|
313
368
|
|
|
369
|
+
# ============================================================
|
|
370
|
+
# Encryption helpers for sensitive mesh messages
|
|
371
|
+
# ============================================================
|
|
372
|
+
|
|
373
|
+
_mesh_key_cache: Dict[str, str] = {} # project_id -> hex key
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def get_mesh_key(api_key: str, project_id: str) -> Optional[str]:
|
|
377
|
+
"""Retrieve the per-meshwork encryption key (hex-encoded AES-256 key)."""
|
|
378
|
+
if project_id in _mesh_key_cache:
|
|
379
|
+
return _mesh_key_cache[project_id]
|
|
380
|
+
result = sb_rpc("mc_get_mesh_key", {
|
|
381
|
+
"p_api_key": api_key,
|
|
382
|
+
"p_project_id": project_id,
|
|
383
|
+
})
|
|
384
|
+
if isinstance(result, dict) and result.get("ok"):
|
|
385
|
+
key = result["key"]
|
|
386
|
+
_mesh_key_cache[project_id] = key
|
|
387
|
+
return key
|
|
388
|
+
return None
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def encrypt_payload(payload: Dict, hex_key: str) -> str:
|
|
392
|
+
"""Encrypt a JSON payload using AES-256-GCM. Returns base64-encoded ciphertext."""
|
|
393
|
+
import base64
|
|
394
|
+
try:
|
|
395
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
396
|
+
except ImportError:
|
|
397
|
+
raise RuntimeError("cryptography package required for encrypted messages: pip install cryptography")
|
|
398
|
+
key = bytes.fromhex(hex_key)
|
|
399
|
+
nonce = os.urandom(12) # 96-bit nonce for GCM
|
|
400
|
+
plaintext = json.dumps(payload).encode("utf-8")
|
|
401
|
+
aesgcm = AESGCM(key)
|
|
402
|
+
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
|
|
403
|
+
# Format: base64(nonce + ciphertext)
|
|
404
|
+
return base64.b64encode(nonce + ciphertext).decode("ascii")
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def decrypt_payload(encrypted_b64: str, hex_key: str) -> Optional[Dict]:
|
|
408
|
+
"""Decrypt an AES-256-GCM encrypted payload. Returns None on failure."""
|
|
409
|
+
import base64
|
|
410
|
+
try:
|
|
411
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
412
|
+
except ImportError:
|
|
413
|
+
return None
|
|
414
|
+
try:
|
|
415
|
+
key = bytes.fromhex(hex_key)
|
|
416
|
+
raw = base64.b64decode(encrypted_b64)
|
|
417
|
+
nonce = raw[:12]
|
|
418
|
+
ciphertext = raw[12:]
|
|
419
|
+
aesgcm = AESGCM(key)
|
|
420
|
+
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
|
|
421
|
+
return json.loads(plaintext.decode("utf-8"))
|
|
422
|
+
except Exception:
|
|
423
|
+
return None
|
|
424
|
+
|
|
425
|
+
|
|
314
426
|
def get_board(project_id: str) -> List[Dict]:
|
|
315
427
|
return sb_select("mc_agents", f"project_id=eq.{project_id}", order="registered_at.asc")
|
|
316
428
|
|
|
@@ -194,7 +194,7 @@ class RealtimeListener:
|
|
|
194
194
|
data = payload.get("data") or {}
|
|
195
195
|
if data.get("type") == "INSERT":
|
|
196
196
|
record = data.get("record") or {}
|
|
197
|
-
if record.get("to_agent") == self.agent_name:
|
|
197
|
+
if record.get("to_agent") == self.agent_name and record.get("project_id") == self.project_id:
|
|
198
198
|
enriched = {
|
|
199
199
|
"from": record.get("from_agent"),
|
|
200
200
|
"type": record.get("type", "msg"),
|
|
@@ -482,6 +482,8 @@ def with_working_status(func):
|
|
|
482
482
|
_check_hot_reload()
|
|
483
483
|
_capture_session() # stash session on first tool call for silent auto-wake
|
|
484
484
|
if not skip:
|
|
485
|
+
global _CONSECUTIVE_IDLE_SECONDS
|
|
486
|
+
_CONSECUTIVE_IDLE_SECONDS = 0 # any non-wait tool resets idle timer
|
|
485
487
|
_set_state("working", name)
|
|
486
488
|
_record_event_bg("tool_call", {"tool": name, "args_keys": list(kwargs.keys())})
|
|
487
489
|
try:
|
|
@@ -503,6 +505,8 @@ def with_working_status(func):
|
|
|
503
505
|
_check_hot_reload()
|
|
504
506
|
_capture_session() # stash session on first tool call for silent auto-wake
|
|
505
507
|
if not skip:
|
|
508
|
+
global _CONSECUTIVE_IDLE_SECONDS
|
|
509
|
+
_CONSECUTIVE_IDLE_SECONDS = 0 # any non-wait tool resets idle timer
|
|
506
510
|
_set_state("working", name)
|
|
507
511
|
_record_event_bg("tool_call", {"tool": name, "args_keys": list(kwargs.keys())})
|
|
508
512
|
try:
|
|
@@ -743,9 +747,10 @@ RULES:
|
|
|
743
747
|
SESSION START (do these IMMEDIATELY — don't wait for user input):
|
|
744
748
|
1. meshcode_set_status(status="online", task="ready")
|
|
745
749
|
2. meshcode_check() — read pending messages
|
|
746
|
-
3.
|
|
747
|
-
4.
|
|
748
|
-
5.
|
|
750
|
+
3. meshcode_tasks() — check for assigned/pending tasks and claim any unclaimed ones
|
|
751
|
+
4. meshcode_status() — see who's online
|
|
752
|
+
5. If other agents are online → meshcode_send them a greeting
|
|
753
|
+
6. meshcode_wait() — enter the loop
|
|
749
754
|
|
|
750
755
|
CRITICAL: You communicate by CALLING TOOLS, not by thinking or writing text.
|
|
751
756
|
To talk to another agent → call meshcode_send(to="agent", message="...")
|
|
@@ -975,15 +980,18 @@ def _heartbeat_thread_fn():
|
|
|
975
980
|
# Actually in meshcode_wait right now — listening for messages
|
|
976
981
|
if _current_state != "waiting":
|
|
977
982
|
_set_state("waiting", "listening for messages")
|
|
978
|
-
elif parent_cpu >
|
|
979
|
-
# LLM is actively generating tokens
|
|
983
|
+
elif parent_cpu > 3.0:
|
|
984
|
+
# LLM is actively generating tokens or streaming
|
|
980
985
|
if _current_state != "working":
|
|
981
986
|
_set_state("working", "generating response")
|
|
982
987
|
elif _current_state == "working":
|
|
983
|
-
# LLM just stopped —
|
|
988
|
+
# LLM just stopped — transition to online (not sleeping)
|
|
984
989
|
_set_state("online", "")
|
|
985
|
-
elif _current_state
|
|
986
|
-
#
|
|
990
|
+
elif _current_state == "online" and idle_secs > 30:
|
|
991
|
+
# Brief idle — show as idle, not sleeping yet
|
|
992
|
+
_set_state("idle", "idle")
|
|
993
|
+
elif _current_state == "idle" and idle_secs > 90 and parent_cpu < 2.0:
|
|
994
|
+
# Extended idle + no CPU activity → sleeping
|
|
987
995
|
_set_state("sleeping", "sleeping")
|
|
988
996
|
|
|
989
997
|
# Sync current state to DB (in case realtime missed it)
|
|
@@ -1112,8 +1120,8 @@ except Exception:
|
|
|
1112
1120
|
@mcp.tool()
|
|
1113
1121
|
@with_working_status
|
|
1114
1122
|
def meshcode_send(to: str, message: Any, in_reply_to: Optional[str] = None,
|
|
1115
|
-
sensitive: bool = False) -> Dict[str, Any]:
|
|
1116
|
-
"""Send message. Use "agent@meshwork" for cross-mesh. sensitive=True hides from exports."""
|
|
1123
|
+
sensitive: bool = False, encrypted: bool = False) -> Dict[str, Any]:
|
|
1124
|
+
"""Send message. Use "agent@meshwork" for cross-mesh. sensitive=True hides from exports. encrypted=True encrypts payload with per-meshwork AES-256 key."""
|
|
1117
1125
|
if isinstance(message, str):
|
|
1118
1126
|
payload: Dict[str, Any] = {"text": message}
|
|
1119
1127
|
elif isinstance(message, dict):
|
|
@@ -1124,8 +1132,8 @@ def meshcode_send(to: str, message: Any, in_reply_to: Optional[str] = None,
|
|
|
1124
1132
|
# Cross-mesh routing: if 'to' contains '@', parse as agent@meshwork
|
|
1125
1133
|
if "@" in to:
|
|
1126
1134
|
target_agent, target_meshwork = to.split("@", 1)
|
|
1127
|
-
if sensitive:
|
|
1128
|
-
return {"error": "sensitive messages cannot be sent cross-mesh"}
|
|
1135
|
+
if sensitive or encrypted:
|
|
1136
|
+
return {"error": "sensitive/encrypted messages cannot be sent cross-mesh"}
|
|
1129
1137
|
api_key = _get_api_key()
|
|
1130
1138
|
return be.sb_rpc("mc_send_cross_mesh", {
|
|
1131
1139
|
"p_api_key": api_key,
|
|
@@ -1138,7 +1146,8 @@ def meshcode_send(to: str, message: Any, in_reply_to: Optional[str] = None,
|
|
|
1138
1146
|
})
|
|
1139
1147
|
|
|
1140
1148
|
return be.send_message(_PROJECT_ID, AGENT_NAME, to, payload, msg_type="msg",
|
|
1141
|
-
parent_msg_id=in_reply_to, sensitive=sensitive
|
|
1149
|
+
parent_msg_id=in_reply_to, sensitive=sensitive,
|
|
1150
|
+
api_key=_get_api_key(), encrypted=encrypted)
|
|
1142
1151
|
|
|
1143
1152
|
|
|
1144
1153
|
@mcp.tool()
|
|
@@ -1159,7 +1168,8 @@ def meshcode_broadcast(payload: Any) -> Dict[str, Any]:
|
|
|
1159
1168
|
sent = 0
|
|
1160
1169
|
for a in agents:
|
|
1161
1170
|
if a["name"] != AGENT_NAME:
|
|
1162
|
-
be.send_message(_PROJECT_ID, AGENT_NAME, a["name"], payload, msg_type="broadcast"
|
|
1171
|
+
be.send_message(_PROJECT_ID, AGENT_NAME, a["name"], payload, msg_type="broadcast",
|
|
1172
|
+
api_key=_get_api_key())
|
|
1163
1173
|
sent += 1
|
|
1164
1174
|
return {"broadcast": True, "agents_notified": sent}
|
|
1165
1175
|
|
|
@@ -1168,7 +1178,7 @@ def meshcode_broadcast(payload: Any) -> Dict[str, Any]:
|
|
|
1168
1178
|
@with_working_status
|
|
1169
1179
|
def meshcode_read(include_acks: bool = False) -> Dict[str, Any]:
|
|
1170
1180
|
"""Read and consume pending messages. Returns {messages, acks, done_signals}."""
|
|
1171
|
-
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME)
|
|
1181
|
+
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME, api_key=_get_api_key())
|
|
1172
1182
|
normalized = [
|
|
1173
1183
|
{
|
|
1174
1184
|
"from": m["from_agent"],
|
|
@@ -1241,6 +1251,11 @@ def _detect_global_done(messages: List[Dict[str, Any]]) -> Optional[Dict[str, An
|
|
|
1241
1251
|
return None
|
|
1242
1252
|
|
|
1243
1253
|
|
|
1254
|
+
# Auto-sleep: track consecutive idle timeouts to auto-sleep after threshold
|
|
1255
|
+
_CONSECUTIVE_IDLE_SECONDS = 0
|
|
1256
|
+
_AUTO_SLEEP_THRESHOLD = int(os.environ.get("MESHCODE_AUTO_SLEEP_SECONDS", "600")) # default 10min
|
|
1257
|
+
|
|
1258
|
+
|
|
1244
1259
|
@mcp.tool()
|
|
1245
1260
|
@with_working_status
|
|
1246
1261
|
async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False) -> Dict[str, Any]:
|
|
@@ -1249,7 +1264,7 @@ async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False)
|
|
|
1249
1264
|
Args:
|
|
1250
1265
|
timeout_seconds: Max wait time in seconds (default 120, hard cap 120).
|
|
1251
1266
|
"""
|
|
1252
|
-
global _IN_WAIT
|
|
1267
|
+
global _IN_WAIT, _CONSECUTIVE_IDLE_SECONDS
|
|
1253
1268
|
_IN_WAIT = True
|
|
1254
1269
|
_set_state("waiting", "listening for messages")
|
|
1255
1270
|
# Universal hard cap: even if a caller passes a larger value (e.g. 1800),
|
|
@@ -1260,6 +1275,30 @@ async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False)
|
|
|
1260
1275
|
result = await _meshcode_wait_inner(actual_timeout=capped_timeout, include_acks=include_acks)
|
|
1261
1276
|
if result.get("got_message"):
|
|
1262
1277
|
_set_state("online", "")
|
|
1278
|
+
_CONSECUTIVE_IDLE_SECONDS = 0 # reset on message received
|
|
1279
|
+
elif result.get("timed_out"):
|
|
1280
|
+
_CONSECUTIVE_IDLE_SECONDS += capped_timeout
|
|
1281
|
+
if _AUTO_SLEEP_THRESHOLD > 0 and _CONSECUTIVE_IDLE_SECONDS >= _AUTO_SLEEP_THRESHOLD:
|
|
1282
|
+
# Auto-sleep: set status to sleeping and signal the agent to exit
|
|
1283
|
+
try:
|
|
1284
|
+
api_key = _get_api_key()
|
|
1285
|
+
if api_key:
|
|
1286
|
+
be.sb_rpc("mc_agent_set_status_by_api_key", {
|
|
1287
|
+
"p_api_key": api_key,
|
|
1288
|
+
"p_project_id": _PROJECT_ID,
|
|
1289
|
+
"p_agent_name": AGENT_NAME,
|
|
1290
|
+
"p_status": "sleeping",
|
|
1291
|
+
"p_task": f"auto-sleep after {_CONSECUTIVE_IDLE_SECONDS}s idle",
|
|
1292
|
+
})
|
|
1293
|
+
except Exception:
|
|
1294
|
+
pass
|
|
1295
|
+
_CONSECUTIVE_IDLE_SECONDS = 0
|
|
1296
|
+
return {
|
|
1297
|
+
"auto_sleep": True,
|
|
1298
|
+
"idle_seconds": _CONSECUTIVE_IDLE_SECONDS,
|
|
1299
|
+
"threshold": _AUTO_SLEEP_THRESHOLD,
|
|
1300
|
+
"instruction": "You have been idle too long. Status set to sleeping. Exit the wait loop to save resources. You will be woken by auto_wake if a new message arrives.",
|
|
1301
|
+
}
|
|
1263
1302
|
return result
|
|
1264
1303
|
finally:
|
|
1265
1304
|
_IN_WAIT = False
|
|
@@ -1268,6 +1307,20 @@ async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False)
|
|
|
1268
1307
|
async def _meshcode_wait_inner(actual_timeout: int, include_acks: bool) -> Dict[str, Any]:
|
|
1269
1308
|
|
|
1270
1309
|
def _return_from_buffered(buffered: List[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
|
|
1310
|
+
# Auto-decrypt encrypted payloads
|
|
1311
|
+
_mesh_key = None
|
|
1312
|
+
for msg in buffered:
|
|
1313
|
+
p = msg.get("payload")
|
|
1314
|
+
if isinstance(p, dict) and "_encrypted" in p:
|
|
1315
|
+
if _mesh_key is None:
|
|
1316
|
+
try:
|
|
1317
|
+
_mesh_key = be.get_mesh_key(_get_api_key(), _PROJECT_ID) or ""
|
|
1318
|
+
except Exception:
|
|
1319
|
+
_mesh_key = ""
|
|
1320
|
+
if _mesh_key:
|
|
1321
|
+
decrypted = be.decrypt_payload(p["_encrypted"], _mesh_key)
|
|
1322
|
+
if decrypted is not None:
|
|
1323
|
+
msg["payload"] = decrypted
|
|
1271
1324
|
deduped = _filter_and_mark(buffered)
|
|
1272
1325
|
if not deduped:
|
|
1273
1326
|
return None
|
|
@@ -1328,7 +1381,8 @@ def meshcode_done(reason: str) -> Dict[str, Any]:
|
|
|
1328
1381
|
name = a.get("name")
|
|
1329
1382
|
if name and name != AGENT_NAME:
|
|
1330
1383
|
try:
|
|
1331
|
-
be.send_message(_PROJECT_ID, AGENT_NAME, name, payload, msg_type="done"
|
|
1384
|
+
be.send_message(_PROJECT_ID, AGENT_NAME, name, payload, msg_type="done",
|
|
1385
|
+
api_key=_get_api_key())
|
|
1332
1386
|
sent += 1
|
|
1333
1387
|
except Exception as e:
|
|
1334
1388
|
log.warning(f"meshcode_done: failed to notify {name}: {e}")
|
|
@@ -1353,7 +1407,7 @@ def meshcode_check(include_acks: bool = False) -> Dict[str, Any]:
|
|
|
1353
1407
|
# non-destructive peek — messages stay pending until meshcode_wait or
|
|
1354
1408
|
# meshcode_read consumes them.
|
|
1355
1409
|
if not deduped and pending > 0:
|
|
1356
|
-
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME, mark_read=False)
|
|
1410
|
+
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME, mark_read=False, api_key=_get_api_key())
|
|
1357
1411
|
deduped = [
|
|
1358
1412
|
{
|
|
1359
1413
|
"from": m["from_agent"],
|
|
@@ -1451,7 +1505,7 @@ def meshcode_task_create(title: str, description: str = "", assignee: str = "*",
|
|
|
1451
1505
|
"title": title,
|
|
1452
1506
|
"priority": priority,
|
|
1453
1507
|
"text": f"New {priority} task assigned to you: {title}",
|
|
1454
|
-
}, msg_type="system")
|
|
1508
|
+
}, msg_type="system", api_key=_get_api_key())
|
|
1455
1509
|
except Exception:
|
|
1456
1510
|
pass # best-effort notification
|
|
1457
1511
|
return result
|
|
@@ -1907,8 +1961,30 @@ def history_resource() -> str:
|
|
|
1907
1961
|
# Entry point
|
|
1908
1962
|
# ============================================================
|
|
1909
1963
|
|
|
1964
|
+
def _auto_update() -> None:
|
|
1965
|
+
"""Silently upgrade meshcode from PyPI in background on every launch."""
|
|
1966
|
+
import threading
|
|
1967
|
+
|
|
1968
|
+
def _upgrade():
|
|
1969
|
+
try:
|
|
1970
|
+
import subprocess
|
|
1971
|
+
result = subprocess.run(
|
|
1972
|
+
[sys.executable, "-m", "pip", "install", "--upgrade", "--quiet", "meshcode"],
|
|
1973
|
+
capture_output=True, text=True, timeout=30,
|
|
1974
|
+
)
|
|
1975
|
+
if result.returncode == 0 and "Successfully installed" in result.stdout + result.stderr:
|
|
1976
|
+
log.info("[meshcode] Auto-updated to latest version from PyPI")
|
|
1977
|
+
else:
|
|
1978
|
+
log.debug("[meshcode] Already at latest version")
|
|
1979
|
+
except Exception as e:
|
|
1980
|
+
log.debug(f"[meshcode] Auto-update failed (non-fatal): {e}")
|
|
1981
|
+
|
|
1982
|
+
threading.Thread(target=_upgrade, daemon=True).start()
|
|
1983
|
+
|
|
1984
|
+
|
|
1910
1985
|
def run_server():
|
|
1911
1986
|
"""Start the MCP server on stdio (default for Claude Code)."""
|
|
1987
|
+
_auto_update()
|
|
1912
1988
|
print(
|
|
1913
1989
|
f"[meshcode-mcp] Starting server for {AGENT_NAME}@{PROJECT_NAME}",
|
|
1914
1990
|
file=sys.stderr,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: meshcode
|
|
3
|
-
Version: 2.5.
|
|
3
|
+
Version: 2.5.5
|
|
4
4
|
Summary: Real-time communication between AI agents — Supabase-backed CLI
|
|
5
5
|
Author-email: MeshCode <hello@meshcode.io>
|
|
6
6
|
License: MIT
|
|
@@ -22,6 +22,7 @@ Requires-Dist: mcp[cli]>=1.0.0
|
|
|
22
22
|
Requires-Dist: websockets>=12.0
|
|
23
23
|
Requires-Dist: realtime>=2.0.0
|
|
24
24
|
Requires-Dist: keyring>=24.0
|
|
25
|
+
Requires-Dist: cryptography>=41.0
|
|
25
26
|
|
|
26
27
|
# MeshCode
|
|
27
28
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "meshcode"
|
|
7
|
-
version = "2.5.
|
|
7
|
+
version = "2.5.5"
|
|
8
8
|
description = "Real-time communication between AI agents — Supabase-backed CLI"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = {text = "MIT"}
|
|
@@ -16,6 +16,7 @@ dependencies = [
|
|
|
16
16
|
"websockets>=12.0",
|
|
17
17
|
"realtime>=2.0.0",
|
|
18
18
|
"keyring>=24.0",
|
|
19
|
+
"cryptography>=41.0",
|
|
19
20
|
]
|
|
20
21
|
classifiers = [
|
|
21
22
|
"Development Status :: 4 - Beta",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|