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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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,
};

/// The key used to store the number of instances of a tapestry.
const INSTANCE_COUNT: &str = "instance_count";

/// A storage handler trait designed for saving and retrieving fragments of a tapestry.
///
/// # 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.
///
/// You can see how the default [`TapestryChest`] struct implementes this trait which uses Redis as
/// its 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.
	///
	/// Tapestry fragments are stored incrementally, also refered to as "instances". Each instance
	/// is identified by an integer, starting at 1 and incrementing by 1 for each new instance.
	///
	/// This method is executed primarily by the [`crate::Loom::weave`] function.
	///
	/// # 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.
	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 + Debug + Clone + Send + Sync>(
		tapestry_id: TID,
		metadata: M,
	) -> crate::Result<()>;
	/// Retrieves the number of instances of a tapestry.
	///
	/// Returns None if the tapestry does not exist.
	async fn get_tapestry<TID: TapestryId>(tapestry_id: TID) -> crate::Result<Option<u16>>;
	/// 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.
	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>(
		tapestry_id: TID,
	) -> crate::Result<Option<M>>;
	/// Deletes a tapestry and all its instances.
	async fn delete_tapestry<TID: TapestryId>(tapestry_id: TID) -> crate::Result<()>;
}

/// Default implementation of [`Config::Chest`]
///
/// 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?;
		debug!("Connected to Redis");

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

		let mut instance = match get_last_instance(&mut con, base_key, None).await? {
			Some(instance) => instance,
			None => {
				con.hset(base_key, INSTANCE_COUNT, 1).await.map_err(|e| {
					error!("Failed to save \"instance_count\" member to {} key: {}", base_key, e);
					LoomError::from(StorageError::Redis(e))
				})?;

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

				1
			},
		};

		if increment {
			con.hincr(base_key, INSTANCE_COUNT, 1).await.map_err(|e| {
				error!("Failed to increment \"instance_count\" member of {} key: {}", base_key, e);
				LoomError::from(StorageError::Redis(e))
			})?;

			instance += 1;

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

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

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

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

		con.hset(
			&instance_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: {}", instance_key, e);
			LoomError::from(StorageError::Redis(e))
		})?;

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

		Ok(())
	}

	async fn save_tapestry_metadata<
		TID: TapestryId,
		M: ToRedisArgs + Debug + Clone + 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?;
		debug!("Connected to Redis");

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

		con.hset(&key, "metadata", metadata.clone()).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 with metadata {:?}", key, metadata.clone());

		Ok(())
	}

	async fn get_tapestry<TID: TapestryId>(tapestry_id: TID) -> crate::Result<Option<u16>> {
		let client = get_client().await.expect("Failed to get redis client");
		let mut con = client.get_async_connection().await?;

		let base_key = &tapestry_id.base_key();

		let exists: bool = con.exists(base_key).await.map_err(|e| {
			error!("Failed to check if {} tapestry_id exists: {}", base_key, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		if !exists {
			return Ok(None);
		}

		let tapestry: u16 = con.hget(base_key, INSTANCE_COUNT).await.map_err(|e| {
			error!("Failed to get {} tapestry_id: {}", base_key, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		Ok(Some(tapestry))
	}

	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?;
		debug!("Connected to Redis");

		let base_key = &tapestry_id.base_key();

		let instance = match get_last_instance(&mut con, base_key, instance).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>(
		tapestry_id: TID,
	) -> crate::Result<Option<M>> {
		let client = get_client().await.expect("Failed to get redis client");
		let mut con = client.get_async_connection().await?;
		debug!("Connected to Redis");

		let key = &tapestry_id.base_key();

		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))
	}

	async fn delete_tapestry<TID: TapestryId>(tapestry_id: TID) -> crate::Result<()> {
		let client = get_client().await.expect("Failed to get redis client");
		let mut con = client.get_async_connection().await?;

		let tapestry_id = &tapestry_id.base_key();

		let exists: bool = con.exists(tapestry_id).await.map_err(|e| {
			error!("Failed to check if {} tapestry_id exists: {}", tapestry_id, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		if !exists {
			debug!("{} tapestry_id does not exist", tapestry_id);
			return Ok(());
		}

		let instance_count: u16 = con.hget(tapestry_id, INSTANCE_COUNT).await.map_err(|e| {
			error!("Failed to get {} tapestry_id: {}", tapestry_id, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		for i in 1..=instance_count {
			let instance_key = format!("{}:{}", tapestry_id, i);

			debug!("Deleting {} instance", instance_key);

			con.del(&instance_key).await.map_err(|e| {
				error!("Failed to delete {} tapestry_id: {}", tapestry_id, e);
				LoomError::from(StorageError::Redis(e))
			})?;
		}

		con.del(tapestry_id).await.map_err(|e| {
			error!("Failed to delete {} tapestry_id: {}", tapestry_id, e);
			LoomError::from(StorageError::Redis(e))
		})?;

		debug!("Deleted {} tapestry_id and {} instances", tapestry_id, instance_count);

		Ok(())
	}
}

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

/// Get 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())
}

/// Get the last instance number of a tapestry.
///
/// If the tapestry does not exist, it will be created and the instance number will be set to 1.
///
/// Passing the `instance` parameter will return the instance number if it exists, or an error if it
/// does not.
async fn get_last_instance(
	con: &mut Connection,
	base_key: &String,
	instance: Option<u64>,
) -> crate::Result<Option<u64>> {
	Ok(match con.exists(base_key).await? {
		true => match instance {
			Some(instance) =>
				if con.exists(&format!("{}:{}", base_key, instance)).await? {
					Some(instance)
				} else {
					return Err(LoomError::from(StorageError::NotFound).into());
				},
			None => con.hget(base_key, INSTANCE_COUNT).await.map_err(|e| {
				error!("Failed to get {} tapestry_id: {}", base_key, e);
				LoomError::from(StorageError::Redis(e))
			})?,
		},
		false => None,
	})
}