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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use async_trait::async_trait;
use redis::{aio::Connection, AsyncCommands, Client, ToRedisArgs};
use serde::de::DeserializeOwned;
use std::fmt::{Debug, Display};
use tokio::sync::OnceCell;
use tracing::{debug, error, instrument};

use crate::{
	types::{LoomError, PromptModelTokens, StorageError},
	Config, ContextMessage, TapestryFragment, TapestryId,
};

/// A storage handler trait designed for saving and retrieving fragments of a tapestry.
///
/// The `TapestryChestHandler` trait provides an asynchronous interface for implementing various
/// storage handlers to manage tapestry fragments.
///
/// # Usage
///
/// Implementations of `TapestryChestHandler` should provide the storage and retrieval mechanisms
/// tailored to specific use-cases or storage backends, such as databases, file systems, or
/// in-memory stores.
///
/// Checkout the out of the box implementation [`TapestryChest`] whichs uses Redis as the storage
/// backend.
#[async_trait]
pub trait TapestryChestHandler<T: Config> {
	/// Defines the error type returned by the handler methods.
	type Error: Display + Debug;

	/// Saves a tapestry fragment.
	///
	/// # Parameters
	///
	/// - `tapestry_id`: Identifies the tapestry.
	/// - `tapestry_fragment`: An instance of `TapestryFragment` to be stored.
	/// - `increment`:
	///     - A boolean flag indicating whether the tapestry instance should be incremented.
	///     - This should typically be `true` when saving a new instance of [`TapestryFragment`],
	///       and `false` when updating an existing one.
	///
	/// # Returns
	///
	/// Returns `Result<(), Self::Error>`. On successful storage, it returns `Ok(())`. If the
	/// storage operation fails, it should return an `Err` variant containing an error of type
	/// `Self::Error`.
	async fn save_tapestry_fragment<TID: TapestryId>(
		tapestry_id: TID,
		tapestry_fragment: TapestryFragment<T>,
		increment: bool,
	) -> crate::Result<()>;
	/// Save tapestry metadata.
	///
	/// Based on application use cases, you can add aditional data for a given [`TapestryId`]
	async fn save_tapestry_metadata<TID: TapestryId, M: ToRedisArgs + Send + Sync>(
		tapestry_id: TID,
		metadata: M,
	) -> crate::Result<()>;
	/// Retrieves the last tapestry fragment, or a fragment at a specified instance.
	///
	/// # Parameters
	///
	/// - `tapestry_id`: Identifies the tapestry.
	/// - `instance`: The instance of the fragment to retrieve. If `None`, the method should
	///   retrieve the last fragment.
	///
	/// # Returns
	///
	/// On successful retrieval, it returns `Ok(Some(TapestryFragment))` or `Ok(None)` if no
	/// fragment was found. If the retrieval operation fails, it should return an `Err` variant
	/// containing an error of type `Self::Error`.
	async fn get_tapestry_fragment<TID: TapestryId>(
		tapestry_id: TID,
		instance: Option<u64>,
	) -> crate::Result<Option<TapestryFragment<T>>>;
	/// Retrieves the last tapestry metadata, or a metadata at a specified instance.
	async fn get_tapestry_metadata<TID: TapestryId, M: DeserializeOwned + Default>(
		tapestry_id: TID,
		instance: Option<u64>,
	) -> crate::Result<Option<M>>;
}

/// Default implementation of [`Config::TapestryChest`]
///
/// Storing and retrieving data using a Redis instance.
pub struct TapestryChest;

#[async_trait]
impl<T: Config> TapestryChestHandler<T> for TapestryChest {
	type Error = StorageError;

