@dorokuma/magpie-gemini-web 0.0.0-stage → 0.15.2
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/CHANGELOG.md +188 -0
- package/LICENSE +21 -0
- package/LICENSE-AISTUDIO +21 -0
- package/LICENSE-UPSTREAM +21 -0
- package/README.en.md +278 -0
- package/README.md +281 -2
- package/docs/backend-release.md +62 -0
- package/docs/logo.svg +16 -0
- package/docs/studio-release.md +312 -0
- package/index.mjs +285 -0
- package/lib/backend-release.json +31 -0
- package/lib/backend.mjs +731 -0
- package/lib/cite.mjs +166 -0
- package/lib/cleanup-detached.cjs +69 -0
- package/lib/cleanup.mjs +515 -0
- package/lib/native.mjs +734 -0
- package/lib/setup.mjs +261 -0
- package/lib/studio-connect-legacy.mjs +273 -0
- package/lib/studio-connect.mjs +269 -0
- package/lib/studio-native.mjs +2543 -0
- package/lib/studio-page.mjs +173 -0
- package/lib/studio-release.json +122 -0
- package/lib/studio-updates.mjs +320 -0
- package/lib/studio-upstream.mjs +135 -0
- package/lib/studio.mjs +1170 -0
- package/lib/usage.mjs +322 -0
- package/package.json +96 -4
- package/patches/studio-0.2.17-preserve-request-bodies.patch +190 -0
- package/patches/studio-0.2.19-managed-config.patch +121 -0
- package/patches/studio-0.3.4-magpie-signin.patch +676 -0
- package/patches/studio-0.3.5-signin-reauth.patch +25 -0
- package/patches/studio-0.3.7-magpie-signin.patch +676 -0
- package/patches/studio-0.3.7-managed-config.patch +121 -0
- package/patches/studio-0.3.7-signin-reauth.patch +25 -0
- package/scripts/backend-smoke.mjs +123 -0
- package/scripts/backend.mjs +57 -0
- package/scripts/studio-archive.go +92 -0
- package/scripts/studio-auto-update-smoke.mjs +116 -0
- package/scripts/studio-lifecycle-smoke.mjs +149 -0
- package/scripts/studio-managed-smoke.mjs +198 -0
- package/scripts/studio-official-smoke.mjs +12 -0
- package/scripts/studio-pins.mjs +52 -0
- package/scripts/studio-regzip.go +75 -0
- package/scripts/studio-release.mjs +287 -0
- package/scripts/studio-signin-candidate.json +102 -0
- package/scripts/studio-smoke.mjs +195 -0
- package/scripts/studio-update-smoke.mjs +82 -0
- package/scripts/studio-user-smoke.mjs +206 -0
- package/scripts/studio.mjs +468 -0
- package/scripts/sync-backend-pins.mjs +217 -0
package/index.mjs
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { normalizeBaseURL, settings, credentials, listModels, forward, UNREACHABLE_HEADER, PLUGIN_ICON } from './lib/backend.mjs';
|
|
2
|
+
import { ensureBackend, localBackend, localSetup } from './lib/native.mjs';
|
|
3
|
+
import { registerMagpieCleanup, isPluginRemovedInActiveMagpie } from './lib/cleanup.mjs';
|
|
4
|
+
import { parseCookieInput, parseAuthuser, importCookie, ensureProxy, validateProxyURL, requireLoginBackend } from './lib/setup.mjs';
|
|
5
|
+
import { usage, poolChoices, poolAccounts, accountEmail } from './lib/usage.mjs';
|
|
6
|
+
|
|
7
|
+
const ID = 'gemini-web';
|
|
8
|
+
const GEMINI_URL = 'https://gemini.google.com/app';
|
|
9
|
+
|
|
10
|
+
// Saved macOS Task sign-ins predate removal of the native bridge. Reject them here so their
|
|
11
|
+
// local key is not sent to the Gemini Web backend.
|
|
12
|
+
function retiredTask(auth) {
|
|
13
|
+
return auth?.type === 'api' && auth.metadata?.mode === 'task';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function retiredTaskError() {
|
|
17
|
+
return Object.assign(new Error('macOS 原生 Task 已停用,请改用 Gemini Web 登录'), { signIn: 'expired' });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// The backend keeps the session alive by rotating its cookies, which signs the browser it came
|
|
21
|
+
// from out of Google. A private window that is closed afterwards gives the backend a session of
|
|
22
|
+
// its own.
|
|
23
|
+
const COOKIE_STEPS = [
|
|
24
|
+
'请用浏览器的无痕 / 隐私窗口(推荐 Firefox),不要用你平时登录着 Google 的窗口:后端会定时刷新这份登录,和浏览器共用会被 Google 踢出登录。',
|
|
25
|
+
`1. 在无痕窗口打开 ${GEMINI_URL} 并登录要使用的 Google 账号`,
|
|
26
|
+
'确认这个窗口的目标账号索引与刚才填写的一致;无痕窗口中的索引可能和普通窗口不同。如地址是 /u/4/app,应填 4;填错请取消并重新登录。',
|
|
27
|
+
'2. 按 F12(macOS 按 ⌥⌘I)打开开发者工具,切到 Network(网络)面板,刷新页面',
|
|
28
|
+
'3. 点列表里任意一个 gemini.google.com 的请求 → Headers(标头)→ Request Headers(请求标头)',
|
|
29
|
+
'4. 找到 cookie 那一行,复制它的值,粘贴到下面',
|
|
30
|
+
'5. 直接关掉无痕窗口,不要点「退出登录」(退出会让这份 Cookie 失效)',
|
|
31
|
+
'Cookie 只保存在本机的 Gemini Web 后端里,不会发到别处。'
|
|
32
|
+
].join('\n');
|
|
33
|
+
|
|
34
|
+
// signInFor is the Magpie sign-in for one account of the local backend's pool: the backend's
|
|
35
|
+
// key, with the account's id for its usage card and its Google email, which names the card and
|
|
36
|
+
// keeps each Google account a sign-in of its own (Magpie takes two sign-ins with one email for
|
|
37
|
+
// one account, and two with neither for one when their key is the same). The account is the
|
|
38
|
+
// pool's own entry (see poolAccounts), which says the way out to read the email by; without
|
|
39
|
+
// one the email isn't read.
|
|
40
|
+
async function signInFor(local, account) {
|
|
41
|
+
const email = account.email || await accountEmail(account).catch(() => undefined);
|
|
42
|
+
return {
|
|
43
|
+
type: 'success',
|
|
44
|
+
key: local.apiKey,
|
|
45
|
+
metadata: {
|
|
46
|
+
baseURL: local.baseURL, account: account.id, ...(email ? { email } : {}),
|
|
47
|
+
// The account index (/u/N) the email is for; usage takes the email only while it holds.
|
|
48
|
+
...(email && account.authuser > 0 ? { authuser: account.authuser } : {})
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// managedBackend is the local backend this plugin set up, when baseURL points at it.
|
|
54
|
+
function managedBackend(baseURL, key) {
|
|
55
|
+
if (isPluginRemovedInActiveMagpie()) return null;
|
|
56
|
+
const local = localBackend() || (key ? localSetup() : null);
|
|
57
|
+
return local && local.baseURL === baseURL && (!key || local.apiKey === key) ? local : null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// forwardManaged is forward(), but when the backend this plugin manages can't be reached it
|
|
61
|
+
// starts the backend and tries once more.
|
|
62
|
+
async function forwardManaged(input, init, getAuth, opts) {
|
|
63
|
+
const retryInput = input instanceof Request ? input.clone() : input;
|
|
64
|
+
const res = await forward(input, init, getAuth, opts);
|
|
65
|
+
if (res.headers.get(UNREACHABLE_HEADER) !== 'unreachable') return res;
|
|
66
|
+
let c;
|
|
67
|
+
try {
|
|
68
|
+
c = credentials(await getAuth(), opts);
|
|
69
|
+
} catch {
|
|
70
|
+
return res;
|
|
71
|
+
}
|
|
72
|
+
if (!managedBackend(c.baseURL, c.key)) return res;
|
|
73
|
+
try {
|
|
74
|
+
await ensureBackend({ proxy: opts.proxy });
|
|
75
|
+
} catch {
|
|
76
|
+
return res;
|
|
77
|
+
}
|
|
78
|
+
return forward(retryInput, init, getAuth, opts);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const GeminiWebPlugin = async (_input, options = {}) => {
|
|
82
|
+
if (!options || typeof options !== 'object' || Array.isArray(options)) {
|
|
83
|
+
settings(options);
|
|
84
|
+
}
|
|
85
|
+
const { studio: _studio, ...webOptions } = options;
|
|
86
|
+
const opts = settings(webOptions);
|
|
87
|
+
registerMagpieCleanup(_input);
|
|
88
|
+
const proxyPrompt = {
|
|
89
|
+
type: 'text',
|
|
90
|
+
key: 'proxy',
|
|
91
|
+
message: '访问 Google 的代理地址(无需代理或已在代理池配置可留空)',
|
|
92
|
+
placeholder: opts.proxy || 'http://127.0.0.1:7890',
|
|
93
|
+
validate: value => {
|
|
94
|
+
try {
|
|
95
|
+
validateProxyURL(value);
|
|
96
|
+
} catch (e) {
|
|
97
|
+
return e.message;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const authuserPrompt = {
|
|
103
|
+
type: 'text',
|
|
104
|
+
key: 'authuser',
|
|
105
|
+
message: 'Google 账号索引(复制 Cookie 的同一窗口中 /u/N/app 的 N;单账号留空为 0,无痕窗口可能不同)',
|
|
106
|
+
placeholder: '0',
|
|
107
|
+
validate: value => {
|
|
108
|
+
try {
|
|
109
|
+
parseAuthuser(value);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
return e.message;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// Bring a backend set up earlier back after Magpie or the computer restarts. Not awaited:
|
|
117
|
+
// requests that arrive first start it themselves through forwardManaged.
|
|
118
|
+
if (localBackend()) {
|
|
119
|
+
ensureBackend({ proxy: opts.proxy })
|
|
120
|
+
.then(async local => {
|
|
121
|
+
if (!opts.proxy) return;
|
|
122
|
+
await requireLoginBackend(local);
|
|
123
|
+
await ensureProxy(local, opts.proxy);
|
|
124
|
+
})
|
|
125
|
+
.catch(err => console.error(`[gemini-web] could not start the local backend: ${err.message}`));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
config: async cfg => {
|
|
130
|
+
cfg.provider ??= {};
|
|
131
|
+
cfg.provider[ID] ??= { name: 'Gemini Web', npm: '@ai-sdk/openai-compatible', api: opts.baseURL, models: {} };
|
|
132
|
+
},
|
|
133
|
+
auth: {
|
|
134
|
+
provider: ID,
|
|
135
|
+
icon: PLUGIN_ICON,
|
|
136
|
+
// Two at once per sign-in, as a person with two Gemini tabs: an agent's first turn and
|
|
137
|
+
// the session title it asks for alongside no longer wait one behind the other, which
|
|
138
|
+
// Gemini Web can take minutes to answer (#33). The backend still caps each way out to
|
|
139
|
+
// Google (per-IP limits), and Magpie's own setting on the provider goes over this.
|
|
140
|
+
maxConcurrency: 2,
|
|
141
|
+
methods: [{
|
|
142
|
+
type: 'oauth',
|
|
143
|
+
label: '一键配置本地 Gemini Web(推荐)',
|
|
144
|
+
prompts: [proxyPrompt, authuserPrompt],
|
|
145
|
+
ask(inputs) {
|
|
146
|
+
if (!inputs || !('proxy' in inputs)) return proxyPrompt;
|
|
147
|
+
return 'authuser' in inputs ? null : authuserPrompt;
|
|
148
|
+
},
|
|
149
|
+
// Installs and starts the pinned backend while the user copies the Gemini cookie; the
|
|
150
|
+
// callback then waits for it, configures its proxy pool when a proxy URL is given, and
|
|
151
|
+
// imports the cookie (importCookie checks the backend can bind the account to a proxy).
|
|
152
|
+
// The result carries the backend's own API key, so Magpie saves an API-key sign-in,
|
|
153
|
+
// one for each Google account (see signInFor).
|
|
154
|
+
async authorize(inputs) {
|
|
155
|
+
const proxy = validateProxyURL(inputs?.proxy || opts.proxy || '');
|
|
156
|
+
const authuser = parseAuthuser(inputs?.authuser);
|
|
157
|
+
const loginURL = authuser ? `https://gemini.google.com/u/${authuser}/app` : GEMINI_URL;
|
|
158
|
+
const ready = ensureBackend({ proxy });
|
|
159
|
+
ready.catch(() => {});
|
|
160
|
+
return {
|
|
161
|
+
url: loginURL,
|
|
162
|
+
method: 'code',
|
|
163
|
+
instructions: COOKIE_STEPS.replaceAll(GEMINI_URL, loginURL),
|
|
164
|
+
async callback(code) {
|
|
165
|
+
let local, cookie, added;
|
|
166
|
+
try {
|
|
167
|
+
cookie = parseCookieInput(code);
|
|
168
|
+
local = await ready;
|
|
169
|
+
added = await importCookie(local, cookie, { authuser, ...(proxy ? { proxy } : {}) });
|
|
170
|
+
} catch (err) {
|
|
171
|
+
return { type: 'failed', error: err.message };
|
|
172
|
+
}
|
|
173
|
+
const account = (await poolAccounts().catch(() => [])).find(a => a.id === added.id);
|
|
174
|
+
return signInFor(local, account || { id: added.id });
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}, {
|
|
179
|
+
type: 'api',
|
|
180
|
+
label: '连接已有后端(自定义地址 + API key)',
|
|
181
|
+
placeholder: 'Backend API key (not a Google cookie)',
|
|
182
|
+
prompts: [{
|
|
183
|
+
type: 'text',
|
|
184
|
+
key: 'baseURL',
|
|
185
|
+
message: 'Backend OpenAI base URL (blank: plugin default)',
|
|
186
|
+
placeholder: opts.baseURL,
|
|
187
|
+
validate: value => {
|
|
188
|
+
try {
|
|
189
|
+
normalizeBaseURL(value || opts.baseURL);
|
|
190
|
+
} catch (e) {
|
|
191
|
+
return e.message;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}],
|
|
195
|
+
// Magpie stores raw prompt inputs as metadata unless authorize returns its own;
|
|
196
|
+
// write the resolved URL so a blank answer doesn't persist as baseURL: "".
|
|
197
|
+
authorize: async inputs => ({
|
|
198
|
+
type: 'success',
|
|
199
|
+
metadata: { baseURL: normalizeBaseURL(inputs?.baseURL || opts.baseURL) }
|
|
200
|
+
})
|
|
201
|
+
}, {
|
|
202
|
+
type: 'oauth',
|
|
203
|
+
label: '添加本地后端里已有的 Google 账号',
|
|
204
|
+
// Magpie asks through ask(), listing the pool as it is now; prompts holds the question
|
|
205
|
+
// for OpenCode, and makes Magpie hand authorize the answer.
|
|
206
|
+
prompts: [{ type: 'select', key: 'account', message: '选择 Google 账号', options: [] }],
|
|
207
|
+
async ask(inputs) {
|
|
208
|
+
if (inputs?.account) return null;
|
|
209
|
+
if (!localBackend()) throw new Error('还没有本地后端:请先用「一键配置本地 Gemini Web」');
|
|
210
|
+
const choices = await poolChoices();
|
|
211
|
+
if (!choices.length) throw new Error('本地后端里没有启用的 Google 账号');
|
|
212
|
+
return {
|
|
213
|
+
type: 'select',
|
|
214
|
+
key: 'account',
|
|
215
|
+
message: '选择 Google 账号',
|
|
216
|
+
placeholder: '',
|
|
217
|
+
options: choices.map(c => ({ label: c.label, value: String(c.id), hint: c.hint }))
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
async authorize(inputs) {
|
|
221
|
+
const local = localBackend();
|
|
222
|
+
if (!local) throw new Error('还没有本地后端:请先用「一键配置本地 Gemini Web」');
|
|
223
|
+
const id = Number(inputs?.account);
|
|
224
|
+
const chosen = (await poolChoices()).find(c => c.id === id);
|
|
225
|
+
if (!chosen) throw new Error(`本地后端里没有启用的账号 #${inputs?.account}`);
|
|
226
|
+
return {
|
|
227
|
+
url: '',
|
|
228
|
+
method: 'auto',
|
|
229
|
+
instructions: '',
|
|
230
|
+
callback: async () => signInFor(local, chosen)
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
}],
|
|
234
|
+
async loader(getAuth) {
|
|
235
|
+
const auth = await getAuth();
|
|
236
|
+
if (retiredTask(auth)) throw retiredTaskError();
|
|
237
|
+
if (auth?.type !== 'api') return {};
|
|
238
|
+
const c = credentials(auth, opts);
|
|
239
|
+
return {
|
|
240
|
+
apiKey: c.key,
|
|
241
|
+
baseURL: c.baseURL,
|
|
242
|
+
fetch: (input, init) => forwardManaged(input, init, getAuth, opts)
|
|
243
|
+
};
|
|
244
|
+
},
|
|
245
|
+
// Magpie's usage card: the 5-hour and weekly limits of each Google account in the local
|
|
246
|
+
// backend's pool, as gemini.google.com/usage shows them. A backend this plugin doesn't
|
|
247
|
+
// manage keeps its accounts out of reach. No read marks the sign-in: the Magpie key
|
|
248
|
+
// belongs to the backend, not to Google.
|
|
249
|
+
async usage(getAuth) {
|
|
250
|
+
const auth = await getAuth();
|
|
251
|
+
if (auth?.type !== 'api') return { error: 'Gemini Web 还没有登录', windows: [], signIn: 'kept' };
|
|
252
|
+
if (retiredTask(auth)) return { error: retiredTaskError().message, windows: [], signIn: 'expired' };
|
|
253
|
+
let c;
|
|
254
|
+
try {
|
|
255
|
+
c = credentials(auth, opts);
|
|
256
|
+
} catch (err) {
|
|
257
|
+
return { error: err.message, windows: [], signIn: 'kept' };
|
|
258
|
+
}
|
|
259
|
+
if (!managedBackend(c.baseURL, c.key)) {
|
|
260
|
+
return { error: '只能读取插件自己管理的本地后端的额度(连接已有后端时读不到)', windows: [], signIn: 'kept' };
|
|
261
|
+
}
|
|
262
|
+
const { account, email, authuser } = auth.metadata || {};
|
|
263
|
+
return { ...(await usage(Number.isInteger(account) ? { account, email, authuser } : {})), signIn: 'kept' };
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
provider: {
|
|
267
|
+
id: ID,
|
|
268
|
+
async models(provider, { auth } = {}) {
|
|
269
|
+
if (auth?.type !== 'api') return provider.models;
|
|
270
|
+
if (retiredTask(auth)) throw retiredTaskError();
|
|
271
|
+
const c = credentials(auth, opts);
|
|
272
|
+
try {
|
|
273
|
+
return await listModels(c, opts);
|
|
274
|
+
} catch (err) {
|
|
275
|
+
if (!err.unreachable || !managedBackend(c.baseURL, c.key)) throw err;
|
|
276
|
+
await ensureBackend({ proxy: opts.proxy });
|
|
277
|
+
return listModels(c, opts);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
export { AIStudioPlugin } from './lib/studio.mjs';
|
|
285
|
+
export default GeminiWebPlugin;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "5.0.0",
|
|
3
|
+
"repository": "dorokuma/gemini-web2api-go",
|
|
4
|
+
"tag": "v5.0.0",
|
|
5
|
+
"binaries": {
|
|
6
|
+
"darwin-x64": {
|
|
7
|
+
"asset": "gemini-web2api-go_v5.0.0_darwin_amd64",
|
|
8
|
+
"sha256": "852af30ffbcfd1402905f855e78d41d54a2f62313085aab420423ea0d855e2c1"
|
|
9
|
+
},
|
|
10
|
+
"darwin-arm64": {
|
|
11
|
+
"asset": "gemini-web2api-go_v5.0.0_darwin_arm64",
|
|
12
|
+
"sha256": "dfdcac8065483176fc42c6387014aa106cfe30175b94c393d7345f596eb26c7a"
|
|
13
|
+
},
|
|
14
|
+
"linux-x64": {
|
|
15
|
+
"asset": "gemini-web2api-go_v5.0.0_linux_amd64",
|
|
16
|
+
"sha256": "7a13f0e9c3003732eeb6346d16be191f486d1eaa035d61f1e8751ee287680e2a"
|
|
17
|
+
},
|
|
18
|
+
"linux-arm64": {
|
|
19
|
+
"asset": "gemini-web2api-go_v5.0.0_linux_arm64",
|
|
20
|
+
"sha256": "c0c468c7c8f85c0772a389586bbc92d7493e27e3b26e5baba0fc749abc427bfe"
|
|
21
|
+
},
|
|
22
|
+
"win32-x64": {
|
|
23
|
+
"asset": "gemini-web2api-go_v5.0.0_windows_amd64.exe",
|
|
24
|
+
"sha256": "08ce950afe2eda4a622f9c1560e578bdb13d00477ac9f20cd5bd62036a4345f4"
|
|
25
|
+
},
|
|
26
|
+
"win32-arm64": {
|
|
27
|
+
"asset": "gemini-web2api-go_v5.0.0_windows_arm64.exe",
|
|
28
|
+
"sha256": "fb36c3b457e6b372b6a528c19ede49637712d384b4623751174aba1d7d621f26"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|