-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from buhe/getlocation
Tool of get location
- Loading branch information
Showing
5 changed files
with
112 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
![](https://p.ipic.vip/2qqnzz.png) | ||
# 🐇 LangChain Swift | ||
# 🐇 Langchain Swift | ||
[![Swift](https://github.com/buhe/langchain-swift/actions/workflows/swift.yml/badge.svg)](https://github.com/buhe/langchain-swift/actions/workflows/swift.yml) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) ![Swift Package Manager](https://img.shields.io/badge/SwiftPM-compatible-brightgreen.svg) [![Twitter](https://img.shields.io/badge/[email protected]?style=flat)](http://twitter.com/buhe1986) | ||
|
||
🚀 A langchain copy, for ios or macOS apps. | ||
🚀 A Langchain copy, for iOS or macOS apps. | ||
|
||
|
||
## Setup | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 顾艳华 on 2023/11/7. | ||
// | ||
|
||
import Foundation | ||
import CoreLocation | ||
// !! Add "Privacy - Location When In Use Usage Description" to Info.plist | ||
public class GetLocationTool: BaseTool, CLLocationManagerDelegate { | ||
|
||
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 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.requestLocation() | ||
//wait | ||
return try await withCheckedThrowingContinuation { continuation in | ||
locationContinuation = continuation | ||
} | ||
|
||
} | ||
|
||
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | ||
let currLocation:CLLocation = locations.last! | ||
let longitude = currLocation.coordinate.longitude | ||
let latitude = currLocation.coordinate.latitude | ||
// signal | ||
locationContinuation?.resume(returning: "\(longitude):\(latitude)") | ||
} | ||
|
||
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { | ||
switch manager.authorizationStatus { | ||
case .authorizedWhenInUse: // Location services are available. | ||
// Insert code here of what should happen when Location services are authorized | ||
// authorizationStatus = .authorizedWhenInUse | ||
// locationManager.requestLocation() | ||
break | ||
|
||
case .restricted: // Location services currently unavailable. | ||
// Insert code here of what should happen when Location services are NOT authorized | ||
authorizationStatus = .restricted | ||
break | ||
|
||
case .denied: // Location services currently unavailable. | ||
// Insert code here of what should happen when Location services are NOT authorized | ||
authorizationStatus = .denied | ||
break | ||
|
||
case .notDetermined: // Authorization not determined yet. | ||
authorizationStatus = .notDetermined | ||
manager.requestWhenInUseAuthorization() | ||
break | ||
|
||
default: | ||
break | ||
} | ||
} | ||
|
||
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { | ||
print("error: \(error.localizedDescription)") | ||
locationContinuation?.resume(throwing: error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters