mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2026-06-15 23:20:32 +03:00
more wip for browser summarization
This commit is contained in:
@@ -13,6 +13,13 @@ self.addEventListener('push', function (event) {
|
||||
self.registration.showNotification(data.title || 'AnythingLLM', {
|
||||
body: data.message,
|
||||
icon: '/favicon.png',
|
||||
...data,
|
||||
data: { ...data }
|
||||
});
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', function (event) {
|
||||
event.notification.close();
|
||||
const { onClickUrl = null } = event.notification.data || {};
|
||||
if (!onClickUrl) return;
|
||||
event.waitUntil(clients.openWindow(onClickUrl));
|
||||
});
|
||||
@@ -1,20 +1,59 @@
|
||||
const { log, conclude } = require('./helpers/index.js');
|
||||
const webpush = require('web-push');
|
||||
const { pushNotificationService } = require('../utils/PushNotifications');
|
||||
const { ApiChatHandler } = require('../utils/chats/apiChatHandler');
|
||||
const { Workspace } = require('../models/workspace');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
// Seen success with gemma3:1B with this format (limited testing)
|
||||
function makeTitlePrompt(response) {
|
||||
return `Generate a short title for a browser notification title based on the following content. Do not provide options or other text. Limit to 3-5 words:\n\n${response.textResponse}`;
|
||||
}
|
||||
|
||||
function makeDescriptionPrompt(response) {
|
||||
return `Summarize the following content into a browser notification message string - do not provide options or other text - just the content should be returned:\n\n${response.textResponse}`;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
console.log('Testing push notification service...');
|
||||
const subscriptions = pushNotificationService.subscriptions;
|
||||
console.log(`Found ${subscriptions.length} subscriptions`);
|
||||
const pushService = pushNotificationService.pushService;
|
||||
if (!pushService) throw new Error('Failed to get push service');
|
||||
|
||||
for (const subscription of subscriptions) {
|
||||
console.log(subscription);
|
||||
webpush.sendNotification(subscription, JSON.stringify({ title: 'Local Push', message: 'Hello, world!' }))
|
||||
.catch(err => {
|
||||
console.error('Failed to send notification', err);
|
||||
});
|
||||
const subscription = pushNotificationService.subscriptions?.[0];
|
||||
if (!subscription) throw new Error('No subscription found');
|
||||
|
||||
const workspace = (await Workspace.where({}))[0]; // tmp
|
||||
const message = '@agent open news.ycombinator.com and get me the latest news that might be interesting to read. I am specifically interested in the latest news about open source projects or AI related news.';
|
||||
|
||||
const chatPayload = {
|
||||
workspace,
|
||||
message,
|
||||
mode: "chat",
|
||||
user: null,
|
||||
sessionId: uuidv4(),
|
||||
}
|
||||
const response = await ApiChatHandler.chatSync(chatPayload);
|
||||
const titleResponse = await ApiChatHandler.chatSync({
|
||||
workspace,
|
||||
message: makeTitlePrompt(response),
|
||||
mode: "chat",
|
||||
user: null,
|
||||
sessionId: uuidv4(),
|
||||
}).then(r => r.textResponse);
|
||||
const descriptionResponse = await ApiChatHandler.chatSync({
|
||||
workspace,
|
||||
message: makeDescriptionPrompt(response),
|
||||
mode: "chat",
|
||||
user: null,
|
||||
sessionId: uuidv4(),
|
||||
}).then(r => r.textResponse);
|
||||
|
||||
await pushService.sendNotification(subscription, JSON.stringify({
|
||||
title: titleResponse,
|
||||
message: descriptionResponse,
|
||||
// onClickUrl: 'https://github.com/mintplex-labs/anything-llm'
|
||||
}));
|
||||
console.log('Successfully sent notification');
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
log(`errored with ${e.message}`)
|
||||
|
||||
@@ -27,6 +27,11 @@ class BackgroundService {
|
||||
const { DocumentSyncQueue } = require("../../models/documentSyncQueue");
|
||||
this.#documentSyncEnabled = await DocumentSyncQueue.enabled();
|
||||
|
||||
if (!this.jobs().length) {
|
||||
this.#log("No jobs to run, schedule, or queue!");
|
||||
return;
|
||||
}
|
||||
|
||||
this.#log("Starting...");
|
||||
this.bree = new Bree({
|
||||
logger: this.logger,
|
||||
@@ -61,10 +66,11 @@ class BackgroundService {
|
||||
interval: "1hr",
|
||||
},
|
||||
] : []),
|
||||
{
|
||||
name: "push-test",
|
||||
interval: "10s",
|
||||
},
|
||||
// {
|
||||
// name: "push-test",
|
||||
// // interval: "1m",
|
||||
// timeout: "10s",
|
||||
// },
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
class PushNotifications {
|
||||
static mailTo = 'anythingllm@localhost';
|
||||
/**
|
||||
* @type {PushNotifications}
|
||||
*/
|
||||
@@ -32,6 +33,22 @@ class PushNotifications {
|
||||
console.log(`\x1b[36m[PushNotifications]\x1b[0m ${text}`, ...args);
|
||||
}
|
||||
|
||||
get pushService() {
|
||||
try {
|
||||
const vapidKeys = this.existingVapidKeys;
|
||||
if (!vapidKeys.publicKey || !vapidKeys.privateKey) throw new Error('VAPID keys not found. Make sure they are generated in the main process first.');
|
||||
webpush.setVapidDetails(
|
||||
`mailto:${this.mailTo}`,
|
||||
vapidKeys.publicKey,
|
||||
vapidKeys.privateKey
|
||||
);
|
||||
return webpush;
|
||||
} catch (e) {
|
||||
console.error('Failed to set VAPID details', e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
get storagePath() {
|
||||
return process.env.NODE_ENV === "development"
|
||||
? path.resolve(__dirname, `../../storage`, 'push-notifications')
|
||||
@@ -85,11 +102,7 @@ class PushNotifications {
|
||||
const instance = PushNotifications.instance;
|
||||
const existingVapidKeys = instance.existingVapidKeys;
|
||||
if (existingVapidKeys.publicKey && existingVapidKeys.privateKey) {
|
||||
webpush.setVapidDetails(
|
||||
'mailto:anythingllm@localhost',
|
||||
existingVapidKeys.publicKey,
|
||||
existingVapidKeys.privateKey
|
||||
);
|
||||
instance.pushService;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -101,11 +114,7 @@ class PushNotifications {
|
||||
if (!fs.existsSync(instance.storagePath)) fs.mkdirSync(instance.storagePath, { recursive: true });
|
||||
fs.writeFileSync(path.resolve(instance.storagePath, `vapid-keys.json`), JSON.stringify(vapidKeys, null, 2));
|
||||
|
||||
webpush.setVapidDetails(
|
||||
'mailto:anythingllm@localhost',
|
||||
vapidKeys.publicKey,
|
||||
vapidKeys.privateKey
|
||||
);
|
||||
instance.pushService;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user