UNPKG

@convo-lang/convo-lang-aws-cdk

Version:
108 lines 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handler = void 0; const convo_lang_1 = require("@convo-lang/convo-lang"); const convo_lang_api_routes_1 = require("@convo-lang/convo-lang-api-routes"); const common_1 = require("@iyio/common"); const node_common_1 = require("@iyio/node-common"); const convo_lang_aws_cdk_lib_1 = require("../convo-lang-aws-cdk-lib"); const price_capping_1 = require("../price-capping"); (0, convo_lang_aws_cdk_lib_1.initBackend)(); class TokenLimitError extends Error { } let currentRemoteAddress = '0.0.0.0'; let routes = null; const getRoutes = () => { return routes ?? (routes = (0, convo_lang_api_routes_1.createConvoLangApiRoutes)({ enableMockRoute: true, prefix: '', onCompletion: async ({ result, flat }) => { if (flat.apiKeyUsedForCompletion) { return; } let price = 0; for (const m of result) { if (m.tokenPrice) { price += m.tokenPrice; } else { price += ((m.inputTokens ?? 0) * convo_lang_1.unknownConvoTokenPrice) + ((m.outputTokens ?? 0) * convo_lang_1.unknownConvoTokenPrice); } } await (0, price_capping_1.storeTokenUsageAsync)(price, { remoteAddress: currentRemoteAddress }); }, completionCtx: { beforeComplete: async (service, input, flat) => { if (flat.apiKeyUsedForCompletion) { return; } const quota = await (0, price_capping_1.checkTokenQuotaAsync)({ remoteAddress: currentRemoteAddress }); if (!quota.allow) { throw new TokenLimitError('Token limit reached'); } }, }, getUsage: async () => { return await (0, price_capping_1.getTokenQuotaAsync)({ remoteAddress: currentRemoteAddress }); } })); }; const ConvoLangAPi = async (fnEvt, input) => { const routes = getRoutes(); const method = fnEvt.method; let path = fnEvt.path; if (path.startsWith('/api/')) { path = path.substring(4); } currentRemoteAddress = fnEvt.remoteAddress || '0.0.0.0'; const qi = path.indexOf('?'); const query = qi === -1 ? {} : (0, common_1.queryParamsToObject)(path.substring(qi)); if (qi !== -1) { path = path.substring(0, qi); } const route = (0, node_common_1.getHttpRoute)(path, method, routes); if (!route) { throw new common_1.NotFoundError('Route not found'); } const regex = (0, common_1.asArrayItem)(route.match); if (regex) { const match = regex.exec(path); if (match) { for (let i = 1; i < match.length; i++) { const value = match[i]; query[i.toString()] = decodeURIComponent(value ?? ''); } } } try { return await route.handler({ req: {}, // convo-lang routes do not use the req object res: {}, // convo-lang routes do not use the res object path, filePath: '.', body: input, method, query }); } catch (ex) { if (ex instanceof TokenLimitError) { const msg = { tags: { [convo_lang_1.convoTags.tokenLimit]: '' }, role: convo_lang_1.convoRoles.assistant, content: ex.message }; return [msg]; } else { throw ex; } } finally { currentRemoteAddress = '0.0.0.0'; } }; exports.handler = (0, common_1.createFnHandler)(ConvoLangAPi, {}); //# sourceMappingURL=ConvoLangApi.js.map