MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / convertContentToAnthropic

Function convertContentToAnthropic

web/src/app/api/v1/token-count/_post.ts:404–475  ·  view source on GitHub ↗
(
  content: any,
  role: 'user' | 'assistant',
)

Source from the content-addressed store, hash-verified

402}
403
404export function convertContentToAnthropic(
405 content: any,
406 role: 'user' | 'assistant',
407): any {
408 if (typeof content === 'string') {
409 return content
410 }
411
412 if (!Array.isArray(content)) {
413 return JSON.stringify(content)
414 }
415
416 const anthropicContent: any[] = []
417
418 for (const part of content) {
419 if (part.type === 'text') {
420 const text = part.text.trim()
421 if (text) {
422 anthropicContent.push({ type: 'text', text })
423 }
424 } else if (part.type === 'tool-call' && role === 'assistant') {
425 anthropicContent.push({
426 type: 'tool_use',
427 id: part.toolCallId ?? 'unknown',
428 name: part.toolName,
429 input: part.input ?? {},
430 })
431 } else if (part.type === 'image') {
432 // Handle image content - the image field can be base64 data or a URL string
433 const imageData = part.image
434 if (typeof imageData === 'string' && imageData) {
435 if (
436 imageData.startsWith('http://') ||
437 imageData.startsWith('https://')
438 ) {
439 // URL-based image
440 anthropicContent.push({
441 type: 'image',
442 source: {
443 type: 'url',
444 url: imageData,
445 },
446 })
447 } else {
448 // Base64 encoded image data
449 anthropicContent.push({
450 type: 'image',
451 source: {
452 type: 'base64',
453 media_type: part.mediaType ?? 'image/png',
454 data: imageData,
455 },
456 })
457 }
458 }
459 // Skip images without valid data
460 } else if (part.type === 'json') {
461 const text =

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected