-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathBardBot.js
More file actions
209 lines (196 loc) · 4.18 KB
/
Copy pathBardBot.js
File metadata and controls
209 lines (196 loc) · 4.18 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import axios from "axios";
import Bot from "../Bot";
import AsyncLock from "async-lock";
function parseResponse(resp) {
let data = JSON.parse(resp.split("\n")[3]);
data = JSON.parse(data[0][2]);
if (!data) {
throw new Error("Failed to parse Bard response");
}
const ids = [...data[1], data[4][0][0]];
let text = data[4][0][1][0];
const images = data[4][0][4];
if (images) {
images.forEach((image) => {
const url = image[0][0][0];
const alt = image[0][4];
const link = image[1][0][0];
const placeholder = image[2];
text = text.replace(
placeholder,
`[](${link} "${link}")`,
);
});
}
return { text, ids };
}
function generateReq(model, prompt, contextIds) {
let modelNumber = model == "gemini-ultra" ? 2 : 1;
// The JSON is ugly and meaningless, but it works
let innerJSON = [
[prompt, 0, null, null, null, null, 0],
["en"],
contextIds,
"",
"",
null,
[1],
0,
null,
null,
1,
0,
null,
null,
null,
null,
null,
null,
modelNumber,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
[],
];
return JSON.stringify([null, JSON.stringify(innerJSON)]);
}
export default class BardBot extends Bot {
static _brandId = "bard";
static _className = "BardBot"; // Class name of the bot
static _model = "gemini-pro"; // gemini-pro or gemini-ultra
static _logoFilename = "gemini-chat-logo.svg"; // Place it in public/bots/
static _loginUrl = "https://gemini.google.com/";
// Remove Electron from the user agent to avoid blank login screen
static _userAgent =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) chatall/1.29.40 Chrome/114.0.5735.134 Safari/537.36";
static _lock = new AsyncLock();
async _checkAvailability() {
const context = await this.getChatContext();
let available = false;
if (context.requestParams.atValue) {
available = true;
}
return available;
}
async _sendPrompt(prompt, onUpdateResponse, callbackParam) {
const context = await this.getChatContext();
return new Promise((resolve, reject) => {
const { requestParams, contextIds } = context;
axios
.post(
"https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate",
new URLSearchParams({
at: requestParams.atValue,
"f.req": generateReq(this.constructor._model, prompt, contextIds),
}),
{
params: {
bl: requestParams.blValue,
_reqid: Math.floor(Math.random() * 900000) + 100000,
rt: "c",
},
},
)
.then(({ data: resp }) => {
const { text, ids } = parseResponse(resp);
context.contextIds = ids;
this.setChatContext(context);
onUpdateResponse(callbackParam, { content: text, done: true });
resolve();
})
.catch((error) => {
reject(error);
});
});
}
async createChatContext() {
const resp = await axios.get("https://gemini.google.com/app");
const atValue = resp.data.match(/"SNlM0e":"([^"]+)"/)?.[1];
const blValue = resp.data.match(/"cfb2h":"([^"]+)"/)?.[1];
if (!atValue || !blValue) {
throw new Error("Failed to fetch Bard at/bl values");
}
return {
requestParams: { atValue, blValue },
contextIds: ["", "", ""],
};
}
}