@shaxpir/duiduidui-models 1.36.3 → 1.37.2

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.
@@ -11,6 +11,11 @@ export interface BayesianScore {
11
11
  }
12
12
  export declare class BayesianScoreModel {
13
13
  static apply(prevScore: BayesianScore, result: ReviewResult, weight: number): BayesianScore;
14
+ /**
15
+ * Reverse a previously applied Bayesian update.
16
+ * Subtracts the same deltas that apply() added, then recalculates theta and uncertainty.
17
+ */
18
+ static reverse(currentScore: BayesianScore, result: ReviewResult, weight: number): BayesianScore;
14
19
  /**
15
20
  * Calculate uncertainty for a given Bayesian score.
16
21
  * Higher values indicate less confidence in the theta estimate.
@@ -24,6 +24,31 @@ class BayesianScoreModel {
24
24
  nextScore.uncertainty = BayesianScoreModel.calculateUncertainty(nextScore.alpha, nextScore.beta);
25
25
  return nextScore;
26
26
  }
27
+ /**
28
+ * Reverse a previously applied Bayesian update.
29
+ * Subtracts the same deltas that apply() added, then recalculates theta and uncertainty.
30
+ */
31
+ static reverse(currentScore, result, weight) {
32
+ if (isNaN(weight) || !isFinite(weight) || weight < 0 || weight > 1) {
33
+ throw new Error(`illegal weight value: ${weight}`);
34
+ }
35
+ const prevScore = shaxpir_common_1.Struct.clone(currentScore);
36
+ if (result == 'EASY') {
37
+ prevScore.alpha -= (weight * 1.0);
38
+ }
39
+ else if (result == 'GOOD') {
40
+ prevScore.alpha -= (weight * 0.7);
41
+ }
42
+ else if (result == 'HARD') {
43
+ prevScore.beta -= (weight * 0.3);
44
+ }
45
+ else if (result == 'FAIL') {
46
+ prevScore.beta -= (weight * 1.0);
47
+ }
48
+ prevScore.theta = prevScore.alpha / (prevScore.alpha + prevScore.beta);
49
+ prevScore.uncertainty = BayesianScoreModel.calculateUncertainty(prevScore.alpha, prevScore.beta);
50
+ return prevScore;
51
+ }
27
52
  /**
28
53
  * Calculate uncertainty for a given Bayesian score.
29
54
  * Higher values indicate less confidence in the theta estimate.
@@ -14,7 +14,14 @@ exports.Condition = {
14
14
  gradeD: () => ({ type: 'grade', grade: 'D' }),
15
15
  gradeF: () => ({ type: 'grade', grade: 'F' }),
16
16
  // Theta conditions
17
- theta: (min, max) => ({ type: 'theta', min, max }),
17
+ theta: (min, max) => {
18
+ const c = { type: 'theta' };
19
+ if (min !== undefined)
20
+ c.min = min;
21
+ if (max !== undefined)
22
+ c.max = max;
23
+ return c;
24
+ },
18
25
  learningZone: () => ({ type: 'theta', min: 0.4, max: 0.6 }),
19
26
  struggling: () => ({ type: 'theta', max: 0.3 }),
20
27
  nearMastery: () => ({ type: 'theta', min: 0.7, max: 0.8 }),
@@ -33,7 +40,14 @@ exports.Condition = {
33
40
  starred: () => ({ type: 'starred', value: true }),
34
41
  notStarred: () => ({ type: 'starred', value: false }),
35
42
  // Difficulty conditions
36
- difficulty: (min, max) => ({ type: 'difficulty', min, max }),
43
+ difficulty: (min, max) => {
44
+ const c = { type: 'difficulty' };
45
+ if (min !== undefined)
46
+ c.min = min;
47
+ if (max !== undefined)
48
+ c.max = max;
49
+ return c;
50
+ },
37
51
  beginner: () => ({ type: 'difficulty', max: 100 }), // Very easy content
38
52
  elementary: () => ({ type: 'difficulty', min: 100, max: 500 }), // Easy
39
53
  intermediate: () => ({ type: 'difficulty', min: 500, max: 1500 }), // Medium
@@ -63,7 +63,7 @@ export declare class Device extends Content {
63
63
  setReviewAutoPlaySpeech(value: boolean): void;
64
64
  setReviewCardLimit(value: number | null): void;
65
65
  setReviewChallenge(value: ChallengeLevel): void;
66
- setReviewCollection(value: string | undefined): void;
66
+ setReviewCollection(value: string | null): void;
67
67
  get reviewCustomConditions(): Conditions;
68
68
  setReviewCustomConditions(value: Conditions): void;
69
69
  getBuiltInDbState(): BuiltInDbState | undefined;
@@ -114,7 +114,12 @@ class Device extends Content_1.Content {
114
114
  setReviewCollection(value) {
115
115
  this.checkDisposed("Device.setReviewCollection");
116
116
  const batch = new Operation_1.BatchOperation(this);
117
- batch.setPathValue(['payload', 'review_config', 'selection', 'collection'], value);
117
+ if (value) {
118
+ batch.setPathValue(['payload', 'review_config', 'selection', 'collection'], value);
119
+ }
120
+ else {
121
+ batch.removeValueAtPath(['payload', 'review_config', 'selection', 'collection']);
122
+ }
118
123
  batch.commit();
119
124
  }
120
125
  // Review Custom Conditions (for Custom mode)
@@ -88,6 +88,11 @@ export declare class Session extends Content {
88
88
  get deviceId(): ContentId;
89
89
  get reviews(): Review[];
90
90
  get reviewCount(): number;
91
+ /**
92
+ * Remove the last review from this session.
93
+ * Decrements review_count.
94
+ */
95
+ removeLastReview(): void;
91
96
  addReview(review: Review): void;
92
97
  get config(): ReviewConfig | undefined;
93
98
  setConfig(config: ReviewConfig): void;
@@ -95,6 +95,20 @@ class Session extends Content_1.Content {
95
95
  this.checkDisposed("Session.getReviewCount");
96
96
  return this._reviewsView.length;
97
97
  }
98
+ /**
99
+ * Remove the last review from this session.
100
+ * Decrements review_count.
101
+ */
102
+ removeLastReview() {
103
+ this.checkDisposed("Session.removeLastReview");
104
+ if (this._reviewsView.length === 0)
105
+ return;
106
+ const lastIndex = this._reviewsView.length - 1;
107
+ this._reviewsView.removeAt(lastIndex);
108
+ const batch = new Operation_1.BatchOperation(this);
109
+ batch.setPathValue(['payload', 'review_count'], this._reviewsView.length);
110
+ batch.commit();
111
+ }
98
112
  addReview(review) {
99
113
  this.checkDisposed("Session.addReview");
100
114
  if (review.hasOwnProperty('context') && review.hasOwnProperty('weight')) {
@@ -113,6 +113,16 @@ export declare class SkillLevelModel {
113
113
  * @returns Updated skill level with new mu and sigma
114
114
  */
115
115
  static update(mu: number, sigma: number, cardDifficulty: number, outcome: ReviewResult, params?: SkillUpdateParams): SkillLevel;
116
+ /**
117
+ * Reverse a previously applied skill level update.
118
+ * Algebraically inverts the forward update: oldMu = (newMu - coeff * difficulty) / (1 - coeff).
119
+ * If the forward update was uninformative (no change), this is also a no-op.
120
+ *
121
+ * Note: If the forward update hit the Math.max(0, ...) clamp on mu, the original value
122
+ * is not perfectly recoverable. In practice this doesn't happen because mu represents
123
+ * a character count and starts at 0 moving upward.
124
+ */
125
+ static reverse(mu: number, sigma: number, cardDifficulty: number, outcome: ReviewResult, params?: SkillUpdateParams): SkillLevel;
116
126
  /**
117
127
  * Map a review outcome to a success score for IRT.
118
128
  *
@@ -73,6 +73,35 @@ class SkillLevelModel {
73
73
  sigma: Math.max(minSigma, Math.min(maxSigma, newSigma))
74
74
  };
75
75
  }
76
+ /**
77
+ * Reverse a previously applied skill level update.
78
+ * Algebraically inverts the forward update: oldMu = (newMu - coeff * difficulty) / (1 - coeff).
79
+ * If the forward update was uninformative (no change), this is also a no-op.
80
+ *
81
+ * Note: If the forward update hit the Math.max(0, ...) clamp on mu, the original value
82
+ * is not perfectly recoverable. In practice this doesn't happen because mu represents
83
+ * a character count and starts at 0 moving upward.
84
+ */
85
+ static reverse(mu, sigma, cardDifficulty, outcome, params = {}) {
86
+ const { updateCoefficient = exports.DEFAULT_SKILL_PARAMS.updateCoefficient, sigmaDecay = exports.DEFAULT_SKILL_PARAMS.sigmaDecay, minSigma = exports.DEFAULT_SKILL_PARAMS.minSigma, maxSigma = exports.DEFAULT_SKILL_PARAMS.maxSigma } = params;
87
+ // Reverse sigma first: oldSigma = newSigma / sigmaDecay
88
+ const oldSigma = sigma / sigmaDecay;
89
+ // Reverse mu: oldMu = (newMu - coeff * difficulty) / (1 - coeff)
90
+ const oldMu = (mu - updateCoefficient * cardDifficulty) / (1 - updateCoefficient);
91
+ // Check if the forward update would have been uninformative from the original state.
92
+ // If so, mu and sigma didn't change — return the current values unchanged.
93
+ const isSuccess = outcome === 'EASY' || outcome === 'GOOD';
94
+ const isFailure = outcome === 'FAIL' || outcome === 'HARD';
95
+ const wasInformative = (isSuccess && cardDifficulty > oldMu) ||
96
+ (isFailure && cardDifficulty < oldMu);
97
+ if (!wasInformative) {
98
+ return { mu, sigma };
99
+ }
100
+ return {
101
+ mu: Math.max(0, oldMu),
102
+ sigma: Math.max(minSigma, Math.min(maxSigma, oldSigma))
103
+ };
104
+ }
76
105
  /**
77
106
  * Map a review outcome to a success score for IRT.
78
107
  *
@@ -91,5 +91,15 @@ export declare class Term extends Content {
91
91
  get impliedReviews(): ImpliedReview[];
92
92
  get reviewCount(): number;
93
93
  get impliedReviewCount(): number;
94
+ /**
95
+ * Remove the last direct review from this term.
96
+ * Decrements review_count and restores last_review_utc from the previous review.
97
+ */
98
+ removeLastReview(): void;
99
+ /**
100
+ * Remove the last implied review from this term.
101
+ * Decrements implied_review_count.
102
+ */
103
+ removeLastImpliedReview(): void;
94
104
  addReview(review: ReviewLike): void;
95
105
  }
@@ -256,6 +256,42 @@ class Term extends Content_1.Content {
256
256
  this.checkDisposed("Term.impliedReviewCount");
257
257
  return this.payload.implied_review_count;
258
258
  }
259
+ /**
260
+ * Remove the last direct review from this term.
261
+ * Decrements review_count and restores last_review_utc from the previous review.
262
+ */
263
+ removeLastReview() {
264
+ this.checkDisposed("Term.removeLastReview");
265
+ if (this._reviewsView.length === 0)
266
+ return;
267
+ const lastIndex = this._reviewsView.length - 1;
268
+ this._reviewsView.removeAt(lastIndex);
269
+ const batch = new Operation_1.BatchOperation(this);
270
+ batch.setPathValue(['payload', 'review_count'], this._reviewsView.length);
271
+ // Restore last_review_utc from the new last review, or null if no reviews remain
272
+ if (this._reviewsView.length > 0) {
273
+ const prevLast = this._reviewsView.get(this._reviewsView.length - 1);
274
+ batch.setPathValue(['payload', 'last_review_utc'], prevLast.at_utc_time);
275
+ }
276
+ else {
277
+ batch.setPathValue(['payload', 'last_review_utc'], null);
278
+ }
279
+ batch.commit();
280
+ }
281
+ /**
282
+ * Remove the last implied review from this term.
283
+ * Decrements implied_review_count.
284
+ */
285
+ removeLastImpliedReview() {
286
+ this.checkDisposed("Term.removeLastImpliedReview");
287
+ if (this._impliedReviewsView.length === 0)
288
+ return;
289
+ const lastIndex = this._impliedReviewsView.length - 1;
290
+ this._impliedReviewsView.removeAt(lastIndex);
291
+ const batch = new Operation_1.BatchOperation(this);
292
+ batch.setPathValue(['payload', 'implied_review_count'], this._impliedReviewsView.length);
293
+ batch.commit();
294
+ }
259
295
  // Add either an implied or explicit review
260
296
  addReview(review) {
261
297
  this.checkDisposed("Term.addReview");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaxpir/duiduidui-models",
3
- "version": "1.36.3",
3
+ "version": "1.37.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shaxpir/duiduidui-models"