ai 5.0.28 → 5.0.29

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # ai
2
2
 
3
+ ## 5.0.29
4
+
5
+ ### Patch Changes
6
+
7
+ - e0e9449: feat(ui): sent isAbort, isDisconnect, isError in useChat onFinish callback
8
+
3
9
  ## 5.0.28
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -56,11 +56,17 @@ npm install @ai-sdk/react
56
56
  ```tsx
57
57
  'use client';
58
58
 
59
+ import { useState } from 'react';
59
60
  import { useChat } from '@ai-sdk/react';
60
61
 
61
62
  export default function Page() {
62
- const { messages, input, handleSubmit, handleInputChange, status } =
63
- useChat();
63
+ const { messages, status, sendMessage } = useChat();
64
+ const [input, setInput] = useState('');
65
+ const handleSubmit = e => {
66
+ e.preventDefault();
67
+ sendMessage({ text: input });
68
+ setInput('');
69
+ };
64
70
 
65
71
  return (
66
72
  <div>
@@ -82,7 +88,7 @@ export default function Page() {
82
88
  <input
83
89
  value={input}
84
90
  placeholder="Send a message..."
85
- onChange={handleInputChange}
91
+ onChange={e => setInput(e.target.value)}
86
92
  disabled={status !== 'ready'}
87
93
  />
88
94
  </form>
package/dist/index.d.mts CHANGED
@@ -4049,9 +4049,22 @@ type ChatOnToolCallCallback<UI_MESSAGE extends UIMessage = UIMessage> = (options
4049
4049
  toolCall: InferUIMessageToolCall<UI_MESSAGE>;
4050
4050
  }) => void | PromiseLike<void>;
4051
4051
  type ChatOnDataCallback<UI_MESSAGE extends UIMessage> = (dataPart: DataUIPart<InferUIMessageData<UI_MESSAGE>>) => void;
4052
+ /**
4053
+ * Function that is called when the assistant response has finished streaming.
4054
+ *
4055
+ * @param message The assistant message that was streamed.
4056
+ * @param messages The full chat history, including the assistant message.
4057
+ *
4058
+ * @param isAbort Indicates whether the request has been aborted.
4059
+ * @param isDisconnect Indicates whether the request has been ended by a network error.
4060
+ * @param isError Indicates whether the request has been ended by an error.
4061
+ */
4052
4062
  type ChatOnFinishCallback<UI_MESSAGE extends UIMessage> = (options: {
4053
4063
  message: UI_MESSAGE;
4054
4064
  messages: UI_MESSAGE[];
4065
+ isAbort: boolean;
4066
+ isDisconnect: boolean;
4067
+ isError: boolean;
4055
4068
  }) => void;
4056
4069
  interface ChatInit<UI_MESSAGE extends UIMessage> {
4057
4070
  /**
@@ -4081,9 +4094,7 @@ interface ChatInit<UI_MESSAGE extends UIMessage> {
4081
4094
  */
4082
4095
  onToolCall?: ChatOnToolCallCallback<UI_MESSAGE>;
4083
4096
  /**
4084
- * Optional callback function that is called when the assistant message is finished streaming.
4085
- *
4086
- * @param message The message that was streamed.
4097
+ * Function that is called when the assistant response has finished streaming.
4087
4098
  */
4088
4099
  onFinish?: ChatOnFinishCallback<UI_MESSAGE>;
4089
4100
  /**
package/dist/index.d.ts CHANGED
@@ -4049,9 +4049,22 @@ type ChatOnToolCallCallback<UI_MESSAGE extends UIMessage = UIMessage> = (options
4049
4049
  toolCall: InferUIMessageToolCall<UI_MESSAGE>;
4050
4050
  }) => void | PromiseLike<void>;
4051
4051
  type ChatOnDataCallback<UI_MESSAGE extends UIMessage> = (dataPart: DataUIPart<InferUIMessageData<UI_MESSAGE>>) => void;
4052
+ /**
4053
+ * Function that is called when the assistant response has finished streaming.
4054
+ *
4055
+ * @param message The assistant message that was streamed.
4056
+ * @param messages The full chat history, including the assistant message.
4057
+ *
4058
+ * @param isAbort Indicates whether the request has been aborted.
4059
+ * @param isDisconnect Indicates whether the request has been ended by a network error.
4060
+ * @param isError Indicates whether the request has been ended by an error.
4061
+ */
4052
4062
  type ChatOnFinishCallback<UI_MESSAGE extends UIMessage> = (options: {
4053
4063
  message: UI_MESSAGE;
4054
4064
  messages: UI_MESSAGE[];
4065
+ isAbort: boolean;
4066
+ isDisconnect: boolean;
4067
+ isError: boolean;
4055
4068
  }) => void;
4056
4069
  interface ChatInit<UI_MESSAGE extends UIMessage> {
4057
4070
  /**
@@ -4081,9 +4094,7 @@ interface ChatInit<UI_MESSAGE extends UIMessage> {
4081
4094
  */
4082
4095
  onToolCall?: ChatOnToolCallCallback<UI_MESSAGE>;
4083
4096
  /**
4084
- * Optional callback function that is called when the assistant message is finished streaming.
4085
- *
4086
- * @param message The message that was streamed.
4097
+ * Function that is called when the assistant response has finished streaming.
4087
4098
  */
4088
4099
  onFinish?: ChatOnFinishCallback<UI_MESSAGE>;
4089
4100
  /**
package/dist/index.js CHANGED
@@ -9517,6 +9517,9 @@ var AbstractChat = class {
9517
9517
  var _a17, _b, _c;
9518
9518
  this.setStatus({ status: "submitted", error: void 0 });
9519
9519
  const lastMessage = this.lastMessage;
9520
+ let isAbort = false;
9521
+ let isDisconnect = false;
9522
+ let isError = false;
9520
9523
  try {
9521
9524
  const activeResponse = {
9522
9525
  state: createStreamingUIMessageState({
@@ -9525,6 +9528,9 @@ var AbstractChat = class {
9525
9528
  }),
9526
9529
  abortController: new AbortController()
9527
9530
  };
9531
+ activeResponse.abortController.signal.addEventListener("abort", () => {
9532
+ isAbort = true;
9533
+ });
9528
9534
  this.activeResponse = activeResponse;
9529
9535
  let stream;
9530
9536
  if (trigger === "resume-stream") {
@@ -9588,21 +9594,33 @@ var AbstractChat = class {
9588
9594
  throw error;
9589
9595
  }
9590
9596
  });
9591
- (_a17 = this.onFinish) == null ? void 0 : _a17.call(this, {
9592
- message: activeResponse.state.message,
9593
- messages: this.state.messages
9594
- });
9595
9597
  this.setStatus({ status: "ready" });
9596
9598
  } catch (err) {
9597
- if (err.name === "AbortError") {
9599
+ if (isAbort || err.name === "AbortError") {
9600
+ isAbort = true;
9598
9601
  this.setStatus({ status: "ready" });
9599
9602
  return null;
9600
9603
  }
9604
+ isError = true;
9605
+ if (err instanceof TypeError && (err.message.toLowerCase().includes("fetch") || err.message.toLowerCase().includes("network"))) {
9606
+ isDisconnect = true;
9607
+ }
9601
9608
  if (this.onError && err instanceof Error) {
9602
9609
  this.onError(err);
9603
9610
  }
9604
9611
  this.setStatus({ status: "error", error: err });
9605
9612
  } finally {
9613
+ try {
9614
+ (_a17 = this.onFinish) == null ? void 0 : _a17.call(this, {
9615
+ message: this.activeResponse.state.message,
9616
+ messages: this.state.messages,
9617
+ isAbort,
9618
+ isDisconnect,
9619
+ isError
9620
+ });
9621
+ } catch (err) {
9622
+ console.error(err);
9623
+ }
9606
9624
  this.activeResponse = void 0;
9607
9625
  }
9608
9626
  if ((_b = this.sendAutomaticallyWhen) == null ? void 0 : _b.call(this, { messages: this.state.messages })) {