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

Function convertMessages

src/services/api/openAICompatInferenceClient.ts:619–682  ·  view source on GitHub ↗
(
  system: unknown,
  messages: unknown,
)

Source from the content-addressed store, hash-verified

617}
618
619function convertMessages(
620 system: unknown,
621 messages: unknown,
622): Array<Record<string, unknown>> {
623 const converted: Array<Record<string, unknown>> = []
624
625 if (typeof system === 'string' && system.length > 0) {
626 converted.push({ role: 'system', content: system })
627 } else if (Array.isArray(system) && system.length > 0) {
628 const text = system
629 .map(block => {
630 if (
631 block &&
632 typeof block === 'object' &&
633 'type' in block &&
634 block.type === 'text' &&
635 'text' in block &&
636 typeof block.text === 'string'
637 ) {
638 return block.text
639 }
640 throw new Error(
641 'Unsupported system block type for OpenAI compat client',
642 )
643 })
644 .join('')
645 if (text.length > 0) {
646 converted.push({ role: 'system', content: text })
647 }
648 }
649
650 if (!Array.isArray(messages)) {
651 throw new Error('Inference request messages must be an array')
652 }
653
654 for (const message of messages) {
655 if (
656 !message ||
657 typeof message !== 'object' ||
658 !('role' in message) ||
659 typeof message.role !== 'string'
660 ) {
661 throw new Error('Unsupported message shape for OpenAI compat client')
662 }
663
664 if (message.role === 'user') {
665 converted.push(...convertUserContent('content' in message ? message.content : ''))
666 continue
667 }
668
669 if (message.role === 'assistant') {
670 converted.push(
671 ...convertAssistantContent('content' in message ? message.content : ''),
672 )
673 continue
674 }
675
676 throw new Error(

Callers 1

Calls 2

convertUserContentFunction · 0.85
convertAssistantContentFunction · 0.85

Tested by

no test coverage detected