From e09e3501faf62bf88473ffdb2cd93bc82bc7bee9 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 23 Oct 2024 06:04:01 +0000 Subject: [PATCH] do not drop genaiscript.d.ts for prompty folders --- packages/core/src/ast.ts | 9 +++++++-- packages/core/src/prompty.ts | 9 ++++----- packages/core/src/template.ts | 2 +- packages/core/src/types/prompt_template.d.ts | 5 +---- packages/sample/genaisrc/defrange.genai.mjs | 2 +- packages/sample/genaisrc/import-prompty.genai.mjs | 4 ++-- packages/sample/genaisrc/import-template.genai.mts | 2 +- packages/sample/src/{ => templates}/basic.prompty | 0 8 files changed, 17 insertions(+), 16 deletions(-) rename packages/sample/src/{ => templates}/basic.prompty (100%) diff --git a/packages/core/src/ast.ts b/packages/core/src/ast.ts index 050025d158..5501a919ae 100644 --- a/packages/core/src/ast.ts +++ b/packages/core/src/ast.ts @@ -1,6 +1,10 @@ /// // Import necessary regular expressions for file type detection and host utilities -import { GENAI_ANYJS_REGEX, GENAI_ANYTS_REGEX } from "./constants" +import { + GENAI_ANYJS_REGEX, + GENAI_ANYTS_REGEX, + PROMPTY_REGEX, +} from "./constants" import { host } from "./host" // Type alias for PromptScript used globally @@ -73,7 +77,8 @@ export class Project { { dirname: string; js?: boolean; ts?: boolean } > = {} for (const t of Object.values(this.templates).filter( - (t) => t.filename // Filter templates that have a filename set + // must have a filename and not propmty + (t) => t.filename && !PROMPTY_REGEX.test(t.filename) )) { const dirname = host.path.dirname(t.filename) // Get directory name from the filename const folder = folders[dirname] || (folders[dirname] = { dirname }) diff --git a/packages/core/src/prompty.ts b/packages/core/src/prompty.ts index 6577b21a45..8cbaa02c4e 100644 --- a/packages/core/src/prompty.ts +++ b/packages/core/src/prompty.ts @@ -84,8 +84,7 @@ function promptyFrontmatterToMeta(frontmatter: PromptyFrontmatter): PromptArgs { let modelName: string = undefined if (api !== "chat") throw new Error("completion api not supported") if (modelParameters?.n > 1) throw new Error("multi-turn not supported") - if (modelParameters?.tools?.length) - throw new Error("streaming not supported") + if (modelParameters?.tools?.length) throw new Error("tools not supported") // resolve model if ( @@ -120,11 +119,11 @@ function promptyFrontmatterToMeta(frontmatter: PromptyFrontmatter): PromptArgs { return meta } -export function promptyParse(text: string): PromptyDocument { +export function promptyParse(filename: string, text: string): PromptyDocument { const { frontmatter = "", content = "" } = splitMarkdown(text) const fm = YAMLTryParse(frontmatter) ?? {} - const meta = fm ? promptyFrontmatterToMeta(fm) : {} - // todo: validate frontmatter? + const meta: PromptArgs = fm ? promptyFrontmatterToMeta(fm) : {} + meta.filename = filename const messages: ChatCompletionMessageParam[] = [] // split diff --git a/packages/core/src/template.ts b/packages/core/src/template.ts index 7a189adbd4..cfdfe134ec 100644 --- a/packages/core/src/template.ts +++ b/packages/core/src/template.ts @@ -411,7 +411,7 @@ export async function parsePromptScript( prj: Project ) { if (PROMPTY_REGEX.test(filename)) { - const doc = await promptyParse(content) + const doc = await promptyParse(filename, content) content = await promptyToGenAIScript(doc) } diff --git a/packages/core/src/types/prompt_template.d.ts b/packages/core/src/types/prompt_template.d.ts index 07dcd2cbcd..6673357d99 100644 --- a/packages/core/src/types/prompt_template.d.ts +++ b/packages/core/src/types/prompt_template.d.ts @@ -747,10 +747,7 @@ interface ExpansionVariables { type MakeOptional = Partial> & Omit -type PromptArgs = Omit< - PromptScript, - "text" | "id" | "jsSource" | "activation" | "defTools" -> +type PromptArgs = Omit type PromptSystemArgs = Omit< PromptArgs, diff --git a/packages/sample/genaisrc/defrange.genai.mjs b/packages/sample/genaisrc/defrange.genai.mjs index 37e10e8b35..287412388c 100644 --- a/packages/sample/genaisrc/defrange.genai.mjs +++ b/packages/sample/genaisrc/defrange.genai.mjs @@ -1,6 +1,6 @@ script({ model: "small", - files: "src/basic.prompty", + files: "src/templates/basic.prompty", tests: { keywords: ["CORRECT1", "CORRECT2", "CORRECT3"], }, diff --git a/packages/sample/genaisrc/import-prompty.genai.mjs b/packages/sample/genaisrc/import-prompty.genai.mjs index 3e5443772f..2e34897da3 100644 --- a/packages/sample/genaisrc/import-prompty.genai.mjs +++ b/packages/sample/genaisrc/import-prompty.genai.mjs @@ -6,14 +6,14 @@ script({ }) const res = await runPrompt((ctx) => { - ctx.importTemplate("src/basic.prompty", { + ctx.importTemplate("src/templates/basic.prompty", { question: "what is the capital of france?", hint: "starts with p", }) }) console.log(`inline: ${res.text}`) -importTemplate("src/basic.prompty", { +importTemplate("src/templates/basic.prompty", { question: "what is the capital of france?", hint: "starts with p", }) diff --git a/packages/sample/genaisrc/import-template.genai.mts b/packages/sample/genaisrc/import-template.genai.mts index a563c9edb2..d63c8dd060 100644 --- a/packages/sample/genaisrc/import-template.genai.mts +++ b/packages/sample/genaisrc/import-template.genai.mts @@ -8,4 +8,4 @@ const question = "What is the capital of France?" const hint = () => { return "Also add 'abracadabra' to the answer." } -importTemplate("src/basic.prompty", { question, hint }) +importTemplate("src/templates/basic.prompty", { question, hint }) diff --git a/packages/sample/src/basic.prompty b/packages/sample/src/templates/basic.prompty similarity index 100% rename from packages/sample/src/basic.prompty rename to packages/sample/src/templates/basic.prompty