Skip to content

Commit

Permalink
Merge branch 'main' into ayush/dev-417-2
Browse files Browse the repository at this point in the history
  • Loading branch information
hyusap authored Dec 26, 2024
2 parents 1bd9601 + d3c5b7b commit 4819f23
Show file tree
Hide file tree
Showing 21 changed files with 1,013 additions and 714 deletions.
5 changes: 5 additions & 0 deletions www/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ AI_API_KEY=
MODEL=
HONCHO_URL=
HONCHO_APP_NAME=

# Langfuse
LANGFUSE_SECRET_KEY=
LANGFUSE_PUBLIC_KEY=
LANGFUSE_BASEURL=
7 changes: 3 additions & 4 deletions www/app/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { FaLightbulb, FaPaperPlane } from 'react-icons/fa';
import Swal from 'sweetalert2';

import { useRef, useEffect, useState, ElementRef, useMemo } from 'react';
import { useRouter } from 'next/navigation';
// import { useRouter } from 'next/navigation';
import { usePostHog } from 'posthog-js/react';
import Turnstile, { useTurnstile } from 'react-turnstile';

import { createClient } from '@/utils/supabase/client';
// import { createClient } from '@/utils/supabase/client';
import { Reaction } from '@/components/messagebox';
import { FiMenu } from 'react-icons/fi';
import Link from 'next/link';
Expand Down Expand Up @@ -42,13 +42,12 @@ async function fetchStream(
honchoThought = ''
) {
try {
const response = await fetch(`/api/chat`, {
const response = await fetch(`/api/chat/${type}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
type,
message,
conversationId,
thought,
Expand Down
191 changes: 106 additions & 85 deletions www/app/actions/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { createClient } from '@/utils/supabase/server';
import { honcho, getHonchoApp, getHonchoUser } from '@/utils/honcho';
import * as Sentry from '@sentry/nextjs';

// TODO add proper authorization check

Expand All @@ -11,101 +12,121 @@ type Conversation = {
};

export async function getConversations() {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

const acc = [];
for await (const convo of honcho.apps.users.sessions.list(
honchoApp.id,
honchoUser.id,
{ is_active: true, reverse: true }
)) {
const name = (convo.metadata?.name as string) ?? 'Untitled';
const instance: Conversation = {
conversationId: convo.id,
name,
};
acc.push(instance);
}
return acc;
return Sentry.startSpan(
{ name: 'server-action.getConversations', op: 'server.action' },
async () => {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

const acc = [];
for await (const convo of honcho.apps.users.sessions.list(
honchoApp.id,
honchoUser.id,
{ is_active: true, reverse: true }
)) {
const name = (convo.metadata?.name as string) ?? 'Untitled';
const instance: Conversation = {
conversationId: convo.id,
name,
};
acc.push(instance);
}
return acc;
}
);
}

export async function createConversation() {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

const session = await honcho.apps.users.sessions.create(
honchoApp.id,
honchoUser.id,
{}
return Sentry.startSpan(
{ name: 'server-action.createConversation', op: 'server.action' },
async () => {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

const session = await honcho.apps.users.sessions.create(
honchoApp.id,
honchoUser.id,
{}
);

return { conversationId: session.id, name: 'Untitled' };
}
);

return { conversationId: session.id, name: 'Untitled' };
}

export async function deleteConversation(conversationId: string) {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

await honcho.apps.users.sessions.delete(
honchoApp.id,
honchoUser.id,
conversationId
return Sentry.startSpan(
{ name: 'server-action.deleteConversation', op: 'server.action' },
async () => {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

await honcho.apps.users.sessions.delete(
honchoApp.id,
honchoUser.id,
conversationId
);

return true;
}
);

return true;
}

export async function updateConversation(conversationId: string, name: string) {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

await honcho.apps.users.sessions.update(
honchoApp.id,
honchoUser.id,
conversationId,
{ metadata: { name } }
return Sentry.startSpan(
{ name: 'server-action.updateConversation', op: 'server.action' },
async () => {
const supabase = createClient();

const {
data: { user },
} = await supabase.auth.getUser();

if (!user) {
throw new Error('Unauthorized');
}

const honchoApp = await getHonchoApp();
const honchoUser = await getHonchoUser(user.id);

await honcho.apps.users.sessions.update(
honchoApp.id,
honchoUser.id,
conversationId,
{ metadata: { name } }
);

return true;
}
);

return true;
}
Loading

0 comments on commit 4819f23

Please sign in to comment.