Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory viewer prod - remove await for new memories #84

Merged
merged 5 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading