|
| 1 | +import { prompt } from "../../prompt"; |
| 2 | +import { loadAll } from "../../../dataconnect/load"; |
| 3 | +import type { ServiceInfo } from "../../../dataconnect/types"; |
| 4 | +import { BUILTIN_SDL, MAIN_INSTRUCTIONS } from "../../util/dataconnect/content"; |
| 5 | +import { compileErrors } from "../../util/dataconnect/compile"; |
| 6 | + |
| 7 | +function renderServices(fdcServices: ServiceInfo[]) { |
| 8 | + if (!fdcServices.length) return "Data Connect Status: <UNCONFIGURED>"; |
| 9 | + |
| 10 | + return `\n\n## Data Connect Schema |
| 11 | +
|
| 12 | +The following is the up-to-date content of existing schema files (their paths are relative to the Data Connect source directory). |
| 13 | +
|
| 14 | +${fdcServices[0].schema.source.files?.map((f) => `\`\`\`graphql ${f.path}\n${f.content}\n\`\`\``).join("\n\n")}`; |
| 15 | +} |
| 16 | + |
| 17 | +function renderErrors(errors?: string) { |
| 18 | + return `\n\n## Current Schema Build Errors\n\n${errors || "<NO ERRORS>"}`; |
| 19 | +} |
| 20 | + |
| 21 | +export const schema = prompt( |
| 22 | + { |
| 23 | + name: "schema", |
| 24 | + description: "Generate or update your Firebase Data Connect schema.", |
| 25 | + arguments: [ |
| 26 | + { |
| 27 | + name: "prompt", |
| 28 | + description: |
| 29 | + "describe the schema you want generated or the edits you want to make to your existing schema", |
| 30 | + required: true, |
| 31 | + }, |
| 32 | + ], |
| 33 | + annotations: { |
| 34 | + title: "Generate Data Connect Schema", |
| 35 | + }, |
| 36 | + }, |
| 37 | + async ({ prompt }, { config, projectId, accountEmail }) => { |
| 38 | + const fdcServices = await loadAll(projectId, config); |
| 39 | + const buildErrors = fdcServices.length |
| 40 | + ? await compileErrors(fdcServices[0].sourceDirectory) |
| 41 | + : ""; |
| 42 | + |
| 43 | + return [ |
| 44 | + { |
| 45 | + role: "user" as const, |
| 46 | + content: { |
| 47 | + type: "text", |
| 48 | + text: ` |
| 49 | +${MAIN_INSTRUCTIONS}\n\n${BUILTIN_SDL} |
| 50 | +
|
| 51 | +==== CURRENT ENVIRONMENT INFO ==== |
| 52 | +
|
| 53 | +User Email: ${accountEmail || "<NONE>"} |
| 54 | +Project ID: ${projectId || "<NONE>"} |
| 55 | +${renderServices(fdcServices)}${renderErrors(buildErrors)} |
| 56 | +
|
| 57 | +==== USER PROMPT ==== |
| 58 | +
|
| 59 | +${prompt} |
| 60 | +
|
| 61 | +==== TASK INSTRUCTIONS ==== |
| 62 | +
|
| 63 | +1. If Data Connect is marked as \`<UNCONFIGURED>\`, first run the \`firebase_init\` tool with \`{dataconnect: {}}\` arguments to initialize it. |
| 64 | +2. If there is not an existing schema to work with (or the existing schema is the commented-out default schema about a movie app), follow the user's prompt to generate a robust schema meeting the specified requirements. |
| 65 | +3. If there is already a schema, perform edits to the existing schema file(s) based on the user's instructions. If schema build errors are present and seem relevant to your changes, attempt to fix them. |
| 66 | +4. After you have performed edits on the schema, run the \`dataconnect_compile\` tool to build the schema and see if there are any errors. Fix errors that are related to the user's prompt or your changes. |
| 67 | +5. If there are errors, attempt to fix them. If you have attempted to fix them 3 times without success, ask the user for help. |
| 68 | +6. If there are no errors, write a brief paragraph summarizing your changes.`, |
| 69 | + }, |
| 70 | + }, |
| 71 | + ]; |
| 72 | + }, |
| 73 | +); |
0 commit comments