@genkit-ai/dotprompt
Version:
Genkit AI framework `.prompt` file format and management library.
1 lines • 3.42 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.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 {\n genkitPlugin,\n InitializedPlugin,\n PluginProvider,\n} from '@genkit-ai/core';\n\nimport { readFileSync } from 'fs';\nimport { basename } from 'path';\nimport { defineDotprompt, Dotprompt, DotpromptRef } from './prompt.js';\nimport { loadPromptFolder, lookupPrompt } from './registry.js';\n\nexport { defineHelper, definePartial } from './template.js';\nexport { defineDotprompt, Dotprompt };\n\nexport interface DotpromptPluginOptions {\n // Directory to look for .prompt files.\n //\n // Note: This directory will be searched recursively, and any sub-directory\n // paths will be included in the prompt name. E.g. - if a prompt file is\n // located at `<dir>/foo/bar.prompt`, the prompt name will be `foo-bar`.\n dir: string;\n}\n\nexport function dotprompt(\n params: DotpromptPluginOptions = { dir: './prompts' }\n): PluginProvider {\n const plugin = genkitPlugin(\n 'dotprompt',\n async (options: DotpromptPluginOptions): Promise<InitializedPlugin> => {\n await loadPromptFolder(options.dir);\n return {};\n }\n );\n return plugin(params);\n}\n\nexport async function prompt<Variables = unknown>(\n name: string,\n options?: { variant?: string }\n): Promise<Dotprompt<Variables>> {\n return (await lookupPrompt(name, options?.variant)) as Dotprompt<Variables>;\n}\n\nexport function promptRef<Variables = unknown>(\n name: string,\n options?: { variant?: string; dir?: string }\n): DotpromptRef<Variables> {\n return new DotpromptRef(name, options);\n}\n\nexport function loadPromptFile(path: string): Dotprompt {\n return Dotprompt.parse(\n basename(path).split('.')[0],\n readFileSync(path, 'utf-8')\n );\n}\n\nexport async function loadPromptUrl(\n name: string,\n url: string\n): Promise<Dotprompt> {\n const fetch = (await import('node-fetch')).default;\n const response = await fetch(url);\n const text = await response.text();\n return Dotprompt.parse(name, text);\n}\n"],"mappings":";;;AAgBA;AAAA,EACE;AAAA,OAGK;AAEP,SAAS,oBAAoB;AAC7B,SAAS,gBAAgB;AACzB,SAAS,iBAAiB,WAAW,oBAAoB;AACzD,SAAS,kBAAkB,oBAAoB;AAE/C,SAAS,cAAc,qBAAqB;AAYrC,SAAS,UACd,SAAiC,EAAE,KAAK,YAAY,GACpC;AAChB,QAAM,SAAS;AAAA,IACb;AAAA,IACA,CAAO,YAAgE;AACrE,YAAM,iBAAiB,QAAQ,GAAG;AAClC,aAAO,CAAC;AAAA,IACV;AAAA,EACF;AACA,SAAO,OAAO,MAAM;AACtB;AAEA,SAAsB,OACpB,MACA,SAC+B;AAAA;AAC/B,WAAQ,MAAM,aAAa,MAAM,mCAAS,OAAO;AAAA,EACnD;AAAA;AAEO,SAAS,UACd,MACA,SACyB;AACzB,SAAO,IAAI,aAAa,MAAM,OAAO;AACvC;AAEO,SAAS,eAAe,MAAyB;AACtD,SAAO,UAAU;AAAA,IACf,SAAS,IAAI,EAAE,MAAM,GAAG,EAAE,CAAC;AAAA,IAC3B,aAAa,MAAM,OAAO;AAAA,EAC5B;AACF;AAEA,SAAsB,cACpB,MACA,KACoB;AAAA;AACpB,UAAM,SAAS,MAAM,OAAO,YAAY,GAAG;AAC3C,UAAM,WAAW,MAAM,MAAM,GAAG;AAChC,UAAM,OAAO,MAAM,SAAS,KAAK;AACjC,WAAO,UAAU,MAAM,MAAM,IAAI;AAAA,EACnC;AAAA;","names":[]}