@kwirthmagnify/kwirth-common-ai 0.5.38 → 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.
Files changed (2) hide show
  1. package/dist/back.js +27 -6
  2. package/package.json +1 -1
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kwirthmagnify/kwirth-common-ai",
3
- "version": "0.5.38",
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",