Feature: Support remote chat on Android hosts

The chat tools were Windows-only because the Android client had no
COMMAND_CHAT handler: the server asked for a chat sub-connection and the
command was silently dropped, leaving chat_open to time out. This adds
the full path on the client and the matching server gating.

Client: COMMAND_CHAT on the main connection spawns a chat thread that
opens an authenticated sub-connection, sends TOKEN_CHAT_START and waits
for the server to bind it. Incoming GBK lines are decoded and dispatched
to ChatSession, which drives an in-app chat activity; when accessibility
is available the activity is pulled to the front, otherwise a tappable
notification is posted. Outgoing text is strictly GBK encoded so emoji
and unmappable characters are rejected instead of becoming '?'. When the
server ends the session the activity closes itself, matching the Windows
client's DestroyWindow, and a new session starts from an empty log.

Server: a new CLIENT_CAP_REMOTE_CHAT bit lets the server tell new APKs
from old ones. ResolveHost now accepts Windows and Android hosts and
rejects older APKs up front with -32005 rather than waiting for the
command to expire. Tool platform masks, instructions, the docs matrix
and mcp/tools.json follow the same change.

Verified end to end on the Android 15 emulator: chat_open pops the
activity, Chinese text from the server renders, a device reply reaches
chat_wait, and ending the session from the master closes the activity
instead of leaving a dead window. The committed ghost.apk is stale and
must be rebuilt before release.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 21:58:44 +01:00
1 parent f33a205fd9
commit 915c587964
14 files changed
+695 -29

No files matched your search

+22 -11
View File
@@ -85,7 +85,7 @@ struct HostTool {
};
bool ResolveHost(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent,
HostTool& ht, bool requireWindows_, std::string& errJson) {
HostTool& ht, bool requireChatSupported_, std::string& errJson) {
uint64_t dev = 0;
std::string err;
if (!ParseHostIdArg(args, dev, err)) {
@@ -97,10 +97,21 @@ bool ResolveHost(const Json::Value& id, const Json::Value& args, CMy2015RemoteDl
errJson = BuildError(id, kErrOffline, "Host not found or offline: " + std::to_string(dev));
return false;
}
if (requireWindows_ && !IsWindowsClient(ctx)) {
errJson = BuildError(id, kErrUnsupported,
"Remote chat is only supported on Windows hosts");
return false;
// Windows 客户端一直有 COMMAND_CHAT 分支;Android 从支持 CLIENT_CAP_REMOTE_CHAT 的版本
// 才开始弹应用内聊天窗口,老 APK 收到会静默丢弃,提前拒绝比等 15s 超时友好。
if (requireChatSupported_) {
const CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
if (clientType == "LNX" || clientType == "MAC") {
errJson = BuildError(id, kErrUnsupported,
"Remote chat is only supported on Windows and Android hosts");
return false;
}
if (clientType == "APK" && !ctx->SupportsChat()) {
errJson = BuildError(id, kErrUnsupported,
"Android client is too old for remote chat: "
"the APK has no COMMAND_CHAT handler, upgrade the client first");
return false;
}
}
ht.dev = dev;
ht.mainCtx = ctx;
@@ -237,7 +248,7 @@ void AppendTools(Json::Value& tools) {
props["id"] = HostIdProp();
props["timeout_ms"] = PropInt(u8"等待客户端弹出聊天窗口的超时,默认 15000,范围 2000-60000");
AddTool(tools, "chat_open",
u8"在指定在线 Windows 主机上弹出「远程交谈」窗口,建立一次多轮文字对话。返回 session_id,后续 chat_send / chat_wait / chat_close 都要带上。客户端使用者会直接看到聊天窗口(无确认框)。同一主机同一时刻只允许一个会话;会话在客户端断开或空闲 10 分钟后自动失效。会话建立后应先用 chat_send 发送问候,再用 chat_wait 等待对方回复。",
u8"在指定在线 Windows / Android 主机上弹出「远程交谈」窗口,建立一次多轮文字对话。返回 session_id,后续 chat_send / chat_wait / chat_close 都要带上。客户端使用者会直接看到聊天窗口(无确认框;Android 上是应用内聊天界面,无障碍服务可用时会自动拉到前台,否则发一条可点开的通知)。同一主机同一时刻只允许一个会话;会话在客户端断开或空闲 10 分钟后自动失效。会话建立后应先用 chat_send 发送问候,再用 chat_wait 等待对方回复。",
MakeSchema(props, { "id" }),
OutputSchema([] {
Json::Value p(Json::objectValue);
@@ -255,7 +266,7 @@ void AppendTools(Json::Value& tools) {
props["session_id"] = SessionIdProp();
props["message"] = PropStr(u8"要发给客户端使用者的纯文本(UTF-8)。不能为空、不能含换行/控制字符、不能超过 4000 个 GBK 字节(约 2000 个汉字);emoji 等 GBK 无法表示的字符会被拒绝");
AddTool(tools, "chat_send",
u8"向会话对端发送一条文本。消息以客户端本地代码页(简体中文为 GBK/CP936)显示;客户端不保证非中文系统能正确显示。发送成功只代表已写入子连接,不代表对方已读。请勿在同一条消息里塞入多段对话;需要等待回复时调用 chat_wait。",
u8"向会话对端发送一条文本。消息以客户端本地代码页(简体中文为 GBK/CP936)显示(Windows 客户端按 UTF-16 渲染,Android 客户端按 GBK 解码);客户端不保证非中文系统能正确显示。发送成功只代表已写入子连接,不代表对方已读。请勿在同一条消息里塞入多段对话;需要等待回复时调用 chat_wait。",
MakeSchema(props, { "id", "session_id", "message" }),
OutputSchema([] {
Json::Value p(Json::objectValue);
@@ -322,7 +333,7 @@ static std::string BuildChatOpen(const Json::Value& id, const Json::Value& args,
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
int timeoutMs = kOpenDefaultMs;
if (!GetOptIntArg(id, args, "timeout_ms", kOpenMinMs, kOpenMaxMs, timeoutMs, errJson))
@@ -383,7 +394,7 @@ static std::string BuildChatSend(const Json::Value& id, const Json::Value& args,
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
const std::string sessionId = OptStr(args, "session_id");
if (sessionId.empty())
@@ -457,7 +468,7 @@ static std::string BuildChatWait(const Json::Value& id, const Json::Value& args,
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
const std::string sessionId = OptStr(args, "session_id");
if (sessionId.empty())
@@ -535,7 +546,7 @@ static std::string BuildChatClose(const Json::Value& id, const Json::Value& args
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
const std::string sessionId = OptStr(args, "session_id");
if (sessionId.empty())
+7 -5
View File
@@ -351,7 +351,9 @@ const char* kServerInstructions =
u8"输入被翻译成触摸手势与 ESC(返回)/ HOME(桌面)/ APPS(任务列表)/ PRINTSCREEN(截屏)"
u8"等全局动作,只支持整屏目标;remote_lock_screen 用系统的锁屏动作;注入依赖"
u8"无障碍服务、抓屏依赖已授权录屏,两者都没有能力位可查,命令下发成功不代表生效),"
u8"卸载主机(uninstall_host,会拉起系统的卸载确认框,需要有人在设备上点确认),以及 "
u8"卸载主机(uninstall_host,会拉起系统的卸载确认框,需要有人在设备上点确认)、"
u8"远程交谈(chat_open / chat_send / chat_wait / chat_close,聊天界面在 App 内,"
u8"需要声明 CLIENT_CAP_REMOTE_CHAT 的新版 APK,老 APK 返回 -32005),以及 "
u8"host_set_remark / host_set_watch_level 这两个只改服务端本地数据的工具。其余一律不支持"
u8"(客户端未实现 SYSTEM 与 SHELL)。\n"
u8"copy_files_c2c 是唯一有方向性的工具:源端 src_id 必须是 win,目标端 dst_id 可以是 "
@@ -10299,10 +10301,10 @@ const ToolPlatform kToolPlatforms[] = {
{ "host_control_window", kPlatWin },
{ "host_control_service", kPlatWin },
{ "host_control_task", kPlatWin },
{ "chat_open", kPlatWin },
{ "chat_send", kPlatWin },
{ "chat_wait", kPlatWin },
{ "chat_close", kPlatWin },
{ "chat_open", kPlatWin|kPlatApk },
{ "chat_send", kPlatWin|kPlatApk },
{ "chat_wait", kPlatWin|kPlatApk },
{ "chat_close", kPlatWin|kPlatApk },
};
// 未知的 clientType 一律按 Windows:EXE/DLL/MEXE/SC/MDLL 都走这个分支,
+8
View File
@@ -155,6 +155,14 @@ public:
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_UNINSTALL) != 0;
}
// 检查客户端是否处理 COMMAND_CHAT(远程交谈子连接)。Windows 客户端一直有该分支但
// 不声明能力位,调用方按平台区分;只有新版 Android 声明,老 APK 收到会静默丢弃。
bool SupportsChat() const {
CString caps = GetClientData(ONLINELIST_CAPABILITIES);
if (caps.IsEmpty()) return false;
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_REMOTE_CHAT) != 0;
}
// 检查客户端是否支持远程协议切换(COMMAND_SET_PROTO)。老客户端没有这个命令,
// 收到也会走 default 丢弃,服务端据此把「协议切换」菜单置灰。
bool SupportsProtoSwitch() const {