@assistant-ui/react 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1178,7 +1178,7 @@ var COMPLETE_STATUS = {
1178
1178
  };
1179
1179
  var toContentPartStatus = (message, partIndex, part) => {
1180
1180
  if (message.role !== "assistant") return COMPLETE_STATUS;
1181
- const isLastPart = partIndex === message.content.length - 1;
1181
+ const isLastPart = partIndex === Math.max(0, message.content.length - 1);
1182
1182
  if (part.type !== "tool-call") {
1183
1183
  if ("reason" in message.status && message.status.reason === "tool-calls" && isLastPart)
1184
1184
  throw new Error(
@@ -1191,9 +1191,16 @@ var toContentPartStatus = (message, partIndex, part) => {
1191
1191
  }
1192
1192
  return message.status;
1193
1193
  };
1194
+ var EMPTY_CONTENT = Object.freeze({ type: "text", text: "" });
1194
1195
  var syncContentPart = ({ message }, useContentPart, partIndex) => {
1195
- const part = message.content[partIndex];
1196
- if (!part) return;
1196
+ let part = message.content[partIndex];
1197
+ if (!part) {
1198
+ if (message.content.length === 0 && partIndex === 0) {
1199
+ part = EMPTY_CONTENT;
1200
+ } else {
1201
+ return;
1202
+ }
1203
+ }
1197
1204
  const status = toContentPartStatus(message, partIndex, part);
1198
1205
  const currentState = useContentPart.getState();
1199
1206
  if (currentState.part === part && currentState.status === status) return;
@@ -1319,7 +1326,7 @@ var ContentPartPrimitiveText = forwardRef7(({ smooth = true, ...rest }, forwarde
1319
1326
  part: { text }
1320
1327
  } = useContentPartText();
1321
1328
  const smoothText = useSmooth(text, smooth);
1322
- return /* @__PURE__ */ jsx14(Primitive4.span, { "data-status": status, ...rest, ref: forwardedRef, children: smoothText });
1329
+ return /* @__PURE__ */ jsx14(Primitive4.span, { "data-status": status.type, ...rest, ref: forwardedRef, children: smoothText });
1323
1330
  });
1324
1331
  ContentPartPrimitiveText.displayName = "ContentPartPrimitive.Text";
1325
1332
 
@@ -1425,7 +1432,7 @@ var MessagePrimitiveContent = ({
1425
1432
  components
1426
1433
  }) => {
1427
1434
  const { useMessage } = useMessageContext();
1428
- const contentLength = useMessage((s) => s.message.content.length);
1435
+ const contentLength = useMessage((s) => s.message.content.length) || 1;
1429
1436
  return new Array(contentLength).fill(null).map((_, idx) => {
1430
1437
  const partIndex = idx;
1431
1438
  return /* @__PURE__ */ jsx16(
@@ -1729,7 +1736,7 @@ var useThreadViewportAutoScroll = ({
1729
1736
  const div = divRef.current;
1730
1737
  if (!div) return;
1731
1738
  const isAtBottom = useViewport.getState().isAtBottom;
1732
- const newIsAtBottom = div.scrollHeight - div.scrollTop <= div.clientHeight;
1739
+ const newIsAtBottom = div.scrollHeight - div.scrollTop <= div.clientHeight + 1;
1733
1740
  if (!newIsAtBottom && lastScrollTop.current < div.scrollTop) {
1734
1741
  } else {
1735
1742
  if (newIsAtBottom) {
@@ -1965,26 +1972,6 @@ var ThreadPrimitiveSuggestion = createActionButton(
1965
1972
  // src/runtimes/local/useLocalRuntime.tsx
1966
1973
  import { useInsertionEffect as useInsertionEffect3, useState as useState8 } from "react";
1967
1974
 
1968
- // src/utils/idUtils.tsx
1969
- import { customAlphabet } from "nanoid/non-secure";
1970
- var generateId = customAlphabet(
1971
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1972
- 7
1973
- );
1974
- var optimisticPrefix = "__optimistic__";
1975
- var generateOptimisticId = () => `${optimisticPrefix}${generateId()}`;
1976
-
1977
- // src/internal.ts
1978
- var internal_exports = {};
1979
- __export(internal_exports, {
1980
- BaseAssistantRuntime: () => BaseAssistantRuntime,
1981
- MessageRepository: () => MessageRepository,
1982
- ProxyConfigProvider: () => ProxyConfigProvider,
1983
- TooltipIconButton: () => TooltipIconButton,
1984
- generateId: () => generateId,
1985
- useSmooth: () => useSmooth
1986
- });
1987
-
1988
1975
  // src/runtimes/core/BaseAssistantRuntime.tsx
1989
1976
  var BaseAssistantRuntime = class {
1990
1977
  constructor(_thread) {
@@ -2008,101 +1995,25 @@ var BaseAssistantRuntime = class {
2008
1995
  };
2009
1996
  };
2010
1997
 
2011
- // src/ui/base/tooltip-icon-button.tsx
2012
- import { forwardRef as forwardRef17 } from "react";
2013
-
2014
- // src/ui/base/tooltip.tsx
2015
- import * as TooltipPrimitive from "@radix-ui/react-tooltip";
2016
-
2017
- // src/ui/utils/withDefaults.tsx
2018
- import {
2019
- forwardRef as forwardRef15
2020
- } from "react";
2021
- import classNames from "classnames";
2022
- import { jsx as jsx25 } from "react/jsx-runtime";
2023
- var withDefaultProps = ({
2024
- className,
2025
- ...defaultProps
2026
- }) => ({ className: classNameProp, ...props }) => {
2027
- return {
2028
- className: classNames(className, classNameProp),
2029
- ...defaultProps,
2030
- ...props
2031
- };
2032
- };
2033
- var withDefaults = (Component, defaultProps) => {
2034
- const getProps = withDefaultProps(defaultProps);
2035
- const WithDefaults = forwardRef15(
2036
- (props, ref) => {
2037
- const ComponentAsAny = Component;
2038
- return /* @__PURE__ */ jsx25(ComponentAsAny, { ...getProps(props), ref });
2039
- }
2040
- );
2041
- WithDefaults.displayName = "withDefaults(" + (typeof Component === "string" ? Component : Component.displayName) + ")";
2042
- return WithDefaults;
2043
- };
2044
-
2045
- // src/ui/base/tooltip.tsx
2046
- import { jsx as jsx26 } from "react/jsx-runtime";
2047
- var Tooltip = (props) => {
2048
- return /* @__PURE__ */ jsx26(TooltipPrimitive.Provider, { children: /* @__PURE__ */ jsx26(TooltipPrimitive.Root, { ...props }) });
2049
- };
2050
- Tooltip.displayName = "Tooltip";
2051
- var TooltipTrigger = TooltipPrimitive.Trigger;
2052
- var TooltipContent = withDefaults(TooltipPrimitive.Content, {
2053
- sideOffset: 4,
2054
- className: "aui-tooltip-content"
1998
+ // src/internal.ts
1999
+ var internal_exports = {};
2000
+ __export(internal_exports, {
2001
+ BaseAssistantRuntime: () => BaseAssistantRuntime,
2002
+ MessageRepository: () => MessageRepository,
2003
+ ProxyConfigProvider: () => ProxyConfigProvider,
2004
+ TooltipIconButton: () => TooltipIconButton,
2005
+ generateId: () => generateId,
2006
+ useSmooth: () => useSmooth
2055
2007
  });
2056
- TooltipContent.displayName = "TooltipContent";
2057
2008
 
2058
- // src/ui/base/button.tsx
2059
- import { cva } from "class-variance-authority";
2060
- import { Primitive as Primitive11 } from "@radix-ui/react-primitive";
2061
- import { forwardRef as forwardRef16 } from "react";
2062
- import { jsx as jsx27 } from "react/jsx-runtime";
2063
- var buttonVariants = cva("aui-button", {
2064
- variants: {
2065
- variant: {
2066
- default: "aui-button-primary",
2067
- outline: "aui-button-outline",
2068
- ghost: "aui-button-ghost"
2069
- },
2070
- size: {
2071
- default: "aui-button-medium",
2072
- icon: "aui-button-icon"
2073
- }
2074
- },
2075
- defaultVariants: {
2076
- variant: "default",
2077
- size: "default"
2078
- }
2079
- });
2080
- var Button = forwardRef16(
2081
- ({ className, variant, size, ...props }, ref) => {
2082
- return /* @__PURE__ */ jsx27(
2083
- Primitive11.button,
2084
- {
2085
- className: buttonVariants({ variant, size, className }),
2086
- ...props,
2087
- ref
2088
- }
2089
- );
2090
- }
2009
+ // src/utils/idUtils.tsx
2010
+ import { customAlphabet } from "nanoid/non-secure";
2011
+ var generateId = customAlphabet(
2012
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
2013
+ 7
2091
2014
  );
2092
- Button.displayName = "Button";
2093
-
2094
- // src/ui/base/tooltip-icon-button.tsx
2095
- import { jsx as jsx28, jsxs as jsxs4 } from "react/jsx-runtime";
2096
- var TooltipIconButton = forwardRef17(({ children, tooltip, side = "bottom", ...rest }, ref) => {
2097
- return /* @__PURE__ */ jsxs4(Tooltip, { children: [
2098
- /* @__PURE__ */ jsx28(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxs4(Button, { variant: "ghost", size: "icon", ...rest, ref, children: [
2099
- children,
2100
- /* @__PURE__ */ jsx28("span", { className: "aui-sr-only", children: tooltip })
2101
- ] }) }),
2102
- /* @__PURE__ */ jsx28(TooltipContent, { side, children: tooltip })
2103
- ] });
2104
- });
2105
- TooltipIconButton.displayName = "TooltipIconButton";
2015
+ var optimisticPrefix = "__optimistic__";
2016
+ var generateOptimisticId = () => `${optimisticPrefix}${generateId()}`;
2106
2017
 
2107
2018
  // src/runtimes/edge/converters/fromCoreMessage.ts
2108
2019
  var fromCoreMessages = (message) => {
@@ -2315,6 +2226,102 @@ var MessageRepository = class {
2315
2226
  }
2316
2227
  };
2317
2228
 
2229
+ // src/ui/base/tooltip-icon-button.tsx
2230
+ import { forwardRef as forwardRef17 } from "react";
2231
+
2232
+ // src/ui/base/tooltip.tsx
2233
+ import * as TooltipPrimitive from "@radix-ui/react-tooltip";
2234
+
2235
+ // src/ui/utils/withDefaults.tsx
2236
+ import {
2237
+ forwardRef as forwardRef15
2238
+ } from "react";
2239
+ import classNames from "classnames";
2240
+ import { jsx as jsx25 } from "react/jsx-runtime";
2241
+ var withDefaultProps = ({
2242
+ className,
2243
+ ...defaultProps
2244
+ }) => ({ className: classNameProp, ...props }) => {
2245
+ return {
2246
+ className: classNames(className, classNameProp),
2247
+ ...defaultProps,
2248
+ ...props
2249
+ };
2250
+ };
2251
+ var withDefaults = (Component, defaultProps) => {
2252
+ const getProps = withDefaultProps(defaultProps);
2253
+ const WithDefaults = forwardRef15(
2254
+ (props, ref) => {
2255
+ const ComponentAsAny = Component;
2256
+ return /* @__PURE__ */ jsx25(ComponentAsAny, { ...getProps(props), ref });
2257
+ }
2258
+ );
2259
+ WithDefaults.displayName = "withDefaults(" + (typeof Component === "string" ? Component : Component.displayName) + ")";
2260
+ return WithDefaults;
2261
+ };
2262
+
2263
+ // src/ui/base/tooltip.tsx
2264
+ import { jsx as jsx26 } from "react/jsx-runtime";
2265
+ var Tooltip = (props) => {
2266
+ return /* @__PURE__ */ jsx26(TooltipPrimitive.Provider, { children: /* @__PURE__ */ jsx26(TooltipPrimitive.Root, { ...props }) });
2267
+ };
2268
+ Tooltip.displayName = "Tooltip";
2269
+ var TooltipTrigger = TooltipPrimitive.Trigger;
2270
+ var TooltipContent = withDefaults(TooltipPrimitive.Content, {
2271
+ sideOffset: 4,
2272
+ className: "aui-tooltip-content"
2273
+ });
2274
+ TooltipContent.displayName = "TooltipContent";
2275
+
2276
+ // src/ui/base/button.tsx
2277
+ import { cva } from "class-variance-authority";
2278
+ import { Primitive as Primitive11 } from "@radix-ui/react-primitive";
2279
+ import { forwardRef as forwardRef16 } from "react";
2280
+ import { jsx as jsx27 } from "react/jsx-runtime";
2281
+ var buttonVariants = cva("aui-button", {
2282
+ variants: {
2283
+ variant: {
2284
+ default: "aui-button-primary",
2285
+ outline: "aui-button-outline",
2286
+ ghost: "aui-button-ghost"
2287
+ },
2288
+ size: {
2289
+ default: "aui-button-medium",
2290
+ icon: "aui-button-icon"
2291
+ }
2292
+ },
2293
+ defaultVariants: {
2294
+ variant: "default",
2295
+ size: "default"
2296
+ }
2297
+ });
2298
+ var Button = forwardRef16(
2299
+ ({ className, variant, size, ...props }, ref) => {
2300
+ return /* @__PURE__ */ jsx27(
2301
+ Primitive11.button,
2302
+ {
2303
+ className: buttonVariants({ variant, size, className }),
2304
+ ...props,
2305
+ ref
2306
+ }
2307
+ );
2308
+ }
2309
+ );
2310
+ Button.displayName = "Button";
2311
+
2312
+ // src/ui/base/tooltip-icon-button.tsx
2313
+ import { jsx as jsx28, jsxs as jsxs4 } from "react/jsx-runtime";
2314
+ var TooltipIconButton = forwardRef17(({ children, tooltip, side = "bottom", ...rest }, ref) => {
2315
+ return /* @__PURE__ */ jsxs4(Tooltip, { children: [
2316
+ /* @__PURE__ */ jsx28(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxs4(Button, { variant: "ghost", size: "icon", ...rest, ref, children: [
2317
+ children,
2318
+ /* @__PURE__ */ jsx28("span", { className: "aui-sr-only", children: tooltip })
2319
+ ] }) }),
2320
+ /* @__PURE__ */ jsx28(TooltipContent, { side, children: tooltip })
2321
+ ] });
2322
+ });
2323
+ TooltipIconButton.displayName = "TooltipIconButton";
2324
+
2318
2325
  // src/runtimes/edge/converters/toLanguageModelMessages.ts
2319
2326
  var assistantMessageSplitter = () => {
2320
2327
  const stash = [];
@@ -3347,31 +3354,10 @@ var useEdgeRuntime = ({
3347
3354
  return useLocalRuntime(adapter, { initialMessages });
3348
3355
  };
3349
3356
 
3350
- // src/runtimes/local/LocalRuntime.tsx
3357
+ // src/runtimes/local/shouldContinue.tsx
3351
3358
  var shouldContinue = (result) => result.status?.type === "requires-action" && result.status.reason === "tool-calls" && result.content.every((c) => c.type !== "tool-call" || !!c.result);
3352
- var LocalRuntime = class extends BaseAssistantRuntime {
3353
- _proxyConfigProvider;
3354
- constructor(adapter, options) {
3355
- const proxyConfigProvider = new ProxyConfigProvider();
3356
- super(new LocalThreadRuntime(proxyConfigProvider, adapter, options));
3357
- this._proxyConfigProvider = proxyConfigProvider;
3358
- }
3359
- set adapter(adapter) {
3360
- this.thread.adapter = adapter;
3361
- }
3362
- registerModelConfigProvider(provider) {
3363
- return this._proxyConfigProvider.registerModelConfigProvider(provider);
3364
- }
3365
- switchToThread(threadId) {
3366
- if (threadId) {
3367
- throw new Error("LocalRuntime does not yet support switching threads");
3368
- }
3369
- return this.thread = new LocalThreadRuntime(
3370
- this._proxyConfigProvider,
3371
- this.thread.adapter
3372
- );
3373
- }
3374
- };
3359
+
3360
+ // src/runtimes/local/LocalThreadRuntime.tsx
3375
3361
  var CAPABILITIES = Object.freeze({
3376
3362
  edit: true,
3377
3363
  reload: true,
@@ -3431,7 +3417,7 @@ var LocalThreadRuntime = class {
3431
3417
  id,
3432
3418
  role: "assistant",
3433
3419
  status: { type: "running" },
3434
- content: [{ type: "text", text: "" }],
3420
+ content: [],
3435
3421
  createdAt: /* @__PURE__ */ new Date()
3436
3422
  };
3437
3423
  do {
@@ -3543,6 +3529,31 @@ var LocalThreadRuntime = class {
3543
3529
  }
3544
3530
  };
3545
3531
 
3532
+ // src/runtimes/local/LocalRuntime.tsx
3533
+ var LocalRuntime = class extends BaseAssistantRuntime {
3534
+ _proxyConfigProvider;
3535
+ constructor(adapter, options) {
3536
+ const proxyConfigProvider = new ProxyConfigProvider();
3537
+ super(new LocalThreadRuntime(proxyConfigProvider, adapter, options));
3538
+ this._proxyConfigProvider = proxyConfigProvider;
3539
+ }
3540
+ set adapter(adapter) {
3541
+ this.thread.adapter = adapter;
3542
+ }
3543
+ registerModelConfigProvider(provider) {
3544
+ return this._proxyConfigProvider.registerModelConfigProvider(provider);
3545
+ }
3546
+ switchToThread(threadId) {
3547
+ if (threadId) {
3548
+ throw new Error("LocalRuntime does not yet support switching threads");
3549
+ }
3550
+ return this.thread = new LocalThreadRuntime(
3551
+ this._proxyConfigProvider,
3552
+ this.thread.adapter
3553
+ );
3554
+ }
3555
+ };
3556
+
3546
3557
  // src/runtimes/local/useLocalRuntime.tsx
3547
3558
  var useLocalRuntime = (adapter, options) => {
3548
3559
  const [runtime] = useState8(() => new LocalRuntime(adapter, options));