Skip to content
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
42 changes: 38 additions & 4 deletions app/(chat)/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Ok, Err, type Result } from 'ts-results-es';

import type { ApiGetConversationMessagesResponse } from '@/app/(chat)/types';
import { extractErrorMessageOrDefault } from '@/lib/utils';

import type {
ApiGetConversationMessagesResponse,
ApiCreateConversationResponse,
ApiGetConversationResponse,
ApiSendMessageResponse,
ApiSendMessageStreamedResponse,
} from './types';
ApiGetAllConversationsResponse,
} from '@/app/(chat)/types';
import { extractErrorMessageOrDefault } from '@/lib/utils';

const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
if (!patternCoreEndpoint) {
Expand Down Expand Up @@ -232,3 +232,37 @@ export const sendMessageStreamed = async (
return Err(extractErrorMessageOrDefault(error));
}
};

/**
* Get all conversations
* @param accessToken
* @returns result containing all conversations of current user
*/
export const getAllConversations = async (
accessToken: string,
projectId: string,
): Promise<Result<ApiGetAllConversationsResponse, string>> => {
try {
const allConversationsResponse = await fetch(
`${patternCoreEndpoint}/playground/conversation/${projectId}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
},
);
if (allConversationsResponse.ok) {
const allConversations: ApiGetAllConversationsResponse = (
await allConversationsResponse.json()
).data;

return Ok(allConversations);
}
return Err(
`Fetching projects failed with error code ${allConversationsResponse.status}`,
);
} catch (error) {
return Err(extractErrorMessageOrDefault(error));
}
};
22 changes: 17 additions & 5 deletions app/(chat)/api/history/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { auth } from '@/app/(auth)/auth';
import { getChatsByUserId } from '@/lib/db/queries';

import { getAllChats } from '../../service';

export async function GET() {
const session = await auth();

if (!session || !session.user) {
if (
!session ||
!session.chainId ||
!session.address ||
!session.accessToken
) {
return Response.json('Unauthorized!', { status: 401 });
}

// biome-ignore lint: Forbidden non-null assertion.
const chats = await getChatsByUserId({ id: session.user.id! });
return Response.json(chats);
const chatsResult = await getAllChats(session.accessToken, session.projectId);

if (chatsResult.isErr()) {
return new Response(chatsResult.error, { status: 400 });
}

const allChats = chatsResult.value;

return Response.json(allChats);
}
41 changes: 40 additions & 1 deletion app/(chat)/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { type Result, Err, Ok } from 'ts-results-es';

import { createConversation, getConversation } from './adapter';
import type { Chat } from '@/lib/db/schema';

import {
createConversation,
getAllConversations,
getConversation,
} from './adapter';
import type { Conversation } from './types';

/**
Expand Down Expand Up @@ -41,6 +47,39 @@ export const getOrCreateConversation = async (
return Ok(conversation);
};

/**
* fetches all of current user's conversations and transforms it into
* ui-friendly chats
* @param accessToken
* @param projectId
* @returns result containing ui-friendly chats list
*/
export const getAllChats = async (
accessToken: string,
projectId: string,
): Promise<Result<Chat[], string>> => {
const allConversationsResult = await getAllConversations(
accessToken,
projectId,
);

if (allConversationsResult.isErr()) {
return Err(allConversationsResult.error);
}

const allConversations = allConversationsResult.value;

const history: Chat[] = allConversations.map((conversation) => ({
createdAt: new Date(conversation.updated_at),
id: conversation.id,
title: conversation.name,
userId: conversation.user_id,
visibility: 'private',
}));

return Ok(history);
};

export {
sendMessage,
sendMessageStreamed,
Expand Down
3 changes: 3 additions & 0 deletions app/(chat)/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export interface Conversation {
id: string;
name: string;
project_id: string;
user_id: string;
updated_at: string;
}

export interface Message {
Expand All @@ -14,3 +16,4 @@ export type ApiGetConversationMessagesResponse = Message[];
export type ApiCreateConversationResponse = Conversation;
export type ApiSendMessageResponse = string;
export type ApiSendMessageStreamedResponse = ReadableStream;
export type ApiGetAllConversationsResponse = Conversation[];
Loading