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