Skip to content

Commit

Permalink
Introduce a new /workflow command (#15854)
Browse files Browse the repository at this point in the history
This subsumes the previous built-in prompt.

Release Notes:

- N/A
  • Loading branch information
as-cii authored Aug 6, 2024
1 parent 889a14a commit 411934b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 50 deletions.
1 change: 1 addition & 0 deletions assets/icons/route.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion crates/assistant/src/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use settings::{update_settings_file, Settings, SettingsStore};
use slash_command::{
active_command, default_command, diagnostics_command, docs_command, fetch_command,
file_command, now_command, project_command, prompt_command, search_command, symbols_command,
tabs_command, term_command,
tabs_command, term_command, workflow_command,
};
use std::sync::Arc;
pub(crate) use streaming_diff::*;
Expand Down Expand Up @@ -260,6 +260,7 @@ fn register_slash_commands(cx: &mut AppContext) {
slash_command_registry.register_command(now_command::NowSlashCommand, true);
slash_command_registry.register_command(diagnostics_command::DiagnosticsSlashCommand, true);
slash_command_registry.register_command(docs_command::DocsSlashCommand, true);
slash_command_registry.register_command(workflow_command::WorkflowSlashCommand, true);
slash_command_registry.register_command(fetch_command::FetchSlashCommand, false);
}

Expand Down
57 changes: 8 additions & 49 deletions crates/assistant/src/prompt_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,12 @@ impl PromptStore {
let mut txn = db_env.write_txn()?;
let metadata = db_env.create_database(&mut txn, Some("metadata.v2"))?;
let bodies = db_env.create_database(&mut txn, Some("bodies.v2"))?;

// Remove edit workflow prompt, as we decided to opt into it using
// a slash command instead.
metadata.delete(&mut txn, &PromptId::EditWorkflow).ok();
bodies.delete(&mut txn, &PromptId::EditWorkflow).ok();

txn.commit()?;

Self::upgrade_dbs(&db_env, metadata, bodies).log_err();
Expand All @@ -1209,17 +1215,13 @@ impl PromptStore {
let metadata_cache = MetadataCache::from_db(metadata, &txn)?;
txn.commit()?;

let store = PromptStore {
Ok(PromptStore {
executor,
env: db_env,
metadata_cache: RwLock::new(metadata_cache),
metadata,
bodies,
};

store.save_built_in_prompts().log_err();

Ok(store)
})
}
})
}
Expand Down Expand Up @@ -1425,49 +1427,6 @@ impl PromptStore {
})
}

fn save_built_in_prompts(&self) -> Result<()> {
self.save_built_in_prompt(
PromptId::EditWorkflow,
"Built-in: Editing Workflow",
"prompts/edit_workflow.md",
)?;
Ok(())
}

/// Write a built-in prompt to the database, preserving the value of the default field
/// if a prompt with this id already exists. This method blocks.
fn save_built_in_prompt(
&self,
id: PromptId,
title: impl Into<SharedString>,
body_path: &str,
) -> Result<()> {
let mut metadata_cache = self.metadata_cache.write();
let existing_metadata = metadata_cache.metadata_by_id.get(&id).cloned();

let prompt_metadata = PromptMetadata {
id,
title: Some(title.into()),
default: existing_metadata.map_or(true, |m| m.default),
saved_at: Utc::now(),
};

metadata_cache.insert(prompt_metadata.clone());

let db_connection = self.env.clone();
let bodies = self.bodies;
let metadata_db = self.metadata;

let mut txn = db_connection.write_txn()?;
metadata_db.put(&mut txn, &id, &prompt_metadata)?;

let body = String::from_utf8(Assets.load(body_path)?.unwrap().to_vec())?;
bodies.put(&mut txn, &id, &body)?;

txn.commit()?;
Ok(())
}

fn save_metadata(
&self,
id: PromptId,
Expand Down
1 change: 1 addition & 0 deletions crates/assistant/src/slash_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod search_command;
pub mod symbols_command;
pub mod tabs_command;
pub mod term_command;
pub mod workflow_command;

pub(crate) struct SlashCommandCompletionProvider {
cancel_flag: Mutex<Arc<AtomicBool>>,
Expand Down
71 changes: 71 additions & 0 deletions crates/assistant/src/slash_command/workflow_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

use anyhow::{Context as _, Result};
use assets::Assets;
use assistant_slash_command::{
ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,
};
use gpui::{AppContext, AssetSource, Task, WeakView};
use language::LspAdapterDelegate;
use text::LineEnding;
use ui::prelude::*;
use workspace::Workspace;

pub(crate) struct WorkflowSlashCommand;

impl SlashCommand for WorkflowSlashCommand {
fn name(&self) -> String {
"workflow".into()
}

fn description(&self) -> String {
"insert a prompt that opts into the edit workflow".into()
}

fn menu_text(&self) -> String {
"Insert Workflow Prompt".into()
}

fn requires_argument(&self) -> bool {
false
}

fn complete_argument(
self: Arc<Self>,
_query: String,
_cancel: Arc<AtomicBool>,
_workspace: Option<WeakView<Workspace>>,
_cx: &mut AppContext,
) -> Task<Result<Vec<ArgumentCompletion>>> {
Task::ready(Ok(Vec::new()))
}

fn run(
self: Arc<Self>,
_argument: Option<&str>,
_workspace: WeakView<Workspace>,
_delegate: Option<Arc<dyn LspAdapterDelegate>>,
_cx: &mut WindowContext,
) -> Task<Result<SlashCommandOutput>> {
let mut text = match Assets
.load("prompts/edit_workflow.md")
.and_then(|prompt| prompt.context("prompts/edit_workflow.md not found"))
{
Ok(prompt) => String::from_utf8_lossy(&prompt).into_owned(),
Err(error) => return Task::ready(Err(error)),
};
LineEnding::normalize(&mut text);
let range = 0..text.len();

Task::ready(Ok(SlashCommandOutput {
text,
sections: vec![SlashCommandOutputSection {
range,
icon: IconName::Route,
label: "Workflow".into(),
}],
run_commands_in_text: false,
}))
}
}
2 changes: 2 additions & 0 deletions crates/ui/src/components/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ pub enum IconName {
Rerun,
Return,
Reveal,
Route,
RotateCcw,
RotateCw,
Save,
Expand Down Expand Up @@ -385,6 +386,7 @@ impl IconName {
IconName::Reveal => "icons/reveal.svg",
IconName::RotateCcw => "icons/rotate_ccw.svg",
IconName::RotateCw => "icons/rotate_cw.svg",
IconName::Route => "icons/route.svg",
IconName::Save => "icons/save.svg",
IconName::Screen => "icons/desktop.svg",
IconName::SearchSelection => "icons/search_selection.svg",
Expand Down

0 comments on commit 411934b

Please sign in to comment.