@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.
@@ -6,6 +6,7 @@ import { createDeepSeek } from '@ai-sdk/deepseek';
6
6
  import { fileURLToPath } from 'url';
7
7
  import { generateObject } from 'ai';
8
8
  import { z } from 'zod';
9
+ import { jsonrepair } from 'jsonrepair';
9
10
  import { defineHook, getStepMetadata, RetryableError, sleep } from 'workflow';
10
11
  import fs3 from 'fs/promises';
11
12
  import { Octokit } from '@octokit/rest';
@@ -3855,6 +3856,156 @@ async function generateExtensionFlow(input) {
3855
3856
 
3856
3857
  // src/ai/flows/auditCurriculumQualityFlow.ts
3857
3858
  init_provider_factory();
3859
+
3860
+ // src/services/prefillService.ts
3861
+ init_streamRunner();
3862
+ init_provider_factory();
3863
+ function closeTruncatedJson(candidate) {
3864
+ let inStr = false;
3865
+ let esc = false;
3866
+ const stack = [];
3867
+ for (let i = 0; i < candidate.length; i++) {
3868
+ const ch = candidate[i];
3869
+ if (esc) {
3870
+ esc = false;
3871
+ continue;
3872
+ }
3873
+ if (inStr) {
3874
+ if (ch === "\\") esc = true;
3875
+ else if (ch === '"') inStr = false;
3876
+ continue;
3877
+ }
3878
+ if (ch === '"') inStr = true;
3879
+ else if (ch === "{" || ch === "[") stack.push(ch);
3880
+ else if (ch === "}" || ch === "]") stack.pop();
3881
+ }
3882
+ if (stack.length === 0 && !inStr) return null;
3883
+ let out = candidate;
3884
+ out = out.replace(/,\s*$/, "");
3885
+ if (inStr) {
3886
+ out = out.replace(/\\+$/, "");
3887
+ out += '"';
3888
+ }
3889
+ while (stack.length > 0) {
3890
+ out += stack.pop() === "{" ? "}" : "]";
3891
+ }
3892
+ return out;
3893
+ }
3894
+ function stripLlmJsonWrappers(rawText) {
3895
+ let cleaned = rawText.replace(/<think>[\s\S]*?<\/think>/gi, "").replace(/```json\s*/gi, "").replace(/```\s*/g, "").trim();
3896
+ const firstBrace = cleaned.indexOf("{");
3897
+ if (firstBrace > 0) {
3898
+ cleaned = cleaned.slice(firstBrace);
3899
+ }
3900
+ return cleaned.trim();
3901
+ }
3902
+ function safeParseJson(rawText) {
3903
+ if (!rawText) return null;
3904
+ const cleaned = stripLlmJsonWrappers(rawText);
3905
+ if (!cleaned) return null;
3906
+ try {
3907
+ return JSON.parse(cleaned);
3908
+ } catch {
3909
+ }
3910
+ const firstBrace = cleaned.indexOf("{");
3911
+ if (firstBrace === -1) return null;
3912
+ const buildCandidates = () => {
3913
+ const list = [];
3914
+ const closed = closeTruncatedJson(cleaned.slice(firstBrace));
3915
+ if (closed) list.push(closed);
3916
+ const lastBrace = cleaned.lastIndexOf("}");
3917
+ if (lastBrace > firstBrace) {
3918
+ list.push(cleaned.slice(firstBrace, lastBrace + 1));
3919
+ }
3920
+ return list;
3921
+ };
3922
+ for (const candidate of buildCandidates()) {
3923
+ try {
3924
+ return JSON.parse(candidate);
3925
+ } catch {
3926
+ }
3927
+ try {
3928
+ return JSON.parse(jsonrepair(candidate));
3929
+ } catch {
3930
+ }
3931
+ }
3932
+ let depth = 0;
3933
+ let inStr = false;
3934
+ let esc = false;
3935
+ for (let i = firstBrace; i < cleaned.length; i++) {
3936
+ const ch = cleaned[i];
3937
+ if (esc) {
3938
+ esc = false;
3939
+ continue;
3940
+ }
3941
+ if (inStr) {
3942
+ if (ch === "\\") esc = true;
3943
+ else if (ch === '"') inStr = false;
3944
+ continue;
3945
+ }
3946
+ if (ch === '"') inStr = true;
3947
+ else if (ch === "{") depth++;
3948
+ else if (ch === "}") {
3949
+ depth--;
3950
+ if (depth === 0) {
3951
+ const candidate = cleaned.slice(firstBrace, i + 1);
3952
+ try {
3953
+ return JSON.parse(candidate);
3954
+ } catch {
3955
+ }
3956
+ try {
3957
+ return JSON.parse(jsonrepair(candidate));
3958
+ } catch {
3959
+ }
3960
+ }
3961
+ }
3962
+ }
3963
+ try {
3964
+ return JSON.parse(jsonrepair(cleaned));
3965
+ } catch {
3966
+ }
3967
+ return null;
3968
+ }
3969
+
3970
+ // src/ai/flows/auditCurriculumQualityFlow.ts
3971
+ 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 }.";
3972
+ function extractRawText(err) {
3973
+ const e = err;
3974
+ if (typeof e?.text === "string" && e.text) return e.text;
3975
+ const cause = e?.cause;
3976
+ if (typeof cause?.text === "string" && cause.text) return cause.text;
3977
+ const msg = typeof e?.message === "string" ? e.message : typeof cause?.message === "string" ? cause.message : "";
3978
+ const m = msg.match(/Text: ([\s\S]+)/);
3979
+ return m ? m[1] : "";
3980
+ }
3981
+ async function generateJudgeObject(opts) {
3982
+ let firstText = "";
3983
+ try {
3984
+ return (await generateObject({ model: opts.model, schema: opts.schema, prompt: opts.prompt })).object;
3985
+ } catch (err) {
3986
+ firstText = extractRawText(err);
3987
+ if (firstText) {
3988
+ const salvaged = safeParseJson(firstText);
3989
+ if (salvaged) {
3990
+ const check = opts.schema.safeParse(salvaged);
3991
+ if (check.success) return check.data;
3992
+ }
3993
+ }
3994
+ try {
3995
+ return (await generateObject({ model: opts.model, schema: opts.schema, prompt: opts.prompt + REPAIR_SUFFIX })).object;
3996
+ } catch (err2) {
3997
+ const secondText = extractRawText(err2);
3998
+ if (secondText && secondText !== firstText) {
3999
+ const salvaged2 = safeParseJson(secondText);
4000
+ if (salvaged2) {
4001
+ const check2 = opts.schema.safeParse(salvaged2);
4002
+ if (check2.success) return check2.data;
4003
+ }
4004
+ }
4005
+ throw err2;
4006
+ }
4007
+ }
4008
+ }
3858
4009
  async function auditCurriculumQualityFlow(input) {
3859
4010
  const model = getAIModel(input.modelOptions);
3860
4011
  const prompt = buildJudgePrompt({
@@ -3865,7 +4016,7 @@ async function auditCurriculumQualityFlow(input) {
3865
4016
  generatedContentJson: input.generatedContentJson,
3866
4017
  standardStatements: input.standardStatements
3867
4018
  });
3868
- const { object } = await generateObject({
4019
+ const object = await generateJudgeObject({
3869
4020
  model,
3870
4021
  schema: CurriculumQualityReportSchema,
3871
4022
  prompt: `${prompt}