|
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 5 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 6 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 7 | + } |
| 8 | + Object.defineProperty(o, k2, desc); |
| 9 | +}) : (function(o, m, k, k2) { |
| 10 | + if (k2 === undefined) k2 = k; |
| 11 | + o[k2] = m[k]; |
| 12 | +})); |
| 13 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 14 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 15 | +}) : function(o, v) { |
| 16 | + o["default"] = v; |
| 17 | +}); |
| 18 | +var __importStar = (this && this.__importStar) || (function () { |
| 19 | + var ownKeys = function(o) { |
| 20 | + ownKeys = Object.getOwnPropertyNames || function (o) { |
| 21 | + var ar = []; |
| 22 | + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; |
| 23 | + return ar; |
| 24 | + }; |
| 25 | + return ownKeys(o); |
| 26 | + }; |
| 27 | + return function (mod) { |
| 28 | + if (mod && mod.__esModule) return mod; |
| 29 | + var result = {}; |
| 30 | + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); |
| 31 | + __setModuleDefault(result, mod); |
| 32 | + return result; |
| 33 | + }; |
| 34 | +})(); |
| 35 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 36 | +exports.componentGeneratorFlow = void 0; |
| 37 | +const google_genai_1 = require("@genkit-ai/google-genai"); |
| 38 | +const genkit_1 = require("genkit"); |
| 39 | +const fs = __importStar(require("fs")); |
| 40 | +const path = __importStar(require("path")); |
| 41 | +const openai_1 = require("@genkit-ai/compat-oai/openai"); |
| 42 | +const genkitx_anthropic_1 = require("genkitx-anthropic"); |
| 43 | +// Read the schema file |
| 44 | +const schemaString = fs.readFileSync(path.join(__dirname, 'schema.json'), 'utf-8'); |
| 45 | +const schema = JSON.parse(schemaString); |
| 46 | +const ai = (0, genkit_1.genkit)({ |
| 47 | + plugins: [(0, google_genai_1.googleAI)({ apiKey: process.env.GEMINI_API_KEY }), (0, openai_1.openAI)(), (0, genkitx_anthropic_1.anthropic)({ apiKey: process.env.ANTHROPIC_API_KEY }),], |
| 48 | +}); |
| 49 | +// Define a UI component generator flow |
| 50 | +exports.componentGeneratorFlow = ai.defineFlow({ |
| 51 | + name: 'componentGeneratorFlow', |
| 52 | + inputSchema: genkit_1.z.object({ prompt: genkit_1.z.string(), model: genkit_1.z.any() }), |
| 53 | + outputSchema: genkit_1.z.any(), |
| 54 | +}, async ({ prompt, model }) => { |
| 55 | + // Generate structured component data using the schema from the file |
| 56 | + const { output } = await ai.generate({ |
| 57 | + prompt, |
| 58 | + model, |
| 59 | + output: { jsonSchema: schema }, |
| 60 | + // config: { |
| 61 | + // thinkingConfig: { thinkingBudget: 0 } |
| 62 | + // }, |
| 63 | + }); |
| 64 | + if (!output) |
| 65 | + throw new Error('Failed to generate component'); |
| 66 | + return output; |
| 67 | +}); |
| 68 | +// Run the flow |
| 69 | +async function main() { |
| 70 | + const models = [ |
| 71 | + openai_1.openAI.model('gpt-5-mini'), |
| 72 | + openai_1.openAI.model('gpt-5'), |
| 73 | + openai_1.openAI.model('gpt-5-nano'), |
| 74 | + google_genai_1.googleAI.model('gemini-2.5-flash'), |
| 75 | + google_genai_1.googleAI.model('gemini-2.5-flash-lite'), |
| 76 | + genkitx_anthropic_1.claude4Sonnet, |
| 77 | + genkitx_anthropic_1.claude35Haiku, |
| 78 | + ]; |
| 79 | + const prompt = `Generate a JSON conforming to the schema to describe the following UI: |
| 80 | +
|
| 81 | +A root node has already been created with ID "root". You need to create a ComponentUpdate message now. |
| 82 | +
|
| 83 | +A vertical list with: |
| 84 | +Dog breed information |
| 85 | +Dog generator |
| 86 | +
|
| 87 | +The dog breed information is a card, which contains a title “Famous Dog breeds”, a header image, and a carousel of different dog breeds. The carousel information should be in the data model at /carousel. |
| 88 | +
|
| 89 | +The dog generator is another card which is a form that generates a fictional dog breed with a description |
| 90 | +- Title |
| 91 | +- Description text explaining what it is |
| 92 | +- Dog breed name (text input) |
| 93 | +- Number of legs (number input) |
| 94 | +- Skills (checkboxes) |
| 95 | +- Button called “Generate” which takes the data above and generates a new dog description |
| 96 | +- A divider |
| 97 | +- A section which shows the generated content |
| 98 | +`; |
| 99 | + for (const model of models) { |
| 100 | + console.log(`Generating component with model: ${model.name}`); |
| 101 | + const component = await (0, exports.componentGeneratorFlow)({ |
| 102 | + prompt, |
| 103 | + model, |
| 104 | + }); |
| 105 | + console.log(JSON.stringify(component, null, 2)); |
| 106 | + } |
| 107 | +} |
| 108 | +main().catch(console.error); |
0 commit comments