UNPKG

donobu

Version:

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

91 lines 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleGenerativeAiGptClient = void 0; exports.fixAssertFields = fixAssertFields; const google_1 = require("@ai-sdk/google"); const v4_1 = require("zod/v4"); const MiscUtils_1 = require("../utils/MiscUtils"); const GptClient_1 = require("./GptClient"); const VercelAiGptClient_1 = require("./VercelAiGptClient"); /** * Implementation using the Google API * @see https://ai.google.dev/api/all-methods#service:-generativelanguage.googleapis.com */ class GoogleGenerativeAiGptClient extends GptClient_1.GptClient { constructor(config) { super(config); const google = (0, google_1.createGoogleGenerativeAI)({ apiKey: config.apiKey, }); const model = google(config.modelName); this.delegate = new VercelAiGptClient_1.VercelAiGptClient(model); } async ping(options) { return this.delegate.ping(options); } async getMessage(messages, options) { return this.delegate.getMessage(MiscUtils_1.MiscUtils.mergeAdjacentUserMessages(messages), options); } async getStructuredOutput(messages, zodSchema, options) { const preprocessedSchema = v4_1.z.preprocess((data) => { fixAssertFields(data); return data; }, zodSchema); const resp = await this.delegate.getStructuredOutput(MiscUtils_1.MiscUtils.mergeAdjacentUserMessages(messages), preprocessedSchema, options); return resp; } async getToolCalls(messages, tools, options) { const preprocessedTools = tools.map((tool) => ({ name: tool.name, description: tool.description, inputSchema: v4_1.z.preprocess((data) => { fixAssertFields(data); return data; }, tool.inputSchema), })); const resp = await this.delegate.getToolCalls(MiscUtils_1.MiscUtils.mergeAdjacentUserMessages(messages), preprocessedTools, options); resp.proposedToolCalls.forEach((t) => { fixAssertFields(t.parameters); }); return resp; } } exports.GoogleGenerativeAiGptClient = GoogleGenerativeAiGptClient; /** * Fix the "assert1" field issue in tool call parameters (Gemini has severe * troubles trying to say the word "assert" on its own... really). * * Recursively search and replace any field containing "assert1" with "assert" */ function fixAssertFields(obj) { if (!obj || typeof obj !== 'object') { return; } if (Array.isArray(obj)) { obj.forEach((item) => fixAssertFields(item)); return; } const objRecord = obj; // Check each property name for keys containing "assert1" const keys = Object.keys(objRecord); const keysToRename = []; keys.forEach((key) => { // If the key contains "assert1", prepare to rename it if (key.includes('assert1')) { // Create the new key by replacing all instances of "assert1" with "assert" const newKey = key.replace(/assert1/g, 'assert'); keysToRename.push({ oldKey: key, newKey }); } // Recursively process nested objects if (objRecord[key] && typeof objRecord[key] === 'object') { fixAssertFields(objRecord[key]); } }); // Apply all renames after iterating to avoid modifying the object during iteration keysToRename.forEach(({ oldKey, newKey }) => { const value = objRecord[oldKey]; delete objRecord[oldKey]; objRecord[newKey] = value; }); } //# sourceMappingURL=GoogleGenerativeAiGptClient.js.map