@wearesage/schema
Version:
A flexible schema definition and validation system for TypeScript with multi-database support
141 lines (117 loc) • 3.41 kB
text/typescript
import { Conversation } from './Conversation';
import { Embeddings } from '../adapters/embeddings';
import { Entity, Property, Id, ManyToOne, Index, Timestamp, Auth , Labels } from '../core/decorators';
import { RelationshipType } from '../adapters/neo4j';
import { PostgresMetadata } from '../adapters/metadata-layer';
export class Message {
id!: string;
// The conversation this message belongs to
conversation!: Conversation;
// Message content
content!: string;
role!: 'user' | 'assistant' | 'system';
// Sender information (who sent this message)
sender?: {
type: 'human' | 'ai';
id: string; // userId or model name
displayName?: string; // For display purposes
};
// AI-specific properties
thinking?: string; // AI's thinking process (for assistant messages)
model?: string; // Model used for this specific message
temperature?: number; // Temperature used for this message
responseTime?: number; // Time taken to generate response (in milliseconds)
tokenCount?: number; // Number of tokens in this message
// Message ordering
messageIndex!: number; // Order of message in conversation (0, 1, 2, ...)
// Message metadata
isEdited?: boolean; // Whether this message was edited after creation
editHistory?: Array<{
content: string;
editedAt: Date;
reason?: string;
}>;
createdAt!: Date;
updatedAt!: Date;
// Semantic embedding for content similarity search
contentEmbedding?: number[];
// Constructor
constructor() {
this.messageIndex = 0;
this.isEdited = false;
}
// Helper methods
editContent(newContent: string, reason?: string): void {
if (!this.editHistory) {
this.editHistory = [];
}
this.editHistory.push({
content: this.content,
editedAt: new Date(),
reason
});
this.content = newContent;
this.isEdited = true;
}
isAssistantMessage(): boolean {
return this.role === 'assistant';
}
isUserMessage(): boolean {
return this.role === 'user';
}
hasThinking(): boolean {
return !!(this.thinking && this.thinking.trim());
}
getAuthorDisplay(): string {
switch (this.role) {
case 'user':
return this.sender?.displayName || 'You';
case 'assistant':
return this.model || 'Assistant';
case 'system':
return 'System';
default:
return 'Unknown';
}
}
}