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
363 changes: 360 additions & 3 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ whichlang = "0.1"
swift-rs = { git = "https://github.com/yujonglee/swift-rs", rev = "41a1605" }

rmcp = "0.14"
sqlx = { version = "0.8", default-features = false }
sysinfo = "0.38"

[patch.crates-io]
Expand Down
2 changes: 2 additions & 0 deletions apps/ai/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Env {
pub stripe: hypr_api_subscription::StripeEnv,
#[serde(flatten)]
pub github_app: hypr_api_support::GitHubAppEnv,
#[serde(flatten)]
pub supabase_db: hypr_api_support::SupabaseDbEnv,

#[serde(flatten)]
pub llm: hypr_llm_proxy::Env,
Expand Down
9 changes: 5 additions & 4 deletions apps/ai/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use env::env;

pub const DEVICE_FINGERPRINT_HEADER: &str = "x-device-fingerprint";

fn app() -> Router {
async fn app() -> Router {
let env = env();

let analytics = {
Expand All @@ -45,7 +45,8 @@ fn app() -> Router {
let nango_config = hypr_api_nango::NangoConfig::new(&env.nango);
let subscription_config =
hypr_api_subscription::SubscriptionConfig::new(&env.supabase, &env.stripe);
let support_config = hypr_api_support::SupportConfig::new(&env.github_app, &env.llm);
let support_config =
hypr_api_support::SupportConfig::new(&env.github_app, &env.llm, &env.supabase_db);

let webhook_routes = Router::new().nest(
"/nango",
Expand Down Expand Up @@ -80,7 +81,7 @@ fn app() -> Router {
.route("/health", axum::routing::get(|| async { "ok" }))
.route("/v", axum::routing::get(version))
.route("/openapi.json", axum::routing::get(openapi_json))
.nest("/support", hypr_api_support::router(support_config))
.nest("/support", hypr_api_support::router(support_config).await)
.merge(webhook_routes)
.merge(pro_routes)
.merge(auth_routes)
Expand Down Expand Up @@ -224,7 +225,7 @@ fn main() -> std::io::Result<()> {
tracing::info!(addr = %addr, "server_listening");

let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app())
axum::serve(listener, app().await)
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/api-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tracing = { workspace = true }

jsonwebtoken = { workspace = true }
octocrab = "0.49"
sqlx = { workspace = true, features = ["runtime-tokio", "tls-rustls", "postgres", "json"] }

askama = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
10 changes: 8 additions & 2 deletions crates/api-support/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use crate::env::{GitHubAppEnv, OpenRouterEnv};
use crate::env::{GitHubAppEnv, OpenRouterEnv, SupabaseDbEnv};

#[derive(Clone)]
pub struct SupportConfig {
pub github: GitHubAppEnv,
pub openrouter: OpenRouterEnv,
pub supabase_db: SupabaseDbEnv,
}

impl SupportConfig {
pub fn new(github: &GitHubAppEnv, openrouter: &OpenRouterEnv) -> Self {
pub fn new(
github: &GitHubAppEnv,
openrouter: &OpenRouterEnv,
supabase_db: &SupabaseDbEnv,
) -> Self {
Self {
github: github.clone(),
openrouter: openrouter.clone(),
supabase_db: supabase_db.clone(),
}
}
}
5 changes: 5 additions & 0 deletions crates/api-support/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ pub struct GitHubAppEnv {
pub github_discussion_category_id: String,
}

#[derive(Clone, Deserialize)]
pub struct SupabaseDbEnv {
pub supabase_db_url: String,
}

pub use hypr_api_env::OpenRouterEnv;
2 changes: 1 addition & 1 deletion crates/api-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ mod routes;
mod state;

pub use config::SupportConfig;
pub use env::{GitHubAppEnv, OpenRouterEnv};
pub use env::{GitHubAppEnv, OpenRouterEnv, SupabaseDbEnv};
pub use openapi::openapi;
pub use routes::router;
12 changes: 11 additions & 1 deletion crates/api-support/src/mcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rmcp::{

use crate::state::AppState;

use super::tools::{self, SubmitBugReportParams, SubmitFeatureRequestParams};
use super::tools::{self, ReadGitHubDataParams, SubmitBugReportParams, SubmitFeatureRequestParams};

#[derive(Clone)]
pub(crate) struct SupportMcpServer {
Expand Down Expand Up @@ -41,6 +41,16 @@ impl SupportMcpServer {
) -> Result<CallToolResult, McpError> {
tools::submit_feature_request(&self.state, params).await
}

#[tool(
description = "Read GitHub data (issues, pull requests, comments, tags) from the database. Data is synced from GitHub via Airbyte."
)]
async fn read_github_data(
&self,
Parameters(params): Parameters<ReadGitHubDataParams>,
) -> Result<CallToolResult, McpError> {
tools::read_github_data(&self.state, params).await
}
}

#[tool_handler]
Expand Down
2 changes: 2 additions & 0 deletions crates/api-support/src/mcp/tools/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod bug_report;
mod feature_request;
mod read_github_data;

pub(crate) use bug_report::{SubmitBugReportParams, submit_bug_report};
pub(crate) use feature_request::{SubmitFeatureRequestParams, submit_feature_request};
pub(crate) use read_github_data::{ReadGitHubDataParams, read_github_data};
123 changes: 123 additions & 0 deletions crates/api-support/src/mcp/tools/read_github_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use rmcp::{
ErrorData as McpError,
model::*,
schemars::{self, JsonSchema},
};
use serde::Deserialize;

use crate::state::AppState;

#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub(crate) enum GitHubTable {
#[schemars(description = "GitHub issues")]
Issues,
#[schemars(description = "GitHub pull requests")]
PullRequests,
#[schemars(description = "GitHub comments")]
Comments,
#[schemars(description = "GitHub tags")]
Tags,
}

impl GitHubTable {
fn as_str(&self) -> &'static str {
match self {
Self::Issues => "issues",
Self::PullRequests => "pull_requests",
Self::Comments => "comments",
Self::Tags => "tags",
}
}
}

#[derive(Debug, Deserialize, JsonSchema)]
pub(crate) struct ReadGitHubDataParams {
#[schemars(description = "The table to read from")]
pub table: GitHubTable,
#[schemars(description = "Maximum number of rows to return (default: 50, max: 500)")]
pub limit: Option<i64>,
#[schemars(description = "Number of rows to skip (default: 0)")]
pub offset: Option<i64>,
#[schemars(
description = "Filter by state (e.g. 'open', 'closed'). Applicable to issues and pull_requests."
)]
pub state: Option<String>,
}

pub(crate) async fn read_github_data(
state: &AppState,
params: ReadGitHubDataParams,
) -> Result<CallToolResult, McpError> {
let table_name = params.table.as_str();
let limit = params.limit.unwrap_or(50).max(0).min(500);
let offset = params.offset.unwrap_or(0).max(0);

if params.state.is_some() {
match params.table {
GitHubTable::Comments | GitHubTable::Tags => {
return Err(McpError::invalid_params(
"The 'state' filter is only applicable to 'issues' and 'pull_requests' tables",
None,
));
}
_ => {}
}
}

let query = if let Some(ref state_filter) = params.state {
let q = format!(
"SELECT to_jsonb(t.*) AS row_data FROM hyprnote_github.{} t WHERE t.state = $1 ORDER BY t._airbyte_extracted_at DESC LIMIT $2 OFFSET $3",
table_name
);
sqlx::query_scalar::<_, serde_json::Value>(&q)
.bind(state_filter)
.bind(limit)
.bind(offset)
.fetch_all(&state.db_pool)
.await
} else {
let q = format!(
"SELECT to_jsonb(t.*) AS row_data FROM hyprnote_github.{} t ORDER BY t._airbyte_extracted_at DESC LIMIT $1 OFFSET $2",
table_name
);
sqlx::query_scalar::<_, serde_json::Value>(&q)
.bind(limit)
.bind(offset)
.fetch_all(&state.db_pool)
.await
};

let rows = query.map_err(|e| McpError::internal_error(e.to_string(), None))?;

let total_count: i64 = if let Some(ref state_filter) = params.state {
let count_query = format!(
"SELECT COUNT(*) FROM hyprnote_github.{} t WHERE t.state = $1",
table_name
);
sqlx::query_scalar(&count_query)
.bind(state_filter)
.fetch_one(&state.db_pool)
.await
.unwrap_or(0)
} else {
let count_query = format!("SELECT COUNT(*) FROM hyprnote_github.{}", table_name);
sqlx::query_scalar(&count_query)
.fetch_one(&state.db_pool)
.await
.unwrap_or(0)
};

let result = serde_json::json!({
"table": table_name,
"total_count": total_count,
"returned_count": rows.len(),
"limit": limit,
"offset": offset,
"rows": rows,
});

Ok(CallToolResult::success(vec![Content::text(
result.to_string(),
)]))
}
4 changes: 2 additions & 2 deletions crates/api-support/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::state::AppState;

pub use feedback::{FeedbackRequest, FeedbackResponse};

pub fn router(config: SupportConfig) -> Router {
let state = AppState::new(config);
pub async fn router(config: SupportConfig) -> Router {
let state = AppState::new(config).await;
let mcp = mcp_service(state.clone());

Router::new()
Expand Down
17 changes: 15 additions & 2 deletions crates/api-support/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use octocrab::Octocrab;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;

use crate::config::SupportConfig;

#[derive(Clone)]
pub(crate) struct AppState {
pub(crate) config: SupportConfig,
pub(crate) octocrab: Octocrab,
pub(crate) db_pool: PgPool,
}

impl AppState {
pub(crate) fn new(config: SupportConfig) -> Self {
pub(crate) async fn new(config: SupportConfig) -> Self {
let key = jsonwebtoken::EncodingKey::from_rsa_pem(
config
.github
Expand All @@ -24,7 +27,17 @@ impl AppState {
.build()
.expect("failed to build octocrab client");

Self { config, octocrab }
let db_pool = PgPoolOptions::new()
.max_connections(5)
.connect(&config.supabase_db.supabase_db_url)
.await
.expect("failed to connect to Supabase Postgres");

Self {
config,
octocrab,
db_pool,
}
}

pub(crate) async fn installation_client(&self) -> Result<Octocrab, octocrab::Error> {
Expand Down
Loading