@@ -8,6 +8,19 @@ export class StorachaService {
88 // Persistent local cache directory for simulated memories when Storacha space is missing.
99 private static STORAGE_DIR = path . join ( process . cwd ( ) , '.agent_storage' ) ;
1010
11+ // Cached Storacha client (singleton to avoid re-creating on every call)
12+ private static _cachedClient : any = null ;
13+
14+ /**
15+ * Returns a cached Storacha client instance.
16+ */
17+ private static async getClient ( ) {
18+ if ( ! StorachaService . _cachedClient ) {
19+ StorachaService . _cachedClient = await create ( ) ;
20+ }
21+ return StorachaService . _cachedClient ;
22+ }
23+
1124 /**
1225 * Initializes the local storage directory.
1326 */
@@ -53,7 +66,7 @@ export class StorachaService {
5366 await StorachaService . ensureCacheDir ( ) ;
5467 try {
5568 return await StorachaService . withRetry ( async ( ) => {
56- const client = await create ( ) ;
69+ const client = await StorachaService . getClient ( ) ;
5770 const blob = new Blob ( [ JSON . stringify ( memory ) ] , { type : 'application/json' } ) ;
5871 const files = [ new File ( [ blob ] , 'memory.json' ) ] ;
5972
@@ -120,7 +133,7 @@ export class StorachaService {
120133 * @returns The CID where the delegation token is stored.
121134 */
122135 static async publishDelegation ( delegationBytes : Uint8Array ) : Promise < string > {
123- const client = await create ( ) ;
136+ const client = await StorachaService . getClient ( ) ;
124137
125138 const blob = new Blob ( [ delegationBytes as any ] , { type : 'application/vnd.ipld.car' } ) ;
126139 const files = [ new File ( [ blob ] , 'delegation.car' ) ] ;
@@ -162,7 +175,7 @@ export class StorachaService {
162175 * @returns The CID of the uploaded content.
163176 */
164177 static async uploadRaw ( data : Uint8Array , filename : string , mimeType : string = 'application/octet-stream' ) : Promise < string > {
165- const client = await create ( ) ;
178+ const client = await StorachaService . getClient ( ) ;
166179
167180 const blob = new Blob ( [ data as any ] , { type : mimeType } ) ;
168181 const files = [ new File ( [ blob ] , filename ) ] ;
0 commit comments