Skip to content

Commit

Permalink
Improved ai interface
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Dec 10, 2023
1 parent 4f418c3 commit e40a82b
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 23 deletions.
16 changes: 14 additions & 2 deletions content/notes/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,20 @@ Menus = {
currentcomment = "";
}
Zotero.ZeNotes.Ai.Bard.paraphrase(annotation["annotationText"]).then(r=>{
annotation.annotationComment = currentcomment+"\n\n<b>[Paraphrase]</b>\n"+r+"\n";
annotation.saveTx({skipSelect:true}).then(e=>{Zotero.ZeNotes.Ui.reload();});
var table = AiUi.createdialog(r);
Dialog.open(table, function(){}, "Choose paraprahse", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
});
});
}
else if(key=="showfile")
Expand Down
29 changes: 23 additions & 6 deletions content/notes/notes.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ body {
outline: none;
}

table {
#notes-table {
border-spacing: 0;
border-collapse: separate;
border-spacing: 0;
Expand All @@ -71,14 +71,14 @@ table {
position: relative;
}

td {
#notes-table td {
padding:0.3em;
border: solid 1px;
vertical-align: top;
cursor: pointer;
}

tr:first-child td {
#notes-table tr:first-child td {
background-color: white;
padding:0.3em;
border: solid 1px;
Expand All @@ -88,11 +88,11 @@ tr:first-child td {
font-weight: bolder;
}

tr:first-child td:first-letter, td:first-child:first-letter {
#notes-table tr:first-child td:first-letter, td:first-child:first-letter {
text-transform: capitalize;
}

td:first-child {
#notes-table td:first-child {
background-color: white;
padding:0.3em;
border: solid 1px;
Expand All @@ -102,7 +102,7 @@ td:first-child {
font-weight: bolder;
}

tr:first-child > td:first-child {
#notes-table tr:first-child > td:first-child {
z-index: 1!important;
}

Expand Down Expand Up @@ -184,4 +184,21 @@ tr:first-child > td:first-child {

.draggable-header {
cursor: pointer;
}

#zn-bard-table {

}

#zn-bard-table td{
border-bottom: solid 1px;
padding: 0.6em;
}

.zn-button {
border: solid 1px;
padding: 0.2em;
margin: 0.2em;
cursor: pointer;
border-radius: 0.1em;
}
2 changes: 2 additions & 0 deletions content/notes/notes.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<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/dialog.js"></script>
<script src="../settings/zntable.js"></script>

</head>
Expand Down
85 changes: 85 additions & 0 deletions content/ui/ai-ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
AiUi = {
editcandidate(id){
var c = document.getElementById(id);
c.contentEditable = "true";
c.style.backgroundColor = "#efedff";
c.focus();
},
savecandidate(id)
{
var c = document.getElementById(id);
c.contentEditable = "false";
c.style.backgroundColor = "";
},
acceptcandidate(id, currentcomment, annotation)
{
var c = document.getElementById(id);
var comment = c.innerText;
annotation.annotationComment = currentcomment+"\n\n<b>[Paraphrase]</b>\n"+comment+"\n";
annotation.saveTx({skipSelect:true}).then(e=>{
Zotero.ZeNotes.Ui.reload();
});
},
createdialog(candidates)
{
var table = document.createElement("table");
table.id = "zn-bard-table";
var th = document.createElement("tr");
table.appendChild(th);
for(h of ["Candidate", "Content", ""])
{
let td = document.createElement("td");
td.innerHTML = h;
th.appendChild(td);
}

var i = 0;
for(line of candidates)
{
let tr = document.createElement("tr");
table.appendChild(tr);


let id = document.createElement("td");
id.innerHTML = i+1;
tr.appendChild(id);

let candidate = document.createElement("td");

candidate.id = "bard-paraphrase-"+i;
candidate.addEventListener("blur", function(e){
AiUi.savecandidate(e.target.id);
})

tr.appendChild(candidate);
candidate.innerHTML = line;

let buttons = document.createElement("td");
tr.appendChild(buttons);

let edit = document.createElement("i");
edit.className = "fas fa-pen zn-button";
edit.dataset.candidateid = "bard-paraphrase-"+i;
edit.addEventListener("click", function(e){
AiUi.editcandidate(e.target.dataset.candidateid);
});

buttons.appendChild(edit);

let accept = document.createElement("i");
accept.className = "fas fa-check zn-button";
accept.dataset.candidateid = "bard-paraphrase-"+i;
accept.title = "Use this candidate."
accept.addEventListener("click", function(e){
if(!confirm("Do you want to use this paraprase?"))
{
return;
}
AiUi.acceptcandidate(e.target.dataset.candidateid, currentcomment, annotation);
});
buttons.appendChild(accept);
i+=1;
}
return table;
}
}
99 changes: 99 additions & 0 deletions content/ui/dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
Depends on jquery-ui
*/

Dialog = {
init()
{
this.create();
},

reload(html)
{
this.contents.innerHTML = html;
},
create()
{
this.dialog = document.createElement("div");
this.contents = document.createElement("div");
document.body.appendChild(this.dialog);
this.dialog.appendChild(this.contents);
this.dialog.id = "main-dialog";
this.dialog.title = "Coding dialog";
this.contents.style = "height: 100%; width: 100%;";
this.contents.innerHTML = ``;
},
resize(width=500, height=500)
{
$("#main-dialog").dialog( "option", "width", width );
$("#main-dialog").dialog( "option", "height", height);
},
close()
{
$("#main-dialog").dialog("close");
},

accept()
{
var buttons = $('#main-dialog').dialog('option', 'buttons');
buttons["OK"]();
},

open(html, callback=null, title="", buttons=null){
this.contents.innerHTML = "";
if(typeof html==="string")
{
this.contents.innerHTML = html;
}
else
{
this.contents.appendChild(html);
}

if(title!="")
{
this.dialog.title = title;
}

if(buttons==null)
{
buttons = {
Cancel: function()
{
$("#main-dialog").dialog( "close" );
},
OK: function() {
$("#main-dialog").dialog( "close" );
if(callback!=null)
{
callback();
}
}
}
}
else if(buttons=="close")
{
buttons = {
Close: function(){
$("#main-dialog").dialog( "close" );
if(callback!=null)
{
callback();
}
}
}
}

$("#main-dialog").dialog({
height: "auto",
width: 750,
height: 500,
modal: true,
buttons: buttons,
});
},
}

window.addEventListener("load", function(){
Dialog.init();
})
20 changes: 13 additions & 7 deletions core/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,34 @@ Ai={
})
.then(data => {
try {
return Promise.resolve(data.candidates[0].output);
return Promise.resolve(data.candidates.map(function(v){return v.output}));
}
catch {
return Promise.resolve("");
catch(e) {
return Promise.resolve(["Error: "+e]);
}
}).catch(e=>{
return Promise.reject("error: "+e);
return Promise.reject(["Error: "+e]);
});
}
}

