@thanh01.pmt/curriculum-kit 1.4.50 → 1.4.52

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.
@@ -8,6 +8,7 @@ var fs2 = require('fs');
8
8
  var path = require('path');
9
9
  var zod = require('zod');
10
10
  var url = require('url');
11
+ var jsonrepair = require('jsonrepair');
11
12
  var pLimit = require('p-limit');
12
13
  var fsPromises = require('fs/promises');
13
14
 
@@ -3598,6 +3599,152 @@ async function generateExtensionFlow(input) {
3598
3599
  });
3599
3600
  return object;
3600
3601
  }
3602
+ function closeTruncatedJson(candidate) {
3603
+ let inStr = false;
3604
+ let esc = false;
3605
+ const stack = [];
3606
+ for (let i = 0; i < candidate.length; i++) {
3607
+ const ch = candidate[i];
3608
+ if (esc) {
3609
+ esc = false;
3610
+ continue;
3611
+ }
3612
+ if (inStr) {
3613
+ if (ch === "\\") esc = true;
3614
+ else if (ch === '"') inStr = false;
3615
+ continue;
3616
+ }
3617
+ if (ch === '"') inStr = true;
3618
+ else if (ch === "{" || ch === "[") stack.push(ch);
3619
+ else if (ch === "}" || ch === "]") stack.pop();
3620
+ }
3621
+ if (stack.length === 0 && !inStr) return null;
3622
+ let out = candidate;
3623
+ out = out.replace(/,\s*$/, "");
3624
+ if (inStr) {
3625
+ out = out.replace(/\\+$/, "");
3626
+ out += '"';
3627
+ }
3628
+ while (stack.length > 0) {
3629
+ out += stack.pop() === "{" ? "}" : "]";
3630
+ }
3631
+ return out;
3632
+ }
3633
+ function stripLlmJsonWrappers(rawText) {
3634
+ let cleaned = rawText.replace(/<think>[\s\S]*?<\/think>/gi, "").replace(/```json\s*/gi, "").replace(/```\s*/g, "").trim();
3635
+ const firstBrace = cleaned.indexOf("{");
3636
+ if (firstBrace > 0) {
3637
+ cleaned = cleaned.slice(firstBrace);
3638
+ }
3639
+ return cleaned.trim();
3640
+ }
3641
+ function safeParseJson(rawText) {
3642
+ if (!rawText) return null;
3643
+ const cleaned = stripLlmJsonWrappers(rawText);
3644
+ if (!cleaned) return null;
3645
+ try {
3646
+ return JSON.parse(cleaned);
3647
+ } catch {
3648
+ }
3649
+ const firstBrace = cleaned.indexOf("{");
3650
+ if (firstBrace === -1) return null;
3651
+ const buildCandidates = () => {
3652
+ const list = [];
3653
+ const closed = closeTruncatedJson(cleaned.slice(firstBrace));
3654
+ if (closed) list.push(closed);
3655
+ const lastBrace = cleaned.lastIndexOf("}");
3656
+ if (lastBrace > firstBrace) {
3657
+ list.push(cleaned.slice(firstBrace, lastBrace + 1));
3658
+ }
3659
+ return list;
3660
+ };
3661
+ for (const candidate of buildCandidates()) {
3662
+ try {
3663
+ return JSON.parse(candidate);
3664
+ } catch {
3665
+ }
3666
+ try {
3667
+ return JSON.parse(jsonrepair.jsonrepair(candidate));
3668
+ } catch {
3669
+ }
3670
+ }
3671
+ let depth = 0;
3672
+ let inStr = false;
3673
+ let esc = false;
3674
+ for (let i = firstBrace; i < cleaned.length; i++) {
3675
+ const ch = cleaned[i];
3676
+ if (esc) {
3677
+ esc = false;
3678
+ continue;
3679
+ }
3680
+ if (inStr) {
3681
+ if (ch === "\\") esc = true;
3682
+ else if (ch === '"') inStr = false;
3683
+ continue;
3684
+ }
3685
+ if (ch === '"') inStr = true;
3686
+ else if (ch === "{") depth++;
3687
+ else if (ch === "}") {
3688
+ depth--;
3689
+ if (depth === 0) {
3690
+ const candidate = cleaned.slice(firstBrace, i + 1);
3691
+ try {
3692
+ return JSON.parse(candidate);
3693
+ } catch {
3694
+ }
3695
+ try {
3696
+ return JSON.parse(jsonrepair.jsonrepair(candidate));
3697
+ } catch {
3698
+ }
3699
+ }
3700
+ }
3701
+ }
3702
+ try {
3703
+ return JSON.parse(jsonrepair.jsonrepair(cleaned));
3704
+ } catch {
3705
+ }
3706
+ return null;
3707
+ }
3708
+
3709
+ // src/ai/flows/auditCurriculumQualityFlow.ts
3710
+ var REPAIR_SUFFIX = "\n\nCRITICAL OUTPUT FORMAT FIX: your previous response could not be parsed as JSON. Output ONLY the raw JSON object \u2014 no markdown code fences, no prose before or after. Start with { and end with }.";
3711
+ function extractRawText(err) {
3712
+ const e = err;
3713
+ if (typeof e?.text === "string" && e.text) return e.text;
3714
+ const cause = e?.cause;
3715
+ if (typeof cause?.text === "string" && cause.text) return cause.text;
3716
+ const msg = typeof e?.message === "string" ? e.message : typeof cause?.message === "string" ? cause.message : "";
3717
+ const m = msg.match(/Text: ([\s\S]+)/);
3718
+ return m ? m[1] : "";
3719
+ }
3720
+ async function generateJudgeObject(opts) {
3721
+ let firstText = "";
3722
+ try {
3723
+ return (await ai.generateObject({ model: opts.model, schema: opts.schema, prompt: opts.prompt })).object;
3724
+ } catch (err) {
3725
+ firstText = extractRawText(err);
3726
+ if (firstText) {
3727
+ const salvaged = safeParseJson(firstText);
3728
+ if (salvaged) {
3729
+ const check = opts.schema.safeParse(salvaged);
3730
+ if (check.success) return check.data;
3731
+ }
3732
+ }
3733
+ try {
3734
+ return (await ai.generateObject({ model: opts.model, schema: opts.schema, prompt: opts.prompt + REPAIR_SUFFIX })).object;
3735
+ } catch (err2) {
3736
+ const secondText = extractRawText(err2);
3737
+ if (secondText && secondText !== firstText) {
3738
+ const salvaged2 = safeParseJson(secondText);
3739
+ if (salvaged2) {
3740
+ const check2 = opts.schema.safeParse(salvaged2);
3741
+ if (check2.success) return check2.data;
3742
+ }
3743
+ }
3744
+ throw err2;
3745
+ }
3746
+ }
3747
+ }
3601
3748
  async function auditCurriculumQualityFlow(input) {
3602
3749
  const model = getAIModel(input.modelOptions);
3603
3750
  const prompt = buildJudgePrompt({
@@ -3608,7 +3755,7 @@ async function auditCurriculumQualityFlow(input) {
3608
3755
  generatedContentJson: input.generatedContentJson,
3609
3756
  standardStatements: input.standardStatements
3610
3757
  });
3611
- const { object } = await ai.generateObject({
3758
+ const object = await generateJudgeObject({
3612
3759
  model,
3613
3760
  schema: CurriculumQualityReportSchema,
3614
3761
  prompt: `${prompt}