Document.ts•2.71 kB
import { v4 as uuidv4 } from 'uuid';
/**
* 文档基本信息接口
*/
export interface IDocument {
id: string;
projectId: string;
name: string;
content: string;
type: string;
createdAt: Date;
updatedAt: Date;
metadata?: Record<string, any>;
}
/**
* 文档类型枚举
*/
export enum DocumentType {
PROJECT_BRIEF = 'projectbrief',
PRODUCT_CONTEXT = 'productContext',
ACTIVE_CONTEXT = 'activeContext',
SYSTEM_PATTERNS = 'systemPatterns',
TECH_CONTEXT = 'techContext',
PROGRESS = 'progress',
TASKS = 'tasks',
CUSTOM = 'custom'
}
/**
* 文档类,用于管理项目中的Markdown文档
*/
export class Document implements IDocument {
id: string;
projectId: string;
name: string;
content: string;
type: string;
createdAt: Date;
updatedAt: Date;
metadata?: Record<string, any>;
constructor(
projectId: string,
name: string,
content: string,
type: DocumentType | string,
metadata?: Record<string, any>
) {
this.id = uuidv4();
this.projectId = projectId;
this.name = name;
this.content = content;
this.type = type;
this.createdAt = new Date();
this.updatedAt = new Date();
this.metadata = metadata || {};
}
/**
* 从JSON对象创建文档实例
*/
static fromJSON(json: any): Document {
const doc = new Document(
json.projectId,
json.name,
json.content,
json.type,
json.metadata
);
doc.id = json.id;
doc.createdAt = new Date(json.createdAt);
doc.updatedAt = new Date(json.updatedAt);
return doc;
}
/**
* 转换为JSON对象
*/
toJSON(): IDocument {
return {
id: this.id,
projectId: this.projectId,
name: this.name,
content: this.content,
type: this.type,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
metadata: this.metadata
};
}
/**
* 更新文档内容
*/
update(content: string, metadata?: Record<string, any>): void {
this.content = content;
if (metadata) {
this.metadata = { ...this.metadata, ...metadata };
}
this.updatedAt = new Date();
}
/**
* 获取项目文档名
*/
static getFileName(type: DocumentType | string): string {
const typeMap: Record<string, string> = {
[DocumentType.PROJECT_BRIEF]: 'projectbrief.md',
[DocumentType.PRODUCT_CONTEXT]: 'productContext.md',
[DocumentType.ACTIVE_CONTEXT]: 'activeContext.md',
[DocumentType.SYSTEM_PATTERNS]: 'systemPatterns.md',
[DocumentType.TECH_CONTEXT]: 'techContext.md',
[DocumentType.PROGRESS]: 'progress.md',
[DocumentType.TASKS]: 'tasks.md'
};
return typeMap[type] || `${type}.md`;
}
}