@runnerpro/backend 1.22.3 → 1.22.4

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.
@@ -260,6 +260,64 @@ function generateObject(options) {
260
260
  });
261
261
  }
262
262
  exports.generateObject = generateObject;
263
+ // Tope por cadena al derivar la firma de unos `messages`. Existe por las llamadas multimodales
264
+ // (analizar-foto manda una imagen en base64 de hasta 10 MB): sin él, cada foto metería megas en la
265
+ // clave y en la columna INPUT PROMPT. Dos payloads enormes que compartan los primeros 4000
266
+ // caracteres caerían en la misma fila, pero el ON CONFLICT acumula, así que el coste total sigue
267
+ // siendo correcto: solo se pierde granularidad.
268
+ const TEXT_SIG_MAX_CHARS = 4000;
269
+ /**
270
+ * Deriva el texto que identifica una llamada de `generateText`.
271
+ *
272
+ * El SDK admite `prompt` (string) o `messages` (array). Sin cubrir el segundo caso, todas las
273
+ * llamadas con `messages` compartirían system+prompt indefinidos y colapsarían en UNA sola fila.
274
+ */
275
+ function textPromptSignature(options) {
276
+ if (typeof (options === null || options === void 0 ? void 0 : options.prompt) === 'string')
277
+ return options.prompt;
278
+ if (!Array.isArray(options === null || options === void 0 ? void 0 : options.messages))
279
+ return undefined;
280
+ try {
281
+ return JSON.stringify(options.messages, (_k, v) => typeof v === 'string' && v.length > TEXT_SIG_MAX_CHARS ? `${v.slice(0, TEXT_SIG_MAX_CHARS)}…(${v.length})` : v);
282
+ }
283
+ catch (_a) {
284
+ return undefined;
285
+ }
286
+ }
287
+ /**
288
+ * Registra en "LLM CACHE" el coste de una llamada de `generateText`.
289
+ *
290
+ * REGISTRO, NO CACHÉ: no hay `lookup` previo y nunca se reutiliza el texto guardado. Cachear
291
+ * `generateText` daría a dos clientes con el mismo contexto la misma respuesta literal, que en el
292
+ * chat del entrenador es inaceptable. La fila existe solo para saber qué modelo respondió y cuánto
293
+ * costó; usa TEXT_CALL_SIG para no compartir espacio de claves con `generateObject`.
294
+ *
295
+ * Se anota el coste YA saneado, que incluye el retry anti-CoT y el sanitizer LITE si llegaron a
296
+ * correr. Ojo con la atribución: el gasto es el total real de la llamada lógica, pero la columna
297
+ * MODEL guarda el modelo principal, así que si intervino el sanitizer parte de ese coste es de LITE.
298
+ *
299
+ * Opt-out con `options.log === false`. Fire-and-forget: nunca bloquea ni rompe la llamada.
300
+ */
301
+ function logTextCost(options, modelName, result) {
302
+ if ((options === null || options === void 0 ? void 0 : options.log) === false)
303
+ return;
304
+ try {
305
+ const prompt = textPromptSignature(options);
306
+ const key = (0, llmCacheStore_1.llmCacheKey)({ system: options === null || options === void 0 ? void 0 : options.system, prompt, schemaSig: llmCacheStore_1.TEXT_CALL_SIG, temperature: options === null || options === void 0 ? void 0 : options.temperature });
307
+ (0, llmCacheStore_1.store)(key, {
308
+ modelName,
309
+ system: options === null || options === void 0 ? void 0 : options.system,
310
+ prompt,
311
+ schemaSig: llmCacheStore_1.TEXT_CALL_SIG,
312
+ temperature: options === null || options === void 0 ? void 0 : options.temperature,
313
+ output: result.text,
314
+ cost: result.cost,
315
+ });
316
+ }
317
+ catch (_a) {
318
+ // best-effort: registrar el coste jamás debe tumbar una generación válida
319
+ }
320
+ }
263
321
  /**
264
322
  * Wrapper de generateText con retry automático, cadena de fallbacks y saneamiento
265
323
  * de chain-of-thought.
@@ -304,6 +362,7 @@ function generateText(options) {
304
362
  const tracker = costTrackingStorage.getStore();
305
363
  if (tracker)
306
364
  tracker.push(cleanResult.cost);
365
+ logTextCost(currentOptions, modelName, cleanResult);
307
366
  return cleanResult;
308
367
  }
309
368
  catch (error) {
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.store = exports.lookup = exports.llmCacheKey = exports.schemaSignature = void 0;
12
+ exports.TEXT_CALL_SIG = exports.store = exports.lookup = exports.llmCacheKey = exports.schemaSignature = void 0;
13
13
  /**
14
14
  * Caché LLM (cache-aside) para `generateObject`.
15
15
  *
@@ -39,6 +39,13 @@ function ensureReady() {
39
39
  return _ready;
40
40
  });
41
41
  }
42
+ /**
43
+ * Firma reservada para las llamadas de `generateText`, que se registran por su coste pero NUNCA
44
+ * se cachean. Al ocupar su propio espacio de claves, una fila de texto no puede ser devuelta por
45
+ * el `lookup` de `generateObject` aunque coincidan system, prompt y temperatura.
46
+ */
47
+ const TEXT_CALL_SIG = '__text__';
48
+ exports.TEXT_CALL_SIG = TEXT_CALL_SIG;
42
49
  /** Firma ligera del schema (zod) para distinguir llamadas con el mismo prompt pero distinta forma. */
