@khanacademy/perseus 41.1.1 → 41.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/dist/all-widgets.d.ts +1 -1
  2. package/dist/article-renderer.d.ts +2 -2
  3. package/dist/basic-widgets.d.ts +1 -1
  4. package/dist/es/index.js +1663 -397
  5. package/dist/es/index.js.map +1 -1
  6. package/dist/extra-widgets.d.ts +1 -1
  7. package/dist/index.d.ts +4 -4
  8. package/dist/index.js +1583 -316
  9. package/dist/index.js.map +1 -1
  10. package/dist/init.d.ts +1 -8
  11. package/dist/perseus-api.d.ts +3 -3
  12. package/dist/perseus-types.d.ts +7 -2
  13. package/dist/prompt-types.d.ts +33 -0
  14. package/dist/renderer.d.ts +8 -2
  15. package/dist/server-item-renderer.d.ts +6 -4
  16. package/dist/types.d.ts +4 -2
  17. package/dist/util/parse-perseus-json/error-tracking-parse-context.d.ts +9 -0
  18. package/dist/util/parse-perseus-json/general-purpose-parsers/any.d.ts +2 -0
  19. package/dist/util/parse-perseus-json/general-purpose-parsers/array.d.ts +2 -0
  20. package/dist/util/parse-perseus-json/general-purpose-parsers/array.typetest.d.ts +1 -0
  21. package/dist/util/parse-perseus-json/general-purpose-parsers/boolean.d.ts +2 -0
  22. package/dist/util/parse-perseus-json/general-purpose-parsers/compose-parsers.d.ts +2 -0
  23. package/dist/util/parse-perseus-json/general-purpose-parsers/constant.d.ts +4 -0
  24. package/dist/util/parse-perseus-json/general-purpose-parsers/defaulted.d.ts +2 -0
  25. package/dist/util/parse-perseus-json/general-purpose-parsers/enumeration.d.ts +2 -0
  26. package/dist/util/parse-perseus-json/general-purpose-parsers/enumeration.typetest.d.ts +1 -0
  27. package/dist/util/parse-perseus-json/general-purpose-parsers/index.d.ts +15 -0
  28. package/dist/util/parse-perseus-json/general-purpose-parsers/is-object.d.ts +1 -0
  29. package/dist/util/parse-perseus-json/general-purpose-parsers/nullable.d.ts +2 -0
  30. package/dist/util/parse-perseus-json/general-purpose-parsers/number.d.ts +2 -0
  31. package/dist/util/parse-perseus-json/general-purpose-parsers/object.d.ts +6 -0
  32. package/dist/util/parse-perseus-json/general-purpose-parsers/optional.d.ts +2 -0
  33. package/dist/util/parse-perseus-json/general-purpose-parsers/pair.d.ts +2 -0
  34. package/dist/util/parse-perseus-json/general-purpose-parsers/record.d.ts +2 -0
  35. package/dist/util/parse-perseus-json/general-purpose-parsers/string.d.ts +2 -0
  36. package/dist/util/parse-perseus-json/general-purpose-parsers/test-helpers.d.ts +8 -0
  37. package/dist/util/parse-perseus-json/general-purpose-parsers/trio.d.ts +2 -0
  38. package/dist/util/parse-perseus-json/general-purpose-parsers/union.d.ts +7 -0
  39. package/dist/util/parse-perseus-json/general-purpose-parsers/union.typetest.d.ts +1 -0
  40. package/dist/util/{parse-perseus-json.d.ts → parse-perseus-json/index.d.ts} +7 -1
  41. package/dist/util/parse-perseus-json/object-path.d.ts +2 -0
  42. package/dist/util/parse-perseus-json/parse-failure-detail.d.ts +2 -0
  43. package/dist/util/parse-perseus-json/parse.d.ts +3 -0
  44. package/dist/util/parse-perseus-json/parser-types.d.ts +30 -0
  45. package/dist/util/parse-perseus-json/perseus-parsers/categorizer-widget.d.ts +3 -0
  46. package/dist/util/parse-perseus-json/perseus-parsers/cs-program-widget.d.ts +3 -0
  47. package/dist/util/parse-perseus-json/perseus-parsers/definition-widget.d.ts +3 -0
  48. package/dist/util/parse-perseus-json/perseus-parsers/dropdown-widget.d.ts +3 -0
  49. package/dist/util/parse-perseus-json/perseus-parsers/explanation-widget.d.ts +3 -0
  50. package/dist/util/parse-perseus-json/perseus-parsers/expression-widget.d.ts +3 -0
  51. package/dist/util/parse-perseus-json/perseus-parsers/hint.d.ts +3 -0
  52. package/dist/util/parse-perseus-json/perseus-parsers/interactive-graph-widget.d.ts +3 -0
  53. package/dist/util/parse-perseus-json/perseus-parsers/perseus-image-background.d.ts +3 -0
  54. package/dist/util/parse-perseus-json/perseus-parsers/perseus-item.d.ts +3 -0
  55. package/dist/util/parse-perseus-json/perseus-parsers/perseus-renderer.d.ts +3 -0
  56. package/dist/util/parse-perseus-json/perseus-parsers/widget.d.ts +3 -0
  57. package/dist/util/parse-perseus-json/perseus-parsers/widgets-map.d.ts +3 -0
  58. package/dist/util/parse-perseus-json/result.d.ts +16 -0
  59. package/dist/validation.types.d.ts +0 -3
  60. package/dist/widget-ai-utils/categorizer/prompt-utils.d.ts +14 -0
  61. package/dist/widget-ai-utils/cs-program/prompt-utils.d.ts +2 -0
  62. package/dist/widget-ai-utils/definition/prompt-utils.d.ts +8 -0
  63. package/dist/widget-ai-utils/dropdown/prompt-utils.d.ts +13 -0
  64. package/dist/widget-ai-utils/explanation/prompt-utils.d.ts +8 -0
  65. package/dist/widget-ai-utils/expression/prompt-utils.d.ts +11 -0
  66. package/dist/widget-ai-utils/graded-group/prompt-utils.d.ts +7 -0
  67. package/dist/widget-ai-utils/graded-group-set/prompt-utils.d.ts +11 -0
  68. package/dist/widget-ai-utils/grapher/prompt-utils.d.ts +18 -0
  69. package/dist/widget-ai-utils/group/prompt-utils.d.ts +5 -0
  70. package/dist/widget-ai-utils/iframe/prompt-utils.d.ts +2 -0
  71. package/dist/widget-ai-utils/image/prompt-utils.d.ts +12 -0
  72. package/dist/widget-ai-utils/input-number/prompt-utils.d.ts +14 -0
  73. package/dist/widget-ai-utils/interaction/prompt-utils.d.ts +2 -0
  74. package/dist/widget-ai-utils/interactive-graph/prompt-utils.d.ts +94 -0
  75. package/dist/widget-ai-utils/label-image/prompt-utils.d.ts +24 -0
  76. package/dist/widget-ai-utils/matcher/prompt-utils.d.ts +17 -0
  77. package/dist/widget-ai-utils/matrix/prompt-utils.d.ts +14 -0
  78. package/dist/widget-ai-utils/measurer/prompt-utils.d.ts +2 -0
  79. package/dist/widget-ai-utils/number-line/prompt-utils.d.ts +16 -0
  80. package/dist/widget-ai-utils/numeric-input/prompt-utils.d.ts +11 -0
  81. package/dist/widget-ai-utils/orderer/prompt-utils.d.ts +13 -0
  82. package/dist/widget-ai-utils/passage/prompt-utils.d.ts +11 -0
  83. package/dist/widget-ai-utils/passage-ref/prompt-utils.d.ts +11 -0
  84. package/dist/widget-ai-utils/phet-simulation/prompt-utils.d.ts +2 -0
  85. package/dist/widget-ai-utils/plotter/prompt-utils.d.ts +2 -0
  86. package/dist/widget-ai-utils/python-program/prompt-utils.d.ts +2 -0
  87. package/dist/widget-ai-utils/radio/prompt-utils.d.ts +15 -0
  88. package/dist/widget-ai-utils/sorter/prompt-utils.d.ts +9 -0
  89. package/dist/widget-ai-utils/unsupported-widget.d.ts +11 -0
  90. package/dist/widget-ai-utils/video/prompt-utils.d.ts +2 -0
  91. package/dist/widgets/__shared__/{noop-validator.d.ts → score-noop.d.ts} +4 -4
  92. package/dist/widgets/categorizer/categorizer.d.ts +10 -19
  93. package/dist/widgets/categorizer/{categorizer-validator.d.ts → score-categorizer.d.ts} +2 -2
  94. package/dist/widgets/cs-program/cs-program.d.ts +8 -19
  95. package/dist/widgets/cs-program/{cs-program-validator.d.ts → score-cs-program.d.ts} +2 -1
  96. package/dist/widgets/definition/definition.d.ts +8 -19
  97. package/dist/widgets/deprecated-standin/deprecated-standin.d.ts +4 -19
  98. package/dist/widgets/dropdown/dropdown.d.ts +10 -19
  99. package/dist/widgets/dropdown/score-dropdown.d.ts +4 -0
  100. package/dist/widgets/explanation/explanation.d.ts +9 -19
  101. package/dist/widgets/expression/expression.d.ts +26 -290
  102. package/dist/widgets/expression/{expression-validator.d.ts → score-expression.d.ts} +2 -2
  103. package/dist/widgets/graded-group/graded-group.d.ts +13 -21
  104. package/dist/widgets/graded-group-set/graded-group-set.d.ts +9 -20
  105. package/dist/widgets/grapher/grapher.d.ts +9 -19
  106. package/dist/widgets/grapher/score-grapher.d.ts +4 -0
  107. package/dist/widgets/group/group.d.ts +7 -19
  108. package/dist/widgets/iframe/iframe.d.ts +7 -19
  109. package/dist/widgets/iframe/{iframe-validator.d.ts → score-iframe.d.ts} +1 -1
  110. package/dist/widgets/image/image.d.ts +9 -19
  111. package/dist/widgets/input-number/input-number.d.ts +11 -19
  112. package/dist/widgets/input-number/{input-number-validator.d.ts → score-input-number.d.ts} +2 -2
  113. package/dist/widgets/interaction/interaction.d.ts +7 -19
  114. package/dist/widgets/interactive-graph.d.ts +8 -19
  115. package/dist/widgets/interactive-graphs/score-interactive-graph.d.ts +4 -0
  116. package/dist/widgets/interactive-graphs/utils.d.ts +6 -0
  117. package/dist/widgets/label-image/label-image.d.ts +10 -86
  118. package/dist/widgets/label-image/{label-image-validator.d.ts → score-label-image.d.ts} +2 -2
  119. package/dist/widgets/matcher/matcher.d.ts +7 -19
  120. package/dist/widgets/matcher/score-matcher.d.ts +4 -0
  121. package/dist/widgets/matrix/matrix.d.ts +10 -19
  122. package/dist/widgets/matrix/{matrix-validator.d.ts → score-matrix.d.ts} +2 -2
  123. package/dist/widgets/measurer/measurer.d.ts +13 -19
  124. package/dist/widgets/molecule/molecule.d.ts +4 -19
  125. package/dist/widgets/number-line/number-line.d.ts +8 -19
  126. package/dist/widgets/number-line/score-number-line.d.ts +4 -0
  127. package/dist/widgets/numeric-input/numeric-input.d.ts +22 -19
  128. package/dist/widgets/numeric-input/{numeric-input-validator.d.ts → score-numeric-input.d.ts} +2 -2
  129. package/dist/widgets/orderer/orderer.d.ts +8 -19
  130. package/dist/widgets/orderer/{orderer-validator.d.ts → score-orderer.d.ts} +1 -1
  131. package/dist/widgets/passage/passage.d.ts +8 -19
  132. package/dist/widgets/passage-ref/passage-ref.d.ts +12 -19
  133. package/dist/widgets/passage-ref-target/passage-ref-target.d.ts +11 -19
  134. package/dist/widgets/phet-simulation/phet-simulation.d.ts +5 -19
  135. package/dist/widgets/plotter/plotter.d.ts +8 -19
  136. package/dist/widgets/plotter/score-plotter.d.ts +4 -0
  137. package/dist/widgets/python-program/python-program.d.ts +4 -19
  138. package/dist/widgets/radio/radio-component.d.ts +3 -1
  139. package/dist/widgets/radio/radio.d.ts +18 -19
  140. package/dist/widgets/radio/{radio-validator.d.ts → score-radio.d.ts} +2 -2
  141. package/dist/widgets/sorter/score-sorter.d.ts +4 -0
  142. package/dist/widgets/sorter/sorter.d.ts +7 -19
  143. package/dist/widgets/table/{table-validator.d.ts → score-table.d.ts} +2 -2
  144. package/dist/widgets/table/table.d.ts +8 -19
  145. package/dist/widgets/video/video.d.ts +7 -19
  146. package/dist/widgets.d.ts +2 -2
  147. package/package.json +4 -4
  148. package/dist/widgets/dropdown/dropdown-validator.d.ts +0 -4
  149. package/dist/widgets/grapher/grapher-validator.d.ts +0 -4
  150. package/dist/widgets/interactive-graphs/interactive-graph-validator.d.ts +0 -4
  151. package/dist/widgets/matcher/matcher-validator.d.ts +0 -4
  152. package/dist/widgets/number-line/number-line-validator.d.ts +0 -4
  153. package/dist/widgets/plotter/plotter-validator.d.ts +0 -4
  154. package/dist/widgets/sorter/sorter-validator.d.ts +0 -4
