Skip to content

Commit

Permalink
implement chat summary with llama
Browse files Browse the repository at this point in the history
  • Loading branch information
deleolajide committed Oct 18, 2024
1 parent c784f47 commit dbb7a51
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
69 changes: 67 additions & 2 deletions docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,10 +1038,11 @@ function setupPadeRoot() {
_converse.api.listen.on('getMessageActionButtons', (el, buttons) => {

if (el.model.get('body')) {
buttons.push({'i18n_text': __('Pin'), 'handler': ev => handlePinAction(el), 'button_class': 'chat-msg__action-pin', 'icon_class': 'fa fa-paperclip', 'name': 'pade-pin'});
buttons.push({'i18n_text': __('Pin'), 'handler': ev => handlePinAction(el), 'button_class': 'chat-msg__action-pin', 'icon_class': 'fa fa-paperclip', 'name': 'pade-pin'});
buttons.push({'i18n_text': __('Reply'), 'handler': ev => handleReplyAction(el), 'button_class': 'chat-msg__action-reply', 'icon_class': 'fas fa-arrow-left', 'name': 'pade-reply'});
buttons.push({'i18n_text': __('Like'), 'handler': ev => handleReactionAction(el.model, ':smiley:'), 'button_class': 'chat-msg__action-thumbsup', 'icon_class': 'fa fa-check', 'name': 'pade-thumbsup'});
buttons.push({'i18n_text': __('Dislike'), 'handler': ev => handleReactionAction(el.model, ':disappointed:'), 'button_class': 'chat-msg__action-thumbsdownp', 'icon_class': 'fa fa-times', 'name': 'pade-thumbsdown'});
buttons.push({'i18n_text': __('Summarize'), 'handler': ev => handleSummarizeAction(el), 'button_class': 'chat-msg__action-summarize', 'icon_class': 'fa fa-info-circle', 'name': 'pade-summarize'});
}
return buttons;
});
Expand Down Expand Up @@ -1703,6 +1704,71 @@ function handleReplyAction(el) {
replyChat(el.model, prefix + ' : ' + selectedText);
}

function handleSummarizeAction(el) {
const timestamp = el.model.get('time');
const timeAgo = timeago.format(new Date(timestamp));
const msgId = el.model.get('msgid');
const messages = el.model.collection.models;
console.debug('handleSummarizeAction', el.model, timestamp, timeAgo, msgId, messages);

let firstMsg = 0;
const msg = document.querySelector('[data-msgid="' + msgId + '"]');

for (let i=0; i<messages.length; i++)
{
if (messages[i].get("msgid") == msgId) {
console.debug("start message collection", i);
firstMsg = i;
break;
}
}

let detail = "";

for (var i=firstMsg; i<messages.length; i++) {
const body = messages[i].get('message');
const from = messages[i].get('from');
const pretty_from = messages[i].get('type') === "groupchat" ? from.split("/")[1] : from.split("@")[0];

if (body && !body.startsWith('>')) {
detail = detail + pretty_from + ": " + body + "\n";
}
}

const llama = _converse.roster.findWhere({'jid': "llama@pade.chat"});

if (llama) {
const llamaJid = getSetting("llamaAddress", "llama@" + _converse.connection.domain) + "/" + llama.presence.resources.models[0].get('name');
const promptText = "summarize this:" + "\n\n" + detail;
console.debug("handleSummarizeAction summary text\n", promptText, llamaJid);

const stanza = $iq({'to': llamaJid, 'from': _converse.connection.jid, 'type': 'get'}).c('query', { 'xmlns': "urn:xmpp:gen-ai:0"}).t(promptText);

_converse.connection.sendIQ(stanza, function(iq) {
const response = iq.querySelector('response')?.innerHTML;
console.debug("handleSummarizeAction response", response, iq);
injectMessage(el.model.chatbox, "Summary from " + timeAgo, response);

}, function(error){
console.error("handleSummarizeAction error", error);
});
}
}

function injectMessage(model, title, body) {
const msgId = 'inject-' + Math.random().toString(36).substr(2,9);
const type = model.get("type") == "chatbox" ? "chat" : "groupchat";
const from = model.get("jid");

let attrs = {message: body, body, id: msgId, msgId, type, from: _converse.jid};

if (type == "groupchat") {
attrs = {message: body, body, id: msgId, msgId, type, from_muc: from, from: from + '/' + title, nick: title};
}

model.queueMessage(attrs);
}

function handlePinAction(el) {
const msgId = el.model.get('msgid');
const message = el.model.get('message');
Expand Down Expand Up @@ -1780,7 +1846,6 @@ function getSelectedChatBox() {
return view;
}


function replyInverseChat(text) {
var box = getSelectedChatBox();

Expand Down
2 changes: 1 addition & 1 deletion docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"short_name": "__MSG_manifest_shortExtensionName__",
"name": "__MSG_manifest_extensionName__",
"description": "__MSG_manifest_extensionDescription__",
"version": "2.1.11",
"version": "2.1.12",
"homepage_url": "https://igniterealtime.github.io/openfire-pade-plugin/help/",
"manifest_version": 3,
"permissions": [
Expand Down
8 changes: 8 additions & 0 deletions docs/options/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ this.manifest = {
"type": "checkbox",
"label": i18n.get("Open auto-start windows minimized")
},
{
"tab": i18n.get("General"),
"group": i18n.get("Preferences"),
"name": "llamaAddress",
"type": "text",
"text": i18n.get("Enter xmpp address for LLaMA AI Assistant"),
"label": i18n.get("LLaMA Address")
},
{
"tab": i18n.get("General"),
"group": i18n.get("Fastpath (Workgroups)"),
Expand Down
1 change: 1 addition & 0 deletions docs/options/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ function doDefaults(background)
setDefaultSetting("renderMedia", true);
setDefaultSetting("notifyAllMessages", true);
setDefaultSetting("useUpDownCursorKeys", true);
setDefaultSetting("llamaAddress", "llama@pade.chat");

// config
setDefaultSetting("startBitrate", 800);
Expand Down

0 comments on commit dbb7a51

Please sign in to comment.