From 5f24274c812f7146e8a2e669bb5d39580220bf6a Mon Sep 17 00:00:00 2001 From: joel Date: Mon, 8 Jan 2024 16:38:37 -0800 Subject: [PATCH] chore: remove "tip writer" --- .../app/tips/_components/tip-assistant.tsx | 22 ---- .../src/inngest/functions/ai/tip-writer.ts | 102 ------------------ .../src/inngest/inngest.config.ts | 2 - .../src/inngest/inngest.server.ts | 3 - .../src/trpc/api/routers/tips.ts | 24 +---- 5 files changed, 1 insertion(+), 152 deletions(-) delete mode 100644 apps/course-builder-web/src/inngest/functions/ai/tip-writer.ts diff --git a/apps/course-builder-web/src/app/tips/_components/tip-assistant.tsx b/apps/course-builder-web/src/app/tips/_components/tip-assistant.tsx index 0f4676e3c..b0b9ce494 100644 --- a/apps/course-builder-web/src/app/tips/_components/tip-assistant.tsx +++ b/apps/course-builder-web/src/app/tips/_components/tip-assistant.tsx @@ -23,9 +23,6 @@ export function TipAssistant({tip}: {tip: Tip}) { const textareaRef = useRef(null) - const {mutateAsync: generateTitle, status: generateTitleStatus} = - api.tips.generateTitle.useMutation() - useSocket({ room: tip._id, onMessage: (messageEvent) => { @@ -54,7 +51,6 @@ export function TipAssistant({tip}: {tip: Tip}) { ref={textareaRef} className="w-full rounded-none border-0 border-b px-5 py-4 pr-10" placeholder="Type a message..." - disabled={generateTitleStatus === 'loading'} rows={4} onKeyDown={async (event) => { if (event.key === 'Enter' && !event.shiftKey) { @@ -97,24 +93,6 @@ export function TipAssistant({tip}: {tip: Tip}) { - -
-

Actions

- -
) diff --git a/apps/course-builder-web/src/inngest/functions/ai/tip-writer.ts b/apps/course-builder-web/src/inngest/functions/ai/tip-writer.ts deleted file mode 100644 index 73c80c82b..000000000 --- a/apps/course-builder-web/src/inngest/functions/ai/tip-writer.ts +++ /dev/null @@ -1,102 +0,0 @@ -import {inngest} from '@/inngest/inngest.server' -import { - AI_TIP_WRITING_REQUESTED_EVENT, - AI_WRITING_COMPLETED_EVENT, -} from '@/inngest/events' -import {type ChatCompletionRequestMessage} from 'openai-edge' -import {sanityQuery} from '@/server/sanity.server' -import {last} from 'lodash' -import {env} from '@/env.mjs' -import {promptActionExecutor} from '@/lib/prompt.action-executor' -import {titles} from '@/inngest/functions/ai/data/titles' -import {type Tip} from '@/lib/tips' -import {type VideoResource} from '@/inngest/functions/transcript-ready' - -export const tipTitleAndSummaryWriter = inngest.createFunction( - {id: `gpt-4-tip-writer`, name: 'GPT-4 Writer'}, - {event: AI_TIP_WRITING_REQUESTED_EVENT}, - async ({event, step}) => { - const workflow = await step.run('Load Workflow', async () => { - return await sanityQuery( - `*[_type == "workflow" && trigger == '${AI_TIP_WRITING_REQUESTED_EVENT}'][0]`, - ) - }) - - const tip = await step.run('Load Tip', async () => { - return await sanityQuery(`*[_type == "tip" && _id == '${event.data.tipId}'][0]{ - _id, - _type, - "_updatedAt": ^._updatedAt, - title, - summary, - body, - "videoResourceId": resources[@->._type == 'videoResource'][0]->_id, - "transcript": resources[@->._type == 'videoResource'][0]->transcript, - "slug": slug.current, - }`) - }) - - const videoResource = await step.run('Load Video Resource', async () => { - return await sanityQuery( - `*[_type == "videoResource" && _id == '${tip.videoResourceId}'][0]`, - ) - }) - - let shouldContinue = Boolean(workflow) - let messages: ChatCompletionRequestMessage[] = [] - - while (workflow.actions.length > 0 && shouldContinue) { - const action = workflow.actions.shift() - switch (action._type) { - case 'prompt': - messages = await step.run(action.title, async () => { - return await promptActionExecutor({ - action, - input: { - transcript: `${ - videoResource.transcript - }\n\n ## Examples of Good Video Titles\n\n* ${titles.join( - '\n * ', - )}`, - summary: tip.title, - }, - requestId: videoResource._id, - messages, - }) - }) - break - default: - shouldContinue = false - } - } - - const finalMessage = JSON.parse(last(messages)?.content || '{}') - - await step.sendEvent('Announce Completion', { - name: AI_WRITING_COMPLETED_EVENT, - data: { - requestId: videoResource._id, - result: finalMessage, - fullPrompt: messages, - }, - }) - - await step.run('Broadcast Completion', async () => { - await fetch( - `${env.NEXT_PUBLIC_PARTY_KIT_URL}/party/${videoResource._id}`, - { - method: 'POST', - body: JSON.stringify({ - body: finalMessage, - requestId: videoResource._id, - name: 'ai.tip.draft.completed', - }), - }, - ).catch((e) => { - console.error(e) - }) - }) - - return finalMessage - }, -) diff --git a/apps/course-builder-web/src/inngest/inngest.config.ts b/apps/course-builder-web/src/inngest/inngest.config.ts index f1cbb3f03..20dff778b 100644 --- a/apps/course-builder-web/src/inngest/inngest.config.ts +++ b/apps/course-builder-web/src/inngest/inngest.config.ts @@ -7,7 +7,6 @@ import { import {transcriptReady} from '@/inngest/functions/transcript-ready' import {videoUploaded} from '@/inngest/functions/video-uploaded' import {addSrtToMuxAsset} from '@/inngest/functions/mux/add-srt-to-mux-asset' -import {tipTitleAndSummaryWriter} from '@/inngest/functions/ai/tip-writer' import {userCreated} from '@/inngest/functions/user-created' import {weeklySignupDigest} from '@/inngest/functions/notify/creator/weekly-signups' import {userSignupAdminEmail} from '@/inngest/functions/notify/creator/user-signup' @@ -26,7 +25,6 @@ export const inngestConfig = { transcriptReady, videoUploaded, addSrtToMuxAsset, - tipTitleAndSummaryWriter, userCreated, weeklySignupDigest, userSignupAdminEmail, diff --git a/apps/course-builder-web/src/inngest/inngest.server.ts b/apps/course-builder-web/src/inngest/inngest.server.ts index 7d8ef625b..df99dc0f6 100644 --- a/apps/course-builder-web/src/inngest/inngest.server.ts +++ b/apps/course-builder-web/src/inngest/inngest.server.ts @@ -1,9 +1,7 @@ import {EventSchemas, Inngest} from 'inngest' import { - type AI_TIP_WRITING_REQUESTED_EVENT, type AI_WRITING_COMPLETED_EVENT, type AI_WRITING_REQUESTED_EVENT, - type AITipWritingRequested, type AIWritingRequestCompleted, type AIWritingRequested, ARTICLE_CHAT_EVENT, @@ -58,7 +56,6 @@ type Events = { [VIDEO_UPLOADED_EVENT]: VideoUploaded [POST_CREATION_REQUESTED_EVENT]: PostCreationRequested [MUX_SRT_READY_EVENT]: MuxSrtReady - [AI_TIP_WRITING_REQUESTED_EVENT]: AITipWritingRequested [USER_CREATED_EVENT]: UserCreated [POSTMARK_WEBHOOK_EVENT]: PostmarkWebhook [CLOUDINARY_WEBHOOK_EVENT]: CloudinaryWebhook diff --git a/apps/course-builder-web/src/trpc/api/routers/tips.ts b/apps/course-builder-web/src/trpc/api/routers/tips.ts index 470e6408b..aaa064f88 100644 --- a/apps/course-builder-web/src/trpc/api/routers/tips.ts +++ b/apps/course-builder-web/src/trpc/api/routers/tips.ts @@ -13,7 +13,7 @@ import slugify from '@sindresorhus/slugify' import {customAlphabet} from 'nanoid' import {inngest} from '@/inngest/inngest.server' -import {AI_TIP_WRITING_REQUESTED_EVENT, TIP_CHAT_EVENT} from '@/inngest/events' +import {TIP_CHAT_EVENT} from '@/inngest/events' import {toChicagoTitleCase} from '@/utils/chicagor-title' const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 5) @@ -161,28 +161,6 @@ export const tipsRouter = createTRPCRouter({ {returnDocuments: true}, ) - return await getTip(input.tipId) - }), - generateTitle: protectedProcedure - .input( - z.object({ - tipId: z.string(), - }), - ) - .mutation(async ({ctx, input}) => { - const session = await getServerAuthSession() - const ability = getAbility({user: session?.user}) - if (!ability.can('create', 'Content')) { - throw new Error('Unauthorized') - } - - await inngest.send({ - name: AI_TIP_WRITING_REQUESTED_EVENT, - data: { - tipId: input.tipId, - }, - }) - return await getTip(input.tipId) }), })