Implement interaction in oasst_api

This commit is contained in:
klotske
2023-01-05 23:27:44 +03:00
parent 739c073328
commit a4e8d829b1
2 changed files with 27 additions and 22 deletions
+23
View File
@@ -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<any> {
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,
});
}
}
+4 -22
View File
@@ -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({