-
Notifications
You must be signed in to change notification settings - Fork 8
feat(PM-1793): Create AI workflow api implementation #34
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
Changes from 11 commits
8753af5
245f8f7
14c1641
6172269
b7dfcf6
0ac583d
7fa9f50
e601a0b
bd57ad1
71baf40
1e407f0
f1b5c62
0a7864c
8915092
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 |
---|---|---|
|
@@ -75,6 +75,7 @@ workflows: | |
only: | ||
- develop | ||
- feat/ai-workflows | ||
- pm-1793 | ||
|
||
- 'build-prod': | ||
context: org-global | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ const modelMappingKeys = [ | |
'review_item_comment', | ||
'llm_provider', | ||
'llm_model', | ||
'ai_workflow' | ||
'ai_workflow', | ||
]; | ||
const subModelMappingKeys = { | ||
review_item_comment: ['reviewItemComment', 'appeal', 'appealResponse'], | ||
|
@@ -813,27 +813,26 @@ async function processType(type: string, subtype?: string) { | |
} | ||
case 'scorecard': { | ||
console.log(`[${type}][${file}] Processing file`); | ||
const processedData = jsonData[key] | ||
.map((sc) => { | ||
const id = nanoid(14); | ||
scorecardIdMap.set(sc.scorecard_id, id); | ||
return { | ||
id: id, | ||
legacyId: sc.scorecard_id, | ||
status: scorecardStatusMap[sc.scorecard_status_id], | ||
type: scorecardTypeMap[sc.scorecard_type_id], | ||
challengeTrack: projectCategoryMap[sc.project_category_id].type, | ||
challengeType: projectCategoryMap[sc.project_category_id].name, | ||
name: sc.name, | ||
version: sc.version, | ||
minScore: parseFloat(sc.min_score), | ||
maxScore: parseFloat(sc.max_score), | ||
createdAt: new Date(sc.create_date), | ||
createdBy: sc.create_user, | ||
updatedAt: new Date(sc.modify_date), | ||
updatedBy: sc.modify_user, | ||
}; | ||
}); | ||
const processedData = jsonData[key].map((sc) => { | ||
const id = nanoid(14); | ||
scorecardIdMap.set(sc.scorecard_id, id); | ||
return { | ||
id: id, | ||
legacyId: sc.scorecard_id, | ||
status: scorecardStatusMap[sc.scorecard_status_id], | ||
type: scorecardTypeMap[sc.scorecard_type_id], | ||
challengeTrack: projectCategoryMap[sc.project_category_id].type, | ||
challengeType: projectCategoryMap[sc.project_category_id].name, | ||
name: sc.name, | ||
version: sc.version, | ||
minScore: parseFloat(sc.min_score), | ||
maxScore: parseFloat(sc.max_score), | ||
createdAt: new Date(sc.create_date), | ||
createdBy: sc.create_user, | ||
updatedAt: new Date(sc.modify_date), | ||
updatedBy: sc.modify_user, | ||
}; | ||
}); | ||
const totalBatches = Math.ceil(processedData.length / batchSize); | ||
for (let i = 0; i < processedData.length; i += batchSize) { | ||
const batchIndex = i / batchSize + 1; | ||
|
@@ -1350,13 +1349,9 @@ async function processType(type: string, subtype?: string) { | |
case 'llm_provider': { | ||
console.log(`[${type}][${subtype}][${file}] Processing file`); | ||
const idToLegacyIdMap = {}; | ||
const processedData = jsonData[key] | ||
.map((c) => { | ||
const processedData = jsonData[key].map((c) => { | ||
const id = nanoid(14); | ||
llmProviderIdMap.set( | ||
c.llm_provider_id, | ||
id, | ||
); | ||
llmProviderIdMap.set(c.llm_provider_id, id); | ||
idToLegacyIdMap[id] = c.llm_provider_id; | ||
return { | ||
id: id, | ||
|
@@ -1387,9 +1382,7 @@ async function processType(type: string, subtype?: string) { | |
data: item, | ||
}) | ||
.catch((err) => { | ||
llmProviderIdMap.delete( | ||
idToLegacyIdMap[item.id], | ||
); | ||
llmProviderIdMap.delete(idToLegacyIdMap[item.id]); | ||
console.error( | ||
`[${type}][${subtype}][${file}] Error code: ${err.code}, LegacyId: ${idToLegacyIdMap[item.id]}`, | ||
); | ||
|
@@ -1402,15 +1395,11 @@ async function processType(type: string, subtype?: string) { | |
case 'llm_model': { | ||
console.log(`[${type}][${subtype}][${file}] Processing file`); | ||
const idToLegacyIdMap = {}; | ||
const processedData = jsonData[key] | ||
.map((c) => { | ||
const processedData = jsonData[key].map((c) => { | ||
const id = nanoid(14); | ||
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. Consider using a more descriptive variable name than |
||
llmModelIdMap.set( | ||
c.llm_model_id, | ||
id, | ||
); | ||
llmModelIdMap.set(c.llm_model_id, id); | ||
idToLegacyIdMap[id] = c.llm_model_id; | ||
console.log(llmProviderIdMap.get(c.provider_id), 'c.provider_id') | ||
console.log(llmProviderIdMap.get(c.provider_id), 'c.provider_id'); | ||
return { | ||
id: id, | ||
providerId: llmProviderIdMap.get(c.provider_id), | ||
|
@@ -1423,7 +1412,7 @@ async function processType(type: string, subtype?: string) { | |
}; | ||
}); | ||
|
||
console.log(llmProviderIdMap, processedData, 'processedData') | ||
console.log(llmProviderIdMap, processedData, 'processedData'); | ||
|
||
const totalBatches = Math.ceil(processedData.length / batchSize); | ||
for (let i = 0; i < processedData.length; i += batchSize) { | ||
|
@@ -1446,9 +1435,7 @@ async function processType(type: string, subtype?: string) { | |
data: item, | ||
}) | ||
.catch((err) => { | ||
llmModelIdMap.delete( | ||
idToLegacyIdMap[item.id], | ||
); | ||
llmModelIdMap.delete(idToLegacyIdMap[item.id]); | ||
console.error( | ||
`[${type}][${subtype}][${file}] Error code: ${err.code}, LegacyId: ${idToLegacyIdMap[item.id]}`, | ||
); | ||
|
@@ -1461,13 +1448,9 @@ async function processType(type: string, subtype?: string) { | |
case 'ai_workflow': { | ||
console.log(`[${type}][${subtype}][${file}] Processing file`); | ||
const idToLegacyIdMap = {}; | ||
const processedData = jsonData[key] | ||
.map((c) => { | ||
const processedData = jsonData[key].map((c) => { | ||
const id = nanoid(14); | ||
aiWorkflowIdMap.set( | ||
c.ai_workflow_id, | ||
id, | ||
); | ||
aiWorkflowIdMap.set(c.ai_workflow_id, id); | ||
idToLegacyIdMap[id] = c.ai_workflow_id; | ||
return { | ||
id: id, | ||
|
@@ -1506,9 +1489,7 @@ async function processType(type: string, subtype?: string) { | |
data: item, | ||
}) | ||
.catch((err) => { | ||
aiWorkflowIdMap.delete( | ||
idToLegacyIdMap[item.id], | ||
); | ||
aiWorkflowIdMap.delete(idToLegacyIdMap[item.id]); | ||
console.error( | ||
`[${type}][${subtype}][${file}] Error code: ${err.code}, LegacyId: ${idToLegacyIdMap[item.id]}`, | ||
); | ||
|
@@ -1687,7 +1668,7 @@ migrate() | |
{ key: 'submissionIdMap', value: submissionIdMap }, | ||
{ key: 'llmProviderIdMap', value: llmProviderIdMap }, | ||
{ key: 'llmModelIdMap', value: llmModelIdMap }, | ||
{ key: 'aiWorkflowIdMap', value: aiWorkflowIdMap } | ||
{ key: 'aiWorkflowIdMap', value: aiWorkflowIdMap }, | ||
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. There is an extra comma at the end of the object in line 1671. This may lead to syntax errors in environments that do not support trailing commas. Consider removing the trailing comma. |
||
].forEach((f) => { | ||
if (!fs.existsSync('.tmp')) { | ||
fs.mkdirSync('.tmp'); | ||
|
hentrymartin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
-- AlterTable | ||
ALTER TABLE "aiWorkflow" ALTER COLUMN "updatedAt" DROP NOT NULL, | ||
ALTER COLUMN "updatedBy" DROP NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "aiWorkflowRunItem" ALTER COLUMN "createdAt" DROP NOT NULL, | ||
ALTER COLUMN "createdBy" DROP NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "aiWorkflowRunItemComment" ALTER COLUMN "updatedAt" DROP NOT NULL, | ||
ALTER COLUMN "updatedBy" DROP NOT NULL; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Controller, Post, Body } from '@nestjs/common'; | ||
import { | ||
ApiBearerAuth, | ||
ApiTags, | ||
ApiOperation, | ||
ApiResponse, | ||
} from '@nestjs/swagger'; | ||
import { AiWorkflowService } from './ai-workflow.service'; | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { CreateAiWorkflowDto } from '../../dto/aiWorkflow.dto'; | ||
import { Scopes } from 'src/shared/decorators/scopes.decorator'; | ||
import { UserRole } from 'src/shared/enums/userRole.enum'; | ||
import { Scope } from 'src/shared/enums/scopes.enum'; | ||
import { Roles } from 'src/shared/guards/tokenRoles.guard'; | ||
|
||
@ApiTags('ai_workflow') | ||
@ApiBearerAuth() | ||
@Controller('/workflows') | ||
export class AiWorkflowController { | ||
constructor(private readonly aiWorkflowService: AiWorkflowService) {} | ||
|
||
@Post() | ||
@Roles(UserRole.Admin) | ||
@Scopes(Scope.CreateWorkflow) | ||
@ApiOperation({ summary: 'Create a new AI workflow' }) | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'The AI workflow has been successfully created.', | ||
}) | ||
@ApiResponse({ status: 403, description: 'Forbidden.' }) | ||
async create(@Body() createAiWorkflowDto: CreateAiWorkflowDto) { | ||
return this.aiWorkflowService.createWithValidation(createAiWorkflowDto); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Injectable, BadRequestException } from '@nestjs/common'; | ||
import { PrismaService } from '../../shared/modules/global/prisma.service'; | ||
import { CreateAiWorkflowDto } from '../../dto/aiWorkflow.dto'; | ||
import { ScorecardStatus } from 'src/dto/scorecard.dto'; | ||
|
||
@Injectable() | ||
export class AiWorkflowService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async scorecardExists(scorecardId: string): Promise<boolean> { | ||
const count = await this.prisma.scorecard.count({ | ||
where: { id: scorecardId, status: ScorecardStatus.ACTIVE }, | ||
}); | ||
return count > 0; | ||
} | ||
|
||
async llmModelExists(llmId: string): Promise<boolean> { | ||
const count = await this.prisma.llmModel.count({ | ||
where: { id: llmId }, | ||
}); | ||
return count > 0; | ||
} | ||
|
||
async createWithValidation(createAiWorkflowDto: CreateAiWorkflowDto) { | ||
const { scorecardId, llmId, ...rest } = createAiWorkflowDto; | ||
hentrymartin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const scorecardExists = await this.scorecardExists(scorecardId); | ||
if (!scorecardExists) { | ||
throw new BadRequestException( | ||
`Scorecard with id ${scorecardId} does not exist or is not active.`, | ||
); | ||
} | ||
|
||
const llmExists = await this.llmModelExists(llmId); | ||
if (!llmExists) { | ||
throw new BadRequestException( | ||
`LLM model with id ${llmId} does not exist.`, | ||
); | ||
} | ||
|
||
return this.prisma.aiWorkflow.create({ | ||
data: { | ||
...rest, | ||
|
||
scorecardId, | ||
llmId, | ||
}, | ||
}); | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
The log statement uses the variable
file
, butfile
is not defined within the current scope. Ensure thatfile
is defined or passed to the function if needed.