@kwirthmagnify/kwirth-common-ai 0.5.65 → 0.5.67

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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ILlm, ILlmProvider, ECapability, EToolEffect, EToolSensitivity, EUsagePeriod, EUsageUnit, IAiToolInfo, IAiToolsetInfo, IToolsetConfig } from './index';
1
+ import { ILlm, ILlmProvider, ECapability, EToolEffect, EToolSensitivity, EUsagePeriod, EUsageUnit, IAiToolInfo, IAiToolsetInfo, IToolsetConfig, IUsageAmounts } from './index';
2
2
  import type { AppsV1Api, CoreV1Api, CustomObjectsApi, NetworkingV1Api } from '@kubernetes/client-node';
3
3
  interface ILogChannel {
4
4
  logInfo?: (msg: string) => void;
@@ -12,12 +12,7 @@ export declare enum EUsageScope {
12
12
  LLM_KEY = "llmkey",
13
13
  CHANNEL = "channel"
14
14
  }
15
- export interface IUsageAmounts {
16
- tokensIn: number;
17
- tokensOut: number;
18
- calls: number;
19
- cost: number;
20
- }
15
+ export type { IUsageAmounts, IUsageTotals } from './index';
21
16
  export interface IUsageLimitExceeded {
22
17
  scope: EUsageScope;
23
18
  unit: EUsageUnit;
@@ -40,6 +35,7 @@ export declare class UsageLimitError extends Error {
40
35
  }
41
36
  export declare const setUsageService: (service: IUsageService | undefined) => void;
42
37
  export declare const getUsageService: () => IUsageService | undefined;
38
+ export declare const usageSubjectForKey: (key: string) => string;
43
39
  export declare const runWithUsageGuard: (options: {
44
40
  model?: unknown;
45
41
  }, call: () => Promise<{
package/dist/back.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildAgentTools = exports.resolveTools = exports.resolveToolRef = exports.invokeToolRef = exports.buildToolHost = exports.listToolsetInfos = exports.listToolsets = exports.getToolset = exports.unregisterToolset = exports.registerToolset = exports.isToolsetGrantedTo = exports.getToolsetGrants = exports.setToolsetGrants = exports.isBuiltInToolsetId = exports.defineTool = exports.runWithToolContext = exports.EToolEffect = exports.zodFromExample = exports.z = exports.tool = exports.stepCountIs = exports.Output = exports.generateText = exports.loadModels = exports.generateVision = exports.runWithUsageGuard = exports.getUsageService = exports.setUsageService = exports.UsageLimitError = exports.EUsageScope = exports.buildModel = void 0;
3
+ exports.buildAgentTools = exports.resolveTools = exports.resolveToolRef = exports.invokeToolRef = exports.buildToolHost = exports.listToolsetInfos = exports.listToolsets = exports.getToolset = exports.unregisterToolset = exports.registerToolset = exports.isToolsetGrantedTo = exports.getToolsetGrants = exports.setToolsetGrants = exports.isBuiltInToolsetId = exports.defineTool = exports.runWithToolContext = exports.EToolEffect = exports.zodFromExample = exports.z = exports.tool = exports.stepCountIs = exports.Output = exports.generateText = exports.loadModels = exports.generateVision = exports.runWithUsageGuard = exports.usageSubjectForKey = exports.getUsageService = exports.setUsageService = exports.UsageLimitError = exports.EUsageScope = exports.buildModel = void 0;
4
4
  const index_1 = require("./index");
5
5
  // Aliased on purpose: the generateText this module EXPORTS is the wrapped one, and nothing below should
6
6
  // be able to reach the raw SDK call by accident — that is exactly how a call would escape the counters.
@@ -93,6 +93,13 @@ const modelOrigin = new WeakMap();
93
93
  it. And the counters table ends up holding no secret.
94
94
  */
95
95
  const keyFingerprint = (key) => (0, crypto_1.createHash)('sha256').update(key).digest('hex').slice(0, 32);
96
+ /*
97
+ The same subject the counters are written under, for whoever needs to READ them back — the core, to
98
+ show an admin what a provider or a model has spent. Exported so that is computed in one place: a
99
+ second implementation of this hash would show consumption that silently belongs to nobody.
100
+ */
101
+ const usageSubjectForKey = (key) => keyFingerprint(key);
102
+ exports.usageSubjectForKey = usageSubjectForKey;
96
103
  /*
97
104
  Records what a model was built from, so generateText can count against it. Called by buildModel; a
98
105
  model built any other way simply has no origin and is not counted — never silently miscounted.
package/dist/front.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { ILlm, ILlmModel, ILlmProvider } from './index';
2
+ import { ILlm, ILlmModel, ILlmProvider, IUsageTotals } from './index';
3
3
  interface ILlmSelectorProps {
4
4
  llms: ILlm[];
5
5
  value: string;
@@ -13,6 +13,8 @@ interface IAiConfigLlmProps {
13
13
  onClose: (llms: ILlm[] | undefined) => void;
14
14
  providers: ILlmProvider[];
15
15
  llms: ILlm[];
16
+ usage?: Record<string, IUsageTotals>;
17
+ usageDurable?: boolean;
16
18
  }
17
19
  declare const AiConfigLlm: React.FC<IAiConfigLlmProps>;
18
20
  interface IAiConfigProviderProps {
@@ -20,6 +22,8 @@ interface IAiConfigProviderProps {
20
22
  providers: ILlmProvider[];
21
23
  onClose: (providers: ILlmProvider[] | undefined) => void;
22
24
  onLoadModels?: (provider: ILlmProvider) => Promise<ILlmModel[]>;
25
+ usage?: Record<string, IUsageTotals>;
26
+ usageDurable?: boolean;
23
27
  }
24
28
  declare const AiConfigProvider: React.FC<IAiConfigProviderProps>;
25
29
  interface IToolSelectorProps {
package/dist/front.js CHANGED
@@ -69,6 +69,75 @@ const downloadJson = async (data, filename) => {
69
69
  a.click();
70
70
  URL.revokeObjectURL(url);
71
71
  };
72
+ const index_1 = require("./index");
73
+ /* The four units in the order an admin thinks about them: what it costs, how often, how much it moved. */
74
+ const USAGE_ROWS = [
75
+ { unit: index_1.EUsageUnit.COST, label: 'Cost (€/$)', of: a => a.cost },
76
+ { unit: index_1.EUsageUnit.CALLS, label: 'Calls', of: a => a.calls },
77
+ { unit: index_1.EUsageUnit.TOKENS_IN, label: 'Input tokens', of: a => a.tokensIn },
78
+ { unit: index_1.EUsageUnit.TOKENS_OUT, label: 'Output tokens', of: a => a.tokensOut }
79
+ ];
80
+ /* 1234.5 → '1,235' and 0.42 → '0.42': a cost needs its decimals, a token count is noise with them. */
81
+ const formatSpent = (value) => value >= 100 ? Math.round(value).toLocaleString() : String(Math.round(value * 100) / 100);
82
+ /*
83
+ The ceilings of one subject — a provider key or a model's own key. Shared by both dialogs because the
84
+ rule is the same in both, and two copies of it would drift.
85
+
86
+ Each unit carries its own switch, apart from its numbers: turning a limit off for a week should not
87
+ cost the admin the numbers he had worked out.
88
+ */
89
+ const UsageLimitsEditor = (props) => {
90
+ var _a;
91
+ const limits = (_a = props.value) !== null && _a !== void 0 ? _a : {};
92
+ const patch = (unit, change) => {
93
+ var _a;
94
+ const current = (_a = limits[unit]) !== null && _a !== void 0 ? _a : { enabled: false };
95
+ props.onChange(Object.assign(Object.assign({}, limits), { [unit]: Object.assign(Object.assign({}, current), change) }));
96
+ };
97
+ const numberOrUndefined = (text) => text === '' ? undefined : +text;
98
+ /*
99
+ Compact on purpose. These rows used to be three paragraphs of prose and four full-height fields,
100
+ which pushed the Add/Update button below the fold — and that button is the one that applies the
101
+ form to the list, so a limit typed above it looked saved and was not. Height here is not a
102
+ cosmetic matter: it is what decides whether the control that commits the edit can be seen.
103
+
104
+ What was dropped is repetition, not meaning. The one thing an admin must not learn the hard way
105
+ — that a tokens or cost ceiling stops the NEXT call, because the amount is only known once the
106
+ model has answered — is still said, in one line, where the limit is set.
107
+ */
108
+ return (react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 0.5, sx: { mt: 0.5 } },
109
+ react_1.default.createElement(material_1.Typography, { variant: 'caption', color: 'text.secondary' },
110
+ "Stop spending on ",
111
+ react_1.default.createElement("b", null, props.subject),
112
+ ": a call goes out only if ",
113
+ react_1.default.createElement("b", null, "no"),
114
+ " enabled ceiling is reached. \u26A0\uFE0F A ",
115
+ react_1.default.createElement("b", null, "calls"),
116
+ " ceiling stops the call that would exceed it; ",
117
+ react_1.default.createElement("b", null, "tokens"),
118
+ " and",
119
+ react_1.default.createElement("b", null, " cost"),
120
+ " are only known after the answer, so those stop the ",
121
+ react_1.default.createElement("b", null, "next"),
122
+ " one."),
123
+ props.usage && props.durable === false &&
124
+ react_1.default.createElement(material_1.Typography, { variant: 'caption', color: 'warning.main' },
125
+ "\u26A0\uFE0F Counters kept ",
126
+ react_1.default.createElement("b", null, "in memory"),
127
+ " (no SQL): they reset on every restart, and so do these limits."),
128
+ USAGE_ROWS.map(({ unit, label, of }) => {
129
+ var _a, _b, _c, _d, _e;
130
+ const w = (_a = limits[unit]) !== null && _a !== void 0 ? _a : { enabled: false };
131
+ // What has been spent goes in the helper text of its own window: next to the number it is
132
+ // measured against, which is the only place it answers anything. It only costs height when
133
+ // there IS usage, which is when it earns it.
134
+ const spent = (amounts) => amounts ? `${formatSpent(of(amounts))} so far` : undefined;
135
+ return (react_1.default.createElement(material_1.Stack, { key: unit, direction: 'row', spacing: 1, alignItems: 'flex-start' },
136
+ react_1.default.createElement(material_1.FormControlLabel, { sx: { width: '150px', mr: 0, mt: 0.5 }, control: react_1.default.createElement(material_1.Checkbox, { size: 'small', checked: !!w.enabled, disabled: props.disabled, onChange: e => patch(unit, { enabled: e.target.checked }) }), label: react_1.default.createElement(material_1.Typography, { variant: 'body2' }, label) }),
137
+ react_1.default.createElement(material_1.TextField, { value: (_b = w.daily) !== null && _b !== void 0 ? _b : '', onChange: e => patch(unit, { daily: numberOrUndefined(e.target.value) }), label: 'Per day', variant: 'standard', type: 'number', fullWidth: true, size: 'small', disabled: props.disabled || !w.enabled, inputProps: { min: 0 }, placeholder: 'no limit', helperText: spent((_c = props.usage) === null || _c === void 0 ? void 0 : _c.daily), FormHelperTextProps: { sx: { mt: 0 } } }),
138
+ react_1.default.createElement(material_1.TextField, { value: (_d = w.monthly) !== null && _d !== void 0 ? _d : '', onChange: e => patch(unit, { monthly: numberOrUndefined(e.target.value) }), label: 'Per month', variant: 'standard', type: 'number', fullWidth: true, size: 'small', disabled: props.disabled || !w.enabled, inputProps: { min: 0 }, placeholder: 'no limit', helperText: spent((_e = props.usage) === null || _e === void 0 ? void 0 : _e.monthly), FormHelperTextProps: { sx: { mt: 0 } } })));
139
+ })));
140
+ };
72
141
  const LlmSelector = ({ llms, value, onChange, label = 'LLM', size = 'small', fullWidth = true }) => {
73
142
  return (react_1.default.createElement(material_1.FormControl, { size: size, fullWidth: fullWidth },
74
143
  react_1.default.createElement(material_1.InputLabel, null, label),
@@ -82,7 +151,7 @@ const LlmSelector = ({ llms, value, onChange, label = 'LLM', size = 'small', ful
82
151
  };
83
152
  exports.LlmSelector = LlmSelector;
84
153
  const AiConfigLlm = (props) => {
85
- var _a, _b;
154
+ var _a, _b, _c;
86
155
  const [llms, setLlms] = (0, react_1.useState)(JSON.parse(JSON.stringify(props.llms)));
87
156
  const importLlmRef = (0, react_1.useRef)(null);
88
157
  const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(null);
@@ -93,6 +162,7 @@ const AiConfigLlm = (props) => {
93
162
  const [temperature, setTemperature] = (0, react_1.useState)(0);
94
163
  const [useProviderKey, setUseProviderKey] = (0, react_1.useState)(true);
95
164
  const [key, setKey] = (0, react_1.useState)('');
165
+ const [limits, setLimits] = (0, react_1.useState)({});
96
166
  const [inputCostPerMillion, setInputCostPerMillion] = (0, react_1.useState)(0);
97
167
  const [outputCostPerMillion, setOutputCostPerMillion] = (0, react_1.useState)(0);
98
168
  // Re-syncs state when the models arrive asynchronously (same race fix as AiConfigProvider): on open,
@@ -103,7 +173,7 @@ const AiConfigLlm = (props) => {
103
173
  setSelectedIndex(null);
104
174
  }, [props.llms]);
105
175
  const onLlmSelected = (index) => {
106
- var _a, _b;
176
+ var _a, _b, _c;
107
177
  const l = llms[index];
108
178
  if (l) {
109
179
  setId(l.id);
@@ -114,6 +184,7 @@ const AiConfigLlm = (props) => {
114
184
  setKey(l.key);
115
185
  setInputCostPerMillion((_a = l.inputCostPerMillion) !== null && _a !== void 0 ? _a : 0);
116
186
  setOutputCostPerMillion((_b = l.outputCostPerMillion) !== null && _b !== void 0 ? _b : 0);
187
+ setLimits((_c = l.limits) !== null && _c !== void 0 ? _c : {});
117
188
  setSelectedIndex(index);
118
189
  }
119
190
  };
@@ -127,9 +198,12 @@ const AiConfigLlm = (props) => {
127
198
  setKey('');
128
199
  setInputCostPerMillion(0);
129
200
  setOutputCostPerMillion(0);
201
+ setLimits({});
130
202
  };
131
203
  const onAdd = () => {
132
- const llm = { id, provider, model, temperature, useProviderKey, key, inputCostPerMillion: inputCostPerMillion === '' ? 0 : inputCostPerMillion, outputCostPerMillion: outputCostPerMillion === '' ? 0 : outputCostPerMillion };
204
+ // Limits only travel with a model that has its OWN key: with a borrowed one the budget is the
205
+ // provider's, and keeping a stale copy here would show a ceiling that nothing enforces.
206
+ const llm = Object.assign({ id, provider, model, temperature, useProviderKey, key, inputCostPerMillion: inputCostPerMillion === '' ? 0 : inputCostPerMillion, outputCostPerMillion: outputCostPerMillion === '' ? 0 : outputCostPerMillion }, (useProviderKey ? {} : { limits }));
133
207
  const updated = [...llms];
134
208
  if (selectedIndex !== null)
135
209
  updated[selectedIndex] = llm;
@@ -144,11 +218,16 @@ const AiConfigLlm = (props) => {
144
218
  setLlms(llms.filter((_, i) => i !== selectedIndex));
145
219
  onNew();
146
220
  };
147
- return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '80vw', maxWidth: '800px', height: '78vh' } } },
148
- react_1.default.createElement(material_1.DialogTitle, null, "AI \u2014 LLM config"),
149
- react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex', height: '100%' } },
221
+ /*
222
+ Height follows the content instead of being pinned at 78vh. A fixed height is what put the
223
+ Add/Update button below the fold on a short screen: the form grew, the dialog did not, and the
224
+ control that applies the form to the list scrolled out of sight.
225
+ */
226
+ return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '80vw', maxWidth: '900px', height: 'auto', maxHeight: '90vh' } } },
227
+ react_1.default.createElement(material_1.DialogTitle, { sx: { pb: 1 } }, "AI \u2014 LLM config"),
228
+ react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex' } },
150
229
  react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', flexDirection: 'column', boxSizing: 'border-box', maxWidth: '40%' } },
151
- react_1.default.createElement(material_1.Box, { sx: { flex: 1, overflowY: 'auto', overflowX: 'hidden' } },
230
+ react_1.default.createElement(material_1.Box, { sx: { flex: 1, overflowY: 'auto', overflowX: 'hidden', maxHeight: '70vh' } },
152
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) },
153
232
  react_1.default.createElement(material_1.Stack, { direction: 'column' },
154
233
  react_1.default.createElement(material_1.Typography, { sx: { fontWeight: selectedIndex === index ? 'bold' : 'normal' } }, llm.id),
@@ -168,17 +247,20 @@ const AiConfigLlm = (props) => {
168
247
  react_1.default.createElement(material_1.InputLabel, null, "Model"),
169
248
  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))))))
