vanilla-agent 1.10.0 → 1.12.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vanilla-agent",
3
- "version": "1.10.0",
3
+ "version": "1.12.0",
4
4
  "description": "Themeable, plugable streaming agent widget for websites, in plain JS with support for voice input and reasoning / tool output.",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -0,0 +1,366 @@
1
+ import { createElement } from "../utils/dom";
2
+ import { renderLucideIcon } from "../utils/icons";
3
+ import { AgentWidgetConfig } from "../types";
4
+
5
+ export interface ComposerElements {
6
+ footer: HTMLElement;
7
+ suggestions: HTMLElement;
8
+ composerForm: HTMLFormElement;
9
+ textarea: HTMLTextAreaElement;
10
+ sendButton: HTMLButtonElement;
11
+ sendButtonWrapper: HTMLElement;
12
+ micButton: HTMLButtonElement | null;
13
+ micButtonWrapper: HTMLElement | null;
14
+ statusText: HTMLElement;
15
+ }
16
+
17
+ export interface ComposerBuildContext {
18
+ config?: AgentWidgetConfig;
19
+ onSubmit?: (text: string) => void;
20
+ disabled?: boolean;
21
+ }
22
+
23
+ /**
24
+ * Helper to get font family CSS value from config preset
25
+ */
26
+ const getFontFamilyValue = (
27
+ family: "sans-serif" | "serif" | "mono"
28
+ ): string => {
29
+ switch (family) {
30
+ case "serif":
31
+ return 'Georgia, "Times New Roman", Times, serif';
32
+ case "mono":
33
+ return '"Courier New", Courier, "Lucida Console", Monaco, monospace';
34
+ case "sans-serif":
35
+ default:
36
+ return '-apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif';
37
+ }
38
+ };
39
+
40
+ /**
41
+ * Build the composer/footer section of the panel.
42
+ * Extracted for reuse and plugin override support.
43
+ */
44
+ export const buildComposer = (context: ComposerBuildContext): ComposerElements => {
45
+ const { config } = context;
46
+
47
+ const footer = createElement(
48
+ "div",
49
+ "tvw-border-t-cw-divider tvw-bg-cw-surface tvw-px-6 tvw-py-4"
50
+ );
51
+
52
+ const suggestions = createElement(
53
+ "div",
54
+ "tvw-mb-3 tvw-flex tvw-flex-wrap tvw-gap-2"
55
+ );
56
+
57
+ // Determine gap based on voice recognition
58
+ const voiceRecognitionEnabledForGap =
59
+ config?.voiceRecognition?.enabled === true;
60
+ const hasSpeechRecognitionForGap =
61
+ typeof window !== "undefined" &&
62
+ (typeof (window as any).webkitSpeechRecognition !== "undefined" ||
63
+ typeof (window as any).SpeechRecognition !== "undefined");
64
+ const shouldUseSmallGap =
65
+ voiceRecognitionEnabledForGap && hasSpeechRecognitionForGap;
66
+ const gapClass = shouldUseSmallGap ? "tvw-gap-1" : "tvw-gap-3";
67
+
68
+ const composerForm = createElement(
69
+ "form",
70
+ `tvw-flex tvw-items-end ${gapClass} tvw-rounded-2xl tvw-border tvw-border-gray-200 tvw-bg-cw-input-background tvw-px-4 tvw-py-3`
71
+ ) as HTMLFormElement;
72
+ // Prevent form from getting focus styles
73
+ composerForm.style.outline = "none";
74
+
75
+ const textarea = createElement("textarea") as HTMLTextAreaElement;
76
+ textarea.placeholder = config?.copy?.inputPlaceholder ?? "Type your message…";
77
+ textarea.className =
78
+ "tvw-min-h-[48px] tvw-flex-1 tvw-resize-none tvw-border-none tvw-bg-transparent tvw-text-sm tvw-text-cw-primary focus:tvw-outline-none focus:tvw-border-none";
79
+ textarea.rows = 1;
80
+
81
+ // Apply font family and weight from config
82
+ const fontFamily = config?.theme?.inputFontFamily ?? "sans-serif";
83
+ const fontWeight = config?.theme?.inputFontWeight ?? "400";
84
+
85
+ textarea.style.fontFamily = getFontFamilyValue(fontFamily);
86
+ textarea.style.fontWeight = fontWeight;
87
+
88
+ // Explicitly remove border and outline on focus to prevent browser defaults
89
+ textarea.style.border = "none";
90
+ textarea.style.outline = "none";
91
+ textarea.style.borderWidth = "0";
92
+ textarea.style.borderStyle = "none";
93
+ textarea.style.borderColor = "transparent";
94
+ textarea.addEventListener("focus", () => {
95
+ textarea.style.border = "none";
96
+ textarea.style.outline = "none";
97
+ textarea.style.borderWidth = "0";
98
+ textarea.style.borderStyle = "none";
99
+ textarea.style.borderColor = "transparent";
100
+ textarea.style.boxShadow = "none";
101
+ });
102
+ textarea.addEventListener("blur", () => {
103
+ textarea.style.border = "none";
104
+ textarea.style.outline = "none";
105
+ });
106
+
107
+ // Send button configuration
108
+ const sendButtonConfig = config?.sendButton ?? {};
109
+ const useIcon = sendButtonConfig.useIcon ?? false;
110
+ const iconText = sendButtonConfig.iconText ?? "↑";
111
+ const iconName = sendButtonConfig.iconName;
112
+ const tooltipText = sendButtonConfig.tooltipText ?? "Send message";
113
+ const showTooltip = sendButtonConfig.showTooltip ?? false;
114
+ const buttonSize = sendButtonConfig.size ?? "40px";
115
+ const backgroundColor = sendButtonConfig.backgroundColor;
116
+ const textColor = sendButtonConfig.textColor;
117
+
118
+ // Create wrapper for tooltip positioning
119
+ const sendButtonWrapper = createElement("div", "tvw-send-button-wrapper");
120
+
121
+ const sendButton = createElement(
122
+ "button",
123
+ useIcon
124
+ ? "tvw-rounded-button tvw-flex tvw-items-center tvw-justify-center disabled:tvw-opacity-50 tvw-cursor-pointer"
125
+ : "tvw-rounded-button tvw-bg-cw-accent tvw-px-4 tvw-py-2 tvw-text-sm tvw-font-semibold disabled:tvw-opacity-50 tvw-cursor-pointer"
126
+ ) as HTMLButtonElement;
127
+
128
+ sendButton.type = "submit";
129
+
130
+ if (useIcon) {
131
+ // Icon mode: circular button
132
+ sendButton.style.width = buttonSize;
133
+ sendButton.style.height = buttonSize;
134
+ sendButton.style.minWidth = buttonSize;
135
+ sendButton.style.minHeight = buttonSize;
136
+ sendButton.style.fontSize = "18px";
137
+ sendButton.style.lineHeight = "1";
138
+
139
+ // Clear any existing content
140
+ sendButton.innerHTML = "";
141
+
142
+ // Use Lucide icon if iconName is provided, otherwise fall back to iconText
143
+ if (iconName) {
144
+ const iconSize = parseFloat(buttonSize) || 24;
145
+ const iconColor =
146
+ textColor && typeof textColor === "string" && textColor.trim()
147
+ ? textColor.trim()
148
+ : "currentColor";
149
+ const iconSvg = renderLucideIcon(iconName, iconSize, iconColor, 2);
150
+ if (iconSvg) {
151
+ sendButton.appendChild(iconSvg);
152
+ sendButton.style.color = iconColor;
153
+ } else {
154
+ // Fallback to text if icon fails to render
155
+ sendButton.textContent = iconText;
156
+ if (textColor) {
157
+ sendButton.style.color = textColor;
158
+ } else {
159
+ sendButton.classList.add("tvw-text-white");
160
+ }
161
+ }
162
+ } else {
163
+ sendButton.textContent = iconText;
164
+ if (textColor) {
165
+ sendButton.style.color = textColor;
166
+ } else {
167
+ sendButton.classList.add("tvw-text-white");
168
+ }
169
+ }
170
+
171
+ if (backgroundColor) {
172
+ sendButton.style.backgroundColor = backgroundColor;
173
+ } else {
174
+ sendButton.classList.add("tvw-bg-cw-primary");
175
+ }
176
+ } else {
177
+ // Text mode: existing behavior
178
+ sendButton.textContent = config?.copy?.sendButtonLabel ?? "Send";
179
+ if (textColor) {
180
+ sendButton.style.color = textColor;
181
+ } else {
182
+ sendButton.classList.add("tvw-text-white");
183
+ }
184
+ }
185
+
186
+ // Apply existing styling from config
187
+ if (sendButtonConfig.borderWidth) {
188
+ sendButton.style.borderWidth = sendButtonConfig.borderWidth;
189
+ sendButton.style.borderStyle = "solid";
190
+ }
191
+ if (sendButtonConfig.borderColor) {
192
+ sendButton.style.borderColor = sendButtonConfig.borderColor;
193
+ }
194
+
195
+ // Apply padding styling (works in both icon and text mode)
196
+ if (sendButtonConfig.paddingX) {
197
+ sendButton.style.paddingLeft = sendButtonConfig.paddingX;
198
+ sendButton.style.paddingRight = sendButtonConfig.paddingX;
199
+ } else {
200
+ sendButton.style.paddingLeft = "";
201
+ sendButton.style.paddingRight = "";
202
+ }
203
+ if (sendButtonConfig.paddingY) {
204
+ sendButton.style.paddingTop = sendButtonConfig.paddingY;
205
+ sendButton.style.paddingBottom = sendButtonConfig.paddingY;
206
+ } else {
207
+ sendButton.style.paddingTop = "";
208
+ sendButton.style.paddingBottom = "";
209
+ }
210
+
211
+ // Add tooltip if enabled
212
+ if (showTooltip && tooltipText) {
213
+ const tooltip = createElement("div", "tvw-send-button-tooltip");
214
+ tooltip.textContent = tooltipText;
215
+ sendButtonWrapper.appendChild(tooltip);
216
+ }
217
+
218
+ sendButtonWrapper.appendChild(sendButton);
219
+
220
+ // Voice recognition mic button
221
+ const voiceRecognitionConfig = config?.voiceRecognition ?? {};
222
+ const voiceRecognitionEnabled = voiceRecognitionConfig.enabled === true;
223
+ let micButton: HTMLButtonElement | null = null;
224
+ let micButtonWrapper: HTMLElement | null = null;
225
+
226
+ // Check browser support for speech recognition
227
+ const hasSpeechRecognition =
228
+ typeof window !== "undefined" &&
229
+ (typeof (window as any).webkitSpeechRecognition !== "undefined" ||
230
+ typeof (window as any).SpeechRecognition !== "undefined");
231
+
232
+ if (voiceRecognitionEnabled && hasSpeechRecognition) {
233
+ micButtonWrapper = createElement("div", "tvw-send-button-wrapper");
234
+ micButton = createElement(
235
+ "button",
236
+ "tvw-rounded-button tvw-flex tvw-items-center tvw-justify-center disabled:tvw-opacity-50 tvw-cursor-pointer"
237
+ ) as HTMLButtonElement;
238
+
239
+ micButton.type = "button";
240
+ micButton.setAttribute("aria-label", "Start voice recognition");
241
+
242
+ const micIconName = voiceRecognitionConfig.iconName ?? "mic";
243
+ const micIconSize = voiceRecognitionConfig.iconSize ?? buttonSize;
244
+ const micIconSizeNum = parseFloat(micIconSize) || 24;
245
+
246
+ // Use dedicated colors from voice recognition config, fallback to send button colors
247
+ const micBackgroundColor =
248
+ voiceRecognitionConfig.backgroundColor ?? backgroundColor;
249
+ const micIconColor = voiceRecognitionConfig.iconColor ?? textColor;
250
+
251
+ micButton.style.width = micIconSize;
252
+ micButton.style.height = micIconSize;
253
+ micButton.style.minWidth = micIconSize;
254
+ micButton.style.minHeight = micIconSize;
255
+ micButton.style.fontSize = "18px";
256
+ micButton.style.lineHeight = "1";
257
+
258
+ // Use Lucide mic icon with configured color (stroke width 1.5 for minimalist outline style)
259
+ const iconColorValue = micIconColor || "currentColor";
260
+ const micIconSvg = renderLucideIcon(
261
+ micIconName,
262
+ micIconSizeNum,
263
+ iconColorValue,
264
+ 1.5
265
+ );
266
+ if (micIconSvg) {
267
+ micButton.appendChild(micIconSvg);
268
+ micButton.style.color = iconColorValue;
269
+ } else {
270
+ // Fallback to text if icon fails
271
+ micButton.textContent = "🎤";
272
+ micButton.style.color = iconColorValue;
273
+ }
274
+
275
+ // Apply background color
276
+ if (micBackgroundColor) {
277
+ micButton.style.backgroundColor = micBackgroundColor;
278
+ } else {
279
+ micButton.classList.add("tvw-bg-cw-primary");
280
+ }
281
+
282
+ // Apply icon/text color
283
+ if (micIconColor) {
284
+ micButton.style.color = micIconColor;
285
+ } else if (!micIconColor && !textColor) {
286
+ micButton.classList.add("tvw-text-white");
287
+ }
288
+
289
+ // Apply border styling
290
+ if (voiceRecognitionConfig.borderWidth) {
291
+ micButton.style.borderWidth = voiceRecognitionConfig.borderWidth;
292
+ micButton.style.borderStyle = "solid";
293
+ }
294
+ if (voiceRecognitionConfig.borderColor) {
295
+ micButton.style.borderColor = voiceRecognitionConfig.borderColor;
296
+ }
297
+
298
+ // Apply padding styling
299
+ if (voiceRecognitionConfig.paddingX) {
300
+ micButton.style.paddingLeft = voiceRecognitionConfig.paddingX;
301
+ micButton.style.paddingRight = voiceRecognitionConfig.paddingX;
302
+ }
303
+ if (voiceRecognitionConfig.paddingY) {
304
+ micButton.style.paddingTop = voiceRecognitionConfig.paddingY;
305
+ micButton.style.paddingBottom = voiceRecognitionConfig.paddingY;
306
+ }
307
+
308
+ micButtonWrapper.appendChild(micButton);
309
+
310
+ // Add tooltip if enabled
311
+ const micTooltipText =
312
+ voiceRecognitionConfig.tooltipText ?? "Start voice recognition";
313
+ const showMicTooltip = voiceRecognitionConfig.showTooltip ?? false;
314
+ if (showMicTooltip && micTooltipText) {
315
+ const tooltip = createElement("div", "tvw-send-button-tooltip");
316
+ tooltip.textContent = micTooltipText;
317
+ micButtonWrapper.appendChild(tooltip);
318
+ }
319
+ }
320
+
321
+ // Focus textarea when composer form container is clicked
322
+ composerForm.addEventListener("click", (e) => {
323
+ // Don't focus if clicking on the send button, mic button, or their wrappers
324
+ if (
325
+ e.target !== sendButton &&
326
+ e.target !== sendButtonWrapper &&
327
+ e.target !== micButton &&
328
+ e.target !== micButtonWrapper
329
+ ) {
330
+ textarea.focus();
331
+ }
332
+ });
333
+
334
+ // Append elements: textarea, mic button (if exists), send button
335
+ composerForm.append(textarea);
336
+ if (micButtonWrapper) {
337
+ composerForm.append(micButtonWrapper);
338
+ }
339
+ composerForm.append(sendButtonWrapper);
340
+
341
+ const statusText = createElement(
342
+ "div",
343
+ "tvw-mt-2 tvw-text-right tvw-text-xs tvw-text-cw-muted"
344
+ );
345
+
346
+ // Apply status indicator config
347
+ const statusConfig = config?.statusIndicator ?? {};
348
+ const isVisible = statusConfig.visible ?? true;
349
+ statusText.style.display = isVisible ? "" : "none";
350
+ statusText.textContent = statusConfig.idleText ?? "Online";
351
+
352
+ footer.append(suggestions, composerForm, statusText);
353
+
354
+ return {
355
+ footer,
356
+ suggestions,
357
+ composerForm,
358
+ textarea,
359
+ sendButton,
360
+ sendButtonWrapper,
361
+ micButton,
362
+ micButtonWrapper,
363
+ statusText
364
+ };
365
+ };
366
+