-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
178 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Body, Controller, Post, UseGuards } from '@nestjs/common'; | ||
import { ReccurenceInput } from '@sigma/types'; | ||
|
||
import { AuthGuard } from 'modules/auth/auth.guard'; | ||
import { Workspace } from 'modules/auth/session.decorator'; | ||
|
||
import TasksAIService from './tasks-ai.service'; | ||
|
||
@Controller({ | ||
version: '1', | ||
path: 'tasks/ai', | ||
}) | ||
export class TasksAIController { | ||
constructor(private tasksAiService: TasksAIService) {} | ||
|
||
@Post('recurrence') | ||
@UseGuards(AuthGuard) | ||
async aiFilters( | ||
@Body() reccurenceInput: ReccurenceInput, | ||
@Workspace() workspaceId: string, | ||
) { | ||
return await this.tasksAiService.recurrence(reccurenceInput, workspaceId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { LLMModelEnum, ReccurenceInput, recurrencePrompt } from '@sigma/types'; | ||
|
||
import AIRequestsService from 'modules/ai-requests/ai-requests.services'; | ||
import { LoggerService } from 'modules/logger/logger.service'; | ||
|
||
@Injectable() | ||
export default class TasksAIService { | ||
private readonly logger: LoggerService = new LoggerService('TasksAIService'); | ||
|
||
constructor(private aiRequestsService: AIRequestsService) {} | ||
|
||
async recurrence(reccurenceInput: ReccurenceInput, workspaceId: string) { | ||
const recurrenceOutput = await this.aiRequestsService.getLLMRequest( | ||
{ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: recurrencePrompt | ||
.replace('{{text}}', reccurenceInput.text) | ||
.replace('{{currentTime}}', reccurenceInput.currentTime), | ||
}, | ||
], | ||
llmModel: LLMModelEnum.CLAUDESONNET, | ||
model: 'recurrence', | ||
}, | ||
workspaceId, | ||
); | ||
const outputMatch = recurrenceOutput.match(/<output>(.*?)<\/output>/s); | ||
|
||
if (!outputMatch) { | ||
this.logger.error({ | ||
message: 'No output found in recurrence response', | ||
where: `TasksAIService.recurrence`, | ||
}); | ||
throw new Error('Invalid response format from AI'); | ||
} | ||
|
||
try { | ||
const jsonStr = outputMatch[1].trim(); | ||
const parsedOutput = JSON.parse(jsonStr); | ||
return parsedOutput; | ||
} catch (error) { | ||
this.logger.error({ | ||
message: 'Failed to parse recurrence JSON output', | ||
payload: error, | ||
where: `TasksAIService.recurrence`, | ||
}); | ||
throw new Error('Invalid JSON in AI response'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters