-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
124 lines (98 loc) · 4.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { User } from "@/cache/user";
import { client } from "@/source/client";
import { extractNumber, getDataFromMail } from "@/lib/email";
import { verifyToken, type TokenData } from "@/lib/encryption";
import { getPromptForQRImages, readQRCode } from "@/lib/image";
import { tryAsync, trys } from "@/lib/utils";
import { getResponse } from "@/source/model";
import { generateTools } from "@/tools/index";
import { getUserInfo, system } from "@/tools/sjcet";
import Events from "@/data/event.json";
import { convertChat } from "./lib/chat";
import { getTranscript } from "./source/voice";
import { MessageQueues } from "./cache/queue";
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message_create', async (message) => {
if (message.fromMe) return;
if (message.isStatus) return;
const oneHourInSeconds = 3600;
const currentTimestamp = Math.floor(Date.now() / 1000);
if (currentTimestamp - message.timestamp > oneHourInSeconds) return
const { number, isUser } = await message.getContact()
if (!isUser) return
const { data: validatedNumber, error: numberError } = trys(() => extractNumber(number))
if (numberError) return
const { data: user, error: userError } = await tryAsync(async () => await User.get(validatedNumber))
// if (userError) return
const role = user?.data.role ?? "NA"
const chat = await message.getChat();
const MSQ = new MessageQueues(validatedNumber)
if (message.hasMedia && message.type === "image") {
const media = await message.downloadMedia();
switch (media.mimetype) {
case "image/jpeg":
case "image/png": {
await message.reply(
MessageQueues.getToolText("getQRfromImage")
);
const { error, data } = await readQRCode(media.data)
if (error) {
await message.reply(
MessageQueues.getSystemText("Unable to identify any QR patterns from the image")
)
return
}
const { data: info, error: infoE } = verifyToken<TokenData>(data)
if (infoE) {
await message.reply(
MessageQueues.getSystemText("Invalid QR code image (unabled to verify token)")
);
return
}
const user = getDataFromMail(info.email)
if (!user.data) {
await message.reply(
MessageQueues.getSystemText("User data from QR code image is not valid")
);
await message.reply(`\`\`\`\n${JSON.stringify(info, null, 2)}\n\`\`\``);
return
}
const { prompt, system } = getPromptForQRImages(user, info)
const res = await getResponse(prompt, system)
await message.reply(res);
}
}
return
}
await MSQ.updateStatus(user?.data)
chat.sendSeen();
chat.sendStateTyping();
console.log("L:", await chat.getLabels())
if (message.hasMedia && (message.type === "audio" || message.type === "ptt")) {
const audio = await message.downloadMedia();
await message.reply(MessageQueues.getToolText("getTranscript"))
const text = await getTranscript(audio.data)
if (text) {
await message.reply(MessageQueues.getSystemText(`Transcription: ${text}`))
}
}
MSQ.addMessage(await chat.fetchMessages({ limit:10 }))
const AITools = await generateTools({ chat, role, number: validatedNumber }, user ?? undefined)
const messages = MSQ.coreMessages
console.log("M:", messages)
AITools({ messages })
.then(async reply => {
await MSQ.sentTools(reply)
await Promise.all(MSQ.texts.map((m, i) => message.reply(m)))
})
.catch(err => {
console.error(err);
message.reply("An error occurred while processing your request. Please try again later.");
})
.finally(() => {
chat.clearState();
})
});
client.initialize();