Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 01/23/24 #693

Merged
merged 7 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/browser/app/api/chat/generate-title/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getAuthOptions } from "@/lib/api/authOptions";
import { createOpenAIApiClient } from "@/lib/api/utils/openai";
import { createSupabaseClient } from "@/lib/supabase/createSupabaseClient";
import { createClient } from "@/lib/supabase/createServerClient";
import { getServerSession } from "next-auth";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
Expand All @@ -11,12 +12,13 @@ export async function POST(request: NextRequest) {
}

// Make sure user is authenticated
const cookieStore = cookies()
const session = await getServerSession(getAuthOptions());
const email = session?.user?.email;
if (!session || !email) {
return NextResponse.json({}, { status: 401 });
}
const supabase = createSupabaseClient(session.supabaseAccessToken as string);
const supabase = createClient(cookieStore, session.supabaseAccessToken as string);
const { data, error } = await supabase
.from("chats")
.select()
Expand Down
8 changes: 2 additions & 6 deletions apps/browser/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
errorAtom,
newGoalSubmittedAtom,
isChatLoadingAtom,
ChatInfo,
welcomeModalAtom,
} from "@/lib/store";
import { useChats } from "@/lib/queries/useChats";
Expand All @@ -26,10 +25,8 @@ import { useRouter } from "next/navigation";
import { useAtom } from "jotai";
import { v4 as uuid } from "uuid";
import { InMemoryFile } from "@nerfzael/memory-fs";
import { useSupabaseClient } from "@/lib/supabase/useSupabaseClient";

