MCPcopy Index your code
hub / github.com/simstudioai/sim / convertToGeminiFormat

Function convertToGeminiFormat

apps/sim/providers/google/utils.ts:171–280  ·  view source on GitHub ↗
(
  request: ProviderRequest,
  providerId = 'google'
)

Source from the content-addressed store, hash-verified

169 * Converts OpenAI-style request format to Gemini format
170 */
171export function convertToGeminiFormat(
172 request: ProviderRequest,
173 providerId = 'google'
174): {
175 contents: Content[]
176 tools: GeminiToolDef[] | undefined
177 systemInstruction: Content | undefined
178} {
179 const contents: Content[] = []
180 let systemInstruction: Content | undefined
181
182 if (request.systemPrompt) {
183 systemInstruction = { parts: [{ text: request.systemPrompt }] }
184 }
185
186 if (request.context) {
187 contents.push({ role: 'user', parts: [{ text: request.context }] })
188 }
189
190 if (request.messages?.length) {
191 for (const message of request.messages) {
192 if (message.role === 'system') {
193 if (!systemInstruction) {
194 systemInstruction = { parts: [{ text: message.content ?? '' }] }
195 } else if (systemInstruction.parts?.[0] && 'text' in systemInstruction.parts[0]) {
196 systemInstruction.parts[0].text = `${systemInstruction.parts[0].text}\n${message.content}`
197 }
198 } else if (message.role === 'user' || message.role === 'assistant') {
199 const geminiRole = message.role === 'user' ? 'user' : 'model'
200 const parts = buildGeminiMessageParts(message.content, message.files, providerId) as Part[]
201
202 if (parts.length > 0) {
203 contents.push({ role: geminiRole, parts })
204 }
205
206 if (message.role === 'assistant' && message.tool_calls?.length) {
207 const functionCalls = message.tool_calls.map((toolCall) => ({
208 functionCall: {
209 name: toolCall.function?.name,
210 args: JSON.parse(toolCall.function?.arguments || '{}') as Record<string, unknown>,
211 },
212 }))
213 contents.push({ role: 'model', parts: functionCalls })
214 }
215 } else if (message.role === 'tool') {
216 if (!message.name) {
217 logger.warn('Tool message missing function name, skipping')
218 continue
219 }
220 let responseData: Record<string, unknown>
221 try {
222 const parsed = JSON.parse(message.content ?? '{}')
223 responseData = ensureStructResponse(parsed)
224 } catch {
225 responseData = { output: message.content }
226 }
227 contents.push({
228 role: 'user',

Callers 2

executeGeminiRequestFunction · 0.90
utils.test.tsFile · 0.90

Calls 6

buildGeminiMessagePartsFunction · 0.90
ensureStructResponseFunction · 0.85
cleanSchemaForGeminiFunction · 0.85
parseMethod · 0.80
warnMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected