extend chat model with CRUD actions for event changes

- Add ProposedEventChange type with create/update/delete actions
- Replace proposedEvent with proposedChange in ChatMessage
- Add currentDate to AIContext for time-aware AI responses
- Add AI test endpoint for development (/api/ai/test)
- Fix MongoUserRepository type safety with explicit toUser mapping
- Update CLAUDE.md documentation
This commit is contained in:
2026-01-03 19:37:27 +01:00
parent 105a9a4980
commit e553103470
7 changed files with 82 additions and 34 deletions

View File

@@ -45,6 +45,27 @@ app.get('/health', (_, res) => {
res.json({ status: 'ok' });
});
// AI Test endpoint (for development only)
app.post('/api/ai/test', async (req, res) => {
try {
const { message } = req.body;
if (!message) {
res.status(400).json({ error: 'message is required' });
return;
}
const result = await aiProvider.processMessage(message, {
userId: 'test-user',
conversationHistory: [],
existingEvents: [],
currentDate: new Date(),
});
res.json(result);
} catch (error) {
console.error('AI test error:', error);
res.status(500).json({ error: String(error) });
}
});
// Start server
async function start() {
try {

View File

@@ -1,19 +1,29 @@
import { User } from '@caldav/shared';
import { UserRepository, CreateUserData } from '../../services/interfaces';
import { UserModel } from './models';
import { UserModel, UserDocument } from './models';
export class MongoUserRepository implements UserRepository {
private toUser(doc: UserDocument): User {
return {
id: doc._id.toString(),
email: doc.email,
displayName: doc.displayName,
createdAt: doc.createdAt,
updatedAt: doc.updatedAt,
};
}
async findById(id: string): Promise<User | null> {
throw new Error('Not implemented');
}
async findByEmail(email: string): Promise<User | null> {
const user = await UserModel.findOne({ email: email.toLowerCase() });
return user ? user.toJSON() as User : null;
return user ? this.toUser(user) : null;
}
async create(data: CreateUserData): Promise<User> {
const user = await UserModel.create(data);
return user.toJSON() as User;
return this.toUser(user);
}
}

View File

@@ -1,10 +1,10 @@
import mongoose, { Schema, Document } from 'mongoose';
import { ChatMessage, Conversation, CreateEventDTO } from '@caldav/shared';
import { ChatMessage, Conversation, CreateEventDTO, UpdateEventDTO, ProposedEventChange } from '@caldav/shared';
export interface ChatMessageDocument extends Omit<ChatMessage, 'id'>, Document {}
export interface ConversationDocument extends Omit<Conversation, 'id'>, Document {}
const ProposedEventSchema = new Schema<CreateEventDTO>(
const EventSchema = new Schema<CreateEventDTO>(
{
title: { type: String, required: true },
description: { type: String },
@@ -17,6 +17,29 @@ const ProposedEventSchema = new Schema<CreateEventDTO>(
{ _id: false }
);
const UpdatesSchema = new Schema<UpdateEventDTO>(
{
title: { type: String },
description: { type: String },
startTime: { type: Date },
endTime: { type: Date },
note: { type: String },
isRecurring: { type: Boolean },
recurrenceRule: { type: String },
},
{ _id: false }
);
const ProposedChangeSchema = new Schema<ProposedEventChange>(
{
action: { type: String, enum: ['create', 'update', 'delete'], required: true },
eventId: { type: String },
event: { type: EventSchema },
updates: { type: UpdatesSchema },
},
{ _id: false }
);
const ChatMessageSchema = new Schema<ChatMessageDocument>(
{
conversationId: {
@@ -32,8 +55,8 @@ const ChatMessageSchema = new Schema<ChatMessageDocument>(
type: String,
required: true,
},
proposedEvent: {
type: ProposedEventSchema,
proposedChange: {
type: ProposedChangeSchema,
},
},
{

View File

@@ -1,14 +1,15 @@
import { CalendarEvent, ChatMessage, CreateEventDTO } from '@caldav/shared';
import { CalendarEvent, ChatMessage, ProposedEventChange } from '@caldav/shared';
export interface AIContext {
userId: string;
conversationHistory: ChatMessage[];
existingEvents: CalendarEvent[];
currentDate: Date;
}
export interface AIResponse {
content: string;
proposedEvent?: CreateEventDTO;
proposedChange?: ProposedEventChange;
}
export interface AIProvider {