mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
Add advanced ai
This commit is contained in:
67
dashboard/server/ai/functions/AI_ComposableChart.ts
Normal file
67
dashboard/server/ai/functions/AI_ComposableChart.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
import { AIPlugin } from "../Plugin";
|
||||
|
||||
export class AiComposableChart extends AIPlugin<['createComposableChart']> {
|
||||
constructor() {
|
||||
super({
|
||||
'createComposableChart': {
|
||||
handler: (data: { labels: string, points: number[] }) => {
|
||||
return { ok: true };
|
||||
},
|
||||
tool: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'createComposableChart',
|
||||
description: 'Creates a chart based on the provided datasets',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
labels: {
|
||||
type: 'array',
|
||||
items: { type: 'string' },
|
||||
description: 'Labels for each data point in the chart'
|
||||
},
|
||||
title: {
|
||||
type: 'string',
|
||||
description: 'Title of the chart to let user understand what is displaying, not include dates'
|
||||
},
|
||||
datasets: {
|
||||
type: 'array',
|
||||
description: 'List of datasets',
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
chartType: {
|
||||
type: 'string',
|
||||
enum: ['line', 'bar'],
|
||||
description: 'The type of chart to display the dataset, either "line" or "bar"'
|
||||
},
|
||||
points: {
|
||||
type: 'array',
|
||||
items: { type: 'number' },
|
||||
description: 'Numerical values for each data point in the chart'
|
||||
},
|
||||
color: {
|
||||
type: 'string',
|
||||
description: 'Color used to represent the dataset'
|
||||
},
|
||||
name: {
|
||||
type: 'string',
|
||||
description: 'Name of the dataset'
|
||||
}
|
||||
},
|
||||
required: ['points', 'color', 'chartType', 'name'],
|
||||
description: 'Data points and style information for the dataset'
|
||||
}
|
||||
}
|
||||
},
|
||||
required: ['labels', 'datasets', 'title']
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const AiComposableChartInstance = new AiComposableChart();
|
||||
80
dashboard/server/ai/functions/AI_Events.ts
Normal file
80
dashboard/server/ai/functions/AI_Events.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { EventModel } from "@schema/metrics/EventSchema";
|
||||
import { executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
import { Types } from "mongoose";
|
||||
import { AIPlugin, AIPlugin_TTool } from "../Plugin";
|
||||
|
||||
|
||||
const getEventsCountTool: AIPlugin_TTool<'getEventsCount'> = {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'getEventsCount',
|
||||
description: 'Gets the number of events received on a date range, can also specify the event name and the metadata associated',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
from: { type: 'string', description: 'ISO string of start date including hours' },
|
||||
to: { type: 'string', description: 'ISO string of end date including hours' },
|
||||
name: { type: 'string', description: 'Name of the events to get' },
|
||||
metadata: { type: 'object', description: 'Metadata of events to get' },
|
||||
},
|
||||
required: ['from', 'to']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getEventsTimelineTool: AIPlugin_TTool<'getEventsTimeline'> = {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'getEventsTimeline',
|
||||
description: 'Gets an array of date and count for events received on a date range. Should be used to create charts.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
from: { type: 'string', description: 'ISO string of start date including hours' },
|
||||
to: { type: 'string', description: 'ISO string of end date including hours' },
|
||||
},
|
||||
required: ['from', 'to']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class AiEvents extends AIPlugin<['getEventsCount', 'getEventsTimeline']> {
|
||||
|
||||
constructor() {
|
||||
|
||||
super({
|
||||
'getEventsCount': {
|
||||
handler: async (data: { project_id: string, from?: string, to?: string, name?: string, metadata?: string }) => {
|
||||
const query: any = {
|
||||
project_id: data.project_id,
|
||||
created_at: {
|
||||
$gt: data.from ? new Date(data.from).getTime() : new Date(2023).getTime(),
|
||||
$lt: data.to ? new Date(data.to).getTime() : new Date().getTime(),
|
||||
}
|
||||
}
|
||||
if (data.metadata) query.metadata = data.metadata;
|
||||
if (data.name) query.name = data.name;
|
||||
const result = await EventModel.countDocuments(query);
|
||||
return { count: result };
|
||||
},
|
||||
tool: getEventsCountTool
|
||||
},
|
||||
'getEventsTimeline': {
|
||||
handler: async (data: { project_id: string, from: string, to: string }) => {
|
||||
const timelineData = await executeTimelineAggregation({
|
||||
projectId: new Types.ObjectId(data.project_id) as any,
|
||||
model: EventModel,
|
||||
from: data.from, to: data.to, slice: 'day'
|
||||
});
|
||||
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, 'day', data.from, data.to);
|
||||
return { data: timelineFilledMerged };
|
||||
},
|
||||
tool: getEventsTimelineTool
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const AiEventsInstance = new AiEvents();
|
||||
|
||||
87
dashboard/server/ai/functions/AI_Visits.ts
Normal file
87
dashboard/server/ai/functions/AI_Visits.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
import { AdvancedTimelineAggregationOptions, executeAdvancedTimelineAggregation, executeTimelineAggregation, fillAndMergeTimelineAggregationV2 } from "~/server/services/TimelineService";
|
||||
import { Types } from "mongoose";
|
||||
import { AIPlugin, AIPlugin_TTool } from "../Plugin";
|
||||
|
||||
|
||||
const getVisitsCountsTool: AIPlugin_TTool<'getVisitsCount'> = {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'getVisitsCount',
|
||||
description: 'Gets the number of visits received on a date range',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
from: { type: 'string', description: 'ISO string of start date including hours' },
|
||||
to: { type: 'string', description: 'ISO string of end date including hours' },
|
||||
website: { type: 'string', description: 'The website of the visits' },
|
||||
page: { type: 'string', description: 'The page of the visit' }
|
||||
},
|
||||
required: ['from', 'to']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getVisitsTimelineTool: AIPlugin_TTool<'getVisitsTimeline'> = {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'getVisitsTimeline',
|
||||
description: 'Gets an array of date and count for events received on a date range. Should be used to create charts.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
from: { type: 'string', description: 'ISO string of start date including hours' },
|
||||
to: { type: 'string', description: 'ISO string of end date including hours' },
|
||||
website: { type: 'string', description: 'The website of the visits' },
|
||||
page: { type: 'string', description: 'The page of the visit' }
|
||||
},
|
||||
required: ['from', 'to']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class AiVisits extends AIPlugin<['getVisitsCount', 'getVisitsTimeline']> {
|
||||
|
||||
constructor() {
|
||||
|
||||
super({
|
||||
'getVisitsCount': {
|
||||
handler: async (data: { project_id: string, from?: string, to?: string, website?: string, page?: string }) => {
|
||||
const query: any = {
|
||||
project_id: data.project_id,
|
||||
created_at: {
|
||||
$gt: data.from ? new Date(data.from).getTime() : new Date(2023).getTime(),
|
||||
$lt: data.to ? new Date(data.to).getTime() : new Date().getTime(),
|
||||
}
|
||||
}
|
||||
if (data.website) query.website = data.website;
|
||||
if (data.page) query.page = data.page;
|
||||
const result = await VisitModel.countDocuments(query);
|
||||
return { count: result };
|
||||
},
|
||||
tool: getVisitsCountsTool
|
||||
},
|
||||
'getVisitsTimeline': {
|
||||
handler: async (data: { project_id: string, from: string, to: string, website?: string, page?: string }) => {
|
||||
const query: AdvancedTimelineAggregationOptions & { customMatch: Record<string, any> } = {
|
||||
projectId: new Types.ObjectId(data.project_id) as any,
|
||||
model: VisitModel,
|
||||
from: data.from, to: data.to, slice: 'day',
|
||||
customMatch: {}
|
||||
}
|
||||
|
||||
if (data.website) query.customMatch.website = data.website;
|
||||
if (data.page) query.customMatch.page = data.page;
|
||||
|
||||
const timelineData = await executeAdvancedTimelineAggregation(query);
|
||||
const timelineFilledMerged = fillAndMergeTimelineAggregationV2(timelineData, 'day', data.from, data.to);
|
||||
return { data: timelineFilledMerged };
|
||||
},
|
||||
tool: getVisitsTimelineTool
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const AiVisitsInstance = new AiVisits();
|
||||
Reference in New Issue
Block a user