@openbkn/bkn-sdk 0.1.1-alpha.8 → 0.1.1-alpha.9

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.
@@ -748,6 +748,15 @@ function admin(ctx) {
748
748
  };
749
749
  }
750
750
 
751
+ // src/api/auth-fetch.ts
752
+ async function authFetch(ctx, send) {
753
+ let res = await send();
754
+ if (res.status === 401 && ctx.refresh && await tryRefresh(ctx)) {
755
+ res = await send();
756
+ }
757
+ return res;
758
+ }
759
+
751
760
  // src/api/agent-chat.ts
752
761
  var FACTORY = "/api/agent-factory";
753
762
  async function fetchAgentInfo(ctx, agentId, version = "v0") {
@@ -816,16 +825,19 @@ async function sendChat(ctx, info, query, opts = {}) {
816
825
  stream: Boolean(opts.stream)
817
826
  };
818
827
  if (opts.conversationId) body.conversation_id = opts.conversationId;
819
- const res = await fetch(`${ctx.baseUrl}${FACTORY}/v1/app/${info.key}/chat/completion`, {
820
- method: "POST",
821
- headers: {
822
- ...buildHeaders(ctx),
823
- "content-type": "application/json",
824
- accept: opts.stream ? "text/event-stream" : "application/json",
825
- "x-language": "zh-CN"
826
- },
827
- body: JSON.stringify(body)
828
- });
828
+ const res = await authFetch(
829
+ ctx,
830
+ () => fetch(`${ctx.baseUrl}${FACTORY}/v1/app/${info.key}/chat/completion`, {
831
+ method: "POST",
832
+ headers: {
833
+ ...buildHeaders(ctx),
834
+ "content-type": "application/json",
835
+ accept: opts.stream ? "text/event-stream" : "application/json",
836
+ "x-language": "zh-CN"
837
+ },
838
+ body: JSON.stringify(body)
839
+ })
840
+ );
829
841
  if (!res.ok) throw new HttpError(res.status, res.statusText, await res.text());
830
842
  const contentType = res.headers.get("content-type") ?? "";
831
843
  if (opts.stream && contentType.includes("text/event-stream")) {
@@ -1039,11 +1051,14 @@ function parseBody(text) {
1039
1051
  }
1040
1052
  async function post(ctx, knId, sessionId, body) {
1041
1053
  applyTls(ctx);
1042
- const res = await fetch(mcpUrl(ctx), {
1043
- method: "POST",
1044
- headers: headers(ctx, knId, sessionId),
1045
- body: JSON.stringify(body)
1046
- });
1054
+ const res = await authFetch(
1055
+ ctx,
1056
+ () => fetch(mcpUrl(ctx), {
1057
+ method: "POST",
1058
+ headers: headers(ctx, knId, sessionId),
1059
+ body: JSON.stringify(body)
1060
+ })
1061
+ );
1047
1062
  const text = await res.text();
1048
1063
  if (!res.ok) throw new HttpError(res.status, res.statusText, text);
1049
1064
  return { res, text };
@@ -2727,7 +2742,10 @@ async function uploadBkn(ctx, tarBuffer, opts = {}) {
2727
2742
  new Blob([new Uint8Array(tarBuffer)], { type: "application/octet-stream" }),
2728
2743
  "bkn.tar"
2729
2744
  );
2730
- const res = await fetch(url, { method: "POST", headers: buildHeaders(ctx), body: form2 });
2745
+ const res = await authFetch(
2746
+ ctx,
2747
+ () => fetch(url, { method: "POST", headers: buildHeaders(ctx), body: form2 })
2748
+ );
2731
2749
  const text = await res.text();
2732
2750
  if (!res.ok) throw new HttpError(res.status, res.statusText, text);
2733
2751
  return text ? JSON.parse(text) : void 0;
@@ -2736,7 +2754,7 @@ async function downloadBkn(ctx, knId, opts = {}) {
2736
2754
  applyTls(ctx);
2737
2755
  const url = new URL(`${ctx.baseUrl}${BKNS}/${encodeURIComponent(knId)}`);
2738
2756
  url.searchParams.set("branch", opts.branch ?? "main");
2739
- const res = await fetch(url, { method: "GET", headers: buildHeaders(ctx) });
2757
+ const res = await authFetch(ctx, () => fetch(url, { method: "GET", headers: buildHeaders(ctx) }));
2740
2758
  if (!res.ok) throw new HttpError(res.status, res.statusText, await res.text());
2741
2759
  return Buffer.from(await res.arrayBuffer());
2742
2760
  }
@@ -3646,15 +3664,18 @@ function deltaContent(chunk) {
3646
3664
  }
3647
3665
  async function chatCompletionsStream(ctx, model, messages, onDelta) {
3648
3666
  applyTls(ctx);
3649
- const res = await fetch(`${ctx.baseUrl}${API}/chat/completions`, {
3650
- method: "POST",
3651
- headers: {
3652
- ...buildHeaders(ctx),
3653
- "content-type": "application/json",
3654
- accept: "text/event-stream"
3655
- },
3656
- body: JSON.stringify({ model, messages, stream: true })
3657
- });
3667
+ const res = await authFetch(
3668
+ ctx,
3669
+ () => fetch(`${ctx.baseUrl}${API}/chat/completions`, {
3670
+ method: "POST",
3671
+ headers: {
3672
+ ...buildHeaders(ctx),
3673
+ "content-type": "application/json",
3674
+ accept: "text/event-stream"
3675
+ },
3676
+ body: JSON.stringify({ model, messages, stream: true })
3677
+ })
3678
+ );
3658
3679
  if (!res.ok) throw new HttpError(res.status, res.statusText, await res.text());
3659
3680
  const reader = res.body?.getReader();
3660
3681
  if (!reader) throw new Error("No response body for stream");
@@ -3761,11 +3782,14 @@ async function registerSkillZip(ctx, bytes, opts = {}) {
3761
3782
  form2.set("file", new Blob([bytes]), opts.filename ?? "skill.zip");
3762
3783
  if (opts.source) form2.set("source", opts.source);
3763
3784
  if (opts.extendInfo) form2.set("extend_info", JSON.stringify(opts.extendInfo));
3764
- const res = await fetch(`${ctx.baseUrl}${BASE5}/skills`, {
3765
- method: "POST",
3766
- headers: buildHeaders(ctx),
3767
- body: form2
3768
- });
3785
+ const res = await authFetch(
3786
+ ctx,
3787
+ () => fetch(`${ctx.baseUrl}${BASE5}/skills`, {
3788
+ method: "POST",
3789
+ headers: buildHeaders(ctx),
3790
+ body: form2
3791
+ })
3792
+ );
3769
3793
  const text = await res.text();
3770
3794
  if (!res.ok) throw new HttpError(res.status, res.statusText, text);
3771
3795
  return text ? JSON.parse(text) : void 0;
@@ -3775,20 +3799,26 @@ async function updateSkillPackageZip(ctx, skillId, bytes, filename = "skill.zip"
3775
3799
  const form2 = new FormData();
3776
3800
  form2.set("file_type", "zip");
3777
3801
  form2.set("file", new Blob([bytes]), filename);
3778
- const res = await fetch(`${ctx.baseUrl}${BASE5}/skills/${encodeURIComponent(skillId)}/package`, {
3779
- method: "PUT",
3780
- headers: buildHeaders(ctx),
3781
- body: form2
3782
- });
3802
+ const res = await authFetch(
3803
+ ctx,
3804
+ () => fetch(`${ctx.baseUrl}${BASE5}/skills/${encodeURIComponent(skillId)}/package`, {
3805
+ method: "PUT",
3806
+ headers: buildHeaders(ctx),
3807
+ body: form2
3808
+ })
3809
+ );
3783
3810
  const text = await res.text();
3784
3811
  if (!res.ok) throw new HttpError(res.status, res.statusText, text);
3785
3812
  return text ? JSON.parse(text) : void 0;
3786
3813
  }
3787
3814
  async function downloadSkill(ctx, skillId) {
3788
3815
  applyTls(ctx);
3789
- const res = await fetch(`${ctx.baseUrl}${BASE5}/skills/${encodeURIComponent(skillId)}/download`, {
3790
- headers: buildHeaders(ctx)
3791
- });
3816
+ const res = await authFetch(
3817
+ ctx,
3818
+ () => fetch(`${ctx.baseUrl}${BASE5}/skills/${encodeURIComponent(skillId)}/download`, {
3819
+ headers: buildHeaders(ctx)
3820
+ })
3821
+ );
3792
3822
  if (!res.ok) throw new HttpError(res.status, res.statusText, await res.text());
3793
3823
  return new Uint8Array(await res.arrayBuffer());
3794
3824
  }
@@ -3942,9 +3972,11 @@ var PATH = "/api/agent-operator-integration/v1/tool-box";
3942
3972
  var IMPEX = "/api/agent-operator-integration/v1/impex";
3943
3973
  async function exportConfig(ctx, id, type = "toolbox") {
3944
3974
  applyTls(ctx);
3945
- const res = await fetch(
3946
- `${ctx.baseUrl}${IMPEX}/export/${encodeURIComponent(type)}/${encodeURIComponent(id)}`,
3947
- { headers: buildHeaders(ctx) }
3975
+ const res = await authFetch(
3976
+ ctx,
3977
+ () => fetch(`${ctx.baseUrl}${IMPEX}/export/${encodeURIComponent(type)}/${encodeURIComponent(id)}`, {
3978
+ headers: buildHeaders(ctx)
3979
+ })
3948
3980
  );
3949
3981
  const buf = new Uint8Array(await res.arrayBuffer());
3950
3982
  if (!res.ok) throw new HttpError(res.status, res.statusText, new TextDecoder().decode(buf));
@@ -3955,11 +3987,14 @@ async function importConfig(ctx, filePath, type = "toolbox") {
3955
3987
  const buf = await readFile2(filePath);
3956
3988
  const form2 = new FormData();
3957
3989
  form2.append("data", new Blob([new Uint8Array(buf)]), basename3(filePath));
3958
- const res = await fetch(`${ctx.baseUrl}${IMPEX}/import/${encodeURIComponent(type)}`, {
3959
- method: "POST",
3960
- headers: buildHeaders(ctx),
3961
- body: form2
3962
- });
3990
+ const res = await authFetch(
3991
+ ctx,
3992
+ () => fetch(`${ctx.baseUrl}${IMPEX}/import/${encodeURIComponent(type)}`, {
3993
+ method: "POST",
3994
+ headers: buildHeaders(ctx),
3995
+ body: form2
3996
+ })
3997
+ );
3963
3998
  const text = await res.text();
3964
3999
  if (!res.ok) throw new HttpError(res.status, res.statusText, text);
3965
4000
  return text ? JSON.parse(text) : text;
@@ -3970,11 +4005,14 @@ async function uploadTool(ctx, boxId, filePath, metadataType = "openapi") {
3970
4005
  const form2 = new FormData();
3971
4006
  form2.append("metadata_type", metadataType);
3972
4007
  form2.append("data", new Blob([new Uint8Array(buf)]), basename3(filePath));
3973
- const res = await fetch(`${ctx.baseUrl}${PATH}/${encodeURIComponent(boxId)}/tool`, {
3974
- method: "POST",
3975
- headers: buildHeaders(ctx),
3976
- body: form2
3977
- });
4008
+ const res = await authFetch(
4009
+ ctx,
4010
+ () => fetch(`${ctx.baseUrl}${PATH}/${encodeURIComponent(boxId)}/tool`, {
4011
+ method: "POST",
4012
+ headers: buildHeaders(ctx),
4013
+ body: form2
4014
+ })
4015
+ );
3978
4016
  const text = await res.text();
3979
4017
  if (!res.ok) throw new HttpError(res.status, res.statusText, text);
3980
4018
  return text ? JSON.parse(text) : text;
@@ -4952,12 +4990,11 @@ async function rawCall(ctx, path, opts = {}) {
4952
4990
  `);
4953
4991
  const controller = new AbortController();
4954
4992
  const timer = setTimeout(() => controller.abort(), opts.timeoutMs ?? 3e4);
4955
- const send = () => fetch(url, { method, headers: headersFor(), body, signal: controller.signal });
4956
4993
  try {
4957
- let res = await send();
4958
- if (res.status === 401 && ctx.refresh && await tryRefresh(ctx)) {
4959
- res = await send();
4960
- }
4994
+ const res = await authFetch(
4995
+ ctx,
4996
+ () => fetch(url, { method, headers: headersFor(), body, signal: controller.signal })
4997
+ );
4961
4998
  return { status: res.status, statusText: res.statusText, body: await res.text() };
4962
4999
  } finally {
4963
5000
  clearTimeout(timer);
@@ -5201,4 +5238,4 @@ export {
5201
5238
  exportCreds,
5202
5239
  auth_exports
5203
5240
  };
5204
- //# sourceMappingURL=chunk-DTU5DGJW.js.map
5241
+ //# sourceMappingURL=chunk-BFE4SU3V.js.map