43
50
  function schemaSignature(schema) {
44
51
  var _a, _b;
@@ -1 +1 @@
1
- {"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../../../src/prompt/ai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,IAAI,sBAAsB,EAAE,YAAY,IAAI,oBAAoB,EAAE,MAAM,IAAI,CAAC;AAEpG,OAAO,EAAiB,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAwKhE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,iBAAe,cAAc,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,GAAG,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CAoD/H;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,iBAAe,YAAY,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CA0C5H;AA+ED;;;;;;;;;;;;;;;;;GAiBG;AACH,iBAAe,mBAAmB,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,CAAC,CAAC;IAAC,SAAS,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAgB5H;AAED,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,mBAAmB,EAAE,CAAC"}
1
+ {"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../../../src/prompt/ai.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,IAAI,sBAAsB,EAAE,YAAY,IAAI,oBAAoB,EAAE,MAAM,IAAI,CAAC;AAEpG,OAAO,EAAiB,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAwKhE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,iBAAe,cAAc,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,sBAAsB,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,GAAG,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CAoD/H;AA4DD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,iBAAe,YAAY,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CA4C5H;AA+ED;;;;;;;;;;;;;;;;;GAiBG;AACH,iBAAe,mBAAmB,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,CAAC,CAAC;IAAC,SAAS,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAgB5H;AAED,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,mBAAmB,EAAE,CAAC"}
@@ -1,4 +1,10 @@
1
1
  import type { CostResult } from './modelPricing';
2
+ /**
3
+ * Firma reservada para las llamadas de `generateText`, que se registran por su coste pero NUNCA
4
+ * se cachean. Al ocupar su propio espacio de claves, una fila de texto no puede ser devuelta por
5
+ * el `lookup` de `generateObject` aunque coincidan system, prompt y temperatura.
6
+ */
7
+ declare const TEXT_CALL_SIG = "__text__";
2
8
  /** Firma ligera del schema (zod) para distinguir llamadas con el mismo prompt pero distinta forma. */
3
9
  declare function schemaSignature(schema: any): string;
4
10
  /**
@@ -38,5 +44,5 @@ declare function store(key: string, data: {
38
44
  output: any;
39
45
  cost?: CostResult;
40
46
  }): void;
41
- export { schemaSignature, llmCacheKey, lookup, store };
47
+ export { schemaSignature, llmCacheKey, lookup, store, TEXT_CALL_SIG };
42
48
  //# sourceMappingURL=llmCacheStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"llmCacheStore.d.ts","sourceRoot":"","sources":["../../../../src/prompt/llmCacheStore.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAejD,sGAAsG;AACtG,iBAAS,eAAe,CAAC,MAAM,EAAE,GAAG,GAAG,MAAM,CAW5C;AAED;;;;GAIG;AACH,iBAAS,WAAW,CAAC,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAAG,MAAM,CAQ/H;AAED;;;;;;GAMG;AACH,iBAAe,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAWtD;AAED;;;;;;;;GAQG;AACH,iBAAS,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,SAA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AE,GAAG,IAAI,CAkCxL;AAED,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runnerpro/backend",
3
- "version": "1.22.3",
3
+ "version": "1.22.4",
4
4
  "description": "A collection of common backend functions",
5
5
  "exports": {
6
6
  ".": "./lib/cjs/index.js"