From 78d47894f2e19626bb5d85520219283359b4b1e1 Mon Sep 17 00:00:00 2001 From: David Onuh Date: Sun, 29 Sep 2024 01:40:23 +0100 Subject: [PATCH] Connect ai dls to cli --- ahnlich/cli/src/connect.rs | 25 ++++++++++++++++++++----- ahnlich/cli/src/term.rs | 1 - 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ahnlich/cli/src/connect.rs b/ahnlich/cli/src/connect.rs index 94409ea1..5dd2e78d 100644 --- a/ahnlich/cli/src/connect.rs +++ b/ahnlich/cli/src/connect.rs @@ -1,12 +1,12 @@ use super::config::cli::Agent; use ahnlich_client_rs::{ - ai::{AIClient, AIConnManager}, + ai::{AIClient, AIConnManager, AIPipeline}, db::{DbClient, DbConnManager, DbPipeline}, prelude::{AIServerResponse, ServerResponse}, }; -use ahnlich_types::{db::ServerDBQuery, ServerType}; +use ahnlich_types::{ai::AIServerQuery, db::ServerDBQuery, ServerType}; use deadpool::managed::Pool; -use dsl::db::parse_db_query; +use dsl::{ai::parse_ai_query, db::parse_db_query}; use crossterm::style::Stylize; use serde::Serialize; @@ -75,7 +75,22 @@ impl AgentPool { pub async fn parse_queries(&self, input: &str) -> Result, String> { match self { - AgentPool::AI(_pool) => Err(String::from("Unimplemented")), + AgentPool::AI(pool) => { + let queries = parse_ai_query(input).map_err(|err| err.to_string())?; + + let server_query = AIServerQuery::from_queries(&queries); + + let conn = pool + .get() + .await + .map_err(|err| format!("Could not get ai client connection {err}"))?; + + let pipeline = AIPipeline::new_from_queries_and_conn(server_query, conn); + + let response = pipeline.exec().await.map_err(|err| err.to_string())?; + + Ok(render(response.into_inner())) + } AgentPool::DB(pool) => { let queries = parse_db_query(input).map_err(|err| err.to_string())?; @@ -112,7 +127,7 @@ fn render(input: Vec>) -> Vec { Ok(success) => format_success( serde_json::to_string_pretty(&success) .map_err(|err| err.to_string()) - .expect("Failed to parse ai success response to json"), + .expect("Failed to parse success response to json"), ), Err(err) => format_error(err), }) diff --git a/ahnlich/cli/src/term.rs b/ahnlich/cli/src/term.rs index f9cadaec..b428b6bc 100644 --- a/ahnlich/cli/src/term.rs +++ b/ahnlich/cli/src/term.rs @@ -45,7 +45,6 @@ impl Term { )))?; stdout.execute(SetForegroundColor(Color::White))?; stdout.flush()?; - stdout.flush()?; Ok(()) }