@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.
package/dist/index.mjs CHANGED
@@ -979,6 +979,52 @@ var init_streamRunner = __esm({
979
979
  DEFAULT_ARTIFACT_STREAM_TOTAL_MS = 48e4;
980
980
  }
981
981
  });
982
+ function cleanNonStreamingFetch() {
983
+ return async (input, init) => {
984
+ const res = await fetch(input, init);
985
+ let wantsStream = false;
986
+ try {
987
+ const body = init?.body;
988
+ if (typeof body === "string") wantsStream = /"stream"\s*:\s*true/.test(body);
989
+ } catch {
990
+ }
991
+ if (wantsStream) return res;
992
+ const contentType = res.headers.get("content-type") || "";
993
+ if (!contentType.includes("text/event-stream") && !contentType.includes("application/json")) return res;
994
+ const text = await res.text();
995
+ const trimmed = text.trim();
996
+ if (trimmed.startsWith("data:")) {
997
+ let content = "";
998
+ let lastChunk = null;
999
+ for (const line of trimmed.split("\n")) {
1000
+ const l = line.trim();
1001
+ if (!l || !l.startsWith("data:")) continue;
1002
+ const jsonStr = l.slice(5).trim();
1003
+ if (jsonStr === "[DONE]") continue;
1004
+ try {
1005
+ const chunk = JSON.parse(jsonStr);
1006
+ lastChunk = chunk;
1007
+ content += chunk.choices?.[0]?.delta?.content || "";
1008
+ } catch {
1009
+ }
1010
+ }
1011
+ const assembled = {
1012
+ id: lastChunk?.id || "chatcmpl-" + Date.now(),
1013
+ object: "chat.completion",
1014
+ created: lastChunk?.created || Math.floor(Date.now() / 1e3),
1015
+ model: lastChunk?.model || "9router",
1016
+ choices: [{ index: 0, message: { role: "assistant", content }, finish_reason: "stop" }],
1017
+ usage: lastChunk?.usage || void 0
1018
+ };
1019
+ const headers = new Headers(res.headers);
1020
+ headers.set("content-type", "application/json");
1021
+ return new Response(JSON.stringify(assembled), { status: res.status, statusText: res.statusText, headers });
1022
+ }
1023
+ const doneIndex = text.indexOf("data: [DONE]");
1024
+ if (doneIndex === -1) return res;
1025
+ return new Response(text.slice(0, doneIndex).trim(), { status: res.status, statusText: res.statusText, headers: res.headers });
1026
+ };
1027
+ }
982
1028
  function getAIModel(options = {}) {
983
1029
  const designated = getDesignatedFallbackConfig();
984
1030
  const defaultProvider = process.env.DEFAULT_AI_PROVIDER || process.env.LLM_PROVIDER || detectDefaultProvider();
@@ -997,7 +1043,8 @@ function getAIModel(options = {}) {
997
1043
  const rawUrl = options.baseURL || resolveApiKey("NINEROUTER_BASE_URL") || resolveApiKey("NINE_ROUTER_BASE_URL") || "https://ai-router.orchable.app/v1";
998
1044
  const ninerouter = createOpenAI({
999
1045
  apiKey,
1000
- baseURL: rawUrl.replace(/^["']|["']$/g, "").trim()
1046
+ baseURL: rawUrl.replace(/^["']|["']$/g, "").trim(),
1047
+ fetch: cleanNonStreamingFetch()
1001
1048
  });
1002
1049
  return ninerouter.chat(resolvedModelName || "fast-reasoning");
1003
1050
  }