Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 9f6a618

Browse files
committed
fix: update message object
Signed-off-by: James <namnh0122@gmail.com>
1 parent 473b442 commit 9f6a618

File tree

16 files changed

+169
-239
lines changed

16 files changed

+169
-239
lines changed

cortex-js/src/domain/models/assistant.interface.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { AssistantResponseFormatOption as OpenAIAssistantResponseFormatOption }
33

44
export interface Assistant extends OpenAiAssistant {
55
avatar?: string;
6-
7-
response_format?: AssistantResponseFormatOption;
8-
9-
tool_resources?: AssistantToolResources;
106
}
117

128
export type AssistantResponseFormatOption = OpenAIAssistantResponseFormatOption;
Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,16 @@
1-
export enum ChatCompletionRole {
2-
System = 'system',
3-
Assistant = 'assistant',
4-
User = 'user',
5-
}
1+
import {
2+
Message as OpenAiMessage,
3+
MessageContent as OpenAiMessageContent,
4+
TextContentBlock as OpenAiTextContentBlock,
5+
} from 'openai/resources/beta/threads/messages';
66

7-
export enum ContentType {
8-
Text = 'text',
9-
Image = 'image',
10-
Pdf = 'pdf',
11-
}
7+
export interface Message extends OpenAiMessage {}
128

13-
export interface ContentValue {
14-
value: string;
15-
annotations: string[];
16-
name?: string;
17-
size?: number;
18-
}
9+
export type MessageContent = OpenAiMessageContent;
1910

20-
export interface ThreadContent {
21-
type: ContentType;
22-
text: ContentValue;
23-
}
11+
export type TextContentBlock = OpenAiTextContentBlock;
2412

25-
/**
26-
* The status of the message.
27-
* @data_transfer_object
28-
*/
29-
export enum MessageStatus {
30-
/** Message is fully loaded. **/
31-
Ready = 'ready',
32-
/** Message is not fully loaded. **/
33-
Pending = 'pending',
34-
/** Message loaded with error. **/
35-
Error = 'error',
36-
/** Message is cancelled streaming */
37-
Stopped = 'stopped',
38-
}
13+
export interface MessageIncompleteDetails
14+
extends OpenAiMessage.IncompleteDetails {}
3915

40-
/**
41-
* The error code which explain what error type. Used in conjunction with MessageStatus.Error
42-
*/
43-
export enum ErrorCode {
44-
InvalidApiKey = 'invalid_api_key',
45-
46-
InsufficientQuota = 'insufficient_quota',
47-
48-
InvalidRequestError = 'invalid_request_error',
49-
50-
Unknown = 'unknown',
51-
}
52-
53-
export interface Message {
54-
/** Unique identifier for the message, generated by default using the ULID method. **/
55-
id: string;
56-
/** Object name **/
57-
object: string;
58-
/** Thread id, default is a ulid. **/
59-
thread_id: string;
60-
/** The assistant id of this thread. **/
61-
assistant_id?: string;
62-
/** The role of the author of this message. **/
63-
role: ChatCompletionRole;
64-
/** The content of this message. **/
65-
content: ThreadContent[];
66-
/** The status of this message. **/
67-
status: MessageStatus;
68-
/** The timestamp indicating when this message was created. Represented in Unix time. **/
69-
created: number;
70-
/** The timestamp indicating when this message was updated. Represented in Unix time. **/
71-
updated?: number;
72-
/** The additional metadata of this message. **/
73-
metadata?: MessageMetadata;
74-
/** The error code which explain what error type. Used in conjunction with MessageStatus.Error */
75-
error_code?: ErrorCode;
76-
}
77-
78-
/**
79-
* The additional metadata of this message.
80-
*/
81-
export interface MessageMetadata {}
16+
export interface MessageAttachment extends OpenAiMessage.Attachment {}

