Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions crates/agentdeck-client/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,23 @@ pub mod types {
value.parse()
}
}
///`POST /v1/workspaces/{id}/links`: the Repository to link.
///`POST /v1/workspaces/{id}/links`: the Repository to link, by name, or by GitHub's id with what the client holds of it.
#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug, PartialEq)]
pub struct NewLink {
///The Repository's `owner/name` (or its GitHub URL). One agentdeck doesn't know by that name is looked up on GitHub first.
///Whether it is archived on GitHub, with `id`; false when absent.
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
pub archived: ::std::option::Option<bool>,
///GitHub's id for it, as the client last read it (Undo after an unlink sends it). The Repository agentdeck keeps with this id is linked, whatever its name now; one it no longer keeps is stored again from `repository`, `url`, `private` and `archived`, without asking GitHub, so this works signed out.
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
pub id: ::std::option::Option<agentdeck_core::RepositoryId>,
///Whether it is private, with `id`; false when absent.
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
pub private: ::std::option::Option<bool>,
///The Repository's `owner/name` (or its GitHub URL). Without `id`, one agentdeck doesn't know by that name is looked up on GitHub first.
pub repository: agentdeck_core::RepositoryName,
///Its GitHub page, with `id`; `https://github.com/<repository>` when absent.
#[serde(skip_serializing_if = "::std::option::Option::is_none")]
pub url: ::std::option::Option<::std::string::String>,
}
impl NewLink {
pub fn builder() -> builder::NewLink {
Expand Down Expand Up @@ -1598,17 +1610,61 @@ pub mod types {
}
#[derive(Clone, Debug)]
pub struct NewLink {
archived: ::std::result::Result<::std::option::Option<bool>, ::std::string::String>,
id: ::std::result::Result<
::std::option::Option<agentdeck_core::RepositoryId>,
::std::string::String,
>,
private: ::std::result::Result<::std::option::Option<bool>, ::std::string::String>,
repository:
::std::result::Result<agentdeck_core::RepositoryName, ::std::string::String>,
url: ::std::result::Result<
::std::option::Option<::std::string::String>,
::std::string::String,
>,
}
impl ::std::default::Default for NewLink {
fn default() -> Self {
Self {
archived: Ok(Default::default()),
id: Ok(Default::default()),
private: Ok(Default::default()),
repository: Err("no value supplied for repository".to_string()),
url: Ok(Default::default()),
}
}
}
impl NewLink {
pub fn archived<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<::std::option::Option<bool>>,
T::Error: ::std::fmt::Display,
{
self.archived = value
.try_into()
.map_err(|e| format!("error converting supplied value for archived: {e}"));
self
}
pub fn id<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<::std::option::Option<agentdeck_core::RepositoryId>>,
T::Error: ::std::fmt::Display,
{
self.id = value
.try_into()
.map_err(|e| format!("error converting supplied value for id: {e}"));
self
}
pub fn private<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<::std::option::Option<bool>>,
T::Error: ::std::fmt::Display,
{
self.private = value
.try_into()
.map_err(|e| format!("error converting supplied value for private: {e}"));
self
}
pub fn repository<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<agentdeck_core::RepositoryName>,
Expand All @@ -1619,21 +1675,39 @@ pub mod types {
.map_err(|e| format!("error converting supplied value for repository: {e}"));
self
}
pub fn url<T>(mut self, value: T) -> Self
where
T: ::std::convert::TryInto<::std::option::Option<::std::string::String>>,
T::Error: ::std::fmt::Display,
{
self.url = value
.try_into()
.map_err(|e| format!("error converting supplied value for url: {e}"));
self
}
}
impl ::std::convert::TryFrom<NewLink> for super::NewLink {
type Error = super::error::ConversionError;
fn try_from(
value: NewLink,
) -> ::std::result::Result<Self, super::error::ConversionError> {
Ok(Self {
archived: value.archived?,
id: value.id?,
private: value.private?,
repository: value.repository?,
url: value.url?,
})
}
}
impl ::std::convert::From<super::NewLink> for NewLink {
fn from(value: super::NewLink) -> Self {
Self {
archived: Ok(value.archived),
id: Ok(value.id),
private: Ok(value.private),
repository: Ok(value.repository),
url: Ok(value.url),
}
}
}
Expand Down Expand Up @@ -4294,7 +4368,7 @@ impl Client {
}
///Links the Workspace to a Repository and answers the Repository with every Workspace that
///
///links it. A Repository agentdeck doesn't know by that name is looked up on GitHub first and stored, as `GET /v1/repositories/resolve` does; one GitHub doesn't show the sign-in is 400 `unknown_repository` and nothing is linked. Linking a pair already linked changes nothing. A Workspace that doesn't exist is 404 `not_found`. Sends `changed` for the Repository, then the Workspace, when the Link is new.
///links it. By name, a Repository agentdeck doesn't know by that name is looked up on GitHub first and stored, as `GET /v1/repositories/resolve` does; one GitHub doesn't show the sign-in is 400 `unknown_repository` and nothing is linked. With `id`, GitHub is never asked: the Repository kept with that id is linked, or, when none is, it is stored from what the client sent, so Undo after an unlink works signed out. Linking a pair already linked changes nothing. A Workspace that doesn't exist is 404 `not_found`. Sends `changed` for the Repository, then the Workspace, when the Link is new.
///
///Sends a `POST` request to `/v1/workspaces/{id}/links`
///
Expand Down
67 changes: 64 additions & 3 deletions crates/agentdeck-client/src/repositories.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Repositories as the GUI reads them (spec §4 `GET /v1/repositories`, §11 Notifications).
//! Repositories as the GUI reads them (spec §4 `GET /v1/repositories`, §11 Notifications),
//! and the Links a Repository group's `…` menu makes and takes away (spec §4
//! `/v1/workspaces/{id}/links`, §11 Pull requests view).

use agentdeck_core::RepositoryName;
use agentdeck_core::{RepositoryId, RepositoryName, WorkspaceId};

use crate::error::OLDER_DAEMON;
use crate::types::{Repository, RepositoryLinks};
use crate::types::{NewLink, Repository, RepositoryLinks};
use crate::{ApiError, Client};

/// Every Repository the daemon keeps, by name, each with the Workspaces linking it, archived
Expand Down Expand Up @@ -34,3 +36,62 @@ pub async fn resolve(client: &Client, name: &RepositoryName) -> Result<Repositor
.await?;
Ok(found.into_inner())
}

impl NewLink {
/// A link by `owner/name`: one agentdeck doesn't keep by that name is looked up on GitHub.
pub fn named(repository: RepositoryName) -> NewLink {
NewLink {
repository,
id: None,
url: None,
private: None,
archived: None,
}
}

/// A link by GitHub's id, carrying what the client holds of `repository`: the daemon links
/// the Repository it keeps with that id, or stores it again from these, never asking GitHub.
pub fn held(repository: &Repository) -> NewLink {
NewLink {
repository: repository.name.clone(),
id: Some(repository.id),
url: Some(repository.url.clone()),
private: Some(repository.private),
archived: Some(repository.archived),
}
}
}

/// Links `workspace` to `repository` by GitHub's id, as the client holds it (spec §4): the
/// Repository the daemon keeps with that id, or, when it no longer keeps one (an unlink took
/// it), one stored again from `repository`, without asking GitHub, so it works signed out.
/// Answers the Repository with every Workspace linking it now.
pub async fn link(
client: &Client,
workspace: WorkspaceId,
repository: &Repository,
) -> Result<RepositoryLinks, ApiError> {
let linked = client
.link_create()
.id(workspace)
.body(NewLink::held(repository))
.send()
.await?;
Ok(linked.into_inner())
}

/// Unlinks `workspace` from Repository `repository`. The daemon deletes the Repository when
/// nothing names it any more (spec §3); [`link`] brings it back.
pub async fn unlink(
client: &Client,
workspace: WorkspaceId,
repository: RepositoryId,
) -> Result<(), ApiError> {
client
.link_delete()
.id(workspace)
.repository(repository)
.send()
.await?;
Ok(())
}
2 changes: 1 addition & 1 deletion crates/agentdeck-core/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
/// fails when `openapi.json` changes without a bump.
///
/// `0` is every daemon from before it existed, whose `daemon.json` has no `api_version`.
pub const API_VERSION: u32 = 24;
pub const API_VERSION: u32 = 25;

/// What `$XDG_RUNTIME_DIR/agentdeck/daemon.json` holds. The daemon writes it once it serves;
/// the GUI and the CLI read it to reach the daemon.
Expand Down
117 changes: 116 additions & 1 deletion crates/agentdeck-gui/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use agentdeck_client::types::{
};
pub use agentdeck_client::workspaces::Change;
use agentdeck_client::{ApiError, Client};
use agentdeck_core::{PaneId, PrRef, RepositoryName, SplitDirection, TabId, WorkspaceId};
use agentdeck_core::{
PaneId, PrRef, RepositoryId, RepositoryName, SplitDirection, TabId, WorkspaceId,
};
use gpui::{App, Task};

use crate::new_agent::{Launch, Launched};
Expand Down Expand Up @@ -96,6 +98,26 @@ pub trait Api: Send + Sync {
cx: &App,
) -> Task<anyhow::Result<Repository>>;

/// Links `workspace` to `repository` by GitHub's id, as the view holds it (spec §4): the
/// Repository the daemon keeps with that id, or, when an unlink took it meanwhile, one
/// stored again from `repository` without asking GitHub, so Undo works signed out.
fn link_repository(
&self,
workspace: WorkspaceId,
repository: Repository,
cx: &App,
) -> Task<anyhow::Result<()>>;

/// Unlinks `workspace` from Repository `repository`; the daemon deletes the Repository once
/// nothing names it (spec §3). A refusal is an [`ApiError`]; `not_found` means the
/// Workspace doesn't link it.
fn unlink_repository(
&self,
workspace: WorkspaceId,
repository: RepositoryId,
cx: &App,
) -> Task<anyhow::Result<()>>;

/// Mutes `workspace`'s notifications, or unmutes them.
fn set_muted(&self, workspace: WorkspaceId, muted: bool, cx: &App) -> Task<anyhow::Result<()>>;

Expand Down Expand Up @@ -291,6 +313,32 @@ impl Api for Daemon {
})
}

