@@ -161,6 +161,9 @@ public struct Tool {
161
161
/// A list of `FunctionDeclarations` available to the model.
162
162
let functionDeclarations : [ FunctionDeclaration ] ?
163
163
164
+ /// Enables the model to execute code as part of generation.
165
+ let codeExecution : CodeExecution ?
166
+
164
167
/// Constructs a new `Tool`.
165
168
///
166
169
/// - Parameters:
@@ -172,8 +175,11 @@ public struct Tool {
172
175
/// populating ``FunctionCall`` in the response. The next conversation turn may contain a
173
176
/// ``FunctionResponse`` in ``ModelContent/Part/functionResponse(_:)`` with the
174
177
/// ``ModelContent/role`` "function", providing generation context for the next model turn.
175
- public init ( functionDeclarations: [ FunctionDeclaration ] ? ) {
178
+ /// - codeExecution: Enables the model to execute code as part of generation, if provided.
179
+ public init ( functionDeclarations: [ FunctionDeclaration ] ? = nil ,
180
+ codeExecution: CodeExecution ? = nil ) {
176
181
self . functionDeclarations = functionDeclarations
182
+ self . codeExecution = codeExecution
177
183
}
178
184
}
179
185
@@ -244,6 +250,55 @@ public struct FunctionResponse: Equatable {
244
250
}
245
251
}
246
252
253
+ /// Tool that executes code generated by the model, automatically returning the result to the model.
254
+ ///
255
+ /// This type has no fields. See ``ExecutableCode`` and ``CodeExecutionResult``, which are only
256
+ /// generated when using this tool.
257
+ public struct CodeExecution {
258
+ /// Constructs a new `CodeExecution` tool.
259
+ public init ( ) { }
260
+ }
261
+
262
+ /// Code generated by the model that is meant to be executed, and the result returned to the model.
263
+ ///
264
+ /// Only generated when using the ``CodeExecution`` tool, in which case the code will automatically
265
+ /// be executed, and a corresponding ``CodeExecutionResult`` will also be generated.
266
+ public struct ExecutableCode : Equatable {
267
+ /// The programming language of the ``code``.
268
+ public let language : String
269
+
270
+ /// The code to be executed.
271
+ public let code : String
272
+ }
273
+
274
+ /// Result of executing the ``ExecutableCode``.
275
+ ///
276
+ /// Only generated when using the ``CodeExecution`` tool, and always follows a part containing the
277
+ /// ``ExecutableCode``.
278
+ public struct CodeExecutionResult : Equatable {
279
+ /// Possible outcomes of the code execution.
280
+ public enum Outcome : String {
281
+ /// An unrecognized code execution outcome was provided.
282
+ case unknown = " OUTCOME_UNKNOWN "
283
+ /// Unspecified status; this value should not be used.
284
+ case unspecified = " OUTCOME_UNSPECIFIED "
285
+ /// Code execution completed successfully.
286
+ case ok = " OUTCOME_OK "
287
+ /// Code execution finished but with a failure; ``CodeExecutionResult/output`` should contain
288
+ /// the failure details from `stderr`.
289
+ case failed = " OUTCOME_FAILED "
290
+ /// Code execution ran for too long, and was cancelled. There may or may not be a partial
291
+ /// ``CodeExecutionResult/output`` present.
292
+ case deadlineExceeded = " OUTCOME_DEADLINE_EXCEEDED "
293
+ }
294
+
295
+ /// Outcome of the code execution.
296
+ public let outcome : Outcome
297
+
298
+ /// Contains `stdout` when code execution is successful, `stderr` or other description otherwise.
299
+ public let output : String
300
+ }
301
+
247
302
// MARK: - Codable Conformance
248
303
249
304
extension FunctionCall : Decodable {
@@ -293,3 +348,31 @@ extension FunctionCallingConfig.Mode: Encodable {}
293
348
extension ToolConfig : Encodable { }
294
349
295
350
extension FunctionResponse : Encodable { }
351
+
352
+ extension CodeExecution : Encodable { }
353
+
354
+ extension ExecutableCode : Codable { }
355
+
356
+ @available ( iOS 15 . 0 , macOS 11 . 0 , macCatalyst 15 . 0 , * )
357
+ extension CodeExecutionResult . Outcome : Codable {
358
+ public init ( from decoder: any Decoder ) throws {
359
+ let value = try decoder. singleValueContainer ( ) . decode ( String . self)
360
+ guard let decodedOutcome = CodeExecutionResult . Outcome ( rawValue: value) else {
361
+ Logging . default
362
+ . error ( " [GoogleGenerativeAI] Unrecognized Outcome with value \" \( value) \" . " )
363
+ self = . unknown
364
+ return
365
+ }
366
+
367
+ self = decodedOutcome
368
+ }
369
+ }
370
+
371
+ @available ( iOS 15 . 0 , macOS 11 . 0 , macCatalyst 15 . 0 , * )
372
+ extension CodeExecutionResult : Codable {
373
+ public init ( from decoder: any Decoder ) throws {
374
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
375
+ outcome = try container. decode ( Outcome . self, forKey: . outcome)
376
+ output = try container. decodeIfPresent ( String . self, forKey: . output) ?? " "
377
+ }
378
+ }
0 commit comments