-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
135 lines (117 loc) · 3.32 KB
/
ContentView.swift
File metadata and controls
135 lines (117 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import FuegoOnAppleSilicon
import SwiftUI
let testCommands = [
"boardsize 19",
"clear_board",
"komi 6.5",
"play b D4",
"showboard",
"genmove w",
"showboard",
"play b E4",
"showboard",
"genmove w",
"showboard",
// "set_free_handicap d16 q16 d4 q4",
// "genmove w"
]
struct ContentView: View {
@State private var sentCommands: [String] = []
@State private var testCommandIndex: Int = 0
@State private var inputText: String = testCommands[0]
@State private var fuegoResponse: String = ""
@State private var fuegoError: String = ""
@State var fuegoBridge: FuegoBridge? = nil
init() {
guard let bundleBookUrl = Bundle.main.url(forResource: "book", withExtension: "dat") else {
fatalError("can't retrieve bundle path")
}
// Just example. Much better to copy book into working dir
let bookDir = bundleBookUrl.deletingLastPathComponent()
if !FileManager.default.changeCurrentDirectoryPath(bookDir.path) {
fatalError("can't set book directory directory")
}
}
func startEngine() async {
if fuegoBridge == nil {
fuegoBridge = FuegoBridge()
print("AI connecting...")
do {
try await fuegoBridge!.startEngine()
print("AI connected")
} catch {
print("AI didn't connect, error: \(error)")
fuegoError = "\(error)"
}
}
}
func stopEngine() async {
print("stopping AI")
await fuegoBridge?.stopEngine()
testCommandIndex = 0
fuegoBridge = nil
sentCommands = []
inputText = testCommands[0]
fuegoResponse = ""
fuegoError = ""
print("stopped AI")
}
func handleResponse(_ response: String) {
print("response →", response)
fuegoResponse = response
testCommandIndex += 1
if testCommandIndex >= testCommands.count {
inputText = ""
} else {
inputText = testCommands[testCommandIndex]
}
fuegoError = ""
}
// current implementation using a callback function:
func submitCommand() {
Task {
guard let fuegoBridge else { return }
sentCommands.append(inputText.lowercased())
do {
if let response = try await fuegoBridge.submitCommand(inputText.lowercased()) {
handleResponse(response)
}
} catch {
fuegoResponse = "error"
fuegoError = "\(error)" // error.localizedDescription
}
}
}
var body: some View {
VStack(spacing: 16) {
if fuegoBridge == nil {
Button("Start Engine") { Task { await startEngine() } }
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(10)
}
if fuegoBridge != nil {
TextField("Enter text here", text: $inputText)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Button("Submit") { submitCommand() }
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(10)
VStack {
ForEach(Array(sentCommands.enumerated()), id: \.offset) { index, command in
Text(command).font(.caption)
}
}
Text("Fuego Response: \(fuegoResponse)")
Text("Error: \(fuegoError)")
Button("Stop Engine") { Task { await stopEngine() } }
.padding()
}
}
.padding()
.onDisappear { Task { await stopEngine() } }
}
}