@kwirthmagnify/kwirth-common-ai 0.5.75 → 0.5.76
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/README.md +29 -1
- package/dist/back.d.ts +2 -1
- package/dist/back.js +40 -5
- package/dist/front.js +114 -55
- package/dist/index.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,35 @@ Shared AI/LLM types and utilities for [Kwirth](https://kwirthmagnify.dev) AI plu
|
|
|
4
4
|
|
|
5
5
|
## What is this?
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
The AI layer every Kwirth plugin shares, so that a plugin wanting to talk to an LLM does not have to
|
|
8
|
+
carry its own provider adapters, its own configuration dialogs, or its own spending controls.
|
|
9
|
+
|
|
10
|
+
It ships in three entry points:
|
|
11
|
+
|
|
12
|
+
- **`@kwirthmagnify/kwirth-common-ai`** — the isomorphic types: `ILlmProvider`, `ILlm`, `ILlmModel`,
|
|
13
|
+
`IAgent`, the AI toolset contract (`IAiToolsetInfo`, `EToolEffect`, `EToolSensitivity`,
|
|
14
|
+
`ECapability`), and the usage-limit model (`IUsageLimits`, `EUsageUnit`, `EUsagePeriod`).
|
|
15
|
+
- **`@kwirthmagnify/kwirth-common-ai/back`** — `buildModel` and the counted `generateText` /
|
|
16
|
+
`streamText`, which enforce the configured ceilings around every call.
|
|
17
|
+
- **`@kwirthmagnify/kwirth-common-ai/front`** — the React dialogs for configuring providers and
|
|
18
|
+
models, plus the LLM and toolset selectors.
|
|
19
|
+
|
|
20
|
+
## Providers
|
|
21
|
+
|
|
22
|
+
One adapter per supported SDK: Google, OpenAI, OpenRouter, Mistral, Groq, DeepSeek, Anthropic, and
|
|
23
|
+
`openai-compat` for any OpenAI-compatible endpoint (Huawei MaaS, vLLM, LM Studio…).
|
|
24
|
+
|
|
25
|
+
## Costs and usage limits
|
|
26
|
+
|
|
27
|
+
Each provider declares **default prices per million tokens**, and each model may override them — within
|
|
28
|
+
one provider, and on one key, each model costs a different amount. A model that states no price of its
|
|
29
|
+
own is charged at its provider's.
|
|
30
|
+
|
|
31
|
+
Ceilings can be set on tokens in, tokens out, calls and cost, per day and per month, and apply to
|
|
32
|
+
whichever is reached first. A model with no limits of its own inherits the provider's.
|
|
33
|
+
|
|
34
|
+
⚠️ Spending is accounted **per API key**: models sharing a key share one budget, because that is what
|
|
35
|
+
the provider bills.
|
|
8
36
|
|
|
9
37
|
## Installation
|
|
10
38
|
|
package/dist/back.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ILlm, ILlmProvider, ECapability, EToolEffect, EToolSensitivity, EUsagePeriod, EUsageUnit, IAiToolInfo, IAiToolsetInfo, IToolsetConfig, IUsageAmounts } from './index';
|
|
1
|
+
import { ILlm, ILlmProvider, ECapability, EToolEffect, EToolSensitivity, EUsagePeriod, EUsageUnit, IAiToolInfo, IAiToolsetInfo, IToolsetConfig, IUsageAmounts, IUsageLimits } from './index';
|
|
2
2
|
import type { AppsV1Api, CoreV1Api, CustomObjectsApi, NetworkingV1Api } from '@kubernetes/client-node';
|
|
3
3
|
interface ILogChannel {
|
|
4
4
|
logInfo?: (msg: string) => void;
|
|
@@ -38,6 +38,7 @@ export declare const setAiLogger: (logger: TAiLogger | undefined) => void;
|
|
|
38
38
|
export declare const setUsageService: (service: IUsageService | undefined) => void;
|
|
39
39
|
export declare const getUsageService: () => IUsageService | undefined;
|
|
40
40
|
export declare const usageSubjectForKey: (key: string) => string;
|
|
41
|
+
export declare const effectiveLimits: (llm: ILlm, prov: ILlmProvider | undefined) => IUsageLimits | undefined;
|
|
41
42
|
export declare const runWithUsageGuard: (options: {
|
|
42
43
|
model?: unknown;
|
|
43
44
|
}, 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.streamText = exports.generateText = exports.loadModels = exports.generateVision = exports.runWithUsageGuard = exports.usageSubjectForKey = exports.getUsageService = exports.setUsageService = exports.setAiLogger = 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.streamText = exports.generateText = exports.loadModels = exports.generateVision = exports.runWithUsageGuard = exports.effectiveLimits = exports.usageSubjectForKey = exports.getUsageService = exports.setUsageService = exports.setAiLogger = 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.
|
|
@@ -113,18 +113,53 @@ const keyFingerprint = (key) => (0, crypto_1.createHash)('sha256').update(key).d
|
|
|
113
113
|
*/
|
|
114
114
|
const usageSubjectForKey = (key) => keyFingerprint(key);
|
|
115
115
|
exports.usageSubjectForKey = usageSubjectForKey;
|
|
116
|
+
/* The four units as they are keyed inside IUsageLimits. Typed off the interface, so adding a fifth unit
|
|
117
|
+
breaks the build here instead of quietly leaving it out of the checks below. */
|
|
118
|
+
const UNIT_KEYS = ['tokensIn', 'tokensOut', 'calls', 'cost'];
|
|
119
|
+
/*
|
|
120
|
+
Does this set of limits say anything at all? An IUsageLimits that exists but has no window with a
|
|
121
|
+
number is the SAME as not having one, and the difference matters because that is exactly what the
|
|
122
|
+
dialog leaves behind when an admin empties the fields: the object survives, emptied.
|
|
123
|
+
|
|
124
|
+
Without this check an emptied model would stop inheriting the provider's limits —silently going from
|
|
125
|
+
limited to unlimited— which is the opposite of what clearing a field asks for.
|
|
126
|
+
*/
|
|
127
|
+
const statesSomething = (limits) => !!limits && UNIT_KEYS.some(k => {
|
|
128
|
+
const w = limits[k];
|
|
129
|
+
return !!w && (w.daily !== undefined || w.monthly !== undefined);
|
|
130
|
+
});
|
|
131
|
+
/*
|
|
132
|
+
Which limits govern a model: ITS OWN if it states any, otherwise the provider's. Model first and the
|
|
133
|
+
provider as a default, and the borrowed key no longer decides — a cheap model and an expensive one on
|
|
134
|
+
the same key are not the same budget.
|
|
135
|
+
|
|
136
|
+
⚠️ What it does NOT do is split the counter. Spending is counted per KEY (EUsageScope.LLM_KEY), so on
|
|
137
|
+
a borrowed key the ceiling set here is measured against what EVERY model sharing that key spent. It is
|
|
138
|
+
still worth setting —it is the only way to cut a model short before the shared budget runs out— but it
|
|
139
|
+
is not a per-model budget, and the dialog says so where the admin types it.
|
|
140
|
+
*/
|
|
141
|
+
const effectiveLimits = (llm, prov) => statesSomething(llm.limits) ? llm.limits : prov === null || prov === void 0 ? void 0 : prov.limits;
|
|
142
|
+
exports.effectiveLimits = effectiveLimits;
|
|
143
|
+
/*
|
|
144
|
+
What a model costs, per million tokens: ITS OWN price if it has one, otherwise the provider's, and 0
|
|
145
|
+
if nobody said. Model first, because within one provider —and on one key— each model costs a different
|
|
146
|
+
amount, which is the whole reason the price could never be a property of the key.
|
|
147
|
+
|
|
148
|
+
`??` and not `||` on purpose: 0 is a stated price —a free or self-hosted model— and must NOT fall
|
|
149
|
+
through to the provider's.
|
|
150
|
+
*/
|
|
151
|
+
const priceOf = (own, fallback) => { var _a; return (_a = own !== null && own !== void 0 ? own : fallback) !== null && _a !== void 0 ? _a : 0; };
|
|
116
152
|
/*
|
|
117
153
|
Records what a model was built from, so generateText can count against it. Called by buildModel; a
|
|
118
154
|
model built any other way simply has no origin and is not counted — never silently miscounted.
|
|
119
155
|
*/
|
|
120
156
|
const rememberOrigin = (model, llm, prov, key) => {
|
|
121
|
-
var _a, _b;
|
|
122
157
|
if (model && typeof model === 'object') {
|
|
123
158
|
modelOrigin.set(model, {
|
|
124
159
|
subject: keyFingerprint(key),
|
|
125
|
-
limits:
|
|
126
|
-
inputCostPerMillion: (
|
|
127
|
-
outputCostPerMillion: (
|
|
160
|
+
limits: (0, exports.effectiveLimits)(llm, prov),
|
|
161
|
+
inputCostPerMillion: priceOf(llm.inputCostPerMillion, prov === null || prov === void 0 ? void 0 : prov.inputCostPerMillion),
|
|
162
|
+
outputCostPerMillion: priceOf(llm.outputCostPerMillion, prov === null || prov === void 0 ? void 0 : prov.outputCostPerMillion)
|
|
128
163
|
});
|
|
129
164
|
}
|
|
130
165
|
return model;
|
package/dist/front.js
CHANGED
|
@@ -70,6 +70,35 @@ const downloadJson = async (data, filename) => {
|
|
|
70
70
|
URL.revokeObjectURL(url);
|
|
71
71
|
};
|
|
72
72
|
const index_1 = require("./index");
|
|
73
|
+
/*
|
|
74
|
+
The height of the content of BOTH config dialogs, and the one number that keeps them still.
|
|
75
|
+
|
|
76
|
+
The rule, and it is not negotiable the next time a field is added: THE CONTENT IS FIXED AND THE DIALOG
|
|
77
|
+
ADAPTS TO IT. One size for the two dialogs, because they are the same form seen twice and nothing is
|
|
78
|
+
more obviously broken than the two resizing against each other when the user switches between them.
|
|
79
|
+
|
|
80
|
+
Sized for the tallest state either form reaches (the limits editor: four units × two windows). Anything
|
|
81
|
+
taller scrolls inside its column — it does NOT grow the box.
|
|
82
|
+
*/
|
|
83
|
+
/*
|
|
84
|
+
🔴 THE CONTENT HAS A FIXED SIZE AND THE DIALOG ADAPTS TO IT. Never the reverse.
|
|
85
|
+
|
|
86
|
+
'Fixed' does NOT mean a magic number of pixels: that is how the gap between the form and the buttons
|
|
87
|
+
appeared —a panel taller than what was inside it— and how a dialog ends up half empty. It means the
|
|
88
|
+
content cannot CHANGE size: every field is always on screen (disabled when it does not apply, never
|
|
89
|
+
removed), captions are a single fixed line, and the columns are COL_WIDTH wide.
|
|
90
|
+
|
|
91
|
+
With nothing left that can grow or vanish, the form measures the same always, the list stretches to
|
|
92
|
+
whatever the form measures, and the dialog is simply as big as the two of them.
|
|
93
|
+
*/
|
|
94
|
+
/*
|
|
95
|
+
And the WIDTH of each of the two form columns, for the same reason. Fixed too, because 'adapts to the
|
|
96
|
+
content' cuts both ways: columns that stretch with the window are not a fixed content either, and a
|
|
97
|
+
percentage width is what once truncated the 'Temperature' label to 'Tempe…'.
|
|
98
|
+
|
|
99
|
+
These two plus the 220px list are the whole content, so the Paper can simply be as wide as they are.
|
|
100
|
+
*/
|
|
101
|
+
const COL_WIDTH = 340;
|
|
73
102
|
/* The four units in the order an admin thinks about them: what it costs, how often, how much it moved. */
|
|
74
103
|
const USAGE_ROWS = [
|
|
75
104
|
{ unit: index_1.EUsageUnit.COST, label: 'Cost (€/$)', of: a => a.cost },
|
|
@@ -126,16 +155,19 @@ const UsageLimitsEditor = (props) => {
|
|
|
126
155
|
react_1.default.createElement("b", null, "in memory"),
|
|
127
156
|
" (no SQL): they reset on every restart, and so do these limits."),
|
|
128
157
|
USAGE_ROWS.map(({ unit, label, of }) => {
|
|
129
|
-
var _a, _b, _c
|
|
158
|
+
var _a, _b, _c;
|
|
130
159
|
const w = (_a = limits[unit]) !== null && _a !== void 0 ? _a : { enabled: false };
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
160
|
+
/*
|
|
161
|
+
🔴 El gasto YA NO va bajo cada campo. Iba en el helperText de su ventana, y aparecía
|
|
162
|
+
sólo cuando había consumo — así que el diálogo CAMBIABA DE TAMAÑO al seleccionar un
|
|
163
|
+
modelo: ocho helpers que aparecen de golpe son ocho líneas más de alto, y el contenido
|
|
164
|
+
salta bajo el cursor. Un diálogo que se mueve mientras lo lees cuesta más que el dato
|
|
165
|
+
que daba, y el dato está en el panel de consumo.
|
|
166
|
+
*/
|
|
135
167
|
return (react_1.default.createElement(material_1.Stack, { key: unit, direction: 'row', spacing: 1, alignItems: 'flex-start' },
|
|
136
168
|
react_1.default.createElement(material_1.FormControlLabel, { sx: { width: '150px', flexShrink: 0, 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'
|
|
138
|
-
react_1.default.createElement(material_1.TextField, { value: (
|
|
169
|
+
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' }),
|
|
170
|
+
react_1.default.createElement(material_1.TextField, { value: (_c = w.monthly) !== null && _c !== void 0 ? _c : '', 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' })));
|
|
139
171
|
})));
|
|
140
172
|
};
|
|
141
173
|
const LlmSelector = ({ llms, value, onChange, label = 'LLM', size = 'small', fullWidth = true }) => {
|
|
@@ -163,8 +195,11 @@ const AiConfigLlm = (props) => {
|
|
|
163
195
|
const [useProviderKey, setUseProviderKey] = (0, react_1.useState)(true);
|
|
164
196
|
const [key, setKey] = (0, react_1.useState)('');
|
|
165
197
|
const [limits, setLimits] = (0, react_1.useState)({});
|
|
166
|
-
|
|
167
|
-
|
|
198
|
+
// Empty, not zero, and that is the whole point: empty means 'the provider's price', while 0 is a
|
|
199
|
+
// stated price — a free or self-hosted model. Defaulting these to 0 made every model declare that it
|
|
200
|
+
// was free, and no provider default could ever be reached.
|
|
201
|
+
const [inputCostPerMillion, setInputCostPerMillion] = (0, react_1.useState)('');
|
|
202
|
+
const [outputCostPerMillion, setOutputCostPerMillion] = (0, react_1.useState)('');
|
|
168
203
|
// Re-syncs state when the models arrive asynchronously (same race fix as AiConfigProvider): on open,
|
|
169
204
|
// the parent mounts the dialog with props.llms empty and requests them over WS; without this, the list
|
|
170
205
|
// stayed empty until the dialog was closed and reopened.
|
|
@@ -172,6 +207,14 @@ const AiConfigLlm = (props) => {
|
|
|
172
207
|
setLlms(JSON.parse(JSON.stringify(props.llms)));
|
|
173
208
|
setSelectedIndex(null);
|
|
174
209
|
}, [props.llms]);
|
|
210
|
+
/* What the selected provider charges, as the placeholder of an empty price field: the fallback is
|
|
211
|
+
only useful if the admin can SEE what they are inheriting before leaving the field blank. Nothing
|
|
212
|
+
declared on the provider, and the fallback is 0, so the placeholder says so instead of staying
|
|
213
|
+
empty — an empty cost box is exactly what made this look like 'free'. */
|
|
214
|
+
const providerCost = (providerName, field) => {
|
|
215
|
+
const p = props.providers.find(p => p.name === providerName);
|
|
216
|
+
return (p === null || p === void 0 ? void 0 : p[field]) !== undefined ? `${p[field]} (provider)` : '0 (provider)';
|
|
217
|
+
};
|
|
175
218
|
const onLlmSelected = (index) => {
|
|
176
219
|
var _a, _b, _c;
|
|
177
220
|
const l = llms[index];
|
|
@@ -182,8 +225,8 @@ const AiConfigLlm = (props) => {
|
|
|
182
225
|
setTemperature(l.temperature);
|
|
183
226
|
setUseProviderKey(l.useProviderKey);
|
|
184
227
|
setKey(l.key);
|
|
185
|
-
setInputCostPerMillion((_a = l.inputCostPerMillion) !== null && _a !== void 0 ? _a :
|
|
186
|
-
setOutputCostPerMillion((_b = l.outputCostPerMillion) !== null && _b !== void 0 ? _b :
|
|
228
|
+
setInputCostPerMillion((_a = l.inputCostPerMillion) !== null && _a !== void 0 ? _a : '');
|
|
229
|
+
setOutputCostPerMillion((_b = l.outputCostPerMillion) !== null && _b !== void 0 ? _b : '');
|
|
187
230
|
setLimits((_c = l.limits) !== null && _c !== void 0 ? _c : {});
|
|
188
231
|
setSelectedIndex(index);
|
|
189
232
|
}
|
|
@@ -196,14 +239,16 @@ const AiConfigLlm = (props) => {
|
|
|
196
239
|
setTemperature(0);
|
|
197
240
|
setUseProviderKey(false);
|
|
198
241
|
setKey('');
|
|
199
|
-
setInputCostPerMillion(
|
|
200
|
-
setOutputCostPerMillion(
|
|
242
|
+
setInputCostPerMillion('');
|
|
243
|
+
setOutputCostPerMillion('');
|
|
201
244
|
setLimits({});
|
|
202
245
|
};
|
|
203
246
|
const onAdd = () => {
|
|
204
|
-
//
|
|
205
|
-
// provider's
|
|
206
|
-
|
|
247
|
+
// Prices and limits travel ALWAYS, borrowed key or not, and a field left empty is left out so the
|
|
248
|
+
// back end falls back to the provider's. Writing a 0 for an empty price would persist 'free', and
|
|
249
|
+
// dropping the limits on a borrowed key cost the admin the only way of cutting one model short
|
|
250
|
+
// before the shared budget ran out.
|
|
251
|
+
const llm = Object.assign(Object.assign(Object.assign({ id, provider, model, temperature, useProviderKey, key }, (inputCostPerMillion === '' ? {} : { inputCostPerMillion })), (outputCostPerMillion === '' ? {} : { outputCostPerMillion })), { limits });
|
|
207
252
|
const updated = [...llms];
|
|
208
253
|
if (selectedIndex !== null)
|
|
209
254
|
updated[selectedIndex] = llm;
|
|
@@ -223,19 +268,19 @@ const AiConfigLlm = (props) => {
|
|
|
223
268
|
Add/Update button below the fold on a short screen: the form grew, the dialog did not, and the
|
|
224
269
|
control that applies the form to the list scrolled out of sight.
|
|
225
270
|
*/
|
|
226
|
-
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '
|
|
271
|
+
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: 'auto', maxWidth: 'none', height: 'auto' } } },
|
|
227
272
|
react_1.default.createElement(material_1.DialogTitle, { sx: { pb: 1 } }, "AI \u2014 LLM config"),
|
|
228
273
|
react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex' } },
|
|
229
|
-
react_1.default.createElement(material_1.Box, { sx: { flex: '0 0 220px',
|
|
230
|
-
react_1.default.createElement(material_1.Box, { sx: {
|
|
274
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: '0 0 220px', boxSizing: 'border-box', position: 'relative' } },
|
|
275
|
+
react_1.default.createElement(material_1.Box, { sx: { position: 'absolute', inset: 0, overflowY: 'auto', overflowX: 'hidden' } },
|
|
231
276
|
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
277
|
react_1.default.createElement(material_1.Stack, { direction: 'column' },
|
|
233
278
|
react_1.default.createElement(material_1.Typography, { sx: { fontWeight: selectedIndex === index ? 'bold' : 'normal' } }, llm.id),
|
|
234
279
|
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',
|
|
280
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', flexDirection: 'column', padding: '16px', minWidth: 0 } },
|
|
236
281
|
react_1.default.createElement(material_1.Stack, { spacing: 2, style: { width: '100%' } },
|
|
237
|
-
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 3, alignItems: '
|
|
238
|
-
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex:
|
|
282
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 3, alignItems: 'stretch', sx: { flexWrap: 'wrap', height: '100%' } },
|
|
283
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: `0 0 ${COL_WIDTH}px`, width: COL_WIDTH } },
|
|
239
284
|
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
285
|
react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
241
286
|
react_1.default.createElement(material_1.InputLabel, null, "Provider"),
|
|
@@ -248,27 +293,26 @@ const AiConfigLlm = (props) => {
|
|
|
248
293
|
react_1.default.createElement(material_1.InputLabel, null, "Model"),
|
|
249
294
|
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
295
|
: (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.
|
|
252
|
-
|
|
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', sx: { flex: 1, minWidth:
|
|
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', sx: { flex: 1, minWidth:
|
|
296
|
+
react_1.default.createElement(material_1.TextField, { value: temperature, onChange: e => setTemperature(+e.target.value), label: 'Temperature', variant: 'standard', type: 'number', fullWidth: true }),
|
|
297
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, sx: { flexWrap: 'nowrap' } },
|
|
298
|
+
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', sx: { flex: 1, minWidth: 0 }, inputProps: { min: 0, step: 0.01 }, placeholder: providerCost(provider, 'inputCostPerMillion'), InputLabelProps: { shrink: true } }),
|
|
299
|
+
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', sx: { flex: 1, minWidth: 0 }, inputProps: { min: 0, step: 0.01 }, placeholder: providerCost(provider, 'outputCostPerMillion'), InputLabelProps: { shrink: true } })),
|
|
255
300
|
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
|
|
301
|
+
react_1.default.createElement(material_1.Typography, { flex: 1 }, "Use provider API Key"),
|
|
257
302
|
react_1.default.createElement(material_1.Checkbox, { checked: useProviderKey, onChange: e => setUseProviderKey(e.target.checked) })),
|
|
258
303
|
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
304
|
endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
|
|
260
305
|
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
306
|
} })),
|
|
262
|
-
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex:
|
|
307
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: `0 0 ${COL_WIDTH}px`, width: COL_WIDTH } },
|
|
263
308
|
react_1.default.createElement(material_1.Typography, { variant: 'subtitle2' }, "Usage limits"),
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
react_1.default.createElement(material_1.
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
react_1.default.createElement(material_1.Button, { variant: 'contained', onClick: onAdd, disabled: !id || !model }, selectedIndex !== null ? 'Update' : 'Add'))))),
|
|
309
|
+
react_1.default.createElement(UsageLimitsEditor, { value: limits, onChange: setLimits, subject: `the key this model uses`, usage: (_c = props.usage) === null || _c === void 0 ? void 0 : _c[id], durable: props.usageDurable }),
|
|
310
|
+
react_1.default.createElement(material_1.Typography, { variant: 'caption', color: 'text.secondary' }, "Left empty, the provider's limits apply. Spending is counted per API key.")))),
|
|
311
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, sx: { pt: 2 } },
|
|
312
|
+
react_1.default.createElement(material_1.Button, { variant: 'outlined', size: 'small', onClick: onNew }, "New"),
|
|
313
|
+
react_1.default.createElement(material_1.Typography, { flex: 1 }),
|
|
314
|
+
react_1.default.createElement(material_1.Button, { color: 'error', onClick: onRemove, disabled: selectedIndex === null }, "Remove"),
|
|
315
|
+
react_1.default.createElement(material_1.Button, { variant: 'contained', onClick: onAdd, disabled: !id || !model }, selectedIndex !== null ? 'Update' : 'Add')))),
|
|
272
316
|
react_1.default.createElement(material_1.DialogActions, null,
|
|
273
317
|
react_1.default.createElement("input", { ref: importLlmRef, type: 'file', accept: '.json', style: { display: 'none' }, onChange: e => {
|
|
274
318
|
var _a;
|
|
@@ -307,16 +351,22 @@ const AiConfigProvider = (props) => {
|
|
|
307
351
|
const [providerKey, setProviderKey] = (0, react_1.useState)('');
|
|
308
352
|
const [providerEndpoint, setProviderEndpoint] = (0, react_1.useState)('');
|
|
309
353
|
const [providerLimits, setProviderLimits] = (0, react_1.useState)({});
|
|
354
|
+
/* The DEFAULT price of this provider's models, which each model may override. Here so that adding a
|
|
355
|
+
model does not mean looking the provider's rate up again — not because the key has a price. */
|
|
356
|
+
const [providerInputCost, setProviderInputCost] = (0, react_1.useState)('');
|
|
357
|
+
const [providerOutputCost, setProviderOutputCost] = (0, react_1.useState)('');
|
|
310
358
|
const [pendingModels, setPendingModels] = (0, react_1.useState)([]); // solo para mostrar count en el botón
|
|
311
359
|
const onProviderSelected = (p, index) => {
|
|
312
|
-
var _a, _b, _c, _d, _e;
|
|
360
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
313
361
|
setProviderName(p.name);
|
|
314
362
|
setProviderType((_a = p.type) !== null && _a !== void 0 ? _a : p.name);
|
|
315
363
|
setProviderKey(p.key);
|
|
316
364
|
setProviderEndpoint((_b = p.endpoint) !== null && _b !== void 0 ? _b : '');
|
|
317
365
|
setProviderLimits((_c = p.limits) !== null && _c !== void 0 ? _c : {});
|
|
318
|
-
|
|
319
|
-
|
|
366
|
+
setProviderInputCost((_d = p.inputCostPerMillion) !== null && _d !== void 0 ? _d : '');
|
|
367
|
+
setProviderOutputCost((_e = p.outputCostPerMillion) !== null && _e !== void 0 ? _e : '');
|
|
368
|
+
loadedModelsRef.current = (_f = p.models) !== null && _f !== void 0 ? _f : [];
|
|
369
|
+
setPendingModels((_g = p.models) !== null && _g !== void 0 ? _g : []);
|
|
320
370
|
setSelectedIndex(index);
|
|
321
371
|
};
|
|
322
372
|
const onNew = () => {
|
|
@@ -327,6 +377,8 @@ const AiConfigProvider = (props) => {
|
|
|
327
377
|
setProviderKey('');
|
|
328
378
|
setProviderEndpoint('');
|
|
329
379
|
setProviderLimits({});
|
|
380
|
+
setProviderInputCost('');
|
|
381
|
+
setProviderOutputCost('');
|
|
330
382
|
loadedModelsRef.current = [];
|
|
331
383
|
setPendingModels([]);
|
|
332
384
|
};
|
|
@@ -335,11 +387,13 @@ const AiConfigProvider = (props) => {
|
|
|
335
387
|
return;
|
|
336
388
|
const endpoint = providerType === 'openai-compat' ? providerEndpoint : undefined;
|
|
337
389
|
const models = loadedModelsRef.current;
|
|
390
|
+
// An empty price is left OUT, never written as 0: a model inheriting it would be counted free.
|
|
391
|
+
const costs = Object.assign(Object.assign({}, (providerInputCost === '' ? { inputCostPerMillion: undefined } : { inputCostPerMillion: providerInputCost })), (providerOutputCost === '' ? { outputCostPerMillion: undefined } : { outputCostPerMillion: providerOutputCost }));
|
|
338
392
|
const updated = [...providers];
|
|
339
393
|
if (selectedIndex !== null)
|
|
340
|
-
updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits });
|
|
394
|
+
updated[selectedIndex] = Object.assign(Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits }), costs);
|
|
341
395
|
else
|
|
342
|
-
updated.push({ name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits });
|
|
396
|
+
updated.push(Object.assign({ name: providerName, type: providerType, key: providerKey, models, endpoint, limits: providerLimits }, costs));
|
|
343
397
|
setProviders(updated);
|
|
344
398
|
onNew();
|
|
345
399
|
};
|
|
@@ -368,11 +422,11 @@ const AiConfigProvider = (props) => {
|
|
|
368
422
|
setLoadingModels(false);
|
|
369
423
|
}
|
|
370
424
|
};
|
|
371
|
-
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '
|
|
425
|
+
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: 'auto', maxWidth: 'none', height: 'auto' } } },
|
|
372
426
|
react_1.default.createElement(material_1.DialogTitle, { sx: { pb: 1 } }, "AI \u2014 Provider config"),
|
|
373
427
|
react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex' } },
|
|
374
|
-
react_1.default.createElement(material_1.Box, { sx: { flex: '0 0 220px',
|
|
375
|
-
react_1.default.createElement(material_1.Box, { sx: {
|
|
428
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: '0 0 220px', boxSizing: 'border-box', position: 'relative' } },
|
|
429
|
+
react_1.default.createElement(material_1.Box, { sx: { position: 'absolute', inset: 0, overflowY: 'auto' } },
|
|
376
430
|
react_1.default.createElement(material_1.List, { sx: { mr: 1 } }, providers.map((p, index) => {
|
|
377
431
|
var _a;
|
|
378
432
|
return (react_1.default.createElement(material_1.ListItemButton, { key: index, selected: selectedIndex === index, onClick: () => onProviderSelected(p, index) },
|
|
@@ -382,10 +436,10 @@ const AiConfigProvider = (props) => {
|
|
|
382
436
|
((_a = p.models) === null || _a === void 0 ? void 0 : _a.length) || 0,
|
|
383
437
|
" models loaded"))));
|
|
384
438
|
})))),
|
|
385
|
-
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex',
|
|
439
|
+
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', flexDirection: 'column', padding: '16px', minWidth: 0 } },
|
|
386
440
|
react_1.default.createElement(material_1.Stack, { spacing: 2, style: { width: '100%' } },
|
|
387
441
|
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:
|
|
442
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: `0 0 ${COL_WIDTH}px`, width: COL_WIDTH } },
|
|
389
443
|
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
444
|
react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
391
445
|
react_1.default.createElement(material_1.InputLabel, null, "Type"),
|
|
@@ -395,16 +449,21 @@ const AiConfigProvider = (props) => {
|
|
|
395
449
|
endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
|
|
396
450
|
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
451
|
} }),
|
|
398
|
-
|
|
399
|
-
|
|
452
|
+
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), disabled: providerType !== 'openai-compat' }),
|
|
453
|
+
react_1.default.createElement(material_1.Typography, { variant: 'subtitle2', sx: { pt: 1 } }, "Default model costs"),
|
|
454
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, sx: { flexWrap: 'wrap' } },
|
|
455
|
+
react_1.default.createElement(material_1.TextField, { value: providerInputCost, onChange: e => setProviderInputCost(e.target.value === '' ? '' : +e.target.value), label: 'Input cost /M', variant: 'standard', type: 'number', sx: { flex: 1, minWidth: '110px' }, inputProps: { min: 0, step: 0.01 } }),
|
|
456
|
+
react_1.default.createElement(material_1.TextField, { value: providerOutputCost, onChange: e => setProviderOutputCost(e.target.value === '' ? '' : +e.target.value), label: 'Output cost /M', variant: 'standard', type: 'number', sx: { flex: 1, minWidth: '110px' }, inputProps: { min: 0, step: 0.01 } })),
|
|
457
|
+
react_1.default.createElement(material_1.Typography, { variant: 'caption', color: 'text.secondary' }, "Used by models that declare no cost of their own.")),
|
|
458
|
+
react_1.default.createElement(material_1.Stack, { direction: 'column', spacing: 1, sx: { flex: `0 0 ${COL_WIDTH}px`, width: COL_WIDTH } },
|
|
400
459
|
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 }))),
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
460
|
+
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 })))),
|
|
461
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, alignItems: 'center', sx: { pt: 2 } },
|
|
462
|
+
react_1.default.createElement(material_1.Button, { variant: 'outlined', onClick: onNew }, "New"),
|
|
463
|
+
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})` : ''}`)),
|
|
464
|
+
react_1.default.createElement(material_1.Typography, { flex: 1 }),
|
|
465
|
+
react_1.default.createElement(material_1.Button, { variant: 'text', color: 'error', onClick: onRemove, disabled: selectedIndex === null }, "Remove"),
|
|
466
|
+
react_1.default.createElement(material_1.Button, { variant: 'contained', onClick: onAdd, disabled: !providerName.trim() || !providerType }, selectedIndex !== null ? 'Update' : 'Add')))),
|
|
408
467
|
react_1.default.createElement(material_1.DialogActions, { sx: { p: 2 } },
|
|
409
468
|
react_1.default.createElement("input", { ref: importProvRef, type: 'file', accept: '.json', style: { display: 'none' }, onChange: e => {
|
|
410
469
|
var _a;
|
package/dist/index.d.ts
CHANGED