@ensembleapp/client-sdk 0.0.51 → 0.0.52

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
@@ -4217,6 +4217,7 @@ function createAuthAwareFetch(tokenRef, onAuthError) {
4217
4217
  return response;
4218
4218
  };
4219
4219
  }
4220
+ var EMPTY_HEADERS = {};
4220
4221
  function useChat({
4221
4222
  api,
4222
4223
  threadId,
@@ -4231,8 +4232,13 @@ function useChat({
4231
4232
  onMessage,
4232
4233
  onUsage
4233
4234
  }) {
4234
- const { baseUrl, token, headers: customHeaders = {} } = api;
4235
- const customHeadersKey = JSON.stringify(customHeaders);
4235
+ const { baseUrl, token, headers } = api;
4236
+ const customHeadersKey = JSON.stringify(headers ?? {});
4237
+ const customHeaders = useMemo(
4238
+ () => headers ?? EMPTY_HEADERS,
4239
+ // eslint-disable-next-line react-hooks/exhaustive-deps
4240
+ [customHeadersKey]
4241
+ );
4236
4242
  const dataContextRef = useRef(dataContext);
4237
4243
  dataContextRef.current = dataContext;
4238
4244
  const tokenRef = useRef(token);
@@ -4289,8 +4295,11 @@ function useChat({
4289
4295
  },
4290
4296
  onData: (dataPart) => {
4291
4297
  onData?.(dataPart);
4292
- if (dataPart && typeof dataPart === "object" && "inputTokens" in dataPart) {
4293
- onUsage?.(dataPart);
4298
+ if (dataPart && typeof dataPart === "object") {
4299
+ const part = dataPart;
4300
+ if (part.type === "data-usage" && part.data) {
4301
+ onUsage?.(part.data);
4302
+ }
4294
4303
  }
4295
4304
  }
4296
4305
  });
@@ -4311,8 +4320,14 @@ function useChat({
4311
4320
  [sendMessage]
4312
4321
  );
4313
4322
  const [isLoadingInitial, setIsLoadingInitial] = useState(true);
4323
+ const hasFetchedRef = useRef(false);
4314
4324
  useEffect(() => {
4325
+ hasFetchedRef.current = false;
4326
+ }, [threadId]);
4327
+ useEffect(() => {
4328
+ if (hasFetchedRef.current) return;
4315
4329
  const fetchInitialMessages = async () => {
4330
+ hasFetchedRef.current = true;
4316
4331
  try {
4317
4332
  const response = await authFetch(`${baseUrl}/chat/messages`, {
4318
4333
  method: "GET",
@@ -4331,6 +4346,7 @@ function useChat({
4331
4346
  }
4332
4347
  } catch (error) {
4333
4348
  console.error("Error fetching initial messages:", error);
4349
+ hasFetchedRef.current = false;
4334
4350
  onError?.(error);
4335
4351
  } finally {
4336
4352
  setIsLoadingInitial(false);