Skip to content

Commit

Permalink
feat(thread): add support for ThreadAssistantMessageReadingCode event
Browse files Browse the repository at this point in the history
This commit introduces a new event type `ThreadAssistantMessageReadingCode` to handle code reading states in the thread. It also updates the `use-thread-run` hook to ignore unknown event types instead of throwing an error.
  • Loading branch information
wsxiaoys committed Jan 11, 2025
1 parent b2a26af commit dbef37d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
8 changes: 8 additions & 0 deletions ee/tabby-schema/src/schema/thread/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ pub struct ThreadAssistantMessageCreated {
pub id: ID,
}

#[derive(GraphQLObject, Clone, Debug)]
pub struct ThreadAssistantMessageReadingCode {
pub snippet: bool,
pub file_list: bool,
// pub commit_history: bool
}

#[derive(GraphQLObject)]
pub struct ThreadAssistantMessageAttachmentsCode {
pub code_source_id: String,
Expand Down Expand Up @@ -282,6 +289,7 @@ pub enum ThreadRunItem {
ThreadRelevantQuestions(ThreadRelevantQuestions),
ThreadUserMessageCreated(ThreadUserMessageCreated),
ThreadAssistantMessageCreated(ThreadAssistantMessageCreated),
ThreadAssistantMessageReadingCode(ThreadAssistantMessageReadingCode),
ThreadAssistantMessageAttachmentsCode(ThreadAssistantMessageAttachmentsCode),
ThreadAssistantMessageAttachmentsDoc(ThreadAssistantMessageAttachmentsDoc),
ThreadAssistantMessageContentDelta(ThreadAssistantMessageContentDelta),
Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-ui/lib/hooks/use-thread-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ export function useThreadRun({
x.completed = true
break
default:
throw new Error('Unknown event ' + JSON.stringify(x))
// Ignore unknown event type.
break
}

return x
Expand Down
6 changes: 2 additions & 4 deletions ee/tabby-webserver/src/service/answer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ use async_openai_alt::{
};
use async_stream::stream;
use futures::stream::BoxStream;
use prompt_tools::{
pipeline_decide_need_codebase_context,
pipeline_related_questions,
};
use prompt_tools::{pipeline_decide_need_codebase_context, pipeline_related_questions};
use tabby_common::{
api::{
code::{
Expand Down Expand Up @@ -116,6 +113,7 @@ impl AnswerService {
if let Some(code_query) = options.code_query.as_ref() {
if let Some(repository) = self.find_repository(&context_info_helper, code_query, policy.clone()).await {
let need_codebase_context = pipeline_decide_need_codebase_context(self.chat.clone(), &query.content).await?;
yield Ok(ThreadRunItem::ThreadAssistantMessageReadingCode(need_codebase_context.clone()));
if need_codebase_context.file_list {
// List at most 300 files in the repository.
match self.repository.list_files(&policy, &repository.kind, &repository.id, None, Some(300)).await {
Expand Down
12 changes: 4 additions & 8 deletions ee/tabby-webserver/src/service/answer/prompt_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use async_openai_alt::types::{
CreateChatCompletionRequestArgs,
};
use tabby_inference::ChatCompletionStream;
use tabby_schema::thread::ThreadAssistantMessageReadingCode;
use tracing::debug;

async fn request_llm(chat: Arc<dyn ChatCompletionStream>, prompt: &str) -> Result<String> {
Expand Down Expand Up @@ -77,17 +78,11 @@ fn detect_content(content: &str, check: &str) -> bool {
content.to_lowercase().contains(check)
}

#[derive(Debug)]
pub struct CodebaseContext {
pub snippet: bool,
pub file_list: bool,
}

/// Decide whether the question requires knowledge from codebase content.
pub async fn pipeline_decide_need_codebase_context(
chat: Arc<dyn ChatCompletionStream>,
question: &str,
) -> Result<CodebaseContext> {
) -> Result<ThreadAssistantMessageReadingCode> {
let prompt = format!(
r#"You are a helpful assistant that helps the user to decide the types of context needed to answer the question. Currently, the following two kinds of context are supported:
SNIPPET: Snippets searched from codebase given the question.
Expand All @@ -99,14 +94,15 @@ Here's a few examples:
"How to implement an embedding api?" -> SNIPPET
"Which file contains http api definitions" -> SNIPPET,FILE_LIST
"How many python files is in the codebase?" -> FILE_LIST
"Which file contains main function?" -> SNIPPET
Here's the original question:
{question}
"#
);

let content = request_llm(chat, &prompt).await?;
let context = CodebaseContext {
let context = ThreadAssistantMessageReadingCode {
snippet: detect_content(&content, "snippet"),
file_list: detect_content(&content, "file_list"),
};
Expand Down

0 comments on commit dbef37d

Please sign in to comment.