@assistant-ui/react 0.5.16 → 0.5.17

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -186,10 +186,13 @@ type ChatModelRunOptions = {
186
186
  messages: ThreadMessage[];
187
187
  abortSignal: AbortSignal;
188
188
  config: ModelConfig;
189
+ /**
190
+ * @deprecated Declare the run function as an AsyncGenerator instead. This method will be removed in v0.6
191
+ */
189
192
  onUpdate: (result: ChatModelRunUpdate) => void;
190
193
  };
191
194
  type ChatModelAdapter = {
192
- run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult>;
195
+ run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult> | AsyncGenerator<ChatModelRunResult, void>;
193
196
  };
194
197
 
195
198
  declare abstract class BaseAssistantRuntime<TThreadRuntime extends ReactThreadRuntime> implements AssistantRuntime {
@@ -296,7 +299,7 @@ type EdgeChatAdapterOptions = {
296
299
  declare class EdgeChatAdapter implements ChatModelAdapter {
297
300
  private options;
298
301
  constructor(options: EdgeChatAdapterOptions);
299
- run({ messages, abortSignal, config, onUpdate }: ChatModelRunOptions): Promise<ChatModelRunResult>;
302
+ run({ messages, abortSignal, config }: ChatModelRunOptions): AsyncGenerator<ChatModelRunResult, void, unknown>;
300
303
  }
301
304
 
302
305
  type EdgeRuntimeOptions = EdgeChatAdapterOptions & LocalRuntimeOptions;
package/dist/index.d.ts CHANGED
@@ -186,10 +186,13 @@ type ChatModelRunOptions = {
186
186
  messages: ThreadMessage[];
187
187
  abortSignal: AbortSignal;
188
188
  config: ModelConfig;
189
+ /**
190
+ * @deprecated Declare the run function as an AsyncGenerator instead. This method will be removed in v0.6
191
+ */
189
192
  onUpdate: (result: ChatModelRunUpdate) => void;
190
193
  };
191
194
  type ChatModelAdapter = {
192
- run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult>;
195
+ run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult> | AsyncGenerator<ChatModelRunResult, void>;
193
196
  };
194
197
 
195
198
  declare abstract class BaseAssistantRuntime<TThreadRuntime extends ReactThreadRuntime> implements AssistantRuntime {
@@ -296,7 +299,7 @@ type EdgeChatAdapterOptions = {
296
299
  declare class EdgeChatAdapter implements ChatModelAdapter {
297
300
  private options;
298
301
  constructor(options: EdgeChatAdapterOptions);
299
- run({ messages, abortSignal, config, onUpdate }: ChatModelRunOptions): Promise<ChatModelRunResult>;
302
+ run({ messages, abortSignal, config }: ChatModelRunOptions): AsyncGenerator<ChatModelRunResult, void, unknown>;
300
303
  }
301
304
 
302
305
  type EdgeRuntimeOptions = EdgeChatAdapterOptions & LocalRuntimeOptions;
package/dist/index.js CHANGED
@@ -3428,7 +3428,7 @@ var EdgeChatAdapter = class {
3428
3428
  constructor(options) {
3429
3429
  this.options = options;
3430
3430
  }
3431
- async run({ messages, abortSignal, config, onUpdate }) {
3431
+ async *run({ messages, abortSignal, config }) {
3432
3432
  const result = await fetch(this.options.api, {
3433
3433
  method: "POST",
3434
3434
  headers: {
@@ -3449,11 +3449,10 @@ var EdgeChatAdapter = class {
3449
3449
  const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream());
3450
3450
  let update;
3451
3451
  for await (update of asAsyncIterable(stream)) {
3452
- onUpdate(update);
3452
+ yield update;
3453
3453
  }
3454
3454
  if (update === void 0)
3455
3455
  throw new Error("No data received from Edge Runtime");
3456
- return update;
3457
3456
  }
3458
3457
  };
3459
3458
 
@@ -3572,21 +3571,27 @@ var LocalThreadRuntime = class {
3572
3571
  });
3573
3572
  }
3574
3573
  try {
3575
- const result = await this.adapter.run({
3574
+ const promiseOrGenerator = this.adapter.run({
3576
3575
  messages,
3577
3576
  abortSignal: this.abortController.signal,
3578
3577
  config: this.configProvider.getModelConfig(),
3579
3578
  onUpdate: updateMessage
3580
3579
  });
3581
- if (result.status?.type === "running")
3582
- throw new Error(
3583
- "Unexpected running status returned from ChatModelAdapter"
3584
- );
3580
+ if (Symbol.asyncIterator in promiseOrGenerator) {
3581
+ for await (const r of promiseOrGenerator) {
3582
+ updateMessage(r);
3583
+ }
3584
+ } else {
3585
+ updateMessage(await promiseOrGenerator);
3586
+ }
3585
3587
  this.abortController = null;
3586
- updateMessage({
3587
- status: { type: "complete", reason: "unknown" },
3588
- ...result
3589
- });
3588
+ if (message.status.type === "running") {
3589
+ updateMessage({
3590
+ status: { type: "complete", reason: "unknown" }
3591
+ });
3592
+ } else {
3593
+ this.notifySubscribers();
3594
+ }
3590
3595
  } catch (e) {
3591
3596
  this.abortController = null;
3592
3597
  if (e instanceof Error && e.name === "AbortError") {