UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

56 lines 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AskAiApi = void 0; const GptApiKeysNotSetupException_1 = require("../exceptions/GptApiKeysNotSetupException"); const GptClientFactory_1 = require("../clients/GptClientFactory"); const InvalidParamValueException_1 = require("../exceptions/InvalidParamValueException"); /** * This API is a lightweight wrapper for a GPT client. */ class AskAiApi { constructor(gptConfigsManager, agentsManager) { this.gptConfigsManager = gptConfigsManager; this.agentsManager = agentsManager; } async ask(req, res) { const askAiRequest = req.body; if (!askAiRequest.prompt) { throw new InvalidParamValueException_1.InvalidParamValueException('prompt', askAiRequest.prompt); } const gptConfig = await this.resolveGptConfig(askAiRequest.gptConfigNameOverride); const gptClient = await GptClientFactory_1.GptClientFactory.createFromGptConfig(gptConfig); const gptRequestMessages = [ { type: 'user', items: [ { type: 'text', text: askAiRequest.prompt, }, ], }, ]; const response = askAiRequest.resultJsonSchema ? await gptClient.getStructuredOutput(gptRequestMessages, askAiRequest.resultJsonSchema) : await gptClient.getMessage(gptRequestMessages); res.json(response); } /** * If specified, attempts to load the given GPT config by name, otherwise, * attempts to load the GPT configuration for the 'flow-runner' agent. */ async resolveGptConfig(gptConfigNameOverride) { if (gptConfigNameOverride) { return await this.gptConfigsManager.get(gptConfigNameOverride); } else { const gptConfigName = await this.agentsManager.get('flow-runner'); if (!gptConfigName) { throw new GptApiKeysNotSetupException_1.GptApiKeysNotSetupException(); } return await this.gptConfigsManager.get(gptConfigName); } } } exports.AskAiApi = AskAiApi; //# sourceMappingURL=AskAiApi.js.map