@tuturuuu/ai 0.0.12 → 0.1.0
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/package.json +20 -19
- package/src/chat-sdk/zalo-personal.ts +507 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tuturuuu/ai",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/tutur3u/platform",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"type-check": "tsgo --project tsconfig.typecheck.json"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@ai-sdk/amazon-bedrock": "^4.0.
|
|
25
|
+
"@ai-sdk/amazon-bedrock": "^4.0.113",
|
|
26
26
|
"@ai-sdk/anthropic": "^3.0.81",
|
|
27
|
-
"@ai-sdk/azure": "^3.0.
|
|
27
|
+
"@ai-sdk/azure": "^3.0.70",
|
|
28
28
|
"@ai-sdk/cerebras": "^2.0.53",
|
|
29
29
|
"@ai-sdk/cohere": "^3.0.36",
|
|
30
30
|
"@ai-sdk/deepgram": "^2.0.33",
|
|
@@ -33,19 +33,19 @@
|
|
|
33
33
|
"@ai-sdk/elevenlabs": "^2.0.33",
|
|
34
34
|
"@ai-sdk/fal": "^2.0.34",
|
|
35
35
|
"@ai-sdk/fireworks": "^2.0.52",
|
|
36
|
-
"@ai-sdk/gateway": "^3.0.
|
|
36
|
+
"@ai-sdk/gateway": "^3.0.125",
|
|
37
37
|
"@ai-sdk/gladia": "^2.0.33",
|
|
38
38
|
"@ai-sdk/google": "^3.0.80",
|
|
39
|
-
"@ai-sdk/google-vertex": "^4.0.
|
|
39
|
+
"@ai-sdk/google-vertex": "^4.0.142",
|
|
40
40
|
"@ai-sdk/groq": "^3.0.39",
|
|
41
41
|
"@ai-sdk/hume": "^2.0.33",
|
|
42
42
|
"@ai-sdk/lmnt": "^2.0.33",
|
|
43
43
|
"@ai-sdk/luma": "^2.0.33",
|
|
44
44
|
"@ai-sdk/mistral": "^3.0.37",
|
|
45
|
-
"@ai-sdk/openai": "^3.0.
|
|
45
|
+
"@ai-sdk/openai": "^3.0.68",
|
|
46
46
|
"@ai-sdk/openai-compatible": "^2.0.47",
|
|
47
47
|
"@ai-sdk/perplexity": "^3.0.33",
|
|
48
|
-
"@ai-sdk/react": "^3.0.
|
|
48
|
+
"@ai-sdk/react": "^3.0.199",
|
|
49
49
|
"@ai-sdk/replicate": "^2.0.33",
|
|
50
50
|
"@ai-sdk/revai": "^2.0.33",
|
|
51
51
|
"@ai-sdk/togetherai": "^2.0.52",
|
|
@@ -68,23 +68,23 @@
|
|
|
68
68
|
"@chat-adapter/whatsapp": "^4.30.0",
|
|
69
69
|
"@json-render/core": "^0.19.0",
|
|
70
70
|
"@json-render/react": "^0.19.0",
|
|
71
|
-
"@liveblocks/chat-sdk-adapter": "^3.19.
|
|
71
|
+
"@liveblocks/chat-sdk-adapter": "^3.19.4",
|
|
72
72
|
"@octokit/rest": "^22.0.1",
|
|
73
73
|
"@resend/chat-sdk-adapter": "^0.2.2",
|
|
74
74
|
"@streamdown/cjk": "^1.0.3",
|
|
75
75
|
"@streamdown/code": "^1.1.1",
|
|
76
76
|
"@streamdown/math": "^1.0.2",
|
|
77
77
|
"@streamdown/mermaid": "^1.0.2",
|
|
78
|
-
"@tuturuuu/google": "
|
|
79
|
-
"@tuturuuu/internal-api": "
|
|
80
|
-
"@tuturuuu/supabase": "
|
|
81
|
-
"@tuturuuu/utils": "
|
|
82
|
-
"@vercel/sandbox": "^2.1.
|
|
78
|
+
"@tuturuuu/google": "0.0.1",
|
|
79
|
+
"@tuturuuu/internal-api": "0.2.1",
|
|
80
|
+
"@tuturuuu/supabase": "0.3.1",
|
|
81
|
+
"@tuturuuu/utils": "0.3.1",
|
|
82
|
+
"@vercel/sandbox": "^2.1.1",
|
|
83
83
|
"@zernio/chat-sdk-adapter": "^0.3.0",
|
|
84
|
-
"ai": "^6.0.
|
|
84
|
+
"ai": "^6.0.197",
|
|
85
85
|
"bash-tool": "^1.3.17",
|
|
86
86
|
"chat": "^4.30.0",
|
|
87
|
-
"chat-adapter-baileys": "^2.0.
|
|
87
|
+
"chat-adapter-baileys": "^2.0.3",
|
|
88
88
|
"chat-adapter-blooio": "^0.1.0",
|
|
89
89
|
"chat-adapter-imessage": "^0.1.1",
|
|
90
90
|
"chat-adapter-mattermost": "^1.1.3",
|
|
@@ -99,14 +99,15 @@
|
|
|
99
99
|
"react-dom": "^19.2.7",
|
|
100
100
|
"streamdown": "^2.5.0",
|
|
101
101
|
"uuid": "^14.0.0",
|
|
102
|
+
"zca-js": "^2.1.2",
|
|
102
103
|
"zod": "^4.4.3"
|
|
103
104
|
},
|
|
104
105
|
"devDependencies": {
|
|
105
|
-
"@tuturuuu/types": "
|
|
106
|
-
"@tuturuuu/typescript-config": "
|
|
107
|
-
"@types/node": "^25.9.
|
|
106
|
+
"@tuturuuu/types": "0.4.1",
|
|
107
|
+
"@tuturuuu/typescript-config": "0.1.1",
|
|
108
|
+
"@types/node": "^25.9.2",
|
|
108
109
|
"@types/qrcode": "^1.5.6",
|
|
109
|
-
"@types/react": "^19.2.
|
|
110
|
+
"@types/react": "^19.2.17",
|
|
110
111
|
"typescript": "^6.0.3"
|
|
111
112
|
},
|
|
112
113
|
"exports": {
|
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Adapter,
|
|
3
|
+
type AdapterPostableMessage,
|
|
4
|
+
type ChatInstance,
|
|
5
|
+
type FetchOptions,
|
|
6
|
+
type FetchResult,
|
|
7
|
+
type FormattedContent,
|
|
8
|
+
Message,
|
|
9
|
+
parseMarkdown,
|
|
10
|
+
type RawMessage,
|
|
11
|
+
type StreamChunk,
|
|
12
|
+
type StreamOptions,
|
|
13
|
+
stringifyMarkdown,
|
|
14
|
+
type ThreadInfo,
|
|
15
|
+
type UserInfo,
|
|
16
|
+
} from 'chat';
|
|
17
|
+
import {
|
|
18
|
+
type API,
|
|
19
|
+
type Credentials,
|
|
20
|
+
ThreadType,
|
|
21
|
+
Zalo,
|
|
22
|
+
type Message as ZcaMessage,
|
|
23
|
+
} from 'zca-js';
|
|
24
|
+
|
|
25
|
+
export interface ZaloPersonalAdapterConfig {
|
|
26
|
+
channelId: string;
|
|
27
|
+
cookieJson: string;
|
|
28
|
+
displayName: string;
|
|
29
|
+
imei: string;
|
|
30
|
+
language?: string;
|
|
31
|
+
ownId?: string | null;
|
|
32
|
+
userAgent: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ZaloPersonalStatus {
|
|
36
|
+
connected: boolean;
|
|
37
|
+
lastError: string | null;
|
|
38
|
+
lastEventAt: string | null;
|
|
39
|
+
ownId: string | null;
|
|
40
|
+
running: boolean;
|
|
41
|
+
startedAt: string | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ZaloPersonalThreadRef {
|
|
45
|
+
externalThreadId: string;
|
|
46
|
+
threadType: ThreadType;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ZaloPersonalSentRaw {
|
|
50
|
+
externalThreadId: string;
|
|
51
|
+
id: string;
|
|
52
|
+
isSelf: true;
|
|
53
|
+
response: unknown;
|
|
54
|
+
text: string;
|
|
55
|
+
threadId: string;
|
|
56
|
+
threadType: ThreadType;
|
|
57
|
+
ts: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type ZaloPersonalRawMessage = ZcaMessage | ZaloPersonalSentRaw;
|
|
61
|
+
|
|
62
|
+
export type ZaloPersonalAdapter = Adapter<
|
|
63
|
+
ZaloPersonalThreadRef,
|
|
64
|
+
ZaloPersonalRawMessage
|
|
65
|
+
> & {
|
|
66
|
+
getPersonalStatus(): ZaloPersonalStatus;
|
|
67
|
+
startPersonalListener(): Promise<ZaloPersonalStatus>;
|
|
68
|
+
stopPersonalListener(): Promise<ZaloPersonalStatus>;
|
|
69
|
+
validateLogin(): Promise<ZaloPersonalStatus>;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const THREAD_ID_PREFIX = 'zalo-personal';
|
|
73
|
+
|
|
74
|
+
export function parseZaloPersonalCookieJson(
|
|
75
|
+
cookieJson: string
|
|
76
|
+
): Credentials['cookie'] {
|
|
77
|
+
const parsed = JSON.parse(cookieJson) as unknown;
|
|
78
|
+
|
|
79
|
+
if (Array.isArray(parsed)) {
|
|
80
|
+
return parsed as Credentials['cookie'];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (isRecord(parsed) && Array.isArray(parsed.cookies)) {
|
|
84
|
+
return parsed as Credentials['cookie'];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
throw new Error('zalo_personal_cookie_json_invalid');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function createZaloPersonalAdapter(
|
|
91
|
+
config: ZaloPersonalAdapterConfig
|
|
92
|
+
): ZaloPersonalAdapter {
|
|
93
|
+
let api: API | null = null;
|
|
94
|
+
let chat: ChatInstance | null = null;
|
|
95
|
+
let listenersAttached = false;
|
|
96
|
+
let status: ZaloPersonalStatus = {
|
|
97
|
+
connected: false,
|
|
98
|
+
lastError: null,
|
|
99
|
+
lastEventAt: null,
|
|
100
|
+
ownId: config.ownId?.trim() || null,
|
|
101
|
+
running: false,
|
|
102
|
+
startedAt: null,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
function setStatus(update: Partial<ZaloPersonalStatus>) {
|
|
106
|
+
status = { ...status, ...update };
|
|
107
|
+
return status;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function encodeThreadId(ref: ZaloPersonalThreadRef) {
|
|
111
|
+
return [
|
|
112
|
+
THREAD_ID_PREFIX,
|
|
113
|
+
config.channelId,
|
|
114
|
+
ref.threadType === ThreadType.Group ? 'group' : 'user',
|
|
115
|
+
ref.externalThreadId,
|
|
116
|
+
].join(':');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function decodeThreadId(threadId: string): ZaloPersonalThreadRef {
|
|
120
|
+
const [prefix, channelId, rawType, ...idParts] = threadId.split(':');
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
prefix !== THREAD_ID_PREFIX ||
|
|
124
|
+
channelId !== config.channelId ||
|
|
125
|
+
!rawType ||
|
|
126
|
+
idParts.length === 0
|
|
127
|
+
) {
|
|
128
|
+
return {
|
|
129
|
+
externalThreadId: threadId,
|
|
130
|
+
threadType: ThreadType.User,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
externalThreadId: idParts.join(':'),
|
|
136
|
+
threadType: rawType === 'group' ? ThreadType.Group : ThreadType.User,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function messageThreadId(raw: ZcaMessage) {
|
|
141
|
+
return encodeThreadId({
|
|
142
|
+
externalThreadId: raw.threadId,
|
|
143
|
+
threadType: raw.type,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function connect() {
|
|
148
|
+
if (api) return api;
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
if (!config.cookieJson.trim() || !config.imei.trim()) {
|
|
152
|
+
throw new Error('zalo_personal_credentials_missing');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const zalo = new Zalo();
|
|
156
|
+
api = await zalo.login({
|
|
157
|
+
cookie: parseZaloPersonalCookieJson(config.cookieJson),
|
|
158
|
+
imei: config.imei,
|
|
159
|
+
language: config.language,
|
|
160
|
+
userAgent: config.userAgent,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const ownId = api.getOwnId();
|
|
164
|
+
setStatus({
|
|
165
|
+
connected: true,
|
|
166
|
+
lastError: null,
|
|
167
|
+
ownId: ownId || status.ownId,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return api;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
setStatus({
|
|
173
|
+
connected: false,
|
|
174
|
+
lastError: error instanceof Error ? error.message : String(error),
|
|
175
|
+
running: false,
|
|
176
|
+
});
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function attachListeners(apiInstance: API) {
|
|
182
|
+
if (listenersAttached) return;
|
|
183
|
+
|
|
184
|
+
apiInstance.listener.on('connected', () => {
|
|
185
|
+
setStatus({
|
|
186
|
+
connected: true,
|
|
187
|
+
lastError: null,
|
|
188
|
+
running: true,
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
apiInstance.listener.on('disconnected', (_code, reason) => {
|
|
192
|
+
setStatus({
|
|
193
|
+
connected: false,
|
|
194
|
+
lastError: reason || null,
|
|
195
|
+
running: false,
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
apiInstance.listener.on('closed', (_code, reason) => {
|
|
199
|
+
setStatus({
|
|
200
|
+
connected: false,
|
|
201
|
+
lastError: reason || null,
|
|
202
|
+
running: false,
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
apiInstance.listener.on('error', (error) => {
|
|
206
|
+
setStatus({
|
|
207
|
+
lastError: error instanceof Error ? error.message : String(error),
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
apiInstance.listener.on('message', (message) => {
|
|
211
|
+
void handleIncomingMessage(message);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
listenersAttached = true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function handleIncomingMessage(raw: ZcaMessage) {
|
|
218
|
+
if (raw.isSelf || typeof raw.data.content !== 'string') {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const sdkMessage = adapter.parseMessage(raw);
|
|
223
|
+
setStatus({ lastEventAt: new Date().toISOString() });
|
|
224
|
+
|
|
225
|
+
await chat?.processMessage(adapter, sdkMessage.threadId, sdkMessage, {
|
|
226
|
+
waitUntil: (task) => {
|
|
227
|
+
void task.catch((error) => {
|
|
228
|
+
setStatus({
|
|
229
|
+
lastError: error instanceof Error ? error.message : String(error),
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function unsupported(feature: string) {
|
|
237
|
+
return new Error(`zalo_personal_${feature}_unsupported`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const adapter: ZaloPersonalAdapter = {
|
|
241
|
+
addReaction: async () => {
|
|
242
|
+
throw unsupported('reaction');
|
|
243
|
+
},
|
|
244
|
+
channelIdFromThreadId: (_threadId) => config.channelId,
|
|
245
|
+
decodeThreadId,
|
|
246
|
+
deleteMessage: async () => {
|
|
247
|
+
throw unsupported('delete');
|
|
248
|
+
},
|
|
249
|
+
disconnect: async () => {
|
|
250
|
+
api?.listener.stop();
|
|
251
|
+
api = null;
|
|
252
|
+
listenersAttached = false;
|
|
253
|
+
setStatus({
|
|
254
|
+
connected: false,
|
|
255
|
+
running: false,
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
editMessage: async () => {
|
|
259
|
+
throw unsupported('edit');
|
|
260
|
+
},
|
|
261
|
+
encodeThreadId,
|
|
262
|
+
fetchMessages: async (
|
|
263
|
+
threadId,
|
|
264
|
+
options?: FetchOptions
|
|
265
|
+
): Promise<FetchResult<ZaloPersonalRawMessage>> => {
|
|
266
|
+
const apiInstance = await connect();
|
|
267
|
+
const thread = decodeThreadId(threadId);
|
|
268
|
+
|
|
269
|
+
if (thread.threadType !== ThreadType.Group) {
|
|
270
|
+
return { messages: [] };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const result = await apiInstance.getGroupChatHistory(
|
|
274
|
+
thread.externalThreadId,
|
|
275
|
+
options?.limit ?? 50
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const messages = result.groupMsgs
|
|
279
|
+
.map((message) => adapter.parseMessage(message))
|
|
280
|
+
.sort(
|
|
281
|
+
(a, b) =>
|
|
282
|
+
a.metadata.dateSent.getTime() - b.metadata.dateSent.getTime()
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
return { messages };
|
|
286
|
+
},
|
|
287
|
+
fetchThread: async (threadId): Promise<ThreadInfo> => {
|
|
288
|
+
const thread = decodeThreadId(threadId);
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
channelId: config.channelId,
|
|
292
|
+
channelName: config.displayName,
|
|
293
|
+
id: threadId,
|
|
294
|
+
isDM: thread.threadType === ThreadType.User,
|
|
295
|
+
metadata: {
|
|
296
|
+
accountMode: 'personal',
|
|
297
|
+
externalThreadId: thread.externalThreadId,
|
|
298
|
+
threadType: thread.threadType === ThreadType.Group ? 'group' : 'user',
|
|
299
|
+
},
|
|
300
|
+
};
|
|
301
|
+
},
|
|
302
|
+
getPersonalStatus: () => status,
|
|
303
|
+
getUser: async (userId): Promise<UserInfo | null> => ({
|
|
304
|
+
fullName: userId,
|
|
305
|
+
isBot: false,
|
|
306
|
+
userId,
|
|
307
|
+
userName: userId,
|
|
308
|
+
}),
|
|
309
|
+
handleWebhook: async () =>
|
|
310
|
+
Response.json(
|
|
311
|
+
{ error: 'Personal Zalo channels use a listener, not webhooks.' },
|
|
312
|
+
{ status: 404 }
|
|
313
|
+
),
|
|
314
|
+
initialize: async (instance) => {
|
|
315
|
+
chat = instance;
|
|
316
|
+
},
|
|
317
|
+
isDM: (threadId) => decodeThreadId(threadId).threadType === ThreadType.User,
|
|
318
|
+
lockScope: 'thread',
|
|
319
|
+
name: 'zalo',
|
|
320
|
+
openDM: async (userId) =>
|
|
321
|
+
encodeThreadId({
|
|
322
|
+
externalThreadId: userId,
|
|
323
|
+
threadType: ThreadType.User,
|
|
324
|
+
}),
|
|
325
|
+
parseMessage: (raw) => {
|
|
326
|
+
if (isSentRaw(raw)) {
|
|
327
|
+
return new Message<ZaloPersonalRawMessage>({
|
|
328
|
+
attachments: [],
|
|
329
|
+
author: {
|
|
330
|
+
fullName: config.displayName,
|
|
331
|
+
isBot: true,
|
|
332
|
+
isMe: true,
|
|
333
|
+
userId: status.ownId ?? 'zalo-personal-self',
|
|
334
|
+
userName: config.displayName,
|
|
335
|
+
},
|
|
336
|
+
formatted: parseMarkdown(raw.text),
|
|
337
|
+
id: raw.id,
|
|
338
|
+
metadata: {
|
|
339
|
+
dateSent: new Date(raw.ts),
|
|
340
|
+
edited: false,
|
|
341
|
+
},
|
|
342
|
+
raw,
|
|
343
|
+
text: raw.text,
|
|
344
|
+
threadId: raw.threadId,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const text =
|
|
349
|
+
typeof raw.data.content === 'string'
|
|
350
|
+
? raw.data.content
|
|
351
|
+
: '[Unsupported Zalo message]';
|
|
352
|
+
const authorId =
|
|
353
|
+
raw.data.uidFrom || raw.data.userId || (raw.isSelf ? status.ownId : '');
|
|
354
|
+
|
|
355
|
+
return new Message<ZaloPersonalRawMessage>({
|
|
356
|
+
attachments: [],
|
|
357
|
+
author: {
|
|
358
|
+
fullName: raw.data.dName || authorId || 'Zalo user',
|
|
359
|
+
isBot: raw.isSelf,
|
|
360
|
+
isMe: raw.isSelf,
|
|
361
|
+
userId: authorId || raw.threadId,
|
|
362
|
+
userName: raw.data.dName || authorId || raw.threadId,
|
|
363
|
+
},
|
|
364
|
+
formatted: parseMarkdown(text),
|
|
365
|
+
id: raw.data.msgId || raw.data.cliMsgId || `${raw.data.ts}`,
|
|
366
|
+
metadata: {
|
|
367
|
+
dateSent: dateFromZaloTimestamp(raw.data.ts),
|
|
368
|
+
edited: false,
|
|
369
|
+
},
|
|
370
|
+
raw,
|
|
371
|
+
text,
|
|
372
|
+
threadId: messageThreadId(raw),
|
|
373
|
+
});
|
|
374
|
+
},
|
|
375
|
+
postMessage: async (threadId, message) => {
|
|
376
|
+
const apiInstance = await connect();
|
|
377
|
+
const thread = decodeThreadId(threadId);
|
|
378
|
+
const text = extractPostableText(message);
|
|
379
|
+
const response = await apiInstance.sendMessage(
|
|
380
|
+
{ msg: text },
|
|
381
|
+
thread.externalThreadId,
|
|
382
|
+
thread.threadType
|
|
383
|
+
);
|
|
384
|
+
const id = String(
|
|
385
|
+
response.message?.msgId ??
|
|
386
|
+
response.attachment.at(0)?.msgId ??
|
|
387
|
+
Date.now()
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
id,
|
|
392
|
+
raw: {
|
|
393
|
+
externalThreadId: thread.externalThreadId,
|
|
394
|
+
id,
|
|
395
|
+
isSelf: true,
|
|
396
|
+
response,
|
|
397
|
+
text,
|
|
398
|
+
threadId,
|
|
399
|
+
threadType: thread.threadType,
|
|
400
|
+
ts: Date.now(),
|
|
401
|
+
},
|
|
402
|
+
threadId,
|
|
403
|
+
};
|
|
404
|
+
},
|
|
405
|
+
removeReaction: async () => {
|
|
406
|
+
throw unsupported('reaction');
|
|
407
|
+
},
|
|
408
|
+
renderFormatted: (content: FormattedContent) => stringifyMarkdown(content),
|
|
409
|
+
startPersonalListener: async () => {
|
|
410
|
+
const apiInstance = await connect();
|
|
411
|
+
attachListeners(apiInstance);
|
|
412
|
+
apiInstance.listener.start({ retryOnClose: true });
|
|
413
|
+
|
|
414
|
+
return setStatus({
|
|
415
|
+
connected: true,
|
|
416
|
+
lastError: null,
|
|
417
|
+
running: true,
|
|
418
|
+
startedAt: status.startedAt ?? new Date().toISOString(),
|
|
419
|
+
});
|
|
420
|
+
},
|
|
421
|
+
startTyping: async (threadId) => {
|
|
422
|
+
const apiInstance = await connect();
|
|
423
|
+
const thread = decodeThreadId(threadId);
|
|
424
|
+
await apiInstance.sendTypingEvent(
|
|
425
|
+
thread.externalThreadId,
|
|
426
|
+
thread.threadType
|
|
427
|
+
);
|
|
428
|
+
},
|
|
429
|
+
stopPersonalListener: async () => {
|
|
430
|
+
api?.listener.stop();
|
|
431
|
+
|
|
432
|
+
return setStatus({
|
|
433
|
+
connected: false,
|
|
434
|
+
running: false,
|
|
435
|
+
});
|
|
436
|
+
},
|
|
437
|
+
stream: async (
|
|
438
|
+
threadId,
|
|
439
|
+
textStream: AsyncIterable<string | StreamChunk>,
|
|
440
|
+
_options?: StreamOptions
|
|
441
|
+
): Promise<RawMessage<ZaloPersonalRawMessage> | null> => {
|
|
442
|
+
let text = '';
|
|
443
|
+
|
|
444
|
+
for await (const chunk of textStream) {
|
|
445
|
+
if (typeof chunk === 'string') {
|
|
446
|
+
text += chunk;
|
|
447
|
+
} else if (chunk.type === 'markdown_text') {
|
|
448
|
+
text += chunk.text;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return adapter.postMessage(threadId, text.trim() || 'Done.');
|
|
453
|
+
},
|
|
454
|
+
userName: config.displayName,
|
|
455
|
+
validateLogin: async () => {
|
|
456
|
+
const apiInstance = await connect();
|
|
457
|
+
|
|
458
|
+
return setStatus({
|
|
459
|
+
connected: true,
|
|
460
|
+
lastError: null,
|
|
461
|
+
ownId: apiInstance.getOwnId() || status.ownId,
|
|
462
|
+
});
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
return adapter;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function dateFromZaloTimestamp(value: string) {
|
|
470
|
+
const numeric = Number.parseInt(value, 10);
|
|
471
|
+
|
|
472
|
+
if (!Number.isFinite(numeric)) {
|
|
473
|
+
return new Date();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return new Date(numeric < 1_000_000_000_000 ? numeric * 1000 : numeric);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function extractPostableText(message: AdapterPostableMessage) {
|
|
480
|
+
if (typeof message === 'string') {
|
|
481
|
+
return message;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (isRecord(message)) {
|
|
485
|
+
if (typeof message.markdown === 'string') {
|
|
486
|
+
return message.markdown;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
if (typeof message.text === 'string') {
|
|
490
|
+
return message.text;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (isRecord(message.ast)) {
|
|
494
|
+
return stringifyMarkdown(message.ast as unknown as FormattedContent);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return String(message);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
502
|
+
return typeof value === 'object' && value !== null;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function isSentRaw(value: unknown): value is ZaloPersonalSentRaw {
|
|
506
|
+
return isRecord(value) && value.isSelf === true && 'response' in value;
|
|
507
|
+
}
|