Skip to content

Commit

Permalink
"Add error handling and print the localized description of the error …
Browse files Browse the repository at this point in the history
…when running the tool in Agent.swift, and add a callback for locationManagerDidChangeAuthorization in GetLocationTool.swift."
  • Loading branch information
buhe committed Nov 8, 2023
1 parent 125142b commit 20d754a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/LangChain/agents/Agent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 7 additions & 5 deletions Sources/LangChain/tools/GetLocationTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@

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<String, Error>?
public override init(callbacks: [BaseCallbackHandler] = []) {
super.init(callbacks: callbacks)
// callback locationManagerDidChangeAuthorization
locationManager.delegate = self
}
public override func name() -> String {
"GetLocation"
}

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
Expand All @@ -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.
Expand Down

0 comments on commit 20d754a

Please sign in to comment.