Skip to content

Commit

Permalink
feat: refresh AI cache on proposal start
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Apr 2, 2024
1 parent 0559c22 commit 9d44738
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib/ai/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fetchProposal, Proposal } from '../../helpers/snapshot';
import { IStorage } from '../storage/types';
import Cache from '../cache';

const tempCacheIds: Map<string, number> = new Map();
const tempCacheIds = new Map<string, number>();

class Summary extends Cache {
proposal?: Proposal | null;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ai/textToSpeech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IStorage } from '../storage/types';

const MIN_BODY_LENGTH = 500;
const MAX_BODY_LENGTH = 4096;
const tempCacheIds: Map<string, number> = new Map();
const tempCacheIds = new Map<string, number>();

export default class TextToSpeech extends Cache {
proposal?: Proposal | null;
Expand Down
10 changes: 9 additions & 1 deletion src/lib/queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sleep } from '../helpers/utils';
import { capture } from '@snapshot-labs/snapshot-sentry';
import Cache from './cache';
import { timeQueueProcess } from './metrics';
import type Cache from './cache';

const queues = new Map<string, Cache>();
const processingItems = new Map<string, Cache>();
Expand All @@ -11,6 +11,14 @@ async function processItem(cacheable: Cache) {
try {
const end = timeQueueProcess.startTimer({ name: cacheable.constructor.name });
processingItems.set(cacheable.id, cacheable);

if (
['Summary', 'TextToSpeech'].includes(cacheable.constructor.name) &&
!!(await cacheable.getCache())
) {
return;
}

await cacheable.createCache();
end();
} catch (e) {
Expand Down
11 changes: 9 additions & 2 deletions src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import express from 'express';
import { rpcError, rpcSuccess, storageEngine } from './helpers/utils';
import { capture } from '@snapshot-labs/snapshot-sentry';
import VotesReport from './lib/votesReport';
import Summary from './lib/ai/summary';
import TextToSpeech from './lib/ai/textToSpeech';
import { queue } from './lib/queue';

const router = express.Router();

function processVotesReport(id: string, event: string) {
function processEvent(id: string, event: string) {
if (event == 'proposal/end') {
queue(new VotesReport(id, storageEngine(process.env.VOTE_REPORT_SUBDIR)));
}

if (event === 'proposal/start') {
queue(new Summary(id, storageEngine(process.env.VOTE_REPORT_SUBDIR)));
queue(new TextToSpeech(id, storageEngine(process.env.VOTE_REPORT_SUBDIR)));
}
}

router.post('/webhook', (req, res) => {
Expand All @@ -27,7 +34,7 @@ router.post('/webhook', (req, res) => {
}

try {
processVotesReport(id, event);
processEvent(id, event);
return rpcSuccess(res, 'Webhook received', id);
} catch (e) {
capture(e, { body });
Expand Down

0 comments on commit 9d44738

Please sign in to comment.