MCPcopy Create free account
hub / github.com/Noumena-Network/code / convertUserContent

Function convertUserContent

src/services/api/openAICompatInferenceClient.ts:451–543  ·  view source on GitHub ↗
(content: unknown)

Source from the content-addressed store, hash-verified

449}
450
451function convertUserContent(content: unknown): Array<Record<string, unknown>> {
452 if (typeof content === 'string') {
453 return [{ role: 'user', content }]
454 }
455
456 if (!Array.isArray(content)) {
457 throw new Error('Unsupported user content format for OpenAI compat client')
458 }
459
460 const messages: Array<Record<string, unknown>> = []
461 let textParts: string[] = []
462 let multimodalParts: Array<Record<string, unknown>> = []
463
464 const flushUserContent = () => {
465 if (multimodalParts.length > 0) {
466 if (textParts.length > 0) {
467 multimodalParts.unshift({
468 type: 'text',
469 text: textParts.join(''),
470 })
471 }
472 messages.push({ role: 'user', content: multimodalParts })
473 textParts = []
474 multimodalParts = []
475 return
476 }
477 if (textParts.length > 0) {
478 messages.push({ role: 'user', content: textParts.join('') })
479 textParts = []
480 }
481 }
482
483 for (const block of content) {
484 if (!block || typeof block !== 'object' || !('type' in block)) {
485 throw new Error('Unsupported user content block in OpenAI compat client')
486 }
487
488 switch (block.type) {
489 case 'text':
490 if ('text' in block && typeof block.text === 'string') {
491 if (multimodalParts.length > 0) {
492 multimodalParts.push({ type: 'text', text: block.text })
493 } else {
494 textParts.push(block.text)
495 }
496 }
497 break
498 case 'image':
499 if (
500 'source' in block &&
501 block.source &&
502 typeof block.source === 'object' &&
503 'type' in block.source &&
504 block.source.type === 'base64' &&
505 'media_type' in block.source &&
506 'data' in block.source &&
507 typeof block.source.media_type === 'string' &&
508 typeof block.source.data === 'string'

Callers 1

convertMessagesFunction · 0.85

Calls 3

randomUUIDFunction · 0.90
flushUserContentFunction · 0.85
convertToolResultContentFunction · 0.85

Tested by

no test coverage detected