1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! Runtime library, providing the core functionality for the [`ai_transform`] macro, including
//! the `OpenAI` client, error types, and the main transformation logic.
//!
//! ## Usage
//!
//! Typically used indirectly through the [`ai_transform`] macro, but can
//! also be used directly:
//!
//! ```rust,no_run
//! use ai_transform_runtime::{transform, error::TransformError};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, Default)]
//! struct User { name: String, age: u32 }
//!
//! #[derive(Serialize, Deserialize, Default, Debug)]
//! struct Profile { full_name: String, years_old: u32, is_adult: bool }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), TransformError> {
//! let user = User { name: "Alice".to_string(), age: 28 };
//! let profile: Profile = transform(user).await?;
//! println!("{:?}", profile);
//! Ok(())
//! }
//! ```
//!
//! ## How It Works
//!
//! 1. **Serialization**: Converts the source value to JSON
//! 2. **Schema Generation**: Creates example JSON for both source and target types
//! 3. **AI Request**: Sends a transformation prompt to `OpenAI`'s API
//! 4. **Response Processing**: Extracts and cleans the JSON response
//! 5. **Deserialization**: Converts the result back to the target type
//!
//! ## Configuration
//!
//! Environment variables:
//! - `OPENAI_API_KEY`: Your `OpenAI` API key (**required**)
//! - `OPENAI_MODEL`: Model to use (default: `"gpt-4o"`)
//! - `OPENAI_BASE_URL`: API base URL (default: `"https://api.openai.com/v1"`)
//!
//! ## Considerations
//!
//! - Each call makes a network request to `OpenAI`'s API
//! - Response time depends on data complexity and `OpenAI`'s response time
//! - API usage incurs costs based on `OpenAI`'s pricing
//!
//! ## Requirements
//!
//! Both source and target types must implement:
//! - `serde::Serialize` + `serde::Deserialize` + `Default`
//!
//! [`ai_transform`]: https://docs.rs/ai-transform
use crateOpenAIClient;
use crateTransformError;
use ;
/// Transforms data from one type to another using AI-powered transformation.
///
/// # Arguments
///
/// * `val` - The source value to transform
///
/// # Returns
///
/// `Result<T, TransformError>` - Success contains transformed value, error contains failure details
///
/// # Examples
///
/// ```rust,no_run
/// use ai_transform_runtime::transform;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize, Default)]
/// struct User { name: String, age: u32 }
///
/// #[derive(Serialize, Deserialize, Default)]
/// struct Profile { full_name: String, years_old: u32 }
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let user = User { name: "Alice".into(), age: 28 };
/// let profile: Profile = transform(user).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// See [`TransformError`] for all possible failure modes
pub async