@assistant-ui/react 0.12.7 → 0.12.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/client/ChainOfThoughtClient.d.ts +15 -0
  2. package/dist/client/ChainOfThoughtClient.d.ts.map +1 -0
  3. package/dist/client/ChainOfThoughtClient.js +21 -0
  4. package/dist/client/ChainOfThoughtClient.js.map +1 -0
  5. package/dist/client/index.d.ts +1 -0
  6. package/dist/client/index.d.ts.map +1 -1
  7. package/dist/client/index.js +1 -0
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/context/providers/ChainOfThoughtByIndicesProvider.d.ts +6 -0
  10. package/dist/context/providers/ChainOfThoughtByIndicesProvider.d.ts.map +1 -0
  11. package/dist/context/providers/ChainOfThoughtByIndicesProvider.js +21 -0
  12. package/dist/context/providers/ChainOfThoughtByIndicesProvider.js.map +1 -0
  13. package/dist/context/providers/ChainOfThoughtPartByIndexProvider.d.ts +5 -0
  14. package/dist/context/providers/ChainOfThoughtPartByIndexProvider.d.ts.map +1 -0
  15. package/dist/context/providers/ChainOfThoughtPartByIndexProvider.js +14 -0
  16. package/dist/context/providers/ChainOfThoughtPartByIndexProvider.js.map +1 -0
  17. package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.d.ts +28 -0
  18. package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.d.ts.map +1 -0
  19. package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.js +27 -0
  20. package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.js.map +1 -0
  21. package/dist/primitives/chainOfThought/ChainOfThoughtParts.d.ts +39 -0
  22. package/dist/primitives/chainOfThought/ChainOfThoughtParts.d.ts.map +1 -0
  23. package/dist/primitives/chainOfThought/ChainOfThoughtParts.js +40 -0
  24. package/dist/primitives/chainOfThought/ChainOfThoughtParts.js.map +1 -0
  25. package/dist/primitives/chainOfThought/ChainOfThoughtRoot.d.ts +28 -0
  26. package/dist/primitives/chainOfThought/ChainOfThoughtRoot.d.ts.map +1 -0
  27. package/dist/primitives/chainOfThought/ChainOfThoughtRoot.js +25 -0
  28. package/dist/primitives/chainOfThought/ChainOfThoughtRoot.js.map +1 -0
  29. package/dist/primitives/chainOfThought/index.d.ts +4 -0
  30. package/dist/primitives/chainOfThought/index.d.ts.map +1 -0
  31. package/dist/primitives/chainOfThought/index.js +4 -0
  32. package/dist/primitives/chainOfThought/index.js.map +1 -0
  33. package/dist/primitives/index.d.ts +1 -0
  34. package/dist/primitives/index.d.ts.map +1 -1
  35. package/dist/primitives/index.js +1 -0
  36. package/dist/primitives/index.js.map +1 -1
  37. package/dist/primitives/message/MessageParts.d.ts +145 -109
  38. package/dist/primitives/message/MessageParts.d.ts.map +1 -1
  39. package/dist/primitives/message/MessageParts.js +48 -23
  40. package/dist/primitives/message/MessageParts.js.map +1 -1
  41. package/dist/types/scopes/chainOfThought.d.ts +40 -0
  42. package/dist/types/scopes/chainOfThought.d.ts.map +1 -0
  43. package/dist/types/scopes/chainOfThought.js +2 -0
  44. package/dist/types/scopes/chainOfThought.js.map +1 -0
  45. package/dist/types/scopes/index.d.ts +1 -0
  46. package/dist/types/scopes/index.d.ts.map +1 -1
  47. package/dist/types/scopes/part.d.ts +6 -0
  48. package/dist/types/scopes/part.d.ts.map +1 -1
  49. package/dist/types/store-augmentation.d.ts +2 -0
  50. package/dist/types/store-augmentation.d.ts.map +1 -1
  51. package/package.json +7 -7
  52. package/src/client/ChainOfThoughtClient.ts +43 -0
  53. package/src/client/index.ts +1 -0
  54. package/src/context/providers/ChainOfThoughtByIndicesProvider.tsx +36 -0
  55. package/src/context/providers/ChainOfThoughtPartByIndexProvider.tsx +20 -0
  56. package/src/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.tsx +51 -0
  57. package/src/primitives/chainOfThought/ChainOfThoughtParts.tsx +89 -0
  58. package/src/primitives/chainOfThought/ChainOfThoughtRoot.tsx +36 -0
  59. package/src/primitives/chainOfThought/index.ts +14 -0
  60. package/src/primitives/index.ts +1 -0
  61. package/src/primitives/message/MessageParts.tsx +214 -142
  62. package/src/types/scopes/chainOfThought.ts +42 -0
  63. package/src/types/scopes/index.ts +7 -0
  64. package/src/types/scopes/part.ts +11 -6
  65. package/src/types/store-augmentation.ts +2 -0
