diff --git a/package.json b/package.json index 8261ef2a..2b042c19 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@commander-js/extra-typings": "12.0.1", "@octokit/rest": "20.0.2", "commander": "12.0.0", + "decimal.js": "10.4.3", "dotenv": "16.4.5", "jsdom": "24.0.0", "markdown-it": "14.1.0", diff --git a/src/get-activity.test.ts b/src/get-activity.test.ts index db3c29db..753b6327 100644 --- a/src/get-activity.test.ts +++ b/src/get-activity.test.ts @@ -20,7 +20,7 @@ describe("GetActivity class", () => { const processor = new Processor(); await processor.run(activity); processor.dump(); - }); + }, 30000); it("should create an instance of GetActivity", () => { expect(activity).toBeInstanceOf(GetActivity); diff --git a/src/parser/content-evaluator-module.ts b/src/parser/content-evaluator-module.ts index e7e48ab8..3fc1f96e 100644 --- a/src/parser/content-evaluator-module.ts +++ b/src/parser/content-evaluator-module.ts @@ -1,3 +1,4 @@ +import Decimal from "decimal.js"; import OpenAI from "openai"; import configuration from "../configuration/config-reader"; import { GetActivity } from "../get-activity"; @@ -25,12 +26,13 @@ export class ContentEvaluatorModule implements Module { const commentBody = comment.content; if (specificationBody && commentBody) { const { relevance } = await this._evaluateComment(specificationBody, commentBody); - const reward = comment.score?.reward ? comment.score.reward * relevance : relevance; + const currentReward = new Decimal(comment.score?.reward ? comment.score.reward : 0); + const reward = currentReward.mul(relevance).toNumber(); comments[i] = { ...comment, score: { ...comment.score, - relevance, + relevance: relevance.toNumber(), reward, }, }; @@ -58,11 +60,11 @@ export class ContentEvaluatorModule implements Module { presence_penalty: 0, }); - return { relevance: Number(response.choices[0].message.content) }; + return { relevance: new Decimal(Number(response.choices[0].message.content)) }; } catch (error) { console.error("Failed to evaluate comment", error); return { - relevance: 0, + relevance: relevance, }; } } diff --git a/src/parser/formatting-evaluator-module.ts b/src/parser/formatting-evaluator-module.ts index 9e63c8ed..5be02ef5 100644 --- a/src/parser/formatting-evaluator-module.ts +++ b/src/parser/formatting-evaluator-module.ts @@ -1,3 +1,4 @@ +import Decimal from "decimal.js"; import { JSDOM } from "jsdom"; import MarkdownIt from "markdown-it"; import configuration from "../configuration/config-reader"; @@ -44,17 +45,22 @@ export class FormattingEvaluatorModule implements Module { const formattingTotal = formatting ? Object.values(formatting).reduce( (acc, curr) => - acc + curr.score * multiplierFactor.formattingMultiplier * (curr.count * multiplierFactor.wordValue), - 0 + acc.add( + new Decimal(curr.score) + .mul(multiplierFactor.formattingMultiplier) + .mul(curr.count) + .mul(multiplierFactor.wordValue) + ), + new Decimal(0) ) - : 0; + : new Decimal(0); comment.score = { ...comment.score, formatting: { content: formatting, ...multiplierFactor, }, - reward: comment.score?.reward ? comment.score.reward + formattingTotal : formattingTotal, + reward: (comment.score?.reward ? formattingTotal.add(comment.score.reward) : formattingTotal).toNumber(), }; } } diff --git a/src/parser/processor.ts b/src/parser/processor.ts index 6b776b8b..aed7c93c 100644 --- a/src/parser/processor.ts +++ b/src/parser/processor.ts @@ -1,3 +1,4 @@ +import Decimal from "decimal.js"; import * as fs from "fs"; import configuration from "../configuration/config-reader"; import { CommentType, GetActivity } from "../get-activity"; @@ -52,19 +53,19 @@ export class Processor { } _sumRewards(obj: Record) { - let totalReward = 0; + let totalReward = new Decimal(0); for (const [key, value] of Object.entries(obj)) { if (Object.prototype.hasOwnProperty.call(obj, key)) { if (key === "reward" && typeof value === "number") { - totalReward += value; + totalReward = totalReward.add(value); } else if (typeof value === "object") { - totalReward += this._sumRewards(value as Record); + totalReward = totalReward.add(this._sumRewards(value as Record)); } } } - return totalReward; + return totalReward.toNumber(); } } diff --git a/yarn.lock b/yarn.lock index 01863eae..54993d7a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3400,7 +3400,7 @@ decamelize@^1.1.0: resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" integrity sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA== -decimal.js@^10.4.3: +decimal.js@10.4.3, decimal.js@^10.4.3: version "10.4.3" resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23" integrity sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==