@athenaintel/react 0.4.2 → 0.4.4

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,36 @@ async function listThreads(backendUrl, auth, opts = {}) {
24181
24182
  }
24182
24183
  return res.json();
24183
24184
  }
24185
+ function deserializeMessage(msg) {
24186
+ if (msg && typeof msg === "object" && "lc" in msg && "kwargs" in msg && msg.type === "constructor") {
24187
+ const kwargs = msg.kwargs;
24188
+ if (Array.isArray(kwargs.tool_calls)) {
24189
+ kwargs.tool_calls = kwargs.tool_calls.map((tc) => {
24190
+ if (tc && typeof tc === "object" && "lc" in tc && "kwargs" in tc) {
24191
+ return tc.kwargs;
24192
+ }
24193
+ return tc;
24194
+ });
24195
+ }
24196
+ return kwargs;
24197
+ }
24198
+ return msg;
24199
+ }
24200
+ async function getThreadState(backendUrl, auth, threadId) {
24201
+ const base2 = getAgoraBaseUrl(backendUrl);
24202
+ const res = await fetch(`${base2}/api/unstable/threads/${threadId}`, {
24203
+ method: "GET",
24204
+ headers: { ...getAuthHeaders(auth) }
24205
+ });
24206
+ if (!res.ok) {
24207
+ throw new Error(`[AthenaSDK] Failed to get thread state: ${res.status}`);
24208
+ }
24209
+ const data = await res.json();
24210
+ if (Array.isArray(data.messages)) {
24211
+ data.messages = data.messages.map(deserializeMessage);
24212
+ }
24213
+ return data;
24214
+ }
24184
24215
  async function archiveThread(backendUrl, auth, threadId) {
24185
24216
  const base2 = getAgoraBaseUrl(backendUrl);
24186
24217
  const res = await fetch(`${base2}/api/conversations/threads/archive`, {
@@ -24252,7 +24283,8 @@ function AthenaRuntimeInner({
24252
24283
  workbench,
24253
24284
  knowledgeBase,
24254
24285
  systemPrompt,
24255
- threadId
24286
+ threadId,
24287
+ initialMessages
24256
24288
  }) {
24257
24289
  const auiTools = React.useMemo(() => Tools({ toolkit: frontendTools }), [frontendTools]);
24258
24290
  const aui = useAui({ tools: auiTools });
@@ -24268,7 +24300,8 @@ function AthenaRuntimeInner({
24268
24300
  workbench,
24269
24301
  knowledgeBase,
24270
24302
  systemPrompt,
24271
- threadId
24303
+ threadId,
24304
+ initialMessages
24272
24305
  });
24273
24306
  const athenaConfig = React.useMemo(
24274
24307
  () => ({ backendUrl, apiKey, token }),
@@ -24276,6 +24309,66 @@ function AthenaRuntimeInner({
24276
24309
  );
24277
24310
  return /* @__PURE__ */ jsxRuntime.jsx(AssistantRuntimeProvider, { aui, runtime, children: /* @__PURE__ */ jsxRuntime.jsx(AthenaContext.Provider, { value: athenaConfig, children: /* @__PURE__ */ jsxRuntime.jsx(TooltipProvider, { children }) }) });
24278
24311
  }
24312
+ function ThreadStateLoader({
24313
+ children,
24314
+ threadId,
24315
+ backendUrl,
24316
+ apiKey,
24317
+ token,
24318
+ ...runtimeProps
24319
+ }) {
24320
+ const [initialMessages, setInitialMessages] = React.useState(void 0);
24321
+ const [isLoading, setIsLoading] = React.useState(!!threadId);
24322
+ React.useEffect(() => {
24323
+ if (!threadId) {
24324
+ setInitialMessages(void 0);
24325
+ setIsLoading(false);
24326
+ return;
24327
+ }
24328
+ let cancelled = false;
24329
+ setIsLoading(true);
24330
+ getThreadState(backendUrl, { apiKey, token }, threadId).then((state) => {
24331
+ var _a2;
24332
+ if (cancelled) return;
24333
+ if (process.env.NODE_ENV !== "production") {
24334
+ console.log("[AthenaThreads] Loaded thread state", {
24335
+ threadId,
24336
+ messageCount: ((_a2 = state.messages) == null ? void 0 : _a2.length) ?? 0
24337
+ });
24338
+ }
24339
+ setInitialMessages(state.messages ?? []);
24340
+ setIsLoading(false);
24341
+ }).catch((err) => {
24342
+ if (cancelled) return;
24343
+ if (process.env.NODE_ENV !== "production") {
24344
+ console.warn("[AthenaThreads] Failed to load thread state, starting fresh", err);
24345
+ }
24346
+ setInitialMessages(void 0);
24347
+ setIsLoading(false);
24348
+ });
24349
+ return () => {
24350
+ cancelled = true;
24351
+ };
24352
+ }, [threadId, backendUrl, apiKey, token]);
24353
+ if (isLoading) {
24354
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "100%", width: "100%" }, children: [
24355
+ /* @__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 } }),
24356
+ /* @__PURE__ */ jsxRuntime.jsx("style", { children: `@keyframes spin { to { transform: rotate(360deg); } }` })
24357
+ ] });
24358
+ }
24359
+ return /* @__PURE__ */ jsxRuntime.jsx(
24360
+ AthenaRuntimeInner,
24361
+ {
24362
+ threadId,
24363
+ backendUrl,
24364
+ apiKey,
24365
+ token,
24366
+ initialMessages,
24367
+ ...runtimeProps,
24368
+ children
24369
+ }
24370
+ );
24371
+ }
24279
24372
  function useActiveThreadFromStore(store) {
24280
24373
  return React.useSyncExternalStore(
24281
24374
  (cb) => {
@@ -24341,7 +24434,7 @@ function AthenaProvider({
24341
24434
  }
24342
24435
  }, [activeThreadId, resolvedThreadId, enableThreadList]);
24343
24436
  const inner = /* @__PURE__ */ jsxRuntime.jsx(
24344
- AthenaRuntimeInner,
24437
+ ThreadStateLoader,
24345
24438
  {
24346
24439
  apiUrl,
24347
24440
  backendUrl: effectiveBackendUrl,