@kwirthmagnify/kwirth-common-ai 0.5.34 → 0.5.36
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 +15 -12
- package/dist/front.d.ts +2 -1
- package/dist/front.js +36 -15
- package/dist/index.d.ts +1 -0
- package/package.json +1 -1
package/dist/back.js
CHANGED
|
@@ -49,13 +49,15 @@ const deepseek_1 = require("@ai-sdk/deepseek");
|
|
|
49
49
|
const ai_sdk_provider_1 = require("@openrouter/ai-sdk-provider");
|
|
50
50
|
const anthropic_1 = require("@ai-sdk/anthropic");
|
|
51
51
|
const buildModel = (llm, providers) => {
|
|
52
|
+
var _a, _b;
|
|
52
53
|
const prov = providers.find(p => p.name === llm.provider);
|
|
53
54
|
const key = llm.useProviderKey ? prov === null || prov === void 0 ? void 0 : prov.key : llm.key;
|
|
54
55
|
if (!key) {
|
|
55
56
|
console.log('Could not find a key');
|
|
56
57
|
return null;
|
|
57
58
|
}
|
|
58
|
-
|
|
59
|
+
const type = (_b = (_a = prov === null || prov === void 0 ? void 0 : prov.type) !== null && _a !== void 0 ? _a : prov === null || prov === void 0 ? void 0 : prov.name) !== null && _b !== void 0 ? _b : llm.provider;
|
|
60
|
+
switch (type) {
|
|
59
61
|
case 'openai': return (0, openai_1.createOpenAI)({ apiKey: key })(llm.model);
|
|
60
62
|
case 'groq': return (0, groq_1.createGroq)({ apiKey: key })(llm.model);
|
|
61
63
|
case 'mistral': return (0, mistral_1.createMistral)({ apiKey: key })(llm.model);
|
|
@@ -65,7 +67,7 @@ const buildModel = (llm, providers) => {
|
|
|
65
67
|
case 'anthropic': return (0, anthropic_1.createAnthropic)({ apiKey: key })(llm.model);
|
|
66
68
|
case 'openai-compat': return (0, openai_1.createOpenAI)({ apiKey: key, baseURL: prov === null || prov === void 0 ? void 0 : prov.endpoint })(llm.model);
|
|
67
69
|
default:
|
|
68
|
-
console.log('Invalid provider',
|
|
70
|
+
console.log('Invalid provider type', type);
|
|
69
71
|
return null;
|
|
70
72
|
}
|
|
71
73
|
};
|
|
@@ -91,11 +93,12 @@ const generateVision = async (opts) => {
|
|
|
91
93
|
};
|
|
92
94
|
exports.generateVision = generateVision;
|
|
93
95
|
const loadModels = async (providers, log) => {
|
|
94
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
96
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
95
97
|
(_a = log.logInfo) === null || _a === void 0 ? void 0 : _a.call(log, 'Loading AI models...');
|
|
96
98
|
for (const provider of providers) {
|
|
97
99
|
try {
|
|
98
|
-
|
|
100
|
+
const type = (_b = provider.type) !== null && _b !== void 0 ? _b : provider.name;
|
|
101
|
+
switch (type) {
|
|
99
102
|
case 'deepseek': {
|
|
100
103
|
const resp = await fetch('https://api.deepseek.com/models', { headers: { Authorization: 'Bearer ' + provider.key } });
|
|
101
104
|
const data = await resp.json();
|
|
@@ -155,7 +158,7 @@ const loadModels = async (providers, log) => {
|
|
|
155
158
|
case 'anthropic': {
|
|
156
159
|
const resp = await fetch('https://api.anthropic.com/v1/models', { headers: { 'x-api-key': provider.key, 'anthropic-version': '2023-06-01' } });
|
|
157
160
|
const data = await resp.json();
|
|
158
|
-
provider.models = ((
|
|
161
|
+
provider.models = ((_c = data.data) !== null && _c !== void 0 ? _c : []).map((m) => {
|
|
159
162
|
var _a;
|
|
160
163
|
return ({
|
|
161
164
|
id: m.id, name: (_a = m.display_name) !== null && _a !== void 0 ? _a : m.id, description: '', type: 'text'
|
|
@@ -165,7 +168,7 @@ const loadModels = async (providers, log) => {
|
|
|
165
168
|
}
|
|
166
169
|
case 'openai-compat': {
|
|
167
170
|
if (!provider.endpoint) {
|
|
168
|
-
(
|
|
171
|
+
(_d = log.logWarning) === null || _d === void 0 ? void 0 : _d.call(log, `Provider 'openai-compat' has no endpoint configured — skipping model load`);
|
|
169
172
|
provider.models = [];
|
|
170
173
|
break;
|
|
171
174
|
}
|
|
@@ -174,12 +177,12 @@ const loadModels = async (providers, log) => {
|
|
|
174
177
|
try {
|
|
175
178
|
const resp = await fetch(modelsUrl, { headers: { Authorization: 'Bearer ' + provider.key } });
|
|
176
179
|
if (!resp.ok) {
|
|
177
|
-
(
|
|
180
|
+
(_e = log.logWarning) === null || _e === void 0 ? void 0 : _e.call(log, `Provider 'openai-compat' /models returned ${resp.status} — models list will be empty`);
|
|
178
181
|
provider.models = [];
|
|
179
182
|
break;
|
|
180
183
|
}
|
|
181
184
|
const data = await resp.json();
|
|
182
|
-
provider.models = ((
|
|
185
|
+
provider.models = ((_g = (_f = data.data) !== null && _f !== void 0 ? _f : data.models) !== null && _g !== void 0 ? _g : []).map((m) => {
|
|
183
186
|
var _a, _b;
|
|
184
187
|
return ({
|
|
185
188
|
id: m.id, name: (_a = m.name) !== null && _a !== void 0 ? _a : m.id, description: (_b = m.description) !== null && _b !== void 0 ? _b : '', type: 'text'
|
|
@@ -187,7 +190,7 @@ const loadModels = async (providers, log) => {
|
|
|
187
190
|
});
|
|
188
191
|
}
|
|
189
192
|
catch (err) {
|
|
190
|
-
(
|
|
193
|
+
(_h = log.logWarning) === null || _h === void 0 ? void 0 : _h.call(log, `Provider 'openai-compat' failed to load models: ${err} — models list will be empty`);
|
|
191
194
|
provider.models = [];
|
|
192
195
|
}
|
|
193
196
|
break;
|
|
@@ -199,12 +202,12 @@ const loadModels = async (providers, log) => {
|
|
|
199
202
|
];
|
|
200
203
|
break;
|
|
201
204
|
default:
|
|
202
|
-
(
|
|
205
|
+
(_j = log.logWarning) === null || _j === void 0 ? void 0 : _j.call(log, `Provider '${provider.name}' has unknown type '${type}', will not be available.`);
|
|
203
206
|
}
|
|
204
|
-
(
|
|
207
|
+
(_k = log.logInfo) === null || _k === void 0 ? void 0 : _k.call(log, `Provider '${provider.name}' loaded ${provider.models.length} models`);
|
|
205
208
|
}
|
|
206
209
|
catch (err) {
|
|
207
|
-
(
|
|
210
|
+
(_l = log.logError) === null || _l === void 0 ? void 0 : _l.call(log, `Error loading models from provider '${provider.name}': ${err}`);
|
|
208
211
|
}
|
|
209
212
|
}
|
|
210
213
|
};
|
package/dist/front.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { ILlm, ILlmProvider } from './index';
|
|
2
|
+
import { ILlm, ILlmModel, ILlmProvider } from './index';
|
|
3
3
|
interface ILlmSelectorProps {
|
|
4
4
|
llms: ILlm[];
|
|
5
5
|
value: string;
|
|
@@ -19,6 +19,7 @@ interface IAiConfigProviderProps {
|
|
|
19
19
|
providersAvailable: string[];
|
|
20
20
|
providers: ILlmProvider[];
|
|
21
21
|
onClose: (providers: ILlmProvider[] | undefined) => void;
|
|
22
|
+
onLoadModels?: (provider: ILlmProvider) => Promise<ILlmModel[]>;
|
|
22
23
|
}
|
|
23
24
|
declare const AiConfigProvider: React.FC<IAiConfigProviderProps>;
|
|
24
25
|
interface IToolSelectorProps {
|
package/dist/front.js
CHANGED
|
@@ -201,30 +201,35 @@ const AiConfigProvider = (props) => {
|
|
|
201
201
|
const importProvRef = (0, react_1.useRef)(null);
|
|
202
202
|
const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(null);
|
|
203
203
|
const [showPassword, setShowPassword] = (0, react_1.useState)(false);
|
|
204
|
+
const [loadingModels, setLoadingModels] = (0, react_1.useState)(false);
|
|
204
205
|
(0, react_1.useEffect)(() => {
|
|
205
206
|
setProviders(JSON.parse(JSON.stringify(props.providers)));
|
|
206
207
|
setSelectedIndex(null);
|
|
207
208
|
}, [props.providers]);
|
|
208
209
|
const [providerName, setProviderName] = (0, react_1.useState)('');
|
|
209
|
-
const [
|
|
210
|
+
const [providerType, setProviderType] = (0, react_1.useState)((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
|
|
211
|
+
const [providerKey, setProviderKey] = (0, react_1.useState)('');
|
|
210
212
|
const [providerEndpoint, setProviderEndpoint] = (0, react_1.useState)('');
|
|
213
|
+
const [pendingModels, setPendingModels] = (0, react_1.useState)([]);
|
|
211
214
|
const onProviderSelected = (p, index) => {
|
|
212
|
-
var _a;
|
|
215
|
+
var _a, _b, _c;
|
|
213
216
|
setProviderName(p.name);
|
|
217
|
+
setProviderType((_a = p.type) !== null && _a !== void 0 ? _a : p.name);
|
|
214
218
|
setProviderKey(p.key);
|
|
215
|
-
setProviderEndpoint((
|
|
219
|
+
setProviderEndpoint((_b = p.endpoint) !== null && _b !== void 0 ? _b : '');
|
|
220
|
+
setPendingModels((_c = p.models) !== null && _c !== void 0 ? _c : []);
|
|
216
221
|
setSelectedIndex(index);
|
|
217
222
|
};
|
|
218
|
-
const onNew = () => { setSelectedIndex(null); setProviderName(''); setProviderKey(''); setProviderEndpoint(''); };
|
|
223
|
+
const onNew = () => { var _a; setSelectedIndex(null); setProviderName(''); setProviderType((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : ''); setProviderKey(''); setProviderEndpoint(''); setPendingModels([]); };
|
|
219
224
|
const onAdd = () => {
|
|
220
|
-
if (!providerName.trim())
|
|
225
|
+
if (!providerName.trim() || !providerType)
|
|
221
226
|
return;
|
|
222
|
-
const endpoint =
|
|
227
|
+
const endpoint = providerType === 'openai-compat' ? providerEndpoint : undefined;
|
|
223
228
|
const updated = [...providers];
|
|
224
229
|
if (selectedIndex !== null)
|
|
225
|
-
updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, key: providerKey, endpoint });
|
|
230
|
+
updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models: pendingModels, endpoint });
|
|
226
231
|
else
|
|
227
|
-
updated.push({ name: providerName, key: providerKey, models:
|
|
232
|
+
updated.push({ name: providerName, type: providerType, key: providerKey, models: pendingModels, endpoint });
|
|
228
233
|
setProviders(updated);
|
|
229
234
|
onNew();
|
|
230
235
|
};
|
|
@@ -234,7 +239,20 @@ const AiConfigProvider = (props) => {
|
|
|
234
239
|
setProviders(providers.filter((_, i) => i !== selectedIndex));
|
|
235
240
|
onNew();
|
|
236
241
|
};
|
|
237
|
-
|
|
242
|
+
const onLoadModels = async () => {
|
|
243
|
+
if (!props.onLoadModels || !providerType || !providerKey)
|
|
244
|
+
return;
|
|
245
|
+
setLoadingModels(true);
|
|
246
|
+
try {
|
|
247
|
+
const endpoint = providerType === 'openai-compat' ? providerEndpoint : undefined;
|
|
248
|
+
const models = await props.onLoadModels({ name: providerName, type: providerType, key: providerKey, models: [], endpoint });
|
|
249
|
+
setPendingModels(models);
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
setLoadingModels(false);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '80vw', maxWidth: '900px', height: '60vh' } } },
|
|
238
256
|
react_1.default.createElement(material_1.DialogTitle, null, "AI \u2014 Provider config"),
|
|
239
257
|
react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex', height: '100%' } },
|
|
240
258
|
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', flexDirection: 'column', boxSizing: 'border-box', maxWidth: '30%' } },
|
|
@@ -251,19 +269,22 @@ const AiConfigProvider = (props) => {
|
|
|
251
269
|
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', alignItems: 'start', padding: '24px' } },
|
|
252
270
|
react_1.default.createElement(material_1.Stack, { spacing: 3, style: { width: '100%' } },
|
|
253
271
|
react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
254
|
-
react_1.default.createElement(material_1.InputLabel, null, "
|
|
255
|
-
react_1.default.createElement(material_1.Select, { value:
|
|
272
|
+
react_1.default.createElement(material_1.InputLabel, null, "Type"),
|
|
273
|
+
react_1.default.createElement(material_1.Select, { value: providerType, onChange: e => { setProviderType(e.target.value); if (!providerName)
|
|
274
|
+
setProviderName(e.target.value); }, variant: 'standard', fullWidth: true }, props.providersAvailable.map(t => (react_1.default.createElement(material_1.MenuItem, { key: t, value: t }, t))))),
|
|
275
|
+
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' }),
|
|
256
276
|
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: {
|
|
257
277
|
endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
|
|
258
278
|
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))))
|
|
259
279
|
} }),
|
|
260
|
-
|
|
280
|
+
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)' })),
|
|
261
281
|
react_1.default.createElement(material_1.Box, { sx: { flexGrow: 1 } }),
|
|
262
|
-
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
|
|
282
|
+
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1, alignItems: 'center' },
|
|
263
283
|
react_1.default.createElement(material_1.Button, { variant: 'outlined', onClick: onNew }, "New"),
|
|
284
|
+
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})` : ''}`)),
|
|
264
285
|
react_1.default.createElement(material_1.Typography, { flex: 1 }),
|
|
265
286
|
react_1.default.createElement(material_1.Button, { variant: 'text', color: 'error', onClick: onRemove, disabled: selectedIndex === null }, "Remove"),
|
|
266
|
-
react_1.default.createElement(material_1.Button, { variant: 'contained', onClick: onAdd, disabled: !providerName }, selectedIndex !== null ? 'Update' : 'Add'))))),
|
|
287
|
+
react_1.default.createElement(material_1.Button, { variant: 'contained', onClick: onAdd, disabled: !providerName.trim() || !providerType }, selectedIndex !== null ? 'Update' : 'Add'))))),
|
|
267
288
|
react_1.default.createElement(material_1.DialogActions, { sx: { p: 2 } },
|
|
268
289
|
react_1.default.createElement("input", { ref: importProvRef, type: 'file', accept: '.json', style: { display: 'none' }, onChange: e => {
|
|
269
290
|
var _a;
|
|
@@ -279,7 +300,7 @@ const AiConfigProvider = (props) => {
|
|
|
279
300
|
e.target.value = '';
|
|
280
301
|
} }),
|
|
281
302
|
react_1.default.createElement(material_1.Button, { startIcon: react_1.default.createElement(FileUpload_1.default, { fontSize: 'small' }), onClick: () => { var _a; return (_a = importProvRef.current) === null || _a === void 0 ? void 0 : _a.click(); } }, "Import"),
|
|
282
|
-
react_1.default.createElement(material_1.Button, { startIcon: react_1.default.createElement(FileDownload_1.default, { fontSize: 'small' }), onClick: () => downloadJson(providers.map(p => (Object.assign({ name: p.name, key: p.key }, (p.endpoint ? { endpoint: p.endpoint } : {})))), 'kwirth-providers.json') }, "Export"),
|
|
303
|
+
react_1.default.createElement(material_1.Button, { startIcon: react_1.default.createElement(FileDownload_1.default, { fontSize: 'small' }), onClick: () => downloadJson(providers.map(p => { var _a; return (Object.assign({ name: p.name, type: (_a = p.type) !== null && _a !== void 0 ? _a : p.name, key: p.key }, (p.endpoint ? { endpoint: p.endpoint } : {}))); }), 'kwirth-providers.json') }, "Export"),
|
|
283
304
|
react_1.default.createElement(material_1.Box, { flex: 1 }),
|
|
284
305
|
react_1.default.createElement(material_1.Button, { onClick: () => props.onClose(providers), color: 'primary', variant: 'contained' }, "Save"),
|
|
285
306
|
react_1.default.createElement(material_1.Button, { onClick: () => props.onClose(undefined), color: 'inherit' }, "Cancel"))));
|
package/dist/index.d.ts
CHANGED