@kenkaiiii/gg-core 6.4.0 → 7.1.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/README.md +20 -9
- package/dist/{chunk-VREIRQMU.js → chunk-BMRRMRUO.js} +19 -14
- package/dist/chunk-BMRRMRUO.js.map +1 -0
- package/dist/{chunk-GXOFTHM5.js → chunk-DNNLWE2F.js} +7 -2
- package/dist/chunk-DNNLWE2F.js.map +1 -0
- package/dist/index.cjs +36 -538
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -182
- package/dist/index.d.ts +6 -182
- package/dist/index.js +6 -510
- package/dist/index.js.map +1 -1
- package/dist/model-registry.cjs +3 -2
- package/dist/model-registry.cjs.map +1 -1
- package/dist/model-registry.js +2 -2
- package/dist/paths.cjs +7 -1
- package/dist/paths.cjs.map +1 -1
- package/dist/paths.d.cts +4 -2
- package/dist/paths.d.ts +4 -2
- package/dist/paths.js +3 -1
- package/package.json +5 -6
- package/dist/chunk-GXOFTHM5.js.map +0 -1
- package/dist/chunk-VREIRQMU.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AuthStorage,
|
|
3
3
|
CHATGPT_PLAN_BASE_URL,
|
|
4
|
-
CHATGPT_PLAN_NOT_ENABLED_MESSAGE,
|
|
5
4
|
CHATGPT_PLAN_SCOPE,
|
|
6
5
|
CHATGPT_USAGE_URL,
|
|
7
6
|
CLAUDE_CODE_AUTH_KEY,
|
|
@@ -89,10 +88,11 @@ import {
|
|
|
89
88
|
verifyIdToken,
|
|
90
89
|
withFileLock,
|
|
91
90
|
withoutPaidTier
|
|
92
|
-
} from "./chunk-
|
|
91
|
+
} from "./chunk-BMRRMRUO.js";
|
|
93
92
|
import {
|
|
93
|
+
configureAppDataDir,
|
|
94
94
|
getAppPaths
|
|
95
|
-
} from "./chunk-
|
|
95
|
+
} from "./chunk-DNNLWE2F.js";
|
|
96
96
|
|
|
97
97
|
// src/openrouter-catalog.ts
|
|
98
98
|
var OPENROUTER_DEFAULT_BASE_URL = "https://openrouter.ai/api/v1";
|
|
@@ -1059,7 +1059,7 @@ function authPriorityNote(provider) {
|
|
|
1059
1059
|
}
|
|
1060
1060
|
|
|
1061
1061
|
// src/provider-usage.ts
|
|
1062
|
-
import {
|
|
1062
|
+
import { getAppIdentity } from "@kenkaiiii/gg-ai";
|
|
1063
1063
|
var SubscriptionUsageError = class extends Error {
|
|
1064
1064
|
constructor(message, status, retryAfterMs2) {
|
|
1065
1065
|
super(message);
|
|
@@ -1137,7 +1137,7 @@ async function fetchKimiUsage(credentials, fetchFn, signal, now) {
|
|
|
1137
1137
|
headers: {
|
|
1138
1138
|
Authorization: "Bearer " + credentials.accessToken,
|
|
1139
1139
|
Accept: "application/json",
|
|
1140
|
-
"User-Agent":
|
|
1140
|
+
"User-Agent": getAppIdentity().userAgent
|
|
1141
1141
|
}
|
|
1142
1142
|
});
|
|
1143
1143
|
const data = await readUsageResponse(response, now());
|
|
@@ -1196,507 +1196,10 @@ async function fetchSubscriptionUsage(provider, credentials, options = {}) {
|
|
|
1196
1196
|
throw new SubscriptionUsageError(message);
|
|
1197
1197
|
}
|
|
1198
1198
|
}
|
|
1199
|
-
|
|
1200
|
-
// src/telegram.ts
|
|
1201
|
-
var TELEGRAM_API = "https://api.telegram.org";
|
|
1202
|
-
var LONG_POLL_SECONDS = 30;
|
|
1203
|
-
var API_TIMEOUT_MS = 3e4;
|
|
1204
|
-
var MAX_MESSAGE_LENGTH = 4096;
|
|
1205
|
-
var TelegramBot = class {
|
|
1206
|
-
token;
|
|
1207
|
-
allowedUserId;
|
|
1208
|
-
offset = 0;
|
|
1209
|
-
running = false;
|
|
1210
|
-
/** Aborts the in-flight long poll so stop() takes effect immediately. */
|
|
1211
|
-
pollAbort = null;
|
|
1212
|
-
onMessage = null;
|
|
1213
|
-
onVoiceMessage = null;
|
|
1214
|
-
onCallback = null;
|
|
1215
|
-
onBotAdded = null;
|
|
1216
|
-
onBotRemoved = null;
|
|
1217
|
-
constructor(config) {
|
|
1218
|
-
this.token = config.botToken;
|
|
1219
|
-
this.allowedUserId = config.allowedUserId;
|
|
1220
|
-
}
|
|
1221
|
-
/** Register handler for incoming text messages. */
|
|
1222
|
-
onText(handler) {
|
|
1223
|
-
this.onMessage = handler;
|
|
1224
|
-
}
|
|
1225
|
-
/** Register handler for incoming voice notes. */
|
|
1226
|
-
onVoice(handler) {
|
|
1227
|
-
this.onVoiceMessage = handler;
|
|
1228
|
-
}
|
|
1229
|
-
/** Register handler for inline keyboard button presses. */
|
|
1230
|
-
onCallbackQuery(handler) {
|
|
1231
|
-
this.onCallback = handler;
|
|
1232
|
-
}
|
|
1233
|
-
/** Register handler for when the bot is added to a group. */
|
|
1234
|
-
onAddedToGroup(handler) {
|
|
1235
|
-
this.onBotAdded = handler;
|
|
1236
|
-
}
|
|
1237
|
-
/** Register handler for when the bot is removed from a group. */
|
|
1238
|
-
onRemovedFromGroup(handler) {
|
|
1239
|
-
this.onBotRemoved = handler;
|
|
1240
|
-
}
|
|
1241
|
-
/** Start long polling. Blocks until stop() is called. */
|
|
1242
|
-
async start() {
|
|
1243
|
-
this.running = true;
|
|
1244
|
-
const me = await this.apiCall("getMe");
|
|
1245
|
-
if (!me.ok) {
|
|
1246
|
-
throw new Error(`Invalid bot token: ${JSON.stringify(me)}`);
|
|
1247
|
-
}
|
|
1248
|
-
while (this.running) {
|
|
1249
|
-
try {
|
|
1250
|
-
const updates = await this.getUpdates();
|
|
1251
|
-
for (const update of updates) {
|
|
1252
|
-
await this.handleUpdate(update);
|
|
1253
|
-
}
|
|
1254
|
-
} catch (err) {
|
|
1255
|
-
if (!this.running) break;
|
|
1256
|
-
console.error(`[telegram] Poll error: ${err instanceof Error ? err.message : err}`);
|
|
1257
|
-
await sleep(3e3);
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
}
|
|
1261
|
-
/** Stop long polling. */
|
|
1262
|
-
stop() {
|
|
1263
|
-
this.running = false;
|
|
1264
|
-
this.pollAbort?.abort();
|
|
1265
|
-
}
|
|
1266
|
-
/** Send a text message to a specific chat. Converts markdown and splits long messages. */
|
|
1267
|
-
async send(chatId, text, buttons) {
|
|
1268
|
-
const converted = toTelegramMarkdown(text);
|
|
1269
|
-
const chunks = splitMessage(converted);
|
|
1270
|
-
for (let i = 0; i < chunks.length; i++) {
|
|
1271
|
-
const isLast = i === chunks.length - 1;
|
|
1272
|
-
const replyMarkup = isLast && buttons ? {
|
|
1273
|
-
inline_keyboard: buttons.map(
|
|
1274
|
-
(row) => row.map((b) => ({ text: b.text, callback_data: b.callback_data }))
|
|
1275
|
-
)
|
|
1276
|
-
} : void 0;
|
|
1277
|
-
await this.apiCall("sendMessage", {
|
|
1278
|
-
chat_id: chatId,
|
|
1279
|
-
text: chunks[i],
|
|
1280
|
-
parse_mode: "Markdown",
|
|
1281
|
-
...replyMarkup ? { reply_markup: replyMarkup } : {}
|
|
1282
|
-
});
|
|
1283
|
-
}
|
|
1284
|
-
}
|
|
1285
|
-
/** Send a plain text message (no markdown parsing) to a specific chat. */
|
|
1286
|
-
async sendPlain(chatId, text) {
|
|
1287
|
-
const chunks = splitMessage(text);
|
|
1288
|
-
for (const chunk of chunks) {
|
|
1289
|
-
await this.apiCall("sendMessage", {
|
|
1290
|
-
chat_id: chatId,
|
|
1291
|
-
text: chunk
|
|
1292
|
-
});
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
/** Send a typing indicator to a specific chat. */
|
|
1296
|
-
async sendTyping(chatId) {
|
|
1297
|
-
await this.apiCall("sendChatAction", {
|
|
1298
|
-
chat_id: chatId,
|
|
1299
|
-
action: "typing"
|
|
1300
|
-
});
|
|
1301
|
-
}
|
|
1302
|
-
/** Get a direct download URL for a Telegram file. */
|
|
1303
|
-
async getFileUrl(fileId) {
|
|
1304
|
-
const result = await this.apiCall("getFile", { file_id: fileId });
|
|
1305
|
-
if (!result.ok) throw new Error(`Failed to get file: ${JSON.stringify(result)}`);
|
|
1306
|
-
const filePath = result.result.file_path;
|
|
1307
|
-
return `${TELEGRAM_API}/file/bot${this.token}/${filePath}`;
|
|
1308
|
-
}
|
|
1309
|
-
// ── Private ───────────────────────────────────────────
|
|
1310
|
-
async getUpdates() {
|
|
1311
|
-
const poll = new AbortController();
|
|
1312
|
-
this.pollAbort = poll;
|
|
1313
|
-
let result;
|
|
1314
|
-
try {
|
|
1315
|
-
result = await this.apiCall(
|
|
1316
|
-
"getUpdates",
|
|
1317
|
-
{
|
|
1318
|
-
offset: this.offset,
|
|
1319
|
-
timeout: LONG_POLL_SECONDS,
|
|
1320
|
-
allowed_updates: ["message", "callback_query", "my_chat_member"]
|
|
1321
|
-
},
|
|
1322
|
-
// A half-open socket would otherwise hang the poll forever.
|
|
1323
|
-
AbortSignal.any([poll.signal, AbortSignal.timeout((LONG_POLL_SECONDS + 15) * 1e3)])
|
|
1324
|
-
);
|
|
1325
|
-
} finally {
|
|
1326
|
-
if (this.pollAbort === poll) this.pollAbort = null;
|
|
1327
|
-
}
|
|
1328
|
-
if (!result.ok || !Array.isArray(result.result)) {
|
|
1329
|
-
throw new Error(`getUpdates failed: ${JSON.stringify(result)}`);
|
|
1330
|
-
}
|
|
1331
|
-
const updates = result.result;
|
|
1332
|
-
if (updates.length > 0) {
|
|
1333
|
-
this.offset = updates[updates.length - 1].update_id + 1;
|
|
1334
|
-
}
|
|
1335
|
-
return updates;
|
|
1336
|
-
}
|
|
1337
|
-
async handleUpdate(update) {
|
|
1338
|
-
if (update.message) {
|
|
1339
|
-
const msg = update.message;
|
|
1340
|
-
if (msg.from.id !== this.allowedUserId) {
|
|
1341
|
-
return;
|
|
1342
|
-
}
|
|
1343
|
-
if (msg.text && this.onMessage) {
|
|
1344
|
-
this.onMessage({
|
|
1345
|
-
text: msg.text,
|
|
1346
|
-
chatId: msg.chat.id,
|
|
1347
|
-
chatType: msg.chat.type,
|
|
1348
|
-
chatTitle: msg.chat.title
|
|
1349
|
-
});
|
|
1350
|
-
} else if (msg.voice && this.onVoiceMessage) {
|
|
1351
|
-
this.onVoiceMessage({
|
|
1352
|
-
fileId: msg.voice.file_id,
|
|
1353
|
-
duration: msg.voice.duration,
|
|
1354
|
-
chatId: msg.chat.id,
|
|
1355
|
-
chatType: msg.chat.type,
|
|
1356
|
-
chatTitle: msg.chat.title
|
|
1357
|
-
});
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
if (update.my_chat_member) {
|
|
1361
|
-
const member = update.my_chat_member;
|
|
1362
|
-
const status = member.new_chat_member.status;
|
|
1363
|
-
if ((status === "member" || status === "administrator") && this.onBotAdded) {
|
|
1364
|
-
this.onBotAdded(member.chat.id, member.chat.title);
|
|
1365
|
-
} else if ((status === "left" || status === "kicked") && this.onBotRemoved) {
|
|
1366
|
-
this.onBotRemoved(member.chat.id);
|
|
1367
|
-
}
|
|
1368
|
-
}
|
|
1369
|
-
if (update.callback_query) {
|
|
1370
|
-
const cb = update.callback_query;
|
|
1371
|
-
if (cb.from.id !== this.allowedUserId) return;
|
|
1372
|
-
await this.apiCall("answerCallbackQuery", { callback_query_id: cb.id });
|
|
1373
|
-
if (cb.data && this.onCallback) {
|
|
1374
|
-
this.onCallback(cb.data, cb.message.chat.id);
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
async apiCall(method, body, signal = AbortSignal.timeout(API_TIMEOUT_MS)) {
|
|
1379
|
-
const url = `${TELEGRAM_API}/bot${this.token}/${method}`;
|
|
1380
|
-
const response = await fetch(url, {
|
|
1381
|
-
method: "POST",
|
|
1382
|
-
headers: { "Content-Type": "application/json" },
|
|
1383
|
-
body: body ? JSON.stringify(body) : void 0,
|
|
1384
|
-
signal
|
|
1385
|
-
});
|
|
1386
|
-
if (!response.ok) {
|
|
1387
|
-
return { ok: false };
|
|
1388
|
-
}
|
|
1389
|
-
return response.json();
|
|
1390
|
-
}
|
|
1391
|
-
};
|
|
1392
|
-
function toTelegramMarkdown(text) {
|
|
1393
|
-
const lines = text.split("\n");
|
|
1394
|
-
const result = [];
|
|
1395
|
-
let inCodeBlock = false;
|
|
1396
|
-
for (const line of lines) {
|
|
1397
|
-
if (line.trimStart().startsWith("```")) {
|
|
1398
|
-
inCodeBlock = !inCodeBlock;
|
|
1399
|
-
result.push(line);
|
|
1400
|
-
continue;
|
|
1401
|
-
}
|
|
1402
|
-
if (inCodeBlock) {
|
|
1403
|
-
result.push(line);
|
|
1404
|
-
continue;
|
|
1405
|
-
}
|
|
1406
|
-
let transformed = line;
|
|
1407
|
-
const headingMatch = transformed.match(/^(#{1,6})\s+(.+)$/);
|
|
1408
|
-
if (headingMatch) {
|
|
1409
|
-
transformed = `*${headingMatch[2]}*`;
|
|
1410
|
-
result.push(transformed);
|
|
1411
|
-
continue;
|
|
1412
|
-
}
|
|
1413
|
-
if (/^(-{3,}|_{3,}|\*{3,})$/.test(transformed.trim())) {
|
|
1414
|
-
result.push("");
|
|
1415
|
-
continue;
|
|
1416
|
-
}
|
|
1417
|
-
transformed = transformed.replace(/\*\*(.+?)\*\*/g, "*$1*");
|
|
1418
|
-
result.push(transformed);
|
|
1419
|
-
}
|
|
1420
|
-
return result.join("\n");
|
|
1421
|
-
}
|
|
1422
|
-
function splitMessage(text) {
|
|
1423
|
-
if (text.length <= MAX_MESSAGE_LENGTH) return [text];
|
|
1424
|
-
const chunks = [];
|
|
1425
|
-
let remaining = text;
|
|
1426
|
-
while (remaining.length > 0) {
|
|
1427
|
-
if (remaining.length <= MAX_MESSAGE_LENGTH) {
|
|
1428
|
-
chunks.push(remaining);
|
|
1429
|
-
break;
|
|
1430
|
-
}
|
|
1431
|
-
let splitAt = remaining.lastIndexOf("\n", MAX_MESSAGE_LENGTH);
|
|
1432
|
-
if (splitAt === -1 || splitAt < MAX_MESSAGE_LENGTH * 0.5) {
|
|
1433
|
-
splitAt = remaining.lastIndexOf(" ", MAX_MESSAGE_LENGTH);
|
|
1434
|
-
}
|
|
1435
|
-
if (splitAt === -1 || splitAt < MAX_MESSAGE_LENGTH * 0.5) {
|
|
1436
|
-
splitAt = MAX_MESSAGE_LENGTH;
|
|
1437
|
-
}
|
|
1438
|
-
chunks.push(remaining.slice(0, splitAt));
|
|
1439
|
-
remaining = remaining.slice(splitAt).trimStart();
|
|
1440
|
-
}
|
|
1441
|
-
return chunks;
|
|
1442
|
-
}
|
|
1443
|
-
function sleep(ms) {
|
|
1444
|
-
return new Promise((r) => setTimeout(r, ms));
|
|
1445
|
-
}
|
|
1446
|
-
|
|
1447
|
-
// src/voice-transcriber.ts
|
|
1448
|
-
var TARGET_SAMPLE_RATE = 16e3;
|
|
1449
|
-
var MODEL_ID = "Xenova/whisper-tiny.en";
|
|
1450
|
-
var transcriber = null;
|
|
1451
|
-
var loadPromise = null;
|
|
1452
|
-
var onProgress = null;
|
|
1453
|
-
function setProgressCallback(cb) {
|
|
1454
|
-
onProgress = cb;
|
|
1455
|
-
}
|
|
1456
|
-
function resample(audio, fromRate, toRate) {
|
|
1457
|
-
if (fromRate === toRate) return audio;
|
|
1458
|
-
const ratio = fromRate / toRate;
|
|
1459
|
-
const newLength = Math.round(audio.length / ratio);
|
|
1460
|
-
const result = new Float32Array(newLength);
|
|
1461
|
-
for (let i = 0; i < newLength; i++) {
|
|
1462
|
-
const srcIndex = i * ratio;
|
|
1463
|
-
const low = Math.floor(srcIndex);
|
|
1464
|
-
const high = Math.min(low + 1, audio.length - 1);
|
|
1465
|
-
const frac = srcIndex - low;
|
|
1466
|
-
result[i] = audio[low] * (1 - frac) + audio[high] * frac;
|
|
1467
|
-
}
|
|
1468
|
-
return result;
|
|
1469
|
-
}
|
|
1470
|
-
function downmixToMono(channelData) {
|
|
1471
|
-
if (channelData.length === 0) return new Float32Array();
|
|
1472
|
-
if (channelData.length === 1) return channelData[0];
|
|
1473
|
-
const samples = channelData[0].length;
|
|
1474
|
-
const out = new Float32Array(samples);
|
|
1475
|
-
const scale = 1 / channelData.length;
|
|
1476
|
-
for (let i = 0; i < samples; i++) {
|
|
1477
|
-
let mixed = 0;
|
|
1478
|
-
for (const channel of channelData) mixed += channel[i] ?? 0;
|
|
1479
|
-
out[i] = mixed * scale;
|
|
1480
|
-
}
|
|
1481
|
-
return out;
|
|
1482
|
-
}
|
|
1483
|
-
async function decodeOggOpus(buffer) {
|
|
1484
|
-
const { OggOpusDecoder } = await import("ogg-opus-decoder");
|
|
1485
|
-
const decoder = new OggOpusDecoder();
|
|
1486
|
-
await decoder.ready;
|
|
1487
|
-
try {
|
|
1488
|
-
const decoded = await decoder.decodeFile(buffer);
|
|
1489
|
-
if (!decoded.channelData?.length || !decoded.channelData[0]?.length) {
|
|
1490
|
-
throw new Error("Decoded audio is empty");
|
|
1491
|
-
}
|
|
1492
|
-
const mono = downmixToMono(decoded.channelData);
|
|
1493
|
-
return resample(mono, decoded.sampleRate, TARGET_SAMPLE_RATE);
|
|
1494
|
-
} finally {
|
|
1495
|
-
decoder.free();
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
async function getTranscriber() {
|
|
1499
|
-
if (transcriber) return transcriber;
|
|
1500
|
-
if (!loadPromise) {
|
|
1501
|
-
loadPromise = (async () => {
|
|
1502
|
-
const { pipeline } = await import("@huggingface/transformers");
|
|
1503
|
-
const instance = await pipeline("automatic-speech-recognition", MODEL_ID, {
|
|
1504
|
-
dtype: "fp32",
|
|
1505
|
-
progress_callback: onProgress ?? void 0
|
|
1506
|
-
});
|
|
1507
|
-
transcriber = instance;
|
|
1508
|
-
return instance;
|
|
1509
|
-
})();
|
|
1510
|
-
}
|
|
1511
|
-
return loadPromise;
|
|
1512
|
-
}
|
|
1513
|
-
function isModelLoaded() {
|
|
1514
|
-
return transcriber !== null;
|
|
1515
|
-
}
|
|
1516
|
-
async function transcribeVoice(fileUrl) {
|
|
1517
|
-
const response = await fetch(fileUrl, { signal: AbortSignal.timeout(6e4) });
|
|
1518
|
-
if (!response.ok) throw new Error(`Failed to download voice file: ${response.status}`);
|
|
1519
|
-
const buffer = new Uint8Array(await response.arrayBuffer());
|
|
1520
|
-
const pcm = await decodeOggOpus(buffer);
|
|
1521
|
-
const asr = await getTranscriber();
|
|
1522
|
-
const result = await asr(pcm);
|
|
1523
|
-
const text = Array.isArray(result) ? result[0]?.text : result.text;
|
|
1524
|
-
return (text ?? "").trim();
|
|
1525
|
-
}
|
|
1526
|
-
|
|
1527
|
-
// src/auto-update.ts
|
|
1528
|
-
import { spawn } from "child_process";
|
|
1529
|
-
import fs from "fs";
|
|
1530
|
-
import path2 from "path";
|
|
1531
|
-
var CHECK_INTERVAL_MS = 60 * 60 * 1e3;
|
|
1532
|
-
var FETCH_TIMEOUT_MS = 1e4;
|
|
1533
|
-
function compareVersions(a, b) {
|
|
1534
|
-
const pa = a.split(".").map(Number);
|
|
1535
|
-
const pb = b.split(".").map(Number);
|
|
1536
|
-
for (let i = 0; i < 3; i++) {
|
|
1537
|
-
const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
1538
|
-
if (diff !== 0) return diff;
|
|
1539
|
-
}
|
|
1540
|
-
return 0;
|
|
1541
|
-
}
|
|
1542
|
-
function performUpdateInBackground(command) {
|
|
1543
|
-
try {
|
|
1544
|
-
const parts = command.split(" ");
|
|
1545
|
-
const child = spawn(parts[0], parts.slice(1), {
|
|
1546
|
-
detached: true,
|
|
1547
|
-
stdio: "ignore",
|
|
1548
|
-
env: { ...process.env, npm_config_loglevel: "silent" }
|
|
1549
|
-
});
|
|
1550
|
-
child.unref();
|
|
1551
|
-
} catch {
|
|
1552
|
-
}
|
|
1553
|
-
}
|
|
1554
|
-
function createAutoUpdater(config) {
|
|
1555
|
-
const REGISTRY_URL = `https://registry.npmjs.org/${config.packageName}/latest`;
|
|
1556
|
-
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).`);
|
|
1557
|
-
let periodicTimer = null;
|
|
1558
|
-
function stateFilePath() {
|
|
1559
|
-
return typeof config.stateFilePath === "function" ? config.stateFilePath() : config.stateFilePath;
|
|
1560
|
-
}
|
|
1561
|
-
function readState() {
|
|
1562
|
-
try {
|
|
1563
|
-
const raw = fs.readFileSync(stateFilePath(), "utf-8");
|
|
1564
|
-
return JSON.parse(raw);
|
|
1565
|
-
} catch {
|
|
1566
|
-
return null;
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
function writeState(state) {
|
|
1570
|
-
try {
|
|
1571
|
-
const filePath = stateFilePath();
|
|
1572
|
-
fs.mkdirSync(path2.dirname(filePath), { recursive: true, mode: 448 });
|
|
1573
|
-
fs.writeFileSync(filePath, JSON.stringify(state));
|
|
1574
|
-
} catch {
|
|
1575
|
-
}
|
|
1576
|
-
}
|
|
1577
|
-
function detectInstallInfo() {
|
|
1578
|
-
const scriptPath = (process.argv[1] ?? "").replace(/\\/g, "/");
|
|
1579
|
-
if (scriptPath.includes("/_npx/")) {
|
|
1580
|
-
return { packageManager: "unknown" /* UNKNOWN */, updateCommand: null };
|
|
1581
|
-
}
|
|
1582
|
-
if (scriptPath.includes("/.pnpm") || scriptPath.includes("/pnpm/global")) {
|
|
1583
|
-
return {
|
|
1584
|
-
packageManager: "pnpm" /* PNPM */,
|
|
1585
|
-
updateCommand: `pnpm add -g ${config.packageName}@latest`
|
|
1586
|
-
};
|
|
1587
|
-
}
|
|
1588
|
-
if (scriptPath.includes("/.yarn/") || scriptPath.includes("/yarn/global")) {
|
|
1589
|
-
return {
|
|
1590
|
-
packageManager: "yarn" /* YARN */,
|
|
1591
|
-
updateCommand: `yarn global add ${config.packageName}@latest`
|
|
1592
|
-
};
|
|
1593
|
-
}
|
|
1594
|
-
return {
|
|
1595
|
-
packageManager: "npm" /* NPM */,
|
|
1596
|
-
updateCommand: `npm install -g ${config.packageName}@latest`
|
|
1597
|
-
};
|
|
1598
|
-
}
|
|
1599
|
-
async function fetchLatestVersion() {
|
|
1600
|
-
try {
|
|
1601
|
-
const controller = new AbortController();
|
|
1602
|
-
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
1603
|
-
const response = await fetch(REGISTRY_URL, { signal: controller.signal });
|
|
1604
|
-
clearTimeout(timeout);
|
|
1605
|
-
const data = await response.json();
|
|
1606
|
-
const version = data.version?.trim();
|
|
1607
|
-
return version && /^\d+\.\d+\.\d+/.test(version) ? version : null;
|
|
1608
|
-
} catch {
|
|
1609
|
-
return null;
|
|
1610
|
-
}
|
|
1611
|
-
}
|
|
1612
|
-
function scheduleBackgroundCheck(currentVersion) {
|
|
1613
|
-
fetchLatestVersion().then((latestVersion) => {
|
|
1614
|
-
const newState = {
|
|
1615
|
-
lastCheckedAt: Date.now(),
|
|
1616
|
-
latestVersion: latestVersion ?? void 0,
|
|
1617
|
-
updatePending: false
|
|
1618
|
-
};
|
|
1619
|
-
if (latestVersion && compareVersions(latestVersion, currentVersion) > 0) {
|
|
1620
|
-
newState.updatePending = true;
|
|
1621
|
-
}
|
|
1622
|
-
writeState(newState);
|
|
1623
|
-
}).catch(() => {
|
|
1624
|
-
});
|
|
1625
|
-
}
|
|
1626
|
-
function checkAndAutoUpdate(currentVersion) {
|
|
1627
|
-
try {
|
|
1628
|
-
const state = readState();
|
|
1629
|
-
let message = null;
|
|
1630
|
-
if (state?.updatePending && state.latestVersion) {
|
|
1631
|
-
if (compareVersions(state.latestVersion, currentVersion) > 0) {
|
|
1632
|
-
const info = detectInstallInfo();
|
|
1633
|
-
if (info.updateCommand) {
|
|
1634
|
-
performUpdateInBackground(info.updateCommand);
|
|
1635
|
-
message = `Ken just shipped ${state.latestVersion}! Installing in the background \u2014 takes effect next launch.`;
|
|
1636
|
-
writeState({
|
|
1637
|
-
...state,
|
|
1638
|
-
lastCheckedAt: Date.now(),
|
|
1639
|
-
updatePending: false,
|
|
1640
|
-
lastUpdateAttempt: Date.now()
|
|
1641
|
-
});
|
|
1642
|
-
}
|
|
1643
|
-
} else {
|
|
1644
|
-
writeState({ ...state, updatePending: false });
|
|
1645
|
-
}
|
|
1646
|
-
}
|
|
1647
|
-
const shouldCheck = !state || Date.now() - state.lastCheckedAt > CHECK_INTERVAL_MS;
|
|
1648
|
-
if (shouldCheck) scheduleBackgroundCheck(currentVersion);
|
|
1649
|
-
return message;
|
|
1650
|
-
} catch {
|
|
1651
|
-
return null;
|
|
1652
|
-
}
|
|
1653
|
-
}
|
|
1654
|
-
function getPendingUpdate(currentVersion) {
|
|
1655
|
-
try {
|
|
1656
|
-
const state = readState();
|
|
1657
|
-
if (!state?.latestVersion) return null;
|
|
1658
|
-
if (compareVersions(state.latestVersion, currentVersion) <= 0) return null;
|
|
1659
|
-
return { latestVersion: state.latestVersion };
|
|
1660
|
-
} catch {
|
|
1661
|
-
return null;
|
|
1662
|
-
}
|
|
1663
|
-
}
|
|
1664
|
-
function startPeriodicUpdateCheck(currentVersion, onUpdate) {
|
|
1665
|
-
if (periodicTimer) return;
|
|
1666
|
-
periodicTimer = setInterval(() => {
|
|
1667
|
-
fetchLatestVersion().then((latestVersion) => {
|
|
1668
|
-
if (!latestVersion) return;
|
|
1669
|
-
if (compareVersions(latestVersion, currentVersion) <= 0) return;
|
|
1670
|
-
const info = detectInstallInfo();
|
|
1671
|
-
if (!info.updateCommand) return;
|
|
1672
|
-
writeState({ lastCheckedAt: Date.now(), latestVersion, updatePending: true });
|
|
1673
|
-
onUpdate(
|
|
1674
|
-
periodicMessage({ currentVersion, latestVersion, updateCommand: info.updateCommand })
|
|
1675
|
-
);
|
|
1676
|
-
stopPeriodicUpdateCheck();
|
|
1677
|
-
}).catch(() => {
|
|
1678
|
-
});
|
|
1679
|
-
}, CHECK_INTERVAL_MS);
|
|
1680
|
-
periodicTimer.unref();
|
|
1681
|
-
}
|
|
1682
|
-
function stopPeriodicUpdateCheck() {
|
|
1683
|
-
if (periodicTimer) {
|
|
1684
|
-
clearInterval(periodicTimer);
|
|
1685
|
-
periodicTimer = null;
|
|
1686
|
-
}
|
|
1687
|
-
}
|
|
1688
|
-
return {
|
|
1689
|
-
checkAndAutoUpdate,
|
|
1690
|
-
getPendingUpdate,
|
|
1691
|
-
startPeriodicUpdateCheck,
|
|
1692
|
-
stopPeriodicUpdateCheck
|
|
1693
|
-
};
|
|
1694
|
-
}
|
|
1695
1199
|
export {
|
|
1696
1200
|
AUTH_PROVIDERS,
|
|
1697
1201
|
AuthStorage,
|
|
1698
1202
|
CHATGPT_PLAN_BASE_URL,
|
|
1699
|
-
CHATGPT_PLAN_NOT_ENABLED_MESSAGE,
|
|
1700
1203
|
CHATGPT_PLAN_SCOPE,
|
|
1701
1204
|
CHATGPT_USAGE_URL,
|
|
1702
1205
|
CLAUDE_CODE_AUTH_KEY,
|
|
@@ -1728,7 +1231,6 @@ export {
|
|
|
1728
1231
|
PLAN_MODE_THINKING_CAP,
|
|
1729
1232
|
RETIRED_SIGN_INS,
|
|
1730
1233
|
SubscriptionUsageError,
|
|
1731
|
-
TelegramBot,
|
|
1732
1234
|
XIAOMI_CREDITS_KEY,
|
|
1733
1235
|
XIAOMI_TOKEN_PLAN_BASE_URLS,
|
|
1734
1236
|
apiKeyStorageKeys,
|
|
@@ -1745,14 +1247,12 @@ export {
|
|
|
1745
1247
|
clearLocalDiscoveryCache,
|
|
1746
1248
|
clearRuntimeModels,
|
|
1747
1249
|
closeLogger,
|
|
1748
|
-
|
|
1749
|
-
decodeOggOpus,
|
|
1250
|
+
configureAppDataDir,
|
|
1750
1251
|
describeAuthMethods,
|
|
1751
1252
|
detectKimiCodeBaseUrl,
|
|
1752
1253
|
detectMiniMaxBaseUrl,
|
|
1753
1254
|
detectXiaomiTokenPlanBaseUrl,
|
|
1754
1255
|
discoverLocalModels,
|
|
1755
|
-
downmixToMono,
|
|
1756
1256
|
dualAuthProvider,
|
|
1757
1257
|
dualAuthProviderByOAuthKey,
|
|
1758
1258
|
endpointRoot,
|
|
@@ -1791,7 +1291,6 @@ export {
|
|
|
1791
1291
|
isLegacyClaudeOAuth,
|
|
1792
1292
|
isLocalModelId,
|
|
1793
1293
|
isLoggerOpen,
|
|
1794
|
-
isModelLoaded,
|
|
1795
1294
|
isPaidModel,
|
|
1796
1295
|
isThinkingLevelSupported,
|
|
1797
1296
|
localAuthStorageKey,
|
|
@@ -1818,17 +1317,14 @@ export {
|
|
|
1818
1317
|
registerLogCleanup,
|
|
1819
1318
|
registerRuntimeModels,
|
|
1820
1319
|
requiredKeyLabel,
|
|
1821
|
-
resample,
|
|
1822
1320
|
resolveClaudeCodeBinary,
|
|
1823
1321
|
revokeChatGPTSignIn,
|
|
1824
1322
|
savedChatGPTAccounts,
|
|
1825
|
-
setProgressCallback,
|
|
1826
1323
|
setProviderCatalog,
|
|
1827
1324
|
signInWithClaudeCode,
|
|
1828
1325
|
signOutOfClaudeCode,
|
|
1829
1326
|
storageKeyProvider,
|
|
1830
1327
|
toModelInfo,
|
|
1831
|
-
transcribeVoice,
|
|
1832
1328
|
usesOpenAICodexTransport,
|
|
1833
1329
|
verifyIdToken,
|
|
1834
1330
|
withFileLock,
|