@assistant-ui/react 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -82,6 +82,12 @@ type ChatModelAdapter = {
82
82
 
83
83
  declare abstract class BaseAssistantRuntime<TThreadRuntime extends ReactThreadRuntime> implements AssistantRuntime {
84
84
  private _thread;
85
+ get capabilities(): Readonly<{
86
+ edit: boolean;
87
+ reload: boolean;
88
+ cancel: boolean;
89
+ copy: boolean;
90
+ }>;
85
91
  constructor(_thread: TThreadRuntime);
86
92
  private _unsubscribe;
87
93
  get thread(): TThreadRuntime;
@@ -114,7 +120,13 @@ declare class LocalThreadRuntime implements ThreadRuntime {
114
120
  adapter: ChatModelAdapter;
115
121
  private _subscriptions;
116
122
  private abortController;
117
- private repository;
123
+ private readonly repository;
124
+ readonly capabilities: Readonly<{
125
+ edit: true;
126
+ reload: true;
127
+ cancel: true;
128
+ copy: true;
129
+ }>;
118
130
  get messages(): ThreadMessage[];
119
131
  get isRunning(): boolean;
120
132
  constructor(_configProviders: Set<ModelConfigProvider>, adapter: ChatModelAdapter);
@@ -131,6 +143,12 @@ declare class LocalThreadRuntime implements ThreadRuntime {
131
143
  declare const useLocalRuntime: (adapter: ChatModelAdapter) => LocalRuntime;
132
144
 
133
145
  type ThreadActionsState = Readonly<{
146
+ capabilities: Readonly<{
147
+ edit: boolean;
148
+ reload: boolean;
149
+ cancel: boolean;
150
+ copy: boolean;
151
+ }>;
134
152
  getBranches: (messageId: string) => readonly string[];
135
153
  switchToBranch: (branchId: string) => void;
136
154
  append: (message: AppendMessage) => void;
@@ -202,10 +220,11 @@ type BaseComposerState = Readonly<{
202
220
  type ReadonlyStore<T> = UseBoundStore<Omit<StoreApi<T>, "setState" | "destroy">>;
203
221
 
204
222
  type EditComposerState = BaseComposerState & Readonly<{
223
+ canCancel: boolean;
205
224
  isEditing: boolean;
206
225
  edit: () => void;
207
226
  send: () => void;
208
- cancel: () => boolean;
227
+ cancel: () => void;
209
228
  }>;
210
229
 
211
230
  type ThreadState = Readonly<{
@@ -271,9 +290,10 @@ type ContentPartContextValue = {
271
290
  declare const useContentPartContext: () => ContentPartContextValue;
272
291
 
273
292
  type ComposerState = BaseComposerState & Readonly<{
293
+ canCancel: boolean;
274
294
  isEditing: true;
275
295
  send: () => void;
276
- cancel: () => boolean;
296
+ cancel: () => void;
277
297
  focus: () => void;
278
298
  onFocus: (listener: () => void) => Unsubscribe;
279
299
  }>;
@@ -605,7 +625,7 @@ type ThreadPrimitiveMessagesProps = {
605
625
  AssistantMessage: ComponentType;
606
626
  };
607
627
  };
608
- declare const ThreadPrimitiveMessages: FC<ThreadPrimitiveMessagesProps>;
628
+ declare const ThreadPrimitiveMessages: react.NamedExoticComponent<ThreadPrimitiveMessagesProps>;
609
629
 
610
630
  type ThreadPrimitiveScrollToBottomProps = ActionButtonProps<typeof useThreadScrollToBottom>;
611
631
  declare const ThreadPrimitiveScrollToBottom: react.ForwardRefExoticComponent<Omit<Omit<react.DetailedHTMLProps<react.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "ref"> & {
package/dist/index.d.ts CHANGED
@@ -82,6 +82,12 @@ type ChatModelAdapter = {
82
82
 
83
83
  declare abstract class BaseAssistantRuntime<TThreadRuntime extends ReactThreadRuntime> implements AssistantRuntime {
84
84
  private _thread;
85
+ get capabilities(): Readonly<{
86
+ edit: boolean;
87
+ reload: boolean;
88
+ cancel: boolean;
89
+ copy: boolean;
90
+ }>;
85
91
  constructor(_thread: TThreadRuntime);
86
92
  private _unsubscribe;
87
93
  get thread(): TThreadRuntime;
@@ -114,7 +120,13 @@ declare class LocalThreadRuntime implements ThreadRuntime {
114
120
  adapter: ChatModelAdapter;
115
121
  private _subscriptions;
116
122
  private abortController;
117
- private repository;
123
+ private readonly repository;
124
+ readonly capabilities: Readonly<{
125
+ edit: true;
126
+ reload: true;
127
+ cancel: true;
128
+ copy: true;
129
+ }>;
118
130
  get messages(): ThreadMessage[];
119
131
  get isRunning(): boolean;
120
132
  constructor(_configProviders: Set<ModelConfigProvider>, adapter: ChatModelAdapter);
@@ -131,6 +143,12 @@ declare class LocalThreadRuntime implements ThreadRuntime {
131
143
  declare const useLocalRuntime: (adapter: ChatModelAdapter) => LocalRuntime;
132
144
 
133
145
  type ThreadActionsState = Readonly<{
146
+ capabilities: Readonly<{
147
+ edit: boolean;
148
+ reload: boolean;
149
+ cancel: boolean;
150
+ copy: boolean;
151
+ }>;
134
152
  getBranches: (messageId: string) => readonly string[];
135
153
  switchToBranch: (branchId: string) => void;
136
154
  append: (message: AppendMessage) => void;
@@ -202,10 +220,11 @@ type BaseComposerState = Readonly<{
202
220
  type ReadonlyStore<T> = UseBoundStore<Omit<StoreApi<T>, "setState" | "destroy">>;
203
221
 
204
222
  type EditComposerState = BaseComposerState & Readonly<{
223
+ canCancel: boolean;
205
224
  isEditing: boolean;
206
225
  edit: () => void;
207
226
  send: () => void;
208
- cancel: () => boolean;
227
+ cancel: () => void;
209
228
  }>;
210
229
 
211
230
  type ThreadState = Readonly<{
@@ -271,9 +290,10 @@ type ContentPartContextValue = {
271
290
  declare const useContentPartContext: () => ContentPartContextValue;
272
291
 
273
292
  type ComposerState = BaseComposerState & Readonly<{
293
+ canCancel: boolean;
274
294
  isEditing: true;
275
295
  send: () => void;
276
- cancel: () => boolean;
296
+ cancel: () => void;
277
297
  focus: () => void;
278
298
  onFocus: (listener: () => void) => Unsubscribe;
279
299
  }>;
@@ -605,7 +625,7 @@ type ThreadPrimitiveMessagesProps = {
605
625
  AssistantMessage: ComponentType;
606
626
  };
607
627
  };
608
- declare const ThreadPrimitiveMessages: FC<ThreadPrimitiveMessagesProps>;
628
+ declare const ThreadPrimitiveMessages: react.NamedExoticComponent<ThreadPrimitiveMessagesProps>;
609
629
 
610
630
  type ThreadPrimitiveScrollToBottomProps = ActionButtonProps<typeof useThreadScrollToBottom>;
611
631
  declare const ThreadPrimitiveScrollToBottom: react.ForwardRefExoticComponent<Omit<Omit<react.DetailedHTMLProps<react.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "ref"> & {
package/dist/index.js CHANGED
@@ -210,11 +210,14 @@ var makeBaseComposer = (set) => ({
210
210
  });
211
211
 
212
212
  // src/context/stores/Composer.ts
213
- var makeComposerStore = (useThread, useThreadMessages, useThreadActions) => {
213
+ var makeComposerStore = (useThreadMessages, useThreadActions) => {
214
214
  const focusListeners = /* @__PURE__ */ new Set();
215
215
  return (0, import_zustand3.create)()((set, get, store) => {
216
216
  return {
217
217
  ...makeBaseComposer(set, get, store),
218
+ get canCancel() {
219
+ return useThreadActions.getState().capabilities.cancel;
220
+ },
218
221
  isEditing: true,
219
222
  send: () => {
220
223
  const { setValue, value } = get();
@@ -226,10 +229,7 @@ var makeComposerStore = (useThread, useThreadMessages, useThreadActions) => {
226
229
  });
227
230
  },
228
231
  cancel: () => {
229
- const thread = useThread.getState();
230
- if (!thread.isRunning) return false;
231
232
  useThreadActions.getState().cancelRun();
232
- return true;
233
233
  },
234
234
  focus: () => {
235
235
  for (const listener of focusListeners) {
@@ -279,6 +279,9 @@ var import_zustand6 = require("zustand");
279
279
  var makeThreadActionStore = (runtimeRef) => {
280
280
  return (0, import_zustand6.create)(
281
281
  () => Object.freeze({
282
+ get capabilities() {
283
+ return runtimeRef.current.capabilities;
284
+ },
282
285
  getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
283
286
  switchToBranch: (branchId) => runtimeRef.current.switchToBranch(branchId),
284
287
  startRun: (parentId) => runtimeRef.current.startRun(parentId),
@@ -310,11 +313,7 @@ var ThreadProvider = ({
310
313
  const useThreadMessages = makeThreadMessagesStore(runtimeRef);
311
314
  const useThreadActions = makeThreadActionStore(runtimeRef);
312
315
  const useViewport = makeThreadViewportStore();
313
- const useComposer = makeComposerStore(
314
- useThread,
315
- useThreadMessages,
316
- useThreadActions
317
- );
316
+ const useComposer = makeComposerStore(useThreadMessages, useThreadActions);
318
317
  return {
319
318
  useThread,
320
319
  useThreadMessages,
@@ -687,7 +686,7 @@ var useBranchPickerPrevious = () => {
687
686
  var import_react21 = require("react");
688
687
  var useComposerCancel = () => {
689
688
  const { useComposer } = useComposerContext();
690
- const disabled = useComposer((c) => !c.isEditing);
689
+ const disabled = useComposer((c) => !c.canCancel);
691
690
  const callback = (0, import_react21.useCallback)(() => {
692
691
  const { cancel } = useComposer.getState();
693
692
  cancel();
@@ -819,15 +818,18 @@ var useThreadSuggestion = ({
819
818
  autoSend
820
819
  }) => {
821
820
  const { useThread, useComposer } = useThreadContext();
821
+ const append = useAppendMessage();
822
822
  const disabled = useThread((t) => t.isRunning);
823
823
  const callback = (0, import_react24.useCallback)(() => {
824
824
  const thread = useThread.getState();
825
825
  const composer = useComposer.getState();
826
- composer.setValue(prompt);
827
826
  if (autoSend && !thread.isRunning) {
828
- composer.send();
827
+ append(prompt);
828
+ composer.setValue("");
829
+ } else {
830
+ composer.setValue(prompt);
829
831
  }
830
- }, [useThread, useComposer, prompt, autoSend]);
832
+ }, [useThread, useComposer, autoSend, append, prompt]);
831
833
  if (disabled) return null;
832
834
  return callback;
833
835
  };
@@ -1262,7 +1264,7 @@ var import_jsx_runtime16 = require("react/jsx-runtime");
1262
1264
  var defaultComponents = {
1263
1265
  Text: () => /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)("p", { style: { whiteSpace: "pre-line" }, children: [
1264
1266
  /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveText, {}),
1265
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveInProgress, { children: " \u25CF" })
1267
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveInProgress, { children: /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("span", { style: { fontFamily: "revert" }, children: " \u25CF" }) })
1266
1268
  ] }),
1267
1269
  Image: () => /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveImage, {}),
1268
1270
  UI: () => /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveDisplay, {}),
@@ -1400,7 +1402,8 @@ var ComposerPrimitiveInput = (0, import_react40.forwardRef)(
1400
1402
  const ref = (0, import_react_compose_refs2.useComposedRefs)(forwardedRef, textareaRef);
1401
1403
  (0, import_react_use_escape_keydown.useEscapeKeydown)((e) => {
1402
1404
  const composer = useComposer.getState();
1403
- if (composer.cancel()) {
1405
+ if (composer.canCancel) {
1406
+ composer.cancel();
1404
1407
  e.preventDefault();
1405
1408
  }
1406
1409
  });
@@ -1674,20 +1677,19 @@ var makeEditComposerStore = ({
1674
1677
  onSend
1675
1678
  }) => (0, import_zustand10.create)()((set, get, store) => ({
1676
1679
  ...makeBaseComposer(set, get, store),
1680
+ canCancel: false,
1677
1681
  isEditing: false,
1678
1682
  edit: () => {
1679
1683
  const value = onEdit();
1680
- set({ isEditing: true, value });
1684
+ set({ isEditing: true, canCancel: true, value });
1681
1685
  },
1682
1686
  send: () => {
1683
1687
  const value = get().value;
1684
- set({ isEditing: false });
1688
+ set({ isEditing: false, canCancel: false });
1685
1689
  onSend(value);
1686
1690
  },
1687
1691
  cancel: () => {
1688
- if (!get().isEditing) return false;
1689
- set({ isEditing: false });
1690
- return true;
1692
+ set({ isEditing: false, canCancel: false });
1691
1693
  }
1692
1694
  }));
1693
1695
 
@@ -1808,9 +1810,9 @@ var ThreadMessageImpl = ({
1808
1810
  };
1809
1811
  var ThreadMessage = (0, import_react48.memo)(
1810
1812
  ThreadMessageImpl,
1811
- (prev, next) => prev.messageIndex === next.messageIndex && prev.components.UserMessage === next.components.UserMessage && prev.components.EditComposer === next.components.EditComposer && prev.components.AssistantMessage === next.components.AssistantMessage
1813
+ (prev, next) => prev.messageIndex === next.messageIndex && prev.components.Message === next.components.Message && prev.components.UserMessage === next.components.UserMessage && prev.components.EditComposer === next.components.EditComposer && prev.components.AssistantMessage === next.components.AssistantMessage
1812
1814
  );
1813
- var ThreadPrimitiveMessages = ({
1815
+ var ThreadPrimitiveMessagesImpl = ({
1814
1816
  components
1815
1817
  }) => {
1816
1818
  const { useThreadMessages } = useThreadContext();
@@ -1828,7 +1830,11 @@ var ThreadPrimitiveMessages = ({
1828
1830
  );
1829
1831
  });
1830
1832
  };
1831
- ThreadPrimitiveMessages.displayName = "ThreadPrimitive.Messages";
1833
+ ThreadPrimitiveMessagesImpl.displayName = "ThreadPrimitive.Messages";
1834
+ var ThreadPrimitiveMessages = (0, import_react48.memo)(
1835
+ ThreadPrimitiveMessagesImpl,
1836
+ (prev, next) => prev.components?.Message === next.components?.Message && prev.components?.UserMessage === next.components?.UserMessage && prev.components?.EditComposer === next.components?.EditComposer && prev.components?.AssistantMessage === next.components?.AssistantMessage
1837
+ );
1832
1838
 
1833
1839
  // src/primitives/thread/ThreadScrollToBottom.tsx
1834
1840
  var ThreadPrimitiveScrollToBottom = createActionButton(
@@ -2012,6 +2018,9 @@ var BaseAssistantRuntime = class {
2012
2018
  this._thread = _thread;
2013
2019
  this._unsubscribe = this._thread.subscribe(this.subscriptionHandler);
2014
2020
  }
2021
+ get capabilities() {
2022
+ return this._thread.capabilities;
2023
+ }
2015
2024
  _unsubscribe;
2016
2025
  get thread() {
2017
2026
  return this._thread;
@@ -2084,6 +2093,12 @@ var LocalRuntime = class extends BaseAssistantRuntime {
2084
2093
  );
2085
2094
  }
2086
2095
  };
2096
+ var CAPABILITIES = Object.freeze({
2097
+ edit: true,
2098
+ reload: true,
2099
+ cancel: true,
2100
+ copy: true
2101
+ });
2087
2102
  var LocalThreadRuntime = class {
2088
2103
  constructor(_configProviders, adapter) {
2089
2104
  this._configProviders = _configProviders;
@@ -2092,6 +2107,7 @@ var LocalThreadRuntime = class {
2092
2107
  _subscriptions = /* @__PURE__ */ new Set();
2093
2108
  abortController = null;
2094
2109
  repository = new MessageRepository();
2110
+ capabilities = CAPABILITIES;
2095
2111
  get messages() {
2096
2112
  return this.repository.getMessages();
2097
2113
  }