@smart-cloud/ai-kit-ui 1.4.14 → 1.4.15

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.4.14",
3
+ "version": "1.4.15",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -22,7 +22,7 @@
22
22
  "@emotion/cache": "^11.14.0",
23
23
  "@emotion/react": "^11.14.0",
24
24
  "@mantine/colors-generator": "^8.3.18",
25
- "@smart-cloud/ai-kit-core": "^1.4.9",
25
+ "@smart-cloud/ai-kit-core": "^1.4.10",
26
26
  "@smart-cloud/wpsuite-core": "^2.2.11",
27
27
  "@tabler/icons-react": "^3.41.1",
28
28
  "chroma-js": "^3.2.0",
@@ -65,8 +65,9 @@ const USE_AUDIO = false; // Set to true to enable audio recording feature (requi
65
65
  // New: history storage support
66
66
  const DEFAULT_PRESERVATION_TIME_DAYS = 1;
67
67
  const DEFAULT_HISTORY_STORAGE: HistoryStorageMode = "localstorage";
68
- const HISTORY_STORAGE_KEY = `ai-kit-chatbot-history-v1:${typeof window !== "undefined" ? window.location.hostname : "unknown"
69
- }`;
68
+ const HISTORY_STORAGE_KEY = `ai-kit-chatbot-history-v1:${
69
+ typeof window !== "undefined" ? window.location.hostname : "unknown"
70
+ }`;
70
71
 
71
72
  export const DEFAULT_CHATBOT_LABELS: Required<AiChatbotLabels> = {
72
73
  modalTitle: "AI Assistant",
@@ -126,6 +127,9 @@ type ChatResponse = {
126
127
  modelId?: string;
127
128
  requestId?: string;
128
129
  messageId?: string;
130
+ outputTokens?: number;
131
+ maxTokens?: number;
132
+ stopReason?: string;
129
133
  };
130
134
  };
131
135
 
@@ -222,7 +226,7 @@ const formatStatusEvent = (
222
226
  case "backend:response":
223
227
  return I18n.get(
224
228
  labels.assistantThinkingLabel ??
225
- DEFAULT_CHATBOT_LABELS.assistantThinkingLabel,
229
+ DEFAULT_CHATBOT_LABELS.assistantThinkingLabel,
226
230
  );
227
231
  case "done":
228
232
  return msg || I18n.get("Done.");
@@ -963,9 +967,9 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
963
967
  blob instanceof File
964
968
  ? blob
965
969
  : new File([blob], attachment.name || "attachment", {
966
- type:
967
- attachment.type || blob.type || "application/octet-stream",
968
- });
970
+ type:
971
+ attachment.type || blob.type || "application/octet-stream",
972
+ });
969
973
 
970
974
  const objectUrl = createObjectUrl(file);
971
975
  if (!objectUrl) continue;
@@ -1054,7 +1058,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1054
1058
  try {
1055
1059
  const activeSessionId =
1056
1060
  sessionRef.current &&
1057
- Date.now() - sessionRef.current.storedAt <
1061
+ Date.now() - sessionRef.current.storedAt <
1058
1062
  emptyHistoryAfterDays * TWENTY_FOUR_HOURS_MS
1059
1063
  ? sessionRef.current.id
1060
1064
  : undefined;
@@ -1148,7 +1152,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1148
1152
  try {
1149
1153
  const activeSessionId =
1150
1154
  sessionRef.current &&
1151
- Date.now() - sessionRef.current.storedAt <
1155
+ Date.now() - sessionRef.current.storedAt <
1152
1156
  emptyHistoryAfterDays * TWENTY_FOUR_HOURS_MS
1153
1157
  ? sessionRef.current.id
1154
1158
  : undefined;
@@ -1187,10 +1191,25 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1187
1191
  };
1188
1192
  }
1189
1193
 
