@@ -379,6 +379,153 @@ Dictionary<string, object> Part.ToJson() {
379
379
} ;
380
380
}
381
381
}
382
+
383
+ /// <summary>
384
+ /// A part containing code that was executed by the model.
385
+ /// </summary>
386
+ public readonly struct ExecutableCodePart : Part {
387
+ public enum CodeLanguage {
388
+ Unspecified = 0 ,
389
+ Python
390
+ }
391
+
392
+ /// <summary>
393
+ /// The language
394
+ /// </summary>
395
+ public CodeLanguage Language { get ; }
396
+ /// <summary>
397
+ /// The code that was executed.
398
+ /// </summary>
399
+ public string Code { get ; }
400
+
401
+ private readonly bool ? _isThought ;
402
+ public bool IsThought { get { return _isThought ?? false ; } }
403
+
404
+ private readonly string _thoughtSignature ;
405
+
406
+ private static CodeLanguage ParseLanguage ( string str ) {
407
+ return str switch {
408
+ "PYTHON" => CodeLanguage . Python ,
409
+ _ => CodeLanguage . Unspecified ,
410
+ } ;
411
+ }
412
+
413
+ private string LanguageAsString {
414
+ get {
415
+ return Language switch {
416
+ CodeLanguage . Python => "PYTHON" ,
417
+ _ => "LANGUAGE_UNSPECIFIED"
418
+ } ;
419
+ }
420
+ }
421
+
422
+ /// <summary>
423
+ /// Intended for internal use only.
424
+ /// </summary>
425
+ internal ExecutableCodePart ( string language , string code ,
426
+ bool ? isThought , string thoughtSignature ) {
427
+ Language = ParseLanguage ( language ) ;
428
+ Code = code ;
429
+ _isThought = isThought ;
430
+ _thoughtSignature = thoughtSignature ;
431
+ }
432
+
433
+ Dictionary < string , object > Part . ToJson ( ) {
434
+ var jsonDict = new Dictionary < string , object > ( ) {
435
+ { "executableCode" , new Dictionary < string , object > ( ) {
436
+ { "language" , LanguageAsString } ,
437
+ { "code" , Code }
438
+ }
439
+ }
440
+ } ;
441
+ jsonDict . AddIfHasValue ( "thought" , _isThought ) ;
442
+ jsonDict . AddIfHasValue ( "thoughtSignature" , _thoughtSignature ) ;
443
+ return jsonDict ;
444
+ }
445
+ }
446
+
447
+ /// <summary>
448
+ /// A part containing the result of executing code.
449
+ /// </summary>
450
+ public readonly struct CodeExecutionResultPart : Part {
451
+ /// <summary>
452
+ /// The outcome of a code execution.
453
+ /// </summary>
454
+ public enum ExecutionOutcome {
455
+ Unspecified = 0 ,
456
+ /// <summary>
457
+ /// The code executed without errors.
458
+ /// </summary>
459
+ Ok ,
460
+ /// <summary>
461
+ /// The code failed to execute.
462
+ /// </summary>
463
+ Failed ,
464
+ /// <summary>
465
+ /// The code took too long to execute.
466
+ /// </summary>
467
+ DeadlineExceeded
468
+ }
469
+
470
+ /// <summary>
471
+ /// The outcome of the code execution.
472
+ /// </summary>
473
+ public ExecutionOutcome Outcome { get ; }
474
+ /// <summary>
475
+ /// The output of the code execution.
476
+ /// </summary>
477
+ public string Output { get ; }
478
+
479
+ private readonly bool ? _isThought ;
480
+ public bool IsThought { get { return _isThought ?? false ; } }
481
+
482
+ private readonly string _thoughtSignature ;
483
+
484
+ private static ExecutionOutcome ParseOutcome ( string str ) {
485
+ return str switch {
486
+ "OUTCOME_UNSPECIFIED" => ExecutionOutcome . Unspecified ,
487
+ "OUTCOME_OK" => ExecutionOutcome . Ok ,
488
+ "OUTCOME_FAILED" => ExecutionOutcome . Failed ,
489
+ "OUTCOME_DEADLINE_EXCEEDED" => ExecutionOutcome . DeadlineExceeded ,
490
+ _ => ExecutionOutcome . Unspecified ,
491
+ } ;
492
+ }
493
+
494
+ private string OutcomeAsString {
495
+ get {
496
+ return Outcome switch {
497
+ ExecutionOutcome . Ok => "OUTCOME_OK" ,
498
+ ExecutionOutcome . Failed => "OUTCOME_FAILED" ,
499
+ ExecutionOutcome . DeadlineExceeded => "OUTCOME_DEADLINE_EXCEEDED" ,
500
+ _ => "OUTCOME_UNSPECIFIED"
501
+ } ;
502
+ }
503
+ }
504
+
505
+ /// <summary>
506
+ /// Intended for internal use only.
507
+ /// </summary>
508
+ internal CodeExecutionResultPart ( string outcome , string output ,
509
+ bool ? isThought , string thoughtSignature ) {
510
+ Outcome = ParseOutcome ( outcome ) ;
511
+ Output = output ;
512
+ _isThought = isThought ;
513
+ _thoughtSignature = thoughtSignature ;
514
+ }
515
+
516
+ Dictionary < string , object > Part . ToJson ( ) {
517
+ var jsonDict = new Dictionary < string , object > ( ) {
518
+ { "codeExecutionResult" , new Dictionary < string , object > ( ) {
519
+ { "outcome" , OutcomeAsString } ,
520
+ { "output" , Output }
521
+ }
522
+ }
523
+ } ;
524
+ jsonDict . AddIfHasValue ( "thought" , _isThought ) ;
525
+ jsonDict . AddIfHasValue ( "thoughtSignature" , _thoughtSignature ) ;
526
+ return jsonDict ;
527
+ }
528
+ }
382
529
383
530
#endregion
384
531
@@ -413,6 +560,24 @@ private static InlineDataPart InlineDataPartFromJson(Dictionary<string, object>
413
560
isThought ,
414
561
thoughtSignature ) ;
415
562
}
563
+
564
+ private static ExecutableCodePart ExecutableCodePartFromJson ( Dictionary < string , object > jsonDict ,
565
+ bool ? isThought , string thoughtSignature ) {
566
+ return new ExecutableCodePart (
567
+ jsonDict . ParseValue < string > ( "language" , JsonParseOptions . ThrowEverything ) ,
568
+ jsonDict . ParseValue < string > ( "code" , JsonParseOptions . ThrowEverything ) ,
569
+ isThought ,
570
+ thoughtSignature ) ;
571
+ }
572
+
573
+ private static CodeExecutionResultPart CodeExecutionResultPartFromJson ( Dictionary < string , object > jsonDict ,
574
+ bool ? isThought , string thoughtSignature ) {
575
+ return new CodeExecutionResultPart (
576
+ jsonDict . ParseValue < string > ( "outcome" , JsonParseOptions . ThrowEverything ) ,
577
+ jsonDict . ParseValue < string > ( "output" , JsonParseOptions . ThrowEverything ) ,
578
+ isThought ,
579
+ thoughtSignature ) ;
580
+ }
416
581
417
582
private static Part PartFromJson ( Dictionary < string , object > jsonDict ) {
418
583
bool ? isThought = jsonDict . ParseNullableValue < bool > ( "thought" ) ;
@@ -427,6 +592,14 @@ private static Part PartFromJson(Dictionary<string, object> jsonDict) {
427
592
innerDict => InlineDataPartFromJson ( innerDict , isThought , thoughtSignature ) ,
428
593
out var inlineDataPart ) ) {
429
594
return inlineDataPart ;
595
+ } else if ( jsonDict . TryParseObject ( "executableCode" ,
596
+ innerDict => ExecutableCodePartFromJson ( innerDict , isThought , thoughtSignature ) ,
597
+ out var executableCodePart ) ) {
598
+ return executableCodePart ;
599
+ } else if ( jsonDict . TryParseObject ( "codeExecutionResult" ,
600
+ innerDict => CodeExecutionResultPartFromJson ( innerDict , isThought , thoughtSignature ) ,
601
+ out var codeExecutionResultPart ) ) {
602
+ return codeExecutionResultPart ;
430
603
} else {
431
604
#if FIREBASEAI_DEBUG_LOGGING
432
605
UnityEngine . Debug . LogWarning ( $ "Received unknown part, with keys: { string . Join ( ',' , jsonDict . Keys ) } ") ;
0 commit comments