Ai.Bard = {

async translate(sentence)
{
var prompts = "Translate the following sentence.";
return this.sendprompt(sentence, prompts)
},

async summarize(sentence, ratio=1/4)
{
var prompts = "Summarize the following in about "+Math.round(sentence.split(" ").length*ratio)+" words:"
alert(prompts);
return this.sendprompt(sentence, prompts);
},

async paraphrase(sentence)
{
var prompts = "Paraphrase the passage below. Tell with an academic writing tone. Do not add additional explanation."
var prompts = "Paraphrase the following:"
return this.sendprompt(sentence, prompts)
},

Expand All @@ -43,7 +49,7 @@ Ai.Bard = {
"text": p,
},
"temperature": 1.0,
"candidate_count": 1
"candidate_count": 3
}

var options = {
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>[email protected]</em:id>
<em:name>ZeNotes</em:name>
<em:version>0.5.1</em:version>
<em:version>0.5.2</em:version>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
<em:updateURL>https://raw.githubusercontent.com/frianasoa/zenotes/main/zenote-update.json</em:updateURL>
<em:homepageURL>https://github.com/frianasoa/zenotes</em:homepageURL>
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Ze Notes",
"version": "0.5.1",
"version": "0.5.2",
"description": "Advanced notes manager",
"homepage_url": "https://github.com/frianasoa/zenotes",
"author": "Fanantenana Rianasoa Andriariniaina",
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"[email protected]": {
"updates": [
{
"version": "0.5.1",
"update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.5.1/zenotes-v0.5.1.xpi",
"update_hash": "sha256:981f84678d6651853137369980b244f193ec464899e0fb6236f82c993dd676fc",
"version": "0.5.2",
"update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.5.2/zenotes-v0.5.2.xpi",
"update_hash": "sha256:8dec1a1fdba6ba5c511366139e29dcf23ab88fccb63437cd723752faba165914",
"applications": {
"gecko": {
"strict_min_version": "60.0"
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<ns1:version>0.5.1</ns1:version>
<ns1:version>0.5.2</ns1:version>
<ns1:targetApplication>
<rdf:Description>
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>5.0.0</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.5.1/zenotes-v0.5.1.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.5.2/zenotes-v0.5.2.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand All @@ -20,7 +20,7 @@
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>4.999</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.5.1/zenotes-v0.5.1.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.5.2/zenotes-v0.5.2.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand Down

0 comments on commit e40a82b

Please sign in to comment.