Skip to content

Commit

Permalink
Merge pull request #84 from autonomys/memory-viewer-prod
Browse files Browse the repository at this point in the history
Memory viewer prod - remove await for new memories
  • Loading branch information
Xm0onh authored Dec 24, 2024
2 parents 9487153 + 748171e commit f471f31
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
34 changes: 18 additions & 16 deletions auto-kol/agent-memory-viewer/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@ process.on('unhandledRejection', async (reason, promise) => {
async function main() {
try {
await initialize();
await resurrection();
resurrection();

const memoryWatcher = watchMemoryHashUpdates(async (agent, cid) => {
try {
logger.info('New memory hash detected', { agent, cid });
const memory = await downloadMemory(cid);
if (memory) {
const savedMemory = await saveMemoryRecord(cid, memory, memory?.previousCid);
logger.info('Memory processed successfully', {
logger.info('New memory hash detected', { agent, cid });

downloadMemory(cid)
.then(async memory => {
if (memory) {
const savedMemory = await saveMemoryRecord(cid, memory, memory?.previousCid);
logger.info('Memory processed successfully', {
cid,
isNew: savedMemory.created_at === savedMemory.created_at
});
}
})
.catch(error => {
logger.error('Error processing memory update', {
error,
agent,
cid,
isNew: savedMemory.created_at === savedMemory.created_at
errorType: error instanceof Error ? error.constructor.name : typeof error
});
}
} catch (error) {
logger.error('Error processing memory update', {
error,
agent,
cid,
errorType: error instanceof Error ? error.constructor.name : typeof error
});
}
});

createWebSocketServer();
Expand Down
4 changes: 2 additions & 2 deletions auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createLogger } from './logger.js';

const logger = createLogger('dsn');

const MAX_RETRIES = 10;
const MAX_RETRIES = 15;
const INITIAL_RETRY_DELAY = 10000;

async function delay(ms: number) {
Expand Down Expand Up @@ -70,4 +70,4 @@ export async function downloadMemory(cid: string, retryCount = 0): Promise<any>
});
throw error;
}
}
}
41 changes: 32 additions & 9 deletions auto-kol/agent-memory-viewer/backend/src/utils/resurrection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ const logger = createLogger('resurrection');
export async function resurrection() {
logger.info('Starting resurrection');
let hash = await getLastMemoryHash();

processResurrection(hash).catch(error => {
logger.error('Error during resurrection:', error);
});
}

async function processResurrection(startHash: string) {
const memories: { hash: string; data: any }[] = [];
let hash = startHash;

while (true) {
const existingMemory = await getMemoryByCid(hash);
Expand All @@ -17,19 +25,34 @@ export async function resurrection() {
break;
}

const memory = await downloadMemory(hash);
if (!memory) break;

memories.push({ hash, data: memory });
hash = memory?.previousCid;

if (!hash) break;
try {
const memory = await downloadMemory(hash);
if (!memory) break;

memories.push({ hash, data: memory });
hash = memory?.previousCid;

if (!hash) break;
} catch (error) {
logger.error('Failed to download memory during resurrection', {
cid: hash,
error
});
break;
}
}

for (let i = memories.length - 1; i >= 0; i--) {
const { hash, data } = memories[i];
await saveMemoryRecord(hash, data, data?.previousCid);
logger.info('Saved memory during resurrection', { cid: hash });
try {
await saveMemoryRecord(hash, data, data?.previousCid);
logger.info('Saved memory during resurrection', { cid: hash });
} catch (error) {
logger.error('Failed to save memory during resurrection', {
cid: hash,
error
});
}
}

logger.info('Resurrection complete', { memoriesProcessed: memories.length });
Expand Down

0 comments on commit f471f31

Please sign in to comment.