@axflow/models
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -67,2 +67,12 @@ import { MessageType, JSONValueType } from '@axflow/models/shared'; | ||
onMessagesChange?: (updatedMessages: MessageType[]) => void; | ||
/** | ||
* Callback that is invoked when a new message is appended to the list of messages. | ||
* | ||
* NOTE: For messages received from the server, this will only be invoked ONE time, when | ||
* the message is complete. That means, for streaming responses, this will not be invoked | ||
* until the stream has finished and the message is complete. If you want to get notified | ||
* for each update, the `onMessagesChange` callback fires every time the list of messages | ||
* changes, which includes message updates from streaming responses. | ||
*/ | ||
onNewMessage?: (message: MessageType) => void; | ||
}; | ||
@@ -69,0 +79,0 @@ /** |
@@ -33,3 +33,3 @@ "use strict"; | ||
} | ||
async function handleStreamingResponse(response, messagesRef, setMessages, accessor) { | ||
async function handleStreamingResponse(response, messagesRef, setMessages, accessor, onNewMessage) { | ||
const responseBody = response.body; | ||
@@ -78,7 +78,9 @@ if (responseBody === null) { | ||
} | ||
const newMessage = messagesRef.current.find((msg) => msg.id === id); | ||
onNewMessage(newMessage); | ||
} | ||
async function handleJsonResponse(response, messagesRef, setMessages, accessor) { | ||
async function handleJsonResponse(response, messagesRef, setMessages, accessor, onNewMessage) { | ||
const responseBody = await response.json(); | ||
const content = accessor(responseBody); | ||
const messages = messagesRef.current.concat({ | ||
const newMessage = { | ||
id: uuid(), | ||
@@ -88,6 +90,8 @@ role: "assistant", | ||
created: Date.now() | ||
}); | ||
}; | ||
const messages = messagesRef.current.concat(newMessage); | ||
setMessages(messages); | ||
onNewMessage(newMessage); | ||
} | ||
async function stableAppend(message, messagesRef, setMessages, url, headers, body, accessor, loadingRef, setLoading, setError, onError) { | ||
async function stableAppend(message, messagesRef, setMessages, url, headers, body, accessor, loadingRef, setLoading, setError, onError, onNewMessage) { | ||
if (loadingRef.current) { | ||
@@ -100,3 +104,4 @@ return; | ||
const requestBody = typeof body === "function" ? body(message, history) : { ...body, messages: history.concat(message) }; | ||
setMessages(messagesRef.current.concat(message)); | ||
setMessages(history.concat(message)); | ||
onNewMessage(message); | ||
let response; | ||
@@ -111,3 +116,3 @@ try { | ||
const handler = isStreaming ? handleStreamingResponse : handleJsonResponse; | ||
await handler(response, messagesRef, setMessages, accessor); | ||
await handler(response, messagesRef, setMessages, accessor, onNewMessage); | ||
} catch (error) { | ||
@@ -131,2 +136,4 @@ setError(error); | ||
}; | ||
var DEFAULT_ON_NEW_MESSAGE = (_message) => { | ||
}; | ||
function useChat(options) { | ||
@@ -148,2 +155,3 @@ options ??= {}; | ||
const onMessagesChange = options.onMessagesChange ?? DEFAULT_ON_MESSAGES_CHANGE; | ||
const onNewMessage = options.onNewMessage ?? DEFAULT_ON_NEW_MESSAGE; | ||
const setMessages = (0, import_react.useCallback)( | ||
@@ -176,3 +184,4 @@ (messages2) => { | ||
setError, | ||
onError | ||
onError, | ||
onNewMessage | ||
); | ||
@@ -179,0 +188,0 @@ } |
@@ -17,3 +17,3 @@ declare class HttpError extends Error { | ||
id: string; | ||
role: 'user' | 'assistant'; | ||
role: 'user' | 'assistant' | 'system'; | ||
data?: JSONValueType[]; | ||
@@ -20,0 +20,0 @@ content: string; |
{ | ||
"name": "@axflow/models", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "Zero-dependency, modular SDK for building robust natural language applications", | ||
@@ -199,3 +199,3 @@ "author": "Axflow (https://axflow.dev)", | ||
}, | ||
"gitHead": "4305adbfd2e4ef5c4158d07326626e8c871b0b72" | ||
"gitHead": "4a4b2880e4d83ebed393e1cd1cc90ff1ed40a44e" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
223000
4198