aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/actions/doers.ts
diff options
context:
space:
mode:
authorDhravya Shah <[email protected]>2024-07-25 17:35:15 -0500
committerDhravya Shah <[email protected]>2024-07-25 17:35:15 -0500
commitc57719446ae95c2bbd432d7b2b6648a23b35c351 (patch)
treec6f7aca777c7f4748cc6dc335fe56fba8725af02 /apps/web/app/actions/doers.ts
parentadd try catch in api/add for better error handling (diff)
parentughh, regenerated migrations. my bad. (diff)
downloadsupermemory-kush/experimental-thread.tar.xz
supermemory-kush/experimental-thread.zip
solve merge conflictskush/experimental-thread
Diffstat (limited to 'apps/web/app/actions/doers.ts')
-rw-r--r--apps/web/app/actions/doers.ts148
1 files changed, 148 insertions, 0 deletions
diff --git a/apps/web/app/actions/doers.ts b/apps/web/app/actions/doers.ts
index f3677f4c..da2bfb5f 100644
--- a/apps/web/app/actions/doers.ts
+++ b/apps/web/app/actions/doers.ts
@@ -22,6 +22,34 @@ import { ChatHistory } from "@repo/shared-types";
import { decipher } from "@/server/encrypt";
import { redirect } from "next/navigation";
import { tweetToMd } from "@repo/shared-types/utils";
+import { ensureAuth } from "../api/ensureAuth";
+import { getRandomSentences } from "@/lib/utils";
+import { getRequestContext } from "@cloudflare/next-on-pages";
+
+export const completeOnboarding = async (): ServerActionReturnType<boolean> => {
+ const data = await auth();
+
+ if (!data || !data.user || !data.user.id) {
+ redirect("/signin");
+ return { error: "Not authenticated", success: false };
+ }
+
+ try {
+ const res = await db
+ .update(users)
+ .set({ hasOnboarded: true })
+ .where(eq(users.id, data.user.id))
+ .returning({ hasOnboarded: users.hasOnboarded });
+
+ if (res.length === 0 || !res[0]?.hasOnboarded) {
+ return { success: false, data: false, error: "Failed to update user" };
+ }
+
+ return { success: true, data: res[0].hasOnboarded };
+ } catch (e) {
+ return { success: false, data: false, error: (e as Error).message };
+ }
+};
export const createSpace = async (
input: string | FormData,
@@ -468,6 +496,7 @@ export const createChatObject = async (
answer: lastChat.answer.parts.map((part) => part.text).join(""),
answerSources: JSON.stringify(lastChat.answer.sources),
threadId,
+ createdAt: new Date(),
});
if (!saved) {
@@ -744,3 +773,122 @@ export async function AddCanvasInfo({
};
}
}
+
+export async function getQuerySuggestions() {
+ const data = await auth();
+
+ if (!data || !data.user || !data.user.id) {
+ redirect("/signin");
+ return { error: "Not authenticated", success: false };
+ }
+
+ const { env } = getRequestContext();
+
+ try {
+ const recommendations = await env.RECOMMENDATIONS.get(data.user.id);
+
+ if (recommendations) {
+ return {
+ success: true,
+ data: JSON.parse(recommendations),
+ };
+ }
+
+ // Randomly choose some storedContent of the user.
+ const content = await db
+ .select()
+ .from(storedContent)
+ .where(eq(storedContent.userId, data.user.id))
+ .orderBy(sql`random()`)
+ .limit(5)
+ .all();
+
+ if (content.length === 0) {
+ return {
+ success: true,
+ data: [],
+ };
+ }
+
+ const fullQuery = content
+ .map((c) => `${c.title} \n\n${c.content}`)
+ .join(" ");
+
+ const suggestionsCall = (await env.AI.run(
+ // @ts-ignore
+ "@cf/meta/llama-3.1-8b-instruct",
+ {
+ messages: [
+ {
+ role: "system",
+ content: `You are a model that suggests questions based on the user's content. you MUST suggest atleast 1 question to ask. AT MAX, create 3 suggestions. not more than that.`,
+ },
+ {
+ role: "user",
+ content: `Run the function based on this input: ${fullQuery.slice(0, 2000)}`,
+ },
+ ],
+ tools: [
+ {
+ type: "function",
+ function: {
+ name: "querySuggestions",
+ description:
+ "Take the user's content to suggest some good questions that they could ask.",
+ parameters: {
+ type: "object",
+ properties: {
+ querySuggestions: {
+ type: "array",
+ description:
+ "Short questions that the user can ask. Give atleast 3 suggestions. No more than 5.",
+ items: {
+ type: "string",
+ },
+ },
+ },
+ required: ["querySuggestions"],
+ },
+ },
+ },
+ ],
+ },
+ )) as {
+ response: string;
+ tool_calls: { name: string; arguments: { querySuggestions: string[] } }[];
+ };
+
+ console.log(
+ "I RAN AN AI CALLS OWOWOWOWOW",
+ JSON.stringify(suggestionsCall, null, 2),
+ );
+
+ const suggestions =
+ suggestionsCall.tool_calls?.[0]?.arguments?.querySuggestions;
+
+ if (!suggestions || suggestions.length === 0) {
+ return {
+ success: false,
+ error: "Failed to get query suggestions",
+ };
+ }
+
+ if (suggestions.length > 0) {
+ await env.RECOMMENDATIONS.put(data.user.id, JSON.stringify(suggestions), {
+ expirationTtl: 60 * 2,
+ });
+ }
+
+ return {
+ success: true,
+ data: suggestions,
+ };
+ } catch (exception) {
+ const error = exception as Error;
+ return {
+ success: false,
+ error: error.message,
+ data: [],
+ };
+ }
+}