Skip to content

Commit 731dad6

Browse files
committed
question generation type fix
1 parent c896050 commit 731dad6

File tree

6 files changed

+66
-28
lines changed

6 files changed

+66
-28
lines changed

apps/api/src/api/attempt/attempt.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import { Roles } from "src/auth/role/roles.global.guard";
3232
import { Logger } from "winston";
3333
import {
3434
GRADE_SUBMISSION_EXCEPTION,
35+
IN_COOLDOWN_PERIOD,
3536
MAX_ATTEMPTS_SUBMISSION_EXCEPTION_MESSAGE,
3637
SUBMISSION_DEADLINE_EXCEPTION_MESSAGE,
37-
IN_COOLDOWN_PERIOD,
3838
} from "../assignment/attempt/api-exceptions/exceptions";
3939
import { BaseAssignmentAttemptResponseDto } from "../assignment/attempt/dto/assignment-attempt/base.assignment.attempt.response.dto";
4040
import { LearnerUpdateAssignmentAttemptRequestDto } from "../assignment/attempt/dto/assignment-attempt/create.update.assignment.attempt.request.dto";
@@ -139,6 +139,7 @@ export class AttemptControllerV2 {
139139
@ApiResponse({ status: 403 })
140140
getAssignmentAttempt(
141141
@Param("attemptId") assignmentAttemptId: number,
142+
@Req() request: UserSessionRequest,
142143
@Query("lang") lang?: string,
143144
): Promise<GetAssignmentAttemptResponseDto> {
144145
return this.attemptService.getAssignmentAttempt(

apps/api/src/api/llm/features/question-generation/services/question-generation.service.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,17 @@ export class QuestionGenerationService implements IQuestionGenerationService {
421421
.int()
422422
.positive()
423423
.optional()
424-
.describe("Maximum word limit for text responses"),
424+
.describe(
425+
"Maximum word limit for text responses (only include for TEXT question types, omit otherwise)",
426+
),
425427
maxCharacters: z
426428
.number()
427429
.int()
428430
.positive()
429431
.optional()
430-
.describe("Maximum character limit for text responses"),
432+
.describe(
433+
"Maximum character limit for text responses (only include for TEXT question types, omit otherwise)",
434+
),
431435
randomizedChoices: z
432436
.boolean()
433437
.optional()
@@ -653,10 +657,14 @@ FORMAT INSTRUCTIONS:
653657
responseType: question.responseType || this.getDefaultResponseType(),
654658
difficultyLevel: question.difficultyLevel,
655659
maxWords:
656-
question.maxWords ||
660+
(question.maxWords && question.maxWords > 0
661+
? question.maxWords
662+
: null) ||
657663
this.getDefaultMaxWords(question.type, question.difficultyLevel),
658664
maxCharacters:
659-
question.maxCharacters ||
665+
(question.maxCharacters && question.maxCharacters > 0
666+
? question.maxCharacters
667+
: null) ||
660668
this.getDefaultMaxCharacters(question.type, question.difficultyLevel),
661669
randomizedChoices:
662670
question.randomizedChoices ??
@@ -685,7 +693,11 @@ FORMAT INSTRUCTIONS:
685693
}
686694

687695
const originalChoice = question.choices[0];
688-
const choiceText = originalChoice.choice?.toLowerCase().trim();
696+
const choiceValue =
697+
typeof originalChoice.choice === "string"
698+
? originalChoice.choice
699+
: String(originalChoice.choice ?? "");
700+
const choiceText = choiceValue.toLowerCase().trim();
689701
const isStatementTrue = choiceText === "true";
690702

691703
return [
@@ -707,22 +719,28 @@ FORMAT INSTRUCTIONS:
707719
return this.getDefaultChoices(question.type, question.difficultyLevel);
708720
}
709721

710-
return question.choices.map((choice: Choice, index: number) => ({
711-
choice: choice.choice?.replaceAll("```", "").trim() || "",
712-
id: choice.id || index + 1,
713-
isCorrect: choice.isCorrect === true,
714-
points:
715-
choice.points === undefined
716-
? choice.isCorrect
717-
? 1
718-
: 0
719-
: Math.round(choice.points),
720-
feedback:
721-
choice.feedback?.replaceAll("```", "").trim() ||
722-
(choice.isCorrect
723-
? "This is the correct answer."
724-
: "This is not the correct answer."),
725-
}));
722+
return question.choices.map((choice: Choice, index: number) => {
723+
const choiceValue =
724+
typeof choice.choice === "string"
725+
? choice.choice
726+
: String(choice.choice ?? "");
727+
return {
728+
choice: choiceValue.replaceAll("```", "").trim() || "",
729+
id: choice.id || index + 1,
730+
isCorrect: choice.isCorrect === true,
731+
points:
732+
choice.points === undefined
733+
? choice.isCorrect
734+
? 1
735+
: 0
736+
: Math.round(choice.points),
737+
feedback:
738+
choice.feedback?.replaceAll("```", "").trim() ||
739+
(choice.isCorrect
740+
? "This is the correct answer."
741+
: "This is not the correct answer."),
742+
};
743+
});
726744
}
727745

728746
private async refineQuestions(
@@ -952,7 +970,11 @@ FORMAT INSTRUCTIONS:
952970
}
953971

954972
const choice = question.choices[0];
955-
const choiceValue = choice.choice?.toString().toLowerCase().trim();
973+
const choiceString =
974+
typeof choice.choice === "string"
975+
? choice.choice
976+
: String(choice.choice ?? "");
977+
const choiceValue = choiceString.toLowerCase().trim();
956978
if (choiceValue !== "true" && choiceValue !== "false") {
957979
return true;
958980
}

apps/api/src/api/llm/features/question-generation/services/question-validator.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,11 @@ export class QuestionValidatorService implements IQuestionValidatorService {
471471

472472
const choice = question.choices[0];
473473

474-
const choiceText = choice.choice?.toString().toLowerCase().trim();
474+
const choiceString =
475+
typeof choice.choice === "string"
476+
? choice.choice
477+
: String(choice.choice ?? "");
478+
const choiceText = choiceString.toLowerCase().trim();
475479

476480
if (choiceText !== "true" && choiceText !== "false") {
477481
issues.push(

apps/web/app/Helpers/checkQuestionsReady.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ export const useQuestionsAreReadyToBePublished = (
7272
}
7373
for (let i = 0; i < choices.length; i++) {
7474
const { choice: choiceText, points } = choices[i];
75-
if (!choiceText?.trim()) {
75+
const choiceString =
76+
typeof choiceText === "string" ? choiceText : String(choiceText ?? "");
77+
if (!choiceString.trim()) {
7678
return {
7779
message: `Question ${index + 1} has an empty choice text.`,
7880
step: 0,

apps/web/app/author/(components)/AuthorQuestionsPage/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ type ClientSnapshot = {
8686
};
8787
timezone: string | null;
8888
};
89-
9089
function parseBrowser(ua: string) {
9190
const pairs = [
9291
[/Edg\/([\d.]+)/i, "Edge"],

apps/web/stores/author.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,14 +1306,24 @@ export const useAuthorStore = createWithEqualityFn<
13061306
return (
13071307
Array.isArray(variant.choices) &&
13081308
variant.choices.every((choice) => {
1309-
return choice.choice.toLowerCase() === "true";
1309+
const choiceValue = choice.choice;
1310+
if (typeof choiceValue === "boolean")
1311+
return choiceValue === true;
1312+
if (typeof choiceValue === "string")
1313+
return choiceValue.toLowerCase() === "true";
1314+
return false;
13101315
})
13111316
);
13121317
} else {
13131318
return (
13141319
Array.isArray(question.choices) &&
13151320
question.choices.every((choice) => {
1316-
return choice.choice.toLowerCase() === "true";
1321+
const choiceValue = choice.choice;
1322+
if (typeof choiceValue === "boolean")
1323+
return choiceValue === true;
1324+
if (typeof choiceValue === "string")
1325+
return choiceValue.toLowerCase() === "true";
1326+
return false;
13171327
})
13181328
);
13191329
}

0 commit comments

Comments
 (0)