Desktop: Fixes #16783: Fix AI chat counting tool results twice towards the size limit (#16784)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ahmet SezenandClaude Opus 5.5 authored and GitHub committed 2026-10-11 16:02:13 +01:00
1 parent 8905795ed1
commit c51a2cb7e1
2 files changed
+15 -4

No files matched your search

+15 -1
View File
@@ -1,5 +1,5 @@
import { _internal, ChatCommands, runNoteChat } from './noteChat';
import { ChatRole, ChatToolCall } from './types';
import { ChatRole, ChatToolCall, ChatToolMessage } from './types';
import { expectThrow, setupDatabase, switchClient, withWarningSilenced } from '../../testing/test-utils';
import Setting from '../../models/Setting';
import { NoteContext } from './tools/types';
@@ -159,6 +159,20 @@ describe('noteChat', () => {
}, 'aiNoteTooLarge');
});
test('assertWithinTokenBudget should count tool results once', async () => {
const toolResult = (content: string): ChatToolMessage => ({
role: ChatRole.Tool, content, toolName: 'editor_readNoteBody', toolCallId: 'call-1', userDescription: '', isEdit: false, isError: false,
});
// About 75 tokens
_internal.assertWithinTokenBudget([toolResult('Testing... '.repeat(27))], 100);
await expectThrow(async () => {
// About 125 tokens
_internal.assertWithinTokenBudget([toolResult('Testing... '.repeat(45))], 100);
}, 'aiNoteTooLarge');
});
test('runTools should describe successful edit operations', async () => {
const toolCalls: ChatToolCall[] = [
{ toolName: 'editor_appendToNote', callId: 'call-1', arguments: { text: 'Test.' }, parseError: null },
-3
View File
@@ -241,9 +241,6 @@ const assertWithinTokenBudget = (history: ChatMessage[], budget: number) => {
if (message.role === ChatRole.Assistant) {
totalTokens += estimateToolCallTokens(message.toolCalls ?? []);
}
if (message.role === ChatRole.Tool) {
totalTokens += estimateTokens(message.content);
}
}
if (totalTokens > budget) {