MCPcopy Create free account
hub / github.com/freecodexyz/free-code / fetchCodexResponse

Function fetchCodexResponse

src/utils/codex-fetch-adapter.ts:88–133  ·  view source on GitHub ↗
(
  messages: Message[],
  model: string,
  options: {
    apiKey?: string
    baseUrl?: string
    stream?: boolean
  } = {}
)

Source from the content-addressed store, hash-verified

86 * Make a request to OpenAI Codex API
87 */
88export async function fetchCodexResponse(
89 messages: Message[],
90 model: string,
91 options: {
92 apiKey?: string
93 baseUrl?: string
94 stream?: boolean
95 } = {}
96): Promise<OpenAIResponse> {
97 const { apiKey, baseUrl = 'https://api.openai.com/v1', stream = false } = options
98
99 if (!apiKey) {
100 throw new Error('OpenAI API key is required for Codex requests')
101 }
102
103 const openAIMessages = messages.map(convertToOpenAIMessage)
104
105 const requestBody = {
106 model,
107 messages: openAIMessages,
108 stream,
109 temperature: 0.7,
110 max_tokens: 4096,
111 }
112
113 try {
114 const response = await fetch(`${baseUrl}/chat/completions`, {
115 method: 'POST',
116 headers: {
117 'Content-Type': 'application/json',
118 'Authorization': `Bearer ${apiKey}`,
119 },
120 body: JSON.stringify(requestBody),
121 })
122
123 if (!response.ok) {
124 throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`)
125 }
126
127 const data = await response.json() as OpenAIResponse
128 return data
129 } catch (error) {
130 logError(error)
131 throw error
132 }
133}
134
135/**
136 * Convert OpenAI response to Claude Code format

Callers

nothing calls this directly

Calls 1

logErrorFunction · 0.70

Tested by

no test coverage detected