Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/utils/incrementalTokenCounter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it, beforeEach } from 'bun:test'
import { roughTokenCountEstimationForMessages } from '../services/tokenEstimation.js'
import { IncrementalTokenCounter, CounterFactory } from './incrementalTokenCounter.js'
import type { Message } from '../types/message.js'

Expand Down Expand Up @@ -247,6 +248,32 @@ describe('IncrementalTokenCounter', () => {
expect(count2).toBeGreaterThan(count1)
})
})

describe('attachment cache invalidation', () => {
it('recalculates when attachment-only messages differ', () => {
const counter = new IncrementalTokenCounter()
const smallAttachment = {
type: 'attachment',
attachment: {
type: 'opened_file_in_ide',
filename: 'a',
},
} as unknown as Message
const largeAttachment = {
type: 'attachment',
attachment: {
type: 'opened_file_in_ide',
filename: 'x'.repeat(2000),
},
} as unknown as Message

const smallCount = counter.getCount([smallAttachment])
const largeCount = counter.getCount([largeAttachment])

expect(largeCount).toBe(roughTokenCountEstimationForMessages([largeAttachment]))
expect(largeCount).toBeGreaterThan(smallCount)
})
})
})

describe('CounterFactory', () => {
Expand All @@ -270,4 +297,4 @@ describe('CounterFactory', () => {
expect(counter).toBeInstanceOf(IncrementalTokenCounter)
})
})
})
})
20 changes: 10 additions & 10 deletions src/utils/incrementalTokenCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export interface CounterStats {
function getMessageHash(messages: readonly Message[]): string {
if (messages.length === 0) return 'empty'

const fullContent = messages.map(m => {
const c = typeof m.message?.content === 'string'
? m.message.content
: Array.isArray(m.message?.content)
? JSON.stringify(m.message.content)
: ''
return c
}).join('|')
const tokenRelevantInput = messages.map(m => ({
type: m.type,
content: m.message?.content ?? null,
attachment: m.attachment ?? null,
}))

return createHash('sha256').update(fullContent).digest('hex').slice(0, 16)
return createHash('sha256')
.update(JSON.stringify(tokenRelevantInput))
.digest('hex')
.slice(0, 16)
}

/**
Expand Down Expand Up @@ -254,4 +254,4 @@ export const CounterFactory = {
estimationMultiplier: 1.2,
})
},
}
}