package/dist/init.d.ts CHANGED
@@ -1,14 +1,7 @@
1
- export type PerseusOptions = {
2
- skipMathJax: boolean;
3
- };
4
1
  /**
5
2
  * This should be called by all clients, specifying whether extra widgets are
6
3
  * needed via `loadExtraWidgets`. It is idempotent, so it's not a problem to
7
4
  * call it multiple times.
8
- *
9
- * skipMathJax:
10
- * If false/undefined, MathJax will be configured, and the
11
- * promise will wait for MathJax to load (if it hasn't already).
12
5
  */
13
- declare const init: (options: PerseusOptions) => Promise<undefined>;
6
+ declare const init: () => void;
14
7
  export default init;
@@ -96,13 +96,13 @@ export declare const ApiOptions: {
96
96
  circle?: boolean | undefined;
97
97
  polygon?: boolean | undefined;
98
98
  point?: boolean | undefined;
99
+ quadratic?: boolean | undefined;
100
+ sinusoid?: boolean | undefined;
99
101
  angle?: boolean | undefined;
100
- segment?: boolean | undefined;
101
102
  "linear-system"?: boolean | undefined;
102
103
  ray?: boolean | undefined;
104
+ segment?: boolean | undefined;
103
105
  "unlimited-polygon"?: boolean | undefined;
104
- quadratic?: boolean | undefined;
105
- sinusoid?: boolean | undefined;
106
106
  "unlimited-point"?: boolean | undefined;
107
107
  } & {
108
108
  "interactive-graph-locked-features-labels"?: boolean | undefined;
@@ -85,6 +85,11 @@ export type PerseusItem = {
85
85
  itemDataVersion: Version;
86
86
  answer: any;
87
87
  };
88
+ /**
89
+ * A "PerseusArticle" is an item that is meant to be rendered as an article.
90
+ * This item is never scored and is rendered by the `ArticleRenderer`.
91
+ */
92
+ export type PerseusArticle = PerseusRenderer | ReadonlyArray<PerseusRenderer>;
88
93
  /**
89
94
  * A "MultiItem" is an advanced Perseus item. It is rendered by the
90
95
  * `MultiRenderer` and you can control the layout of individual parts of the
@@ -119,7 +124,7 @@ export type PerseusImageDetail = {
119
124
  };
120
125
  export declare const ItemExtras: readonly ["calculator", "chi2Table", "financialCalculatorMonthlyPayment", "financialCalculatorTotalAmount", "financialCalculatorTimeToPayOff", "periodicTable", "periodicTableWithKey", "tTable", "zTable"];
121
126
  export type PerseusAnswerArea = Record<(typeof ItemExtras)[number], boolean>;
122
- type WidgetOptions<Type extends string, Options> = {
127
+ export type WidgetOptions<Type extends string, Options> = {
123
128
  type: Type;
124
129
  static?: boolean;
125
130
  graded?: boolean;
@@ -366,7 +371,7 @@ export type PerseusInteractiveGraphWidgetOptions = {
366
371
  fullGraphAriaLabel?: string;
367
372
  fullGraphAriaDescription?: string;
368
373
  };
369
- declare const lockedFigureColorNames: readonly ["blue", "green", "grayH", "purple", "pink", "orange", "red"];
374
+ export declare const lockedFigureColorNames: readonly ["blue", "green", "grayH", "purple", "pink", "orange", "red"];
370
375
  export type LockedFigureColor = (typeof lockedFigureColorNames)[number];
371
376
  export declare const lockedFigureColors: Record<LockedFigureColor, string>;
372
377
  export type LockedFigure = LockedPointType | LockedLineType | LockedVectorType | LockedEllipseType | LockedPolygonType | LockedFunctionType | LockedLabelType;
@@ -0,0 +1,33 @@
1
+ import type { CategorizerPromptJSON } from "./widget-ai-utils/categorizer/prompt-utils";
2
+ import type { DefinitionPromptJSON } from "./widget-ai-utils/definition/prompt-utils";
3
+ import type { DropdownPromptJSON } from "./widget-ai-utils/dropdown/prompt-utils";
4
+ import type { ExplanationPromptJSON } from "./widget-ai-utils/explanation/prompt-utils";
5
+ import type { ExpressionPromptJSON } from "./widget-ai-utils/expression/prompt-utils";
6
+ import type { GradedGroupPromptJSON } from "./widget-ai-utils/graded-group/prompt-utils";
7
+ import type { GradedGroupSetPromptJSON } from "./widget-ai-utils/graded-group-set/prompt-utils";
8
+ import type { GrapherPromptJSON } from "./widget-ai-utils/grapher/prompt-utils";
9
+ import type { GroupPromptJSON } from "./widget-ai-utils/group/prompt-utils";
10
+ import type { ImagePromptJSON } from "./widget-ai-utils/image/prompt-utils";
11
+ import type { InputNumberPromptJSON } from "./widget-ai-utils/input-number/prompt-utils";
12
+ import type { LabelImagePromptJSON } from "./widget-ai-utils/label-image/prompt-utils";
13
+ import type { MatcherPromptJSON } from "./widget-ai-utils/matcher/prompt-utils";
14
+ import type { MatrixPromptJSON } from "./widget-ai-utils/matrix/prompt-utils";
15
+ import type { NumberLinePromptJSON } from "./widget-ai-utils/number-line/prompt-utils";
16
+ import type { NumericInputPromptJSON } from "./widget-ai-utils/numeric-input/prompt-utils";
17
+ import type { OrdererPromptJSON } from "./widget-ai-utils/orderer/prompt-utils";
18
+ import type { PassagePromptJSON } from "./widget-ai-utils/passage/prompt-utils";
19
+ import type { PassageRefPromptJSON } from "./widget-ai-utils/passage-ref/prompt-utils";
20
+ import type { RadioPromptJSON } from "./widget-ai-utils/radio/prompt-utils";
21
+ import type { SorterPromptJSON } from "./widget-ai-utils/sorter/prompt-utils";
22
+ import type { UnsupportedWidgetPromptJSON } from "./widget-ai-utils/unsupported-widget";
23
+ export type UnsupportedWidget = "cs-program" | "iframe" | "interaction" | "interactive-graph-unsupported" | "measurer" | "phet-simulation" | "plotter" | "python-program" | "video";
24
+ export type WidgetPromptJSON = CategorizerPromptJSON | DefinitionPromptJSON | DropdownPromptJSON | ExplanationPromptJSON | ExpressionPromptJSON | GradedGroupPromptJSON | GradedGroupSetPromptJSON | GrapherPromptJSON | GroupPromptJSON | ImagePromptJSON | InputNumberPromptJSON | LabelImagePromptJSON | MatcherPromptJSON | MatrixPromptJSON | NumberLinePromptJSON | NumericInputPromptJSON | OrdererPromptJSON | PassagePromptJSON | PassageRefPromptJSON | RadioPromptJSON | SorterPromptJSON | UnsupportedWidgetPromptJSON;
25
+ export type RendererPromptJSON = {
26
+ content: string;
27
+ widgets: {
28
+ [widgetId: string]: WidgetPromptJSON;
29
+ };
30
+ };
31
+ export interface GetPromptJSONInterface {
32
+ getPromptJSON(): RendererPromptJSON;
33
+ }
@@ -5,6 +5,7 @@ import TranslationLinter from "./translation-linter";
5
5
  import WidgetContainer from "./widget-container";
6
6
  import type { DependenciesContext } from "./dependencies";
7
7
  import type { PerseusRenderer, PerseusWidget, PerseusWidgetOptions, PerseusWidgetsMap, ShowSolutions } from "./perseus-types";
8
+ import type { GetPromptJSONInterface, RendererPromptJSON } from "./prompt-types";
8
9
  import type { PerseusStrings } from "./strings";
9
10
  import type { APIOptions, APIOptionsWithDefaults, FilterCriterion, FocusPath, PerseusScore, SerializedState, Widget, WidgetProps } from "./types";
10
11
  import type { UserInputArray, UserInputMap } from "./validation.types";
@@ -70,7 +71,7 @@ type State = {
70
71
  lastUsedWidgetId: string | null | undefined;
71
72
  };
72
73
  type DefaultProps = Required<Pick<Props, "alwaysUpdate" | "content" | "findExternalWidgets" | "highlightedWidgets" | "images" | "linterContext" | "onInteractWithWidget" | "onRender" | "onSerializedStateUpdated" | "questionCompleted" | "showSolutions" | "reviewMode" | "serializedState" | "widgets">>;
73
- declare class Renderer extends React.Component<Props, State> {
74
+ declare class Renderer extends React.Component<Props, State> implements GetPromptJSONInterface {
74
75
  static contextType: React.Context<{
75
76
  strings: PerseusStrings;
76
77
  locale: string;
@@ -107,7 +108,7 @@ declare class Renderer extends React.Component<Props, State> {
107
108
  _getDefaultWidgetInfo: (widgetId: string) => any;
108
109
  _getWidgetInfo: (widgetId: string) => PerseusWidget;
109
110
  renderWidget: (impliedType: string, id: string, state: WidgetState) => null | React.ReactNode;
110
- getWidgetProps: (id: string) => WidgetProps<any, PerseusWidgetOptions>;
111
+ getWidgetProps(widgetId: string): WidgetProps<any, PerseusWidgetOptions>;
111
112
  /**
112
113
  * Serializes the questions state so it can be recovered.
113
114
  *
@@ -211,6 +212,11 @@ declare class Renderer extends React.Component<Props, State> {
211
212
  * the content.
212
213
  */
213
214
  getWidgetIds: () => ReadonlyArray<string>;
215
+ /**
216
+ * Returns a JSON representation of the content and widgets
217
+ * that can be passed to an LLM for prompt context.
218
+ */
219
+ getPromptJSON(): RendererPromptJSON;
214
220
  /**
215
221
  * Returns an object mapping from widget ID to perseus-style score.
216
222
  * The keys of this object are the values of the array returned
@@ -11,6 +11,7 @@ import * as React from "react";
11
11
  import { PerseusI18nContext } from "./components/i18n-context";
12
12
  import Renderer from "./renderer";
13
13
  import type { PerseusItem, ShowSolutions } from "./perseus-types";
14
+ import type { GetPromptJSONInterface, RendererPromptJSON } from "./prompt-types";
14
15
  import type { FocusPath, PerseusDependenciesV2, SharedRendererProps } from "./types";
15
16
  import type { KeypadAPI } from "@khanacademy/math-input";
16
17
  import type { KeypadContextRendererInterface, RendererInterface, KEScore } from "@khanacademy/perseus-core";
@@ -39,7 +40,7 @@ type SerializedState = {
39
40
  question: any;
40
41
  hints: any;
41
42
  };
42
- export declare class ServerItemRenderer extends React.Component<Props, State> implements RendererInterface, KeypadContextRendererInterface {
43
+ export declare class ServerItemRenderer extends React.Component<Props, State> implements RendererInterface, KeypadContextRendererInterface, GetPromptJSONInterface {
43
44
  static contextType: React.Context<{
44
45
  strings: import("./strings").PerseusStrings;
45
46
  locale: string;
@@ -74,6 +75,7 @@ export declare class ServerItemRenderer extends React.Component<Props, State> im
74
75
  focus(): boolean | null | undefined;
75
76
  blur(): void;
76
77
  getNumHints(): number;
78
+ getPromptJSON(): RendererPromptJSON;
77
79
  /**
78
80
  * Grades the item.
79
81
  */
@@ -134,13 +136,13 @@ declare const ref: React.ForwardRefExoticComponent<Omit<Pick<SharedRendererProps
134
136
  circle?: boolean | undefined;
135
137
  polygon?: boolean | undefined;
136
138
  point?: boolean | undefined;
139
+ quadratic?: boolean | undefined;
140
+ sinusoid?: boolean | undefined;
137
141
  angle?: boolean | undefined;
138
- segment?: boolean | undefined;
139
142
  "linear-system"?: boolean | undefined;
140
143
  ray?: boolean | undefined;
144
+ segment?: boolean | undefined;
141
145
  "unlimited-polygon"?: boolean | undefined;
142
- quadratic?: boolean | undefined;
143
- sinusoid?: boolean | undefined;
144
146
  "unlimited-point"?: boolean | undefined;
145
147
  } & {
146
148
  "interactive-graph-locked-features-labels"?: boolean | undefined;
package/dist/types.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { ILogger } from "./logging/log";
2
2
  import type { Item } from "./multi-items/item-types";
3
3
  import type { Hint, PerseusAnswerArea, PerseusGraphType, PerseusWidget, PerseusWidgetsMap } from "./perseus-types";
4
+ import type { WidgetPromptJSON } from "./prompt-types";
4
5
  import type { PerseusStrings } from "./strings";
5
6
  import type { SizeClass } from "./util/sizing-utils";
6
7
  import type { Rubric, UserInput, UserInputArray, UserInputMap } from "./validation.types";
@@ -55,6 +56,7 @@ export interface Widget {
55
56
  getUserInput?: () => UserInputArray | UserInput | undefined;
56
57
  showRationalesForCurrentlySelectedChoices?: (options?: any) => void;
57
58
  examples?: () => ReadonlyArray<string>;
59
+ getPromptJSON?: () => WidgetPromptJSON;
58
60
  }
59
61
  export type ImageDict = {
60
62
  [url: string]: Dimensions;
@@ -352,7 +354,7 @@ type TrackingSequenceExtraArguments = {
352
354
  export type Alignment = "default" | "block" | "inline-block" | "inline" | "float-left" | "float-right" | "full-width";
353
355
  type WidgetOptions = any;
354
356
  export type WidgetTransform = (arg1: WidgetOptions, strings: PerseusStrings, problemNumber?: number) => any;
355
- export type WidgetValidatorFunction = (userInput: UserInput, rubric: Rubric, string?: PerseusStrings, locale?: string) => PerseusScore;
357
+ export type WidgetScorerFunction = (userInput: UserInput, rubric: Rubric, string?: PerseusStrings, locale?: string) => PerseusScore;
356
358
  export type WidgetExports<T extends React.ComponentType<any> & Widget = React.ComponentType<any>> = Readonly<{
357
359
  name: string;
358
360
  displayName: string;
@@ -380,7 +382,7 @@ export type WidgetExports<T extends React.ComponentType<any> & Widget = React.Co
380
382
  /** transforms the widget options to the props used to render the widget for
381
383
  static renders */
382
384
  staticTransform?: WidgetTransform;
383
- validator?: WidgetValidatorFunction;
385
+ scorer?: WidgetScorerFunction;
384
386
  getOneCorrectAnswerFromRubric?: (rubric: Rubric) => string | null | undefined;
385
387
  /**
386
388
  A map of major version numbers (as a string, eg "1") to a function that
@@ -0,0 +1,9 @@
1
+ import type { ParseContext, Mismatch, PathSegment } from "./parser-types";
2
+ import type { Failure, Success } from "./result";
3
+ export declare class ErrorTrackingParseContext implements ParseContext {
4
+ private readonly path;
5
+ constructor(path: PathSegment[]);
6
+ failure(expected: string | string[], badValue: unknown): Failure<Mismatch[]>;
7
+ forSubtree(key: PathSegment): ParseContext;
8
+ success<T>(value: T): Success<T>;
9
+ }
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare const any: Parser<any>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function array<T>(elementParser: Parser<T>): Parser<T[]>;
@@ -0,0 +1,2 @@
1
+ import type { ParseContext, ParseResult } from "../parser-types";
2
+ export declare function boolean(rawValue: unknown, ctx: ParseContext): ParseResult<boolean>;
@@ -0,0 +1,2 @@
1
+ import type { ParsedValue, PartialParser, Parser } from "../parser-types";
2
+ export declare function composeParsers<A extends Parser<any>, B extends PartialParser<ParsedValue<A>, any>>(parserA: A, parserB: B): Parser<ParsedValue<B>>;
@@ -0,0 +1,4 @@
1
+ import type { Parser } from "../parser-types";
2
+ type Primitive = string | number | boolean | null | undefined;
3
+ export declare function constant<T extends Primitive>(acceptedValue: T): Parser<T>;
4
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function defaulted<T, Default extends T | null | undefined>(parser: Parser<T>, fallback: (missingValue: null | undefined) => Default): Parser<T | Default>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function enumeration<T extends readonly string[]>(...acceptedValues: T): Parser<T[number]>;
@@ -0,0 +1,15 @@
1
+ export * from "./any";
2
+ export * from "./array";
3
+ export * from "./boolean";
4
+ export * from "./constant";
5
+ export * from "./enumeration";
6
+ export * from "./is-object";
7
+ export * from "./nullable";
8
+ export * from "./number";
9
+ export * from "./object";
10
+ export * from "./optional";
11
+ export * from "./pair";
12
+ export * from "./record";
13
+ export * from "./string";
14
+ export * from "./trio";
15
+ export * from "./union";
@@ -0,0 +1 @@
1
+ export declare function isObject(x: unknown): x is Record<keyof any, unknown>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function nullable<T>(parseValue: Parser<T>): Parser<T | null>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare const number: Parser<number>;
@@ -0,0 +1,6 @@
1
+ import type { ParsedValue, Parser } from "../parser-types";
2
+ type ObjectSchema = Record<keyof any, Parser<any>>;
3
+ export declare function object<S extends ObjectSchema>(schema: S): Parser<{
4
+ [K in keyof S]: ParsedValue<S[K]>;
5
+ }>;
6
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function optional<T>(parseValue: Parser<T>): Parser<T | undefined>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function pair<A, B>(parseA: Parser<A>, parseB: Parser<B>): Parser<[A, B]>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function record<K extends string, V>(parseKey: Parser<K>, parseValue: Parser<V>): Parser<Record<K, V>>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare const string: Parser<string>;
@@ -0,0 +1,8 @@
1
+ import { ErrorTrackingParseContext } from "../error-tracking-parse-context";
2
+ import type { Mismatch, ParsedValue, Parser } from "../parser-types";
3
+ import type { Failure } from "../result";
4
+ export declare function ctx(): ErrorTrackingParseContext;
5
+ export declare const anyFailure: Failure<any>;
6
+ export declare const anySuccess: import("../result").Success<any>;
7
+ export declare function parseFailureWith(expected: Partial<Mismatch>): Failure<Mismatch[]>;
8
+ export declare function summonParsedValue<P extends Parser<any>>(): ParsedValue<P>;
@@ -0,0 +1,2 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function trio<A, B, C>(parseA: Parser<A>, parseB: Parser<B>, parseC: Parser<C>): Parser<[A, B, C]>;
@@ -0,0 +1,7 @@
1
+ import type { Parser } from "../parser-types";
2
+ export declare function union<T>(parseBranch: Parser<T>): UnionBuilder<T>;
3
+ export declare class UnionBuilder<T> {
4
+ parser: Parser<T>;
5
+ constructor(parser: Parser<T>);
6
+ or<New>(newBranch: Parser<New>): UnionBuilder<T | New>;
7
+ }
@@ -1,4 +1,5 @@
1
- import type { PerseusItem } from "../perseus-types";
1
+ import type { Result } from "./result";
2
+ import type { PerseusItem } from "../../perseus-types";
2
3
  /**
3
4
  * Helper to parse PerseusItem JSON
4
5
  * Why not just use JSON.parse? We want:
@@ -8,3 +9,8 @@ import type { PerseusItem } from "../perseus-types";
8
9
  * @returns {PerseusItem} the parsed PerseusItem object
9
10
  */
10
11
  export declare function parsePerseusItem(json: string): PerseusItem;
12
+ /**
13
+ * Typesafe version of parsePerseusItem, which runtime-typechecks the JSON.
14
+ * TODO(benchristel): Replace parsePerseusItem with this function.
15
+ */
16
+ export declare function parseAndTypecheckPerseusItem(json: string): Result<PerseusItem, string>;
@@ -0,0 +1,2 @@
1
+ import type { PathSegment } from "./parser-types";
2
+ export declare function formatPath(path: PathSegment[]): string;
@@ -0,0 +1,2 @@
1
+ import type { Mismatch } from "./parser-types";
2
+ export declare function message(failure: Mismatch): string;
@@ -0,0 +1,3 @@
1
+ import type { Parser } from "./parser-types";
2
+ import type { Result } from "./result";
3
+ export declare function parse<T>(value: unknown, parser: Parser<T>): Result<T, string>;
@@ -0,0 +1,30 @@
1
+ import type { Success, Failure, Result } from "./result";
2
+ export type Parser<T> = (rawVal: unknown, ctx: ParseContext) => ParseResult<T>;
3
+ export type PartialParser<Input, Output> = (rawVal: Input, ctx: ParseContext) => ParseResult<Output>;
4
+ export type ParseResult<T> = Result<T, Mismatch[]>;
5
+ export type Mismatch = {
6
+ expected: string[];
7
+ badValue: unknown;
8
+ path: PathSegment[];
9
+ };
10
+ export type PathSegment = keyof any;
11
+ export interface ParseContext {
12
+ /**
13
+ * Returns a Success result based on the current state of the parse.
14
+ * @param value The value to return in the Success.
15
+ */
16
+ success<T>(value: T): Success<T>;
17
+ /**
18
+ * Returns a Failure result based on the current state of the parse.
19
+ *
20
+ * @param expected one or more descriptions of the types that were expected.
21
+ * An array of several expected types has "any" semantics - any of the
22
+ * listed types is valid.
23
+ * @param badValue the value that caused the parse to fail.
24
+ */
25
+ failure(expected: string | string[], badValue: unknown): Failure<Mismatch[]>;
26
+ forSubtree(key: PathSegment): ParseContext;
27
+ }
28
+ export type ParsedValue<P extends Parser<any>> = Extract<ReturnType<P>, {
29
+ type: "success";
30
+ }>["value"];
@@ -0,0 +1,3 @@
1
+ import type { CategorizerWidget } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseCategorizerWidget: Parser<CategorizerWidget>;
@@ -0,0 +1,3 @@
1
+ import type { CSProgramWidget } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseCSProgramWidget: Parser<CSProgramWidget>;
@@ -0,0 +1,3 @@
1
+ import type { DefinitionWidget } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseDefinitionWidget: Parser<DefinitionWidget>;
@@ -0,0 +1,3 @@
1
+ import type { DropdownWidget } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseDropdownWidget: Parser<DropdownWidget>;
@@ -0,0 +1,3 @@
1
+ import type { ExplanationWidget } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseExplanationWidget: Parser<ExplanationWidget>;
@@ -0,0 +1,3 @@
1
+ import type { ExpressionWidget } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseExpressionWidget: Parser<ExpressionWidget>;
@@ -0,0 +1,3 @@
1
+ import type { Hint } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseHint: Parser<Hint>;
@@ -0,0 +1,3 @@
1
+ import type { InteractiveGraphWidget } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseInteractiveGraphWidget: Parser<InteractiveGraphWidget>;
@@ -0,0 +1,3 @@
1
+ import type { Parser } from "../parser-types";
2
+ import type { PerseusImageBackground } from "@khanacademy/perseus";
3
+ export declare const parsePerseusImageBackground: Parser<PerseusImageBackground>;
@@ -0,0 +1,3 @@
1
+ import type { PerseusItem } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parsePerseusItem: Parser<PerseusItem>;
@@ -0,0 +1,3 @@
1
+ import type { PerseusRenderer } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parsePerseusRenderer: Parser<PerseusRenderer>;
@@ -0,0 +1,3 @@
1
+ import type { WidgetOptions } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare function parseWidget<Type extends string, Options>(parseType: Parser<Type>, parseOptions: Parser<Options>): Parser<WidgetOptions<Type, Options>>;
@@ -0,0 +1,3 @@
1
+ import type { PerseusWidgetsMap } from "../../../perseus-types";
2
+ import type { Parser } from "../parser-types";
3
+ export declare const parseWidgetsMap: Parser<PerseusWidgetsMap>;
@@ -0,0 +1,16 @@
1
+ export type Result<S, F> = Success<S> | Failure<F>;
2
+ export type Success<T> = {
3
+ type: "success";
4
+ value: T;
5
+ };
6
+ export type Failure<T> = {
7
+ type: "failure";
8
+ detail: T;
9
+ };
10
+ export declare function success<T>(value: T): Success<T>;
11
+ export declare function failure<T>(detail: T): Failure<T>;
12
+ export declare function isFailure<S, F>(result: Result<S, F>): result is Failure<F>;
13
+ export declare function isSuccess<S, F>(result: Result<S, F>): result is Success<S>;
14
+ export declare function assertFailure<S, F>(result: Result<S, F>): asserts result is Failure<F>;
15
+ export declare function assertSuccess<S, F>(result: Result<S, F>): asserts result is Success<S>;
16
+ export declare function all<S, F>(results: Array<Result<S, F>>, combineFailureDetails?: (a: F, b: F) => F): Result<S[], F>;
@@ -94,9 +94,6 @@ export type PerseusRadioRubric = {
94
94
  };
95
95
  export type PerseusRadioUserInput = {
96
96
  choicesSelected: ReadonlyArray<boolean>;
97
- numCorrect?: number;
98
- noneOfTheAboveIndex?: number | null | undefined;
99
- noneOfTheAboveSelected?: boolean;
100
97
  };
101
98
  export type PerseusSorterRubric = {
102
99
  correct: ReadonlyArray<string>;
@@ -0,0 +1,14 @@
1
+ import type { PerseusCategorizerUserInput } from "../../validation.types";
2
+ import type categorizer from "../../widgets/categorizer/categorizer";
3
+ import type React from "react";
4
+ export type CategorizerPromptJSON = {
5
+ type: "categorizer";
6
+ options: {
7
+ items: ReadonlyArray<string>;
8
+ categories: ReadonlyArray<string>;
9
+ };
10
+ userInput: {
11
+ itemToCategoryMapping: ReadonlyArray<number>;
12
+ };
13
+ };
14
+ export declare const getPromptJSON: (renderProps: React.ComponentProps<typeof categorizer.widget>, userInput: PerseusCategorizerUserInput) => CategorizerPromptJSON;
@@ -0,0 +1,2 @@
1
+ import { type UnsupportedWidgetPromptJSON } from "../unsupported-widget";
2
+ export declare const getPromptJSON: () => UnsupportedWidgetPromptJSON;
@@ -0,0 +1,8 @@
1
+ import type definition from "../../widgets/definition/definition";
2
+ import type React from "react";
3
+ export type DefinitionPromptJSON = {
4
+ type: "definition";
5
+ definition: string;
6
+ togglePrompt: string;
7
+ };
8
+ export declare const getPromptJSON: (renderProps: React.ComponentProps<typeof definition.widget>) => DefinitionPromptJSON;
@@ -0,0 +1,13 @@
1
+ import type { PerseusDropdownUserInput } from "../../validation.types";
2
+ import type dropdown from "../../widgets/dropdown/dropdown";
3
+ import type React from "react";
4
+ export type DropdownPromptJSON = {
5
+ type: "dropdown";
6
+ options: {
7
+ items: ReadonlyArray<string>;
8
+ };
9
+ userInput: {
10
+ selectedIndex: number;
11
+ };
12
+ };
13
+ export declare const getPromptJSON: (renderProps: React.ComponentProps<typeof dropdown.widget>, userInput: PerseusDropdownUserInput) => DropdownPromptJSON;
@@ -0,0 +1,8 @@
1
+ import type explanation from "../../widgets/explanation/explanation";
2
+ import type React from "react";
3
+ export type ExplanationPromptJSON = {
4
+ type: "explanation";
5
+ showPrompt: string;
6
+ explanation: string;
7
+ };
8
+ export declare const getPromptJSON: (renderProps: React.ComponentProps<typeof explanation.widget>) => ExplanationPromptJSON;
@@ -0,0 +1,11 @@
1
+ import type { PerseusExpressionUserInput } from "../../validation.types";
2
+ import type expression from "../../widgets/expression/expression";
3
+ import type React from "react";
4
+ export type ExpressionPromptJSON = {
5
+ type: "expression";
6
+ label?: string;
7
+ userInput: {
8
+ value: string;
9
+ };
10
+ };
11
+ export declare const getPromptJSON: (renderProps: React.ComponentProps<typeof expression.widget>, userInput: PerseusExpressionUserInput) => ExpressionPromptJSON;
@@ -0,0 +1,7 @@
1
+ import type { RendererPromptJSON } from "../../prompt-types";
2
+ export type GradedGroupPromptJSON = RendererPromptJSON & {
3
+ type: "graded-group";
4
+ title: string;
5
+ hint: RendererPromptJSON | string;
6
+ };
7
+ export declare const getPromptJSON: (title: string, rendererJSON: RendererPromptJSON | undefined, hintRendererJSON: RendererPromptJSON) => GradedGroupPromptJSON;
@@ -0,0 +1,11 @@
1
+ import type gradedGroupSet from "../../widgets/graded-group-set";
2
+ import type { GradedGroupPromptJSON } from "../graded-group/prompt-utils";
3
+ import type React from "react";
4
+ export type GradedGroupSetPromptJSON = {
5
+ type: "graded-group-set";
6
+ options: {
7
+ groupCount: number;
8
+ currentGroup: GradedGroupPromptJSON;
9
+ };
10
+ };
11
+ export declare const getPromptJSON: (renderProps: React.ComponentProps<typeof gradedGroupSet.widget>, activeGroupJSON: GradedGroupPromptJSON) => GradedGroupSetPromptJSON;
@@ -0,0 +1,18 @@
1
+ import type { GrapherAnswerTypes } from "../../perseus-types";
2
+ import type { PerseusGrapherUserInput } from "../../validation.types";
3
+ import type grapher from "../../widgets/grapher/grapher";
4
+ import type React from "react";
5
+ export type GrapherPromptJSON = {
6
+ type: "grapher";
7
+ options: {
8
+ availableTypes: ReadonlyArray<string>;
9
+ range: [x: [min: number, max: number], y: [min: number, max: number]];
10
+ labels: ReadonlyArray<string>;
11
+ tickStep: [number, number];
12
+ gridStep?: [number, number];
13
+ snapStep?: [number, number];
14
+ backgroundImageUrl?: string | null;
15
+ };
16
+ userInput: GrapherAnswerTypes;
17
+ };
18
+ export declare const getPromptJSON: (renderProps: React.ComponentProps<typeof grapher.widget>, userInput: PerseusGrapherUserInput) => GrapherPromptJSON;