Add GitHub Copilot Chat Support (#14842)
# Summary This commit implements Github Copilot Chat support within the existing Assistant panel/framework. It required a little bit of trickery and internal API modification, as Copilot doesn't use the same authentication-style as all of the existing providers, opting to use OAuth and a short lived API key instead of a straight API key. All existing Assistant features should work. Release Notes: - Added Github Copilot Chat support ([#4673](https://github.com/zed-industries/zed/issues/4673)). ## Screenshots <img width="1552" alt="A screenshot showing a conversation between a user and Github Copilot Chat within the Zed editor." src="https://github.com/user-attachments/assets/73eaf6a2-792b-4c40-a7fe-f763bd6417d7"> --------- Co-authored-by: Bennet Bo Fenner <bennet@zed.dev>
This commit is contained in:
parent
d93891ba63
commit
6f0655810e
14 changed files with 808 additions and 14 deletions
|
@ -25,12 +25,14 @@ anthropic = { workspace = true, features = ["schemars"] }
|
|||
anyhow.workspace = true
|
||||
client.workspace = true
|
||||
collections.workspace = true
|
||||
copilot = { workspace = true, features = ["schemars"] }
|
||||
editor.workspace = true
|
||||
feature_flags.workspace = true
|
||||
futures.workspace = true
|
||||
google_ai = { workspace = true, features = ["schemars"] }
|
||||
gpui.workspace = true
|
||||
http_client.workspace = true
|
||||
inline_completion_button.workspace = true
|
||||
menu.workspace = true
|
||||
ollama = { workspace = true, features = ["schemars"] }
|
||||
open_ai = { workspace = true, features = ["schemars"] }
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
pub mod anthropic;
|
||||
pub mod cloud;
|
||||
pub mod copilot_chat;
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
pub mod fake;
|
||||
pub mod google;
|
||||
|
|
359
crates/language_model/src/provider/copilot_chat.rs
Normal file
359
crates/language_model/src/provider/copilot_chat.rs
Normal file
|
@ -0,0 +1,359 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use copilot::copilot_chat::{
|
||||
ChatMessage, CopilotChat, Model as CopilotChatModel, Request as CopilotChatRequest,
|
||||
Role as CopilotChatRole,
|
||||
};
|
||||
use copilot::{Copilot, Status};
|
||||
use futures::future::BoxFuture;
|
||||
use futures::stream::BoxStream;
|
||||
use futures::{FutureExt, StreamExt};
|
||||
use gpui::{
|
||||
percentage, svg, Animation, AnimationExt, AnyView, AppContext, AsyncAppContext, Model,
|
||||
ModelContext, Render, Subscription, Task, Transformation,
|
||||
};
|
||||
use settings::{Settings, SettingsStore};
|
||||
use std::time::Duration;
|
||||
use strum::IntoEnumIterator;
|
||||
use ui::{
|
||||
div, v_flex, Button, ButtonCommon, Clickable, Color, Context, FixedWidth, IconName,
|
||||
IconPosition, IconSize, IntoElement, Label, LabelCommon, ParentElement, Styled, ViewContext,
|
||||
VisualContext, WindowContext,
|
||||
};
|
||||
|
||||
use crate::settings::AllLanguageModelSettings;
|
||||
use crate::LanguageModelProviderState;
|
||||
use crate::{
|
||||
LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider,
|
||||
LanguageModelProviderId, LanguageModelProviderName, LanguageModelRequest, Role,
|
||||
};
|
||||
|
||||
use super::open_ai::count_open_ai_tokens;
|
||||
|
||||
const PROVIDER_ID: &str = "copilot_chat";
|
||||
const PROVIDER_NAME: &str = "GitHub Copilot Chat";
|
||||
|
||||
#[derive(Default, Clone, Debug, PartialEq)]
|
||||
pub struct CopilotChatSettings {
|
||||
pub low_speed_timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
pub struct CopilotChatLanguageModelProvider {
|
||||
state: Model<State>,
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
_copilot_chat_subscription: Option<Subscription>,
|
||||
_settings_subscription: Subscription,
|
||||
}
|
||||
|
||||
impl CopilotChatLanguageModelProvider {
|
||||
pub fn new(cx: &mut AppContext) -> Self {
|
||||
let state = cx.new_model(|cx| {
|
||||
let _copilot_chat_subscription = CopilotChat::global(cx)
|
||||
.map(|copilot_chat| cx.observe(&copilot_chat, |_, _, cx| cx.notify()));
|
||||
State {
|
||||
_copilot_chat_subscription,
|
||||
_settings_subscription: cx.observe_global::<SettingsStore>(|_, cx| {
|
||||
cx.notify();
|
||||
}),
|
||||
}
|
||||
});
|
||||
|
||||
Self { state }
|
||||
}
|
||||
}
|
||||
|
||||
impl LanguageModelProviderState for CopilotChatLanguageModelProvider {
|
||||
fn subscribe<T: 'static>(&self, cx: &mut ModelContext<T>) -> Option<Subscription> {
|
||||
Some(cx.observe(&self.state, |_, _, cx| {
|
||||
cx.notify();
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl LanguageModelProvider for CopilotChatLanguageModelProvider {
|
||||
fn id(&self) -> LanguageModelProviderId {
|
||||
LanguageModelProviderId(PROVIDER_ID.into())
|
||||
}
|
||||
|
||||
fn name(&self) -> LanguageModelProviderName {
|
||||
LanguageModelProviderName(PROVIDER_NAME.into())
|
||||
}
|
||||
|
||||
fn provided_models(&self, _cx: &AppContext) -> Vec<Arc<dyn LanguageModel>> {
|
||||
CopilotChatModel::iter()
|
||||
.map(|model| Arc::new(CopilotChatLanguageModel { model }) as Arc<dyn LanguageModel>)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn is_authenticated(&self, cx: &AppContext) -> bool {
|
||||
CopilotChat::global(cx)
|
||||
.map(|m| m.read(cx).is_authenticated())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn authenticate(&self, cx: &AppContext) -> Task<Result<()>> {
|
||||
let result = if self.is_authenticated(cx) {
|
||||
Ok(())
|
||||
} else if let Some(copilot) = Copilot::global(cx) {
|
||||
let error_msg = match copilot.read(cx).status() {
|
||||
Status::Disabled => anyhow::anyhow!("Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."),
|
||||
Status::Error(e) => anyhow::anyhow!(format!("Received the following error while signing into Copilot: {e}")),
|
||||
Status::Starting { task: _ } => anyhow::anyhow!("Copilot is still starting, please wait for Copilot to start then try again"),
|
||||
Status::Unauthorized => anyhow::anyhow!("Unable to authorize with Copilot. Please make sure that you have an active Copilot and Copilot Chat subscription."),
|
||||
Status::Authorized => return Task::ready(Ok(())),
|
||||
Status::SignedOut => anyhow::anyhow!("You have signed out of Copilot. Please sign in to Copilot and try again."),
|
||||
Status::SigningIn { prompt: _ } => anyhow::anyhow!("Still signing into Copilot..."),
|
||||
};
|
||||
Err(error_msg)
|
||||
} else {
|
||||
Err(anyhow::anyhow!(
|
||||
"Copilot must be enabled for Copilot Chat to work. Please enable Copilot and try again."
|
||||
))
|
||||
};
|
||||
Task::ready(result)
|
||||
}
|
||||
|
||||
fn authentication_prompt(&self, cx: &mut WindowContext) -> AnyView {
|
||||
cx.new_view(|cx| AuthenticationPrompt::new(cx)).into()
|
||||
}
|
||||
|
||||
fn reset_credentials(&self, cx: &AppContext) -> Task<Result<()>> {
|
||||
let Some(copilot) = Copilot::global(cx) else {
|
||||
return Task::ready(Err(anyhow::anyhow!(
|
||||
"Copilot is not available. Please ensure Copilot is enabled and running and try again."
|
||||
)));
|
||||
};
|
||||
|
||||
let state = self.state.clone();
|
||||
|
||||
cx.spawn(|mut cx| async move {
|
||||
cx.update_model(&copilot, |model, cx| model.sign_out(cx))?
|
||||
.await?;
|
||||
|
||||
cx.update_model(&state, |_, cx| {
|
||||
cx.notify();
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CopilotChatLanguageModel {
|
||||
model: CopilotChatModel,
|
||||
}
|
||||
|
||||
impl LanguageModel for CopilotChatLanguageModel {
|
||||
fn id(&self) -> LanguageModelId {
|
||||
LanguageModelId::from(self.model.id().to_string())
|
||||
}
|
||||
|
||||
fn name(&self) -> LanguageModelName {
|
||||
LanguageModelName::from(self.model.display_name().to_string())
|
||||
}
|
||||
|
||||
fn provider_id(&self) -> LanguageModelProviderId {
|
||||
LanguageModelProviderId(PROVIDER_ID.into())
|
||||
}
|
||||
|
||||
fn provider_name(&self) -> LanguageModelProviderName {
|
||||
LanguageModelProviderName(PROVIDER_NAME.into())
|
||||
}
|
||||
|
||||
fn telemetry_id(&self) -> String {
|
||||
format!("copilot_chat/{}", self.model.id())
|
||||
}
|
||||
|
||||
fn max_token_count(&self) -> usize {
|
||||
self.model.max_token_count()
|
||||
}
|
||||
|
||||
fn count_tokens(
|
||||
&self,
|
||||
request: LanguageModelRequest,
|
||||
cx: &AppContext,
|
||||
) -> BoxFuture<'static, Result<usize>> {
|
||||
let model = match self.model {
|
||||
CopilotChatModel::Gpt4 => open_ai::Model::Four,
|
||||
CopilotChatModel::Gpt3_5Turbo => open_ai::Model::ThreePointFiveTurbo,
|
||||
};
|
||||
|
||||
count_open_ai_tokens(request, model, cx)
|
||||
}
|
||||
|
||||
fn stream_completion(
|
||||
&self,
|
||||
request: LanguageModelRequest,
|
||||
cx: &AsyncAppContext,
|
||||
) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
|
||||
if let Some(message) = request.messages.last() {
|
||||
if message.content.trim().is_empty() {
|
||||
const EMPTY_PROMPT_MSG: &str =
|
||||
"Empty prompts aren't allowed. Please provide a non-empty prompt.";
|
||||
return futures::future::ready(Err(anyhow::anyhow!(EMPTY_PROMPT_MSG))).boxed();
|
||||
}
|
||||
|
||||
// Copilot Chat has a restriction that the final message must be from the user.
|
||||
// While their API does return an error message for this, we can catch it earlier
|
||||
// and provide a more helpful error message.
|
||||
if !matches!(message.role, Role::User) {
|
||||
const USER_ROLE_MSG: &str = "The final message must be from the user. To provide a system prompt, you must provide the system prompt followed by a user prompt.";
|
||||
return futures::future::ready(Err(anyhow::anyhow!(USER_ROLE_MSG))).boxed();
|
||||
}
|
||||
}
|
||||
|
||||
let request = self.to_copilot_chat_request(request);
|
||||
let Ok(low_speed_timeout) = cx.update(|cx| {
|
||||
AllLanguageModelSettings::get_global(cx)
|
||||
.copilot_chat
|
||||
.low_speed_timeout
|
||||
}) else {
|
||||
return futures::future::ready(Err(anyhow::anyhow!("App state dropped"))).boxed();
|
||||
};
|
||||
|
||||
cx.spawn(|mut cx| async move {
|
||||
let response = CopilotChat::stream_completion(request, low_speed_timeout, &mut cx).await?;
|
||||
let stream = response
|
||||
.filter_map(|response| async move {
|
||||
match response {
|
||||
Ok(result) => {
|
||||
let choice = result.choices.first();
|
||||
match choice {
|
||||
Some(choice) => Some(Ok(choice.delta.content.clone().unwrap_or_default())),
|
||||
None => Some(Err(anyhow::anyhow!(
|
||||
"The Copilot Chat API returned a response with no choices, but hadn't finished the message yet. Please try again."
|
||||
))),
|
||||
}
|
||||
}
|
||||
Err(err) => Some(Err(err)),
|
||||
}
|
||||
})
|
||||
.boxed();
|
||||
Ok(stream)
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
impl CopilotChatLanguageModel {
|
||||
pub fn to_copilot_chat_request(&self, request: LanguageModelRequest) -> CopilotChatRequest {
|
||||
CopilotChatRequest::new(
|
||||
self.model.clone(),
|
||||
request
|
||||
.messages
|
||||
.into_iter()
|
||||
.map(|msg| ChatMessage {
|
||||
role: match msg.role {
|
||||
Role::User => CopilotChatRole::User,
|
||||
Role::Assistant => CopilotChatRole::Assistant,
|
||||
Role::System => CopilotChatRole::System,
|
||||
},
|
||||
content: msg.content,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct AuthenticationPrompt {
|
||||
copilot_status: Option<copilot::Status>,
|
||||
_subscription: Option<Subscription>,
|
||||
}
|
||||
|
||||
impl AuthenticationPrompt {
|
||||
pub fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
let copilot = Copilot::global(cx);
|
||||
|
||||
Self {
|
||||
copilot_status: copilot.as_ref().map(|copilot| copilot.read(cx).status()),
|
||||
_subscription: copilot.as_ref().map(|copilot| {
|
||||
cx.observe(copilot, |this, model, cx| {
|
||||
this.copilot_status = Some(model.read(cx).status());
|
||||
cx.notify();
|
||||
})
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for AuthenticationPrompt {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let loading_icon = svg()
|
||||
.size_8()
|
||||
.path(IconName::ArrowCircle.path())
|
||||
.text_color(cx.text_style().color)
|
||||
.with_animation(
|
||||
"icon_circle_arrow",
|
||||
Animation::new(Duration::from_secs(2)).repeat(),
|
||||
|svg, delta| svg.with_transformation(Transformation::rotate(percentage(delta))),
|
||||
);
|
||||
|
||||
const ERROR_LABEL: &str = "Copilot Chat requires the Copilot plugin to be available and running. Please ensure Copilot is running and try again, or use a different Assistant provider.";
|
||||
match &self.copilot_status {
|
||||
Some(status) => match status {
|
||||
Status::Disabled => {
|
||||
return v_flex().gap_6().p_4().child(Label::new(ERROR_LABEL));
|
||||
}
|
||||
Status::Starting { task: _ } => {
|
||||
const LABEL: &str = "Starting Copilot...";
|
||||
return v_flex()
|
||||
.gap_6()
|
||||
.p_4()
|
||||
.justify_center()
|
||||
.items_center()
|
||||
.child(Label::new(LABEL))
|
||||
.child(loading_icon);
|
||||
}
|
||||
Status::SigningIn { prompt: _ } => {
|
||||
const LABEL: &str = "Signing in to Copilot...";
|
||||
return v_flex()
|
||||
.gap_6()
|
||||
.p_4()
|
||||
.justify_center()
|
||||
.items_center()
|
||||
.child(Label::new(LABEL))
|
||||
.child(loading_icon);
|
||||
}
|
||||
Status::Error(_) => {
|
||||
const LABEL: &str = "Copilot had issues starting. Please try restarting it. If the issue persists, try reinstalling Copilot.";
|
||||
return v_flex()
|
||||
.gap_6()
|
||||
.p_4()
|
||||
.child(Label::new(LABEL))
|
||||
.child(svg().size_8().path(IconName::CopilotError.path()));
|
||||
}
|
||||
_ => {
|
||||
const LABEL: &str =
|
||||
"To use the assistant panel or inline assistant, you must login to GitHub Copilot. Your GitHub account must have an active Copilot Chat subscription.";
|
||||
v_flex().gap_6().p_4().child(Label::new(LABEL)).child(
|
||||
v_flex()
|
||||
.gap_2()
|
||||
.child(
|
||||
Button::new("sign_in", "Sign In")
|
||||
.icon_color(Color::Muted)
|
||||
.icon(IconName::Github)
|
||||
.icon_position(IconPosition::Start)
|
||||
.icon_size(IconSize::Medium)
|
||||
.style(ui::ButtonStyle::Filled)
|
||||
.full_width()
|
||||
.on_click(|_, cx| {
|
||||
inline_completion_button::initiate_sign_in(cx)
|
||||
}),
|
||||
)
|
||||
.child(
|
||||
div().flex().w_full().items_center().child(
|
||||
Label::new("Sign in to start using Github Copilot Chat.")
|
||||
.color(Color::Muted)
|
||||
.size(ui::LabelSize::Small),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
None => v_flex().gap_6().p_4().child(Label::new(ERROR_LABEL)),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
use crate::{
|
||||
provider::{
|
||||
anthropic::AnthropicLanguageModelProvider, cloud::CloudLanguageModelProvider,
|
||||
google::GoogleLanguageModelProvider, ollama::OllamaLanguageModelProvider,
|
||||
open_ai::OpenAiLanguageModelProvider,
|
||||
copilot_chat::CopilotChatLanguageModelProvider, google::GoogleLanguageModelProvider,
|
||||
ollama::OllamaLanguageModelProvider, open_ai::OpenAiLanguageModelProvider,
|
||||
},
|
||||
LanguageModel, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderState,
|
||||
};
|
||||
|
@ -44,6 +44,7 @@ fn register_language_model_providers(
|
|||
GoogleLanguageModelProvider::new(client.http_client(), cx),
|
||||
cx,
|
||||
);
|
||||
registry.register_provider(CopilotChatLanguageModelProvider::new(cx), cx);
|
||||
|
||||
cx.observe_flag::<feature_flags::LanguageModels, _>(move |enabled, cx| {
|
||||
let client = client.clone();
|
||||
|
|
|
@ -9,6 +9,7 @@ use settings::{Settings, SettingsSources};
|
|||
use crate::provider::{
|
||||
anthropic::AnthropicSettings,
|
||||
cloud::{self, ZedDotDevSettings},
|
||||
copilot_chat::CopilotChatSettings,
|
||||
google::GoogleSettings,
|
||||
ollama::OllamaSettings,
|
||||
open_ai::OpenAiSettings,
|
||||
|
@ -26,6 +27,7 @@ pub struct AllLanguageModelSettings {
|
|||
pub openai: OpenAiSettings,
|
||||
pub zed_dot_dev: ZedDotDevSettings,
|
||||
pub google: GoogleSettings,
|
||||
pub copilot_chat: CopilotChatSettings,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
|
||||
|
@ -36,6 +38,7 @@ pub struct AllLanguageModelSettingsContent {
|
|||
#[serde(rename = "zed.dev")]
|
||||
pub zed_dot_dev: Option<ZedDotDevSettingsContent>,
|
||||
pub google: Option<GoogleSettingsContent>,
|
||||
pub copilot_chat: Option<CopilotChatSettingsContent>,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
|
||||
|
@ -70,6 +73,11 @@ pub struct ZedDotDevSettingsContent {
|
|||
available_models: Option<Vec<cloud::AvailableModel>>,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
|
||||
pub struct CopilotChatSettingsContent {
|
||||
low_speed_timeout_in_seconds: Option<u64>,
|
||||
}
|
||||
|
||||
impl settings::Settings for AllLanguageModelSettings {
|
||||
const KEY: Option<&'static str> = Some("language_models");
|
||||
|
||||
|
@ -165,6 +173,15 @@ impl settings::Settings for AllLanguageModelSettings {
|
|||
.as_ref()
|
||||
.and_then(|s| s.available_models.clone()),
|
||||
);
|
||||
|
||||
if let Some(low_speed_timeout) = value
|
||||
.copilot_chat
|
||||
.as_ref()
|
||||
.and_then(|s| s.low_speed_timeout_in_seconds)
|
||||
{
|
||||
settings.copilot_chat.low_speed_timeout =
|
||||
Some(Duration::from_secs(low_speed_timeout));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(settings)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue