@ensembleapp/client-sdk 0.0.50 → 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,7 +4232,13 @@ function useChat({
4231
4232
  onMessage,
4232
4233
  onUsage
4233
4234
  }) {
4234
- const { baseUrl, token, headers: customHeaders = {} } = api;
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
+ );
4235
4242
  const dataContextRef = useRef(dataContext);
4236
4243
  dataContextRef.current = dataContext;
4237
4244
  const tokenRef = useRef(token);
@@ -4288,8 +4295,11 @@ function useChat({
4288
4295
  },
4289
4296
  onData: (dataPart) => {
4290
4297
  onData?.(dataPart);
4291
- if (dataPart && typeof dataPart === "object" && "inputTokens" in dataPart) {
4292
- 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
+ }
4293
4303
  }
4294
4304
  }
4295
4305
  });
@@ -4310,8 +4320,14 @@ function useChat({
4310
4320
  [sendMessage]
4311
4321
  );
4312
4322
  const [isLoadingInitial, setIsLoadingInitial] = useState(true);
4323
+ const hasFetchedRef = useRef(false);
4313
4324
  useEffect(() => {
4325
+ hasFetchedRef.current = false;
4326
+ }, [threadId]);
4327
+ useEffect(() => {
4328
+ if (hasFetchedRef.current) return;
4314
4329
  const fetchInitialMessages = async () => {
4330
+ hasFetchedRef.current = true;
4315
4331
  try {
4316
4332
  const response = await authFetch(`${baseUrl}/chat/messages`, {
4317
4333
  method: "GET",
@@ -4330,13 +4346,14 @@ function useChat({
4330
4346
  }
4331
4347
  } catch (error) {
4332
4348
  console.error("Error fetching initial messages:", error);
4349
+ hasFetchedRef.current = false;
4333
4350
  onError?.(error);
4334
4351
  } finally {
4335
4352
  setIsLoadingInitial(false);
4336
4353
  }
4337
4354
  };
4338
4355
  fetchInitialMessages();
4339
- }, [agentExecutionId, agentId, authFetch, baseUrl, customHeaders, onError, setMessages, threadId]);
4356
+ }, [agentExecutionId, agentId, authFetch, baseUrl, customHeadersKey, threadId]);
4340
4357
  useEffect(() => {
4341
4358
  if (!onMessage || messages.length === 0) return;
4342
4359
  const lastMessage = messages[messages.length - 1];