fn link_repository(
&self,
workspace: WorkspaceId,
repository: Repository,
cx: &App,
) -> Task<anyhow::Result<()>> {
let client = self.client.clone();
runtime::spawn(cx, async move {
agentdeck_client::repositories::link(&client, workspace, &repository).await?;
Ok(())
})
}

fn unlink_repository(
&self,
workspace: WorkspaceId,
repository: RepositoryId,
cx: &App,
) -> Task<anyhow::Result<()>> {
let client = self.client.clone();
runtime::spawn(cx, async move {
agentdeck_client::repositories::unlink(&client, workspace, repository).await?;
Ok(())
})
}

fn set_muted(&self, workspace: WorkspaceId, muted: bool, cx: &App) -> Task<anyhow::Result<()>> {
let client = self.client.clone();
runtime::spawn(cx, async move {
Expand Down Expand Up @@ -496,6 +544,15 @@ pub(crate) mod fake {
}
}

/// The daemon's `not_found` refusal, saying `message`.
fn not_found(message: &str) -> anyhow::Error {
ApiError {
message: message.to_owned(),
code: Some("not_found".into()),
}
.into()
}

/// Answers every read with what it holds now, and counts the snapshot reads. Writes
/// change what it holds, as the daemon would, but broadcast nothing.
#[derive(Default)]
Expand Down Expand Up @@ -532,6 +589,10 @@ pub(crate) mod fake {
pub muted: Mutex<Vec<(WorkspaceId, bool)>>,
/// The edits asked for: the workspace and what to change, in order.
pub edits: Mutex<Vec<(WorkspaceId, Change)>>,
/// The links asked for by GitHub's id: the workspace and the Repository, in order.
pub links: Mutex<Vec<(WorkspaceId, Repository)>>,
/// The unlinks asked for: the workspace and the Repository, in order.
pub unlinks: Mutex<Vec<(WorkspaceId, agentdeck_core::RepositoryId)>>,
/// The Repositories GitHub shows, by `owner/name` in lower case: what a typed name
/// resolves to and what linking a Repository the snapshot doesn't hold finds.
github_repositories: Mutex<HashMap<String, Repository>>,
Expand Down Expand Up @@ -1084,6 +1145,60 @@ pub(crate) mod fake {
Task::ready(self.github(&name))
}

fn link_repository(
&self,
workspace: WorkspaceId,
repository: Repository,
_cx: &App,
) -> Task<anyhow::Result<()>> {
self.links
.lock()
.unwrap()
.push((workspace, repository.clone()));
if let Some(err) = self.failure() {
return Task::ready(Err(err));
}
let mut snapshot = self.snapshot.lock().unwrap();
if !snapshot.workspaces.iter().any(|w| w.id == workspace) {
return Task::ready(Err(not_found("workspace not found")));
}
if !(snapshot.repositories.iter()).any(|r| r.id == repository.id) {
snapshot.repositories.push(RepositoryLinks {
id: repository.id,
name: repository.name,
url: repository.url,
private: repository.private,
archived: repository.archived,
unreachable: false,
workspaces: Vec::new(),
});
(snapshot.repositories).sort_by_key(|r| r.name.to_string().to_lowercase());
}
let held = (snapshot.repositories.iter_mut()).find(|r| r.id == repository.id);
if let Some(held) = held.filter(|r| !r.workspaces.contains(&workspace)) {
held.workspaces.push(workspace);
}
Task::ready(Ok(()))
}

fn unlink_repository(
&self,
workspace: WorkspaceId,
repository: agentdeck_core::RepositoryId,
_cx: &App,
) -> Task<anyhow::Result<()>> {
self.unlinks.lock().unwrap().push((workspace, repository));
if let Some(err) = self.failure() {
return Task::ready(Err(err));
}
let linked = (self.snapshot.lock().unwrap().repositories.iter())
.any(|r| r.id == repository && r.workspaces.contains(&workspace));
if !linked {
return Task::ready(Err(not_found("link not found")));
}
Task::ready(self.relink(workspace, &[], &[repository]))
}

fn set_muted(
&self,
workspace: WorkspaceId,
Expand Down
Loading