170
249
  : (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 })),
171
- react_1.default.createElement(material_1.TextField, { value: temperature, onChange: e => setTemperature(+e.target.value), label: 'Model temperature', variant: 'standard', type: 'number', fullWidth: true }),
172
250
  react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
173
- react_1.default.createElement(material_1.TextField, { value: inputCostPerMillion, onChange: e => setInputCostPerMillion(e.target.value === '' ? '' : +e.target.value), label: 'Input cost / M tokens (\u20AC/$)', variant: 'standard', type: 'number', fullWidth: true, inputProps: { min: 0, step: 0.01 } }),
174
- react_1.default.createElement(material_1.TextField, { value: outputCostPerMillion, onChange: e => setOutputCostPerMillion(e.target.value === '' ? '' : +e.target.value), label: 'Output cost / M tokens (\u20AC/$)', variant: 'standard', type: 'number', fullWidth: true, inputProps: { min: 0, step: 0.01 } })),
251
+ react_1.default.createElement(material_1.TextField, { value: temperature, onChange: e => setTemperature(+e.target.value), label: 'Temperature', variant: 'standard', type: 'number', sx: { width: '28%' } }),
252
+ 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 } }),
253
+ 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 } })),
175
254
  react_1.default.createElement(material_1.Stack, { direction: 'row', alignItems: 'center' },
176
255
  react_1.default.createElement(material_1.Typography, { flex: 1 }, "Use provider API Key (or enter a specific one)"),
177
256
  react_1.default.createElement(material_1.Checkbox, { checked: useProviderKey, onChange: e => setUseProviderKey(e.target.checked) })),
