-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(tui): ghost-text follow-up prompt suggestion #2781
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
punkcanyang
wants to merge
4
commits into
Hmbown:main
Choose a base branch
from
punkcanyang:feat/prompt-suggestion
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
4 commits
Select commit
Hold shift + click to select a range
02c3579
feat(tui): ghost-text prompt suggestion after each turn
punkcanyang eb1d08b
fix(tui): address review — turn token, try_lock, static client, lifec…
punkcanyang e687b07
fix(tui): make prompt suggestion configurable (opt-in, default off)
punkcanyang 80246f0
test(tui): add prompt suggestion config and widget tests
punkcanyang 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,125 @@ | ||
| //! Ghost-text follow-up prompt suggestion. | ||
| //! | ||
| //! After each completed turn, a lightweight API call generates ONE short | ||
| //! follow-up question the user might want to ask next. The suggestion is | ||
| //! rendered as dimmed ghost text in the composer when the input is empty. | ||
|
|
||
| use std::sync::OnceLock; | ||
|
|
||
| use reqwest::header::{AUTHORIZATION, CONTENT_TYPE}; | ||
| use serde_json::Value; | ||
| use tracing::debug; | ||
|
|
||
| /// Reusable static client — avoids creating a new connection pool per request. | ||
| fn suggestion_client() -> &'static reqwest::Client { | ||
| static CLIENT: OnceLock<reqwest::Client> = OnceLock::new(); | ||
| CLIENT.get_or_init(reqwest::Client::new) | ||
| } | ||
|
|
||
| /// Generate a follow-up prompt suggestion based on recent messages. | ||
| /// | ||
| /// Sends the conversation summary to the API with a system prompt that | ||
| /// asks for a single short follow-up question. Returns `None` on failure | ||
| /// or empty result — callers treat this as best-effort. | ||
| pub async fn generate_suggestion( | ||
| api_key: &str, | ||
| base_url: &str, | ||
| model: &str, | ||
| recent_messages: &str, | ||
| ) -> Option<String> { | ||
| let client = suggestion_client(); | ||
| let body = serde_json::json!({ | ||
| "model": model, | ||
| "messages": [ | ||
| { | ||
| "role": "system", | ||
| "content": "\ | ||
| You are a helpful assistant. Based on the recent conversation context, generate \ | ||
| ONE short follow-up question (under 60 characters) the user might want to ask \ | ||
| next. Reply with ONLY the question text, nothing else — no quotes, no explanations, \ | ||
| no prefixes." | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": format!( | ||
| "Recent conversation:\n{recent_messages}\n\n\ | ||
| Generate ONE short follow-up question the user might ask next:" | ||
| ) | ||
| } | ||
| ], | ||
| "max_tokens": 64, | ||
| "temperature": 0.3, | ||
| "stream": false | ||
| }); | ||
|
|
||
| let url = format!("{}/chat/completions", base_url.trim_end_matches('/')); | ||
| debug!(%url, %model, "generating prompt suggestion"); | ||
| let response = match client | ||
| .post(&url) | ||
| .header(AUTHORIZATION, format!("Bearer {api_key}")) | ||
| .header(CONTENT_TYPE, "application/json") | ||
| .timeout(std::time::Duration::from_secs(10)) | ||
| .json(&body) | ||
| .send() | ||
| .await | ||
| { | ||
| Ok(r) => r, | ||
| Err(_) => return None, | ||
| }; | ||
|
|
||
| let value: Value = match response.json().await { | ||
| Ok(v) => v, | ||
| Err(_) => return None, | ||
| }; | ||
|
|
||
| let suggestion = value["choices"][0]["message"]["content"] | ||
| .as_str() | ||
| .map(|s| s.trim().trim_matches('"').to_string()) | ||
| .filter(|s| !s.is_empty() && s.len() <= 200)?; | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| debug!(text = %suggestion, "prompt suggestion generated"); | ||
| Some(suggestion) | ||
| } | ||
|
|
||
| /// Extract the first text line from a single message. | ||
| fn message_summary(m: &crate::models::Message) -> Option<String> { | ||
| let role = match m.role.as_str() { | ||
| "user" => "User", | ||
| "assistant" => "Assistant", | ||
| _ => return None, | ||
| }; | ||
| let text = m | ||
| .content | ||
| .iter() | ||
| .filter_map(|block| match block { | ||
| crate::models::ContentBlock::Text { text, .. } => Some(text.as_str()), | ||
| _ => None, | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join(" "); | ||
| let first_line = text.lines().next().unwrap_or("").trim(); | ||
| if first_line.is_empty() { | ||
| return None; | ||
| } | ||
| let truncated: String = first_line | ||
| .chars() | ||
| .take(120) | ||
| .chain(if first_line.chars().count() > 120 { | ||
| Some('…') | ||
| } else { | ||
| None | ||
| }) | ||
| .collect(); | ||
| Some(format!("{role}: {truncated}")) | ||
| } | ||
|
|
||
| /// Build a one-line-per-message summary of recent conversation context. | ||
| /// Takes the last N messages, skipping tool-only messages. | ||
| pub fn summarize_recent_messages(messages: &[crate::models::Message], limit: usize) -> String { | ||
| let start = messages.len().saturating_sub(limit); | ||
| messages[start..] | ||
| .iter() | ||
| .filter_map(message_summary) | ||
| .collect::<Vec<_>>() | ||
| .join("\n") | ||
| } | ||
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
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.
The comment and PR description state that the suggestion is "cleared on new input" (typing dismisses it). However, the current implementation only hides the ghost text when
input_textis non-empty, but does not actually clearprompt_suggestion(set it toNone). If a user types a character and then deletes it, the same suggestion will reappear. To match the described behavior,app.prompt_suggestionshould be explicitly set toNonewhen new input is inserted (e.g., insideinsert_charandinsert_str).