@@ -37,34 +37,43 @@ public interface Part {
37
37
38
38
/* * Represents text or string based data sent to and received from requests. */
39
39
public class TextPart
40
- internal constructor (public val text: String , public override val isThought: Boolean = false ) :
41
- Part {
40
+ internal constructor (
41
+ public val text: String ,
42
+ public override val isThought: Boolean ,
43
+ internal val thoughtSignature: String?
44
+ ) : Part {
42
45
43
- public constructor (text: String ) : this (text, false )
46
+ public constructor (text: String ) : this (text, false , null )
44
47
45
48
@Serializable
46
- internal data class Internal (val text : String , val thought : Boolean? = null ) : InternalPart
49
+ internal data class Internal (
50
+ val text : String ,
51
+ val thought : Boolean? = null ,
52
+ val thoughtSignature : String? = null
53
+ ) : InternalPart
47
54
}
48
55
49
56
public class CodeExecutionResultPart
50
57
internal constructor (
51
58
public val outcome: String ,
52
59
public val output: String ,
53
- public override val isThought: Boolean = false
60
+ public override val isThought: Boolean ,
61
+ internal val thoughtSignature: String?
54
62
) : Part {
55
63
56
- public constructor (outcome: String , output: String ) : this (outcome, output, false )
64
+ public constructor (outcome: String , output: String ) : this (outcome, output, false , null )
57
65
58
66
@Serializable
59
67
internal data class Internal (
60
- @SerialName(" codeExecutionResult" ) val codeExecutionResult : CodeExecutionResult
68
+ @SerialName(" codeExecutionResult" ) val codeExecutionResult : CodeExecutionResult ,
69
+ val thought : Boolean? = null ,
70
+ val thoughtSignature : String? = null
61
71
) : InternalPart {
62
72
63
73
@Serializable
64
74
internal data class CodeExecutionResult (
65
75
@SerialName(" outcome" ) val outcome : String ,
66
- val output : String ,
67
- val thought : Boolean? = null
76
+ val output : String
68
77
)
69
78
}
70
79
}
@@ -73,20 +82,23 @@ public class ExecutableCodePart
73
82
internal constructor (
74
83
public val language: String ,
75
84
public val code: String ,
76
- public override val isThought: Boolean = false
85
+ public override val isThought: Boolean ,
86
+ internal val thoughtSignature: String?
77
87
) : Part {
78
88
79
- public constructor (language: String , code: String ) : this (language, code, false )
89
+ public constructor (language: String , code: String ) : this (language, code, false , null )
80
90
81
91
@Serializable
82
- internal data class Internal (@SerialName(" executableCode" ) val executableCode : ExecutableCode ) :
83
- InternalPart {
92
+ internal data class Internal (
93
+ @SerialName(" executableCode" ) val executableCode : ExecutableCode ,
94
+ val thought : Boolean? = null ,
95
+ val thoughtSignature : String? = null
96
+ ) : InternalPart {
84
97
85
98
@Serializable
86
99
internal data class ExecutableCode (
87
100
@SerialName(" language" ) val language : String ,
88
- val code : String ,
89
- val thought : Boolean? = null
101
+ val code : String
90
102
)
91
103
}
92
104
}
@@ -98,16 +110,20 @@ internal constructor(
98
110
* @param image [Bitmap] to convert into a [Part]
99
111
*/
100
112
public class ImagePart
101
- internal constructor (public val image: Bitmap , public override val isThought: Boolean = false ) :
102
- Part {
113
+ internal constructor (
114
+ public val image: Bitmap ,
115
+ public override val isThought: Boolean ,
116
+ internal val thoughtSignature: String?
117
+ ) : Part {
103
118
104
- public constructor (image: Bitmap ) : this (image, false )
119
+ public constructor (image: Bitmap ) : this (image, false , null )
105
120
106
121
internal fun toInlineDataPart () =
107
122
InlineDataPart (
108
123
android.util.Base64 .decode(encodeBitmapToBase64Jpeg(image), BASE_64_FLAGS ),
109
124
" image/jpeg" ,
110
- isThought
125
+ isThought,
126
+ thoughtSignature
111
127
)
112
128
}
113
129
@@ -122,21 +138,24 @@ public class InlineDataPart
122
138
internal constructor (
123
139
public val inlineData: ByteArray ,
124
140
public val mimeType: String ,
125
- public override val isThought: Boolean = false
141
+ public override val isThought: Boolean ,
142
+ internal val thoughtSignature: String?
126
143
) : Part {
127
144
128
- public constructor (inlineData: ByteArray , mimeType: String ) : this (inlineData, mimeType, false )
145
+ public constructor (
146
+ inlineData: ByteArray ,
147
+ mimeType: String
148
+ ) : this (inlineData, mimeType, false , null )
129
149
130
150
@Serializable
131
- internal data class Internal (@SerialName(" inlineData" ) val inlineData : InlineData ) :
132
- InternalPart {
151
+ internal data class Internal (
152
+ @SerialName(" inlineData" ) val inlineData : InlineData ,
153
+ val thought : Boolean? = null ,
154
+ val thoughtSignature : String? = null
155
+ ) : InternalPart {
133
156
134
157
@Serializable
135
- internal data class InlineData (
136
- @SerialName(" mimeType" ) val mimeType : String ,
137
- val data : Base64 ,
138
- val thought : Boolean? = null
139
- )
158
+ internal data class InlineData (@SerialName(" mimeType" ) val mimeType : String , val data : Base64 )
140
159
}
141
160
}
142
161
@@ -153,25 +172,29 @@ internal constructor(
153
172
public val name: String ,
154
173
public val args: Map <String , JsonElement >,
155
174
public val id: String? = null ,
156
- public override val isThought: Boolean = false
175
+ public override val isThought: Boolean ,
176
+ internal val thoughtSignature: String?
157
177
) : Part {
158
178
159
179
@JvmOverloads
160
180
public constructor (
161
181
name: String ,
162
182
args: Map <String , JsonElement >,
163
- id: String? = null
164
- ) : this (name, args, id, false )
183
+ id: String? = null ,
184
+ ) : this (name, args, id, false , null )
165
185
166
186
@Serializable
167
- internal data class Internal (val functionCall : FunctionCall ) : InternalPart {
187
+ internal data class Internal (
188
+ val functionCall : FunctionCall ,
189
+ val thought : Boolean? = null ,
190
+ val thoughtSignature : String? = null
191
+ ) : InternalPart {
168
192
169
193
@Serializable
170
194
internal data class FunctionCall (
171
195
val name : String ,
172
196
val args : Map <String , JsonElement ?>? = null ,
173
- val id : String? = null ,
174
- val thought : Boolean? = null
197
+ val id : String? = null
175
198
)
176
199
}
177
200
}
@@ -188,30 +211,34 @@ internal constructor(
188
211
public val name: String ,
189
212
public val response: JsonObject ,
190
213
public val id: String? = null ,
191
- public override val isThought: Boolean = false
214
+ public override val isThought: Boolean ,
215
+ internal val thoughtSignature: String?
192
216
) : Part {
193
217
194
218
@JvmOverloads
195
219
public constructor (
196
220
name: String ,
197
221
response: JsonObject ,
198
222
id: String? = null
199
- ) : this (name, response, id, false )
223
+ ) : this (name, response, id, false , null )
200
224
201
225
@Serializable
202
- internal data class Internal (val functionResponse : FunctionResponse ) : InternalPart {
226
+ internal data class Internal (
227
+ val functionResponse : FunctionResponse ,
228
+ val thought : Boolean? = null ,
229
+ val thoughtSignature : String? = null
230
+ ) : InternalPart {
203
231
204
232
@Serializable
205
233
internal data class FunctionResponse (
206
234
val name : String ,
207
235
val response : JsonObject ,
208
- val id : String? = null ,
209
- val thought : Boolean? = null
236
+ val id : String? = null
210
237
)
211
238
}
212
239
213
240
internal fun toInternalFunctionCall (): Internal .FunctionResponse {
214
- return Internal .FunctionResponse (name, response, id, isThought )
241
+ return Internal .FunctionResponse (name, response, id)
215
242
}
216
243
}
217
244
@@ -227,19 +254,23 @@ public class FileDataPart
227
254
internal constructor (
228
255
public val uri: String ,
229
256
public val mimeType: String ,
230
- public override val isThought: Boolean = false
257
+ public override val isThought: Boolean ,
258
+ internal val thoughtSignature: String?
231
259
) : Part {
232
260
233
- public constructor (uri: String , mimeType: String ) : this (uri, mimeType, false )
261
+ public constructor (uri: String , mimeType: String ) : this (uri, mimeType, false , null )
234
262
235
263
@Serializable
236
- internal data class Internal (@SerialName(" file_data" ) val fileData : FileData ) : InternalPart {
264
+ internal data class Internal (
265
+ @SerialName(" file_data" ) val fileData : FileData ,
266
+ val thought : Boolean? = null ,
267
+ val thoughtSignature : String? = null
268
+ ) : InternalPart {
237
269
238
270
@Serializable
239
271
internal data class FileData (
240
272
@SerialName(" mime_type" ) val mimeType : String ,
241
- @SerialName(" file_uri" ) val fileUri : String ,
242
- val thought : Boolean? = null
273
+ @SerialName(" file_uri" ) val fileUri : String
243
274
)
244
275
}
245
276
}
@@ -281,36 +312,51 @@ internal object PartSerializer :
281
312
282
313
internal fun Part.toInternal (): InternalPart {
283
314
return when (this ) {
284
- is TextPart -> TextPart .Internal (text, isThought)
315
+ is TextPart -> TextPart .Internal (text, isThought, thoughtSignature )
285
316
is ImagePart ->
286
317
InlineDataPart .Internal (
287
- InlineDataPart .Internal .InlineData (" image/jpeg" , encodeBitmapToBase64Jpeg(image), isThought)
318
+ InlineDataPart .Internal .InlineData (" image/jpeg" , encodeBitmapToBase64Jpeg(image)),
319
+ isThought,
320
+ thoughtSignature
288
321
)
289
322
is InlineDataPart ->
290
323
InlineDataPart .Internal (
291
324
InlineDataPart .Internal .InlineData (
292
325
mimeType,
293
- android.util.Base64 .encodeToString(inlineData, BASE_64_FLAGS ),
294
- isThought
295
- )
326
+ android.util.Base64 .encodeToString(inlineData, BASE_64_FLAGS )
327
+ ),
328
+ isThought,
329
+ thoughtSignature
296
330
)
297
331
is FunctionCallPart ->
298
- FunctionCallPart .Internal (FunctionCallPart .Internal .FunctionCall (name, args, id, isThought))
332
+ FunctionCallPart .Internal (
333
+ FunctionCallPart .Internal .FunctionCall (name, args, id),
334
+ isThought,
335
+ thoughtSignature
336
+ )
299
337
is FunctionResponsePart ->
300
338
FunctionResponsePart .Internal (
301
- FunctionResponsePart .Internal .FunctionResponse (name, response, id, isThought)
339
+ FunctionResponsePart .Internal .FunctionResponse (name, response, id),
340
+ isThought,
341
+ thoughtSignature
302
342
)
303
343
is FileDataPart ->
304
344
FileDataPart .Internal (
305
- FileDataPart .Internal .FileData (mimeType = mimeType, fileUri = uri, thought = isThought)
345
+ FileDataPart .Internal .FileData (mimeType = mimeType, fileUri = uri),
346
+ isThought,
347
+ thoughtSignature
306
348
)
307
349
is ExecutableCodePart ->
308
350
ExecutableCodePart .Internal (
309
- ExecutableCodePart .Internal .ExecutableCode (language, code, isThought)
351
+ ExecutableCodePart .Internal .ExecutableCode (language, code),
352
+ isThought,
353
+ thoughtSignature
310
354
)
311
355
is CodeExecutionResultPart ->
312
356
CodeExecutionResultPart .Internal (
313
- CodeExecutionResultPart .Internal .CodeExecutionResult (outcome, output, isThought)
357
+ CodeExecutionResultPart .Internal .CodeExecutionResult (outcome, output),
358
+ isThought,
359
+ thoughtSignature
314
360
)
315
361
else ->
316
362
throw com.google.firebase.ai.type.SerializationException (
@@ -328,42 +374,46 @@ private fun encodeBitmapToBase64Jpeg(input: Bitmap): String {
328
374
329
375
internal fun InternalPart.toPublic (): Part {
330
376
return when (this ) {
331
- is TextPart .Internal -> TextPart (text, thought ? : false )
377
+ is TextPart .Internal -> TextPart (text, thought ? : false , thoughtSignature )
332
378
is InlineDataPart .Internal -> {
333
379
val data = android.util.Base64 .decode(inlineData.data, BASE_64_FLAGS )
334
380
if (inlineData.mimeType.contains(" image" )) {
335
- ImagePart (decodeBitmapFromImage(data), inlineData. thought ? : false )
381
+ ImagePart (decodeBitmapFromImage(data), thought ? : false , thoughtSignature )
336
382
} else {
337
- InlineDataPart (data, inlineData.mimeType, inlineData. thought ? : false )
383
+ InlineDataPart (data, inlineData.mimeType, thought ? : false , thoughtSignature )
338
384
}
339
385
}
340
386
is FunctionCallPart .Internal ->
341
387
FunctionCallPart (
342
388
functionCall.name,
343
389
functionCall.args.orEmpty().mapValues { it.value ? : JsonNull },
344
390
functionCall.id,
345
- functionCall.thought ? : false
391
+ thought ? : false ,
392
+ thoughtSignature
346
393
)
347
394
is FunctionResponsePart .Internal ->
348
395
FunctionResponsePart (
349
396
functionResponse.name,
350
397
functionResponse.response,
351
398
functionResponse.id,
352
- functionResponse.thought ? : false
399
+ thought ? : false ,
400
+ thoughtSignature
353
401
)
354
402
is FileDataPart .Internal ->
355
- FileDataPart (fileData.mimeType, fileData.fileUri, fileData. thought ? : false )
403
+ FileDataPart (fileData.mimeType, fileData.fileUri, thought ? : false , thoughtSignature )
356
404
is ExecutableCodePart .Internal ->
357
405
ExecutableCodePart (
358
406
executableCode.language,
359
407
executableCode.code,
360
- executableCode.thought ? : false
408
+ thought ? : false ,
409
+ thoughtSignature
361
410
)
362
411
is CodeExecutionResultPart .Internal ->
363
412
CodeExecutionResultPart (
364
413
codeExecutionResult.outcome,
365
414
codeExecutionResult.output,
366
- codeExecutionResult.thought ? : false
415
+ thought ? : false ,
416
+ thoughtSignature
367
417
)
368
418
else ->
369
419
throw com.google.firebase.ai.type.SerializationException (
0 commit comments