kimiflare 0.17.0 → 0.18.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 CHANGED
@@ -653,7 +653,8 @@ async function logTurnDebug(ctx) {
653
653
  toolTotalReducedBytes: toolTotalReduced,
654
654
  toolSavingsPct: toolTotalRaw > 0 ? Math.round((toolTotalRaw - toolTotalReduced) / toolTotalRaw * 100) : 0,
655
655
  cacheDiagnostics,
656
- compaction: ctx.compaction
656
+ compaction: ctx.compaction,
657
+ shadowStrip: ctx.shadowStrip
657
658
  });
658
659
  }
659
660
  var LOG_VERSION;
@@ -665,6 +666,53 @@ var init_cost_debug = __esm({
665
666
  }
666
667
  });
667
668
 
669
+ // src/agent/strip-reasoning.ts
670
+ function stripHistoricalReasoning(messages, opts2 = {}) {
671
+ const keepLast = opts2.keepLast ?? DEFAULT_KEEP_LAST;
672
+ const assistantIndices = [];
673
+ for (let i = 0; i < messages.length; i++) {
674
+ if (messages[i].role === "assistant") {
675
+ assistantIndices.push(i);
676
+ }
677
+ }
678
+ const preservedSet = keepLast === 0 ? /* @__PURE__ */ new Set() : new Set(assistantIndices.slice(-keepLast));
679
+ return messages.map((m, idx) => {
680
+ if (m.role !== "assistant") return m;
681
+ if (preservedSet.has(idx)) return m;
682
+ const next = { ...m };
683
+ delete next.reasoning_content;
684
+ if (next.tool_calls && next.tool_calls.length > 0) {
685
+ if (typeof next.content === "string") {
686
+ next.content = "";
687
+ } else if (Array.isArray(next.content)) {
688
+ next.content = next.content.map(
689
+ (part) => part.type === "text" ? { ...part, text: "" } : part
690
+ );
691
+ }
692
+ return next;
693
+ }
694
+ const textLen = typeof next.content === "string" ? next.content.length : Array.isArray(next.content) ? next.content.filter((p) => p.type === "text").reduce((sum, p) => sum + p.text.length, 0) : 0;
695
+ if (textLen <= SUBSTANTIVE_TEXT_THRESHOLD) {
696
+ if (typeof next.content === "string") {
697
+ next.content = "";
698
+ } else if (Array.isArray(next.content)) {
699
+ next.content = next.content.map(
700
+ (part) => part.type === "text" ? { ...part, text: "" } : part
701
+ );
702
+ }
703
+ }
704
+ return next;
705
+ });
706
+ }
707
+ var DEFAULT_KEEP_LAST, SUBSTANTIVE_TEXT_THRESHOLD;
708
+ var init_strip_reasoning = __esm({
709
+ "src/agent/strip-reasoning.ts"() {
710
+ "use strict";
711
+ DEFAULT_KEEP_LAST = 1;
712
+ SUBSTANTIVE_TEXT_THRESHOLD = 200;
713
+ }
714
+ });
715
+
668
716
  // src/agent/loop.ts
669
717
  async function runAgentTurn(opts2) {
670
718
  const max = opts2.maxToolIterations ?? 50;
@@ -679,11 +727,44 @@ async function runAgentTurn(opts2) {
679
727
  let content = "";
680
728
  let reasoning = "";
681
729
  opts2.callbacks.onAssistantStart?.();
730
+ const stripReasoning = process.env.KIMIFLARE_STRIP_REASONING === "1";
731
+ const shadowStrip = process.env.KIMIFLARE_SHADOW_STRIP === "1";
732
+ const keepLastRaw = process.env.KIMIFLARE_REASONING_KEEP_LAST;
733
+ const keepLast = keepLastRaw ? parseInt(keepLastRaw, 10) : 1;
734
+ let apiMessages = opts2.messages;
735
+ let shadowStripMetrics;
736
+ if (stripReasoning || shadowStrip) {
737
+ const stripped = stripHistoricalReasoning(opts2.messages, {
738
+ keepLast: Number.isNaN(keepLast) ? 1 : keepLast
739
+ });
740
+ if (shadowStrip) {
741
+ const originalSections = analyzePrompt(opts2.messages);
742
+ const strippedSections = analyzePrompt(stripped);
743
+ const originalApproxTokens = originalSections.reduce(
744
+ (sum, s) => sum + s.approxTokens,
745
+ 0
746
+ );
747
+ const strippedApproxTokens = strippedSections.reduce(
748
+ (sum, s) => sum + s.approxTokens,
749
+ 0
750
+ );
751
+ shadowStripMetrics = {
752
+ originalApproxTokens,
753
+ strippedApproxTokens,
754
+ savingsPct: originalApproxTokens > 0 ? Math.round(
755
+ (originalApproxTokens - strippedApproxTokens) / originalApproxTokens * 100
756
+ ) : 0
757
+ };
758
+ }
759
+ if (stripReasoning) {
760
+ apiMessages = stripped;
761
+ }
762
+ }
682
763
  const events = runKimi({
683
764
  accountId: opts2.accountId,
684
765
  apiToken: opts2.apiToken,
685
766
  model: opts2.model,
686
- messages: opts2.messages,
767
+ messages: apiMessages,
687
768
  tools: toolDefs,
688
769
  signal: opts2.signal,
689
770
  temperature: opts2.temperature,
@@ -750,7 +831,8 @@ async function runAgentTurn(opts2) {
750
831
  messages: opts2.messages,
751
832
  previousMessages,
752
833
  toolResults,
753
- usage: lastUsage
834
+ usage: lastUsage,
835
+ shadowStrip: shadowStripMetrics
754
836
  });
755
837
  }
756
838
  return;
@@ -778,7 +860,8 @@ async function runAgentTurn(opts2) {
778
860
  messages: opts2.messages,
779
861
  previousMessages,
780
862
  toolResults,
781
- usage: lastUsage
863
+ usage: lastUsage,
864
+ shadowStrip: shadowStripMetrics
782
865
  });
783
866
  }
784
867
  }
@@ -800,6 +883,7 @@ var init_loop = __esm({
800
883
  init_registry();
801
884
  init_messages();
802
885
  init_cost_debug();
886
+ init_strip_reasoning();
803
887
  }
804
888
  });
805
889