generated from CubeGPT/CubeAgents
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathui.py
393 lines (311 loc) · 9.54 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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) -> bool:
"""
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) -> bool:
"""
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) -> bool:
"""
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) -> bool:
"""
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) -> bool:
"""
Generates the plugin.
Args:
args (dict): A dictionary containing the necessary 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)
target_dir = f"codes/{artifact_name}/target"
jar_files = [f for f in os.listdir(target_dir) if f.endswith('.jar')]
if jar_files:
print(f"Build complete. Find your plugin at '{target_dir}/{jar_files[0]}'")
else:
error_msg = result
print("Build failed. This is because the code LLM generated has syntax errors. Please try again or switch to a better LLM like o1 or r1. IT IS NOT A BUG OF BUKKITGPT.")
return True
def edit(args: dict) -> bool:
"""
Edits the plugin.
Args:
args (dict): A dictionary containing the arguments.
Returns:
bool: Always True.
"""
# Get user inputs
original_jar = args["OriginalJAR"].get()
edit_request = args["EditRequest"].get()
# Get the decompiled path
decompiled_path = f"codes/decompiled/{original_jar.split('/')[-1].split('.')[0]}"
# Decompile the jar
core.decompile_jar(original_jar, decompiled_path)
# Generate a buildable maven folder in the decompiled path
os.makedirs(f"{decompiled_path}/src/main/java", exist_ok=True)
# Delete decompiled_path/summary.txt
summary_path = os.path.join(decompiled_path, "summary.txt")
if os.path.exists(summary_path):
os.remove(summary_path)
# Move decompiled_path/* to decompiled_path/src/main/java
for item in os.listdir(decompiled_path):
if item != 'src':
s = os.path.join(decompiled_path, item)
d = os.path.join(decompiled_path, "src/main/java", item)
if os.path.isdir(s):
shutil.move(s, d)
elif os.path.isfile(s):
shutil.move(s, d)
# Create empty pom.xml
with open(f"{decompiled_path}/pom.xml", "w") as f:
f.write("<!-- Replace with the pom.xml code -->")
# Generate request
code_text = core.code_to_text(decompiled_path)
response = core.askgpt(
config.SYS_EDIT,
config.USR_EDIT.replace("ORIGINAL_CODE", code_text).replace("REQUEST", edit_request),
config.GENERATION_MODEL,
disable_json_mode=True
)
# Extract the response
diffs = core.parse_edit_response(response)
logger(f"[DEBUG] Extracted diffs: {diffs}")
# Apply edit
response = core.apply_diff_changes(diffs, decompiled_path)
if response[0] == False:
error_message = response[1]
# TODO: Pass the error to the LLM and fix that
print(f"The diff LLM generated is invalid. Please try again or switch to a better LLM like o1 or r1. IT IS NOT A BUG OF BUKKITGPT.")
else:
print("Edit complete. Recompiling...")
result = build.build_plugin(decompiled_path, path=True)
target_dir = f"{decompiled_path}/target"
jar_files = [f for f in os.listdir(target_dir) if f.endswith('.jar')]
if jar_files:
print(f"Build complete. Find your plugin at '{target_dir}/{jar_files[0]}'")
else:
error_msg = result
print("Build failed. This is because the code LLM generated has syntax errors. Please try again or switch to a better LLM like o1 or r1. IT IS NOT A BUG OF BUKKITGPT.")
return True
# ---------- Main Program ----------#
root = CreateQGUI(title="BukkitGPT-v3",
tab_names=["Generate", "Edit", "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,
)
)
# Edit Page #
root.add_notebook_tool(
ChooseFileTextButton(
name="OriginalJAR",
label_info="Original JAR",
tab_index=1
)
)
root.add_notebook_tool(
InputBox(
name="EditRequest",
default="Add a command to send a message to all players.",
label_info="Edit Request",
tab_index=1
)
)
root.add_notebook_tool(
RunButton(
bind_func=edit,
name="Edit",
text="Edit Plugin",
checked_text="Editing...",
tab_index=1
)
)
# Settings Page
root.add_notebook_tool(
InputBox(
name="API_KEY",
default=config.API_KEY,
label_info="API Key",
tab_index=2
)
)
root.add_notebook_tool(
InputBox(
name="BASE_URL",
default=config.BASE_URL,
label_info="BASE URL",
tab_index=2
)
)
config_buttons = HorizontalToolsCombine(
[
BaseButton(
bind_func=save_apply_config,
name="Save & Apply Config",
text="Save & Apply",
tab_index=2
),
BaseButton(
bind_func=load_config,
name="Load Config",
text="Load Config",
tab_index=2
),
BaseButton(
bind_func=open_config,
name="Open Config",
text="Open Full Config",
tab_index=2
),
]
)
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=3
)
)
root.add_notebook_tool(
Label(
name="DevTool_CONFIG_API_KEY_DISPLAY",
text=f"CONFIG.API_KEY = {config.API_KEY}",
tab_index=3
)
)
root.add_notebook_tool(
Label(
name="DevTools_CONFIG_BASE_URL_DISPLAY",
text=f"CONFIG.BASE_URL = {config.BASE_URL}",
tab_index=3
)
)
root.add_notebook_tool(
RunButton(
bind_func=print_args, name="Print Args", text="Print Args", tab_index=3
)
)
root.add_notebook_tool(
RunButton(
bind_func=raise_error, name="Raise Error", text="Raise Error", tab_index=3
)
)
# Sidebar
root.set_navigation_about(
author="CubeGPT Team",
version=config.VERSION_NUMBER,
github_url="https://github.com/CubeGPT/BukkitGPT-v3",
)
# Run
root.run()