( toolName: string, callId: string, llmContent: PartListUnion, )
| 148 | } |
| 149 | |
| 150 | export function convertToFunctionResponse( |
| 151 | toolName: string, |
| 152 | callId: string, |
| 153 | llmContent: PartListUnion, |
| 154 | ): PartListUnion { |
| 155 | const contentToProcess = |
| 156 | Array.isArray(llmContent) && llmContent.length === 1 |
| 157 | ? llmContent[0] |
| 158 | : llmContent; |
| 159 | |
| 160 | if (typeof contentToProcess === 'string') { |
| 161 | return createFunctionResponsePart(callId, toolName, contentToProcess); |
| 162 | } |
| 163 | |
| 164 | if (Array.isArray(contentToProcess)) { |
| 165 | const functionResponse = createFunctionResponsePart( |
| 166 | callId, |
| 167 | toolName, |
| 168 | 'Tool execution succeeded.', |
| 169 | ); |
| 170 | return [functionResponse, ...contentToProcess]; |
| 171 | } |
| 172 | |
| 173 | // After this point, contentToProcess is a single Part object. |
| 174 | if (contentToProcess.functionResponse) { |
| 175 | if (contentToProcess.functionResponse.response?.['content']) { |
| 176 | const stringifiedOutput = |
| 177 | getResponseTextFromParts( |
| 178 | contentToProcess.functionResponse.response['content'] as Part[], |
| 179 | ) || ''; |
| 180 | return createFunctionResponsePart(callId, toolName, stringifiedOutput); |
| 181 | } |
| 182 | // It's a functionResponse that we should pass through as is. |
| 183 | return contentToProcess; |
| 184 | } |
| 185 | |
| 186 | if (contentToProcess.inlineData || contentToProcess.fileData) { |
| 187 | const mimeType = |
| 188 | contentToProcess.inlineData?.mimeType || |
| 189 | contentToProcess.fileData?.mimeType || |
| 190 | 'unknown'; |
| 191 | const functionResponse = createFunctionResponsePart( |
| 192 | callId, |
| 193 | toolName, |
| 194 | `Binary content of type ${mimeType} was processed.`, |
| 195 | ); |
| 196 | return [functionResponse, contentToProcess]; |
| 197 | } |
| 198 | |
| 199 | if (contentToProcess.text !== undefined) { |
| 200 | return createFunctionResponsePart(callId, toolName, contentToProcess.text); |
| 201 | } |
| 202 | |
| 203 | // Default case for other kinds of parts. |
| 204 | return createFunctionResponsePart( |
| 205 | callId, |
| 206 | toolName, |
| 207 | 'Tool execution succeeded.', |
no test coverage detected