fix(server): always return created timestamp of chat messages (#6658)

This commit is contained in:
forehalo 2024-04-22 08:39:41 +00:00
parent b7ade43c2e
commit 94de6f5853
No known key found for this signature in database
GPG Key ID: 56709255DC7EC728
3 changed files with 12 additions and 5 deletions

View File

@ -103,8 +103,8 @@ class ChatMessageType implements Partial<ChatMessage> {
@Field(() => GraphQLJSON, { nullable: true })
params!: Record<string, string> | undefined;
@Field(() => Date, { nullable: true })
createdAt!: Date | undefined;
@Field(() => Date)
createdAt!: Date;
}
@ObjectType('CopilotHistories')

View File

@ -18,7 +18,6 @@ import {
getTokenEncoder,
ListHistoriesOptions,
PromptMessage,
PromptMessageSchema,
PromptParams,
SubmittedMessage,
} from './types';
@ -324,6 +323,7 @@ export class ChatSessionService {
role: true,
content: true,
params: true,
createdAt: true,
},
orderBy: {
createdAt: 'asc',
@ -338,7 +338,7 @@ export class ChatSessionService {
Promise.all(
sessions.map(async ({ id, promptName, messages, createdAt }) => {
try {
const ret = PromptMessageSchema.array().safeParse(messages);
const ret = ChatMessageSchema.array().safeParse(messages);
if (ret.success) {
const prompt = await this.prompt.get(promptName);
if (!prompt) {
@ -356,6 +356,13 @@ export class ChatSessionService {
.filter(({ role }) => role !== 'system')
: [];
// `createdAt` is required for history sorting in frontend, let's fake the creating time of prompt messages
(preload as ChatMessage[]).forEach((msg, i) => {
msg.createdAt = new Date(
createdAt.getTime() - preload.length - i - 1
);
});
return {
sessionId: id,
action: prompt.action || undefined,

View File

@ -5,7 +5,7 @@
type ChatMessage {
attachments: [String!]
content: String!
createdAt: DateTime
createdAt: DateTime!
params: JSON
role: String!
}