178
257
  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: {
179
258
  endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
180
259
  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))))
181
- } })),
260
+ } }),
261
+ useProviderKey
262
+ ? 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.")
263
+ : 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 })),
182
264
  react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
183
265
  react_1.default.createElement(material_1.Button, { variant: 'outlined', size: 'small', onClick: onNew }, "New"),
184
266
  react_1.default.createElement(material_1.Typography, { flex: 1 }),
@@ -206,7 +288,7 @@ const AiConfigLlm = (props) => {
206
288
  };
207
289
  exports.AiConfigLlm = AiConfigLlm;
208
290
  const AiConfigProvider = (props) => {
209
- var _a;
291
+ var _a, _b;
210
292
  const [providers, setProviders] = (0, react_1.useState)(JSON.parse(JSON.stringify(props.providers)));
211
293
  const importProvRef = (0, react_1.useRef)(null);
212
294
  const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(null);
@@ -221,15 +303,17 @@ const AiConfigProvider = (props) => {
221
303
  const [providerType, setProviderType] = (0, react_1.useState)((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
222
304
  const [providerKey, setProviderKey] = (0, react_1.useState)('');
223
305
  const [providerEndpoint, setProviderEndpoint] = (0, react_1.useState)('');
306
+ const [providerLimits, setProviderLimits] = (0, react_1.useState)({});
224
307
  const [pendingModels, setPendingModels] = (0, react_1.useState)([]); // solo para mostrar count en el botón
225
308
  const onProviderSelected = (p, index) => {
226
- var _a, _b, _c, _d;
309
+ var _a, _b, _c, _d, _e;
227
310
  setProviderName(p.name);
228
311
  setProviderType((_a = p.type) !== null && _a !== void 0 ? _a : p.name);
229
312
  setProviderKey(p.key);
230
313
  setProviderEndpoint((_b = p.endpoint) !== null && _b !== void 0 ? _b : '');
231
- loadedModelsRef.current = (_c = p.models) !== null && _c !== void 0 ? _c : [];
232
- setPendingModels((_d = p.models) !== null && _d !== void 0 ? _d : []);
314
+ setProviderLimits((_c = p.limits) !== null && _c !== void 0 ? _c : {});
315
+ loadedModelsRef.current = (_d = p.models) !== null && _d !== void 0 ? _d : [];
316
+ setPendingModels((_e = p.models) !== null && _e !== void 0 ? _e : []);
233
317
  setSelectedIndex(index);
234
318
  };
235
319
  const onNew = () => {
@@ -239,6 +323,7 @@ const AiConfigProvider = (props) => {
239
323
  setProviderType((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
240
324
  setProviderKey('');
241
325
  setProviderEndpoint('');
326
+ setProviderLimits({});
242
327
  loadedModelsRef.current = [];
243
328
  setPendingModels([]);
244
329
  };
@@ -249,9 +334,9 @@ const AiConfigProvider = (props) => {
249
334
  const models = loadedModelsRef.current;
250
335
  const updated = [...providers];
251
336
  if (selectedIndex !== null)
252
- updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models, endpoint });
337
+ updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits });
253
338
  else
254
- updated.push({ name: providerName, type: providerType, key: providerKey, models, endpoint });
339
+ updated.push({ name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits });
255
340
  setProviders(updated);
256
341
  onNew();
257
342
  };
@@ -284,7 +369,7 @@ const AiConfigProvider = (props) => {
284
369
  react_1.default.createElement(material_1.DialogTitle, null, "AI \u2014 Provider config"),
285
370
  react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex' } },
286
371
  react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', flexDirection: 'column', boxSizing: 'border-box', maxWidth: '30%' } },
287
- react_1.default.createElement(material_1.Box, { sx: { flex: 1, overflowY: 'auto', maxHeight: '60vh' } },
372
+ react_1.default.createElement(material_1.Box, { sx: { flex: 1, overflowY: 'auto', maxHeight: '70vh' } },
288
373
  react_1.default.createElement(material_1.List, { sx: { mr: 1 } }, providers.map((p, index) => {
289
374
  var _a;
290
375
  return (react_1.default.createElement(material_1.ListItemButton, { key: index, selected: selectedIndex === index, onClick: () => onProviderSelected(p, index) },
@@ -295,7 +380,7 @@ const AiConfigProvider = (props) => {
295
380
  " models loaded"))));
296
381
  })))),
297
382
  react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', alignItems: 'start', padding: '24px' } },
298
- react_1.default.createElement(material_1.Stack, { spacing: 3, style: { width: '100%' } },
383
+ react_1.default.createElement(material_1.Stack, { spacing: 2, style: { width: '100%' } },
299
384
  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' }),
300
385
  react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
301
386
  react_1.default.createElement(material_1.InputLabel, null, "Type"),
@@ -306,6 +391,7 @@ const AiConfigProvider = (props) => {
306
391
  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))))
307
392
  } }),
308
393
  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)' })),
394
+ 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 }),
309
395
  react_1.default.createElement(material_1.Box, { sx: { flexGrow: 1 } }),
310
396
  react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, alignItems: 'center' },
311
397
  react_1.default.createElement(material_1.Button, { variant: 'outlined', onClick: onNew }, "New"),
package/dist/index.d.ts CHANGED
@@ -28,6 +28,16 @@ export interface IUsageLimits {
28
28
  calls?: IUsageWindows;
29
29
  cost?: IUsageWindows;
30
30
  }
31
+ export interface IUsageAmounts {
32
+ tokensIn: number;
33
+ tokensOut: number;
34
+ calls: number;
35
+ cost: number;
36
+ }
37
+ export interface IUsageTotals {
38
+ daily: IUsageAmounts;
39
+ monthly: IUsageAmounts;
40
+ }
31
41
  export interface ILlmProvider {
32
42
  name: string;
33
43
  type: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kwirthmagnify/kwirth-common-ai",
3
- "version": "0.5.65",
3
+ "version": "0.5.67",
4
4
  "description": "Shared AI/LLM types and utilities for Kwirth AI plugins",
5
5
  "scripts": {
6
6
  "build": "del .\\dist\\* /s /q 2>nul & tsc",