assistant-stream 0.0.26 → 0.0.28

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.js CHANGED
@@ -823,6 +823,10 @@ var AssistantStreamControllerImpl = class {
823
823
  this._closeSubscriber = callback;
824
824
  }
825
825
  _addPart(part, stream) {
826
+ if (this._append) {
827
+ this._append.controller.close();
828
+ this._append = void 0;
829
+ }
826
830
  this.enqueue({
827
831
  type: "part-start",
828
832
  part,
@@ -839,9 +843,6 @@ var AssistantStreamControllerImpl = class {
839
843
  }
840
844
  appendText(textDelta) {
841
845
  if (this._append?.kind !== "text") {
842
- if (this._append) {
843
- this._append.controller.close();
844
- }
845
846
  this._append = {
846
847
  kind: "text",
847
848
  controller: this.addTextPart()
@@ -851,9 +852,6 @@ var AssistantStreamControllerImpl = class {
851
852
  }
852
853
  appendReasoning(textDelta) {
853
854
  if (this._append?.kind !== "reasoning") {
854
- if (this._append) {
855
- this._append.controller.close();
856
- }
857
855
  this._append = {
858
856
  kind: "reasoning",
859
857
  controller: this.addReasoningPart()
@@ -1000,6 +998,22 @@ function createAssistantStreamResponse(callback) {
1000
998
 
1001
999
  // src/core/effects/ToolExecutionStream.ts
1002
1000
  var import_secure_json_parse = __toESM(require("secure-json-parse"));
1001
+
1002
+ // src/core/utils/withPromiseOrValue.ts
1003
+ function withPromiseOrValue(callback, thenHandler, catchHandler) {
1004
+ try {
1005
+ const promiseOrValue = callback();
1006
+ if (typeof promiseOrValue === "object" && promiseOrValue !== null && "then" in promiseOrValue) {
1007
+ return promiseOrValue.then(thenHandler, catchHandler);
1008
+ } else {
1009
+ thenHandler(promiseOrValue);
1010
+ }
1011
+ } catch (e) {
1012
+ catchHandler(e);
1013
+ }
1014
+ }
1015
+
1016
+ // src/core/effects/ToolExecutionStream.ts
1003
1017
  var ToolExecutionStream = class extends PipeableTransformStream {
1004
1018
  constructor(toolCallback) {
1005
1019
  const toolCallPromises = /* @__PURE__ */ new Map();
@@ -1029,58 +1043,49 @@ var ToolExecutionStream = class extends PipeableTransformStream {
1029
1043
  const argsText = toolCallArgsText[toolCallId];
1030
1044
  if (!argsText)
1031
1045
  throw new Error("Unexpected tool call without args");
1032
- const executeTool = () => {
1033
- let args;
1034
- try {
1035
- args = import_secure_json_parse.default.parse(argsText);
1036
- } catch (e) {
1037
- throw new Error(
1038
- `Function parameter parsing failed. ${JSON.stringify(e.message)}`
1039
- );
1040
- }
1041
- return toolCallback({
1042
- toolCallId,
1043
- toolName,
1044
- args
1045
- });
1046
- };
1047
- let promiseOrUndefined;
1048
- try {
1049
- promiseOrUndefined = executeTool();
1050
- } catch (e) {
1051
- controller.enqueue({
1052
- type: "result",
1053
- path: chunk.path,
1054
- result: String(e),
1055
- isError: true
1056
- });
1057
- break;
1058
- }
1059
- if (promiseOrUndefined instanceof Promise) {
1060
- const toolCallPromise = promiseOrUndefined.then((c) => {
1046
+ const promise = withPromiseOrValue(
1047
+ () => {
1048
+ let args;
1049
+ try {
1050
+ args = import_secure_json_parse.default.parse(argsText);
1051
+ } catch (e) {
1052
+ throw new Error(
1053
+ `Function parameter parsing failed. ${JSON.stringify(e.message)}`
1054
+ );
1055
+ }
1056
+ return toolCallback({
1057
+ toolCallId,
1058
+ toolName,
1059
+ args
1060
+ });
1061
+ },
1062
+ (c) => {
1061
1063
  if (c === void 0) return;
1064
+ if (c.artifact !== void 0) {
1065
+ controller.enqueue({
1066
+ type: "artifact",
1067
+ path: chunk.path,
1068
+ artifact: c.artifact
1069
+ });
1070
+ }
1062
1071
  controller.enqueue({
1063
1072
  type: "result",
1064
1073
  path: chunk.path,
1065
- result: c,
1066
- isError: false
1074
+ result: c.result,
1075
+ isError: c.isError
1067
1076
  });
1068
- }).catch((e) => {
1077
+ },
1078
+ (e) => {
1069
1079
  controller.enqueue({
1070
1080
  type: "result",
1071
1081
  path: chunk.path,
1072
1082
  result: String(e),
1073
1083
  isError: true
1074
1084
  });
1075
- });
1076
- toolCallPromises.set(toolCallId, toolCallPromise);
1077
- } else if (promiseOrUndefined !== void 0) {
1078
- controller.enqueue({
1079
- type: "result",
1080
- path: chunk.path,
1081
- result: promiseOrUndefined,
1082
- isError: false
1083
- });
1085
+ }
1086
+ );
1087
+ if (promise) {
1088
+ toolCallPromises.set(toolCallId, promise);
1084
1089
  }
1085
1090
  break;
1086
1091
  }
@@ -1694,8 +1699,10 @@ var handleErrorChunk = (message, chunk) => {
1694
1699
  };
1695
1700
  };
1696
1701
  var AssistantMessageAccumulator = class extends TransformStream {
1697
- constructor() {
1698
- let message = createInitialMessage();
1702
+ constructor({
1703
+ initialMessage
1704
+ } = {}) {
1705
+ let message = initialMessage ?? createInitialMessage();
1699
1706
  super({
1700
1707
  transform(chunk, controller) {
1701
1708
  const type = chunk.type;