diff --git a/README.md b/README.md index c1c08a99..2b18f570 100644 --- a/README.md +++ b/README.md @@ -135,25 +135,25 @@ Remove-Item -Recurse -Force (Join-Path $env:APPDATA "bt") -ErrorAction SilentlyC ## Commands -| Command | Description | -| ------------- | ------------------------------------------------------------------ | -| `bt init` | Initialize `.bt/` config directory and link to a project | -| `bt login` | Log in to Braintrust or refresh an OAuth login | -| `bt logout` | Remove a saved Braintrust login | -| `bt switch` | Switch org and project context | -| `bt status` | Show current org and project context | -| `bt datasets` | Manage datasets and dataset pipelines | -| `bt eval` | Run eval files (Unix only) | -| `bt sql` | Run SQL queries against Braintrust | -| `bt view` | View logs, traces, and spans | -| `bt projects` | Manage projects (list, create, view, delete) | -| `bt datasets` | Manage remote datasets (list, create, update, view, delete) | -| `bt prompts` | Manage prompts (list, view, delete) | -| `bt functions` | Manage functions (list, view, invoke, update, push, pull, delete) | +| Command | Description | +| -------------- | ------------------------------------------------------------------ | +| `bt init` | Initialize `.bt/` config directory and link to a project | +| `bt login` | Log in to Braintrust or refresh an OAuth login | +| `bt logout` | Remove a saved Braintrust login | +| `bt switch` | Switch org and project context | +| `bt status` | Show current org and project context | +| `bt datasets` | Manage datasets and dataset pipelines | +| `bt eval` | Run eval files (Unix only) | +| `bt sql` | Run SQL queries against Braintrust | +| `bt view` | View logs, traces, and spans | +| `bt projects` | Manage projects (list, create, view, delete) | +| `bt datasets` | Manage remote datasets (list, create, update, view, delete) | +| `bt prompts` | Manage prompts (list, view, update, delete) | +| `bt functions` | Manage functions (list, view, invoke, update, push, pull, delete) | | `bt tools` | Manage tools (list, view, invoke, update, delete) | -| `bt scorers` | Manage scorers (list, create, view, invoke, update, delete) | -| `bt sync` | Synchronize project logs between Braintrust and local NDJSON files | -| `bt update` | Update bt in-place | +| `bt scorers` | Manage scorers (list, create, view, invoke, update, delete) | +| `bt sync` | Synchronize project logs between Braintrust and local NDJSON files | +| `bt update` | Update bt in-place | ## `bt scorers` @@ -183,6 +183,7 @@ bt scorers update helpfulness --messages @messages.json bt scorers update helpfulness --model gpt-5.4-nano bt functions update my-function --description "Updated" bt tools update my-tool --patch @tool-patch.json +bt prompts update my-prompt --messages @messages.json ``` The API replaces `prompt_data` rather than merging it, so `bt` reads the current definition and sends a materialized replacement with your changes. A concurrent edit can therefore be overwritten. diff --git a/src/prompts/api.rs b/src/prompts/api.rs index 5a40a8e7..fe1e6507 100644 --- a/src/prompts/api.rs +++ b/src/prompts/api.rs @@ -1,5 +1,6 @@ use anyhow::Result; use serde::{Deserialize, Serialize}; +use serde_json::Value; use urlencoding::encode; use crate::http::ApiClient; @@ -51,3 +52,13 @@ pub async fn delete_prompt(client: &ApiClient, prompt_id: &str) -> Result<()> { let path = format!("/v1/prompt/{}", encode(prompt_id)); client.delete(&path).await } + +/// Partially update a prompt by id via `PATCH /v1/prompt/{id}`. +/// +/// Top-level fields are patched, but object-valued fields such as `prompt_data` +/// are replaced wholesale. Callers updating `prompt_data` must materialize the +/// complete value before sending the request. +pub async fn patch_prompt(client: &ApiClient, prompt_id: &str, body: &Value) -> Result { + let path = format!("/v1/prompt/{}", encode(prompt_id)); + client.patch(&path, body).await +} diff --git a/src/prompts/mod.rs b/src/prompts/mod.rs index 440ac341..bfe6bf41 100644 --- a/src/prompts/mod.rs +++ b/src/prompts/mod.rs @@ -8,6 +8,7 @@ pub(crate) use crate::project_context::ProjectContext as ResolvedContext; mod api; mod delete; mod list; +mod update; mod view; #[derive(Debug, Clone, Args)] @@ -16,6 +17,7 @@ Examples: bt prompts list bt prompts view my-prompt bt prompts delete my-prompt + bt prompts update my-prompt --messages @messages.json ")] pub struct PromptsArgs { #[command(subcommand)] @@ -28,6 +30,8 @@ enum PromptsCommands { List, /// View a prompt's content View(ViewArgs), + /// Update a prompt in place (prompt configuration, metadata, or arbitrary patch) + Update(Box), /// Delete a prompt Delete(DeleteArgs), } @@ -87,6 +91,7 @@ pub async fn run(base: BaseArgs, args: PromptsArgs) -> Result<()> { Some(PromptsCommands::View(p)) => { view::run(&ctx, p.slug(), base.json, p.web, base.verbose).await } + Some(PromptsCommands::Update(p)) => update::run(&ctx, &p, base.json).await, Some(PromptsCommands::Delete(p)) => delete::run(&ctx, p.slug(), p.force).await, } } @@ -100,8 +105,16 @@ fn prompts_command_is_read_only(command: Option<&PromptsCommands>) -> bool { #[cfg(test)] mod tests { + use clap::Parser; + use super::*; + #[derive(Debug, Parser)] + struct PromptsArgsHarness { + #[command(flatten)] + args: PromptsArgs, + } + #[test] fn prompts_routes_list_and_view_to_read_only_auth() { assert!(prompts_command_is_read_only(None)); @@ -125,4 +138,19 @@ mod tests { }) ))); } + + #[test] + fn prompts_routes_update_to_validated_auth() { + let parsed = PromptsArgsHarness::try_parse_from([ + "bt-prompts", + "update", + "my-prompt", + "--description", + "updated", + "--yes", + ]) + .expect("parse update"); + + assert!(!prompts_command_is_read_only(parsed.args.command.as_ref())); + } } diff --git a/src/prompts/update.rs b/src/prompts/update.rs new file mode 100644 index 00000000..01ea086e --- /dev/null +++ b/src/prompts/update.rs @@ -0,0 +1,426 @@ +use anyhow::{anyhow, bail, Context, Result}; +use clap::Args; +use dialoguer::Confirm; +use serde_json::{json, Map, Value}; + +use crate::{ + functions::{ + prompt_config::{validate_prompt_data_patch, PromptConfigArgs}, + prompt_patch::materialize_prompt_data_patch, + }, + ui::{is_interactive, print_command_status, with_spinner, CommandStatus}, + utils::{merge_json_objects, read_text_source, read_yaml_object_source}, +}; + +use super::{api, ResolvedContext}; + +/// Update a prompt's configuration or metadata in place. +/// +/// The endpoint replaces `prompt_data` wholesale, so the command reads the +/// current prompt and materializes a complete replacement while changing only +/// the fields requested by the user. +#[derive(Debug, Clone, Args)] +#[command(after_help = "\ +Examples: + bt prompts update my-prompt --messages @messages.json + bt prompts update my-prompt --model gpt-5.4-nano + bt prompts update my-prompt --description \"Customer support prompt\" + bt prompts update my-prompt --patch '{\"prompt_data\":{\"options\":{\"model\":\"gpt-5.4-nano\"}}}' + bt prompts update my-prompt --patch @prompt-patch.json +")] +pub struct UpdateArgs { + /// Prompt slug (positional) + #[arg(value_name = "SLUG", conflicts_with = "slug_flag")] + slug_positional: Option, + + /// Prompt slug (flag) + #[arg(long = "slug", short = 's')] + slug_flag: Option, + + /// Replacement chat messages source: inline JSON, @PATH to read from a + /// file, or - for stdin. + #[arg(long, value_name = "SOURCE")] + messages: Option, + + /// Update the model used by the prompt. + #[arg(long, short = 'm', value_name = "MODEL")] + model: Option, + + #[command(flatten)] + prompt_config: PromptConfigArgs, + + /// Deep-merge metadata from inline YAML, @PATH, or stdin (-). + #[arg(long, value_name = "SOURCE")] + metadata: Option, + + /// Update the prompt description. + #[arg(long, short = 'd', value_name = "TEXT")] + description: Option, + + /// Arbitrary JSON object deep-merged into the prompt. Accepts inline JSON, + /// @PATH to read JSON from a file, or - for stdin. + #[arg(long, value_name = "SOURCE")] + patch: Option, + + /// Skip the confirmation prompt. + #[arg(long, short = 'y')] + yes: bool, +} + +impl UpdateArgs { + fn slug(&self) -> Option<&str> { + self.slug_positional + .as_deref() + .or(self.slug_flag.as_deref()) + } +} + +pub async fn run(ctx: &ResolvedContext, args: &UpdateArgs, json_output: bool) -> Result<()> { + let project_name = &ctx.project.name; + let mut body = build_patch_body(args)?; + + let prompt = match args.slug() { + Some(slug) => with_spinner( + "Loading prompt...", + api::get_prompt_by_slug(&ctx.client, project_name, slug), + ) + .await? + .ok_or_else(|| anyhow!("prompt with slug '{slug}' not found"))?, + None => { + if !is_interactive() { + bail!("prompt slug required. Use: bt prompts update [--patch ...]"); + } + super::delete::select_prompt_interactive(&ctx.client, project_name).await? + } + }; + + with_spinner( + "Validating model parameters...", + validate_prompt_data_patch( + ctx, + prompt.prompt_data.as_ref(), + &mut body, + args.prompt_config.refresh_models(), + ), + ) + .await? + .warn_if_incomplete(); + materialize_prompt_data_patch(&mut body, prompt.prompt_data.as_ref()); + + if !args.yes && is_interactive() { + let confirm = Confirm::new() + .with_prompt(format!( + "Update prompt '{}' in {}?", + prompt.name, project_name + )) + .default(false) + .interact()?; + if !confirm { + return Ok(()); + } + } + + let updated = match with_spinner( + "Updating prompt...", + api::patch_prompt(&ctx.client, &prompt.id, &body), + ) + .await + { + Ok(value) => { + print_command_status( + CommandStatus::Success, + &format!("Updated '{}'", prompt.name), + ); + value + } + Err(error) => { + print_command_status( + CommandStatus::Error, + &format!("Failed to update '{}'", prompt.name), + ); + return Err(error); + } + }; + + if json_output { + println!("{}", serde_json::to_string(&updated)?); + } else if !crate::ui::is_quiet() { + eprintln!( + "Run `bt prompts view {}` to inspect the updated prompt.", + prompt.slug + ); + } + + Ok(()) +} + +fn build_patch_body(args: &UpdateArgs) -> Result { + let mut patch: Map = Map::new(); + + if let Some(description) = args.description.as_deref() { + patch.insert( + "description".to_string(), + Value::String(description.to_string()), + ); + } + + if let Some(source) = args.metadata.as_deref() { + patch.insert( + "metadata".to_string(), + Value::Object(read_yaml_object_source(source, "prompt metadata")?), + ); + } + + if let Some(messages) = resolve_messages(args)? { + let prompt_data_patch = json!({ + "prompt_data": { + "prompt": { "type": "chat", "messages": messages }, + }, + }); + merge_json_objects( + &mut patch, + prompt_data_patch + .as_object() + .expect("prompt data patch is an object"), + ); + } + + let prompt_config = args + .prompt_config + .build_prompt_data_patch(args.model.as_deref())?; + if !prompt_config.is_empty() { + let prompt_data_patch = json!({ "prompt_data": prompt_config }); + merge_json_objects( + &mut patch, + prompt_data_patch + .as_object() + .expect("prompt data patch is an object"), + ); + } + + let extra = resolve_extra_patch(args)?; + if let Some(extra_obj) = extra { + merge_json_objects(&mut patch, &extra_obj); + } + + if patch.is_empty() { + bail!("no updates requested. Pass an update flag; see `bt prompts update --help`"); + } + + Ok(Value::Object(patch)) +} + +fn resolve_messages(args: &UpdateArgs) -> Result> { + match args.messages.as_deref() { + Some(source) => { + let raw = read_text_source(source, "messages")?; + let parsed: Value = serde_json::from_str(&raw).context("invalid JSON in --messages")?; + match parsed { + Value::Array(_) => Ok(Some(parsed)), + _ => bail!("--messages must be a JSON array of chat messages"), + } + } + None => Ok(None), + } +} + +fn resolve_extra_patch(args: &UpdateArgs) -> Result>> { + let Some(source) = args.patch.as_deref() else { + return Ok(None); + }; + let raw = read_text_source(source, "patch")?; + parse_patch_object(&raw).map(Some) +} + +fn parse_patch_object(raw: &str) -> Result> { + let value: Value = serde_json::from_str(raw).context("invalid JSON in --patch")?; + match value { + Value::Object(map) => Ok(map), + _ => bail!("--patch must be a JSON object"), + } +} + +#[cfg(test)] +mod tests { + use clap::Parser; + + use super::*; + + #[derive(Debug, Parser)] + struct UpdateArgsHarness { + #[command(flatten)] + args: UpdateArgs, + } + + fn args(model: Option<&str>, description: Option<&str>) -> UpdateArgs { + UpdateArgs { + slug_positional: Some("test-prompt".to_string()), + slug_flag: None, + messages: None, + model: model.map(ToOwned::to_owned), + prompt_config: PromptConfigArgs::default(), + metadata: None, + description: description.map(ToOwned::to_owned), + patch: None, + yes: true, + } + } + + #[test] + fn build_patch_body_messages_writes_chat_block() { + let mut args = args(None, None); + args.messages = Some(r#"[{"role":"user","content":"hi"}]"#.to_string()); + let body = build_patch_body(&args).expect("patch body"); + assert_eq!( + body["prompt_data"]["prompt"]["type"], + serde_json::json!("chat") + ); + assert_eq!( + body["prompt_data"]["prompt"]["messages"], + serde_json::json!([{"role":"user","content":"hi"}]) + ); + } + + #[test] + fn build_patch_body_model_merges_into_prompt_data() { + let args = args(Some("gpt-4o-mini"), None); + let body = build_patch_body(&args).expect("patch body"); + assert_eq!( + body["prompt_data"]["options"]["model"], + serde_json::json!("gpt-4o-mini") + ); + } + + #[test] + fn build_patch_body_messages_and_model_combine() { + let mut args = args(Some("gpt-4o-mini"), None); + args.messages = Some(r#"[{"role":"user","content":"Answer it."}]"#.to_string()); + let body = build_patch_body(&args).expect("patch body"); + assert_eq!( + body["prompt_data"]["prompt"]["messages"], + json!([{"role": "user", "content": "Answer it."}]) + ); + assert_eq!( + body["prompt_data"]["options"]["model"], + serde_json::json!("gpt-4o-mini") + ); + } + + #[test] + fn build_patch_body_updates_prompt_configuration_and_metadata() { + let parsed = UpdateArgsHarness::try_parse_from([ + "test", + "test-prompt", + "--temperature", + "0.3", + "--max-tokens", + "100", + "--template-format", + "mustache", + "--metadata", + "owner: test-team", + ]) + .expect("parse update"); + + let body = build_patch_body(&parsed.args).expect("patch body"); + assert_eq!(body["prompt_data"]["options"]["params"]["temperature"], 0.3); + assert_eq!(body["prompt_data"]["options"]["params"]["max_tokens"], 100); + assert_eq!(body["prompt_data"]["template_format"], "mustache"); + assert_eq!(body["metadata"]["owner"], "test-team"); + } + + #[test] + fn build_patch_body_description_is_top_level() { + let args = args(None, Some("Customer support prompt")); + let body = build_patch_body(&args).expect("patch body"); + assert_eq!( + body["description"], + serde_json::json!("Customer support prompt") + ); + } + + #[test] + fn build_patch_body_reads_at_prefixed_messages_file() { + let dir = tempfile::tempdir().expect("tempdir"); + let path = dir.path().join("messages.json"); + std::fs::write( + &path, + r#"[{"role":"user","content":"Answer from a file."}]"#, + ) + .expect("write messages"); + let source = format!("@{}", path.display()); + + let mut args = args(None, None); + args.messages = Some(source); + let body = build_patch_body(&args).expect("patch body"); + assert_eq!( + body["prompt_data"]["prompt"]["messages"], + json!([{"role": "user", "content": "Answer from a file."}]) + ); + } + + #[test] + fn build_patch_body_reads_at_prefixed_patch_file() { + let dir = tempfile::tempdir().expect("tempdir"); + let path = dir.path().join("patch.json"); + std::fs::write(&path, r#"{"description":"From a file"}"#).expect("write patch"); + let source = format!("@{}", path.display()); + + let mut args = args(None, None); + args.patch = Some(source); + let body = build_patch_body(&args).expect("patch body"); + assert_eq!(body["description"], "From a file"); + } + + #[test] + fn build_patch_body_rejects_empty_update() { + let args = args(None, None); + let err = build_patch_body(&args).expect_err("should reject empty"); + assert!(err.to_string().contains("no updates requested")); + } + + #[test] + fn build_patch_body_extra_patch_merges_into_prompt_data() { + let mut args = args(None, None); + args.patch = + Some(r#"{"prompt_data":{"options":{"params":{"temperature":0}}}}"#.to_string()); + let body = build_patch_body(&args).expect("patch body"); + assert_eq!( + body["prompt_data"]["options"]["params"]["temperature"], + serde_json::json!(0) + ); + } + + #[test] + fn parse_patch_object_rejects_non_object() { + let err = parse_patch_object("[1,2,3]").expect_err("should reject"); + assert!(err.to_string().contains("JSON object")); + } + + #[test] + fn merge_objects_deep_merges_nested_maps() { + let mut target = serde_json::json!({ + "prompt_data": { "options": { "model": "gpt-4o" } } + }) + .as_object() + .expect("object") + .clone(); + let source = serde_json::json!({ + "prompt_data": { "options": { "temperature": 0 } } + }) + .as_object() + .expect("object") + .clone(); + + merge_json_objects(&mut target, &source); + + assert_eq!( + target["prompt_data"]["options"]["model"], + serde_json::json!("gpt-4o") + ); + assert_eq!( + target["prompt_data"]["options"]["temperature"], + serde_json::json!(0) + ); + } +} diff --git a/tests/cli.rs b/tests/cli.rs index a17c3bdb..734ac3c2 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -985,6 +985,20 @@ fn scorer_update_help_is_conflict_free() { .stdout(predicate::str::contains("--metadata")); } +#[test] +fn prompt_update_help_is_conflict_free() { + bt_command() + .args(["prompts", "update", "--help"]) + .assert() + .success() + .stdout(predicate::str::contains("--patch ")) + .stdout(predicate::str::contains("--patch-file").not()) + .stdout(predicate::str::contains("--messages ")) + .stdout(predicate::str::contains("--temperature")) + .stdout(predicate::str::contains("--template-format")) + .stdout(predicate::str::contains("--metadata")); +} + #[test] fn topics_report_help_accepts_global_org_short_conflict_free() { bt_command()