git.delta.rocks / remowt / refs/heads / trunk

difftreelog

source

crates/remowt-ui-prompt/src/bifrost.rs2.2 KiBsourcehistory
1use bifrostlink::{Config, Rpc};2use bifrostlink_macros::endpoints;3use serde::{Deserialize, Serialize};45use crate::{Error, Prompter, Source};67pub struct PromptEndpoints<P>(pub P);89#[endpoints(ns = 2)]10impl<P> PromptEndpoints<P>11where12	P: Prompter + Send + Sync + 'static,13{14	#[endpoints(id = 1, cancel)]15	async fn prompt_enum(16		&self,17		prompt: String,18		description: String,19		variants: Vec<String>,20		source: Vec<Source>,21	) -> Result<u32, Error> {22		let variants: Vec<&str> = variants.iter().map(|v| v.as_str()).collect();23		self.024			.prompt_enum(&prompt, &description, &variants, &source)25			.await26	}2728	#[endpoints(id = 2, cancel)]29	async fn prompt_text(30		&self,31		echo: bool,32		prompt: String,33		description: String,34		source: Vec<Source>,35	) -> Result<String, Error> {36		self.037			.prompt_text(echo, &prompt, &description, &source)38			.await39	}4041	#[endpoints(id = 3, cancel)]42	async fn display_text(43		&self,44		error: bool,45		description: String,46		source: Vec<Source>,47	) -> Result<(), Error> {48		self.0.display_text(error, &description, &source).await49	}50}5152impl<C: Config> Prompter for PromptEndpointsClient<C>53where54	Error: ToString,55{56	async fn prompt_enum(57		&self,58		prompt: &str,59		description: &str,60		variants: &[&str],61		source: &[Source],62	) -> crate::Result<u32> {63		self.prompt_enum(64			prompt.to_owned(),65			description.to_owned(),66			variants.iter().map(|v| (*v).to_owned()).collect(),67			source.to_vec(),68		)69		.await70		.map_err(|e| Error::Remote(e.to_string()))?71	}7273	async fn prompt_text(74		&self,75		echo: bool,76		prompt: &str,77		description: &str,78		source: &[Source],79	) -> crate::Result<String> {80		self.prompt_text(81			echo,82			prompt.to_owned(),83			description.to_owned(),84			source.to_vec(),85		)86		.await87		.map_err(|e| Error::Remote(e.to_string()))?88	}8990	async fn display_text(91		&self,92		error: bool,93		description: &str,94		source: &[Source],95	) -> crate::Result<()> {96		self.display_text(error, description.to_owned(), source.to_vec())97			.await98			.map_err(|e| Error::Remote(e.to_string()))?99	}100}101102pub fn serve_prompts<P, C>(rpc: &mut Rpc<C>, prompt: P)103where104	P: Prompter + Send + Sync + 'static,105	C: Config,106	C::Error: From<Error>,107{108	PromptEndpoints(prompt).register_endpoints(rpc);109}