-
Notifications
You must be signed in to change notification settings - Fork 0
feat: weekly report #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: weekly report #177
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,91 @@ | ||||||||||||||||||||||||
| import process from 'node:process' | ||||||||||||||||||||||||
| import { repository } from '@roll-stack/database' | ||||||||||||||||||||||||
| import { format } from 'date-fns' | ||||||||||||||||||||||||
| import { ru } from 'date-fns/locale/ru' | ||||||||||||||||||||||||
| import OpenAI from 'openai' | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const logger = useLogger('task:ai:weekly-report') | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export default defineTask({ | ||||||||||||||||||||||||
| meta: { | ||||||||||||||||||||||||
| name: 'ai:weekly-report', | ||||||||||||||||||||||||
| description: 'Prepare and post weekly report', | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| async run() { | ||||||||||||||||||||||||
| if (process.env.NODE_ENV !== 'production') { | ||||||||||||||||||||||||
| logger.info('Skipping task in non-production environment') | ||||||||||||||||||||||||
| return { result: true } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| const { ai } = useRuntimeConfig() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const tasks = await repository.task.listCompletedThisWeek() | ||||||||||||||||||||||||
| const staff = await repository.user.findStaff() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function preparePerformer(performerId: string | null) { | ||||||||||||||||||||||||
| const user = staff.find((user) => user.id === performerId) | ||||||||||||||||||||||||
| if (!user) { | ||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| name: user?.name, | ||||||||||||||||||||||||
| surname: user?.surname, | ||||||||||||||||||||||||
| caption: user?.caption, | ||||||||||||||||||||||||
| completedTasksCount: tasks.filter((task) => task.performerId === user?.id).length, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const preparedTasks = tasks.map((task) => { | ||||||||||||||||||||||||
| const performer = preparePerformer(task.performerId) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||
| completedAt: task.completedAt, | ||||||||||||||||||||||||
| name: task.name, | ||||||||||||||||||||||||
| description: task.description, | ||||||||||||||||||||||||
| report: task.report, | ||||||||||||||||||||||||
| performer, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const client = new OpenAI({ | ||||||||||||||||||||||||
| apiKey: ai.apiKey, | ||||||||||||||||||||||||
| baseURL: ai.baseUrl, | ||||||||||||||||||||||||
| timeout: 120000, | ||||||||||||||||||||||||
| maxRetries: 2, | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const response = await client.chat.completions.create({ | ||||||||||||||||||||||||
| model: ai.modelPro, | ||||||||||||||||||||||||
| messages: [ | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| role: 'system', | ||||||||||||||||||||||||
| content: ai.weeklyReportPrompt, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| role: 'user', | ||||||||||||||||||||||||
| content: JSON.stringify(preparedTasks), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const finalMessage = response.choices[0].message.content | ||||||||||||||||||||||||
| if (!finalMessage) { | ||||||||||||||||||||||||
| return { result: true } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Flow item | ||||||||||||||||||||||||
| const week = format(new Date(), 'w', { locale: ru }) | ||||||||||||||||||||||||
| await repository.flow.createItem({ | ||||||||||||||||||||||||
| type: 'weekly_task_report', | ||||||||||||||||||||||||
| title: `Задачи за неделю ${week}`, | ||||||||||||||||||||||||
| description: finalMessage, | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||
| errorResolver(error) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return { result: true } | ||||||||||||||||||||||||
|
Comment on lines
+85
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don’t mask failures; propagate to the scheduler. Currently returns success after errors, hiding incidents. Apply: - } catch (error) {
- errorResolver(error)
- }
-
- return { result: true }
+ } catch (error) {
+ errorResolver(error)
+ throw error
+ }
+
+ return { result: true }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PII to third‑party AI: anonymize or gate behind config.
preparedTasksincludes names/surnames/captions. Add an opt‑in (e.g., runtimeConfig.ai.allowPII) or hash/initials before sending.Example guard:
🤖 Prompt for AI Agents