@kwirthmagnify/kwirth-common-ai 0.5.37 → 0.5.39

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 CHANGED
@@ -49,7 +49,7 @@ 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
+ var _a, _b, _c;
53
53
  const prov = providers.find(p => p.name === llm.provider);
54
54
  const key = llm.useProviderKey ? prov === null || prov === void 0 ? void 0 : prov.key : llm.key;
55
55
  if (!key) {
@@ -65,7 +65,12 @@ const buildModel = (llm, providers) => {
65
65
  case 'deepseek': return (0, deepseek_1.createDeepSeek)({ apiKey: key })(llm.model);
66
66
  case 'openrouter': return (0, ai_sdk_provider_1.createOpenRouter)({ apiKey: key })(llm.model);
67
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);
68
+ case 'openai-compat': {
69
+ // Mismo criterio que loadModels: el baseURL de OpenAI debe terminar en /v1 (el SDK le añade
70
+ // /chat/completions). Si el endpoint no lo trae, se lo añadimos → si no, 404 en el chat.
71
+ const b = ((_c = prov === null || prov === void 0 ? void 0 : prov.endpoint) !== null && _c !== void 0 ? _c : '').replace(/\/+$/, '');
72
+ return (0, openai_1.createOpenAI)({ apiKey: key, baseURL: b.endsWith('/v1') ? b : `${b}/v1` })(llm.model);
73
+ }
69
74
  default:
70
75
  console.log('Invalid provider type', type);
71
76
  return null;
@@ -76,19 +81,35 @@ exports.buildModel = buildModel;
76
81
  // un schema Zod (patrón Output.object). Reutilizable por toda la plataforma (Iter la usa para extraer un mapa
77
82
  // de un diagrama). No lanza: los errores vuelven en `error`.
78
83
  const generateVision = async (opts) => {
79
- var _a, _b, _c;
84
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
80
85
  try {
81
86
  const imagePart = opts.mediaType
82
87
  ? { type: 'image', image: opts.image, mediaType: opts.mediaType }
83
88
  : { type: 'image', image: opts.image };
84
- const { output, text, usage } = await (0, ai_1.generateText)(Object.assign({ model: opts.model, temperature: (_a = opts.temperature) !== null && _a !== void 0 ? _a : 0, system: opts.system, messages: [{
89
+ console.log('[generateVision] request', {
90
+ modelId: (_d = (_b = (_a = opts.model) === null || _a === void 0 ? void 0 : _a.modelId) !== null && _b !== void 0 ? _b : (_c = opts.model) === null || _c === void 0 ? void 0 : _c.model) !== null && _d !== void 0 ? _d : '(unknown)',
91
+ provider: (_j = (_f = (_e = opts.model) === null || _e === void 0 ? void 0 : _e.provider) !== null && _f !== void 0 ? _f : (_h = (_g = opts.model) === null || _g === void 0 ? void 0 : _g.config) === null || _h === void 0 ? void 0 : _h.provider) !== null && _j !== void 0 ? _j : '(unknown)',
92
+ mediaType: opts.mediaType, imageBytes: ((_k = opts.image) !== null && _k !== void 0 ? _k : '').length,
93
+ systemLen: ((_l = opts.system) !== null && _l !== void 0 ? _l : '').length, providerOptions: opts.providerOptions,
94
+ });
95
+ const { output, text, usage } = await (0, ai_1.generateText)(Object.assign({ model: opts.model, temperature: (_m = opts.temperature) !== null && _m !== void 0 ? _m : 0, system: opts.system, messages: [{
85
96
  role: 'user',
86
97
  content: [{ type: 'text', text: opts.prompt }, imagePart],
87
98
  }], output: ai_1.Output.object({ schema: opts.schema }) }, (opts.providerOptions ? { providerOptions: opts.providerOptions } : {})));
88
- return { object: (_b = output) !== null && _b !== void 0 ? _b : null, text: text !== null && text !== void 0 ? text : '', usage };
99
+ console.log('[generateVision] ok', { hasOutput: output != null, textLen: (text !== null && text !== void 0 ? text : '').length, usage });
100
+ return { object: (_o = output) !== null && _o !== void 0 ? _o : null, text: text !== null && text !== void 0 ? text : '', usage };
89
101
  }
90
102
  catch (err) {
91
- return { object: null, text: '', error: (_c = err === null || err === void 0 ? void 0 : err.message) !== null && _c !== void 0 ? _c : String(err) };
103
+ const e = err;
104
+ const body = typeof (e === null || e === void 0 ? void 0 : e.responseBody) === 'string' ? e.responseBody : ((e === null || e === void 0 ? void 0 : e.responseBody) ? JSON.stringify(e.responseBody) : undefined);
105
+ // TRAZA COMPLETA del fallo de la llamada al LLM (URL, status, cuerpo de respuesta, causa).
106
+ console.error('[generateVision] FAILED', {
107
+ name: e === null || e === void 0 ? void 0 : e.name, message: e === null || e === void 0 ? void 0 : e.message, statusCode: e === null || e === void 0 ? void 0 : e.statusCode, url: e === null || e === void 0 ? void 0 : e.url,
108
+ responseBody: body, cause: (_q = (_p = e === null || e === void 0 ? void 0 : e.cause) === null || _p === void 0 ? void 0 : _p.message) !== null && _q !== void 0 ? _q : e === null || e === void 0 ? void 0 : e.cause,
109
+ requestBody: typeof (e === null || e === void 0 ? void 0 : e.requestBodyValues) === 'object' ? '[requestBodyValues present]' : undefined,
110
+ });
111
+ const parts = [e === null || e === void 0 ? void 0 : e.statusCode, e === null || e === void 0 ? void 0 : e.message, e === null || e === void 0 ? void 0 : e.url, body].filter(Boolean).map((x) => String(x));
112
+ return { object: null, text: '', error: parts.join(' | ') || String(err) };
92
113
  }
93
114
  };
94
115
  exports.generateVision = generateVision;
package/dist/front.js CHANGED
@@ -202,6 +202,7 @@ const AiConfigProvider = (props) => {
202
202
  const [selectedIndex, setSelectedIndex] = (0, react_1.useState)(null);
203
203
  const [showPassword, setShowPassword] = (0, react_1.useState)(false);
204
204
  const [loadingModels, setLoadingModels] = (0, react_1.useState)(false);
205
+ const loadedModelsRef = (0, react_1.useRef)([]);
205
206
  (0, react_1.useEffect)(() => {
206
207
  setProviders(JSON.parse(JSON.stringify(props.providers)));
207
208
  setSelectedIndex(null);
@@ -210,26 +211,37 @@ const AiConfigProvider = (props) => {
210
211
  const [providerType, setProviderType] = (0, react_1.useState)((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
211
212
  const [providerKey, setProviderKey] = (0, react_1.useState)('');
212
213
  const [providerEndpoint, setProviderEndpoint] = (0, react_1.useState)('');
213
- const [pendingModels, setPendingModels] = (0, react_1.useState)([]);
214
+ const [pendingModels, setPendingModels] = (0, react_1.useState)([]); // solo para mostrar count en el botón
214
215
  const onProviderSelected = (p, index) => {
215
- var _a, _b, _c;
216
+ var _a, _b, _c, _d;
216
217
  setProviderName(p.name);
217
218
  setProviderType((_a = p.type) !== null && _a !== void 0 ? _a : p.name);
218
219
  setProviderKey(p.key);
219
220
  setProviderEndpoint((_b = p.endpoint) !== null && _b !== void 0 ? _b : '');
220
- setPendingModels((_c = p.models) !== null && _c !== void 0 ? _c : []);
221
+ loadedModelsRef.current = (_c = p.models) !== null && _c !== void 0 ? _c : [];
222
+ setPendingModels((_d = p.models) !== null && _d !== void 0 ? _d : []);
221
223
  setSelectedIndex(index);
222
224
  };
223
- const onNew = () => { var _a; setSelectedIndex(null); setProviderName(''); setProviderType((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : ''); setProviderKey(''); setProviderEndpoint(''); setPendingModels([]); };
225
+ const onNew = () => {
226
+ var _a;
227
+ setSelectedIndex(null);
228
+ setProviderName('');
229
+ setProviderType((_a = props.providersAvailable[0]) !== null && _a !== void 0 ? _a : '');
230
+ setProviderKey('');
231
+ setProviderEndpoint('');
232
+ loadedModelsRef.current = [];
233
+ setPendingModels([]);
234
+ };
224
235
  const onAdd = () => {
225
236
  if (!providerName.trim() || !providerType)
226
237
  return;
227
238
  const endpoint = providerType === 'openai-compat' ? providerEndpoint : undefined;
239
+ const models = loadedModelsRef.current;
228
240
  const updated = [...providers];
229
241
  if (selectedIndex !== null)
230
- updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models: pendingModels, endpoint });
242
+ updated[selectedIndex] = Object.assign(Object.assign({}, updated[selectedIndex]), { name: providerName, type: providerType, key: providerKey, models, endpoint });
231
243
  else
232
- updated.push({ name: providerName, type: providerType, key: providerKey, models: pendingModels, endpoint });
244
+ updated.push({ name: providerName, type: providerType, key: providerKey, models, endpoint });
233
245
  setProviders(updated);
234
246
  onNew();
235
247
  };
@@ -246,6 +258,7 @@ const AiConfigProvider = (props) => {
246
258
  try {
247
259
  const endpoint = providerType === 'openai-compat' ? providerEndpoint : undefined;
248
260
  const models = await props.onLoadModels({ name: providerName, type: providerType, key: providerKey, models: [], endpoint });
261
+ loadedModelsRef.current = models;
249
262
  setPendingModels(models);
250
263
  if (selectedIndex !== null) {
251
264
  const updated = [...providers];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kwirthmagnify/kwirth-common-ai",
3
- "version": "0.5.37",
3
+ "version": "0.5.39",
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",