-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy path_post.ts
More file actions
491 lines (434 loc) · 13.7 KB
/
_post.ts
File metadata and controls
491 lines (434 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
import {
isClaudeModel,
toAnthropicModelId,
} from '@codebuff/common/constants/claude-oauth'
import { isOpenAIProviderModel } from '@codebuff/common/constants/chatgpt-oauth'
import { getErrorObject } from '@codebuff/common/util/error'
import { env } from '@codebuff/internal/env'
import { NextResponse } from 'next/server'
import { z } from 'zod/v4'
import { parseJsonBody, requireUserFromApiKey } from '../_helpers'
import type { TrackEventFn } from '@codebuff/common/types/contracts/analytics'
import type { GetUserInfoFromApiKeyFn } from '@codebuff/common/types/contracts/database'
import type {
Logger,
LoggerWithContextFn,
} from '@codebuff/common/types/contracts/logger'
import type { NextRequest } from 'next/server'
const tokenCountRequestSchema = z.object({
messages: z.array(z.any()),
system: z.string().optional(),
model: z.string().optional(),
tools: z.array(z.object({
name: z.string(),
description: z.string().optional(),
input_schema: z.any().optional(),
})).optional(),
})
type TokenCountRequest = z.infer<typeof tokenCountRequestSchema>
const DEFAULT_ANTHROPIC_MODEL = 'claude-opus-4-6'
export async function postTokenCount(params: {
req: NextRequest
getUserInfoFromApiKey: GetUserInfoFromApiKeyFn
logger: Logger
loggerWithContext: LoggerWithContextFn
trackEvent: TrackEventFn
fetch: typeof globalThis.fetch
}) {
const {
req,
getUserInfoFromApiKey,
logger: baseLogger,
loggerWithContext,
trackEvent,
fetch,
} = params
// Authenticate user
const userResult = await requireUserFromApiKey({
req,
getUserInfoFromApiKey,
logger: baseLogger,
loggerWithContext,
trackEvent,
authErrorEvent: AnalyticsEvent.TOKEN_COUNT_AUTH_ERROR,
})
if (!userResult.ok) {
return userResult.response
}
const { userId, logger } = userResult.data
// Parse request body
const bodyResult = await parseJsonBody({
req,
schema: tokenCountRequestSchema,
logger,
trackEvent,
validationErrorEvent: AnalyticsEvent.TOKEN_COUNT_VALIDATION_ERROR,
})
if (!bodyResult.ok) {
return bodyResult.response
}
const { messages, system, model, tools } = bodyResult.data
try {
const useOpenAI = model != null && false // isOpenAIProviderModel(model)
const inputTokens = useOpenAI
? await countTokensViaOpenAI({ messages, system, model, fetch, logger })
: await countTokensViaAnthropic({
messages,
system,
model,
tools,
fetch,
logger,
})
logger.info({
userId,
messageCount: messages.length,
hasSystem: !!system,
hasTools: !!tools,
toolCount: tools?.length,
model: model ?? DEFAULT_ANTHROPIC_MODEL,
tokenCount: inputTokens,
provider: useOpenAI ? 'openai' : 'anthropic',
},
`Token count: ${inputTokens}`
)
return NextResponse.json({ inputTokens })
} catch (error) {
logger.error(
{ error: getErrorObject(error), userId },
'Failed to count tokens',
)
return NextResponse.json(
{ error: 'Failed to count tokens' },
{ status: 500 },
)
}
}
// Buffer to add to token count for non-Anthropic models since tokenizers differ
const NON_ANTHROPIC_TOKEN_BUFFER = 0.3
export async function countTokensViaOpenAI(params: {
messages: TokenCountRequest['messages']
system: string | undefined
model: string
fetch: typeof globalThis.fetch
logger: Logger
}): Promise<number> {
const { messages, system, model, fetch, logger } = params
const openaiModelId = model.startsWith('openai/')
? model.slice('openai/'.length)
: model
const input = convertToResponsesApiInput(messages)
const response = await fetch(
'https://api.openai.com/v1/responses/input_tokens',
{
method: 'POST',
headers: {
Authorization: `Bearer ${env.OPENAI_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: openaiModelId,
input,
...(system && { instructions: system }),
}),
},
)
if (!response.ok) {
const errorText = await response.text()
logger.error(
{ status: response.status, errorText, model },
'OpenAI token count API error',
)
throw new Error(`OpenAI API error: ${response.status} - ${errorText}`)
}
const data = await response.json()
return data.input_tokens
}
export type ResponsesApiContentPart =
| { type: 'input_text'; text: string }
| { type: 'input_image'; image_url: string }
export type ResponsesApiInputItem =
| { type: 'message'; role: 'user' | 'assistant' | 'developer'; content: string | ResponsesApiContentPart[] }
| { type: 'function_call'; id: string; name: string; arguments: string }
| { type: 'function_call_output'; call_id: string; output: string }
export function convertToResponsesApiInput(
messages: TokenCountRequest['messages'],
): ResponsesApiInputItem[] {
const input: ResponsesApiInputItem[] = []
for (const message of messages) {
if (message.role === 'system') {
const content = buildMessageContent(message.content)
if (content) {
input.push({ type: 'message', role: 'developer', content })
}
continue
}
if (message.role === 'tool') {
input.push({
type: 'function_call_output',
call_id: message.toolCallId ?? 'unknown',
output: formatToolContent(message.content),
})
continue
}
if (message.role === 'user') {
const content = buildMessageContent(message.content)
if (content) {
input.push({ type: 'message', role: 'user', content })
}
continue
}
if (message.role === 'assistant') {
const content = buildMessageContent(message.content)
if (content) {
input.push({ type: 'message', role: 'assistant', content })
}
if (Array.isArray(message.content)) {
for (const part of message.content) {
if (part.type === 'tool-call') {
input.push({
type: 'function_call',
id: part.toolCallId ?? 'unknown',
name: part.toolName,
arguments: JSON.stringify(part.input ?? {}),
})
}
}
}
}
}
return input
}
function buildMessageContent(
content: unknown,
): string | ResponsesApiContentPart[] | null {
if (typeof content === 'string') return content || null
if (!Array.isArray(content)) {
const text = JSON.stringify(content)
return text || null
}
const hasImages = content.some(
(part) => part.type === 'image' && typeof part.image === 'string' && part.image,
)
if (!hasImages) {
const text = extractTextParts(content)
return text || null
}
const parts: ResponsesApiContentPart[] = []
for (const part of content) {
if (part.type === 'text' && typeof part.text === 'string' && part.text) {
parts.push({ type: 'input_text', text: part.text })
} else if (part.type === 'json') {
const text = typeof part.value === 'string' ? part.value : JSON.stringify(part.value)
if (text) {
parts.push({ type: 'input_text', text })
}
} else if (part.type === 'image') {
const imageUrl = toImageUrl(part.image, part.mediaType)
if (imageUrl) {
parts.push({ type: 'input_image', image_url: imageUrl })
}
}
}
return parts.length > 0 ? parts : null
}
function toImageUrl(image: unknown, mediaType?: string): string | null {
if (typeof image !== 'string' || !image) return null
if (image.startsWith('http://') || image.startsWith('https://') || image.startsWith('data:')) {
return image
}
return `data:${mediaType ?? 'image/png'};base64,${image}`
}
function extractTextParts(content: Array<Record<string, unknown>>): string {
const parts: string[] = []
for (const part of content) {
if (part.type === 'text' && typeof part.text === 'string') {
parts.push(part.text)
} else if (part.type === 'json') {
parts.push(typeof part.value === 'string' ? part.value : JSON.stringify(part.value))
}
}
return parts.join('\n')
}
async function countTokensViaAnthropic(params: {
messages: TokenCountRequest['messages']
system: string | undefined
model: string | undefined
tools: TokenCountRequest['tools']
fetch: typeof globalThis.fetch
logger: Logger
}): Promise<number> {
const { messages, system, model, tools, fetch, logger } = params
// Convert messages to Anthropic format
const anthropicMessages = convertToAnthropicMessages(messages)
// Convert model from OpenRouter format (e.g. "anthropic/claude-opus-4.5") to Anthropic format (e.g. "claude-opus-4-5-20251101")
// For non-Anthropic models, use the default Anthropic model for token counting
const isNonAnthropicModel = !model || !isClaudeModel(model)
const anthropicModelId = isNonAnthropicModel
? DEFAULT_ANTHROPIC_MODEL
: toAnthropicModelId(model)
// Use the count_tokens endpoint (beta) or make a minimal request
const response = await fetch(
'https://api.anthropic.com/v1/messages/count_tokens',
{
method: 'POST',
headers: {
'x-api-key': env.ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01',
'anthropic-beta': 'token-counting-2024-11-01',
'content-type': 'application/json',
},
body: JSON.stringify({
model: anthropicModelId,
messages: anthropicMessages,
...(system && { system }),
...(tools && { tools }),
}),
},
)
if (!response.ok) {
const errorText = await response.text()
logger.error(
{
status: response.status,
errorText,
messages: anthropicMessages,
system,
model,
},
'Anthropic token count API error',
)
throw new Error(`Anthropic API error: ${response.status} - ${errorText}`)
}
const data = await response.json()
const baseTokens = data.input_tokens
// Add 30% buffer for OpenAI and Gemini models since their tokenizers differ from Anthropic's
// Other non-Anthropic models (x-ai, qwen, deepseek, etc.) are routed through providers that
// use similar tokenization, so the buffer is not needed and was causing premature context pruning.
const isOpenAIModel = model ? isOpenAIProviderModel(model) : false
const isGeminiModel = model?.startsWith('google/') ?? false
if (isOpenAIModel || isGeminiModel) {
return Math.ceil(baseTokens * (1 + NON_ANTHROPIC_TOKEN_BUFFER))
}
return baseTokens
}
export function convertToAnthropicMessages(
messages: TokenCountRequest['messages'],
): Array<{ role: 'user' | 'assistant'; content: any }> {
const result: Array<{ role: 'user' | 'assistant'; content: any }> = []
for (const message of messages) {
// Skip system messages - they're handled separately
if (message.role === 'system') {
continue
}
// Handle tool messages by converting to user messages with tool_result
if (message.role === 'tool') {
result.push({
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: message.toolCallId ?? 'unknown',
content: formatToolContent(message.content),
},
],
})
continue
}
// Handle user and assistant messages
if (message.role === 'user' || message.role === 'assistant') {
const content = convertContentToAnthropic(message.content, message.role)
if (content) {
result.push({
role: message.role,
content,
})
}
}
}
return result
}
export function convertContentToAnthropic(
content: any,
role: 'user' | 'assistant',
): any {
if (typeof content === 'string') {
return content
}
if (!Array.isArray(content)) {
return JSON.stringify(content)
}
const anthropicContent: any[] = []
for (const part of content) {
if (part.type === 'text') {
const text = part.text.trim()
if (text) {
anthropicContent.push({ type: 'text', text })
}
} else if (part.type === 'tool-call' && role === 'assistant') {
anthropicContent.push({
type: 'tool_use',
id: part.toolCallId ?? 'unknown',
name: part.toolName,
input: part.input ?? {},
})
} else if (part.type === 'image') {
// Handle image content - the image field can be base64 data or a URL string
const imageData = part.image
if (typeof imageData === 'string' && imageData) {
if (
imageData.startsWith('http://') ||
imageData.startsWith('https://')
) {
// URL-based image
anthropicContent.push({
type: 'image',
source: {
type: 'url',
url: imageData,
},
})
} else {
// Base64 encoded image data
anthropicContent.push({
type: 'image',
source: {
type: 'base64',
media_type: part.mediaType ?? 'image/png',
data: imageData,
},
})
}
}
// Skip images without valid data
} else if (part.type === 'json') {
const text =
typeof part.value === 'string'
? part.value.trim()
: JSON.stringify(part.value).trim()
if (text) {
anthropicContent.push({
type: 'text',
text,
})
}
}
}
return anthropicContent.length > 0 ? anthropicContent : undefined
}
export function formatToolContent(content: any): string {
if (typeof content === 'string') {
return content
}
if (Array.isArray(content)) {
return content
.map((part) => {
if (part.type === 'text') return part.text
if (part.type === 'json') return JSON.stringify(part.value)
return JSON.stringify(part)
})
.join('\n')
}
return JSON.stringify(content)
}