donobu
Version:
Create browser automations with an LLM agent and replay them as Playwright scripts.
68 lines • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AskAiApi = void 0;
const v4_1 = require("zod/v4");
const GptApiKeysNotSetupException_1 = require("../exceptions/GptApiKeysNotSetupException");
const JsonSchemaUtils_1 = require("../utils/JsonSchemaUtils");
/**
* This API is a lightweight wrapper for a GPT client.
*/
class AskAiApi {
constructor(gptClientFactory, gptConfigsManager, agentsManager) {
this.gptClientFactory = gptClientFactory;
this.gptConfigsManager = gptConfigsManager;
this.agentsManager = agentsManager;
}
async ask(req, res) {
const parsedBody = v4_1.z
.object({
/**
* The prompt to use with the AI to generate a response from.
*/
prompt: v4_1.z.string(),
/**
* The name of a specific GPT configuration to use. If this is not defined,
* the default agent configuration will be used.
**/
gptConfigNameOverride: v4_1.z.string().optional(),
/** If non-null, will attempt to respond using this JSON-schema. */
resultJsonSchema: v4_1.z.custom().optional(),
})
.parse(req.body);
const gptConfig = await this.resolveGptConfig(parsedBody.gptConfigNameOverride);
const gptClient = await this.gptClientFactory.createClient(gptConfig);
const gptRequestMessages = [
{
type: 'user',
items: [
{
type: 'text',
text: parsedBody.prompt,
},
],
},
];
const response = parsedBody.resultJsonSchema
? await gptClient.getStructuredOutput(gptRequestMessages, (0, JsonSchemaUtils_1.jsonSchemaToZod)(parsedBody.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