foliko 1.0.65 → 1.0.67

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.
@@ -6,6 +6,24 @@
6
6
  const { EventEmitter } = require('../utils/event-emitter')
7
7
  const tiktoken = require('tiktoken')
8
8
 
9
+ // 模型上下文限制表(留 15-20% 余量给 system prompt 和输出)
10
+ const MODEL_CONTEXT_LIMITS = {
11
+ // DeepSeek
12
+ 'deepseek-chat': 28000,
13
+ 'deepseek-coder': 28000,
14
+ // MiniMax
15
+ 'MiniMax-M2.7': 90000,
16
+ // OpenAI
17
+ 'gpt-4': 100000,
18
+ 'gpt-4o': 100000,
19
+ 'gpt-4o-mini': 100000,
20
+ 'gpt-4-turbo': 100000,
21
+ // Anthropic
22
+ 'claude-3-5-sonnet': 150000,
23
+ 'claude-3-opus': 150000,
24
+ 'claude-3-sonnet': 150000,
25
+ }
26
+
9
27
  class AgentChatHandler extends EventEmitter {
10
28
  /**
11
29
  * @param {Agent} agent - Agent 实例
@@ -28,10 +46,14 @@ class AgentChatHandler extends EventEmitter {
28
46
  this._tools = new Map()
29
47
  this._maxSteps = 20
30
48
 
31
- // 上下文压缩配置
32
- this._maxContextTokens = config.maxContextTokens || 100000
33
- this._compressionThreshold = config.compressionThreshold || 0.75
34
- this._keepRecentMessages = config.keepRecentMessages || 30
49
+ // 上下文压缩配置:根据模型自动设置限制
50
+ const modelKey = Object.keys(MODEL_CONTEXT_LIMITS).find(k =>
51
+ this.model.toLowerCase().includes(k.toLowerCase())
52
+ )
53
+ const defaultLimit = modelKey ? MODEL_CONTEXT_LIMITS[modelKey] : 40000
54
+ this._maxContextTokens = config.maxContextTokens || defaultLimit
55
+ this._compressionThreshold = config.compressionThreshold || 0.6 // 60% 就压缩,早触发
56
+ this._keepRecentMessages = config.keepRecentMessages || 20 // 保留最近 20 条
35
57
  this._enableSmartCompress = config.enableSmartCompress !== false // 默认开启智能摘要
36
58
  this._encoder = null
37
59
  this._compressionCount = 0 // 压缩次数统计