@bitfab/sdk 0.53.1 → 0.53.3

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,7 +6,7 @@ import {
6
6
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-XPIB2WYS.js";
9
+ } from "./chunk-YE53VBBW.js";
10
10
  import "./chunk-H6LZRFMN.js";
11
11
  export {
12
12
  BitfabError,
@@ -17,4 +17,4 @@ export {
17
17
  parseRetryAfterMs,
18
18
  serializePayloadBody
19
19
  };
20
- //# sourceMappingURL=http-4ZQB3KK4.js.map
20
+ //# sourceMappingURL=http-DCZ44JXM.js.map
@@ -6,7 +6,7 @@ import {
6
6
  flushTraces,
7
7
  parseRetryAfterMs,
8
8
  serializePayloadBody
9
- } from "./chunk-I5IJASRB.js";
9
+ } from "./chunk-W5GNZSKR.js";
10
10
  export {
11
11
  BitfabError,
12
12
  HttpClient,
@@ -16,4 +16,4 @@ export {
16
16
  parseRetryAfterMs,
17
17
  serializePayloadBody
18
18
  };
19
- //# sourceMappingURL=http-IWUYUIPY.js.map
19
+ //# sourceMappingURL=http-NB6D7OIN.js.map
package/dist/index.cjs CHANGED
@@ -42,7 +42,7 @@ var __version__, __packageName__;
42
42
  var init_version_generated = __esm({
43
43
  "src/version.generated.ts"() {
44
44
  "use strict";
45
- __version__ = "0.53.1";
45
+ __version__ = "0.53.3";
46
46
  __packageName__ = "@bitfab/sdk";
47
47
  }
48
48
  });
@@ -6710,6 +6710,17 @@ function runWithSeedContext(ctx, fn) {
6710
6710
  init_serialize();
6711
6711
  init_simulationPlan();
6712
6712
  init_spanOrigin();
6713
+
6714
+ // src/streamFinalizationError.ts
6715
+ var StreamFinalizationError = class extends Error {
6716
+ constructor(message, output) {
6717
+ super(message);
6718
+ this.output = output;
6719
+ this.name = "StreamFinalizationError";
6720
+ }
6721
+ };
6722
+
6723
+ // src/client.ts
6713
6724
  init_traceMetadata();
6714
6725
 
6715
6726
  // src/tracing.ts
@@ -6983,52 +6994,110 @@ function summarizeGenerate(result, model) {
6983
6994
  }
6984
6995
  return summary;
6985
6996
  }
6986
- function accumulateStream(onComplete, model) {
6997
+ function accumulateStream(source, onComplete, model) {
6998
+ const reader = source.getReader();
6987
6999
  let text = "";
6988
7000
  const toolCalls = [];
6989
7001
  let usage;
6990
7002
  let finishReason;
6991
7003
  let completed = false;
6992
- const complete = () => {
7004
+ let streamFailure;
7005
+ const complete = (failure = streamFailure) => {
6993
7006
  if (completed) {
6994
7007
  return;
6995
7008
  }
6996
7009
  completed = true;
6997
- const summary = {
7010
+ let error;
7011
+ if (failure !== void 0) {
7012
+ try {
7013
+ error = failure.error instanceof Error ? failure.error.message : String(
7014
+ failure.error ?? (failure.cancelled === true ? "Stream cancelled" : "Stream failed")
7015
+ );
7016
+ } catch {
7017
+ error = "Stream failed";
7018
+ }
7019
+ }
7020
+ onComplete({
6998
7021
  text,
6999
7022
  toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
7000
7023
  usage,
7001
- finishReason
7002
- };
7003
- if (model) {
7004
- summary.model = model;
7005
- }
7006
- onComplete(summary);
7024
+ finishReason,
7025
+ ...model !== void 0 && { model },
7026
+ ...failure !== void 0 && { error },
7027
+ ...failure?.cancelled === true && { cancelled: true }
7028
+ });
7007
7029
  };
7008
- return new TransformStream({
7009
- transform(part, controller) {
7010
- try {
7011
- if (part?.type === "text-delta") {
7012
- text += part.delta ?? part.textDelta ?? "";
7013
- } else if (part?.type === "tool-call") {
7014
- toolCalls.push({
7015
- toolCallId: part.toolCallId,
7016
- toolName: part.toolName,
7017
- input: part.input ?? part.args
7018
- });
7019
- } else if (part?.type === "finish") {
7020
- usage = part.usage;
7021
- finishReason = part.finishReason;
7022
- complete();
7030
+ return new ReadableStream(
7031
+ {
7032
+ start(controller) {
7033
+ void reader.closed.then(
7034
+ () => {
7035
+ queueMicrotask(() => {
7036
+ if (!completed) {
7037
+ complete();
7038
+ reader.releaseLock();
7039
+ controller.close();
7040
+ }
7041
+ });
7042
+ },
7043
+ (error) => {
7044
+ if (!completed) {
7045
+ complete({ error });
7046
+ controller.error(error);
7047
+ reader.releaseLock();
7048
+ }
7049
+ }
7050
+ );
7051
+ },
7052
+ async pull(controller) {
7053
+ try {
7054
+ const { done, value: part } = await reader.read();
7055
+ if (completed) {
7056
+ return;
7057
+ }
7058
+ if (done) {
7059
+ complete();
7060
+ reader.releaseLock();
7061
+ controller.close();
7062
+ return;
7063
+ }
7064
+ try {
7065
+ if (part?.type === "text-delta") {
7066
+ text += part.delta ?? part.textDelta ?? "";
7067
+ } else if (part?.type === "tool-call") {
7068
+ toolCalls.push({
7069
+ toolCallId: part.toolCallId,
7070
+ toolName: part.toolName,
7071
+ input: part.input ?? part.args
7072
+ });
7073
+ } else if (part?.type === "error") {
7074
+ streamFailure = { error: part.error };
7075
+ } else if (part?.type === "finish") {
7076
+ usage = part.usage;
7077
+ finishReason = part.finishReason;
7078
+ }
7079
+ } catch {
7080
+ }
7081
+ controller.enqueue(part);
7082
+ } catch (error) {
7083
+ if (!completed) {
7084
+ complete({ error });
7085
+ reader.releaseLock();
7086
+ controller.error(error);
7087
+ }
7088
+ }
7089
+ },
7090
+ async cancel(reason) {
7091
+ complete({ error: reason, cancelled: true });
7092
+ try {
7093
+ await reader.cancel(reason);
7094
+ } finally {
7095
+ reader.releaseLock();
7023
7096
  }
7024
- } catch {
7025
7097
  }
7026
- controller.enqueue(part);
7027
7098
  },
7028
- flush() {
7029
- complete();
7030
- }
7031
- });
7099
+ { highWaterMark: 0 }
7100
+ );
7032
7101
  }
7033
7102
  var BitfabVercelAiHandler = class {
7034
7103
  constructor(config) {
@@ -7069,14 +7138,22 @@ var BitfabVercelAiHandler = class {
7069
7138
  // awaits the summary the accumulator resolves once the stream drains.
7070
7139
  {
7071
7140
  type: "llm",
7072
- finalize: () => summary,
7141
+ finalize: async () => {
7142
+ const output = await summary;
7143
+ if (output.error !== void 0) {
7144
+ throw new StreamFinalizationError(output.error, output);
7145
+ }
7146
+ return output;
7147
+ },
7073
7148
  surface: "inherit",
7074
7149
  instrumentation: "vercel-ai"
7075
7150
  },
7076
7151
  async () => {
7077
7152
  const result = await doStream();
7078
- const stream = result.stream.pipeThrough(
7079
- accumulateStream(resolveSummary, label)
7153
+ const stream = accumulateStream(
7154
+ result.stream,
7155
+ resolveSummary,
7156
+ label
7080
7157
  );
7081
7158
  return { ...result, stream };
7082
7159
  }
@@ -8571,8 +8648,8 @@ var Bitfab = class {
8571
8648
  void self.httpClient.trackDeferred(
8572
8649
  Promise.resolve().then(() => options.finalize(result)).then((output) => sendSpan({ result: output })).catch(
8573
8650
  (error) => sendSpan({
8574
- result: void 0,
8575
- error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
8651
+ result: error instanceof StreamFinalizationError ? error.output : void 0,
8652
+ error: error instanceof StreamFinalizationError ? error.message : error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
8576
8653
  })
8577
8654
  )
8578
8655
  );