cortex-js/src/infrastructure/commanders/usecases/chat.cli.usecases.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import {
2-
ChatCompletionRole,
3-
ContentType,
4-
MessageStatus,
5-
} from '@/domain/models/message.interface';
61
import { exit, stdin, stdout } from 'node:process';
72
import * as readline from 'node:readline/promises';
83
import { ChatCompletionMessage } from '@/infrastructure/dtos/chat/chat-completion-message.dto';
@@ -19,6 +14,7 @@ import { CreateMessageDto } from '@/infrastructure/dtos/messages/create-message.
1914
import { MessagesUsecases } from '@/usecases/messages/messages.usecases';
2015
import { ModelParameterParser } from '@/utils/model-parameter.parser';
2116
import { ChatUsecases } from '@/usecases/chat/chat.usecases';
17+
import { TextContentBlock } from '@/domain/models/message.interface';
2218

2319
@Injectable()
2420
export class ChatCliUsecases {
@@ -46,7 +42,7 @@ export class ChatCliUsecases {
4642
const messages: ChatCompletionMessage[] = (
4743
await this.messagesUsecases.getLastMessagesByThread(thread.id, 10)
4844
).map((message) => ({
49-
content: message.content[0].text.value,
45+
content: (message.content[0] as TextContentBlock).text.value,
5046
role: message.role,
5147
}));
5248

@@ -76,22 +72,22 @@ export class ChatCliUsecases {
7672

7773
messages.push({
7874
content: userInput,
79-
role: ChatCompletionRole.User,
75+
role: 'user',
8076
});
8177

8278
const createMessageDto: CreateMessageDto = {
8379
thread_id: thread.id,
84-
role: ChatCompletionRole.User,
80+
role: 'user',
8581
content: [
8682
{
87-
type: ContentType.Text,
83+
type: 'text',
8884
text: {
8985
value: userInput,
9086
annotations: [],
9187
},
9288
},
9389
],
94-
status: MessageStatus.Ready,
90+
status: 'completed',
9591
};
9692
this.messagesUsecases.create(createMessageDto);
9793

@@ -127,22 +123,22 @@ export class ChatCliUsecases {
127123
stdout.write(assistantResponse);
128124
messages.push({
129125
content: assistantResponse,
130-
role: ChatCompletionRole.Assistant,
126+
role: 'assistant',
131127
});
132128

133129
const createMessageDto: CreateMessageDto = {
134130
thread_id: thread.id,
135-
role: ChatCompletionRole.Assistant,
131+
role: 'assistant',
136132
content: [
137133
{
138-
type: ContentType.Text,
134+
type: 'text',
139135
text: {
140136
value: assistantResponse,
141137
annotations: [],
142138
},
143139
},
144140
],
145-
status: MessageStatus.Ready,
141+
status: 'completed',
146142
};
147143

148144
this.messagesUsecases.create(createMessageDto).then(() => {
@@ -164,21 +160,21 @@ export class ChatCliUsecases {
164160
response.on('end', () => {
165161
messages.push({
166162
content: assistantResponse,
167-
role: ChatCompletionRole.Assistant,
163+
role: 'assistant',
168164
});
169165
const createMessageDto: CreateMessageDto = {
170166
thread_id: thread.id,
171-
role: ChatCompletionRole.Assistant,
167+
role: 'assistant',
172168
content: [
173169
{
174-
type: ContentType.Text,
170+
type: 'text',
175171
text: {
176172
value: assistantResponse,
177173
annotations: [],
178174
},
179175
},
180176
],
181-
status: MessageStatus.Ready,
177+
status: 'completed',
182178
};
183179

184180
this.messagesUsecases.create(createMessageDto).then(() => {

cortex-js/src/infrastructure/dtos/assistants/create-assistant.dto.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IsArray, IsOptional, IsString } from 'class-validator';
1+
import { IsArray, IsNumber, IsOptional, IsString } from 'class-validator';
22
import { Assistant } from '@/domain/models/assistant.interface';
33
import { ApiProperty } from '@nestjs/swagger';
44

@@ -63,4 +63,22 @@ export class CreateAssistantDto implements Partial<Assistant> {
6363
})
6464
@IsOptional()
6565
metadata: unknown | null;
66+
67+
@ApiProperty({
68+
description: 'Top p.',
69+
example: '0.7',
70+
default: '0.7',
71+
})
72+
@IsOptional()
73+
@IsNumber()
74+
top_p?: number;
75+
76+
@ApiProperty({
77+
description: 'Temperature.',
78+
example: '0.7',
79+
default: '0.7',
80+
})
81+
@IsOptional()
82+
@IsNumber()
83+
temperature?: number;
6684
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { IsEnum, IsString } from 'class-validator';
2-
import { ChatCompletionRole } from '@/domain/models/message.interface';
1+
import { IsString } from 'class-validator';
32
import { ApiProperty } from '@nestjs/swagger';
43

54
export class ChatCompletionMessage {
@@ -10,6 +9,5 @@ export class ChatCompletionMessage {
109
@ApiProperty({
1110
description: 'The role of the entity in the chat completion.',
1211
})
13-
@IsEnum(ChatCompletionRole)
14-
role: ChatCompletionRole;
12+
role: 'user' | 'assistant';
1513
}

cortex-js/src/infrastructure/dtos/messages/content-value.dto.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsArray, IsEnum, IsString, ValidateNested } from 'class-validator';
3-
import {
4-
ChatCompletionRole,
5-
ErrorCode,
6-
Message,
7-
MessageStatus,
8-
} from '@/domain/models/message.interface';
9-
import { ThreadContentDto } from './thread-content.dto';
10-
import { Type } from 'class-transformer';
2+
import { IsArray, IsString } from 'class-validator';
3+
import { Message, MessageContent } from '@/domain/models/message.interface';
114

125
export class CreateMessageDto implements Partial<Message> {
136
@ApiProperty({
@@ -21,18 +14,14 @@ export class CreateMessageDto implements Partial<Message> {
2114
assistant_id?: string;
2215

2316
@ApiProperty({ description: 'The sources of the messages.' })
24-
@IsEnum(ChatCompletionRole)
25-
role: ChatCompletionRole;
17+
role: 'user' | 'assistant';
2618

2719
@ApiProperty({ description: 'The content of the messages.' })
2820
@IsArray()
29-
@ValidateNested({ each: true })
30-
@Type(() => ThreadContentDto)
31-
content: ThreadContentDto[];
21+
content: MessageContent[];
3222

3323
@ApiProperty({ description: 'Current status of the message.' })
34-
@IsEnum(MessageStatus)
35-
status: MessageStatus;
24+
status: 'in_progress' | 'incomplete' | 'completed';
3625

3726
@ApiProperty({
3827
description:
@@ -43,8 +32,4 @@ export class CreateMessageDto implements Partial<Message> {
4332
@ApiProperty({ description: 'Type of the message.' })
4433
@IsString()
4534
type?: string;
46-
47-
@ApiProperty({ description: 'Specifies the cause of any error.' })
48-
@IsEnum(ErrorCode)
49-
error_code?: ErrorCode;
5035
}

cortex-js/src/infrastructure/dtos/messages/thread-content.dto.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

cortex-js/src/infrastructure/dtos/threads/create-message.dto.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { ChatCompletionRole } from '@/domain/models/message.interface';
21
import { ApiProperty } from '@nestjs/swagger';
3-
import { IsEnum } from 'class-validator';
42

53
export class CreateMessageDto {
64
@ApiProperty({
75
description: `The role of the entity that is creating the message. Allowed values include:
86
- user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
97
- assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.`,
108
})
11-
@IsEnum(ChatCompletionRole)
12-
role: ChatCompletionRole;
9+
role: 'user' | 'assistant';
1310

1411
@ApiProperty({
1512
description: 'The text contents of the message.',

cortex-js/src/infrastructure/dtos/threads/create-thread-assistant.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { IsArray, IsNumber, IsOptional, IsString } from 'class-validator';
22
import { ApiProperty } from '@nestjs/swagger';
33
import {
44
Assistant,
5+
AssistantResponseFormatOption,
56
AssistantToolResources,
67
} from '@/domain/models/assistant.interface';
7-
import { AssistantResponseFormatOption } from 'openai/resources/beta/threads/threads';
88

99
export class CreateThreadAssistantDto implements Assistant {
1010
@ApiProperty({

0 commit comments

Comments
 (0)