@kenkaiiii/gg-core 4.4.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/LICENSE +21 -0
- package/dist/chunk-TZNVRILI.js +24 -0
- package/dist/chunk-TZNVRILI.js.map +1 -0
- package/dist/chunk-USAVZGPP.js +284 -0
- package/dist/chunk-USAVZGPP.js.map +1 -0
- package/dist/index.cjs +1988 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +295 -0
- package/dist/index.d.ts +295 -0
- package/dist/index.js +1635 -0
- package/dist/index.js.map +1 -0
- package/dist/model-registry.cjs +315 -0
- package/dist/model-registry.cjs.map +1 -0
- package/dist/model-registry.d.cts +57 -0
- package/dist/model-registry.d.ts +57 -0
- package/dist/model-registry.js +21 -0
- package/dist/model-registry.js.map +1 -0
- package/dist/paths.cjs +58 -0
- package/dist/paths.cjs.map +1 -0
- package/dist/paths.d.cts +16 -0
- package/dist/paths.d.ts +16 -0
- package/dist/paths.js +7 -0
- package/dist/paths.js.map +1 -0
- package/package.json +55 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1635 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MODELS,
|
|
3
|
+
getContextWindow,
|
|
4
|
+
getDefaultModel,
|
|
5
|
+
getMaxThinkingLevel,
|
|
6
|
+
getModel,
|
|
7
|
+
getModelsForProvider,
|
|
8
|
+
getSummaryModel,
|
|
9
|
+
usesOpenAICodexTransport
|
|
10
|
+
} from "./chunk-USAVZGPP.js";
|
|
11
|
+
import {
|
|
12
|
+
getAppPaths
|
|
13
|
+
} from "./chunk-TZNVRILI.js";
|
|
14
|
+
|
|
15
|
+
// src/thinking-level.ts
|
|
16
|
+
var OPENAI_GPT_THINKING_LEVELS = ["medium", "high", "xhigh"];
|
|
17
|
+
var ANTHROPIC_OPUS_48_47_THINKING_LEVELS = [
|
|
18
|
+
"low",
|
|
19
|
+
"medium",
|
|
20
|
+
"high",
|
|
21
|
+
"xhigh",
|
|
22
|
+
"max"
|
|
23
|
+
];
|
|
24
|
+
var ANTHROPIC_ADAPTIVE_THINKING_LEVELS = [
|
|
25
|
+
"low",
|
|
26
|
+
"medium",
|
|
27
|
+
"high",
|
|
28
|
+
"max"
|
|
29
|
+
];
|
|
30
|
+
function isOpenAIGptModel(provider, model) {
|
|
31
|
+
return provider === "openai" && model.startsWith("gpt-");
|
|
32
|
+
}
|
|
33
|
+
function isAnthropicOpus48Or47Model(provider, model) {
|
|
34
|
+
return provider === "anthropic" && /opus-4-8|opus-4-7/.test(model);
|
|
35
|
+
}
|
|
36
|
+
function isAnthropicAdaptiveModel(provider, model) {
|
|
37
|
+
return provider === "anthropic" && /opus-4-8|opus-4-7|opus-4-6|sonnet-4-6/.test(model);
|
|
38
|
+
}
|
|
39
|
+
function getSupportedThinkingLevels(provider, model) {
|
|
40
|
+
const maxLevel = getMaxThinkingLevel(model);
|
|
41
|
+
if (isAnthropicAdaptiveModel(provider, model)) {
|
|
42
|
+
const levels = isAnthropicOpus48Or47Model(provider, model) ? ANTHROPIC_OPUS_48_47_THINKING_LEVELS : ANTHROPIC_ADAPTIVE_THINKING_LEVELS;
|
|
43
|
+
const maxIndex2 = levels.indexOf(maxLevel);
|
|
44
|
+
if (maxIndex2 === -1) return ["low", "medium", "high"];
|
|
45
|
+
return levels.slice(0, maxIndex2 + 1);
|
|
46
|
+
}
|
|
47
|
+
if (!isOpenAIGptModel(provider, model)) return [maxLevel];
|
|
48
|
+
const maxIndex = OPENAI_GPT_THINKING_LEVELS.indexOf(maxLevel);
|
|
49
|
+
if (maxIndex === -1) return ["medium"];
|
|
50
|
+
return OPENAI_GPT_THINKING_LEVELS.slice(0, maxIndex + 1);
|
|
51
|
+
}
|
|
52
|
+
function isThinkingLevelSupported(provider, model, level) {
|
|
53
|
+
return getSupportedThinkingLevels(provider, model).includes(level);
|
|
54
|
+
}
|
|
55
|
+
function getNextThinkingLevel(provider, model, current) {
|
|
56
|
+
const supportedLevels = getSupportedThinkingLevels(provider, model);
|
|
57
|
+
const shouldCycleLevels = isOpenAIGptModel(provider, model) || isAnthropicAdaptiveModel(provider, model);
|
|
58
|
+
if (!shouldCycleLevels) {
|
|
59
|
+
return current ? void 0 : supportedLevels[0];
|
|
60
|
+
}
|
|
61
|
+
if (!current) return supportedLevels[0];
|
|
62
|
+
const index = supportedLevels.indexOf(current);
|
|
63
|
+
if (index === -1) return supportedLevels[0];
|
|
64
|
+
return supportedLevels[index + 1];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/logger.ts
|
|
68
|
+
import fs from "fs";
|
|
69
|
+
import path from "path";
|
|
70
|
+
import { randomBytes } from "crypto";
|
|
71
|
+
var MAX_BYTES = 10 * 1024 * 1024;
|
|
72
|
+
var fd = null;
|
|
73
|
+
var sessionId = "";
|
|
74
|
+
var appName = "app";
|
|
75
|
+
var cleanups = [];
|
|
76
|
+
function rotateIfNeeded(filePath) {
|
|
77
|
+
try {
|
|
78
|
+
const st = fs.statSync(filePath);
|
|
79
|
+
if (st.size < MAX_BYTES) return;
|
|
80
|
+
const rotated = `${filePath}.1`;
|
|
81
|
+
try {
|
|
82
|
+
fs.unlinkSync(rotated);
|
|
83
|
+
} catch {
|
|
84
|
+
}
|
|
85
|
+
fs.renameSync(filePath, rotated);
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function openLog(filePath, name) {
|
|
90
|
+
if (fd !== null) return false;
|
|
91
|
+
appName = name;
|
|
92
|
+
try {
|
|
93
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 448 });
|
|
94
|
+
} catch {
|
|
95
|
+
}
|
|
96
|
+
rotateIfNeeded(filePath);
|
|
97
|
+
try {
|
|
98
|
+
fd = fs.openSync(filePath, "a");
|
|
99
|
+
} catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
sessionId = randomBytes(4).toString("hex");
|
|
103
|
+
try {
|
|
104
|
+
fs.writeSync(fd, "\n");
|
|
105
|
+
} catch {
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
function getSessionId() {
|
|
110
|
+
return sessionId;
|
|
111
|
+
}
|
|
112
|
+
function isLoggerOpen() {
|
|
113
|
+
return fd !== null;
|
|
114
|
+
}
|
|
115
|
+
function log(level, category, message, data) {
|
|
116
|
+
if (fd === null) return;
|
|
117
|
+
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
118
|
+
let line = `[${ts}] [sid=${sessionId}] [${level}] [${category}] ${message}`;
|
|
119
|
+
if (data) {
|
|
120
|
+
const pairs = Object.entries(data).map(([k, v]) => `${k}=${typeof v === "string" ? v : JSON.stringify(v)}`).join(" ");
|
|
121
|
+
if (pairs) line += ` ${pairs}`;
|
|
122
|
+
}
|
|
123
|
+
line += "\n";
|
|
124
|
+
try {
|
|
125
|
+
fs.writeSync(fd, line);
|
|
126
|
+
} catch {
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function registerLogCleanup(fn) {
|
|
130
|
+
cleanups.push(fn);
|
|
131
|
+
}
|
|
132
|
+
function closeLogger(opts) {
|
|
133
|
+
if (fd === null) return;
|
|
134
|
+
if (opts?.shutdownLine !== false) log("INFO", "shutdown", `${appName} shutting down`);
|
|
135
|
+
try {
|
|
136
|
+
fs.closeSync(fd);
|
|
137
|
+
} catch {
|
|
138
|
+
}
|
|
139
|
+
fd = null;
|
|
140
|
+
for (const unsub of cleanups) unsub();
|
|
141
|
+
cleanups = [];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/file-lock.ts
|
|
145
|
+
import fs2 from "fs/promises";
|
|
146
|
+
import { setTimeout as setTimeout2 } from "timers/promises";
|
|
147
|
+
var STALE_TIMEOUT_MS = 1e4;
|
|
148
|
+
var RETRY_INTERVAL_MS = 50;
|
|
149
|
+
var MAX_WAIT_MS = 5e3;
|
|
150
|
+
async function withFileLock(filePath, fn) {
|
|
151
|
+
const lockPath = filePath + ".lock";
|
|
152
|
+
await acquireLock(lockPath);
|
|
153
|
+
try {
|
|
154
|
+
return await fn();
|
|
155
|
+
} finally {
|
|
156
|
+
await releaseLock(lockPath);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async function acquireLock(lockPath) {
|
|
160
|
+
const startTime = Date.now();
|
|
161
|
+
while (true) {
|
|
162
|
+
try {
|
|
163
|
+
const info = { pid: process.pid, timestamp: Date.now() };
|
|
164
|
+
await fs2.writeFile(lockPath, JSON.stringify(info), { flag: "wx" });
|
|
165
|
+
return;
|
|
166
|
+
} catch (err) {
|
|
167
|
+
if (err.code !== "EEXIST") throw err;
|
|
168
|
+
try {
|
|
169
|
+
const content = await fs2.readFile(lockPath, "utf-8");
|
|
170
|
+
const info = JSON.parse(content);
|
|
171
|
+
const isProcessAlive = isAlive(info.pid);
|
|
172
|
+
const isStale = Date.now() - info.timestamp > STALE_TIMEOUT_MS;
|
|
173
|
+
if (!isProcessAlive || isStale) {
|
|
174
|
+
await fs2.unlink(lockPath).catch(() => {
|
|
175
|
+
});
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
} catch {
|
|
179
|
+
await fs2.unlink(lockPath).catch(() => {
|
|
180
|
+
});
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (Date.now() - startTime > MAX_WAIT_MS) {
|
|
184
|
+
await fs2.unlink(lockPath).catch(() => {
|
|
185
|
+
});
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
await setTimeout2(RETRY_INTERVAL_MS);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async function releaseLock(lockPath) {
|
|
193
|
+
await fs2.unlink(lockPath).catch(() => {
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
function isAlive(pid) {
|
|
197
|
+
try {
|
|
198
|
+
process.kill(pid, 0);
|
|
199
|
+
return true;
|
|
200
|
+
} catch (err) {
|
|
201
|
+
if (err.code === "EPERM") return true;
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/claude-code-version.ts
|
|
207
|
+
import fs3 from "fs/promises";
|
|
208
|
+
import path2 from "path";
|
|
209
|
+
var NPM_LATEST_URL = "https://registry.npmjs.org/@anthropic-ai/claude-code/latest";
|
|
210
|
+
var CACHE_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
211
|
+
var FETCH_TIMEOUT_MS = 3e3;
|
|
212
|
+
var FALLBACK_VERSION = "2.1.88";
|
|
213
|
+
var memoryCache = null;
|
|
214
|
+
var inflight = null;
|
|
215
|
+
function cachePath() {
|
|
216
|
+
return path2.join(getAppPaths().agentDir, "claude-code-version.json");
|
|
217
|
+
}
|
|
218
|
+
async function readDiskCache() {
|
|
219
|
+
try {
|
|
220
|
+
const raw = await fs3.readFile(cachePath(), "utf-8");
|
|
221
|
+
const parsed = JSON.parse(raw);
|
|
222
|
+
if (typeof parsed.version === "string" && typeof parsed.fetchedAt === "number") {
|
|
223
|
+
return parsed;
|
|
224
|
+
}
|
|
225
|
+
return null;
|
|
226
|
+
} catch {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async function writeDiskCache(data) {
|
|
231
|
+
try {
|
|
232
|
+
await fs3.mkdir(getAppPaths().agentDir, { recursive: true, mode: 448 });
|
|
233
|
+
await fs3.writeFile(cachePath(), JSON.stringify(data), { mode: 384 });
|
|
234
|
+
} catch (err) {
|
|
235
|
+
log(
|
|
236
|
+
"WARN",
|
|
237
|
+
"claude-code-version",
|
|
238
|
+
`Failed to write cache: ${err instanceof Error ? err.message : String(err)}`
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async function fetchLatest() {
|
|
243
|
+
const controller = new AbortController();
|
|
244
|
+
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
245
|
+
try {
|
|
246
|
+
const response = await fetch(NPM_LATEST_URL, { signal: controller.signal });
|
|
247
|
+
if (!response.ok) return null;
|
|
248
|
+
const data = await response.json();
|
|
249
|
+
if (typeof data.version === "string" && /^\d/.test(data.version)) {
|
|
250
|
+
return data.version;
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
} catch {
|
|
254
|
+
return null;
|
|
255
|
+
} finally {
|
|
256
|
+
clearTimeout(timer);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async function getClaudeCodeVersion() {
|
|
260
|
+
if (memoryCache && Date.now() < memoryCache.expiresAt) {
|
|
261
|
+
return memoryCache.version;
|
|
262
|
+
}
|
|
263
|
+
if (inflight) return inflight;
|
|
264
|
+
inflight = (async () => {
|
|
265
|
+
const disk = await readDiskCache();
|
|
266
|
+
const diskFresh = disk && Date.now() - disk.fetchedAt < CACHE_TTL_MS;
|
|
267
|
+
if (disk && diskFresh) {
|
|
268
|
+
memoryCache = { version: disk.version, expiresAt: Date.now() + CACHE_TTL_MS };
|
|
269
|
+
return disk.version;
|
|
270
|
+
}
|
|
271
|
+
const fetched = await fetchLatest();
|
|
272
|
+
if (fetched) {
|
|
273
|
+
await writeDiskCache({ version: fetched, fetchedAt: Date.now() });
|
|
274
|
+
memoryCache = { version: fetched, expiresAt: Date.now() + CACHE_TTL_MS };
|
|
275
|
+
return fetched;
|
|
276
|
+
}
|
|
277
|
+
const resolved = disk?.version ?? FALLBACK_VERSION;
|
|
278
|
+
memoryCache = { version: resolved, expiresAt: Date.now() + 5 * 60 * 1e3 };
|
|
279
|
+
log(
|
|
280
|
+
"WARN",
|
|
281
|
+
"claude-code-version",
|
|
282
|
+
`Failed to fetch latest Claude Code version; using ${resolved}`
|
|
283
|
+
);
|
|
284
|
+
return resolved;
|
|
285
|
+
})();
|
|
286
|
+
try {
|
|
287
|
+
return await inflight;
|
|
288
|
+
} finally {
|
|
289
|
+
inflight = null;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async function getClaudeCliUserAgent() {
|
|
293
|
+
const version = await getClaudeCodeVersion();
|
|
294
|
+
return `claude-cli/${version} (external, cli)`;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/auth-storage.ts
|
|
298
|
+
import fs4 from "fs/promises";
|
|
299
|
+
import crypto5 from "crypto";
|
|
300
|
+
|
|
301
|
+
// src/oauth/anthropic.ts
|
|
302
|
+
import crypto2 from "crypto";
|
|
303
|
+
|
|
304
|
+
// src/oauth/pkce.ts
|
|
305
|
+
function base64urlEncode(bytes) {
|
|
306
|
+
let binary = "";
|
|
307
|
+
for (const byte of bytes) {
|
|
308
|
+
binary += String.fromCharCode(byte);
|
|
309
|
+
}
|
|
310
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
311
|
+
}
|
|
312
|
+
async function generatePKCE() {
|
|
313
|
+
const verifierBytes = new Uint8Array(32);
|
|
314
|
+
crypto.getRandomValues(verifierBytes);
|
|
315
|
+
const verifier = base64urlEncode(verifierBytes);
|
|
316
|
+
const data = new TextEncoder().encode(verifier);
|
|
317
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
318
|
+
const challenge = base64urlEncode(new Uint8Array(hashBuffer));
|
|
319
|
+
return { verifier, challenge };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/oauth/anthropic.ts
|
|
323
|
+
var CLIENT_ID = atob("OWQxYzI1MGEtZTYxYi00NGQ5LTg4ZWQtNTk0NGQxOTYyZjVl");
|
|
324
|
+
var AUTHORIZE_URL = "https://claude.ai/oauth/authorize";
|
|
325
|
+
var TOKEN_URLS = [
|
|
326
|
+
"https://platform.claude.com/v1/oauth/token",
|
|
327
|
+
"https://console.anthropic.com/v1/oauth/token"
|
|
328
|
+
];
|
|
329
|
+
var REDIRECT_URI = "https://platform.claude.com/oauth/code/callback";
|
|
330
|
+
var SCOPES = "org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload";
|
|
331
|
+
async function postTokenRequest(body, label) {
|
|
332
|
+
const encoded = JSON.stringify(body);
|
|
333
|
+
const headers = {
|
|
334
|
+
"Content-Type": "application/json",
|
|
335
|
+
"User-Agent": await getClaudeCliUserAgent(),
|
|
336
|
+
"anthropic-beta": "oauth-2025-04-20"
|
|
337
|
+
};
|
|
338
|
+
let lastError = null;
|
|
339
|
+
for (const url of TOKEN_URLS) {
|
|
340
|
+
let response;
|
|
341
|
+
try {
|
|
342
|
+
response = await fetch(url, { method: "POST", headers, body: encoded });
|
|
343
|
+
} catch (err) {
|
|
344
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (response.ok) {
|
|
348
|
+
return await response.json();
|
|
349
|
+
}
|
|
350
|
+
const text = await response.text();
|
|
351
|
+
if (response.status >= 400 && response.status < 500) {
|
|
352
|
+
throw new Error(`Anthropic ${label} failed (${response.status}): ${text}`);
|
|
353
|
+
}
|
|
354
|
+
lastError = new Error(`Anthropic ${label} failed (${response.status}): ${text}`);
|
|
355
|
+
}
|
|
356
|
+
throw lastError ?? new Error(`Anthropic ${label} failed: all endpoints unreachable`);
|
|
357
|
+
}
|
|
358
|
+
function toCredentials(data) {
|
|
359
|
+
return {
|
|
360
|
+
accessToken: data.access_token,
|
|
361
|
+
refreshToken: data.refresh_token,
|
|
362
|
+
expiresAt: Date.now() + data.expires_in * 1e3 - 5 * 60 * 1e3
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
async function loginAnthropic(callbacks) {
|
|
366
|
+
const { verifier, challenge } = await generatePKCE();
|
|
367
|
+
const state = crypto2.randomBytes(16).toString("hex");
|
|
368
|
+
const params = new URLSearchParams({
|
|
369
|
+
code: "true",
|
|
370
|
+
client_id: CLIENT_ID,
|
|
371
|
+
response_type: "code",
|
|
372
|
+
redirect_uri: REDIRECT_URI,
|
|
373
|
+
scope: SCOPES,
|
|
374
|
+
code_challenge: challenge,
|
|
375
|
+
code_challenge_method: "S256",
|
|
376
|
+
state
|
|
377
|
+
});
|
|
378
|
+
const authUrl = `${AUTHORIZE_URL}?${params}`;
|
|
379
|
+
callbacks.onOpenUrl(authUrl);
|
|
380
|
+
const raw = await callbacks.onPromptCode("Paste the code from the browser (format: code#state):");
|
|
381
|
+
const parts = raw.trim().split("#");
|
|
382
|
+
if (parts.length !== 2 || !parts[0] || parts[1] !== state) {
|
|
383
|
+
throw new Error("Invalid code or state mismatch. Please try again.");
|
|
384
|
+
}
|
|
385
|
+
return exchangeAnthropicCode(parts[0], parts[1], verifier);
|
|
386
|
+
}
|
|
387
|
+
async function exchangeAnthropicCode(code, state, verifier) {
|
|
388
|
+
const data = await postTokenRequest(
|
|
389
|
+
{
|
|
390
|
+
grant_type: "authorization_code",
|
|
391
|
+
client_id: CLIENT_ID,
|
|
392
|
+
code,
|
|
393
|
+
state,
|
|
394
|
+
redirect_uri: REDIRECT_URI,
|
|
395
|
+
code_verifier: verifier
|
|
396
|
+
},
|
|
397
|
+
"token exchange"
|
|
398
|
+
);
|
|
399
|
+
return toCredentials(data);
|
|
400
|
+
}
|
|
401
|
+
async function refreshAnthropicToken(refreshToken) {
|
|
402
|
+
const data = await postTokenRequest(
|
|
403
|
+
{
|
|
404
|
+
grant_type: "refresh_token",
|
|
405
|
+
client_id: CLIENT_ID,
|
|
406
|
+
refresh_token: refreshToken
|
|
407
|
+
},
|
|
408
|
+
"token refresh"
|
|
409
|
+
);
|
|
410
|
+
return toCredentials(data);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// src/oauth/openai.ts
|
|
414
|
+
import http from "http";
|
|
415
|
+
import crypto3 from "crypto";
|
|
416
|
+
var CLIENT_ID2 = "app_EMoamEEZ73f0CkXaXp7hrann";
|
|
417
|
+
var AUTHORIZE_URL2 = "https://auth.openai.com/oauth/authorize";
|
|
418
|
+
var TOKEN_URL = "https://auth.openai.com/oauth/token";
|
|
419
|
+
var REDIRECT_URI2 = "http://localhost:1455/auth/callback";
|
|
420
|
+
var SCOPE = "openid profile email offline_access api.connectors.read api.connectors.invoke";
|
|
421
|
+
var JWT_CLAIM_PATH = "https://api.openai.com/auth";
|
|
422
|
+
async function loginOpenAI(callbacks) {
|
|
423
|
+
const { verifier, challenge } = await generatePKCE();
|
|
424
|
+
const state = crypto3.randomBytes(16).toString("hex");
|
|
425
|
+
const url = new URL(AUTHORIZE_URL2);
|
|
426
|
+
url.searchParams.set("response_type", "code");
|
|
427
|
+
url.searchParams.set("client_id", CLIENT_ID2);
|
|
428
|
+
url.searchParams.set("redirect_uri", REDIRECT_URI2);
|
|
429
|
+
url.searchParams.set("scope", SCOPE);
|
|
430
|
+
url.searchParams.set("code_challenge", challenge);
|
|
431
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
432
|
+
url.searchParams.set("state", state);
|
|
433
|
+
url.searchParams.set("id_token_add_organizations", "true");
|
|
434
|
+
url.searchParams.set("codex_cli_simplified_flow", "true");
|
|
435
|
+
url.searchParams.set("originator", "ggcoder");
|
|
436
|
+
let code;
|
|
437
|
+
try {
|
|
438
|
+
code = await loginWithServer(url.toString(), state, callbacks);
|
|
439
|
+
} catch {
|
|
440
|
+
callbacks.onOpenUrl(url.toString());
|
|
441
|
+
const raw = await callbacks.onPromptCode(
|
|
442
|
+
"Could not start local server. Paste the callback URL or code from the browser:"
|
|
443
|
+
);
|
|
444
|
+
const parsed = parseAuthorizationInput(raw);
|
|
445
|
+
if (!parsed.code) {
|
|
446
|
+
throw new Error("No authorization code found in input.");
|
|
447
|
+
}
|
|
448
|
+
code = parsed.code;
|
|
449
|
+
}
|
|
450
|
+
const creds = await exchangeOpenAICode(code, verifier);
|
|
451
|
+
const accountId = getAccountId(creds.accessToken);
|
|
452
|
+
if (!accountId) {
|
|
453
|
+
throw new Error("Failed to extract accountId from OpenAI token.");
|
|
454
|
+
}
|
|
455
|
+
creds.accountId = accountId;
|
|
456
|
+
return creds;
|
|
457
|
+
}
|
|
458
|
+
function parseAuthorizationInput(input) {
|
|
459
|
+
const value = input.trim();
|
|
460
|
+
if (!value) return {};
|
|
461
|
+
try {
|
|
462
|
+
const url = new URL(value);
|
|
463
|
+
return {
|
|
464
|
+
code: url.searchParams.get("code") ?? void 0,
|
|
465
|
+
state: url.searchParams.get("state") ?? void 0
|
|
466
|
+
};
|
|
467
|
+
} catch {
|
|
468
|
+
}
|
|
469
|
+
if (value.includes("#")) {
|
|
470
|
+
const [code, state] = value.split("#", 2);
|
|
471
|
+
return { code, state };
|
|
472
|
+
}
|
|
473
|
+
if (value.includes("code=")) {
|
|
474
|
+
const params = new URLSearchParams(value);
|
|
475
|
+
return {
|
|
476
|
+
code: params.get("code") ?? void 0,
|
|
477
|
+
state: params.get("state") ?? void 0
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
return { code: value };
|
|
481
|
+
}
|
|
482
|
+
function decodeJwt(token) {
|
|
483
|
+
try {
|
|
484
|
+
const parts = token.split(".");
|
|
485
|
+
if (parts.length !== 3) return null;
|
|
486
|
+
const decoded = atob(parts[1]);
|
|
487
|
+
return JSON.parse(decoded);
|
|
488
|
+
} catch {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function getAccountId(accessToken) {
|
|
493
|
+
const payload = decodeJwt(accessToken);
|
|
494
|
+
const auth = payload?.[JWT_CLAIM_PATH];
|
|
495
|
+
const accountId = auth?.chatgpt_account_id;
|
|
496
|
+
return typeof accountId === "string" && accountId.length > 0 ? accountId : null;
|
|
497
|
+
}
|
|
498
|
+
async function loginWithServer(authUrl, expectedState, callbacks) {
|
|
499
|
+
return new Promise((resolve, reject) => {
|
|
500
|
+
let receivedCode = null;
|
|
501
|
+
const server = http.createServer((req, res) => {
|
|
502
|
+
const url = new URL(req.url || "", "http://localhost");
|
|
503
|
+
if (url.pathname !== "/auth/callback") {
|
|
504
|
+
res.statusCode = 404;
|
|
505
|
+
res.end("Not found");
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
if (url.searchParams.get("state") !== expectedState) {
|
|
509
|
+
res.statusCode = 400;
|
|
510
|
+
res.end("State mismatch");
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
receivedCode = url.searchParams.get("code");
|
|
514
|
+
res.writeHead(200, { "Content-Type": "text/html" });
|
|
515
|
+
res.end("<html><body><h1>Login successful!</h1><p>You can close this tab.</p></body></html>");
|
|
516
|
+
server.close();
|
|
517
|
+
});
|
|
518
|
+
server.on("error", (err) => {
|
|
519
|
+
reject(err);
|
|
520
|
+
});
|
|
521
|
+
server.listen(1455, "127.0.0.1", () => {
|
|
522
|
+
callbacks.onOpenUrl(authUrl);
|
|
523
|
+
callbacks.onStatus("Waiting for browser callback...");
|
|
524
|
+
});
|
|
525
|
+
const timeout = setTimeout(() => {
|
|
526
|
+
if (!receivedCode) {
|
|
527
|
+
server.close();
|
|
528
|
+
}
|
|
529
|
+
}, 12e4);
|
|
530
|
+
timeout.unref();
|
|
531
|
+
server.on("close", () => {
|
|
532
|
+
clearTimeout(timeout);
|
|
533
|
+
if (receivedCode) {
|
|
534
|
+
resolve(receivedCode);
|
|
535
|
+
} else {
|
|
536
|
+
reject(new Error("Server closed without receiving code"));
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
async function exchangeOpenAICode(code, verifier) {
|
|
542
|
+
const response = await fetch(TOKEN_URL, {
|
|
543
|
+
method: "POST",
|
|
544
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
545
|
+
body: new URLSearchParams({
|
|
546
|
+
grant_type: "authorization_code",
|
|
547
|
+
client_id: CLIENT_ID2,
|
|
548
|
+
code,
|
|
549
|
+
redirect_uri: REDIRECT_URI2,
|
|
550
|
+
code_verifier: verifier
|
|
551
|
+
})
|
|
552
|
+
});
|
|
553
|
+
if (!response.ok) {
|
|
554
|
+
const text = await response.text();
|
|
555
|
+
throw new Error(`OpenAI token exchange failed (${response.status}): ${text}`);
|
|
556
|
+
}
|
|
557
|
+
const data = await response.json();
|
|
558
|
+
return {
|
|
559
|
+
accessToken: data.access_token,
|
|
560
|
+
refreshToken: data.refresh_token,
|
|
561
|
+
expiresAt: Date.now() + data.expires_in * 1e3
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
async function refreshOpenAIToken(refreshToken) {
|
|
565
|
+
const response = await fetch(TOKEN_URL, {
|
|
566
|
+
method: "POST",
|
|
567
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
568
|
+
body: new URLSearchParams({
|
|
569
|
+
grant_type: "refresh_token",
|
|
570
|
+
refresh_token: refreshToken,
|
|
571
|
+
client_id: CLIENT_ID2
|
|
572
|
+
})
|
|
573
|
+
});
|
|
574
|
+
if (!response.ok) {
|
|
575
|
+
const text = await response.text();
|
|
576
|
+
throw new Error(`OpenAI token refresh failed (${response.status}): ${text}`);
|
|
577
|
+
}
|
|
578
|
+
const data = await response.json();
|
|
579
|
+
const creds = {
|
|
580
|
+
accessToken: data.access_token,
|
|
581
|
+
refreshToken: data.refresh_token,
|
|
582
|
+
expiresAt: Date.now() + data.expires_in * 1e3
|
|
583
|
+
};
|
|
584
|
+
const accountId = getAccountId(creds.accessToken);
|
|
585
|
+
if (accountId) {
|
|
586
|
+
creds.accountId = accountId;
|
|
587
|
+
}
|
|
588
|
+
return creds;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/oauth/gemini.ts
|
|
592
|
+
import http2 from "http";
|
|
593
|
+
import crypto4 from "crypto";
|
|
594
|
+
var CLIENT_ID_ENV = "GGCODER_GEMINI_OAUTH_CLIENT_ID";
|
|
595
|
+
var CLIENT_SECRET_ENV = "GGCODER_GEMINI_OAUTH_CLIENT_SECRET";
|
|
596
|
+
var DEFAULT_CLIENT_ID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com";
|
|
597
|
+
var DEFAULT_CLIENT_SECRET = "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";
|
|
598
|
+
var AUTHORIZE_URL3 = "https://accounts.google.com/o/oauth2/v2/auth";
|
|
599
|
+
var TOKEN_URL2 = "https://oauth2.googleapis.com/token";
|
|
600
|
+
var CODE_ASSIST_BASE_URL = "https://cloudcode-pa.googleapis.com";
|
|
601
|
+
var CODE_ASSIST_API_VERSION = "v1internal";
|
|
602
|
+
var CODE_ASSIST_POST_RETRIES = 3;
|
|
603
|
+
var CODE_ASSIST_POST_RETRY_DELAY_MS = 100;
|
|
604
|
+
var SCOPE2 = [
|
|
605
|
+
"https://www.googleapis.com/auth/cloud-platform",
|
|
606
|
+
"https://www.googleapis.com/auth/userinfo.email",
|
|
607
|
+
"https://www.googleapis.com/auth/userinfo.profile"
|
|
608
|
+
].join(" ");
|
|
609
|
+
var USER_TIER_FREE = "free-tier";
|
|
610
|
+
var USER_TIER_LEGACY = "legacy-tier";
|
|
611
|
+
var USER_TIER_STANDARD = "standard-tier";
|
|
612
|
+
var VALIDATION_REQUIRED_REASON = "VALIDATION_REQUIRED";
|
|
613
|
+
var VPC_SC_REASON = "SECURITY_POLICY_VIOLATED";
|
|
614
|
+
var CodeAssistHttpError = class extends Error {
|
|
615
|
+
status;
|
|
616
|
+
body;
|
|
617
|
+
constructor(label, status, body) {
|
|
618
|
+
super(`Gemini Code Assist ${label} failed (${status}): ${body}`);
|
|
619
|
+
this.name = "CodeAssistHttpError";
|
|
620
|
+
this.status = status;
|
|
621
|
+
this.body = body;
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
async function loginGemini(callbacks) {
|
|
625
|
+
const { clientId, clientSecret } = getGeminiOAuthClientCredentials();
|
|
626
|
+
const { verifier, challenge } = await generatePKCE();
|
|
627
|
+
const state = crypto4.randomBytes(32).toString("hex");
|
|
628
|
+
const redirectUri = await getLoopbackRedirectUri();
|
|
629
|
+
const url = new URL(AUTHORIZE_URL3);
|
|
630
|
+
url.searchParams.set("response_type", "code");
|
|
631
|
+
url.searchParams.set("client_id", clientId);
|
|
632
|
+
url.searchParams.set("redirect_uri", redirectUri);
|
|
633
|
+
url.searchParams.set("scope", SCOPE2);
|
|
634
|
+
url.searchParams.set("access_type", "offline");
|
|
635
|
+
url.searchParams.set("prompt", "consent");
|
|
636
|
+
url.searchParams.set("code_challenge", challenge);
|
|
637
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
638
|
+
url.searchParams.set("state", state);
|
|
639
|
+
let code;
|
|
640
|
+
try {
|
|
641
|
+
code = await loginWithServer2(url.toString(), redirectUri, state, callbacks);
|
|
642
|
+
} catch {
|
|
643
|
+
callbacks.onOpenUrl(url.toString());
|
|
644
|
+
const raw = await callbacks.onPromptCode(
|
|
645
|
+
"Could not start local server. Paste the callback URL or code from the browser:"
|
|
646
|
+
);
|
|
647
|
+
const parsed = parseAuthorizationInput2(raw);
|
|
648
|
+
if (!parsed.code) {
|
|
649
|
+
throw new Error("No authorization code found in input.");
|
|
650
|
+
}
|
|
651
|
+
if (parsed.state && parsed.state !== state) {
|
|
652
|
+
throw new Error("Invalid state. Please try again.");
|
|
653
|
+
}
|
|
654
|
+
code = parsed.code;
|
|
655
|
+
}
|
|
656
|
+
const creds = await exchangeGeminiCode(code, verifier, redirectUri, clientId, clientSecret);
|
|
657
|
+
callbacks.onStatus("Setting up Gemini Code Assist access...");
|
|
658
|
+
const projectId = await setupCodeAssistProject(creds.accessToken, callbacks);
|
|
659
|
+
return {
|
|
660
|
+
...creds,
|
|
661
|
+
projectId
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
async function refreshGeminiToken(refreshToken) {
|
|
665
|
+
const { clientId, clientSecret } = getGeminiOAuthClientCredentials();
|
|
666
|
+
const data = await postTokenRequest2({
|
|
667
|
+
grant_type: "refresh_token",
|
|
668
|
+
refresh_token: refreshToken,
|
|
669
|
+
client_id: clientId,
|
|
670
|
+
client_secret: clientSecret
|
|
671
|
+
});
|
|
672
|
+
return {
|
|
673
|
+
accessToken: data.access_token,
|
|
674
|
+
refreshToken: data.refresh_token ?? refreshToken,
|
|
675
|
+
expiresAt: Date.now() + data.expires_in * 1e3 - 5 * 60 * 1e3
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
function getGeminiOAuthClientCredentials() {
|
|
679
|
+
const clientId = process.env[CLIENT_ID_ENV]?.trim() || DEFAULT_CLIENT_ID;
|
|
680
|
+
const clientSecret = process.env[CLIENT_SECRET_ENV]?.trim() || DEFAULT_CLIENT_SECRET;
|
|
681
|
+
return { clientId, clientSecret };
|
|
682
|
+
}
|
|
683
|
+
async function getLoopbackRedirectUri() {
|
|
684
|
+
return new Promise((resolve, reject) => {
|
|
685
|
+
const server = http2.createServer();
|
|
686
|
+
server.listen(0, "127.0.0.1", () => {
|
|
687
|
+
const addr = server.address();
|
|
688
|
+
server.close(() => {
|
|
689
|
+
if (addr && typeof addr === "object") {
|
|
690
|
+
resolve(`http://127.0.0.1:${addr.port}/oauth2callback`);
|
|
691
|
+
} else {
|
|
692
|
+
reject(new Error("Failed to allocate OAuth callback port."));
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
});
|
|
696
|
+
server.on("error", reject);
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
function parseAuthorizationInput2(input) {
|
|
700
|
+
const value = input.trim();
|
|
701
|
+
if (!value) return {};
|
|
702
|
+
try {
|
|
703
|
+
const url = new URL(value);
|
|
704
|
+
return {
|
|
705
|
+
code: url.searchParams.get("code") ?? void 0,
|
|
706
|
+
state: url.searchParams.get("state") ?? void 0
|
|
707
|
+
};
|
|
708
|
+
} catch {
|
|
709
|
+
}
|
|
710
|
+
if (value.includes("code=")) {
|
|
711
|
+
const params = new URLSearchParams(value);
|
|
712
|
+
return {
|
|
713
|
+
code: params.get("code") ?? void 0,
|
|
714
|
+
state: params.get("state") ?? void 0
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
return { code: value };
|
|
718
|
+
}
|
|
719
|
+
async function loginWithServer2(authUrl, redirectUri, expectedState, callbacks) {
|
|
720
|
+
const redirect = new URL(redirectUri);
|
|
721
|
+
const port = Number(redirect.port);
|
|
722
|
+
return new Promise((resolve, reject) => {
|
|
723
|
+
let receivedCode = null;
|
|
724
|
+
const server = http2.createServer((req, res) => {
|
|
725
|
+
const url = new URL(req.url || "", redirect.origin);
|
|
726
|
+
if (url.pathname !== redirect.pathname) {
|
|
727
|
+
res.statusCode = 404;
|
|
728
|
+
res.end("Not found");
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
if (url.searchParams.get("state") !== expectedState) {
|
|
732
|
+
res.statusCode = 400;
|
|
733
|
+
res.end("State mismatch");
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
receivedCode = url.searchParams.get("code");
|
|
737
|
+
res.writeHead(200, { "Content-Type": "text/html", Connection: "close" });
|
|
738
|
+
res.end("<html><body><h1>Login successful!</h1><p>You can close this tab.</p></body></html>");
|
|
739
|
+
server.close();
|
|
740
|
+
});
|
|
741
|
+
server.on("error", (err) => reject(err));
|
|
742
|
+
server.listen(port, "127.0.0.1", () => {
|
|
743
|
+
callbacks.onOpenUrl(authUrl);
|
|
744
|
+
callbacks.onStatus("Waiting for browser callback...");
|
|
745
|
+
});
|
|
746
|
+
const timeout = setTimeout(() => {
|
|
747
|
+
if (!receivedCode) server.close();
|
|
748
|
+
}, 12e4);
|
|
749
|
+
timeout.unref();
|
|
750
|
+
server.on("close", () => {
|
|
751
|
+
clearTimeout(timeout);
|
|
752
|
+
if (receivedCode) {
|
|
753
|
+
resolve(receivedCode);
|
|
754
|
+
} else {
|
|
755
|
+
reject(new Error("Server closed without receiving code."));
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
async function exchangeGeminiCode(code, verifier, redirectUri, clientId, clientSecret) {
|
|
761
|
+
const data = await postTokenRequest2({
|
|
762
|
+
grant_type: "authorization_code",
|
|
763
|
+
client_id: clientId,
|
|
764
|
+
client_secret: clientSecret,
|
|
765
|
+
code,
|
|
766
|
+
redirect_uri: redirectUri,
|
|
767
|
+
code_verifier: verifier
|
|
768
|
+
});
|
|
769
|
+
if (!data.refresh_token) {
|
|
770
|
+
throw new Error("Gemini OAuth did not return a refresh token. Please try login again.");
|
|
771
|
+
}
|
|
772
|
+
return {
|
|
773
|
+
accessToken: data.access_token,
|
|
774
|
+
refreshToken: data.refresh_token,
|
|
775
|
+
expiresAt: Date.now() + data.expires_in * 1e3 - 5 * 60 * 1e3
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
async function postTokenRequest2(body) {
|
|
779
|
+
const response = await fetch(TOKEN_URL2, {
|
|
780
|
+
method: "POST",
|
|
781
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
782
|
+
body: new URLSearchParams(body)
|
|
783
|
+
});
|
|
784
|
+
if (!response.ok) {
|
|
785
|
+
const text = await response.text();
|
|
786
|
+
throw new Error(`Gemini token request failed (${response.status}): ${text}`);
|
|
787
|
+
}
|
|
788
|
+
return await response.json();
|
|
789
|
+
}
|
|
790
|
+
async function setupCodeAssistProject(accessToken, callbacks) {
|
|
791
|
+
const envProject = process.env.GOOGLE_CLOUD_PROJECT ?? process.env.GOOGLE_CLOUD_PROJECT_ID;
|
|
792
|
+
if (envProject && /^\d+$/.test(envProject)) {
|
|
793
|
+
throw new Error("GOOGLE_CLOUD_PROJECT must be a project ID, not a numeric project number.");
|
|
794
|
+
}
|
|
795
|
+
const coreMetadata = {
|
|
796
|
+
ideType: "IDE_UNSPECIFIED",
|
|
797
|
+
platform: "PLATFORM_UNSPECIFIED",
|
|
798
|
+
pluginType: "GEMINI"
|
|
799
|
+
};
|
|
800
|
+
const projectMetadata = {
|
|
801
|
+
...coreMetadata,
|
|
802
|
+
...envProject ? { duetProject: envProject } : {}
|
|
803
|
+
};
|
|
804
|
+
let loadRes;
|
|
805
|
+
while (true) {
|
|
806
|
+
loadRes = await loadCodeAssist(accessToken, envProject, projectMetadata);
|
|
807
|
+
const validation = getValidationRequiredTier(loadRes);
|
|
808
|
+
if (!validation) break;
|
|
809
|
+
callbacks.onStatus(
|
|
810
|
+
`Gemini Code Assist requires account validation${validation.reasonMessage ? `: ${validation.reasonMessage}` : ""}`
|
|
811
|
+
);
|
|
812
|
+
callbacks.onOpenUrl(validation.validationUrl);
|
|
813
|
+
const answer = await callbacks.onPromptCode(
|
|
814
|
+
"Complete validation in the browser, then press Enter to retry (or type cancel):"
|
|
815
|
+
);
|
|
816
|
+
if (answer.trim().toLowerCase() === "cancel") {
|
|
817
|
+
throw new Error("Gemini Code Assist account validation was cancelled.");
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
if (loadRes.currentTier) {
|
|
821
|
+
const project2 = loadRes.cloudaicompanionProject ?? envProject;
|
|
822
|
+
if (!project2) throwProjectError(loadRes);
|
|
823
|
+
return project2;
|
|
824
|
+
}
|
|
825
|
+
const tier = getOnboardTier(loadRes);
|
|
826
|
+
const onboardReq = tier.id === USER_TIER_FREE ? {
|
|
827
|
+
tierId: tier.id,
|
|
828
|
+
cloudaicompanionProject: void 0,
|
|
829
|
+
metadata: coreMetadata
|
|
830
|
+
} : {
|
|
831
|
+
tierId: tier.id,
|
|
832
|
+
cloudaicompanionProject: envProject,
|
|
833
|
+
metadata: projectMetadata
|
|
834
|
+
};
|
|
835
|
+
let operation = await codeAssistPost(
|
|
836
|
+
accessToken,
|
|
837
|
+
"onboardUser",
|
|
838
|
+
onboardReq
|
|
839
|
+
);
|
|
840
|
+
while (!operation.done && operation.name) {
|
|
841
|
+
await new Promise((resolve) => setTimeout(resolve, 5e3));
|
|
842
|
+
operation = await codeAssistGet(accessToken, operation.name);
|
|
843
|
+
}
|
|
844
|
+
const project = operation.response?.cloudaicompanionProject?.id ?? envProject;
|
|
845
|
+
if (!project) throwProjectError(loadRes);
|
|
846
|
+
return project;
|
|
847
|
+
}
|
|
848
|
+
async function loadCodeAssist(accessToken, envProject, metadata) {
|
|
849
|
+
try {
|
|
850
|
+
return await codeAssistPost(accessToken, "loadCodeAssist", {
|
|
851
|
+
...envProject ? { cloudaicompanionProject: envProject } : {},
|
|
852
|
+
metadata
|
|
853
|
+
});
|
|
854
|
+
} catch (err) {
|
|
855
|
+
if (err instanceof CodeAssistHttpError && isVpcScAffectedError(err)) {
|
|
856
|
+
return { currentTier: { id: USER_TIER_STANDARD } };
|
|
857
|
+
}
|
|
858
|
+
if (err instanceof CodeAssistHttpError && err.status === 403 && envProject === "cloudshell-gca") {
|
|
859
|
+
throw new Error(
|
|
860
|
+
"Access to the default Cloud Shell Gemini project was denied.\nPlease set your own Google Cloud project by running:\ngcloud config set project [PROJECT_ID]\nor setting export GOOGLE_CLOUD_PROJECT=...",
|
|
861
|
+
{ cause: err }
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
throw err;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
function getValidationRequiredTier(response) {
|
|
868
|
+
return response.ineligibleTiers?.find(
|
|
869
|
+
(tier) => tier.reasonCode === VALIDATION_REQUIRED_REASON && typeof tier.validationUrl === "string"
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
function getOnboardTier(response) {
|
|
873
|
+
const defaultTier = response.allowedTiers?.find((tier) => tier.isDefault);
|
|
874
|
+
return defaultTier ?? { id: USER_TIER_LEGACY, name: "" };
|
|
875
|
+
}
|
|
876
|
+
function throwProjectError(response) {
|
|
877
|
+
const reasons = response.ineligibleTiers?.map((tier) => tier.reasonMessage ?? tier.tierName).filter((reason) => Boolean(reason));
|
|
878
|
+
if (reasons && reasons.length > 0) {
|
|
879
|
+
throw new Error(`Gemini Code Assist setup failed: ${reasons.join(", ")}`);
|
|
880
|
+
}
|
|
881
|
+
throw new Error(
|
|
882
|
+
"Gemini requires a Google Cloud project for this account. Set GOOGLE_CLOUD_PROJECT and try again."
|
|
883
|
+
);
|
|
884
|
+
}
|
|
885
|
+
async function codeAssistPost(accessToken, method, body) {
|
|
886
|
+
let lastError;
|
|
887
|
+
for (let attempt = 0; attempt <= CODE_ASSIST_POST_RETRIES; attempt++) {
|
|
888
|
+
try {
|
|
889
|
+
return await codeAssistRequest(getCodeAssistMethodUrl(method), accessToken, method, {
|
|
890
|
+
method: "POST",
|
|
891
|
+
body: JSON.stringify(body)
|
|
892
|
+
});
|
|
893
|
+
} catch (err) {
|
|
894
|
+
if (!(err instanceof CodeAssistHttpError) || attempt === CODE_ASSIST_POST_RETRIES || !shouldRetryCodeAssistStatus(err.status)) {
|
|
895
|
+
throw err;
|
|
896
|
+
}
|
|
897
|
+
lastError = err;
|
|
898
|
+
}
|
|
899
|
+
await new Promise((resolve) => setTimeout(resolve, CODE_ASSIST_POST_RETRY_DELAY_MS));
|
|
900
|
+
}
|
|
901
|
+
throw lastError ?? new Error(`Gemini Code Assist ${method} failed.`);
|
|
902
|
+
}
|
|
903
|
+
async function codeAssistGet(accessToken, operationName) {
|
|
904
|
+
return codeAssistRequest(getCodeAssistOperationUrl(operationName), accessToken, "operation", {
|
|
905
|
+
method: "GET"
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
async function codeAssistRequest(url, accessToken, label, init) {
|
|
909
|
+
const response = await fetch(url, {
|
|
910
|
+
...init,
|
|
911
|
+
headers: codeAssistHeaders(accessToken)
|
|
912
|
+
});
|
|
913
|
+
if (!response.ok) {
|
|
914
|
+
const text = await response.text();
|
|
915
|
+
throw new CodeAssistHttpError(label, response.status, text);
|
|
916
|
+
}
|
|
917
|
+
return await response.json();
|
|
918
|
+
}
|
|
919
|
+
function getCodeAssistBaseUrl() {
|
|
920
|
+
const endpoint = process.env.CODE_ASSIST_ENDPOINT ?? CODE_ASSIST_BASE_URL;
|
|
921
|
+
const version = process.env.CODE_ASSIST_API_VERSION || CODE_ASSIST_API_VERSION;
|
|
922
|
+
return `${endpoint}/${version}`;
|
|
923
|
+
}
|
|
924
|
+
function getCodeAssistMethodUrl(method) {
|
|
925
|
+
return `${getCodeAssistBaseUrl()}:${method}`;
|
|
926
|
+
}
|
|
927
|
+
function getCodeAssistOperationUrl(operationName) {
|
|
928
|
+
return `${getCodeAssistBaseUrl()}/${operationName}`;
|
|
929
|
+
}
|
|
930
|
+
function shouldRetryCodeAssistStatus(status) {
|
|
931
|
+
return status === 429 || status === 499 || status >= 500 && status <= 599;
|
|
932
|
+
}
|
|
933
|
+
function isVpcScAffectedError(error) {
|
|
934
|
+
try {
|
|
935
|
+
const parsed = JSON.parse(error.body);
|
|
936
|
+
if (!parsed || typeof parsed !== "object" || !("error" in parsed)) return false;
|
|
937
|
+
const details = parsed.error?.details;
|
|
938
|
+
return Array.isArray(details) ? details.some(
|
|
939
|
+
(detail) => detail != null && typeof detail === "object" && "reason" in detail && detail.reason === VPC_SC_REASON
|
|
940
|
+
) : false;
|
|
941
|
+
} catch {
|
|
942
|
+
return false;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
function codeAssistHeaders(accessToken) {
|
|
946
|
+
return {
|
|
947
|
+
Authorization: `Bearer ${accessToken}`,
|
|
948
|
+
"Content-Type": "application/json",
|
|
949
|
+
"User-Agent": "google-gemini-cli",
|
|
950
|
+
"X-Goog-Api-Client": "gemini-cli/0.0.0"
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
// src/auth-storage.ts
|
|
955
|
+
var AuthStorage = class {
|
|
956
|
+
data = {};
|
|
957
|
+
filePath;
|
|
958
|
+
loaded = false;
|
|
959
|
+
/** Per-provider lock to serialize concurrent refresh calls. */
|
|
960
|
+
refreshLocks = /* @__PURE__ */ new Map();
|
|
961
|
+
constructor(filePath) {
|
|
962
|
+
this.filePath = filePath ?? getAppPaths().authFile;
|
|
963
|
+
}
|
|
964
|
+
/** Path to the on-disk auth file. Useful for status output. */
|
|
965
|
+
get path() {
|
|
966
|
+
return this.filePath;
|
|
967
|
+
}
|
|
968
|
+
/** List provider keys with stored credentials. */
|
|
969
|
+
async listProviders() {
|
|
970
|
+
await this.ensureLoaded();
|
|
971
|
+
return Object.keys(this.data);
|
|
972
|
+
}
|
|
973
|
+
/** True if credentials exist for `provider`. */
|
|
974
|
+
async hasCredentials(provider) {
|
|
975
|
+
await this.ensureLoaded();
|
|
976
|
+
return Boolean(this.data[provider]);
|
|
977
|
+
}
|
|
978
|
+
async load() {
|
|
979
|
+
await withFileLock(this.filePath, async () => {
|
|
980
|
+
try {
|
|
981
|
+
const content = await fs4.readFile(this.filePath, "utf-8");
|
|
982
|
+
this.data = JSON.parse(content);
|
|
983
|
+
log("INFO", "auth", `Loaded credentials from ${this.filePath}`, {
|
|
984
|
+
providers: Object.keys(this.data).join(",") || "(none)"
|
|
985
|
+
});
|
|
986
|
+
} catch (err) {
|
|
987
|
+
this.data = {};
|
|
988
|
+
const code = err.code;
|
|
989
|
+
if (code === "ENOENT") {
|
|
990
|
+
log("INFO", "auth", `No auth file found at ${this.filePath} (first run)`);
|
|
991
|
+
} else {
|
|
992
|
+
log(
|
|
993
|
+
"ERROR",
|
|
994
|
+
"auth",
|
|
995
|
+
`Failed to load auth file: ${err instanceof Error ? err.message : String(err)}`,
|
|
996
|
+
{ path: this.filePath, code: code ?? "unknown" }
|
|
997
|
+
);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
1001
|
+
this.loaded = true;
|
|
1002
|
+
}
|
|
1003
|
+
async ensureLoaded() {
|
|
1004
|
+
if (!this.loaded) await this.load();
|
|
1005
|
+
}
|
|
1006
|
+
async getCredentials(provider) {
|
|
1007
|
+
await this.ensureLoaded();
|
|
1008
|
+
return this.data[provider];
|
|
1009
|
+
}
|
|
1010
|
+
async setCredentials(provider, creds) {
|
|
1011
|
+
await this.ensureLoaded();
|
|
1012
|
+
this.data[provider] = creds;
|
|
1013
|
+
await this.save();
|
|
1014
|
+
}
|
|
1015
|
+
async clearCredentials(provider) {
|
|
1016
|
+
await this.ensureLoaded();
|
|
1017
|
+
delete this.data[provider];
|
|
1018
|
+
await this.save();
|
|
1019
|
+
}
|
|
1020
|
+
async clearAll() {
|
|
1021
|
+
this.data = {};
|
|
1022
|
+
await this.save();
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Returns valid credentials, auto-refreshing if expired.
|
|
1026
|
+
* If `forceRefresh` is true, refreshes even if the token hasn't expired
|
|
1027
|
+
* (useful when the provider rejects a token with 401 before its stored expiry).
|
|
1028
|
+
* Throws if not logged in.
|
|
1029
|
+
*/
|
|
1030
|
+
async resolveCredentials(provider, opts) {
|
|
1031
|
+
await this.ensureLoaded();
|
|
1032
|
+
const creds = this.data[provider];
|
|
1033
|
+
if (!creds) {
|
|
1034
|
+
throw new NotLoggedInError(provider);
|
|
1035
|
+
}
|
|
1036
|
+
if (provider === "glm" || provider === "moonshot" || provider === "xiaomi" || provider === "minimax" || provider === "deepseek" || provider === "openrouter") {
|
|
1037
|
+
return creds;
|
|
1038
|
+
}
|
|
1039
|
+
if (!opts?.forceRefresh && Date.now() < creds.expiresAt) {
|
|
1040
|
+
return creds;
|
|
1041
|
+
}
|
|
1042
|
+
const existing = this.refreshLocks.get(provider);
|
|
1043
|
+
if (existing) return existing;
|
|
1044
|
+
const refreshPromise = withFileLock(this.filePath, async () => {
|
|
1045
|
+
try {
|
|
1046
|
+
const content = await fs4.readFile(this.filePath, "utf-8");
|
|
1047
|
+
const freshData = JSON.parse(content);
|
|
1048
|
+
const freshCreds = freshData[provider];
|
|
1049
|
+
if (freshCreds && !opts?.forceRefresh && Date.now() < freshCreds.expiresAt) {
|
|
1050
|
+
this.data[provider] = freshCreds;
|
|
1051
|
+
return freshCreds;
|
|
1052
|
+
}
|
|
1053
|
+
} catch {
|
|
1054
|
+
}
|
|
1055
|
+
const refreshFn = provider === "anthropic" ? refreshAnthropicToken : provider === "gemini" ? refreshGeminiToken : refreshOpenAIToken;
|
|
1056
|
+
let refreshed;
|
|
1057
|
+
try {
|
|
1058
|
+
refreshed = await refreshFn(creds.refreshToken);
|
|
1059
|
+
} catch (err) {
|
|
1060
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1061
|
+
const isAuthFailure = /\((401|400)\)/.test(msg) || /invalid_grant|invalid_token|invalid.*refresh/i.test(msg) || /unauthorized/i.test(msg);
|
|
1062
|
+
if (isAuthFailure) {
|
|
1063
|
+
delete this.data[provider];
|
|
1064
|
+
await atomicWriteFile(this.filePath, JSON.stringify(this.data, null, 2));
|
|
1065
|
+
throw new NotLoggedInError(provider);
|
|
1066
|
+
}
|
|
1067
|
+
throw err;
|
|
1068
|
+
}
|
|
1069
|
+
if (!refreshed.accountId && creds.accountId) {
|
|
1070
|
+
refreshed.accountId = creds.accountId;
|
|
1071
|
+
}
|
|
1072
|
+
if (!refreshed.projectId && creds.projectId) {
|
|
1073
|
+
refreshed.projectId = creds.projectId;
|
|
1074
|
+
}
|
|
1075
|
+
this.data[provider] = refreshed;
|
|
1076
|
+
await atomicWriteFile(this.filePath, JSON.stringify(this.data, null, 2));
|
|
1077
|
+
return refreshed;
|
|
1078
|
+
});
|
|
1079
|
+
this.refreshLocks.set(provider, refreshPromise);
|
|
1080
|
+
try {
|
|
1081
|
+
return await refreshPromise;
|
|
1082
|
+
} finally {
|
|
1083
|
+
this.refreshLocks.delete(provider);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Returns a valid access token, auto-refreshing if expired.
|
|
1088
|
+
* Throws if not logged in.
|
|
1089
|
+
*/
|
|
1090
|
+
async resolveToken(provider) {
|
|
1091
|
+
const creds = await this.resolveCredentials(provider);
|
|
1092
|
+
return creds.accessToken;
|
|
1093
|
+
}
|
|
1094
|
+
async save() {
|
|
1095
|
+
await withFileLock(this.filePath, async () => {
|
|
1096
|
+
await atomicWriteFile(this.filePath, JSON.stringify(this.data, null, 2));
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
async function atomicWriteFile(filePath, content) {
|
|
1101
|
+
const tmpPath = `${filePath}.${process.pid}.${Date.now()}.${crypto5.randomUUID().slice(0, 8)}.tmp`;
|
|
1102
|
+
try {
|
|
1103
|
+
await fs4.writeFile(tmpPath, content, { encoding: "utf-8", mode: 384 });
|
|
1104
|
+
await fs4.rename(tmpPath, filePath);
|
|
1105
|
+
} catch (err) {
|
|
1106
|
+
await fs4.unlink(tmpPath).catch(() => {
|
|
1107
|
+
});
|
|
1108
|
+
throw err;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
var NotLoggedInError = class extends Error {
|
|
1112
|
+
provider;
|
|
1113
|
+
constructor(provider) {
|
|
1114
|
+
super(`Not logged in to ${provider}. Run "ggcoder login" to authenticate.`);
|
|
1115
|
+
this.name = "NotLoggedInError";
|
|
1116
|
+
this.provider = provider;
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
// src/telegram.ts
|
|
1121
|
+
var TELEGRAM_API = "https://api.telegram.org";
|
|
1122
|
+
var MAX_MESSAGE_LENGTH = 4096;
|
|
1123
|
+
var TelegramBot = class {
|
|
1124
|
+
token;
|
|
1125
|
+
allowedUserId;
|
|
1126
|
+
offset = 0;
|
|
1127
|
+
running = false;
|
|
1128
|
+
onMessage = null;
|
|
1129
|
+
onVoiceMessage = null;
|
|
1130
|
+
onCallback = null;
|
|
1131
|
+
onBotAdded = null;
|
|
1132
|
+
onBotRemoved = null;
|
|
1133
|
+
constructor(config) {
|
|
1134
|
+
this.token = config.botToken;
|
|
1135
|
+
this.allowedUserId = config.allowedUserId;
|
|
1136
|
+
}
|
|
1137
|
+
/** Register handler for incoming text messages. */
|
|
1138
|
+
onText(handler) {
|
|
1139
|
+
this.onMessage = handler;
|
|
1140
|
+
}
|
|
1141
|
+
/** Register handler for incoming voice notes. */
|
|
1142
|
+
onVoice(handler) {
|
|
1143
|
+
this.onVoiceMessage = handler;
|
|
1144
|
+
}
|
|
1145
|
+
/** Register handler for inline keyboard button presses. */
|
|
1146
|
+
onCallbackQuery(handler) {
|
|
1147
|
+
this.onCallback = handler;
|
|
1148
|
+
}
|
|
1149
|
+
/** Register handler for when the bot is added to a group. */
|
|
1150
|
+
onAddedToGroup(handler) {
|
|
1151
|
+
this.onBotAdded = handler;
|
|
1152
|
+
}
|
|
1153
|
+
/** Register handler for when the bot is removed from a group. */
|
|
1154
|
+
onRemovedFromGroup(handler) {
|
|
1155
|
+
this.onBotRemoved = handler;
|
|
1156
|
+
}
|
|
1157
|
+
/** Start long polling. Blocks until stop() is called. */
|
|
1158
|
+
async start() {
|
|
1159
|
+
this.running = true;
|
|
1160
|
+
const me = await this.apiCall("getMe");
|
|
1161
|
+
if (!me.ok) {
|
|
1162
|
+
throw new Error(`Invalid bot token: ${JSON.stringify(me)}`);
|
|
1163
|
+
}
|
|
1164
|
+
while (this.running) {
|
|
1165
|
+
try {
|
|
1166
|
+
const updates = await this.getUpdates();
|
|
1167
|
+
for (const update of updates) {
|
|
1168
|
+
await this.handleUpdate(update);
|
|
1169
|
+
}
|
|
1170
|
+
} catch (err) {
|
|
1171
|
+
if (!this.running) break;
|
|
1172
|
+
console.error(`[telegram] Poll error: ${err instanceof Error ? err.message : err}`);
|
|
1173
|
+
await sleep(3e3);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
/** Stop long polling. */
|
|
1178
|
+
stop() {
|
|
1179
|
+
this.running = false;
|
|
1180
|
+
}
|
|
1181
|
+
/** Send a text message to a specific chat. Converts markdown and splits long messages. */
|
|
1182
|
+
async send(chatId, text, buttons) {
|
|
1183
|
+
const converted = toTelegramMarkdown(text);
|
|
1184
|
+
const chunks = splitMessage(converted);
|
|
1185
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
1186
|
+
const isLast = i === chunks.length - 1;
|
|
1187
|
+
const replyMarkup = isLast && buttons ? {
|
|
1188
|
+
inline_keyboard: buttons.map(
|
|
1189
|
+
(row) => row.map((b) => ({ text: b.text, callback_data: b.callback_data }))
|
|
1190
|
+
)
|
|
1191
|
+
} : void 0;
|
|
1192
|
+
await this.apiCall("sendMessage", {
|
|
1193
|
+
chat_id: chatId,
|
|
1194
|
+
text: chunks[i],
|
|
1195
|
+
parse_mode: "Markdown",
|
|
1196
|
+
...replyMarkup ? { reply_markup: replyMarkup } : {}
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
/** Send a plain text message (no markdown parsing) to a specific chat. */
|
|
1201
|
+
async sendPlain(chatId, text) {
|
|
1202
|
+
const chunks = splitMessage(text);
|
|
1203
|
+
for (const chunk of chunks) {
|
|
1204
|
+
await this.apiCall("sendMessage", {
|
|
1205
|
+
chat_id: chatId,
|
|
1206
|
+
text: chunk
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
/** Send a typing indicator to a specific chat. */
|
|
1211
|
+
async sendTyping(chatId) {
|
|
1212
|
+
await this.apiCall("sendChatAction", {
|
|
1213
|
+
chat_id: chatId,
|
|
1214
|
+
action: "typing"
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
/** Get a direct download URL for a Telegram file. */
|
|
1218
|
+
async getFileUrl(fileId) {
|
|
1219
|
+
const result = await this.apiCall("getFile", { file_id: fileId });
|
|
1220
|
+
if (!result.ok) throw new Error(`Failed to get file: ${JSON.stringify(result)}`);
|
|
1221
|
+
const filePath = result.result.file_path;
|
|
1222
|
+
return `${TELEGRAM_API}/file/bot${this.token}/${filePath}`;
|
|
1223
|
+
}
|
|
1224
|
+
// ── Private ───────────────────────────────────────────
|
|
1225
|
+
async getUpdates() {
|
|
1226
|
+
const result = await this.apiCall("getUpdates", {
|
|
1227
|
+
offset: this.offset,
|
|
1228
|
+
timeout: 30,
|
|
1229
|
+
allowed_updates: ["message", "callback_query", "my_chat_member"]
|
|
1230
|
+
});
|
|
1231
|
+
if (!result.ok || !Array.isArray(result.result)) return [];
|
|
1232
|
+
const updates = result.result;
|
|
1233
|
+
if (updates.length > 0) {
|
|
1234
|
+
this.offset = updates[updates.length - 1].update_id + 1;
|
|
1235
|
+
}
|
|
1236
|
+
return updates;
|
|
1237
|
+
}
|
|
1238
|
+
async handleUpdate(update) {
|
|
1239
|
+
if (update.message) {
|
|
1240
|
+
const msg = update.message;
|
|
1241
|
+
if (msg.from.id !== this.allowedUserId) {
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
if (msg.text && this.onMessage) {
|
|
1245
|
+
this.onMessage({
|
|
1246
|
+
text: msg.text,
|
|
1247
|
+
chatId: msg.chat.id,
|
|
1248
|
+
chatType: msg.chat.type,
|
|
1249
|
+
chatTitle: msg.chat.title
|
|
1250
|
+
});
|
|
1251
|
+
} else if (msg.voice && this.onVoiceMessage) {
|
|
1252
|
+
this.onVoiceMessage({
|
|
1253
|
+
fileId: msg.voice.file_id,
|
|
1254
|
+
duration: msg.voice.duration,
|
|
1255
|
+
chatId: msg.chat.id,
|
|
1256
|
+
chatType: msg.chat.type,
|
|
1257
|
+
chatTitle: msg.chat.title
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
if (update.my_chat_member) {
|
|
1262
|
+
const member = update.my_chat_member;
|
|
1263
|
+
const status = member.new_chat_member.status;
|
|
1264
|
+
if ((status === "member" || status === "administrator") && this.onBotAdded) {
|
|
1265
|
+
this.onBotAdded(member.chat.id, member.chat.title);
|
|
1266
|
+
} else if ((status === "left" || status === "kicked") && this.onBotRemoved) {
|
|
1267
|
+
this.onBotRemoved(member.chat.id);
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
if (update.callback_query) {
|
|
1271
|
+
const cb = update.callback_query;
|
|
1272
|
+
if (cb.from.id !== this.allowedUserId) return;
|
|
1273
|
+
await this.apiCall("answerCallbackQuery", { callback_query_id: cb.id });
|
|
1274
|
+
if (cb.data && this.onCallback) {
|
|
1275
|
+
this.onCallback(cb.data, cb.message.chat.id);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
async apiCall(method, body) {
|
|
1280
|
+
const url = `${TELEGRAM_API}/bot${this.token}/${method}`;
|
|
1281
|
+
const response = await fetch(url, {
|
|
1282
|
+
method: "POST",
|
|
1283
|
+
headers: { "Content-Type": "application/json" },
|
|
1284
|
+
body: body ? JSON.stringify(body) : void 0
|
|
1285
|
+
});
|
|
1286
|
+
if (!response.ok) {
|
|
1287
|
+
return { ok: false };
|
|
1288
|
+
}
|
|
1289
|
+
return response.json();
|
|
1290
|
+
}
|
|
1291
|
+
};
|
|
1292
|
+
function toTelegramMarkdown(text) {
|
|
1293
|
+
const lines = text.split("\n");
|
|
1294
|
+
const result = [];
|
|
1295
|
+
let inCodeBlock = false;
|
|
1296
|
+
for (const line of lines) {
|
|
1297
|
+
if (line.trimStart().startsWith("```")) {
|
|
1298
|
+
inCodeBlock = !inCodeBlock;
|
|
1299
|
+
result.push(line);
|
|
1300
|
+
continue;
|
|
1301
|
+
}
|
|
1302
|
+
if (inCodeBlock) {
|
|
1303
|
+
result.push(line);
|
|
1304
|
+
continue;
|
|
1305
|
+
}
|
|
1306
|
+
let transformed = line;
|
|
1307
|
+
const headingMatch = transformed.match(/^(#{1,6})\s+(.+)$/);
|
|
1308
|
+
if (headingMatch) {
|
|
1309
|
+
transformed = `*${headingMatch[2]}*`;
|
|
1310
|
+
result.push(transformed);
|
|
1311
|
+
continue;
|
|
1312
|
+
}
|
|
1313
|
+
if (/^(-{3,}|_{3,}|\*{3,})$/.test(transformed.trim())) {
|
|
1314
|
+
result.push("");
|
|
1315
|
+
continue;
|
|
1316
|
+
}
|
|
1317
|
+
transformed = transformed.replace(/\*\*(.+?)\*\*/g, "*$1*");
|
|
1318
|
+
result.push(transformed);
|
|
1319
|
+
}
|
|
1320
|
+
return result.join("\n");
|
|
1321
|
+
}
|
|
1322
|
+
function splitMessage(text) {
|
|
1323
|
+
if (text.length <= MAX_MESSAGE_LENGTH) return [text];
|
|
1324
|
+
const chunks = [];
|
|
1325
|
+
let remaining = text;
|
|
1326
|
+
while (remaining.length > 0) {
|
|
1327
|
+
if (remaining.length <= MAX_MESSAGE_LENGTH) {
|
|
1328
|
+
chunks.push(remaining);
|
|
1329
|
+
break;
|
|
1330
|
+
}
|
|
1331
|
+
let splitAt = remaining.lastIndexOf("\n", MAX_MESSAGE_LENGTH);
|
|
1332
|
+
if (splitAt === -1 || splitAt < MAX_MESSAGE_LENGTH * 0.5) {
|
|
1333
|
+
splitAt = remaining.lastIndexOf(" ", MAX_MESSAGE_LENGTH);
|
|
1334
|
+
}
|
|
1335
|
+
if (splitAt === -1 || splitAt < MAX_MESSAGE_LENGTH * 0.5) {
|
|
1336
|
+
splitAt = MAX_MESSAGE_LENGTH;
|
|
1337
|
+
}
|
|
1338
|
+
chunks.push(remaining.slice(0, splitAt));
|
|
1339
|
+
remaining = remaining.slice(splitAt).trimStart();
|
|
1340
|
+
}
|
|
1341
|
+
return chunks;
|
|
1342
|
+
}
|
|
1343
|
+
function sleep(ms) {
|
|
1344
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
// src/voice-transcriber.ts
|
|
1348
|
+
var TARGET_SAMPLE_RATE = 16e3;
|
|
1349
|
+
var MODEL_ID = "Xenova/whisper-tiny.en";
|
|
1350
|
+
var transcriber = null;
|
|
1351
|
+
var loadPromise = null;
|
|
1352
|
+
var onProgress = null;
|
|
1353
|
+
function setProgressCallback(cb) {
|
|
1354
|
+
onProgress = cb;
|
|
1355
|
+
}
|
|
1356
|
+
function resample(audio, fromRate, toRate) {
|
|
1357
|
+
if (fromRate === toRate) return audio;
|
|
1358
|
+
const ratio = fromRate / toRate;
|
|
1359
|
+
const newLength = Math.round(audio.length / ratio);
|
|
1360
|
+
const result = new Float32Array(newLength);
|
|
1361
|
+
for (let i = 0; i < newLength; i++) {
|
|
1362
|
+
const srcIndex = i * ratio;
|
|
1363
|
+
const low = Math.floor(srcIndex);
|
|
1364
|
+
const high = Math.min(low + 1, audio.length - 1);
|
|
1365
|
+
const frac = srcIndex - low;
|
|
1366
|
+
result[i] = audio[low] * (1 - frac) + audio[high] * frac;
|
|
1367
|
+
}
|
|
1368
|
+
return result;
|
|
1369
|
+
}
|
|
1370
|
+
function downmixToMono(channelData) {
|
|
1371
|
+
if (channelData.length === 0) return new Float32Array();
|
|
1372
|
+
if (channelData.length === 1) return channelData[0];
|
|
1373
|
+
const samples = channelData[0].length;
|
|
1374
|
+
const out = new Float32Array(samples);
|
|
1375
|
+
const scale = 1 / channelData.length;
|
|
1376
|
+
for (let i = 0; i < samples; i++) {
|
|
1377
|
+
let mixed = 0;
|
|
1378
|
+
for (const channel of channelData) mixed += channel[i] ?? 0;
|
|
1379
|
+
out[i] = mixed * scale;
|
|
1380
|
+
}
|
|
1381
|
+
return out;
|
|
1382
|
+
}
|
|
1383
|
+
async function decodeOggOpus(buffer) {
|
|
1384
|
+
const { OggOpusDecoder } = await import("ogg-opus-decoder");
|
|
1385
|
+
const decoder = new OggOpusDecoder();
|
|
1386
|
+
await decoder.ready;
|
|
1387
|
+
try {
|
|
1388
|
+
const decoded = await decoder.decodeFile(buffer);
|
|
1389
|
+
if (!decoded.channelData?.length || !decoded.channelData[0]?.length) {
|
|
1390
|
+
throw new Error("Decoded audio is empty");
|
|
1391
|
+
}
|
|
1392
|
+
const mono = downmixToMono(decoded.channelData);
|
|
1393
|
+
return resample(mono, decoded.sampleRate, TARGET_SAMPLE_RATE);
|
|
1394
|
+
} finally {
|
|
1395
|
+
decoder.free();
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
async function getTranscriber() {
|
|
1399
|
+
if (transcriber) return transcriber;
|
|
1400
|
+
if (!loadPromise) {
|
|
1401
|
+
loadPromise = (async () => {
|
|
1402
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
1403
|
+
const instance = await pipeline("automatic-speech-recognition", MODEL_ID, {
|
|
1404
|
+
dtype: "fp32",
|
|
1405
|
+
progress_callback: onProgress ?? void 0
|
|
1406
|
+
});
|
|
1407
|
+
transcriber = instance;
|
|
1408
|
+
return instance;
|
|
1409
|
+
})();
|
|
1410
|
+
}
|
|
1411
|
+
return loadPromise;
|
|
1412
|
+
}
|
|
1413
|
+
function isModelLoaded() {
|
|
1414
|
+
return transcriber !== null;
|
|
1415
|
+
}
|
|
1416
|
+
async function transcribeVoice(fileUrl) {
|
|
1417
|
+
const response = await fetch(fileUrl);
|
|
1418
|
+
if (!response.ok) throw new Error(`Failed to download voice file: ${response.status}`);
|
|
1419
|
+
const buffer = new Uint8Array(await response.arrayBuffer());
|
|
1420
|
+
const pcm = await decodeOggOpus(buffer);
|
|
1421
|
+
const asr = await getTranscriber();
|
|
1422
|
+
const result = await asr(pcm);
|
|
1423
|
+
const text = Array.isArray(result) ? result[0]?.text : result.text;
|
|
1424
|
+
return (text ?? "").trim();
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
// src/auto-update.ts
|
|
1428
|
+
import { spawn } from "child_process";
|
|
1429
|
+
import fs5 from "fs";
|
|
1430
|
+
import path3 from "path";
|
|
1431
|
+
var CHECK_INTERVAL_MS = 60 * 60 * 1e3;
|
|
1432
|
+
var FETCH_TIMEOUT_MS2 = 1e4;
|
|
1433
|
+
function compareVersions(a, b) {
|
|
1434
|
+
const pa = a.split(".").map(Number);
|
|
1435
|
+
const pb = b.split(".").map(Number);
|
|
1436
|
+
for (let i = 0; i < 3; i++) {
|
|
1437
|
+
const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
1438
|
+
if (diff !== 0) return diff;
|
|
1439
|
+
}
|
|
1440
|
+
return 0;
|
|
1441
|
+
}
|
|
1442
|
+
function performUpdateInBackground(command) {
|
|
1443
|
+
try {
|
|
1444
|
+
const parts = command.split(" ");
|
|
1445
|
+
const child = spawn(parts[0], parts.slice(1), {
|
|
1446
|
+
detached: true,
|
|
1447
|
+
stdio: "ignore",
|
|
1448
|
+
env: { ...process.env, npm_config_loglevel: "silent" }
|
|
1449
|
+
});
|
|
1450
|
+
child.unref();
|
|
1451
|
+
} catch {
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
function createAutoUpdater(config) {
|
|
1455
|
+
const REGISTRY_URL = `https://registry.npmjs.org/${config.packageName}/latest`;
|
|
1456
|
+
const periodicMessage = config.periodicMessage ?? (({ currentVersion, latestVersion, updateCommand }) => `Ken just pushed a fresh update \u2014 ${currentVersion} \u2192 ${latestVersion}! I'll grab it on next launch (or run ${updateCommand} if you can't wait).`);
|
|
1457
|
+
let periodicTimer = null;
|
|
1458
|
+
function stateFilePath() {
|
|
1459
|
+
return typeof config.stateFilePath === "function" ? config.stateFilePath() : config.stateFilePath;
|
|
1460
|
+
}
|
|
1461
|
+
function readState() {
|
|
1462
|
+
try {
|
|
1463
|
+
const raw = fs5.readFileSync(stateFilePath(), "utf-8");
|
|
1464
|
+
return JSON.parse(raw);
|
|
1465
|
+
} catch {
|
|
1466
|
+
return null;
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
function writeState(state) {
|
|
1470
|
+
try {
|
|
1471
|
+
const filePath = stateFilePath();
|
|
1472
|
+
fs5.mkdirSync(path3.dirname(filePath), { recursive: true, mode: 448 });
|
|
1473
|
+
fs5.writeFileSync(filePath, JSON.stringify(state));
|
|
1474
|
+
} catch {
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
function detectInstallInfo() {
|
|
1478
|
+
const scriptPath = (process.argv[1] ?? "").replace(/\\/g, "/");
|
|
1479
|
+
if (scriptPath.includes("/_npx/")) {
|
|
1480
|
+
return { packageManager: "unknown" /* UNKNOWN */, updateCommand: null };
|
|
1481
|
+
}
|
|
1482
|
+
if (scriptPath.includes("/.pnpm") || scriptPath.includes("/pnpm/global")) {
|
|
1483
|
+
return {
|
|
1484
|
+
packageManager: "pnpm" /* PNPM */,
|
|
1485
|
+
updateCommand: `pnpm add -g ${config.packageName}@latest`
|
|
1486
|
+
};
|
|
1487
|
+
}
|
|
1488
|
+
if (scriptPath.includes("/.yarn/") || scriptPath.includes("/yarn/global")) {
|
|
1489
|
+
return {
|
|
1490
|
+
packageManager: "yarn" /* YARN */,
|
|
1491
|
+
updateCommand: `yarn global add ${config.packageName}@latest`
|
|
1492
|
+
};
|
|
1493
|
+
}
|
|
1494
|
+
return {
|
|
1495
|
+
packageManager: "npm" /* NPM */,
|
|
1496
|
+
updateCommand: `npm install -g ${config.packageName}@latest`
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
async function fetchLatestVersion() {
|
|
1500
|
+
try {
|
|
1501
|
+
const controller = new AbortController();
|
|
1502
|
+
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS2);
|
|
1503
|
+
const response = await fetch(REGISTRY_URL, { signal: controller.signal });
|
|
1504
|
+
clearTimeout(timeout);
|
|
1505
|
+
const data = await response.json();
|
|
1506
|
+
const version = data.version?.trim();
|
|
1507
|
+
return version && /^\d+\.\d+\.\d+/.test(version) ? version : null;
|
|
1508
|
+
} catch {
|
|
1509
|
+
return null;
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
function scheduleBackgroundCheck(currentVersion) {
|
|
1513
|
+
fetchLatestVersion().then((latestVersion) => {
|
|
1514
|
+
const newState = {
|
|
1515
|
+
lastCheckedAt: Date.now(),
|
|
1516
|
+
latestVersion: latestVersion ?? void 0,
|
|
1517
|
+
updatePending: false
|
|
1518
|
+
};
|
|
1519
|
+
if (latestVersion && compareVersions(latestVersion, currentVersion) > 0) {
|
|
1520
|
+
newState.updatePending = true;
|
|
1521
|
+
}
|
|
1522
|
+
writeState(newState);
|
|
1523
|
+
}).catch(() => {
|
|
1524
|
+
});
|
|
1525
|
+
}
|
|
1526
|
+
function checkAndAutoUpdate(currentVersion) {
|
|
1527
|
+
try {
|
|
1528
|
+
const state = readState();
|
|
1529
|
+
let message = null;
|
|
1530
|
+
if (state?.updatePending && state.latestVersion) {
|
|
1531
|
+
if (compareVersions(state.latestVersion, currentVersion) > 0) {
|
|
1532
|
+
const info = detectInstallInfo();
|
|
1533
|
+
if (info.updateCommand) {
|
|
1534
|
+
performUpdateInBackground(info.updateCommand);
|
|
1535
|
+
message = `Ken just shipped ${state.latestVersion}! Installing in the background \u2014 takes effect next launch.`;
|
|
1536
|
+
writeState({
|
|
1537
|
+
...state,
|
|
1538
|
+
lastCheckedAt: Date.now(),
|
|
1539
|
+
updatePending: false,
|
|
1540
|
+
lastUpdateAttempt: Date.now()
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
} else {
|
|
1544
|
+
writeState({ ...state, updatePending: false });
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
const shouldCheck = !state || Date.now() - state.lastCheckedAt > CHECK_INTERVAL_MS;
|
|
1548
|
+
if (shouldCheck) scheduleBackgroundCheck(currentVersion);
|
|
1549
|
+
return message;
|
|
1550
|
+
} catch {
|
|
1551
|
+
return null;
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
function getPendingUpdate(currentVersion) {
|
|
1555
|
+
try {
|
|
1556
|
+
const state = readState();
|
|
1557
|
+
if (!state?.latestVersion) return null;
|
|
1558
|
+
if (compareVersions(state.latestVersion, currentVersion) <= 0) return null;
|
|
1559
|
+
return { latestVersion: state.latestVersion };
|
|
1560
|
+
} catch {
|
|
1561
|
+
return null;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
function startPeriodicUpdateCheck(currentVersion, onUpdate) {
|
|
1565
|
+
if (periodicTimer) return;
|
|
1566
|
+
periodicTimer = setInterval(() => {
|
|
1567
|
+
fetchLatestVersion().then((latestVersion) => {
|
|
1568
|
+
if (!latestVersion) return;
|
|
1569
|
+
if (compareVersions(latestVersion, currentVersion) <= 0) return;
|
|
1570
|
+
const info = detectInstallInfo();
|
|
1571
|
+
if (!info.updateCommand) return;
|
|
1572
|
+
writeState({ lastCheckedAt: Date.now(), latestVersion, updatePending: true });
|
|
1573
|
+
onUpdate(
|
|
1574
|
+
periodicMessage({ currentVersion, latestVersion, updateCommand: info.updateCommand })
|
|
1575
|
+
);
|
|
1576
|
+
stopPeriodicUpdateCheck();
|
|
1577
|
+
}).catch(() => {
|
|
1578
|
+
});
|
|
1579
|
+
}, CHECK_INTERVAL_MS);
|
|
1580
|
+
periodicTimer.unref();
|
|
1581
|
+
}
|
|
1582
|
+
function stopPeriodicUpdateCheck() {
|
|
1583
|
+
if (periodicTimer) {
|
|
1584
|
+
clearInterval(periodicTimer);
|
|
1585
|
+
periodicTimer = null;
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
return {
|
|
1589
|
+
checkAndAutoUpdate,
|
|
1590
|
+
getPendingUpdate,
|
|
1591
|
+
startPeriodicUpdateCheck,
|
|
1592
|
+
stopPeriodicUpdateCheck
|
|
1593
|
+
};
|
|
1594
|
+
}
|
|
1595
|
+
export {
|
|
1596
|
+
AuthStorage,
|
|
1597
|
+
MODELS,
|
|
1598
|
+
NotLoggedInError,
|
|
1599
|
+
TelegramBot,
|
|
1600
|
+
closeLogger,
|
|
1601
|
+
createAutoUpdater,
|
|
1602
|
+
decodeOggOpus,
|
|
1603
|
+
downmixToMono,
|
|
1604
|
+
generatePKCE,
|
|
1605
|
+
getAppPaths,
|
|
1606
|
+
getClaudeCliUserAgent,
|
|
1607
|
+
getClaudeCodeVersion,
|
|
1608
|
+
getContextWindow,
|
|
1609
|
+
getDefaultModel,
|
|
1610
|
+
getMaxThinkingLevel,
|
|
1611
|
+
getModel,
|
|
1612
|
+
getModelsForProvider,
|
|
1613
|
+
getNextThinkingLevel,
|
|
1614
|
+
getSessionId,
|
|
1615
|
+
getSummaryModel,
|
|
1616
|
+
getSupportedThinkingLevels,
|
|
1617
|
+
isLoggerOpen,
|
|
1618
|
+
isModelLoaded,
|
|
1619
|
+
isThinkingLevelSupported,
|
|
1620
|
+
log,
|
|
1621
|
+
loginAnthropic,
|
|
1622
|
+
loginGemini,
|
|
1623
|
+
loginOpenAI,
|
|
1624
|
+
openLog,
|
|
1625
|
+
refreshAnthropicToken,
|
|
1626
|
+
refreshGeminiToken,
|
|
1627
|
+
refreshOpenAIToken,
|
|
1628
|
+
registerLogCleanup,
|
|
1629
|
+
resample,
|
|
1630
|
+
setProgressCallback,
|
|
1631
|
+
transcribeVoice,
|
|
1632
|
+
usesOpenAICodexTransport,
|
|
1633
|
+
withFileLock
|
|
1634
|
+
};
|
|
1635
|
+
//# sourceMappingURL=index.js.map
|