@kwirthmagnify/kwirth-common-ai 0.5.65 → 0.5.66

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,66 @@ 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
+ return (react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { mt: 1 } },
99
+ react_1.default.createElement(material_1.Typography, { variant: 'body2', color: 'text.secondary' },
100
+ "Stop spending on ",
101
+ react_1.default.createElement("b", null, props.subject),
102
+ ". A call goes out only if ",
103
+ react_1.default.createElement("b", null, "no"),
104
+ " enabled ceiling is reached \u2014 whichever is passed first blocks it, and the channel is told why."),
105
+ react_1.default.createElement(material_1.Typography, { variant: 'caption', color: 'text.secondary' },
106
+ "\u26A0\uFE0F A ",
107
+ react_1.default.createElement("b", null, "calls"),
108
+ " ceiling stops the call that would exceed it. ",
109
+ react_1.default.createElement("b", null, "Tokens"),
110
+ " and ",
111
+ react_1.default.createElement("b", null, "cost"),
112
+ " are only known once the model has answered, so those stop the ",
113
+ react_1.default.createElement("b", null, "next"),
114
+ " call: the one that crosses the line has already been paid for."),
115
+ props.usage && props.durable === false &&
116
+ react_1.default.createElement(material_1.Typography, { variant: 'caption', color: 'warning.main' },
117
+ "\u26A0\uFE0F These counters are kept ",
118
+ react_1.default.createElement("b", null, "in memory"),
119
+ ", because this Kwirth has no SQL configured: they go back to zero on every restart, and so do the limits that depend on them."),
120
+ USAGE_ROWS.map(({ unit, label, of }) => {
121
+ var _a, _b, _c, _d, _e;
122
+ const w = (_a = limits[unit]) !== null && _a !== void 0 ? _a : { enabled: false };
123
+ // What has been spent goes in the helper text of its own window: next to the number it is
124
+ // measured against, which is the only place it answers anything.
125
+ const spent = (amounts) => amounts ? `${formatSpent(of(amounts))} so far` : undefined;
126
+ return (react_1.default.createElement(material_1.Stack, { key: unit, direction: 'row', spacing: 1, alignItems: 'flex-start' },
127
+ react_1.default.createElement(material_1.FormControlLabel, { sx: { width: '190px', mr: 0, mt: 1 }, control: react_1.default.createElement(material_1.Checkbox, { checked: !!w.enabled, disabled: props.disabled, onChange: e => patch(unit, { enabled: e.target.checked }) }), label: label }),
128
+ 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, disabled: props.disabled || !w.enabled, inputProps: { min: 0 }, placeholder: 'no limit', helperText: spent((_c = props.usage) === null || _c === void 0 ? void 0 : _c.daily) }),
129
+ 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, disabled: props.disabled || !w.enabled, inputProps: { min: 0 }, placeholder: 'no limit', helperText: spent((_e = props.usage) === null || _e === void 0 ? void 0 : _e.monthly) })));
130
+ })));
131
+ };
72
132
  const LlmSelector = ({ llms, value, onChange, label = 'LLM', size = 'small', fullWidth = true }) => {
73
133
  return (react_1.default.createElement(material_1.FormControl, { size: size, fullWidth: fullWidth },
74
134
  react_1.default.createElement(material_1.InputLabel, null, label),
@@ -82,7 +142,7 @@ const LlmSelector = ({ llms, value, onChange, label = 'LLM', size = 'small', ful
82
142
  };
83
143
  exports.LlmSelector = LlmSelector;
84
144
  const AiConfigLlm = (props) => {
85
- var _a, _b;
145
+ var _a, _b, _c;
86
146
  const [llms, setLlms] = (0, react_1.useState)(JSON.parse(JSON.stringify(props.llms)));
87
147
  const importLlmRef = (0, react_1.useRef)(null);
88
148
  const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(null);
@@ -93,6 +153,7 @@ const AiConfigLlm = (props) => {
93
153
  const [temperature, setTemperature] = (0, react_1.useState)(0);
94
154
  const [useProviderKey, setUseProviderKey] = (0, react_1.useState)(true);
95
155
  const [key, setKey] = (0, react_1.useState)('');
156
+ const [limits, setLimits] = (0, react_1.useState)({});
96
157
  const [inputCostPerMillion, setInputCostPerMillion] = (0, react_1.useState)(0);
97
158
  const [outputCostPerMillion, setOutputCostPerMillion] = (0, react_1.useState)(0);
98
159
  // Re-syncs state when the models arrive asynchronously (same race fix as AiConfigProvider): on open,
@@ -103,7 +164,7 @@ const AiConfigLlm = (props) => {
103
164
  setSelectedIndex(null);
104
165
  }, [props.llms]);
105
166
  const onLlmSelected = (index) => {
106
- var _a, _b;
167
+ var _a, _b, _c;
107
168
  const l = llms[index];
108
169
  if (l) {
109
170
  setId(l.id);
@@ -114,6 +175,7 @@ const AiConfigLlm = (props) => {
114
175
  setKey(l.key);
115
176
  setInputCostPerMillion((_a = l.inputCostPerMillion) !== null && _a !== void 0 ? _a : 0);
116
177
  setOutputCostPerMillion((_b = l.outputCostPerMillion) !== null && _b !== void 0 ? _b : 0);
178
+ setLimits((_c = l.limits) !== null && _c !== void 0 ? _c : {});
117
179
  setSelectedIndex(index);
118
180
  }
119
181
  };
@@ -127,9 +189,12 @@ const AiConfigLlm = (props) => {
127
189
  setKey('');
128
190
  setInputCostPerMillion(0);
129
191
  setOutputCostPerMillion(0);
192
+ setLimits({});
130
193
  };
131
194
  const onAdd = () => {
132
- const llm = { id, provider, model, temperature, useProviderKey, key, inputCostPerMillion: inputCostPerMillion === '' ? 0 : inputCostPerMillion, outputCostPerMillion: outputCostPerMillion === '' ? 0 : outputCostPerMillion };
195
+ // Limits only travel with a model that has its OWN key: with a borrowed one the budget is the
196
+ // provider's, and keeping a stale copy here would show a ceiling that nothing enforces.
197
+ const llm = Object.assign({ id, provider, model, temperature, useProviderKey, key, inputCostPerMillion: inputCostPerMillion === '' ? 0 : inputCostPerMillion, outputCostPerMillion: outputCostPerMillion === '' ? 0 : outputCostPerMillion }, (useProviderKey ? {} : { limits }));
133
198
  const updated = [...llms];
134
199
  if (selectedIndex !== null)
135
200
  updated[selectedIndex] = llm;
@@ -178,7 +243,10 @@ const AiConfigLlm = (props) => {
178
243
  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
244
  endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
180
245
  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
- } })),
246
+ } }),
247
+ useProviderKey
248
+ ? 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.")
249
+ : 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
250
  react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
183
251
  react_1.default.createElement(material_1.Button, { variant: 'outlined', size: 'small', onClick: onNew }, "New"),
184
252
  react_1.default.createElement(material_1.Typography, { flex: 1 }),
@@ -206,7 +274,7 @@ const AiConfigLlm = (props) => {
206
274
  };
207
275
  exports.AiConfigLlm = AiConfigLlm;
208
276
  const AiConfigProvider = (props) => {
209
- var _a;
277
+ var _a, _b;
210
278
  const [providers, setProviders] = (0, react_1.useState)(JSON.parse(JSON.stringify(props.providers)));
211
279
  const importProvRef = (0, react_1.useRef)(null);
212
280
  const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(null);
@@ -221,15 +289,17 @@ const AiConfigProvider = (props) => {
221
289
  const [providerType, setProviderType] = (0, react_1.useState)((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
222
290
  const [providerKey, setProviderKey] = (0, react_1.useState)('');
223
291
  const [providerEndpoint, setProviderEndpoint] = (0, react_1.useState)('');
292
+ const [providerLimits, setProviderLimits] = (0, react_1.useState)({});
224
293
  const [pendingModels, setPendingModels] = (0, react_1.useState)([]); // solo para mostrar count en el botón
225
294
  const onProviderSelected = (p, index) => {
226
- var _a, _b, _c, _d;
295
+ var _a, _b, _c, _d, _e;
227
296
  setProviderName(p.name);
228
297
  setProviderType((_a = p.type) !== null && _a !== void 0 ? _a : p.name);
229
298
  setProviderKey(p.key);
230
299
  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 : []);
300
+ setProviderLimits((_c = p.limits) !== null && _c !== void 0 ? _c : {});
301
+ loadedModelsRef.current = (_d = p.models) !== null && _d !== void 0 ? _d : [];
302
+ setPendingModels((_e = p.models) !== null && _e !== void 0 ? _e : []);
233
303
  setSelectedIndex(index);
234
304
  };
235
305
  const onNew = () => {
@@ -239,6 +309,7 @@ const AiConfigProvider = (props) => {
239
309
  setProviderType((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
240
310
  setProviderKey('');
241
311
  setProviderEndpoint('');
312
+ setProviderLimits({});
242
313
  loadedModelsRef.current = [];
243
314
  setPendingModels([]);
244
315
  };
@@ -249,9 +320,9 @@ const AiConfigProvider = (props) => {
249
320
  const models = loadedModelsRef.current;
250
321
  const updated = [...providers];
251
322
  if (selectedIndex !== null)
252
- updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models, endpoint });
323
+ updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits });
253
324
  else
254
- updated.push({ name: providerName, type: providerType, key: providerKey, models, endpoint });
325
+ updated.push({ name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits });
255
326
  setProviders(updated);
256
327
  onNew();
257
328
  };
@@ -306,6 +377,7 @@ const AiConfigProvider = (props) => {
306
377
  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
378
  } }),
308
379
  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)' })),
380
+ 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
381
  react_1.default.createElement(material_1.Box, { sx: { flexGrow: 1 } }),
310
382
  react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, alignItems: 'center' },
311
383
  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.66",
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",