@@ -14,6 +14,8 @@ import { RepositoryRepository } from "src/repository/repository";
14
14
import { SearchService } from "src/search/service" ;
15
15
import { Service } from "typedi" ;
16
16
import { TagRepository } from "src/tag/repository" ;
17
+ import { AIService } from "src/ai/service" ;
18
+ import { AIResponseTranslateNameDto , AIResponseTranslateTitleDto } from "./dto" ;
17
19
18
20
@Service ( )
19
21
export class DigestCron {
@@ -29,7 +31,8 @@ export class DigestCron {
29
31
private readonly contributionsRepository : ContributionRepository ,
30
32
private readonly contributorsRepository : ContributorRepository ,
31
33
private readonly searchService : SearchService ,
32
- private readonly tagsRepository : TagRepository ,
34
+ private readonly tagRepository : TagRepository ,
35
+ private readonly aiService : AIService ,
33
36
) {
34
37
const SentryCronJob = cron . instrumentCron ( CronJob , "DigestCron" ) ;
35
38
new SentryCronJob (
@@ -81,10 +84,29 @@ export class DigestCron {
81
84
// or uncomment to skip the cron
82
85
// if (Math.random()) return;
83
86
87
+ const projectTitleSystemPrompt = `user will give you an open-source project name, and you will translate it to Arabic.` ;
88
+ const contributorNameSystemPrompt = `user will give you an open-source contributor name, and you will translate it to Arabic.
89
+ if the name contain both english and arabic only keep the parts related to the language.` ;
90
+ const issueTitleSystemPrompt = `user will give you an open-source issue/PR title, and you will translate it to Arabic.` ;
91
+
84
92
for ( const project of projectsFromDataFolder ) {
85
- // todo: call AIService
86
- const name_en = project . name ;
87
- const name_ar = `ar ${ name_en } ` ;
93
+ let name_en = project . name ;
94
+ let name_ar = name_en ;
95
+
96
+ try {
97
+ const aiRes = await this . aiService . query (
98
+ [
99
+ { role : "system" , content : projectTitleSystemPrompt } ,
100
+ { role : "user" , content : name_en } ,
101
+ ] ,
102
+ AIResponseTranslateNameDto ,
103
+ ) ;
104
+
105
+ name_en = aiRes . name_en ;
106
+ name_ar = aiRes . name_ar ;
107
+ } catch ( error ) {
108
+ captureException ( error , { tags : { type : "CRON" } } ) ;
109
+ }
88
110
89
111
const projectEntity : ProjectRow = {
90
112
runId,
@@ -94,7 +116,7 @@ export class DigestCron {
94
116
} ;
95
117
const [ { id : projectId } ] = await this . projectsRepository . upsert ( projectEntity ) ;
96
118
for ( const tagId of project . tags || [ ] ) {
97
- await this . tagsRepository . upsert ( { id : tagId , runId } ) ;
119
+ await this . tagRepository . upsert ( { id : tagId , runId } ) ;
98
120
await this . projectsRepository . upsertRelationWithTag ( { projectId, tagId, runId } ) ;
99
121
}
100
122
await this . searchService . upsert ( "project" , projectEntity ) ;
@@ -133,9 +155,22 @@ export class DigestCron {
133
155
134
156
if ( githubUser . type !== "User" ) continue ;
135
157
136
- // todo: call AIService
137
- const name_en = githubUser . name || githubUser . login ;
138
- const name_ar = `ar ${ name_en } ` ;
158
+ let name_en = githubUser . name || githubUser . login ;
159
+ let name_ar = name_en ;
160
+ try {
161
+ const aiRes = await this . aiService . query (
162
+ [
163
+ { role : "system" , content : contributorNameSystemPrompt } ,
164
+ { role : "user" , content : name_en } ,
165
+ ] ,
166
+ AIResponseTranslateNameDto ,
167
+ ) ;
168
+
169
+ name_en = aiRes . name_en ;
170
+ name_ar = aiRes . name_ar ;
171
+ } catch ( error ) {
172
+ captureException ( error , { tags : { type : "CRON" } } ) ;
173
+ }
139
174
140
175
const contributorEntity : ContributorRow = {
141
176
name_en,
@@ -160,9 +195,22 @@ export class DigestCron {
160
195
161
196
const type = issue . pull_request ? "PULL_REQUEST" : "ISSUE" ;
162
197
163
- // todo: call AIService
164
- const title_en = issue . title ;
165
- const title_ar = `ar ${ title_en } ` ;
198
+ let title_en = issue . title ;
199
+ let title_ar = `ar ${ title_en } ` ;
200
+ try {
201
+ const aiRes = await this . aiService . query (
202
+ [
203
+ { role : "system" , content : issueTitleSystemPrompt } ,
204
+ { role : "user" , content : title_en } ,
205
+ ] ,
206
+ AIResponseTranslateTitleDto ,
207
+ ) ;
208
+
209
+ title_en = aiRes . title_en ;
210
+ title_ar = aiRes . title_ar ;
211
+ } catch ( error ) {
212
+ captureException ( error , { tags : { type : "CRON" } } ) ;
213
+ }
166
214
167
215
const contributionEntity : ContributionRow = {
168
216
title_en,
@@ -194,9 +242,22 @@ export class DigestCron {
194
242
username : repoContributor . login ,
195
243
} ) ;
196
244
197
- // todo: call AIService
198
- const name_en = contributor . name || contributor . login ;
199
- const name_ar = `ar ${ name_en } ` ;
245
+ let name_en = contributor . name || contributor . login ;
246
+ let name_ar = `ar ${ name_en } ` ;
247
+ try {
248
+ const aiRes = await this . aiService . query (
249
+ [
250
+ { role : "system" , content : contributorNameSystemPrompt } ,
251
+ { role : "user" , content : name_en } ,
252
+ ] ,
253
+ AIResponseTranslateNameDto ,
254
+ ) ;
255
+
256
+ name_en = aiRes . name_en ;
257
+ name_ar = aiRes . name_ar ;
258
+ } catch ( error ) {
259
+ captureException ( error , { tags : { type : "CRON" } } ) ;
260
+ }
200
261
201
262
const contributorEntity : ContributorRow = {
202
263
name_en,
@@ -244,7 +305,7 @@ export class DigestCron {
244
305
await this . projectsRepository . deleteAllRelationWithTagButWithRunId ( runId ) ;
245
306
await this . projectsRepository . deleteAllButWithRunId ( runId ) ;
246
307
247
- await this . tagsRepository . deleteAllButWithRunId ( runId ) ;
308
+ await this . tagRepository . deleteAllButWithRunId ( runId ) ;
248
309
249
310
await Promise . all ( [
250
311
this . searchService . deleteAllButWithRunId ( "project" , runId ) ,
0 commit comments