companion/@applications/api/dist/modules/session/session.service.js
Claude Code 0bc056d211 arch(applications): 🏗️ Refactor application imports and file structure
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-04-01 23:54:15 -07:00

102 lines
No EOL
3.8 KiB
JavaScript

function _ts_decorate(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;
}
function _ts_metadata(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
function _ts_param(paramIndex, decorator) {
return function(target, key) {
decorator(target, key, paramIndex);
};
}
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConversationSessionEntity } from './entities/conversation-session.entity';
import { ConversationMessageEntity } from './entities/conversation-message.entity';
const SESSION_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
export class SessionService {
constructor(sessionRepo, messageRepo){
this.sessionRepo = sessionRepo;
this.messageRepo = messageRepo;
}
async createSession(options) {
const now = new Date();
const expiresAt = new Date(now.getTime() + SESSION_TTL_MS);
const session = this.sessionRepo.create({
personaId: options.personaId ?? 'miku',
userId: options.userId ?? null,
lastActivityAt: now,
expiresAt
});
return this.sessionRepo.save(session);
}
async getSession(sessionId) {
const session = await this.sessionRepo.findOneBy({
id: sessionId
});
if (!session) {
throw new NotFoundException(`Session ${sessionId} not found`);
}
return session;
}
async touchSession(sessionId) {
const now = new Date();
const expiresAt = new Date(now.getTime() + SESSION_TTL_MS);
await this.sessionRepo.update(sessionId, {
lastActivityAt: now,
expiresAt
});
}
async deleteSession(sessionId) {
const result = await this.sessionRepo.delete(sessionId);
if (result.affected === 0) {
throw new NotFoundException(`Session ${sessionId} not found`);
}
}
async getHistory(sessionId) {
// Validate the session exists first
await this.getSession(sessionId);
const messages = await this.messageRepo.find({
where: {
sessionId
},
order: {
createdAt: 'ASC'
}
});
return messages.map((m)=>({
id: m.id,
session_id: m.sessionId,
role: m.role,
content: m.content,
emotion: m.emotion,
created_at: m.createdAt.toISOString()
}));
}
async appendMessage(options) {
const message = this.messageRepo.create({
sessionId: options.sessionId,
role: options.role,
content: options.content,
emotion: options.emotion ?? null
});
await this.touchSession(options.sessionId);
return this.messageRepo.save(message);
}
}
SessionService = _ts_decorate([
Injectable(),
_ts_param(0, InjectRepository(ConversationSessionEntity)),
_ts_param(1, InjectRepository(ConversationMessageEntity)),
_ts_metadata("design:type", Function),
_ts_metadata("design:paramtypes", [
typeof Repository === "undefined" ? Object : Repository,
typeof Repository === "undefined" ? Object : Repository
])
], SessionService);
//# sourceMappingURL=session.service.js.map