Skip to content

Commit dc0a08a

Browse files
authored
Merge pull request #10 from pattern-tech/feat/history
Feat: history
2 parents ff6bd73 + 61dbb0b commit dc0a08a

File tree

4 files changed

+98
-10
lines changed

4 files changed

+98
-10
lines changed

app/(chat)/adapter.ts

+38-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Ok, Err, type Result } from 'ts-results-es';
22

3-
import type { ApiGetConversationMessagesResponse } from '@/app/(chat)/types';
4-
import { extractErrorMessageOrDefault } from '@/lib/utils';
5-
63
import type {
4+
ApiGetConversationMessagesResponse,
75
ApiCreateConversationResponse,
86
ApiGetConversationResponse,
97
ApiSendMessageResponse,
108
ApiSendMessageStreamedResponse,
11-
} from './types';
9+
ApiGetAllConversationsResponse,
10+
} from '@/app/(chat)/types';
11+
import { extractErrorMessageOrDefault } from '@/lib/utils';
1212

1313
const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
1414
if (!patternCoreEndpoint) {
@@ -232,3 +232,37 @@ export const sendMessageStreamed = async (
232232
return Err(extractErrorMessageOrDefault(error));
233233
}
234234
};
235+
236+
/**
237+
* Get all conversations
238+
* @param accessToken
239+
* @returns result containing all conversations of current user
240+
*/
241+
export const getAllConversations = async (
242+
accessToken: string,
243+
projectId: string,
244+
): Promise<Result<ApiGetAllConversationsResponse, string>> => {
245+
try {
246+
const allConversationsResponse = await fetch(
247+
`${patternCoreEndpoint}/playground/conversation/${projectId}`,
248+
{
249+
headers: {
250+
Authorization: `Bearer ${accessToken}`,
251+
'Content-Type': 'application/json',
252+
},
253+
},
254+
);
255+
if (allConversationsResponse.ok) {
256+
const allConversations: ApiGetAllConversationsResponse = (
257+
await allConversationsResponse.json()
258+
).data;
259+
260+
return Ok(allConversations);
261+
}
262+
return Err(
263+
`Fetching projects failed with error code ${allConversationsResponse.status}`,
264+
);
265+
} catch (error) {
266+
return Err(extractErrorMessageOrDefault(error));
267+
}
268+
};

app/(chat)/api/history/route.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import { auth } from '@/app/(auth)/auth';
2-
import { getChatsByUserId } from '@/lib/db/queries';
2+
3+
import { getAllChats } from '../../service';
34

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

7-
if (!session || !session.user) {
8+
if (
9+
!session ||
10+
!session.chainId ||
11+
!session.address ||
12+
!session.accessToken
13+
) {
814
return Response.json('Unauthorized!', { status: 401 });
915
}
1016

11-
// biome-ignore lint: Forbidden non-null assertion.
12-
const chats = await getChatsByUserId({ id: session.user.id! });
13-
return Response.json(chats);
17+
const chatsResult = await getAllChats(session.accessToken, session.projectId);
18+
19+
if (chatsResult.isErr()) {
20+
return new Response(chatsResult.error, { status: 400 });
21+
}
22+
23+
const allChats = chatsResult.value;
24+
25+
return Response.json(allChats);
1426
}

app/(chat)/service.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { type Result, Err, Ok } from 'ts-results-es';
22

3-
import { createConversation, getConversation } from './adapter';
3+
import type { Chat } from '@/lib/db/schema';
4+
5+
import {
6+
createConversation,
7+
getAllConversations,
8+
getConversation,
9+
} from './adapter';
410
import type { Conversation } from './types';
511

612
/**
@@ -41,6 +47,39 @@ export const getOrCreateConversation = async (
4147
return Ok(conversation);
4248
};
4349

50+
/**
51+
* fetches all of current user's conversations and transforms it into
52+
* ui-friendly chats
53+
* @param accessToken
54+
* @param projectId
55+
* @returns result containing ui-friendly chats list
56+
*/
57+
export const getAllChats = async (
58+
accessToken: string,
59+
projectId: string,
60+
): Promise<Result<Chat[], string>> => {
61+
const allConversationsResult = await getAllConversations(
62+
accessToken,
63+
projectId,
64+
);
65+
66+
if (allConversationsResult.isErr()) {
67+
return Err(allConversationsResult.error);
68+
}
69+
70+
const allConversations = allConversationsResult.value;
71+
72+
const history: Chat[] = allConversations.map((conversation) => ({
73+
createdAt: new Date(conversation.updated_at),
74+
id: conversation.id,
75+
title: conversation.name,
76+
userId: conversation.user_id,
77+
visibility: 'private',
78+
}));
79+
80+
return Ok(history);
81+
};
82+
4483
export {
4584
sendMessage,
4685
sendMessageStreamed,

app/(chat)/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export interface Conversation {
22
id: string;
33
name: string;
44
project_id: string;
5+
user_id: string;
6+
updated_at: string;
57
}
68

79
export interface Message {
@@ -14,3 +16,4 @@ export type ApiGetConversationMessagesResponse = Message[];
1416
export type ApiCreateConversationResponse = Conversation;
1517
export type ApiSendMessageResponse = string;
1618
export type ApiSendMessageStreamedResponse = ReadableStream;
19+
export type ApiGetAllConversationsResponse = Conversation[];

0 commit comments

Comments
 (0)