@smart-cloud/ai-kit-ui 1.5.2 → 1.5.4
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/dist/ShadowBoundary.d.ts +1 -1
- package/dist/ai-feature/AiFeatureBorder.d.ts +1 -1
- package/dist/ai-feature/ProofreadDiff.d.ts +1 -1
- package/dist/ai-kit-ui.css +31 -4
- package/dist/errorHandling.d.ts +21 -0
- package/dist/index.cjs +9 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +9 -9
- package/dist/useAiRun.d.ts +2 -0
- package/package.json +40 -19
- package/src/ai-chatbot/AiChatbot.tsx +6 -2
- package/src/ai-feature/AiFeature.tsx +38 -23
- package/src/doc-search/DocSearch.tsx +44 -27
- package/src/errorHandling.ts +258 -0
- package/src/i18n/en.ts +15 -0
- package/src/i18n/hu.ts +15 -0
- package/src/index.tsx +1 -0
- package/src/styles/ai-kit-ui.css +31 -4
- package/src/useAiRun.ts +23 -29
- package/tests/error-handling.test.ts +82 -0
package/dist/useAiRun.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { AiKitLanguageCode, type AiKitStatusEvent } from "@smart-cloud/ai-kit-core";
|
|
2
|
+
import { type AiRunErrorDetails } from "./errorHandling";
|
|
2
3
|
export type AiRunState<T> = {
|
|
3
4
|
busy: boolean;
|
|
4
5
|
error: string | null;
|
|
6
|
+
errorDetails: AiRunErrorDetails | null;
|
|
5
7
|
statusEvent: AiKitStatusEvent | null;
|
|
6
8
|
result: T | null;
|
|
7
9
|
isCancelled: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smart-cloud/ai-kit-ui",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"build:js": "tsup --minify",
|
|
12
12
|
"build:types": "tsc -p tsconfig.types.json",
|
|
13
13
|
"lint": "eslint 'src/**/*.{ts,tsx}' --report-unused-disable-directives --max-warnings 0",
|
|
14
|
-
"test": "
|
|
15
|
-
"release:npm": "WPSUITE_PREMIUM=true yarn build && npm publish --access=public"
|
|
14
|
+
"test": "node --test tests/*.test.ts",
|
|
15
|
+
"release:npm": "WPSUITE_PREMIUM=true yarn build && npm publish --access=public --registry=https://registry.npmjs.org/"
|
|
16
16
|
},
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"registry": "https://registry.npmjs.org/",
|
|
@@ -22,11 +22,11 @@
|
|
|
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.5.
|
|
26
|
-
"@smart-cloud/wpsuite-core": "^2.3.
|
|
27
|
-
"@tabler/icons-react": "^3.
|
|
25
|
+
"@smart-cloud/ai-kit-core": "^1.5.2",
|
|
26
|
+
"@smart-cloud/wpsuite-core": "^2.3.2",
|
|
27
|
+
"@tabler/icons-react": "^3.46.0",
|
|
28
28
|
"chroma-js": "^3.2.0",
|
|
29
|
-
"country-flag-icons": "^1.6.
|
|
29
|
+
"country-flag-icons": "^1.6.20",
|
|
30
30
|
"react-markdown": "^10.1.0",
|
|
31
31
|
"rehype-parse": "^9.0.1",
|
|
32
32
|
"rehype-raw": "^7.0.0",
|
|
@@ -40,36 +40,57 @@
|
|
|
40
40
|
"unified": "^11.0.5"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
+
"@aws-amplify/core": "^6.18.0",
|
|
43
44
|
"@mantine/core": "^8.3.18",
|
|
44
45
|
"@mantine/hooks": "^8.3.18",
|
|
45
46
|
"@mantine/modals": "^8.3.18",
|
|
46
|
-
"@
|
|
47
|
-
"aws-amplify": "^6.
|
|
47
|
+
"@smart-cloud/aws-amplify-ui": "^6.16.0",
|
|
48
|
+
"@smart-cloud/aws-amplify-ui-react": "^6.16.0",
|
|
49
|
+
"@wordpress/data": "^10.52.0",
|
|
50
|
+
"@wordpress/i18n": "^6.25.0",
|
|
51
|
+
"aws-amplify": "^6.20.0",
|
|
52
|
+
"jose": "^6.2.5",
|
|
48
53
|
"react": "^18.3.1",
|
|
49
54
|
"react-dom": "^18.3.1"
|
|
50
55
|
},
|
|
51
56
|
"devDependencies": {
|
|
57
|
+
"@aws-amplify/core": "^6.18.0",
|
|
52
58
|
"@eslint/js": "^10.0.1",
|
|
53
59
|
"@mantine/core": "^8.3.18",
|
|
54
60
|
"@mantine/hooks": "^8.3.18",
|
|
55
61
|
"@mantine/modals": "^8.3.18",
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
58
|
-
"@types/
|
|
62
|
+
"@smart-cloud/aws-amplify-ui": "^6.16.0",
|
|
63
|
+
"@smart-cloud/aws-amplify-ui-react": "^6.16.0",
|
|
64
|
+
"@types/dom-chromium-ai": "^0.0.17",
|
|
65
|
+
"@types/jquery": "^4.0.1",
|
|
66
|
+
"@types/react": "^18.3.31",
|
|
59
67
|
"@types/react-dom": "^18.3.7",
|
|
60
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
61
|
-
"@typescript-eslint/parser": "^8.
|
|
62
|
-
"@wordpress/data": "^10.
|
|
68
|
+
"@typescript-eslint/eslint-plugin": "^8.65.0",
|
|
69
|
+
"@typescript-eslint/parser": "^8.65.0",
|
|
70
|
+
"@wordpress/data": "^10.52.0",
|
|
71
|
+
"@wordpress/i18n": "^6.25.0",
|
|
63
72
|
"ajv": "^8.20.0",
|
|
64
|
-
"aws-amplify": "^6.
|
|
65
|
-
"eslint": "^10.
|
|
66
|
-
"globals": "^17.
|
|
73
|
+
"aws-amplify": "^6.20.0",
|
|
74
|
+
"eslint": "^10.8.0",
|
|
75
|
+
"globals": "^17.8.0",
|
|
67
76
|
"jquery": "^4.0.0",
|
|
77
|
+
"jose": "^6.2.5",
|
|
68
78
|
"react": "^18.3.1",
|
|
69
79
|
"react-dom": "^18.3.1",
|
|
70
80
|
"tsup": "^8.5.1",
|
|
71
81
|
"typescript": "^6.0.3",
|
|
72
|
-
"typescript-eslint": "^8.
|
|
82
|
+
"typescript-eslint": "^8.65.0"
|
|
83
|
+
},
|
|
84
|
+
"resolutions": {
|
|
85
|
+
"brace-expansion": "^5.0.9",
|
|
86
|
+
"esbuild": "^0.28.1",
|
|
87
|
+
"fast-xml-builder": "^1.3.0",
|
|
88
|
+
"fast-xml-parser": "^5.10.1",
|
|
89
|
+
"fast-uri": "^3.1.4",
|
|
90
|
+
"flatted": "^3.4.4",
|
|
91
|
+
"picomatch": "^4.0.5",
|
|
92
|
+
"rollup": "^4.62.3",
|
|
93
|
+
"@emotion/react/**/yaml": "^1.10.3"
|
|
73
94
|
},
|
|
74
95
|
"exports": {
|
|
75
96
|
".": {
|
|
@@ -1232,7 +1232,6 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
|
|
|
1232
1232
|
// mark last sent timestamp on successful request completion
|
|
1233
1233
|
setLastUserSentAt(userMessageCreatedAt);
|
|
1234
1234
|
} catch (e) {
|
|
1235
|
-
console.error("Error during ask()", e);
|
|
1236
1235
|
// Cancel: treat as not sent, no error bubble
|
|
1237
1236
|
if (
|
|
1238
1237
|
cancelRequestedRef.current ||
|
|
@@ -1244,6 +1243,7 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
|
|
|
1244
1243
|
|
|
1245
1244
|
const msg =
|
|
1246
1245
|
(e as Error)?.message?.trim() || I18n.get(labels.unexpectedErrorLabel);
|
|
1246
|
+
setStatusLineError(msg);
|
|
1247
1247
|
|
|
1248
1248
|
// show error inside chat (assistant side)
|
|
1249
1249
|
setMessages((prev) => {
|
|
@@ -2012,7 +2012,11 @@ const AiChatbotBase: FC<AiChatbotProps & AiKitShellInjectedProps> = (props) => {
|
|
|
2012
2012
|
|
|
2013
2013
|
{/* Status line (below bubbles) */}
|
|
2014
2014
|
{showStatusLine && (
|
|
2015
|
-
<Group
|
|
2015
|
+
<Group
|
|
2016
|
+
className="ai-status-line"
|
|
2017
|
+
role={statusLineError ? "alert" : "status"}
|
|
2018
|
+
aria-live={statusLineError ? "assertive" : "polite"}
|
|
2019
|
+
>
|
|
2016
2020
|
<Text className="ai-status-text">
|
|
2017
2021
|
<em>{statusLineText}</em>
|
|
2018
2022
|
</Text>
|
|
@@ -64,6 +64,11 @@ import {
|
|
|
64
64
|
|
|
65
65
|
import { translations } from "../i18n";
|
|
66
66
|
import { PoweredBy } from "../poweredBy";
|
|
67
|
+
import {
|
|
68
|
+
getAiRunErrorMessage,
|
|
69
|
+
type AiRunErrorDetails,
|
|
70
|
+
normalizeAiRunError,
|
|
71
|
+
} from "../errorHandling";
|
|
67
72
|
import {
|
|
68
73
|
isBackendConfigured,
|
|
69
74
|
readDefaultOutputLanguage,
|
|
@@ -324,8 +329,7 @@ async function parseImageMetadataFromPromptResult(
|
|
|
324
329
|
: parsed.description
|
|
325
330
|
: "",
|
|
326
331
|
};
|
|
327
|
-
} catch
|
|
328
|
-
console.warn("AI Kit: failed to parse JSON metadata output", e);
|
|
332
|
+
} catch {
|
|
329
333
|
return {};
|
|
330
334
|
}
|
|
331
335
|
}
|
|
@@ -375,8 +379,7 @@ async function parsePostMetadataFromPromptResult(
|
|
|
375
379
|
: parsed.excerpt
|
|
376
380
|
: "",
|
|
377
381
|
};
|
|
378
|
-
} catch
|
|
379
|
-
console.warn("AI Kit: failed to parse JSON metadata output", e);
|
|
382
|
+
} catch {
|
|
380
383
|
return {};
|
|
381
384
|
}
|
|
382
385
|
}
|
|
@@ -467,6 +470,8 @@ const AiFeatureBase: FC<AiFeatureProps & AiKitShellInjectedProps> = (props) => {
|
|
|
467
470
|
const [optionsOpen, setOptionsOpen] = useState<boolean>(false);
|
|
468
471
|
const [backendConfigured, setBackendConfigured] = useState(false);
|
|
469
472
|
const [error, setError] = useState<string | null>(null);
|
|
473
|
+
const [errorDetails, setErrorDetails] =
|
|
474
|
+
useState<AiRunErrorDetails | null>(null);
|
|
470
475
|
const [generated, setGenerated] = useState<never | null>(null);
|
|
471
476
|
const [text, setText] = useState<string | undefined>(defaults?.text);
|
|
472
477
|
const [image] = useState<Blob | undefined>(defaults?.image);
|
|
@@ -664,6 +669,7 @@ const AiFeatureBase: FC<AiFeatureProps & AiKitShellInjectedProps> = (props) => {
|
|
|
664
669
|
setOptionsOpen(false);
|
|
665
670
|
}
|
|
666
671
|
setError(null);
|
|
672
|
+
setErrorDetails(null);
|
|
667
673
|
setGenerated(null);
|
|
668
674
|
|
|
669
675
|
const input = await inputText;
|
|
@@ -762,13 +768,6 @@ const AiFeatureBase: FC<AiFeatureProps & AiKitShellInjectedProps> = (props) => {
|
|
|
762
768
|
setDetectedLanguage(inputLang);
|
|
763
769
|
}
|
|
764
770
|
if (outLang === inputLang) {
|
|
765
|
-
console.warn(
|
|
766
|
-
"AI Kit: input and output languages are the same",
|
|
767
|
-
{
|
|
768
|
-
inputLang,
|
|
769
|
-
outLang,
|
|
770
|
-
},
|
|
771
|
-
);
|
|
772
771
|
setError(
|
|
773
772
|
I18n.get("Input and output languages cannot be the same."),
|
|
774
773
|
);
|
|
@@ -971,10 +970,9 @@ Follow these additional instructions: ${instructions}`
|
|
|
971
970
|
);
|
|
972
971
|
})) as string | null;
|
|
973
972
|
setGenerated(parsed as never);
|
|
974
|
-
} catch
|
|
973
|
+
} catch {
|
|
975
974
|
// If parsing fails, keep raw in the modal. User can still copy/paste.
|
|
976
975
|
setGenerated(cleaned as never);
|
|
977
|
-
console.warn("AI Kit: failed to parse SEO JSON", e);
|
|
978
976
|
}
|
|
979
977
|
|
|
980
978
|
break;
|
|
@@ -1057,21 +1055,22 @@ Follow these additional instructions: ${instructions}`
|
|
|
1057
1055
|
);
|
|
1058
1056
|
})) as string | null;
|
|
1059
1057
|
setGenerated(parsed as never);
|
|
1060
|
-
} catch
|
|
1058
|
+
} catch {
|
|
1061
1059
|
// If parsing fails, keep raw in the modal. User can still copy/paste.
|
|
1062
1060
|
setGenerated(cleaned as never);
|
|
1063
|
-
console.warn("AI Kit: failed to parse SEO JSON", e);
|
|
1064
1061
|
}
|
|
1065
1062
|
break;
|
|
1066
1063
|
}
|
|
1067
1064
|
}
|
|
1068
1065
|
}
|
|
1069
1066
|
} catch (e) {
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1067
|
+
const details = normalizeAiRunError(e);
|
|
1068
|
+
if (details.kind !== "cancelled") {
|
|
1069
|
+
setErrorDetails(details);
|
|
1070
|
+
setError(
|
|
1071
|
+
getAiRunErrorMessage(details, (message) => I18n.get(message)),
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1075
1074
|
}
|
|
1076
1075
|
setState(undefined);
|
|
1077
1076
|
},
|
|
@@ -1158,6 +1157,7 @@ Follow these additional instructions: ${instructions}`
|
|
|
1158
1157
|
setFeatureOpen(false);
|
|
1159
1158
|
setGenerated(null);
|
|
1160
1159
|
setError(null);
|
|
1160
|
+
setErrorDetails(null);
|
|
1161
1161
|
autoRunOnceRef.current = false;
|
|
1162
1162
|
ai.reset();
|
|
1163
1163
|
if (!showOpenButton) {
|
|
@@ -1213,8 +1213,7 @@ Follow these additional instructions: ${instructions}`
|
|
|
1213
1213
|
await waitForAiKitReady();
|
|
1214
1214
|
const v = await isBackendConfigured();
|
|
1215
1215
|
if (alive) setBackendConfigured(v);
|
|
1216
|
-
} catch
|
|
1217
|
-
console.error(e);
|
|
1216
|
+
} catch {
|
|
1218
1217
|
if (alive) setBackendConfigured(false);
|
|
1219
1218
|
}
|
|
1220
1219
|
})();
|
|
@@ -1508,7 +1507,23 @@ Follow these additional instructions: ${instructions}`
|
|
|
1508
1507
|
)}
|
|
1509
1508
|
|
|
1510
1509
|
{/* ERROR */}
|
|
1511
|
-
{error &&
|
|
1510
|
+
{error && (
|
|
1511
|
+
<Alert
|
|
1512
|
+
color="red"
|
|
1513
|
+
role="alert"
|
|
1514
|
+
aria-live="assertive"
|
|
1515
|
+
data-ai-kit-error
|
|
1516
|
+
>
|
|
1517
|
+
<Stack gap={2}>
|
|
1518
|
+
<Text size="sm">{I18n.get(error)}</Text>
|
|
1519
|
+
{errorDetails?.requestId ? (
|
|
1520
|
+
<Text size="xs" c="dimmed">
|
|
1521
|
+
{I18n.get("Request ID")}: {errorDetails.requestId}
|
|
1522
|
+
</Text>
|
|
1523
|
+
) : null}
|
|
1524
|
+
</Stack>
|
|
1525
|
+
</Alert>
|
|
1526
|
+
)}
|
|
1512
1527
|
|
|
1513
1528
|
{/* OVERRIDABLE PARAMETERS */}
|
|
1514
1529
|
{allowOverrideParameters && mode !== "proofread" && (
|
|
@@ -237,7 +237,7 @@ const DocSearchBase: FC<Props> = (props) => {
|
|
|
237
237
|
} | null>(null);
|
|
238
238
|
const AUDIO_CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours
|
|
239
239
|
|
|
240
|
-
const { busy, error, statusEvent, result, run, cancel, reset } =
|
|
240
|
+
const { busy, error, errorDetails, statusEvent, result, run, cancel, reset } =
|
|
241
241
|
useAiRun<SearchResult>();
|
|
242
242
|
|
|
243
243
|
const autoRunOnceRef = useRef(false);
|
|
@@ -441,8 +441,8 @@ const DocSearchBase: FC<Props> = (props) => {
|
|
|
441
441
|
const data = await loadMetadataOptionsFromBackend(effectiveContext);
|
|
442
442
|
|
|
443
443
|
setMetadataOptions(data);
|
|
444
|
-
} catch
|
|
445
|
-
|
|
444
|
+
} catch {
|
|
445
|
+
// Search remains available when optional filter metadata is unavailable.
|
|
446
446
|
} finally {
|
|
447
447
|
setLoadingMetadata(false);
|
|
448
448
|
}
|
|
@@ -487,29 +487,33 @@ const DocSearchBase: FC<Props> = (props) => {
|
|
|
487
487
|
}
|
|
488
488
|
|
|
489
489
|
reset();
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
...(enableUserFilters &&
|
|
504
|
-
selectedSubcategories.length > 0 && {
|
|
505
|
-
userSelectedSubcategories: selectedSubcategories,
|
|
490
|
+
try {
|
|
491
|
+
await run(async ({ signal, onStatus }) => {
|
|
492
|
+
return await sendSearchMessage(
|
|
493
|
+
{
|
|
494
|
+
sessionId,
|
|
495
|
+
...(q && { query: q }),
|
|
496
|
+
...(audioBlob && { audio: audioBlob }), // Pass Blob directly
|
|
497
|
+
topK,
|
|
498
|
+
// Include user-selected filters if enabled
|
|
499
|
+
// Always send userSelectedCategories array when enableUserFilters is true (even if empty)
|
|
500
|
+
// to prevent backend from applying its own kb-filter
|
|
501
|
+
...(enableUserFilters && {
|
|
502
|
+
userSelectedCategories: selectedCategories,
|
|
506
503
|
}),
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
504
|
+
...(enableUserFilters &&
|
|
505
|
+
selectedSubcategories.length > 0 && {
|
|
506
|
+
userSelectedSubcategories: selectedSubcategories,
|
|
507
|
+
}),
|
|
508
|
+
...(enableUserFilters &&
|
|
509
|
+
selectedTags.length > 0 && { userSelectedTags: selectedTags }),
|
|
510
|
+
},
|
|
511
|
+
{ signal, onStatus, context },
|
|
512
|
+
);
|
|
513
|
+
});
|
|
514
|
+
} catch {
|
|
515
|
+
// useAiRun stores a normalized, localized error for the accessible UI.
|
|
516
|
+
}
|
|
513
517
|
}, [
|
|
514
518
|
context,
|
|
515
519
|
inputText,
|
|
@@ -1050,8 +1054,21 @@ const DocSearchBase: FC<Props> = (props) => {
|
|
|
1050
1054
|
)}
|
|
1051
1055
|
|
|
1052
1056
|
{error ? (
|
|
1053
|
-
<Alert
|
|
1054
|
-
|
|
1057
|
+
<Alert
|
|
1058
|
+
color="red"
|
|
1059
|
+
title={I18n.get("Error")}
|
|
1060
|
+
role="alert"
|
|
1061
|
+
aria-live="assertive"
|
|
1062
|
+
data-ai-kit-error
|
|
1063
|
+
>
|
|
1064
|
+
<Stack gap={2}>
|
|
1065
|
+
<Text size="sm">{error}</Text>
|
|
1066
|
+
{errorDetails?.requestId ? (
|
|
1067
|
+
<Text size="xs" c="dimmed">
|
|
1068
|
+
{I18n.get("Request ID")}: {errorDetails.requestId}
|
|
1069
|
+
</Text>
|
|
1070
|
+
) : null}
|
|
1071
|
+
</Stack>
|
|
1055
1072
|
</Alert>
|
|
1056
1073
|
) : null}
|
|
1057
1074
|
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
export type AiRunErrorKind =
|
|
2
|
+
| "cancelled"
|
|
3
|
+
| "human-verification"
|
|
4
|
+
| "authorization"
|
|
5
|
+
| "throttled"
|
|
6
|
+
| "validation"
|
|
7
|
+
| "network"
|
|
8
|
+
| "server"
|
|
9
|
+
| "general";
|
|
10
|
+
|
|
11
|
+
export type AiRunErrorDetails = {
|
|
12
|
+
kind: AiRunErrorKind;
|
|
13
|
+
status?: number;
|
|
14
|
+
code?: string;
|
|
15
|
+
safeMessage?: string;
|
|
16
|
+
requestId?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type AiRunErrorFeedback = {
|
|
20
|
+
message: string | null;
|
|
21
|
+
details: AiRunErrorDetails | null;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type UnknownRecord = Record<string, unknown>;
|
|
25
|
+
|
|
26
|
+
function asRecord(value: unknown): UnknownRecord | null {
|
|
27
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
28
|
+
? (value as UnknownRecord)
|
|
29
|
+
: null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function asNonEmptyString(value: unknown): string | undefined {
|
|
33
|
+
if (typeof value !== "string") return undefined;
|
|
34
|
+
const normalized = Array.from(value, (character) => {
|
|
35
|
+
const code = character.charCodeAt(0);
|
|
36
|
+
return code <= 31 || code === 127 ? " " : character;
|
|
37
|
+
})
|
|
38
|
+
.join("")
|
|
39
|
+
.trim();
|
|
40
|
+
return normalized ? normalized.slice(0, 400) : undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function asStatus(value: unknown): number | undefined {
|
|
44
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
45
|
+
return Number.isInteger(parsed) && parsed >= 100 && parsed <= 599
|
|
46
|
+
? parsed
|
|
47
|
+
: undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function parsePayload(value: unknown): UnknownRecord | null {
|
|
51
|
+
const record = asRecord(value);
|
|
52
|
+
if (record) return record;
|
|
53
|
+
if (typeof value !== "string") return null;
|
|
54
|
+
|
|
55
|
+
const trimmed = value.trim();
|
|
56
|
+
const candidates = [trimmed];
|
|
57
|
+
const firstBrace = trimmed.indexOf("{");
|
|
58
|
+
const lastBrace = trimmed.lastIndexOf("}");
|
|
59
|
+
if (firstBrace >= 0 && lastBrace > firstBrace) {
|
|
60
|
+
candidates.push(trimmed.slice(firstBrace, lastBrace + 1));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const candidate of candidates) {
|
|
64
|
+
try {
|
|
65
|
+
return asRecord(JSON.parse(candidate));
|
|
66
|
+
} catch {
|
|
67
|
+
// Continue with the next safe JSON candidate.
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function headerValue(headers: unknown, name: string): string | undefined {
|
|
74
|
+
if (headers && typeof (headers as Headers).get === "function") {
|
|
75
|
+
return asNonEmptyString((headers as Headers).get(name));
|
|
76
|
+
}
|
|
77
|
+
const record = asRecord(headers);
|
|
78
|
+
if (!record) return undefined;
|
|
79
|
+
const entry = Object.entries(record).find(
|
|
80
|
+
([key]) => key.toLowerCase() === name.toLowerCase(),
|
|
81
|
+
);
|
|
82
|
+
return asNonEmptyString(entry?.[1]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function collectErrorRecords(error: unknown): UnknownRecord[] {
|
|
86
|
+
const records: UnknownRecord[] = [];
|
|
87
|
+
const visited = new Set<unknown>();
|
|
88
|
+
let current: unknown = error;
|
|
89
|
+
|
|
90
|
+
for (let depth = 0; depth < 4 && current && !visited.has(current); depth++) {
|
|
91
|
+
visited.add(current);
|
|
92
|
+
const record = asRecord(current);
|
|
93
|
+
if (!record) break;
|
|
94
|
+
records.push(record);
|
|
95
|
+
|
|
96
|
+
const response = asRecord(record.response);
|
|
97
|
+
if (response) records.push(response);
|
|
98
|
+
const body = parsePayload(response?.body ?? record.body ?? response?.data);
|
|
99
|
+
if (body) {
|
|
100
|
+
records.push(body);
|
|
101
|
+
const nestedError = asRecord(body.error);
|
|
102
|
+
if (nestedError) records.push(nestedError);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const embedded = parsePayload(record.message);
|
|
106
|
+
if (embedded) {
|
|
107
|
+
records.push(embedded);
|
|
108
|
+
const nestedError = asRecord(embedded.error);
|
|
109
|
+
if (nestedError) records.push(nestedError);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
current = record.cause;
|
|
113
|
+
}
|
|
114
|
+
return records;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function isAiRunAbort(error: unknown): boolean {
|
|
118
|
+
const records = collectErrorRecords(error);
|
|
119
|
+
return records.some((record) => {
|
|
120
|
+
const name = asNonEmptyString(record.name) ?? "";
|
|
121
|
+
const code = asNonEmptyString(record.code) ?? "";
|
|
122
|
+
const message = asNonEmptyString(record.message) ?? "";
|
|
123
|
+
return (
|
|
124
|
+
name === "AbortError" ||
|
|
125
|
+
code === "ABORT_ERR" ||
|
|
126
|
+
/\b(?:abort(?:ed)?|cancel(?:led|ed)?)\b/i.test(message)
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function normalizeAiRunError(error: unknown): AiRunErrorDetails {
|
|
132
|
+
if (isAiRunAbort(error)) {
|
|
133
|
+
return { kind: "cancelled" };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const records = collectErrorRecords(error);
|
|
137
|
+
const status = records
|
|
138
|
+
.map((record) =>
|
|
139
|
+
asStatus(
|
|
140
|
+
record.status ??
|
|
141
|
+
record.statusCode ??
|
|
142
|
+
asRecord(record.$metadata)?.httpStatusCode,
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
.find((value) => value !== undefined);
|
|
146
|
+
const code = records
|
|
147
|
+
.map((record) => asNonEmptyString(record.code ?? record.errorCode))
|
|
148
|
+
.find(Boolean);
|
|
149
|
+
const safeMessage = records
|
|
150
|
+
.map((record) =>
|
|
151
|
+
parsePayload(record.message)
|
|
152
|
+
? undefined
|
|
153
|
+
: asNonEmptyString(record.message),
|
|
154
|
+
)
|
|
155
|
+
.find(Boolean);
|
|
156
|
+
const requestId =
|
|
157
|
+
records
|
|
158
|
+
.map((record) =>
|
|
159
|
+
asNonEmptyString(
|
|
160
|
+
record.requestId ??
|
|
161
|
+
record.requestID ??
|
|
162
|
+
record.request_id ??
|
|
163
|
+
asRecord(record.$metadata)?.requestId,
|
|
164
|
+
),
|
|
165
|
+
)
|
|
166
|
+
.find(Boolean) ??
|
|
167
|
+
records
|
|
168
|
+
.map((record) =>
|
|
169
|
+
headerValue(record.headers ?? asRecord(record.response)?.headers, "x-request-id"),
|
|
170
|
+
)
|
|
171
|
+
.find(Boolean);
|
|
172
|
+
|
|
173
|
+
const normalizedCode = (code ?? "").toUpperCase();
|
|
174
|
+
const combined = `${normalizedCode} ${safeMessage ?? ""}`;
|
|
175
|
+
let kind: AiRunErrorKind = "general";
|
|
176
|
+
|
|
177
|
+
if (/HUMAN_VERIFICATION|RECAPTCHA|CAPTCHA/.test(combined)) {
|
|
178
|
+
kind = "human-verification";
|
|
179
|
+
} else if (
|
|
180
|
+
status === 401 ||
|
|
181
|
+
status === 403 ||
|
|
182
|
+
/UNAUTH|FORBIDDEN|ACCESS_DENIED|PERMISSION/.test(combined)
|
|
183
|
+
) {
|
|
184
|
+
kind = "authorization";
|
|
185
|
+
} else if (status === 429 || /THROTTL|RATE_LIMIT|TOO_MANY/.test(combined)) {
|
|
186
|
+
kind = "throttled";
|
|
187
|
+
} else if (
|
|
188
|
+
status === 400 ||
|
|
189
|
+
status === 409 ||
|
|
190
|
+
status === 422 ||
|
|
191
|
+
/VALIDATION|INVALID_REQUEST/.test(combined)
|
|
192
|
+
) {
|
|
193
|
+
kind = "validation";
|
|
194
|
+
} else if (
|
|
195
|
+
status === undefined &&
|
|
196
|
+
(error instanceof TypeError ||
|
|
197
|
+
/NETWORK|FAILED TO FETCH|LOAD FAILED|CONNECTION/.test(combined))
|
|
198
|
+
) {
|
|
199
|
+
kind = "network";
|
|
200
|
+
} else if (status !== undefined && status >= 500) {
|
|
201
|
+
kind = "server";
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { kind, status, code, safeMessage, requestId };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function getAiRunErrorMessage(
|
|
208
|
+
details: AiRunErrorDetails,
|
|
209
|
+
translate: (message: string) => string = (message) => message,
|
|
210
|
+
): string {
|
|
211
|
+
const messages: Record<AiRunErrorKind, string> = {
|
|
212
|
+
cancelled: "",
|
|
213
|
+
"human-verification":
|
|
214
|
+
"We couldn't verify that you're human. Please try again.",
|
|
215
|
+
authorization:
|
|
216
|
+
"You are not authorized to use this AI feature. Please sign in or contact the site owner.",
|
|
217
|
+
throttled: "Too many requests. Please wait a moment and try again.",
|
|
218
|
+
validation:
|
|
219
|
+
"The request could not be processed. Review your input and try again.",
|
|
220
|
+
network:
|
|
221
|
+
"We couldn't reach the AI service. Check your connection and try again.",
|
|
222
|
+
server: "The AI service is temporarily unavailable. Please try again.",
|
|
223
|
+
general: "Something went wrong. Please try again.",
|
|
224
|
+
};
|
|
225
|
+
return translate(messages[details.kind]);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function clearAiRunErrorFeedback(): AiRunErrorFeedback {
|
|
229
|
+
return { message: null, details: null };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function createAiRunErrorFeedback(
|
|
233
|
+
error: unknown,
|
|
234
|
+
translate?: (message: string) => string,
|
|
235
|
+
): AiRunErrorFeedback {
|
|
236
|
+
const details = normalizeAiRunError(error);
|
|
237
|
+
return {
|
|
238
|
+
message:
|
|
239
|
+
details.kind === "cancelled"
|
|
240
|
+
? null
|
|
241
|
+
: getAiRunErrorMessage(details, translate),
|
|
242
|
+
details,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export class AiRunError extends Error {
|
|
247
|
+
readonly details: AiRunErrorDetails;
|
|
248
|
+
|
|
249
|
+
constructor(
|
|
250
|
+
details: AiRunErrorDetails,
|
|
251
|
+
message: string = getAiRunErrorMessage(details),
|
|
252
|
+
options?: ErrorOptions,
|
|
253
|
+
) {
|
|
254
|
+
super(message, options);
|
|
255
|
+
this.name = "AiRunError";
|
|
256
|
+
this.details = details;
|
|
257
|
+
}
|
|
258
|
+
}
|
package/src/i18n/en.ts
CHANGED
|
@@ -177,6 +177,21 @@ export const enDict: Record<string, string> = {
|
|
|
177
177
|
shorter: "shorter",
|
|
178
178
|
Show: "Show",
|
|
179
179
|
"Something went wrong.": "Something went wrong.",
|
|
180
|
+
"Something went wrong. Please try again.":
|
|
181
|
+
"Something went wrong. Please try again.",
|
|
182
|
+
"We couldn't verify that you're human. Please try again.":
|
|
183
|
+
"We couldn't verify that you're human. Please try again.",
|
|
184
|
+
"You are not authorized to use this AI feature. Please sign in or contact the site owner.":
|
|
185
|
+
"You are not authorized to use this AI feature. Please sign in or contact the site owner.",
|
|
186
|
+
"Too many requests. Please wait a moment and try again.":
|
|
187
|
+
"Too many requests. Please wait a moment and try again.",
|
|
188
|
+
"The request could not be processed. Review your input and try again.":
|
|
189
|
+
"The request could not be processed. Review your input and try again.",
|
|
190
|
+
"We couldn't reach the AI service. Check your connection and try again.":
|
|
191
|
+
"We couldn't reach the AI service. Check your connection and try again.",
|
|
192
|
+
"The AI service is temporarily unavailable. Please try again.":
|
|
193
|
+
"The AI service is temporarily unavailable. Please try again.",
|
|
194
|
+
"Request ID": "Request ID",
|
|
180
195
|
Sources: "Sources",
|
|
181
196
|
Spanish: "Spanish",
|
|
182
197
|
Stop: "Stop",
|