kimiflare 0.36.1 → 0.36.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +41 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -559,9 +559,11 @@ function validateModelId(model) {
|
|
|
559
559
|
function buildKimiRequestTarget(opts2) {
|
|
560
560
|
validateModelId(opts2.model);
|
|
561
561
|
if (opts2.cloudMode) {
|
|
562
|
+
const headers2 = opts2.cloudToken ? { Authorization: `Bearer ${opts2.cloudToken}` } : {};
|
|
563
|
+
if (opts2.cloudDeviceId) headers2["X-Device-ID"] = opts2.cloudDeviceId;
|
|
562
564
|
return {
|
|
563
565
|
url: "https://api.kimiflare.com/v1/chat",
|
|
564
|
-
headers:
|
|
566
|
+
headers: headers2
|
|
565
567
|
};
|
|
566
568
|
}
|
|
567
569
|
if (!opts2.gateway?.id) {
|
|
@@ -1760,7 +1762,8 @@ Use console.log() to return results. Only console.log output will be sent back t
|
|
|
1760
1762
|
sessionId: opts2.sessionId,
|
|
1761
1763
|
gateway: opts2.gateway,
|
|
1762
1764
|
cloudMode: opts2.cloudMode,
|
|
1763
|
-
cloudToken: opts2.cloudToken
|
|
1765
|
+
cloudToken: opts2.cloudToken,
|
|
1766
|
+
cloudDeviceId: opts2.cloudDeviceId
|
|
1764
1767
|
});
|
|
1765
1768
|
for await (const ev of events) {
|
|
1766
1769
|
switch (ev.type) {
|
|
@@ -4664,24 +4667,30 @@ function generateCode() {
|
|
|
4664
4667
|
}
|
|
4665
4668
|
return out;
|
|
4666
4669
|
}
|
|
4670
|
+
function generateDeviceId() {
|
|
4671
|
+
const arr = new Uint8Array(16);
|
|
4672
|
+
crypto.getRandomValues(arr);
|
|
4673
|
+
return Array.from(arr, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
4674
|
+
}
|
|
4667
4675
|
function generateDeviceCodes() {
|
|
4668
4676
|
const deviceCode = `device-${generateCode()}-${Date.now()}`;
|
|
4669
4677
|
const userCode = `${generateCode()}-${generateCode()}`;
|
|
4670
4678
|
const authUrl = `${CLOUD_API_URL}/auth/github?code=${encodeURIComponent(userCode)}`;
|
|
4671
|
-
|
|
4679
|
+
const deviceId = generateDeviceId();
|
|
4680
|
+
return { deviceCode, userCode, authUrl, deviceId };
|
|
4672
4681
|
}
|
|
4673
4682
|
async function registerDevice(codes) {
|
|
4674
4683
|
const registerRes = await fetch(`${CLOUD_API_URL}/auth/device`, {
|
|
4675
4684
|
method: "POST",
|
|
4676
4685
|
headers: { "Content-Type": "application/json" },
|
|
4677
|
-
body: JSON.stringify({ device_code: codes.deviceCode, user_code: codes.userCode })
|
|
4686
|
+
body: JSON.stringify({ device_code: codes.deviceCode, user_code: codes.userCode, device_id: codes.deviceId })
|
|
4678
4687
|
});
|
|
4679
4688
|
if (!registerRes.ok) {
|
|
4680
4689
|
const err = await registerRes.json().catch(() => ({}));
|
|
4681
4690
|
throw new Error(`Failed to register device: ${err.error || registerRes.statusText}`);
|
|
4682
4691
|
}
|
|
4683
4692
|
}
|
|
4684
|
-
async function pollForToken(deviceCode) {
|
|
4693
|
+
async function pollForToken(deviceCode, deviceId) {
|
|
4685
4694
|
const pollRes = await fetch(`${CLOUD_API_URL}/auth/poll`, {
|
|
4686
4695
|
method: "POST",
|
|
4687
4696
|
headers: { "Content-Type": "application/json" },
|
|
@@ -4692,18 +4701,19 @@ async function pollForToken(deviceCode) {
|
|
|
4692
4701
|
if (pollData.status === "approved" && pollData.access_token) {
|
|
4693
4702
|
const creds = {
|
|
4694
4703
|
accessToken: pollData.access_token,
|
|
4695
|
-
expiresAt: Math.floor(Date.now() / 1e3) + 7 * 24 * 60 * 60
|
|
4704
|
+
expiresAt: Math.floor(Date.now() / 1e3) + 7 * 24 * 60 * 60,
|
|
4696
4705
|
// 7 days
|
|
4706
|
+
deviceId
|
|
4697
4707
|
};
|
|
4698
4708
|
await saveCloudCredentials(creds);
|
|
4699
4709
|
return creds;
|
|
4700
4710
|
}
|
|
4701
4711
|
return null;
|
|
4702
4712
|
}
|
|
4703
|
-
async function fetchCloudUsage(token) {
|
|
4704
|
-
const
|
|
4705
|
-
|
|
4706
|
-
});
|
|
4713
|
+
async function fetchCloudUsage(token, deviceId) {
|
|
4714
|
+
const headers = { Authorization: `Bearer ${token}` };
|
|
4715
|
+
if (deviceId) headers["X-Device-ID"] = deviceId;
|
|
4716
|
+
const res = await fetch(`${CLOUD_API_URL}/v1/usage`, { headers });
|
|
4707
4717
|
if (!res.ok) return null;
|
|
4708
4718
|
const data = await res.json();
|
|
4709
4719
|
if (typeof data.remaining !== "number" || typeof data.input_token_limit !== "number" || typeof data.input_tokens_used !== "number" || typeof data.expires_at !== "string") {
|
|
@@ -4720,7 +4730,7 @@ async function loadCloudCredentials() {
|
|
|
4720
4730
|
try {
|
|
4721
4731
|
const raw = await readFile11(cloudCredPath(), "utf8");
|
|
4722
4732
|
const parsed = JSON.parse(raw);
|
|
4723
|
-
if (parsed.expiresAt && parsed.expiresAt > Date.now() / 1e3) {
|
|
4733
|
+
if (parsed.expiresAt && parsed.expiresAt > Date.now() / 1e3 && parsed.accessToken) {
|
|
4724
4734
|
return parsed;
|
|
4725
4735
|
}
|
|
4726
4736
|
} catch {
|
|
@@ -4746,7 +4756,7 @@ async function authenticateDevice(onStatus) {
|
|
|
4746
4756
|
while (Date.now() - startTime < POLL_TIMEOUT_MS) {
|
|
4747
4757
|
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
|
|
4748
4758
|
onStatus({ url: codes.authUrl, userCode: codes.userCode, polling: true });
|
|
4749
|
-
const creds = await pollForToken(codes.deviceCode);
|
|
4759
|
+
const creds = await pollForToken(codes.deviceCode, codes.deviceId);
|
|
4750
4760
|
if (creds) return creds;
|
|
4751
4761
|
}
|
|
4752
4762
|
throw new Error("Authentication timed out. Please try again.");
|
|
@@ -7978,9 +7988,9 @@ function Onboarding({ onDone, onCancel }) {
|
|
|
7978
7988
|
return;
|
|
7979
7989
|
}
|
|
7980
7990
|
try {
|
|
7981
|
-
const creds = await pollForToken(cloudAuth.codes.deviceCode);
|
|
7991
|
+
const creds = await pollForToken(cloudAuth.codes.deviceCode, cloudAuth.codes.deviceId);
|
|
7982
7992
|
if (creds && !cancelled) {
|
|
7983
|
-
const usage = await fetchCloudUsage(creds.accessToken);
|
|
7993
|
+
const usage = await fetchCloudUsage(creds.accessToken, creds.deviceId);
|
|
7984
7994
|
if (usage && !cancelled) {
|
|
7985
7995
|
setCloudAuth({
|
|
7986
7996
|
phase: "success",
|
|
@@ -12983,13 +12993,15 @@ function App({
|
|
|
12983
12993
|
initialUpdateResult,
|
|
12984
12994
|
initialLspScope,
|
|
12985
12995
|
initialLspProjectPath,
|
|
12986
|
-
initialCloudToken
|
|
12996
|
+
initialCloudToken,
|
|
12997
|
+
initialCloudDeviceId
|
|
12987
12998
|
}) {
|
|
12988
12999
|
const { exit } = useApp();
|
|
12989
13000
|
const [cfg, setCfg] = useState10(initialCfg);
|
|
12990
13001
|
const [lspScope, setLspScope] = useState10(initialLspScope);
|
|
12991
13002
|
const [lspProjectPath, setLspProjectPath] = useState10(initialLspProjectPath);
|
|
12992
13003
|
const [cloudToken, setCloudToken] = useState10(initialCloudToken);
|
|
13004
|
+
const [cloudDeviceId, setCloudDeviceId] = useState10(initialCloudDeviceId);
|
|
12993
13005
|
const [events, setRawEvents] = useState10([]);
|
|
12994
13006
|
const setEvents = useCallback2(
|
|
12995
13007
|
(updater) => {
|
|
@@ -13043,7 +13055,7 @@ function App({
|
|
|
13043
13055
|
let cancelled = false;
|
|
13044
13056
|
const fetchBudget = async () => {
|
|
13045
13057
|
const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
|
|
13046
|
-
const usage2 = await fetchCloudUsage2(initialCloudToken);
|
|
13058
|
+
const usage2 = await fetchCloudUsage2(initialCloudToken, cloudDeviceId ?? initialCloudDeviceId);
|
|
13047
13059
|
if (usage2 && !cancelled) {
|
|
13048
13060
|
setCloudBudget({ remaining: usage2.remaining, limit: usage2.input_token_limit });
|
|
13049
13061
|
}
|
|
@@ -13967,6 +13979,7 @@ function App({
|
|
|
13967
13979
|
codeMode: effectiveCodeMode,
|
|
13968
13980
|
cloudMode: cfg.cloudMode,
|
|
13969
13981
|
cloudToken: cloudToken ?? initialCloudToken,
|
|
13982
|
+
cloudDeviceId: cloudDeviceId ?? initialCloudDeviceId,
|
|
13970
13983
|
onIterationEnd,
|
|
13971
13984
|
onFileChange: (path, content) => {
|
|
13972
13985
|
if (content) {
|
|
@@ -15177,6 +15190,7 @@ ${lines.join("\n")}` }]);
|
|
|
15177
15190
|
codeMode: effectiveCodeMode,
|
|
15178
15191
|
cloudMode: cfg.cloudMode,
|
|
15179
15192
|
cloudToken: cloudToken ?? initialCloudToken,
|
|
15193
|
+
cloudDeviceId: cloudDeviceId ?? initialCloudDeviceId,
|
|
15180
15194
|
onIterationEnd,
|
|
15181
15195
|
intentClassification: classification,
|
|
15182
15196
|
onFileChange: (path, content2) => {
|
|
@@ -15678,7 +15692,7 @@ ${lines.join("\n")}` }]);
|
|
|
15678
15692
|
] })
|
|
15679
15693
|
] }) });
|
|
15680
15694
|
}
|
|
15681
|
-
async function renderApp(cfg, updateResult, lspScope = "global", lspProjectPath = null, cloudToken) {
|
|
15695
|
+
async function renderApp(cfg, updateResult, lspScope = "global", lspProjectPath = null, cloudToken, cloudDeviceId) {
|
|
15682
15696
|
const instance = render(
|
|
15683
15697
|
/* @__PURE__ */ jsx23(
|
|
15684
15698
|
App,
|
|
@@ -15687,7 +15701,8 @@ async function renderApp(cfg, updateResult, lspScope = "global", lspProjectPath
|
|
|
15687
15701
|
initialUpdateResult: updateResult,
|
|
15688
15702
|
initialLspScope: lspScope,
|
|
15689
15703
|
initialLspProjectPath: lspProjectPath,
|
|
15690
|
-
initialCloudToken: cloudToken
|
|
15704
|
+
initialCloudToken: cloudToken,
|
|
15705
|
+
initialCloudDeviceId: cloudDeviceId
|
|
15691
15706
|
}
|
|
15692
15707
|
),
|
|
15693
15708
|
{
|
|
@@ -15901,7 +15916,7 @@ program.command("usage").description("Show Kimiflare Cloud token usage (requires
|
|
|
15901
15916
|
process.exit(1);
|
|
15902
15917
|
}
|
|
15903
15918
|
const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
|
|
15904
|
-
const usage = await fetchCloudUsage2(creds.accessToken);
|
|
15919
|
+
const usage = await fetchCloudUsage2(creds.accessToken, creds.deviceId);
|
|
15905
15920
|
if (!usage) {
|
|
15906
15921
|
console.error("Failed to fetch usage: invalid response from server");
|
|
15907
15922
|
process.exit(1);
|
|
@@ -15944,7 +15959,7 @@ Kimiflare Cloud Authentication`);
|
|
|
15944
15959
|
});
|
|
15945
15960
|
console.log(`Authenticated! Token expires at ${new Date(creds.expiresAt * 1e3).toISOString()}`);
|
|
15946
15961
|
const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
|
|
15947
|
-
const usage = await fetchCloudUsage2(creds.accessToken);
|
|
15962
|
+
const usage = await fetchCloudUsage2(creds.accessToken, creds.deviceId);
|
|
15948
15963
|
if (usage) {
|
|
15949
15964
|
console.log(`
|
|
15950
15965
|
Token budget: ${usage.remaining.toLocaleString()} / ${usage.input_token_limit.toLocaleString()} remaining`);
|
|
@@ -15979,6 +15994,7 @@ async function main() {
|
|
|
15979
15994
|
}
|
|
15980
15995
|
const cloudMode = opts.cloud ?? cfg?.cloudMode ?? false;
|
|
15981
15996
|
let cloudToken;
|
|
15997
|
+
let cloudDeviceId;
|
|
15982
15998
|
if (cloudMode) {
|
|
15983
15999
|
const { loadCloudCredentials: loadCloudCredentials2, authenticateDevice: authenticateDevice2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
|
|
15984
16000
|
let cloudCreds = await loadCloudCredentials2();
|
|
@@ -15987,6 +16003,7 @@ async function main() {
|
|
|
15987
16003
|
process.exit(2);
|
|
15988
16004
|
}
|
|
15989
16005
|
cloudToken = cloudCreds.accessToken;
|
|
16006
|
+
cloudDeviceId = cloudCreds.deviceId;
|
|
15990
16007
|
cfg = {
|
|
15991
16008
|
...cfg ?? { accountId: "", apiToken: "", model: DEFAULT_MODEL },
|
|
15992
16009
|
cloudMode: true
|
|
@@ -16008,6 +16025,7 @@ async function main() {
|
|
|
16008
16025
|
showReasoning: !!opts.reasoning,
|
|
16009
16026
|
codeMode: cfg.codeMode,
|
|
16010
16027
|
cloudToken,
|
|
16028
|
+
cloudDeviceId,
|
|
16011
16029
|
continueOnLimit: !!opts.continueOnLimit,
|
|
16012
16030
|
maxInputTokens: opts.maxInputTokens,
|
|
16013
16031
|
updateResult
|
|
@@ -16023,9 +16041,9 @@ async function main() {
|
|
|
16023
16041
|
const { renderApp: renderApp2 } = await Promise.resolve().then(() => (init_app(), app_exports));
|
|
16024
16042
|
if (cfg) {
|
|
16025
16043
|
const model = opts.model ?? cfg.model ?? DEFAULT_MODEL;
|
|
16026
|
-
await renderApp2({ ...cfg, model }, updateResult, lspScope, lspProjectPath, cloudToken);
|
|
16044
|
+
await renderApp2({ ...cfg, model }, updateResult, lspScope, lspProjectPath, cloudToken, cloudDeviceId);
|
|
16027
16045
|
} else {
|
|
16028
|
-
await renderApp2(null, updateResult, lspScope, lspProjectPath, cloudToken);
|
|
16046
|
+
await renderApp2(null, updateResult, lspScope, lspProjectPath, cloudToken, cloudDeviceId);
|
|
16029
16047
|
}
|
|
16030
16048
|
}
|
|
16031
16049
|
function gatewayFromPrintOpts(opts2) {
|
|
@@ -16077,6 +16095,7 @@ async function runPrintMode(opts2) {
|
|
|
16077
16095
|
maxInputTokens: opts2.maxInputTokens,
|
|
16078
16096
|
cloudMode: opts2.cloudMode,
|
|
16079
16097
|
cloudToken: opts2.cloudToken,
|
|
16098
|
+
cloudDeviceId: opts2.cloudDeviceId,
|
|
16080
16099
|
coauthor: opts2.coauthor !== false ? { name: opts2.coauthorName || "kimiflare", email: opts2.coauthorEmail || "kimiflare@proton.me" } : void 0,
|
|
16081
16100
|
callbacks: {
|
|
16082
16101
|
onReasoningDelta: opts2.showReasoning ? (delta) => {
|