NERDDISCO commited on
Commit
684a661
•
1 Parent(s): 1c782e2

feat: better error handling when interacting with OpenAI, moved examples around, use env to load version

Browse files
Files changed (1) hide show
  1. src/components/GameCreator.tsx +127 -72
src/components/GameCreator.tsx CHANGED
@@ -1,6 +1,6 @@
1
  import { useEffect, useMemo, useRef, useState } from "react";
2
 
3
- import { AxiosError } from "axios";
4
  import AcUnitIcon from "@mui/icons-material/AcUnit";
5
  import LocalFireDepartmentIcon from "@mui/icons-material/LocalFireDepartment";
6
  import CheckIcon from "@mui/icons-material/Check";
@@ -65,6 +65,7 @@ import Secret from "@/components/base/secret";
65
  import { toOpenAI } from "@/services/api";
66
  import { createClient } from "@/services/api/openai";
67
  import { RainbowListItemButton } from "./base/boxes";
 
68
  const MonacoEditor = dynamic(import("@monaco-editor/react"), { ssr: false });
69
 
70
  export interface ShareProps {
@@ -88,7 +89,7 @@ export default function GameCreator() {
88
 
89
  const { mode, systemMode } = useColorScheme();
90
 
91
- const { call, subscribe } = useHost(ref, "2DGameGPT");
92
 
93
  const connection = useRef(false);
94
  const [tries, setTries] = useState(1);
@@ -160,6 +161,56 @@ export default function GameCreator() {
160
  signal: abortController.current.signal,
161
  });
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  setAnswers(previousAnswers => [answer, ...previousAnswers]);
164
  setRunningId(answer.id);
165
  setActiveId(answer.id);
@@ -168,8 +219,9 @@ export default function GameCreator() {
168
  reload();
169
  } catch (error) {
170
  if ((error as { message?: string }).message !== "canceled") {
171
- setErrorMessage((error as AxiosError).message);
172
- console.error(error);
 
173
  }
174
  } finally {
175
  setLoading(false);
@@ -212,7 +264,9 @@ export default function GameCreator() {
212
  inset: 0,
213
  overflow: "hidden",
214
  flexDirection: { md: "row" },
215
- height: "95vh",
 
 
216
  }}
217
  >
218
  <Stack
@@ -231,7 +285,7 @@ export default function GameCreator() {
231
  </Typography>
232
 
233
  <Typography variant="body2" sx={{ ml: 1 }}>
234
- v1.0.0
235
  </Typography>
236
  </Stack>
237
 
@@ -296,7 +350,7 @@ export default function GameCreator() {
296
  <Stack sx={{ p: 1, pl: 0, gap: 1 }}>
297
  <Secret label="OpenAI API Key" name="openAIAPIKey" />
298
 
299
- <Stack direction="row" spacing={1}>
300
  <TextField
301
  multiline
302
  fullWidth
@@ -313,8 +367,12 @@ export default function GameCreator() {
313
  }}
314
  />
315
 
316
- <Stack spacing={1}>
317
- <FormControl variant="outlined" sx={{ minWidth: 180 }}>
 
 
 
 
318
  <InputLabel id="gpt-command-select-label">
319
  Command
320
  </InputLabel>
@@ -345,8 +403,7 @@ export default function GameCreator() {
345
 
346
  <ButtonGroup
347
  variant="contained"
348
- fullWidth
349
- sx={{ flexGrow: 1, maxHeight: "96px" }}
350
  >
351
  <Button
352
  form="gpt-form"
@@ -386,45 +443,6 @@ export default function GameCreator() {
386
 
387
  {errorMessage && <Alert severity="error">{errorMessage}</Alert>}
388
 
389
- <Stack direction="row" spacing={1} alignItems="center">
390
- <Typography>Examples</Typography>
391
-
392
- <ExampleButton
393
- title={"Space Invaders"}
394
- text={
395
- "Space Invaders. Single player ship at the bottom, a row of invaders at the top moving side-to-side and descending. The player ship can move left and right, and shoot bullets to destroy the invaders. Handle game over scenario when an invader reaches the player's level or all invaders are dead. Collision detection for both invaders and player. "
396
- }
397
- onClick={setPrompt}
398
- />
399
-
400
- <ExampleButton
401
- title="Jump & Run"
402
- text={
403
- "Jump & Run. Player collects coins to enter the next level. Various platform heights. Gras platform covers the whole ground. Space key for jumping, arrow keys for movement."
404
- }
405
- onClick={setPrompt}
406
- />
407
-
408
- <ExampleButton
409
- title="Flappy Bird"
410
- text={
411
- "Flappy Bird. Intro screen, start the game by pressing space key. Bird starts flying on the left center of the screen. Gradually falls slowly. Pressing the space key over and over lets the bird fly higher. Pipes move from the right of the screen to the left. The pipes have a huge opening so that the bird can easily fly through. Collision detection when the bird hits the ground or a pipe. When collision detected, then show intro screen. Player gets a point for each passed pipe without an collision. Score is shown in the top left while bird is flying. High score on intro screen."
412
- }
413
- onClick={setPrompt}
414
- />
415
-
416
- <ExampleButton
417
- title="Asteroids"
418
- text={
419
- "Asteroids. Control spaceship-movement using arrow keys. Fire bullets with space key to destroy asteroids, breaking them into smaller pieces. Earn points for destroying asteroids, with higher scores for smaller ones. Collision detection when spaceship hits asteroid, collision reduces spaceship health, game over when health is 0."
420
- }
421
- // text={
422
- // "Asteroids. Space ship can fly around in space using the arrow keys. Irregular shaped objects called asteroids flying around the space ship. The space ship can shoot bullets using the space key. When a bullet hits an asteroid, it splits in smaller irregular shaped objects; when asteroid is completely destroyed, the player scores one point. When the space ship collides with an asteroid, it looses 1 health; when the health is 0, the game is over. Restart the game via pressing space key."
423
- // }
424
- onClick={setPrompt}
425
- />
426
- </Stack>
427
-
428
  <Paper variant="outlined">
429
  <Accordion disableGutters square elevation={0}>
430
  <AccordionSummary
@@ -439,29 +457,27 @@ export default function GameCreator() {
439
  <Typography>Options</Typography>
440
  </AccordionSummary>
441
  <AccordionDetails>
442
- <Stack gap={2}>
443
- <FormControl
444
- fullWidth
445
- variant="outlined"
446
- sx={{ mb: 3 }}
 
 
 
 
 
 
 
 
 
447
  >
448
- <InputLabel id="gpt-model-select-label">
449
- Model
450
- </InputLabel>
451
- <Select
452
- labelId="gpt-model-select-label"
453
- id="gpt-model-select"
454
- name="model"
455
- defaultValue="gpt-3.5-turbo"
456
- label="Model"
457
- >
458
- <MenuItem value="gpt-3.5-turbo">
459
- GPT 3.5 turbo
460
- </MenuItem>
461
- <MenuItem value="gpt-4">GPT 4</MenuItem>
462
- </Select>
463
- </FormControl>
464
- </Stack>
465
  <Stack
466
  spacing={2}
467
  direction="row"
@@ -514,6 +530,45 @@ export default function GameCreator() {
514
  </AccordionDetails>
515
  </Accordion>
516
  </Paper>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
  </Stack>
518
  </Box>
519
 
 
1
  import { useEffect, useMemo, useRef, useState } from "react";
2
 
3
+ import axios, { AxiosError } from "axios";
4
  import AcUnitIcon from "@mui/icons-material/AcUnit";
5
  import LocalFireDepartmentIcon from "@mui/icons-material/LocalFireDepartment";
6
  import CheckIcon from "@mui/icons-material/Check";
 
65
  import { toOpenAI } from "@/services/api";
66
  import { createClient } from "@/services/api/openai";
67
  import { RainbowListItemButton } from "./base/boxes";
68
+ import { CustomAxiosError } from "@/services/api/axios";
69
  const MonacoEditor = dynamic(import("@monaco-editor/react"), { ssr: false });
70
 
71
  export interface ShareProps {
 
89
 
90
  const { mode, systemMode } = useColorScheme();
91
 
92
+ const { call, subscribe } = useHost(ref, "2DGameCreator");
93
 
94
  const connection = useRef(false);
95
  const [tries, setTries] = useState(1);
 
161
  signal: abortController.current.signal,
162
  });
163
 
164
+ setAnswers(previousAnswers => [answer, ...previousAnswers]);
165
+ setRunningId(answer.id);
166
+ setActiveId(answer.id);
167
+ setTemplate(prettify(answer.content));
168
+ setErrorMessage("");
169
+ reload();
170
+ } catch (error) {
171
+ const err = error as CustomAxiosError;
172
+ console.error(err);
173
+
174
+ let errorMessage = "";
175
+
176
+ // If error is not canceled (from AbortController)
177
+ if (err.message !== "canceled") {
178
+ // If we have an error message from the data.error.message, use that
179
+ if (err.data?.error?.message && err.data.error.message !== "") {
180
+ errorMessage = err.data.error.message;
181
+ }
182
+ // If there's no message but there's a code, use the code
183
+ else if (err.data?.error?.code) {
184
+ errorMessage = err.data.error.code;
185
+ }
186
+ // If there's neither a message nor a code, use the error's own message
187
+ else if (err.message) {
188
+ errorMessage = err.message;
189
+ } else {
190
+ errorMessage = "UNKNOWN_ERROR";
191
+ }
192
+ }
193
+
194
+ setErrorMessage(errorMessage);
195
+ } finally {
196
+ setLoading(false);
197
+ }
198
+ };
199
+
200
+ const handleSubmitServer = async (event: React.FormEvent<HTMLFormElement>) => {
201
+ event.preventDefault();
202
+ const formData = new FormData(event.target as HTMLFormElement);
203
+ const formObject = Object.fromEntries(formData);
204
+ try {
205
+ setLoading(true);
206
+
207
+ abortController.current = new AbortController();
208
+
209
+ const { data } = await axios.post("/api/generate", formObject, {
210
+ signal: abortController.current.signal,
211
+ });
212
+ const answer = data;
213
+
214
  setAnswers(previousAnswers => [answer, ...previousAnswers]);
215
  setRunningId(answer.id);
216
  setActiveId(answer.id);
 
219
  reload();
220
  } catch (error) {
221
  if ((error as { message?: string }).message !== "canceled") {
222
+ const err = error as AxiosError;
223
+ console.error(err);
224
+ setErrorMessage(err.response?.data?.message ?? err.message);
225
  }
226
  } finally {
227
  setLoading(false);
 
264
  inset: 0,
265
  overflow: "hidden",
266
  flexDirection: { md: "row" },
267
+ height: {
268
+ md: "95vh",
269
+ },
270
  }}
271
  >
272
  <Stack
 
285
  </Typography>
286
 
287
  <Typography variant="body2" sx={{ ml: 1 }}>
288
+ {process.env.NEXT_PUBLIC_VERSION}
289
  </Typography>
290
  </Stack>
291
 
 
350
  <Stack sx={{ p: 1, pl: 0, gap: 1 }}>
351
  <Secret label="OpenAI API Key" name="openAIAPIKey" />
352
 
353
+ <Stack direction="column" spacing={1}>
354
  <TextField
355
  multiline
356
  fullWidth
 
367
  }}
368
  />
369
 
370
+ <Stack
371
+ spacing={1}
372
+ direction="row"
373
+ sx={{ justifyContent: "end" }}
374
+ >
375
+ <FormControl variant="outlined" sx={{ minWidth: 200 }}>
376
  <InputLabel id="gpt-command-select-label">
377
  Command
378
  </InputLabel>
 
403
 
404
  <ButtonGroup
405
  variant="contained"
406
+ sx={{ maxHeight: "96px" }}
 
407
  >
408
  <Button
409
  form="gpt-form"
 
443
 
444
  {errorMessage && <Alert severity="error">{errorMessage}</Alert>}
445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  <Paper variant="outlined">
447
  <Accordion disableGutters square elevation={0}>
448
  <AccordionSummary
 
457
  <Typography>Options</Typography>
458
  </AccordionSummary>
459
  <AccordionDetails>
460
+ <FormControl
461
+ fullWidth
462
+ variant="outlined"
463
+ sx={{ mb: 3 }}
464
+ >
465
+ <InputLabel id="gpt-model-select-label">
466
+ Model
467
+ </InputLabel>
468
+ <Select
469
+ labelId="gpt-model-select-label"
470
+ id="gpt-model-select"
471
+ name="model"
472
+ defaultValue="gpt-3.5-turbo"
473
+ label="Model"
474
  >
475
+ <MenuItem value="gpt-3.5-turbo">
476
+ GPT 3.5 turbo
477
+ </MenuItem>
478
+ <MenuItem value="gpt-4">GPT 4</MenuItem>
479
+ </Select>
480
+ </FormControl>
 
 
 
 
 
 
 
 
 
 
 
481
  <Stack
482
  spacing={2}
483
  direction="row"
 
530
  </AccordionDetails>
531
  </Accordion>
532
  </Paper>
533
+
534
+ <Stack direction="row" spacing={1} alignItems="center">
535
+ <Typography>Examples</Typography>
536
+
537
+ <ExampleButton
538
+ title={"Space Invaders"}
539
+ text={
540
+ "Space Invaders. Single player ship at the bottom, a row of invaders at the top moving side-to-side and descending. The player ship can move left and right, and shoot bullets to destroy the invaders. Handle game over scenario when an invader reaches the player's level or all invaders are dead. Collision detection for both invaders and player. "
541
+ }
542
+ onClick={setPrompt}
543
+ />
544
+
545
+ {/* <ExampleButton
546
+ title="Jump & Run"
547
+ text={
548
+ "Jump & Run. Player collects coins to enter the next level. Various platform heights. Gras platform covers the whole ground. Space key for jumping, arrow keys for movement."
549
+ }
550
+ onClick={setPrompt}
551
+ /> */}
552
+
553
+ <ExampleButton
554
+ title="Flappy Bird"
555
+ text={
556
+ "Flappy Bird. Intro screen, start the game by pressing space key. Bird starts flying on the left center of the screen. Gradually falls slowly. Pressing the space key over and over lets the bird fly higher. Pipes move from the right of the screen to the left. The pipes have a huge opening so that the bird can easily fly through. Collision detection when the bird hits the ground or a pipe. When collision detected, then show intro screen. Player gets a point for each passed pipe without an collision. Score is shown in the top left while bird is flying. High score on intro screen."
557
+ }
558
+ onClick={setPrompt}
559
+ />
560
+
561
+ <ExampleButton
562
+ title="Asteroids"
563
+ text={
564
+ "Asteroids. Control spaceship-movement using arrow keys. Fire bullets with space key to destroy asteroids, breaking them into smaller pieces. Earn points for destroying asteroids, with higher scores for smaller ones. Collision detection when spaceship hits asteroid, collision reduces spaceship health, game over when health is 0."
565
+ }
566
+ // text={
567
+ // "Asteroids. Space ship can fly around in space using the arrow keys. Irregular shaped objects called asteroids flying around the space ship. The space ship can shoot bullets using the space key. When a bullet hits an asteroid, it splits in smaller irregular shaped objects; when asteroid is completely destroyed, the player scores one point. When the space ship collides with an asteroid, it looses 1 health; when the health is 0, the game is over. Restart the game via pressing space key."
568
+ // }
569
+ onClick={setPrompt}
570
+ />
571
+ </Stack>
572
  </Stack>
573
  </Box>
574