|
| 1 | +const { |
| 2 | + AIMessage, |
| 3 | + HumanMessage, |
| 4 | + ToolMessage, |
| 5 | + FunctionMessage, |
| 6 | +} = require('@langchain/core/messages'); |
| 7 | +const { syncBudgetDerivedFields } = require('@librechat/agents'); |
| 8 | + |
| 9 | +function snapshot(messageTokens = 1000, calibrationRatio = 1) { |
| 10 | + return { |
| 11 | + calibrationRatio, |
| 12 | + contextBudget: 1100, |
| 13 | + effectiveInstructionTokens: 100, |
| 14 | + remainingContextTokens: 1000 - messageTokens, |
| 15 | + breakdown: { messageTokens }, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function result(id, name) { |
| 20 | + return new ToolMessage({ content: 'result', tool_call_id: id, name }); |
| 21 | +} |
| 22 | + |
| 23 | +describe('agents tool context accounting', () => { |
| 24 | + test('counts retained results and tool-only invocations without double counting raw calls', () => { |
| 25 | + const usage = snapshot(); |
| 26 | + const messages = [ |
| 27 | + new HumanMessage('question'), |
| 28 | + new AIMessage({ |
| 29 | + content: '', |
| 30 | + tool_calls: [{ id: 'a', name: 'read_file', args: {} }], |
| 31 | + additional_kwargs: { |
| 32 | + tool_calls: [ |
| 33 | + { id: 'a', type: 'function', function: { name: 'read_file', arguments: '{}' } }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + }), |
| 37 | + result('a'), |
| 38 | + new AIMessage('answer'), |
| 39 | + ]; |
| 40 | + syncBudgetDerivedFields(usage, messages, () => 10); |
| 41 | + expect(usage.breakdown.toolMessageTokens).toBe(20); |
| 42 | + expect(usage.breakdown.toolMessageTokenCounts).toEqual({ read_file: 10 }); |
| 43 | + }); |
| 44 | + |
| 45 | + test('keeps visible text and media assistant content outside the tool share', () => { |
| 46 | + const content = [ |
| 47 | + 'explaining', |
| 48 | + ['explaining'], |
| 49 | + [{ type: 'image_url', image_url: { url: 'https://example.com/image.png' } }], |
| 50 | + ]; |
| 51 | + for (const mixed of content) { |
| 52 | + const usage = snapshot(); |
| 53 | + const messages = [ |
| 54 | + new AIMessage({ content: mixed, tool_calls: [{ id: 'a', name: 'read_file', args: {} }] }), |
| 55 | + result('a'), |
| 56 | + ]; |
| 57 | + syncBudgetDerivedFields(usage, messages, () => 10); |
| 58 | + expect(usage.breakdown.toolMessageTokens).toBe(10); |
| 59 | + expect(usage.breakdown.toolMessageTokenCounts).toEqual({ read_file: 10 }); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + test('counts tool-only reasoning and inline provider results without attributing invocation overhead', () => { |
| 64 | + const usage = snapshot(); |
| 65 | + syncBudgetDerivedFields( |
| 66 | + usage, |
| 67 | + [ |
| 68 | + new AIMessage({ |
| 69 | + content: [ |
| 70 | + { type: 'thinking', thinking: 'reasoning' }, |
| 71 | + { type: 'tool_use', id: 'a', name: 'file_search', input: {} }, |
| 72 | + { type: 'web_search_tool_result', tool_use_id: 'a', content: [] }, |
| 73 | + ], |
| 74 | + }), |
| 75 | + result('a', 'file_search'), |
| 76 | + ], |
| 77 | + () => 10, |
| 78 | + ); |
| 79 | + expect(usage.breakdown.toolMessageTokens).toBe(20); |
| 80 | + expect(usage.breakdown.toolMessageTokenCounts).toEqual({ file_search: 10 }); |
| 81 | + }); |
| 82 | + |
| 83 | + test('attributes raw calls, legacy functions, and unknown results without guessing an unrelated name', () => { |
| 84 | + const usage = snapshot(); |
| 85 | + syncBudgetDerivedFields( |
| 86 | + usage, |
| 87 | + [ |
| 88 | + new AIMessage({ |
| 89 | + content: '', |
| 90 | + additional_kwargs: { |
| 91 | + tool_calls: [ |
| 92 | + { id: 'raw', type: 'function', function: { name: 'raw_tool', arguments: '{}' } }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + }), |
| 96 | + result('raw'), |
| 97 | + new AIMessage({ |
| 98 | + content: '', |
| 99 | + additional_kwargs: { function_call: { name: 'legacy_tool', arguments: '{}' } }, |
| 100 | + }), |
| 101 | + new FunctionMessage({ content: 'legacy result', name: '' }), |
| 102 | + result('missing', 'explicit_tool'), |
| 103 | + result('missing'), |
| 104 | + ], |
| 105 | + () => 10, |
| 106 | + ); |
| 107 | + expect(usage.breakdown.toolMessageTokens).toBe(60); |
| 108 | + expect(usage.breakdown.toolMessageTokenCounts).toEqual({ |
| 109 | + raw_tool: 10, |
| 110 | + legacy_tool: 10, |
| 111 | + explicit_tool: 10, |
| 112 | + unknown_tool: 10, |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + test('preserves prototype-sensitive tool names through JSON serialization', () => { |
| 117 | + const usage = snapshot(); |
| 118 | + syncBudgetDerivedFields( |
| 119 | + usage, |
| 120 | + [result('a', '__proto__'), result('b', 'constructor'), result('c', 'toString')], |
| 121 | + () => 10, |
| 122 | + ); |
| 123 | + const counts = JSON.parse(JSON.stringify(usage)).breakdown.toolMessageTokenCounts; |
| 124 | + expect(Object.hasOwn(counts, '__proto__')).toBe(true); |
| 125 | + expect(counts.__proto__).toBe(10); |
| 126 | + expect(counts.constructor).toBe(10); |
| 127 | + expect(counts.toString).toBe(10); |
| 128 | + }); |
| 129 | + |
| 130 | + test('apportions fractional calibration and budget clamping without exceeding the tool total', () => { |
| 131 | + for (const ratio of [0.5, 1, 1.5, 5]) { |
| 132 | + for (const available of [0, 1, 2, 10]) { |
| 133 | + const usage = snapshot(available, ratio); |
| 134 | + syncBudgetDerivedFields( |
| 135 | + usage, |
| 136 | + [result('a', 'a'), result('b', 'b'), result('c', 'c')], |
| 137 | + () => 1, |
| 138 | + ); |
| 139 | + const { toolMessageTokens, toolMessageTokenCounts } = usage.breakdown; |
| 140 | + expect(toolMessageTokens).toBe(Math.min(available, Math.round(3 * ratio))); |
| 141 | + const counts = Object.values(toolMessageTokenCounts ?? {}); |
| 142 | + expect(counts.every((count) => Number.isSafeInteger(count) && count >= 0)).toBe(true); |
| 143 | + expect(counts.reduce((sum, count) => sum + count, 0)).toBe(toolMessageTokens); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + test('distinguishes a known empty share from an unavailable counter', () => { |
| 149 | + const known = snapshot(); |
| 150 | + syncBudgetDerivedFields(known, [new HumanMessage('hello')], () => 10); |
| 151 | + expect(known.breakdown.toolMessageTokens).toBe(0); |
| 152 | + expect(known.breakdown.toolMessageTokenCounts).toBeUndefined(); |
| 153 | + const unavailable = snapshot(); |
| 154 | + syncBudgetDerivedFields(unavailable, [result('a')]); |
| 155 | + expect(unavailable.breakdown.toolMessageTokens).toBeUndefined(); |
| 156 | + }); |
| 157 | + |
| 158 | + test('drops an unavailable tool share without failing the model call', () => { |
| 159 | + for (const value of [NaN, Infinity, -1, Number.MAX_SAFE_INTEGER + 1]) { |
| 160 | + const usage = snapshot(); |
| 161 | + usage.breakdown.toolMessageTokens = 20; |
| 162 | + usage.breakdown.toolMessageTokenCounts = { read_file: 10 }; |
| 163 | + syncBudgetDerivedFields(usage, [result('a')], () => value); |
| 164 | + expect(usage.breakdown.toolMessageTokens).toBeUndefined(); |
| 165 | + expect(usage.breakdown.toolMessageTokenCounts).toBeUndefined(); |
| 166 | + expect(usage.breakdown.messageTokens).toBe(1000); |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + test('accepts approximate fractional token counters', () => { |
| 171 | + const usage = snapshot(); |
| 172 | + syncBudgetDerivedFields(usage, [result('a', 'file_search')], () => 1.5); |
| 173 | + expect(usage.breakdown.toolMessageTokens).toBe(2); |
| 174 | + expect(usage.breakdown.toolMessageTokenCounts).toEqual({ file_search: 2 }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments