kimiflare 0.36.0 → 0.36.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 CHANGED
@@ -483,6 +483,9 @@ function isRetryable(err, attempt) {
483
483
  return false;
484
484
  }
485
485
  async function* runKimi(opts2) {
486
+ if (opts2.cloudMode && !opts2.cloudToken) {
487
+ throw new KimiApiError("kimiflare: cloud mode requires a cloud token. Run `kimiflare auth cloud` to authenticate.", void 0, 401);
488
+ }
486
489
  const { url, headers: gatewayHeaders } = buildKimiRequestTarget(opts2);
487
490
  const body = {
488
491
  messages: sanitizeMessagesForApi(opts2.messages),
@@ -4702,7 +4705,16 @@ async function fetchCloudUsage(token) {
4702
4705
  headers: { Authorization: `Bearer ${token}` }
4703
4706
  });
4704
4707
  if (!res.ok) return null;
4705
- return await res.json();
4708
+ const data = await res.json();
4709
+ if (typeof data.remaining !== "number" || typeof data.input_token_limit !== "number" || typeof data.input_tokens_used !== "number" || typeof data.expires_at !== "string") {
4710
+ return null;
4711
+ }
4712
+ return {
4713
+ input_token_limit: data.input_token_limit,
4714
+ input_tokens_used: data.input_tokens_used,
4715
+ remaining: data.remaining,
4716
+ expires_at: data.expires_at
4717
+ };
4706
4718
  }
4707
4719
  async function loadCloudCredentials() {
4708
4720
  try {
@@ -12977,6 +12989,7 @@ function App({
12977
12989
  const [cfg, setCfg] = useState10(initialCfg);
12978
12990
  const [lspScope, setLspScope] = useState10(initialLspScope);
12979
12991
  const [lspProjectPath, setLspProjectPath] = useState10(initialLspProjectPath);
12992
+ const [cloudToken, setCloudToken] = useState10(initialCloudToken);
12980
12993
  const [events, setRawEvents] = useState10([]);
12981
12994
  const setEvents = useCallback2(
12982
12995
  (updater) => {
@@ -13953,7 +13966,7 @@ function App({
13953
13966
  memoryManager: memoryManagerRef.current,
13954
13967
  codeMode: effectiveCodeMode,
13955
13968
  cloudMode: cfg.cloudMode,
13956
- cloudToken: initialCloudToken,
13969
+ cloudToken: cloudToken ?? initialCloudToken,
13957
13970
  onIterationEnd,
13958
13971
  onFileChange: (path, content) => {
13959
13972
  if (content) {
@@ -15163,7 +15176,7 @@ ${lines.join("\n")}` }]);
15163
15176
  keepLastImageTurns: cfg.imageHistoryTurns ?? 2,
15164
15177
  codeMode: effectiveCodeMode,
15165
15178
  cloudMode: cfg.cloudMode,
15166
- cloudToken: initialCloudToken,
15179
+ cloudToken: cloudToken ?? initialCloudToken,
15167
15180
  onIterationEnd,
15168
15181
  intentClassification: classification,
15169
15182
  onFileChange: (path, content2) => {
@@ -15362,6 +15375,7 @@ ${lines.join("\n")}` }]);
15362
15375
  const { loadCloudCredentials: loadCloudCredentials2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
15363
15376
  const creds = await loadCloudCredentials2();
15364
15377
  if (creds) {
15378
+ setCloudToken(creds.accessToken);
15365
15379
  setEvents((e) => [
15366
15380
  ...e,
15367
15381
  { kind: "info", key: mkKey(), text: "configuration saved \u2014 welcome to kimiflare! (cloud mode)" }
@@ -15886,14 +15900,12 @@ program.command("usage").description("Show Kimiflare Cloud token usage (requires
15886
15900
  console.error("Not authenticated with Kimiflare Cloud. Run: kimiflare auth cloud");
15887
15901
  process.exit(1);
15888
15902
  }
15889
- const res = await fetch("https://api.kimiflare.com/v1/usage", {
15890
- headers: { Authorization: `Bearer ${creds.accessToken}` }
15891
- });
15892
- if (!res.ok) {
15893
- console.error(`Failed to fetch usage: ${res.status} ${res.statusText}`);
15903
+ const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
15904
+ const usage = await fetchCloudUsage2(creds.accessToken);
15905
+ if (!usage) {
15906
+ console.error("Failed to fetch usage: invalid response from server");
15894
15907
  process.exit(1);
15895
15908
  }
15896
- const usage = await res.json();
15897
15909
  console.log(`Token budget: ${usage.remaining.toLocaleString()} / ${usage.input_token_limit.toLocaleString()} remaining`);
15898
15910
  console.log(`Used: ${usage.input_tokens_used.toLocaleString()}`);
15899
15911
  console.log(`Grant expires: ${usage.expires_at}`);
@@ -15931,11 +15943,9 @@ Kimiflare Cloud Authentication`);
15931
15943
  }
15932
15944
  });
15933
15945
  console.log(`Authenticated! Token expires at ${new Date(creds.expiresAt * 1e3).toISOString()}`);
15934
- const usageRes = await fetch("https://api.kimiflare.com/v1/usage", {
15935
- headers: { Authorization: `Bearer ${creds.accessToken}` }
15936
- });
15937
- if (usageRes.ok) {
15938
- const usage = await usageRes.json();
15946
+ const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
15947
+ const usage = await fetchCloudUsage2(creds.accessToken);
15948
+ if (usage) {
15939
15949
  console.log(`
15940
15950
  Token budget: ${usage.remaining.toLocaleString()} / ${usage.input_token_limit.toLocaleString()} remaining`);
15941
15951
  console.log(`Grant expires: ${usage.expires_at}`);