@athenaintel/react 0.4.1 → 0.4.3

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.cjs CHANGED
@@ -7640,7 +7640,7 @@ const toAppendMessage = (messages, message) => {
7640
7640
  startRun: message.startRun
7641
7641
  };
7642
7642
  };
7643
- const getThreadState = (runtime, threadListItemState) => {
7643
+ const getThreadState$1 = (runtime, threadListItemState) => {
7644
7644
  const lastMessage = runtime.messages.at(-1);
7645
7645
  return Object.freeze({
7646
7646
  threadId: threadListItemState.id,
@@ -7663,7 +7663,7 @@ class ThreadRuntimeImpl {
7663
7663
  __publicField(this, "_eventSubscriptionSubjects", /* @__PURE__ */ new Map());
7664
7664
  const stateBinding = new ShallowMemoizeSubject({
7665
7665
  path: threadBinding.path,
7666
- getState: () => getThreadState(threadBinding.getState(), threadListItemBinding.getState()),
7666
+ getState: () => getThreadState$1(threadBinding.getState(), threadListItemBinding.getState()),
7667
7667
  subscribe: (callback) => {
7668
7668
  const sub1 = threadBinding.subscribe(callback);
7669
7669
  const sub2 = threadListItemBinding.subscribe(callback);
@@ -20776,7 +20776,8 @@ const useAthenaRuntime = (config2) => {
20776
20776
  workbench = [],
20777
20777
  knowledgeBase = [],
20778
20778
  systemPrompt,
20779
- threadId: threadIdProp
20779
+ threadId: threadIdProp,
20780
+ initialMessages
20780
20781
  } = config2;
20781
20782
  const generatedIdRef = React.useRef(null);
20782
20783
  if (generatedIdRef.current === null) {
@@ -20794,7 +20795,7 @@ const useAthenaRuntime = (config2) => {
20794
20795
  const apiKeyRef = React.useRef(apiKey);
20795
20796
  apiKeyRef.current = apiKey;
20796
20797
  const runtime = useAssistantTransportRuntime({
20797
- initialState: { messages: [] },
20798
+ initialState: { messages: initialMessages ?? [] },
20798
20799
  converter,
20799
20800
  api: apiUrl,
20800
20801
  headers: async () => {
@@ -24181,6 +24182,17 @@ async function listThreads(backendUrl, auth, opts = {}) {
24181
24182
  }
24182
24183
  return res.json();
24183
24184
  }
24185
+ async function getThreadState(backendUrl, auth, threadId) {
24186
+ const base2 = getAgoraBaseUrl(backendUrl);
24187
+ const res = await fetch(`${base2}/api/unstable/threads/${threadId}`, {
24188
+ method: "GET",
24189
+ headers: { ...getAuthHeaders(auth) }
24190
+ });
24191
+ if (!res.ok) {
24192
+ throw new Error(`[AthenaSDK] Failed to get thread state: ${res.status}`);
24193
+ }
24194
+ return res.json();
24195
+ }
24184
24196
  async function archiveThread(backendUrl, auth, threadId) {
24185
24197
  const base2 = getAgoraBaseUrl(backendUrl);
24186
24198
  const res = await fetch(`${base2}/api/conversations/threads/archive`, {
@@ -24212,6 +24224,9 @@ function createThreadListStore(config2) {
24212
24224
  }
24213
24225
  },
24214
24226
  switchThread: (threadId) => {
24227
+ if (process.env.NODE_ENV !== "production") {
24228
+ console.log("[AthenaThreads] switchThread called", { threadId, prev: get2().activeThreadId });
24229
+ }
24215
24230
  set2({ activeThreadId: threadId });
24216
24231
  },
24217
24232
  newThread: async () => {
@@ -24249,7 +24264,8 @@ function AthenaRuntimeInner({
24249
24264
  workbench,
24250
24265
  knowledgeBase,
24251
24266
  systemPrompt,
24252
- threadId
24267
+ threadId,
24268
+ initialMessages
24253
24269
  }) {
24254
24270
  const auiTools = React.useMemo(() => Tools({ toolkit: frontendTools }), [frontendTools]);
24255
24271
  const aui = useAui({ tools: auiTools });
@@ -24265,7 +24281,8 @@ function AthenaRuntimeInner({
24265
24281
  workbench,
24266
24282
  knowledgeBase,
24267
24283
  systemPrompt,
24268
- threadId
24284
+ threadId,
24285
+ initialMessages
24269
24286
  });
24270
24287
  const athenaConfig = React.useMemo(
24271
24288
  () => ({ backendUrl, apiKey, token }),
@@ -24273,6 +24290,66 @@ function AthenaRuntimeInner({
24273
24290
  );
24274
24291
  return /* @__PURE__ */ jsxRuntime.jsx(AssistantRuntimeProvider, { aui, runtime, children: /* @__PURE__ */ jsxRuntime.jsx(AthenaContext.Provider, { value: athenaConfig, children: /* @__PURE__ */ jsxRuntime.jsx(TooltipProvider, { children }) }) });
24275
24292
  }
24293
+ function ThreadStateLoader({
24294
+ children,
24295
+ threadId,
24296
+ backendUrl,
24297
+ apiKey,
24298
+ token,
24299
+ ...runtimeProps
24300
+ }) {
24301
+ const [initialMessages, setInitialMessages] = React.useState(void 0);
24302
+ const [isLoading, setIsLoading] = React.useState(!!threadId);
24303
+ React.useEffect(() => {
24304
+ if (!threadId) {
24305
+ setInitialMessages(void 0);
24306
+ setIsLoading(false);
24307
+ return;
24308
+ }
24309
+ let cancelled = false;
24310
+ setIsLoading(true);
24311
+ getThreadState(backendUrl, { apiKey, token }, threadId).then((state) => {
24312
+ var _a2;
24313
+ if (cancelled) return;
24314
+ if (process.env.NODE_ENV !== "production") {
24315
+ console.log("[AthenaThreads] Loaded thread state", {
24316
+ threadId,
24317
+ messageCount: ((_a2 = state.messages) == null ? void 0 : _a2.length) ?? 0
24318
+ });
24319
+ }
24320
+ setInitialMessages(state.messages ?? []);
24321
+ setIsLoading(false);
24322
+ }).catch((err) => {
24323
+ if (cancelled) return;
24324
+ if (process.env.NODE_ENV !== "production") {
24325
+ console.warn("[AthenaThreads] Failed to load thread state, starting fresh", err);
24326
+ }
24327
+ setInitialMessages(void 0);
24328
+ setIsLoading(false);
24329
+ });
24330
+ return () => {
24331
+ cancelled = true;
24332
+ };
24333
+ }, [threadId, backendUrl, apiKey, token]);
24334
+ if (isLoading) {
24335
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "100%", width: "100%" }, children: [
24336
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { width: 24, height: 24, border: "2px solid currentColor", borderTopColor: "transparent", borderRadius: "50%", animation: "spin 0.6s linear infinite", opacity: 0.4 } }),
24337
+ /* @__PURE__ */ jsxRuntime.jsx("style", { children: `@keyframes spin { to { transform: rotate(360deg); } }` })
24338
+ ] });
24339
+ }
24340
+ return /* @__PURE__ */ jsxRuntime.jsx(
24341
+ AthenaRuntimeInner,
24342
+ {
24343
+ threadId,
24344
+ backendUrl,
24345
+ apiKey,
24346
+ token,
24347
+ initialMessages,
24348
+ ...runtimeProps,
24349
+ children
24350
+ }
24351
+ );
24352
+ }
24276
24353
  function useActiveThreadFromStore(store) {
24277
24354
  return React.useSyncExternalStore(
24278
24355
  (cb) => {
@@ -24304,6 +24381,17 @@ function AthenaProvider({
24304
24381
  const parentAuthToken = useParentAuth();
24305
24382
  const effectiveToken = tokenProp ?? parentAuthToken;
24306
24383
  const effectiveBackendUrl = backendUrl ?? DEFAULT_BACKEND_URL;
24384
+ React.useEffect(() => {
24385
+ if (process.env.NODE_ENV !== "production") {
24386
+ console.log("[AthenaAuth] AthenaProvider auth state", {
24387
+ hasTokenProp: !!tokenProp,
24388
+ hasParentAuthToken: !!parentAuthToken,
24389
+ hasEffectiveToken: !!effectiveToken,
24390
+ hasApiKey: !!apiKey,
24391
+ authMethod: effectiveToken ? "Bearer token (PropelAuth)" : apiKey ? "X-API-KEY" : "NONE"
24392
+ });
24393
+ }
24394
+ }, [tokenProp, parentAuthToken, effectiveToken, apiKey]);
24307
24395
  const threadListStoreRef = React.useRef(null);
24308
24396
  if (enableThreadList && !threadListStoreRef.current) {
24309
24397
  threadListStoreRef.current = createThreadListStore({
@@ -24316,8 +24404,18 @@ function AthenaProvider({
24316
24404
  enableThreadList ? threadListStoreRef.current : null
24317
24405
  );
24318
24406
  const resolvedThreadId = threadIdProp ?? activeThreadId ?? void 0;
24407
+ React.useEffect(() => {
24408
+ if (process.env.NODE_ENV !== "production") {
24409
+ console.log("[AthenaThreads] AthenaProvider render", {
24410
+ activeThreadId,
24411
+ resolvedThreadId,
24412
+ enableThreadList,
24413
+ hasStore: !!threadListStoreRef.current
24414
+ });
24415
+ }
24416
+ }, [activeThreadId, resolvedThreadId, enableThreadList]);
24319
24417
  const inner = /* @__PURE__ */ jsxRuntime.jsx(
24320
- AthenaRuntimeInner,
24418
+ ThreadStateLoader,
24321
24419
  {
24322
24420
  apiUrl,
24323
24421
  backendUrl: effectiveBackendUrl,