-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(playground): Extract and display variables from all message temp…
…lates as "inputs" (#4994) * feat(playground): Extract and display variables from all message templates as "inputs" * docs(playground): Add comments * fix(playground): Switch default language back to Mustache * docs(playground): Add comments and improve typing readability for template utils * feat(playground): Support text completion prompt variable substitution
- Loading branch information
1 parent
0fa0c87
commit b8cd777
Showing
10 changed files
with
190 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Enum for the different template languages supported by the template editor | ||
* | ||
* - FString: `variables look like {variable}` | ||
* - Mustache: `variables look like {{variable}}` | ||
* | ||
* @example | ||
* ```tsx | ||
* <TemplateEditor language={TemplateLanguages.Mustache} /> | ||
* ``` | ||
*/ | ||
export const TemplateLanguages = { | ||
FString: "f-string", // {variable} | ||
Mustache: "mustache", // {{variable}} | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { assertUnreachable } from "@phoenix/typeUtils"; | ||
|
||
import { extractVariablesFromFString, formatFString } from "./language/fString"; | ||
import { | ||
extractVariablesFromMustacheLike, | ||
formatMustacheLike, | ||
} from "./language/mustacheLike"; | ||
import { TemplateLanguages } from "./constants"; | ||
import { TemplateLanguage } from "./types"; | ||
|
||
/** | ||
* A function that formats a template with the given variables | ||
*/ | ||
export type FormatFn = (arg: { | ||
text: string; | ||
variables: Record<string, string | number | boolean | undefined>; | ||
}) => string; | ||
|
||
/** | ||
* A function that extracts the variables from a template | ||
*/ | ||
export type ExtractVariablesFn = (template: string) => string[]; | ||
|
||
/** | ||
* Get an object of isomorphic functions for processing templates of the given language | ||
* | ||
* @param templateLanguage - The language of the template to process | ||
* | ||
* @returns An object containing the `format` and `extractVariables` functions. | ||
* These functions share the same signature despite the different underlying | ||
* templating languages. | ||
*/ | ||
export const getTemplateLanguageUtils = ( | ||
templateLanguage: TemplateLanguage | ||
): { | ||
format: FormatFn; | ||
extractVariables: ExtractVariablesFn; | ||
} => { | ||
switch (templateLanguage) { | ||
case TemplateLanguages.FString: | ||
return { | ||
format: formatFString, | ||
extractVariables: extractVariablesFromFString, | ||
}; | ||
case TemplateLanguages.Mustache: | ||
return { | ||
format: formatMustacheLike, | ||
extractVariables: extractVariablesFromMustacheLike, | ||
}; | ||
default: | ||
assertUnreachable(templateLanguage); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { TemplateLanguages } from "./constants"; | ||
|
||
export type TemplateLanguage = | ||
(typeof TemplateLanguages)[keyof typeof TemplateLanguages]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters