Rework prompt frontmatter (#12262)

Moved some things around so prompts now always have front-matter to
return, either by creating a prompt with default front-matter, or
bailing earlier on importing the prompt to the library.

In the future we'll improve visibility of malformed prompts in the
`prompts` folder in the prompt manager UI.

Fixes:

- Prompts inserted with the `/prompt` command now only include their
body, not the entire file including metadata.
- Prompts with an invalid title will now show "Untitled prompt" instead
of an empty line.

Release Notes:

- N/A
This commit is contained in:
Nate Butler 2024-05-24 19:15:02 -04:00 committed by GitHub
parent 461e7d00a6
commit 800c1ba916
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 112 deletions

View file

@ -43,12 +43,7 @@ impl SlashCommand for PromptSlashCommand {
.prompts()
.into_iter()
.enumerate()
.filter_map(|(ix, prompt)| {
prompt
.1
.title()
.map(|title| StringMatchCandidate::new(ix, title.into()))
})
.map(|(ix, prompt)| StringMatchCandidate::new(ix, prompt.1.title().to_string()))
.collect::<Vec<_>>();
let matches = fuzzy::match_strings(
&candidates,
@ -86,11 +81,10 @@ impl SlashCommand for PromptSlashCommand {
let prompt = library
.prompts()
.into_iter()
.filter_map(|prompt| prompt.1.title().map(|title| (title, prompt)))
.find(|(t, _)| t == &title)
.find(|prompt| &prompt.1.title().to_string() == &title)
.with_context(|| format!("no prompt found with title {:?}", title))?
.1;
Ok(prompt.1.content().to_owned())
Ok(prompt.body())
});
SlashCommandInvocation {
output,