@assistant-ui/react 0.5.22 → 0.5.23

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -77,6 +77,33 @@ type ToolCallContentPartProps<TArgs extends Record<string, unknown> = any, TResu
77
77
  };
78
78
  type ToolCallContentPartComponent<TArgs extends Record<string, unknown> = any, TResult = any> = ComponentType<ToolCallContentPartProps<TArgs, TResult>>;
79
79
 
80
+ interface ExportedMessageRepository {
81
+ headId?: string | null;
82
+ messages: Array<{
83
+ message: ThreadMessage;
84
+ parentId: string | null;
85
+ }>;
86
+ }
87
+ declare class MessageRepository {
88
+ private messages;
89
+ private head;
90
+ private root;
91
+ private performOp;
92
+ getMessages(): ThreadMessage[];
93
+ addOrUpdateMessage(parentId: string | null, message: ThreadMessage): void;
94
+ getMessage(messageId: string): {
95
+ parentId: string | null;
96
+ message: ThreadMessage;
97
+ };
98
+ appendOptimisticMessage(parentId: string | null, message: CoreMessage): string;
99
+ deleteMessage(messageId: string, replacementId?: string | null | undefined): void;
100
+ getBranches(messageId: string): string[];
101
+ switchToBranch(messageId: string): void;
102
+ resetHead(messageId: string | null): void;
103
+ export(): ExportedMessageRepository;
104
+ import({ headId, messages }: ExportedMessageRepository): void;
105
+ }
106
+
80
107
  type LocalRuntimeOptions = {
81
108
  initialMessages?: readonly CoreMessage[] | undefined;
82
109
  maxToolRoundtrips?: number;
@@ -109,6 +136,8 @@ declare class LocalThreadRuntime implements ThreadRuntime {
109
136
  private notifySubscribers;
110
137
  subscribe(callback: () => void): Unsubscribe;
111
138
  addToolResult({ messageId, toolCallId, result }: AddToolResultOptions): void;
139
+ export(): ExportedMessageRepository;
140
+ import(data: ExportedMessageRepository): void;
112
141
  }
113
142
 
114
143
  declare class LocalRuntime extends BaseAssistantRuntime<LocalThreadRuntime> {
@@ -439,24 +468,6 @@ declare class ProxyConfigProvider implements ModelConfigProvider {
439
468
  registerModelConfigProvider(provider: ModelConfigProvider): () => void;
440
469
  }
441
470
 
442
- declare class MessageRepository {
443
- private messages;
444
- private head;
445
- private root;
446
- private performOp;
447
- getMessages(): ThreadMessage[];
448
- addOrUpdateMessage(parentId: string | null, message: ThreadMessage): void;
449
- getMessage(messageId: string): {
450
- parentId: string | null;
451
- message: ThreadMessage;
452
- };
453
- appendOptimisticMessage(parentId: string | null, message: CoreMessage): string;
454
- deleteMessage(messageId: string, replacementId?: string | null | undefined): void;
455
- getBranches(messageId: string): string[];
456
- switchToBranch(messageId: string): void;
457
- resetHead(messageId: string | null): void;
458
- }
459
-
460
471
  type TextContentPartState = Readonly<{
461
472
  status: ContentPartStatus;
462
473
  part: TextContentPart;
package/dist/index.d.ts CHANGED
@@ -77,6 +77,33 @@ type ToolCallContentPartProps<TArgs extends Record<string, unknown> = any, TResu
77
77
  };
78
78
  type ToolCallContentPartComponent<TArgs extends Record<string, unknown> = any, TResult = any> = ComponentType<ToolCallContentPartProps<TArgs, TResult>>;
79
79
 
80
+ interface ExportedMessageRepository {
81
+ headId?: string | null;
82
+ messages: Array<{
83
+ message: ThreadMessage;
84
+ parentId: string | null;
85
+ }>;
86
+ }
87
+ declare class MessageRepository {
88
+ private messages;
89
+ private head;
90
+ private root;
91
+ private performOp;
92
+ getMessages(): ThreadMessage[];
93
+ addOrUpdateMessage(parentId: string | null, message: ThreadMessage): void;
94
+ getMessage(messageId: string): {
95
+ parentId: string | null;
96
+ message: ThreadMessage;
97
+ };
98
+ appendOptimisticMessage(parentId: string | null, message: CoreMessage): string;
99
+ deleteMessage(messageId: string, replacementId?: string | null | undefined): void;
100
+ getBranches(messageId: string): string[];
101
+ switchToBranch(messageId: string): void;
102
+ resetHead(messageId: string | null): void;
103
+ export(): ExportedMessageRepository;
104
+ import({ headId, messages }: ExportedMessageRepository): void;
105
+ }
106
+
80
107
  type LocalRuntimeOptions = {
81
108
  initialMessages?: readonly CoreMessage[] | undefined;
82
109
  maxToolRoundtrips?: number;
@@ -109,6 +136,8 @@ declare class LocalThreadRuntime implements ThreadRuntime {
109
136
  private notifySubscribers;
110
137
  subscribe(callback: () => void): Unsubscribe;
111
138
  addToolResult({ messageId, toolCallId, result }: AddToolResultOptions): void;
139
+ export(): ExportedMessageRepository;
140
+ import(data: ExportedMessageRepository): void;
112
141
  }
113
142
 
114
143
  declare class LocalRuntime extends BaseAssistantRuntime<LocalThreadRuntime> {
@@ -439,24 +468,6 @@ declare class ProxyConfigProvider implements ModelConfigProvider {
439
468
  registerModelConfigProvider(provider: ModelConfigProvider): () => void;
440
469
  }
441
470
 
442
- declare class MessageRepository {
443
- private messages;
444
- private head;
445
- private root;
446
- private performOp;
447
- getMessages(): ThreadMessage[];
448
- addOrUpdateMessage(parentId: string | null, message: ThreadMessage): void;
449
- getMessage(messageId: string): {
450
- parentId: string | null;
451
- message: ThreadMessage;
452
- };
453
- appendOptimisticMessage(parentId: string | null, message: CoreMessage): string;
454
- deleteMessage(messageId: string, replacementId?: string | null | undefined): void;
455
- getBranches(messageId: string): string[];
456
- switchToBranch(messageId: string): void;
457
- resetHead(messageId: string | null): void;
458
- }
459
-
460
471
  type TextContentPartState = Readonly<{
461
472
  status: ContentPartStatus;
462
473
  part: TextContentPart;
package/dist/index.js CHANGED
@@ -2273,6 +2273,25 @@ var MessageRepository = (_class4 = class {constructor() { _class4.prototype.__in
2273
2273
  }
2274
2274
  }
2275
2275
  }
2276
+ export() {
2277
+ const exportItems = [];
2278
+ for (const [, message] of this.messages) {
2279
+ exportItems.push({
2280
+ message: message.current,
2281
+ parentId: _nullishCoalesce(_optionalChain([message, 'access', _84 => _84.prev, 'optionalAccess', _85 => _85.current, 'access', _86 => _86.id]), () => ( null))
2282
+ });
2283
+ }
2284
+ return {
2285
+ headId: _nullishCoalesce(_optionalChain([this, 'access', _87 => _87.head, 'optionalAccess', _88 => _88.current, 'access', _89 => _89.id]), () => ( null)),
2286
+ messages: exportItems
2287
+ };
2288
+ }
2289
+ import({ headId, messages }) {
2290
+ for (const { message, parentId } of messages) {
2291
+ this.addOrUpdateMessage(parentId, message);
2292
+ }
2293
+ this.resetHead(_nullishCoalesce(_nullishCoalesce(headId, () => ( _optionalChain([messages, 'access', _90 => _90.at, 'call', _91 => _91(-1), 'optionalAccess', _92 => _92.message, 'access', _93 => _93.id]))), () => ( null)));
2294
+ }
2276
2295
  }, _class4);
2277
2296
 
2278
2297
  // src/ui/base/tooltip-icon-button.tsx
@@ -2434,7 +2453,7 @@ var fromLanguageModelMessages = (lm, { mergeRoundtrips }) => {
2434
2453
  });
2435
2454
  if (mergeRoundtrips) {
2436
2455
  const previousMessage = messages[messages.length - 1];
2437
- if (_optionalChain([previousMessage, 'optionalAccess', _84 => _84.role]) === "assistant") {
2456
+ if (_optionalChain([previousMessage, 'optionalAccess', _94 => _94.role]) === "assistant") {
2438
2457
  previousMessage.content.push(...newContent);
2439
2458
  break;
2440
2459
  }
@@ -2447,7 +2466,7 @@ var fromLanguageModelMessages = (lm, { mergeRoundtrips }) => {
2447
2466
  }
2448
2467
  case "tool": {
2449
2468
  const previousMessage = messages[messages.length - 1];
2450
- if (_optionalChain([previousMessage, 'optionalAccess', _85 => _85.role]) !== "assistant")
2469
+ if (_optionalChain([previousMessage, 'optionalAccess', _95 => _95.role]) !== "assistant")
2451
2470
  throw new Error(
2452
2471
  "A tool message must be preceded by an assistant message."
2453
2472
  );
@@ -2651,7 +2670,7 @@ var useEdgeRuntime = ({
2651
2670
  };
2652
2671
 
2653
2672
  // src/runtimes/local/shouldContinue.tsx
2654
- var shouldContinue = (result) => _optionalChain([result, 'access', _86 => _86.status, 'optionalAccess', _87 => _87.type]) === "requires-action" && result.status.reason === "tool-calls" && result.content.every((c) => c.type !== "tool-call" || !!c.result);
2673
+ var shouldContinue = (result) => _optionalChain([result, 'access', _96 => _96.status, 'optionalAccess', _97 => _97.type]) === "requires-action" && result.status.reason === "tool-calls" && result.content.every((c) => c.type !== "tool-call" || !!c.result);
2655
2674
 
2656
2675
  // src/runtimes/local/LocalThreadRuntime.tsx
2657
2676
  var CAPABILITIES = Object.freeze({
@@ -2666,7 +2685,7 @@ var LocalThreadRuntime = (_class5 = class {
2666
2685
  this.configProvider = configProvider;
2667
2686
  this.adapter = adapter;
2668
2687
  this.options = options;
2669
- if (_optionalChain([options, 'optionalAccess', _88 => _88.initialMessages])) {
2688
+ if (_optionalChain([options, 'optionalAccess', _98 => _98.initialMessages])) {
2670
2689
  let parentId = null;
2671
2690
  const messages = fromCoreMessages(options.initialMessages);
2672
2691
  for (const message of messages) {
@@ -2724,7 +2743,7 @@ var LocalThreadRuntime = (_class5 = class {
2724
2743
  }
2725
2744
  async performRoundtrip(parentId, message) {
2726
2745
  const messages = this.repository.getMessages();
2727
- _optionalChain([this, 'access', _89 => _89.abortController, 'optionalAccess', _90 => _90.abort, 'call', _91 => _91()]);
2746
+ _optionalChain([this, 'access', _99 => _99.abortController, 'optionalAccess', _100 => _100.abort, 'call', _101 => _101()]);
2728
2747
  this.abortController = new AbortController();
2729
2748
  const initialContent = message.content;
2730
2749
  const initialRoundtrips = message.roundtrips;
@@ -2733,13 +2752,13 @@ var LocalThreadRuntime = (_class5 = class {
2733
2752
  ...message,
2734
2753
  ...m.content ? { content: [...initialContent, ..._nullishCoalesce(m.content, () => ( []))] } : void 0,
2735
2754
  status: _nullishCoalesce(m.status, () => ( message.status)),
2736
- ..._optionalChain([m, 'access', _92 => _92.roundtrips, 'optionalAccess', _93 => _93.length]) ? { roundtrips: [..._nullishCoalesce(initialRoundtrips, () => ( [])), ...m.roundtrips] } : void 0
2755
+ ..._optionalChain([m, 'access', _102 => _102.roundtrips, 'optionalAccess', _103 => _103.length]) ? { roundtrips: [..._nullishCoalesce(initialRoundtrips, () => ( [])), ...m.roundtrips] } : void 0
2737
2756
  };
2738
2757
  this.repository.addOrUpdateMessage(parentId, message);
2739
2758
  this.notifySubscribers();
2740
2759
  };
2741
- const maxToolRoundtrips = _nullishCoalesce(_optionalChain([this, 'access', _94 => _94.options, 'optionalAccess', _95 => _95.maxToolRoundtrips]), () => ( 1));
2742
- const toolRoundtrips = _nullishCoalesce(_optionalChain([message, 'access', _96 => _96.roundtrips, 'optionalAccess', _97 => _97.length]), () => ( 0));
2760
+ const maxToolRoundtrips = _nullishCoalesce(_optionalChain([this, 'access', _104 => _104.options, 'optionalAccess', _105 => _105.maxToolRoundtrips]), () => ( 1));
2761
+ const toolRoundtrips = _nullishCoalesce(_optionalChain([message, 'access', _106 => _106.roundtrips, 'optionalAccess', _107 => _107.length]), () => ( 0));
2743
2762
  if (toolRoundtrips > maxToolRoundtrips) {
2744
2763
  updateMessage({
2745
2764
  status: {
@@ -2831,6 +2850,13 @@ var LocalThreadRuntime = (_class5 = class {
2831
2850
  this.performRoundtrip(parentId, message);
2832
2851
  }
2833
2852
  }
2853
+ export() {
2854
+ return this.repository.export();
2855
+ }
2856
+ import(data) {
2857
+ this.repository.import(data);
2858
+ this.notifySubscribers();
2859
+ }
2834
2860
  }, _class5);
2835
2861
 
2836
2862
  // src/runtimes/local/LocalRuntime.tsx
@@ -3012,7 +3038,7 @@ var useExternalStoreSync = (adapter, updateData) => {
3012
3038
 
3013
3039
  // src/runtimes/external-store/ExternalStoreThreadRuntime.tsx
3014
3040
  var hasUpcomingMessage = (isRunning, messages) => {
3015
- return isRunning && _optionalChain([messages, 'access', _98 => _98[messages.length - 1], 'optionalAccess', _99 => _99.role]) !== "assistant";
3041
+ return isRunning && _optionalChain([messages, 'access', _108 => _108[messages.length - 1], 'optionalAccess', _109 => _109.role]) !== "assistant";
3016
3042
  };
3017
3043
  var ExternalStoreThreadRuntime = (_class7 = class {
3018
3044
  constructor(store) {;_class7.prototype.__init17.call(this);_class7.prototype.__init18.call(this);_class7.prototype.__init19.call(this);_class7.prototype.__init20.call(this);_class7.prototype.__init21.call(this);_class7.prototype.__init22.call(this);_class7.prototype.__init23.call(this);_class7.prototype.__init24.call(this);_class7.prototype.__init25.call(this);
@@ -3052,7 +3078,7 @@ var ExternalStoreThreadRuntime = (_class7 = class {
3052
3078
  this.updateMessages(this.repository.getMessages());
3053
3079
  }
3054
3080
  async append(message) {
3055
- if (message.parentId !== (_nullishCoalesce(_optionalChain([this, 'access', _110 => _110.messages, 'access', _111 => _111.at, 'call', _112 => _112(-1), 'optionalAccess', _113 => _113.id]), () => ( null)))) {
3081
+ if (message.parentId !== (_nullishCoalesce(_optionalChain([this, 'access', _120 => _120.messages, 'access', _121 => _121.at, 'call', _122 => _122(-1), 'optionalAccess', _123 => _123.id]), () => ( null)))) {
3056
3082
  if (!this.store.onEdit)
3057
3083
  throw new Error("Runtime does not support editing messages.");
3058
3084
  await this.store.onEdit(message);
@@ -3083,7 +3109,7 @@ var ExternalStoreThreadRuntime = (_class7 = class {
3083
3109
  return () => this._subscriptions.delete(callback);
3084
3110
  }
3085
3111
  __init23() {this.updateMessages = (messages) => {
3086
- _optionalChain([this, 'access', _114 => _114.store, 'access', _115 => _115.setMessages, 'optionalCall', _116 => _116(
3112
+ _optionalChain([this, 'access', _124 => _124.store, 'access', _125 => _125.setMessages, 'optionalCall', _126 => _126(
3087
3113
  messages.flatMap(getExternalStoreMessage).filter((m) => m != null)
3088
3114
  )]);
3089
3115
  }}
@@ -3096,7 +3122,7 @@ var ExternalStoreThreadRuntime = (_class7 = class {
3096
3122
  for (let i = 0; i < vm.length; i++) {
3097
3123
  const message = vm[i];
3098
3124
  const parent = vm[i - 1];
3099
- this.repository.addOrUpdateMessage(_nullishCoalesce(_optionalChain([parent, 'optionalAccess', _117 => _117.id]), () => ( null)), message);
3125
+ this.repository.addOrUpdateMessage(_nullishCoalesce(_optionalChain([parent, 'optionalAccess', _127 => _127.id]), () => ( null)), message);
3100
3126
  }
3101
3127
  if (this.assistantOptimisticId) {
3102
3128
  this.repository.deleteMessage(this.assistantOptimisticId);
@@ -3104,7 +3130,7 @@ var ExternalStoreThreadRuntime = (_class7 = class {
3104
3130
  }
3105
3131
  if (hasUpcomingMessage(isRunning, vm)) {
3106
3132
  this.assistantOptimisticId = this.repository.appendOptimisticMessage(
3107
- _nullishCoalesce(_optionalChain([vm, 'access', _118 => _118.at, 'call', _119 => _119(-1), 'optionalAccess', _120 => _120.id]), () => ( null)),
3133
+ _nullishCoalesce(_optionalChain([vm, 'access', _128 => _128.at, 'call', _129 => _129(-1), 'optionalAccess', _130 => _130.id]), () => ( null)),
3108
3134
  {
3109
3135
  role: "assistant",
3110
3136
  content: []
@@ -3112,7 +3138,7 @@ var ExternalStoreThreadRuntime = (_class7 = class {
3112
3138
  );
3113
3139
  }
3114
3140
  this.repository.resetHead(
3115
- _nullishCoalesce(_nullishCoalesce(this.assistantOptimisticId, () => ( _optionalChain([vm, 'access', _121 => _121.at, 'call', _122 => _122(-1), 'optionalAccess', _123 => _123.id]))), () => ( null))
3141
+ _nullishCoalesce(_nullishCoalesce(this.assistantOptimisticId, () => ( _optionalChain([vm, 'access', _131 => _131.at, 'call', _132 => _132(-1), 'optionalAccess', _133 => _133.id]))), () => ( null))
3116
3142
  );
3117
3143
  this.messages = this.repository.getMessages();
3118
3144
  this.isDisabled = isDisabled;
@@ -3191,7 +3217,7 @@ var ThreadConfigProvider = ({
3191
3217
  }) => {
3192
3218
  const assistant = useAssistantContext({ optional: true });
3193
3219
  const configProvider = config && Object.keys(_nullishCoalesce(config, () => ( {}))).length > 0 ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, ThreadConfigContext.Provider, { value: config, children }) : /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _jsxruntime.Fragment, { children });
3194
- if (!_optionalChain([config, 'optionalAccess', _124 => _124.runtime])) return configProvider;
3220
+ if (!_optionalChain([config, 'optionalAccess', _134 => _134.runtime])) return configProvider;
3195
3221
  if (assistant) {
3196
3222
  throw new Error(
3197
3223
  "You provided a runtime to <Thread> while simulataneously using <AssistantRuntimeProvider>. This is not allowed."
@@ -3407,7 +3433,7 @@ var AssistantMessageContent = _react.forwardRef.call(void 0, ({ components: comp
3407
3433
  {
3408
3434
  components: {
3409
3435
  ...componentsProp,
3410
- Text: _nullishCoalesce(_nullishCoalesce(_optionalChain([componentsProp, 'optionalAccess', _125 => _125.Text]), () => ( components.Text)), () => ( content_part_default.Text))
3436
+ Text: _nullishCoalesce(_nullishCoalesce(_optionalChain([componentsProp, 'optionalAccess', _135 => _135.Text]), () => ( components.Text)), () => ( content_part_default.Text))
3411
3437
  }
3412
3438
  }
3413
3439
  ) });
@@ -3582,7 +3608,7 @@ var ThreadWelcomeSuggestion = ({
3582
3608
  };
3583
3609
  var ThreadWelcomeSuggestions = () => {
3584
3610
  const { welcome: { suggestions } = {} } = useThreadConfig();
3585
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, ThreadWelcomeSuggestionContainer, { children: _optionalChain([suggestions, 'optionalAccess', _126 => _126.map, 'call', _127 => _127((suggestion, idx) => {
3611
+ return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, ThreadWelcomeSuggestionContainer, { children: _optionalChain([suggestions, 'optionalAccess', _136 => _136.map, 'call', _137 => _137((suggestion, idx) => {
3586
3612
  const key = `${suggestion.prompt}-${idx}`;
3587
3613
  return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, ThreadWelcomeSuggestion, { suggestion }, key);
3588
3614
  })]) });
@@ -3660,7 +3686,7 @@ var UserMessageContent = _react.forwardRef.call(void 0,
3660
3686
  {
3661
3687
  components: {
3662
3688
  ...components,
3663
- Text: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _128 => _128.Text]), () => ( content_part_default.Text))
3689
+ Text: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _138 => _138.Text]), () => ( content_part_default.Text))
3664
3690
  }
3665
3691
  }
3666
3692
  ) });
@@ -3762,10 +3788,10 @@ var ThreadMessages = ({ components, ...rest }) => {
3762
3788
  thread_exports.Messages,
3763
3789
  {
3764
3790
  components: {
3765
- UserMessage: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _129 => _129.UserMessage]), () => ( user_message_default)),
3766
- EditComposer: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _130 => _130.EditComposer]), () => ( edit_composer_default)),
3767
- AssistantMessage: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _131 => _131.AssistantMessage]), () => ( assistant_message_default)),
3768
- SystemMessage: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _132 => _132.SystemMessage]), () => ( SystemMessage))
3791
+ UserMessage: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _139 => _139.UserMessage]), () => ( user_message_default)),
3792
+ EditComposer: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _140 => _140.EditComposer]), () => ( edit_composer_default)),
3793
+ AssistantMessage: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _141 => _141.AssistantMessage]), () => ( assistant_message_default)),
3794
+ SystemMessage: _nullishCoalesce(_optionalChain([components, 'optionalAccess', _142 => _142.SystemMessage]), () => ( SystemMessage))
3769
3795
  },
3770
3796
  ...rest
3771
3797
  }