mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 12:15:40 +08:00
feat(ai): add endpoints to chat with ai service (#8585)
Co-authored-by: Riddhiagrawal001 <riddhi.agrawal2112@gmail.com> Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -452,6 +452,7 @@ pub(crate) async fn fetch_raw_secrets(
|
||||
|
||||
Settings {
|
||||
server: conf.server,
|
||||
chat: conf.chat,
|
||||
master_database,
|
||||
redis: conf.redis,
|
||||
log: conf.log,
|
||||
|
||||
@ -70,6 +70,7 @@ pub struct Settings<S: SecretState> {
|
||||
pub server: Server,
|
||||
pub proxy: Proxy,
|
||||
pub env: Env,
|
||||
pub chat: ChatSettings,
|
||||
pub master_database: SecretStateContainer<Database, S>,
|
||||
#[cfg(feature = "olap")]
|
||||
pub replica_database: SecretStateContainer<Database, S>,
|
||||
@ -195,6 +196,13 @@ pub struct Platform {
|
||||
pub allow_connected_merchants: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Default)]
|
||||
#[serde(default)]
|
||||
pub struct ChatSettings {
|
||||
pub enabled: bool,
|
||||
pub hyperswitch_ai_host: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
pub struct Multitenancy {
|
||||
pub tenants: TenantConfig,
|
||||
@ -1016,6 +1024,7 @@ impl Settings<SecuredSecret> {
|
||||
self.secrets.get_inner().validate()?;
|
||||
self.locker.validate()?;
|
||||
self.connectors.validate("connectors")?;
|
||||
self.chat.validate()?;
|
||||
|
||||
self.cors.validate()?;
|
||||
|
||||
|
||||
@ -343,3 +343,15 @@ impl super::settings::OpenRouter {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl super::settings::ChatSettings {
|
||||
pub fn validate(&self) -> Result<(), ApplicationError> {
|
||||
use common_utils::fp_utils::when;
|
||||
|
||||
when(self.enabled && self.hyperswitch_ai_host.is_empty(), || {
|
||||
Err(ApplicationError::InvalidConfigurationValueError(
|
||||
"hyperswitch ai host must be set if chat is enabled".into(),
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,4 +72,5 @@ pub mod relay;
|
||||
#[cfg(feature = "v2")]
|
||||
pub mod revenue_recovery;
|
||||
|
||||
pub mod chat;
|
||||
pub mod tokenization;
|
||||
|
||||
57
crates/router/src/core/chat.rs
Normal file
57
crates/router/src/core/chat.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use api_models::chat as chat_api;
|
||||
use common_utils::{
|
||||
consts,
|
||||
errors::CustomResult,
|
||||
request::{Method, RequestBuilder, RequestContent},
|
||||
};
|
||||
use error_stack::ResultExt;
|
||||
use external_services::http_client;
|
||||
use hyperswitch_domain_models::chat as chat_domain;
|
||||
use router_env::{instrument, logger, tracing};
|
||||
|
||||
use crate::{
|
||||
db::errors::chat::ChatErrors,
|
||||
routes::SessionState,
|
||||
services::{authentication as auth, ApplicationResponse},
|
||||
};
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn get_data_from_hyperswitch_ai_workflow(
|
||||
state: SessionState,
|
||||
user_from_token: auth::UserFromToken,
|
||||
req: chat_api::ChatRequest,
|
||||
) -> CustomResult<ApplicationResponse<chat_api::ChatResponse>, ChatErrors> {
|
||||
let url = format!("{}/webhook", state.conf.chat.hyperswitch_ai_host);
|
||||
|
||||
let request_body = chat_domain::HyperswitchAiDataRequest {
|
||||
query: chat_domain::GetDataMessage {
|
||||
message: req.message,
|
||||
},
|
||||
org_id: user_from_token.org_id,
|
||||
merchant_id: user_from_token.merchant_id,
|
||||
profile_id: user_from_token.profile_id,
|
||||
};
|
||||
logger::info!("Request for AI service: {:?}", request_body);
|
||||
|
||||
let request = RequestBuilder::new()
|
||||
.method(Method::Post)
|
||||
.url(&url)
|
||||
.attach_default_headers()
|
||||
.set_body(RequestContent::Json(Box::new(request_body.clone())))
|
||||
.build();
|
||||
|
||||
let response = http_client::send_request(
|
||||
&state.conf.proxy,
|
||||
request,
|
||||
Some(consts::REQUEST_TIME_OUT_FOR_AI_SERVICE),
|
||||
)
|
||||
.await
|
||||
.change_context(ChatErrors::InternalServerError)
|
||||
.attach_printable("Error when sending request to AI service")?
|
||||
.json::<_>()
|
||||
.await
|
||||
.change_context(ChatErrors::InternalServerError)
|
||||
.attach_printable("Error when deserializing response from AI service")?;
|
||||
|
||||
Ok(ApplicationResponse::Json(response))
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
pub mod chat;
|
||||
pub mod customers_error_response;
|
||||
pub mod error_handlers;
|
||||
pub mod transformers;
|
||||
|
||||
37
crates/router/src/core/errors/chat.rs
Normal file
37
crates/router/src/core/errors/chat.rs
Normal file
@ -0,0 +1,37 @@
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ChatErrors {
|
||||
#[error("User InternalServerError")]
|
||||
InternalServerError,
|
||||
#[error("Missing Config error")]
|
||||
MissingConfigError,
|
||||
#[error("Chat response deserialization failed")]
|
||||
ChatResponseDeserializationFailed,
|
||||
}
|
||||
|
||||
impl common_utils::errors::ErrorSwitch<api_models::errors::types::ApiErrorResponse> for ChatErrors {
|
||||
fn switch(&self) -> api_models::errors::types::ApiErrorResponse {
|
||||
use api_models::errors::types::{ApiError, ApiErrorResponse as AER};
|
||||
let sub_code = "AI";
|
||||
match self {
|
||||
Self::InternalServerError => {
|
||||
AER::InternalServerError(ApiError::new("HE", 0, self.get_error_message(), None))
|
||||
}
|
||||
Self::MissingConfigError => {
|
||||
AER::InternalServerError(ApiError::new(sub_code, 1, self.get_error_message(), None))
|
||||
}
|
||||
Self::ChatResponseDeserializationFailed => {
|
||||
AER::BadRequest(ApiError::new(sub_code, 2, self.get_error_message(), None))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ChatErrors {
|
||||
pub fn get_error_message(&self) -> String {
|
||||
match self {
|
||||
Self::InternalServerError => "Something went wrong".to_string(),
|
||||
Self::MissingConfigError => "Missing webhook url".to_string(),
|
||||
Self::ChatResponseDeserializationFailed => "Failed to parse chat response".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -189,7 +189,8 @@ pub fn mk_app(
|
||||
.service(routes::MerchantAccount::server(state.clone()))
|
||||
.service(routes::User::server(state.clone()))
|
||||
.service(routes::ApiKeys::server(state.clone()))
|
||||
.service(routes::Routing::server(state.clone()));
|
||||
.service(routes::Routing::server(state.clone()))
|
||||
.service(routes::Chat::server(state.clone()));
|
||||
|
||||
#[cfg(feature = "v1")]
|
||||
{
|
||||
|
||||
@ -75,6 +75,8 @@ pub mod process_tracker;
|
||||
#[cfg(feature = "v2")]
|
||||
pub mod proxy;
|
||||
|
||||
pub mod chat;
|
||||
|
||||
#[cfg(feature = "dummy_connector")]
|
||||
pub use self::app::DummyConnector;
|
||||
#[cfg(feature = "v2")]
|
||||
@ -86,7 +88,7 @@ pub use self::app::Recon;
|
||||
#[cfg(feature = "v2")]
|
||||
pub use self::app::Tokenization;
|
||||
pub use self::app::{
|
||||
ApiKeys, AppState, ApplePayCertificatesMigration, Authentication, Cache, Cards, Configs,
|
||||
ApiKeys, AppState, ApplePayCertificatesMigration, Authentication, Cache, Cards, Chat, Configs,
|
||||
ConnectorOnboarding, Customers, Disputes, EphemeralKey, FeatureMatrix, Files, Forex, Gsm,
|
||||
Health, Hypersense, Mandates, MerchantAccount, MerchantConnectorAccount, PaymentLink,
|
||||
PaymentMethods, Payments, Poll, ProcessTracker, Profile, ProfileAcquirer, ProfileNew, Refunds,
|
||||
|
||||
@ -59,8 +59,8 @@ use super::verification::{apple_pay_merchant_registration, retrieve_apple_pay_ve
|
||||
#[cfg(feature = "oltp")]
|
||||
use super::webhooks::*;
|
||||
use super::{
|
||||
admin, api_keys, cache::*, connector_onboarding, disputes, files, gsm, health::*, profiles,
|
||||
relay, user, user_role,
|
||||
admin, api_keys, cache::*, chat, connector_onboarding, disputes, files, gsm, health::*,
|
||||
profiles, relay, user, user_role,
|
||||
};
|
||||
#[cfg(feature = "v1")]
|
||||
use super::{apple_pay_certificates_migration, blocklist, payment_link, webhook_events};
|
||||
@ -2215,6 +2215,23 @@ impl Gsm {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Chat;
|
||||
|
||||
#[cfg(feature = "olap")]
|
||||
impl Chat {
|
||||
pub fn server(state: AppState) -> Scope {
|
||||
let mut route = web::scope("/chat").app_data(web::Data::new(state.clone()));
|
||||
if state.conf.chat.enabled {
|
||||
route = route.service(
|
||||
web::scope("/ai").service(
|
||||
web::resource("/data")
|
||||
.route(web::post().to(chat::get_data_from_hyperswitch_ai_workflow)),
|
||||
),
|
||||
);
|
||||
}
|
||||
route
|
||||
}
|
||||
}
|
||||
pub struct ThreeDsDecisionRule;
|
||||
|
||||
#[cfg(feature = "oltp")]
|
||||
|
||||
38
crates/router/src/routes/chat.rs
Normal file
38
crates/router/src/routes/chat.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use actix_web::{web, HttpRequest, HttpResponse};
|
||||
#[cfg(feature = "olap")]
|
||||
use api_models::chat as chat_api;
|
||||
use router_env::{instrument, tracing, Flow};
|
||||
|
||||
use super::AppState;
|
||||
use crate::{
|
||||
core::{api_locking, chat as chat_core},
|
||||
services::{
|
||||
api,
|
||||
authentication::{self as auth},
|
||||
authorization::permissions::Permission,
|
||||
},
|
||||
};
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn get_data_from_hyperswitch_ai_workflow(
|
||||
state: web::Data<AppState>,
|
||||
http_req: HttpRequest,
|
||||
payload: web::Json<chat_api::ChatRequest>,
|
||||
) -> HttpResponse {
|
||||
let flow = Flow::GetDataFromHyperswitchAiFlow;
|
||||
Box::pin(api::server_wrap(
|
||||
flow.clone(),
|
||||
state,
|
||||
&http_req,
|
||||
payload.into_inner(),
|
||||
|state, user: auth::UserFromToken, payload, _| {
|
||||
chat_core::get_data_from_hyperswitch_ai_workflow(state, user, payload)
|
||||
},
|
||||
// At present, the AI service retrieves data scoped to the merchant level
|
||||
&auth::JWTAuth {
|
||||
permission: Permission::MerchantPaymentRead,
|
||||
},
|
||||
api_locking::LockAction::NotApplicable,
|
||||
))
|
||||
.await
|
||||
}
|
||||
@ -35,6 +35,7 @@ pub enum ApiIdentifier {
|
||||
UserRole,
|
||||
ConnectorOnboarding,
|
||||
Recon,
|
||||
AiWorkflow,
|
||||
Poll,
|
||||
ApplePayCertificatesMigration,
|
||||
Relay,
|
||||
@ -300,6 +301,8 @@ impl From<Flow> for ApiIdentifier {
|
||||
| Flow::DeleteTheme
|
||||
| Flow::CloneConnector => Self::User,
|
||||
|
||||
Flow::GetDataFromHyperswitchAiFlow => Self::AiWorkflow,
|
||||
|
||||
Flow::ListRolesV2
|
||||
| Flow::ListInvitableRolesAtEntityLevel
|
||||
| Flow::ListUpdatableRolesAtEntityLevel
|
||||
|
||||
Reference in New Issue
Block a user