Skip to content

Commit ee14c20

Browse files
committed
added ai service, some refactorng
1 parent 018b70a commit ee14c20

File tree

7 files changed

+49
-23
lines changed

7 files changed

+49
-23
lines changed
File renamed without changes.
File renamed without changes.

src/models/eventsFactory.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const mongo = require('../mongo');
77
const Event = require('../models/event');
88
const { ObjectID } = require('mongodb');
99
const { composeEventPayloadByRepetition } = require('../utils/merge');
10-
const { vercelAIApi } = require('../vercel-ai');
1110

1211
const MAX_DB_READ_BATCH_SIZE = Number(process.env.MAX_DB_READ_BATCH_SIZE);
1312

@@ -179,27 +178,6 @@ class EventsFactory extends Factory {
179178
return new Event(searchResult);
180179
}
181180

182-
/**
183-
* Generate AI suggestion for the event
184-
*
185-
* @param {string} projectId - event's project
186-
* @param {string} eventId - event id
187-
* @param {string} originalEventId - original event id
188-
* @returns {Promise<string>} AI suggestion for the event
189-
*/
190-
async aiSuggestion(eventId, originalEventId) {
191-
const repetition = await this.getEventRepetition(eventId, originalEventId);
192-
193-
if (!repetition) {
194-
throw new Error('Repetition not found');
195-
}
196-
197-
const payload = repetition.payload;
198-
const solution = await vercelAIApi.generateSuggestion(payload);
199-
200-
return solution;
201-
}
202-
203181
/**
204182
* Returns events that grouped by day
205183
*

src/resolvers/event.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const getEventsFactory = require('./helpers/eventsFactory').default;
22
const sendPersonalNotification = require('../utils/personalNotifications').default;
3+
const { aiService } = require('../services/ai');
34

45
/**
56
* See all types and fields here {@see ../typeDefs/event.graphql}
@@ -100,7 +101,7 @@ module.exports = {
100101
async aiSuggestion({ projectId, _id: eventId, originalEventId }, _args, context) {
101102
const factory = getEventsFactory(context, projectId);
102103

103-
return factory.aiSuggestion(eventId, originalEventId);
104+
return aiService.generateSuggestion(factory, eventId, originalEventId);
104105
},
105106

106107
/**

src/services/ai.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { vercelAIApi } from '../integrations/vercel-ai/';
2+
import { EventsFactoryInterface } from './types';
3+
4+
/**
5+
* Service for interacting with AI
6+
*/
7+
export class AIService {
8+
/**
9+
* Generate suggestion for the event
10+
*
11+
* @param eventsFactory - events factory
12+
* @param eventId - event id
13+
* @param originalEventId - original event id
14+
* @returns {Promise<string>} - suggestion
15+
*/
16+
public async generateSuggestion(eventsFactory: EventsFactoryInterface, eventId: string, originalEventId: string): Promise<string> {
17+
const event = await eventsFactory.getEventRepetition(eventId, originalEventId);
18+
19+
if (!event) {
20+
throw new Error('Event not found');
21+
}
22+
23+
return vercelAIApi.generateSuggestion(event.payload);
24+
}
25+
}
26+
27+
export const aiService = new AIService();

src/services/types.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { EventAddons, EventData } from '@hawk.so/types';
2+
3+
type Event = {
4+
_id: string;
5+
payload: EventData<EventAddons>;
6+
};
7+
8+
/**
9+
* Interface for events factory
10+
*/
11+
export interface EventsFactoryInterface {
12+
/**
13+
* Get event repetition
14+
*
15+
* @param repetitionId - repetition id
16+
* @param originalEventId - original event id
17+
* @returns {Promise<EventData<EventAddons>>} - event repetition
18+
*/
19+
getEventRepetition(repetitionId: string, originalEventId: string): Promise<Event>;
20+
}

0 commit comments

Comments
 (0)