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/cli/index.js CHANGED
@@ -859,6 +859,25 @@ function encode(text) {
859
859
  function countTokens(text) {
860
860
  return encode(text).length;
861
861
  }
862
+ function estimateConversationTokens(messages) {
863
+ let total = 0;
864
+ for (const m of messages) {
865
+ if (typeof m.content === "string" && m.content) {
866
+ total += countTokens(m.content);
867
+ }
868
+ if (m.tool_calls && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
869
+ total += countTokens(JSON.stringify(m.tool_calls));
870
+ }
871
+ }
872
+ return total;
873
+ }
874
+ function estimateRequestTokens(messages, toolSpecs) {
875
+ let total = estimateConversationTokens(messages);
876
+ if (toolSpecs && toolSpecs.length > 0) {
877
+ total += countTokens(JSON.stringify(toolSpecs));
878
+ }
879
+ return total;
880
+ }
862
881
 
863
882
  // src/repair/flatten.ts
864
883
  function analyzeSchema(schema) {
@@ -2002,7 +2021,32 @@ var CacheFirstLoop = class {
2002
2021
  content: `${iter}/${this.maxToolIters} tool calls used \u2014 approaching budget. Press Esc to force a summary now.`
2003
2022
  };
2004
2023
  }
2005
- const messages = this.buildMessages(pendingUser);
2024
+ let messages = this.buildMessages(pendingUser);
2025
+ {
2026
+ const ctxMax2 = DEEPSEEK_CONTEXT_TOKENS[this.model] ?? DEFAULT_CONTEXT_TOKENS;
2027
+ const estimate = estimateRequestTokens(messages, this.prefix.toolSpecs);
2028
+ if (estimate / ctxMax2 > 0.95) {
2029
+ const result = this.compact(1e3);
2030
+ if (result.healedCount > 0) {
2031
+ yield {
2032
+ turn: this._turn,
2033
+ role: "warning",
2034
+ content: `preflight: request ~${estimate.toLocaleString()}/${ctxMax2.toLocaleString()} tokens (${Math.round(
2035
+ estimate / ctxMax2 * 100
2036
+ )}%) \u2014 pre-compacted ${result.healedCount} tool result(s), saved ${result.tokensSaved.toLocaleString()} tokens. Sending.`
2037
+ };
2038
+ messages = this.buildMessages(pendingUser);
2039
+ } else {
2040
+ yield {
2041
+ turn: this._turn,
2042
+ role: "warning",
2043
+ content: `preflight: request ~${estimate.toLocaleString()}/${ctxMax2.toLocaleString()} tokens (${Math.round(
2044
+ estimate / ctxMax2 * 100
2045
+ )}%) and nothing to auto-compact \u2014 DeepSeek will likely 400. Run /forget or /clear to start fresh.`
2046
+ };
2047
+ }
2048
+ }
2049
+ }
2006
2050
  let assistantContent = "";
2007
2051
  let reasoningContent = "";
2008
2052
  let toolCalls = [];