@zhin.js/adapter-napcat 1.0.1 → 1.1.2
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.
- package/CHANGELOG.md +757 -0
- package/{skills/napcat/PERMITS.md → PERMITS.md} +3 -3
- package/README.md +63 -95
- package/adapters/napcat/index.js +47 -0
- package/adapters/napcat/index.ts +64 -0
- package/agents/napcat/agent.json +20 -0
- package/agents/napcat/boundaries.md +3 -0
- package/agents/napcat/conventions.md +3 -0
- package/agents/napcat/skills/napcat-account/SKILL.md +25 -0
- package/agents/napcat/skills/napcat-account/tools/delete_friend/index.js +18 -0
- package/agents/napcat/skills/napcat-account/tools/delete_friend/index.ts +18 -0
- package/agents/napcat/skills/napcat-account/tools/get_user_status/index.js +16 -0
- package/agents/napcat/skills/napcat-account/tools/get_user_status/index.ts +16 -0
- package/agents/napcat/skills/napcat-account/tools/send_like/index.js +18 -0
- package/agents/napcat/skills/napcat-account/tools/send_like/index.ts +18 -0
- package/agents/napcat/skills/napcat-account/tools/set_avatar/index.js +17 -0
- package/agents/napcat/skills/napcat-account/tools/set_avatar/index.ts +17 -0
- package/agents/napcat/skills/napcat-account/tools/set_online_status/index.js +18 -0
- package/agents/napcat/skills/napcat-account/tools/set_online_status/index.ts +18 -0
- package/agents/napcat/skills/napcat-account/tools/set_profile/index.js +21 -0
- package/agents/napcat/skills/napcat-account/tools/set_profile/index.ts +21 -0
- package/agents/napcat/skills/napcat-account/tools/set_signature/index.js +17 -0
- package/agents/napcat/skills/napcat-account/tools/set_signature/index.ts +17 -0
- package/agents/napcat/skills/napcat-files-history/SKILL.md +22 -0
- package/agents/napcat/skills/napcat-files-history/tools/download_file/index.js +17 -0
- package/agents/napcat/skills/napcat-files-history/tools/download_file/index.ts +17 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_friend_msg_history/index.js +18 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_friend_msg_history/index.ts +18 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_group_file_url/index.js +19 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_group_file_url/index.ts +19 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_group_msg_history/index.js +19 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_group_msg_history/index.ts +19 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_group_root_files/index.js +17 -0
- package/agents/napcat/skills/napcat-files-history/tools/get_group_root_files/index.ts +17 -0
- package/agents/napcat/skills/napcat-files-history/tools/upload_group_file/index.js +21 -0
- package/agents/napcat/skills/napcat-files-history/tools/upload_group_file/index.ts +21 -0
- package/agents/napcat/skills/napcat-group-content/SKILL.md +21 -0
- package/agents/napcat/skills/napcat-group-content/tools/del_group_notice/index.js +20 -0
- package/agents/napcat/skills/napcat-group-content/tools/del_group_notice/index.ts +20 -0
- package/agents/napcat/skills/napcat-group-content/tools/delete_essence_msg/index.js +19 -0
- package/agents/napcat/skills/napcat-group-content/tools/delete_essence_msg/index.ts +19 -0
- package/agents/napcat/skills/napcat-group-content/tools/get_essence_list/index.js +17 -0
- package/agents/napcat/skills/napcat-group-content/tools/get_essence_list/index.ts +17 -0
- package/agents/napcat/skills/napcat-group-content/tools/get_group_notice/index.js +17 -0
- package/agents/napcat/skills/napcat-group-content/tools/get_group_notice/index.ts +17 -0
- package/agents/napcat/skills/napcat-group-content/tools/send_group_notice/index.js +21 -0
- package/agents/napcat/skills/napcat-group-content/tools/send_group_notice/index.ts +21 -0
- package/agents/napcat/skills/napcat-group-content/tools/set_essence_msg/index.js +19 -0
- package/agents/napcat/skills/napcat-group-content/tools/set_essence_msg/index.ts +19 -0
- package/agents/napcat/skills/napcat-group-settings/SKILL.md +23 -0
- package/agents/napcat/skills/napcat-group-settings/tools/get_group_info_ex/index.js +17 -0
- package/agents/napcat/skills/napcat-group-settings/tools/get_group_info_ex/index.ts +17 -0
- package/agents/napcat/skills/napcat-group-settings/tools/get_group_shut_list/index.js +17 -0
- package/agents/napcat/skills/napcat-group-settings/tools/get_group_shut_list/index.ts +17 -0
- package/agents/napcat/skills/napcat-group-settings/tools/group_sign/index.js +18 -0
- package/agents/napcat/skills/napcat-group-settings/tools/group_sign/index.ts +18 -0
- package/agents/napcat/skills/napcat-group-settings/tools/set_group_portrait/index.js +20 -0
- package/agents/napcat/skills/napcat-group-settings/tools/set_group_portrait/index.ts +20 -0
- package/agents/napcat/skills/napcat-group-settings/tools/set_title/index.js +21 -0
- package/agents/napcat/skills/napcat-group-settings/tools/set_title/index.ts +21 -0
- package/agents/napcat/skills/napcat-media/SKILL.md +23 -0
- package/agents/napcat/skills/napcat-media/tools/ai_tts/index.js +19 -0
- package/agents/napcat/skills/napcat-media/tools/ai_tts/index.ts +19 -0
- package/agents/napcat/skills/napcat-media/tools/get_ai_characters/index.js +17 -0
- package/agents/napcat/skills/napcat-media/tools/get_ai_characters/index.ts +17 -0
- package/agents/napcat/skills/napcat-media/tools/get_mini_app_ark/index.js +20 -0
- package/agents/napcat/skills/napcat-media/tools/get_mini_app_ark/index.ts +20 -0
- package/agents/napcat/skills/napcat-media/tools/ocr_image/index.js +16 -0
- package/agents/napcat/skills/napcat-media/tools/ocr_image/index.ts +16 -0
- package/agents/napcat/skills/napcat-media/tools/translate/index.js +16 -0
- package/agents/napcat/skills/napcat-media/tools/translate/index.ts +16 -0
- package/agents/napcat/skills/napcat-messaging/SKILL.md +22 -0
- package/agents/napcat/skills/napcat-messaging/tools/forward_single_msg/index.js +22 -0
- package/agents/napcat/skills/napcat-messaging/tools/forward_single_msg/index.ts +28 -0
- package/agents/napcat/skills/napcat-messaging/tools/mark_msg_as_read/index.js +17 -0
- package/agents/napcat/skills/napcat-messaging/tools/mark_msg_as_read/index.ts +17 -0
- package/agents/napcat/skills/napcat-messaging/tools/send_forward_msg/index.js +19 -0
- package/agents/napcat/skills/napcat-messaging/tools/send_forward_msg/index.ts +27 -0
- package/agents/napcat/skills/napcat-messaging/tools/send_poke/index.js +18 -0
- package/agents/napcat/skills/napcat-messaging/tools/send_poke/index.ts +18 -0
- package/agents/napcat/skills/napcat-messaging/tools/set_emoji_reaction/index.js +18 -0
- package/agents/napcat/skills/napcat-messaging/tools/set_emoji_reaction/index.ts +18 -0
- package/agents/napcat/system.md +3 -0
- package/commands/napcat/endpoint/add/[id]/index.js +3 -0
- package/commands/napcat/endpoint/add/[id]/index.ts +3 -0
- package/commands/napcat/endpoint/definition.js +29 -0
- package/commands/napcat/endpoint/definition.ts +26 -0
- package/commands/napcat/endpoint/list/index.js +3 -0
- package/commands/napcat/endpoint/list/index.ts +3 -0
- package/commands/napcat/endpoint/remove/[id]/index.js +3 -0
- package/commands/napcat/endpoint/remove/[id]/index.ts +3 -0
- package/lib/client.d.ts +52 -0
- package/lib/client.js +62 -0
- package/lib/endpoint-management.d.ts +15 -0
- package/lib/endpoint-management.js +47 -0
- package/lib/http-endpoint.d.ts +27 -0
- package/lib/http-endpoint.js +177 -0
- package/lib/index.d.ts +11 -22
- package/lib/index.js +10 -59
- package/lib/napcat-inbound.d.ts +12 -5
- package/lib/napcat-inbound.js +28 -13
- package/lib/napcat-runtime-state.d.ts +1 -0
- package/lib/napcat-runtime-state.js +6 -0
- package/lib/onebot-get-msg.d.ts +16 -3
- package/lib/onebot-get-msg.js +53 -1
- package/lib/protocol.d.ts +147 -0
- package/lib/protocol.js +318 -0
- package/lib/side-event-dispatch.d.ts +7 -0
- package/lib/side-event-dispatch.js +36 -0
- package/lib/webhook.d.ts +2 -0
- package/lib/webhook.js +14 -0
- package/lib/ws-endpoint.d.ts +27 -0
- package/lib/ws-endpoint.js +241 -0
- package/lib/ws-transport.d.ts +13 -0
- package/lib/ws-transport.js +75 -0
- package/lib/ws-types.d.ts +17 -0
- package/lib/ws-types.js +1 -0
- package/lib/wss-auth.d.ts +2 -0
- package/lib/wss-auth.js +16 -0
- package/lib/wss-endpoint.d.ts +29 -0
- package/lib/wss-endpoint.js +196 -0
- package/package.json +76 -34
- package/plugin.js +18 -0
- package/schema.json +131 -0
- package/src/client.ts +79 -0
- package/src/endpoint-management.ts +72 -0
- package/src/http-endpoint.ts +234 -0
- package/src/index.ts +62 -87
- package/src/napcat-inbound.ts +34 -10
- package/src/napcat-runtime-state.ts +7 -0
- package/src/onebot-get-msg.ts +69 -5
- package/src/protocol.ts +465 -0
- package/src/side-event-dispatch.ts +48 -0
- package/src/webhook.ts +16 -0
- package/src/ws-endpoint.ts +301 -0
- package/src/ws-transport.ts +105 -0
- package/src/ws-types.ts +20 -0
- package/src/wss-auth.ts +17 -0
- package/src/wss-endpoint.ts +247 -0
- package/client/NapCatManagement.tsx +0 -113
- package/client/index.tsx +0 -11
- package/client/tsconfig.json +0 -7
- package/client/utils/api.ts +0 -30
- package/dist/index.js +0 -27
- package/lib/adapter.d.ts +0 -28
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -94
- package/lib/adapter.js.map +0 -1
- package/lib/agent-prompt.d.ts +0 -3
- package/lib/agent-prompt.d.ts.map +0 -1
- package/lib/agent-prompt.js +0 -105
- package/lib/agent-prompt.js.map +0 -1
- package/lib/endpoint-base.d.ts +0 -143
- package/lib/endpoint-base.d.ts.map +0 -1
- package/lib/endpoint-base.js +0 -334
- package/lib/endpoint-base.js.map +0 -1
- package/lib/endpoint-http.d.ts +0 -18
- package/lib/endpoint-http.d.ts.map +0 -1
- package/lib/endpoint-http.js +0 -119
- package/lib/endpoint-http.js.map +0 -1
- package/lib/endpoint-ws-client.d.ts +0 -21
- package/lib/endpoint-ws-client.d.ts.map +0 -1
- package/lib/endpoint-ws-client.js +0 -165
- package/lib/endpoint-ws-client.js.map +0 -1
- package/lib/endpoint-ws-server.d.ts +0 -21
- package/lib/endpoint-ws-server.d.ts.map +0 -1
- package/lib/endpoint-ws-server.js +0 -150
- package/lib/endpoint-ws-server.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/napcat-inbound.d.ts.map +0 -1
- package/lib/napcat-inbound.js.map +0 -1
- package/lib/onebot-get-msg.d.ts.map +0 -1
- package/lib/onebot-get-msg.js.map +0 -1
- package/lib/routes.d.ts +0 -4
- package/lib/routes.d.ts.map +0 -1
- package/lib/routes.js +0 -61
- package/lib/routes.js.map +0 -1
- package/lib/tools.d.ts +0 -8
- package/lib/tools.d.ts.map +0 -1
- package/lib/tools.js +0 -605
- package/lib/tools.js.map +0 -1
- package/lib/types.d.ts +0 -293
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -6
- package/lib/types.js.map +0 -1
- package/lib/typing-indicator.d.ts +0 -45
- package/lib/typing-indicator.d.ts.map +0 -1
- package/lib/typing-indicator.js +0 -132
- package/lib/typing-indicator.js.map +0 -1
- package/plugin.yml +0 -3
- package/skills/napcat/SKILL.md +0 -311
- package/src/adapter.ts +0 -101
- package/src/agent-prompt.ts +0 -114
- package/src/endpoint-base.ts +0 -376
- package/src/endpoint-http.ts +0 -109
- package/src/endpoint-ws-client.ts +0 -164
- package/src/endpoint-ws-server.ts +0 -150
- package/src/routes.ts +0 -61
- package/src/tools.ts +0 -632
- package/src/types.ts +0 -279
- package/src/typing-indicator.ts +0 -184
package/lib/client.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { NapCatEvent } from './protocol.js';
|
|
2
|
+
export type NapcatApiCall = (action: string, params?: Record<string, unknown>) => Promise<unknown>;
|
|
3
|
+
/** Transport-independent NapCat protocol Client produced by every Endpoint mode. */
|
|
4
|
+
export declare class NapcatClient {
|
|
5
|
+
readonly callApi: NapcatApiCall;
|
|
6
|
+
constructor(callApi: NapcatApiCall);
|
|
7
|
+
setTitle(groupId: number, userId: number, title: string, duration?: number): Promise<boolean>;
|
|
8
|
+
sendLike(userId: number, times?: number): Promise<unknown>;
|
|
9
|
+
deleteFriend(userId: number): Promise<unknown>;
|
|
10
|
+
markMsgAsRead(messageId: number): Promise<unknown>;
|
|
11
|
+
ocrImage(image: string): Promise<unknown>;
|
|
12
|
+
setQQProfile(nickname: string, company?: string, email?: string, college?: string, personalNote?: string): Promise<unknown>;
|
|
13
|
+
setGroupPortrait(groupId: number, file: string): Promise<unknown>;
|
|
14
|
+
setEssenceMsg(messageId: number): Promise<unknown>;
|
|
15
|
+
deleteEssenceMsg(messageId: number): Promise<unknown>;
|
|
16
|
+
getEssenceMsgList(groupId: number): Promise<unknown>;
|
|
17
|
+
sendGroupSign(groupId: number): Promise<unknown>;
|
|
18
|
+
sendGroupNotice(groupId: number, content: string, image?: string): Promise<unknown>;
|
|
19
|
+
getGroupNotice(groupId: number): Promise<unknown>;
|
|
20
|
+
deleteGroupNotice(groupId: number, noticeId: string): Promise<unknown>;
|
|
21
|
+
uploadGroupFile(groupId: number, file: string, name: string, folder?: string): Promise<unknown>;
|
|
22
|
+
getGroupRootFiles(groupId: number): Promise<unknown>;
|
|
23
|
+
getGroupFileUrl(groupId: number, fileId: string, busid: number): Promise<unknown>;
|
|
24
|
+
downloadFile(url: string, threadCount?: number, headers?: string[]): Promise<unknown>;
|
|
25
|
+
setOnlineStatus(status: number, extStatus: number): Promise<unknown>;
|
|
26
|
+
setQQAvatar(file: string): Promise<unknown>;
|
|
27
|
+
forwardFriendSingleMsg(userId: number, messageId: number): Promise<unknown>;
|
|
28
|
+
forwardGroupSingleMsg(groupId: number, messageId: number): Promise<unknown>;
|
|
29
|
+
translateEn2Zh(sourceText: string): Promise<unknown>;
|
|
30
|
+
setMsgEmojiLike(messageId: number, emojiId: string): Promise<unknown>;
|
|
31
|
+
sendForwardMsg(messageType: 'private' | 'group', id: number, messages: unknown[]): Promise<unknown>;
|
|
32
|
+
getFriendMsgHistory(userId: number, messageSeq?: number, count?: number): Promise<unknown>;
|
|
33
|
+
getGroupMsgHistory(groupId: number, messageSeq?: number, count?: number): Promise<unknown>;
|
|
34
|
+
setSelfLongnick(longnick: string): Promise<unknown>;
|
|
35
|
+
getGroupInfoEx(groupId: number): Promise<unknown>;
|
|
36
|
+
sendPoke(userId: number, groupId?: number): Promise<unknown>;
|
|
37
|
+
ncGetUserStatus(userId: number): Promise<unknown>;
|
|
38
|
+
getGroupShutList(groupId: number): Promise<unknown>;
|
|
39
|
+
getMiniAppArk(type: string, title: string, desc: string, picUrl: string, jumpUrl: string): Promise<unknown>;
|
|
40
|
+
getAiCharacters(groupId: number): Promise<unknown>;
|
|
41
|
+
sendGroupAiRecord(groupId: number, characterId: string, text: string): Promise<unknown>;
|
|
42
|
+
}
|
|
43
|
+
export type NapcatClientEventMap = Record<string, NapCatEvent>;
|
|
44
|
+
declare module '@zhin.js/feature-kit' {
|
|
45
|
+
interface AdapterClientRegistry {
|
|
46
|
+
readonly napcat: {
|
|
47
|
+
readonly client: NapcatClient;
|
|
48
|
+
readonly events: NapcatClientEventMap;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export declare const napcatClient: import("@zhin.js/adapter").EndpointClientToken<NapcatClient, NapcatClientEventMap>;
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { defineEndpointClient } from 'zhin.js/adapter';
|
|
2
|
+
/** Transport-independent NapCat protocol Client produced by every Endpoint mode. */
|
|
3
|
+
export class NapcatClient {
|
|
4
|
+
callApi;
|
|
5
|
+
constructor(callApi) {
|
|
6
|
+
this.callApi = callApi;
|
|
7
|
+
}
|
|
8
|
+
async setTitle(groupId, userId, title, duration = -1) {
|
|
9
|
+
await this.callApi('set_group_special_title', {
|
|
10
|
+
group_id: groupId,
|
|
11
|
+
user_id: userId,
|
|
12
|
+
special_title: title,
|
|
13
|
+
duration,
|
|
14
|
+
});
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
sendLike(userId, times = 1) { return this.callApi('send_like', { user_id: userId, times }); }
|
|
18
|
+
deleteFriend(userId) { return this.callApi('delete_friend', { user_id: userId }); }
|
|
19
|
+
markMsgAsRead(messageId) { return this.callApi('mark_msg_as_read', { message_id: messageId }); }
|
|
20
|
+
ocrImage(image) { return this.callApi('ocr_image', { image }); }
|
|
21
|
+
setQQProfile(nickname, company, email, college, personalNote) {
|
|
22
|
+
return this.callApi('set_qq_profile', { nickname, company, email, college, personal_note: personalNote });
|
|
23
|
+
}
|
|
24
|
+
setGroupPortrait(groupId, file) { return this.callApi('set_group_portrait', { group_id: groupId, file }); }
|
|
25
|
+
setEssenceMsg(messageId) { return this.callApi('set_essence_msg', { message_id: messageId }); }
|
|
26
|
+
deleteEssenceMsg(messageId) { return this.callApi('delete_essence_msg', { message_id: messageId }); }
|
|
27
|
+
getEssenceMsgList(groupId) { return this.callApi('get_essence_msg_list', { group_id: groupId }); }
|
|
28
|
+
sendGroupSign(groupId) { return this.callApi('send_group_sign', { group_id: groupId }); }
|
|
29
|
+
sendGroupNotice(groupId, content, image) { return this.callApi('_send_group_notice', { group_id: groupId, content, image }); }
|
|
30
|
+
getGroupNotice(groupId) { return this.callApi('_get_group_notice', { group_id: groupId }); }
|
|
31
|
+
deleteGroupNotice(groupId, noticeId) { return this.callApi('_del_group_notice', { group_id: groupId, notice_id: noticeId }); }
|
|
32
|
+
uploadGroupFile(groupId, file, name, folder) { return this.callApi('upload_group_file', { group_id: groupId, file, name, folder }); }
|
|
33
|
+
getGroupRootFiles(groupId) { return this.callApi('get_group_root_files', { group_id: groupId }); }
|
|
34
|
+
getGroupFileUrl(groupId, fileId, busid) { return this.callApi('get_group_file_url', { group_id: groupId, file_id: fileId, busid }); }
|
|
35
|
+
downloadFile(url, threadCount = 1, headers) { return this.callApi('download_file', { url, thread_count: threadCount, headers }); }
|
|
36
|
+
setOnlineStatus(status, extStatus) { return this.callApi('set_online_status', { status, ext_status: extStatus }); }
|
|
37
|
+
setQQAvatar(file) { return this.callApi('set_qq_avatar', { file }); }
|
|
38
|
+
forwardFriendSingleMsg(userId, messageId) { return this.callApi('forward_friend_single_msg', { user_id: userId, message_id: messageId }); }
|
|
39
|
+
forwardGroupSingleMsg(groupId, messageId) { return this.callApi('forward_group_single_msg', { group_id: groupId, message_id: messageId }); }
|
|
40
|
+
translateEn2Zh(sourceText) { return this.callApi('translate_en2zh', { source_text: sourceText }); }
|
|
41
|
+
setMsgEmojiLike(messageId, emojiId) { return this.callApi('set_msg_emoji_like', { message_id: messageId, emoji_id: emojiId }); }
|
|
42
|
+
sendForwardMsg(messageType, id, messages) {
|
|
43
|
+
return this.callApi('send_forward_msg', {
|
|
44
|
+
message_type: messageType,
|
|
45
|
+
[messageType === 'group' ? 'group_id' : 'user_id']: id,
|
|
46
|
+
messages,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
getFriendMsgHistory(userId, messageSeq, count) { return this.callApi('get_friend_msg_history', { user_id: userId, message_seq: messageSeq, count }); }
|
|
50
|
+
getGroupMsgHistory(groupId, messageSeq, count) { return this.callApi('get_group_msg_history', { group_id: groupId, message_seq: messageSeq, count }); }
|
|
51
|
+
setSelfLongnick(longnick) { return this.callApi('set_self_longnick', { longNick: longnick }); }
|
|
52
|
+
getGroupInfoEx(groupId) { return this.callApi('get_group_info_ex', { group_id: groupId }); }
|
|
53
|
+
sendPoke(userId, groupId) { return this.callApi('send_poke', { user_id: userId, group_id: groupId }); }
|
|
54
|
+
ncGetUserStatus(userId) { return this.callApi('nc_get_user_status', { user_id: userId }); }
|
|
55
|
+
getGroupShutList(groupId) { return this.callApi('get_group_shut_list', { group_id: groupId }); }
|
|
56
|
+
getMiniAppArk(type, title, desc, picUrl, jumpUrl) { return this.callApi('get_mini_app_ark', { type, title, desc, picUrl, jumpUrl }); }
|
|
57
|
+
getAiCharacters(groupId) { return this.callApi('get_ai_characters', { group_id: groupId }); }
|
|
58
|
+
sendGroupAiRecord(groupId, characterId, text) {
|
|
59
|
+
return this.callApi('send_group_ai_record', { group_id: groupId, character: characterId, text });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export const napcatClient = defineEndpointClient('napcat');
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NapCat endpoint management 语义端口(Console 社交面 RPC 消费,见
|
|
3
|
+
* packages/im/adapter/src/endpoint-management.ts)。
|
|
4
|
+
*
|
|
5
|
+
* 归一化取舍:
|
|
6
|
+
* - friend → {user_id:number, nickname, remark: remark ?? ''}
|
|
7
|
+
* - group → {group_id:number, name: group_name ?? name}
|
|
8
|
+
* - 群成员列表保持 OneBot11 原生形状,仅保证数组
|
|
9
|
+
*/
|
|
10
|
+
import type { EndpointManagement } from 'zhin.js/adapter';
|
|
11
|
+
/** 管理面只依赖 endpoint 的 callApi(ws/wss/http 三种传输各自实现)。 */
|
|
12
|
+
export interface NapCatManagementCaller {
|
|
13
|
+
callApi(action: string, params?: Record<string, unknown>): Promise<unknown>;
|
|
14
|
+
}
|
|
15
|
+
export declare function createNapCatEndpointManagement(endpoint: NapCatManagementCaller): EndpointManagement;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export function createNapCatEndpointManagement(endpoint) {
|
|
2
|
+
return Object.freeze({
|
|
3
|
+
async listFriends() {
|
|
4
|
+
const data = await endpoint.callApi('get_friend_list');
|
|
5
|
+
return toArray(data).map((value) => {
|
|
6
|
+
const friend = asRecord(value);
|
|
7
|
+
return {
|
|
8
|
+
user_id: toNumberId(friend.user_id, 'user_id'),
|
|
9
|
+
nickname: String(friend.nickname ?? ''),
|
|
10
|
+
remark: String(friend.remark ?? ''),
|
|
11
|
+
};
|
|
12
|
+
});
|
|
13
|
+
},
|
|
14
|
+
async listGroups() {
|
|
15
|
+
const data = await endpoint.callApi('get_group_list');
|
|
16
|
+
return toArray(data).map((value) => {
|
|
17
|
+
const group = asRecord(value);
|
|
18
|
+
return {
|
|
19
|
+
group_id: toNumberId(group.group_id, 'group_id'),
|
|
20
|
+
name: String(group.group_name ?? group.name ?? ''),
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
async listGroupMembers(groupId) {
|
|
25
|
+
const data = await endpoint.callApi('get_group_member_list', {
|
|
26
|
+
group_id: toNumberId(groupId, 'group_id'),
|
|
27
|
+
});
|
|
28
|
+
return toArray(data);
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function asRecord(value) {
|
|
33
|
+
return value !== null && typeof value === 'object'
|
|
34
|
+
? value
|
|
35
|
+
: {};
|
|
36
|
+
}
|
|
37
|
+
function toArray(value) {
|
|
38
|
+
return Array.isArray(value) ? value : [];
|
|
39
|
+
}
|
|
40
|
+
/** console RPC 传入的 gid/uid 可能是字符串,统一收敛为数字。 */
|
|
41
|
+
function toNumberId(value, label) {
|
|
42
|
+
const n = Number(value);
|
|
43
|
+
if (!Number.isFinite(n) || String(value ?? '').trim() === '') {
|
|
44
|
+
throw new TypeError(`napcat ${label} 必须是数字: ${String(value)}`);
|
|
45
|
+
}
|
|
46
|
+
return n;
|
|
47
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Endpoint } from 'zhin.js/adapter';
|
|
2
|
+
import { type EndpointControl, type EndpointManagement, type EndpointSendRequest } from 'zhin.js/adapter';
|
|
3
|
+
import type { HttpHost } from '@zhin.js/host-http';
|
|
4
|
+
import type { CapabilityId } from 'zhin.js';
|
|
5
|
+
import { callNapCatHttpAction, type NapCatEvent, type NapCatHttpConfig } from './protocol.js';
|
|
6
|
+
import { NapcatClient } from './client.js';
|
|
7
|
+
export interface NapCatHttpEndpointOptions {
|
|
8
|
+
readonly id: CapabilityId;
|
|
9
|
+
readonly http: HttpHost;
|
|
10
|
+
readonly config: NapCatHttpConfig;
|
|
11
|
+
readonly callHttpAction?: typeof callNapCatHttpAction;
|
|
12
|
+
}
|
|
13
|
+
export declare class NapCatHttpEndpoint extends Endpoint<NapcatClient> {
|
|
14
|
+
#private;
|
|
15
|
+
readonly client: NapcatClient;
|
|
16
|
+
readonly management: EndpointManagement;
|
|
17
|
+
readonly control: EndpointControl;
|
|
18
|
+
readonly content: import("@zhin.js/adapter").EndpointContentPort;
|
|
19
|
+
constructor(options: NapCatHttpEndpointOptions);
|
|
20
|
+
start(): Promise<void>;
|
|
21
|
+
open(): void;
|
|
22
|
+
close(): void;
|
|
23
|
+
stop(): Promise<void>;
|
|
24
|
+
send({ conversation, payload }: EndpointSendRequest): Promise<string>;
|
|
25
|
+
recallMessage(messageId: string): Promise<void>;
|
|
26
|
+
admit(ev: NapCatEvent): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Endpoint } from 'zhin.js/adapter';
|
|
2
|
+
import { createRecallEndpointControl, } from 'zhin.js/adapter';
|
|
3
|
+
import { formatCompact, getAdapterLogger } from '@zhin.js/logger';
|
|
4
|
+
import { createNapCatEndpointManagement } from './endpoint-management.js';
|
|
5
|
+
import { InboundMessageDeduper, isNapCatBotMentioned, isSelfMessage, normalizeMessage, } from './napcat-inbound.js';
|
|
6
|
+
import { buildSendAction, callNapCatHttpAction, formatInboundContent, formatOutboundSegments, isMessageEvent, napcatInboundConversation, napcatOutboundTarget, senderNickname, senderUserId, } from './protocol.js';
|
|
7
|
+
import { receiveNapCatSideEvent } from './side-event-dispatch.js';
|
|
8
|
+
import { createNapCatContentPort } from './onebot-get-msg.js';
|
|
9
|
+
import { NapcatClient } from './client.js';
|
|
10
|
+
import { readRequestBody } from './webhook.js';
|
|
11
|
+
import { verifyNapCatAccessToken } from './wss-auth.js';
|
|
12
|
+
export class NapCatHttpEndpoint extends Endpoint {
|
|
13
|
+
client = new NapcatClient((action, params) => this.#callApi(action, params));
|
|
14
|
+
#logger;
|
|
15
|
+
#options;
|
|
16
|
+
#inboundDeduper = new InboundMessageDeduper();
|
|
17
|
+
management = createNapCatEndpointManagement(this.client);
|
|
18
|
+
control = createRecallEndpointControl((id) => this.recallMessage(id));
|
|
19
|
+
content = createNapCatContentPort((action, params) => this.client.callApi(action, params));
|
|
20
|
+
#callHttpAction;
|
|
21
|
+
#routeReleases = [];
|
|
22
|
+
#open = false;
|
|
23
|
+
#started = false;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
super();
|
|
26
|
+
this.#logger = getAdapterLogger('napcat', options.config.id);
|
|
27
|
+
this.#options = options;
|
|
28
|
+
this.#callHttpAction = options.callHttpAction ?? callNapCatHttpAction;
|
|
29
|
+
}
|
|
30
|
+
async start() {
|
|
31
|
+
if (this.#started)
|
|
32
|
+
return;
|
|
33
|
+
this.#started = true;
|
|
34
|
+
this.#setupRoutes();
|
|
35
|
+
this.#logger.info(formatCompact({
|
|
36
|
+
op: 'listen',
|
|
37
|
+
endpoint: this.#options.config.id,
|
|
38
|
+
mode: 'http',
|
|
39
|
+
path: this.#options.config.post_path,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
open() {
|
|
43
|
+
this.#open = true;
|
|
44
|
+
}
|
|
45
|
+
close() {
|
|
46
|
+
this.#open = false;
|
|
47
|
+
}
|
|
48
|
+
async stop() {
|
|
49
|
+
this.#open = false;
|
|
50
|
+
for (const release of this.#routeReleases.splice(0))
|
|
51
|
+
release();
|
|
52
|
+
this.#inboundDeduper.clear();
|
|
53
|
+
this.#started = false;
|
|
54
|
+
this.#logger.debug(formatCompact({ op: 'disconnect' }));
|
|
55
|
+
}
|
|
56
|
+
async send({ conversation, payload }) {
|
|
57
|
+
const message = formatOutboundSegments(payload);
|
|
58
|
+
const { action, params } = buildSendAction(napcatOutboundTarget(conversation), message);
|
|
59
|
+
const resp = await this.#callHttpAction({
|
|
60
|
+
http_url: this.#options.config.http_url,
|
|
61
|
+
access_token: this.#options.config.access_token,
|
|
62
|
+
}, action, params);
|
|
63
|
+
const data = resp.data;
|
|
64
|
+
const messageId = data?.message_id != null ? String(data.message_id) : '';
|
|
65
|
+
this.#logger.debug(formatCompact({
|
|
66
|
+
op: 'napcat_send',
|
|
67
|
+
endpoint: this.#options.config.id,
|
|
68
|
+
target: `${conversation.kind}:${conversation.id}`,
|
|
69
|
+
messageId,
|
|
70
|
+
}));
|
|
71
|
+
return messageId;
|
|
72
|
+
}
|
|
73
|
+
async recallMessage(messageId) {
|
|
74
|
+
if (!messageId)
|
|
75
|
+
return;
|
|
76
|
+
await this.client.callApi('delete_msg', { message_id: Number(messageId) });
|
|
77
|
+
}
|
|
78
|
+
#callApi(action, params = {}) {
|
|
79
|
+
return this.#callHttpAction({
|
|
80
|
+
http_url: this.#options.config.http_url,
|
|
81
|
+
access_token: this.#options.config.access_token,
|
|
82
|
+
}, action, params).then((resp) => resp.data);
|
|
83
|
+
}
|
|
84
|
+
admit(ev) {
|
|
85
|
+
if (!this.#open)
|
|
86
|
+
return;
|
|
87
|
+
void this.emitPlatform(napCatPlatformEventName(ev), ev).catch((error) => {
|
|
88
|
+
this.#logger.warn(formatCompact({
|
|
89
|
+
op: 'napcat_platform_event_failed',
|
|
90
|
+
error: error instanceof Error ? error.message : String(error),
|
|
91
|
+
}));
|
|
92
|
+
});
|
|
93
|
+
if (!isMessageEvent(ev)) {
|
|
94
|
+
receiveNapCatSideEvent((name, payload) => this.emit(name, payload), this.#options.config.id, this.client, ev, this.#logger);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (isSelfMessage(ev))
|
|
98
|
+
return;
|
|
99
|
+
const msgId = String(ev.message_id);
|
|
100
|
+
if (!this.#inboundDeduper.shouldProcess(msgId))
|
|
101
|
+
return;
|
|
102
|
+
if (Array.isArray(ev.message) || typeof ev.message === 'string') {
|
|
103
|
+
ev = { ...ev, message: normalizeMessage(ev.message) };
|
|
104
|
+
}
|
|
105
|
+
const conversation = napcatInboundConversation(String(this.#options.id), ev);
|
|
106
|
+
const nickname = senderNickname(ev);
|
|
107
|
+
const mentioned = isNapCatBotMentioned(ev);
|
|
108
|
+
void this.emit('message.receive', {
|
|
109
|
+
conversation,
|
|
110
|
+
message: { conversation, id: msgId },
|
|
111
|
+
content: formatInboundContent(ev),
|
|
112
|
+
sender: {
|
|
113
|
+
id: senderUserId(ev),
|
|
114
|
+
name: nickname,
|
|
115
|
+
...(ev.sender?.role ? { roles: [ev.sender.role] } : {}),
|
|
116
|
+
},
|
|
117
|
+
endpointId: this.#options.config.id,
|
|
118
|
+
...(mentioned ? { mentioned: true } : {}),
|
|
119
|
+
metadata: Object.freeze({
|
|
120
|
+
message_type: ev.message_type,
|
|
121
|
+
user_id: ev.user_id != null ? String(ev.user_id) : undefined,
|
|
122
|
+
group_id: ev.group_id != null ? String(ev.group_id) : undefined,
|
|
123
|
+
time: ev.time,
|
|
124
|
+
self_id: ev.self_id != null ? String(ev.self_id) : undefined,
|
|
125
|
+
role: ev.sender?.role,
|
|
126
|
+
...(nickname ? { nickname } : {}),
|
|
127
|
+
}),
|
|
128
|
+
}).catch((err) => {
|
|
129
|
+
this.#logger.warn(formatCompact({
|
|
130
|
+
op: 'napcat_gateway_receive_failed',
|
|
131
|
+
target: `${conversation.kind}:${conversation.id}`,
|
|
132
|
+
error: err instanceof Error ? err.message : String(err),
|
|
133
|
+
}));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
#setupRoutes() {
|
|
137
|
+
const path = this.#options.config.post_path;
|
|
138
|
+
this.#routeReleases.push(this.#options.http.route('POST', path, async (request, response) => {
|
|
139
|
+
await this.#handlePost(request, response);
|
|
140
|
+
}, { summary: 'NapCat HTTP event callback', tags: ['napcat'] }));
|
|
141
|
+
}
|
|
142
|
+
async #handlePost(request, response) {
|
|
143
|
+
try {
|
|
144
|
+
if (!verifyNapCatAccessToken(this.#options.config.access_token, request)) {
|
|
145
|
+
response.writeHead(403, { 'Content-Type': 'application/json' });
|
|
146
|
+
response.end(JSON.stringify({ message: 'Unauthorized' }));
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const raw = await readRequestBody(request);
|
|
150
|
+
let ev;
|
|
151
|
+
try {
|
|
152
|
+
ev = JSON.parse(raw);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
response.writeHead(400, { 'Content-Type': 'application/json' });
|
|
156
|
+
response.end(JSON.stringify({ message: 'Invalid JSON' }));
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (this.#open)
|
|
160
|
+
this.admit(ev);
|
|
161
|
+
response.writeHead(200, { 'Content-Type': 'application/json' });
|
|
162
|
+
response.end(JSON.stringify({ status: 'ok' }));
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
this.#logger.error('NapCat HTTP webhook error:', error);
|
|
166
|
+
if (!response.headersSent) {
|
|
167
|
+
response.writeHead(500, { 'Content-Type': 'application/json' });
|
|
168
|
+
response.end(JSON.stringify({ message: 'Internal Server Error' }));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function napCatPlatformEventName(ev) {
|
|
174
|
+
return [ev.post_type, ev.message_type ?? ev.notice_type ?? ev.request_type, ev.sub_type]
|
|
175
|
+
.filter(Boolean)
|
|
176
|
+
.join('.');
|
|
177
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,22 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export { NapCatHttpEndpoint } from './endpoint
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
11
|
-
|
|
12
|
-
namespace Plugin {
|
|
13
|
-
interface Contexts {
|
|
14
|
-
web: PageManager;
|
|
15
|
-
router: Router;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
interface Adapters {
|
|
19
|
-
napcat: NapCatAdapter;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
export { buildSendAction, buildWsConnectOptions, callNapCatHttpAction, formatInboundContent, formatOutboundSegments, getChannelId, isMessageEvent, mediaRefToOneBotFile, napcatInboundConversation, napcatOutboundTarget, resolveNapCatConfig, senderNickname, senderUserId, type MessageSegment, type NapCatActionRequest, type NapCatActionResponse, type NapCatConfigBase, type NapCatEndpointConfig, type NapCatEvent, type NapCatHttpConfig, type NapCatMessageEvent, type NapCatSender, type NapCatWireSegment, type NapCatWsConfig, type NapCatWssConfig, type ParsedSendTarget, type ResolvedNapCatConfig, } from './protocol.js';
|
|
2
|
+
export { InboundMessageDeduper, isNapCatBotMentioned, isSelfMessage, normalizeMessage, } from './napcat-inbound.js';
|
|
3
|
+
export { napcatClient, type NapcatClient, type NapcatClientEventMap, } from './client.js';
|
|
4
|
+
export { parseOneBotGetMsgResponse } from './onebot-get-msg.js';
|
|
5
|
+
export { NapCatWsEndpoint, type NapCatWsEndpointOptions, } from './ws-endpoint.js';
|
|
6
|
+
export { NapCatWssEndpoint, type NapCatWssEndpointOptions, } from './wss-endpoint.js';
|
|
7
|
+
export { NapCatHttpEndpoint, type NapCatHttpEndpointOptions, } from './http-endpoint.js';
|
|
8
|
+
export type { NapCatWsSocket, NapCatWsCreateOptions } from './ws-types.js';
|
|
9
|
+
export { callNapCatWsAction, decodeWsPayload, handleNapCatWsMessage, rejectAllPending, startNapCatHeartbeat, } from './ws-transport.js';
|
|
10
|
+
export { verifyNapCatAccessToken } from './wss-auth.js';
|
|
11
|
+
export { readRequestBody } from './webhook.js';
|
package/lib/index.js
CHANGED
|
@@ -1,59 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import { registerRoutes } from './routes.js';
|
|
12
|
-
export * from './types.js';
|
|
13
|
-
export { NapCatWsClient } from './endpoint-ws-client.js';
|
|
14
|
-
export { NapCatWsServer } from './endpoint-ws-server.js';
|
|
15
|
-
export { NapCatHttpEndpoint } from './endpoint-http.js';
|
|
16
|
-
export { NapCatAdapter } from './adapter.js';
|
|
17
|
-
export { NapCatEndpointBase } from './endpoint-base.js';
|
|
18
|
-
export { NapCatTypingIndicatorManager, enableTypingIndicator, } from './typing-indicator.js';
|
|
19
|
-
const plugin = usePlugin();
|
|
20
|
-
const { provide, useContext } = plugin;
|
|
21
|
-
// ── 适配器注册 ─────────────────────────────────────────────────────
|
|
22
|
-
provide({
|
|
23
|
-
name: 'napcat',
|
|
24
|
-
description: 'NapCatQQ 适配器(OneBot11 + go-cqhttp + NapCat 扩展,正向/反向 WS + HTTP)',
|
|
25
|
-
mounted: async (p) => {
|
|
26
|
-
registerAgentPromptContributor(createNapCatAgentPromptContributor());
|
|
27
|
-
const adapter = new NapCatAdapter(p);
|
|
28
|
-
await adapter.start();
|
|
29
|
-
return adapter;
|
|
30
|
-
},
|
|
31
|
-
dispose: async (adapter) => {
|
|
32
|
-
unregisterAgentPromptContributor('napcat');
|
|
33
|
-
await adapter.stop();
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
// ── AI 工具注册 ────────────────────────────────────────────────────
|
|
37
|
-
useContext('tool', 'napcat', (toolService, napcat) => {
|
|
38
|
-
const disposers = [];
|
|
39
|
-
disposers.push(registerDefaultGroupPlatformPermitChecker('napcat'));
|
|
40
|
-
const groupTools = createGroupManagementTools(napcat, 'napcat');
|
|
41
|
-
disposers.push(...groupTools.map(t => toolService.addTool(t, plugin.name)));
|
|
42
|
-
const napcatTools = createNapCatTools(napcat);
|
|
43
|
-
disposers.push(...napcatTools.map(t => toolService.addTool(t, plugin.name)));
|
|
44
|
-
return () => disposers.forEach(d => d());
|
|
45
|
-
});
|
|
46
|
-
// ── Web 控制台入口 ─────────────────────────────────────────────────
|
|
47
|
-
useContext('web', (pageManager) => {
|
|
48
|
-
pageManager.addEntry({
|
|
49
|
-
id: 'napcat',
|
|
50
|
-
development: path.resolve(import.meta.dirname, '../client/index.tsx'),
|
|
51
|
-
production: path.resolve(import.meta.dirname, '../dist/index.js'),
|
|
52
|
-
meta: { name: 'NapCat' },
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
// ── HTTP 路由 ──────────────────────────────────────────────────────
|
|
56
|
-
useContext('router', 'napcat', async (router, napcat) => {
|
|
57
|
-
registerRoutes(router, napcat);
|
|
58
|
-
});
|
|
59
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { buildSendAction, buildWsConnectOptions, callNapCatHttpAction, formatInboundContent, formatOutboundSegments, getChannelId, isMessageEvent, mediaRefToOneBotFile, napcatInboundConversation, napcatOutboundTarget, resolveNapCatConfig, senderNickname, senderUserId, } from './protocol.js';
|
|
2
|
+
export { InboundMessageDeduper, isNapCatBotMentioned, isSelfMessage, normalizeMessage, } from './napcat-inbound.js';
|
|
3
|
+
export { napcatClient, } from './client.js';
|
|
4
|
+
export { parseOneBotGetMsgResponse } from './onebot-get-msg.js';
|
|
5
|
+
export { NapCatWsEndpoint, } from './ws-endpoint.js';
|
|
6
|
+
export { NapCatWssEndpoint, } from './wss-endpoint.js';
|
|
7
|
+
export { NapCatHttpEndpoint, } from './http-endpoint.js';
|
|
8
|
+
export { callNapCatWsAction, decodeWsPayload, handleNapCatWsMessage, rejectAllPending, startNapCatHeartbeat, } from './ws-transport.js';
|
|
9
|
+
export { verifyNapCatAccessToken } from './wss-auth.js';
|
|
10
|
+
export { readRequestBody } from './webhook.js';
|
package/lib/napcat-inbound.d.ts
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* NapCat 入站消息治理:去重、自发过滤、消息归一化
|
|
3
3
|
*/
|
|
4
|
-
import type { NapCatMessageEvent, MessageSegment } from './
|
|
4
|
+
import type { NapCatMessageEvent, MessageSegment } from './protocol.js';
|
|
5
5
|
export declare class InboundMessageDeduper {
|
|
6
6
|
private readonly seen;
|
|
7
7
|
shouldProcess(messageId: string): boolean;
|
|
8
8
|
clear(): void;
|
|
9
9
|
}
|
|
10
10
|
/** 判断是否为 bot 自身发出的消息 */
|
|
11
|
-
export declare function isSelfMessage(event: NapCatMessageEvent
|
|
11
|
+
export declare function isSelfMessage(event: NapCatMessageEvent | {
|
|
12
|
+
post_type?: string;
|
|
13
|
+
self_id?: number | string;
|
|
14
|
+
user_id?: number | string;
|
|
15
|
+
}): boolean;
|
|
12
16
|
/**
|
|
13
17
|
* 将 message 字段归一化为 MessageSegment[]。
|
|
14
18
|
* NapCat 通常返回数组,但某些配置下可能返回 CQ 字符串。
|
|
15
19
|
*/
|
|
16
20
|
export declare function normalizeMessage(message: MessageSegment[] | string): MessageSegment[];
|
|
17
21
|
/**
|
|
18
|
-
*
|
|
22
|
+
* OneBot11 mentioned 判定:消息段 {type:'at', data:{qq}} 的 qq 等于本机 uin
|
|
23
|
+
* (事件 self_id)时为 true;`qq === 'all'` 不算。兼容 CQ 字符串形式。
|
|
19
24
|
*/
|
|
20
|
-
export declare function
|
|
21
|
-
|
|
25
|
+
export declare function isNapCatBotMentioned(ev: {
|
|
26
|
+
self_id?: number | string;
|
|
27
|
+
message?: MessageSegment[] | string;
|
|
28
|
+
}): boolean;
|
package/lib/napcat-inbound.js
CHANGED
|
@@ -37,19 +37,34 @@ export function normalizeMessage(message) {
|
|
|
37
37
|
}
|
|
38
38
|
return [];
|
|
39
39
|
}
|
|
40
|
+
const CQ_AT_RE = /\[CQ:at,qq=([^\],\s]+)\]/g;
|
|
41
|
+
function atTargetMatchesUin(target, uin) {
|
|
42
|
+
const qq = String(target ?? '').trim();
|
|
43
|
+
return Boolean(qq) && qq !== 'all' && Number(qq) === Number(uin);
|
|
44
|
+
}
|
|
40
45
|
/**
|
|
41
|
-
*
|
|
46
|
+
* OneBot11 mentioned 判定:消息段 {type:'at', data:{qq}} 的 qq 等于本机 uin
|
|
47
|
+
* (事件 self_id)时为 true;`qq === 'all'` 不算。兼容 CQ 字符串形式。
|
|
42
48
|
*/
|
|
43
|
-
export function
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
export function isNapCatBotMentioned(ev) {
|
|
50
|
+
if (ev.self_id == null)
|
|
51
|
+
return false;
|
|
52
|
+
const uin = String(ev.self_id).trim();
|
|
53
|
+
if (!uin)
|
|
54
|
+
return false;
|
|
55
|
+
const segments = normalizeMessage(ev.message ?? []);
|
|
56
|
+
for (const seg of segments) {
|
|
57
|
+
if (seg.type === 'at' && atTargetMatchesUin(seg.data?.qq, uin))
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
const text = segments
|
|
61
|
+
.map((seg) => (seg.type === 'text' ? String(seg.data?.text ?? '') : ''))
|
|
62
|
+
.join('');
|
|
63
|
+
if (!text)
|
|
64
|
+
return false;
|
|
65
|
+
for (const match of text.matchAll(CQ_AT_RE)) {
|
|
66
|
+
if (atTargetMatchesUin(match[1], uin))
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
54
70
|
}
|
|
55
|
-
//# sourceMappingURL=napcat-inbound.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const napcatRuntimeStateToken: import("zhin.js").Token<import("@zhin.js/adapter").EndpointRuntimeState>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NapCat 插件实例的运行时状态:adapter create() 注册的 endpoint 列表。
|
|
3
|
+
* 由 plugin.ts setup() provide,adapter create 与 `napcat endpoint` 命令共享(同一 owner generation)。
|
|
4
|
+
*/
|
|
5
|
+
import { defineEndpointRuntimeStateToken } from 'zhin.js/adapter';
|
|
6
|
+
export const napcatRuntimeStateToken = defineEndpointRuntimeStateToken('napcat');
|
package/lib/onebot-get-msg.d.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export interface NapcatMessageLookup {
|
|
2
|
+
messageId: string;
|
|
3
|
+
sender?: {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
content: Array<{
|
|
8
|
+
type: string;
|
|
9
|
+
data?: Record<string, unknown>;
|
|
10
|
+
}>;
|
|
11
|
+
raw?: string;
|
|
12
|
+
time?: number;
|
|
13
|
+
}
|
|
14
|
+
export declare function parseOneBotGetMsgResponse(messageId: string, data: unknown): NapcatMessageLookup;
|
|
15
|
+
export declare function createNapCatContentPort(callApi: (action: string, params?: Record<string, unknown>) => Promise<unknown>): EndpointContentPort;
|
|
16
|
+
import type { EndpointContentPort } from 'zhin.js/adapter';
|