elizaos_plugin_copilot_proxy/
lib.rs1#![warn(missing_docs)]
10
11pub mod client;
13pub mod config;
15pub mod error;
17pub mod providers;
19pub mod service;
21pub mod types;
23
24pub use client::CopilotProxyClient;
25pub use config::{
26 normalize_base_url, CopilotProxyConfig, AVAILABLE_MODELS, DEFAULT_BASE_URL,
27 DEFAULT_CONTEXT_WINDOW, DEFAULT_LARGE_MODEL, DEFAULT_MAX_TOKENS, DEFAULT_SMALL_MODEL,
28 DEFAULT_TIMEOUT_SECONDS,
29};
30pub use error::{CopilotProxyError, Result};
31pub use providers::{
32 get_available_models, get_default_models, is_known_model, CopilotProxyModelProvider,
33 ModelCost, ModelDefinition, ModelProviderConfig,
34};
35pub use service::{get_service, initialize_service, CopilotProxyService};
36pub use types::{
37 ChatCompletionChoice, ChatCompletionRequest, ChatCompletionResponse, ChatMessage, ChatRole,
38 ModelInfo, ModelsResponse, TextGenerationParams, TextGenerationResult, TokenUsage,
39};
40
41use anyhow::Result as AnyhowResult;
42use std::sync::Arc;
43
44pub struct CopilotProxyPlugin {
46 provider: CopilotProxyModelProvider,
47}
48
49impl CopilotProxyPlugin {
50 pub fn new(config: CopilotProxyConfig) -> Result<Self> {
52 config.validate()?;
53 Ok(Self {
54 provider: CopilotProxyModelProvider::new(config),
55 })
56 }
57
58 pub fn from_env() -> Result<Self> {
60 let config = CopilotProxyConfig::from_env();
61 Self::new(config)
62 }
63
64 pub async fn initialize(&self) -> Result<()> {
66 self.provider.initialize().await
67 }
68
69 pub async fn is_available(&self) -> bool {
71 self.provider.is_available().await
72 }
73
74 pub async fn generate_text(&self, prompt: &str) -> Result<String> {
76 self.provider.generate_text_large(prompt).await
77 }
78
79 pub async fn generate_text_small(&self, prompt: &str) -> Result<String> {
81 self.provider.generate_text_small(prompt).await
82 }
83
84 pub async fn generate_text_large(&self, prompt: &str) -> Result<String> {
86 self.provider.generate_text_large(prompt).await
87 }
88
89 pub async fn generate_object_small(&self, prompt: &str) -> Result<serde_json::Value> {
91 self.provider.generate_object_small(prompt).await
92 }
93
94 pub async fn generate_object_large(&self, prompt: &str) -> Result<serde_json::Value> {
96 self.provider.generate_object_large(prompt).await
97 }
98
99 pub fn provider(&self) -> &CopilotProxyModelProvider {
101 &self.provider
102 }
103
104 pub async fn shutdown(&self) {
106 self.provider.shutdown().await
107 }
108}
109
110pub fn get_copilot_proxy_plugin() -> AnyhowResult<CopilotProxyPlugin> {
112 CopilotProxyPlugin::from_env()
113 .map_err(|e| anyhow::anyhow!("Failed to create Copilot Proxy plugin: {}", e))
114}
115
116pub fn create_copilot_proxy_elizaos_plugin() -> AnyhowResult<elizaos::types::Plugin> {
118 use elizaos::types::{Plugin, PluginDefinition};
119 use std::collections::HashMap;
120
121 let plugin = Arc::new(get_copilot_proxy_plugin()?);
122
123 let mut model_handlers: HashMap<String, elizaos::types::ModelHandlerFn> = HashMap::new();
124
125 let plugin_large = plugin.clone();
127 model_handlers.insert(
128 "TEXT_LARGE".to_string(),
129 Box::new(move |params: serde_json::Value| {
130 let plugin = plugin_large.clone();
131 Box::pin(async move {
132 let prompt = params
133 .get("prompt")
134 .and_then(|v| v.as_str())
135 .unwrap_or("");
136 plugin
137 .generate_text_large(prompt)
138 .await
139 .map_err(|e| anyhow::anyhow!("Copilot Proxy error: {}", e))
140 })
141 }),
142 );
143
144 let plugin_small = plugin.clone();
146 model_handlers.insert(
147 "TEXT_SMALL".to_string(),
148 Box::new(move |params: serde_json::Value| {
149 let plugin = plugin_small.clone();
150 Box::pin(async move {
151 let prompt = params
152 .get("prompt")
153 .and_then(|v| v.as_str())
154 .unwrap_or("");
155 plugin
156 .generate_text_small(prompt)
157 .await
158 .map_err(|e| anyhow::anyhow!("Copilot Proxy error: {}", e))
159 })
160 }),
161 );
162
163 let plugin_obj_large = plugin.clone();
165 model_handlers.insert(
166 "OBJECT_LARGE".to_string(),
167 Box::new(move |params: serde_json::Value| {
168 let plugin = plugin_obj_large.clone();
169 Box::pin(async move {
170 let prompt = params
171 .get("prompt")
172 .and_then(|v| v.as_str())
173 .unwrap_or("");
174 let result = plugin
175 .generate_object_large(prompt)
176 .await
177 .map_err(|e| anyhow::anyhow!("Copilot Proxy error: {}", e))?;
178 Ok(serde_json::to_string(&result).unwrap_or_default())
179 })
180 }),
181 );
182
183 let plugin_obj_small = plugin.clone();
185 model_handlers.insert(
186 "OBJECT_SMALL".to_string(),
187 Box::new(move |params: serde_json::Value| {
188 let plugin = plugin_obj_small.clone();
189 Box::pin(async move {
190 let prompt = params
191 .get("prompt")
192 .and_then(|v| v.as_str())
193 .unwrap_or("");
194 let result = plugin
195 .generate_object_small(prompt)
196 .await
197 .map_err(|e| anyhow::anyhow!("Copilot Proxy error: {}", e))?;
198 Ok(serde_json::to_string(&result).unwrap_or_default())
199 })
200 }),
201 );
202
203 Ok(Plugin {
204 definition: PluginDefinition {
205 name: "copilot-proxy".to_string(),
206 description: "Copilot Proxy model provider for elizaOS".to_string(),
207 ..Default::default()
208 },
209 model_handlers,
210 ..Default::default()
211 })
212}