Skip to content

Commit

Permalink
Merge pull request #45 from webdevcody/feat-error-handling
Browse files Browse the repository at this point in the history
Proper error handling/Display AI model reasoning
  • Loading branch information
webdevcody authored Oct 16, 2024
2 parents 52471c2 + 1fd7524 commit bb45e47
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
27 changes: 18 additions & 9 deletions app/games/[gameId]/result.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
"use client";

import { Visualizer } from "./visualizer";
import { api } from "@/convex/_generated/api";
import { Doc } from "@/convex/_generated/dataModel";
import { api } from "@/convex/_generated/api";
import { useQuery } from "convex/react";
import { Visualizer } from "./visualizer";

export const Result = ({ result }: { result: Doc<"results"> }) => {
const map = useQuery(api.maps.getMapByLevel, {
level: result.level,
});

if (!map) return null;
if (!map) {
return <div>Map was not found</div>;
}

if (result.status === "inProgress") {
return <div>Game in progress...</div>;
}

if (result.status === "failed") {
return <div className="text-red-500">{result.error}</div>;
}

return (
<div className="flex items-center gap-8" key={map._id}>
<div>Level {map.level}</div>
<div className="flex items-center gap-8">
<div className="whitespace-nowrap">Level {map.level}</div>
<Visualizer map={result.map} />
<div
className={`font-bold ${result.isWin ? "text-green-500" : "text-red-500"}`}
>
{result.isWin ? "Won" : "Lost"}
<div className="flex flex-col">
<div
className={`font-bold ${result.isWin ? "text-green-500" : "text-red-500"}`}
>
{result.isWin ? "Won" : "Lost"}
</div>
{result.reasoning !== "" && <p>{result.reasoning}</p>}
</div>
</div>
);
Expand Down
32 changes: 13 additions & 19 deletions convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,9 @@ export const playMapAction = internalAction({
existingMap[0][1] = "B";
existingMap[0][2] = "B";

const game = new ZombieSurvival(existingMap);

while (!game.finished()) {
game.step();
}

const isWin = !game.getPlayer().dead();

await ctx.runMutation(internal.results.updateResult, {
resultId,
isWin,
isWin: ZombieSurvival.isWin(existingMap),
reasoning: "This is a mock response",
map: existingMap,
});
Expand All @@ -159,23 +151,25 @@ export const playMapAction = internalAction({

try {
const { solution, reasoning } = await runModel(args.modelId, map.grid);
const game = new ZombieSurvival(solution);

while (!game.finished()) {
game.step();
}

const isWin = !game.getPlayer().dead();

await ctx.runMutation(internal.results.updateResult, {
resultId,
isWin,
isWin: ZombieSurvival.isWin(solution),
reasoning,
map: solution,
});
} catch (error) {
// todo: handle error
console.log(error);
const errorMessage =
error instanceof Error
? error.message
: typeof error === "string"
? error
: "Unexpected error happened";

await ctx.runMutation(internal.results.failResult, {
resultId,
error: errorMessage,
});
}
},
});
19 changes: 19 additions & 0 deletions convex/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ export const createInitialResult = internalMutation({
},
});

export const failResult = internalMutation({
args: {
resultId: v.id("results"),
error: v.string(),
},
handler: async (ctx, args) => {
const result = await ctx.db.get(args.resultId);

if (!result) {
throw new Error("Result not found");
}

await ctx.db.patch(args.resultId, {
error: args.error,
status: "failed",
});
},
});

export const updateResult = internalMutation({
args: {
resultId: v.id("results"),
Expand Down
7 changes: 6 additions & 1 deletion convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export default defineSchema({
level: v.number(),
isWin: v.boolean(),
reasoning: v.string(),
error: v.optional(v.string()),
map: v.array(v.array(v.string())),
status: v.union(v.literal("inProgress"), v.literal("completed")),
status: v.union(
v.literal("inProgress"),
v.literal("completed"),
v.literal("failed"),
),
}).index("by_gameId_level", ["gameId", "level"]),
});

0 comments on commit bb45e47

Please sign in to comment.