reasonix 0.5.3 → 0.5.4

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
@@ -780,6 +780,25 @@ function encode(text) {
780
780
  function countTokens(text) {
781
781
  return encode(text).length;
782
782
  }
783
+ function estimateConversationTokens(messages) {
784
+ let total = 0;
785
+ for (const m of messages) {
786
+ if (typeof m.content === "string" && m.content) {
787
+ total += countTokens(m.content);
788
+ }
789
+ if (m.tool_calls && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
790
+ total += countTokens(JSON.stringify(m.tool_calls));
791
+ }
792
+ }
793
+ return total;
794
+ }
795
+ function estimateRequestTokens(messages, toolSpecs) {
796
+ let total = estimateConversationTokens(messages);
797
+ if (toolSpecs && toolSpecs.length > 0) {
798
+ total += countTokens(JSON.stringify(toolSpecs));
799
+ }
800
+ return total;
801
+ }
783
802
 
784
803
  // src/repair/flatten.ts
785
804
  function analyzeSchema(schema) {
@@ -1923,7 +1942,32 @@ var CacheFirstLoop = class {
1923
1942
  content: `${iter}/${this.maxToolIters} tool calls used \u2014 approaching budget. Press Esc to force a summary now.`
1924
1943
  };
1925
1944
  }
1926
- const messages = this.buildMessages(pendingUser);
1945
+ let messages = this.buildMessages(pendingUser);
1946
+ {
1947
+ const ctxMax2 = DEEPSEEK_CONTEXT_TOKENS[this.model] ?? DEFAULT_CONTEXT_TOKENS;
1948
+ const estimate = estimateRequestTokens(messages, this.prefix.toolSpecs);
1949
+ if (estimate / ctxMax2 > 0.95) {
1950
+ const result = this.compact(1e3);
1951
+ if (result.healedCount > 0) {
1952
+ yield {
1953
+ turn: this._turn,
1954
+ role: "warning",
1955
+ content: `preflight: request ~${estimate.toLocaleString()}/${ctxMax2.toLocaleString()} tokens (${Math.round(
1956
+ estimate / ctxMax2 * 100
1957
+ )}%) \u2014 pre-compacted ${result.healedCount} tool result(s), saved ${result.tokensSaved.toLocaleString()} tokens. Sending.`
1958
+ };
1959
+ messages = this.buildMessages(pendingUser);
1960
+ } else {
1961
+ yield {
1962
+ turn: this._turn,
1963
+ role: "warning",
1964
+ content: `preflight: request ~${estimate.toLocaleString()}/${ctxMax2.toLocaleString()} tokens (${Math.round(
1965
+ estimate / ctxMax2 * 100
1966
+ )}%) and nothing to auto-compact \u2014 DeepSeek will likely 400. Run /forget or /clear to start fresh.`
1967
+ };
1968
+ }
1969
+ }
1970
+ }
1927
1971
  let assistantContent = "";
1928
1972
  let reasoningContent = "";
1929
1973
  let toolCalls = [];