1194
+ const resultText =
1195
+ typeof res.result === "string" ? res.result.trim() : "";
1196
+ if (!resultText) {
1197
+ throw new Error(I18n.get(ai.error ?? labels.emptyResponseLabel));
1198
+ }
1199
+
1200
+ if (res.metadata?.stopReason === "max_tokens") {
1201
+ console.warn("Chat response hit the backend output token limit", {
1202
+ maxTokens: res.metadata?.maxTokens,
1203
+ requestId: res.metadata?.requestId,
1204
+ modelId: res.metadata?.modelId,
1205
+ outputTokens: res.metadata?.outputTokens,
1206
+ });
1207
+ }
1208
+
1190
1209
  const assistantMessage: ChatMessage = {
1191
1210
  id: res.metadata?.messageId || createMessageId("assistant"),
1192
1211
  role: "assistant",
1193
- content: res.result || "",
1212
+ content: resultText,
1194
1213
  citations: res.citations,
1195
1214
  createdAt: Date.now(),
1196
1215
  };
@@ -1776,53 +1795,53 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
1776
1795
  att.mediaType === "image" ||
1777
1796
  (!att.mediaType && att.type.startsWith("image/")),
1778
1797
  ).length > 0 && (
1779
- <Group
1780
- className="ai-thumbs ai-message-thumbs"
1781
- gap="xs"
1782
- >
1783
- {msg.attachments
1784
- .filter(
1785
- (att) =>
1786
- att.mediaType === "image" ||
1787
- (!att.mediaType &&
1788
- att.type.startsWith("image/")),
1789
- )
1790
- .map((attachment) => (
1791
- <button
1792
- key={attachment.id}
1793
- type="button"
1794
- className="thumb"
1795
- style={{
1796
- backgroundImage: attachment.objectUrl
1797
- ? `url(${attachment.objectUrl})`
1798
- : undefined,
1799
- backgroundSize: "cover",
1800
- backgroundPosition: "center",
1801
- backgroundRepeat: "no-repeat",
1802
- }}
1803
- onClick={() =>
1804
- openAttachmentPreview(
1805
- attachment.objectUrl,
1806
- attachment.name,
1807
- )
1808
- }
1809
- disabled={!attachment.objectUrl}
1810
- title={
1811
- attachment.name || I18n.get("View image")
1812
- }
1813
- aria-label={
1814
- attachment.name || I18n.get("View image")
1815
- }
1816
- >
1817
- {!attachment.objectUrl && (
1818
- <Text size="xs" c="dimmed">
1819
- {I18n.get("Image no longer available")}
1820
- </Text>
1821
- )}
1822
- </button>
1823
- ))}
1824
- </Group>
1825
- )}
1798
+ <Group
1799
+ className="ai-thumbs ai-message-thumbs"
1800
+ gap="xs"
1801
+ >
1802
+ {msg.attachments
1803
+ .filter(
1804
+ (att) =>
1805
+ att.mediaType === "image" ||
1806
+ (!att.mediaType &&
1807
+ att.type.startsWith("image/")),
1808
+ )
1809
+ .map((attachment) => (
1810
+ <button
1811
+ key={attachment.id}
1812
+ type="button"
1813
+ className="thumb"
1814
+ style={{
1815
+ backgroundImage: attachment.objectUrl
1816
+ ? `url(${attachment.objectUrl})`
1817
+ : undefined,
1818
+ backgroundSize: "cover",
1819
+ backgroundPosition: "center",
1820
+ backgroundRepeat: "no-repeat",
1821
+ }}
1822
+ onClick={() =>
1823
+ openAttachmentPreview(
1824
+ attachment.objectUrl,
1825
+ attachment.name,
1826
+ )
1827
+ }
1828
+ disabled={!attachment.objectUrl}
1829
+ title={
1830
+ attachment.name || I18n.get("View image")
1831
+ }
1832
+ aria-label={
1833
+ attachment.name || I18n.get("View image")
1834
+ }
1835
+ >
1836
+ {!attachment.objectUrl && (
1837
+ <Text size="xs" c="dimmed">
1838
+ {I18n.get("Image no longer available")}
1839
+ </Text>
1840
+ )}
1841
+ </button>
1842
+ ))}
1843
+ </Group>
1844
+ )}
1826
1845
 
1827
1846
  {/* Audio attachments */}
1828
1847
  {msg.attachments