	async fn save_tapestry_fragment<TID: TapestryId>(
		tapestry_id: TID,
		tapestry_fragment: TapestryFragment<T>,
		increment: bool,
	) -> crate::Result<()> {
		let client = get_client().await.expect("Failed to get redis client");
		let mut con = client.get_async_connection().await.expect("Failed to get redis connection");
		debug!("Connected to Redis");

		let base_key: &String = &tapestry_id.base_key();

		let mut instance = match get_score_from_last_zset_member(&mut con, base_key).await? {
			Some(instance) => instance,
			None => {
				con.zadd(base_key, 0, 0).await.map_err(|e| {
					error!("Failed to save tapestry fragment to Redis: {}", e);
					LoomError::from(StorageError::Redis(e))
				})?;

				0
			},
		};

		if increment {
			con.zincr(base_key, 0, 1).await.map_err(|e| {
				error!("Failed to save tapestry fragment to Redis: {}", e);
				LoomError::from(StorageError::Redis(e))
			})?;

			instance += 1;

			debug!("Incremented instance to {} for {}", instance, base_key);
		}

		let key = format!("{base_key}:{instance}");

		con.hset(&key, "context_tokens", tapestry_fragment.context_tokens)
			.await
			.map_err(|e| {
				error!("Failed to save \"context_tokens\" member to {} key: {}", key, e);
				LoomError::from(StorageError::Redis(e))
			})?;

		debug!("Saved \"context_tokens\" member to {} key", key);

		con.hset(
			&key,
			"context_messages",
			serde_json::to_vec(&tapestry_fragment.context_messages).map_err(|e| {
				error!("Failed to serialize tapestry fragment context_messages: {}", e);
				StorageError::Parsing
			})?,
		)
		.await
		.map_err(|e| {
			error!("Failed to save \"context_messages\" member to {} key: {}", key, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		debug!("Saved \"context_messages\" member to {} key", key);

		Ok(())
	}

	async fn save_tapestry_metadata<TID: TapestryId, M: ToRedisArgs + Send + Sync>(
		tapestry_id: TID,
		metadata: M,
	) -> crate::Result<()> {
		let client = get_client().await.expect("Failed to get redis client");
		let mut con = client.get_async_connection().await.expect("Failed to get redis connection");
		debug!("Connected to Redis");

		let base_key: &String = &tapestry_id.base_key();

		let instance = match get_score_from_last_zset_member(&mut con, base_key).await? {
			Some(instance) => instance,
			None => {
				con.zadd(base_key, 0, 0).await.map_err(|e| {
					error!("Failed to save tapestry fragment to Redis: {}", e);
					LoomError::from(StorageError::Redis(e))
				})?;

				0
			},
		};

		let key = format!("{base_key}:{instance}");

		con.hset(&key, "metadata", metadata).await.map_err(|e| {
			error!("Failed to save \"metadata\" member to {} key: {}", key, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		debug!("Saved \"metadata\" member to {} key", key);

		Ok(())
	}

	async fn get_tapestry_fragment<TID: TapestryId>(
		tapestry_id: TID,
		instance: Option<u64>,
	) -> crate::Result<Option<TapestryFragment<T>>> {
		let client = get_client().await.expect("Failed to get redis client");
		let mut con = client.get_async_connection().await.expect("Failed to get redis connection");
		debug!("Connected to Redis");

		let base_key = &tapestry_id.base_key();

		let instance = match instance {
			Some(instance) => instance,
			None => match get_score_from_last_zset_member(&mut con, base_key).await? {
				Some(instance) => instance,
				None => return Ok(None),
			},
		};

		let key = format!("{base_key}:{instance}");

		let tapestry_fragment = TapestryFragment {
			context_tokens: {
				let context_tokens_str: String =
					con.hget(&key, "context_tokens").await.map_err(|e| {
						error!("Failed to get \"context_tokens\" member from {} key: {}", key, e);
						LoomError::from(StorageError::Redis(e))
					})?;
				context_tokens_str.parse::<PromptModelTokens<T>>().map_err(|_| {
					error!("Failed to parse \"context_tokens\" member from key: {}", key);
					StorageError::Parsing
				})?
			},
			context_messages: {
				let context_messages_raw: Vec<u8> =
					con.hget(&key, "context_messages").await.map_err(|e| {
						error!("Failed to get \"context_messages\" member from {} key: {}", key, e);
						LoomError::from(StorageError::Redis(e))
					})?;

				serde_json::from_slice::<Vec<ContextMessage<T>>>(&context_messages_raw).map_err(
					|e| {
						error!("Failed to parse tapestry fragment context_messages: {}", e);
						StorageError::Parsing
					},
				)?
			},
		};

		Ok(Some(tapestry_fragment))
	}

	async fn get_tapestry_metadata<TID: TapestryId, M: DeserializeOwned + Default>(
		tapestry_id: TID,
		instance: Option<u64>,
	) -> crate::Result<Option<M>> {
		let client = get_client().await.expect("Failed to get redis client");
		let mut con = client.get_async_connection().await.expect("Failed to get redis connection");
		debug!("Connected to Redis");

		let base_key = &tapestry_id.base_key();

		let instance = match instance {
			Some(instance) => instance,
			None => match get_score_from_last_zset_member(&mut con, base_key).await? {
				Some(instance) => instance,
				None => return Ok(None),
			},
		};

		let key = format!("{base_key}:{instance}");

		let metadata_raw: Vec<u8> = con.hget(&key, "metadata").await.map_err(|e| {
			error!("Failed to get \"metadata\" member from {} key: {}", key, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		let tapestry_metadata = serde_json::from_slice::<M>(&metadata_raw).map_err(|e| {
			error!("Failed to parse tapestry fragment metadata: {}", e);
			StorageError::Parsing
		})?;

		Ok(Some(tapestry_metadata))
	}
}

/// Storage client to access GCP Storage
static REDIS_CLIENT: OnceCell<Client> = OnceCell::const_new();

/// Get and/or initialize the Redis Client.
#[instrument]
async fn get_client() -> Result<Client, redis::RedisError> {
	Ok(REDIS_CLIENT
		.get_or_init(async || {
			debug!("Initializing Redis client");

			let protocol = std::env::var("REDIS_PROTOCOL").unwrap_or_else(|_| "redis".to_string());
			let host = std::env::var("REDIS_HOST").unwrap_or_else(|_| "redis".to_string());
			let port = std::env::var("REDIS_PORT").unwrap_or_else(|_| "6379".to_string());
			let password = std::env::var("REDIS_PASSWORD").unwrap_or_default();

			match redis::Client::open(format!("{}://:{}@{}:{}", protocol, password, host, port)) {
				Ok(client) => client,
				Err(e) => {
					let m = format!("Failed to initialize Redis client: {}", e);
					error!(m);
					panic!("{}", m)
				},
			}
		})
		.await
		.clone())
}

async fn get_score_from_last_zset_member(
	con: &mut Connection,
	base_key: &String,
) -> super::Result<Option<u64>> {
	debug!("Executing ZRANGE_WITHSCORES {}...", base_key);
	let member_score: Vec<String> = con.zrange_withscores(base_key, -1, -1).await.map_err(|e| {
		error!("Failed to save tapestry fragment to Redis: {}", e);
		LoomError::from(StorageError::Redis(e))
	})?;
	debug!("Result ZRANGE_WITHSCORES: {:?}", member_score);

	let instance = match member_score.is_empty() {
		true => return Ok(None),
		false if member_score.len() == 2 => member_score[1].parse::<u64>().unwrap_or(0),
		false => {
			error!("Unexpected member score length: {}", member_score.len());
			return Err(LoomError::from(StorageError::Parsing).into());
		},
	};

	Ok(Some(instance))
}