@widgetic/creator 0.4.43 → 0.4.45
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/CreatorApp.svelte +88 -37
- package/dist/CreatorApp.svelte.d.ts +1 -0
- package/dist/components/WidgetDetails.svelte +18 -4
- package/dist/creator-types.d.ts +5 -0
- package/dist/errors/toUserFriendlyError.d.ts +44 -0
- package/dist/errors/toUserFriendlyError.js +236 -0
- package/dist/i18n/language.d.ts +77 -119
- package/dist/i18n/messages.d.ts +9 -0
- package/dist/i18n/messages.js +9 -0
- package/dist/i18n/statusText.d.ts +8 -3
- package/dist/i18n/statusText.js +48 -12
- package/dist/pageHelpers.d.ts +9 -3
- package/dist/pageHelpers.js +37 -15
- package/package.json +11 -11
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { ResponseError } from '@widgetic/api-sdk';
|
|
2
|
+
import { CREATOR_MESSAGES } from '../i18n/messages.js';
|
|
3
|
+
/** Display options wired through chat and status UI (context literals live here for i18n audits). */
|
|
4
|
+
export const CODEGEN_ERROR_DISPLAY = { context: 'codeGeneration' };
|
|
5
|
+
export const CANVAS_LOAD_ERROR_DISPLAY = { context: 'canvasLoad' };
|
|
6
|
+
/** Canonical English shown to visitors; single source of truth is the catalog (statusText KNOWN_TEXTS mirrors these). */
|
|
7
|
+
export const USER_FRIENDLY_COPY = {
|
|
8
|
+
providerNotConfigured: CREATOR_MESSAGES['creator.error.codeGenerationUnavailable'],
|
|
9
|
+
providerNotConfiguredGeneric: CREATOR_MESSAGES['creator.error.featureUnavailable'],
|
|
10
|
+
rateLimit: CREATOR_MESSAGES['creator.error.tooManyRequestsPleaseTry'],
|
|
11
|
+
usageLimit: CREATOR_MESSAGES['creator.error.usageLimitExceeded'],
|
|
12
|
+
timeout: CREATOR_MESSAGES['creator.error.requestTimedOut'],
|
|
13
|
+
network: CREATOR_MESSAGES['creator.error.couldNotReachTheServer'],
|
|
14
|
+
authExpired: CREATOR_MESSAGES['creator.status.authenticationExpired'],
|
|
15
|
+
notFound: CREATOR_MESSAGES['creator.error.resourceNotFound'],
|
|
16
|
+
canvasNotFound: CREATOR_MESSAGES['creator.error.canvasNotFound'],
|
|
17
|
+
permission: CREATOR_MESSAGES['creator.error.permissionDenied'],
|
|
18
|
+
validation: CREATOR_MESSAGES['creator.error.invalidRequest'],
|
|
19
|
+
server: CREATOR_MESSAGES['creator.error.serverError'],
|
|
20
|
+
generic: CREATOR_MESSAGES['creator.error.somethingWentWrong'],
|
|
21
|
+
codeGenerationFailed: CREATOR_MESSAGES['creator.error.codeGenerationFailed'],
|
|
22
|
+
};
|
|
23
|
+
const TECHNICAL_DETAIL_MAX = 300;
|
|
24
|
+
const ENV_KEY_RE = /\b(?:BASETEN|OPENAI|ANTHROPIC|GOOGLE|GEMINI|MISTRAL|COHERE|TOGETHER|FIREWORKS|DEEPSEEK|GROQ)[_\w]*_API_KEY\b/i;
|
|
25
|
+
const MISSING_KEY_RE = /missing api key|api key.*not configured|set \w+ in environment/i;
|
|
26
|
+
const PROVIDER_RE = /provider\s+["'`]|model not found|model unavailable|unavailable model/i;
|
|
27
|
+
const HTTP_STATUS_IN_TEXT_RE = /\b(?:HTTP\s*)?\b[45]\d{2}\b/;
|
|
28
|
+
const STACK_OR_CLASS_RE = /\bat\s+\S+\s*\(|ResponseError|TypeError|ZodError|SyntaxError/i;
|
|
29
|
+
function trimTechnicalDetail(raw) {
|
|
30
|
+
const oneLine = raw.replace(/\s+/g, ' ').trim();
|
|
31
|
+
if (oneLine.length <= TECHNICAL_DETAIL_MAX)
|
|
32
|
+
return oneLine;
|
|
33
|
+
return `${oneLine.slice(0, TECHNICAL_DETAIL_MAX)}…`;
|
|
34
|
+
}
|
|
35
|
+
/** Strip marks and prefixes the creator adds before showing a stored failure. */
|
|
36
|
+
export function stripErrorDecorations(text) {
|
|
37
|
+
let trimmed = text.trim();
|
|
38
|
+
trimmed = trimmed.replace(/^[^\p{L}\p{N}]+\s+/u, '');
|
|
39
|
+
trimmed = trimmed.replace(/^Code generation failed:\s*/i, '');
|
|
40
|
+
return trimmed.trim();
|
|
41
|
+
}
|
|
42
|
+
function readHttpStatus(error) {
|
|
43
|
+
if (error instanceof ResponseError && typeof error.response?.status === 'number') {
|
|
44
|
+
return error.response.status;
|
|
45
|
+
}
|
|
46
|
+
const response = error?.response;
|
|
47
|
+
if (response && typeof response.status === 'number')
|
|
48
|
+
return response.status;
|
|
49
|
+
const code = error?.code;
|
|
50
|
+
if (typeof code === 'number' && code >= 100 && code < 600)
|
|
51
|
+
return code;
|
|
52
|
+
if (typeof code === 'string' && /^\d{3}$/.test(code))
|
|
53
|
+
return Number(code);
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
function readErrorName(error) {
|
|
57
|
+
if (error instanceof Error && error.name)
|
|
58
|
+
return error.name;
|
|
59
|
+
if (typeof error === 'object' && error !== null && 'name' in error) {
|
|
60
|
+
const name = error.name;
|
|
61
|
+
if (typeof name === 'string')
|
|
62
|
+
return name;
|
|
63
|
+
}
|
|
64
|
+
return '';
|
|
65
|
+
}
|
|
66
|
+
function readRawMessage(error) {
|
|
67
|
+
if (error === undefined || error === null)
|
|
68
|
+
return '';
|
|
69
|
+
if (typeof error === 'string')
|
|
70
|
+
return stripErrorDecorations(error);
|
|
71
|
+
if (typeof error === 'number' || typeof error === 'boolean' || typeof error === 'bigint') {
|
|
72
|
+
return String(error);
|
|
73
|
+
}
|
|
74
|
+
if (error instanceof Error && typeof error.message === 'string') {
|
|
75
|
+
return stripErrorDecorations(error.message);
|
|
76
|
+
}
|
|
77
|
+
if (typeof error === 'object') {
|
|
78
|
+
const obj = error;
|
|
79
|
+
if (typeof obj.message === 'string' && obj.message.trim()) {
|
|
80
|
+
return stripErrorDecorations(obj.message);
|
|
81
|
+
}
|
|
82
|
+
if (typeof obj.error === 'string' && obj.error.trim()) {
|
|
83
|
+
return stripErrorDecorations(obj.error);
|
|
84
|
+
}
|
|
85
|
+
if (obj.error && typeof obj.error === 'object') {
|
|
86
|
+
const nested = readRawMessage(obj.error);
|
|
87
|
+
if (nested)
|
|
88
|
+
return nested;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return trimTechnicalDetail(JSON.stringify(error));
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return '';
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
function httpStatusFromText(raw) {
|
|
100
|
+
const match = raw.match(/\b(?:HTTP\s*)?([45]\d{2})\b/i);
|
|
101
|
+
return match ? Number(match[1]) : null;
|
|
102
|
+
}
|
|
103
|
+
function classify(raw, status, errorName, _context) {
|
|
104
|
+
const text = raw.toLowerCase();
|
|
105
|
+
const statusHit = status ?? httpStatusFromText(raw);
|
|
106
|
+
if (statusHit === 429 || /\brate\s*limit|too many requests/i.test(text))
|
|
107
|
+
return 'rateLimit';
|
|
108
|
+
if (statusHit === 402 ||
|
|
109
|
+
/usage limit|quota|credit|plan limit|budget exceeded/i.test(text) ||
|
|
110
|
+
text === 'usage limit exceeded') {
|
|
111
|
+
return 'usageLimit';
|
|
112
|
+
}
|
|
113
|
+
if (statusHit === 401 || /session expired|authentication expired|\bjwt\b/i.test(text))
|
|
114
|
+
return 'authExpired';
|
|
115
|
+
if (statusHit === 404 || /resource not found|not found/i.test(text))
|
|
116
|
+
return 'notFound';
|
|
117
|
+
if (statusHit === 403 || /forbidden|permission denied/i.test(text))
|
|
118
|
+
return 'permission';
|
|
119
|
+
if (statusHit === 400 || /validation|invalid request|bad request|zod/i.test(text))
|
|
120
|
+
return 'validation';
|
|
121
|
+
if ((statusHit !== null && statusHit >= 500) || /internal server error|service unavailable/i.test(text)) {
|
|
122
|
+
return 'server';
|
|
123
|
+
}
|
|
124
|
+
if (MISSING_KEY_RE.test(text) ||
|
|
125
|
+
ENV_KEY_RE.test(raw) ||
|
|
126
|
+
PROVIDER_RE.test(text) ||
|
|
127
|
+
/model not found|model unavailable/i.test(raw)) {
|
|
128
|
+
return 'providerNotConfigured';
|
|
129
|
+
}
|
|
130
|
+
if (/timed out|timeout|abort/i.test(text) || errorName === 'AbortError' || /\bETIMEDOUT\b/.test(raw)) {
|
|
131
|
+
return 'timeout';
|
|
132
|
+
}
|
|
133
|
+
if (/failed to fetch|networkerror|could not reach the server|econnrefused|enotfound|network request failed|widget builder is not reachable|not reachable/i.test(text)) {
|
|
134
|
+
return 'network';
|
|
135
|
+
}
|
|
136
|
+
if (HTTP_STATUS_IN_TEXT_RE.test(raw) || STACK_OR_CLASS_RE.test(raw) || ENV_KEY_RE.test(raw)) {
|
|
137
|
+
return 'unknown';
|
|
138
|
+
}
|
|
139
|
+
if (text.length === 0)
|
|
140
|
+
return 'unknown';
|
|
141
|
+
return 'unknown';
|
|
142
|
+
}
|
|
143
|
+
function messageForCategory(category, context) {
|
|
144
|
+
switch (category) {
|
|
145
|
+
case 'providerNotConfigured':
|
|
146
|
+
return context === 'codeGeneration'
|
|
147
|
+
? USER_FRIENDLY_COPY.providerNotConfigured
|
|
148
|
+
: USER_FRIENDLY_COPY.providerNotConfiguredGeneric;
|
|
149
|
+
case 'rateLimit':
|
|
150
|
+
return USER_FRIENDLY_COPY.rateLimit;
|
|
151
|
+
case 'usageLimit':
|
|
152
|
+
return USER_FRIENDLY_COPY.usageLimit;
|
|
153
|
+
case 'timeout':
|
|
154
|
+
return USER_FRIENDLY_COPY.timeout;
|
|
155
|
+
case 'network':
|
|
156
|
+
return USER_FRIENDLY_COPY.network;
|
|
157
|
+
case 'authExpired':
|
|
158
|
+
return USER_FRIENDLY_COPY.authExpired;
|
|
159
|
+
case 'notFound':
|
|
160
|
+
return context === 'canvasLoad' ? USER_FRIENDLY_COPY.canvasNotFound : USER_FRIENDLY_COPY.notFound;
|
|
161
|
+
case 'permission':
|
|
162
|
+
return USER_FRIENDLY_COPY.permission;
|
|
163
|
+
case 'validation':
|
|
164
|
+
return USER_FRIENDLY_COPY.validation;
|
|
165
|
+
case 'server':
|
|
166
|
+
return USER_FRIENDLY_COPY.server;
|
|
167
|
+
case 'unknown':
|
|
168
|
+
default:
|
|
169
|
+
return USER_FRIENDLY_COPY.generic;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** When the text is already one of our canonical user strings, keep it. */
|
|
173
|
+
function passthroughIfAlreadyFriendly(raw, category, context) {
|
|
174
|
+
const trimmed = raw.trim();
|
|
175
|
+
if (!trimmed)
|
|
176
|
+
return null;
|
|
177
|
+
if (category === 'notFound' && context === 'canvasLoad' && trimmed === USER_FRIENDLY_COPY.notFound) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const friendlyValues = new Set(Object.values(USER_FRIENDLY_COPY));
|
|
181
|
+
if (friendlyValues.has(trimmed))
|
|
182
|
+
return trimmed;
|
|
183
|
+
if (category === 'unknown' && !isLikelyTechnicalError(trimmed)) {
|
|
184
|
+
return trimmed;
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
/** Whether a stored line still looks like a raw server or runtime error. */
|
|
189
|
+
export function isLikelyTechnicalError(text) {
|
|
190
|
+
const trimmed = text.trim();
|
|
191
|
+
if (!trimmed)
|
|
192
|
+
return false;
|
|
193
|
+
if (MISSING_KEY_RE.test(trimmed) || ENV_KEY_RE.test(trimmed) || PROVIDER_RE.test(trimmed))
|
|
194
|
+
return true;
|
|
195
|
+
if (HTTP_STATUS_IN_TEXT_RE.test(trimmed) || STACK_OR_CLASS_RE.test(trimmed))
|
|
196
|
+
return true;
|
|
197
|
+
if (/direct llm|widget builder is not reachable|econnrefused|failed to fetch/i.test(trimmed))
|
|
198
|
+
return true;
|
|
199
|
+
if (trimmed.length > 400 && /[{[\]}]|"\w+":/.test(trimmed))
|
|
200
|
+
return true;
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
export function toUserFriendlyError(error, options) {
|
|
204
|
+
const context = options.context ?? 'generic';
|
|
205
|
+
const raw = readRawMessage(error);
|
|
206
|
+
const status = readHttpStatus(error);
|
|
207
|
+
const errorName = readErrorName(error);
|
|
208
|
+
const category = classify(raw, status, errorName, context);
|
|
209
|
+
const allowPassthrough = typeof error === 'string' || error instanceof Error || (typeof error === 'object' && error !== null);
|
|
210
|
+
const passthrough = allowPassthrough && typeof error !== 'number' && typeof error !== 'boolean' && typeof error !== 'bigint'
|
|
211
|
+
? passthroughIfAlreadyFriendly(raw, category, context)
|
|
212
|
+
: null;
|
|
213
|
+
const userMessage = passthrough ?? messageForCategory(category, context);
|
|
214
|
+
const technicalSource = raw || (typeof error === 'string' ? error : error instanceof Error ? error.message : '');
|
|
215
|
+
const technicalDetail = options.debugMode && technicalSource.trim() ? trimTechnicalDetail(technicalSource) : null;
|
|
216
|
+
return { userMessage, technicalDetail, category };
|
|
217
|
+
}
|
|
218
|
+
/** What to store or show: friendly text, with technical detail only in debug mode. */
|
|
219
|
+
export function formatUserFacingErrorText(error, options) {
|
|
220
|
+
const { userMessage, technicalDetail } = toUserFriendlyError(error, options);
|
|
221
|
+
if (!technicalDetail || technicalDetail === userMessage)
|
|
222
|
+
return userMessage;
|
|
223
|
+
return `${userMessage} (${technicalDetail})`;
|
|
224
|
+
}
|
|
225
|
+
/** Sanitize a string that is already stored in chat or UI state. */
|
|
226
|
+
export function friendlyTextFromStoredError(storedText, options) {
|
|
227
|
+
const stripped = stripErrorDecorations(storedText);
|
|
228
|
+
if (!stripped)
|
|
229
|
+
return storedText.trim();
|
|
230
|
+
const result = toUserFriendlyError(stripped, options);
|
|
231
|
+
if (!options.debugMode)
|
|
232
|
+
return result.userMessage;
|
|
233
|
+
if (!result.technicalDetail)
|
|
234
|
+
return result.userMessage;
|
|
235
|
+
return `${result.userMessage} (${result.technicalDetail})`;
|
|
236
|
+
}
|