kimiflare 0.36.2 → 0.38.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/dist/index.js +54 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -486,6 +486,7 @@ async function* runKimi(opts2) {
|
|
|
486
486
|
if (opts2.cloudMode && !opts2.cloudToken) {
|
|
487
487
|
throw new KimiApiError("kimiflare: cloud mode requires a cloud token. Run `kimiflare auth cloud` to authenticate.", void 0, 401);
|
|
488
488
|
}
|
|
489
|
+
const requestId = opts2.requestId ?? crypto.randomUUID();
|
|
489
490
|
const { url, headers: gatewayHeaders } = buildKimiRequestTarget(opts2);
|
|
490
491
|
const body = {
|
|
491
492
|
messages: sanitizeMessagesForApi(opts2.messages),
|
|
@@ -510,6 +511,7 @@ async function* runKimi(opts2) {
|
|
|
510
511
|
headers["X-Session-ID"] = opts2.sessionId;
|
|
511
512
|
headers["x-session-affinity"] = opts2.sessionId;
|
|
512
513
|
}
|
|
514
|
+
headers["X-Request-ID"] = requestId;
|
|
513
515
|
res = await fetch(url, {
|
|
514
516
|
method: "POST",
|
|
515
517
|
headers,
|
|
@@ -547,7 +549,31 @@ async function* runKimi(opts2) {
|
|
|
547
549
|
if (!res.body) throw new KimiApiError("kimiflare: empty response body", void 0, res.status);
|
|
548
550
|
const meta = readGatewayMeta(res.headers);
|
|
549
551
|
if (meta) yield { type: "gateway_meta", meta };
|
|
550
|
-
|
|
552
|
+
let lastUsage = null;
|
|
553
|
+
for await (const ev of parseStream(res.body, opts2.signal)) {
|
|
554
|
+
if (ev.type === "usage") lastUsage = ev.usage;
|
|
555
|
+
yield ev;
|
|
556
|
+
}
|
|
557
|
+
if (opts2.cloudMode && lastUsage && opts2.cloudToken) {
|
|
558
|
+
const reportUrl = "https://api.kimiflare.com/v1/usage/report";
|
|
559
|
+
const reportHeaders = {
|
|
560
|
+
Authorization: `Bearer ${opts2.cloudToken}`,
|
|
561
|
+
"Content-Type": "application/json"
|
|
562
|
+
};
|
|
563
|
+
if (opts2.cloudDeviceId) reportHeaders["X-Device-ID"] = opts2.cloudDeviceId;
|
|
564
|
+
if (opts2.sessionId) reportHeaders["X-Session-ID"] = opts2.sessionId;
|
|
565
|
+
fetch(reportUrl, {
|
|
566
|
+
method: "POST",
|
|
567
|
+
headers: reportHeaders,
|
|
568
|
+
body: JSON.stringify({
|
|
569
|
+
request_id: requestId,
|
|
570
|
+
prompt_tokens: lastUsage.prompt_tokens,
|
|
571
|
+
completion_tokens: lastUsage.completion_tokens,
|
|
572
|
+
cached_tokens: lastUsage.prompt_tokens_details?.cached_tokens ?? 0
|
|
573
|
+
})
|
|
574
|
+
}).catch(() => {
|
|
575
|
+
});
|
|
576
|
+
}
|
|
551
577
|
return;
|
|
552
578
|
}
|
|
553
579
|
}
|
|
@@ -6878,9 +6904,9 @@ function buildRightParts(usage, contextLimit, sessionUsage, gatewayMeta, cloudMo
|
|
|
6878
6904
|
parts.push(`out ${sessionUsage.completionTokens}`);
|
|
6879
6905
|
parts.push(`ctx ${pct}%`);
|
|
6880
6906
|
if (cloudMode) {
|
|
6881
|
-
parts.push(`\x1B[9m
|
|
6907
|
+
parts.push(`\x1B[9m$${sessionUsage.cost.toFixed(5)}\x1B[29m`);
|
|
6882
6908
|
} else {
|
|
6883
|
-
parts.push(
|
|
6909
|
+
parts.push(`$${sessionUsage.cost.toFixed(5)}`);
|
|
6884
6910
|
}
|
|
6885
6911
|
} else {
|
|
6886
6912
|
const cached = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
@@ -6889,9 +6915,9 @@ function buildRightParts(usage, contextLimit, sessionUsage, gatewayMeta, cloudMo
|
|
|
6889
6915
|
parts.push(`out ${usage.completion_tokens}`);
|
|
6890
6916
|
parts.push(`ctx ${pct}%`);
|
|
6891
6917
|
if (cloudMode) {
|
|
6892
|
-
parts.push(`\x1B[9m
|
|
6918
|
+
parts.push(`\x1B[9m$${cost.total.toFixed(5)}\x1B[29m`);
|
|
6893
6919
|
} else {
|
|
6894
|
-
parts.push(
|
|
6920
|
+
parts.push(`$${cost.total.toFixed(5)}`);
|
|
6895
6921
|
}
|
|
6896
6922
|
}
|
|
6897
6923
|
if (cloudMode && cloudBudget) {
|
|
@@ -6902,6 +6928,7 @@ function buildRightParts(usage, contextLimit, sessionUsage, gatewayMeta, cloudMo
|
|
|
6902
6928
|
return parts;
|
|
6903
6929
|
}
|
|
6904
6930
|
function formatTokens(n) {
|
|
6931
|
+
if (n >= 1e9) return `${(n / 1e9).toFixed(1)}B`;
|
|
6905
6932
|
if (n >= 1e6) return `${(n / 1e6).toFixed(1)}M`;
|
|
6906
6933
|
if (n >= 1e3) return `${(n / 1e3).toFixed(1)}K`;
|
|
6907
6934
|
return String(n);
|
|
@@ -14047,6 +14074,17 @@ function App({
|
|
|
14047
14074
|
const sid = ensureSessionId();
|
|
14048
14075
|
void recordUsage(sid, u, gatewayUsageLookupFromConfig(cfg, meta ?? gatewayMetaRef.current));
|
|
14049
14076
|
void getCostReport(sid).then((report) => setSessionUsage(report.session));
|
|
14077
|
+
if (cfg?.cloudMode && (cloudToken ?? initialCloudToken)) {
|
|
14078
|
+
const token = cloudToken ?? initialCloudToken;
|
|
14079
|
+
const did = cloudDeviceId ?? initialCloudDeviceId;
|
|
14080
|
+
void (async () => {
|
|
14081
|
+
const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
|
|
14082
|
+
const usage2 = await fetchCloudUsage2(token, did);
|
|
14083
|
+
if (usage2) {
|
|
14084
|
+
setCloudBudget({ remaining: usage2.remaining, limit: usage2.input_token_limit });
|
|
14085
|
+
}
|
|
14086
|
+
})();
|
|
14087
|
+
}
|
|
14050
14088
|
},
|
|
14051
14089
|
onGatewayMeta: updateGatewayMeta,
|
|
14052
14090
|
askPermission: (req) => new Promise((resolve2) => {
|
|
@@ -15126,6 +15164,17 @@ ${lines.join("\n")}` }]);
|
|
|
15126
15164
|
const sid = ensureSessionId();
|
|
15127
15165
|
void recordUsage(sid, u, gatewayUsageLookupFromConfig(cfg, meta ?? gatewayMetaRef.current));
|
|
15128
15166
|
void getCostReport(sid).then((report) => setSessionUsage(report.session));
|
|
15167
|
+
if (cfg?.cloudMode && (cloudToken ?? initialCloudToken)) {
|
|
15168
|
+
const token = cloudToken ?? initialCloudToken;
|
|
15169
|
+
const did = cloudDeviceId ?? initialCloudDeviceId;
|
|
15170
|
+
void (async () => {
|
|
15171
|
+
const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
|
|
15172
|
+
const usage2 = await fetchCloudUsage2(token, did);
|
|
15173
|
+
if (usage2) {
|
|
15174
|
+
setCloudBudget({ remaining: usage2.remaining, limit: usage2.input_token_limit });
|
|
15175
|
+
}
|
|
15176
|
+
})();
|
|
15177
|
+
}
|
|
15129
15178
|
},
|
|
15130
15179
|
onGatewayMeta: updateGatewayMeta,
|
|
15131
15180
|
onTasks: (nextTasks) => {
|