Skip to content

Commit b2647ed

Browse files
authored
Add rationales to the getPromptJSON output of Radio widget and Label Image widget (#2557)
## Summary: Add rationales to the getPromptJSON output of Radio widget and Label Image widget. This is a small addition to the information that getPromptJSON already returns, and only affects these specific widgets. The intent is to have rationales accessible when we use getPromptJSON to get the state of widgets. Issue: TUT-2087 ## Test plan: - Updated tests - Run all tests Author: marekweb Reviewers: jeremywiebe, benchristel, anakaren-rojas, marekweb, MikeKlemarewski, tony-dinh Required Reviewers: Approved By: jeremywiebe, benchristel, anakaren-rojas Checks: ✅ 8 checks were successful Pull Request URL: #2557
1 parent a4d6bd8 commit b2647ed

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

.changeset/metal-spies-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/perseus": minor
3+
---
4+
5+
Add rationales to the getPromptJSON output of Radio widget and Label Image widget

packages/perseus/src/widget-ai-utils/group/group-ai-utils.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ describe("Group AI utils", () => {
127127
options: [
128128
{value: "$45$"},
129129
{value: "$42$"},
130-
{value: "$30$"},
130+
{
131+
value: "$30$",
132+
rationale:
133+
"Here's a clue, this isn't the correct answer!",
134+
},
131135
{value: "$18$"},
132136
{value: "$15$"},
133137
],

packages/perseus/src/widget-ai-utils/label-image/label-image-ai-utils.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,19 @@ describe("LabelImage AI utils", () => {
142142
markers: [
143143
{
144144
label: "The fourth unlabeled bar line.",
145+
answers: ["SUVs"],
145146
},
146147
{
147148
label: "The third unlabeled bar line.",
149+
answers: ["Trucks"],
148150
},
149151
{
150152
label: "The second unlabeled bar line.",
153+
answers: ["Cars"],
151154
},
152155
{
153156
label: "The first unlabeled bar line.",
157+
answers: ["Vans"],
154158
},
155159
],
156160
choices: ["Trucks", "Vans", "Cars", "SUVs"],
@@ -211,15 +215,19 @@ describe("LabelImage AI utils", () => {
211215
markers: [
212216
{
213217
label: "The fourth unlabeled bar line.",
218+
answers: ["SUVs"],
214219
},
215220
{
216221
label: "The third unlabeled bar line.",
222+
answers: ["Trucks"],
217223
},
218224
{
219225
label: "The second unlabeled bar line.",
226+
answers: ["Cars"],
220227
},
221228
{
222229
label: "The first unlabeled bar line.",
230+
answers: ["Vans"],
223231
},
224232
],
225233
},

packages/perseus/src/widget-ai-utils/label-image/label-image-ai-utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type BaseMarker = {
88

99
type UserInputMarker = {
1010
label: string;
11+
answers?: string[];
1112
selected?: ReadonlyArray<string>;
1213
};
1314

@@ -29,9 +30,13 @@ export const getPromptJSON = (
2930
userInput: PerseusLabelImageUserInput,
3031
): LabelImagePromptJSON => {
3132
const propMarkers = renderProps.markers.map((marker) => {
32-
return {
33+
const userInputMarker: UserInputMarker = {
3334
label: marker.label,
3435
};
36+
if (marker.answers?.length) {
37+
userInputMarker.answers = marker.answers;
38+
}
39+
return userInputMarker;
3540
});
3641

3742
const inputMarkers = userInput.markers.map((marker) => {

packages/perseus/src/widget-ai-utils/radio/radio-ai-utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type React from "react";
77

88
type BasicOption = {
99
value: string;
10+
rationale?: string;
1011
};
1112

1213
export type RadioPromptJSON = {
@@ -26,9 +27,13 @@ export const getPromptJSON = (
2627
const choices = renderProps.choices || [];
2728

2829
const options = choices.map((choice) => {
29-
return {
30+
const option: BasicOption = {
3031
value: choice.content,
3132
};
33+
if (choice.clue) {
34+
option.rationale = choice.clue;
35+
}
36+
return option;
3237
});
3338

3439
return {

0 commit comments

Comments
 (0)