@ai-sdk/react
Advanced tools
+23
-2
@@ -170,2 +170,11 @@ var __accessCheck = (obj, member, msg) => { | ||
| // src/chat.react.ts | ||
| function cloneMetadata(metadata) { | ||
| if (Array.isArray(metadata)) { | ||
| return [...metadata]; | ||
| } | ||
| if (metadata != null && typeof metadata === "object" && (Object.getPrototypeOf(metadata) === Object.prototype || Object.getPrototypeOf(metadata) === null)) { | ||
| return { ...metadata }; | ||
| } | ||
| return metadata; | ||
| } | ||
| var _messages, _status, _error, _messagesCallbacks, _statusCallbacks, _errorCallbacks, _callMessagesCallbacks, _callStatusCallbacks, _callErrorCallbacks; | ||
@@ -191,3 +200,2 @@ var ReactChatState = class { | ||
| ...__privateGet(this, _messages).slice(0, index), | ||
| // We deep clone the message here to ensure the new React Compiler (currently in RC) detects deeply nested parts/metadata changes: | ||
| this.snapshot(message), | ||
@@ -198,3 +206,16 @@ ...__privateGet(this, _messages).slice(index + 1) | ||
| }; | ||
| this.snapshot = (value) => structuredClone(value); | ||
| this.snapshot = (value) => { | ||
| if (value == null || typeof value !== "object" || !("parts" in value) || !Array.isArray(value.parts)) { | ||
| return value; | ||
| } | ||
| const message = value; | ||
| const snapshot = { | ||
| ...message, | ||
| parts: message.parts.map((part) => ({ ...part })) | ||
| }; | ||
| if ("metadata" in message) { | ||
| snapshot.metadata = cloneMetadata(message.metadata); | ||
| } | ||
| return snapshot; | ||
| }; | ||
| this["~registerMessagesCallback"] = (onChange, throttleWaitMs) => { | ||
@@ -201,0 +222,0 @@ const callback = throttleWaitMs ? throttle(onChange, throttleWaitMs) : onChange; |
+3
-3
| { | ||
| "name": "@ai-sdk/react", | ||
| "version": "4.0.66", | ||
| "version": "4.0.67", | ||
| "type": "module", | ||
@@ -31,6 +31,6 @@ "license": "Apache-2.0", | ||
| "throttleit": "2.1.0", | ||
| "@ai-sdk/mcp": "2.0.31", | ||
| "@ai-sdk/provider": "4.0.7", | ||
| "@ai-sdk/mcp": "2.0.31", | ||
| "@ai-sdk/provider-utils": "5.0.27", | ||
| "ai": "7.0.63" | ||
| "ai": "7.0.64" | ||
| }, | ||
@@ -37,0 +37,0 @@ "devDependencies": { |
+39
-2
@@ -10,2 +10,19 @@ import { | ||
| function cloneMetadata<METADATA>(metadata: METADATA): METADATA { | ||
| if (Array.isArray(metadata)) { | ||
| return [...metadata] as METADATA; | ||
| } | ||
| if ( | ||
| metadata != null && | ||
| typeof metadata === 'object' && | ||
| (Object.getPrototypeOf(metadata) === Object.prototype || | ||
| Object.getPrototypeOf(metadata) === null) | ||
| ) { | ||
| return { ...metadata } as METADATA; | ||
| } | ||
| return metadata; | ||
| } | ||
| class ReactChatState< | ||
@@ -66,3 +83,2 @@ UI_MESSAGE extends UIMessage, | ||
| ...this.#messages.slice(0, index), | ||
| // We deep clone the message here to ensure the new React Compiler (currently in RC) detects deeply nested parts/metadata changes: | ||
| this.snapshot(message), | ||
@@ -74,4 +90,25 @@ ...this.#messages.slice(index + 1), | ||
| snapshot = <T>(value: T): T => structuredClone(value); | ||
| snapshot = <T>(value: T): T => { | ||
| if ( | ||
| value == null || | ||
| typeof value !== 'object' || | ||
| !('parts' in value) || | ||
| !Array.isArray(value.parts) | ||
| ) { | ||
| return value; | ||
| } | ||
| const message = value as UI_MESSAGE; | ||
| const snapshot = { | ||
| ...message, | ||
| parts: message.parts.map(part => ({ ...part })), | ||
| }; | ||
| if ('metadata' in message) { | ||
| snapshot.metadata = cloneMetadata(message.metadata); | ||
| } | ||
| return snapshot as T; | ||
| }; | ||
| '~registerMessagesCallback' = ( | ||
@@ -78,0 +115,0 @@ onChange: () => void, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
308065
0.85%3809
1.38%+ Added
+ Added
- Removed
- Removed
Updated