Skip to content

Commit

Permalink
refactoring a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Oct 15, 2024
1 parent 420cc51 commit 50d48cd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down
26 changes: 23 additions & 3 deletions convex/games/mutations.ts → convex/games.ts
Original file line number Diff line number Diff line change
@@ -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() },
Expand All @@ -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,
Expand Down
23 changes: 0 additions & 23 deletions convex/games/actions.ts

This file was deleted.

20 changes: 15 additions & 5 deletions convex/openai.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 50d48cd

Please sign in to comment.