@assistant-ui/react 0.5.27 → 0.5.29

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1,13 +1,15 @@
1
1
  "use client";
2
2
  import {
3
+ PipeableTransformStream,
3
4
  mergeModelConfigs,
4
5
  runResultStream,
6
+ streamPartEncoderStream,
5
7
  toCoreMessage,
6
8
  toCoreMessages,
7
9
  toLanguageModelMessages,
8
10
  toLanguageModelTools,
9
11
  toolResultStream
10
- } from "./chunk-ZWRFAYHH.mjs";
12
+ } from "./chunk-DXLSOE2J.mjs";
11
13
  import {
12
14
  __export
13
15
  } from "./chunk-BJPOCE4O.mjs";
@@ -164,13 +166,11 @@ var getThreadStateFromRuntime = (runtime) => {
164
166
  if (lastMessage?.role !== "assistant")
165
167
  return Object.freeze({
166
168
  isDisabled: runtime.isDisabled,
167
- isRunning: false,
168
- unstable_canAppendNew: !runtime.isDisabled
169
+ isRunning: false
169
170
  });
170
171
  return Object.freeze({
171
172
  isDisabled: runtime.isDisabled,
172
- isRunning: lastMessage.status.type === "running",
173
- unstable_canAppendNew: !runtime.isDisabled && lastMessage.status.type !== "running" && lastMessage.status.type !== "requires-action"
173
+ isRunning: lastMessage.status.type === "running"
174
174
  });
175
175
  };
