@kwirthmagnify/kwirth-common-ai 0.5.38 → 0.5.40

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 +28 -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,13 @@ 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
+ // baseURL debe terminar en /v1 (el SDK añade el path). Y usamos .chat() → Chat Completions API
70
+ // (/v1/chat/completions): los endpoints OpenAI-compatibles (Huawei MaaS, etc.) NO exponen la
71
+ // Responses API (/v1/responses) → si no, 404 APIG.0101.
72
+ const b = ((_c = prov === null || prov === void 0 ? void 0 : prov.endpoint) !== null && _c !== void 0 ? _c : '').replace(/\/+$/, '');
73
+ return (0, openai_1.createOpenAI)({ apiKey: key, baseURL: b.endsWith('/v1') ? b : `${b}/v1` }).chat(llm.model);
74
+ }
69
75
  default:
70
76
  console.log('Invalid provider type', type);
71
77
  return null;
@@ -76,19 +82,35 @@ exports.buildModel = buildModel;
76
82
  // un schema Zod (patrón Output.object). Reutilizable por toda la plataforma (Iter la usa para extraer un mapa
77
83
  // de un diagrama). No lanza: los errores vuelven en `error`.
78
84
  const generateVision = async (opts) => {
79
- var _a, _b, _c;
85
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
80
86
  try {
81
87
  const imagePart = opts.mediaType
82
88
  ? { type: 'image', image: opts.image, mediaType: opts.mediaType }
83
89
  : { 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: [{
90
+ console.log('[generateVision] request', {
91
+ 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)',
92
+ 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)',
93
+ mediaType: opts.mediaType, imageBytes: ((_k = opts.image) !== null && _k !== void 0 ? _k : '').length,
94
+ systemLen: ((_l = opts.system) !== null && _l !== void 0 ? _l : '').length, providerOptions: opts.providerOptions,
95
+ });
96
+ 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
97
  role: 'user',
86
98
  content: [{ type: 'text', text: opts.prompt }, imagePart],
87
99
  }], 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 };
100
+ console.log('[generateVision] ok', { hasOutput: output != null, textLen: (text !== null && text !== void 0 ? text : '').length, usage });
101
+ return { object: (_o = output) !== null && _o !== void 0 ? _o : null, text: text !== null && text !== void 0 ? text : '', usage };
89
102
  }
90
103
  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) };
104
+ const e = err;
105
+ 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);
106
+ // TRAZA COMPLETA del fallo de la llamada al LLM (URL, status, cuerpo de respuesta, causa).
107
+ console.error('[generateVision] FAILED', {
108
+ 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,
109
+ 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,
110
+ requestBody: typeof (e === null || e === void 0 ? void 0 : e.requestBodyValues) === 'object' ? '[requestBodyValues present]' : undefined,
111
+ });
112
+ 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));
113
+ return { object: null, text: '', error: parts.join(' | ') || String(err) };
92
114
  }
93
115
  };
94
116
  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.40",
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",