Skip to content

Commit

Permalink
fix style of functions to arrow func
Browse files Browse the repository at this point in the history
  • Loading branch information
Xm0onh committed Jan 17, 2025
1 parent 4f4fa09 commit 7b28007
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 75 deletions.
4 changes: 2 additions & 2 deletions src/agents/tools/utils/dsn/dsnDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface BaseMemory {
[key: string]: unknown;
}

export async function download(cid: string): Promise<BaseMemory> {
export const download = async (cid: string): Promise<BaseMemory> => {
return withRetry(
async () => {
const api = createAutoDriveApi({
Expand Down Expand Up @@ -47,4 +47,4 @@ export async function download(cid: string): Promise<BaseMemory> {
},
},
);
}
};
6 changes: 3 additions & 3 deletions src/agents/tools/utils/dsn/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { createLogger } from '../../../../utils/logger.js';

const logger = createLogger('retry-utility');

export async function withRetry<T>(
export const withRetry = async <T>(
operation: () => Promise<T>,
{
maxRetries = 5,
initialDelayMs = 1000,
operationName = 'Operation',
shouldRetry = (_error: unknown): boolean => true,
} = {},
): Promise<T> {
): Promise<T> => {
const attempt = async (retriesLeft: number, currentDelay: number): Promise<T> => {
try {
return await operation();
Expand All @@ -33,4 +33,4 @@ export async function withRetry<T>(
};

return attempt(maxRetries, initialDelayMs);
}
};
9 changes: 4 additions & 5 deletions src/cli/resurrect.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { join } from 'path';
import { mkdirSync } from 'fs';
import { createLogger } from '../utils/logger.js';
import MemoryResurrector from '../utils/resurrection.js';
import { downloadAllMemories } from '../utils/resurrection.js';

const logger = createLogger('resurrect-cli');

async function main() {
const main = async () => {
try {
const outputDir = join(process.cwd(), 'memories');
mkdirSync(outputDir, { recursive: true });

logger.info('Starting memory resurrection...');
const resurrector = new MemoryResurrector(outputDir);
const result = await resurrector.downloadAllMemories();
const result = await downloadAllMemories(outputDir);

logger.info(
`Memory resurrection complete. Processed: ${result.processed}, Failed: ${result.failed}`,
Expand All @@ -22,6 +21,6 @@ async function main() {
logger.error('Failed to resurrect memories:', error);
process.exit(1);
}
}
};

main();
142 changes: 77 additions & 65 deletions src/utils/resurrection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,95 @@ import { join } from 'path';
const logger = createLogger('memory-resurrector');
const STATE_FILE = join(process.cwd(), 'memories', 'last-processed-cid.json');

class MemoryResurrector {
private failedCids: Set<string> = new Set();
private processedCount: number = 0;

constructor(private outputDir: string) {}

private getLastProcessedCid(): string | null {
try {
if (!existsSync(STATE_FILE)) {
return null;
}
const data = JSON.parse(readFileSync(STATE_FILE, 'utf-8'));
return data.lastProcessedCid;
} catch (error) {
logger.error('Failed to read last processed CID:', error);
const getLastProcessedCid = (): string | null => {
try {
if (!existsSync(STATE_FILE)) {
return null;
}
const data = JSON.parse(readFileSync(STATE_FILE, 'utf-8'));
return data.lastProcessedCid;
} catch (error) {
logger.error('Failed to read last processed CID:', error);
return null;
}

private saveLastProcessedCid(cid: string): void {
try {
writeFileSync(STATE_FILE, JSON.stringify({ lastProcessedCid: cid }, null, 2));
logger.info(`Saved last processed CID: ${cid}`);
} catch (error) {
logger.error('Failed to save last processed CID:', error);
}
};

const saveLastProcessedCid = (cid: string): void => {
try {
writeFileSync(STATE_FILE, JSON.stringify({ lastProcessedCid: cid }, null, 2));
logger.info(`Saved last processed CID: ${cid}`);
} catch (error) {
logger.error('Failed to save last processed CID:', error);
}
};

const fetchMemoryChain = async (
currentCid: string,
stopAtCid: string | null,
outputDir: string,
failedCids: Set<string> = new Set(),
processedCount = 0,
): Promise<{ processedCount: number; failedCids: Set<string> }> => {
if (!currentCid || failedCids.has(currentCid) || currentCid === stopAtCid) {
return { processedCount, failedCids };
}

private async fetchMemoryChain(currentCid: string, stopAtCid: string | null): Promise<void> {
if (!currentCid || this.failedCids.has(currentCid) || currentCid === stopAtCid) {
return;
try {
const content = await download(currentCid);
const filename = `${currentCid}.json`;
const filepath = join(outputDir, filename);
writeFileSync(filepath, JSON.stringify(content, null, 2));

processedCount++;
logger.info(`Successfully fetched and saved memory ${currentCid}`);

if (content.previousCid && content.previousCid !== stopAtCid) {
return fetchMemoryChain(
content.previousCid,
stopAtCid,
outputDir,
failedCids,
processedCount,
);
}
} catch (error) {
logger.error(`Failed to fetch memory ${currentCid}:`, error);
failedCids.add(currentCid);
}

try {
const content = await download(currentCid);

const filename = `${currentCid}.json`;
const filepath = join(this.outputDir, filename);
writeFileSync(filepath, JSON.stringify(content, null, 2));

this.processedCount++;
logger.info(`Successfully fetched and saved memory ${currentCid}`);
return { processedCount, failedCids };
};

if (content.previousCid && content.previousCid !== stopAtCid) {
await this.fetchMemoryChain(content.previousCid, stopAtCid);
}
} catch (error) {
logger.error(`Failed to fetch memory ${currentCid}:`, error);
this.failedCids.add(currentCid);
}
const downloadAllMemories = async (
outputDir: string,
): Promise<{ processed: number; failed: number }> => {
const latestCid = await getLastMemoryCid();
if (!latestCid) {
logger.info('No memories found (empty CID)');
return { processed: 0, failed: 0 };
}

async downloadAllMemories(): Promise<{ processed: number; failed: number }> {
const latestCid = await getLastMemoryCid();
if (!latestCid) {
logger.info('No memories found (empty CID)');
return { processed: 0, failed: 0 };
}

const lastProcessedCid = this.getLastProcessedCid();
if (lastProcessedCid === latestCid) {
logger.info('Already up to date with latest CID');
return { processed: 0, failed: 0 };
}
const lastProcessedCid = getLastProcessedCid();
if (lastProcessedCid === latestCid) {
logger.info('Already up to date with latest CID');
return { processed: 0, failed: 0 };
}

logger.info(`Starting download from ${latestCid} to ${lastProcessedCid || 'genesis'}`);
await this.fetchMemoryChain(latestCid, lastProcessedCid);
logger.info(`Starting download from ${latestCid} to ${lastProcessedCid || 'genesis'}`);
const { processedCount, failedCids } = await fetchMemoryChain(
latestCid,
lastProcessedCid,
outputDir,
);

this.saveLastProcessedCid(latestCid);
saveLastProcessedCid(latestCid);

logger.info(`Downloaded ${this.processedCount} memories, failed CIDs: ${this.failedCids.size}`);
logger.info(`Downloaded ${processedCount} memories, failed CIDs: ${failedCids.size}`);

return {
processed: this.processedCount,
failed: this.failedCids.size,
};
}
}
return {
processed: processedCount,
failed: failedCids.size,
};
};

export default MemoryResurrector;
export { downloadAllMemories };

0 comments on commit 7b28007

Please sign in to comment.