toGeminiPart function
Part
toGeminiPart(
- Part p
)
Implementation
@visibleForTesting
gcl.Part toGeminiPart(Part p) {
final thoughtSignature = p.metadata?['thoughtSignature'] != null
? p.metadata!['thoughtSignature'] as String
: null;
if (p.isReasoning) {
return gcl.Part(
text: p.reasoning,
thought: true,
thoughtSignature: thoughtSignature,
);
}
if (p.isText) {
return gcl.Part(text: p.text, thoughtSignature: thoughtSignature);
}
if (p.isToolRequest) {
return gcl.Part(
functionCall: gcl.FunctionCall(
id: p.toolRequest!.ref ?? '',
name: _toGeminiToolName(p.toolRequest!.name),
args: p.toolRequest!.input, // already a map
),
thoughtSignature: thoughtSignature,
);
}
if (p.isToolResponse) {
return gcl.Part(
functionResponse: gcl.FunctionResponse(
id: p.toolResponse!.ref ?? '',
name: _toGeminiToolName(p.toolResponse!.name),
response: {'output': p.toolResponse!.output},
),
thoughtSignature: thoughtSignature,
);
}
if (p.isMedia) {
final media = p.media;
if (media!.url.startsWith('data:')) {
final uri = Uri.parse(media.url);
if (uri.data != null) {
return gcl.Part.fromJson({
'inlineData': {
'mimeType': media.contentType ?? uri.data!.mimeType,
'data': base64Encode(uri.data!.contentAsBytes()),
},
'thoughtSignature': ?thoughtSignature,
});
}
}
return gcl.Part.fromJson({
'fileData': {'mimeType': media.contentType ?? '', 'fileUri': media.url},
'thoughtSignature': ?thoughtSignature,
});
}
if (p.isCustom && p.custom!['codeExecutionResult'] != null) {
p as CustomPart;
return gcl.Part(
codeExecutionResult: gcl.CodeExecutionResult(
outcome:
(p.custom['codeExecutionResult'] as Map<String, dynamic>)['outcome']
as String?,
output:
(p.custom['codeExecutionResult'] as Map<String, dynamic>)['output']
as String?,
),
thoughtSignature: thoughtSignature,
);
}
if (p.isCustom && p.custom!['executableCode'] != null) {
p as CustomPart;
return gcl.Part(
executableCode: gcl.ExecutableCode(
language:
(p.custom['executableCode'] as Map<String, dynamic>)['language']
as String?,
code:
(p.custom['executableCode'] as Map<String, dynamic>)['code']
as String?,
),
thoughtSignature: thoughtSignature,
);
}
throw UnimplementedError('Unsupported part type: $p');
}