harbor run -t openthoughts/tasktrove-mix-h2-language-proportional-mix-h2-lang-proportional-3315
harbor download openthoughts/tasktrove-mix-h2-language-proportional-mix-h2-lang-proportional-3315
solution file
import { query } from "../db"; import { userLoggedIn } from "../authchecks"; import { NextApiResponse, NextApiRequest } from "next"; import { createEmbedding } from "../openai"; export default async function handler(req: NextApiRequest, res: NextApiResponse) { const userid = await userLoggedIn(req, res); if (userid == "") { res.status(401).send({ response: "Not logged in" }); return; } if (req.method == "POST") { await postRequest(req, res, userid); } else if (req.method == "GET") { await getRequest(req, res, userid); } } async function getRequest(req: NextApiRequest, res: NextApiResponse, userid: string) { // Gets all custom terms associated with the userID const customTermsQuery = await query( `SELECT term, context, termid FROM userterms WHERE userid = $1`, [userid] ); const contexts = customTermsQuery.rows.map((row) => (row as any).context); const termids = customTermsQuery.rows.map(row => (row as any).termid); const terms = customTermsQuery.rows.map(row => (row as any).term); res.status(200).send({ terms: terms, contexts: contexts, termids: termids }); } async function postRequest(req: NextApiRequest, res: NextApiResponse, userid: string) { const term = req.body.term as string; const context = req.body.context as string; // Inserts the term and context into the userterms table await query( `INSERT INTO userterms (userid, term, context) VALUES ($1, $2, $3)`, [userid, term, context] ); // Gets the termid of the term just inserted const termidQuery = await query( `SELECT termid FROM userterms WHERE userid = $1 AND term = $2`, [userid, term] ); const termid = (termidQuery.rows[0] as any).termid; // Breaks the context into paragraphs and inserts them into the usercontext table const paragraphs = context.split("\n\n"); for (let i = 1; i <= paragraphs.length; i++) {
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py
/app/solution.py