kimiflare 0.37.0 → 0.38.1
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/dist/index.js +252 -8
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
2
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
3
7
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
8
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
9
|
}) : x)(function(x) {
|
|
@@ -9,10 +13,29 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
9
13
|
var __esm = (fn, res) => function __init() {
|
|
10
14
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
15
|
};
|
|
16
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
17
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
18
|
+
};
|
|
12
19
|
var __export = (target, all) => {
|
|
13
20
|
for (var name in all)
|
|
14
21
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
22
|
};
|
|
23
|
+
var __copyProps = (to, from, except, desc) => {
|
|
24
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
25
|
+
for (let key of __getOwnPropNames(from))
|
|
26
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
27
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
28
|
+
}
|
|
29
|
+
return to;
|
|
30
|
+
};
|
|
31
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
32
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
33
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
34
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
35
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
36
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
37
|
+
mod
|
|
38
|
+
));
|
|
16
39
|
|
|
17
40
|
// src/config.ts
|
|
18
41
|
var config_exports = {};
|
|
@@ -486,6 +509,7 @@ async function* runKimi(opts2) {
|
|
|
486
509
|
if (opts2.cloudMode && !opts2.cloudToken) {
|
|
487
510
|
throw new KimiApiError("kimiflare: cloud mode requires a cloud token. Run `kimiflare auth cloud` to authenticate.", void 0, 401);
|
|
488
511
|
}
|
|
512
|
+
const requestId = opts2.requestId ?? crypto.randomUUID();
|
|
489
513
|
const { url, headers: gatewayHeaders } = buildKimiRequestTarget(opts2);
|
|
490
514
|
const body = {
|
|
491
515
|
messages: sanitizeMessagesForApi(opts2.messages),
|
|
@@ -510,6 +534,7 @@ async function* runKimi(opts2) {
|
|
|
510
534
|
headers["X-Session-ID"] = opts2.sessionId;
|
|
511
535
|
headers["x-session-affinity"] = opts2.sessionId;
|
|
512
536
|
}
|
|
537
|
+
headers["X-Request-ID"] = requestId;
|
|
513
538
|
res = await fetch(url, {
|
|
514
539
|
method: "POST",
|
|
515
540
|
headers,
|
|
@@ -547,7 +572,31 @@ async function* runKimi(opts2) {
|
|
|
547
572
|
if (!res.body) throw new KimiApiError("kimiflare: empty response body", void 0, res.status);
|
|
548
573
|
const meta = readGatewayMeta(res.headers);
|
|
549
574
|
if (meta) yield { type: "gateway_meta", meta };
|
|
550
|
-
|
|
575
|
+
let lastUsage = null;
|
|
576
|
+
for await (const ev of parseStream(res.body, opts2.signal)) {
|
|
577
|
+
if (ev.type === "usage") lastUsage = ev.usage;
|
|
578
|
+
yield ev;
|
|
579
|
+
}
|
|
580
|
+
if (opts2.cloudMode && lastUsage && opts2.cloudToken) {
|
|
581
|
+
const reportUrl = "https://api.kimiflare.com/v1/usage/report";
|
|
582
|
+
const reportHeaders = {
|
|
583
|
+
Authorization: `Bearer ${opts2.cloudToken}`,
|
|
584
|
+
"Content-Type": "application/json"
|
|
585
|
+
};
|
|
586
|
+
if (opts2.cloudDeviceId) reportHeaders["X-Device-ID"] = opts2.cloudDeviceId;
|
|
587
|
+
if (opts2.sessionId) reportHeaders["X-Session-ID"] = opts2.sessionId;
|
|
588
|
+
fetch(reportUrl, {
|
|
589
|
+
method: "POST",
|
|
590
|
+
headers: reportHeaders,
|
|
591
|
+
body: JSON.stringify({
|
|
592
|
+
request_id: requestId,
|
|
593
|
+
prompt_tokens: lastUsage.prompt_tokens,
|
|
594
|
+
completion_tokens: lastUsage.completion_tokens,
|
|
595
|
+
cached_tokens: lastUsage.prompt_tokens_details?.cached_tokens ?? 0
|
|
596
|
+
})
|
|
597
|
+
}).catch(() => {
|
|
598
|
+
});
|
|
599
|
+
}
|
|
551
600
|
return;
|
|
552
601
|
}
|
|
553
602
|
}
|
|
@@ -1391,6 +1440,197 @@ var init_api_generator = __esm({
|
|
|
1391
1440
|
}
|
|
1392
1441
|
});
|
|
1393
1442
|
|
|
1443
|
+
// node_modules/node-gyp-build/node-gyp-build.js
|
|
1444
|
+
var require_node_gyp_build = __commonJS({
|
|
1445
|
+
"node_modules/node-gyp-build/node-gyp-build.js"(exports, module) {
|
|
1446
|
+
"use strict";
|
|
1447
|
+
var fs = __require("fs");
|
|
1448
|
+
var path = __require("path");
|
|
1449
|
+
var os2 = __require("os");
|
|
1450
|
+
var runtimeRequire = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
|
|
1451
|
+
var vars = process.config && process.config.variables || {};
|
|
1452
|
+
var prebuildsOnly = !!process.env.PREBUILDS_ONLY;
|
|
1453
|
+
var abi = process.versions.modules;
|
|
1454
|
+
var runtime = isElectron() ? "electron" : isNwjs() ? "node-webkit" : "node";
|
|
1455
|
+
var arch = process.env.npm_config_arch || os2.arch();
|
|
1456
|
+
var platform3 = process.env.npm_config_platform || os2.platform();
|
|
1457
|
+
var libc = process.env.LIBC || (isAlpine(platform3) ? "musl" : "glibc");
|
|
1458
|
+
var armv = process.env.ARM_VERSION || (arch === "arm64" ? "8" : vars.arm_version) || "";
|
|
1459
|
+
var uv = (process.versions.uv || "").split(".")[0];
|
|
1460
|
+
module.exports = load;
|
|
1461
|
+
function load(dir) {
|
|
1462
|
+
return runtimeRequire(load.resolve(dir));
|
|
1463
|
+
}
|
|
1464
|
+
load.resolve = load.path = function(dir) {
|
|
1465
|
+
dir = path.resolve(dir || ".");
|
|
1466
|
+
try {
|
|
1467
|
+
var name = runtimeRequire(path.join(dir, "package.json")).name.toUpperCase().replace(/-/g, "_");
|
|
1468
|
+
if (process.env[name + "_PREBUILD"]) dir = process.env[name + "_PREBUILD"];
|
|
1469
|
+
} catch (err) {
|
|
1470
|
+
}
|
|
1471
|
+
if (!prebuildsOnly) {
|
|
1472
|
+
var release2 = getFirst(path.join(dir, "build/Release"), matchBuild);
|
|
1473
|
+
if (release2) return release2;
|
|
1474
|
+
var debug = getFirst(path.join(dir, "build/Debug"), matchBuild);
|
|
1475
|
+
if (debug) return debug;
|
|
1476
|
+
}
|
|
1477
|
+
var prebuild = resolve2(dir);
|
|
1478
|
+
if (prebuild) return prebuild;
|
|
1479
|
+
var nearby = resolve2(path.dirname(process.execPath));
|
|
1480
|
+
if (nearby) return nearby;
|
|
1481
|
+
var target = [
|
|
1482
|
+
"platform=" + platform3,
|
|
1483
|
+
"arch=" + arch,
|
|
1484
|
+
"runtime=" + runtime,
|
|
1485
|
+
"abi=" + abi,
|
|
1486
|
+
"uv=" + uv,
|
|
1487
|
+
armv ? "armv=" + armv : "",
|
|
1488
|
+
"libc=" + libc,
|
|
1489
|
+
"node=" + process.versions.node,
|
|
1490
|
+
process.versions.electron ? "electron=" + process.versions.electron : "",
|
|
1491
|
+
typeof __webpack_require__ === "function" ? "webpack=true" : ""
|
|
1492
|
+
// eslint-disable-line
|
|
1493
|
+
].filter(Boolean).join(" ");
|
|
1494
|
+
throw new Error("No native build was found for " + target + "\n loaded from: " + dir + "\n");
|
|
1495
|
+
function resolve2(dir2) {
|
|
1496
|
+
var tuples = readdirSync(path.join(dir2, "prebuilds")).map(parseTuple);
|
|
1497
|
+
var tuple = tuples.filter(matchTuple(platform3, arch)).sort(compareTuples)[0];
|
|
1498
|
+
if (!tuple) return;
|
|
1499
|
+
var prebuilds = path.join(dir2, "prebuilds", tuple.name);
|
|
1500
|
+
var parsed = readdirSync(prebuilds).map(parseTags);
|
|
1501
|
+
var candidates = parsed.filter(matchTags(runtime, abi));
|
|
1502
|
+
var winner = candidates.sort(compareTags(runtime))[0];
|
|
1503
|
+
if (winner) return path.join(prebuilds, winner.file);
|
|
1504
|
+
}
|
|
1505
|
+
};
|
|
1506
|
+
function readdirSync(dir) {
|
|
1507
|
+
try {
|
|
1508
|
+
return fs.readdirSync(dir);
|
|
1509
|
+
} catch (err) {
|
|
1510
|
+
return [];
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
function getFirst(dir, filter) {
|
|
1514
|
+
var files = readdirSync(dir).filter(filter);
|
|
1515
|
+
return files[0] && path.join(dir, files[0]);
|
|
1516
|
+
}
|
|
1517
|
+
function matchBuild(name) {
|
|
1518
|
+
return /\.node$/.test(name);
|
|
1519
|
+
}
|
|
1520
|
+
function parseTuple(name) {
|
|
1521
|
+
var arr = name.split("-");
|
|
1522
|
+
if (arr.length !== 2) return;
|
|
1523
|
+
var platform4 = arr[0];
|
|
1524
|
+
var architectures = arr[1].split("+");
|
|
1525
|
+
if (!platform4) return;
|
|
1526
|
+
if (!architectures.length) return;
|
|
1527
|
+
if (!architectures.every(Boolean)) return;
|
|
1528
|
+
return { name, platform: platform4, architectures };
|
|
1529
|
+
}
|
|
1530
|
+
function matchTuple(platform4, arch2) {
|
|
1531
|
+
return function(tuple) {
|
|
1532
|
+
if (tuple == null) return false;
|
|
1533
|
+
if (tuple.platform !== platform4) return false;
|
|
1534
|
+
return tuple.architectures.includes(arch2);
|
|
1535
|
+
};
|
|
1536
|
+
}
|
|
1537
|
+
function compareTuples(a, b) {
|
|
1538
|
+
return a.architectures.length - b.architectures.length;
|
|
1539
|
+
}
|
|
1540
|
+
function parseTags(file) {
|
|
1541
|
+
var arr = file.split(".");
|
|
1542
|
+
var extension = arr.pop();
|
|
1543
|
+
var tags = { file, specificity: 0 };
|
|
1544
|
+
if (extension !== "node") return;
|
|
1545
|
+
for (var i = 0; i < arr.length; i++) {
|
|
1546
|
+
var tag = arr[i];
|
|
1547
|
+
if (tag === "node" || tag === "electron" || tag === "node-webkit") {
|
|
1548
|
+
tags.runtime = tag;
|
|
1549
|
+
} else if (tag === "napi") {
|
|
1550
|
+
tags.napi = true;
|
|
1551
|
+
} else if (tag.slice(0, 3) === "abi") {
|
|
1552
|
+
tags.abi = tag.slice(3);
|
|
1553
|
+
} else if (tag.slice(0, 2) === "uv") {
|
|
1554
|
+
tags.uv = tag.slice(2);
|
|
1555
|
+
} else if (tag.slice(0, 4) === "armv") {
|
|
1556
|
+
tags.armv = tag.slice(4);
|
|
1557
|
+
} else if (tag === "glibc" || tag === "musl") {
|
|
1558
|
+
tags.libc = tag;
|
|
1559
|
+
} else {
|
|
1560
|
+
continue;
|
|
1561
|
+
}
|
|
1562
|
+
tags.specificity++;
|
|
1563
|
+
}
|
|
1564
|
+
return tags;
|
|
1565
|
+
}
|
|
1566
|
+
function matchTags(runtime2, abi2) {
|
|
1567
|
+
return function(tags) {
|
|
1568
|
+
if (tags == null) return false;
|
|
1569
|
+
if (tags.runtime && tags.runtime !== runtime2 && !runtimeAgnostic(tags)) return false;
|
|
1570
|
+
if (tags.abi && tags.abi !== abi2 && !tags.napi) return false;
|
|
1571
|
+
if (tags.uv && tags.uv !== uv) return false;
|
|
1572
|
+
if (tags.armv && tags.armv !== armv) return false;
|
|
1573
|
+
if (tags.libc && tags.libc !== libc) return false;
|
|
1574
|
+
return true;
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
|
+
function runtimeAgnostic(tags) {
|
|
1578
|
+
return tags.runtime === "node" && tags.napi;
|
|
1579
|
+
}
|
|
1580
|
+
function compareTags(runtime2) {
|
|
1581
|
+
return function(a, b) {
|
|
1582
|
+
if (a.runtime !== b.runtime) {
|
|
1583
|
+
return a.runtime === runtime2 ? -1 : 1;
|
|
1584
|
+
} else if (a.abi !== b.abi) {
|
|
1585
|
+
return a.abi ? -1 : 1;
|
|
1586
|
+
} else if (a.specificity !== b.specificity) {
|
|
1587
|
+
return a.specificity > b.specificity ? -1 : 1;
|
|
1588
|
+
} else {
|
|
1589
|
+
return 0;
|
|
1590
|
+
}
|
|
1591
|
+
};
|
|
1592
|
+
}
|
|
1593
|
+
function isNwjs() {
|
|
1594
|
+
return !!(process.versions && process.versions.nw);
|
|
1595
|
+
}
|
|
1596
|
+
function isElectron() {
|
|
1597
|
+
if (process.versions && process.versions.electron) return true;
|
|
1598
|
+
if (process.env.ELECTRON_RUN_AS_NODE) return true;
|
|
1599
|
+
return typeof window !== "undefined" && window.process && window.process.type === "renderer";
|
|
1600
|
+
}
|
|
1601
|
+
function isAlpine(platform4) {
|
|
1602
|
+
return platform4 === "linux" && fs.existsSync("/etc/alpine-release");
|
|
1603
|
+
}
|
|
1604
|
+
load.parseTags = parseTags;
|
|
1605
|
+
load.matchTags = matchTags;
|
|
1606
|
+
load.compareTags = compareTags;
|
|
1607
|
+
load.parseTuple = parseTuple;
|
|
1608
|
+
load.matchTuple = matchTuple;
|
|
1609
|
+
load.compareTuples = compareTuples;
|
|
1610
|
+
}
|
|
1611
|
+
});
|
|
1612
|
+
|
|
1613
|
+
// node_modules/node-gyp-build/index.js
|
|
1614
|
+
var require_node_gyp_build2 = __commonJS({
|
|
1615
|
+
"node_modules/node-gyp-build/index.js"(exports, module) {
|
|
1616
|
+
"use strict";
|
|
1617
|
+
var runtimeRequire = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
|
|
1618
|
+
if (typeof runtimeRequire.addon === "function") {
|
|
1619
|
+
module.exports = runtimeRequire.addon.bind(runtimeRequire);
|
|
1620
|
+
} else {
|
|
1621
|
+
module.exports = require_node_gyp_build();
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
});
|
|
1625
|
+
|
|
1626
|
+
// node_modules/isolated-vm/isolated-vm.js
|
|
1627
|
+
var require_isolated_vm = __commonJS({
|
|
1628
|
+
"node_modules/isolated-vm/isolated-vm.js"(exports, module) {
|
|
1629
|
+
"use strict";
|
|
1630
|
+
module.exports = require_node_gyp_build2()(__dirname).ivm;
|
|
1631
|
+
}
|
|
1632
|
+
});
|
|
1633
|
+
|
|
1394
1634
|
// src/code-mode/sandbox.ts
|
|
1395
1635
|
import { join as join6, dirname as dirname3 } from "path";
|
|
1396
1636
|
import { pathToFileURL } from "url";
|
|
@@ -1446,7 +1686,7 @@ async function transpileOrStrip(code, cwd) {
|
|
|
1446
1686
|
};
|
|
1447
1687
|
}
|
|
1448
1688
|
async function runWithIsolatedVm(opts2) {
|
|
1449
|
-
const { Isolate } = await
|
|
1689
|
+
const { Isolate } = await Promise.resolve().then(() => __toESM(require_isolated_vm(), 1));
|
|
1450
1690
|
const isolate = new Isolate({ memoryLimit: opts2.memoryLimitMB ?? 128 });
|
|
1451
1691
|
const context = await isolate.createContext();
|
|
1452
1692
|
const jail = context.global;
|
|
@@ -1463,7 +1703,7 @@ async function runWithIsolatedVm(opts2) {
|
|
|
1463
1703
|
await context.eval(`var console = { log: function(...args) { _log(args.map(String).join(" ")); } };`);
|
|
1464
1704
|
const toolMap = new Map(opts2.tools.map((t) => [t.name, t]));
|
|
1465
1705
|
for (const tool of opts2.tools) {
|
|
1466
|
-
const ref = new (await
|
|
1706
|
+
const ref = new (await Promise.resolve().then(() => __toESM(require_isolated_vm(), 1))).Reference(
|
|
1467
1707
|
async (argsJson) => {
|
|
1468
1708
|
const args = JSON.parse(argsJson);
|
|
1469
1709
|
const toolCallId = `code_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
@@ -1596,14 +1836,18 @@ async function runInSandbox(opts2) {
|
|
|
1596
1836
|
} catch (err) {
|
|
1597
1837
|
const message2 = err instanceof Error ? err.message : String(err);
|
|
1598
1838
|
if (message2.includes("isolated-vm") || message2.includes("Cannot find module") || message2.includes("bindings")) {
|
|
1599
|
-
|
|
1839
|
+
const result2 = await runWithNodeVm(opts2);
|
|
1840
|
+
return { ...result2, warnings: [ISOLATED_VM_FALLBACK_WARNING, ...result2.warnings ?? []] };
|
|
1600
1841
|
}
|
|
1601
|
-
|
|
1842
|
+
const result = await runWithNodeVm(opts2);
|
|
1843
|
+
return { ...result, warnings: [ISOLATED_VM_FALLBACK_WARNING, ...result.warnings ?? []] };
|
|
1602
1844
|
}
|
|
1603
1845
|
}
|
|
1846
|
+
var ISOLATED_VM_FALLBACK_WARNING;
|
|
1604
1847
|
var init_sandbox = __esm({
|
|
1605
1848
|
"src/code-mode/sandbox.ts"() {
|
|
1606
1849
|
"use strict";
|
|
1850
|
+
ISOLATED_VM_FALLBACK_WARNING = "\u26A0\uFE0F Code sandbox is running without memory limits or true process isolation (isolated-vm unavailable or failed to load). For a secure sandbox, install with Node 22 LTS: nvm install 22 && nvm use 22 && npm install -g kimiflare";
|
|
1607
1851
|
}
|
|
1608
1852
|
});
|
|
1609
1853
|
|
|
@@ -4049,13 +4293,13 @@ async function checkDeployStatus() {
|
|
|
4049
4293
|
}
|
|
4050
4294
|
return { wrangler, wranglerAuth, docker, workerUrl };
|
|
4051
4295
|
}
|
|
4052
|
-
var
|
|
4296
|
+
var __dirname2, REMOTE_DIR, WORKER_DIR;
|
|
4053
4297
|
var init_deploy = __esm({
|
|
4054
4298
|
"src/remote/deploy.ts"() {
|
|
4055
4299
|
"use strict";
|
|
4056
4300
|
init_config();
|
|
4057
|
-
|
|
4058
|
-
REMOTE_DIR = join11(
|
|
4301
|
+
__dirname2 = dirname6(fileURLToPath3(import.meta.url));
|
|
4302
|
+
REMOTE_DIR = join11(__dirname2, "..", "..", "..", "remote");
|
|
4059
4303
|
WORKER_DIR = join11(REMOTE_DIR, "worker");
|
|
4060
4304
|
}
|
|
4061
4305
|
});
|