@thanh01.pmt/curriculum-kit 1.4.31 → 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.
@@ -949,6 +949,52 @@ var init_streamRunner = __esm({
949
949
  DEFAULT_ARTIFACT_STREAM_TOTAL_MS = 48e4;
950
950
  }
951
951
  });
952
+ function cleanNonStreamingFetch() {
953
+ return async (input, init) => {
954
+ const res = await fetch(input, init);
955
+ let wantsStream = false;
956
+ try {
957
+ const body = init?.body;
958
+ if (typeof body === "string") wantsStream = /"stream"\s*:\s*true/.test(body);
959
+ } catch {
960
+ }
961
+ if (wantsStream) return res;
962
+ const contentType = res.headers.get("content-type") || "";
963
+ if (!contentType.includes("text/event-stream") && !contentType.includes("application/json")) return res;
964
+ const text = await res.text();
965
+ const trimmed = text.trim();
966
+ if (trimmed.startsWith("data:")) {
967
+ let content = "";
968
+ let lastChunk = null;
969
+ for (const line of trimmed.split("\n")) {
970
+ const l = line.trim();
971
+ if (!l || !l.startsWith("data:")) continue;
972
+ const jsonStr = l.slice(5).trim();
973
+ if (jsonStr === "[DONE]") continue;
974
+ try {
975
+ const chunk = JSON.parse(jsonStr);
976
+ lastChunk = chunk;
977
+ content += chunk.choices?.[0]?.delta?.content || "";
978
+ } catch {
979
+ }
980
+ }
981
+ const assembled = {
982
+ id: lastChunk?.id || "chatcmpl-" + Date.now(),
983
+ object: "chat.completion",
984
+ created: lastChunk?.created || Math.floor(Date.now() / 1e3),
985
+ model: lastChunk?.model || "9router",
986
+ choices: [{ index: 0, message: { role: "assistant", content }, finish_reason: "stop" }],
987
+ usage: lastChunk?.usage || void 0
988
+ };
989
+ const headers = new Headers(res.headers);
990
+ headers.set("content-type", "application/json");
991
+ return new Response(JSON.stringify(assembled), { status: res.status, statusText: res.statusText, headers });
992
+ }
993
+ const doneIndex = text.indexOf("data: [DONE]");
994
+ if (doneIndex === -1) return res;
995
+ return new Response(text.slice(0, doneIndex).trim(), { status: res.status, statusText: res.statusText, headers: res.headers });
996
+ };
997
+ }
952
998
  function getAIModel(options = {}) {
953
999
  const designated = getDesignatedFallbackConfig();
954
1000
  const defaultProvider = process.env.DEFAULT_AI_PROVIDER || process.env.LLM_PROVIDER || detectDefaultProvider();
@@ -967,7 +1013,8 @@ function getAIModel(options = {}) {
967
1013
  const rawUrl = options.baseURL || resolveApiKey("NINEROUTER_BASE_URL") || resolveApiKey("NINE_ROUTER_BASE_URL") || "https://ai-router.orchable.app/v1";
968
1014
  const ninerouter = createOpenAI({
969
1015
  apiKey,
970
- baseURL: rawUrl.replace(/^["']|["']$/g, "").trim()
1016
+ baseURL: rawUrl.replace(/^["']|["']$/g, "").trim(),
1017
+ fetch: cleanNonStreamingFetch()
971
1018
  });
972
1019
  return ninerouter.chat(resolvedModelName || "fast-reasoning");
973
1020
  }