-
Notifications
You must be signed in to change notification settings - Fork 1
Add prompts command with list, view, and delete subcommands #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
parkerhendo
wants to merge
8
commits into
main
Choose a base branch
from
parker/prompts-cmd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
58dcc62
feat(prompts): Add skeleton for prompts command with list, view, and …
parkerhendo 2601317
feat(prompts): implement list command
parkerhendo 19a89c3
refactor(prompts): replace manual table formatting with comfy_table
parkerhendo e500261
feat(prompts): implement delete and view commands with table improvem…
parkerhendo d69c84c
fix(ui): handle multi-byte characters in text truncation
parkerhendo c7a7e2b
refactor(prompts): use API query parameters and add pager support
parkerhendo 4edb9ec
feat(args): make base arguments globally available in CLI
parkerhendo 2b5741f
refactor(prompts): add project validation before command execution
parkerhendo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
| use anyhow::Result; | ||
| use serde::{Deserialize, Serialize}; | ||
| use urlencoding::encode; | ||
|
|
||
| use crate::http::ApiClient; | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct Prompt { | ||
| pub id: String, | ||
| pub name: String, | ||
| pub slug: String, | ||
| pub project_id: String, | ||
| #[serde(default)] | ||
| pub description: Option<String>, | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| struct ListResponse { | ||
| objects: Vec<Prompt>, | ||
| } | ||
|
|
||
| pub async fn list_prompts(client: &ApiClient, project: &str) -> Result<Vec<Prompt>> { | ||
| let path = format!( | ||
| "/v1/prompt?org_name={}&project_name={}", | ||
| encode(client.org_name()), | ||
| encode(project) | ||
| ); | ||
| let list: ListResponse = client.get(&path).await?; | ||
|
|
||
| Ok(list.objects) | ||
| } | ||
|
|
||
| pub async fn get_prompt_by_name(client: &ApiClient, project: &str, name: &str) -> Result<Prompt> { | ||
| let path = format!( | ||
| "/v1/prompt?org_name={}&project_name={}&prompt_name={}", | ||
| encode(client.org_name()), | ||
| encode(project), | ||
| encode(name) | ||
| ); | ||
| let list: ListResponse = client.get(&path).await?; | ||
| list.objects | ||
| .into_iter() | ||
| .next() | ||
| .ok_or_else(|| anyhow::anyhow!("prompt '{name}' not found")) | ||
| } | ||
|
|
||
| pub async fn delete_prompt(client: &ApiClient, prompt_id: &str) -> Result<()> { | ||
| let path = format!("/v1/prompt/{}", encode(prompt_id)); | ||
| client.delete(&path).await | ||
| } | ||
This file contains hidden or 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,67 @@ | ||
| use std::io::IsTerminal; | ||
|
|
||
| use anyhow::{bail, Result}; | ||
| use dialoguer::Confirm; | ||
|
|
||
| use crate::{ | ||
| http::ApiClient, | ||
| prompts::api::{self, Prompt}, | ||
| ui::{self, print_command_status, with_spinner, CommandStatus}, | ||
| }; | ||
|
|
||
| pub async fn run(client: &ApiClient, project: &str, name: Option<&str>) -> Result<()> { | ||
| let prompt = match name { | ||
| Some(n) => api::get_prompt_by_name(client, project, n).await?, | ||
| None => { | ||
| if !std::io::stdin().is_terminal() { | ||
| bail!("prompt name required. Use: bt prompts delete <name>"); | ||
| } | ||
| select_prompt_interactive(client, project).await? | ||
| } | ||
| }; | ||
|
|
||
| if std::io::stdin().is_terminal() { | ||
| let confirm = Confirm::new() | ||
| .with_prompt(format!( | ||
| "Delete prompt '{}' from {}?", | ||
| &prompt.name, project | ||
| )) | ||
| .default(false) | ||
| .interact()?; | ||
|
|
||
| if !confirm { | ||
| return Ok(()); | ||
| } | ||
| } | ||
|
|
||
| match with_spinner("Deleting prompt...", api::delete_prompt(client, &prompt.id)).await { | ||
| Ok(_) => { | ||
| print_command_status( | ||
| CommandStatus::Success, | ||
| &format!("Deleted '{}'", prompt.name), | ||
| ); | ||
| Ok(()) | ||
| } | ||
| Err(e) => { | ||
| print_command_status( | ||
| CommandStatus::Error, | ||
| &format!("Failed to delete '{}'", prompt.name), | ||
| ); | ||
| Err(e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub async fn select_prompt_interactive(client: &ApiClient, project: &str) -> Result<Prompt> { | ||
| let mut prompts = | ||
| with_spinner("Loading prompts...", api::list_prompts(client, project)).await?; | ||
| if prompts.is_empty() { | ||
| bail!("no prompts found"); | ||
| } | ||
|
|
||
| prompts.sort_by(|a, b| a.name.cmp(&b.name)); | ||
| let names: Vec<&str> = prompts.iter().map(|p| p.name.as_str()).collect(); | ||
|
|
||
| let selection = ui::fuzzy_select("Select prompt", &names)?; | ||
| Ok(prompts[selection].clone()) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not for this PR but it would be nice if we could derive these from our openapi spec