agentic_evolve_core/composition/
adapter.rs1use crate::types::error::EvolveResult;
4use crate::types::pattern::Pattern;
5
6#[derive(Debug, Default)]
8pub struct AdapterGenerator;
9
10impl AdapterGenerator {
11 pub fn new() -> Self {
12 Self
13 }
14
15 pub fn generate_adapter(
16 &self,
17 source: &Pattern,
18 target: &Pattern,
19 ) -> EvolveResult<AdapterCode> {
20 let needs_type_conversion = source.signature.return_type
21 != target
22 .signature
23 .params
24 .first()
25 .map(|p| p.param_type.clone());
26 let needs_async_bridge = source.signature.is_async != target.signature.is_async;
27
28 let code =
29 self.build_adapter_code(source, target, needs_type_conversion, needs_async_bridge);
30
31 Ok(AdapterCode {
32 code,
33 source_pattern_id: source.id.as_str().to_string(),
34 target_pattern_id: target.id.as_str().to_string(),
35 needs_type_conversion,
36 needs_async_bridge,
37 })
38 }
39
40 fn build_adapter_code(
41 &self,
42 source: &Pattern,
43 target: &Pattern,
44 needs_type_conversion: bool,
45 needs_async_bridge: bool,
46 ) -> String {
47 let mut code = String::new();
48
49 if needs_async_bridge && source.signature.is_async && !target.signature.is_async {
50 code.push_str("// Async-to-sync bridge\n");
51 code.push_str(&format!(
52 "let result = tokio::runtime::Handle::current().block_on({}_result);\n",
53 source.signature.name
54 ));
55 }
56
57 if needs_type_conversion {
58 code.push_str(&format!(
59 "// Type conversion: {} output -> {} input\n",
60 source.signature.name, target.signature.name
61 ));
62 code.push_str(&format!(
63 "let adapted = {}_output.into();\n",
64 source.signature.name
65 ));
66 }
67
68 if code.is_empty() {
69 code.push_str("// Direct connection - no adapter needed\n");
70 }
71
72 code
73 }
74}
75
76#[derive(Debug, Clone)]
78pub struct AdapterCode {
79 pub code: String,
80 pub source_pattern_id: String,
81 pub target_pattern_id: String,
82 pub needs_type_conversion: bool,
83 pub needs_async_bridge: bool,
84}