From a4e8d829b19707aef93e0aea5cfd56d4a19ef087 Mon Sep 17 00:00:00 2001 From: klotske Date: Thu, 5 Jan 2023 23:27:44 +0300 Subject: [PATCH 1/3] Implement interaction in oasst_api --- website/src/lib/oasst_api_client.ts | 23 +++++++++++++++++++++++ website/src/pages/api/update_task.ts | 26 ++++---------------------- 2 files changed, 27 insertions(+), 22 deletions(-) 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({ From f5b49a952ec2b0cf382a4258e62997c2f69df560 Mon Sep 17 00:00:00 2001 From: klotske Date: Thu, 5 Jan 2023 23:46:39 +0300 Subject: [PATCH 2/3] Add the taskInteraction contract test --- .../contract/oasst_api_contract_tests.cy.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/website/cypress/contract/oasst_api_contract_tests.cy.ts b/website/cypress/contract/oasst_api_contract_tests.cy.ts index ff5bb156..550fd1dd 100644 --- a/website/cypress/contract/oasst_api_contract_tests.cy.ts +++ b/website/cypress/contract/oasst_api_contract_tests.cy.ts @@ -23,6 +23,27 @@ describe("Contract test for Oasst API", function () { expect(await oasstApiClient.ackTask(task.id, "321")).to.be.null; }); + it("can record a taskInteraction", async () => { + const task = await oasstApiClient.fetchTask("random", { + sub: "test", + name: "test", + email: "test", + }); + expect( + await oasstApiClient.interactTask( + "text_reply_to_message", + task.id, + "1", + { text: "Test" }, + { + sub: "test", + name: "test", + email: "test", + } + ) + ).to.be.not.null; + }); + // TODO(#354): Add test for 204 // TODO(#354): Add test for parsing >=300, throwing an OasstError // TODO(#354): Add test for parsing >=300, throwing a generic error From 3c330dcd13fc99aee507b82ae8b5889e87a5e8f8 Mon Sep 17 00:00:00 2001 From: klotske Date: Fri, 6 Jan 2023 13:44:46 +0300 Subject: [PATCH 3/3] Added the oasstApiClient global var and init --- .../cypress/contract/oasst_api_contract_tests.cy.ts | 2 +- website/src/lib/oasst_api_client.ts | 13 ++++++++++++- website/src/pages/api/new_task/[task_type].ts | 4 +--- website/src/pages/api/update_task.ts | 4 +--- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/website/cypress/contract/oasst_api_contract_tests.cy.ts b/website/cypress/contract/oasst_api_contract_tests.cy.ts index 550fd1dd..1ba408d6 100644 --- a/website/cypress/contract/oasst_api_contract_tests.cy.ts +++ b/website/cypress/contract/oasst_api_contract_tests.cy.ts @@ -1,4 +1,4 @@ -import OasstApiClient from "src/lib/oasst_api_client"; +import { OasstApiClient } from "src/lib/oasst_api_client"; describe("Contract test for Oasst API", function () { // Assumes this is running the mock server. diff --git a/website/src/lib/oasst_api_client.ts b/website/src/lib/oasst_api_client.ts index b0caf3c6..7e22544a 100644 --- a/website/src/lib/oasst_api_client.ts +++ b/website/src/lib/oasst_api_client.ts @@ -1,5 +1,10 @@ import { JWT } from "next-auth/jwt"; +declare global { + // eslint-disable-next-line no-var + var oasstApiClient: OasstApiClient | undefined; +} + class OasstError { message: string; errorCode: number; @@ -12,7 +17,7 @@ class OasstError { } } -export default class OasstApiClient { +export class OasstApiClient { constructor(private readonly oasstApiUrl: string, private readonly oasstApiKey: string) {} private async post(path: string, body: any): Promise { @@ -85,3 +90,9 @@ export default class OasstApiClient { }); } } + +export const oasstApiClient = + globalThis.oasstApiClient || new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY); +if (process.env.NODE_ENV !== "production") { + globalThis.oasstApiClient = oasstApiClient; +} diff --git a/website/src/pages/api/new_task/[task_type].ts b/website/src/pages/api/new_task/[task_type].ts index bbe31bef..addcf3d8 100644 --- a/website/src/pages/api/new_task/[task_type].ts +++ b/website/src/pages/api/new_task/[task_type].ts @@ -1,5 +1,5 @@ import { getToken } from "next-auth/jwt"; -import OasstApiClient from "src/lib/oasst_api_client"; +import { oasstApiClient } from "src/lib/oasst_api_client"; import prisma from "src/lib/prismadb"; /** @@ -21,8 +21,6 @@ const handler = async (req, res) => { return; } - const oasstApiClient = new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY); - // Fetch the new task. const task = await oasstApiClient.fetchTask(task_type, token); diff --git a/website/src/pages/api/update_task.ts b/website/src/pages/api/update_task.ts index fbc8da18..4eea8c1e 100644 --- a/website/src/pages/api/update_task.ts +++ b/website/src/pages/api/update_task.ts @@ -1,5 +1,5 @@ import { getToken } from "next-auth/jwt"; -import OasstApiClient from "src/lib/oasst_api_client"; +import { oasstApiClient } from "src/lib/oasst_api_client"; import prisma from "src/lib/prismadb"; /** @@ -19,8 +19,6 @@ 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);