Skip to content

Commit 7123807

Browse files
committed
throw error if no OpenAI API key is set
1 parent c7ca392 commit 7123807

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

app/package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/client/app/DemoAppPage.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
getAllTasksByUser,
1010
} from 'wasp/client/operations';
1111

12-
import { useState, useEffect, useMemo } from 'react';
12+
import { useState, useMemo } from 'react';
1313
import { CgSpinner } from 'react-icons/cg';
1414
import { TiDelete } from 'react-icons/ti';
1515
import { type GeneratedSchedule } from '../../shared/types';
16-
import { MainTask, Subtask } from '@wasp/shared/types';
16+
import { MainTask, Subtask } from '../../shared/types';
1717

1818
export default function DemoAppPage() {
1919
return (
@@ -282,7 +282,7 @@ function TaskTable({ schedule }: { schedule: GeneratedSchedule }) {
282282
<table className='table-auto w-full border-separate border border-spacing-2 rounded-md border-slate-200 shadow-sm'>
283283
{!!schedule.mainTasks ? (
284284
schedule.mainTasks
285-
.map((mainTask) => <MainTask key={mainTask.name} mainTask={mainTask} subtasks={schedule.subtasks} />)
285+
.map((mainTask) => <MainTaskTable key={mainTask.name} mainTask={mainTask} subtasks={schedule.subtasks} />)
286286
.sort((a, b) => {
287287
const priorityOrder = ['low', 'medium', 'high'];
288288
if (a.props.mainTask.priority && b.props.mainTask.priority) {
@@ -303,7 +303,7 @@ function TaskTable({ schedule }: { schedule: GeneratedSchedule }) {
303303
);
304304
}
305305

306-
function MainTask({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtask[] }) {
306+
function MainTaskTable({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtask[] }) {
307307
return (
308308
<>
309309
<thead>
@@ -337,7 +337,7 @@ function MainTask({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtas
337337
: 'bg-yellow-50'
338338
}`}
339339
>
340-
<Subtask description={subtask.description} time={subtask.time} />
340+
<SubtaskTable description={subtask.description} time={subtask.time} />
341341
</td>
342342
</tr>
343343
</tbody>
@@ -351,7 +351,7 @@ function MainTask({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtas
351351
);
352352
}
353353

354-
function Subtask({ description, time }: { description: string; time: number }) {
354+
function SubtaskTable({ description, time }: { description: string; time: number }) {
355355
const [isDone, setIsDone] = useState<boolean>(false);
356356

357357
const convertHrsToMinutes = (time: number) => {

app/src/server/actions.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ import {
1111
type CreateFile,
1212
} from 'wasp/server/operations';
1313
import Stripe from 'stripe';
14-
import fetch from 'node-fetch';
15-
import type { StripePaymentResult } from './types';
14+
import type { GeneratedSchedule, StripePaymentResult } from '../shared/types';
1615
import { fetchStripeCustomer, createStripeCheckoutSession } from './payments/stripeUtils.js';
1716
import { TierIds } from '../shared/constants.js';
1817
import { getUploadFileSignedURLFromS3 } from './file-upload/s3Utils.js';
1918
import OpenAI from 'openai';
2019

21-
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });
20+
const openai = setupOpenAI();
21+
function setupOpenAI() {
22+
if (!process.env.OPENAI_API_KEY) {
23+
return new HttpError(500, 'OpenAI API key is not set');
24+
}
25+
return new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
26+
}
2227

2328
export const stripePayment: StripePayment<string, StripePaymentResult> = async (tier, context) => {
2429
if (!context.user) {
@@ -106,6 +111,11 @@ export const generateGptResponse: GenerateGptResponse<GptPayload, GeneratedSched
106111
});
107112
}
108113

114+
// check if openai is initialized correctly with the API key
115+
if (openai instanceof Error) {
116+
throw openai;
117+
}
118+
109119
const completion = await openai.chat.completions.create({
110120
model: 'gpt-3.5-turbo',
111121
messages: [

app/src/shared/types.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
import { User } from '@wasp/entities';
2-
import { Prisma } from '@prisma/client';
3-
4-
export type Context = {
5-
user: User;
6-
entities: {
7-
User: Prisma.UserDelegate<{}>;
8-
};
9-
};
10-
111
export type StripePaymentResult = {
122
sessionUrl: string | null;
133
sessionId: string;

0 commit comments

Comments
 (0)