function Dojo({ params }: { params: { id?: string } }) {
const supabase = useSupabaseClient();
const [evoService, setEvoService] = useAtom(evoServiceAtom);
const [newGoalSubmitted, setNewGoalSubmitted] = useAtom(newGoalSubmittedAtom);
const [isChatLoading, setIsChatLoading] = useAtom(isChatLoadingAtom);
Expand All @@ -45,14 +42,13 @@ function Dojo({ params }: { params: { id?: string } }) {
const { data: chats, isLoading: isChatsLoading } = useChats();
const router = useRouter();
const { status: sessionStatus, data: sessionData } = useSession();
const isAuthenticated = sessionStatus === "authenticated" && !!supabase;
const isAuthenticated = sessionStatus === "authenticated";

const { mutateAsync: createChat } = useCreateChat();
const { mutateAsync: updateChatTitle } = useUpdateChatTitle();
const { logs, isConnected, isStarting, isRunning, handleStart, status } = useEvoService(
chatId,
isAuthenticated,
supabase
isAuthenticated
);

const workspaceUploadUpdate = useWorkspaceUploadUpdate();
Expand Down
17 changes: 7 additions & 10 deletions apps/browser/components/providers/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Provider as JotaiProvider } from "jotai";
import ReactQueryProvider from "./ReactQueryProvider";
import { SessionProvider } from "next-auth/react";
import ToastProvider from "./ToastProvider";
import SupabaseClientProvider from "@/lib/supabase/SupabaseClientProvider";

export function Providers({ children }: { children: React.ReactNode }) {
return (
Expand All @@ -14,15 +13,13 @@ export function Providers({ children }: { children: React.ReactNode }) {
// Don't re-fetch session when window is focused
refetchOnWindowFocus={false}
>
<SupabaseClientProvider>
<JotaiProvider>
<ToastProvider>
<ReactQueryProvider>
{children}
</ReactQueryProvider>
</ToastProvider>
</JotaiProvider>
</SupabaseClientProvider>
<JotaiProvider>
<ToastProvider>
<ReactQueryProvider>
{children}
</ReactQueryProvider>
</ToastProvider>
</JotaiProvider>
</SessionProvider>
);
}
25 changes: 15 additions & 10 deletions apps/browser/lib/hooks/useEvoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EvoThreadCallbacks, EvoThreadConfig } from "@/lib/services/evo/EvoThrea
import { useAddChatLog } from "@/lib/mutations/useAddChatLog";
import { useAddMessages } from "@/lib/mutations/useAddMessages";
import { useAddVariable } from "@/lib/mutations/useAddVariable";
import { Chat, fetchChats, useChats } from "@/lib/queries/useChats";
import { Chat, fetchChats } from "@/lib/queries/useChats";
import { SupabaseWorkspace } from "@/lib/supabase/SupabaseWorkspace";
import { useWorkspaceFilesUpdate } from "@/lib/hooks/useWorkspaceFilesUpdate";
import { useWorkspaceUploadUpdate } from "@/lib/hooks/useWorkspaceUploadUpdate";
Expand All @@ -20,12 +20,12 @@ import { Workspace, InMemoryWorkspace } from "@evo-ninja/agent-utils";
import { ChatLogType, ChatMessage } from "@evo-ninja/agents";
import { useState, useEffect } from "react";
import { useAtom } from "jotai";
import { EvoSupabaseClient } from "../supabase/EvoSupabaseClient";
import { useSession } from "next-auth/react";
import { createSupabaseBrowserClient } from "../supabase/createBrowserClient";

export const useEvoService = (
chatId: string | "<anon>" | undefined,
isAuthenticated: boolean,
supabase: EvoSupabaseClient | undefined
isAuthenticated: boolean
): {
logs: ChatLog[];
isConnected: boolean;
Expand All @@ -42,6 +42,7 @@ export const useEvoService = (
const [, setCapReached] = useAtom(capReachedAtom);
const [, setSettingsModalOpen] = useAtom(settingsModalAtom);
const [, setError] = useAtom(errorAtom);
const { data: session } = useSession()

// State
const [isConnected, setIsConnected] = useState<boolean>(false);
Expand Down Expand Up @@ -125,7 +126,7 @@ export const useEvoService = (
};
}

const { data: chats, error } = await fetchChats(supabase!);
const { data: chats, error } = await fetchChats(session?.supabaseAccessToken as string);

if (error) {
console.error(error);
Expand All @@ -142,11 +143,15 @@ export const useEvoService = (
};

async function loadWorkspace(chatId: string): Promise<Workspace> {
// isAuthenticated is only true if there's a supabase instance
// so we can safely assume that it's not undefined
const workspace = isAuthenticated ?
new SupabaseWorkspace(chatId, supabase!.storage) :
new InMemoryWorkspace();
const workspace = (() => {
if (session?.supabaseAccessToken) {
const supabase = createSupabaseBrowserClient(session.supabaseAccessToken)
return new SupabaseWorkspace(chatId, supabase.storage)
} else {
return new InMemoryWorkspace()
}
})()


await workspaceUploadUpdate(workspace);

Expand Down
9 changes: 5 additions & 4 deletions apps/browser/lib/mutations/useAddChatLog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { ChatLog } from "@/components/Chat"
import { Row } from "../supabase/types"
import { useSupabaseClient } from "../supabase/useSupabaseClient"
import { createSupabaseBrowserClient } from "../supabase/createBrowserClient"
import { useSession } from "next-auth/react"

const mapChatLogToLogDTO = (
chatId: string,
Expand All @@ -17,17 +18,17 @@ const mapChatLogToLogDTO = (

export const useAddChatLog = () => {
const queryClient = useQueryClient();
const supabase = useSupabaseClient();
const { data: session } = useSession();

return useMutation({
mutationFn: async (args: {
chatId: string;
log: ChatLog;
}) => {
if (!supabase) {
if (!session?.supabaseAccessToken) {
throw new Error("Not authenticated");
}

const supabase = createSupabaseBrowserClient(session.supabaseAccessToken);
const { error } = await supabase
.from("logs")
.insert(
Expand Down
9 changes: 5 additions & 4 deletions apps/browser/lib/mutations/useAddMessages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { ChatLogType, ChatMessage } from "@evo-ninja/agents"
import { Row } from "../supabase/types"
import { useSupabaseClient } from "../supabase/useSupabaseClient"
import { createSupabaseBrowserClient } from "../supabase/createBrowserClient"
import { useSession } from "next-auth/react"

const mapChatMessageToMessageDTO = (
chatId: string,
Expand Down Expand Up @@ -64,18 +65,18 @@ const mapChatMessageToMessageDTO = (

export const useAddMessages = () => {
const queryClient = useQueryClient();
const supabase = useSupabaseClient();
const { data: session } = useSession();

return useMutation({
mutationFn: async (args: {
chatId: string;
messages: ChatMessage[];
type: ChatLogType;
}) => {
if (!supabase) {
if (!session?.supabaseAccessToken) {
throw new Error("Not authenticated");
}

const supabase = createSupabaseBrowserClient(session.supabaseAccessToken);
const { error } = await supabase
.from("messages")
.insert(
Expand Down
10 changes: 5 additions & 5 deletions apps/browser/lib/mutations/useAddVariable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { Row } from "../supabase/types"
import { useSupabaseClient } from "../supabase/useSupabaseClient"
import { createSupabaseBrowserClient } from "../supabase/createBrowserClient"
import { useSession } from "next-auth/react"

const mapVariableToVariableDTO = (
chatId: string,
Expand All @@ -16,18 +17,17 @@ const mapVariableToVariableDTO = (

export const useAddVariable = () => {
const queryClient = useQueryClient();
const supabase = useSupabaseClient();

const { data: session } = useSession();
return useMutation({
mutationFn: async (args: {
chatId: string;
key: string;
value: string;
}) => {
if (!supabase) {
if (!session?.supabaseAccessToken) {
throw new Error("Not authenticated");
}

const supabase = createSupabaseBrowserClient(session.supabaseAccessToken);
const { error } = await supabase
.from("variables")
.insert(
Expand Down
9 changes: 5 additions & 4 deletions apps/browser/lib/mutations/useCreateChat.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Chat } from "@/lib/queries/useChats"
import { createSupabaseBrowserClient } from "@/lib/supabase/createBrowserClient";
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useSupabaseClient } from "../supabase/useSupabaseClient";
import { useSession } from "next-auth/react";

export const useCreateChat = () => {
const queryClient = useQueryClient();
const supabase = useSupabaseClient();
const { data : session } = useSession()

return useMutation({
mutationFn: async (chatId: string) => {
if (!supabase) {
if (!session?.supabaseAccessToken ) {
throw new Error("Not authenticated");
}

const supabase = createSupabaseBrowserClient(session.supabaseAccessToken);
const { data, error } = await supabase
.from("chats")
.insert({
Expand Down
11 changes: 6 additions & 5 deletions apps/browser/lib/mutations/useDeleteChat.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useSupabaseClient } from "../supabase/useSupabaseClient";
import { createSupabaseBrowserClient } from "@/lib/supabase/createBrowserClient";
import { useSession } from "next-auth/react";

export const useDeleteChat = () => {
const queryClient = useQueryClient();
const supabase = useSupabaseClient();
const { data : session } = useSession()

return useMutation({
mutationFn: async (chatId: string) => {
if (!supabase) {
if (!session?.supabaseAccessToken) {
throw new Error("Not authenticated");
}

const supabase = createSupabaseBrowserClient(session.supabaseAccessToken);
const { error } = await supabase
.from("chats")
.delete()
Expand Down
8 changes: 5 additions & 3 deletions apps/browser/lib/mutations/useUpdateChatTitle.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useSupabaseClient } from "../supabase/useSupabaseClient";
import { createSupabaseBrowserClient } from "@/lib/supabase/createBrowserClient";
import { useSession } from "next-auth/react";

export const useUpdateChatTitle = () => {
const queryClient = useQueryClient();
const supabase = useSupabaseClient();
const { data : session } = useSession()

return useMutation({
mutationFn: async (args: { chatId: string; title: string }) => {
if (!supabase) {
if (!session?.supabaseAccessToken) {
throw new Error("Not authenticated");
}

const supabase = createSupabaseBrowserClient(session.supabaseAccessToken);
const { error } = await supabase
.from("chats")
.update({ title: args.title })
Expand Down
17 changes: 9 additions & 8 deletions apps/browser/lib/queries/useChats.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useQuery } from "@tanstack/react-query"
import { useSession } from "next-auth/react"
import { ChatMessage } from "@evo-ninja/agents"
import { PostgrestError } from "@supabase/supabase-js"
import { ChatLog } from "@/components/Chat"
import { Json } from "../supabase/dbTypes"
import { useSupabaseClient } from "../supabase/useSupabaseClient"
import { createSupabaseBrowserClient } from "../supabase/createBrowserClient"

export interface Chat {
id: string;
Expand Down Expand Up @@ -125,10 +126,11 @@ const mapChatDTOtoChat = (dto: ChatDTO): Chat => {
}
}

export const fetchChats = async (supabase: any): Promise<{
export const fetchChats = async (supabaseToken: string): Promise<{
data: Chat[] | undefined,
error: Error | undefined
error: PostgrestError | undefined
}> => {
const supabase = createSupabaseBrowserClient(supabaseToken);
const { data, error } = await supabase
.from('chats')
.select(`
Expand Down Expand Up @@ -168,18 +170,17 @@ export const fetchChats = async (supabase: any): Promise<{

export const useChats = () => {
const { data: session } = useSession();
const supabase = useSupabaseClient();

return useQuery({
queryKey: ['chats', session?.user?.email, supabase],
enabled: !!session?.user?.email && !!supabase,
queryKey: ['chats', session?.user?.email],
enabled: !!session?.user?.email,
refetchOnMount: false,
queryFn: async () => {
if (!session?.user?.email || !supabase) {
if (!session?.user?.email || !session?.supabaseAccessToken) {
throw new Error("Not authenticated")
}

const { data, error } = await fetchChats(supabase);
const { data, error } = await fetchChats(session.supabaseAccessToken);

if (error) {
console.error(error)
Expand Down
Loading
Loading