generated from CubeGPT/CubeAgents
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ui.py
344 lines (269 loc) · 8.27 KB
/
ui.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from cube_qgui.__init__ import CreateQGUI
from cube_qgui.banner_tools import *
from cube_qgui.notebook_tools import *
from playwright.sync_api import Playwright, sync_playwright
import os
import shutil
import uuid
from log_writer import logger
import config
import core
import build
# ---------- Functions ----------#
def open_config(args: dict):
"""
Opens the config file.
Args:
args (dict): A dictionary containing the necessary arguments.
Returns:
bool: Always True.
"""
os.system("notepad config.yaml")
return True
def save_apply_config(args: dict):
"""
Saves and applies the configuration.
Args:
args (dict): A dictionary containing the necessary arguments.
Returns:
bool: Always True.
"""
keys = ["API_KEY", "BASE_URL"]
for key in keys:
value = args[key].get()
if key == "ADVANCED_MODE":
value = True if value == 1 else False
else:
pass
config.edit_config(key, value)
config.load_config()
args["DevTool_CONFIG_API_KEY_DISPLAY"].set(f"CONFIG.API_KEY = {config.API_KEY}")
args["DevTools_CONFIG_BASE_URL_DISPLAY"].set(f"CONFIG.BASE_URL = {config.BASE_URL}")
return True
def load_config(args: dict):
"""
Loads the configuration.
Args:
args (dict): A dictionary containing the necessary arguments.
Returns:
bool: Always True.
"""
config.load_config()
args["API_KEY"].set(config.API_KEY)
args["BASE_URL"].set(config.BASE_URL)
return True
def print_args(args: dict):
"""
Prints the arguments.
Args:
args (dict): A dictionary containing the arguments.
Returns:
bool: Always True.
"""
for arg, v_fun in args.items():
print(f"Name: {arg}, Value: {v_fun.get()}")
return True
def raise_error(args: dict):
"""
Raises an error.
Args:
args (dict): A dictionary containing the arguments.
"""
raise Exception("This is a test error.")
# ---------- Generate Function ----------#
def generate(args: dict):
"""
Generates the plugin.
Args:
args (dict): A dictionary containing the arguments.
Returns:
bool: Always True.
"""
global error_msg, pkg_id_path
# Get user inputs
name = args["PluginName"].get()
description = args["PluginDescription"].get()
artifact_name = name.replace(" ", "")
package_id = f"org.cubegpt.{uuid.uuid4().hex[:8]}"
pkg_id_path = ""
for id in package_id.split("."):
pkg_id_path += id + "/"
logger(f"user_input -> name: {name}")
logger(f"user_input -> description: {description}")
logger(f"random_generate -> package_id: {package_id}")
logger(f"str_path -> pkg_id_path: {pkg_id_path}")
print("Generating plugin...")
codes = core.askgpt(
config.SYS_GEN.replace("%ARTIFACT_NAME%", artifact_name).replace(
"%PKG_ID_LST%", pkg_id_path
),
config.USR_GEN.replace("%DESCRIPTION", description),
config.GENERATION_MODEL,
)
logger(f"codes: {codes}")
core.response_to_action(codes)
print("Code generated. Building now...")
result = build.build_plugin(artifact_name)
if "BUILD SUCCESS" in result:
print(
f"Build complete. Find your plugin at 'codes/{artifact_name}/target/{artifact_name}.jar'"
)
elif "Compilation failure":
error_msg = result
print(
"Build failed. To pass the error to ChatGPT && let it fix, jump to the Fixing page and click the Fix button."
)
else:
print(
"Unknown error. Please check the logs && send the log to @BaimoQilin on discord."
)
return True
def fix(args: dict):
"""
Fixes the error.
Args:
args (dict): A dictionary containing the arguments.
Returns:
bool: Always True.
"""
artifact_name = args["PluginName"].get()
print("Passing the error to ChatGPT...")
files = [
f"codes/{artifact_name}/src/main/java/{pkg_id_path}Main.java",
f"codes/{artifact_name}/src/main/resources/plugin.yml",
f"codes/{artifact_name}/src/main/resources/config.yml",
f"codes/{artifact_name}/pom.xml",
]
ids = ["main_java", "plugin_yml", "config_yml", "pom_xml"]
main_java = None
plugin_yml = None
config_yml = None
pom_xml = None
for file in files:
with open(file, "r") as f:
code = f.read()
id = ids[files.index(file)]
globals()[id] = code
print("Generating...")
codes = core.askgpt(
config.SYS_FIX.replace("%ARTIFACT_NAME%", str(artifact_name)),
config.USR_FIX.replace("%MAIN_JAVA%", str(main_java))
.replace("%PLUGIN_YML%", str(plugin_yml))
.replace("%CONFIG_YML%", str(config_yml))
.replace("%POM_XML%", str(pom_xml))
.replave("%PKG_ID_LST%", pkg_id_path)
.replace("%P_ERROR_MSG%", str(error_msg)),
config.FIXING_MODEL,
)
shutil.rmtree(f"codes/{artifact_name}")
core.response_to_action(codes)
print("Code generated. Building now...")
result = build.build_plugin(artifact_name)
if "BUILD SUCCESS" in result:
print(
f"Build complete. Find your plugin at 'codes/{artifact_name}/target/{artifact_name}.jar'"
)
else:
print(
"Build failed again. Please check the logs && send the log to @BaimoQilin on discord."
)
return True
# ---------- Main Program ----------#
root = CreateQGUI(title="BukkitGPT-v3", tab_names=["Generate", "Settings", "DevTools"])
error_msg = None
logger("Starting program.")
# Initialize Core
core.initialize()
print("BukkitGPT v3 beta console running")
# Banner
root.add_banner_tool(GitHub("https://github.com/CubeGPT/BukkitGPT-v3"))
# Generate Page
root.add_notebook_tool(
InputBox(name="PluginName", default="ExamplePlugin", label_info="Plugin Name")
)
root.add_notebook_tool(
InputBox(
name="PluginDescription",
default="Send msg 'hello' to every joined player.",
label_info="Plugin Description",
)
)
root.add_notebook_tool(
RunButton(
bind_func=generate,
name="Generate",
text="Generate Plugin",
checked_text="Generating...",
tab_index=0,
)
)
# Fixing Page #
# root.add_notebook_tool(Label(name="Fixing_DESCRIPTION", text="This is a fixing page. If the build fails, click the Fix button to fix the error in the LATEST build.", tab_index=1))
# root.add_notebook_tool(RunButton(bind_func=fix, name="Fix", text="Fix", checked_text="Fixing...", tab_index=1))
# Settings Page
root.add_notebook_tool(
InputBox(name="API_KEY", default=config.API_KEY, label_info="API Key", tab_index=1)
)
root.add_notebook_tool(
InputBox(
name="BASE_URL", default=config.BASE_URL, label_info="BASE URL", tab_index=1
)
)
config_buttons = HorizontalToolsCombine(
[
BaseButton(
bind_func=save_apply_config,
name="Save & Apply Config",
text="Save & Apply",
tab_index=1,
),
BaseButton(
bind_func=load_config, name="Load Config", text="Load Config", tab_index=1
),
BaseButton(
bind_func=open_config,
name="Open Config",
text="Open Full Config",
tab_index=1,
),
]
)
root.add_notebook_tool(config_buttons)
# DevTools Page
root.add_notebook_tool(
Label(
name="DevTool_DESCRIPTION",
text="This is a testing page for developers. Ignore it if you are a normal user.",
tab_index=2,
)
)
root.add_notebook_tool(
Label(
name="DevTool_CONFIG_API_KEY_DISPLAY",
text=f"CONFIG.API_KEY = {config.API_KEY}",
tab_index=2,
)
)
root.add_notebook_tool(
Label(
name="DevTools_CONFIG_BASE_URL_DISPLAY",
text=f"CONFIG.BASE_URL = {config.BASE_URL}",
tab_index=2,
)
)
root.add_notebook_tool(
RunButton(bind_func=print_args, name="Print Args", text="Print Args", tab_index=2)
)
root.add_notebook_tool(
RunButton(
bind_func=raise_error, name="Raise Error", text="Raise Error", tab_index=2
)
)
# Sidebar
root.set_navigation_about(
author="CubeGPT Team",
version=config.VERSION_NUMBER,
github_url="https://github.com/CubeGPT/BukkitGPT-v3",
)
# Run
root.run()