hands test
This commit is contained in:
@@ -26,28 +26,31 @@ struct Tool {
|
||||
let name: String
|
||||
let description: String
|
||||
let schema: String
|
||||
let run: @MainActor ([String: Any]) async throws -> [Content]
|
||||
let run: ([String: Any]) async throws -> [Content]
|
||||
}
|
||||
|
||||
let stdoutLock = NSLock()
|
||||
|
||||
@main
|
||||
struct Server {
|
||||
static func main() {
|
||||
_ = NSApplication.shared
|
||||
AXUIElementSetMessagingTimeout(AXUIElementCreateSystemWide(), 3)
|
||||
Thread.detachNewThread {
|
||||
let inFlight = DispatchGroup()
|
||||
while let line = readLine() {
|
||||
let done = DispatchSemaphore(value: 0)
|
||||
Task { @MainActor in
|
||||
inFlight.enter()
|
||||
Task.detached {
|
||||
await handle(line)
|
||||
done.signal()
|
||||
inFlight.leave()
|
||||
}
|
||||
done.wait()
|
||||
}
|
||||
inFlight.wait()
|
||||
exit(0)
|
||||
}
|
||||
RunLoop.main.run()
|
||||
}
|
||||
|
||||
@MainActor
|
||||
static func handle(_ line: String) async {
|
||||
guard let data = line.data(using: .utf8),
|
||||
let req = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
||||
@@ -75,12 +78,13 @@ struct Server {
|
||||
default:
|
||||
resp["error"] = ["code": -32601, "message": "method not found: \(req["method"] ?? "")"]
|
||||
}
|
||||
let out = try! JSONSerialization.data(withJSONObject: resp, options: .withoutEscapingSlashes)
|
||||
FileHandle.standardOutput.write(out)
|
||||
FileHandle.standardOutput.write(Data("\n".utf8))
|
||||
var out = try! JSONSerialization.data(withJSONObject: resp, options: .withoutEscapingSlashes)
|
||||
out.append(Data("\n".utf8))
|
||||
stdoutLock.withLock {
|
||||
FileHandle.standardOutput.write(out)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
static func call(_ params: [String: Any]) async -> [String: Any] {
|
||||
let name = params["name"] as? String ?? ""
|
||||
let args = params["arguments"] as? [String: Any] ?? [:]
|
||||
|
||||
Reference in New Issue
Block a user