|
| 1 | +import { isValidChannelId } from './dag.mjs' |
| 2 | + |
| 3 | +/** |
| 4 | + * 创建本地 Char RPC 分发器。 |
| 5 | + * @param {Function} loadChat 加载聊天元数据的函数 |
| 6 | + * @param {Function} getChatRequest 构造 chatReplyRequest 的函数 |
| 7 | + * @returns {Function} tryInvokeLocalCharRpc |
| 8 | + */ |
| 9 | +export function createCharRpcDispatcher(loadChat, getChatRequest) { |
| 10 | + /** |
| 11 | + * 尝试在本节点群会话上调用指定 `memberId` 对应角色的 Char 方法(用于 WS RPC)。 |
| 12 | + * |
| 13 | + * @param {string} groupId 群组 id |
| 14 | + * @param {string} memberId `username:charname` 或纯 `charname`(后者视为群主用户下的角色) |
| 15 | + * @param {string} method 方法名(如 `GetReply`、`GetPrompt`) |
| 16 | + * @param {unknown[]} [args] 已 JSON 反序列化的参数表 |
| 17 | + * @returns {Promise<{ kind: 'result', value: unknown } | { kind: 'not_local' } | { kind: 'method_not_found' } | { kind: 'error', message: string }>} 本机可调用时返回 `result`;非群主成员角色返回 `not_local`;无对应方法返回 `method_not_found`;执行异常返回 `error` |
| 18 | + */ |
| 19 | + return async function tryInvokeLocalCharRpc(groupId, memberId, method, args = []) { |
| 20 | + const list = Array.isArray(args) ? args : [] |
| 21 | + const chatMetadata = await loadChat(groupId) |
| 22 | + if (!chatMetadata) return { kind: 'not_local' } |
| 23 | + |
| 24 | + const owner = chatMetadata.username |
| 25 | + let charname = memberId |
| 26 | + if (typeof memberId === 'string' && memberId.includes(':')) { |
| 27 | + const idx = memberId.indexOf(':') |
| 28 | + const u = memberId.slice(0, idx) |
| 29 | + const cn = memberId.slice(idx + 1) |
| 30 | + if (u !== owner) return { kind: 'not_local' } |
| 31 | + charname = cn |
| 32 | + } |
| 33 | + |
| 34 | + const char = chatMetadata.LastTimeSlice.chars[charname] |
| 35 | + if (!char) return { kind: 'not_local' } |
| 36 | + |
| 37 | + /** @returns {string | null} 从 RPC 参数推断频道 id */ |
| 38 | + const inferChannelId = () => { |
| 39 | + const ext = list[0]?.extension |
| 40 | + const ch = ext?.channelId |
| 41 | + if (isValidChannelId(ch)) return ch |
| 42 | + const ev = list[0]?.chatReplyRequest?.extension |
| 43 | + const ch2 = ev?.channelId |
| 44 | + if (isValidChannelId(ch2)) return ch2 |
| 45 | + return null |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + switch (method) { |
| 50 | + case 'UpdateInfo': { |
| 51 | + const fn = char.interfaces?.info?.UpdateInfo |
| 52 | + if (typeof fn !== 'function') return { kind: 'method_not_found' } |
| 53 | + return { kind: 'result', value: await fn(list[0] ?? []) } |
| 54 | + } |
| 55 | + case 'GetData': { |
| 56 | + const fn = char.interfaces?.config?.GetData |
| 57 | + if (typeof fn !== 'function') return { kind: 'method_not_found' } |
| 58 | + return { kind: 'result', value: await fn() } |
| 59 | + } |
| 60 | + case 'SetData': { |
| 61 | + const fn = char.interfaces?.config?.SetData |
| 62 | + if (typeof fn !== 'function') return { kind: 'method_not_found' } |
| 63 | + await fn(list[0]) |
| 64 | + return { kind: 'result', value: null } |
| 65 | + } |
| 66 | + case 'GetGreeting': |
| 67 | + case 'GetGroupGreeting': { |
| 68 | + const fn = method === 'GetGreeting' |
| 69 | + ? char.interfaces?.chat?.GetGreeting |
| 70 | + : char.interfaces?.chat?.GetGroupGreeting |
| 71 | + if (typeof fn !== 'function') return { kind: 'method_not_found' } |
| 72 | + const request = await getChatRequest(groupId, charname, inferChannelId()) |
| 73 | + return { kind: 'result', value: await fn(request, Number(list[1]) || 0) } |
| 74 | + } |
| 75 | + case 'GetPrompt': |
| 76 | + case 'GetPromptForOther': { |
| 77 | + const fn = method === 'GetPrompt' |
| 78 | + ? char.interfaces?.chat?.GetPrompt |
| 79 | + : char.interfaces?.chat?.GetPromptForOther |
| 80 | + if (typeof fn !== 'function') return { kind: 'method_not_found' } |
| 81 | + const request = await getChatRequest(groupId, charname, inferChannelId()) |
| 82 | + return { kind: 'result', value: await fn(request) } |
| 83 | + } |
| 84 | + case 'TweakPrompt': |
| 85 | + case 'TweakPromptForOther': { |
| 86 | + const fn = method === 'TweakPrompt' |
| 87 | + ? char.interfaces?.chat?.TweakPrompt |
| 88 | + : char.interfaces?.chat?.TweakPromptForOther |
| 89 | + if (typeof fn !== 'function') return { kind: 'result', value: null } |
| 90 | + const request = await getChatRequest(groupId, charname, inferChannelId()) |
| 91 | + await fn(request, list[1], list[2], Number(list[3]) || 0) |
| 92 | + return { kind: 'result', value: null } |
| 93 | + } |
| 94 | + case 'GetReply': { |
| 95 | + const fn = char.interfaces?.chat?.GetReply |
| 96 | + if (typeof fn !== 'function') return { kind: 'method_not_found' } |
| 97 | + const request = await getChatRequest(groupId, charname, inferChannelId()) |
| 98 | + return { kind: 'result', value: await fn(request) } |
| 99 | + } |
| 100 | + case 'onMessage': { |
| 101 | + const fn = char.interfaces?.chat?.onMessage |
| 102 | + if (typeof fn !== 'function') return { kind: 'result', value: false } |
| 103 | + const ev = list[0] || {} |
| 104 | + const onlineCount = Number(ev.onlineCount) || 1 |
| 105 | + const rid = typeof ev.chatReplyRequest?.char_id === 'string' ? ev.chatReplyRequest.char_id : charname |
| 106 | + const request = await getChatRequest(groupId, rid, inferChannelId()) |
| 107 | + return { kind: 'result', value: await fn({ chatReplyRequest: request, onlineCount }) } |
| 108 | + } |
| 109 | + case 'MessageEdit': |
| 110 | + case 'MessageEditing': |
| 111 | + case 'MessageDelete': { |
| 112 | + const fn = char.interfaces?.chat?.[method] |
| 113 | + if (typeof fn !== 'function') return { kind: 'method_not_found' } |
| 114 | + return { kind: 'result', value: await fn(list[0]) } |
| 115 | + } |
| 116 | + default: |
| 117 | + return { kind: 'method_not_found' } |
| 118 | + } |
| 119 | + } |
| 120 | + catch (e) { |
| 121 | + return { kind: 'error', message: String(e?.message || e) } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments