@kwirthmagnify/kwirth-common-ai 0.5.33 → 0.5.35
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 +6 -2
- package/dist/back.js +56 -9
- package/dist/front.js +25 -14
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/package.json +2 -1
package/dist/back.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import { IBackChannelObject } from '@kwirthmagnify/kwirth-common';
|
|
2
1
|
import { ILlm, ILlmProvider, IAgent } from './index';
|
|
2
|
+
interface ILogChannel {
|
|
3
|
+
logInfo?: (msg: string) => void;
|
|
4
|
+
logWarning?: (msg: string) => void;
|
|
5
|
+
logError?: (msg: string) => void;
|
|
6
|
+
}
|
|
3
7
|
import { LanguageModel } from 'ai';
|
|
4
8
|
import { z } from 'zod';
|
|
5
9
|
export declare const buildModel: (llm: ILlm, providers: ILlmProvider[]) => LanguageModel | null;
|
|
@@ -23,7 +27,7 @@ export interface IVisionOptions<T> {
|
|
|
23
27
|
mediaType?: string;
|
|
24
28
|
}
|
|
25
29
|
export declare const generateVision: <T>(opts: IVisionOptions<T>) => Promise<IVisionResult<T>>;
|
|
26
|
-
export declare const loadModels: (providers: ILlmProvider[], log:
|
|
30
|
+
export declare const loadModels: (providers: ILlmProvider[], log: ILogChannel) => Promise<void>;
|
|
27
31
|
export { generateText, Output, stepCountIs, tool } from 'ai';
|
|
28
32
|
export { z } from 'zod';
|
|
29
33
|
export declare const zodFromExample: (example: Record<string, unknown>) => z.ZodObject<Record<string, z.ZodTypeAny>>;
|
package/dist/back.js
CHANGED
|
@@ -47,22 +47,27 @@ const mistral_1 = require("@ai-sdk/mistral");
|
|
|
47
47
|
const google_1 = require("@ai-sdk/google");
|
|
48
48
|
const deepseek_1 = require("@ai-sdk/deepseek");
|
|
49
49
|
const ai_sdk_provider_1 = require("@openrouter/ai-sdk-provider");
|
|
50
|
+
const anthropic_1 = require("@ai-sdk/anthropic");
|
|
50
51
|
const buildModel = (llm, providers) => {
|
|
51
|
-
var _a;
|
|
52
|
-
const
|
|
52
|
+
var _a, _b;
|
|
53
|
+
const prov = providers.find(p => p.name === llm.provider);
|
|
54
|
+
const key = llm.useProviderKey ? prov === null || prov === void 0 ? void 0 : prov.key : llm.key;
|
|
53
55
|
if (!key) {
|
|
54
56
|
console.log('Could not find a key');
|
|
55
57
|
return null;
|
|
56
58
|
}
|
|
57
|
-
|
|
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) {
|
|
58
61
|
case 'openai': return (0, openai_1.createOpenAI)({ apiKey: key })(llm.model);
|
|
59
62
|
case 'groq': return (0, groq_1.createGroq)({ apiKey: key })(llm.model);
|
|
60
63
|
case 'mistral': return (0, mistral_1.createMistral)({ apiKey: key })(llm.model);
|
|
61
64
|
case 'google': return (0, google_1.createGoogleGenerativeAI)({ apiKey: key })(llm.model);
|
|
62
65
|
case 'deepseek': return (0, deepseek_1.createDeepSeek)({ apiKey: key })(llm.model);
|
|
63
66
|
case 'openrouter': return (0, ai_sdk_provider_1.createOpenRouter)({ apiKey: key })(llm.model);
|
|
67
|
+
case 'anthropic': return (0, anthropic_1.createAnthropic)({ apiKey: key })(llm.model);
|
|
68
|
+
case 'openai-compat': return (0, openai_1.createOpenAI)({ apiKey: key, baseURL: prov === null || prov === void 0 ? void 0 : prov.endpoint })(llm.model);
|
|
64
69
|
default:
|
|
65
|
-
console.log('Invalid provider',
|
|
70
|
+
console.log('Invalid provider type', type);
|
|
66
71
|
return null;
|
|
67
72
|
}
|
|
68
73
|
};
|
|
@@ -88,11 +93,12 @@ const generateVision = async (opts) => {
|
|
|
88
93
|
};
|
|
89
94
|
exports.generateVision = generateVision;
|
|
90
95
|
const loadModels = async (providers, log) => {
|
|
91
|
-
var _a, _b, _c, _d;
|
|
96
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
92
97
|
(_a = log.logInfo) === null || _a === void 0 ? void 0 : _a.call(log, 'Loading AI models...');
|
|
93
98
|
for (const provider of providers) {
|
|
94
99
|
try {
|
|
95
|
-
|
|
100
|
+
const type = (_b = provider.type) !== null && _b !== void 0 ? _b : provider.name;
|
|
101
|
+
switch (type) {
|
|
96
102
|
case 'deepseek': {
|
|
97
103
|
const resp = await fetch('https://api.deepseek.com/models', { headers: { Authorization: 'Bearer ' + provider.key } });
|
|
98
104
|
const data = await resp.json();
|
|
@@ -104,6 +110,7 @@ const loadModels = async (providers, log) => {
|
|
|
104
110
|
case 'google': {
|
|
105
111
|
const resp = await fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${provider.key}`);
|
|
106
112
|
const data = await resp.json();
|
|
113
|
+
console.log(data);
|
|
107
114
|
provider.models = data.models.map((m) => ({
|
|
108
115
|
id: m.name.startsWith('models/') ? m.name.substring(7) : m.name,
|
|
109
116
|
name: m.displayName,
|
|
@@ -148,6 +155,46 @@ const loadModels = async (providers, log) => {
|
|
|
148
155
|
});
|
|
149
156
|
break;
|
|
150
157
|
}
|
|
158
|
+
case 'anthropic': {
|
|
159
|
+
const resp = await fetch('https://api.anthropic.com/v1/models', { headers: { 'x-api-key': provider.key, 'anthropic-version': '2023-06-01' } });
|
|
160
|
+
const data = await resp.json();
|
|
161
|
+
provider.models = ((_c = data.data) !== null && _c !== void 0 ? _c : []).map((m) => {
|
|
162
|
+
var _a;
|
|
163
|
+
return ({
|
|
164
|
+
id: m.id, name: (_a = m.display_name) !== null && _a !== void 0 ? _a : m.id, description: '', type: 'text'
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
case 'openai-compat': {
|
|
170
|
+
if (!provider.endpoint) {
|
|
171
|
+
(_d = log.logWarning) === null || _d === void 0 ? void 0 : _d.call(log, `Provider 'openai-compat' has no endpoint configured — skipping model load`);
|
|
172
|
+
provider.models = [];
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
const base = provider.endpoint.replace(/\/+$/, '');
|
|
176
|
+
const modelsUrl = base.endsWith('/v1') ? `${base}/models` : `${base}/v1/models`;
|
|
177
|
+
try {
|
|
178
|
+
const resp = await fetch(modelsUrl, { headers: { Authorization: 'Bearer ' + provider.key } });
|
|
179
|
+
if (!resp.ok) {
|
|
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`);
|
|
181
|
+
provider.models = [];
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
const data = await resp.json();
|
|
185
|
+
provider.models = ((_g = (_f = data.data) !== null && _f !== void 0 ? _f : data.models) !== null && _g !== void 0 ? _g : []).map((m) => {
|
|
186
|
+
var _a, _b;
|
|
187
|
+
return ({
|
|
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'
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
catch (err) {
|
|
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`);
|
|
194
|
+
provider.models = [];
|
|
195
|
+
}
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
151
198
|
case 'kwirth':
|
|
152
199
|
provider.models = [
|
|
153
200
|
{ id: 'alberto-1-flash-gordon-lite', name: 'Alberto model quick response', description: 'Albert #1 model', type: 'text' },
|
|
@@ -155,12 +202,12 @@ const loadModels = async (providers, log) => {
|
|
|
155
202
|
];
|
|
156
203
|
break;
|
|
157
204
|
default:
|
|
158
|
-
(
|
|
205
|
+
(_j = log.logWarning) === null || _j === void 0 ? void 0 : _j.call(log, `Provider '${provider.name}' has unknown type '${type}', will not be available.`);
|
|
159
206
|
}
|
|
160
|
-
(
|
|
207
|
+
(_k = log.logInfo) === null || _k === void 0 ? void 0 : _k.call(log, `Provider '${provider.name}' loaded ${provider.models.length} models`);
|
|
161
208
|
}
|
|
162
209
|
catch (err) {
|
|
163
|
-
(
|
|
210
|
+
(_l = log.logError) === null || _l === void 0 ? void 0 : _l.call(log, `Error loading models from provider '${provider.name}': ${err}`);
|
|
164
211
|
}
|
|
165
212
|
}
|
|
166
213
|
};
|
package/dist/front.js
CHANGED
|
@@ -72,7 +72,7 @@ const LlmSelector = ({ llms, value, onChange, label = 'LLM', size = 'small', ful
|
|
|
72
72
|
};
|
|
73
73
|
exports.LlmSelector = LlmSelector;
|
|
74
74
|
const AiConfigLlm = (props) => {
|
|
75
|
-
var _a;
|
|
75
|
+
var _a, _b;
|
|
76
76
|
const [llms, setLlms] = (0, react_1.useState)(JSON.parse(JSON.stringify(props.llms)));
|
|
77
77
|
const importLlmRef = (0, react_1.useRef)(null);
|
|
78
78
|
const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(null);
|
|
@@ -153,9 +153,11 @@ const AiConfigLlm = (props) => {
|
|
|
153
153
|
var _a;
|
|
154
154
|
return (react_1.default.createElement(material_1.MenuItem, { key: p.name, value: p.name, disabled: !((_a = p.models) === null || _a === void 0 ? void 0 : _a.length) }, p.name));
|
|
155
155
|
}))),
|
|
156
|
-
|
|
157
|
-
react_1.default.createElement(material_1.
|
|
158
|
-
|
|
156
|
+
((_a = props.providers.find(p => p.name === provider)) === null || _a === void 0 ? void 0 : _a.models.length)
|
|
157
|
+
? (react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
158
|
+
react_1.default.createElement(material_1.InputLabel, null, "Model"),
|
|
159
|
+
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))))))
|
|
160
|
+
: (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 })),
|
|
159
161
|
react_1.default.createElement(material_1.TextField, { value: temperature, onChange: e => setTemperature(+e.target.value), label: 'Model temperature', variant: 'standard', type: 'number', fullWidth: true }),
|
|
160
162
|
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
|
|
161
163
|
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 } }),
|
|
@@ -204,21 +206,27 @@ const AiConfigProvider = (props) => {
|
|
|
204
206
|
setSelectedIndex(null);
|
|
205
207
|
}, [props.providers]);
|
|
206
208
|
const [providerName, setProviderName] = (0, react_1.useState)('');
|
|
207
|
-
const [
|
|
209
|
+
const [providerType, setProviderType] = (0, react_1.useState)((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
|
|
210
|
+
const [providerKey, setProviderKey] = (0, react_1.useState)('');
|
|
211
|
+
const [providerEndpoint, setProviderEndpoint] = (0, react_1.useState)('');
|
|
208
212
|
const onProviderSelected = (p, index) => {
|
|
213
|
+
var _a, _b;
|
|
209
214
|
setProviderName(p.name);
|
|
215
|
+
setProviderType((_a = p.type) !== null && _a !== void 0 ? _a : p.name);
|
|
210
216
|
setProviderKey(p.key);
|
|
217
|
+
setProviderEndpoint((_b = p.endpoint) !== null && _b !== void 0 ? _b : '');
|
|
211
218
|
setSelectedIndex(index);
|
|
212
219
|
};
|
|
213
|
-
const onNew = () => { setSelectedIndex(null); setProviderName(''); setProviderKey(''); };
|
|
220
|
+
const onNew = () => { var _a; setSelectedIndex(null); setProviderName(''); setProviderType((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : ''); setProviderKey(''); setProviderEndpoint(''); };
|
|
214
221
|
const onAdd = () => {
|
|
215
|
-
if (!providerName.trim())
|
|
222
|
+
if (!providerName.trim() || !providerType)
|
|
216
223
|
return;
|
|
224
|
+
const endpoint = providerType === 'openai-compat' ? providerEndpoint : undefined;
|
|
217
225
|
const updated = [...providers];
|
|
218
226
|
if (selectedIndex !== null)
|
|
219
|
-
updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, key: providerKey });
|
|
227
|
+
updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, endpoint });
|
|
220
228
|
else
|
|
221
|
-
updated.push({ name: providerName, key: providerKey, models: [] });
|
|
229
|
+
updated.push({ name: providerName, type: providerType, key: providerKey, models: [], endpoint });
|
|
222
230
|
setProviders(updated);
|
|
223
231
|
onNew();
|
|
224
232
|
};
|
|
@@ -228,7 +236,7 @@ const AiConfigProvider = (props) => {
|
|
|
228
236
|
setProviders(providers.filter((_, i) => i !== selectedIndex));
|
|
229
237
|
onNew();
|
|
230
238
|
};
|
|
231
|
-
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '80vw', maxWidth: '900px', height: '
|
|
239
|
+
return (react_1.default.createElement(material_1.Dialog, { open: true, onClose: () => props.onClose(undefined), PaperProps: { sx: { width: '80vw', maxWidth: '900px', height: '60vh' } } },
|
|
232
240
|
react_1.default.createElement(material_1.DialogTitle, null, "AI \u2014 Provider config"),
|
|
233
241
|
react_1.default.createElement(material_1.DialogContent, { style: { display: 'flex', height: '100%' } },
|
|
234
242
|
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', flexDirection: 'column', boxSizing: 'border-box', maxWidth: '30%' } },
|
|
@@ -245,18 +253,21 @@ const AiConfigProvider = (props) => {
|
|
|
245
253
|
react_1.default.createElement(material_1.Box, { sx: { flex: 1, display: 'flex', alignItems: 'start', padding: '24px' } },
|
|
246
254
|
react_1.default.createElement(material_1.Stack, { spacing: 3, style: { width: '100%' } },
|
|
247
255
|
react_1.default.createElement(material_1.FormControl, { variant: 'standard', sx: { width: '100%' } },
|
|
248
|
-
react_1.default.createElement(material_1.InputLabel, null, "
|
|
249
|
-
react_1.default.createElement(material_1.Select, { value:
|
|
256
|
+
react_1.default.createElement(material_1.InputLabel, null, "Type"),
|
|
257
|
+
react_1.default.createElement(material_1.Select, { value: providerType, onChange: e => { setProviderType(e.target.value); if (!providerName)
|
|
258
|
+
setProviderName(e.target.value); }, variant: 'standard', fullWidth: true }, props.providersAvailable.map(t => (react_1.default.createElement(material_1.MenuItem, { key: t, value: t }, t))))),
|
|
259
|
+
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' }),
|
|
250
260
|
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: {
|
|
251
261
|
endAdornment: (react_1.default.createElement(material_1.InputAdornment, { position: 'end' },
|
|
252
262
|
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))))
|
|
253
263
|
} }),
|
|
264
|
+
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)' })),
|
|
254
265
|
react_1.default.createElement(material_1.Box, { sx: { flexGrow: 1 } }),
|
|
255
266
|
react_1.default.createElement(material_1.Stack, { direction: 'row', spacing: 1 },
|
|
256
267
|
react_1.default.createElement(material_1.Button, { variant: 'outlined', onClick: onNew }, "New"),
|
|
257
268
|
react_1.default.createElement(material_1.Typography, { flex: 1 }),
|
|
258
269
|
react_1.default.createElement(material_1.Button, { variant: 'text', color: 'error', onClick: onRemove, disabled: selectedIndex === null }, "Remove"),
|
|
259
|
-
react_1.default.createElement(material_1.Button, { variant: 'contained', onClick: onAdd, disabled: !providerName }, selectedIndex !== null ? 'Update' : 'Add'))))),
|
|
270
|
+
react_1.default.createElement(material_1.Button, { variant: 'contained', onClick: onAdd, disabled: !providerName.trim() || !providerType }, selectedIndex !== null ? 'Update' : 'Add'))))),
|
|
260
271
|
react_1.default.createElement(material_1.DialogActions, { sx: { p: 2 } },
|
|
261
272
|
react_1.default.createElement("input", { ref: importProvRef, type: 'file', accept: '.json', style: { display: 'none' }, onChange: e => {
|
|
262
273
|
var _a;
|
|
@@ -272,7 +283,7 @@ const AiConfigProvider = (props) => {
|
|
|
272
283
|
e.target.value = '';
|
|
273
284
|
} }),
|
|
274
285
|
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"),
|
|
275
|
-
react_1.default.createElement(material_1.Button, { startIcon: react_1.default.createElement(FileDownload_1.default, { fontSize: 'small' }), onClick: () => downloadJson(providers.map(p => ({ name: p.name, key: p.key })), 'kwirth-providers.json') }, "Export"),
|
|
286
|
+
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"),
|
|
276
287
|
react_1.default.createElement(material_1.Box, { flex: 1 }),
|
|
277
288
|
react_1.default.createElement(material_1.Button, { onClick: () => props.onClose(providers), color: 'primary', variant: 'contained' }, "Save"),
|
|
278
289
|
react_1.default.createElement(material_1.Button, { onClick: () => props.onClose(undefined), color: 'inherit' }, "Cancel"))));
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.STORAGE_KEY_AGENTS = exports.PROVIDERS_AVAILABLE = exports.STORAGE_KEY_LLMS = exports.STORAGE_KEY_PROVIDERS = void 0;
|
|
4
4
|
exports.STORAGE_KEY_PROVIDERS = 'kwirth-ai-providers';
|
|
5
5
|
exports.STORAGE_KEY_LLMS = 'kwirth-ai-llms';
|
|
6
|
-
exports.PROVIDERS_AVAILABLE = ['google', 'openai', 'openrouter', 'mistral', 'groq', 'deepseek'];
|
|
6
|
+
exports.PROVIDERS_AVAILABLE = ['google', 'openai', 'openrouter', 'mistral', 'groq', 'deepseek', 'anthropic', 'openai-compat'];
|
|
7
7
|
exports.STORAGE_KEY_AGENTS = 'kwirth-ai-agents';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kwirthmagnify/kwirth-common-ai",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.35",
|
|
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",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"author": "Julio Fernandez",
|
|
46
46
|
"license": "ISC",
|
|
47
47
|
"dependencies": {
|
|
48
|
+
"@ai-sdk/anthropic": "^3.0.116",
|
|
48
49
|
"@ai-sdk/deepseek": "^2.0.29",
|
|
49
50
|
"@ai-sdk/google": "^3.0.64",
|
|
50
51
|
"@ai-sdk/groq": "^3.0.35",
|