176
176
  var makeThreadStore = (runtimeRef) => {
@@ -274,7 +274,7 @@ var ThreadProvider = ({
274
274
  const onThreadUpdate = () => {
275
275
  const oldState = context.useThread.getState();
276
276
  const state = getThreadStateFromRuntime(thread);
277
- if (oldState.isDisabled !== state.isDisabled || oldState.isRunning !== state.isRunning || oldState.unstable_canAppendNew !== state.unstable_canAppendNew) {
277
+ if (oldState.isDisabled !== state.isDisabled || oldState.isRunning !== state.isRunning) {
278
278
  context.useThread.setState(
279
279
  getThreadStateFromRuntime(thread),
280
280
  true
@@ -680,7 +680,7 @@ var useComposerSend = () => {
680
680
  const { useComposer } = useComposerContext();
681
681
  const disabled = useCombinedStore(
682
682
  [useThread, useComposer],
683
- (t, c) => !t.unstable_canAppendNew || !c.isEditing || c.value.length === 0
683
+ (t, c) => t.isRunning || !c.isEditing || c.value.length === 0
684
684
  );
685
685
  const callback = useCallback11(() => {
686
686
  const composerState = useComposer.getState();
@@ -1578,8 +1578,8 @@ var ComposerPrimitiveInput = forwardRef12(
1578
1578
  const handleKeyPress = (e) => {
1579
1579
  if (isDisabled) return;
1580
1580
  if (e.key === "Enter" && e.shiftKey === false) {
1581
- const { unstable_canAppendNew } = useThread.getState();
1582
- if (unstable_canAppendNew) {
1581
+ const { isRunning } = useThread.getState();
1582
+ if (!isRunning) {
1583
1583
  e.preventDefault();
1584
1584
  textareaRef.current?.closest("form")?.requestSubmit();
1585
1585
  }
@@ -2524,6 +2524,52 @@ var fromLanguageModelTools = (tools) => {
2524
2524
  );
2525
2525
  };
2526
2526
 
2527
+ // src/runtimes/edge/streams/utils/chunkByLineStream.ts
2528
+ function chunkByLineStream() {
2529
+ let buffer = "";
2530
+ return new TransformStream({
2531
+ transform(chunk, controller) {
2532
+ buffer += chunk;
2533
+ const lines = buffer.split("\n");
2534
+ for (let i = 0; i < lines.length - 1; i++) {
2535
+ controller.enqueue(lines[i]);
2536
+ }
2537
+ buffer = lines[lines.length - 1];
2538
+ },
2539
+ flush(controller) {
2540
+ if (buffer) {
2541
+ controller.enqueue(buffer);
2542
+ }
2543
+ }
2544
+ });
2545
+ }
2546
+
2547
+ // src/runtimes/edge/streams/utils/streamPartDecoderStream.ts
2548
+ var decodeStreamPart = (part) => {
2549
+ const index = part.indexOf(":");
2550
+ if (index === -1) throw new Error("Invalid stream part");
2551
+ return {
2552
+ type: part.slice(0, index),
2553
+ value: JSON.parse(part.slice(index + 1))
2554
+ };
2555
+ };
2556
+ function streamPartDecoderStream() {
2557
+ const decodeStream = new TransformStream({
2558
+ transform(chunk, controller) {
2559
+ controller.enqueue(decodeStreamPart(chunk));
2560
+ }
2561
+ });
2562
+ return new PipeableTransformStream((readable) => {
2563
+ return readable.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(decodeStream);
2564
+ });
2565
+ }
2566
+
2567
+ // src/runtimes/edge/streams/utils/index.ts
2568
+ var streamUtils = {
2569
+ streamPartEncoderStream,
2570
+ streamPartDecoderStream
2571
+ };
2572
+
2527
2573
  // src/runtimes/edge/useEdgeRuntime.ts
2528
2574
  import { useState as useState8 } from "react";
2529
2575
 
@@ -2532,9 +2578,8 @@ function assistantDecoderStream() {
2532
2578
  const toolCallNames = /* @__PURE__ */ new Map();
2533
2579
  let currentToolCall;
2534
2580
  return new TransformStream({
2535
- transform(chunk, controller) {
2536
- const [code, value] = parseStreamPart(chunk);
2537
- if (currentToolCall && code !== "2" /* ToolCallArgsTextDelta */ && code !== "E" /* Error */) {
2581
+ transform({ type, value }, controller) {
2582
+ if (currentToolCall && type !== "2" /* ToolCallArgsTextDelta */ && type !== "E" /* Error */) {
2538
2583
  controller.enqueue({
2539
2584
  type: "tool-call",
2540
2585
  toolCallType: "function",
@@ -2544,7 +2589,7 @@ function assistantDecoderStream() {
2544
2589
  });
2545
2590
  currentToolCall = void 0;
2546
2591
  }
2547
- switch (code) {
2592
+ switch (type) {
2548
2593
  case "0" /* TextDelta */: {
2549
2594
  controller.enqueue({
2550
2595
  type: "text-delta",
@@ -2595,41 +2640,13 @@ function assistantDecoderStream() {
2595
2640
  break;
2596
2641
  }
2597
2642
  default: {
2598
- const unhandledType = code;
2643
+ const unhandledType = type;
2599
2644
  throw new Error(`Unhandled chunk type: ${unhandledType}`);
2600
2645
  }
2601
2646
  }
2602
2647
  }
2603
2648
  });
2604
2649
  }
2605
- var parseStreamPart = (part) => {
2606
- const index = part.indexOf(":");
2607
- if (index === -1) throw new Error("Invalid stream part");
2608
- return [
2609
- part.slice(0, index),
2610
- JSON.parse(part.slice(index + 1))
2611
- ];
2612
- };
2613
-
2614
- // src/runtimes/edge/streams/chunkByLineStream.ts
2615
- function chunkByLineStream() {
2616
- let buffer = "";
2617
- return new TransformStream({
2618
- transform(chunk, controller) {
2619
- buffer += chunk;
2620
- const lines = buffer.split("\n");
2621
- for (let i = 0; i < lines.length - 1; i++) {
2622
- controller.enqueue(lines[i]);
2623
- }
2624
- buffer = lines[lines.length - 1];
2625
- },
2626
- flush(controller) {
2627
- if (buffer) {
2628
- controller.enqueue(buffer);
2629
- }
2630
- }
2631
- });
2632
- }
2633
2650
 
2634
2651
  // src/runtimes/edge/EdgeChatAdapter.ts
2635
2652
  function asAsyncIterable(source) {
@@ -2667,7 +2684,7 @@ var EdgeChatAdapter = class {
2667
2684
  if (result.status !== 200) {
2668
2685
  throw new Error(`Status ${result.status}: ${await result.text()}`);
2669
2686
  }
2670
- const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream());
2687
+ const stream = result.body.pipeThrough(streamPartDecoderStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools, abortSignal)).pipeThrough(runResultStream());
2671
2688
  let update;
2672
2689
  for await (update of asAsyncIterable(stream)) {
2673
2690
  yield update;
@@ -3901,6 +3918,7 @@ export {
3901
3918
  getExternalStoreMessage,
3902
3919
  makeAssistantTool,
3903
3920
  makeAssistantToolUI,
3921
+ streamUtils,
3904
3922
  toCoreMessage,
3905
3923
  toCoreMessages,
3906
3924
  toLanguageModelMessages,