@cloudbase/agent-adapter-wx 0.0.18
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/README.md +221 -0
- package/dist/index.d.mts +700 -0
- package/dist/index.d.ts +700 -0
- package/dist/index.js +1257 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1199 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# @cloudbase/agent-adapter-wx
|
|
2
|
+
|
|
3
|
+
WeChat adapter for AG-Kit - Forward LLM messages to WeChat platforms
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
✅ **Multi-Platform Support**: WeChat Mini Program, Official Account (Service/Subscription), Enterprise WeChat Customer Service
|
|
8
|
+
✅ **Automatic Token Management**: Built-in caching and refresh mechanism
|
|
9
|
+
✅ **Type-Safe**: Full TypeScript support with Zod validation
|
|
10
|
+
✅ **Simple API**: Easy-to-use interface for sending messages
|
|
11
|
+
✅ **Batch Operations**: Send multiple messages at once
|
|
12
|
+
✅ **Async Support**: Fire-and-forget message sending
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @cloudbase/agent-adapter-wx
|
|
18
|
+
# or
|
|
19
|
+
yarn add @cloudbase/agent-adapter-wx
|
|
20
|
+
# or
|
|
21
|
+
pnpm add @cloudbase/agent-adapter-wx
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { WeChatSender, WeChatPlatform } from "@cloudbase/agent-adapter-wx";
|
|
28
|
+
|
|
29
|
+
// Initialize sender
|
|
30
|
+
const sender = new WeChatSender({
|
|
31
|
+
platform: WeChatPlatform.MINI_APP,
|
|
32
|
+
appId: "your-app-id",
|
|
33
|
+
appSecret: "your-app-secret",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Send a message
|
|
37
|
+
await sender.send({
|
|
38
|
+
toUser: "user-openid",
|
|
39
|
+
message: {
|
|
40
|
+
content: "Hello from LLM!",
|
|
41
|
+
type: "text",
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage Examples
|
|
47
|
+
|
|
48
|
+
### Send LLM Response to WeChat
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { WeChatSender, WeChatPlatform } from "@cloudbase/agent-adapter-wx";
|
|
52
|
+
|
|
53
|
+
// Initialize for WeChat Mini Program
|
|
54
|
+
const sender = new WeChatSender({
|
|
55
|
+
platform: WeChatPlatform.MINI_APP,
|
|
56
|
+
appId: process.env.WECHAT_APP_ID!,
|
|
57
|
+
appSecret: process.env.WECHAT_APP_SECRET!,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Get response from any LLM (OpenAI, Claude, etc.)
|
|
61
|
+
const llmResponse = await getLLMResponse("What is the weather today?");
|
|
62
|
+
|
|
63
|
+
// Send to WeChat
|
|
64
|
+
await sender.send({
|
|
65
|
+
toUser: "user-openid",
|
|
66
|
+
message: {
|
|
67
|
+
content: llmResponse.content,
|
|
68
|
+
type: "text",
|
|
69
|
+
},
|
|
70
|
+
recommendQuestions: [
|
|
71
|
+
"What about tomorrow?",
|
|
72
|
+
"Show me the forecast",
|
|
73
|
+
"Temperature details",
|
|
74
|
+
],
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Enterprise WeChat Customer Service
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const sender = new WeChatSender({
|
|
82
|
+
platform: WeChatPlatform.CUSTOM_SERVICE,
|
|
83
|
+
appId: "your-corp-id",
|
|
84
|
+
appSecret: "your-corp-secret",
|
|
85
|
+
openKfId: "your-kf-id",
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
await sender.send({
|
|
89
|
+
toUser: "user-openid",
|
|
90
|
+
message: {
|
|
91
|
+
content: "Your AI assistant response",
|
|
92
|
+
type: "text",
|
|
93
|
+
},
|
|
94
|
+
msgId: "original-message-id",
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Batch Sending
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const messages = [
|
|
102
|
+
{
|
|
103
|
+
toUser: "user1",
|
|
104
|
+
message: { content: "Message 1", type: "text" as const },
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
toUser: "user2",
|
|
108
|
+
message: { content: "Message 2", type: "text" as const },
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
const results = await sender.sendBatch(messages);
|
|
113
|
+
console.log(results);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Async Sending (Fire and Forget)
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// Send without waiting for response
|
|
120
|
+
sender.sendAsync({
|
|
121
|
+
toUser: "user-openid",
|
|
122
|
+
message: {
|
|
123
|
+
content: "Notification message",
|
|
124
|
+
type: "text",
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## API Reference
|
|
130
|
+
|
|
131
|
+
### WeChatSender
|
|
132
|
+
|
|
133
|
+
#### Constructor
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
new WeChatSender(config: WeChatConfig)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### Methods
|
|
140
|
+
|
|
141
|
+
- `send(options: SendMessageOptions, originMsg?: any): Promise<WeChatAPIResponse>`
|
|
142
|
+
- `sendBatch(messages: SendMessageOptions[]): Promise<WeChatAPIResponse[]>`
|
|
143
|
+
- `sendAsync(options: SendMessageOptions, originMsg?: any): Promise<void>`
|
|
144
|
+
- `clearCache(): void`
|
|
145
|
+
- `getConfig(): WeChatConfig`
|
|
146
|
+
|
|
147
|
+
### Types
|
|
148
|
+
|
|
149
|
+
#### WeChatPlatform
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
enum WeChatPlatform {
|
|
153
|
+
CUSTOM_SERVICE = "WXCustomerService",
|
|
154
|
+
MINI_APP = "WXMiniapp",
|
|
155
|
+
SERVICE = "WXService",
|
|
156
|
+
SUBSCRIPTION = "WXSubscription",
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### WeChatConfig
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
interface WeChatConfig {
|
|
164
|
+
platform: WeChatPlatform;
|
|
165
|
+
appId: string;
|
|
166
|
+
appSecret: string;
|
|
167
|
+
openKfId?: string;
|
|
168
|
+
tokenCacheTTL?: number; // default: 7200000 (2 hours)
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
#### SendMessageOptions
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
interface SendMessageOptions {
|
|
176
|
+
toUser: string;
|
|
177
|
+
message: {
|
|
178
|
+
content: string;
|
|
179
|
+
type: "text" | "image" | "event" | "voice";
|
|
180
|
+
imageUrl?: string;
|
|
181
|
+
};
|
|
182
|
+
recommendQuestions?: string[];
|
|
183
|
+
msgId?: string;
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Platform-Specific Notes
|
|
188
|
+
|
|
189
|
+
### WeChat Mini Program
|
|
190
|
+
- Uses customer service message API
|
|
191
|
+
- Requires user to interact with mini program first
|
|
192
|
+
|
|
193
|
+
### Official Account (Service/Subscription)
|
|
194
|
+
- Uses customer service message API
|
|
195
|
+
- User must follow the account
|
|
196
|
+
|
|
197
|
+
### Enterprise WeChat Customer Service
|
|
198
|
+
- Requires `openKfId` configuration
|
|
199
|
+
- Supports message threading with `msgId`
|
|
200
|
+
|
|
201
|
+
## Error Handling
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
try {
|
|
205
|
+
await sender.send({
|
|
206
|
+
toUser: "user-openid",
|
|
207
|
+
message: { content: "Hello", type: "text" },
|
|
208
|
+
});
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.error("Failed to send message:", error);
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
ISC
|
|
217
|
+
|
|
218
|
+
## Contributing
|
|
219
|
+
|
|
220
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|
|
221
|
+
|