Skip to content

Commit

Permalink
throw error if no OpenAI API key is set
Browse files Browse the repository at this point in the history
vincanger committed Feb 27, 2024
1 parent c7ca392 commit 7123807
Showing 4 changed files with 21 additions and 23 deletions.
6 changes: 2 additions & 4 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions app/src/client/app/DemoAppPage.tsx
Original file line number Diff line number Diff line change
@@ -9,11 +9,11 @@ import {
getAllTasksByUser,
} from 'wasp/client/operations';

import { useState, useEffect, useMemo } from 'react';
import { useState, useMemo } from 'react';
import { CgSpinner } from 'react-icons/cg';
import { TiDelete } from 'react-icons/ti';
import { type GeneratedSchedule } from '../../shared/types';
import { MainTask, Subtask } from '@wasp/shared/types';
import { MainTask, Subtask } from '../../shared/types';

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

function MainTask({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtask[] }) {
function MainTaskTable({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtask[] }) {
return (
<>
<thead>
@@ -337,7 +337,7 @@ function MainTask({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtas
: 'bg-yellow-50'
}`}
>
<Subtask description={subtask.description} time={subtask.time} />
<SubtaskTable description={subtask.description} time={subtask.time} />
</td>
</tr>
</tbody>
@@ -351,7 +351,7 @@ function MainTask({ mainTask, subtasks }: { mainTask: MainTask; subtasks: Subtas
);
}

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

const convertHrsToMinutes = (time: number) => {
16 changes: 13 additions & 3 deletions app/src/server/actions.ts
Original file line number Diff line number Diff line change
@@ -11,14 +11,19 @@ import {
type CreateFile,
} from 'wasp/server/operations';
import Stripe from 'stripe';
import fetch from 'node-fetch';
import type { StripePaymentResult } from './types';
import type { GeneratedSchedule, StripePaymentResult } from '../shared/types';
import { fetchStripeCustomer, createStripeCheckoutSession } from './payments/stripeUtils.js';
import { TierIds } from '../shared/constants.js';
import { getUploadFileSignedURLFromS3 } from './file-upload/s3Utils.js';
import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });
const openai = setupOpenAI();
function setupOpenAI() {
if (!process.env.OPENAI_API_KEY) {
return new HttpError(500, 'OpenAI API key is not set');
}
return new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
}

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

// check if openai is initialized correctly with the API key
if (openai instanceof Error) {
throw openai;
}

const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
10 changes: 0 additions & 10 deletions app/src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import { User } from '@wasp/entities';
import { Prisma } from '@prisma/client';

export type Context = {
user: User;
entities: {
User: Prisma.UserDelegate<{}>;
};
};

export type StripePaymentResult = {
sessionUrl: string | null;
sessionId: string;

0 comments on commit 7123807

Please sign in to comment.