prosemirror-suggestcat-plugin 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{PluginKey as e,TextSelection as t,Plugin as n}from"prosemirror-state";import{Decoration as o,DecorationSet as s}from"prosemirror-view";import a from"fast-diff";import{__awaiter as i}from"tslib";import r from"lodash.debounce";import{docToTextWithMapping as c,textPosToDocPos as d}from"@emergence-engineering/prosemirror-text-map";import{StepMap as l,Mapping as p}from"prosemirror-transform";import{Fragment as g,Slice as u}from"prosemirror-model";import{getDiff as m,isIdentity as f}from"@emergence-engineering/fast-diff-merge";export{getDiff}from"@emergence-engineering/fast-diff-merge";var h,v,y,x,S,b,w;!function(e){e.suggestionUpdate="suggestionUpdate",e.acceptSuggestion="acceptSuggestion",e.openSuggestion="openSuggestion",e.closeSuggestion="closeSuggestion",e.discardSuggestion="discardSuggestion"}(h||(h={})),function(e){e.grammarSuggestPopup="grammar-suggest-popup"}(v||(v={})),function(e){e.Casual="Casual",e.Confident="Confident",e.Straightforward="Straightforward",e.Friendly="Friendly"}(y||(y={})),function(e){e.English="English",e.Spanish="Spanish",e.French="French",e.German="German",e.Italian="Italian",e.Portuguese="Portuguese",e.Dutch="Dutch",e.Russian="Russian",e.Chinese="Chinese",e.Korean="Korean",e.Japanese="Japanese"}(x||(x={})),function(e){e.Complete="Complete",e.Improve="Improve",e.MakeLonger="MakeLonger",e.MakeShorter="MakeShorter",e.Simplify="Simplify",e.Explain="Explain",e.ActionItems="ActionItems"}(S||(S={})),function(e){e.ChangeTone="ChangeTone",e.Translate="Translate"}(b||(b={})),function(e){e.idle="idle",e.new="new",e.streaming="streaming",e.finished="finished",e.accepted="accepted",e.cancelled="cancelled",e.rejected="rejected",e.done="done",e.error="error"}(w||(w={}));const j=new e("completePlugin"),C=new e("grammarSuggestPlugin"),T=e=>{let t="",n="",o=!1;return e.descendants(((e,s)=>{if(e.type.isBlock){if(!o)return void(o=!0);t+=`${n}\n`,n=""}else e.isText&&(n+=`${e.text}`)})),t+=n,t},M=(e,t)=>{if(e===t)return{start:e.length,end:e.length,oldStart:e.length,oldEnd:e.length,oldText:"",newText:""};const n=a(e,t),o=n[0],s=o[0]===a.EQUAL?o[1].length-1:0,i=((e,t)=>{if(t[t.length-1][0]!==a.EQUAL)return e.length;const n=t.slice(0,t.length-1);let o=0;for(const e of n){const[t,n]=e;t===a.EQUAL&&(o+=n.length),t===a.DELETE&&(o+=n.length)}return o})(e,n),r=e.lastIndexOf("\n",s),c=e.indexOf("\n",i),d=-1===r?0:r+1,l=-1===c?e.length:c,p=e.slice(d,l),g=t.slice(d,t.length-(e.length-l));return{start:d,end:d+g.length,oldStart:d,oldEnd:l,oldText:p,newText:g}},O=(e,t)=>i(void 0,void 0,void 0,(function*(){const n=[...t.split("\n")];return fetch("https://prosemirror-ai-plugin.web.app/api/suggestion",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:n}})}).then((e=>e.ok?e.json():Promise.reject(e))).then((e=>(null==e?void 0:e.length)?{result:e.join("\n"),fixed:!0}:{fixed:!1,result:t})).catch((e=>(e.text().then((t=>{console.error({status:e.status,text:t})})),{fixed:!1,result:t})))})),L=(e,n)=>{var o;const s=null===(o=C.getState(e.state))||void 0===o?void 0:o.decorations.find(0,e.state.doc.nodeSize,(e=>e.id===n.spec.id))[0];if(!s)return;const{text:a}=s.spec,{from:i,to:r}=s,c={type:h.acceptSuggestion,id:n.spec.id},d=a.split("\n").map((t=>t?e.state.schema.node("paragraph",null,e.state.schema.text(t||"")):e.state.schema.node("paragraph"))),l=g.fromArray(d);let{tr:p}=e.state;p=p.setSelection(t.create(e.state.doc,i,r)),p.selection.replace(p,new u(l,1,1)),p.setMeta(C,c),e.dispatch(p)},E=(e,t)=>{const{spec:n}=t,o={type:h.discardSuggestion,id:n.id},s=e.state.tr.setMeta(C,o);e.dispatch(s)},k=(e,t)=>{const n=C.getState(e.state);if(!n)return!1;const{decorations:o}=n,s=o.find(t,t)[0];if(!s){const t={type:h.closeSuggestion};return e.dispatch(e.state.tr.setMeta(C,t)),!1}const a=n.popupDecoration.find()[0];if((null==a?void 0:a.spec.id)===s.spec.id)return!1;const i={type:h.openSuggestion,decoration:s};return e.dispatch(e.state.tr.setMeta(C,i)),!1},D={debounceMs:2e3,createUpdatePopup:(e,t,n,o,s)=>{const a=document.createElement("div");a.className="grammar-suggest-tooltip";const{spec:i}=t,r=e.dom.getBoundingClientRect();a.id=v.grammarSuggestPopup;const c=e.coordsAtPos(n),d=e.dom.scrollTop||0,l=e.dom.scrollLeft||0;a.style.left=`${c.left-r.left+l}px`,a.style.top=`${c.bottom-r.top+d+5}px`;const p=document.createElement("div");p.className="grammar-suggest-tooltip-apply",p.innerText=i.text||i.originalText,i.text||(p.style.textDecoration="line-through",p.style.color="red"),p.onclick=()=>{o(e,t)},a.appendChild(p);const g=document.createElement("div");return g.innerHTML="<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'><path d='M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM12 10.5858L14.8284 7.75736L16.2426 9.17157L13.4142 12L16.2426 14.8284L14.8284 16.2426L12 13.4142L9.17157 16.2426L7.75736 14.8284L10.5858 12L7.75736 9.17157L9.17157 7.75736L12 10.5858Z'></path></svg>",g.className="grammar-suggest-tooltip-discard",g.onclick=()=>{s(e,t)},a.appendChild(g),a},withYjs:!1},P={maxSelection:1e3},A=(e,t=D)=>{let a=!1;return new n({key:C,state:{init:()=>({lastText:"",decorations:s.empty,popupDecoration:s.empty}),apply(e,n,a,i){const r=e.getMeta(C);return(null==r?void 0:r.type)===h.suggestionUpdate?((e,t,n)=>{const{changedRegion:s,fix:a,mapping:i,text:r}=t,c=m(s.newText,a.result).filter((e=>!f(e))).filter((e=>e.original!==`${e.replacement}\n`)).map((({from:e,to:t,original:n,replacement:a})=>{const r=d(s.start+e,i),c=d(s.start+(a.endsWith("\n")?t-1:t),i),l={text:a.endsWith("\n")?a.slice(0,-1):a,originalText:n,id:{}};return o.inline(r,c,{class:"grammarSuggestion "+(""===a?"removalSuggestion":"")},l)}));return Object.assign(Object.assign({},e),{decorations:e.decorations.add(n.doc,c),lastText:r})})(n,r,e):(null==r?void 0:r.type)===h.acceptSuggestion?((e,t,n)=>{const o=e.decorations.remove(e.decorations.find(0,n.doc.nodeSize,(e=>e.id===t.id)));return Object.assign(Object.assign({},e),{lastText:T(n.doc),decorations:o.map(n.mapping,n.doc),popupDecoration:s.empty})})(n,r,e):(null==r?void 0:r.type)===h.openSuggestion?((e,t,n,a)=>{const{decoration:i}=t,r={id:i.spec.id};return Object.assign(Object.assign({},e),{popupDecoration:s.create(n.doc,[o.widget(i.from,((e,t)=>{const n=t();return n?a.createUpdatePopup(e,i,n,L,E):document.createElement("div")}),Object.assign(Object.assign({},r),{stopEvent:()=>!0}))])})})(n,r,e,t):(null==r?void 0:r.type)===h.closeSuggestion?Object.assign(Object.assign({},n),{popupDecoration:s.empty}):(null==r?void 0:r.type)===h.discardSuggestion?((e,t,n)=>Object.assign(Object.assign({},e),{decorations:e.decorations.remove(e.decorations.find(0,n.doc.nodeSize,(e=>e.id===t.id))),popupDecoration:s.empty}))(n,r,e):e.docChanged?((e,t,n,o)=>{const s=c(n.doc).text,{text:a,mapping:i}=c(t.doc);if(a===s)return e;let r=t.mapping;if(o){const e=t.doc.content.findDiffStart(n.doc.content),o=n.doc.content.findDiffEnd(t.doc.content),s=new l(o&&e?[e,o.a-e,o.b-e]:[0,0,0]);r=new p([s])}const g=M(s,a),u=e.decorations.map(r,t.doc),m=d(g.start,i),f=d(g.end,i),h=e.popupDecoration.map(r,t.doc);return Object.assign(Object.assign({},e),{decorations:u.remove(u.find(m,f)),popupDecoration:h.remove(h.find(m,f))})})(n,e,a,t.withYjs):n}},props:{handleClick:k,decorations:e=>{const t=C.getState(e);if(!t)return null;const n=j.getState(e);return(null==n?void 0:n.status)!==w.idle?t.decorations:t.decorations.add(e.doc,t.popupDecoration.find())}},view(){const n=((e,t)=>r((e=>{var n;const o=c(e.state.doc),s=(null===(n=C.getState(e.state))||void 0===n?void 0:n.lastText)||"",a=M(s,o.text);O(t,a.newText).then((t=>{if(T(e.state.doc)!==o.text)return;const n={type:h.suggestionUpdate,fix:t,changedRegion:a,mapping:o.mapping,text:o.text};e.dispatch(e.state.tr.setMeta(C,n))})).catch((e=>{console.error("Grammar suggest API error",e)}))}),e.debounceMs))(t,e);return{update(e,t){const o=C.getState(e.state);e.state.doc.textBetween(0,e.state.doc.nodeSize-2,"/n")===t.doc.textBetween(0,t.doc.nodeSize-2,"/n")&&a||!o||o.lastText===T(e.state.doc)||(a=!0,n(e))}}}})},I=(e,t,n,o,s,a,r)=>i(void 0,void 0,void 0,(function*(){var e;let n="";const c=new AbortController;try{const d=null===(e=(yield fetch("https://suggestion-gw5lxik4dq-uc.a.run.app",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:"Bearer -qKivjCv6MfQSmgF438PjEY7RnLfqoVe"},signal:c.signal,body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:[t],task:s,params:r}})})).body)||void 0===e?void 0:e.getReader(),l=({done:e,value:t})=>i(void 0,void 0,void 0,(function*(){var i;if(e)return;if(null===(i=j.getState(o.state))||void 0===i?void 0:i.isCancelled)return void(yield c.abort());const r=(new TextDecoder).decode(t);try{n+=r,o.dispatch(o.state.tr.setMeta(j,Object.assign({type:s,status:w.streaming,result:n},a&&{selection:a})))}catch(e){console.error("Could not parse stream message",r,e)}return null==d?void 0:d.read().then(l)}));yield null==d?void 0:d.read().then(l),o.dispatch(o.state.tr.setMeta(j,Object.assign({type:s,status:w.finished,result:n},a&&{selection:a})))}catch(e){console.error("Error:",e)}})),B=(e,t=P)=>{let o=!1;return new n({key:j,state:{init:()=>({status:w.idle}),apply(e,t,n,s){const a=e.getMeta(j);return t.status===w.done||t.status===w.rejected?(o=!1,{status:w.idle}):(null==a?void 0:a.status)===w.cancelled?Object.assign(Object.assign(Object.assign({},t),a),{isCancelled:!0}):a&&(e=>Object.values(b).includes(e.type)||Object.values(S).includes(e.type))(a)?t.type&&a.type!==t.type?t:Object.assign(Object.assign({},t),a):t}},view:()=>({update(e,n){const s=j.getState(e.state);let a=e.state.tr;if((null==s?void 0:s.status)===w.new&&!o)switch(s.type){case S.Complete:o=!0,((e,t,n)=>{i(void 0,void 0,void 0,(function*(){const{doc:e}=t.state,n=[];e.descendants((e=>{"paragraph"===e.type.name&&n.push(e.textContent)}));let o="";o=n.length>=2?n.slice(-2).join(" "):n.join(" "),I(0,o,0,t,S.Complete)}))})(0,e);break;case S.MakeLonger:case S.MakeShorter:case S.Improve:case S.Simplify:case S.Explain:case S.ActionItems:case b.Translate:case b.ChangeTone:o=!0,((e,t,n,o,s,a)=>{const i=n.state.selection;if(!i)return void n.dispatch(n.state.tr.setMeta(j,{task:e,status:w.done}));if(n.state.doc.textBetween(i.from,i.to).length>s)return void n.dispatch(n.state.tr.setMeta(j,{type:e,status:w.error,error:"Selection is too big"}));const r=n.state.doc.textBetween(i.from,i.to,"\n");I(0,r,0,n,e,i,a)})(s.type,0,e,0,t.maxSelection,s.params)}if((null==s?void 0:s.status)===w.accepted){if(s.error)return a.setMeta(j,{type:s.type,status:w.done}),void e.dispatch(a);switch(s.type){case S.Complete:a=a.insertText(s.result||"",e.state.doc.nodeSize-2),a.setMeta(j,{type:S.Complete,status:w.done}),e.dispatch(a),e.focus();break;case S.MakeLonger:case S.MakeShorter:case S.Improve:case S.Simplify:case S.Explain:case S.ActionItems:case b.Translate:case b.ChangeTone:if(s.selection&&s.result){const t=s.result.split("\n\n").map((t=>e.state.schema.node("paragraph",null,e.state.schema.text(t)))),n=g.fromArray(t);a.selection.replace(a,new u(n,0,0))}a.setMeta(j,{type:s.type,status:w.done}),e.dispatch(a),e.focus()}}}})})};export{v as GrammarSuggestElementClass,h as GrammarSuggestMetaType,y as MoodParamType,b as OpenAiPromptsWithParam,S as OpenAiPromptsWithoutParam,w as Status,x as TranslationTargetLanguage,B as completePlugin,j as completePluginKey,I as completeRequest,P as defaultCompleteOptions,D as defaultOptions,M as getChangedRegions,A as grammarSuggestPlugin,O as suggescatRequest};
|
|
1
|
+
import{PluginKey as e,TextSelection as t,Plugin as n}from"prosemirror-state";import{Decoration as o,DecorationSet as s}from"prosemirror-view";import a from"fast-diff";import{__awaiter as i}from"tslib";import r from"lodash.debounce";import{docToTextWithMapping as c,textPosToDocPos as d}from"@emergence-engineering/prosemirror-text-map";import{StepMap as l,Mapping as p}from"prosemirror-transform";import{Fragment as g,Slice as u}from"prosemirror-model";import{getDiff as m,isIdentity as f}from"@emergence-engineering/fast-diff-merge";export{getDiff}from"@emergence-engineering/fast-diff-merge";var h,v,y,S,x,b,w;!function(e){e.suggestionUpdate="suggestionUpdate",e.acceptSuggestion="acceptSuggestion",e.openSuggestion="openSuggestion",e.closeSuggestion="closeSuggestion",e.discardSuggestion="discardSuggestion"}(h||(h={})),function(e){e.grammarSuggestPopup="grammar-suggest-popup"}(v||(v={})),function(e){e.Casual="Casual",e.Confident="Confident",e.Straightforward="Straightforward",e.Friendly="Friendly"}(y||(y={})),function(e){e.English="English",e.Spanish="Spanish",e.French="French",e.German="German",e.Italian="Italian",e.Portuguese="Portuguese",e.Dutch="Dutch",e.Russian="Russian",e.Chinese="Chinese",e.Korean="Korean",e.Japanese="Japanese"}(S||(S={})),function(e){e.Complete="Complete",e.Improve="Improve",e.MakeLonger="MakeLonger",e.MakeShorter="MakeShorter",e.Simplify="Simplify",e.Explain="Explain",e.ActionItems="ActionItems"}(x||(x={})),function(e){e.ChangeTone="ChangeTone",e.Translate="Translate"}(b||(b={})),function(e){e.idle="idle",e.new="new",e.streaming="streaming",e.finished="finished",e.accepted="accepted",e.cancelled="cancelled",e.rejected="rejected",e.done="done",e.error="error"}(w||(w={}));const j=new e("completePlugin"),C=new e("grammarSuggestPlugin"),T=e=>{let t="",n="",o=!1;return e.descendants(((e,s)=>{if(e.type.isBlock){if(!o)return void(o=!0);t+=`${n}\n`,n=""}else e.isText&&(n+=`${e.text}`)})),t+=n,t},M=(e,t)=>{if(e===t)return{start:e.length,end:e.length,oldStart:e.length,oldEnd:e.length,oldText:"",newText:""};const n=a(e,t),o=n[0],s=o[0]===a.EQUAL?o[1].length-1:0,i=((e,t)=>{if(t[t.length-1][0]!==a.EQUAL)return e.length;const n=t.slice(0,t.length-1);let o=0;for(const e of n){const[t,n]=e;t===a.EQUAL&&(o+=n.length),t===a.DELETE&&(o+=n.length)}return o})(e,n),r=e.lastIndexOf("\n",s),c=e.indexOf("\n",i),d=-1===r?0:r+1,l=-1===c?e.length:c,p=e.slice(d,l),g=t.slice(d,t.length-(e.length-l));return{start:d,end:d+g.length,oldStart:d,oldEnd:l,oldText:p,newText:g}},O=(e,t)=>i(void 0,void 0,void 0,(function*(){const n=[...t.split("\n")];return fetch("https://prosemirror-ai-plugin.web.app/api/suggestion",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:n}})}).then((e=>e.ok?e.json():Promise.reject(e))).then((e=>(null==e?void 0:e.length)?{result:e.join("\n"),fixed:!0}:{fixed:!1,result:t})).catch((e=>(e.text().then((t=>{console.error({status:e.status,text:t})})),{fixed:!1,result:t})))})),L=(e,n)=>{var o;const s=null===(o=C.getState(e.state))||void 0===o?void 0:o.decorations.find(0,e.state.doc.nodeSize,(e=>e.id===n.spec.id))[0];if(!s)return;const{text:a}=s.spec,{from:i,to:r}=s,c={type:h.acceptSuggestion,id:n.spec.id},d=a.split("\n").map((t=>t?e.state.schema.node("paragraph",null,e.state.schema.text(t||"")):e.state.schema.node("paragraph"))),l=g.fromArray(d);let{tr:p}=e.state;p=p.setSelection(t.create(e.state.doc,i,r)),p.selection.replace(p,new u(l,1,1)),p.setMeta(C,c),e.dispatch(p)},E=(e,t)=>{const{spec:n}=t,o={type:h.discardSuggestion,id:n.id},s=e.state.tr.setMeta(C,o);e.dispatch(s)},k=(e,t)=>{const n=C.getState(e.state);if(!n)return!1;const{decorations:o}=n,s=o.find(t,t)[0];if(!s){const t={type:h.closeSuggestion};return e.dispatch(e.state.tr.setMeta(C,t)),!1}const a=n.popupDecoration.find()[0];if((null==a?void 0:a.spec.id)===s.spec.id)return!1;const i={type:h.openSuggestion,decoration:s};return e.dispatch(e.state.tr.setMeta(C,i)),!1},D={debounceMs:2e3,createUpdatePopup:(e,t,n,o,s)=>{const a=document.createElement("div");a.className="grammar-suggest-tooltip";const{spec:i}=t,r=e.dom.getBoundingClientRect();a.id=v.grammarSuggestPopup;const c=e.coordsAtPos(n),d=e.dom.scrollTop||0,l=e.dom.scrollLeft||0;a.style.left=`${c.left-r.left+l}px`,a.style.top=`${c.bottom-r.top+d+5}px`;const p=document.createElement("div");p.className="grammar-suggest-tooltip-apply",p.innerText=i.text||i.originalText,i.text||(p.style.textDecoration="line-through",p.style.color="red"),p.onclick=()=>{o(e,t)},a.appendChild(p);const g=document.createElement("div");return g.innerHTML="<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'><path d='M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM12 10.5858L14.8284 7.75736L16.2426 9.17157L13.4142 12L16.2426 14.8284L14.8284 16.2426L12 13.4142L9.17157 16.2426L7.75736 14.8284L10.5858 12L7.75736 9.17157L9.17157 7.75736L12 10.5858Z'></path></svg>",g.className="grammar-suggest-tooltip-discard",g.onclick=()=>{s(e,t)},a.appendChild(g),a},withYjs:!1},P={maxSelection:1e3},A=(e,t=D)=>{let a=!1;return new n({key:C,state:{init:()=>({lastText:"",decorations:s.empty,popupDecoration:s.empty}),apply(e,n,a,i){const r=e.getMeta(C);return(null==r?void 0:r.type)===h.suggestionUpdate?((e,t,n)=>{const{changedRegion:s,fix:a,mapping:i,text:r}=t,c=m(s.newText,a.result).filter((e=>!f(e))).filter((e=>e.original!==`${e.replacement}\n`)).map((({from:e,to:t,original:n,replacement:a})=>{const r=d(s.start+e,i),c=d(s.start+(a.endsWith("\n")?t-1:t),i),l={text:a.endsWith("\n")?a.slice(0,-1):a,originalText:n,id:{}};return o.inline(r,c,{class:"grammarSuggestion "+(""===a?"removalSuggestion":"")},l)}));return Object.assign(Object.assign({},e),{decorations:e.decorations.add(n.doc,c),lastText:r})})(n,r,e):(null==r?void 0:r.type)===h.acceptSuggestion?((e,t,n)=>{const o=e.decorations.remove(e.decorations.find(0,n.doc.nodeSize,(e=>e.id===t.id)));return Object.assign(Object.assign({},e),{lastText:T(n.doc),decorations:o.map(n.mapping,n.doc),popupDecoration:s.empty})})(n,r,e):(null==r?void 0:r.type)===h.openSuggestion?((e,t,n,a)=>{const{decoration:i}=t,r={id:i.spec.id};return Object.assign(Object.assign({},e),{popupDecoration:s.create(n.doc,[o.widget(i.from,((e,t)=>{const n=t();return n?a.createUpdatePopup(e,i,n,L,E):document.createElement("div")}),Object.assign(Object.assign({},r),{stopEvent:()=>!0}))])})})(n,r,e,t):(null==r?void 0:r.type)===h.closeSuggestion?Object.assign(Object.assign({},n),{popupDecoration:s.empty}):(null==r?void 0:r.type)===h.discardSuggestion?((e,t,n)=>Object.assign(Object.assign({},e),{decorations:e.decorations.remove(e.decorations.find(0,n.doc.nodeSize,(e=>e.id===t.id))),popupDecoration:s.empty}))(n,r,e):e.docChanged?((e,t,n,o)=>{const s=c(n.doc).text,{text:a,mapping:i}=c(t.doc);if(a===s)return e;let r=t.mapping;if(o){const e=t.doc.content.findDiffStart(n.doc.content),o=n.doc.content.findDiffEnd(t.doc.content),s=new l(o&&e?[e,o.a-e,o.b-e]:[0,0,0]);r=new p([s])}const g=M(s,a),u=e.decorations.map(r,t.doc),m=d(g.start,i),f=d(g.end,i),h=e.popupDecoration.map(r,t.doc);return Object.assign(Object.assign({},e),{decorations:u.remove(u.find(m,f)),popupDecoration:h.remove(h.find(m,f))})})(n,e,a,t.withYjs):n}},props:{handleClick:k,decorations:e=>{const t=C.getState(e);if(!t)return null;const n=j.getState(e);return(null==n?void 0:n.status)!==w.idle?t.decorations:t.decorations.add(e.doc,t.popupDecoration.find())}},view(){const n=((e,t)=>r((e=>{var n;const o=c(e.state.doc),s=(null===(n=C.getState(e.state))||void 0===n?void 0:n.lastText)||"",a=M(s,o.text);O(t,a.newText).then((t=>{if(T(e.state.doc)!==o.text)return;const n={type:h.suggestionUpdate,fix:t,changedRegion:a,mapping:o.mapping,text:o.text};e.dispatch(e.state.tr.setMeta(C,n))})).catch((e=>{console.error("Grammar suggest API error",e)}))}),e.debounceMs))(t,e);return{update(e,t){const o=C.getState(e.state);e.state.doc.textBetween(0,e.state.doc.nodeSize-2,"/n")===t.doc.textBetween(0,t.doc.nodeSize-2,"/n")&&a||!o||o.lastText===T(e.state.doc)||(a=!0,n(e))}}}})},I=(e,t,n,o,s,a,r)=>i(void 0,void 0,void 0,(function*(){var e;let n="";const c=new AbortController;try{const d=null===(e=(yield fetch("https://suggestion-gw5lxik4dq-uc.a.run.app",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:"Bearer -qKivjCv6MfQSmgF438PjEY7RnLfqoVe"},signal:c.signal,body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:[t],task:s,params:r}})})).body)||void 0===e?void 0:e.getReader(),l=({done:e,value:t})=>i(void 0,void 0,void 0,(function*(){var i;if(e)return;if(null===(i=j.getState(o.state))||void 0===i?void 0:i.isCancelled)return void(yield c.abort());const r=(new TextDecoder).decode(t);try{n+=r,o.dispatch(o.state.tr.setMeta(j,Object.assign({type:s,status:w.streaming,result:n},a&&{selection:a})))}catch(e){console.error("Could not parse stream message",r,e)}return null==d?void 0:d.read().then(l)}));yield null==d?void 0:d.read().then(l),o.dispatch(o.state.tr.setMeta(j,Object.assign({type:s,status:w.finished,result:n},a&&{selection:a})))}catch(e){console.error("Error:",e)}})),B=(e,t=P)=>{let o=!1;return new n({key:j,state:{init:()=>({status:w.idle}),apply(e,t,n,s){const a=e.getMeta(j);return console.log("complatePluginMeta",{meta:a,pluginState:t,state:s}),t.status===w.done||t.status===w.rejected?(o=!1,{status:w.idle}):(null==a?void 0:a.status)===w.cancelled?Object.assign(Object.assign(Object.assign({},t),a),{isCancelled:!0}):a&&(e=>Object.values(b).includes(e.type)||Object.values(x).includes(e.type))(a)?t.type&&a.type!==t.type?t:Object.assign(Object.assign({},t),a):t}},view:()=>({update(e,n){const s=j.getState(e.state);console.log({pluginState:s});let a=e.state.tr;if((null==s?void 0:s.status)===w.new&&!o)switch(s.type){case x.Complete:o=!0,((e,t,n)=>{i(void 0,void 0,void 0,(function*(){const{doc:e}=t.state,n=[];e.descendants((e=>{"paragraph"===e.type.name&&n.push(e.textContent)}));let o="";o=n.length>=2?n.slice(-2).join(" "):n.join(" "),I(0,o,0,t,x.Complete)}))})(0,e);break;case x.MakeLonger:case x.MakeShorter:case x.Improve:case x.Simplify:case x.Explain:case x.ActionItems:case b.Translate:case b.ChangeTone:o=!0,((e,t,n,o,s,a)=>{const i=n.state.selection;if(!i)return void n.dispatch(n.state.tr.setMeta(j,{task:e,status:w.done}));if(n.state.doc.textBetween(i.from,i.to).length>s)return void n.dispatch(n.state.tr.setMeta(j,{type:e,status:w.error,error:"Selection is too big"}));const r=n.state.doc.textBetween(i.from,i.to,"\n");I(0,r,0,n,e,i,a)})(s.type,0,e,0,t.maxSelection,s.params)}if((null==s?void 0:s.status)===w.accepted){if(s.error)return a.setMeta(j,{type:s.type,status:w.done}),void e.dispatch(a);switch(s.type){case x.Complete:a=a.insertText(s.result||"",e.state.doc.nodeSize-2),a.setMeta(j,{type:x.Complete,status:w.done}),e.dispatch(a),e.focus();break;case x.MakeLonger:case x.MakeShorter:case x.Improve:case x.Simplify:case x.Explain:case x.ActionItems:case b.Translate:case b.ChangeTone:if(s.selection&&s.result){const t=s.result.split("\n\n").map((t=>e.state.schema.node("paragraph",null,e.state.schema.text(t)))),n=g.fromArray(t);a.selection.replace(a,new u(n,0,0))}a.setMeta(j,{type:s.type,status:w.done}),e.dispatch(a),e.focus()}}}})})};export{v as GrammarSuggestElementClass,h as GrammarSuggestMetaType,y as MoodParamType,b as OpenAiPromptsWithParam,x as OpenAiPromptsWithoutParam,w as Status,S as TranslationTargetLanguage,B as completePlugin,j as completePluginKey,I as completeRequest,P as defaultCompleteOptions,D as defaultOptions,M as getChangedRegions,A as grammarSuggestPlugin,O as suggescatRequest};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,t,o,s,a,r,n=require("prosemirror-state"),i=require("prosemirror-view"),p=require("fast-diff"),c=require("tslib"),d=require("lodash.debounce"),l=require("@emergence-engineering/prosemirror-text-map"),u=require("prosemirror-transform"),g=require("prosemirror-model"),m=require("@emergence-engineering/fast-diff-merge");exports.GrammarSuggestMetaType=void 0,(e=exports.GrammarSuggestMetaType||(exports.GrammarSuggestMetaType={})).suggestionUpdate="suggestionUpdate",e.acceptSuggestion="acceptSuggestion",e.openSuggestion="openSuggestion",e.closeSuggestion="closeSuggestion",e.discardSuggestion="discardSuggestion",exports.GrammarSuggestElementClass=void 0,(exports.GrammarSuggestElementClass||(exports.GrammarSuggestElementClass={})).grammarSuggestPopup="grammar-suggest-popup",exports.MoodParamType=void 0,(t=exports.MoodParamType||(exports.MoodParamType={})).Casual="Casual",t.Confident="Confident",t.Straightforward="Straightforward",t.Friendly="Friendly",exports.TranslationTargetLanguage=void 0,(o=exports.TranslationTargetLanguage||(exports.TranslationTargetLanguage={})).English="English",o.Spanish="Spanish",o.French="French",o.German="German",o.Italian="Italian",o.Portuguese="Portuguese",o.Dutch="Dutch",o.Russian="Russian",o.Chinese="Chinese",o.Korean="Korean",o.Japanese="Japanese",exports.OpenAiPromptsWithoutParam=void 0,(s=exports.OpenAiPromptsWithoutParam||(exports.OpenAiPromptsWithoutParam={})).Complete="Complete",s.Improve="Improve",s.MakeLonger="MakeLonger",s.MakeShorter="MakeShorter",s.Simplify="Simplify",s.Explain="Explain",s.ActionItems="ActionItems",exports.OpenAiPromptsWithParam=void 0,(a=exports.OpenAiPromptsWithParam||(exports.OpenAiPromptsWithParam={})).ChangeTone="ChangeTone",a.Translate="Translate",exports.Status=void 0,(r=exports.Status||(exports.Status={})).idle="idle",r.new="new",r.streaming="streaming",r.finished="finished",r.accepted="accepted",r.cancelled="cancelled",r.rejected="rejected",r.done="done",r.error="error";const x=new n.PluginKey("completePlugin"),h=new n.PluginKey("grammarSuggestPlugin"),S=e=>{let t="",o="",s=!1;return e.descendants(((e,a)=>{if(e.type.isBlock){if(!s)return void(s=!0);t+=`${o}\n`,o=""}else e.isText&&(o+=`${e.text}`)})),t+=o,t},f=(e,t)=>{if(e===t)return{start:e.length,end:e.length,oldStart:e.length,oldEnd:e.length,oldText:"",newText:""};const o=p(e,t),s=o[0],a=s[0]===p.EQUAL?s[1].length-1:0,r=((e,t)=>{if(t[t.length-1][0]!==p.EQUAL)return e.length;const o=t.slice(0,t.length-1);let s=0;for(const e of o){const[t,o]=e;t===p.EQUAL&&(s+=o.length),t===p.DELETE&&(s+=o.length)}return s})(e,o),n=e.lastIndexOf("\n",a),i=e.indexOf("\n",r),c=-1===n?0:n+1,d=-1===i?e.length:i,l=e.slice(c,d),u=t.slice(c,t.length-(e.length-d));return{start:c,end:c+u.length,oldStart:c,oldEnd:d,oldText:l,newText:u}},P=(e,t)=>c.__awaiter(void 0,void 0,void 0,(function*(){const o=[...t.split("\n")];return fetch("https://prosemirror-ai-plugin.web.app/api/suggestion",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:o}})}).then((e=>e.ok?e.json():Promise.reject(e))).then((e=>(null==e?void 0:e.length)?{result:e.join("\n"),fixed:!0}:{fixed:!1,result:t})).catch((e=>(e.text().then((t=>{console.error({status:e.status,text:t})})),{fixed:!1,result:t})))})),y=(e,t)=>{var o;const s=null===(o=h.getState(e.state))||void 0===o?void 0:o.decorations.find(0,e.state.doc.nodeSize,(e=>e.id===t.spec.id))[0];if(!s)return;const{text:a}=s.spec,{from:r,to:i}=s,p={type:exports.GrammarSuggestMetaType.acceptSuggestion,id:t.spec.id},c=a.split("\n").map((t=>t?e.state.schema.node("paragraph",null,e.state.schema.text(t||"")):e.state.schema.node("paragraph"))),d=g.Fragment.fromArray(c);let{tr:l}=e.state;l=l.setSelection(n.TextSelection.create(e.state.doc,r,i)),l.selection.replace(l,new g.Slice(d,1,1)),l.setMeta(h,p),e.dispatch(l)},v=(e,t)=>{const{spec:o}=t,s={type:exports.GrammarSuggestMetaType.discardSuggestion,id:o.id},a=e.state.tr.setMeta(h,s);e.dispatch(a)},T=(e,t)=>{const o=h.getState(e.state);if(!o)return!1;const{decorations:s}=o,a=s.find(t,t)[0];if(!a){const t={type:exports.GrammarSuggestMetaType.closeSuggestion};return e.dispatch(e.state.tr.setMeta(h,t)),!1}const r=o.popupDecoration.find()[0];if((null==r?void 0:r.spec.id)===a.spec.id)return!1;const n={type:exports.GrammarSuggestMetaType.openSuggestion,decoration:a};return e.dispatch(e.state.tr.setMeta(h,n)),!1},O={debounceMs:2e3,createUpdatePopup:(e,t,o,s,a)=>{const r=document.createElement("div");r.className="grammar-suggest-tooltip";const{spec:n}=t,i=e.dom.getBoundingClientRect();r.id=exports.GrammarSuggestElementClass.grammarSuggestPopup;const p=e.coordsAtPos(o),c=e.dom.scrollTop||0,d=e.dom.scrollLeft||0;r.style.left=`${p.left-i.left+d}px`,r.style.top=`${p.bottom-i.top+c+5}px`;const l=document.createElement("div");l.className="grammar-suggest-tooltip-apply",l.innerText=n.text||n.originalText,n.text||(l.style.textDecoration="line-through",l.style.color="red"),l.onclick=()=>{s(e,t)},r.appendChild(l);const u=document.createElement("div");return u.innerHTML="<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'><path d='M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM12 10.5858L14.8284 7.75736L16.2426 9.17157L13.4142 12L16.2426 14.8284L14.8284 16.2426L12 13.4142L9.17157 16.2426L7.75736 14.8284L10.5858 12L7.75736 9.17157L9.17157 7.75736L12 10.5858Z'></path></svg>",u.className="grammar-suggest-tooltip-discard",u.onclick=()=>{a(e,t)},r.appendChild(u),r},withYjs:!1},M={maxSelection:1e3},w=(e,t,o,s,a,r,n)=>c.__awaiter(void 0,void 0,void 0,(function*(){var e;let o="";const i=new AbortController;try{const p=null===(e=(yield fetch("https://suggestion-gw5lxik4dq-uc.a.run.app",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:"Bearer -qKivjCv6MfQSmgF438PjEY7RnLfqoVe"},signal:i.signal,body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:[t],task:a,params:n}})})).body)||void 0===e?void 0:e.getReader(),d=({done:e,value:t})=>c.__awaiter(void 0,void 0,void 0,(function*(){var n;if(e)return;if(null===(n=x.getState(s.state))||void 0===n?void 0:n.isCancelled)return void(yield i.abort());const c=(new TextDecoder).decode(t);try{o+=c,s.dispatch(s.state.tr.setMeta(x,Object.assign({type:a,status:exports.Status.streaming,result:o},r&&{selection:r})))}catch(e){console.error("Could not parse stream message",c,e)}return null==p?void 0:p.read().then(d)}));yield null==p?void 0:p.read().then(d),s.dispatch(s.state.tr.setMeta(x,Object.assign({type:a,status:exports.Status.finished,result:o},r&&{selection:r})))}catch(e){console.error("Error:",e)}}));Object.defineProperty(exports,"getDiff",{enumerable:!0,get:function(){return m.getDiff}}),exports.completePlugin=(e,t=M)=>{let o=!1;return new n.Plugin({key:x,state:{init:()=>({status:exports.Status.idle}),apply(e,t,s,a){const r=e.getMeta(x);return t.status===exports.Status.done||t.status===exports.Status.rejected?(o=!1,{status:exports.Status.idle}):(null==r?void 0:r.status)===exports.Status.cancelled?Object.assign(Object.assign(Object.assign({},t),r),{isCancelled:!0}):r&&(e=>Object.values(exports.OpenAiPromptsWithParam).includes(e.type)||Object.values(exports.OpenAiPromptsWithoutParam).includes(e.type))(r)?t.type&&r.type!==t.type?t:Object.assign(Object.assign({},t),r):t}},view:()=>({update(e,s){const a=x.getState(e.state);let r=e.state.tr;if((null==a?void 0:a.status)===exports.Status.new&&!o)switch(a.type){case exports.OpenAiPromptsWithoutParam.Complete:o=!0,((e,t,o)=>{c.__awaiter(void 0,void 0,void 0,(function*(){const{doc:e}=t.state,o=[];e.descendants((e=>{"paragraph"===e.type.name&&o.push(e.textContent)}));let s="";s=o.length>=2?o.slice(-2).join(" "):o.join(" "),w(0,s,0,t,exports.OpenAiPromptsWithoutParam.Complete)}))})(0,e);break;case exports.OpenAiPromptsWithoutParam.MakeLonger:case exports.OpenAiPromptsWithoutParam.MakeShorter:case exports.OpenAiPromptsWithoutParam.Improve:case exports.OpenAiPromptsWithoutParam.Simplify:case exports.OpenAiPromptsWithoutParam.Explain:case exports.OpenAiPromptsWithoutParam.ActionItems:case exports.OpenAiPromptsWithParam.Translate:case exports.OpenAiPromptsWithParam.ChangeTone:o=!0,((e,t,o,s,a,r)=>{const n=o.state.selection;if(!n)return void o.dispatch(o.state.tr.setMeta(x,{task:e,status:exports.Status.done}));if(o.state.doc.textBetween(n.from,n.to).length>a)return void o.dispatch(o.state.tr.setMeta(x,{type:e,status:exports.Status.error,error:"Selection is too big"}));const i=o.state.doc.textBetween(n.from,n.to,"\n");w(0,i,0,o,e,n,r)})(a.type,0,e,0,t.maxSelection,a.params)}if((null==a?void 0:a.status)===exports.Status.accepted){if(a.error)return r.setMeta(x,{type:a.type,status:exports.Status.done}),void e.dispatch(r);switch(a.type){case exports.OpenAiPromptsWithoutParam.Complete:r=r.insertText(a.result||"",e.state.doc.nodeSize-2),r.setMeta(x,{type:exports.OpenAiPromptsWithoutParam.Complete,status:exports.Status.done}),e.dispatch(r),e.focus();break;case exports.OpenAiPromptsWithoutParam.MakeLonger:case exports.OpenAiPromptsWithoutParam.MakeShorter:case exports.OpenAiPromptsWithoutParam.Improve:case exports.OpenAiPromptsWithoutParam.Simplify:case exports.OpenAiPromptsWithoutParam.Explain:case exports.OpenAiPromptsWithoutParam.ActionItems:case exports.OpenAiPromptsWithParam.Translate:case exports.OpenAiPromptsWithParam.ChangeTone:if(a.selection&&a.result){const t=a.result.split("\n\n").map((t=>e.state.schema.node("paragraph",null,e.state.schema.text(t)))),o=g.Fragment.fromArray(t);r.selection.replace(r,new g.Slice(o,0,0))}r.setMeta(x,{type:a.type,status:exports.Status.done}),e.dispatch(r),e.focus()}}}})})},exports.completePluginKey=x,exports.completeRequest=w,exports.defaultCompleteOptions=M,exports.defaultOptions=O,exports.getChangedRegions=f,exports.grammarSuggestPlugin=(e,t=O)=>{let o=!1;return new n.Plugin({key:h,state:{init:()=>({lastText:"",decorations:i.DecorationSet.empty,popupDecoration:i.DecorationSet.empty}),apply(e,o,s,a){const r=e.getMeta(h);return(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.suggestionUpdate?((e,t,o)=>{const{changedRegion:s,fix:a,mapping:r,text:n}=t,p=m.getDiff(s.newText,a.result).filter((e=>!m.isIdentity(e))).filter((e=>e.original!==`${e.replacement}\n`)).map((({from:e,to:t,original:o,replacement:a})=>{const n=l.textPosToDocPos(s.start+e,r),p=l.textPosToDocPos(s.start+(a.endsWith("\n")?t-1:t),r),c={text:a.endsWith("\n")?a.slice(0,-1):a,originalText:o,id:{}};return i.Decoration.inline(n,p,{class:"grammarSuggestion "+(""===a?"removalSuggestion":"")},c)}));return Object.assign(Object.assign({},e),{decorations:e.decorations.add(o.doc,p),lastText:n})})(o,r,e):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.acceptSuggestion?((e,t,o)=>{const s=e.decorations.remove(e.decorations.find(0,o.doc.nodeSize,(e=>e.id===t.id)));return Object.assign(Object.assign({},e),{lastText:S(o.doc),decorations:s.map(o.mapping,o.doc),popupDecoration:i.DecorationSet.empty})})(o,r,e):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.openSuggestion?((e,t,o,s)=>{const{decoration:a}=t,r={id:a.spec.id};return Object.assign(Object.assign({},e),{popupDecoration:i.DecorationSet.create(o.doc,[i.Decoration.widget(a.from,((e,t)=>{const o=t();return o?s.createUpdatePopup(e,a,o,y,v):document.createElement("div")}),Object.assign(Object.assign({},r),{stopEvent:()=>!0}))])})})(o,r,e,t):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.closeSuggestion?Object.assign(Object.assign({},o),{popupDecoration:i.DecorationSet.empty}):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.discardSuggestion?((e,t,o)=>Object.assign(Object.assign({},e),{decorations:e.decorations.remove(e.decorations.find(0,o.doc.nodeSize,(e=>e.id===t.id))),popupDecoration:i.DecorationSet.empty}))(o,r,e):e.docChanged?((e,t,o,s)=>{const a=l.docToTextWithMapping(o.doc).text,{text:r,mapping:n}=l.docToTextWithMapping(t.doc);if(r===a)return e;let i=t.mapping;if(s){const e=t.doc.content.findDiffStart(o.doc.content),s=o.doc.content.findDiffEnd(t.doc.content),a=s&&e?new u.StepMap([e,s.a-e,s.b-e]):new u.StepMap([0,0,0]);i=new u.Mapping([a])}const p=f(a,r),c=e.decorations.map(i,t.doc),d=l.textPosToDocPos(p.start,n),g=l.textPosToDocPos(p.end,n),m=e.popupDecoration.map(i,t.doc);return Object.assign(Object.assign({},e),{decorations:c.remove(c.find(d,g)),popupDecoration:m.remove(m.find(d,g))})})(o,e,s,t.withYjs):o}},props:{handleClick:T,decorations:e=>{const t=h.getState(e);if(!t)return null;const o=x.getState(e);return(null==o?void 0:o.status)!==exports.Status.idle?t.decorations:t.decorations.add(e.doc,t.popupDecoration.find())}},view(){const s=((e,t)=>d((e=>{var o;const s=l.docToTextWithMapping(e.state.doc),a=(null===(o=h.getState(e.state))||void 0===o?void 0:o.lastText)||"",r=f(a,s.text);P(t,r.newText).then((t=>{if(S(e.state.doc)!==s.text)return;const o={type:exports.GrammarSuggestMetaType.suggestionUpdate,fix:t,changedRegion:r,mapping:s.mapping,text:s.text};e.dispatch(e.state.tr.setMeta(h,o))})).catch((e=>{console.error("Grammar suggest API error",e)}))}),e.debounceMs))(t,e);return{update(e,t){const a=h.getState(e.state);e.state.doc.textBetween(0,e.state.doc.nodeSize-2,"/n")===t.doc.textBetween(0,t.doc.nodeSize-2,"/n")&&o||!a||a.lastText===S(e.state.doc)||(o=!0,s(e))}}}})},exports.suggescatRequest=P;
|
|
1
|
+
"use strict";var e,t,o,s,a,r,n=require("prosemirror-state"),i=require("prosemirror-view"),p=require("fast-diff"),c=require("tslib"),d=require("lodash.debounce"),l=require("@emergence-engineering/prosemirror-text-map"),g=require("prosemirror-transform"),u=require("prosemirror-model"),m=require("@emergence-engineering/fast-diff-merge");exports.GrammarSuggestMetaType=void 0,(e=exports.GrammarSuggestMetaType||(exports.GrammarSuggestMetaType={})).suggestionUpdate="suggestionUpdate",e.acceptSuggestion="acceptSuggestion",e.openSuggestion="openSuggestion",e.closeSuggestion="closeSuggestion",e.discardSuggestion="discardSuggestion",exports.GrammarSuggestElementClass=void 0,(exports.GrammarSuggestElementClass||(exports.GrammarSuggestElementClass={})).grammarSuggestPopup="grammar-suggest-popup",exports.MoodParamType=void 0,(t=exports.MoodParamType||(exports.MoodParamType={})).Casual="Casual",t.Confident="Confident",t.Straightforward="Straightforward",t.Friendly="Friendly",exports.TranslationTargetLanguage=void 0,(o=exports.TranslationTargetLanguage||(exports.TranslationTargetLanguage={})).English="English",o.Spanish="Spanish",o.French="French",o.German="German",o.Italian="Italian",o.Portuguese="Portuguese",o.Dutch="Dutch",o.Russian="Russian",o.Chinese="Chinese",o.Korean="Korean",o.Japanese="Japanese",exports.OpenAiPromptsWithoutParam=void 0,(s=exports.OpenAiPromptsWithoutParam||(exports.OpenAiPromptsWithoutParam={})).Complete="Complete",s.Improve="Improve",s.MakeLonger="MakeLonger",s.MakeShorter="MakeShorter",s.Simplify="Simplify",s.Explain="Explain",s.ActionItems="ActionItems",exports.OpenAiPromptsWithParam=void 0,(a=exports.OpenAiPromptsWithParam||(exports.OpenAiPromptsWithParam={})).ChangeTone="ChangeTone",a.Translate="Translate",exports.Status=void 0,(r=exports.Status||(exports.Status={})).idle="idle",r.new="new",r.streaming="streaming",r.finished="finished",r.accepted="accepted",r.cancelled="cancelled",r.rejected="rejected",r.done="done",r.error="error";const x=new n.PluginKey("completePlugin"),h=new n.PluginKey("grammarSuggestPlugin"),S=e=>{let t="",o="",s=!1;return e.descendants(((e,a)=>{if(e.type.isBlock){if(!s)return void(s=!0);t+=`${o}\n`,o=""}else e.isText&&(o+=`${e.text}`)})),t+=o,t},f=(e,t)=>{if(e===t)return{start:e.length,end:e.length,oldStart:e.length,oldEnd:e.length,oldText:"",newText:""};const o=p(e,t),s=o[0],a=s[0]===p.EQUAL?s[1].length-1:0,r=((e,t)=>{if(t[t.length-1][0]!==p.EQUAL)return e.length;const o=t.slice(0,t.length-1);let s=0;for(const e of o){const[t,o]=e;t===p.EQUAL&&(s+=o.length),t===p.DELETE&&(s+=o.length)}return s})(e,o),n=e.lastIndexOf("\n",a),i=e.indexOf("\n",r),c=-1===n?0:n+1,d=-1===i?e.length:i,l=e.slice(c,d),g=t.slice(c,t.length-(e.length-d));return{start:c,end:c+g.length,oldStart:c,oldEnd:d,oldText:l,newText:g}},P=(e,t)=>c.__awaiter(void 0,void 0,void 0,(function*(){const o=[...t.split("\n")];return fetch("https://prosemirror-ai-plugin.web.app/api/suggestion",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:o}})}).then((e=>e.ok?e.json():Promise.reject(e))).then((e=>(null==e?void 0:e.length)?{result:e.join("\n"),fixed:!0}:{fixed:!1,result:t})).catch((e=>(e.text().then((t=>{console.error({status:e.status,text:t})})),{fixed:!1,result:t})))})),y=(e,t)=>{var o;const s=null===(o=h.getState(e.state))||void 0===o?void 0:o.decorations.find(0,e.state.doc.nodeSize,(e=>e.id===t.spec.id))[0];if(!s)return;const{text:a}=s.spec,{from:r,to:i}=s,p={type:exports.GrammarSuggestMetaType.acceptSuggestion,id:t.spec.id},c=a.split("\n").map((t=>t?e.state.schema.node("paragraph",null,e.state.schema.text(t||"")):e.state.schema.node("paragraph"))),d=u.Fragment.fromArray(c);let{tr:l}=e.state;l=l.setSelection(n.TextSelection.create(e.state.doc,r,i)),l.selection.replace(l,new u.Slice(d,1,1)),l.setMeta(h,p),e.dispatch(l)},v=(e,t)=>{const{spec:o}=t,s={type:exports.GrammarSuggestMetaType.discardSuggestion,id:o.id},a=e.state.tr.setMeta(h,s);e.dispatch(a)},T=(e,t)=>{const o=h.getState(e.state);if(!o)return!1;const{decorations:s}=o,a=s.find(t,t)[0];if(!a){const t={type:exports.GrammarSuggestMetaType.closeSuggestion};return e.dispatch(e.state.tr.setMeta(h,t)),!1}const r=o.popupDecoration.find()[0];if((null==r?void 0:r.spec.id)===a.spec.id)return!1;const n={type:exports.GrammarSuggestMetaType.openSuggestion,decoration:a};return e.dispatch(e.state.tr.setMeta(h,n)),!1},O={debounceMs:2e3,createUpdatePopup:(e,t,o,s,a)=>{const r=document.createElement("div");r.className="grammar-suggest-tooltip";const{spec:n}=t,i=e.dom.getBoundingClientRect();r.id=exports.GrammarSuggestElementClass.grammarSuggestPopup;const p=e.coordsAtPos(o),c=e.dom.scrollTop||0,d=e.dom.scrollLeft||0;r.style.left=`${p.left-i.left+d}px`,r.style.top=`${p.bottom-i.top+c+5}px`;const l=document.createElement("div");l.className="grammar-suggest-tooltip-apply",l.innerText=n.text||n.originalText,n.text||(l.style.textDecoration="line-through",l.style.color="red"),l.onclick=()=>{s(e,t)},r.appendChild(l);const g=document.createElement("div");return g.innerHTML="<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24'><path d='M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM12 10.5858L14.8284 7.75736L16.2426 9.17157L13.4142 12L16.2426 14.8284L14.8284 16.2426L12 13.4142L9.17157 16.2426L7.75736 14.8284L10.5858 12L7.75736 9.17157L9.17157 7.75736L12 10.5858Z'></path></svg>",g.className="grammar-suggest-tooltip-discard",g.onclick=()=>{a(e,t)},r.appendChild(g),r},withYjs:!1},M={maxSelection:1e3},w=(e,t,o,s,a,r,n)=>c.__awaiter(void 0,void 0,void 0,(function*(){var e;let o="";const i=new AbortController;try{const p=null===(e=(yield fetch("https://suggestion-gw5lxik4dq-uc.a.run.app",{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:"Bearer -qKivjCv6MfQSmgF438PjEY7RnLfqoVe"},signal:i.signal,body:JSON.stringify({model:"gpt-3.5-turbo",modelParams:{input:[t],task:a,params:n}})})).body)||void 0===e?void 0:e.getReader(),d=({done:e,value:t})=>c.__awaiter(void 0,void 0,void 0,(function*(){var n;if(e)return;if(null===(n=x.getState(s.state))||void 0===n?void 0:n.isCancelled)return void(yield i.abort());const c=(new TextDecoder).decode(t);try{o+=c,s.dispatch(s.state.tr.setMeta(x,Object.assign({type:a,status:exports.Status.streaming,result:o},r&&{selection:r})))}catch(e){console.error("Could not parse stream message",c,e)}return null==p?void 0:p.read().then(d)}));yield null==p?void 0:p.read().then(d),s.dispatch(s.state.tr.setMeta(x,Object.assign({type:a,status:exports.Status.finished,result:o},r&&{selection:r})))}catch(e){console.error("Error:",e)}}));Object.defineProperty(exports,"getDiff",{enumerable:!0,get:function(){return m.getDiff}}),exports.completePlugin=(e,t=M)=>{let o=!1;return new n.Plugin({key:x,state:{init:()=>({status:exports.Status.idle}),apply(e,t,s,a){const r=e.getMeta(x);return console.log("complatePluginMeta",{meta:r,pluginState:t,state:a}),t.status===exports.Status.done||t.status===exports.Status.rejected?(o=!1,{status:exports.Status.idle}):(null==r?void 0:r.status)===exports.Status.cancelled?Object.assign(Object.assign(Object.assign({},t),r),{isCancelled:!0}):r&&(e=>Object.values(exports.OpenAiPromptsWithParam).includes(e.type)||Object.values(exports.OpenAiPromptsWithoutParam).includes(e.type))(r)?t.type&&r.type!==t.type?t:Object.assign(Object.assign({},t),r):t}},view:()=>({update(e,s){const a=x.getState(e.state);console.log({pluginState:a});let r=e.state.tr;if((null==a?void 0:a.status)===exports.Status.new&&!o)switch(a.type){case exports.OpenAiPromptsWithoutParam.Complete:o=!0,((e,t,o)=>{c.__awaiter(void 0,void 0,void 0,(function*(){const{doc:e}=t.state,o=[];e.descendants((e=>{"paragraph"===e.type.name&&o.push(e.textContent)}));let s="";s=o.length>=2?o.slice(-2).join(" "):o.join(" "),w(0,s,0,t,exports.OpenAiPromptsWithoutParam.Complete)}))})(0,e);break;case exports.OpenAiPromptsWithoutParam.MakeLonger:case exports.OpenAiPromptsWithoutParam.MakeShorter:case exports.OpenAiPromptsWithoutParam.Improve:case exports.OpenAiPromptsWithoutParam.Simplify:case exports.OpenAiPromptsWithoutParam.Explain:case exports.OpenAiPromptsWithoutParam.ActionItems:case exports.OpenAiPromptsWithParam.Translate:case exports.OpenAiPromptsWithParam.ChangeTone:o=!0,((e,t,o,s,a,r)=>{const n=o.state.selection;if(!n)return void o.dispatch(o.state.tr.setMeta(x,{task:e,status:exports.Status.done}));if(o.state.doc.textBetween(n.from,n.to).length>a)return void o.dispatch(o.state.tr.setMeta(x,{type:e,status:exports.Status.error,error:"Selection is too big"}));const i=o.state.doc.textBetween(n.from,n.to,"\n");w(0,i,0,o,e,n,r)})(a.type,0,e,0,t.maxSelection,a.params)}if((null==a?void 0:a.status)===exports.Status.accepted){if(a.error)return r.setMeta(x,{type:a.type,status:exports.Status.done}),void e.dispatch(r);switch(a.type){case exports.OpenAiPromptsWithoutParam.Complete:r=r.insertText(a.result||"",e.state.doc.nodeSize-2),r.setMeta(x,{type:exports.OpenAiPromptsWithoutParam.Complete,status:exports.Status.done}),e.dispatch(r),e.focus();break;case exports.OpenAiPromptsWithoutParam.MakeLonger:case exports.OpenAiPromptsWithoutParam.MakeShorter:case exports.OpenAiPromptsWithoutParam.Improve:case exports.OpenAiPromptsWithoutParam.Simplify:case exports.OpenAiPromptsWithoutParam.Explain:case exports.OpenAiPromptsWithoutParam.ActionItems:case exports.OpenAiPromptsWithParam.Translate:case exports.OpenAiPromptsWithParam.ChangeTone:if(a.selection&&a.result){const t=a.result.split("\n\n").map((t=>e.state.schema.node("paragraph",null,e.state.schema.text(t)))),o=u.Fragment.fromArray(t);r.selection.replace(r,new u.Slice(o,0,0))}r.setMeta(x,{type:a.type,status:exports.Status.done}),e.dispatch(r),e.focus()}}}})})},exports.completePluginKey=x,exports.completeRequest=w,exports.defaultCompleteOptions=M,exports.defaultOptions=O,exports.getChangedRegions=f,exports.grammarSuggestPlugin=(e,t=O)=>{let o=!1;return new n.Plugin({key:h,state:{init:()=>({lastText:"",decorations:i.DecorationSet.empty,popupDecoration:i.DecorationSet.empty}),apply(e,o,s,a){const r=e.getMeta(h);return(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.suggestionUpdate?((e,t,o)=>{const{changedRegion:s,fix:a,mapping:r,text:n}=t,p=m.getDiff(s.newText,a.result).filter((e=>!m.isIdentity(e))).filter((e=>e.original!==`${e.replacement}\n`)).map((({from:e,to:t,original:o,replacement:a})=>{const n=l.textPosToDocPos(s.start+e,r),p=l.textPosToDocPos(s.start+(a.endsWith("\n")?t-1:t),r),c={text:a.endsWith("\n")?a.slice(0,-1):a,originalText:o,id:{}};return i.Decoration.inline(n,p,{class:"grammarSuggestion "+(""===a?"removalSuggestion":"")},c)}));return Object.assign(Object.assign({},e),{decorations:e.decorations.add(o.doc,p),lastText:n})})(o,r,e):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.acceptSuggestion?((e,t,o)=>{const s=e.decorations.remove(e.decorations.find(0,o.doc.nodeSize,(e=>e.id===t.id)));return Object.assign(Object.assign({},e),{lastText:S(o.doc),decorations:s.map(o.mapping,o.doc),popupDecoration:i.DecorationSet.empty})})(o,r,e):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.openSuggestion?((e,t,o,s)=>{const{decoration:a}=t,r={id:a.spec.id};return Object.assign(Object.assign({},e),{popupDecoration:i.DecorationSet.create(o.doc,[i.Decoration.widget(a.from,((e,t)=>{const o=t();return o?s.createUpdatePopup(e,a,o,y,v):document.createElement("div")}),Object.assign(Object.assign({},r),{stopEvent:()=>!0}))])})})(o,r,e,t):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.closeSuggestion?Object.assign(Object.assign({},o),{popupDecoration:i.DecorationSet.empty}):(null==r?void 0:r.type)===exports.GrammarSuggestMetaType.discardSuggestion?((e,t,o)=>Object.assign(Object.assign({},e),{decorations:e.decorations.remove(e.decorations.find(0,o.doc.nodeSize,(e=>e.id===t.id))),popupDecoration:i.DecorationSet.empty}))(o,r,e):e.docChanged?((e,t,o,s)=>{const a=l.docToTextWithMapping(o.doc).text,{text:r,mapping:n}=l.docToTextWithMapping(t.doc);if(r===a)return e;let i=t.mapping;if(s){const e=t.doc.content.findDiffStart(o.doc.content),s=o.doc.content.findDiffEnd(t.doc.content),a=s&&e?new g.StepMap([e,s.a-e,s.b-e]):new g.StepMap([0,0,0]);i=new g.Mapping([a])}const p=f(a,r),c=e.decorations.map(i,t.doc),d=l.textPosToDocPos(p.start,n),u=l.textPosToDocPos(p.end,n),m=e.popupDecoration.map(i,t.doc);return Object.assign(Object.assign({},e),{decorations:c.remove(c.find(d,u)),popupDecoration:m.remove(m.find(d,u))})})(o,e,s,t.withYjs):o}},props:{handleClick:T,decorations:e=>{const t=h.getState(e);if(!t)return null;const o=x.getState(e);return(null==o?void 0:o.status)!==exports.Status.idle?t.decorations:t.decorations.add(e.doc,t.popupDecoration.find())}},view(){const s=((e,t)=>d((e=>{var o;const s=l.docToTextWithMapping(e.state.doc),a=(null===(o=h.getState(e.state))||void 0===o?void 0:o.lastText)||"",r=f(a,s.text);P(t,r.newText).then((t=>{if(S(e.state.doc)!==s.text)return;const o={type:exports.GrammarSuggestMetaType.suggestionUpdate,fix:t,changedRegion:r,mapping:s.mapping,text:s.text};e.dispatch(e.state.tr.setMeta(h,o))})).catch((e=>{console.error("Grammar suggest API error",e)}))}),e.debounceMs))(t,e);return{update(e,t){const a=h.getState(e.state);e.state.doc.textBetween(0,e.state.doc.nodeSize-2,"/n")===t.doc.textBetween(0,t.doc.nodeSize-2,"/n")&&o||!a||a.lastText===S(e.state.doc)||(o=!0,s(e))}}}})},exports.suggescatRequest=P;
|