@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.d.ts CHANGED
@@ -6,6 +6,7 @@ import { ComponentPropsWithRef } from 'react';
6
6
  import { FC } from 'react';
7
7
  import { ForwardRefExoticComponent } from 'react';
8
8
  import { JSX } from 'react/jsx-runtime';
9
+ import { LangChainMessage } from '@assistant-ui/react-langgraph';
9
10
  import * as React_2 from 'react';
10
11
  import { ReactNode } from 'react';
11
12
  import { RefAttributes } from 'react';
@@ -152,6 +153,8 @@ export declare interface AthenaRuntimeConfig {
152
153
  systemPrompt?: string;
153
154
  /** Thread ID override. Auto-generated if not provided. */
154
155
  threadId?: string;
156
+ /** Pre-loaded messages for existing threads (from thread switching). */
157
+ initialMessages?: LangChainMessage[];
155
158
  }
156
159
 
157
160
  export declare const BrowseToolUI: ToolCallMessagePartComponent;
package/dist/index.js CHANGED
@@ -7622,7 +7622,7 @@ const toAppendMessage = (messages, message) => {
7622
7622
  startRun: message.startRun
7623
7623
  };
7624
7624
  };
7625
- const getThreadState = (runtime, threadListItemState) => {
7625
+ const getThreadState$1 = (runtime, threadListItemState) => {
7626
7626
  const lastMessage = runtime.messages.at(-1);
7627
7627
  return Object.freeze({
7628
7628
  threadId: threadListItemState.id,
@@ -7645,7 +7645,7 @@ class ThreadRuntimeImpl {
7645
7645
  __publicField(this, "_eventSubscriptionSubjects", /* @__PURE__ */ new Map());
7646
7646
  const stateBinding = new ShallowMemoizeSubject({
7647
7647
  path: threadBinding.path,
7648
- getState: () => getThreadState(threadBinding.getState(), threadListItemBinding.getState()),
7648
+ getState: () => getThreadState$1(threadBinding.getState(), threadListItemBinding.getState()),
7649
7649
  subscribe: (callback) => {
7650
7650
  const sub1 = threadBinding.subscribe(callback);
7651
7651
  const sub2 = threadListItemBinding.subscribe(callback);
@@ -20758,7 +20758,8 @@ const useAthenaRuntime = (config2) => {
20758
20758
  workbench = [],
20759
20759
  knowledgeBase = [],
20760
20760
  systemPrompt,
20761
- threadId: threadIdProp
20761
+ threadId: threadIdProp,
20762
+ initialMessages
20762
20763
  } = config2;
20763
20764
  const generatedIdRef = useRef(null);
20764
20765
  if (generatedIdRef.current === null) {
@@ -20776,7 +20777,7 @@ const useAthenaRuntime = (config2) => {
20776
20777
  const apiKeyRef = useRef(apiKey);
20777
20778
  apiKeyRef.current = apiKey;
20778
20779
  const runtime = useAssistantTransportRuntime({
20779
- initialState: { messages: [] },
20780
+ initialState: { messages: initialMessages ?? [] },
20780
20781
  converter,
20781
20782
  api: apiUrl,
20782
20783
  headers: async () => {
@@ -24163,6 +24164,36 @@ async function listThreads(backendUrl, auth, opts = {}) {
24163
24164
  }
24164
24165
  return res.json();
24165
24166
  }
24167
+ function deserializeMessage(msg) {
24168
+ if (msg && typeof msg === "object" && "lc" in msg && "kwargs" in msg && msg.type === "constructor") {
24169
+ const kwargs = msg.kwargs;
24170
+ if (Array.isArray(kwargs.tool_calls)) {
24171
+ kwargs.tool_calls = kwargs.tool_calls.map((tc) => {
24172
+ if (tc && typeof tc === "object" && "lc" in tc && "kwargs" in tc) {
24173
+ return tc.kwargs;
24174
+ }
24175
+ return tc;
24176
+ });
24177
+ }
24178
+ return kwargs;
24179
+ }
24180
+ return msg;
24181
+ }
24182
+ async function getThreadState(backendUrl, auth, threadId) {
24183
+ const base2 = getAgoraBaseUrl(backendUrl);
24184
+ const res = await fetch(`${base2}/api/unstable/threads/${threadId}`, {
24185
+ method: "GET",
24186
+ headers: { ...getAuthHeaders(auth) }
24187
+ });
24188
+ if (!res.ok) {
24189
+ throw new Error(`[AthenaSDK] Failed to get thread state: ${res.status}`);
24190
+ }
24191
+ const data = await res.json();
24192
+ if (Array.isArray(data.messages)) {
24193
+ data.messages = data.messages.map(deserializeMessage);
24194
+ }
24195
+ return data;
24196
+ }
24166
24197
  async function archiveThread(backendUrl, auth, threadId) {
24167
24198
  const base2 = getAgoraBaseUrl(backendUrl);
24168
24199
  const res = await fetch(`${base2}/api/conversations/threads/archive`, {
@@ -24234,7 +24265,8 @@ function AthenaRuntimeInner({
24234
24265
  workbench,
24235
24266
  knowledgeBase,
24236
24267
  systemPrompt,
24237
- threadId
24268
+ threadId,
24269
+ initialMessages
24238
24270
  }) {
24239
24271
  const auiTools = useMemo(() => Tools({ toolkit: frontendTools }), [frontendTools]);
24240
24272
  const aui = useAui({ tools: auiTools });
@@ -24250,7 +24282,8 @@ function AthenaRuntimeInner({
24250
24282
  workbench,
24251
24283
  knowledgeBase,
24252
24284
  systemPrompt,
24253
- threadId
24285
+ threadId,
24286
+ initialMessages
24254
24287
  });
24255
24288
  const athenaConfig = useMemo(
24256
24289
  () => ({ backendUrl, apiKey, token }),
@@ -24258,6 +24291,66 @@ function AthenaRuntimeInner({
24258
24291
  );
24259
24292
  return /* @__PURE__ */ jsx(AssistantRuntimeProvider, { aui, runtime, children: /* @__PURE__ */ jsx(AthenaContext.Provider, { value: athenaConfig, children: /* @__PURE__ */ jsx(TooltipProvider, { children }) }) });
24260
24293
  }
24294
+ function ThreadStateLoader({
24295
+ children,
24296
+ threadId,
24297
+ backendUrl,
24298
+ apiKey,
24299
+ token,
24300
+ ...runtimeProps
24301
+ }) {
24302
+ const [initialMessages, setInitialMessages] = useState(void 0);
24303
+ const [isLoading, setIsLoading] = useState(!!threadId);
24304
+ useEffect(() => {
24305
+ if (!threadId) {
24306
+ setInitialMessages(void 0);
24307
+ setIsLoading(false);
24308
+ return;
24309
+ }
24310
+ let cancelled = false;
24311
+ setIsLoading(true);
24312
+ getThreadState(backendUrl, { apiKey, token }, threadId).then((state) => {
24313
+ var _a2;
24314
+ if (cancelled) return;
24315
+ if (process.env.NODE_ENV !== "production") {
24316
+ console.log("[AthenaThreads] Loaded thread state", {
24317
+ threadId,
24318
+ messageCount: ((_a2 = state.messages) == null ? void 0 : _a2.length) ?? 0
24319
+ });
24320
+ }
24321
+ setInitialMessages(state.messages ?? []);
24322
+ setIsLoading(false);
24323
+ }).catch((err) => {
24324
+ if (cancelled) return;
24325
+ if (process.env.NODE_ENV !== "production") {
24326
+ console.warn("[AthenaThreads] Failed to load thread state, starting fresh", err);
24327
+ }
24328
+ setInitialMessages(void 0);
24329
+ setIsLoading(false);
24330
+ });
24331
+ return () => {
24332
+ cancelled = true;
24333
+ };
24334
+ }, [threadId, backendUrl, apiKey, token]);
24335
+ if (isLoading) {
24336
+ return /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "100%", width: "100%" }, children: [
24337
+ /* @__PURE__ */ jsx("div", { style: { width: 24, height: 24, border: "2px solid currentColor", borderTopColor: "transparent", borderRadius: "50%", animation: "spin 0.6s linear infinite", opacity: 0.4 } }),
24338
+ /* @__PURE__ */ jsx("style", { children: `@keyframes spin { to { transform: rotate(360deg); } }` })
24339
+ ] });
24340
+ }
24341
+ return /* @__PURE__ */ jsx(
24342
+ AthenaRuntimeInner,
24343
+ {
24344
+ threadId,
24345
+ backendUrl,
24346
+ apiKey,
24347
+ token,
24348
+ initialMessages,
24349
+ ...runtimeProps,
24350
+ children
24351
+ }
24352
+ );
24353
+ }
24261
24354
  function useActiveThreadFromStore(store) {
24262
24355
  return useSyncExternalStore(
24263
24356
  (cb) => {
@@ -24323,7 +24416,7 @@ function AthenaProvider({
24323
24416
  }
24324
24417
  }, [activeThreadId, resolvedThreadId, enableThreadList]);
24325
24418
  const inner = /* @__PURE__ */ jsx(
24326
- AthenaRuntimeInner,
24419
+ ThreadStateLoader,
24327
24420
  {
24328
24421
  apiUrl,
24329
24422
  backendUrl: effectiveBackendUrl,