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

Remove Separate API Service for Web Interface #174

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
112 changes: 112 additions & 0 deletions www/app/actions/conversations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use server';

import { createClient } from '@/utils/supabase/server';
import { honcho, getHonchoApp } from '@/utils/honcho';

// TODO add proper authorization check

type Conversation = {
conversationId: string;
name: string;
};

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 honcho.apps.users.getOrCreate(honchoApp.id, 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 honcho.apps.users.getOrCreate(honchoApp.id, user.id);

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

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 honcho.apps.users.getOrCreate(honchoApp.id, user.id);

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

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 honcho.apps.users.getOrCreate(honchoApp.id, user.id);

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

return true;
}
132 changes: 132 additions & 0 deletions www/app/actions/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
'use server';
import { createClient } from '@/utils/supabase/server';
import { honcho, getHonchoApp } from '@/utils/honcho';
import { Message } from '@/utils/types';

const defaultMessage: Message = {
content: `I'm your Aristotelian learning companion — here to help you follow your curiosity in whatever direction you like. My engineering makes me extremely receptive to your needs and interests. You can reply normally, and I’ll always respond!\n\nIf I'm off track, just say so!\n\nNeed to leave or just done chatting? Let me know! I’m conversational by design so I’ll say goodbye 😊.`,
isUser: false,
id: '',
};

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

const honchoApp = await getHonchoApp();

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

if (!user) {
throw new Error('Unauthorized');
}
const honchoUser = await honcho.apps.users.getOrCreate(honchoApp.id, user.id);
const session = await honcho.apps.users.sessions.get(
honchoApp.id,
honchoUser.id,
conversationId
);
const messages = [];
// TODO check if empty params is necessary
for await (const message of honcho.apps.users.sessions.messages.list(
honchoApp.id,
honchoUser.id,
session.id,
{}
)) {
messages.push({
id: message.id,
content: message.content,
isUser: message.is_user,
metadata: message.metadata,
});
}

return [defaultMessage, ...messages];
}

export async function getThought(conversationId: string, messageId: string) {
const supabase = createClient();

const honchoApp = await getHonchoApp();

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

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

const honchoUser = await honcho.apps.users.getOrCreate(honchoApp.id, user.id);

try {
const thoughts = await honcho.apps.users.sessions.metamessages.list(
honchoApp.id,
honchoUser.id,
conversationId,
{
message_id: messageId,
metamessage_type: 'thought',
filter: { type: 'assistant' },
}
);

return thoughts.items[0]?.content || null;
} catch (error) {
console.error('Error in getThought:', error);
throw new Error('Internal server error');
}
}

export async function addOrRemoveReaction(
conversationId: string,
messageId: string,
reaction: 'thumbs_up' | 'thumbs_down' | null
) {
const supabase = createClient();

const honchoApp = await getHonchoApp();

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

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

if (reaction && !['thumbs_up', 'thumbs_down'].includes(reaction)) {
throw new Error('Invalid reaction type');
}

const honchoUser = await honcho.apps.users.getOrCreate(honchoApp.id, user.id);

const message = await honcho.apps.users.sessions.messages.get(
honchoApp.id,
honchoUser.id,
conversationId,
messageId
);

if (!message) {
throw new Error('Message not found');
}

const metadata = message.metadata || {};

if (reaction === null) {
delete metadata.reaction;
} else {
metadata.reaction = reaction;
}

await honcho.apps.users.sessions.messages.update(
honchoApp.id,
honchoUser.id,
conversationId,
messageId,
{ metadata }
);
}
Loading