|
|
@@ -11,6 +11,7 @@ including both outbound messaging and realtime WebSocket connections.
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
+import json
|
|
|
import random
|
|
|
|
|
|
# Put the vendored dependencies for THIS platform on sys.path before importing
|
|
|
@@ -3614,10 +3615,10 @@ async def _handle_chatto_send_dm(args: dict) -> dict:
|
|
|
message = args.get("message") or ""
|
|
|
|
|
|
if not message:
|
|
|
- return {"success": False, "error": "message is required"}
|
|
|
+ return json.dumps({"success": False, "error": "message is required"})
|
|
|
|
|
|
if not user_login and not user_id:
|
|
|
- return {"success": False, "error": "Exactly one of user_login or user_id is required"}
|
|
|
+ return json.dumps({"success": False, "error": "Exactly one of user_login or user_id is required"})
|
|
|
|
|
|
# --- Read config: explicit args first, then fall back to resolved config ---
|
|
|
explicit_base_url = args.get("base_url") or ""
|
|
|
@@ -3637,12 +3638,12 @@ async def _handle_chatto_send_dm(args: dict) -> dict:
|
|
|
else:
|
|
|
base_url = "https://chat.chatto.run"
|
|
|
if not token:
|
|
|
- return {"success": False, "error": "Chatto token not configured"}
|
|
|
+ return json.dumps({"success": False, "error": "Chatto token not configured"})
|
|
|
|
|
|
try:
|
|
|
client = ChattoClient(base_url=base_url, token=token)
|
|
|
except Exception as exc:
|
|
|
- return {"success": False, "error": f"Failed to create Chatto client: {exc}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Failed to create Chatto client: {exc}"})
|
|
|
|
|
|
try:
|
|
|
# 1. Resolve the target user
|
|
|
@@ -3653,15 +3654,15 @@ async def _handle_chatto_send_dm(args: dict) -> dict:
|
|
|
|
|
|
member = member_data.get("member")
|
|
|
if not member:
|
|
|
- return {"success": False, "error": f"User not found (login={user_login or user_id})"}
|
|
|
+ return json.dumps({"success": False, "error": f"User not found (login={user_login or user_id})"})
|
|
|
|
|
|
target_user = member.get("user")
|
|
|
if not target_user:
|
|
|
- return {"success": False, "error": "User record missing in member data"}
|
|
|
+ return json.dumps({"success": False, "error": "User record missing in member data"})
|
|
|
|
|
|
target_user_id = target_user.get("id")
|
|
|
if not target_user_id:
|
|
|
- return {"success": False, "error": "User has no ID"}
|
|
|
+ return json.dumps({"success": False, "error": "User has no ID"})
|
|
|
|
|
|
target_login = target_user.get("login", user_login)
|
|
|
|
|
|
@@ -3696,19 +3697,19 @@ async def _handle_chatto_send_dm(args: dict) -> dict:
|
|
|
msg = await client.post_message(dm_room_id, chunk)
|
|
|
message_ids.append(msg.id)
|
|
|
|
|
|
- return {
|
|
|
+ return json.dumps({
|
|
|
"success": True,
|
|
|
"dm_room_id": dm_room_id,
|
|
|
"recipient": target_login,
|
|
|
"recipient_user_id": target_user_id,
|
|
|
"message_ids": message_ids,
|
|
|
"chunks_sent": len(message_ids),
|
|
|
- }
|
|
|
+ })
|
|
|
|
|
|
except ChattoAuthError as exc:
|
|
|
- return {"success": False, "error": f"Authentication error: {exc}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
|
|
|
except ChattoError as exc:
|
|
|
- return {"success": False, "error": f"Chatto API error: {exc}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
|
|
|
finally:
|
|
|
try:
|
|
|
await client.close()
|
|
|
@@ -3723,10 +3724,10 @@ async def _handle_chatto_send_room(args: dict) -> dict:
|
|
|
message = args.get("message") or ""
|
|
|
|
|
|
if not message:
|
|
|
- return {"success": False, "error": "message is required"}
|
|
|
+ return json.dumps({"success": False, "error": "message is required"})
|
|
|
|
|
|
if not room_id and not room_name:
|
|
|
- return {"success": False, "error": "Exactly one of room_id or room_name is required"}
|
|
|
+ return json.dumps({"success": False, "error": "Exactly one of room_id or room_name is required"})
|
|
|
|
|
|
# --- Read config: explicit args first, then fall back to resolved config ---
|
|
|
explicit_token = args.get("token") or ""
|
|
|
@@ -3742,12 +3743,12 @@ async def _handle_chatto_send_room(args: dict) -> dict:
|
|
|
if not base_url:
|
|
|
base_url = _resolved_chatto_config.base_url.value or "https://chat.chatto.run" if _resolved_chatto_config is not None else "https://chat.chatto.run"
|
|
|
if not token:
|
|
|
- return {"success": False, "error": "Chatto token not configured"}
|
|
|
+ return json.dumps({"success": False, "error": "Chatto token not configured"})
|
|
|
|
|
|
try:
|
|
|
client = ChattoClient(base_url=base_url, token=token)
|
|
|
except Exception as exc:
|
|
|
- return {"success": False, "error": f"Failed to create Chatto client: {exc}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Failed to create Chatto client: {exc}"})
|
|
|
|
|
|
try:
|
|
|
# 1. Resolve the target room
|
|
|
@@ -3761,14 +3762,14 @@ async def _handle_chatto_send_room(args: dict) -> dict:
|
|
|
found = rws
|
|
|
break
|
|
|
if not found or not found.room:
|
|
|
- return {"success": False, "error": f"Room not found: {room_name}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Room not found: {room_name}"})
|
|
|
room_id = found.room.id
|
|
|
room_label = found.room.name
|
|
|
else:
|
|
|
# Verify room exists
|
|
|
rws = await client.get_room(room_id)
|
|
|
if not rws or not rws.room:
|
|
|
- return {"success": False, "error": f"Room not found: {room_id}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Room not found: {room_id}"})
|
|
|
room_label = rws.room.name
|
|
|
|
|
|
# 2. Post the message
|
|
|
@@ -3781,18 +3782,18 @@ async def _handle_chatto_send_room(args: dict) -> dict:
|
|
|
msg = await client.post_message(room_id, chunk)
|
|
|
message_ids.append(msg.id)
|
|
|
|
|
|
- return {
|
|
|
+ return json.dumps({
|
|
|
"success": True,
|
|
|
"room_id": room_id,
|
|
|
"room_name": room_label,
|
|
|
"message_ids": message_ids,
|
|
|
"chunks_sent": len(message_ids),
|
|
|
- }
|
|
|
+ })
|
|
|
|
|
|
except ChattoAuthError as exc:
|
|
|
- return {"success": False, "error": f"Authentication error: {exc}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Authentication error: {exc}"})
|
|
|
except ChattoError as exc:
|
|
|
- return {"success": False, "error": f"Chatto API error: {exc}"}
|
|
|
+ return json.dumps({"success": False, "error": f"Chatto API error: {exc}"})
|
|
|
finally:
|
|
|
try:
|
|
|
await client.close()
|
|
|
@@ -3810,6 +3811,7 @@ def register(ctx) -> None:
|
|
|
toolset="chatto-platform",
|
|
|
schema=CHATTO_SEND_DM_SCHEMA,
|
|
|
handler=_handle_chatto_send_dm,
|
|
|
+ is_async=True,
|
|
|
description=CHATTO_SEND_DM_SCHEMA["description"],
|
|
|
)
|
|
|
ctx.register_tool(
|
|
|
@@ -3817,6 +3819,7 @@ def register(ctx) -> None:
|
|
|
toolset="chatto-platform",
|
|
|
schema=CHATTO_SEND_ROOM_SCHEMA,
|
|
|
handler=_handle_chatto_send_room,
|
|
|
+ is_async=True,
|
|
|
description=CHATTO_SEND_ROOM_SCHEMA["description"],
|
|
|
)
|
|
|
|