@@ -0,0 +1,43 @@
1
+ import { resource, tapMemo, tapState } from "@assistant-ui/tap";
2
+ import { type ClientOutput } from "@assistant-ui/store";
3
+ import type {
4
+ ChainOfThoughtState,
5
+ ChainOfThoughtPart,
6
+ } from "../types/scopes/chainOfThought";
7
+ import type { MessagePartStatus } from "../types/AssistantTypes";
8
+ import type { PartMethods } from "../types/scopes/part";
9
+
10
+ const COMPLETE_STATUS: MessagePartStatus = Object.freeze({
11
+ type: "complete",
12
+ });
13
+
14
+ export const ChainOfThoughtClient = resource(
15
+ ({
16
+ parts,
17
+ getMessagePart,
18
+ }: {
19
+ parts: readonly ChainOfThoughtPart[];
20
+ getMessagePart: (selector: { index: number }) => PartMethods;
21
+ }): ClientOutput<"chainOfThought"> => {
22
+ const [collapsed, setCollapsed] = tapState(true);
23
+
24
+ const status = tapMemo(() => {
25
+ const lastPart = parts[parts.length - 1];
26
+ return lastPart?.status ?? COMPLETE_STATUS;
27
+ }, [parts]);
28
+
29
+ const state = tapMemo<ChainOfThoughtState>(
30
+ () => ({ parts, collapsed, status }),
31
+ [parts, collapsed, status],
32
+ );
33
+
34
+ return {
35
+ state,
36
+ methods: {
37
+ getState: () => state,
38
+ setCollapsed,
39
+ part: getMessagePart,
40
+ },
41
+ };
42
+ },
43
+ );
@@ -10,3 +10,4 @@ export {
10
10
  InMemoryThreadList,
11
11
  type InMemoryThreadListProps,
12
12
  } from "./InMemoryThreadList";
