Files
kontrans.pagedev.pl/node_modules/@lowire/loop/lib/cache.js
2026-03-03 23:49:13 +01:00

67 lines
2.7 KiB
JavaScript

"use strict";
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cachedComplete = cachedComplete;
const crypto_1 = __importDefault(require("crypto"));
async function cachedComplete(provider, conversation, caches, options) {
const c = hideSecrets(conversation, options.secrets);
const result = await cachedCompleteNoSecrets(provider, c, caches, options);
return unhideSecrets(result, options.secrets);
}
async function cachedCompleteNoSecrets(provider, conversation, caches, options) {
if (!caches)
return await provider.complete(conversation, options);
const keyObject = {
conversation: options.cacheMode === 'lax' ? { ...conversation, tools: [] } : conversation,
maxTokens: options.maxTokens,
reasoning: options.reasoning,
temperature: options.temperature,
};
const key = calculateSha1(JSON.stringify(keyObject));
if (!process.env.LOWIRE_NO_CACHE && caches.input[key]) {
caches.output[key] = caches.input[key];
return caches.input[key] ?? caches.output[key];
}
if (!process.env.LOWIRE_NO_CACHE && caches.output[key])
return caches.output[key];
if (process.env.LOWIRE_FORCE_CACHE)
throw new Error('Cache missing but LOWIRE_FORCE_CACHE is set' + JSON.stringify(conversation, null, 2));
const result = await provider.complete(conversation, options);
caches.output[key] = result;
return result;
}
function hideSecrets(conversation, secrets = {}) {
let text = JSON.stringify(conversation);
for (const [key, value] of Object.entries(secrets))
text = text.replaceAll(value, `%${key}%`);
return JSON.parse(text);
}
function unhideSecrets(message, secrets = {}) {
let text = JSON.stringify(message);
for (const [key, value] of Object.entries(secrets))
text = text.replaceAll(`%${key}%`, value);
return JSON.parse(text);
}
function calculateSha1(text) {
const hash = crypto_1.default.createHash('sha1');
hash.update(text);
return hash.digest('hex');
}