36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
import test from 'brittle'
|
|
import { readFileSync } from 'node:fs'
|
|
import vm from 'node:vm'
|
|
|
|
const STATE_SRC = readFileSync(new URL('../lib/agent-state.js', import.meta.url), 'utf8')
|
|
|
|
function loadTrimHelpers() {
|
|
const sandbox = { console, TextEncoder, TextDecoder }
|
|
vm.createContext(sandbox)
|
|
vm.runInContext(
|
|
STATE_SRC +
|
|
'\n;this.__exports = { bareAgentTrimMessages, bareAgentTrimMessagesForCtx, bareAgentEstimateTokens }',
|
|
sandbox
|
|
)
|
|
return sandbox.__exports
|
|
}
|
|
|
|
test('trim for ctx shrinks system when tools consume budget', async (t) => {
|
|
const { bareAgentTrimMessagesForCtx, bareAgentEstimateTokens } = loadTrimHelpers()
|
|
const tools = [{ name: 'x', description: 'y'.repeat(8000), parameters: {} }]
|
|
const msgs = [
|
|
{ role: 'system', content: 'S'.repeat(20_000) },
|
|
{ role: 'user', content: 'hello' }
|
|
]
|
|
const out = bareAgentTrimMessagesForCtx(msgs, 4096, {
|
|
tools,
|
|
reserveCompletion: 512
|
|
})
|
|
t.is(out.length, 2)
|
|
t.ok(String(/** @type {any} */ (out[0]).content).length < 20_000)
|
|
t.ok(
|
|
bareAgentEstimateTokens(out) + bareAgentEstimateTokens(tools) + 512 <=
|
|
4096 + 200
|
|
)
|
|
})
|