13
+ export { ChainOfThoughtClient } from "./ChainOfThoughtClient";
@@ -0,0 +1,36 @@
1
+ "use client";
2
+
3
+ import { type FC, type PropsWithChildren } from "react";
4
+ import { useAui, useAuiState, AuiProvider } from "@assistant-ui/store";
5
+ import { ChainOfThoughtClient } from "../../client/ChainOfThoughtClient";
6
+ import type { ChainOfThoughtPart } from "../../types/scopes/chainOfThought";
7
+
8
+ export const ChainOfThoughtByIndicesProvider: FC<
9
+ PropsWithChildren<{
10
+ startIndex: number;
11
+ endIndex: number;
12
+ }>
13
+ > = ({ startIndex, endIndex, children }) => {
14
+ const parts = useAuiState(({ message }) => message.parts).slice(
15
+ startIndex,
16
+ endIndex + 1,
17
+ ) as ChainOfThoughtPart[];
18
+
19
+ const parentAui = useAui();
20
+
21
+ const aui = useAui({
22
+ chainOfThought: ChainOfThoughtClient({
23
+ parts,
24
+ getMessagePart: ({ index }) => {
25
+ if (index < 0 || index >= parts.length) {
26
+ throw new Error(
27
+ `ChainOfThought part index ${index} is out of bounds (0..${parts.length - 1})`,
28
+ );
29
+ }
30
+ return parentAui.message().part({ index: startIndex + index });
31
+ },
32
+ }),
33
+ });
34
+
35
+ return <AuiProvider value={aui}>{children}</AuiProvider>;
36
+ };
@@ -0,0 +1,20 @@
1
+ "use client";
2
+
3
+ import { type FC, type PropsWithChildren } from "react";
4
+ import { useAui, AuiProvider, Derived } from "@assistant-ui/store";
5
+
6
+ export const ChainOfThoughtPartByIndexProvider: FC<
7
+ PropsWithChildren<{
8
+ index: number;
9
+ }>
10
+ > = ({ index, children }) => {
11
+ const aui = useAui({
12
+ part: Derived({
13
+ source: "chainOfThought",
14
+ query: { type: "index", index },
15
+ get: (aui) => aui.chainOfThought().part({ index }),
16
+ }),
17
+ });
18
+
19
+ return <AuiProvider value={aui}>{children}</AuiProvider>;
20
+ };
@@ -0,0 +1,51 @@
1
+ "use client";
2
+
3
+ import {
4
+ ActionButtonElement,
5
+ ActionButtonProps,
6
+ createActionButton,
7
+ } from "../../utils/createActionButton";
8
+ import { useCallback } from "react";
9
+ import { useAuiState, useAui } from "@assistant-ui/store";
10
+
11
+ const useChainOfThoughtAccordionTrigger = () => {
12
+ const aui = useAui();
13
+ const collapsed = useAuiState(
14
+ ({ chainOfThought }) => chainOfThought.collapsed,
15
+ );
16
+
17
+ const callback = useCallback(() => {
18
+ aui.chainOfThought().setCollapsed(!collapsed);
19
+ }, [aui, collapsed]);
20
+
21
+ return callback;
22
+ };
23
+
24
+ export namespace ChainOfThoughtPrimitiveAccordionTrigger {
25
+ export type Element = ActionButtonElement;
26
+ /**
27
+ * Props for the ChainOfThoughtPrimitive.AccordionTrigger component.
28
+ * Inherits all button element props and action button functionality.
29
+ */
30
+ export type Props = ActionButtonProps<
31
+ typeof useChainOfThoughtAccordionTrigger
32
+ >;
33
+ }
34
+
35
+ /**
36
+ * A button component that toggles the collapsed state of the chain of thought accordion.
37
+ *
38
+ * This component automatically handles the toggle functionality, expanding or collapsing
39
+ * the chain of thought parts when clicked.
40
+ *
41
+ * @example
42
+ * ```tsx
43
+ * <ChainOfThoughtPrimitive.AccordionTrigger>
44
+ * Toggle Reasoning
45
+ * </ChainOfThoughtPrimitive.AccordionTrigger>
46
+ * ```
47
+ */
48
+ export const ChainOfThoughtPrimitiveAccordionTrigger = createActionButton(
49
+ "ChainOfThoughtPrimitive.AccordionTrigger",
50
+ useChainOfThoughtAccordionTrigger,
51
+ );
@@ -0,0 +1,89 @@
1
+ "use client";
2
+
3
+ import {
4
+ type ComponentType,
5
+ type FC,
6
+ type PropsWithChildren,
7
+ useMemo,
8
+ } from "react";
9
+ import { useAuiState } from "@assistant-ui/store";
10
+ import { ChainOfThoughtPartByIndexProvider } from "../../context/providers/ChainOfThoughtPartByIndexProvider";
11
+ import { MessagePartComponent } from "../message/MessageParts";
12
+ import type {
13
+ ReasoningMessagePartComponent,
14
+ ToolCallMessagePartComponent,
15
+ } from "../../types/MessagePartComponentTypes";
16
+
17
+ export namespace ChainOfThoughtPrimitiveParts {
18
+ export type Props = {
19
+ /**
20
+ * Component configuration for rendering chain of thought parts.
21
+ */
22
+ components?: {
23
+ /** Component for rendering reasoning parts */
24
+ Reasoning?: ReasoningMessagePartComponent | undefined;
25
+ /** Fallback component for tool-call parts */
26
+ tools?: {
27
+ Fallback?: ToolCallMessagePartComponent | undefined;
28
+ };
29
+ /** Layout component to wrap the rendered parts when expanded */
30
+ Layout?: ComponentType<PropsWithChildren> | undefined;
31
+ };
32
+ };
33
+ }
34
+
35
+ /**
36
+ * Renders the parts within a chain of thought, with support for collapsed/expanded states.
37
+ *
38
+ * When collapsed, no parts are shown. When expanded, all parts are rendered
39
+ * using the provided component configuration through the part scope mechanism.
40
+ *
41
+ * @example
42
+ * ```tsx
43
+ * <ChainOfThoughtPrimitive.Parts
44
+ * components={{
45
+ * Reasoning: ({ text }) => <p className="reasoning">{text}</p>,
46
+ * tools: {
47
+ * Fallback: ({ toolName }) => <div>Tool: {toolName}</div>
48
+ * }
49
+ * }}
50
+ * />
51
+ * ```
52
+ */
53
+ export const ChainOfThoughtPrimitiveParts: FC<
54
+ ChainOfThoughtPrimitiveParts.Props
55
+ > = ({ components }) => {
56
+ const partsLength = useAuiState(
57
+ ({ chainOfThought }) => chainOfThought.parts.length,
58
+ );
59
+
60
+ const messageComponents = useMemo(
61
+ () => ({
62
+ Reasoning: components?.Reasoning,
63
+ tools: {
64
+ Fallback: components?.tools?.Fallback,
65
+ },
66
+ }),
67
+ [components?.Reasoning, components?.tools?.Fallback],
68
+ );
69
+
70
+ const Layout = components?.Layout;
71
+
72
+ const elements = useMemo(() => {
73
+ return Array.from({ length: partsLength }, (_, index) => (
74
+ <ChainOfThoughtPartByIndexProvider key={index} index={index}>
75
+ {Layout ? (
76
+ <Layout>
77
+ <MessagePartComponent components={messageComponents} />
78
+ </Layout>
79
+ ) : (
80
+ <MessagePartComponent components={messageComponents} />
81
+ )}
82
+ </ChainOfThoughtPartByIndexProvider>
83
+ ));
84
+ }, [partsLength, messageComponents, Layout]);
85
+
86
+ return <>{elements}</>;
87
+ };
88
+
89
+ ChainOfThoughtPrimitiveParts.displayName = "ChainOfThoughtPrimitive.Parts";
@@ -0,0 +1,36 @@
1
+ "use client";
2
+
3
+ import { Primitive } from "@radix-ui/react-primitive";
4
+ import { type ComponentRef, forwardRef, ComponentPropsWithoutRef } from "react";
5
+
6
+ type PrimitiveDivProps = ComponentPropsWithoutRef<typeof Primitive.div>;
7
+
8
+ export namespace ChainOfThoughtPrimitiveRoot {
9
+ export type Element = ComponentRef<typeof Primitive.div>;
10
+ export type Props = PrimitiveDivProps;
11
+ }
12
+
13
+ /**
14
+ * The root container for chain of thought components.
15
+ *
16
+ * This component provides a wrapper for chain of thought content,
17
+ * including reasoning and tool-call parts that can be collapsed in an accordion.
18
+ *
19
+ * @example
20
+ * ```tsx
21
+ * <ChainOfThoughtPrimitive.Root>
22
+ * <ChainOfThoughtPrimitive.AccordionTrigger>
23
+ * Toggle reasoning
24
+ * </ChainOfThoughtPrimitive.AccordionTrigger>
25
+ * <ChainOfThoughtPrimitive.Parts />
26
+ * </ChainOfThoughtPrimitive.Root>
27
+ * ```
28
+ */
29
+ export const ChainOfThoughtPrimitiveRoot = forwardRef<
30
+ ChainOfThoughtPrimitiveRoot.Element,
31
+ ChainOfThoughtPrimitiveRoot.Props
32
+ >((props, ref) => {
33
+ return <Primitive.div {...props} ref={ref} />;
34
+ });
35
+
36
+ ChainOfThoughtPrimitiveRoot.displayName = "ChainOfThoughtPrimitive.Root";
@@ -0,0 +1,14 @@
1
+ export {
2
+ ChainOfThoughtPrimitiveRoot as Root,
3
+ type ChainOfThoughtPrimitiveRoot as RootPrimitive,
4
+ } from "./ChainOfThoughtRoot";
5
+
6
+ export {
7
+ ChainOfThoughtPrimitiveAccordionTrigger as AccordionTrigger,
8
+ type ChainOfThoughtPrimitiveAccordionTrigger as AccordionTriggerPrimitive,
9
+ } from "./ChainOfThoughtAccordionTrigger";
10
+
11
+ export {
12
+ ChainOfThoughtPrimitiveParts as Parts,
13
+ type ChainOfThoughtPrimitiveParts as PartsPrimitive,
14
+ } from "./ChainOfThoughtParts";
@@ -3,6 +3,7 @@ export * as ActionBarMorePrimitive from "./actionBarMore";
3
3
  export * as AssistantModalPrimitive from "./assistantModal";
4
4
  export * as AttachmentPrimitive from "./attachment";
5
5
  export * as BranchPickerPrimitive from "./branchPicker";
6
+ export * as ChainOfThoughtPrimitive from "./chainOfThought";
6
7
  export * as ComposerPrimitive from "./composer";
7
8
  export * as MessagePartPrimitive from "./messagePart";
8
9
  export * as ErrorPrimitive from "./error";