@smart-cloud/ai-kit-ui 1.5.1 → 1.5.3

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.
@@ -49,6 +49,11 @@
49
49
  /* Useful “bridge” vars */
50
50
  --ai-kit-button-padding-x: var(--button-padding-x, 12px);
51
51
  --ai-kit-input-padding-x: var(--input-padding-x, 12px);
52
+ --ai-kit-action-transition:
53
+ background-color 160ms ease, border-color 160ms ease, color 160ms ease,
54
+ box-shadow 160ms ease, opacity 160ms ease, transform 160ms ease;
55
+ --ai-kit-action-focus-ring: var(--mantine-primary-color-filled, #1c7ed6);
56
+ --ai-kit-action-disabled-opacity: 0.6;
52
57
 
53
58
  /* ---------------------------
54
59
  1) Semantic color tokens
@@ -359,6 +364,31 @@
359
364
  min-width: var(--ai-kit-actions-button-min-width, 140px);
360
365
  }
361
366
 
367
+ /* Leave action colors to Mantine's resolved primary palette and the shared
368
+ WP Suite Theme CSS; this layer only standardizes interaction states. */
369
+ .ai-feature-root .mantine-Button-root,
370
+ .doc-search-root .mantine-Button-root,
371
+ .ai-docs-ask .mantine-Button-root,
372
+ .ai-docs-ask .mantine-ActionIcon-root {
373
+ transition: var(--ai-kit-action-transition);
374
+ }
375
+
376
+ .ai-feature-root .mantine-Button-root:focus-visible,
377
+ .doc-search-root .mantine-Button-root:focus-visible,
378
+ .ai-docs-ask .mantine-Button-root:focus-visible,
379
+ .ai-docs-ask .mantine-ActionIcon-root:focus-visible {
380
+ outline: 2px solid var(--ai-kit-action-focus-ring);
381
+ outline-offset: 2px;
382
+ }
383
+
384
+ .ai-feature-root .mantine-Button-root:disabled,
385
+ .doc-search-root .mantine-Button-root:disabled,
386
+ .ai-docs-ask .mantine-Button-root:disabled,
387
+ .ai-docs-ask .mantine-ActionIcon-root:disabled {
388
+ cursor: not-allowed;
389
+ opacity: var(--ai-kit-action-disabled-opacity);
390
+ }
391
+
362
392
  @media (max-width: var(--ai-kit-actions-breakpoint, 520px)) {
363
393
  .ai-kit-actions {
364
394
  flex-direction: column;
@@ -463,10 +493,7 @@
463
493
  gap: 2px;
464
494
 
465
495
  box-shadow: var(--ai-kit-launcher-shadow);
466
- transition:
467
- transform 0.2s ease,
468
- box-shadow 0.2s ease,
469
- background 0.2s ease;
496
+ transition: var(--ai-kit-action-transition);
470
497
 
471
498
  padding: var(--ai-kit-space-3);
472
499
  pointer-events: auto;
package/src/useAiRun.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { ApiError } from "@aws-amplify/api";
2
1
  import {
3
2
  AiKitLanguageCode,
4
3
  AiKitPlugin,
@@ -9,11 +8,20 @@ import {
9
8
  type AiKitStatusEvent,
10
9
  } from "@smart-cloud/ai-kit-core";
11
10
  import { getWpSuite } from "@smart-cloud/wpsuite-core";
11
+ import { I18n } from "aws-amplify/utils";
12
12
  import { useCallback, useRef, useState } from "react";
13
+ import {
14
+ AiRunError,
15
+ type AiRunErrorDetails,
16
+ clearAiRunErrorFeedback,
17
+ createAiRunErrorFeedback,
18
+ isAiRunAbort,
19
+ } from "./errorHandling";
13
20
 
14
21
  export type AiRunState<T> = {
15
22
  busy: boolean;
16
23
  error: string | null;
24
+ errorDetails: AiRunErrorDetails | null;
17
25
  statusEvent: AiKitStatusEvent | null;
18
26
  result: T | null;
19
27
  isCancelled: boolean;
@@ -34,27 +42,6 @@ export type UseAiRunResult<T> = AiRunState<T> & {
34
42
  signal: AbortSignal | null;
35
43
  };
36
44
 
37
- function getErrorMessage(err: unknown): string {
38
- if ((err as ApiError)?.response?.body) {
39
- try {
40
- return JSON.parse((err as ApiError)!.response!.body!).message;
41
- } catch {
42
- /* empty */
43
- }
44
- }
45
- if (err instanceof Error) {
46
- return err.message;
47
- }
48
- if (typeof err === "string") {
49
- return err;
50
- }
51
- try {
52
- return JSON.stringify(err);
53
- } catch {
54
- return "Unknown error";
55
- }
56
- }
57
-
58
45
  /**
59
46
  * Returns true if backend routing is configured in the current AiKit settings.
60
47
  *
@@ -102,7 +89,7 @@ export function useAiRun<T>(): UseAiRunResult<T> {
102
89
  const lastSourceRef = useRef<"on-device" | "backend" | null>(null);
103
90
 
104
91
  const [busy, setBusy] = useState(false);
105
- const [error, setError] = useState<string | null>(null);
92
+ const [errorFeedback, setErrorFeedback] = useState(clearAiRunErrorFeedback);
106
93
  const [statusEvent, setStatusEvent] = useState<AiKitStatusEvent | null>(null);
107
94
  const [result, setResult] = useState<T | null>(null);
108
95
  const [isCancelled, setIsCancelled] = useState(false);
@@ -118,7 +105,7 @@ export function useAiRun<T>(): UseAiRunResult<T> {
118
105
  ctrlRef.current?.abort();
119
106
  ctrlRef.current = null;
120
107
  setBusy(false);
121
- setError(null);
108
+ setErrorFeedback(clearAiRunErrorFeedback());
122
109
  setStatusEvent(null);
123
110
  setResult(null);
124
111
  setIsCancelled(false);
@@ -135,7 +122,7 @@ export function useAiRun<T>(): UseAiRunResult<T> {
135
122
  ctrlRef.current = ctrl;
136
123
 
137
124
  setBusy(true);
138
- setError(null);
125
+ setErrorFeedback(clearAiRunErrorFeedback());
139
126
  setStatusEvent(null);
140
127
  setResult(null);
141
128
  setIsCancelled(false);
@@ -156,12 +143,18 @@ export function useAiRun<T>(): UseAiRunResult<T> {
156
143
  setResult(value);
157
144
  return value;
158
145
  } catch (err) {
159
- if (ctrl.signal.aborted) {
146
+ if (ctrl.signal.aborted || isAiRunAbort(err)) {
160
147
  setIsCancelled(true);
161
- setError(null);
148
+ setErrorFeedback(clearAiRunErrorFeedback());
162
149
  return null;
163
150
  }
164
- throw new Error(getErrorMessage(err), { cause: err });
151
+ const feedback = createAiRunErrorFeedback(err, (message) =>
152
+ I18n.get(message),
153
+ );
154
+ setErrorFeedback(feedback);
155
+ throw new AiRunError(feedback.details!, feedback.message!, {
156
+ cause: err,
157
+ });
165
158
  } finally {
166
159
  setBusy(false);
167
160
  setStatusEvent(null);
@@ -175,7 +168,8 @@ export function useAiRun<T>(): UseAiRunResult<T> {
175
168
 
176
169
  return {
177
170
  busy,
178
- error,
171
+ error: errorFeedback.message,
172
+ errorDetails: errorFeedback.details,
179
173
  statusEvent,
180
174
  result,
181
175
  isCancelled,
@@ -96,18 +96,26 @@ export function withAiKitShell<P extends object>(
96
96
  }, [currentLanguage, direction, directionInStore, directionOverride]);
97
97
 
98
98
  const stylesheets = useMemo(
99
- () => [
100
- (
101
- WpSuite as never as {
102
- constants: { aiKit: { mantineCssHref: string } };
103
- }
104
- )?.constants?.aiKit?.mantineCssHref,
105
- (
106
- WpSuite as never as {
107
- constants: { aiKit: { aiKitUiCssHref: string } };
108
- }
109
- )?.constants?.aiKit?.aiKitUiCssHref,
110
- ],
99
+ () =>
100
+ [
101
+ (
102
+ WpSuite as never as {
103
+ constants: { aiKit: { mantineCssHref: string } };
104
+ }
105
+ )?.constants?.aiKit?.mantineCssHref,
106
+ (
107
+ WpSuite as never as {
108
+ constants: { aiKit: { aiKitUiCssHref: string } };
109
+ }
110
+ )?.constants?.aiKit?.aiKitUiCssHref,
111
+ (
112
+ WpSuite as never as {
113
+ constants: { aiKit: { wpsuiteThemeCssHref?: string } };
114
+ }
115
+ )?.constants?.aiKit?.wpsuiteThemeCssHref,
116
+ ].filter(
117
+ (href): href is string => typeof href === "string" && href.length > 0,
118
+ ),
111
119
  [WpSuite],
112
120
  );
113
121
 
@@ -0,0 +1,82 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import {
5
+ clearAiRunErrorFeedback,
6
+ createAiRunErrorFeedback,
7
+ getAiRunErrorMessage,
8
+ normalizeAiRunError,
9
+ } from "../src/errorHandling.ts";
10
+
11
+ function backendError(status: number, body: Record<string, unknown>) {
12
+ return {
13
+ name: "ApiError",
14
+ message: `Backend request failed: ${JSON.stringify(body)}`,
15
+ response: {
16
+ statusCode: status,
17
+ body: JSON.stringify(body),
18
+ headers: { "x-request-id": body.requestId },
19
+ },
20
+ };
21
+ }
22
+
23
+ test("normalizes reCAPTCHA rejection without exposing request data", () => {
24
+ const details = normalizeAiRunError(
25
+ backendError(403, {
26
+ code: "HUMAN_VERIFICATION_ERROR",
27
+ message: "Assessment rejected",
28
+ requestId: "req-human-1",
29
+ token: "must-not-leak",
30
+ }),
31
+ );
32
+
33
+ assert.deepEqual(details, {
34
+ kind: "human-verification",
35
+ status: 403,
36
+ code: "HUMAN_VERIFICATION_ERROR",
37
+ safeMessage: "Assessment rejected",
38
+ requestId: "req-human-1",
39
+ });
40
+ assert.equal(
41
+ getAiRunErrorMessage(details),
42
+ "We couldn't verify that you're human. Please try again.",
43
+ );
44
+ assert.doesNotMatch(JSON.stringify(details), /must-not-leak/);
45
+ });
46
+
47
+ test("distinguishes authorization, throttling, network, and backend failures", () => {
48
+ assert.equal(
49
+ normalizeAiRunError(backendError(401, { code: "UNAUTHORIZED" })).kind,
50
+ "authorization",
51
+ );
52
+ assert.equal(
53
+ normalizeAiRunError(backendError(429, { code: "THROTTLED" })).kind,
54
+ "throttled",
55
+ );
56
+ assert.equal(
57
+ normalizeAiRunError(new TypeError("Failed to fetch")).kind,
58
+ "network",
59
+ );
60
+ assert.equal(
61
+ normalizeAiRunError(backendError(503, { code: "UPSTREAM_ERROR" })).kind,
62
+ "server",
63
+ );
64
+ });
65
+
66
+ test("treats intentional cancellation as non-error feedback", () => {
67
+ const feedback = createAiRunErrorFeedback(
68
+ new DOMException("Aborted", "AbortError"),
69
+ );
70
+ assert.equal(feedback.details?.kind, "cancelled");
71
+ assert.equal(feedback.message, null);
72
+ });
73
+
74
+ test("beginning a successful retry clears stale error feedback", () => {
75
+ const failed = createAiRunErrorFeedback(
76
+ backendError(429, { code: "THROTTLED" }),
77
+ );
78
+ assert.equal(failed.details?.kind, "throttled");
79
+
80
+ const retry = clearAiRunErrorFeedback();
81
+ assert.deepEqual(retry, { message: null, details: null });
82
+ });