Skip to content

Commit

Permalink
do not drop genaiscript.d.ts for prompty folders
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 23, 2024
1 parent bfe4a57 commit e09e350
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 16 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/// <reference path="./types/prompt_template.d.ts" />
// 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
Expand Down Expand Up @@ -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 })
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/prompty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,7 @@ interface ExpansionVariables {

type MakeOptional<T, P extends keyof T> = Partial<Pick<T, P>> & Omit<T, P>

type PromptArgs = Omit<
PromptScript,
"text" | "id" | "jsSource" | "activation" | "defTools"
>
type PromptArgs = Omit<PromptScript, "text" | "id" | "jsSource" | "defTools">

type PromptSystemArgs = Omit<
PromptArgs,
Expand Down
2 changes: 1 addition & 1 deletion packages/sample/genaisrc/defrange.genai.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script({
model: "small",
files: "src/basic.prompty",
files: "src/templates/basic.prompty",
tests: {
keywords: ["CORRECT1", "CORRECT2", "CORRECT3"],
},
Expand Down
4 changes: 2 additions & 2 deletions packages/sample/genaisrc/import-prompty.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
2 changes: 1 addition & 1 deletion packages/sample/genaisrc/import-template.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
File renamed without changes.

0 comments on commit e09e350

Please sign in to comment.