* 过滤工具上下文
(
toolContext: ContextData['layers']['tool'],
options: Required<FilterOptions>
)
| 92 | * 过滤工具上下文 |
| 93 | */ |
| 94 | private filterTools( |
| 95 | toolContext: ContextData['layers']['tool'], |
| 96 | options: Required<FilterOptions> |
| 97 | ): ContextData['layers']['tool'] { |
| 98 | let filteredCalls = [...toolContext.recentCalls]; |
| 99 | |
| 100 | // 时间窗口过滤 |
| 101 | if (options.timeWindow > 0) { |
| 102 | const cutoffTime = Date.now() - options.timeWindow; |
| 103 | filteredCalls = filteredCalls.filter(call => call.timestamp >= cutoffTime); |
| 104 | } |
| 105 | |
| 106 | // 保留最近的成功调用和失败调用(用于学习) |
| 107 | const successCalls = filteredCalls.filter(call => call.status === 'success'); |
| 108 | const failedCalls = filteredCalls.filter(call => call.status === 'error'); |
| 109 | |
| 110 | // 限制每种状态的调用数量 |
| 111 | const maxSuccessfulCalls = Math.min(20, successCalls.length); |
| 112 | const maxFailedCalls = Math.min(10, failedCalls.length); |
| 113 | |
| 114 | const limitedCalls = [ |
| 115 | ...successCalls.slice(-maxSuccessfulCalls), |
| 116 | ...failedCalls.slice(-maxFailedCalls), |
| 117 | ].sort((a, b) => a.timestamp - b.timestamp); |
| 118 | |
| 119 | return { |
| 120 | recentCalls: limitedCalls, |
| 121 | toolStates: toolContext.toolStates, |
| 122 | dependencies: toolContext.dependencies, |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * 按优先级过滤消息 |