meshcode 2.5.3__tar.gz → 2.5.4__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.4}/PKG-INFO +2 -1
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/__init__.py +1 -1
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/backend.py +124 -12
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/realtime.py +1 -1
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/server.py +69 -16
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode.egg-info/PKG-INFO +2 -1
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode.egg-info/requires.txt +1 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/pyproject.toml +2 -1
- {meshcode-2.5.3 → meshcode-2.5.4}/README.md +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/cli.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/comms_v4.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/invites.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/launcher.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/launcher_install.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/__init__.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/__main__.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/test_backend.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/test_realtime.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/meshcode_mcp/test_server_wrapper.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/preferences.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/protocol_v2.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/run_agent.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/secrets.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/self_update.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode/setup_clients.py +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode.egg-info/SOURCES.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode.egg-info/dependency_links.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode.egg-info/entry_points.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/meshcode.egg-info/top_level.txt +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/setup.cfg +0 -0
- {meshcode-2.5.3 → meshcode-2.5.4}/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.4
|
|
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.4"
|
|
@@ -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:
|
|
@@ -975,15 +979,18 @@ def _heartbeat_thread_fn():
|
|
|
975
979
|
# Actually in meshcode_wait right now — listening for messages
|
|
976
980
|
if _current_state != "waiting":
|
|
977
981
|
_set_state("waiting", "listening for messages")
|
|
978
|
-
elif parent_cpu >
|
|
979
|
-
# LLM is actively generating tokens
|
|
982
|
+
elif parent_cpu > 3.0:
|
|
983
|
+
# LLM is actively generating tokens or streaming
|
|
980
984
|
if _current_state != "working":
|
|
981
985
|
_set_state("working", "generating response")
|
|
982
986
|
elif _current_state == "working":
|
|
983
|
-
# LLM just stopped —
|
|
987
|
+
# LLM just stopped — transition to online (not sleeping)
|
|
984
988
|
_set_state("online", "")
|
|
985
|
-
elif _current_state
|
|
986
|
-
#
|
|
989
|
+
elif _current_state == "online" and idle_secs > 30:
|
|
990
|
+
# Brief idle — show as idle, not sleeping yet
|
|
991
|
+
_set_state("idle", "idle")
|
|
992
|
+
elif _current_state == "idle" and idle_secs > 90 and parent_cpu < 2.0:
|
|
993
|
+
# Extended idle + no CPU activity → sleeping
|
|
987
994
|
_set_state("sleeping", "sleeping")
|
|
988
995
|
|
|
989
996
|
# Sync current state to DB (in case realtime missed it)
|
|
@@ -1112,8 +1119,8 @@ except Exception:
|
|
|
1112
1119
|
@mcp.tool()
|
|
1113
1120
|
@with_working_status
|
|
1114
1121
|
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."""
|
|
1122
|
+
sensitive: bool = False, encrypted: bool = False) -> Dict[str, Any]:
|
|
1123
|
+
"""Send message. Use "agent@meshwork" for cross-mesh. sensitive=True hides from exports. encrypted=True encrypts payload with per-meshwork AES-256 key."""
|
|
1117
1124
|
if isinstance(message, str):
|
|
1118
1125
|
payload: Dict[str, Any] = {"text": message}
|
|
1119
1126
|
elif isinstance(message, dict):
|
|
@@ -1124,8 +1131,8 @@ def meshcode_send(to: str, message: Any, in_reply_to: Optional[str] = None,
|
|
|
1124
1131
|
# Cross-mesh routing: if 'to' contains '@', parse as agent@meshwork
|
|
1125
1132
|
if "@" in to:
|
|
1126
1133
|
target_agent, target_meshwork = to.split("@", 1)
|
|
1127
|
-
if sensitive:
|
|
1128
|
-
return {"error": "sensitive messages cannot be sent cross-mesh"}
|
|
1134
|
+
if sensitive or encrypted:
|
|
1135
|
+
return {"error": "sensitive/encrypted messages cannot be sent cross-mesh"}
|
|
1129
1136
|
api_key = _get_api_key()
|
|
1130
1137
|
return be.sb_rpc("mc_send_cross_mesh", {
|
|
1131
1138
|
"p_api_key": api_key,
|
|
@@ -1138,7 +1145,8 @@ def meshcode_send(to: str, message: Any, in_reply_to: Optional[str] = None,
|
|
|
1138
1145
|
})
|
|
1139
1146
|
|
|
1140
1147
|
return be.send_message(_PROJECT_ID, AGENT_NAME, to, payload, msg_type="msg",
|
|
1141
|
-
parent_msg_id=in_reply_to, sensitive=sensitive
|
|
1148
|
+
parent_msg_id=in_reply_to, sensitive=sensitive,
|
|
1149
|
+
api_key=_get_api_key(), encrypted=encrypted)
|
|
1142
1150
|
|
|
1143
1151
|
|
|
1144
1152
|
@mcp.tool()
|
|
@@ -1159,7 +1167,8 @@ def meshcode_broadcast(payload: Any) -> Dict[str, Any]:
|
|
|
1159
1167
|
sent = 0
|
|
1160
1168
|
for a in agents:
|
|
1161
1169
|
if a["name"] != AGENT_NAME:
|
|
1162
|
-
be.send_message(_PROJECT_ID, AGENT_NAME, a["name"], payload, msg_type="broadcast"
|
|
1170
|
+
be.send_message(_PROJECT_ID, AGENT_NAME, a["name"], payload, msg_type="broadcast",
|
|
1171
|
+
api_key=_get_api_key())
|
|
1163
1172
|
sent += 1
|
|
1164
1173
|
return {"broadcast": True, "agents_notified": sent}
|
|
1165
1174
|
|
|
@@ -1168,7 +1177,7 @@ def meshcode_broadcast(payload: Any) -> Dict[str, Any]:
|
|
|
1168
1177
|
@with_working_status
|
|
1169
1178
|
def meshcode_read(include_acks: bool = False) -> Dict[str, Any]:
|
|
1170
1179
|
"""Read and consume pending messages. Returns {messages, acks, done_signals}."""
|
|
1171
|
-
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME)
|
|
1180
|
+
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME, api_key=_get_api_key())
|
|
1172
1181
|
normalized = [
|
|
1173
1182
|
{
|
|
1174
1183
|
"from": m["from_agent"],
|
|
@@ -1241,6 +1250,11 @@ def _detect_global_done(messages: List[Dict[str, Any]]) -> Optional[Dict[str, An
|
|
|
1241
1250
|
return None
|
|
1242
1251
|
|
|
1243
1252
|
|
|
1253
|
+
# Auto-sleep: track consecutive idle timeouts to auto-sleep after threshold
|
|
1254
|
+
_CONSECUTIVE_IDLE_SECONDS = 0
|
|
1255
|
+
_AUTO_SLEEP_THRESHOLD = int(os.environ.get("MESHCODE_AUTO_SLEEP_SECONDS", "600")) # default 10min
|
|
1256
|
+
|
|
1257
|
+
|
|
1244
1258
|
@mcp.tool()
|
|
1245
1259
|
@with_working_status
|
|
1246
1260
|
async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False) -> Dict[str, Any]:
|
|
@@ -1249,7 +1263,7 @@ async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False)
|
|
|
1249
1263
|
Args:
|
|
1250
1264
|
timeout_seconds: Max wait time in seconds (default 120, hard cap 120).
|
|
1251
1265
|
"""
|
|
1252
|
-
global _IN_WAIT
|
|
1266
|
+
global _IN_WAIT, _CONSECUTIVE_IDLE_SECONDS
|
|
1253
1267
|
_IN_WAIT = True
|
|
1254
1268
|
_set_state("waiting", "listening for messages")
|
|
1255
1269
|
# Universal hard cap: even if a caller passes a larger value (e.g. 1800),
|
|
@@ -1260,6 +1274,30 @@ async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False)
|
|
|
1260
1274
|
result = await _meshcode_wait_inner(actual_timeout=capped_timeout, include_acks=include_acks)
|
|
1261
1275
|
if result.get("got_message"):
|
|
1262
1276
|
_set_state("online", "")
|
|
1277
|
+
_CONSECUTIVE_IDLE_SECONDS = 0 # reset on message received
|
|
1278
|
+
elif result.get("timed_out"):
|
|
1279
|
+
_CONSECUTIVE_IDLE_SECONDS += capped_timeout
|
|
1280
|
+
if _AUTO_SLEEP_THRESHOLD > 0 and _CONSECUTIVE_IDLE_SECONDS >= _AUTO_SLEEP_THRESHOLD:
|
|
1281
|
+
# Auto-sleep: set status to sleeping and signal the agent to exit
|
|
1282
|
+
try:
|
|
1283
|
+
api_key = _get_api_key()
|
|
1284
|
+
if api_key:
|
|
1285
|
+
be.sb_rpc("mc_agent_set_status_by_api_key", {
|
|
1286
|
+
"p_api_key": api_key,
|
|
1287
|
+
"p_project_id": _PROJECT_ID,
|
|
1288
|
+
"p_agent_name": AGENT_NAME,
|
|
1289
|
+
"p_status": "sleeping",
|
|
1290
|
+
"p_task": f"auto-sleep after {_CONSECUTIVE_IDLE_SECONDS}s idle",
|
|
1291
|
+
})
|
|
1292
|
+
except Exception:
|
|
1293
|
+
pass
|
|
1294
|
+
_CONSECUTIVE_IDLE_SECONDS = 0
|
|
1295
|
+
return {
|
|
1296
|
+
"auto_sleep": True,
|
|
1297
|
+
"idle_seconds": _CONSECUTIVE_IDLE_SECONDS,
|
|
1298
|
+
"threshold": _AUTO_SLEEP_THRESHOLD,
|
|
1299
|
+
"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.",
|
|
1300
|
+
}
|
|
1263
1301
|
return result
|
|
1264
1302
|
finally:
|
|
1265
1303
|
_IN_WAIT = False
|
|
@@ -1268,6 +1306,20 @@ async def meshcode_wait(timeout_seconds: int = 120, include_acks: bool = False)
|
|
|
1268
1306
|
async def _meshcode_wait_inner(actual_timeout: int, include_acks: bool) -> Dict[str, Any]:
|
|
1269
1307
|
|
|
1270
1308
|
def _return_from_buffered(buffered: List[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
|
|
1309
|
+
# Auto-decrypt encrypted payloads
|
|
1310
|
+
_mesh_key = None
|
|
1311
|
+
for msg in buffered:
|
|
1312
|
+
p = msg.get("payload")
|
|
1313
|
+
if isinstance(p, dict) and "_encrypted" in p:
|
|
1314
|
+
if _mesh_key is None:
|
|
1315
|
+
try:
|
|
1316
|
+
_mesh_key = be.get_mesh_key(_get_api_key(), _PROJECT_ID) or ""
|
|
1317
|
+
except Exception:
|
|
1318
|
+
_mesh_key = ""
|
|
1319
|
+
if _mesh_key:
|
|
1320
|
+
decrypted = be.decrypt_payload(p["_encrypted"], _mesh_key)
|
|
1321
|
+
if decrypted is not None:
|
|
1322
|
+
msg["payload"] = decrypted
|
|
1271
1323
|
deduped = _filter_and_mark(buffered)
|
|
1272
1324
|
if not deduped:
|
|
1273
1325
|
return None
|
|
@@ -1328,7 +1380,8 @@ def meshcode_done(reason: str) -> Dict[str, Any]:
|
|
|
1328
1380
|
name = a.get("name")
|
|
1329
1381
|
if name and name != AGENT_NAME:
|
|
1330
1382
|
try:
|
|
1331
|
-
be.send_message(_PROJECT_ID, AGENT_NAME, name, payload, msg_type="done"
|
|
1383
|
+
be.send_message(_PROJECT_ID, AGENT_NAME, name, payload, msg_type="done",
|
|
1384
|
+
api_key=_get_api_key())
|
|
1332
1385
|
sent += 1
|
|
1333
1386
|
except Exception as e:
|
|
1334
1387
|
log.warning(f"meshcode_done: failed to notify {name}: {e}")
|
|
@@ -1353,7 +1406,7 @@ def meshcode_check(include_acks: bool = False) -> Dict[str, Any]:
|
|
|
1353
1406
|
# non-destructive peek — messages stay pending until meshcode_wait or
|
|
1354
1407
|
# meshcode_read consumes them.
|
|
1355
1408
|
if not deduped and pending > 0:
|
|
1356
|
-
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME, mark_read=False)
|
|
1409
|
+
raw = be.read_inbox(_PROJECT_ID, AGENT_NAME, mark_read=False, api_key=_get_api_key())
|
|
1357
1410
|
deduped = [
|
|
1358
1411
|
{
|
|
1359
1412
|
"from": m["from_agent"],
|
|
@@ -1451,7 +1504,7 @@ def meshcode_task_create(title: str, description: str = "", assignee: str = "*",
|
|
|
1451
1504
|
"title": title,
|
|
1452
1505
|
"priority": priority,
|
|
1453
1506
|
"text": f"New {priority} task assigned to you: {title}",
|
|
1454
|
-
}, msg_type="system")
|
|
1507
|
+
}, msg_type="system", api_key=_get_api_key())
|
|
1455
1508
|
except Exception:
|
|
1456
1509
|
pass # best-effort notification
|
|
1457
1510
|
return result
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: meshcode
|
|
3
|
-
Version: 2.5.
|
|
3
|
+
Version: 2.5.4
|
|
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.4"
|
|
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
|