@smart-cloud/ai-kit-ui 1.5.2 → 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.
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,
@@ -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
+ });