@smart-cloud/ai-kit-ui 1.1.12 → 1.1.13

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": "@smart-cloud/ai-kit-ui",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -19,9 +19,11 @@
19
19
  "dependencies": {
20
20
  "@emotion/cache": "^11.14.0",
21
21
  "@emotion/react": "^11.14.0",
22
+ "@mantine/colors-generator": "^8.3.13",
22
23
  "@smart-cloud/ai-kit-core": "^1.1.5",
23
24
  "@smart-cloud/wpsuite-core": "^2.0.5",
24
25
  "@tabler/icons-react": "^3.36.1",
26
+ "chroma-js": "^3.2.0",
25
27
  "react-markdown": "^10.1.0",
26
28
  "rehype-sanitize": "^6.0.0",
27
29
  "rehype-stringify": "^10.0.1",
@@ -12,9 +12,6 @@ export type ShadowBoundaryProps = {
12
12
  /** Optional class name applied to the host element. */
13
13
  className?: string;
14
14
 
15
- /** Optional raw CSS text injected into the shadow root (as <style>). */
16
- innerCSS?: string;
17
-
18
15
  /** ID of the element inside the shadow root used as the portal target. */
19
16
  rootElementId: string;
20
17
 
@@ -89,22 +86,9 @@ function installAiKitPropertyRegistry() {
89
86
  doc.head.appendChild(style);
90
87
  }
91
88
 
92
- // tiny stable hash to detect innerCSS changes without forcing huge deps churn
93
- function hashStringDjb2(str: string): string {
94
- let hash = 5381;
95
- for (let i = 0; i < str.length; i++) {
96
- hash = ((hash << 5) + hash) ^ str.charCodeAt(i);
97
- }
98
- // unsigned + base36
99
- return (hash >>> 0).toString(36);
100
- }
101
-
102
- const STYLE_TEXT_ID = "ai-kit-style-text";
103
-
104
89
  export function ShadowBoundary({
105
90
  stylesheets,
106
91
  className,
107
- innerCSS,
108
92
  children,
109
93
  rootElementId,
110
94
  mode = "local",
@@ -121,10 +105,6 @@ export function ShadowBoundary({
121
105
  return all.join("|");
122
106
  }, [stylesheets]);
123
107
 
124
- const innerCSSHash = useMemo(() => {
125
- return innerCSS ? hashStringDjb2(innerCSS) : "";
126
- }, [innerCSS]);
127
-
128
108
  useLayoutEffect(() => {
129
109
  if (!hostRef.current) return;
130
110
 
@@ -206,29 +186,6 @@ export function ShadowBoundary({
206
186
  stylesKey ? stylesKey.split("|") : [],
207
187
  );
208
188
 
209
- // 6) Optional: inject raw style text into the shadow root
210
- const existingStyle = shadow.getElementById(
211
- STYLE_TEXT_ID,
212
- ) as HTMLStyleElement | null;
213
-
214
- if (innerCSS) {
215
- if (!existingStyle) {
216
- const s = doc.createElement("style");
217
- s.id = STYLE_TEXT_ID;
218
- s.setAttribute("data-hash", innerCSSHash);
219
- s.textContent = innerCSS;
220
- rootEl.appendChild(s);
221
- } else {
222
- const prevHash = existingStyle.getAttribute("data-hash") || "";
223
- if (prevHash !== innerCSSHash) {
224
- existingStyle.setAttribute("data-hash", innerCSSHash);
225
- existingStyle.textContent = innerCSS;
226
- }
227
- }
228
- } else if (existingStyle) {
229
- existingStyle.remove();
230
- }
231
-
232
189
  setShadowRoot(shadow);
233
190
  setPortalTarget(rootEl);
234
191
 
@@ -236,7 +193,7 @@ export function ShadowBoundary({
236
193
  mo.disconnect();
237
194
  mq?.removeEventListener?.("change", onMq);
238
195
  };
239
- }, [mode, overlayRootId, rootElementId, stylesKey, innerCSS, innerCSSHash]);
196
+ }, [mode, overlayRootId, rootElementId, stylesKey]);
240
197
 
241
198
  const emotionCache = useMemo(() => {
242
199
  if (!portalTarget) return null;
@@ -1156,6 +1156,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1156
1156
  ? I18n.get(labels.restoreSizeLabel)
1157
1157
  : I18n.get(labels.maximizeLabel)
1158
1158
  }
1159
+ data-ai-kit-maximize-button
1159
1160
  >
1160
1161
  {isMaximized ? (
1161
1162
  <IconMinimize size={16} />
@@ -1202,12 +1203,17 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1202
1203
  >
1203
1204
  <Stack className="ai-chat-bubble">
1204
1205
  <Text className="ai-chat-header">
1205
- <Text fw="bolder" size="xs">
1206
+ <Text
1207
+ fw="bolder"
1208
+ size="xs"
1209
+ style={{ whiteSpace: "nowrap" }}
1210
+ >
1206
1211
  {isUser
1207
1212
  ? I18n.get(labels.userLabel)
1208
1213
  : I18n.get(labels.assistantLabel)}
1209
1214
  </Text>
1210
- <Text size="xs">
1215
+ &nbsp;
1216
+ <Text size="xs" style={{ whiteSpace: "nowrap" }}>
1211
1217
  {new Date(msg.createdAt).toLocaleTimeString([], {
1212
1218
  hour: "2-digit",
1213
1219
  minute: "2-digit",
@@ -1238,6 +1244,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1238
1244
  onClick={() => handleEditCanceled(msg)}
1239
1245
  title={I18n.get(labels.editLabel)}
1240
1246
  aria-label={I18n.get(labels.editLabel)}
1247
+ data-ai-kit-edit-button
1241
1248
  >
1242
1249
  <IconPencil size={14} />
1243
1250
  </ActionIcon>
@@ -1291,6 +1298,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1291
1298
  onClick={() => updateFeedback(msg.id, "accepted")}
1292
1299
  aria-label={I18n.get(labels.acceptResponseLabel)}
1293
1300
  disabled={ai.busy}
1301
+ data-ai-kit-feedback-accept-button
1294
1302
  >
1295
1303
  👍
1296
1304
  </Button>
@@ -1302,6 +1310,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1302
1310
  onClick={() => updateFeedback(msg.id, "rejected")}
1303
1311
  aria-label={I18n.get(labels.rejectResponseLabel)}
1304
1312
  disabled={ai.busy}
1313
+ data-ai-kit-feedback-reject-button
1305
1314
  >
1306
1315
  👎
1307
1316
  </Button>
@@ -1356,13 +1365,18 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1356
1365
  {I18n.get("Are you sure you want to reset the conversation?")}
1357
1366
  </Text>
1358
1367
  <Group justify="flex-end" mt="md">
1359
- <Button variant="default" onClick={cancelReset}>
1368
+ <Button
1369
+ variant="default"
1370
+ onClick={cancelReset}
1371
+ data-ai-kit-no-button
1372
+ >
1360
1373
  {I18n.get("No")}
1361
1374
  </Button>
1362
1375
  <Button
1363
- color="red"
1376
+ color="var(--ai-kit-color-danger, red)"
1364
1377
  onClick={confirmReset}
1365
1378
  disabled={!hasMessages && !isChatBusy}
1379
+ data-ai-kit-yes-button
1366
1380
  >
1367
1381
  {I18n.get("Yes")}
1368
1382
  </Button>
@@ -1390,6 +1404,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1390
1404
  leftSection={<IconTrash size={18} />}
1391
1405
  onClick={handleResetClick}
1392
1406
  disabled={!hasMessages && !isChatBusy}
1407
+ data-ai-kit-reset-button
1393
1408
  >
1394
1409
  {I18n.get(labels.resetLabel)}
1395
1410
  </Button>
@@ -1402,6 +1417,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1402
1417
  onClick={() => fileInputRef.current?.click()}
1403
1418
  disabled={images.length >= resolvedMaxImages}
1404
1419
  title={I18n.get(labels.addImageLabel)}
1420
+ data-ai-kit-add-image-button
1405
1421
  >
1406
1422
  {I18n.get(labels.addLabel)}
1407
1423
  </Button>
@@ -1420,6 +1436,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1420
1436
  variant="filled"
1421
1437
  onClick={onSendOrCancel}
1422
1438
  disabled={!isChatBusy && !canSend}
1439
+ data-ai-kit-send-button
1423
1440
  >
1424
1441
  {sendOrCancelLabel}
1425
1442
  </Button>
@@ -1450,6 +1467,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1450
1467
  p={0}
1451
1468
  className="remove-image-button"
1452
1469
  title={I18n.get(labels.removeImageLabel)}
1470
+ data-ai-kit-remove-image-button
1453
1471
  >
1454
1472
  X
1455
1473
  </Button>
@@ -609,6 +609,7 @@
609
609
 
610
610
  /* Header / status */
611
611
  .ai-chat-header-bar {
612
+ background: var(--ai-kit-chat-surface);
612
613
  display: flex;
613
614
  align-items: center;
614
615
  justify-content: space-between;
@@ -704,6 +705,8 @@
704
705
  word-break: break-word;
705
706
  hyphens: auto;
706
707
  line-height: var(--ai-kit-line-height);
708
+ width: fit-content;
709
+ max-width: 80%;
707
710
  }
708
711
 
709
712
  .ai-chat-row.user .ai-chat-bubble {
@@ -1,3 +1,4 @@
1
+ import { generateColors } from "@mantine/colors-generator";
1
2
  import {
2
3
  colorsTuple,
3
4
  createTheme,
@@ -13,7 +14,7 @@ import {
13
14
  } from "@smart-cloud/ai-kit-core";
14
15
  import { useSelect } from "@wordpress/data";
15
16
  import { I18n } from "aws-amplify/utils";
16
- import { type ComponentType, useMemo, useState } from "react";
17
+ import { type ComponentType, useCallback, useMemo, useState } from "react";
17
18
  import { ShadowBoundary } from "./ShadowBoundary";
18
19
 
19
20
  export type AiKitShellInjectedProps = {
@@ -21,6 +22,17 @@ export type AiKitShellInjectedProps = {
21
22
  rootElement: HTMLElement;
22
23
  };
23
24
 
25
+ function hashStringDjb2(str: string): string {
26
+ let hash = 5381;
27
+ for (let i = 0; i < str.length; i++) {
28
+ hash = ((hash << 5) + hash) ^ str.charCodeAt(i);
29
+ }
30
+ // unsigned + base36
31
+ return (hash >>> 0).toString(36);
32
+ }
33
+
34
+ const STYLE_TEXT_ID = "ai-kit-style-text";
35
+
24
36
  export function withAiKitShell<P extends object>(
25
37
  RootComponent: ComponentType<P & AiKitShellInjectedProps>,
26
38
  propOverrides?: Partial<AiWorkerProps>,
@@ -96,10 +108,16 @@ export function withAiKitShell<P extends object>(
96
108
  if (colors) {
97
109
  customColors = {};
98
110
  Object.keys(colors).forEach((c) => {
99
- customColors![c] = colorsTuple(colors[c]);
111
+ try {
112
+ customColors![c] = generateColors(colors[c]);
113
+ } catch {
114
+ customColors![c] = colorsTuple(colors[c]);
115
+ }
100
116
  });
101
117
  }
102
118
 
119
+ console.log("withAiKitShell rendered", { customColors });
120
+
103
121
  const theme = createTheme({
104
122
  respectReducedMotion: true,
105
123
  ...(customColors && { colors: customColors }),
@@ -159,13 +177,43 @@ export function withAiKitShell<P extends object>(
159
177
  },
160
178
  });
161
179
 
180
+ const innerCSSHash = useMemo(() => {
181
+ return innerCSS ? hashStringDjb2(innerCSS) : "";
182
+ }, [innerCSS]);
183
+
184
+ const injectStyle = useCallback(
185
+ (rootElement: HTMLDivElement) => {
186
+ const existingStyle = rootElement.ownerDocument.getElementById(
187
+ STYLE_TEXT_ID,
188
+ ) as HTMLStyleElement | null;
189
+
190
+ if (innerCSS) {
191
+ if (!existingStyle) {
192
+ const s = rootElement.ownerDocument.createElement("style");
193
+ s.id = STYLE_TEXT_ID;
194
+ s.setAttribute("data-hash", innerCSSHash);
195
+ s.textContent = innerCSS;
196
+ rootElement.appendChild(s);
197
+ } else {
198
+ const prevHash = existingStyle.getAttribute("data-hash") || "";
199
+ if (prevHash !== innerCSSHash) {
200
+ existingStyle.setAttribute("data-hash", innerCSSHash);
201
+ existingStyle.textContent = innerCSS;
202
+ }
203
+ }
204
+ } else if (existingStyle) {
205
+ existingStyle.remove();
206
+ }
207
+ },
208
+ [innerCSS, innerCSSHash],
209
+ );
210
+
162
211
  return (
163
212
  <ShadowBoundary
164
213
  mode={variation === "modal" && !showOpenButton ? "overlay" : "local"}
165
214
  variation={variation}
166
215
  overlayRootId="ai-kit-overlay-root"
167
216
  stylesheets={stylesheets}
168
- innerCSS={innerCSS}
169
217
  className={className}
170
218
  rootElementId={
171
219
  variation === "modal" && !showOpenButton
@@ -174,6 +222,7 @@ export function withAiKitShell<P extends object>(
174
222
  }
175
223
  >
176
224
  {({ rootElement }) => {
225
+ injectStyle(rootElement);
177
226
  rootElement.setAttribute(
178
227
  "data-ai-kit-variation",
179
228
  variation || "default",
@@ -195,7 +244,6 @@ export function withAiKitShell<P extends object>(
195
244
  <MantineProvider
196
245
  forceColorScheme={resolved}
197
246
  theme={theme}
198
- cssVariablesSelector={`#${rootElement.id}`}
199
247
  getRootElement={() => rootElement as unknown as HTMLElement}
200
248
  >
201
249
  <RootComponent