@kwirthmagnify/kwirth-common-ai 0.5.68 → 0.5.70
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/back.js +59 -7
- package/dist/front.js +51 -45
- package/package.json +1 -1
package/dist/back.js
CHANGED
|
@@ -81,6 +81,11 @@ class UsageLimitError extends Error {
|
|
|
81
81
|
}
|
|
82
82
|
exports.UsageLimitError = UsageLimitError;
|
|
83
83
|
let usageService;
|
|
84
|
+
/* Accounting that degrades has to SAY SO, and loudly: while the counters are unreachable the ceilings are
|
|
85
|
+
not being enforced, and that is the kind of thing nobody notices until the bill arrives. `console.warn`
|
|
86
|
+
on purpose — this module has no logger of its own and must not acquire a dependency on one to be able
|
|
87
|
+
to report that it is flying blind. */
|
|
88
|
+
const logUsage = (msg) => console.warn(`[kwirth-common-ai] ${msg}`);
|
|
84
89
|
/* The core calls this at startup. Without it everything below is a no-op and the LLM is reached as ever. */
|
|
85
90
|
const setUsageService = (service) => { usageService = service; };
|
|
86
91
|
exports.setUsageService = setUsageService;
|
|
@@ -171,21 +176,68 @@ const runWithUsageGuard = async (options, call) => {
|
|
|
171
176
|
const origin = options.model && typeof options.model === 'object' ? modelOrigin.get(options.model) : undefined;
|
|
172
177
|
if (!service || !origin)
|
|
173
178
|
return await call();
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
+
/*
|
|
180
|
+
🔴 The bookkeeping must never be what breaks the feature.
|
|
181
|
+
|
|
182
|
+
Both halves of this used to be plain `await`s on a database, sitting in the critical path of every
|
|
183
|
+
AI call. When that database has a bad moment the cost is not a missing counter: it is the whole
|
|
184
|
+
call. Measured on 2026-10-14 — `core-ai-usage: Connection Error: Connection ended unexpectedly` one
|
|
185
|
+
second in, and then **exactly 120 seconds** until the caller's own timeout gave up. The AI never
|
|
186
|
+
got a chance; the two minutes were spent waiting on an accounting read that was never going to
|
|
187
|
+
answer, and the user saw a ticket open with no resolution guidance for no reason at all.
|
|
188
|
+
|
|
189
|
+
So: the read is BOUNDED and FAILS OPEN, and the write does not block the answer.
|
|
190
|
+
|
|
191
|
+
Failing open is a deliberate trade and it is the uncomfortable one: for the span of a sick usage
|
|
192
|
+
database, the ceilings are not enforced. The alternative is worse — an accounting hiccup leaving
|
|
193
|
+
every channel in Kwirth without AI. It is said loudly in the log so it cannot pass unnoticed, and
|
|
194
|
+
the limits apply again the moment the database answers.
|
|
195
|
+
*/
|
|
196
|
+
const READ_TIMEOUT_MS = 5000;
|
|
197
|
+
let read;
|
|
198
|
+
const readStarted = Date.now();
|
|
199
|
+
try {
|
|
200
|
+
read = await Promise.race([
|
|
201
|
+
service.read(EUsageScope.LLM_KEY, origin.subject),
|
|
202
|
+
new Promise(r => setTimeout(() => r(undefined), READ_TIMEOUT_MS))
|
|
203
|
+
]);
|
|
204
|
+
if (!read)
|
|
205
|
+
logUsage(`AI usage: the counters did not answer in ${READ_TIMEOUT_MS} ms — the call goes ahead and the LIMITS ARE NOT ENFORCED for it`);
|
|
179
206
|
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
logUsage(`AI usage: the counters could not be read (${err}) — the call goes ahead and the LIMITS ARE NOT ENFORCED for it`);
|
|
209
|
+
}
|
|
210
|
+
/* 🔴 Se mide y se dice cuando pasa de un segundo, porque sin esto no hay forma de saber dónde se fue
|
|
211
|
+
el tiempo. El 2026-10-14 una llamada agotó un tope de 120 s y no se pudo determinar si el culpable
|
|
212
|
+
era el LLM o esta lectura: el `abortSignal` del llamante arranca ANTES de llegar aquí, así que el
|
|
213
|
+
aborto salta dentro del SDK en los dos casos y el mensaje es idéntico. Un tramo sin instrumentar
|
|
214
|
+
obliga a adivinar, y adivinar ya costó una tarde. */
|
|
215
|
+
const readMs = Date.now() - readStarted;
|
|
216
|
+
if (readMs > 1000)
|
|
217
|
+
logUsage(`AI usage: reading the counters took ${readMs} ms before the model was even called`);
|
|
218
|
+
if (read) {
|
|
219
|
+
const exceeded = findExceeded(EUsageScope.LLM_KEY, origin.limits, read.daily, read.monthly);
|
|
220
|
+
if (exceeded) {
|
|
221
|
+
(_a = service.onCut) === null || _a === void 0 ? void 0 : _a.call(service, exceeded);
|
|
222
|
+
throw new UsageLimitError(exceeded);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const callStarted = Date.now();
|
|
180
226
|
const result = await call();
|
|
227
|
+
const callMs = Date.now() - callStarted;
|
|
228
|
+
if (callMs > 30000)
|
|
229
|
+
logUsage(`AI usage: the model took ${Math.round(callMs / 1000)} s to answer (counters read in ${readMs} ms)`);
|
|
181
230
|
const tokensIn = (_c = (_b = result.usage) === null || _b === void 0 ? void 0 : _b.inputTokens) !== null && _c !== void 0 ? _c : 0;
|
|
182
231
|
const tokensOut = (_e = (_d = result.usage) === null || _d === void 0 ? void 0 : _d.outputTokens) !== null && _e !== void 0 ? _e : 0;
|
|
183
|
-
await
|
|
232
|
+
/* Sin `await`: la respuesta ya está. Esperar al apunte solo añade el tiempo de la base a lo que el
|
|
233
|
+
usuario percibe, y si la base está mal, lo añade entero. Lo que se pierde al fallar es un contador;
|
|
234
|
+
lo que se perdía antes era la llamada. */
|
|
235
|
+
void service.add(EUsageScope.LLM_KEY, origin.subject, {
|
|
184
236
|
tokensIn,
|
|
185
237
|
tokensOut,
|
|
186
238
|
calls: 1,
|
|
187
239
|
cost: costOf(origin, tokensIn, tokensOut)
|
|
188
|
-
});
|
|
240
|
+
}).catch(err => logUsage(`AI usage: could not record ${tokensIn + tokensOut} token(s) of a call that DID happen (${err}) — the counters are now short`));
|
|
189
241
|
return result;
|
|
190
242
|
};
|
|
191
243
|
exports.runWithUsageGuard = runWithUsageGuard;
|
package/dist/front.js
CHANGED
|
@@ -223,44 +223,47 @@ const AiConfigLlm = (props) => {
|
|
|
223
223
|
Add/Update button below the fold on a short screen: the form grew, the dialog did not, and the
|
|
224
224
|
control that applies the form to the list scrolled out of sight.
|
|
225
225
|
*/
|
|
226
|
-
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '
|
|
226
|
+
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '90vw', maxWidth: '1200px', height: 'auto', maxHeight: '90vh' } } },
|
|
227
227
|
react_1.default.createElement(material_1.DialogTitle, { sx: { pb: 1 } }, "AI \u2014 LLM config"),
|
|
228
228
|
react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex' } },
|
|
229
|
-
react_1.default.createElement(material_1.Box, { sx: { flex:
|
|
229
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: '0 0 220px', display: 'flex', flexDirection: 'column', boxSizing: 'border-box' } },
|
|
230
230
|
react_1.default.createElement(material_1.Box, { sx: { flex: 1, overflowY: 'auto', overflowX: 'hidden', maxHeight: '70vh' } },
|
|
231
231
|
react_1.default.createElement(material_1.List, { sx: { flexGrow: 1, mr: 2, width: '100%' } }, llms.map((llm, index) => (react_1.default.createElement(material_1.ListItemButton, { key: index, selected: selectedIndex === index, onClick: () => onLlmSelected(index) },
|
|
232
232
|
react_1.default.createElement(material_1.Stack, { direction: 'column' },
|
|
233
233
|
react_1.default.createElement(material_1.Typography, { sx: { fontWeight: selectedIndex === index ? 'bold' : 'normal' } }, llm.id),
|
|
234
234
|
react_1.default.createElement(material_1.Typography, { color: 'darkgray', fontSize: 12 }, llm.provider)))))))),
|
|
235
|
-
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', alignItems: 'start', padding: '16px' } },
|
|
235
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', alignItems: 'start', padding: '16px', minWidth: 0 } },
|
|
236
236
|
react_1.default.createElement(material_1.Stack, { spacing: 2, style: { width: '100%' } },
|
|
237
|
-
react_1.default.createElement(material_1.Stack, { direction: '
|
|
238
|
-
react_1.default.createElement(material_1.
|
|
239
|
-
|
|
240
|
-
react_1.default.createElement(material_1.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
react_1.default.createElement(material_1.
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
react_1.default.createElement(material_1.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
react_1.default.createElement(material_1.
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
react_1.default.createElement(material_1.
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
237
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 3, alignItems: 'flex-start', sx: { flexWrap: 'wrap' } },
|
|
238
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: 1, minWidth: '260px' } },
|
|
239
|
+
react_1.default.createElement(material_1.TextField, { value: id, onChange: e => setId(e.target.value), placeholder: 'Enter LLM id', label: 'LLM ID', variant: 'standard', fullWidth: true }),
|
|
240
|
+
react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
241
|
+
react_1.default.createElement(material_1.InputLabel, null, "Provider"),
|
|
242
|
+
react_1.default.createElement(material_1.Select, { value: provider, onChange: e => { setProvider(e.target.value); setModel(''); }, variant: 'standard', fullWidth: true }, props.providers.map(p => {
|
|
243
|
+
var _a;
|
|
244
|
+
return (react_1.default.createElement(material_1.MenuItem, { key: p.name, value: p.name, disabled: !((_a = p.models) === null || _a === void 0 ? void 0 : _a.length) }, p.name));
|
|
245
|
+
}))),
|
|
246
|
+
((_a = props.providers.find(p => p.name === provider)) === null || _a === void 0 ? void 0 : _a.models.length)
|
|
247
|
+
? (react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
248
|
+
react_1.default.createElement(material_1.InputLabel, null, "Model"),
|
|
249
|
+
react_1.default.createElement(material_1.Select, { value: model, onChange: e => setModel(e.target.value), variant: 'standard', fullWidth: true, displayEmpty: true }, (_b = props.providers.find(p => p.name === provider)) === null || _b === void 0 ? void 0 : _b.models.map((m, i) => (react_1.default.createElement(material_1.MenuItem, { key: i, value: m.id }, m.name))))))
|
|
250
|
+
: (react_1.default.createElement(material_1.TextField, { value: model, onChange: e => setModel(e.target.value), label: 'Model ID', placeholder: 'e.g. claude-sonnet-4-5, glm-5.2', variant: 'standard', fullWidth: true })),
|
|
251
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
|
|
252
|
+
react_1.default.createElement(material_1.TextField, { value: temperature, onChange: e => setTemperature(+e.target.value), label: 'Temperature', variant: 'standard', type: 'number', sx: { width: '28%' } }),
|
|
253
|
+
react_1.default.createElement(material_1.TextField, { value: inputCostPerMillion, onChange: e => setInputCostPerMillion(e.target.value === '' ? '' : +e.target.value), label: 'Input cost /M', variant: 'standard', type: 'number', fullWidth: true, inputProps: { min: 0, step: 0.01 } }),
|
|
254
|
+
react_1.default.createElement(material_1.TextField, { value: outputCostPerMillion, onChange: e => setOutputCostPerMillion(e.target.value === '' ? '' : +e.target.value), label: 'Output cost /M', variant: 'standard', type: 'number', fullWidth: true, inputProps: { min: 0, step: 0.01 } })),
|
|
255
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', alignItems: 'center' },
|
|
256
|
+
react_1.default.createElement(material_1.Typography, { flex: 1 }, "Use provider API Key (or enter a specific one)"),
|
|
257
|
+
react_1.default.createElement(material_1.Checkbox, { checked: useProviderKey, onChange: e => setUseProviderKey(e.target.checked) })),
|
|
258
|
+
react_1.default.createElement(material_1.TextField, { value: key, onChange: e => setKey(e.target.value), disabled: useProviderKey, label: 'API Key', placeholder: 'Enter API Key', variant: 'standard', fullWidth: true, type: showPassword ? 'text' : 'password', InputProps: {
|
|
259
|
+
endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
|
|
260
|
+
react_1.default.createElement(material_1.IconButton, { onClick: () => setShowPassword(!showPassword), edge: 'end' }, showPassword ? react_1.default.createElement(VisibilityOff_1.default, null) : react_1.default.createElement(Visibility_1.default, null))))
|
|
261
|
+
} })),
|
|
262
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: 1, minWidth: '260px' } },
|
|
263
|
+
react_1.default.createElement(material_1.Typography, { variant: 'subtitle2' }, "Usage limits"),
|
|
264
|
+
useProviderKey
|
|
265
|
+
? react_1.default.createElement(material_1.Typography, { variant: 'caption', color: 'text.secondary' }, "Usage limits for this model live on its provider, because it borrows the provider's key \u2014 and that key is what gets billed.")
|
|
266
|
+
: react_1.default.createElement(UsageLimitsEditor, { value: limits, onChange: setLimits, subject: `this model's own key`, usage: (_c = props.usage) === null || _c === void 0 ? void 0 : _c[id], durable: props.usageDurable }))),
|
|
264
267
|
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
|
|
265
268
|
react_1.default.createElement(material_1.Button, { variant: 'outlined', size: 'small', onClick: onNew }, "New"),
|
|
266
269
|
react_1.default.createElement(material_1.Typography, { flex: 1 }),
|
|
@@ -365,10 +368,10 @@ const AiConfigProvider = (props) => {
|
|
|
365
368
|
setLoadingModels(false);
|
|
366
369
|
}
|
|
367
370
|
};
|
|
368
|
-
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '
|
|
371
|
+
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '90vw', maxWidth: '1200px', height: 'auto', maxHeight: '90vh' } } },
|
|
369
372
|
react_1.default.createElement(material_1.DialogTitle, { sx: { pb: 1 } }, "AI \u2014 Provider config"),
|
|
370
373
|
react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex' } },
|
|
371
|
-
react_1.default.createElement(material_1.Box, { sx: { flex:
|
|
374
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: '0 0 220px', display: 'flex', flexDirection: 'column', boxSizing: 'border-box' } },
|
|
372
375
|
react_1.default.createElement(material_1.Box, { sx: { flex: 1, overflowY: 'auto', maxHeight: '70vh' } },
|
|
373
376
|
react_1.default.createElement(material_1.List, { sx: { mr: 1 } }, providers.map((p, index) => {
|
|
374
377
|
var _a;
|
|
@@ -379,20 +382,23 @@ const AiConfigProvider = (props) => {
|
|
|
379
382
|
((_a = p.models) === null || _a === void 0 ? void 0 : _a.length) || 0,
|
|
380
383
|
" models loaded"))));
|
|
381
384
|
})))),
|
|
382
|
-
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', alignItems: 'start', padding: '
|
|
385
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', alignItems: 'start', padding: '16px', minWidth: 0 } },
|
|
383
386
|
react_1.default.createElement(material_1.Stack, { spacing: 2, style: { width: '100%' } },
|
|
384
|
-
react_1.default.createElement(material_1.
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
387
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 3, alignItems: 'flex-start', sx: { flexWrap: 'wrap' } },
|
|
388
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: 1, minWidth: '260px' } },
|
|
389
|
+
react_1.default.createElement(material_1.TextField, { label: 'Name', variant: 'standard', fullWidth: true, placeholder: 'e.g. huawei-maas, openai-prod', value: providerName, onChange: e => setProviderName(e.target.value), helperText: 'Unique identifier for this provider instance' }),
|
|
390
|
+
react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
391
|
+
react_1.default.createElement(material_1.InputLabel, null, "Type"),
|
|
392
|
+
react_1.default.createElement(material_1.Select, { value: providerType, onChange: e => { setProviderType(e.target.value); if (!providerName)
|
|
393
|
+
setProviderName(e.target.value); }, variant: 'standard', fullWidth: true }, props.providersAvailable.map(t => (react_1.default.createElement(material_1.MenuItem, { key: t, value: t }, t))))),
|
|
394
|
+
react_1.default.createElement(material_1.TextField, { label: 'API Key / Token', type: showPassword ? 'text' : 'password', variant: 'standard', fullWidth: true, value: providerKey, onChange: e => setProviderKey(e.target.value), helperText: 'This key can be afterwards linked to specific uses.', InputProps: {
|
|
395
|
+
endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
|
|
396
|
+
react_1.default.createElement(material_1.IconButton, { onClick: () => setShowPassword(!showPassword), edge: 'end' }, showPassword ? react_1.default.createElement(VisibilityOff_1.default, null) : react_1.default.createElement(Visibility_1.default, null))))
|
|
397
|
+
} }),
|
|
398
|
+
providerType === 'openai-compat' && (react_1.default.createElement(material_1.TextField, { label: 'Base URL', variant: 'standard', fullWidth: true, placeholder: 'https://your-maas-host/v1', value: providerEndpoint, onChange: e => setProviderEndpoint(e.target.value), helperText: 'OpenAI-compatible API base URL (e.g. Huawei MaaS, vLLM, LM Studio\u2026)' }))),
|
|
399
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: 1, minWidth: '260px' } },
|
|
400
|
+
react_1.default.createElement(material_1.Typography, { variant: 'subtitle2' }, "Usage limits"),
|
|
401
|
+
react_1.default.createElement(UsageLimitsEditor, { value: providerLimits, onChange: setProviderLimits, subject: `this provider's key`, disabled: !providerName.trim(), usage: (_b = props.usage) === null || _b === void 0 ? void 0 : _b[providerName], durable: props.usageDurable }))),
|
|
396
402
|
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, alignItems: 'center' },
|
|
397
403
|
react_1.default.createElement(material_1.Button, { variant: 'outlined', onClick: onNew }, "New"),
|
|
398
404
|
props.onLoadModels && (react_1.default.createElement(material_1.Button, { variant: 'outlined', onClick: onLoadModels, disabled: !providerType || !providerKey || loadingModels, startIcon: loadingModels ? react_1.default.createElement(material_1.CircularProgress, { size: 14 }) : undefined }, loadingModels ? 'Loading...' : `Load models${pendingModels.length ? ` (${pendingModels.length})` : ''}`)),
|