@genkit-ai/dotprompt
Version:
Genkit AI framework `.prompt` file format and management library.
1 lines • 7.42 kB
Source Map (JSON)
{"version":3,"sources":["../src/registry.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptAction } from '@genkit-ai/ai';\nimport { GenkitError } from '@genkit-ai/core';\nimport { logger } from '@genkit-ai/core/logging';\nimport { lookupAction } from '@genkit-ai/core/registry';\nimport { existsSync, readdir, readFileSync } from 'fs';\nimport { basename, join, resolve } from 'path';\nimport { Dotprompt } from './prompt.js';\nimport { definePartial } from './template.js';\n\nexport function registryDefinitionKey(\n name: string,\n variant?: string,\n ns?: string\n) {\n // \"ns/prompt.variant\" where ns and variant are optional\n return `${ns ? `${ns}/` : ''}${name}${variant ? `.${variant}` : ''}`;\n}\n\nexport function registryLookupKey(name: string, variant?: string, ns?: string) {\n return `/prompt/${registryDefinitionKey(name, variant, ns)}`;\n}\n\nexport async function lookupPrompt(\n name: string,\n variant?: string,\n dir: string = './prompts'\n): Promise<Dotprompt> {\n let registryPrompt =\n (await lookupAction(registryLookupKey(name, variant))) ||\n (await lookupAction(registryLookupKey(name, variant, 'dotprompt')));\n if (registryPrompt) {\n return Dotprompt.fromAction(registryPrompt as PromptAction);\n } else {\n // Handle the case where initialization isn't complete\n // or a file was added after the prompt folder was loaded.\n return maybeLoadPrompt(dir, name, variant);\n }\n}\n\nasync function maybeLoadPrompt(\n dir: string,\n name: string,\n variant?: string\n): Promise<Dotprompt> {\n const expectedFileName = `${name}${variant ? `.${variant}` : ''}.prompt`;\n const promptFolder = resolve(dir);\n const promptExists = existsSync(join(promptFolder, expectedFileName));\n if (promptExists) {\n return loadPrompt(promptFolder, expectedFileName);\n } else {\n throw new GenkitError({\n source: 'dotprompt',\n status: 'NOT_FOUND',\n message: `Could not find '${expectedFileName}' in the prompts folder.`,\n });\n }\n}\n\nexport async function loadPromptFolder(\n dir: string = './prompts'\n): Promise<void> {\n const promptsPath = resolve(dir);\n return new Promise<void>((resolve, reject) => {\n if (existsSync(promptsPath)) {\n readdir(\n promptsPath,\n {\n withFileTypes: true,\n recursive: true,\n },\n (err, dirEnts) => {\n if (err) {\n reject(err);\n } else {\n dirEnts.forEach(async (dirEnt) => {\n if (dirEnt.isFile() && dirEnt.name.endsWith('.prompt')) {\n if (dirEnt.name.startsWith('_')) {\n console.log(dirEnt.name);\n const partialName = dirEnt.name.substring(\n 1,\n dirEnt.name.length - 7\n );\n definePartial(\n partialName,\n readFileSync(join(dirEnt.path, dirEnt.name), {\n encoding: 'utf8',\n })\n );\n logger.debug(\n `Registered Dotprompt partial \"${partialName}\" from \"${join(dirEnt.path, dirEnt.name)}\"`\n );\n } else {\n // If this prompt is in a subdirectory, we need to include that\n // in the namespace to prevent naming conflicts.\n let prefix = '';\n if (promptsPath !== dirEnt.path) {\n prefix = dirEnt.path\n .replace(`${promptsPath}/`, '')\n .replace(/\\//g, '-');\n }\n loadPrompt(dirEnt.path, dirEnt.name, prefix);\n }\n }\n });\n resolve();\n }\n }\n );\n } else {\n resolve();\n }\n });\n}\n\nexport function loadPrompt(\n path: string,\n filename: string,\n prefix = ''\n): Dotprompt {\n let name = `${prefix ? `${prefix}-` : ''}${basename(filename, '.prompt')}`;\n let variant: string | null = null;\n if (name.includes('.')) {\n const parts = name.split('.');\n name = parts[0];\n variant = parts[1];\n }\n const source = readFileSync(join(path, filename), 'utf8');\n const prompt = Dotprompt.parse(name, source);\n if (variant) {\n prompt.variant = variant;\n }\n prompt.define({ ns: `dotprompt` });\n return prompt;\n}\n"],"mappings":";;;AAiBA,SAAS,mBAAmB;AA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