aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/actions
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
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')
-rw-r--r--apps/web/app/actions/doers.ts148
-rw-r--r--apps/web/app/actions/fetchers.ts24
2 files changed, 170 insertions, 2 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: [],
+ };
+ }
+}
diff --git a/apps/web/app/actions/fetchers.ts b/apps/web/app/actions/fetchers.ts
index 7071ecb3..8d6802a7 100644
--- a/apps/web/app/actions/fetchers.ts
+++ b/apps/web/app/actions/fetchers.ts
@@ -1,6 +1,6 @@
"use server";
-import { and, asc, eq, exists, inArray, not, or, sql } from "drizzle-orm";
+import { and, asc, eq, exists, not, or } from "drizzle-orm";
import { db } from "../../server/db";
import {
canvas,
@@ -13,15 +13,32 @@ import {
spacesAccess,
storedContent,
StoredSpace,
+ User,
users,
} from "../../server/db/schema";
-import { ServerActionReturnType, Space } from "./types";
+import { ServerActionReturnType } from "./types";
import { auth } from "../../server/auth";
import { ChatHistory, SourceZod } from "@repo/shared-types";
import { z } from "zod";
import { redirect } from "next/navigation";
import { cookies, headers } from "next/headers";
+export const getUser = async (): ServerActionReturnType<User> => {
+ const data = await auth();
+
+ if (!data || !data.user || !data.user.id) {
+ redirect("/signin");
+ return { error: "Not authenticated", success: false };
+ }
+
+ console.log("data.user.id", data.user.id);
+ const user = await db.query.users.findFirst({
+ where: eq(users.id, data.user.id),
+ });
+
+ return { success: true, data: user };
+};
+
export const getSpaces = async (): ServerActionReturnType<StoredSpace[]> => {
const data = await auth();
@@ -218,6 +235,9 @@ export const getFullChatThread = async (
],
sources: sources ?? [],
},
+ proModeProcessing: {
+ queries: [],
+ },
};
},
);