From 50d48cd9f3aa995a5d7d5490d114c5d0ebbbf750 Mon Sep 17 00:00:00 2001 From: Cody Seibert Date: Tue, 15 Oct 2024 14:41:31 -0400 Subject: [PATCH] refactoring a bit --- convex/_generated/api.d.ts | 2 ++ convex/{games/mutations.ts => games.ts} | 26 ++++++++++++++++++++++--- convex/games/actions.ts | 23 ---------------------- convex/openai.ts | 20 ++++++++++++++----- 4 files changed, 40 insertions(+), 31 deletions(-) rename convex/{games/mutations.ts => games.ts} (60%) delete mode 100644 convex/games/actions.ts diff --git a/convex/_generated/api.d.ts b/convex/_generated/api.d.ts index b1983a9..2464f9b 100644 --- a/convex/_generated/api.d.ts +++ b/convex/_generated/api.d.ts @@ -16,6 +16,7 @@ import type { FunctionReference, } from "convex/server"; import type * as auth from "../auth.js"; +import type * as games from "../games.js"; import type * as http from "../http.js"; import type * as messages from "../messages.js"; import type * as openai from "../openai.js"; @@ -31,6 +32,7 @@ import type * as users from "../users.js"; */ declare const fullApi: ApiFromModules<{ auth: typeof auth; + games: typeof games; http: typeof http; messages: typeof messages; openai: typeof openai; diff --git a/convex/games/mutations.ts b/convex/games.ts similarity index 60% rename from convex/games/mutations.ts rename to convex/games.ts index d34af42..9ecec66 100644 --- a/convex/games/mutations.ts +++ b/convex/games.ts @@ -1,6 +1,26 @@ import { v } from "convex/values"; -import { internal } from "../_generated/api"; -import { internalMutation } from "../_generated/server"; +import { internalAction, internalMutation } from "./_generated/server"; +import { internal } from "./_generated/api"; + +export const playMapAction = internalAction({ + args: { + mapId: v.id("maps"), + gameId: v.id("games"), + modelId: v.string(), + level: v.number(), + }, + handler: async (ctx, args) => { + await ctx.runMutation(internal.games.insertScore, { + modelId: args.modelId, + gameId: args.gameId, + level: args.level, + }); + + console.log( + `Playing map ${args.mapId} for game ${args.gameId} at level ${args.level}`, + ); + }, +}); export const createNewGame = internalMutation({ args: { modelId: v.string() }, @@ -21,7 +41,7 @@ export const createNewGame = internalMutation({ } // not sure if this is the way to do this - await ctx.scheduler.runAfter(0, internal.games.actions.playMapAction, { + await ctx.scheduler.runAfter(0, internal.games.playMapAction, { mapId: firstMap._id, gameId, modelId: args.modelId, diff --git a/convex/games/actions.ts b/convex/games/actions.ts deleted file mode 100644 index 76ca3b9..0000000 --- a/convex/games/actions.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { v } from "convex/values"; -import { internal } from "../_generated/api"; -import { internalAction } from "../_generated/server"; - -export const playMapAction = internalAction({ - args: { - mapId: v.id("maps"), - gameId: v.id("games"), - modelId: v.string(), - level: v.number(), - }, - handler: async (ctx, args) => { - await ctx.runMutation(internal.games.mutations.insertScore, { - modelId: args.modelId, - gameId: args.gameId, - level: args.level, - }); - - console.log( - `Playing map ${args.mapId} for game ${args.gameId} at level ${args.level}`, - ); - }, -}); diff --git a/convex/openai.ts b/convex/openai.ts index 4b0a65f..3cbb5d5 100644 --- a/convex/openai.ts +++ b/convex/openai.ts @@ -1,25 +1,35 @@ import OpenAI from "openai"; -import { action, query } from "./_generated/server"; +import { action } from "./_generated/server"; import { v } from "convex/values"; import { z } from "zod"; import { zodResponseFormat } from "openai/helpers/zod"; -const openai = new OpenAI(); - const ResponseSchema = z.object({ map: z.array(z.array(z.number())), reasoning: z.string(), playerCoordinates: z.array(z.number()), - BoxCoordinates: z.array(z.array(z.number())), + boxCoordinates: z.array(z.array(z.number())), }); -export const playerMap = action({ +export const playMapAction = action({ args: { map: v.array(v.array(v.number())), mapId: v.string(), level: v.number(), }, handler: async (_, args) => { + if (process.env.MOCK_OPEN_AI) { + return { + map: args.map, + reasoning: "This is a mock response", + playerCoordinates: [0, 0], + boxCoordinates: [], + }; + } + + // moving here for now so that I can get this deployed without a real key + const openai = new OpenAI(); + try { const completion = await openai.beta.chat.completions.parse({ model: "gpt-4o-2024-08-06",