HerzaJ commited on
Commit
30991e5
·
verified ·
1 Parent(s): 4c63adf

Create haiku.js

Browse files
Files changed (1) hide show
  1. plugins/haiku.js +206 -0
plugins/haiku.js ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios');
2
+
3
+ const TEMP_MAIL_API = 'https://api.internal.temp-mail.io/api/v3';
4
+ const CHATAI_API = 'https://chataibot.pro/api';
5
+
6
+ const headers = {
7
+ 'Content-Type': 'application/json',
8
+ 'Accept-Language': 'en'
9
+ };
10
+
11
+ const tempMailHeaders = {
12
+ ...headers,
13
+ 'Application-Name': 'web',
14
+ 'Application-Version': '4.0.0',
15
+ 'X-CORS-Header': 'iaWg3pchvFx48fY'
16
+ };
17
+
18
+ const handler = async (req, res) => {
19
+ try {
20
+ const { text } = req.query;
21
+
22
+ if (!text) {
23
+ return res.status(400).json({
24
+ success: false,
25
+ error: 'Missing required parameter: text'
26
+ });
27
+ }
28
+
29
+ const result = await gpt4(text, chatId = null);
30
+ res.json({
31
+ author: "Herza",
32
+ success: true,
33
+ data: {
34
+ msg: result.response,
35
+ chatID: result.chatId
36
+ }
37
+ });
38
+
39
+ } catch (error) {
40
+ res.status(500).json({
41
+ success: false,
42
+ error: error.message
43
+ });
44
+ }
45
+ };
46
+
47
+ module.exports = {
48
+ name: 'Haiku Claude AIv3',
49
+ description: 'Generate responses using Haiku Anthropic Model v3',
50
+ type: 'GET',
51
+ routes: ['api/AI/haiku'],
52
+ tags: ['ai', 'Anthropic', 'Claude'],
53
+ main: ['AI'],
54
+ parameters: ['text', 'chatId', 'key'],
55
+ enabled: true,
56
+ handler
57
+ };
58
+
59
+ async function createTempEmail() {
60
+ const { data } = await axios.post(`${TEMP_MAIL_API}/email/new`, {
61
+ min_name_length: 10,
62
+ max_name_length: 10
63
+ }, { headers: tempMailHeaders });
64
+ return data;
65
+ }
66
+
67
+ async function registerAccount(email) {
68
+ const { data } = await axios.post(`${CHATAI_API}/register`, {
69
+ email,
70
+ password: 'Mxlineeecero5632@_++((+))9098+-+-!;::::#$',
71
+ isAdvertisingAccepted: false,
72
+ mainSiteUrl: 'https://chataibot.pro/api',
73
+ utmSource: '',
74
+ utmCampaign: '',
75
+ connectBusiness: ''
76
+ }, { headers });
77
+ return data;
78
+ }
79
+
80
+ async function getMessages(email) {
81
+ const { data } = await axios.get(`${TEMP_MAIL_API}/email/${email}/messages`, {
82
+ headers: tempMailHeaders
83
+ });
84
+ return data;
85
+ }
86
+
87
+ async function waitForVerificationCode(email, maxAttempts = 10) {
88
+ for (let i = 0; i < maxAttempts; i++) {
89
+ await new Promise(r => setTimeout(r, 3000));
90
+ const msgs = await getMessages(email);
91
+ const chatAiMsg = msgs.find(m => m.from.includes('chataibot.pro'));
92
+ if (chatAiMsg) {
93
+ const match = chatAiMsg.body_text.match(/Your code: (\d+)/);
94
+ if (match) return match[1];
95
+ }
96
+ }
97
+ throw new Error('Verification code not received');
98
+ }
99
+
100
+ async function verifyAccount(email, token) {
101
+ const { data, headers: responseHeaders } = await axios.post(`${CHATAI_API}/register/verify`, {
102
+ email,
103
+ token,
104
+ connectBusiness: ''
105
+ }, { headers });
106
+
107
+ const cookies = responseHeaders['set-cookie'] || [];
108
+ return { jwtToken: data.jwtToken, cookies };
109
+ }
110
+
111
+ async function createChat(jwtToken, cookies, title = 'New Chat') {
112
+ const cookieString = cookies.map(c => c.split(';')[0]).join('; ');
113
+
114
+ await axios.post(`${CHATAI_API}/message/change-context-model`, {
115
+ chatId: 0,
116
+ title,
117
+ isInternational: true
118
+ }, {
119
+ headers: {
120
+ ...headers,
121
+ 'Authorization': `Bearer ${jwtToken}`,
122
+ 'Cookie': cookieString
123
+ }
124
+ });
125
+
126
+ const { data } = await axios.post(`${CHATAI_API}/message/context`, {
127
+ title,
128
+ chatModel: 'claude-3-haiku'
129
+ }, {
130
+ headers: {
131
+ ...headers,
132
+ 'Authorization': `Bearer ${jwtToken}`,
133
+ 'Cookie': cookieString
134
+ }
135
+ });
136
+ return data.id;
137
+ }
138
+
139
+ async function sendMessage(jwtToken, cookies, text, chatId) {
140
+ const cookieString = cookies.map(c => c.split(';')[0]).join('; ');
141
+
142
+ const { data } = await axios.post(`${CHATAI_API}/message/streaming`, {
143
+ text,
144
+ chatId,
145
+ withPotentialQuestions: true,
146
+ linksToParse: []
147
+ }, {
148
+ headers: {
149
+ ...headers,
150
+ 'Authorization': `Bearer ${jwtToken}`,
151
+ 'Cookie': cookieString
152
+ },
153
+ responseType: 'text'
154
+ });
155
+
156
+ const lines = data.split('}{').map((line, i, arr) => {
157
+ if (i === 0) return line + '}';
158
+ if (i === arr.length - 1) return '{' + line;
159
+ return '{' + line + '}';
160
+ });
161
+
162
+ let result = '';
163
+ for (const line of lines) {
164
+ try {
165
+ const json = JSON.parse(line);
166
+ if (json.type === 'chunk') {
167
+ result += json.data;
168
+ } else if (json.type === 'finalResult') {
169
+ return json.data.mainText;
170
+ }
171
+ } catch (e) {}
172
+ }
173
+
174
+ return result;
175
+ }
176
+
177
+ let cachedToken = null;
178
+ let cachedCookies = null;
179
+ let cachedChatId = null;
180
+
181
+ async function gpt4(query, chatId = null) {
182
+ try {
183
+ if (!cachedToken) {
184
+ const { email } = await createTempEmail();
185
+ await registerAccount(email);
186
+ const code = await waitForVerificationCode(email);
187
+ const verifyResult = await verifyAccount(email, code);
188
+ cachedToken = verifyResult.jwtToken;
189
+ cachedCookies = verifyResult.cookies;
190
+ }
191
+
192
+ if (!chatId && !cachedChatId) {
193
+ cachedChatId = await createChat(cachedToken, cachedCookies, query);
194
+ }
195
+
196
+ const targetChatId = chatId || cachedChatId;
197
+ const response = await sendMessage(cachedToken, cachedCookies, query, targetChatId);
198
+
199
+ return { response, chatId: targetChatId };
200
+ } catch (err) {
201
+ cachedToken = null;
202
+ cachedCookies = null;
203
+ cachedChatId = null;
204
+ throw err;
205
+ }
206
+ }