Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Refactor avatar key retrieval in S3Storage and update AvatarPoolService

Muhammad Hamza Khan3fdbf445a03d
diff --git a/packages/app/src/backend/services/avatar-pool.service.ts b/packages/app/src/backend/services/avatar-pool.service.ts
index 2f59200c..f9e5a48e 100644
--- a/packages/app/src/backend/services/avatar-pool.service.ts
+++ b/packages/app/src/backend/services/avatar-pool.service.ts
@@ -15,13 +15,14 @@ const CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000; // 30 days
*/
async function refreshCache(): Promise<void> {
try {
- // Cast to S3Storage to access listAvatarKeys method
+ // Cast to S3Storage to access listKeys method
const s3Storage = assetStorage as any;
- if (typeof s3Storage.listAvatarKeys === 'function') {
- cachedAvatarKeys = await s3Storage.listAvatarKeys();
+ if (typeof s3Storage.listKeys === 'function') {
+ // Fallback to generic listKeys method if listAvatarKeys is not available
+ cachedAvatarKeys = await s3Storage.listKeys('teams/', 2500, /avatar-/i);
cacheTimestamp = Date.now();
} else {
- throw new Error('listAvatarKeys method not available on storage');
+ throw new Error('listKeys method not available on storage');
}
} catch (error) {
console.error('[AvatarPool] Failed to refresh cache:', error);
diff --git a/packages/app/src/backend/services/storage/S3Storage.class.ts b/packages/app/src/backend/services/storage/S3Storage.class.ts
index bdabd6b7..07e0eed2 100644
--- a/packages/app/src/backend/services/storage/S3Storage.class.ts
+++ b/packages/app/src/backend/services/storage/S3Storage.class.ts
@@ -209,13 +209,20 @@ export default class S3Storage implements StaticStorage {
return res.Body as NodeJS.ReadableStream;
}
- public async listAvatarKeys(
- prefix: string = 'teams/',
- maxKeys: number = 2500,
+ /**
+ * Lists S3 object keys matching a given pattern
+ * @param prefix - S3 prefix to filter keys (default: empty string for root)
+ * @param maxKeys - Maximum number of keys to return (default: 2500)
+ * @param pattern - Regular expression pattern to match keys (default: matches all keys)
+ * @returns Promise resolving to an array of matching S3 keys
+ */
+ public async listKeys(
+ prefix: string = '',
+ maxKeys: number = 1000,
+ pattern?: RegExp,
): Promise<string[]> {
- const avatarKeys: string[] = [];
+ const keys: string[] = [];
let continuationToken: string | undefined;
- const avatarPattern = /avatar-/i;
try {
do {
@@ -230,21 +237,26 @@ export default class S3Storage implements StaticStorage {
const contents = response.Contents ?? [];
for (const obj of contents) {
- if (obj.Key && avatarPattern.test(obj.Key)) {
- avatarKeys.push(obj.Key);
- if (avatarKeys.length >= maxKeys) {
- break;
+ if (obj.Key) {
+ // If pattern is provided, test against it; otherwise include all keys
+ if (!pattern || pattern.test(obj.Key)) {
+ keys.push(obj.Key);
+ if (keys.length >= maxKeys) {
+ break;
+ }
}
}
}
continuationToken = response.NextContinuationToken;
- } while (continuationToken && avatarKeys.length < maxKeys);
+ } while (continuationToken && keys.length < maxKeys);
- logger.info(`Found ${avatarKeys.length} avatar keys`);
- return avatarKeys;
+ const patternInfo = pattern ? ` matching pattern ${pattern.source}` : '';
+ logger.info(`Found ${keys.length} keys${patternInfo}`);
+ return keys;
} catch (error) {
- logger.error(`Error listing avatar keys: `, error);
+ const patternInfo = pattern ? ` matching pattern ${pattern.source}` : '';
+ logger.error(`Error listing keys${patternInfo}: `, error);
throw error;
}
}