From 20d754aa8c52f682bb675fcb20edb461c8a5f6c6 Mon Sep 17 00:00:00 2001 From: buhe Date: Wed, 8 Nov 2023 12:08:52 +0800 Subject: [PATCH] "Add error handling and print the localized description of the error when running the tool in Agent.swift, and add a callback for locationManagerDidChangeAuthorization in GetLocationTool.swift." --- Sources/LangChain/agents/Agent.swift | 2 +- Sources/LangChain/tools/GetLocationTool.swift | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/LangChain/agents/Agent.swift b/Sources/LangChain/agents/Agent.swift index 61739aa..7d51d13 100644 --- a/Sources/LangChain/agents/Agent.swift +++ b/Sources/LangChain/agents/Agent.swift @@ -126,7 +126,7 @@ public class AgentExecutor: DefaultChain { } return (step, observation) } catch { -// print("\(error) at run \(tool.name()) tool.") + print("\(error.localizedDescription) at run \(tool.name()) tool.") let observation = try! await InvalidTool(tool_name: tool.name()).run(args: action.input) return (step, observation) } diff --git a/Sources/LangChain/tools/GetLocationTool.swift b/Sources/LangChain/tools/GetLocationTool.swift index 1833e15..d9d81d3 100644 --- a/Sources/LangChain/tools/GetLocationTool.swift +++ b/Sources/LangChain/tools/GetLocationTool.swift @@ -7,15 +7,16 @@ import Foundation import CoreLocation - +// !! Add "Privacy - Location When In Use Usage Description" to Info.plist public class GetLocationTool: BaseTool, CLLocationManagerDelegate { - // Add "Privacy - Location When In Use Usage Description" to Info.plist let locationManager:CLLocationManager = CLLocationManager() var authorizationStatus: CLAuthorizationStatus? private var locationContinuation: CheckedContinuation? public override init(callbacks: [BaseCallbackHandler] = []) { super.init(callbacks: callbacks) + // callback locationManagerDidChangeAuthorization + locationManager.delegate = self } public override func name() -> String { "GetLocation" @@ -23,14 +24,15 @@ public class GetLocationTool: BaseTool, CLLocationManagerDelegate { public override func description() -> String { """ - Tool to get current location. + Tool of get current location. Input must be here. Returns the current longitude and latitude, such as -78.4:38.5. """ } public override func _run(args: String) async throws -> String { - locationManager.delegate = self + + locationManager.requestLocation() //wait return try await withCheckedThrowingContinuation { continuation in locationContinuation = continuation @@ -51,7 +53,7 @@ public class GetLocationTool: BaseTool, CLLocationManagerDelegate { case .authorizedWhenInUse: // Location services are available. // Insert code here of what should happen when Location services are authorized // authorizationStatus = .authorizedWhenInUse - locationManager.requestLocation() +// locationManager.requestLocation() break case .restricted: // Location services currently unavailable.