Skip to content

Commit

Permalink
attempt to set the api key for supermaven via the client
Browse files Browse the repository at this point in the history
  • Loading branch information
rgbkrk committed May 2, 2024
1 parent 1549050 commit 2c78ee2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ impl Render for InlineCompletionButton {
AccountStatus::Ready => SupermavenButtonStatus::Ready,
}
}
Supermaven::Error { error } => {
SupermavenButtonStatus::Errored(error.to_string())
}
};

let icon = status.to_icon();
Expand Down
1 change: 1 addition & 0 deletions crates/supermaven/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ doctest = false

[dependencies]
anyhow.workspace = true
client.workspace = true
collections.workspace = true
editor.workspace = true
gpui.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/supermaven/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum OutboundMessage {
SetApiKey { api_key: String },
StateUpdate(StateUpdateMessage),
UseFreeVersion,
}
Expand Down Expand Up @@ -138,6 +139,7 @@ pub enum SupermavenMessage {
ServiceTier {
service_tier: ServiceTier,
},

Set(SupermavenSetMessage),
#[serde(other)]
Unknown,
Expand Down
55 changes: 41 additions & 14 deletions crates/supermaven/src/supermaven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ mod supermaven_completion_provider;
pub use supermaven_completion_provider::*;

use anyhow::{Context as _, Result};
#[allow(unused_imports)]
use client::{proto, Client};
use collections::BTreeMap;
use futures::{channel::mpsc, io::BufReader, AsyncBufReadExt, StreamExt};
use futures::{channel::mpsc, io::BufReader, AsyncBufReadExt, SinkExt as _, StreamExt};
use gpui::{AppContext, AsyncAppContext, EntityId, Global, Model, ModelContext, Task, WeakModel};
use language::{language_settings::all_language_settings, Anchor, Buffer, ToOffset};
use messages::*;
Expand All @@ -18,9 +20,9 @@ use smol::{
};
use std::{ops::Range, path::PathBuf, process::Stdio, sync::Arc};
use ui::prelude::*;
use util::{http::HttpClient, ResultExt};
use util::ResultExt;

pub fn init(client: Arc<dyn HttpClient>, cx: &mut AppContext) {
pub fn init(client: Arc<Client>, cx: &mut AppContext) {
let supermaven = cx.new_model(|_| Supermaven::Starting);
Supermaven::set_global(supermaven.clone(), cx);

Expand All @@ -47,6 +49,7 @@ pub enum Supermaven {
Starting,
FailedDownload { error: anyhow::Error },
Spawned(SupermavenAgent),
Error { error: anyhow::Error },
}

#[derive(Clone)]
Expand All @@ -71,19 +74,21 @@ impl Supermaven {
cx.set_global(SupermavenGlobal(supermaven));
}

pub fn start(&mut self, client: Arc<dyn HttpClient>, cx: &mut ModelContext<Self>) {
pub fn start(&mut self, client: Arc<Client>, cx: &mut ModelContext<Self>) {
if let Self::Starting = self {
cx.spawn(|this, mut cx| async move {
let binary_path = supermaven_api::get_supermaven_agent_path(client).await?;
let binary_path =
supermaven_api::get_supermaven_agent_path(client.http_client()).await?;

this.update(&mut cx, |this, cx| {
if let Self::Starting = this {
*this = Self::Spawned(SupermavenAgent::new(binary_path, cx)?);
*this =
Self::Spawned(SupermavenAgent::new(binary_path, client.clone(), cx)?);
}
anyhow::Ok(())
})
})
.detach_and_log_err(cx);
.detach_and_log_err(cx)
}
}

Expand Down Expand Up @@ -171,10 +176,18 @@ pub struct SupermavenAgent {
_handle_incoming_messages: Task<Result<()>>,
pub account_status: AccountStatus,
service_tier: Option<ServiceTier>,
#[allow(dead_code)]
api_key: Option<String>,
#[allow(dead_code)]
client: Arc<Client>,
}

impl SupermavenAgent {
fn new(binary_path: PathBuf, cx: &mut ModelContext<Supermaven>) -> Result<Self> {
fn new(
binary_path: PathBuf,
client: Arc<Client>,
cx: &mut ModelContext<Supermaven>,
) -> Result<Self> {
let mut process = Command::new(&binary_path)
.arg("stdio")
.stdin(Stdio::piped())
Expand All @@ -194,9 +207,6 @@ impl SupermavenAgent {
.context("failed to get stdout for process")?;

let (outgoing_tx, outgoing_rx) = mpsc::unbounded();
outgoing_tx
.unbounded_send(OutboundMessage::UseFreeVersion)
.unwrap();

Ok(Self {
_process: process,
Expand All @@ -209,6 +219,8 @@ impl SupermavenAgent {
.spawn(|this, cx| Self::handle_incoming_messages(this, stdout, cx)),
account_status: AccountStatus::Unknown,
service_tier: None,
api_key: None,
client,
})
}

Expand Down Expand Up @@ -247,11 +259,26 @@ impl SupermavenAgent {
continue;
};

this.update(&mut cx, |this, _cx| {
this.update(&mut cx, |this, cx| {
if let Supermaven::Spawned(this) = this {
this.handle_message(message)
this.handle_message(message);

if let AccountStatus::NeedsActivation { .. } = &this.account_status {
let client = this.client.clone();
let mut outgoing_tx = this.outgoing_tx.clone();
return cx.spawn(|_, _| async move {
let api_key =
client.request(proto::GetSupermavenApiKey {}).await?.api_key;
outgoing_tx
.send(OutboundMessage::SetApiKey { api_key })
.await?;
anyhow::Ok(())
});
}
}
})?;
Task::ready(Ok(()))
})?
.await?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/zed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ fn init_ui(args: Args) {
node_runtime.clone(),
cx,
);
supermaven::init(client.http_client(), cx);
supermaven::init(client.clone(), cx);

assistant::init(client.clone(), cx);
assistant2::init(client.clone(), cx);
Expand Down

0 comments on commit 2c78ee2

Please sign in to comment.