Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Swift tool calls implementation draft (#667)

Aleksandr Golokoz0b5eca2d9327
diff --git a/Cargo.lock b/Cargo.lock
index 291a76a63..f60ee873f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3750,6 +3750,7 @@ dependencies = [
name = "nagare"
version = "0.5.14"
dependencies = [
+ "async-trait",
"bindings",
"bindings-types",
"chrono",
diff --git a/bindings/swift/Sources/Examples/CLI.swift b/bindings/swift/Sources/Examples/CLI.swift
index 37885c587..1177245e3 100644
--- a/bindings/swift/Sources/Examples/CLI.swift
+++ b/bindings/swift/Sources/Examples/CLI.swift
@@ -12,7 +12,7 @@ struct Example: AsyncParsableCommand {
@Argument(
help:
- "Mode: chat | chat-for-summarization | chat-for-classification | quick-start | snippets | cloud | ssm | structured-output | classifier",
+ "Mode: chat | chat-cloud | chat-structured-output | classification | quick-start | tool-calls",
transform: { $0.lowercased() })
var mode: String = "chat"
@@ -28,6 +28,8 @@ struct Example: AsyncParsableCommand {
try await runChatStructuredOutput()
case "classification":
try await runClassification()
+ case "tool-calls":
+ try await runToolCalls()
default:
throw ValidationError("Unknown mode: \(mode)")
}
diff --git a/bindings/swift/Sources/Examples/ToolCalls.swift b/bindings/swift/Sources/Examples/ToolCalls.swift
new file mode 100644
index 000000000..c66b32b67
--- /dev/null
+++ b/bindings/swift/Sources/Examples/ToolCalls.swift
@@ -0,0 +1,61 @@
+import FoundationModels
+import Uzu
+
+@Generable
+private struct Coordinate: Codable, Sendable {
+ @Guide(description: "Latitude in decimal degrees.")
+ let latitude: Double
+
+ @Guide(description: "Longitude in decimal degrees.")
+ let longitude: Double
+}
+
+private struct GetCurrentLocationTool: Tool {
+ let description = "Return the current location in coordinates."
+
+ @Generable
+ struct Arguments {}
+
+ func call(arguments: Arguments) async throws -> Coordinate {
+ Coordinate(latitude: 51.5074, longitude: -0.1278)
+ }
+}
+
+private struct GetCurrentTemperatureTool: Tool {
+ let description = "Return the temperature at the provided coordinates."
+
+ func call(arguments: Coordinate) async throws -> Double {
+ _ = arguments
+ return 25.0
+ }
+}
+
+public func runToolCalls() async throws {
+ let engine = try await Engine.create(config: .create())
+ guard let model = try await engine.model(identifier: "mlx-community/Qwen3.5-9B-MLX-8bit") else {
+ throw ToolCallsExampleError.modelNotFound
+ }
+ for try await update in try await engine.download(model: model).iterator() {
+ print("Download progress: \(update.progress())")
+ }
+
+ let session = try await engine.chat(model: model, config: .create())
+ try await session.addTool(GetCurrentLocationTool())
+ try await session.addTool(GetCurrentTemperatureTool())
+
+ let messages = [
+ ChatMessage.system().withText(text: "You are a helpful assistant"),
+ ChatMessage.user().withText(text: "What temperature is it now at my location?"),
+ ]
+ let replies = try await session.reply(input: messages, config: .create())
+ guard let message = replies.last?.message else {
+ return
+ }
+
+ print("Reasoning: \(message.reasoning() ?? "")")
+ print("Text: \(message.text() ?? "")")
+}
+
+private enum ToolCallsExampleError: Swift.Error {
+ case modelNotFound
+}
diff --git a/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swift b/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swift
index 29203a0fa..0320c5447 100644
--- a/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swift
+++ b/bindings/swift/Sources/Uzu/Generated/uzu/nagare.swift
@@ -414,7 +414,13 @@ fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
// Public interface members begin here.
-
+// Magic number for the Rust proxy to call using the same mechanism as every other method,
+// to free the callback once it's dropped by Rust.
+private let IDX_CALLBACK_FREE: Int32 = 0
+// Callback return codes
+private let UNIFFI_CALLBACK_SUCCESS: Int32 = 0
+private let UNIFFI_CALLBACK_ERROR: Int32 = 1
+private let UNIFFI_CALLBACK_UNEXPECTED_ERROR: Int32 = 2
#if swift(>=5.8)
@_documentation(visibility: private)
@@ -478,6 +484,10 @@ fileprivate struct FfiConverterString: FfiConverter {
public protocol ChatSessionProtocol: AnyObject, Sendable {
+ func addForeignTool(tool: ForeignTool) async throws
+
+ func addForeignTools(tools: [ForeignTool]) async throws
+
func messages() async -> [ChatMessage]
func reply(input: [ChatMessage], config: ChatReplyConfig) async throws -> [ChatReply]
@@ -542,6 +552,40 @@ open class ChatSession: ChatSessionProtocol, @unchecked Sendable {
+open func addForeignTool(tool: ForeignTool)async throws {
+ return
+ try await uniffiRustCallAsync(
+ rustFutureFunc: {
+ uniffi_nagare_fn_method_chatsession_add_foreign_tool(
+ self.uniffiCloneHandle(),
+ FfiConverterTypeForeignTool_lower(tool)
+ )
+ },
+ pollFunc: ffi_nagare_rust_future_poll_void,
+ completeFunc: ffi_nagare_rust_future_complete_void,
+ freeFunc: ffi_nagare_rust_future_free_void,
+ liftFunc: { $0 },
+ errorHandler: FfiConverterTypeChatSessionError_lift
+ )
+}
+
+open func addForeignTools(tools: [ForeignTool])async throws {
+ return
+ try await uniffiRustCallAsync(
+ rustFutureFunc: {
+ uniffi_nagare_fn_method_chatsession_add_foreign_tools(
+ self.uniffiCloneHandle(),
+ FfiConverterSequenceTypeForeignTool.lower(tools)
+ )
+ },
+ pollFunc: ffi_nagare_rust_future_poll_void,
+ completeFunc: ffi_nagare_rust_future_complete_void,
+ freeFunc: ffi_nagare_rust_future_free_void,
+ liftFunc: { $0 },
+ errorHandler: FfiConverterTypeChatSessionError_lift
+ )
+}
+
open func messages()async -> [ChatMessage] {
return
try! await uniffiRustCallAsync(
@@ -961,6 +1005,121 @@ public func FfiConverterTypeClassificationSession_lower(_ value: ClassificationS
+public protocol ForeignToolProtocol: AnyObject, Sendable {
+
+}
+open class ForeignTool: ForeignToolProtocol, @unchecked Sendable {
+ fileprivate let handle: UInt64
+
+ /// Used to instantiate a [FFIObject] without an actual handle, for fakes in tests, mostly.
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ public struct NoHandle {
+ public init() {}
+ }
+
+ // TODO: We'd like this to be `private` but for Swifty reasons,
+ // we can't implement `FfiConverter` without making this `required` and we can't
+ // make it `required` without making it `public`.
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ required public init(unsafeFromHandle handle: UInt64) {
+ self.handle = handle
+ }
+
+ // This constructor can be used to instantiate a fake object.
+ // - Parameter noHandle: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].
+ //
+ // - Warning:
+ // Any object instantiated with this constructor cannot be passed to an actual Rust-backed object. Since there isn't a backing handle the FFI lower functions will crash.
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ public init(noHandle: NoHandle) {
+ self.handle = 0
+ }
+
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ public func uniffiCloneHandle() -> UInt64 {
+ return try! rustCall { uniffi_nagare_fn_clone_foreigntool(self.handle, $0) }
+ }
+public convenience init(definition: ToolFunction, handler: ForeignToolHandler) {
+ let handle =
+ try! rustCall() {
+ uniffi_nagare_fn_constructor_foreigntool_new(
+ FfiConverterTypeToolFunction_lower(definition),
+ FfiConverterCallbackInterfaceForeignToolHandler_lower(handler),$0
+ )
+}
+ self.init(unsafeFromHandle: handle)
+}
+
+ deinit {
+ if handle == 0 {
+ // Mock objects have handle=0 don't try to free them
+ return
+ }
+
+ try! rustCall { uniffi_nagare_fn_free_foreigntool(handle, $0) }
+ }
+
+
+
+
+
+
+}
+
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public struct FfiConverterTypeForeignTool: FfiConverter {
+ typealias FfiType = UInt64
+ typealias SwiftType = ForeignTool
+
+ public static func lift(_ handle: UInt64) throws -> ForeignTool {
+ return ForeignTool(unsafeFromHandle: handle)
+ }
+
+ public static func lower(_ value: ForeignTool) -> UInt64 {
+ return value.uniffiCloneHandle()
+ }
+
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ForeignTool {
+ let handle: UInt64 = try readInt(&buf)
+ return try lift(handle)
+ }
+
+ public static func write(_ value: ForeignTool, into buf: inout [UInt8]) {
+ writeInt(&buf, lower(value))
+ }
+}
+
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeForeignTool_lift(_ handle: UInt64) throws -> ForeignTool {
+ return try FfiConverterTypeForeignTool.lift(handle)
+}
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeForeignTool_lower(_ value: ForeignTool) -> UInt64 {
+ return FfiConverterTypeForeignTool.lower(value)
+}
+
+
+
+
+
+
public protocol TextToSpeechSessionProtocol: AnyObject, Sendable {
func state() async -> TextToSpeechSessionState
@@ -1685,6 +1844,80 @@ public func FfiConverterTypeClassificationSessionState_lower(_ value: Classifica
+public enum ForeignToolError: Swift.Error, Equatable, Hashable, Codable, Foundation.LocalizedError {
+
+
+
+ case Invocation(message: String
+ )
+
+
+
+
+
+
+ public var errorDescription: String? {
+ String(reflecting: self)
+ }
+
+}
+
+#if compiler(>=6)
+extension ForeignToolError: Sendable {}
+#endif
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public struct FfiConverterTypeForeignToolError: FfiConverterRustBuffer {
+ typealias SwiftType = ForeignToolError
+
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> ForeignToolError {
+ let variant: Int32 = try readInt(&buf)
+ switch variant {
+
+
+
+
+ case 1: return .Invocation(
+ message: try FfiConverterString.read(from: &buf)
+ )
+
+ default: throw UniffiInternalError.unexpectedEnumCase
+ }
+ }
+
+ public static func write(_ value: ForeignToolError, into buf: inout [UInt8]) {
+ switch value {
+
+
+
+
+
+ case let .Invocation(message):
+ writeInt(&buf, Int32(1))
+ FfiConverterString.write(message, into: &buf)
+
+ }
+ }
+}
+
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeForeignToolError_lift(_ buf: RustBuffer) throws -> ForeignToolError {
+ return try FfiConverterTypeForeignToolError.lift(buf)
+}
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterTypeForeignToolError_lower(_ value: ForeignToolError) -> RustBuffer {
+ return FfiConverterTypeForeignToolError.lower(value)
+}
+
+
public enum TextToSpeechSessionError: Swift.Error, Equatable, Hashable, Codable, Foundation.LocalizedError {
@@ -1916,6 +2149,149 @@ public func FfiConverterTypeTextToSpeechSessionStreamChunk_lower(_ value: TextTo
}
+
+
+
+public protocol ForeignToolHandler: AnyObject, Sendable {
+
+ func invokeJson(argumentsJson: String) async throws -> String
+
+}
+
+
+// Put the implementation in a struct so we don't pollute the top-level namespace
+fileprivate struct UniffiCallbackInterfaceForeignToolHandler {
+
+ // Create the VTable using a series of closures.
+ // Swift automatically converts these into C callback functions.
+ //
+ // This creates 1-element array, since this seems to be the only way to construct a const
+ // pointer that we can pass to the Rust code.
+ static let vtable: [UniffiVTableCallbackInterfaceForeignToolHandler] = [UniffiVTableCallbackInterfaceForeignToolHandler(
+ uniffiFree: { (uniffiHandle: UInt64) -> () in
+ do {
+ try FfiConverterCallbackInterfaceForeignToolHandler.handleMap.remove(handle: uniffiHandle)
+ } catch {
+ print("Uniffi callback interface ForeignToolHandler: handle missing in uniffiFree")
+ }
+ },
+ uniffiClone: { (uniffiHandle: UInt64) -> UInt64 in
+ do {
+ return try FfiConverterCallbackInterfaceForeignToolHandler.handleMap.clone(handle: uniffiHandle)
+ } catch {
+ fatalError("Uniffi callback interface ForeignToolHandler: handle missing in uniffiClone")
+ }
+ },
+ invokeJson: { (
+ uniffiHandle: UInt64,
+ argumentsJson: RustBuffer,
+ uniffiFutureCallback: @escaping UniffiForeignFutureCompleteRustBuffer,
+ uniffiCallbackData: UInt64,
+ uniffiOutDroppedCallback: UnsafeMutablePointer<UniffiForeignFutureDroppedCallbackStruct>
+ ) in
+ let makeCall = {
+ () async throws -> String in
+ guard let uniffiObj = try? FfiConverterCallbackInterfaceForeignToolHandler.handleMap.get(handle: uniffiHandle) else {
+ throw UniffiInternalError.unexpectedStaleHandle
+ }
+ return try await uniffiObj.invokeJson(
+ argumentsJson: try FfiConverterString.lift(argumentsJson)
+ )
+ }
+
+ let uniffiHandleSuccess = { (returnValue: String) in
+ uniffiFutureCallback(
+ uniffiCallbackData,
+ UniffiForeignFutureResultRustBuffer(
+ returnValue: FfiConverterString.lower(returnValue),
+ callStatus: RustCallStatus()
+ )
+ )
+ }
+ let uniffiHandleError = { (statusCode, errorBuf) in
+ uniffiFutureCallback(
+ uniffiCallbackData,
+ UniffiForeignFutureResultRustBuffer(
+ returnValue: RustBuffer.empty(),
+ callStatus: RustCallStatus(code: statusCode, errorBuf: errorBuf)
+ )
+ )
+ }
+ uniffiTraitInterfaceCallAsyncWithError(
+ makeCall: makeCall,
+ handleSuccess: uniffiHandleSuccess,
+ handleError: uniffiHandleError,
+ lowerError: FfiConverterTypeForeignToolError_lower,
+ droppedCallback: uniffiOutDroppedCallback
+ )
+ }
+ )]
+}
+
+private func uniffiCallbackInitForeignToolHandler() {
+ uniffi_nagare_fn_init_callback_vtable_foreigntoolhandler(UniffiCallbackInterfaceForeignToolHandler.vtable)
+}
+
+// FfiConverter protocol for callback interfaces
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+fileprivate struct FfiConverterCallbackInterfaceForeignToolHandler {
+ fileprivate static let handleMap = UniffiHandleMap<ForeignToolHandler>()
+}
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+extension FfiConverterCallbackInterfaceForeignToolHandler : FfiConverter {
+ typealias SwiftType = ForeignToolHandler
+ typealias FfiType = UInt64
+
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ public static func lift(_ handle: UInt64) throws -> SwiftType {
+ try handleMap.get(handle: handle)
+ }
+
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType {
+ let handle: UInt64 = try readInt(&buf)
+ return try lift(handle)
+ }
+
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ public static func lower(_ v: SwiftType) -> UInt64 {
+ return handleMap.insert(obj: v)
+ }
+
+#if swift(>=5.8)
+ @_documentation(visibility: private)
+#endif
+ public static func write(_ v: SwiftType, into buf: inout [UInt8]) {
+ writeInt(&buf, lower(v))
+ }
+}
+
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterCallbackInterfaceForeignToolHandler_lift(_ handle: UInt64) throws -> ForeignToolHandler {
+ return try FfiConverterCallbackInterfaceForeignToolHandler.lift(handle)
+}
+
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+public func FfiConverterCallbackInterfaceForeignToolHandler_lower(_ v: ForeignToolHandler) -> UInt64 {
+ return FfiConverterCallbackInterfaceForeignToolHandler.lower(v)
+}
+
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
@@ -1964,6 +2340,31 @@ fileprivate struct FfiConverterOptionTypeTextToSpeechSessionStreamChunk: FfiConv
}
}
+#if swift(>=5.8)
+@_documentation(visibility: private)
+#endif
+fileprivate struct FfiConverterSequenceTypeForeignTool: FfiConverterRustBuffer {
+ typealias SwiftType = [ForeignTool]
+
+ public static func write(_ value: [ForeignTool], into buf: inout [UInt8]) {
+ let len = Int32(value.count)
+ writeInt(&buf, len)
+ for item in value {
+ FfiConverterTypeForeignTool.write(item, into: &buf)
+ }
+ }
+
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> [ForeignTool] {
+ let len: Int32 = try readInt(&buf)
+ var seq = [ForeignTool]()
+ seq.reserveCapacity(Int(len))
+ for _ in 0 ..< len {
+ seq.append(try FfiConverterTypeForeignTool.read(from: &buf))
+ }
+ return seq
+ }
+}
+
#if swift(>=5.8)
@_documentation(visibility: private)
#endif
@@ -2086,6 +2487,96 @@ fileprivate func uniffiFutureContinuationCallback(handle: UInt64, pollResult: In
print("uniffiFutureContinuationCallback invalid handle")
}
}
+private func uniffiTraitInterfaceCallAsync<T>(
+ makeCall: @escaping () async throws -> T,
+ handleSuccess: @escaping (T) -> (),
+ handleError: @escaping (Int8, RustBuffer) -> (),
+ droppedCallback: UnsafeMutablePointer<UniffiForeignFutureDroppedCallbackStruct>
+) {
+ let task = Task {
+ // Note: it's important we call either `handleSuccess` or `handleError` exactly once. Each
+ // call consumes an Arc reference, which means there should be no possibility of a double
+ // call. The following code is structured so that will will never call both `handleSuccess`
+ // and `handleError`, even in the face of weird errors.
+ //
+ // On platforms that need extra machinery to make C-ABI calls, like JNA or ctypes, it's
+ // possible that we fail to make either call. However, it doesn't seem like this is
+ // possible on Swift since swift can just make the C call directly.
+ var callResult: T
+ do {
+ callResult = try await makeCall()
+ } catch {
+ handleError(CALL_UNEXPECTED_ERROR, FfiConverterString.lower(String(describing: error)))
+ return
+ }
+ handleSuccess(callResult)
+ }
+ let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
+ droppedCallback.pointee = UniffiForeignFutureDroppedCallbackStruct(
+ handle: handle,
+ free: uniffiForeignFutureDroppedCallback
+ )
+}
+
+private func uniffiTraitInterfaceCallAsyncWithError<T, E>(
+ makeCall: @escaping () async throws -> T,
+ handleSuccess: @escaping (T) -> (),
+ handleError: @escaping (Int8, RustBuffer) -> (),
+ lowerError: @escaping (E) -> RustBuffer,
+ droppedCallback: UnsafeMutablePointer<UniffiForeignFutureDroppedCallbackStruct>
+) {
+ let task = Task {
+ // See the note in uniffiTraitInterfaceCallAsync for details on `handleSuccess` and
+ // `handleError`.
+ var callResult: T
+ do {
+ callResult = try await makeCall()
+ } catch let error as E {
+ handleError(CALL_ERROR, lowerError(error))
+ return
+ } catch {
+ handleError(CALL_UNEXPECTED_ERROR, FfiConverterString.lower(String(describing: error)))
+ return
+ }
+ handleSuccess(callResult)
+ }
+ let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
+ droppedCallback.pointee = UniffiForeignFutureDroppedCallbackStruct(
+ handle: handle,
+ free: uniffiForeignFutureDroppedCallback
+ )
+}
+
+// Borrow the callback handle map implementation to store foreign future handles
+// TODO: consolidate the handle-map code (https://github.com/mozilla/uniffi-rs/pull/1823)
+fileprivate let UNIFFI_FOREIGN_FUTURE_HANDLE_MAP = UniffiHandleMap<UniffiForeignFutureTask>()
+
+// Protocol for tasks that handle foreign futures.
+//
+// Defining a protocol allows all tasks to be stored in the same handle map. This can't be done
+// with the task object itself, since has generic parameters.
+fileprivate protocol UniffiForeignFutureTask {
+ func cancel()
+}
+
+extension Task: UniffiForeignFutureTask {}
+
+private func uniffiForeignFutureDroppedCallback(handle: UInt64) {
+ do {
+ let task = try UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.remove(handle: handle)
+ // Set the cancellation flag on the task. If it's still running, the code can check the
+ // cancellation flag or call `Task.checkCancellation()`. If the task has completed, this is
+ // a no-op.
+ task.cancel()
+ } catch {
+ print("uniffiForeignFutureDroppedCallback: handle missing from handlemap")
+ }
+}
+
+// For testing
+public func uniffiForeignFutureHandleCountNagare() -> Int {
+ UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.count
+}
private enum InitializationResult {
case ok
@@ -2102,6 +2593,12 @@ private let initializationResult: InitializationResult = {
if bindings_contract_version != scaffolding_contract_version {
return InitializationResult.contractVersionMismatch
}
+ if (uniffi_nagare_checksum_method_chatsession_add_foreign_tool() != 48906) {
+ return InitializationResult.apiChecksumMismatch
+ }
+ if (uniffi_nagare_checksum_method_chatsession_add_foreign_tools() != 57388) {
+ return InitializationResult.apiChecksumMismatch
+ }
if (uniffi_nagare_checksum_method_chatsession_messages() != 35188) {
return InitializationResult.apiChecksumMismatch
}
@@ -2144,7 +2641,14 @@ private let initializationResult: InitializationResult = {
if (uniffi_nagare_checksum_method_texttospeechsessionstream_next() != 21181) {
return InitializationResult.apiChecksumMismatch
}
+ if (uniffi_nagare_checksum_constructor_foreigntool_new() != 14839) {
+ return InitializationResult.apiChecksumMismatch
+ }
+ if (uniffi_nagare_checksum_method_foreigntoolhandler_invoke_json() != 764) {
+ return InitializationResult.apiChecksumMismatch
+ }
+ uniffiCallbackInitForeignToolHandler()
uniffiEnsureShojiInitialized()
return InitializationResult.ok
}()
diff --git a/bindings/swift/Sources/Uzu/Tool.swift b/bindings/swift/Sources/Uzu/Tool.swift
new file mode 100644
index 000000000..ea88c2ba3
--- /dev/null
+++ b/bindings/swift/Sources/Uzu/Tool.swift
@@ -0,0 +1,64 @@
+import Foundation
+import FoundationModels
+
+public enum FoundationModelsToolError: Error, Equatable, LocalizedError, Sendable {
+ case parametersMustBeObject(toolName: String)
+
+ public var errorDescription: String? {
+ switch self {
+ case .parametersMustBeObject(let toolName):
+ return "FoundationModels tool '\(toolName)' must describe its parameters with an object schema"
+ }
+ }
+}
+
+extension ChatSession {
+ public func addTool<T: FoundationModels.Tool>(_ tool: T) async throws
+ where T.Output: FoundationModels.ConvertibleToGeneratedContent {
+ let definition = try foundationModelsToolDefinition(for: tool)
+ try await addForeignTool(
+ tool: ForeignTool(
+ definition: definition,
+ handler: FoundationModelsToolHandler(tool: tool)
+ )
+ )
+ }
+}
+
+final class FoundationModelsToolHandler<T: FoundationModels.Tool>: ForeignToolHandler,
+ @unchecked Sendable
+where T.Output: FoundationModels.ConvertibleToGeneratedContent {
+ private let tool: T
+
+ init(tool: T) {
+ self.tool = tool
+ }
+
+ func invokeJson(argumentsJson: String) async throws -> String {
+ do {
+ let content = try FoundationModels.GeneratedContent(json: argumentsJson)
+ let arguments = try T.Arguments(content)
+ let output = try await tool.call(arguments: arguments)
+ return output.generatedContent.jsonString
+ } catch let error as ForeignToolError {
+ throw error
+ } catch {
+ throw ForeignToolError.Invocation(message: String(describing: error))
+ }
+ }
+}
+
+func foundationModelsToolDefinition<T: FoundationModels.Tool>(for tool: T) throws -> ToolFunction {
+ let parametersJson = tool.parameters.debugDescription
+ let parameters = try JSONSerialization.jsonObject(with: Data(parametersJson.utf8))
+ guard let schema = parameters as? [String: Any], schema["type"] as? String == "object" else {
+ throw FoundationModelsToolError.parametersMustBeObject(toolName: tool.name)
+ }
+
+ return ToolFunction(
+ name: tool.name,
+ description: tool.description,
+ parameters: Value(json: parametersJson),
+ returnDefinition: nil
+ )
+}
diff --git a/bindings/swift/Tests/UzuTests/ToolTests.swift b/bindings/swift/Tests/UzuTests/ToolTests.swift
new file mode 100644
index 000000000..25046103d
--- /dev/null
+++ b/bindings/swift/Tests/UzuTests/ToolTests.swift
@@ -0,0 +1,87 @@
+import Foundation
+import FoundationModels
+import XCTest
+@testable import Uzu
+
+@Generable
+private struct Coordinate: Codable, Sendable {
+ @Guide(description: "Latitude in decimal degrees.")
+ let latitude: Double
+
+ @Guide(description: "Longitude in decimal degrees.")
+ let longitude: Double
+}
+
+@Generable
+private struct EmptyInput {}
+
+private struct GetCurrentLocationTool: Tool {
+ let description = "Return the current location in coordinates."
+
+ func call(arguments: EmptyInput) async throws -> Coordinate {
+ Coordinate(latitude: 51.5074, longitude: -0.1278)
+ }
+}
+
+private struct GetCurrentTemperatureTool: Tool {
+ let name = "get_current_temperature"
+ let description = "Return the temperature at the provided coordinates."
+
+ func call(arguments: Coordinate) async throws -> Double {
+ _ = arguments
+ return 25.0
+ }
+}
+
+private struct EchoTool: Tool {
+ let description = "Echo the provided text."
+ let parameters = String.generationSchema
+
+ func call(arguments: String) async throws -> String {
+ arguments
+ }
+}
+
+final class ToolTests: XCTestCase {
+ func testFoundationModelsToolBuildsDefinitionAndInvokesHandler() async throws {
+ let tool = GetCurrentTemperatureTool()
+ let definition = try foundationModelsToolDefinition(for: tool)
+
+ XCTAssertEqual(definition.name, "get_current_temperature")
+ XCTAssertEqual(definition.description, "Return the temperature at the provided coordinates.")
+
+ let parameters = try XCTUnwrap(definition.parameters)
+ let schema = try JSONSerialization.jsonObject(with: Data(parameters.json.utf8)) as? [String: Any]
+ XCTAssertEqual(schema?["type"] as? String, "object")
+
+ let handler = FoundationModelsToolHandler(tool: tool)
+ let resultJson = try await handler.invokeJson(
+ argumentsJson: #"{"latitude":51.5074,"longitude":-0.1278}"#
+ )
+ XCTAssertEqual(resultJson, "25")
+ }
+
+ func testFoundationModelsToolSupportsEmptyArgumentsAndStructuredOutput() async throws {
+ let tool = GetCurrentLocationTool()
+ let definition = try foundationModelsToolDefinition(for: tool)
+
+ XCTAssertEqual(definition.name, "GetCurrentLocationTool")
+
+ let handler = FoundationModelsToolHandler(tool: tool)
+ let resultJson = try await handler.invokeJson(argumentsJson: "{}")
+ let result = try JSONDecoder().decode(Coordinate.self, from: Data(resultJson.utf8))
+ XCTAssertEqual(result.latitude, 51.5074)
+ XCTAssertEqual(result.longitude, -0.1278)
+ }
+
+ func testFoundationModelsToolRejectsNonObjectArgumentsSchema() throws {
+ let tool = EchoTool()
+
+ XCTAssertThrowsError(try foundationModelsToolDefinition(for: tool)) { error in
+ XCTAssertEqual(
+ error as? FoundationModelsToolError,
+ .parametersMustBeObject(toolName: "EchoTool")
+ )
+ }
+ }
+}
diff --git a/crates/nagare/Cargo.toml b/crates/nagare/Cargo.toml
index 97c284a86..5cb24606e 100644
--- a/crates/nagare/Cargo.toml
+++ b/crates/nagare/Cargo.toml
@@ -22,6 +22,7 @@ bindings = [
bindings-uniffi = [
"bindings-types/bindings-uniffi",
"shoji/bindings-uniffi",
+ "dep:async-trait",
"dep:uniffi",
]
bindings-napi = [
@@ -68,6 +69,7 @@ tokio-util.workspace = true
tokenizers.workspace = true
tokio = { workspace = true, features = ["full"] }
uniffi = { workspace = true, optional = true }
+async-trait = { workspace = true, optional = true }
napi = { workspace = true, optional = true }
napi-derive = { workspace = true, optional = true }
pyo3 = { workspace = true, optional = true }
diff --git a/crates/nagare/src/chat/mod.rs b/crates/nagare/src/chat/mod.rs
index 3b4c82c2a..7af075585 100644
--- a/crates/nagare/src/chat/mod.rs
+++ b/crates/nagare/src/chat/mod.rs
@@ -29,6 +29,8 @@ use tokio::sync::{Mutex, mpsc, mpsc::UnboundedSender};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;
+#[cfg(feature = "bindings-uniffi")]
+use crate::tool::bindings_uniffi::ForeignTool;
use crate::{
telemetry::{Telemetry, TelemetryEvent},
tool::{func_def::ToolDescriptor, registry::ToolRegistry},
@@ -504,6 +506,27 @@ impl ChatSession {
}
}
+#[cfg(feature = "bindings-uniffi")]
+#[uniffi::export(async_runtime = "tokio")]
+impl ChatSession {
+ pub async fn add_foreign_tool(
+ &self,
+ tool: Arc<ForeignTool>,
+ ) -> Result<(), ChatSessionError> {
+ let mut session = self.clone();
+ session.add_tool(tool.descriptor()).await
+ }
+
+ pub async fn add_foreign_tools(
+ &self,
+ tools: Vec<Arc<ForeignTool>>,
+ ) -> Result<(), ChatSessionError> {
+ let descriptors = tools.iter().map(|tool| tool.descriptor()).collect();
+ let mut session = self.clone();
+ session.add_tools(descriptors).await
+ }
+}
+
#[bindings::export(Implementation)]
impl ChatSession {
#[bindings::export(Method(Getter))]
diff --git a/crates/nagare/src/tool/bindings_uniffi.rs b/crates/nagare/src/tool/bindings_uniffi.rs
new file mode 100644
index 000000000..06a84960f
--- /dev/null
+++ b/crates/nagare/src/tool/bindings_uniffi.rs
@@ -0,0 +1,64 @@
+use std::sync::Arc;
+
+use shoji::types::basic::ToolFunction;
+
+use super::func_def::{ErrorFuture, ToolDescriptor};
+
+#[derive(Debug, Clone, thiserror::Error, uniffi::Error)]
+pub enum ForeignToolError {
+ #[error("Tool invocation failed: {message}")]
+ Invocation {
+ message: String,
+ },
+}
+
+#[uniffi::export(callback_interface)]
+#[async_trait::async_trait]
+pub trait ForeignToolHandler: Send + Sync {
+ async fn invoke_json(
+ &self,
+ arguments_json: String,
+ ) -> Result<String, ForeignToolError>;
+}
+
+#[derive(uniffi::Object)]
+pub struct ForeignTool {
+ descriptor: ToolDescriptor,
+}
+
+#[uniffi::export]
+impl ForeignTool {
+ #[uniffi::constructor]
+ pub fn new(
+ definition: ToolFunction,
+ handler: Box<dyn ForeignToolHandler>,
+ ) -> Arc<Self> {
+ let handler: Arc<dyn ForeignToolHandler> = Arc::from(handler);
+ let descriptor = ToolDescriptor::new(
+ definition.name,
+ definition.description,
+ definition.parameters,
+ definition.return_definition,
+ Box::new(move |arguments| {
+ let handler = Arc::clone(&handler);
+ Box::new(async move {
+ let json = handler
+ .invoke_json(arguments.json)
+ .await
+ .map_err(|error| -> ErrorFuture { error.to_string().into() })?;
+ let value = serde_json::from_str::<serde_json::Value>(&json)?;
+ Ok(value.into())
+ })
+ }),
+ );
+ Arc::new(Self {
+ descriptor,
+ })
+ }
+}
+
+impl ForeignTool {
+ pub(crate) fn descriptor(&self) -> ToolDescriptor {
+ self.descriptor.clone()
+ }
+}
diff --git a/crates/nagare/src/tool/mod.rs b/crates/nagare/src/tool/mod.rs
index 386eb3239..ec3344224 100644
--- a/crates/nagare/src/tool/mod.rs
+++ b/crates/nagare/src/tool/mod.rs
@@ -1,3 +1,5 @@
+#[cfg(feature = "bindings-uniffi")]
+pub mod bindings_uniffi;
pub mod func_def;
pub mod registry;
pub mod schema;