UNPKG

@codervisor/devlog-core

Version:

Core devlog management functionality

185 lines (184 loc) 7.05 kB
/** * TypeORM entity for chat sessions * Maps to the ChatSession interface and chat_sessions table */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var ChatSessionEntity_1; import 'reflect-metadata'; import { Column, Entity, Index, PrimaryColumn } from 'typeorm'; import { JsonColumn, getStorageType } from './decorators.js'; /** * Chat session entity matching the ChatSession interface */ let ChatSessionEntity = ChatSessionEntity_1 = class ChatSessionEntity { id; agent; timestamp; // ISO string workspace; workspacePath; title; status; messageCount; duration; metadata; tags; importedAt; // ISO string updatedAt; // ISO string archived; linkedDevlogs; /** * Convert entity to ChatSession interface */ toChatSession() { return { id: this.id, agent: this.agent, timestamp: this.timestamp, workspace: this.workspace, workspacePath: this.workspacePath, title: this.title, status: this.status, messageCount: this.messageCount, duration: this.duration, metadata: this.parseJsonField(this.metadata, {}), tags: this.parseJsonField(this.tags, []), importedAt: this.importedAt, updatedAt: this.updatedAt, linkedDevlogs: this.parseJsonField(this.linkedDevlogs, []), archived: this.archived, }; } /** * Create entity from ChatSession interface */ static fromChatSession(session) { const entity = new ChatSessionEntity_1(); entity.id = session.id; entity.agent = session.agent; entity.timestamp = session.timestamp; entity.workspace = session.workspace; entity.workspacePath = session.workspacePath; entity.title = session.title; entity.status = session.status || 'imported'; entity.messageCount = session.messageCount || 0; entity.duration = session.duration; entity.metadata = entity.stringifyJsonField(session.metadata || {}); entity.tags = entity.stringifyJsonField(session.tags || []); entity.importedAt = session.importedAt; entity.updatedAt = session.updatedAt; entity.linkedDevlogs = entity.stringifyJsonField(session.linkedDevlogs || []); entity.archived = session.archived || false; return entity; } /** * Helper method for JSON field parsing (database-specific) */ parseJsonField(value, defaultValue) { if (value === null || value === undefined) { return defaultValue; } // For SQLite, values are stored as text and need parsing if (getStorageType() === 'sqlite' && typeof value === 'string') { try { return JSON.parse(value); } catch { return defaultValue; } } // For PostgreSQL and MySQL, JSON fields are handled natively return value; } /** * Helper method for JSON field stringification (database-specific) */ stringifyJsonField(value) { if (value === null || value === undefined) { return value; } // For SQLite, we need to stringify JSON data if (getStorageType() === 'sqlite') { return typeof value === 'string' ? value : JSON.stringify(value); } // For PostgreSQL and MySQL, return the object directly return value; } }; __decorate([ PrimaryColumn({ type: 'varchar', length: 255 }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "id", void 0); __decorate([ Column({ type: 'varchar', length: 100 }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "agent", void 0); __decorate([ Column({ type: 'varchar', length: 255 }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "timestamp", void 0); __decorate([ Column({ type: 'varchar', length: 500, nullable: true }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "workspace", void 0); __decorate([ Column({ type: 'varchar', length: 1000, nullable: true, name: 'workspace_path' }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "workspacePath", void 0); __decorate([ Column({ type: 'varchar', length: 500, nullable: true }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "title", void 0); __decorate([ Column({ type: 'varchar', length: 50, default: 'imported' }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "status", void 0); __decorate([ Column({ type: 'integer', default: 0, name: 'message_count' }), __metadata("design:type", Number) ], ChatSessionEntity.prototype, "messageCount", void 0); __decorate([ Column({ type: 'integer', nullable: true }), __metadata("design:type", Number) ], ChatSessionEntity.prototype, "duration", void 0); __decorate([ JsonColumn({ default: getStorageType() === 'sqlite' ? '{}' : {} }), __metadata("design:type", Object) ], ChatSessionEntity.prototype, "metadata", void 0); __decorate([ JsonColumn({ default: getStorageType() === 'sqlite' ? '[]' : [] }), __metadata("design:type", Array) ], ChatSessionEntity.prototype, "tags", void 0); __decorate([ Column({ type: 'varchar', length: 255, name: 'imported_at' }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "importedAt", void 0); __decorate([ Column({ type: 'varchar', length: 255, name: 'updated_at' }), __metadata("design:type", String) ], ChatSessionEntity.prototype, "updatedAt", void 0); __decorate([ Column({ type: 'boolean', default: false }), __metadata("design:type", Boolean) ], ChatSessionEntity.prototype, "archived", void 0); __decorate([ JsonColumn({ default: getStorageType() === 'sqlite' ? '[]' : [], name: 'linked_devlogs' }), __metadata("design:type", Array) ], ChatSessionEntity.prototype, "linkedDevlogs", void 0); ChatSessionEntity = ChatSessionEntity_1 = __decorate([ Entity('chat_sessions'), Index(['agent']), Index(['timestamp']), Index(['workspace']), Index(['status']), Index(['importedAt']), Index(['archived']) ], ChatSessionEntity); export { ChatSessionEntity };