kimiflare 0.13.5 → 0.13.6

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
@@ -230,87 +230,57 @@ async function* parseStream(body, signal) {
230
230
  const toolCalls = /* @__PURE__ */ new Map();
231
231
  let lastUsage = null;
232
232
  let finishReason = null;
233
- let timeoutId = null;
234
- let timedOut = false;
235
- const resetTimeout = () => {
236
- if (timeoutId) clearTimeout(timeoutId);
237
- timeoutId = setTimeout(() => {
238
- timedOut = true;
239
- }, STREAM_TIMEOUT_MS);
240
- };
241
- try {
242
- for await (const dataStr of readSSE(body, signal)) {
243
- if (timedOut) {
244
- throw new KimiApiError(
245
- `kimiflare: stream timed out (no data for ${STREAM_TIMEOUT_MS / 1e3}s)`,
246
- void 0,
247
- void 0
248
- );
249
- }
250
- resetTimeout();
251
- if (dataStr === "[DONE]") break;
252
- let chunk = null;
253
- try {
254
- chunk = JSON.parse(dataStr);
255
- } catch {
256
- continue;
233
+ for await (const dataStr of readSSE(body, signal)) {
234
+ if (dataStr === "[DONE]") break;
235
+ let chunk = null;
236
+ try {
237
+ chunk = JSON.parse(dataStr);
238
+ } catch {
239
+ continue;
240
+ }
241
+ if (!chunk) continue;
242
+ if (chunk.usage) {
243
+ lastUsage = chunk.usage;
244
+ yield { type: "usage", usage: chunk.usage };
245
+ }
246
+ const choice = chunk.choices?.[0];
247
+ if (!choice) continue;
248
+ const d = choice.delta;
249
+ if (d) {
250
+ if (typeof d.reasoning_content === "string" && d.reasoning_content.length) {
251
+ yield { type: "reasoning", delta: d.reasoning_content };
257
252
  }
258
- if (!chunk) continue;
259
- if (chunk.usage) {
260
- lastUsage = chunk.usage;
261
- yield { type: "usage", usage: chunk.usage };
253
+ if (typeof d.content === "string" && d.content.length) {
254
+ yield { type: "text", delta: d.content };
262
255
  }
263
- const choice = chunk.choices?.[0];
264
- if (!choice) continue;
265
- const d = choice.delta;
266
- if (d) {
267
- if (typeof d.reasoning_content === "string" && d.reasoning_content.length) {
268
- yield { type: "reasoning", delta: d.reasoning_content };
269
- }
270
- if (typeof d.content === "string" && d.content.length) {
271
- yield { type: "text", delta: d.content };
272
- }
273
- if (Array.isArray(d.tool_calls)) {
274
- for (const tc of d.tool_calls) {
275
- const idx = typeof tc.index === "number" ? tc.index : 0;
276
- let buf = toolCalls.get(idx);
277
- const incomingName = tc.function?.name ?? null;
278
- const incomingId = tc.id ?? null;
279
- if (!buf) {
280
- buf = { id: incomingId ?? `tc_${idx}`, name: incomingName ?? "", args: "" };
281
- toolCalls.set(idx, buf);
282
- if (buf.name) {
283
- yield { type: "tool_call_start", index: idx, id: buf.id, name: buf.name };
284
- }
285
- } else {
286
- if (!buf.name && incomingName) {
287
- buf.name = incomingName;
288
- yield { type: "tool_call_start", index: idx, id: buf.id, name: buf.name };
289
- }
290
- if (buf.id.startsWith("tc_") && incomingId) buf.id = incomingId;
256
+ if (Array.isArray(d.tool_calls)) {
257
+ for (const tc of d.tool_calls) {
258
+ const idx = typeof tc.index === "number" ? tc.index : 0;
259
+ let buf = toolCalls.get(idx);
260
+ const incomingName = tc.function?.name ?? null;
261
+ const incomingId = tc.id ?? null;
262
+ if (!buf) {
263
+ buf = { id: incomingId ?? `tc_${idx}`, name: incomingName ?? "", args: "" };
264
+ toolCalls.set(idx, buf);
265
+ if (buf.name) {
266
+ yield { type: "tool_call_start", index: idx, id: buf.id, name: buf.name };
291
267
  }
292
- const argDelta = tc.function?.arguments;
293
- if (typeof argDelta === "string" && argDelta.length) {
294
- buf.args += argDelta;
295
- yield { type: "tool_call_args", index: idx, argsDelta: argDelta };
268
+ } else {
269
+ if (!buf.name && incomingName) {
270
+ buf.name = incomingName;
271
+ yield { type: "tool_call_start", index: idx, id: buf.id, name: buf.name };
296
272
  }
273
+ if (buf.id.startsWith("tc_") && incomingId) buf.id = incomingId;
274
+ }
275
+ const argDelta = tc.function?.arguments;
276
+ if (typeof argDelta === "string" && argDelta.length) {
277
+ buf.args += argDelta;
278
+ yield { type: "tool_call_args", index: idx, argsDelta: argDelta };
297
279
  }
298
280
  }
299
281
  }
300
- if (choice.finish_reason) finishReason = choice.finish_reason;
301
282
  }
302
- for (const [idx, buf] of [...toolCalls.entries()].sort((a, b) => a[0] - b[0])) {
303
- if (!buf.name) continue;
304
- yield {
305
- type: "tool_call_complete",
306
- index: idx,
307
- id: buf.id,
308
- name: buf.name,
309
- arguments: buf.args
310
- };
311
- }
312
- } finally {
313
- if (timeoutId) clearTimeout(timeoutId);
283
+ if (choice.finish_reason) finishReason = choice.finish_reason;
314
284
  }
315
285
  for (const [idx, buf] of [...toolCalls.entries()].sort((a, b) => a[0] - b[0])) {
316
286
  if (!buf.name) continue;
@@ -384,7 +354,7 @@ function sleep(ms, signal) {
384
354
  signal?.addEventListener("abort", onAbort, { once: true });
385
355
  });
386
356
  }
387
- var RETRYABLE_CODES, MAX_ATTEMPTS, STREAM_TIMEOUT_MS;
357
+ var RETRYABLE_CODES, MAX_ATTEMPTS;
388
358
  var init_client = __esm({
389
359
  "src/agent/client.ts"() {
390
360
  "use strict";
@@ -393,7 +363,6 @@ var init_client = __esm({
393
363
  init_messages();
394
364
  RETRYABLE_CODES = /* @__PURE__ */ new Set([3040]);
395
365
  MAX_ATTEMPTS = 5;
396
- STREAM_TIMEOUT_MS = 6e4;
397
366
  }
398
367
  });
399
368
 
@@ -3623,60 +3592,6 @@ function findImagePaths(text) {
3623
3592
  }
3624
3593
  return [...new Set(paths)];
3625
3594
  }
3626
- function stripImagesFromHistory(messages) {
3627
- for (const m of messages) {
3628
- if (!Array.isArray(m.content)) continue;
3629
- let changed = false;
3630
- const next = [];
3631
- for (const part of m.content) {
3632
- if (part.type === "image_url") {
3633
- changed = true;
3634
- next.push({ type: "text", text: "[image]" });
3635
- } else {
3636
- next.push(part);
3637
- }
3638
- }
3639
- if (changed) {
3640
- m.content = next;
3641
- }
3642
- }
3643
- }
3644
- function truncateOldToolResults(messages, keepRecent) {
3645
- const toolIndices = [];
3646
- for (let i = 0; i < messages.length; i++) {
3647
- if (messages[i].role === "tool") toolIndices.push(i);
3648
- }
3649
- const cutoff = toolIndices.length - keepRecent;
3650
- for (let i = 0; i < cutoff; i++) {
3651
- const idx = toolIndices[i];
3652
- const m = messages[idx];
3653
- const text = typeof m.content === "string" ? m.content : "";
3654
- if (text.length > 500) {
3655
- m.content = text.slice(0, 500) + "\n\u2026 [truncated]";
3656
- }
3657
- }
3658
- }
3659
- async function maybeAutoCompact(messages, cfg, onInfo) {
3660
- const heapUsed = process.memoryUsage().heapUsed;
3661
- if (heapUsed < HEAP_AUTO_COMPACT_THRESHOLD || messages.length <= MSG_AUTO_COMPACT_THRESHOLD) {
3662
- return false;
3663
- }
3664
- onInfo("auto-compacting to reduce memory usage");
3665
- const result = await compactMessages({
3666
- accountId: cfg.accountId,
3667
- apiToken: cfg.apiToken,
3668
- model: cfg.model,
3669
- messages,
3670
- keepLastTurns: 2
3671
- });
3672
- if (result.replacedCount > 0) {
3673
- messages.length = 0;
3674
- messages.push(...result.newMessages);
3675
- onInfo(`compacted ${result.replacedCount} messages into a summary`);
3676
- return true;
3677
- }
3678
- return false;
3679
- }
3680
3595
  function App({ initialCfg, initialUpdateResult }) {
3681
3596
  const { exit } = useApp();
3682
3597
  const [cfg, setCfg] = useState6(initialCfg);
@@ -4096,13 +4011,6 @@ function App({ initialCfg, initialUpdateResult }) {
4096
4011
  })
4097
4012
  }
4098
4013
  });
4099
- stripImagesFromHistory(messagesRef.current);
4100
- truncateOldToolResults(messagesRef.current, 8);
4101
- await maybeAutoCompact(
4102
- messagesRef.current,
4103
- cfg,
4104
- (text) => setEvents((es) => [...es, { kind: "info", key: mkKey(), text }])
4105
- );
4106
4014
  if (existsSync(join6(cwd, "KIMI.md"))) {
4107
4015
  messagesRef.current[0] = {
4108
4016
  role: "system",
@@ -4545,14 +4453,7 @@ use: /thinking low | medium | high`
4545
4453
  })
4546
4454
  }
4547
4455
  });
4548
- stripImagesFromHistory(messagesRef.current);
4549
- truncateOldToolResults(messagesRef.current, 8);
4550
- await maybeAutoCompact(
4551
- messagesRef.current,
4552
- cfg,
4553
- (text2) => setEvents((es) => [...es, { kind: "info", key: mkKey(), text: text2 }])
4554
- );
4555
- void saveSessionSafe();
4456
+ await saveSessionSafe();
4556
4457
  } catch (e) {
4557
4458
  if (e.name === "AbortError") {
4558
4459
  setEvents((es) => [...es, { kind: "info", key: mkKey(), text: "(aborted)" }]);
@@ -4742,7 +4643,7 @@ async function renderApp(cfg, updateResult) {
4742
4643
  const instance = render(/* @__PURE__ */ jsx13(App, { initialCfg: cfg, initialUpdateResult: updateResult }));
4743
4644
  await instance.waitUntilExit();
4744
4645
  }
4745
- var CONTEXT_LIMIT, AUTO_COMPACT_SUGGEST_PCT, MAX_EVENTS, nextAssistantId, nextKey, mkKey, MAX_IMAGES_PER_MESSAGE, EFFORT_DESCRIPTIONS, HEAP_AUTO_COMPACT_THRESHOLD, MSG_AUTO_COMPACT_THRESHOLD;
4646
+ var CONTEXT_LIMIT, AUTO_COMPACT_SUGGEST_PCT, MAX_EVENTS, nextAssistantId, nextKey, mkKey, MAX_IMAGES_PER_MESSAGE, EFFORT_DESCRIPTIONS;
4746
4647
  var init_app = __esm({
4747
4648
  "src/app.tsx"() {
4748
4649
  "use strict";
@@ -4779,8 +4680,6 @@ var init_app = __esm({
4779
4680
  medium: "medium \u2014 balanced (default). Solid quality on most edits, fast on trivial prompts.",
4780
4681
  high: "high \u2014 deepest reasoning; slowest. Best for complex debugging, architecture, multi-file refactors."
4781
4682
  };
4782
- HEAP_AUTO_COMPACT_THRESHOLD = 2e9;
4783
- MSG_AUTO_COMPACT_THRESHOLD = 50;
4784
4683
  }
4785
4684
  });
4786
4685