Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
bentwnghk committed Dec 24, 2024
2 parents 179f003 + 13f1eb4 commit e9cb266
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
7 changes: 7 additions & 0 deletions changelog/v1.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"children": {
"fixes": ["Fix image input on pglite."]
},
"date": "2024-12-24",
"version": "1.39.1"
},
{
"children": {
"features": ["Upgrade to next15 and react19."]
Expand Down
8 changes: 6 additions & 2 deletions src/database/server/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export class MessageModel {
// **************** Query *************** //
query = async (
{ current = 0, pageSize = 1000, sessionId, topicId }: QueryMessageParams = {},
options: { postProcessUrl?: (path: string | null) => Promise<string> } = {},
options: {
postProcessUrl?: (path: string | null, file: { fileType: string }) => Promise<string>;
} = {},
): Promise<MessageItem[]> => {
const offset = current * pageSize;

Expand Down Expand Up @@ -130,7 +132,9 @@ export class MessageModel {
const relatedFileList = await Promise.all(
rawRelatedFileList.map(async (file) => ({
...file,
url: options.postProcessUrl ? await options.postProcessUrl(file.url) : (file.url as string),
url: options.postProcessUrl
? await options.postProcessUrl(file.url, file as any)
: (file.url as string),
})),
);

Expand Down
26 changes: 22 additions & 4 deletions src/services/message/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { clientDB } from '@/database/client/db';
import { MessageItem } from '@/database/schemas';
import { MessageModel } from '@/database/server/models/message';
import { BaseClientService } from '@/services/baseClientService';
import { clientS3Storage } from '@/services/file/ClientS3';
import {
ChatMessage,
ChatMessageError,
Expand Down Expand Up @@ -34,10 +35,20 @@ export class ClientService extends BaseClientService implements IMessageService
}

async getMessages(sessionId: string, topicId?: string) {
const data = await this.messageModel.query({
sessionId: this.toDbSessionId(sessionId),
topicId,
});
const data = await this.messageModel.query(
{
sessionId: this.toDbSessionId(sessionId),
topicId,
},
{
postProcessUrl: async (url, file) => {
const hash = (url as string).replace('client-s3://', '');
const base64 = await this.getBase64ByFileHash(hash);

return `data:${file.fileType};base64,${base64}`;
},
},
);

return data as unknown as ChatMessage[];
}
Expand Down Expand Up @@ -115,4 +126,11 @@ export class ClientService extends BaseClientService implements IMessageService
private toDbSessionId(sessionId: string | undefined) {
return sessionId === INBOX_SESSION_ID ? undefined : sessionId;
}

private async getBase64ByFileHash(hash: string) {
const fileItem = await clientS3Storage.getObject(hash);
if (!fileItem) throw new Error('file not found');

return Buffer.from(await fileItem.arrayBuffer()).toString('base64');
}
}

0 comments on commit e9cb266

Please sign in to comment.