@zhin.js/adapter-napcat 0.1.13 → 0.1.15
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 +36 -13
- package/README.md +121 -0
- package/client/NapCatManagement.tsx +113 -0
- package/client/index.tsx +11 -0
- package/client/tsconfig.json +7 -0
- package/client/utils/api.ts +30 -0
- package/dist/index.js +27 -0
- package/lib/adapter.d.ts +3 -0
- package/lib/adapter.d.ts.map +1 -1
- package/lib/adapter.js +5 -2
- package/lib/adapter.js.map +1 -1
- package/lib/agent-prompt.d.ts +3 -0
- package/lib/agent-prompt.d.ts.map +1 -0
- package/lib/agent-prompt.js +107 -0
- package/lib/agent-prompt.js.map +1 -0
- package/lib/bot-base.d.ts +6 -1
- package/lib/bot-base.d.ts.map +1 -1
- package/lib/bot-base.js +35 -1
- package/lib/bot-base.js.map +1 -1
- package/lib/bot-http.d.ts +2 -1
- package/lib/bot-http.d.ts.map +1 -1
- package/lib/bot-http.js +23 -1
- package/lib/bot-http.js.map +1 -1
- package/lib/bot-ws-client.d.ts +2 -0
- package/lib/bot-ws-client.d.ts.map +1 -1
- package/lib/bot-ws-client.js +32 -0
- package/lib/bot-ws-client.js.map +1 -1
- package/lib/bot-ws-server.d.ts +2 -1
- package/lib/bot-ws-server.d.ts.map +1 -1
- package/lib/bot-ws-server.js +14 -0
- package/lib/bot-ws-server.js.map +1 -1
- package/lib/index.d.ts +5 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +22 -1
- package/lib/index.js.map +1 -1
- package/lib/napcat-inbound.d.ts +21 -0
- package/lib/napcat-inbound.d.ts.map +1 -0
- package/lib/napcat-inbound.js +55 -0
- package/lib/napcat-inbound.js.map +1 -0
- package/lib/onebot-get-msg.d.ts +3 -0
- package/lib/onebot-get-msg.d.ts.map +1 -0
- package/lib/onebot-get-msg.js +27 -0
- package/lib/onebot-get-msg.js.map +1 -0
- package/lib/routes.d.ts +4 -0
- package/lib/routes.d.ts.map +1 -0
- package/lib/routes.js +61 -0
- package/lib/routes.js.map +1 -0
- package/lib/types.d.ts +18 -0
- package/lib/types.d.ts.map +1 -1
- package/lib/typing-indicator.d.ts +45 -0
- package/lib/typing-indicator.d.ts.map +1 -0
- package/lib/typing-indicator.js +132 -0
- package/lib/typing-indicator.js.map +1 -0
- package/package.json +22 -6
- package/plugin.yml +1 -1
- package/src/adapter.ts +3 -3
- package/src/agent-prompt.ts +115 -0
- package/src/bot-base.ts +36 -1
- package/src/bot-http.ts +19 -1
- package/src/bot-ws-client.ts +34 -0
- package/src/bot-ws-server.ts +16 -1
- package/src/index.ts +41 -2
- package/src/napcat-inbound.ts +57 -0
- package/src/onebot-get-msg.ts +33 -0
- package/src/routes.ts +61 -0
- package/src/types.ts +13 -0
- package/src/typing-indicator.ts +183 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NapCat Typing Indicator 集成
|
|
3
|
+
*
|
|
4
|
+
* 群聊:set_msg_emoji_like(表情回应)
|
|
5
|
+
* 私聊:set_input_status(输入状态)或短暂提示消息
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { NapCatBotBase } from './bot-base.js';
|
|
9
|
+
import {
|
|
10
|
+
TypingIndicatorManager,
|
|
11
|
+
ReactionTypingIndicatorAdapter,
|
|
12
|
+
type TypingIndicatorOptions,
|
|
13
|
+
type TypingIndicatorConfig,
|
|
14
|
+
type TypingIndicator,
|
|
15
|
+
} from '@zhin.js/agent';
|
|
16
|
+
|
|
17
|
+
export interface NapCatTypingIndicatorConfig {
|
|
18
|
+
enabled: boolean;
|
|
19
|
+
defaultEmoji: string;
|
|
20
|
+
autoRemove: boolean;
|
|
21
|
+
removeDelay: number;
|
|
22
|
+
privateConfig?: Partial<TypingIndicatorConfig>;
|
|
23
|
+
groupConfig?: Partial<TypingIndicatorConfig>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const DEFAULT_CONFIG: NapCatTypingIndicatorConfig = {
|
|
27
|
+
enabled: true,
|
|
28
|
+
defaultEmoji: '128516',
|
|
29
|
+
autoRemove: true,
|
|
30
|
+
removeDelay: 5000,
|
|
31
|
+
privateConfig: {
|
|
32
|
+
type: 'message',
|
|
33
|
+
message: '正在思考中...',
|
|
34
|
+
autoRemove: true,
|
|
35
|
+
removeDelay: 3000,
|
|
36
|
+
},
|
|
37
|
+
groupConfig: {
|
|
38
|
+
type: 'reaction',
|
|
39
|
+
emoji: '128516',
|
|
40
|
+
autoRemove: true,
|
|
41
|
+
removeDelay: 5000,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export class NapCatTypingIndicatorManager {
|
|
46
|
+
private manager: TypingIndicatorManager;
|
|
47
|
+
private config: NapCatTypingIndicatorConfig;
|
|
48
|
+
private bot: NapCatBotBase;
|
|
49
|
+
|
|
50
|
+
constructor(bot: NapCatBotBase, config: Partial<NapCatTypingIndicatorConfig> = {}) {
|
|
51
|
+
this.bot = bot;
|
|
52
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
53
|
+
this.manager = new TypingIndicatorManager({
|
|
54
|
+
type: 'reaction',
|
|
55
|
+
emoji: this.config.defaultEmoji,
|
|
56
|
+
autoRemove: this.config.autoRemove,
|
|
57
|
+
removeDelay: this.config.removeDelay,
|
|
58
|
+
});
|
|
59
|
+
this.registerAdapter();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private registerAdapter(): void {
|
|
63
|
+
const adapter = new ReactionTypingIndicatorAdapter(
|
|
64
|
+
async (messageId: string, emoji: string) => {
|
|
65
|
+
return await this.bot.$addReaction(messageId, emoji);
|
|
66
|
+
},
|
|
67
|
+
async (messageId: string, reactionId: string) => {
|
|
68
|
+
await this.bot.$removeReaction(messageId, reactionId);
|
|
69
|
+
},
|
|
70
|
+
async (options: TypingIndicatorOptions, content: string) => {
|
|
71
|
+
try {
|
|
72
|
+
if (options.sceneType === 'group' && options.groupId) {
|
|
73
|
+
return await this.bot.$sendMessage({
|
|
74
|
+
type: 'group',
|
|
75
|
+
id: options.groupId,
|
|
76
|
+
context: 'napcat',
|
|
77
|
+
bot: this.bot.$id,
|
|
78
|
+
content: [{ type: 'text', data: { text: content } }],
|
|
79
|
+
});
|
|
80
|
+
} else if (options.userId) {
|
|
81
|
+
return await this.bot.$sendMessage({
|
|
82
|
+
type: 'private',
|
|
83
|
+
id: options.userId,
|
|
84
|
+
context: 'napcat',
|
|
85
|
+
bot: this.bot.$id,
|
|
86
|
+
content: [{ type: 'text', data: { text: content } }],
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
async (messageId: string) => {
|
|
95
|
+
try {
|
|
96
|
+
await this.bot.$recallMessage(messageId);
|
|
97
|
+
} catch { /* ignore */ }
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
this.manager.registerAdapter(adapter);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async start(options: {
|
|
104
|
+
messageId?: string;
|
|
105
|
+
sessionId: string;
|
|
106
|
+
userId?: string;
|
|
107
|
+
groupId?: string;
|
|
108
|
+
sceneType: 'private' | 'group';
|
|
109
|
+
}): Promise<TypingIndicator> {
|
|
110
|
+
this.bot.logger.debug('[NapCat:TypingIndicator] start called', {
|
|
111
|
+
enabled: this.config.enabled,
|
|
112
|
+
sceneType: options.sceneType,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (!this.config.enabled) {
|
|
116
|
+
return {
|
|
117
|
+
start: async () => {},
|
|
118
|
+
stop: async () => {},
|
|
119
|
+
isActive: () => false,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const config = options.sceneType === 'group'
|
|
124
|
+
? this.config.groupConfig
|
|
125
|
+
: this.config.privateConfig;
|
|
126
|
+
|
|
127
|
+
const typingOptions: TypingIndicatorOptions = {
|
|
128
|
+
messageId: options.messageId,
|
|
129
|
+
sessionId: options.sessionId,
|
|
130
|
+
userId: options.userId,
|
|
131
|
+
groupId: options.groupId,
|
|
132
|
+
platform: 'napcat',
|
|
133
|
+
botId: this.bot.$id,
|
|
134
|
+
sceneType: options.sceneType,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
return await this.manager.start(typingOptions, config);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async stop(options: {
|
|
141
|
+
sessionId: string;
|
|
142
|
+
userId?: string;
|
|
143
|
+
groupId?: string;
|
|
144
|
+
}): Promise<void> {
|
|
145
|
+
const typingOptions: TypingIndicatorOptions = {
|
|
146
|
+
sessionId: options.sessionId,
|
|
147
|
+
userId: options.userId,
|
|
148
|
+
groupId: options.groupId,
|
|
149
|
+
platform: 'napcat',
|
|
150
|
+
botId: this.bot.$id,
|
|
151
|
+
sceneType: 'private',
|
|
152
|
+
};
|
|
153
|
+
await this.manager.stop(typingOptions);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async stopAll(): Promise<void> {
|
|
157
|
+
await this.manager.stopAll();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
updateConfig(config: Partial<NapCatTypingIndicatorConfig>): void {
|
|
161
|
+
this.config = { ...this.config, ...config };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
getConfig(): NapCatTypingIndicatorConfig {
|
|
165
|
+
return { ...this.config };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
declare module './bot-base.js' {
|
|
170
|
+
interface NapCatBotBase {
|
|
171
|
+
$typingIndicator?: NapCatTypingIndicatorManager;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function enableTypingIndicator(
|
|
176
|
+
bot: NapCatBotBase,
|
|
177
|
+
config?: Partial<NapCatTypingIndicatorConfig>,
|
|
178
|
+
): NapCatTypingIndicatorManager {
|
|
179
|
+
if (!(bot as any).$typingIndicator) {
|
|
180
|
+
(bot as any).$typingIndicator = new NapCatTypingIndicatorManager(bot, config);
|
|
181
|
+
}
|
|
182
|
+
return (bot as any).$typingIndicator;
|
|
183
|
+
}
|