diff --git a/website/src/lib/oasst_api_client.ts b/website/src/lib/oasst_api_client.ts index 45a0859e..b0caf3c6 100644 --- a/website/src/lib/oasst_api_client.ts +++ b/website/src/lib/oasst_api_client.ts @@ -61,4 +61,27 @@ export default class OasstApiClient { message_id: messageId, }); } + + // TODO return a strongly typed Task? + // This method is used to record interaction with task while fetching next task. + // This is a raw Json type, so we can't use it to strongly type the task. + async interactTask( + updateType: string, + messageId: string, + userMessageId: string, + content: object, + userToken: JWT + ): Promise { + return this.post("/api/v1/tasks/interaction", { + type: updateType, + user: { + id: userToken.sub, + display_name: userToken.name || userToken.email, + auth_method: "local", + }, + message_id: messageId, + user_message_id: userMessageId, + ...content, + }); + } } diff --git a/website/src/pages/api/update_task.ts b/website/src/pages/api/update_task.ts index 2d371354..fbc8da18 100644 --- a/website/src/pages/api/update_task.ts +++ b/website/src/pages/api/update_task.ts @@ -1,4 +1,5 @@ import { getToken } from "next-auth/jwt"; +import OasstApiClient from "src/lib/oasst_api_client"; import prisma from "src/lib/prismadb"; /** @@ -18,6 +19,8 @@ const handler = async (req, res) => { return; } + const oasstApiClient = new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY); + // Parse out the local task ID and the interaction contents. const { id, content, update_type } = await JSON.parse(req.body); @@ -34,28 +37,7 @@ const handler = async (req, res) => { }, }); - // Send the interaction to the Task Backend. This automatically fetches the - // next task in the sequence (or the done task). - // TODO(#353): Move this into OasstApiClient. - const interactionRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/interaction`, { - method: "POST", - headers: { - "X-API-Key": process.env.FASTAPI_KEY, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - type: update_type, - user: { - id: token.sub, - display_name: token.name || token.email, - auth_method: "local", - }, - message_id: id, - user_message_id: interaction.id, - ...content, - }), - }); - const newTask = await interactionRes.json(); + const newTask = await oasstApiClient.interactTask(update_type, id, interaction.id, content, token); // Stores the new task with our database. const newRegisteredTask = await prisma.registeredTask.create({