MCPcopy
hub / github.com/claude-code-best/claude-code / messageParamsToOpenAIRoleContent

Function messageParamsToOpenAIRoleContent

src/utils/sideQuery.ts:131–153  ·  view source on GitHub ↗

* Convert Anthropic MessageParam[] to a list of {role, content} objects * suitable for OpenAI-compatible chat.completions APIs.

(
  messages: MessageParam[],
)

Source from the content-addressed store, hash-verified

129 * suitable for OpenAI-compatible chat.completions APIs.
130 */
131function messageParamsToOpenAIRoleContent(
132 messages: MessageParam[],
133): Array<{ role: 'user' | 'assistant'; content: string }> {
134 const result: Array<{ role: 'user' | 'assistant'; content: string }> = []
135 for (const m of messages) {
136 if (m.role !== 'user' && m.role !== 'assistant') continue
137 const text =
138 typeof m.content === 'string'
139 ? m.content
140 : Array.isArray(m.content)
141 ? m.content
142 .filter(
143 (b): b is { type: 'text'; text: string } => b.type === 'text',
144 )
145 .map(b => b.text)
146 .join('\n')
147 : ''
148 if (text) {
149 result.push({ role: m.role as 'user' | 'assistant', content: text })
150 }
151 }
152 return result
153}
154
155/**
156 * Lightweight API wrapper for "side queries" outside the main conversation loop.

Callers 1

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected