@assistant-ui/vue 0.0.0 → 0.0.2

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 (231) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -2
  3. package/dist/AuiIf.d.ts +27 -0
  4. package/dist/AuiIf.d.ts.map +1 -0
  5. package/dist/AuiIf.js +30 -0
  6. package/dist/AuiIf.js.map +1 -0
  7. package/dist/AuiProvider.d.ts +41 -0
  8. package/dist/AuiProvider.d.ts.map +1 -0
  9. package/dist/AuiProvider.js +61 -0
  10. package/dist/AuiProvider.js.map +1 -0
  11. package/dist/context.d.ts +10 -0
  12. package/dist/context.d.ts.map +1 -0
  13. package/dist/context.js +17 -0
  14. package/dist/context.js.map +1 -0
  15. package/dist/index.d.ts +34 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +35 -0
  18. package/dist/primitives/AttachmentByIndexProvider.d.ts +30 -0
  19. package/dist/primitives/AttachmentByIndexProvider.d.ts.map +1 -0
  20. package/dist/primitives/AttachmentByIndexProvider.js +63 -0
  21. package/dist/primitives/AttachmentByIndexProvider.js.map +1 -0
  22. package/dist/primitives/ChainOfThoughtPrimitiveAccordionTrigger.d.ts +11 -0
  23. package/dist/primitives/ChainOfThoughtPrimitiveAccordionTrigger.d.ts.map +1 -0
  24. package/dist/primitives/ChainOfThoughtPrimitiveAccordionTrigger.js +31 -0
  25. package/dist/primitives/ChainOfThoughtPrimitiveAccordionTrigger.js.map +1 -0
  26. package/dist/primitives/ChainOfThoughtPrimitiveParts.d.ts +19 -0
  27. package/dist/primitives/ChainOfThoughtPrimitiveParts.d.ts.map +1 -0
  28. package/dist/primitives/ChainOfThoughtPrimitiveParts.js +85 -0
  29. package/dist/primitives/ChainOfThoughtPrimitiveParts.js.map +1 -0
  30. package/dist/primitives/ComposerPrimitiveCancel.d.ts +12 -0
  31. package/dist/primitives/ComposerPrimitiveCancel.d.ts.map +1 -0
  32. package/dist/primitives/ComposerPrimitiveCancel.js +33 -0
  33. package/dist/primitives/ComposerPrimitiveCancel.js.map +1 -0
  34. package/dist/primitives/ComposerPrimitiveInput.d.ts +26 -0
  35. package/dist/primitives/ComposerPrimitiveInput.d.ts.map +1 -0
  36. package/dist/primitives/ComposerPrimitiveInput.js +57 -0
  37. package/dist/primitives/ComposerPrimitiveInput.js.map +1 -0
  38. package/dist/primitives/ComposerPrimitiveSend.d.ts +13 -0
  39. package/dist/primitives/ComposerPrimitiveSend.d.ts.map +1 -0
  40. package/dist/primitives/ComposerPrimitiveSend.js +31 -0
  41. package/dist/primitives/ComposerPrimitiveSend.js.map +1 -0
  42. package/dist/primitives/MessageByIdProvider.d.ts +23 -0
  43. package/dist/primitives/MessageByIdProvider.d.ts.map +1 -0
  44. package/dist/primitives/MessageByIdProvider.js +70 -0
  45. package/dist/primitives/MessageByIdProvider.js.map +1 -0
  46. package/dist/primitives/MessagePrimitiveParts.d.ts +51 -0
  47. package/dist/primitives/MessagePrimitiveParts.d.ts.map +1 -0
  48. package/dist/primitives/MessagePrimitiveParts.js +93 -0
  49. package/dist/primitives/MessagePrimitiveParts.js.map +1 -0
  50. package/dist/primitives/PartByIndexProvider.d.ts +21 -0
  51. package/dist/primitives/PartByIndexProvider.d.ts.map +1 -0
  52. package/dist/primitives/PartByIndexProvider.js +50 -0
  53. package/dist/primitives/PartByIndexProvider.js.map +1 -0
  54. package/dist/primitives/ThreadListItemPrimitiveRoot.d.ts +7 -0
  55. package/dist/primitives/ThreadListItemPrimitiveRoot.d.ts.map +1 -0
  56. package/dist/primitives/ThreadListItemPrimitiveRoot.js +36 -0
  57. package/dist/primitives/ThreadListItemPrimitiveRoot.js.map +1 -0
  58. package/dist/primitives/ThreadListItemPrimitiveTrigger.d.ts +14 -0
  59. package/dist/primitives/ThreadListItemPrimitiveTrigger.d.ts.map +1 -0
  60. package/dist/primitives/ThreadListItemPrimitiveTrigger.js +58 -0
  61. package/dist/primitives/ThreadListItemPrimitiveTrigger.js.map +1 -0
  62. package/dist/primitives/ThreadListPrimitiveRoot.d.ts +7 -0
  63. package/dist/primitives/ThreadListPrimitiveRoot.d.ts.map +1 -0
  64. package/dist/primitives/ThreadListPrimitiveRoot.js +16 -0
  65. package/dist/primitives/ThreadListPrimitiveRoot.js.map +1 -0
  66. package/dist/primitives/ThreadPrimitiveMessages.d.ts +21 -0
  67. package/dist/primitives/ThreadPrimitiveMessages.d.ts.map +1 -0
  68. package/dist/primitives/ThreadPrimitiveMessages.js +35 -0
  69. package/dist/primitives/ThreadPrimitiveMessages.js.map +1 -0
  70. package/dist/primitives/ThreadPrimitiveScrollToBottom.d.ts +25 -0
  71. package/dist/primitives/ThreadPrimitiveScrollToBottom.d.ts.map +1 -0
  72. package/dist/primitives/ThreadPrimitiveScrollToBottom.js +43 -0
  73. package/dist/primitives/ThreadPrimitiveScrollToBottom.js.map +1 -0
  74. package/dist/primitives/ThreadPrimitiveViewport.d.ts +57 -0
  75. package/dist/primitives/ThreadPrimitiveViewport.d.ts.map +1 -0
  76. package/dist/primitives/ThreadPrimitiveViewport.js +99 -0
  77. package/dist/primitives/ThreadPrimitiveViewport.js.map +1 -0
  78. package/dist/primitives/ThreadPrimitiveViewportFooter.d.ts +13 -0
  79. package/dist/primitives/ThreadPrimitiveViewportFooter.d.ts.map +1 -0
  80. package/dist/primitives/ThreadPrimitiveViewportFooter.js +46 -0
  81. package/dist/primitives/ThreadPrimitiveViewportFooter.js.map +1 -0
  82. package/dist/primitives/actionBar.d.ts +41 -0
  83. package/dist/primitives/actionBar.d.ts.map +1 -0
  84. package/dist/primitives/actionBar.js +110 -0
  85. package/dist/primitives/actionBar.js.map +1 -0
  86. package/dist/primitives/attachment.d.ts +27 -0
  87. package/dist/primitives/attachment.d.ts.map +1 -0
  88. package/dist/primitives/attachment.js +63 -0
  89. package/dist/primitives/attachment.js.map +1 -0
  90. package/dist/primitives/attrDisabled.d.ts +2 -0
  91. package/dist/primitives/attrDisabled.d.ts.map +1 -0
  92. package/dist/primitives/attrDisabled.js +6 -0
  93. package/dist/primitives/attrDisabled.js.map +1 -0
  94. package/dist/primitives/branchPicker.d.ts +18 -0
  95. package/dist/primitives/branchPicker.d.ts.map +1 -0
  96. package/dist/primitives/branchPicker.js +48 -0
  97. package/dist/primitives/branchPicker.js.map +1 -0
  98. package/dist/primitives/composerAttachments.d.ts +57 -0
  99. package/dist/primitives/composerAttachments.d.ts.map +1 -0
  100. package/dist/primitives/composerAttachments.js +132 -0
  101. package/dist/primitives/composerAttachments.js.map +1 -0
  102. package/dist/primitives/error.d.ts +15 -0
  103. package/dist/primitives/error.d.ts.map +1 -0
  104. package/dist/primitives/error.js +29 -0
  105. package/dist/primitives/error.js.map +1 -0
  106. package/dist/primitives/lastValidCache.d.ts +6 -0
  107. package/dist/primitives/lastValidCache.d.ts.map +1 -0
  108. package/dist/primitives/lastValidCache.js +9 -0
  109. package/dist/primitives/lastValidCache.js.map +1 -0
  110. package/dist/primitives/message.d.ts +12 -0
  111. package/dist/primitives/message.d.ts.map +1 -0
  112. package/dist/primitives/message.js +44 -0
  113. package/dist/primitives/message.js.map +1 -0
  114. package/dist/primitives/messageAttachments.d.ts +12 -0
  115. package/dist/primitives/messageAttachments.d.ts.map +1 -0
  116. package/dist/primitives/messageAttachments.js +27 -0
  117. package/dist/primitives/messageAttachments.js.map +1 -0
  118. package/dist/primitives/stableKeys.d.ts +5 -0
  119. package/dist/primitives/stableKeys.d.ts.map +1 -0
  120. package/dist/primitives/stableKeys.js +14 -0
  121. package/dist/primitives/stableKeys.js.map +1 -0
  122. package/dist/primitives/suggestions.d.ts +66 -0
  123. package/dist/primitives/suggestions.d.ts.map +1 -0
  124. package/dist/primitives/suggestions.js +137 -0
  125. package/dist/primitives/suggestions.js.map +1 -0
  126. package/dist/primitives/thread.d.ts +11 -0
  127. package/dist/primitives/thread.d.ts.map +1 -0
  128. package/dist/primitives/thread.js +37 -0
  129. package/dist/primitives/thread.js.map +1 -0
  130. package/dist/primitives/threadList.d.ts +82 -0
  131. package/dist/primitives/threadList.d.ts.map +1 -0
  132. package/dist/primitives/threadList.js +128 -0
  133. package/dist/primitives/threadList.js.map +1 -0
  134. package/dist/primitives/threadListFocusGroup.d.ts +14 -0
  135. package/dist/primitives/threadListFocusGroup.d.ts.map +1 -0
  136. package/dist/primitives/threadListFocusGroup.js +32 -0
  137. package/dist/primitives/threadListFocusGroup.js.map +1 -0
  138. package/dist/primitives/threadListStructural.d.ts +29 -0
  139. package/dist/primitives/threadListStructural.d.ts.map +1 -0
  140. package/dist/primitives/threadListStructural.js +55 -0
  141. package/dist/primitives/threadListStructural.js.map +1 -0
  142. package/dist/primitives/useComposerSendState.d.ts +6 -0
  143. package/dist/primitives/useComposerSendState.d.ts.map +1 -0
  144. package/dist/primitives/useComposerSendState.js +15 -0
  145. package/dist/primitives/useComposerSendState.js.map +1 -0
  146. package/dist/primitives/viewportContext.d.ts +12 -0
  147. package/dist/primitives/viewportContext.d.ts.map +1 -0
  148. package/dist/primitives/viewportContext.js +6 -0
  149. package/dist/primitives/viewportContext.js.map +1 -0
  150. package/dist/useAui.d.ts +15 -0
  151. package/dist/useAui.d.ts.map +1 -0
  152. package/dist/useAui.js +21 -0
  153. package/dist/useAui.js.map +1 -0
  154. package/dist/useAuiEvent.d.ts +17 -0
  155. package/dist/useAuiEvent.d.ts.map +1 -0
  156. package/dist/useAuiEvent.js +52 -0
  157. package/dist/useAuiEvent.js.map +1 -0
  158. package/dist/useAuiState.d.ts +23 -0
  159. package/dist/useAuiState.d.ts.map +1 -0
  160. package/dist/useAuiState.js +42 -0
  161. package/dist/useAuiState.js.map +1 -0
  162. package/dist/useScrollLock.d.ts +3 -0
  163. package/dist/useScrollLock.d.ts.map +1 -0
  164. package/dist/useScrollLock.js +53 -0
  165. package/dist/useScrollLock.js.map +1 -0
  166. package/package.json +57 -6
  167. package/src/AuiIf.ts +34 -0
  168. package/src/AuiProvider.ts +90 -0
  169. package/src/__tests__/AuiIf.test.ts +54 -0
  170. package/src/__tests__/AuiProvider.test.ts +266 -0
  171. package/src/__tests__/byindex-shrink-race.test.ts +411 -0
  172. package/src/__tests__/dist-graph.test.ts +49 -0
  173. package/src/__tests__/fixtures.ts +51 -0
  174. package/src/__tests__/primitives-actionbar.test.ts +469 -0
  175. package/src/__tests__/primitives-attachment.test.ts +493 -0
  176. package/src/__tests__/primitives-content.test.ts +829 -0
  177. package/src/__tests__/primitives-keyed-items.test.ts +270 -0
  178. package/src/__tests__/primitives-keyed-messages.test.ts +264 -0
  179. package/src/__tests__/primitives-structural.test.ts +573 -0
  180. package/src/__tests__/primitives-suggestions.test.ts +374 -0
  181. package/src/__tests__/primitives-threadlist.test.ts +443 -0
  182. package/src/__tests__/primitives-tool-ui.test.ts +579 -0
  183. package/src/__tests__/primitives.test.ts +408 -0
  184. package/src/__tests__/useAuiEvent.test.ts +159 -0
  185. package/src/__tests__/useAuiState.test.ts +124 -0
  186. package/src/context.ts +31 -0
  187. package/src/index.ts +87 -0
  188. package/src/primitives/AttachmentByIndexProvider.ts +88 -0
  189. package/src/primitives/ChainOfThoughtPrimitiveAccordionTrigger.test.ts +105 -0
  190. package/src/primitives/ChainOfThoughtPrimitiveAccordionTrigger.ts +38 -0
  191. package/src/primitives/ChainOfThoughtPrimitiveParts.test.ts +254 -0
  192. package/src/primitives/ChainOfThoughtPrimitiveParts.ts +112 -0
  193. package/src/primitives/ComposerPrimitiveCancel.ts +41 -0
  194. package/src/primitives/ComposerPrimitiveInput.ts +70 -0
  195. package/src/primitives/ComposerPrimitiveSend.ts +39 -0
  196. package/src/primitives/MessageByIdProvider.ts +89 -0
  197. package/src/primitives/MessagePrimitiveParts.tool-interaction.test.ts +88 -0
  198. package/src/primitives/MessagePrimitiveParts.ts +167 -0
  199. package/src/primitives/PartByIndexProvider.ts +62 -0
  200. package/src/primitives/ThreadListItemPrimitiveRoot.ts +57 -0
  201. package/src/primitives/ThreadListItemPrimitiveTrigger.ts +74 -0
  202. package/src/primitives/ThreadListKeyboardNav.test.ts +298 -0
  203. package/src/primitives/ThreadListPrimitiveRoot.ts +18 -0
  204. package/src/primitives/ThreadPrimitiveMessages.ts +38 -0
  205. package/src/primitives/ThreadPrimitiveScrollToBottom.ts +65 -0
  206. package/src/primitives/ThreadPrimitiveViewport.ts +120 -0
  207. package/src/primitives/ThreadPrimitiveViewportFooter.test.ts +400 -0
  208. package/src/primitives/ThreadPrimitiveViewportFooter.ts +60 -0
  209. package/src/primitives/actionBar.ts +159 -0
  210. package/src/primitives/attachment.ts +76 -0
  211. package/src/primitives/attrDisabled.ts +6 -0
  212. package/src/primitives/branchPicker.ts +77 -0
  213. package/src/primitives/composerAttachments.ts +181 -0
  214. package/src/primitives/error.ts +36 -0
  215. package/src/primitives/lastValidCache.ts +12 -0
  216. package/src/primitives/message.ts +54 -0
  217. package/src/primitives/messageAttachments.ts +32 -0
  218. package/src/primitives/stableKeys.test.ts +19 -0
  219. package/src/primitives/stableKeys.ts +21 -0
  220. package/src/primitives/suggestions.ts +179 -0
  221. package/src/primitives/thread.ts +46 -0
  222. package/src/primitives/threadList.ts +163 -0
  223. package/src/primitives/threadListFocusGroup.ts +56 -0
  224. package/src/primitives/threadListStructural.ts +86 -0
  225. package/src/primitives/useComposerSendState.ts +13 -0
  226. package/src/primitives/viewportContext.ts +16 -0
  227. package/src/useAui.ts +18 -0
  228. package/src/useAuiEvent.ts +64 -0
  229. package/src/useAuiState.ts +55 -0
  230. package/src/useScrollLock.test.ts +207 -0
  231. package/src/useScrollLock.ts +79 -0
@@ -0,0 +1,408 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { createApp, defineComponent, h, nextTick, type Component } from "vue";
3
+ import { flushTapSync } from "@assistant-ui/tap";
4
+ import { AuiConfig } from "@assistant-ui/store/client";
5
+ import { RuntimeAdapter } from "@assistant-ui/core/store";
6
+ import type { AppendMessage, ExternalStoreAdapter } from "@assistant-ui/core";
7
+ import {
8
+ AssistantRuntimeImpl,
9
+ ExternalStoreRuntimeCore,
10
+ } from "@assistant-ui/core/internal";
11
+ import { AuiProvider } from "../AuiProvider";
12
+ import { useAuiState } from "../useAuiState";
13
+ import { ThreadPrimitiveMessages } from "../primitives/ThreadPrimitiveMessages";
14
+ import { ComposerPrimitiveInput } from "../primitives/ComposerPrimitiveInput";
15
+ import { ComposerPrimitiveSend } from "../primitives/ComposerPrimitiveSend";
16
+ import { ComposerPrimitiveCancel } from "../primitives/ComposerPrimitiveCancel";
17
+
18
+ type DemoMessage = { role: "user" | "assistant"; text: string };
19
+
20
+ const createTestRuntime = () => {
21
+ let messages: DemoMessage[] = [];
22
+ let isRunning = false;
23
+ let isDisabled = false;
24
+ const onNew = vi.fn<(message: AppendMessage) => Promise<void>>(
25
+ async () => {},
26
+ );
27
+ const onCancel = vi.fn(async () => {});
28
+ const makeAdapter = (): ExternalStoreAdapter<DemoMessage> => ({
29
+ messages,
30
+ isRunning,
31
+ isDisabled,
32
+ convertMessage: (message) => ({
33
+ role: message.role,
34
+ content: [{ type: "text", text: message.text }],
35
+ }),
36
+ onNew,
37
+ onCancel,
38
+ });
39
+ const core = new ExternalStoreRuntimeCore(makeAdapter());
40
+ const runtime = new AssistantRuntimeImpl(core);
41
+ const sync = () => core.setAdapter(makeAdapter());
42
+ const append = (message: DemoMessage) => {
43
+ messages = [...messages, message];
44
+ sync();
45
+ };
46
+ const setRunning = (value: boolean) => {
47
+ isRunning = value;
48
+ sync();
49
+ };
50
+ const setDisabled = (value: boolean) => {
51
+ isDisabled = value;
52
+ sync();
53
+ };
54
+ return { runtime, append, setRunning, setDisabled, onNew, onCancel };
55
+ };
56
+
57
+ const MessageProbe = defineComponent({
58
+ setup() {
59
+ const role = useAuiState((s) => s.message.role);
60
+ const text = useAuiState((s) => {
61
+ const part = s.message.content[0];
62
+ return part?.type === "text" ? part.text : "";
63
+ });
64
+ return () => h("li", { class: "msg", "data-role": role.value }, text.value);
65
+ },
66
+ });
67
+
68
+ const mountChat = (runtime: AssistantRuntimeImpl, view: Component) => {
69
+ const app = createApp(
70
+ defineComponent({
71
+ setup: () => () =>
72
+ h(
73
+ AuiProvider,
74
+ { config: AuiConfig({ threads: RuntimeAdapter(runtime) }) },
75
+ { default: () => h(view) },
76
+ ),
77
+ }),
78
+ );
79
+ const el = document.createElement("div");
80
+ app.mount(el);
81
+ return { el, unmount: () => app.unmount() };
82
+ };
83
+
84
+ describe("vue primitives", () => {
85
+ it("renders one scoped slot instance per thread message", async () => {
86
+ const { runtime, append } = createTestRuntime();
87
+ const View = defineComponent({
88
+ setup: () => () =>
89
+ h(ThreadPrimitiveMessages, null, { default: () => h(MessageProbe) }),
90
+ });
91
+ const { el, unmount } = mountChat(runtime, View);
92
+
93
+ expect(el.querySelectorAll("li.msg")).toHaveLength(0);
94
+
95
+ flushTapSync(() => append({ role: "user", text: "hi" }));
96
+ flushTapSync(() => append({ role: "assistant", text: "hello!" }));
97
+ await vi.waitFor(async () => {
98
+ await nextTick();
99
+ expect(el.querySelectorAll("li.msg")).toHaveLength(2);
100
+ });
101
+
102
+ const items = [...el.querySelectorAll("li.msg")];
103
+ expect(items.map((item) => item.getAttribute("data-role"))).toEqual([
104
+ "user",
105
+ "assistant",
106
+ ]);
107
+ expect(items.map((item) => item.textContent)).toEqual(["hi", "hello!"]);
108
+
109
+ unmount();
110
+ });
111
+
112
+ it("binds the composer text and submits on Enter", async () => {
113
+ const { runtime, onNew } = createTestRuntime();
114
+ const View = defineComponent({
115
+ setup: () => () => h(ComposerPrimitiveInput),
116
+ });
117
+ const { el, unmount } = mountChat(runtime, View);
118
+
119
+ const textarea = el.querySelector("textarea")!;
120
+ textarea.value = "hello";
121
+ textarea.dispatchEvent(new Event("input"));
122
+
123
+ textarea.dispatchEvent(
124
+ new KeyboardEvent("keydown", { key: "Enter", cancelable: true }),
125
+ );
126
+ await vi.waitFor(() => {
127
+ expect(onNew).toHaveBeenCalledTimes(1);
128
+ });
129
+ expect(onNew.mock.calls[0]![0]).toMatchObject({
130
+ content: [{ type: "text", text: "hello" }],
131
+ });
132
+
133
+ unmount();
134
+ });
135
+
136
+ it("keeps Shift+Enter and IME composition as newlines", async () => {
137
+ const { runtime, onNew } = createTestRuntime();
138
+ const View = defineComponent({
139
+ setup: () => () => h(ComposerPrimitiveInput),
140
+ });
141
+ const { el, unmount } = mountChat(runtime, View);
142
+
143
+ const textarea = el.querySelector("textarea")!;
144
+ textarea.value = "hello";
145
+ textarea.dispatchEvent(new Event("input"));
146
+
147
+ const shiftEnter = new KeyboardEvent("keydown", {
148
+ key: "Enter",
149
+ shiftKey: true,
150
+ cancelable: true,
151
+ });
152
+ expect(textarea.dispatchEvent(shiftEnter)).toBe(true);
153
+
154
+ const composingEnter = new KeyboardEvent("keydown", {
155
+ key: "Enter",
156
+ isComposing: true,
157
+ cancelable: true,
158
+ });
159
+ expect(textarea.dispatchEvent(composingEnter)).toBe(true);
160
+
161
+ await vi.waitFor(() => {
162
+ expect(runtime.thread.composer.getState().text).toBe("hello");
163
+ });
164
+ expect(onNew).not.toHaveBeenCalled();
165
+
166
+ unmount();
167
+ });
168
+
169
+ it("does not submit on Safari's post-compositionend Enter", async () => {
170
+ const { runtime, onNew } = createTestRuntime();
171
+ const View = defineComponent({
172
+ setup: () => () => h(ComposerPrimitiveInput),
173
+ });
174
+ const { el, unmount } = mountChat(runtime, View);
175
+
176
+ const textarea = el.querySelector("textarea")!;
177
+ textarea.dispatchEvent(
178
+ new CompositionEvent("compositionstart", { bubbles: true }),
179
+ );
180
+ textarea.value = "hello";
181
+ textarea.dispatchEvent(
182
+ new InputEvent("input", { bubbles: true, isComposing: true }),
183
+ );
184
+ textarea.dispatchEvent(
185
+ new CompositionEvent("compositionend", {
186
+ data: "hello",
187
+ bubbles: true,
188
+ }),
189
+ );
190
+
191
+ const composingEnter = new KeyboardEvent("keydown", {
192
+ key: "Enter",
193
+ keyCode: 229,
194
+ isComposing: false,
195
+ cancelable: true,
196
+ });
197
+ expect(textarea.dispatchEvent(composingEnter)).toBe(true);
198
+ await vi.waitFor(() => {
199
+ expect(runtime.thread.composer.getState().text).toBe("hello");
200
+ });
201
+ expect(onNew).not.toHaveBeenCalled();
202
+
203
+ const plainEnter = new KeyboardEvent("keydown", {
204
+ key: "Enter",
205
+ keyCode: 13,
206
+ cancelable: true,
207
+ });
208
+ expect(textarea.dispatchEvent(plainEnter)).toBe(false);
209
+ await vi.waitFor(() => {
210
+ expect(onNew).toHaveBeenCalledTimes(1);
211
+ });
212
+
213
+ unmount();
214
+ });
215
+
216
+ it("does not submit on Enter when submitOnEnter is false", async () => {
217
+ const { runtime, onNew } = createTestRuntime();
218
+ const View = defineComponent({
219
+ setup: () => () => h(ComposerPrimitiveInput, { submitOnEnter: false }),
220
+ });
221
+ const { el, unmount } = mountChat(runtime, View);
222
+
223
+ const textarea = el.querySelector("textarea")!;
224
+ textarea.value = "hello";
225
+ textarea.dispatchEvent(new Event("input"));
226
+ const enter = new KeyboardEvent("keydown", {
227
+ key: "Enter",
228
+ cancelable: true,
229
+ });
230
+ expect(textarea.dispatchEvent(enter)).toBe(true);
231
+
232
+ await vi.waitFor(() => {
233
+ expect(runtime.thread.composer.getState().text).toBe("hello");
234
+ });
235
+ expect(onNew).not.toHaveBeenCalled();
236
+
237
+ unmount();
238
+ });
239
+
240
+ it("disables the input while the thread is disabled", async () => {
241
+ const { runtime, setDisabled } = createTestRuntime();
242
+ const View = defineComponent({
243
+ setup: () => () => h(ComposerPrimitiveInput),
244
+ });
245
+ const { el, unmount } = mountChat(runtime, View);
246
+
247
+ expect(el.querySelector("textarea")!.disabled).toBe(false);
248
+
249
+ flushTapSync(() => setDisabled(true));
250
+ await vi.waitFor(async () => {
251
+ await nextTick();
252
+ expect(el.querySelector("textarea")!.disabled).toBe(true);
253
+ });
254
+
255
+ unmount();
256
+ });
257
+
258
+ it("names the textarea input unless the caller passes a name", () => {
259
+ const { runtime } = createTestRuntime();
260
+ const View = defineComponent({
261
+ setup: () => () => [
262
+ h(ComposerPrimitiveInput),
263
+ h(ComposerPrimitiveInput, { name: "prompt" }),
264
+ ],
265
+ });
266
+ const { el, unmount } = mountChat(runtime, View);
267
+
268
+ expect(
269
+ [...el.querySelectorAll("textarea")].map((textarea) => textarea.name),
270
+ ).toEqual(["input", "prompt"]);
271
+
272
+ unmount();
273
+ });
274
+
275
+ it("cancels a running turn through the cancel button", async () => {
276
+ const { runtime, setRunning, onCancel } = createTestRuntime();
277
+ const View = defineComponent({
278
+ setup: () => () =>
279
+ h(ComposerPrimitiveCancel, null, { default: () => "Stop" }),
280
+ });
281
+ const { el, unmount } = mountChat(runtime, View);
282
+
283
+ flushTapSync(() => setRunning(true));
284
+ const button = el.querySelector("button")!;
285
+ await vi.waitFor(async () => {
286
+ await nextTick();
287
+ expect(button.disabled).toBe(false);
288
+ });
289
+
290
+ button.click();
291
+ await vi.waitFor(() => {
292
+ expect(onCancel).toHaveBeenCalledTimes(1);
293
+ });
294
+
295
+ unmount();
296
+ });
297
+
298
+ it("disables send on an empty composer and sends on click", async () => {
299
+ const { runtime, onNew } = createTestRuntime();
300
+ const View = defineComponent({
301
+ setup: () => () => [
302
+ h(ComposerPrimitiveInput),
303
+ h(ComposerPrimitiveSend, null, { default: () => "Send" }),
304
+ ],
305
+ });
306
+ const { el, unmount } = mountChat(runtime, View);
307
+
308
+ const button = el.querySelector("button")!;
309
+ expect(button.disabled).toBe(true);
310
+
311
+ const textarea = el.querySelector("textarea")!;
312
+ textarea.value = "hello";
313
+ textarea.dispatchEvent(new Event("input"));
314
+ await vi.waitFor(async () => {
315
+ await nextTick();
316
+ expect(button.disabled).toBe(false);
317
+ });
318
+
319
+ button.click();
320
+ await vi.waitFor(() => {
321
+ expect(onNew).toHaveBeenCalledTimes(1);
322
+ });
323
+
324
+ unmount();
325
+ });
326
+
327
+ it("stays inert for a message edit composer that is not editing", async () => {
328
+ const { runtime, append, onNew } = createTestRuntime();
329
+ const View = defineComponent({
330
+ setup: () => () =>
331
+ h(ThreadPrimitiveMessages, null, {
332
+ default: () => h(ComposerPrimitiveInput),
333
+ }),
334
+ });
335
+ const { el, unmount } = mountChat(runtime, View);
336
+
337
+ flushTapSync(() => append({ role: "user", text: "hi" }));
338
+ await vi.waitFor(async () => {
339
+ await nextTick();
340
+ expect(el.querySelector("textarea")).not.toBeNull();
341
+ });
342
+
343
+ const textarea = el.querySelector("textarea")!;
344
+ expect(textarea.value).toBe("");
345
+ textarea.value = "typing";
346
+ textarea.dispatchEvent(new Event("input"));
347
+
348
+ expect(textarea.value).toBe("");
349
+ await new Promise((resolve) => setTimeout(resolve));
350
+ expect(onNew).not.toHaveBeenCalled();
351
+
352
+ unmount();
353
+ });
354
+
355
+ it("keeps a caller-disabled send button disabled and inert", async () => {
356
+ const { runtime, onNew } = createTestRuntime();
357
+ const View = defineComponent({
358
+ setup: () => () => [
359
+ h(ComposerPrimitiveInput),
360
+ h(
361
+ ComposerPrimitiveSend,
362
+ { disabled: true },
363
+ {
364
+ default: () => "Send",
365
+ },
366
+ ),
367
+ ],
368
+ });
369
+ const { el, unmount } = mountChat(runtime, View);
370
+
371
+ const textarea = el.querySelector("textarea")!;
372
+ textarea.value = "hello";
373
+ flushTapSync(() => textarea.dispatchEvent(new Event("input")));
374
+
375
+ const button = el.querySelector("button")!;
376
+ await nextTick();
377
+ expect(button.disabled).toBe(true);
378
+
379
+ button.disabled = false;
380
+ button.click();
381
+ await new Promise((resolve) => setTimeout(resolve));
382
+ expect(onNew).not.toHaveBeenCalled();
383
+
384
+ unmount();
385
+ });
386
+
387
+ it("disables cancel while the runtime cannot cancel", () => {
388
+ const runtime = new AssistantRuntimeImpl(
389
+ new ExternalStoreRuntimeCore({
390
+ messages: [] as DemoMessage[],
391
+ convertMessage: (message: DemoMessage) => ({
392
+ role: message.role,
393
+ content: [{ type: "text", text: message.text }],
394
+ }),
395
+ onNew: async () => {},
396
+ }),
397
+ );
398
+ const View = defineComponent({
399
+ setup: () => () =>
400
+ h(ComposerPrimitiveCancel, null, { default: () => "Stop" }),
401
+ });
402
+ const { el, unmount } = mountChat(runtime, View);
403
+
404
+ expect(el.querySelector("button")!.disabled).toBe(true);
405
+
406
+ unmount();
407
+ });
408
+ });
@@ -0,0 +1,159 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import {
3
+ computed,
4
+ createApp,
5
+ defineComponent,
6
+ h,
7
+ ref,
8
+ type Component,
9
+ } from "vue";
10
+ import { flushTapSync } from "@assistant-ui/tap";
11
+ import { AuiConfig } from "@assistant-ui/store/client";
12
+ import { AuiProvider } from "../AuiProvider";
13
+ import { useAui } from "../useAui";
14
+ import { useAuiEvent } from "../useAuiEvent";
15
+ import {
16
+ ThreadClient,
17
+ flushEvents,
18
+ messageDerived,
19
+ type AnyClient,
20
+ } from "./fixtures";
21
+
22
+ const mountWithProvider = (setup: () => void) => {
23
+ const Probe = defineComponent({
24
+ setup() {
25
+ setup();
26
+ return () => null;
27
+ },
28
+ });
29
+ const app = createApp(
30
+ defineComponent({
31
+ setup: () => () =>
32
+ h(
33
+ AuiProvider,
34
+ {
35
+ config: AuiConfig({
36
+ thread: ThreadClient(),
37
+ message: messageDerived(),
38
+ } as never),
39
+ },
40
+ { default: () => h(Probe) },
41
+ ),
42
+ }) as Component,
43
+ );
44
+ app.mount(document.createElement("div"));
45
+ return { unmount: () => app.unmount() };
46
+ };
47
+
48
+ describe("useAuiEvent", () => {
49
+ afterEach(() => {
50
+ vi.restoreAllMocks();
51
+ });
52
+
53
+ it("delivers scope-filtered events on a microtask", async () => {
54
+ let aui!: AnyClient;
55
+ const cb = vi.fn();
56
+ const { unmount } = mountWithProvider(() => {
57
+ aui = useAui() as AnyClient;
58
+ useAuiEvent("message.pinged" as never, cb as never);
59
+ });
60
+
61
+ flushTapSync(() => aui.thread.message({ index: 1 }).ping("other"));
62
+ await flushEvents();
63
+ expect(cb).not.toHaveBeenCalled();
64
+
65
+ flushTapSync(() => aui.message.ping("bound"));
66
+ await flushEvents();
67
+ expect(cb).toHaveBeenCalledTimes(1);
68
+ expect(cb).toHaveBeenCalledWith({ id: "m0", value: "bound" });
69
+
70
+ unmount();
71
+ });
72
+
73
+ it("follows the client across structural changes", async () => {
74
+ let aui!: AnyClient;
75
+ const cb = vi.fn();
76
+ const { unmount } = mountWithProvider(() => {
77
+ aui = useAui() as AnyClient;
78
+ useAuiEvent("message.pinged" as never, cb as never);
79
+ });
80
+
81
+ flushTapSync(() => aui.thread.setSelected(1));
82
+
83
+ flushTapSync(() => aui.message.ping("after-rebind"));
84
+ await flushEvents();
85
+ expect(cb).toHaveBeenCalledTimes(1);
86
+ expect(cb).toHaveBeenCalledWith({ id: "m1", value: "after-rebind" });
87
+
88
+ unmount();
89
+ });
90
+
91
+ it("stops delivering after unmount", async () => {
92
+ let aui!: AnyClient;
93
+ const cb = vi.fn();
94
+ const { unmount } = mountWithProvider(() => {
95
+ aui = useAui() as AnyClient;
96
+ useAuiEvent("message.pinged" as never, cb as never);
97
+ });
98
+
99
+ unmount();
100
+
101
+ flushTapSync(() => aui.thread.message({ index: 0 }).ping("late"));
102
+ await flushEvents();
103
+ expect(cb).not.toHaveBeenCalled();
104
+ });
105
+ it("binds once its scope appears in a live config, without detaching", async () => {
106
+ let aui!: AnyClient;
107
+ const cb = vi.fn();
108
+ const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
109
+ const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
110
+ const present = ref(false);
111
+ const Probe = defineComponent({
112
+ setup() {
113
+ aui = useAui() as AnyClient;
114
+ useAuiEvent("message.pinged" as never, cb as never);
115
+ return () => null;
116
+ },
117
+ });
118
+ const app = createApp(
119
+ defineComponent({
120
+ setup: () => {
121
+ const config = computed(() =>
122
+ present.value
123
+ ? (AuiConfig({
124
+ thread: ThreadClient(),
125
+ message: messageDerived(),
126
+ } as never) as never)
127
+ : (AuiConfig({ thread: ThreadClient() } as never) as never),
128
+ );
129
+ return () =>
130
+ h(
131
+ AuiProvider,
132
+ { config: config.value },
133
+ { default: () => h(Probe) },
134
+ );
135
+ },
136
+ }) as Component,
137
+ );
138
+ app.mount(document.createElement("div"));
139
+
140
+ try {
141
+ expect(warnSpy).toHaveBeenCalled();
142
+ expect(cb).not.toHaveBeenCalled();
143
+
144
+ present.value = true;
145
+ await vi.waitFor(() =>
146
+ expect((aui.message as AnyClient).getState().id).toBe("m0"),
147
+ );
148
+
149
+ flushTapSync(() => aui.message.ping("hello"));
150
+ await flushEvents();
151
+ expect(cb).toHaveBeenCalledWith({ id: "m0", value: "hello" });
152
+ expect(errorSpy).not.toHaveBeenCalled();
153
+ } finally {
154
+ warnSpy.mockRestore();
155
+ errorSpy.mockRestore();
156
+ app.unmount();
157
+ }
158
+ });
159
+ });
@@ -0,0 +1,124 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import {
3
+ createApp,
4
+ defineComponent,
5
+ h,
6
+ nextTick,
7
+ watch,
8
+ type Component,
9
+ type ComputedRef,
10
+ } from "vue";
11
+ import { flushTapSync } from "@assistant-ui/tap";
12
+ import { AuiConfig } from "@assistant-ui/store/client";
13
+ import { AuiProvider } from "../AuiProvider";
14
+ import { useAui } from "../useAui";
15
+ import { useAuiState } from "../useAuiState";
16
+ import { ThreadClient, messageDerived, type AnyClient } from "./fixtures";
17
+
18
+ const mountWithProvider = (setup: () => void) => {
19
+ const Probe = defineComponent({
20
+ setup() {
21
+ setup();
22
+ return () => null;
23
+ },
24
+ });
25
+ const app = createApp(
26
+ defineComponent({
27
+ setup: () => () =>
28
+ h(
29
+ AuiProvider,
30
+ {
31
+ config: AuiConfig({
32
+ thread: ThreadClient(),
33
+ message: messageDerived(),
34
+ } as never),
35
+ },
36
+ { default: () => h(Probe) },
37
+ ),
38
+ }) as Component,
39
+ );
40
+ app.mount(document.createElement("div"));
41
+ return { unmount: () => app.unmount() };
42
+ };
43
+
44
+ describe("useAuiState", () => {
45
+ it("selects state and updates when the store notifies", () => {
46
+ let aui!: AnyClient;
47
+ let selected!: ComputedRef<number>;
48
+ const { unmount } = mountWithProvider(() => {
49
+ aui = useAui() as AnyClient;
50
+ selected = useAuiState((s) => (s as AnyClient).thread.selected);
51
+ });
52
+
53
+ expect(selected.value).toBe(0);
54
+
55
+ flushTapSync(() => aui.thread.setSelected(1));
56
+ expect(selected.value).toBe(1);
57
+
58
+ unmount();
59
+ });
60
+
61
+ it("updates through the real scheduler without a manual flush", async () => {
62
+ let aui!: AnyClient;
63
+ let selected!: ComputedRef<number>;
64
+ const { unmount } = mountWithProvider(() => {
65
+ aui = useAui() as AnyClient;
66
+ selected = useAuiState((s) => (s as AnyClient).thread.selected);
67
+ });
68
+
69
+ aui.thread.setSelected(1);
70
+ // The tap scheduler flushes on a MessageChannel macrotask whose ordering
71
+ // against timers is unspecified; poll instead of racing a single timer
72
+ await vi.waitFor(() => {
73
+ expect(selected.value).toBe(1);
74
+ });
75
+
76
+ unmount();
77
+ });
78
+
79
+ it("re-runs watchers only when the slice changes by Object.is", async () => {
80
+ let aui!: AnyClient;
81
+ let id!: ComputedRef<string>;
82
+ const watcher = vi.fn();
83
+ const { unmount } = mountWithProvider(() => {
84
+ aui = useAui() as AnyClient;
85
+ id = useAuiState((s) => (s as AnyClient).message.id);
86
+ watch(id, watcher);
87
+ });
88
+
89
+ flushTapSync(() => aui.thread.message({ index: 0 }).setText("hello"));
90
+ await nextTick();
91
+ expect(watcher).not.toHaveBeenCalled();
92
+
93
+ flushTapSync(() => aui.thread.setSelected(1));
94
+ await nextTick();
95
+ expect(watcher).toHaveBeenCalledTimes(1);
96
+ expect(id.value).toBe("m1");
97
+
98
+ unmount();
99
+ });
100
+
101
+ it("throws when the selector returns the whole state", () => {
102
+ let state!: ComputedRef<unknown>;
103
+ const { unmount } = mountWithProvider(() => {
104
+ state = useAuiState((s) => s);
105
+ });
106
+
107
+ expect(() => state.value).toThrow(
108
+ "You tried to return the entire AssistantState.",
109
+ );
110
+
111
+ unmount();
112
+ });
113
+
114
+ it("resolves unavailable scopes to undefined via optional", () => {
115
+ let missing!: ComputedRef<unknown>;
116
+ const { unmount } = mountWithProvider(() => {
117
+ missing = useAuiState((s) => (s.optional as AnyClient).missing);
118
+ });
119
+
120
+ expect(missing.value).toBeUndefined();
121
+
122
+ unmount();
123
+ });
124
+ });