Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/agent-runtime/src/run-agent-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
countTokens,
countTokensJson,
countTokensMessages,
safeJsonStringify,
} from './util/token-counter'

import type { AgentTemplate } from '@codebuff/common/types/agent-template'
Expand Down Expand Up @@ -940,7 +941,7 @@ export async function loopAgentSteps(
const toolDefinitions = mapValues(tools, (tool) => ({
description:
typeof tool.description === 'string' ? tool.description : undefined,
inputSchema: tool.inputSchema as {},
inputSchema: JSON.parse(safeJsonStringify(tool.inputSchema) ?? 'null'),
}))

const additionalToolDefinitionsWithCache = async () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/agent-runtime/src/util/__tests__/token-counter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@ import {
countTokens,
countTokensJson,
countTokensMessages,
safeJsonStringify,
} from '../token-counter'

import type { Message } from '@codebuff/common/types/messages/codebuff-message'

describe('safeJsonStringify', () => {
test('handles circular references and drops function properties', () => {
const schema: { name: string; self?: unknown; transform?: () => void } = {
name: 'tool input',
transform: () => {},
}
schema.self = schema

expect(safeJsonStringify(schema)).toBe(
'{"name":"tool input","self":"[Circular]"}',
)
expect(() => countTokensJson(schema)).not.toThrow()
expect(countTokensJson(schema)).toBeGreaterThan(0)
})

test('preserves JSON string serialization when counting strings', () => {
expect(countTokensJson('tool input')).toBe(
countTokens(JSON.stringify('tool input')),
)
})
})

describe('countTokensMessages', () => {
test('counts text content plus per-message overhead', () => {
const messages = [
Expand Down
20 changes: 19 additions & 1 deletion packages/agent-runtime/src/util/token-counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,27 @@ export function countTokens(text: string): number {
}
}

/**
* Serialize arbitrary values for token counting and persisted tool metadata.
* Circular references are represented explicitly; functions follow
* JSON.stringify semantics and are omitted from object properties.
*/
export function safeJsonStringify(value: unknown): string | undefined {
const seen = new WeakSet<object>()
return JSON.stringify(value, (_key, nestedValue) => {
if (typeof nestedValue === 'function') return undefined
if (typeof nestedValue === 'object' && nestedValue !== null) {
if (seen.has(nestedValue)) return '[Circular]'
seen.add(nestedValue)
}
return nestedValue
})
}

export function countTokensJson(value: unknown): number {
// JSON.stringify(undefined) returns undefined; fall back to '' so countTokens
// always gets a string.
return countTokens(JSON.stringify(value) ?? '')
return countTokens(safeJsonStringify(value) ?? '')
}

/**
Expand Down Expand Up @@ -95,6 +112,7 @@ export function countTokensMessages(messages: Message[]): number {
return total
}


export function countTokensForFiles(
files: Record<string, string | null>,
): Record<string, number> {
Expand Down
Loading