Skip to content

Commit

Permalink
Added custom prompt to apply to cell, row and table.
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Dec 18, 2023
1 parent 6f219f9 commit 49291c0
Show file tree
Hide file tree
Showing 29 changed files with 489 additions and 67 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
8 changes: 4 additions & 4 deletions content/lib/jquery-ui/1.13.2/jquery-ui.min.css

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions content/lib/jquery-ui/1.13.2/jquery-ui.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions content/lib/jquery-ui/1.13.2/jquery-ui.structure.min.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions content/lib/jquery-ui/1.13.2/jquery-ui.theme.min.css

Large diffs are not rendered by default.

173 changes: 141 additions & 32 deletions content/notes/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,37 @@ Menus = {

if(Zotero.ZeNotes.Prefs.getb("bard-api-key")!="")
{
items_ai["paraphrase-bard"] = {name: "Paraphrase annotation (Bard)", icon: "fa-b"};
items_ai["custom-prompt-bard"] = {name: "Custom prompt (Bard)", icon: "fa-b"};
items_ai["paraphrase-annotation"] = {
name: "Paraphrase annotation",
icon: "fa-repeat",
items: {
"paraphrase-bard": {name: "Using bard", icon: "fa-b"},
}
}

items_ai["custom-prompt-on-cell"] = {
name: "Run custom prompt on cell",
icon: "fa-square",
items: {
"custom-prompt-cell-bard": {name: "Using bard", icon: "fa-b"},
}
}

items_ai["custom-prompt-on-row"] = {
name: "Run custom prompt on row",
icon: "fa-arrow-right",
items: {
"custom-prompt-row-bard": {name: "Using bard", icon: "fa-b"},
}
}

items_ai["custom-prompt-on-table"] = {
name: "Run custom prompt on table",
icon: "fa-table",
items: {
"custom-prompt-table-bard": {name: "Using bard", icon: "fa-b"},
}
}
items_ai["sep-ai-01"] = "---------";
}

Expand Down Expand Up @@ -215,7 +244,7 @@ Menus = {
var annotationpagelabel = td.dataset.pagelabel;
var annotationid = td.dataset.annotationid;
var annotationdomid = td.dataset.annotationdomid;
var filekey = td.dataset.filekey;
var notekey = td.dataset.notekey;

Expand All @@ -239,9 +268,9 @@ Menus = {
}
else if(key=="edit")
{
if(notekey=="")
if(!notekey)
{
this.createnote(itemkey, column);
this.createnote(itemkey, column);
}
else
{
Expand All @@ -258,6 +287,11 @@ Menus = {

var annotation = Zotero.Items.get(annotationid);
var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}


var html = document.createElement("div");
html.style = "width:100%; padding: 0.5em;"
Expand Down Expand Up @@ -335,51 +369,100 @@ Menus = {
});
}

else if(key=="custom-prompt-bard")
else if(key=="summarize-row-annotations")
{
// var data = Table.rowdata(td.closest("tr"));
var data = Table.tabledata(td.closest("tr"));
Zotero.ZeNotes.Ai.Bard.batchsummarize(data).then(r=>{
var div = document.createElement("div");
div.innerHTML = r;
Dialog.open(div, function(){}, "Summary", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
});
});
}

else if(key.includes("custom-prompt-"))
{
var customprompt = Zotero.ZeNotes.Prefs.get("bard-custom-prompt");
if(!annotationkey)
{
alert("Annotation not found!");
return;
}
var annotation = Zotero.Items.get(annotationid);

var target = "cell";
data = {
"Direct quote": annotation["annotationText"],
}

if(key.includes("-row"))
{
target = "row";
data = Table.rowdata(td.closest("tr"));

}
else if(key.includes("-table"))
{
target = "table";
data = Table.tabledata(td.closest("tr"));
}



if(Zotero.ZeNotes.Prefs.getb("bard-api-key")=="")
{
alert("Please set API key first.\nGo to ZeNotes > Settings > General Settings > AI API settings");
return;
}

var annotation = Zotero.Items.get(annotationid);
var customprompt = Zotero.ZeNotes.Prefs.get(target+"-custom-prompt");

var currentcomment = annotation.annotationComment;

if(currentcomment==null)
{
currentcomment = "";
}
Zotero.ZeNotes.Ai.Bard.customprompt(annotation["annotationText"]).then(r=>{
var table = AiUi.createdialog(annotation, currentcomment, r, "bard");
var model = Zotero.ZeNotes.Prefs.get("bard-model");

var div = document.createElement("div");
div.innerHTML = "<h2>Custom prompt</h2> "+customprompt+"<br/><br/>"+annotation["annotationText"]+"<br/><br/>"
div.appendChild(table);

Dialog.open(div, function(){}, "Edit and choose a candidate [Bard: "+model+"]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
if(key.includes("-bard"))
{
Zotero.ZeNotes.Ai.Bard.customprompt(JSON.stringify(data), target).then(r=>{
var table = AiUi.createdialog(annotation, currentcomment, r, "bard");
var model = Zotero.ZeNotes.Prefs.get("bard-model");

var div = document.createElement("div");
div.innerHTML = "<h2>Custom prompt</h2> "+customprompt+"<hr/>"+this.displayjson(data);

TabbedDialog.open(table, div, function(){}, "Edit and choose a candidate [Bard: "+model+"]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
});
});
});
}
}



else if(key=="paraphrase-bard")
{
if(!annotationkey)
Expand Down Expand Up @@ -545,7 +628,7 @@ Menus = {
// alert(key);
}
},

async addrow()
{
var io = { dataIn: { search: '', name }, dataOut: null, singleSelection: true};
Expand Down Expand Up @@ -592,7 +675,8 @@ Menus = {

opennote(itemID, col, parentKey)
{
var pane = Zotero.getActiveZoteroPane();

var pane = Zotero.getActiveZoteroPane();
if (!pane.canEdit()) {
pane.displayCannotEditLibraryMessage();
return;
Expand Down Expand Up @@ -630,7 +714,7 @@ Menus = {

createnote(itemid, column)
{
var note = new Zotero.Item('note');
var note = new Zotero.Item('note');
note.setNote("&lt;&lt;"+column+"&gt;&gt;<br/>New note");
note.parentKey = itemid;
note.addTag(column);
Expand Down Expand Up @@ -727,7 +811,32 @@ Menus = {
}
}
});
}
},

displayjson(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
json = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'json-number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'json-key';
} else {
cls = 'json-string';
}
} else if (/true|false/.test(match)) {
cls = 'json-boolean';
} else if (/null/.test(match)) {
cls = 'json-null';
}
return '<span class="' + cls + '">' + match + '</span>';
});

json = json.split("\n").join("<br/>").split(" ").join("&#160;&#160;");
return json;
}
}

window.addEventListener("load", function(){
Expand Down
14 changes: 14 additions & 0 deletions content/notes/notes.css
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,18 @@ body {
margin: 0.2em;
cursor: pointer;
border-radius: 0.1em;
}

.json-key {
color: red;
}
.json-number {
color: blue;
}
.json-string {
color: green;
}
.json-null {
color: blue;
font-weight: bold;
}
26 changes: 14 additions & 12 deletions content/notes/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Notes = {
var trh = document.createElement("tr");
var tdh = document.createElement("td");
table.appendChild(trh);
trh.className = "zn-data-row";
trs[c] = trh;
Notes.innerHTML(tdh, c);
tdh.className = "context-menu-header draggable-header";
Expand Down Expand Up @@ -184,12 +185,12 @@ Notes = {

td.querySelectorAll(".annotation").forEach(a=>{
a.addEventListener("mouseover", function(e){
e.target.parentNode.dataset.annotationid = e.target.dataset.annotationid;
e.target.parentNode.dataset.attachmentid = e.target.dataset.attachmentid;
e.target.parentNode.dataset.attachmentkey = e.target.dataset.attachmentkey;
e.target.parentNode.dataset.annotationpage = e.target.dataset.annotationpage;
e.target.parentNode.dataset.annotationkey = e.target.dataset.annotationkey;
e.target.parentNode.dataset.annotationdomid = e.target.id;
e.target.closest("td").dataset.annotationid = e.target.dataset.annotationid;
e.target.closest("td").dataset.attachmentid = e.target.dataset.attachmentid;
e.target.closest("td").dataset.attachmentkey = e.target.dataset.attachmentkey;
e.target.closest("td").dataset.annotationpage = e.target.dataset.annotationpage;
e.target.closest("td").dataset.annotationkey = e.target.dataset.annotationkey;
e.target.closest("td").dataset.annotationdomid = e.target.id;
});
});

Expand Down Expand Up @@ -252,6 +253,7 @@ Notes = {

items.forEach(v=>{
var tr = document.createElement("tr");
tr.className = "zn-data-row";
table.appendChild(tr);
columns.forEach(c=>{
let td = document.createElement("td");
Expand Down Expand Up @@ -292,12 +294,12 @@ Notes = {

td.querySelectorAll(".annotation").forEach(a=>{
a.addEventListener("mouseover", function(e){
e.target.parentNode.dataset.annotationid = e.target.dataset.annotationid;
e.target.parentNode.dataset.attachmentid = e.target.dataset.attachmentid;
e.target.parentNode.dataset.attachmentkey = e.target.dataset.attachmentkey;
e.target.parentNode.dataset.annotationpage = e.target.dataset.annotationpage;
e.target.parentNode.dataset.annotationkey = e.target.dataset.annotationkey;
e.target.parentNode.dataset.annotationdomid = e.target.id;
e.target.closest("td").dataset.annotationid = e.currentTarget.dataset.annotationid;
e.target.closest("td").dataset.attachmentid = e.currentTarget.dataset.attachmentid;
e.target.closest("td").dataset.attachmentkey = e.currentTarget.dataset.attachmentkey;
e.target.closest("td").dataset.annotationpage = e.currentTarget.dataset.annotationpage;
e.target.closest("td").dataset.annotationkey = e.currentTarget.dataset.annotationkey;
e.target.closest("td").dataset.annotationdomid = e.currentTarget.id;
});
});
td.style.minWidth = hsize+"px";
Expand Down
3 changes: 3 additions & 0 deletions content/notes/notes.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

<script src="zotero.js"></script>
<script src="io.js"></script>
<script src="table.js"></script>
<script src="find.js"></script>
<script src="menus.js"></script>
<script src="notes.js"></script>
<script src="../ui/ai-ui.js"></script>
<script src="../ui/tabbed-dialog.js"></script>
<script src="../ui/dialog.js"></script>

<script src="../settings/zntable.js"></script>
<script src="../settings/languages.js"></script>

Expand Down
Loading

0 comments on commit 49291c0

Please sign in to comment.