@thanh01.pmt/curriculum-kit 1.4.32 → 1.4.33

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.
@@ -702,6 +702,52 @@ function getDesignatedFallbackConfig() {
702
702
  }
703
703
 
704
704
  // src/ai/provider-factory.ts
705
+ function cleanNonStreamingFetch() {
706
+ return async (input, init) => {
707
+ const res = await fetch(input, init);
708
+ let wantsStream = false;
709
+ try {
710
+ const body = init?.body;
711
+ if (typeof body === "string") wantsStream = /"stream"\s*:\s*true/.test(body);
712
+ } catch {
713
+ }
714
+ if (wantsStream) return res;
715
+ const contentType = res.headers.get("content-type") || "";
716
+ if (!contentType.includes("text/event-stream") && !contentType.includes("application/json")) return res;
717
+ const text = await res.text();
718
+ const trimmed = text.trim();
719
+ if (trimmed.startsWith("data:")) {
720
+ let content = "";
721
+ let lastChunk = null;
722
+ for (const line of trimmed.split("\n")) {
723
+ const l = line.trim();
724
+ if (!l || !l.startsWith("data:")) continue;
725
+ const jsonStr = l.slice(5).trim();
726
+ if (jsonStr === "[DONE]") continue;
727
+ try {
728
+ const chunk = JSON.parse(jsonStr);
729
+ lastChunk = chunk;
730
+ content += chunk.choices?.[0]?.delta?.content || "";
731
+ } catch {
732
+ }
733
+ }
734
+ const assembled = {
735
+ id: lastChunk?.id || "chatcmpl-" + Date.now(),
736
+ object: "chat.completion",
737
+ created: lastChunk?.created || Math.floor(Date.now() / 1e3),
738
+ model: lastChunk?.model || "9router",
739
+ choices: [{ index: 0, message: { role: "assistant", content }, finish_reason: "stop" }],
740
+ usage: lastChunk?.usage || void 0
741
+ };
742
+ const headers = new Headers(res.headers);
743
+ headers.set("content-type", "application/json");
744
+ return new Response(JSON.stringify(assembled), { status: res.status, statusText: res.statusText, headers });
745
+ }
746
+ const doneIndex = text.indexOf("data: [DONE]");
747
+ if (doneIndex === -1) return res;
748
+ return new Response(text.slice(0, doneIndex).trim(), { status: res.status, statusText: res.statusText, headers: res.headers });
749
+ };
750
+ }
705
751
  var DEFAULT_MODELS = {
706
752
  "9router": "fast-reasoning",
707
753
  ninerouter: "fast-reasoning",
@@ -732,7 +778,8 @@ function getAIModel(options = {}) {
732
778
  const rawUrl = options.baseURL || resolveApiKey("NINEROUTER_BASE_URL") || resolveApiKey("NINE_ROUTER_BASE_URL") || "https://ai-router.orchable.app/v1";
733
779
  const ninerouter = openai.createOpenAI({
734
780
  apiKey,
735
- baseURL: rawUrl.replace(/^["']|["']$/g, "").trim()
781
+ baseURL: rawUrl.replace(/^["']|["']$/g, "").trim(),
782
+ fetch: cleanNonStreamingFetch()
736
783
  });
737
784
  return ninerouter.chat(resolvedModelName || "fast-reasoning");
738
785
  }