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
3 changed files with 12 additions and 5 deletions

View File

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

View File

@@ -18,7 +18,6 @@ import {
getTokenEncoder, getTokenEncoder,
ListHistoriesOptions, ListHistoriesOptions,
PromptMessage, PromptMessage,
PromptMessageSchema,
PromptParams, PromptParams,
SubmittedMessage, SubmittedMessage,
} from './types'; } from './types';
@@ -324,6 +323,7 @@ export class ChatSessionService {
role: true, role: true,
content: true, content: true,
params: true, params: true,
createdAt: true,
}, },
orderBy: { orderBy: {
createdAt: 'asc', createdAt: 'asc',
@@ -338,7 +338,7 @@ export class ChatSessionService {
Promise.all( Promise.all(
sessions.map(async ({ id, promptName, messages, createdAt }) => { sessions.map(async ({ id, promptName, messages, createdAt }) => {
try { try {
const ret = PromptMessageSchema.array().safeParse(messages); const ret = ChatMessageSchema.array().safeParse(messages);
if (ret.success) { if (ret.success) {
const prompt = await this.prompt.get(promptName); const prompt = await this.prompt.get(promptName);
if (!prompt) { if (!prompt) {
@@ -356,6 +356,13 @@ export class ChatSessionService {
.filter(({ role }) => role !== 'system') .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 { return {
sessionId: id, sessionId: id,
action: prompt.action || undefined, action: prompt.action || undefined,

View File

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