prosemirror-suggestcat-plugin 2.2.3 → 2.3.0
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/README.md +94 -3
- package/dist/index.es.js +1 -1
- package/dist/index.js +1 -1
- package/dist/src/api/request.d.ts +3 -0
- package/dist/src/api/request.d.ts.map +1 -1
- package/dist/src/api/streaming.d.ts.map +1 -1
- package/dist/src/api/types.d.ts +1 -0
- package/dist/src/api/types.d.ts.map +1 -1
- package/dist/src/autoComplete/actions.d.ts +11 -2
- package/dist/src/autoComplete/actions.d.ts.map +1 -1
- package/dist/src/autoComplete/index.d.ts +2 -2
- package/dist/src/autoComplete/index.d.ts.map +1 -1
- package/dist/src/autoComplete/plugin.d.ts.map +1 -1
- package/dist/src/autoComplete/streaming.d.ts +2 -1
- package/dist/src/autoComplete/streaming.d.ts.map +1 -1
- package/dist/src/autoComplete/types.d.ts +9 -1
- package/dist/src/autoComplete/types.d.ts.map +1 -1
- package/dist/src/completeV2/actions.d.ts +7 -2
- package/dist/src/completeV2/actions.d.ts.map +1 -1
- package/dist/src/completeV2/index.d.ts +1 -1
- package/dist/src/completeV2/index.d.ts.map +1 -1
- package/dist/src/grammarSuggestV2/actions.d.ts +3 -0
- package/dist/src/grammarSuggestV2/actions.d.ts.map +1 -1
- package/dist/src/grammarSuggestV2/index.d.ts +1 -1
- package/dist/src/grammarSuggestV2/index.d.ts.map +1 -1
- package/dist/src/grammarSuggestV2/plugin.d.ts.map +1 -1
- package/dist/src/grammarSuggestV2/processor.d.ts +5 -3
- package/dist/src/grammarSuggestV2/processor.d.ts.map +1 -1
- package/dist/src/grammarSuggestV2/types.d.ts +2 -0
- package/dist/src/grammarSuggestV2/types.d.ts.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -75,6 +75,7 @@ view.dispatch(
|
|
|
75
75
|
| `maxRetries` | `number` | `3` | Max retries per paragraph |
|
|
76
76
|
| `backoffBase` | `number` | `2000` | Base backoff delay in ms |
|
|
77
77
|
| `debounceMs` | `number` | `1000` | Debounce delay before re-checking |
|
|
78
|
+
| `systemPrompt` | `string` | -- | Custom system prompt for grammar requests (overrides default behavior) |
|
|
78
79
|
| `createPopup` | `"react" \| function` | -- | `"react"` for React popups, or a factory function returning an HTMLElement |
|
|
79
80
|
|
|
80
81
|
### CompleteV2Options
|
|
@@ -94,6 +95,7 @@ view.dispatch(
|
|
|
94
95
|
| `apiEndpoint` | `string` | SuggestCat API | Custom endpoint URL |
|
|
95
96
|
| `model` | `string` | `"openai:gpt-4o-mini"` | AI model to use |
|
|
96
97
|
| `ghostTextClass` | `string` | `"autoCompleteGhostText"` | CSS class for ghost text decoration |
|
|
98
|
+
| `systemPrompt` | `string` | -- | Custom system prompt for autocomplete requests |
|
|
97
99
|
|
|
98
100
|
## API
|
|
99
101
|
|
|
@@ -107,8 +109,10 @@ view.dispatch(
|
|
|
107
109
|
| `discardSuggestion(view, id)` | function | Discard a grammar suggestion |
|
|
108
110
|
| `selectSuggestion(view, id)` | function | Select a suggestion (for popup) |
|
|
109
111
|
| `deselectSuggestion(view)` | function | Deselect the current suggestion |
|
|
110
|
-
| `requestHint(
|
|
111
|
-
| `getSelectedDecoration(
|
|
112
|
+
| `requestHint(apiKey, original, replacement, options?)` | function | Request an AI explanation for a suggestion (options: `{ endpoint?, model?, systemPrompt? }`) |
|
|
113
|
+
| `getSelectedDecoration(view, key)` | function | Get the currently selected decoration |
|
|
114
|
+
| `setGrammarSystemPrompt(view, key, prompt?)` | function | Set or clear a custom system prompt for grammar checking |
|
|
115
|
+
| `grammarSuggestInit(view, key, systemPrompt?)` | function | Initialize grammar checking, optionally with a custom system prompt |
|
|
112
116
|
| `setGrammarSuggestEnabled(view, enabled)` | function | Enable/disable grammar checking |
|
|
113
117
|
|
|
114
118
|
### Complete plugin
|
|
@@ -118,6 +122,7 @@ view.dispatch(
|
|
|
118
122
|
| `completePluginV2(apiKey, options?)` | Plugin factory | Creates the completion plugin |
|
|
119
123
|
| `completeV2Key` | `PluginKey` | Plugin key |
|
|
120
124
|
| `startTask(view, taskType, params?)` | function | Start an AI task |
|
|
125
|
+
| `startCustomTask(view, systemPrompt)` | function | Start a task with a custom system prompt |
|
|
121
126
|
| `acceptResult(view)` | function | Accept the streamed result |
|
|
122
127
|
| `rejectResult(view)` | function | Reject the streamed result |
|
|
123
128
|
| `cancelTask(view)` | function | Cancel an in-progress task |
|
|
@@ -134,7 +139,9 @@ view.dispatch(
|
|
|
134
139
|
|---|---|---|
|
|
135
140
|
| `autoCompletePlugin(apiKey, options?)` | Plugin factory | Creates the autocomplete plugin |
|
|
136
141
|
| `autoCompleteKey` | `PluginKey` | Plugin key |
|
|
137
|
-
| `setAutoCompleteEnabled(view, enabled)` | function | Enable/disable autocomplete |
|
|
142
|
+
| `setAutoCompleteEnabled(view, enabled, systemPrompt?)` | function | Enable/disable autocomplete, optionally with a custom prompt |
|
|
143
|
+
| `autoCompleteInit(view, systemPrompt?)` | function | Enable autocomplete with an optional custom system prompt |
|
|
144
|
+
| `setAutoCompleteSystemPrompt(view, prompt?)` | function | Set or clear a custom system prompt without toggling enabled |
|
|
138
145
|
| `acceptAutoCompletion(view)` | function | Accept the ghost text |
|
|
139
146
|
| `dismissAutoCompletion(view)` | function | Dismiss the ghost text |
|
|
140
147
|
| `isAutoCompleteEnabled(view)` | function | Check if autocomplete is enabled |
|
|
@@ -150,6 +157,90 @@ view.dispatch(
|
|
|
150
157
|
| `createGrammarApiConfig(apiKey, endpoint?, model?)` | function | Build a grammar-specific API config |
|
|
151
158
|
| `getDiff(oldText, newText)` | function | Re-exported from `@emergence-engineering/fast-diff-merge` |
|
|
152
159
|
|
|
160
|
+
## Custom Prompts
|
|
161
|
+
|
|
162
|
+
All three plugins support custom system prompts, allowing you to control the AI behavior for your use case.
|
|
163
|
+
|
|
164
|
+
### Grammar — custom system prompt
|
|
165
|
+
|
|
166
|
+
Set a system prompt at plugin creation, at init time, or dynamically at runtime:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
// At creation time (baked into plugin options)
|
|
170
|
+
const grammarPlugin = grammarSuggestPluginV2("<API_KEY>", {
|
|
171
|
+
systemPrompt: "You are a strict academic reviewer. Return only the fixed text, keeping separators intact.",
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// At init time (recommended — combines init + system prompt)
|
|
175
|
+
import { grammarSuggestInit, grammarSuggestV2Key } from "prosemirror-suggestcat-plugin";
|
|
176
|
+
|
|
177
|
+
grammarSuggestInit(view, grammarSuggestV2Key, "Your custom prompt here");
|
|
178
|
+
// Without a custom prompt (uses default behavior):
|
|
179
|
+
grammarSuggestInit(view, grammarSuggestV2Key);
|
|
180
|
+
|
|
181
|
+
// Or dynamically at runtime (change prompt without re-init)
|
|
182
|
+
import { setGrammarSystemPrompt } from "prosemirror-suggestcat-plugin";
|
|
183
|
+
|
|
184
|
+
setGrammarSystemPrompt(view, grammarSuggestV2Key, "Your custom prompt here");
|
|
185
|
+
// Clear custom prompt to restore defaults:
|
|
186
|
+
setGrammarSystemPrompt(view, grammarSuggestV2Key, undefined);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Autocomplete — custom system prompt
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// At creation time (baked into plugin options)
|
|
193
|
+
const autoComplete = autoCompletePlugin("<API_KEY>", {
|
|
194
|
+
systemPrompt: "Complete sentences in a formal, technical tone.",
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// At enable time (recommended — combines enable + system prompt)
|
|
198
|
+
import { autoCompleteInit } from "prosemirror-suggestcat-plugin";
|
|
199
|
+
|
|
200
|
+
autoCompleteInit(view, "Your custom prompt here");
|
|
201
|
+
// Or with setAutoCompleteEnabled:
|
|
202
|
+
setAutoCompleteEnabled(view, true, "Your custom prompt here");
|
|
203
|
+
|
|
204
|
+
// Dynamically change prompt without toggling enabled
|
|
205
|
+
import { setAutoCompleteSystemPrompt } from "prosemirror-suggestcat-plugin";
|
|
206
|
+
|
|
207
|
+
setAutoCompleteSystemPrompt(view, "Your custom prompt here");
|
|
208
|
+
// Clear:
|
|
209
|
+
setAutoCompleteSystemPrompt(view, undefined);
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Complete — custom task
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
import { startCustomTask } from "prosemirror-suggestcat-plugin";
|
|
216
|
+
|
|
217
|
+
// Select text first, then:
|
|
218
|
+
startCustomTask(view, "Rewrite this text as API documentation.");
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Hint — custom system prompt
|
|
222
|
+
|
|
223
|
+
The grammar hint (the "?" button explaining a correction) also supports a custom prompt:
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import { requestHint } from "prosemirror-suggestcat-plugin";
|
|
227
|
+
|
|
228
|
+
const hint = await requestHint(apiKey, originalText, replacement, {
|
|
229
|
+
systemPrompt: "Explain the correction as a writing tutor would, with examples.",
|
|
230
|
+
});
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
When using the React `GrammarPopup`, pass the `hintSystemPrompt` prop:
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
<GrammarPopup
|
|
237
|
+
editorView={view}
|
|
238
|
+
editorState={editorState}
|
|
239
|
+
apiKey={apiKey}
|
|
240
|
+
hintSystemPrompt="Explain the correction in simple terms."
|
|
241
|
+
/>
|
|
242
|
+
```
|
|
243
|
+
|
|
153
244
|
## Styles
|
|
154
245
|
|
|
155
246
|
```typescript
|
package/dist/index.es.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Fragment as e,Slice as t}from"prosemirror-model";import{PluginKey as n,Plugin as s,TextSelection as a}from"prosemirror-state";import r from"fast-diff";import{getDiff as o,isIdentity as i}from"@emergence-engineering/fast-diff-merge";export{getDiff}from"@emergence-engineering/fast-diff-merge";import{Decoration as c,DecorationSet as l}from"prosemirror-view";import{textPosToDocPos as d,UnitStatus as u,ActionType as p,dispatchAction as m,createBlockRunnerKey as g,blockRunnerPlugin as E}from"@emergence-engineering/prosemirror-block-runner";var S,f,T,h,R,y;!function(e){e.suggestionUpdate="suggestionUpdate",e.acceptSuggestion="acceptSuggestion",e.openSuggestion="openSuggestion",e.closeSuggestion="closeSuggestion",e.discardSuggestion="discardSuggestion",e.setEnabled="setEnabled"}(S||(S={})),function(e){e.grammarSuggestPopup="grammar-suggest-popup"}(f||(f={})),function(e){e.Casual="Casual",e.Confident="Confident",e.Straightforward="Straightforward",e.Friendly="Friendly"}(T||(T={})),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"}(h||(h={})),function(e){e.Complete="Complete",e.SmallComplete="SmallComplete",e.Improve="Improve",e.MakeLonger="MakeLonger",e.MakeShorter="MakeShorter",e.Simplify="Simplify",e.Explain="Explain",e.ActionItems="ActionItems"}(R||(R={})),function(e){e.ChangeTone="ChangeTone",e.Translate="Translate",e.Hint="Hint",e.Custom="Custom"}(y||(y={}));const C=R,A=y;var M,x,I;!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"}(M||(M={})),function(e){e.IDLE="idle",e.PENDING="pending",e.STREAMING="streaming",e.PREVIEW="preview",e.APPLYING="applying"}(x||(x={})),function(e){e.START_TASK="START_TASK",e.STREAM_UPDATE="STREAM_UPDATE",e.STREAM_COMPLETE="STREAM_COMPLETE",e.STREAM_ERROR="STREAM_ERROR",e.ACCEPT_RESULT="ACCEPT_RESULT",e.REJECT_RESULT="REJECT_RESULT",e.CANCEL_TASK="CANCEL_TASK",e.CLEAR_ERROR="CLEAR_ERROR",e.SET_ENABLED="SET_ENABLED"}(I||(I={}));const N={maxSelection:1e3},P="https://suggestion-gw5lxik4dq-uc.a.run.app",D="https://prosemirror-ai-plugin.web.app/api/suggestion",w="openai:gpt-4o-mini";function L(e){return{apiKey:e.apiKey,endpoint:e.endpoint??P,model:e.model??w}}function b(e){return{apiKey:e.apiKey,endpoint:e.endpoint??D,model:e.model??w}}async function _(e,t){const{apiKey:n,text:s,task:a,params:r,endpoint:o=P,model:i=w,signal:c}=e;let l="";const d={model:i,modelParams:{input:[s],task:a,params:r}};try{const e=await fetch(o,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`},signal:c,body:JSON.stringify(d)});if(!e.ok)throw new Error(`HTTP error! status: ${e.status}`);const s=e.body?.getReader();if(!s)throw new Error("No response body");for(;;){const{done:e,value:n}=await s.read();if(e)break;if(c?.aborted)return;const a=(new TextDecoder).decode(n);l+=a,t.onChunk(a,l)}c?.aborted||t.onComplete(l)}catch(e){if(e instanceof Error&&"AbortError"===e.name)return;t.onError(e instanceof Error?e:new Error(String(e)))}}async function v(e){const{apiKey:t,text:n,endpoint:s=D,model:a=w}=e,r={model:a,modelParams:{input:[...n.split("\n")]}};try{const e=await fetch(s,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`},body:JSON.stringify(r)});if(!e.ok){const t=await e.text();return console.error({status:e.status,text:t}),{fixed:!1,result:n,error:!0}}const a=await e.json();return a?.length?{result:a.join("\n"),fixed:!0}:{fixed:!1,result:n}}catch(e){return console.error("Grammar request error:",e),{fixed:!1,result:n,error:!0}}}const O=new WeakMap;function k(e){const t=O.get(e);t&&(t.abort(),O.delete(e))}function G(e){return O.has(e)}const U=new n("completePluginV2"),K={status:x.IDLE,streamedResult:"",enabled:!0};function V(n,r={}){const{maxSelection:o,apiEndpoint:i,model:c}={...N,...r};let l=!1;return new s({key:U,state:{init:()=>({...K}),apply(e,t){const n=e.getMeta(U);return n?function(e,t){switch(t.type){case I.SET_ENABLED:return{...e,enabled:t.enabled,...t.enabled?{}:K};case I.START_TASK:return e.status!==x.IDLE?e:{...e,status:x.PENDING,taskType:t.taskType,params:t.params,selection:t.selection,streamedResult:"",error:void 0};case I.STREAM_UPDATE:return e.status!==x.PENDING&&e.status!==x.STREAMING?e:{...e,status:x.STREAMING,streamedResult:t.result};case I.STREAM_COMPLETE:return e.status!==x.PENDING&&e.status!==x.STREAMING?e:{...e,status:x.PREVIEW,streamedResult:t.result};case I.STREAM_ERROR:return{...e,status:x.IDLE,error:t.error,streamedResult:"",taskType:void 0,params:void 0,selection:void 0};case I.ACCEPT_RESULT:return e.status!==x.PREVIEW?e:{...e,status:x.APPLYING};case I.REJECT_RESULT:return e.status!==x.PREVIEW?e:{...K,enabled:e.enabled};case I.CANCEL_TASK:return{...K,enabled:e.enabled};case I.CLEAR_ERROR:return{...e,error:void 0};default:return e}}(t,n):t.status===x.APPLYING?{...K,enabled:t.enabled}:t}},view:()=>({update(s){const r=U.getState(s.state);if(r&&r.enabled){if(r.status===x.PENDING&&!l){const{text:e,error:t}=function(e,t,n){const{taskType:s,selection:a}=t;if(s===R.Complete){const{doc:t}=e.state,n=[];return t.descendants(e=>{"paragraph"===e.type.name&&n.push(e.textContent)}),{text:n.length>=2?n.slice(-2).join(" "):n.join(" ")}}return a?e.state.doc.textBetween(a.from,a.to).length>n?{text:"",error:"Selection is too big"}:{text:e.state.doc.textBetween(a.from,a.to,"\n")}:{text:"",error:"No selection"}}(s,r,o);if(t)return void s.dispatch(s.state.tr.setMeta(U,{type:I.STREAM_ERROR,error:t}));if(!e)return void s.dispatch(s.state.tr.setMeta(U,{type:I.STREAM_ERROR,error:"No text to process"}));l=!0,async function({view:e,pluginKey:t,apiKey:n,text:s,task:a,params:r,apiEndpoint:o,model:i}){k(e);const c=new AbortController;O.set(e,c);try{await _({apiKey:n,text:s,task:a,params:r,endpoint:o,model:i,signal:c.signal},{onChunk:(n,s)=>{e.isDestroyed||e.dispatch(e.state.tr.setMeta(t,{type:I.STREAM_UPDATE,result:s}))},onComplete:n=>{e.isDestroyed||e.dispatch(e.state.tr.setMeta(t,{type:I.STREAM_COMPLETE,result:n}))},onError:n=>{console.error("Streaming request error:",n),e.isDestroyed||e.dispatch(e.state.tr.setMeta(t,{type:I.STREAM_ERROR,error:n.message}))}})}finally{O.delete(e)}}({view:s,pluginKey:U,apiKey:n,text:e,task:r.taskType,params:r.params,apiEndpoint:i,model:c}).finally(()=>{l=!1})}r.status===x.APPLYING&&function(n,s){const{taskType:r,selection:o,streamedResult:i}=s;if(!i)return;let{tr:c}=n.state;if(r===R.Complete)c=c.insertText(i,n.state.doc.nodeSize-2);else if(o){const s=i.split("\n\n").map(e=>n.state.schema.node("paragraph",null,e?n.state.schema.text(e):void 0)),r=e.fromArray(s);c=c.setSelection(a.create(n.state.doc,o.from,o.to)),c.selection.replace(c,new t(r,0,0))}n.dispatch(c),n.focus()}(s,r)}},destroy(){}})})}function B(e,t,n){const s=e.state.selection;e.dispatch(e.state.tr.setMeta(U,{type:I.START_TASK,taskType:t,params:n,selection:s.empty?void 0:s}))}function W(e){e.dispatch(e.state.tr.setMeta(U,{type:I.ACCEPT_RESULT}))}function H(e){e.dispatch(e.state.tr.setMeta(U,{type:I.REJECT_RESULT}))}function F(e){k(e),e.dispatch(e.state.tr.setMeta(U,{type:I.CANCEL_TASK}))}function $(e,t){t||k(e),e.dispatch(e.state.tr.setMeta(U,{type:I.SET_ENABLED,enabled:t}))}function j(e){e.dispatch(e.state.tr.setMeta(U,{type:I.CLEAR_ERROR}))}function q(e){return U.getState(e.state)}const J={debounceMs:2e3,createUpdatePopup:(e,t,n,s,a)=>{const r=document.createElement("div");r.className="grammar-suggest-tooltip";const{spec:o}=t,i=e.dom.getBoundingClientRect();r.id=f.grammarSuggestPopup;const c=e.coordsAtPos(n),l=e.dom.scrollTop||0,d=e.dom.scrollLeft||0;r.style.left=`${c.left-i.left+d}px`,r.style.top=`${c.bottom-i.top+l+5}px`;const u=document.createElement("div");u.className="grammar-suggest-tooltip-apply",u.innerText=o.text||o.originalText,o.text||(u.style.textDecoration="line-through",u.style.color="red"),u.onclick=()=>{s(e,t)},r.appendChild(u);const p=document.createElement("div");return p.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>",p.className="grammar-suggest-tooltip-discard",p.onclick=()=>{a(e,t)},r.appendChild(p),r},withYjs:!1},Q={maxSelection:1e3},z=new n("completePlugin"),Y=new n("grammarSuggestPlugin"),Z=(e,t)=>{e.dispatch(e.state.tr.setMeta(Y,{type:S.setEnabled,enabled:t}))},X=(e,t)=>{e.dispatch(e.state.tr.setMeta(z,{type:"setEnabled",enabled:t}))},ee=(e,t)=>{if(e===t)return{start:e.length,end:e.length,oldStart:e.length,oldEnd:e.length,oldText:"",newText:""};const n=r(e,t),s=n[0],a=s[0]===r.EQUAL?s[1].length-1:0,o=((e,t)=>{if(t[t.length-1][0]!==r.EQUAL)return e.length;const n=t.slice(0,t.length-1);let s=0;for(const e of n){const[t,n]=e;t===r.EQUAL&&(s+=n.length),t===r.DELETE&&(s+=n.length)}return s})(e,n),i=e.lastIndexOf("\n",a),c=e.indexOf("\n",o),l=-1===i?0:i+1,d=-1===c?e.length:c,u=e.slice(l,d),p=t.slice(l,t.length-(e.length-d));return{start:l,end:l+p.length,oldStart:l,oldEnd:d,oldText:u,newText:p}},te=(e,t)=>e.fixed&&0!==e.suggestions.length?e.suggestions.map(n=>{const s=t.from+1+d(n.from,t.mapping),a=t.from+1+d(n.to,t.mapping),r=""===n.replacement,o={id:{},unitId:t.id,originalText:n.original,replacement:n.replacement,response:e};return c.inline(s,a,{class:"grammarSuggestionV2 "+(r?"removalSuggestionV2":"")},o)}):[],ne=(e,t)=>{const n=t.contextState.selectedSuggestionId;return n?e.map(e=>{const t=e.spec;return t.id===n?c.inline(e.from,e.to,{class:"grammarSuggestionV2 grammarSuggestionV2-selected"},t):e}):e},se=e=>{let t,n;const s=Math.max(0,e.waitUntil-Date.now());switch(e.status){case u.QUEUED:case u.WAITING:t="⏳",n="grammarWidgetV2 queued";break;case u.PROCESSING:t="🔍",n="grammarWidgetV2 processing";break;case u.BACKOFF:t=`🔄 ${Math.ceil(s/1e3)}s`,n="grammarWidgetV2 backoff";break;case u.ERROR:t="❌",n="grammarWidgetV2 error";break;case u.DIRTY:t="✏️",n="grammarWidgetV2 dirty";break;default:return}const a=document.createElement("span");if(a.className=n,a.textContent=t,e.status===u.BACKOFF&&s>0){const t=setInterval(()=>{const n=Math.max(0,e.waitUntil-Date.now());a.textContent=`🔄 ${Math.ceil(n/1e3)}s`,(n<=0||!a.isConnected)&&clearInterval(t)},1e3)}return c.widget(e.from+1,a,{side:-1})};const ae=e=>{const{apiKey:t,apiEndpoint:n,model:s,modelStateManager:a}="string"==typeof e?{apiKey:e,apiEndpoint:void 0,model:void 0,modelStateManager:void 0}:e;return async(e,r)=>{try{const e=a?.getCurrentModel()??s,d=await v({apiKey:t,text:r.text,endpoint:n,model:e});if(d.error)return a?.handleFailure(),{error:new Error("Grammar API error")};if(a?.handleSuccess(),!d.fixed)return{data:{fixed:!1,originalText:r.text,fixedText:r.text,suggestions:[]}};const u=(c=r.text,l=d.result,o(c,l).filter(e=>!i(e)).filter(e=>e.original!==`${e.replacement}\n`).map(e=>({from:e.from,to:e.replacement.endsWith("\n")?e.to-1:e.to,original:e.original,replacement:e.replacement.endsWith("\n")?e.replacement.slice(0,-1):e.replacement})));return{data:{fixed:u.length>0,originalText:r.text,fixedText:d.result,suggestions:u}}}catch(e){return a?.handleFailure(),{error:e instanceof Error?e:new Error(String(e))}}var c,l}};function re(e,t,n){const s=t.getState(e.state);if(s)return s.decorations.find(e=>e.spec.id===n)}function oe(n,s,r){const o=re(n,s,r);if(!o)return;const i=o.spec,{replacement:c}=i,{from:l,to:d}=o,u=c.split("\n").map(e=>e?n.state.schema.node("paragraph",null,n.state.schema.text(e)):n.state.schema.node("paragraph")),m=e.fromArray(u);let{tr:g}=n.state;g=g.setSelection(a.create(n.state.doc,l,d)),g.selection.replace(g,new t(m,1,1)),g=g.setMeta(s,{type:p.REMOVE_DECORATION,id:r}),n.dispatch(g)}function ie(e,t,n){m(e,t,{type:p.REMOVE_DECORATION,id:n})}function ce(e,t,n){m(e,t,{type:p.UPDATE_CONTEXT,contextState:{selectedSuggestionId:n}})}function le(e,t){m(e,t,{type:p.UPDATE_CONTEXT,contextState:{selectedSuggestionId:void 0}})}function de(e,t){const n=t.getState(e.state);if(n?.contextState.selectedSuggestionId)return re(e,t,n.contextState.selectedSuggestionId)}async function ue(e,t,n,s){const a=s?.endpoint??P,r=s?.modelStateManager,o=r?.getCurrentModel()??s?.model??w;return new Promise((s,i)=>{const c={previousPromptType:"Grammar",oldVersion:t,newVersion:n};_({apiKey:e,text:t,task:y.Hint,params:c,endpoint:a,model:o},{onChunk:()=>{},onComplete:e=>{r?.handleSuccess(),s(e)},onError:e=>{r?.handleFailure(),i(e)}})})}const pe={selectedSuggestionId:void 0},me=g("grammarSuggestV2"),ge=(e,t,n,s,a,r)=>{const o=t.spec,i=document.createElement("div");i.className="grammarPopupV2";const c=document.createElement("div");c.className="grammarPopupV2-mainRow";const l=document.createElement("span");l.className="grammarPopupV2-original",l.textContent=`"${o.originalText}"`;const d=document.createElement("span");d.className="grammarPopupV2-arrow",d.textContent=" → ";const u=document.createElement("span");u.className="grammarPopupV2-replacement",u.textContent=""===o.replacement?"(remove)":`"${o.replacement}"`;const p=document.createElement("button");p.className="grammarPopupV2-hint",p.textContent="?",p.title="Why this suggestion?";const m=document.createElement("div");m.className="grammarPopupV2-hintArea",m.style.display="none";let g=!1;p.onclick=async e=>{if(e.preventDefault(),e.stopPropagation(),"none"===m.style.display){if(m.style.display="block",!g){m.innerHTML='<span class="grammarPopupV2-loading">Loading...</span>';try{const e=await r();m.innerHTML="";const t=document.createElement("span");t.className="grammarPopupV2-hintText",t.textContent=e,m.appendChild(t),g=!0}catch{m.innerHTML='<span class="grammarPopupV2-hintError">Could not load hint</span>'}}}else m.style.display="none"};const E=document.createElement("button");E.className="grammarPopupV2-accept",E.textContent="✓",E.onclick=e=>{e.preventDefault(),e.stopPropagation(),s()};const S=document.createElement("button");return S.className="grammarPopupV2-discard",S.textContent="✕",S.onclick=e=>{e.preventDefault(),e.stopPropagation(),a()},c.appendChild(l),c.appendChild(d),c.appendChild(u),c.appendChild(p),c.appendChild(E),c.appendChild(S),i.appendChild(c),i.appendChild(m),i};function Ee(e,t={}){const{apiEndpoint:n,model:a,fallback:r,batchSize:o=4,maxRetries:i=3,backoffBase:d=2e3,debounceMs:u=1e3,createPopup:p=ge}=t,m=r?function(e){const{primaryModel:t,fallbackModel:n,failureThreshold:s}=e;let a=0;return{getCurrentModel:()=>n&&s>0&&a>=s?n:t,handleSuccess(){a=0},handleFailure(){n&&a++}}}({primaryModel:a,fallbackModel:r.fallbackModel,failureThreshold:r.failureThreshold??3}):void 0,g=ae({apiKey:e,apiEndpoint:n,model:a,modelStateManager:m}),S=E({pluginKey:me,unitProcessor:g,decorationFactory:te,decorationTransformer:ne,widgetFactory:se,initialContextState:pe,options:{batchSize:o,maxRetries:i,backoffBase:d,dirtyHandling:{debounceDelay:u}}});return new s({key:me,state:S.spec.state,props:{...S.spec.props,decorations(t){const s=S.spec.props?.decorations,r=s?s.call(S,t):l.empty;if("react"===p)return r;const o=me.getState(t);if(!o?.contextState.selectedSuggestionId)return r;const i=o.decorations.find(e=>e.spec.id===o.contextState.selectedSuggestionId);if(!i)return r;const d=i.spec,u=c.widget(i.from,(t,s)=>{const r=s();return void 0===r?document.createElement("div"):p(t,i,r,()=>oe(t,me,i.spec.id),()=>ie(t,me,i.spec.id),()=>ue(e,d.originalText,d.replacement,{endpoint:n,model:a,modelStateManager:m}))},{id:i.spec.id,side:-1,stopEvent:()=>!0});return r.add(t.doc,[u])},handleClick(e,t,n){const s=me.getState(e.state);if(!s)return!1;const a=s.decorations.find(e=>e.from<=t&&t<=e.to);return a?(a.spec.id===s.contextState.selectedSuggestionId||ce(e,me,a.spec.id),!1):(s.contextState.selectedSuggestionId&&le(e,me),!1)}},view:S.spec.view})}var Se,fe;!function(e){e.IDLE="idle",e.DEBOUNCING="debouncing",e.PENDING="pending",e.STREAMING="streaming",e.SHOWING="showing"}(Se||(Se={})),function(e){e.SET_ENABLED="SET_ENABLED",e.START_DEBOUNCE="START_DEBOUNCE",e.START_REQUEST="START_REQUEST",e.STREAM_UPDATE="STREAM_UPDATE",e.STREAM_COMPLETE="STREAM_COMPLETE",e.STREAM_ERROR="STREAM_ERROR",e.DISMISS="DISMISS",e.ACCEPT="ACCEPT"}(fe||(fe={}));const Te={debounceMs:500,maxContextLength:2e3,ghostTextClass:"autoCompleteGhostText"},he=new WeakMap;function Re(e){const t=he.get(e);t&&(t.abort(),he.delete(e))}function ye(e){return he.has(e)}function Ce(e,t){if(!t)return t;const{doc:n,selection:s}=e.state,a=s.from;if(a<=0)return t;const r=n.textBetween(a-1,a,""),o=t.charAt(0),i=/[\s\-]/;return!i.test(r)&&!i.test(o)?` ${t}`:t}const Ae=new n("autoCompletePlugin"),Me={status:Se.IDLE,enabled:!0,suggestion:"",cursorPos:0};function xe(e,t){switch(t.type){case fe.SET_ENABLED:return{...Me,enabled:t.enabled};case fe.START_DEBOUNCE:return{...e,status:Se.DEBOUNCING,cursorPos:t.cursorPos,suggestion:"",error:void 0};case fe.START_REQUEST:return e.status!==Se.DEBOUNCING?e:{...e,status:Se.PENDING};case fe.STREAM_UPDATE:return e.status!==Se.PENDING&&e.status!==Se.STREAMING?e:{...e,status:Se.STREAMING,suggestion:t.suggestion};case fe.STREAM_COMPLETE:return e.status!==Se.PENDING&&e.status!==Se.STREAMING?e:{...e,status:Se.SHOWING,suggestion:t.suggestion};case fe.STREAM_ERROR:return{...e,status:Se.IDLE,error:t.error,suggestion:""};case fe.DISMISS:case fe.ACCEPT:return{...e,status:Se.IDLE,suggestion:""};default:return e}}function Ie(e,t={}){const n={...Te,...t},{debounceMs:a,maxContextLength:r,apiEndpoint:o,model:i,ghostTextClass:d}=n;let u=null,p=!1,m=null,g=null;return new s({key:Ae,state:{init:()=>({...Me}),apply(e,t,n,s){const a=e.getMeta(Ae);if(a)return xe(t,a);if(e.docChanged&&(t.status===Se.SHOWING||t.status===Se.STREAMING)){if(s.selection.from!==t.cursorPos)return xe(t,{type:fe.DISMISS})}return!e.selectionSet||e.docChanged||t.status!==Se.SHOWING&&t.status!==Se.STREAMING?t:xe(t,{type:fe.DISMISS})}},props:{decorations(e){const t=Ae.getState(e);if(!t||!t.enabled)return l.empty;if((t.status===Se.STREAMING||t.status===Se.SHOWING)&&t.suggestion){const n=document.createElement("span");n.className=d,n.textContent=t.suggestion,n.setAttribute("contenteditable","false");const s=c.widget(t.cursorPos,n,{side:1});return l.create(e.doc,[s])}return l.empty},handleKeyDown(e,t){const n=Ae.getState(e.state);return!(!n||!n.enabled)&&("Tab"===t.key&&!t.shiftKey&&n.status===Se.SHOWING&&n.suggestion?(t.preventDefault(),m=n.suggestion,g=n.cursorPos,e.dispatch(e.state.tr.setMeta(Ae,{type:fe.ACCEPT})),!0):"Escape"===t.key&&(n.status===Se.STREAMING||n.status===Se.SHOWING)&&(t.preventDefault(),Re(e),e.dispatch(e.state.tr.setMeta(Ae,{type:fe.DISMISS})),!0))}},view:()=>({update(t,n){const s=Ae.getState(t.state),c=Ae.getState(n);if(s&&s.enabled){if(null!==m&&null!==g){const e=m,n=g;m=null,g=null;const s=t.state.tr.insertText(e,n);return t.dispatch(s),void t.focus()}if(t.state,t.state.doc!==n.doc&&c?.status!==Se.PENDING&&c?.status!==Se.STREAMING){t.state.tr,u&&(clearTimeout(u),u=null),Re(t);const e=t.state.selection.from;t.dispatch(t.state.tr.setMeta(Ae,{type:fe.START_DEBOUNCE,cursorPos:e})),u=setTimeout(()=>{u=null;const e=Ae.getState(t.state);e?.status===Se.DEBOUNCING&&t.dispatch(t.state.tr.setMeta(Ae,{type:fe.START_REQUEST}))},a)}if(s.status===Se.PENDING&&!p){if(!function(e){const{doc:t,selection:n}=e.state,s=n.from,a=t.resolve(s),r=a.parent,o=a.start();if(s!==o+r.content.size)return!1;const i=r.textBetween(0,s-o,"");return!!i.trim()&&!/[.!?]\s*$/.test(i)}(t))return void t.dispatch(t.state.tr.setMeta(Ae,{type:fe.DISMISS}));const n=function(e,t){const{doc:n,selection:s}=e.state,a=s.from,r=n.textBetween(0,a,"\n");return r.length>t?r.slice(-t):r}(t,r);if(!n.trim())return void t.dispatch(t.state.tr.setMeta(Ae,{type:fe.DISMISS}));p=!0,async function({view:e,pluginKey:t,apiKey:n,context:s,apiEndpoint:a,model:r}){Re(e);const o=new AbortController;he.set(e,o);try{await _({apiKey:n,text:s,task:R.SmallComplete,endpoint:a,model:r,signal:o.signal},{onChunk:(n,s)=>{e.isDestroyed||e.dispatch(e.state.tr.setMeta(t,{type:fe.STREAM_UPDATE,suggestion:Ce(e,s)}))},onComplete:n=>{e.isDestroyed||e.dispatch(e.state.tr.setMeta(t,{type:fe.STREAM_COMPLETE,suggestion:Ce(e,n)}))},onError:n=>{console.error("Auto-complete streaming error:",n),e.isDestroyed||e.dispatch(e.state.tr.setMeta(t,{type:fe.STREAM_ERROR,error:n.message}))}})}finally{he.delete(e)}}({view:t,pluginKey:Ae,apiKey:e,context:n,apiEndpoint:o,model:i}).finally(()=>{p=!1})}}else u&&(clearTimeout(u),u=null)},destroy(){u&&(clearTimeout(u),u=null)}})})}function Ne(e,t){t||Re(e),e.dispatch(e.state.tr.setMeta(Ae,{type:fe.SET_ENABLED,enabled:t}))}function Pe(e){const t=Ae.getState(e.state);if(!t||!t.suggestion)return!1;const n=e.state.tr.insertText(t.suggestion,t.cursorPos);return e.dispatch(n),e.dispatch(e.state.tr.setMeta(Ae,{type:fe.DISMISS})),e.focus(),!0}function De(e){Re(e),e.dispatch(e.state.tr.setMeta(Ae,{type:fe.DISMISS}))}function we(e){return Ae.getState(e.state)}function Le(e){const t=Ae.getState(e.state);return t?.enabled??!1}function be(e){const t=Ae.getState(e.state);return!!t?.suggestion}const _e=V;export{y as AiPromptsWithParam,R as AiPromptsWithoutParam,fe as AutoCompleteActionType,Se as AutoCompleteStatus,I as CompleteActionType,x as CompleteStatus,P as DEFAULT_COMPLETION_ENDPOINT,D as DEFAULT_GRAMMAR_ENDPOINT,w as DEFAULT_MODEL,f as GrammarSuggestElementClass,S as GrammarSuggestMetaType,T as MoodParamType,A as OpenAiPromptsWithParam,C as OpenAiPromptsWithoutParam,M as Status,h as TranslationTargetLanguage,Pe as acceptAutoCompletion,W as acceptResult,oe as acceptSuggestion,Ae as autoCompleteKey,Ie as autoCompletePlugin,k as cancelActiveRequest,Re as cancelAutoCompleteRequest,F as cancelTask,j as clearError,_e as completePlugin,z as completePluginKey,V as completePluginV2,U as completeV2Key,L as createApiConfig,b as createGrammarApiConfig,ae as createGrammarProcessor,Te as defaultAutoCompleteOptions,Q as defaultCompleteOptions,N as defaultCompleteV2Options,J as defaultOptions,le as deselectSuggestion,ie as discardSuggestion,De as dismissAutoCompletion,we as getAutoCompleteState,ee as getChangedRegions,q as getCompleteState,de as getSelectedDecoration,te as grammarDecorationFactory,ne as grammarDecorationTransformer,v as grammarRequest,Y as grammarSuggestPluginKey,Ee as grammarSuggestPluginV2,me as grammarSuggestV2Key,se as grammarWidgetFactory,G as hasActiveRequest,ye as hasAutoCompleteRequest,be as hasAutoCompletion,Le as isAutoCompleteEnabled,H as rejectResult,ue as requestHint,ce as selectSuggestion,Ne as setAutoCompleteEnabled,X as setCompleteEnabled,$ as setEnabled,Z as setGrammarSuggestEnabled,B as startTask,_ as streamingRequest};
|
|
1
|
+
import{Fragment as t,Slice as e}from"prosemirror-model";import{PluginKey as n,Plugin as s,TextSelection as a}from"prosemirror-state";import o from"fast-diff";import{getDiff as r,isIdentity as i}from"@emergence-engineering/fast-diff-merge";export{getDiff}from"@emergence-engineering/fast-diff-merge";import{Decoration as c,DecorationSet as l}from"prosemirror-view";import{textPosToDocPos as p,UnitStatus as u,ActionType as d,dispatchAction as m,createBlockRunnerKey as g,blockRunnerPlugin as E}from"@emergence-engineering/prosemirror-block-runner";var S,f,T,y,h,R;!function(t){t.suggestionUpdate="suggestionUpdate",t.acceptSuggestion="acceptSuggestion",t.openSuggestion="openSuggestion",t.closeSuggestion="closeSuggestion",t.discardSuggestion="discardSuggestion",t.setEnabled="setEnabled"}(S||(S={})),function(t){t.grammarSuggestPopup="grammar-suggest-popup"}(f||(f={})),function(t){t.Casual="Casual",t.Confident="Confident",t.Straightforward="Straightforward",t.Friendly="Friendly"}(T||(T={})),function(t){t.English="English",t.Spanish="Spanish",t.French="French",t.German="German",t.Italian="Italian",t.Portuguese="Portuguese",t.Dutch="Dutch",t.Russian="Russian",t.Chinese="Chinese",t.Korean="Korean",t.Japanese="Japanese"}(y||(y={})),function(t){t.Complete="Complete",t.SmallComplete="SmallComplete",t.Improve="Improve",t.MakeLonger="MakeLonger",t.MakeShorter="MakeShorter",t.Simplify="Simplify",t.Explain="Explain",t.ActionItems="ActionItems"}(h||(h={})),function(t){t.ChangeTone="ChangeTone",t.Translate="Translate",t.Hint="Hint",t.Custom="Custom"}(R||(R={}));const C=h,M=R;var P,A,x;!function(t){t.idle="idle",t.new="new",t.streaming="streaming",t.finished="finished",t.accepted="accepted",t.cancelled="cancelled",t.rejected="rejected",t.done="done",t.error="error"}(P||(P={})),function(t){t.IDLE="idle",t.PENDING="pending",t.STREAMING="streaming",t.PREVIEW="preview",t.APPLYING="applying"}(A||(A={})),function(t){t.START_TASK="START_TASK",t.STREAM_UPDATE="STREAM_UPDATE",t.STREAM_COMPLETE="STREAM_COMPLETE",t.STREAM_ERROR="STREAM_ERROR",t.ACCEPT_RESULT="ACCEPT_RESULT",t.REJECT_RESULT="REJECT_RESULT",t.CANCEL_TASK="CANCEL_TASK",t.CLEAR_ERROR="CLEAR_ERROR",t.SET_ENABLED="SET_ENABLED"}(x||(x={}));const I={maxSelection:1e3},N="https://suggestion-gw5lxik4dq-uc.a.run.app",D="https://prosemirror-ai-plugin.web.app/api/suggestion",w="openai:gpt-4o-mini";function _(t){return{apiKey:t.apiKey,endpoint:t.endpoint??N,model:t.model??w}}function L(t){return{apiKey:t.apiKey,endpoint:t.endpoint??D,model:t.model??w}}async function b(t,e){const{apiKey:n,text:s,task:a,params:o,endpoint:r=N,model:i=w,signal:c}=t;let l="";const p={model:i,modelParams:{input:[s],task:a,params:o,stream:!0}};try{const t=await fetch(r,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`},signal:c,body:JSON.stringify(p)});if(!t.ok)throw new Error(`HTTP error! status: ${t.status}`);const s=t.body?.getReader();if(!s)throw new Error("No response body");for(;;){const{done:t,value:n}=await s.read();if(t)break;if(c?.aborted)return;const a=(new TextDecoder).decode(n);l+=a,e.onChunk(a,l)}c?.aborted||e.onComplete(l)}catch(t){if(t instanceof Error&&"AbortError"===t.name)return;e.onError(t instanceof Error?t:new Error(String(t)))}}async function v(t){const{apiKey:e,text:n,endpoint:s=D,model:a=w,task:o,params:r}=t,i={model:a,modelParams:{input:[...n.split("\n")],stream:!1,...o&&{task:o},...r&&{params:r}}};try{const t=await fetch(s,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(i)});if(!t.ok){const e=await t.text();return console.error({status:t.status,text:e}),{fixed:!1,result:n,error:!0}}const a=await t.json();return a?.length?{result:a.join("\n"),fixed:!0}:{fixed:!1,result:n}}catch(t){return console.error("Grammar request error:",t),{fixed:!1,result:n,error:!0}}}const O=new WeakMap;function k(t){const e=O.get(t);e&&(e.abort(),O.delete(t))}function G(t){return O.has(t)}const U=new n("completePluginV2"),K={status:A.IDLE,streamedResult:"",enabled:!0};function V(n,o={}){const{maxSelection:r,apiEndpoint:i,model:c}={...I,...o};let l=!1;return new s({key:U,state:{init:()=>({...K}),apply(t,e){const n=t.getMeta(U);return n?function(t,e){switch(e.type){case x.SET_ENABLED:return{...t,enabled:e.enabled,...e.enabled?{}:K};case x.START_TASK:return t.status!==A.IDLE?t:{...t,status:A.PENDING,taskType:e.taskType,params:e.params,selection:e.selection,streamedResult:"",error:void 0};case x.STREAM_UPDATE:return t.status!==A.PENDING&&t.status!==A.STREAMING?t:{...t,status:A.STREAMING,streamedResult:e.result};case x.STREAM_COMPLETE:return t.status!==A.PENDING&&t.status!==A.STREAMING?t:{...t,status:A.PREVIEW,streamedResult:e.result};case x.STREAM_ERROR:return{...t,status:A.IDLE,error:e.error,streamedResult:"",taskType:void 0,params:void 0,selection:void 0};case x.ACCEPT_RESULT:return t.status!==A.PREVIEW?t:{...t,status:A.APPLYING};case x.REJECT_RESULT:return t.status!==A.PREVIEW?t:{...K,enabled:t.enabled};case x.CANCEL_TASK:return{...K,enabled:t.enabled};case x.CLEAR_ERROR:return{...t,error:void 0};default:return t}}(e,n):e.status===A.APPLYING?{...K,enabled:e.enabled}:e}},view:()=>({update(s){const o=U.getState(s.state);if(o&&o.enabled){if(o.status===A.PENDING&&!l){const{text:t,error:e}=function(t,e,n){const{taskType:s,selection:a}=e;if(s===h.Complete){const{doc:e}=t.state,n=[];return e.descendants(t=>{"paragraph"===t.type.name&&n.push(t.textContent)}),{text:n.length>=2?n.slice(-2).join(" "):n.join(" ")}}return a?t.state.doc.textBetween(a.from,a.to).length>n?{text:"",error:"Selection is too big"}:{text:t.state.doc.textBetween(a.from,a.to,"\n")}:{text:"",error:"No selection"}}(s,o,r);if(e)return void s.dispatch(s.state.tr.setMeta(U,{type:x.STREAM_ERROR,error:e}));if(!t)return void s.dispatch(s.state.tr.setMeta(U,{type:x.STREAM_ERROR,error:"No text to process"}));l=!0,async function({view:t,pluginKey:e,apiKey:n,text:s,task:a,params:o,apiEndpoint:r,model:i}){k(t);const c=new AbortController;O.set(t,c);try{await b({apiKey:n,text:s,task:a,params:o,endpoint:r,model:i,signal:c.signal},{onChunk:(n,s)=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:x.STREAM_UPDATE,result:s}))},onComplete:n=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:x.STREAM_COMPLETE,result:n}))},onError:n=>{console.error("Streaming request error:",n),t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:x.STREAM_ERROR,error:n.message}))}})}finally{O.delete(t)}}({view:s,pluginKey:U,apiKey:n,text:t,task:o.taskType,params:o.params,apiEndpoint:i,model:c}).finally(()=>{l=!1})}o.status===A.APPLYING&&function(n,s){const{taskType:o,selection:r,streamedResult:i}=s;if(!i)return;let{tr:c}=n.state;if(o===h.Complete)c=c.insertText(i,n.state.doc.nodeSize-2);else if(r){const s=i.split("\n\n").map(t=>n.state.schema.node("paragraph",null,t?n.state.schema.text(t):void 0)),o=t.fromArray(s);c=c.setSelection(a.create(n.state.doc,r.from,r.to)),c.selection.replace(c,new e(o,0,0))}n.dispatch(c),n.focus()}(s,o)}},destroy(){}})})}function B(t,e,n){const s=t.state.selection;t.dispatch(t.state.tr.setMeta(U,{type:x.START_TASK,taskType:e,params:n,selection:s.empty?void 0:s}))}function W(t){t.dispatch(t.state.tr.setMeta(U,{type:x.ACCEPT_RESULT}))}function H(t){t.dispatch(t.state.tr.setMeta(U,{type:x.REJECT_RESULT}))}function F(t){k(t),t.dispatch(t.state.tr.setMeta(U,{type:x.CANCEL_TASK}))}function $(t,e){e||k(t),t.dispatch(t.state.tr.setMeta(U,{type:x.SET_ENABLED,enabled:e}))}function Y(t){t.dispatch(t.state.tr.setMeta(U,{type:x.CLEAR_ERROR}))}function j(t){return U.getState(t.state)}function q(t,e){B(t,R.Custom,{systemPrompt:e})}const J={debounceMs:2e3,createUpdatePopup:(t,e,n,s,a)=>{const o=document.createElement("div");o.className="grammar-suggest-tooltip";const{spec:r}=e,i=t.dom.getBoundingClientRect();o.id=f.grammarSuggestPopup;const c=t.coordsAtPos(n),l=t.dom.scrollTop||0,p=t.dom.scrollLeft||0;o.style.left=`${c.left-i.left+p}px`,o.style.top=`${c.bottom-i.top+l+5}px`;const u=document.createElement("div");u.className="grammar-suggest-tooltip-apply",u.innerText=r.text||r.originalText,r.text||(u.style.textDecoration="line-through",u.style.color="red"),u.onclick=()=>{s(t,e)},o.appendChild(u);const d=document.createElement("div");return d.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>",d.className="grammar-suggest-tooltip-discard",d.onclick=()=>{a(t,e)},o.appendChild(d),o},withYjs:!1},Q={maxSelection:1e3},z=new n("completePlugin"),X=new n("grammarSuggestPlugin"),Z=(t,e)=>{t.dispatch(t.state.tr.setMeta(X,{type:S.setEnabled,enabled:e}))},tt=(t,e)=>{t.dispatch(t.state.tr.setMeta(z,{type:"setEnabled",enabled:e}))},et=(t,e)=>{if(t===e)return{start:t.length,end:t.length,oldStart:t.length,oldEnd:t.length,oldText:"",newText:""};const n=o(t,e),s=n[0],a=s[0]===o.EQUAL?s[1].length-1:0,r=((t,e)=>{if(e[e.length-1][0]!==o.EQUAL)return t.length;const n=e.slice(0,e.length-1);let s=0;for(const t of n){const[e,n]=t;e===o.EQUAL&&(s+=n.length),e===o.DELETE&&(s+=n.length)}return s})(t,n),i=t.lastIndexOf("\n",a),c=t.indexOf("\n",r),l=-1===i?0:i+1,p=-1===c?t.length:c,u=t.slice(l,p),d=e.slice(l,e.length-(t.length-p));return{start:l,end:l+d.length,oldStart:l,oldEnd:p,oldText:u,newText:d}},nt=(t,e)=>t.fixed&&0!==t.suggestions.length?t.suggestions.map(n=>{const s=e.from+1+p(n.from,e.mapping),a=e.from+1+p(n.to,e.mapping),o=""===n.replacement,r={id:{},unitId:e.id,originalText:n.original,replacement:n.replacement,response:t};return c.inline(s,a,{class:"grammarSuggestionV2 "+(o?"removalSuggestionV2":"")},r)}):[],st=(t,e)=>{const n=e.contextState.selectedSuggestionId;return n?t.map(t=>{const e=t.spec;return e.id===n?c.inline(t.from,t.to,{class:"grammarSuggestionV2 grammarSuggestionV2-selected"},e):t}):t},at=t=>{let e,n;const s=Math.max(0,t.waitUntil-Date.now());switch(t.status){case u.QUEUED:case u.WAITING:e="⏳",n="grammarWidgetV2 queued";break;case u.PROCESSING:e="🔍",n="grammarWidgetV2 processing";break;case u.BACKOFF:e=`🔄 ${Math.ceil(s/1e3)}s`,n="grammarWidgetV2 backoff";break;case u.ERROR:e="❌",n="grammarWidgetV2 error";break;case u.DIRTY:e="✏️",n="grammarWidgetV2 dirty";break;default:return}const a=document.createElement("span");if(a.className=n,a.textContent=e,t.status===u.BACKOFF&&s>0){const e=setInterval(()=>{const n=Math.max(0,t.waitUntil-Date.now());a.textContent=`🔄 ${Math.ceil(n/1e3)}s`,(n<=0||!a.isConnected)&&clearInterval(e)},1e3)}return c.widget(t.from+1,a,{side:-1})};const ot=t=>{const{apiKey:e,apiEndpoint:n,model:s,modelStateManager:a,pluginKey:o}="string"==typeof t?{apiKey:t,apiEndpoint:void 0,model:void 0,modelStateManager:void 0,pluginKey:void 0}:t;return async(t,c)=>{try{const u=a?.getCurrentModel()??s,d=o?o.getState(t.state)?.contextState.systemPrompt:void 0,m=await v({apiKey:e,text:c.text,endpoint:n,model:u,...d&&{task:R.Custom,params:{systemPrompt:d}}});if(m.error)return a?.handleFailure(),{error:new Error("Grammar API error")};if(a?.handleSuccess(),!m.fixed)return{data:{fixed:!1,originalText:c.text,fixedText:c.text,suggestions:[]}};const g=(l=c.text,p=m.result,r(l,p).filter(t=>!i(t)).filter(t=>t.original!==`${t.replacement}\n`).map(t=>({from:t.from,to:t.replacement.endsWith("\n")?t.to-1:t.to,original:t.original,replacement:t.replacement.endsWith("\n")?t.replacement.slice(0,-1):t.replacement})));return{data:{fixed:g.length>0,originalText:c.text,fixedText:m.result,suggestions:g}}}catch(t){return a?.handleFailure(),{error:t instanceof Error?t:new Error(String(t))}}var l,p}};function rt(t,e,n){const s=e.getState(t.state);if(s)return s.decorations.find(t=>t.spec.id===n)}function it(n,s,o){const r=rt(n,s,o);if(!r)return;const i=r.spec,{replacement:c}=i,{from:l,to:p}=r,u=c.split("\n").map(t=>t?n.state.schema.node("paragraph",null,n.state.schema.text(t)):n.state.schema.node("paragraph")),m=t.fromArray(u);let{tr:g}=n.state;g=g.setSelection(a.create(n.state.doc,l,p)),g.selection.replace(g,new e(m,1,1)),g=g.setMeta(s,{type:d.REMOVE_DECORATION,id:o}),n.dispatch(g)}function ct(t,e,n){m(t,e,{type:d.REMOVE_DECORATION,id:n})}function lt(t,e,n){m(t,e,{type:d.UPDATE_CONTEXT,contextState:{selectedSuggestionId:n}})}function pt(t,e){m(t,e,{type:d.UPDATE_CONTEXT,contextState:{selectedSuggestionId:void 0}})}function ut(t,e){const n=e.getState(t.state);if(n?.contextState.selectedSuggestionId)return rt(t,e,n.contextState.selectedSuggestionId)}function dt(t,e,n){m(t,e,{type:d.UPDATE_CONTEXT,contextState:{systemPrompt:n}})}function mt(t,e,n){void 0!==n&&m(t,e,{type:d.UPDATE_CONTEXT,contextState:{systemPrompt:n}}),t.dispatch(t.state.tr.setMeta(e,{type:d.INIT,metadata:{}}))}async function gt(t,e,n,s){const a=s?.endpoint??N,o=s?.modelStateManager,r=o?.getCurrentModel()??s?.model??w;return new Promise((i,c)=>{const l=s?.systemPrompt?R.Custom:R.Hint,p=s?.systemPrompt?{systemPrompt:s.systemPrompt}:{previousPromptType:"Grammar",oldVersion:e,newVersion:n};b({apiKey:t,text:s?.systemPrompt?`Original: \n\`\`\`\n${e}\n\`\`\`\n\nReplacement: \n\`\`\`\n${n}\n\`\`\`\n`:e,task:l,params:p,endpoint:a,model:r},{onChunk:()=>{},onComplete:t=>{o?.handleSuccess(),i(t)},onError:t=>{o?.handleFailure(),c(t)}})})}const Et={selectedSuggestionId:void 0},St=g("grammarSuggestV2"),ft=(t,e,n,s,a,o)=>{const r=e.spec,i=document.createElement("div");i.className="grammarPopupV2";const c=document.createElement("div");c.className="grammarPopupV2-mainRow";const l=document.createElement("span");l.className="grammarPopupV2-original",l.textContent=`"${r.originalText}"`;const p=document.createElement("span");p.className="grammarPopupV2-arrow",p.textContent=" → ";const u=document.createElement("span");u.className="grammarPopupV2-replacement",u.textContent=""===r.replacement?"(remove)":`"${r.replacement}"`;const d=document.createElement("button");d.className="grammarPopupV2-hint",d.textContent="?",d.title="Why this suggestion?";const m=document.createElement("div");m.className="grammarPopupV2-hintArea",m.style.display="none";let g=!1;d.onclick=async t=>{if(t.preventDefault(),t.stopPropagation(),"none"===m.style.display){if(m.style.display="block",!g){m.innerHTML='<span class="grammarPopupV2-loading">Loading...</span>';try{const t=await o();m.innerHTML="";const e=document.createElement("span");e.className="grammarPopupV2-hintText",e.textContent=t,m.appendChild(e),g=!0}catch{m.innerHTML='<span class="grammarPopupV2-hintError">Could not load hint</span>'}}}else m.style.display="none"};const E=document.createElement("button");E.className="grammarPopupV2-accept",E.textContent="✓",E.onclick=t=>{t.preventDefault(),t.stopPropagation(),s()};const S=document.createElement("button");return S.className="grammarPopupV2-discard",S.textContent="✕",S.onclick=t=>{t.preventDefault(),t.stopPropagation(),a()},c.appendChild(l),c.appendChild(p),c.appendChild(u),c.appendChild(d),c.appendChild(E),c.appendChild(S),i.appendChild(c),i.appendChild(m),i};function Tt(t,e={}){const{apiEndpoint:n,model:a,fallback:o,batchSize:r=4,maxRetries:i=3,backoffBase:p=2e3,debounceMs:u=1e3,systemPrompt:d,createPopup:m=ft}=e,g=o?function(t){const{primaryModel:e,fallbackModel:n,failureThreshold:s}=t;let a=0;return{getCurrentModel:()=>n&&s>0&&a>=s?n:e,handleSuccess(){a=0},handleFailure(){n&&a++}}}({primaryModel:a,fallbackModel:o.fallbackModel,failureThreshold:o.failureThreshold??3}):void 0,S=ot({apiKey:t,apiEndpoint:n,model:a,modelStateManager:g,pluginKey:St}),f=E({pluginKey:St,unitProcessor:S,decorationFactory:nt,decorationTransformer:st,widgetFactory:at,initialContextState:{...Et,systemPrompt:d},options:{batchSize:r,maxRetries:i,backoffBase:p,dirtyHandling:{debounceDelay:u}}});return new s({key:St,state:f.spec.state,props:{...f.spec.props,decorations(e){const s=f.spec.props?.decorations,o=s?s.call(f,e):l.empty;if("react"===m)return o;const r=St.getState(e);if(!r?.contextState.selectedSuggestionId)return o;const i=r.decorations.find(t=>t.spec.id===r.contextState.selectedSuggestionId);if(!i)return o;const p=i.spec,u=c.widget(i.from,(e,s)=>{const o=s();return void 0===o?document.createElement("div"):m(e,i,o,()=>it(e,St,i.spec.id),()=>ct(e,St,i.spec.id),()=>gt(t,p.originalText,p.replacement,{endpoint:n,model:a,modelStateManager:g}))},{id:i.spec.id,side:-1,stopEvent:()=>!0});return o.add(e.doc,[u])},handleClick(t,e,n){const s=St.getState(t.state);if(!s)return!1;const a=s.decorations.find(t=>t.from<=e&&e<=t.to);return a?(a.spec.id===s.contextState.selectedSuggestionId||lt(t,St,a.spec.id),!1):(s.contextState.selectedSuggestionId&&pt(t,St),!1)}},view:f.spec.view})}var yt,ht;!function(t){t.IDLE="idle",t.DEBOUNCING="debouncing",t.PENDING="pending",t.STREAMING="streaming",t.SHOWING="showing"}(yt||(yt={})),function(t){t.SET_ENABLED="SET_ENABLED",t.SET_SYSTEM_PROMPT="SET_SYSTEM_PROMPT",t.START_DEBOUNCE="START_DEBOUNCE",t.START_REQUEST="START_REQUEST",t.STREAM_UPDATE="STREAM_UPDATE",t.STREAM_COMPLETE="STREAM_COMPLETE",t.STREAM_ERROR="STREAM_ERROR",t.DISMISS="DISMISS",t.ACCEPT="ACCEPT"}(ht||(ht={}));const Rt={debounceMs:500,maxContextLength:2e3,ghostTextClass:"autoCompleteGhostText"},Ct=new WeakMap;function Mt(t){const e=Ct.get(t);e&&(e.abort(),Ct.delete(t))}function Pt(t){return Ct.has(t)}function At(t,e){if(!e)return e;const{doc:n,selection:s}=t.state,a=s.from;if(a<=0)return e;const o=n.textBetween(a-1,a,""),r=e.charAt(0),i=/[\s\-]/;return!i.test(o)&&!i.test(r)?` ${e}`:e}const xt=new n("autoCompletePlugin"),It={status:yt.IDLE,enabled:!0,suggestion:"",cursorPos:0};function Nt(t,e){switch(e.type){case ht.SET_ENABLED:return{...It,enabled:e.enabled,systemPrompt:e.systemPrompt??t.systemPrompt};case ht.SET_SYSTEM_PROMPT:return{...t,systemPrompt:e.systemPrompt};case ht.START_DEBOUNCE:return{...t,status:yt.DEBOUNCING,cursorPos:e.cursorPos,suggestion:"",error:void 0};case ht.START_REQUEST:return t.status!==yt.DEBOUNCING?t:{...t,status:yt.PENDING};case ht.STREAM_UPDATE:return t.status!==yt.PENDING&&t.status!==yt.STREAMING?t:{...t,status:yt.STREAMING,suggestion:e.suggestion};case ht.STREAM_COMPLETE:return t.status!==yt.PENDING&&t.status!==yt.STREAMING?t:{...t,status:yt.SHOWING,suggestion:e.suggestion};case ht.STREAM_ERROR:return{...t,status:yt.IDLE,error:e.error,suggestion:""};case ht.DISMISS:case ht.ACCEPT:return{...t,status:yt.IDLE,suggestion:""};default:return t}}function Dt(t,e={}){const n={...Rt,...e},{debounceMs:a,maxContextLength:o,apiEndpoint:r,model:i,ghostTextClass:p,systemPrompt:u}=n;let d=null,m=!1,g=null,E=null;return new s({key:xt,state:{init:()=>({...It,systemPrompt:u}),apply(t,e,n,s){const a=t.getMeta(xt);if(a)return Nt(e,a);if(t.docChanged&&(e.status===yt.SHOWING||e.status===yt.STREAMING)){if(s.selection.from!==e.cursorPos)return Nt(e,{type:ht.DISMISS})}return!t.selectionSet||t.docChanged||e.status!==yt.SHOWING&&e.status!==yt.STREAMING?e:Nt(e,{type:ht.DISMISS})}},props:{decorations(t){const e=xt.getState(t);if(!e||!e.enabled)return l.empty;if((e.status===yt.STREAMING||e.status===yt.SHOWING)&&e.suggestion){const n=document.createElement("span");n.className=p,n.textContent=e.suggestion,n.setAttribute("contenteditable","false");const s=c.widget(e.cursorPos,n,{side:1});return l.create(t.doc,[s])}return l.empty},handleKeyDown(t,e){const n=xt.getState(t.state);return!(!n||!n.enabled)&&("Tab"===e.key&&!e.shiftKey&&n.status===yt.SHOWING&&n.suggestion?(e.preventDefault(),g=n.suggestion,E=n.cursorPos,t.dispatch(t.state.tr.setMeta(xt,{type:ht.ACCEPT})),!0):"Escape"===e.key&&(n.status===yt.STREAMING||n.status===yt.SHOWING)&&(e.preventDefault(),Mt(t),t.dispatch(t.state.tr.setMeta(xt,{type:ht.DISMISS})),!0))}},view:()=>({update(e,n){const s=xt.getState(e.state),c=xt.getState(n);if(s&&s.enabled){if(null!==g&&null!==E){const t=g,n=E;g=null,E=null;const s=e.state.tr.insertText(t,n);return e.dispatch(s),void e.focus()}if(e.state,e.state.doc!==n.doc&&c?.status!==yt.PENDING&&c?.status!==yt.STREAMING){e.state.tr,d&&(clearTimeout(d),d=null),Mt(e);const t=e.state.selection.from;e.dispatch(e.state.tr.setMeta(xt,{type:ht.START_DEBOUNCE,cursorPos:t})),d=setTimeout(()=>{d=null;const t=xt.getState(e.state);t?.status===yt.DEBOUNCING&&e.dispatch(e.state.tr.setMeta(xt,{type:ht.START_REQUEST}))},a)}if(s.status===yt.PENDING&&!m){if(!function(t){const{doc:e,selection:n}=t.state,s=n.from,a=e.resolve(s),o=a.parent,r=a.start();if(s!==r+o.content.size)return!1;const i=o.textBetween(0,s-r,"");return!!i.trim()&&!/[.!?]\s*$/.test(i)}(e))return void e.dispatch(e.state.tr.setMeta(xt,{type:ht.DISMISS}));const n=function(t,e){const{doc:n,selection:s}=t.state,a=s.from,o=n.textBetween(0,a,"\n");return o.length>e?o.slice(-e):o}(e,o);if(!n.trim())return void e.dispatch(e.state.tr.setMeta(xt,{type:ht.DISMISS}));m=!0;const s=xt.getState(e.state)?.systemPrompt;(async function({view:t,pluginKey:e,apiKey:n,context:s,apiEndpoint:a,model:o,systemPrompt:r}){Mt(t);const i=new AbortController;Ct.set(t,i);try{await b({apiKey:n,text:s,task:r?R.Custom:h.SmallComplete,...r&&{params:{systemPrompt:r}},endpoint:a,model:o,signal:i.signal},{onChunk:(n,s)=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:ht.STREAM_UPDATE,suggestion:At(t,s)}))},onComplete:n=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:ht.STREAM_COMPLETE,suggestion:At(t,n)}))},onError:n=>{console.error("Auto-complete streaming error:",n),t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:ht.STREAM_ERROR,error:n.message}))}})}finally{Ct.delete(t)}})({view:e,pluginKey:xt,apiKey:t,context:n,apiEndpoint:r,model:i,systemPrompt:s}).finally(()=>{m=!1})}}else d&&(clearTimeout(d),d=null)},destroy(){d&&(clearTimeout(d),d=null)}})})}function wt(t,e,n){e||Mt(t),t.dispatch(t.state.tr.setMeta(xt,{type:ht.SET_ENABLED,enabled:e,systemPrompt:n}))}function _t(t,e){t.dispatch(t.state.tr.setMeta(xt,{type:ht.SET_SYSTEM_PROMPT,systemPrompt:e}))}function Lt(t,e){t.dispatch(t.state.tr.setMeta(xt,{type:ht.SET_ENABLED,enabled:!0,systemPrompt:e}))}function bt(t){const e=xt.getState(t.state);if(!e||!e.suggestion)return!1;const n=t.state.tr.insertText(e.suggestion,e.cursorPos);return t.dispatch(n),t.dispatch(t.state.tr.setMeta(xt,{type:ht.DISMISS})),t.focus(),!0}function vt(t){Mt(t),t.dispatch(t.state.tr.setMeta(xt,{type:ht.DISMISS}))}function Ot(t){return xt.getState(t.state)}function kt(t){const e=xt.getState(t.state);return e?.enabled??!1}function Gt(t){const e=xt.getState(t.state);return!!e?.suggestion}const Ut=V;export{R as AiPromptsWithParam,h as AiPromptsWithoutParam,ht as AutoCompleteActionType,yt as AutoCompleteStatus,x as CompleteActionType,A as CompleteStatus,N as DEFAULT_COMPLETION_ENDPOINT,D as DEFAULT_GRAMMAR_ENDPOINT,w as DEFAULT_MODEL,f as GrammarSuggestElementClass,S as GrammarSuggestMetaType,T as MoodParamType,M as OpenAiPromptsWithParam,C as OpenAiPromptsWithoutParam,P as Status,y as TranslationTargetLanguage,bt as acceptAutoCompletion,W as acceptResult,it as acceptSuggestion,Lt as autoCompleteInit,xt as autoCompleteKey,Dt as autoCompletePlugin,k as cancelActiveRequest,Mt as cancelAutoCompleteRequest,F as cancelTask,Y as clearError,Ut as completePlugin,z as completePluginKey,V as completePluginV2,U as completeV2Key,_ as createApiConfig,L as createGrammarApiConfig,ot as createGrammarProcessor,Rt as defaultAutoCompleteOptions,Q as defaultCompleteOptions,I as defaultCompleteV2Options,J as defaultOptions,pt as deselectSuggestion,ct as discardSuggestion,vt as dismissAutoCompletion,Ot as getAutoCompleteState,et as getChangedRegions,j as getCompleteState,ut as getSelectedDecoration,nt as grammarDecorationFactory,st as grammarDecorationTransformer,v as grammarRequest,mt as grammarSuggestInit,X as grammarSuggestPluginKey,Tt as grammarSuggestPluginV2,St as grammarSuggestV2Key,at as grammarWidgetFactory,G as hasActiveRequest,Pt as hasAutoCompleteRequest,Gt as hasAutoCompletion,kt as isAutoCompleteEnabled,H as rejectResult,gt as requestHint,lt as selectSuggestion,wt as setAutoCompleteEnabled,_t as setAutoCompleteSystemPrompt,tt as setCompleteEnabled,$ as setEnabled,Z as setGrammarSuggestEnabled,dt as setGrammarSystemPrompt,q as startCustomTask,B as startTask,b as streamingRequest};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t,e,o,s,r,n=require("prosemirror-model"),a=require("prosemirror-state"),i=require("fast-diff"),p=require("@emergence-engineering/fast-diff-merge"),l=require("prosemirror-view"),c=require("@emergence-engineering/prosemirror-block-runner");exports.GrammarSuggestMetaType=void 0,(t=exports.GrammarSuggestMetaType||(exports.GrammarSuggestMetaType={})).suggestionUpdate="suggestionUpdate",t.acceptSuggestion="acceptSuggestion",t.openSuggestion="openSuggestion",t.closeSuggestion="closeSuggestion",t.discardSuggestion="discardSuggestion",t.setEnabled="setEnabled",exports.GrammarSuggestElementClass=void 0,(exports.GrammarSuggestElementClass||(exports.GrammarSuggestElementClass={})).grammarSuggestPopup="grammar-suggest-popup",exports.MoodParamType=void 0,(e=exports.MoodParamType||(exports.MoodParamType={})).Casual="Casual",e.Confident="Confident",e.Straightforward="Straightforward",e.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.AiPromptsWithoutParam=void 0,(s=exports.AiPromptsWithoutParam||(exports.AiPromptsWithoutParam={})).Complete="Complete",s.SmallComplete="SmallComplete",s.Improve="Improve",s.MakeLonger="MakeLonger",s.MakeShorter="MakeShorter",s.Simplify="Simplify",s.Explain="Explain",s.ActionItems="ActionItems",exports.AiPromptsWithParam=void 0,(r=exports.AiPromptsWithParam||(exports.AiPromptsWithParam={})).ChangeTone="ChangeTone",r.Translate="Translate",r.Hint="Hint",r.Custom="Custom";const u=exports.AiPromptsWithoutParam,m=exports.AiPromptsWithParam;var d,g,S;exports.Status=void 0,(d=exports.Status||(exports.Status={})).idle="idle",d.new="new",d.streaming="streaming",d.finished="finished",d.accepted="accepted",d.cancelled="cancelled",d.rejected="rejected",d.done="done",d.error="error",exports.CompleteStatus=void 0,(g=exports.CompleteStatus||(exports.CompleteStatus={})).IDLE="idle",g.PENDING="pending",g.STREAMING="streaming",g.PREVIEW="preview",g.APPLYING="applying",exports.CompleteActionType=void 0,(S=exports.CompleteActionType||(exports.CompleteActionType={})).START_TASK="START_TASK",S.STREAM_UPDATE="STREAM_UPDATE",S.STREAM_COMPLETE="STREAM_COMPLETE",S.STREAM_ERROR="STREAM_ERROR",S.ACCEPT_RESULT="ACCEPT_RESULT",S.REJECT_RESULT="REJECT_RESULT",S.CANCEL_TASK="CANCEL_TASK",S.CLEAR_ERROR="CLEAR_ERROR",S.SET_ENABLED="SET_ENABLED";const x={maxSelection:1e3},E="https://suggestion-gw5lxik4dq-uc.a.run.app",T="https://prosemirror-ai-plugin.web.app/api/suggestion",A="openai:gpt-4o-mini";async function C(t,e){const{apiKey:o,text:s,task:r,params:n,endpoint:a=E,model:i=A,signal:p}=t;let l="";const c={model:i,modelParams:{input:[s],task:r,params:n}};try{const t=await fetch(a,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o}`},signal:p,body:JSON.stringify(c)});if(!t.ok)throw new Error(`HTTP error! status: ${t.status}`);const s=t.body?.getReader();if(!s)throw new Error("No response body");for(;;){const{done:t,value:o}=await s.read();if(t)break;if(p?.aborted)return;const r=(new TextDecoder).decode(o);l+=r,e.onChunk(r,l)}p?.aborted||e.onComplete(l)}catch(t){if(t instanceof Error&&"AbortError"===t.name)return;e.onError(t instanceof Error?t:new Error(String(t)))}}async function y(t){const{apiKey:e,text:o,endpoint:s=T,model:r=A}=t,n={model:r,modelParams:{input:[...o.split("\n")]}};try{const t=await fetch(s,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(n)});if(!t.ok){const e=await t.text();return console.error({status:t.status,text:e}),{fixed:!1,result:o,error:!0}}const r=await t.json();return r?.length?{result:r.join("\n"),fixed:!0}:{fixed:!1,result:o}}catch(t){return console.error("Grammar request error:",t),{fixed:!1,result:o,error:!0}}}const f=new WeakMap;function h(t){const e=f.get(t);e&&(e.abort(),f.delete(t))}const R=new a.PluginKey("completePluginV2"),P={status:exports.CompleteStatus.IDLE,streamedResult:"",enabled:!0};function M(t,e={}){const{maxSelection:o,apiEndpoint:s,model:r}={...x,...e};let i=!1;return new a.Plugin({key:R,state:{init:()=>({...P}),apply(t,e){const o=t.getMeta(R);return o?function(t,e){switch(e.type){case exports.CompleteActionType.SET_ENABLED:return{...t,enabled:e.enabled,...e.enabled?{}:P};case exports.CompleteActionType.START_TASK:return t.status!==exports.CompleteStatus.IDLE?t:{...t,status:exports.CompleteStatus.PENDING,taskType:e.taskType,params:e.params,selection:e.selection,streamedResult:"",error:void 0};case exports.CompleteActionType.STREAM_UPDATE:return t.status!==exports.CompleteStatus.PENDING&&t.status!==exports.CompleteStatus.STREAMING?t:{...t,status:exports.CompleteStatus.STREAMING,streamedResult:e.result};case exports.CompleteActionType.STREAM_COMPLETE:return t.status!==exports.CompleteStatus.PENDING&&t.status!==exports.CompleteStatus.STREAMING?t:{...t,status:exports.CompleteStatus.PREVIEW,streamedResult:e.result};case exports.CompleteActionType.STREAM_ERROR:return{...t,status:exports.CompleteStatus.IDLE,error:e.error,streamedResult:"",taskType:void 0,params:void 0,selection:void 0};case exports.CompleteActionType.ACCEPT_RESULT:return t.status!==exports.CompleteStatus.PREVIEW?t:{...t,status:exports.CompleteStatus.APPLYING};case exports.CompleteActionType.REJECT_RESULT:return t.status!==exports.CompleteStatus.PREVIEW?t:{...P,enabled:t.enabled};case exports.CompleteActionType.CANCEL_TASK:return{...P,enabled:t.enabled};case exports.CompleteActionType.CLEAR_ERROR:return{...t,error:void 0};default:return t}}(e,o):e.status===exports.CompleteStatus.APPLYING?{...P,enabled:e.enabled}:e}},view:()=>({update(e){const p=R.getState(e.state);if(p&&p.enabled){if(p.status===exports.CompleteStatus.PENDING&&!i){const{text:n,error:a}=function(t,e,o){const{taskType:s,selection:r}=e;if(s===exports.AiPromptsWithoutParam.Complete){const{doc:e}=t.state,o=[];return e.descendants(t=>{"paragraph"===t.type.name&&o.push(t.textContent)}),{text:o.length>=2?o.slice(-2).join(" "):o.join(" ")}}return r?t.state.doc.textBetween(r.from,r.to).length>o?{text:"",error:"Selection is too big"}:{text:t.state.doc.textBetween(r.from,r.to,"\n")}:{text:"",error:"No selection"}}(e,p,o);if(a)return void e.dispatch(e.state.tr.setMeta(R,{type:exports.CompleteActionType.STREAM_ERROR,error:a}));if(!n)return void e.dispatch(e.state.tr.setMeta(R,{type:exports.CompleteActionType.STREAM_ERROR,error:"No text to process"}));i=!0,async function({view:t,pluginKey:e,apiKey:o,text:s,task:r,params:n,apiEndpoint:a,model:i}){h(t);const p=new AbortController;f.set(t,p);try{await C({apiKey:o,text:s,task:r,params:n,endpoint:a,model:i,signal:p.signal},{onChunk:(o,s)=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.CompleteActionType.STREAM_UPDATE,result:s}))},onComplete:o=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.CompleteActionType.STREAM_COMPLETE,result:o}))},onError:o=>{console.error("Streaming request error:",o),t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.CompleteActionType.STREAM_ERROR,error:o.message}))}})}finally{f.delete(t)}}({view:e,pluginKey:R,apiKey:t,text:n,task:p.taskType,params:p.params,apiEndpoint:s,model:r}).finally(()=>{i=!1})}p.status===exports.CompleteStatus.APPLYING&&function(t,e){const{taskType:o,selection:s,streamedResult:r}=e;if(!r)return;let{tr:i}=t.state;if(o===exports.AiPromptsWithoutParam.Complete)i=i.insertText(r,t.state.doc.nodeSize-2);else if(s){const e=r.split("\n\n").map(e=>t.state.schema.node("paragraph",null,e?t.state.schema.text(e):void 0)),o=n.Fragment.fromArray(e);i=i.setSelection(a.TextSelection.create(t.state.doc,s.from,s.to)),i.selection.replace(i,new n.Slice(o,0,0))}t.dispatch(i),t.focus()}(e,p)}},destroy(){}})})}const D={debounceMs:2e3,createUpdatePopup:(t,e,o,s,r)=>{const n=document.createElement("div");n.className="grammar-suggest-tooltip";const{spec:a}=e,i=t.dom.getBoundingClientRect();n.id=exports.GrammarSuggestElementClass.grammarSuggestPopup;const p=t.coordsAtPos(o),l=t.dom.scrollTop||0,c=t.dom.scrollLeft||0;n.style.left=`${p.left-i.left+c}px`,n.style.top=`${p.bottom-i.top+l+5}px`;const u=document.createElement("div");u.className="grammar-suggest-tooltip-apply",u.innerText=a.text||a.originalText,a.text||(u.style.textDecoration="line-through",u.style.color="red"),u.onclick=()=>{s(t,e)},n.appendChild(u);const m=document.createElement("div");return m.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>",m.className="grammar-suggest-tooltip-discard",m.onclick=()=>{r(t,e)},n.appendChild(m),n},withYjs:!1},I=new a.PluginKey("completePlugin"),N=new a.PluginKey("grammarSuggestPlugin"),L=(t,e)=>t.fixed&&0!==t.suggestions.length?t.suggestions.map(o=>{const s=e.from+1+c.textPosToDocPos(o.from,e.mapping),r=e.from+1+c.textPosToDocPos(o.to,e.mapping),n=""===o.replacement,a={id:{},unitId:e.id,originalText:o.original,replacement:o.replacement,response:t};return l.Decoration.inline(s,r,{class:"grammarSuggestionV2 "+(n?"removalSuggestionV2":"")},a)}):[],b=(t,e)=>{const o=e.contextState.selectedSuggestionId;return o?t.map(t=>{const e=t.spec;return e.id===o?l.Decoration.inline(t.from,t.to,{class:"grammarSuggestionV2 grammarSuggestionV2-selected"},e):t}):t},w=t=>{let e,o;const s=Math.max(0,t.waitUntil-Date.now());switch(t.status){case c.UnitStatus.QUEUED:case c.UnitStatus.WAITING:e="⏳",o="grammarWidgetV2 queued";break;case c.UnitStatus.PROCESSING:e="🔍",o="grammarWidgetV2 processing";break;case c.UnitStatus.BACKOFF:e=`🔄 ${Math.ceil(s/1e3)}s`,o="grammarWidgetV2 backoff";break;case c.UnitStatus.ERROR:e="❌",o="grammarWidgetV2 error";break;case c.UnitStatus.DIRTY:e="✏️",o="grammarWidgetV2 dirty";break;default:return}const r=document.createElement("span");if(r.className=o,r.textContent=e,t.status===c.UnitStatus.BACKOFF&&s>0){const e=setInterval(()=>{const o=Math.max(0,t.waitUntil-Date.now());r.textContent=`🔄 ${Math.ceil(o/1e3)}s`,(o<=0||!r.isConnected)&&clearInterval(e)},1e3)}return l.Decoration.widget(t.from+1,r,{side:-1})};const v=t=>{const{apiKey:e,apiEndpoint:o,model:s,modelStateManager:r}="string"==typeof t?{apiKey:t,apiEndpoint:void 0,model:void 0,modelStateManager:void 0}:t;return async(t,n)=>{try{const t=r?.getCurrentModel()??s,l=await y({apiKey:e,text:n.text,endpoint:o,model:t});if(l.error)return r?.handleFailure(),{error:new Error("Grammar API error")};if(r?.handleSuccess(),!l.fixed)return{data:{fixed:!1,originalText:n.text,fixedText:n.text,suggestions:[]}};const c=(a=n.text,i=l.result,p.getDiff(a,i).filter(t=>!p.isIdentity(t)).filter(t=>t.original!==`${t.replacement}\n`).map(t=>({from:t.from,to:t.replacement.endsWith("\n")?t.to-1:t.to,original:t.original,replacement:t.replacement.endsWith("\n")?t.replacement.slice(0,-1):t.replacement})));return{data:{fixed:c.length>0,originalText:n.text,fixedText:l.result,suggestions:c}}}catch(t){return r?.handleFailure(),{error:t instanceof Error?t:new Error(String(t))}}var a,i}};function _(t,e,o){const s=e.getState(t.state);if(s)return s.decorations.find(t=>t.spec.id===o)}function O(t,e,o){const s=_(t,e,o);if(!s)return;const r=s.spec,{replacement:i}=r,{from:p,to:l}=s,u=i.split("\n").map(e=>e?t.state.schema.node("paragraph",null,t.state.schema.text(e)):t.state.schema.node("paragraph")),m=n.Fragment.fromArray(u);let{tr:d}=t.state;d=d.setSelection(a.TextSelection.create(t.state.doc,p,l)),d.selection.replace(d,new n.Slice(m,1,1)),d=d.setMeta(e,{type:c.ActionType.REMOVE_DECORATION,id:o}),t.dispatch(d)}function G(t,e,o){c.dispatchAction(t,e,{type:c.ActionType.REMOVE_DECORATION,id:o})}function k(t,e,o){c.dispatchAction(t,e,{type:c.ActionType.UPDATE_CONTEXT,contextState:{selectedSuggestionId:o}})}function U(t,e){c.dispatchAction(t,e,{type:c.ActionType.UPDATE_CONTEXT,contextState:{selectedSuggestionId:void 0}})}async function K(t,e,o,s){const r=s?.endpoint??E,n=s?.modelStateManager,a=n?.getCurrentModel()??s?.model??A;return new Promise((s,i)=>{const p={previousPromptType:"Grammar",oldVersion:e,newVersion:o};C({apiKey:t,text:e,task:exports.AiPromptsWithParam.Hint,params:p,endpoint:r,model:a},{onChunk:()=>{},onComplete:t=>{n?.handleSuccess(),s(t)},onError:t=>{n?.handleFailure(),i(t)}})})}const W={selectedSuggestionId:void 0},V=c.createBlockRunnerKey("grammarSuggestV2"),B=(t,e,o,s,r,n)=>{const a=e.spec,i=document.createElement("div");i.className="grammarPopupV2";const p=document.createElement("div");p.className="grammarPopupV2-mainRow";const l=document.createElement("span");l.className="grammarPopupV2-original",l.textContent=`"${a.originalText}"`;const c=document.createElement("span");c.className="grammarPopupV2-arrow",c.textContent=" → ";const u=document.createElement("span");u.className="grammarPopupV2-replacement",u.textContent=""===a.replacement?"(remove)":`"${a.replacement}"`;const m=document.createElement("button");m.className="grammarPopupV2-hint",m.textContent="?",m.title="Why this suggestion?";const d=document.createElement("div");d.className="grammarPopupV2-hintArea",d.style.display="none";let g=!1;m.onclick=async t=>{if(t.preventDefault(),t.stopPropagation(),"none"===d.style.display){if(d.style.display="block",!g){d.innerHTML='<span class="grammarPopupV2-loading">Loading...</span>';try{const t=await n();d.innerHTML="";const e=document.createElement("span");e.className="grammarPopupV2-hintText",e.textContent=t,d.appendChild(e),g=!0}catch{d.innerHTML='<span class="grammarPopupV2-hintError">Could not load hint</span>'}}}else d.style.display="none"};const S=document.createElement("button");S.className="grammarPopupV2-accept",S.textContent="✓",S.onclick=t=>{t.preventDefault(),t.stopPropagation(),s()};const x=document.createElement("button");return x.className="grammarPopupV2-discard",x.textContent="✕",x.onclick=t=>{t.preventDefault(),t.stopPropagation(),r()},p.appendChild(l),p.appendChild(c),p.appendChild(u),p.appendChild(m),p.appendChild(S),p.appendChild(x),i.appendChild(p),i.appendChild(d),i};var F,q;exports.AutoCompleteStatus=void 0,(F=exports.AutoCompleteStatus||(exports.AutoCompleteStatus={})).IDLE="idle",F.DEBOUNCING="debouncing",F.PENDING="pending",F.STREAMING="streaming",F.SHOWING="showing",exports.AutoCompleteActionType=void 0,(q=exports.AutoCompleteActionType||(exports.AutoCompleteActionType={})).SET_ENABLED="SET_ENABLED",q.START_DEBOUNCE="START_DEBOUNCE",q.START_REQUEST="START_REQUEST",q.STREAM_UPDATE="STREAM_UPDATE",q.STREAM_COMPLETE="STREAM_COMPLETE",q.STREAM_ERROR="STREAM_ERROR",q.DISMISS="DISMISS",q.ACCEPT="ACCEPT";const H={debounceMs:500,maxContextLength:2e3,ghostTextClass:"autoCompleteGhostText"},$=new WeakMap;function j(t){const e=$.get(t);e&&(e.abort(),$.delete(t))}function J(t,e){if(!e)return e;const{doc:o,selection:s}=t.state,r=s.from;if(r<=0)return e;const n=o.textBetween(r-1,r,""),a=e.charAt(0),i=/[\s\-]/;return!i.test(n)&&!i.test(a)?` ${e}`:e}const Q=new a.PluginKey("autoCompletePlugin"),z={status:exports.AutoCompleteStatus.IDLE,enabled:!0,suggestion:"",cursorPos:0};function Y(t,e){switch(e.type){case exports.AutoCompleteActionType.SET_ENABLED:return{...z,enabled:e.enabled};case exports.AutoCompleteActionType.START_DEBOUNCE:return{...t,status:exports.AutoCompleteStatus.DEBOUNCING,cursorPos:e.cursorPos,suggestion:"",error:void 0};case exports.AutoCompleteActionType.START_REQUEST:return t.status!==exports.AutoCompleteStatus.DEBOUNCING?t:{...t,status:exports.AutoCompleteStatus.PENDING};case exports.AutoCompleteActionType.STREAM_UPDATE:return t.status!==exports.AutoCompleteStatus.PENDING&&t.status!==exports.AutoCompleteStatus.STREAMING?t:{...t,status:exports.AutoCompleteStatus.STREAMING,suggestion:e.suggestion};case exports.AutoCompleteActionType.STREAM_COMPLETE:return t.status!==exports.AutoCompleteStatus.PENDING&&t.status!==exports.AutoCompleteStatus.STREAMING?t:{...t,status:exports.AutoCompleteStatus.SHOWING,suggestion:e.suggestion};case exports.AutoCompleteActionType.STREAM_ERROR:return{...t,status:exports.AutoCompleteStatus.IDLE,error:e.error,suggestion:""};case exports.AutoCompleteActionType.DISMISS:case exports.AutoCompleteActionType.ACCEPT:return{...t,status:exports.AutoCompleteStatus.IDLE,suggestion:""};default:return t}}const Z=M;Object.defineProperty(exports,"getDiff",{enumerable:!0,get:function(){return p.getDiff}}),exports.DEFAULT_COMPLETION_ENDPOINT=E,exports.DEFAULT_GRAMMAR_ENDPOINT=T,exports.DEFAULT_MODEL=A,exports.OpenAiPromptsWithParam=m,exports.OpenAiPromptsWithoutParam=u,exports.acceptAutoCompletion=function(t){const e=Q.getState(t.state);if(!e||!e.suggestion)return!1;const o=t.state.tr.insertText(e.suggestion,e.cursorPos);return t.dispatch(o),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS})),t.focus(),!0},exports.acceptResult=function(t){t.dispatch(t.state.tr.setMeta(R,{type:exports.CompleteActionType.ACCEPT_RESULT}))},exports.acceptSuggestion=O,exports.autoCompleteKey=Q,exports.autoCompletePlugin=function(t,e={}){const o={...H,...e},{debounceMs:s,maxContextLength:r,apiEndpoint:n,model:i,ghostTextClass:p}=o;let c=null,u=!1,m=null,d=null;return new a.Plugin({key:Q,state:{init:()=>({...z}),apply(t,e,o,s){const r=t.getMeta(Q);if(r)return Y(e,r);if(t.docChanged&&(e.status===exports.AutoCompleteStatus.SHOWING||e.status===exports.AutoCompleteStatus.STREAMING)){if(s.selection.from!==e.cursorPos)return Y(e,{type:exports.AutoCompleteActionType.DISMISS})}return!t.selectionSet||t.docChanged||e.status!==exports.AutoCompleteStatus.SHOWING&&e.status!==exports.AutoCompleteStatus.STREAMING?e:Y(e,{type:exports.AutoCompleteActionType.DISMISS})}},props:{decorations(t){const e=Q.getState(t);if(!e||!e.enabled)return l.DecorationSet.empty;if((e.status===exports.AutoCompleteStatus.STREAMING||e.status===exports.AutoCompleteStatus.SHOWING)&&e.suggestion){const o=document.createElement("span");o.className=p,o.textContent=e.suggestion,o.setAttribute("contenteditable","false");const s=l.Decoration.widget(e.cursorPos,o,{side:1});return l.DecorationSet.create(t.doc,[s])}return l.DecorationSet.empty},handleKeyDown(t,e){const o=Q.getState(t.state);return!(!o||!o.enabled)&&("Tab"===e.key&&!e.shiftKey&&o.status===exports.AutoCompleteStatus.SHOWING&&o.suggestion?(e.preventDefault(),m=o.suggestion,d=o.cursorPos,t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.ACCEPT})),!0):"Escape"===e.key&&(o.status===exports.AutoCompleteStatus.STREAMING||o.status===exports.AutoCompleteStatus.SHOWING)&&(e.preventDefault(),j(t),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS})),!0))}},view:()=>({update(e,o){const a=Q.getState(e.state),p=Q.getState(o);if(a&&a.enabled){if(null!==m&&null!==d){const t=m,o=d;m=null,d=null;const s=e.state.tr.insertText(t,o);return e.dispatch(s),void e.focus()}if(e.state,e.state.doc!==o.doc&&p?.status!==exports.AutoCompleteStatus.PENDING&&p?.status!==exports.AutoCompleteStatus.STREAMING){e.state.tr,c&&(clearTimeout(c),c=null),j(e);const t=e.state.selection.from;e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.START_DEBOUNCE,cursorPos:t})),c=setTimeout(()=>{c=null;const t=Q.getState(e.state);t?.status===exports.AutoCompleteStatus.DEBOUNCING&&e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.START_REQUEST}))},s)}if(a.status===exports.AutoCompleteStatus.PENDING&&!u){if(!function(t){const{doc:e,selection:o}=t.state,s=o.from,r=e.resolve(s),n=r.parent,a=r.start();if(s!==a+n.content.size)return!1;const i=n.textBetween(0,s-a,"");return!!i.trim()&&!/[.!?]\s*$/.test(i)}(e))return void e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS}));const o=function(t,e){const{doc:o,selection:s}=t.state,r=s.from,n=o.textBetween(0,r,"\n");return n.length>e?n.slice(-e):n}(e,r);if(!o.trim())return void e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS}));u=!0,async function({view:t,pluginKey:e,apiKey:o,context:s,apiEndpoint:r,model:n}){j(t);const a=new AbortController;$.set(t,a);try{await C({apiKey:o,text:s,task:exports.AiPromptsWithoutParam.SmallComplete,endpoint:r,model:n,signal:a.signal},{onChunk:(o,s)=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.AutoCompleteActionType.STREAM_UPDATE,suggestion:J(t,s)}))},onComplete:o=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.AutoCompleteActionType.STREAM_COMPLETE,suggestion:J(t,o)}))},onError:o=>{console.error("Auto-complete streaming error:",o),t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.AutoCompleteActionType.STREAM_ERROR,error:o.message}))}})}finally{$.delete(t)}}({view:e,pluginKey:Q,apiKey:t,context:o,apiEndpoint:n,model:i}).finally(()=>{u=!1})}}else c&&(clearTimeout(c),c=null)},destroy(){c&&(clearTimeout(c),c=null)}})})},exports.cancelActiveRequest=h,exports.cancelAutoCompleteRequest=j,exports.cancelTask=function(t){h(t),t.dispatch(t.state.tr.setMeta(R,{type:exports.CompleteActionType.CANCEL_TASK}))},exports.clearError=function(t){t.dispatch(t.state.tr.setMeta(R,{type:exports.CompleteActionType.CLEAR_ERROR}))},exports.completePlugin=Z,exports.completePluginKey=I,exports.completePluginV2=M,exports.completeV2Key=R,exports.createApiConfig=function(t){return{apiKey:t.apiKey,endpoint:t.endpoint??E,model:t.model??A}},exports.createGrammarApiConfig=function(t){return{apiKey:t.apiKey,endpoint:t.endpoint??T,model:t.model??A}},exports.createGrammarProcessor=v,exports.defaultAutoCompleteOptions=H,exports.defaultCompleteOptions={maxSelection:1e3},exports.defaultCompleteV2Options=x,exports.defaultOptions=D,exports.deselectSuggestion=U,exports.discardSuggestion=G,exports.dismissAutoCompletion=function(t){j(t),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS}))},exports.getAutoCompleteState=function(t){return Q.getState(t.state)},exports.getChangedRegions=(t,e)=>{if(t===e)return{start:t.length,end:t.length,oldStart:t.length,oldEnd:t.length,oldText:"",newText:""};const o=i(t,e),s=o[0],r=s[0]===i.EQUAL?s[1].length-1:0,n=((t,e)=>{if(e[e.length-1][0]!==i.EQUAL)return t.length;const o=e.slice(0,e.length-1);let s=0;for(const t of o){const[e,o]=t;e===i.EQUAL&&(s+=o.length),e===i.DELETE&&(s+=o.length)}return s})(t,o),a=t.lastIndexOf("\n",r),p=t.indexOf("\n",n),l=-1===a?0:a+1,c=-1===p?t.length:p,u=t.slice(l,c),m=e.slice(l,e.length-(t.length-c));return{start:l,end:l+m.length,oldStart:l,oldEnd:c,oldText:u,newText:m}},exports.getCompleteState=function(t){return R.getState(t.state)},exports.getSelectedDecoration=function(t,e){const o=e.getState(t.state);if(o?.contextState.selectedSuggestionId)return _(t,e,o.contextState.selectedSuggestionId)},exports.grammarDecorationFactory=L,exports.grammarDecorationTransformer=b,exports.grammarRequest=y,exports.grammarSuggestPluginKey=N,exports.grammarSuggestPluginV2=function(t,e={}){const{apiEndpoint:o,model:s,fallback:r,batchSize:n=4,maxRetries:i=3,backoffBase:p=2e3,debounceMs:u=1e3,createPopup:m=B}=e,d=r?function(t){const{primaryModel:e,fallbackModel:o,failureThreshold:s}=t;let r=0;return{getCurrentModel:()=>o&&s>0&&r>=s?o:e,handleSuccess(){r=0},handleFailure(){o&&r++}}}({primaryModel:s,fallbackModel:r.fallbackModel,failureThreshold:r.failureThreshold??3}):void 0,g=v({apiKey:t,apiEndpoint:o,model:s,modelStateManager:d}),S=c.blockRunnerPlugin({pluginKey:V,unitProcessor:g,decorationFactory:L,decorationTransformer:b,widgetFactory:w,initialContextState:W,options:{batchSize:n,maxRetries:i,backoffBase:p,dirtyHandling:{debounceDelay:u}}});return new a.Plugin({key:V,state:S.spec.state,props:{...S.spec.props,decorations(e){const r=S.spec.props?.decorations,n=r?r.call(S,e):l.DecorationSet.empty;if("react"===m)return n;const a=V.getState(e);if(!a?.contextState.selectedSuggestionId)return n;const i=a.decorations.find(t=>t.spec.id===a.contextState.selectedSuggestionId);if(!i)return n;const p=i.spec,c=l.Decoration.widget(i.from,(e,r)=>{const n=r();return void 0===n?document.createElement("div"):m(e,i,n,()=>O(e,V,i.spec.id),()=>G(e,V,i.spec.id),()=>K(t,p.originalText,p.replacement,{endpoint:o,model:s,modelStateManager:d}))},{id:i.spec.id,side:-1,stopEvent:()=>!0});return n.add(e.doc,[c])},handleClick(t,e,o){const s=V.getState(t.state);if(!s)return!1;const r=s.decorations.find(t=>t.from<=e&&e<=t.to);return r?(r.spec.id===s.contextState.selectedSuggestionId||k(t,V,r.spec.id),!1):(s.contextState.selectedSuggestionId&&U(t,V),!1)}},view:S.spec.view})},exports.grammarSuggestV2Key=V,exports.grammarWidgetFactory=w,exports.hasActiveRequest=function(t){return f.has(t)},exports.hasAutoCompleteRequest=function(t){return $.has(t)},exports.hasAutoCompletion=function(t){const e=Q.getState(t.state);return!!e?.suggestion},exports.isAutoCompleteEnabled=function(t){const e=Q.getState(t.state);return e?.enabled??!1},exports.rejectResult=function(t){t.dispatch(t.state.tr.setMeta(R,{type:exports.CompleteActionType.REJECT_RESULT}))},exports.requestHint=K,exports.selectSuggestion=k,exports.setAutoCompleteEnabled=function(t,e){e||j(t),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.SET_ENABLED,enabled:e}))},exports.setCompleteEnabled=(t,e)=>{t.dispatch(t.state.tr.setMeta(I,{type:"setEnabled",enabled:e}))},exports.setEnabled=function(t,e){e||h(t),t.dispatch(t.state.tr.setMeta(R,{type:exports.CompleteActionType.SET_ENABLED,enabled:e}))},exports.setGrammarSuggestEnabled=(t,e)=>{t.dispatch(t.state.tr.setMeta(N,{type:exports.GrammarSuggestMetaType.setEnabled,enabled:e}))},exports.startTask=function(t,e,o){const s=t.state.selection;t.dispatch(t.state.tr.setMeta(R,{type:exports.CompleteActionType.START_TASK,taskType:e,params:o,selection:s.empty?void 0:s}))},exports.streamingRequest=C;
|
|
1
|
+
"use strict";var t,e,o,s,r,n=require("prosemirror-model"),a=require("prosemirror-state"),p=require("fast-diff"),i=require("@emergence-engineering/fast-diff-merge"),c=require("prosemirror-view"),l=require("@emergence-engineering/prosemirror-block-runner");exports.GrammarSuggestMetaType=void 0,(t=exports.GrammarSuggestMetaType||(exports.GrammarSuggestMetaType={})).suggestionUpdate="suggestionUpdate",t.acceptSuggestion="acceptSuggestion",t.openSuggestion="openSuggestion",t.closeSuggestion="closeSuggestion",t.discardSuggestion="discardSuggestion",t.setEnabled="setEnabled",exports.GrammarSuggestElementClass=void 0,(exports.GrammarSuggestElementClass||(exports.GrammarSuggestElementClass={})).grammarSuggestPopup="grammar-suggest-popup",exports.MoodParamType=void 0,(e=exports.MoodParamType||(exports.MoodParamType={})).Casual="Casual",e.Confident="Confident",e.Straightforward="Straightforward",e.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.AiPromptsWithoutParam=void 0,(s=exports.AiPromptsWithoutParam||(exports.AiPromptsWithoutParam={})).Complete="Complete",s.SmallComplete="SmallComplete",s.Improve="Improve",s.MakeLonger="MakeLonger",s.MakeShorter="MakeShorter",s.Simplify="Simplify",s.Explain="Explain",s.ActionItems="ActionItems",exports.AiPromptsWithParam=void 0,(r=exports.AiPromptsWithParam||(exports.AiPromptsWithParam={})).ChangeTone="ChangeTone",r.Translate="Translate",r.Hint="Hint",r.Custom="Custom";const u=exports.AiPromptsWithoutParam,m=exports.AiPromptsWithParam;var d,g,S;exports.Status=void 0,(d=exports.Status||(exports.Status={})).idle="idle",d.new="new",d.streaming="streaming",d.finished="finished",d.accepted="accepted",d.cancelled="cancelled",d.rejected="rejected",d.done="done",d.error="error",exports.CompleteStatus=void 0,(g=exports.CompleteStatus||(exports.CompleteStatus={})).IDLE="idle",g.PENDING="pending",g.STREAMING="streaming",g.PREVIEW="preview",g.APPLYING="applying",exports.CompleteActionType=void 0,(S=exports.CompleteActionType||(exports.CompleteActionType={})).START_TASK="START_TASK",S.STREAM_UPDATE="STREAM_UPDATE",S.STREAM_COMPLETE="STREAM_COMPLETE",S.STREAM_ERROR="STREAM_ERROR",S.ACCEPT_RESULT="ACCEPT_RESULT",S.REJECT_RESULT="REJECT_RESULT",S.CANCEL_TASK="CANCEL_TASK",S.CLEAR_ERROR="CLEAR_ERROR",S.SET_ENABLED="SET_ENABLED";const x={maxSelection:1e3},T="https://suggestion-gw5lxik4dq-uc.a.run.app",E="https://prosemirror-ai-plugin.web.app/api/suggestion",A="openai:gpt-4o-mini";async function C(t,e){const{apiKey:o,text:s,task:r,params:n,endpoint:a=T,model:p=A,signal:i}=t;let c="";const l={model:p,modelParams:{input:[s],task:r,params:n,stream:!0}};try{const t=await fetch(a,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o}`},signal:i,body:JSON.stringify(l)});if(!t.ok)throw new Error(`HTTP error! status: ${t.status}`);const s=t.body?.getReader();if(!s)throw new Error("No response body");for(;;){const{done:t,value:o}=await s.read();if(t)break;if(i?.aborted)return;const r=(new TextDecoder).decode(o);c+=r,e.onChunk(r,c)}i?.aborted||e.onComplete(c)}catch(t){if(t instanceof Error&&"AbortError"===t.name)return;e.onError(t instanceof Error?t:new Error(String(t)))}}async function y(t){const{apiKey:e,text:o,endpoint:s=E,model:r=A,task:n,params:a}=t,p={model:r,modelParams:{input:[...o.split("\n")],stream:!1,...n&&{task:n},...a&&{params:a}}};try{const t=await fetch(s,{method:"POST",cache:"no-cache",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(p)});if(!t.ok){const e=await t.text();return console.error({status:t.status,text:e}),{fixed:!1,result:o,error:!0}}const r=await t.json();return r?.length?{result:r.join("\n"),fixed:!0}:{fixed:!1,result:o}}catch(t){return console.error("Grammar request error:",t),{fixed:!1,result:o,error:!0}}}const f=new WeakMap;function h(t){const e=f.get(t);e&&(e.abort(),f.delete(t))}const P=new a.PluginKey("completePluginV2"),R={status:exports.CompleteStatus.IDLE,streamedResult:"",enabled:!0};function M(t,e={}){const{maxSelection:o,apiEndpoint:s,model:r}={...x,...e};let p=!1;return new a.Plugin({key:P,state:{init:()=>({...R}),apply(t,e){const o=t.getMeta(P);return o?function(t,e){switch(e.type){case exports.CompleteActionType.SET_ENABLED:return{...t,enabled:e.enabled,...e.enabled?{}:R};case exports.CompleteActionType.START_TASK:return t.status!==exports.CompleteStatus.IDLE?t:{...t,status:exports.CompleteStatus.PENDING,taskType:e.taskType,params:e.params,selection:e.selection,streamedResult:"",error:void 0};case exports.CompleteActionType.STREAM_UPDATE:return t.status!==exports.CompleteStatus.PENDING&&t.status!==exports.CompleteStatus.STREAMING?t:{...t,status:exports.CompleteStatus.STREAMING,streamedResult:e.result};case exports.CompleteActionType.STREAM_COMPLETE:return t.status!==exports.CompleteStatus.PENDING&&t.status!==exports.CompleteStatus.STREAMING?t:{...t,status:exports.CompleteStatus.PREVIEW,streamedResult:e.result};case exports.CompleteActionType.STREAM_ERROR:return{...t,status:exports.CompleteStatus.IDLE,error:e.error,streamedResult:"",taskType:void 0,params:void 0,selection:void 0};case exports.CompleteActionType.ACCEPT_RESULT:return t.status!==exports.CompleteStatus.PREVIEW?t:{...t,status:exports.CompleteStatus.APPLYING};case exports.CompleteActionType.REJECT_RESULT:return t.status!==exports.CompleteStatus.PREVIEW?t:{...R,enabled:t.enabled};case exports.CompleteActionType.CANCEL_TASK:return{...R,enabled:t.enabled};case exports.CompleteActionType.CLEAR_ERROR:return{...t,error:void 0};default:return t}}(e,o):e.status===exports.CompleteStatus.APPLYING?{...R,enabled:e.enabled}:e}},view:()=>({update(e){const i=P.getState(e.state);if(i&&i.enabled){if(i.status===exports.CompleteStatus.PENDING&&!p){const{text:n,error:a}=function(t,e,o){const{taskType:s,selection:r}=e;if(s===exports.AiPromptsWithoutParam.Complete){const{doc:e}=t.state,o=[];return e.descendants(t=>{"paragraph"===t.type.name&&o.push(t.textContent)}),{text:o.length>=2?o.slice(-2).join(" "):o.join(" ")}}return r?t.state.doc.textBetween(r.from,r.to).length>o?{text:"",error:"Selection is too big"}:{text:t.state.doc.textBetween(r.from,r.to,"\n")}:{text:"",error:"No selection"}}(e,i,o);if(a)return void e.dispatch(e.state.tr.setMeta(P,{type:exports.CompleteActionType.STREAM_ERROR,error:a}));if(!n)return void e.dispatch(e.state.tr.setMeta(P,{type:exports.CompleteActionType.STREAM_ERROR,error:"No text to process"}));p=!0,async function({view:t,pluginKey:e,apiKey:o,text:s,task:r,params:n,apiEndpoint:a,model:p}){h(t);const i=new AbortController;f.set(t,i);try{await C({apiKey:o,text:s,task:r,params:n,endpoint:a,model:p,signal:i.signal},{onChunk:(o,s)=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.CompleteActionType.STREAM_UPDATE,result:s}))},onComplete:o=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.CompleteActionType.STREAM_COMPLETE,result:o}))},onError:o=>{console.error("Streaming request error:",o),t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.CompleteActionType.STREAM_ERROR,error:o.message}))}})}finally{f.delete(t)}}({view:e,pluginKey:P,apiKey:t,text:n,task:i.taskType,params:i.params,apiEndpoint:s,model:r}).finally(()=>{p=!1})}i.status===exports.CompleteStatus.APPLYING&&function(t,e){const{taskType:o,selection:s,streamedResult:r}=e;if(!r)return;let{tr:p}=t.state;if(o===exports.AiPromptsWithoutParam.Complete)p=p.insertText(r,t.state.doc.nodeSize-2);else if(s){const e=r.split("\n\n").map(e=>t.state.schema.node("paragraph",null,e?t.state.schema.text(e):void 0)),o=n.Fragment.fromArray(e);p=p.setSelection(a.TextSelection.create(t.state.doc,s.from,s.to)),p.selection.replace(p,new n.Slice(o,0,0))}t.dispatch(p),t.focus()}(e,i)}},destroy(){}})})}function I(t,e,o){const s=t.state.selection;t.dispatch(t.state.tr.setMeta(P,{type:exports.CompleteActionType.START_TASK,taskType:e,params:o,selection:s.empty?void 0:s}))}const D={debounceMs:2e3,createUpdatePopup:(t,e,o,s,r)=>{const n=document.createElement("div");n.className="grammar-suggest-tooltip";const{spec:a}=e,p=t.dom.getBoundingClientRect();n.id=exports.GrammarSuggestElementClass.grammarSuggestPopup;const i=t.coordsAtPos(o),c=t.dom.scrollTop||0,l=t.dom.scrollLeft||0;n.style.left=`${i.left-p.left+l}px`,n.style.top=`${i.bottom-p.top+c+5}px`;const u=document.createElement("div");u.className="grammar-suggest-tooltip-apply",u.innerText=a.text||a.originalText,a.text||(u.style.textDecoration="line-through",u.style.color="red"),u.onclick=()=>{s(t,e)},n.appendChild(u);const m=document.createElement("div");return m.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>",m.className="grammar-suggest-tooltip-discard",m.onclick=()=>{r(t,e)},n.appendChild(m),n},withYjs:!1},N=new a.PluginKey("completePlugin"),_=new a.PluginKey("grammarSuggestPlugin"),L=(t,e)=>t.fixed&&0!==t.suggestions.length?t.suggestions.map(o=>{const s=e.from+1+l.textPosToDocPos(o.from,e.mapping),r=e.from+1+l.textPosToDocPos(o.to,e.mapping),n=""===o.replacement,a={id:{},unitId:e.id,originalText:o.original,replacement:o.replacement,response:t};return c.Decoration.inline(s,r,{class:"grammarSuggestionV2 "+(n?"removalSuggestionV2":"")},a)}):[],b=(t,e)=>{const o=e.contextState.selectedSuggestionId;return o?t.map(t=>{const e=t.spec;return e.id===o?c.Decoration.inline(t.from,t.to,{class:"grammarSuggestionV2 grammarSuggestionV2-selected"},e):t}):t},w=t=>{let e,o;const s=Math.max(0,t.waitUntil-Date.now());switch(t.status){case l.UnitStatus.QUEUED:case l.UnitStatus.WAITING:e="⏳",o="grammarWidgetV2 queued";break;case l.UnitStatus.PROCESSING:e="🔍",o="grammarWidgetV2 processing";break;case l.UnitStatus.BACKOFF:e=`🔄 ${Math.ceil(s/1e3)}s`,o="grammarWidgetV2 backoff";break;case l.UnitStatus.ERROR:e="❌",o="grammarWidgetV2 error";break;case l.UnitStatus.DIRTY:e="✏️",o="grammarWidgetV2 dirty";break;default:return}const r=document.createElement("span");if(r.className=o,r.textContent=e,t.status===l.UnitStatus.BACKOFF&&s>0){const e=setInterval(()=>{const o=Math.max(0,t.waitUntil-Date.now());r.textContent=`🔄 ${Math.ceil(o/1e3)}s`,(o<=0||!r.isConnected)&&clearInterval(e)},1e3)}return c.Decoration.widget(t.from+1,r,{side:-1})};const v=t=>{const{apiKey:e,apiEndpoint:o,model:s,modelStateManager:r,pluginKey:n}="string"==typeof t?{apiKey:t,apiEndpoint:void 0,model:void 0,modelStateManager:void 0,pluginKey:void 0}:t;return async(t,a)=>{try{const l=r?.getCurrentModel()??s,u=n?n.getState(t.state)?.contextState.systemPrompt:void 0,m=await y({apiKey:e,text:a.text,endpoint:o,model:l,...u&&{task:exports.AiPromptsWithParam.Custom,params:{systemPrompt:u}}});if(m.error)return r?.handleFailure(),{error:new Error("Grammar API error")};if(r?.handleSuccess(),!m.fixed)return{data:{fixed:!1,originalText:a.text,fixedText:a.text,suggestions:[]}};const d=(p=a.text,c=m.result,i.getDiff(p,c).filter(t=>!i.isIdentity(t)).filter(t=>t.original!==`${t.replacement}\n`).map(t=>({from:t.from,to:t.replacement.endsWith("\n")?t.to-1:t.to,original:t.original,replacement:t.replacement.endsWith("\n")?t.replacement.slice(0,-1):t.replacement})));return{data:{fixed:d.length>0,originalText:a.text,fixedText:m.result,suggestions:d}}}catch(t){return r?.handleFailure(),{error:t instanceof Error?t:new Error(String(t))}}var p,c}};function O(t,e,o){const s=e.getState(t.state);if(s)return s.decorations.find(t=>t.spec.id===o)}function k(t,e,o){const s=O(t,e,o);if(!s)return;const r=s.spec,{replacement:p}=r,{from:i,to:c}=s,u=p.split("\n").map(e=>e?t.state.schema.node("paragraph",null,t.state.schema.text(e)):t.state.schema.node("paragraph")),m=n.Fragment.fromArray(u);let{tr:d}=t.state;d=d.setSelection(a.TextSelection.create(t.state.doc,i,c)),d.selection.replace(d,new n.Slice(m,1,1)),d=d.setMeta(e,{type:l.ActionType.REMOVE_DECORATION,id:o}),t.dispatch(d)}function G(t,e,o){l.dispatchAction(t,e,{type:l.ActionType.REMOVE_DECORATION,id:o})}function U(t,e,o){l.dispatchAction(t,e,{type:l.ActionType.UPDATE_CONTEXT,contextState:{selectedSuggestionId:o}})}function K(t,e){l.dispatchAction(t,e,{type:l.ActionType.UPDATE_CONTEXT,contextState:{selectedSuggestionId:void 0}})}async function W(t,e,o,s){const r=s?.endpoint??T,n=s?.modelStateManager,a=n?.getCurrentModel()??s?.model??A;return new Promise((p,i)=>{const c=s?.systemPrompt?exports.AiPromptsWithParam.Custom:exports.AiPromptsWithParam.Hint,l=s?.systemPrompt?{systemPrompt:s.systemPrompt}:{previousPromptType:"Grammar",oldVersion:e,newVersion:o};C({apiKey:t,text:s?.systemPrompt?`Original: \n\`\`\`\n${e}\n\`\`\`\n\nReplacement: \n\`\`\`\n${o}\n\`\`\`\n`:e,task:c,params:l,endpoint:r,model:a},{onChunk:()=>{},onComplete:t=>{n?.handleSuccess(),p(t)},onError:t=>{n?.handleFailure(),i(t)}})})}const V={selectedSuggestionId:void 0},B=l.createBlockRunnerKey("grammarSuggestV2"),F=(t,e,o,s,r,n)=>{const a=e.spec,p=document.createElement("div");p.className="grammarPopupV2";const i=document.createElement("div");i.className="grammarPopupV2-mainRow";const c=document.createElement("span");c.className="grammarPopupV2-original",c.textContent=`"${a.originalText}"`;const l=document.createElement("span");l.className="grammarPopupV2-arrow",l.textContent=" → ";const u=document.createElement("span");u.className="grammarPopupV2-replacement",u.textContent=""===a.replacement?"(remove)":`"${a.replacement}"`;const m=document.createElement("button");m.className="grammarPopupV2-hint",m.textContent="?",m.title="Why this suggestion?";const d=document.createElement("div");d.className="grammarPopupV2-hintArea",d.style.display="none";let g=!1;m.onclick=async t=>{if(t.preventDefault(),t.stopPropagation(),"none"===d.style.display){if(d.style.display="block",!g){d.innerHTML='<span class="grammarPopupV2-loading">Loading...</span>';try{const t=await n();d.innerHTML="";const e=document.createElement("span");e.className="grammarPopupV2-hintText",e.textContent=t,d.appendChild(e),g=!0}catch{d.innerHTML='<span class="grammarPopupV2-hintError">Could not load hint</span>'}}}else d.style.display="none"};const S=document.createElement("button");S.className="grammarPopupV2-accept",S.textContent="✓",S.onclick=t=>{t.preventDefault(),t.stopPropagation(),s()};const x=document.createElement("button");return x.className="grammarPopupV2-discard",x.textContent="✕",x.onclick=t=>{t.preventDefault(),t.stopPropagation(),r()},i.appendChild(c),i.appendChild(l),i.appendChild(u),i.appendChild(m),i.appendChild(S),i.appendChild(x),p.appendChild(i),p.appendChild(d),p};var q,H;exports.AutoCompleteStatus=void 0,(q=exports.AutoCompleteStatus||(exports.AutoCompleteStatus={})).IDLE="idle",q.DEBOUNCING="debouncing",q.PENDING="pending",q.STREAMING="streaming",q.SHOWING="showing",exports.AutoCompleteActionType=void 0,(H=exports.AutoCompleteActionType||(exports.AutoCompleteActionType={})).SET_ENABLED="SET_ENABLED",H.SET_SYSTEM_PROMPT="SET_SYSTEM_PROMPT",H.START_DEBOUNCE="START_DEBOUNCE",H.START_REQUEST="START_REQUEST",H.STREAM_UPDATE="STREAM_UPDATE",H.STREAM_COMPLETE="STREAM_COMPLETE",H.STREAM_ERROR="STREAM_ERROR",H.DISMISS="DISMISS",H.ACCEPT="ACCEPT";const $={debounceMs:500,maxContextLength:2e3,ghostTextClass:"autoCompleteGhostText"},j=new WeakMap;function Y(t){const e=j.get(t);e&&(e.abort(),j.delete(t))}function J(t,e){if(!e)return e;const{doc:o,selection:s}=t.state,r=s.from;if(r<=0)return e;const n=o.textBetween(r-1,r,""),a=e.charAt(0),p=/[\s\-]/;return!p.test(n)&&!p.test(a)?` ${e}`:e}const Q=new a.PluginKey("autoCompletePlugin"),z={status:exports.AutoCompleteStatus.IDLE,enabled:!0,suggestion:"",cursorPos:0};function X(t,e){switch(e.type){case exports.AutoCompleteActionType.SET_ENABLED:return{...z,enabled:e.enabled,systemPrompt:e.systemPrompt??t.systemPrompt};case exports.AutoCompleteActionType.SET_SYSTEM_PROMPT:return{...t,systemPrompt:e.systemPrompt};case exports.AutoCompleteActionType.START_DEBOUNCE:return{...t,status:exports.AutoCompleteStatus.DEBOUNCING,cursorPos:e.cursorPos,suggestion:"",error:void 0};case exports.AutoCompleteActionType.START_REQUEST:return t.status!==exports.AutoCompleteStatus.DEBOUNCING?t:{...t,status:exports.AutoCompleteStatus.PENDING};case exports.AutoCompleteActionType.STREAM_UPDATE:return t.status!==exports.AutoCompleteStatus.PENDING&&t.status!==exports.AutoCompleteStatus.STREAMING?t:{...t,status:exports.AutoCompleteStatus.STREAMING,suggestion:e.suggestion};case exports.AutoCompleteActionType.STREAM_COMPLETE:return t.status!==exports.AutoCompleteStatus.PENDING&&t.status!==exports.AutoCompleteStatus.STREAMING?t:{...t,status:exports.AutoCompleteStatus.SHOWING,suggestion:e.suggestion};case exports.AutoCompleteActionType.STREAM_ERROR:return{...t,status:exports.AutoCompleteStatus.IDLE,error:e.error,suggestion:""};case exports.AutoCompleteActionType.DISMISS:case exports.AutoCompleteActionType.ACCEPT:return{...t,status:exports.AutoCompleteStatus.IDLE,suggestion:""};default:return t}}const Z=M;Object.defineProperty(exports,"getDiff",{enumerable:!0,get:function(){return i.getDiff}}),exports.DEFAULT_COMPLETION_ENDPOINT=T,exports.DEFAULT_GRAMMAR_ENDPOINT=E,exports.DEFAULT_MODEL=A,exports.OpenAiPromptsWithParam=m,exports.OpenAiPromptsWithoutParam=u,exports.acceptAutoCompletion=function(t){const e=Q.getState(t.state);if(!e||!e.suggestion)return!1;const o=t.state.tr.insertText(e.suggestion,e.cursorPos);return t.dispatch(o),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS})),t.focus(),!0},exports.acceptResult=function(t){t.dispatch(t.state.tr.setMeta(P,{type:exports.CompleteActionType.ACCEPT_RESULT}))},exports.acceptSuggestion=k,exports.autoCompleteInit=function(t,e){t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.SET_ENABLED,enabled:!0,systemPrompt:e}))},exports.autoCompleteKey=Q,exports.autoCompletePlugin=function(t,e={}){const o={...$,...e},{debounceMs:s,maxContextLength:r,apiEndpoint:n,model:p,ghostTextClass:i,systemPrompt:l}=o;let u=null,m=!1,d=null,g=null;return new a.Plugin({key:Q,state:{init:()=>({...z,systemPrompt:l}),apply(t,e,o,s){const r=t.getMeta(Q);if(r)return X(e,r);if(t.docChanged&&(e.status===exports.AutoCompleteStatus.SHOWING||e.status===exports.AutoCompleteStatus.STREAMING)){if(s.selection.from!==e.cursorPos)return X(e,{type:exports.AutoCompleteActionType.DISMISS})}return!t.selectionSet||t.docChanged||e.status!==exports.AutoCompleteStatus.SHOWING&&e.status!==exports.AutoCompleteStatus.STREAMING?e:X(e,{type:exports.AutoCompleteActionType.DISMISS})}},props:{decorations(t){const e=Q.getState(t);if(!e||!e.enabled)return c.DecorationSet.empty;if((e.status===exports.AutoCompleteStatus.STREAMING||e.status===exports.AutoCompleteStatus.SHOWING)&&e.suggestion){const o=document.createElement("span");o.className=i,o.textContent=e.suggestion,o.setAttribute("contenteditable","false");const s=c.Decoration.widget(e.cursorPos,o,{side:1});return c.DecorationSet.create(t.doc,[s])}return c.DecorationSet.empty},handleKeyDown(t,e){const o=Q.getState(t.state);return!(!o||!o.enabled)&&("Tab"===e.key&&!e.shiftKey&&o.status===exports.AutoCompleteStatus.SHOWING&&o.suggestion?(e.preventDefault(),d=o.suggestion,g=o.cursorPos,t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.ACCEPT})),!0):"Escape"===e.key&&(o.status===exports.AutoCompleteStatus.STREAMING||o.status===exports.AutoCompleteStatus.SHOWING)&&(e.preventDefault(),Y(t),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS})),!0))}},view:()=>({update(e,o){const a=Q.getState(e.state),i=Q.getState(o);if(a&&a.enabled){if(null!==d&&null!==g){const t=d,o=g;d=null,g=null;const s=e.state.tr.insertText(t,o);return e.dispatch(s),void e.focus()}if(e.state,e.state.doc!==o.doc&&i?.status!==exports.AutoCompleteStatus.PENDING&&i?.status!==exports.AutoCompleteStatus.STREAMING){e.state.tr,u&&(clearTimeout(u),u=null),Y(e);const t=e.state.selection.from;e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.START_DEBOUNCE,cursorPos:t})),u=setTimeout(()=>{u=null;const t=Q.getState(e.state);t?.status===exports.AutoCompleteStatus.DEBOUNCING&&e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.START_REQUEST}))},s)}if(a.status===exports.AutoCompleteStatus.PENDING&&!m){if(!function(t){const{doc:e,selection:o}=t.state,s=o.from,r=e.resolve(s),n=r.parent,a=r.start();if(s!==a+n.content.size)return!1;const p=n.textBetween(0,s-a,"");return!!p.trim()&&!/[.!?]\s*$/.test(p)}(e))return void e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS}));const o=function(t,e){const{doc:o,selection:s}=t.state,r=s.from,n=o.textBetween(0,r,"\n");return n.length>e?n.slice(-e):n}(e,r);if(!o.trim())return void e.dispatch(e.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS}));m=!0;const s=Q.getState(e.state)?.systemPrompt;(async function({view:t,pluginKey:e,apiKey:o,context:s,apiEndpoint:r,model:n,systemPrompt:a}){Y(t);const p=new AbortController;j.set(t,p);try{await C({apiKey:o,text:s,task:a?exports.AiPromptsWithParam.Custom:exports.AiPromptsWithoutParam.SmallComplete,...a&&{params:{systemPrompt:a}},endpoint:r,model:n,signal:p.signal},{onChunk:(o,s)=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.AutoCompleteActionType.STREAM_UPDATE,suggestion:J(t,s)}))},onComplete:o=>{t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.AutoCompleteActionType.STREAM_COMPLETE,suggestion:J(t,o)}))},onError:o=>{console.error("Auto-complete streaming error:",o),t.isDestroyed||t.dispatch(t.state.tr.setMeta(e,{type:exports.AutoCompleteActionType.STREAM_ERROR,error:o.message}))}})}finally{j.delete(t)}})({view:e,pluginKey:Q,apiKey:t,context:o,apiEndpoint:n,model:p,systemPrompt:s}).finally(()=>{m=!1})}}else u&&(clearTimeout(u),u=null)},destroy(){u&&(clearTimeout(u),u=null)}})})},exports.cancelActiveRequest=h,exports.cancelAutoCompleteRequest=Y,exports.cancelTask=function(t){h(t),t.dispatch(t.state.tr.setMeta(P,{type:exports.CompleteActionType.CANCEL_TASK}))},exports.clearError=function(t){t.dispatch(t.state.tr.setMeta(P,{type:exports.CompleteActionType.CLEAR_ERROR}))},exports.completePlugin=Z,exports.completePluginKey=N,exports.completePluginV2=M,exports.completeV2Key=P,exports.createApiConfig=function(t){return{apiKey:t.apiKey,endpoint:t.endpoint??T,model:t.model??A}},exports.createGrammarApiConfig=function(t){return{apiKey:t.apiKey,endpoint:t.endpoint??E,model:t.model??A}},exports.createGrammarProcessor=v,exports.defaultAutoCompleteOptions=$,exports.defaultCompleteOptions={maxSelection:1e3},exports.defaultCompleteV2Options=x,exports.defaultOptions=D,exports.deselectSuggestion=K,exports.discardSuggestion=G,exports.dismissAutoCompletion=function(t){Y(t),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.DISMISS}))},exports.getAutoCompleteState=function(t){return Q.getState(t.state)},exports.getChangedRegions=(t,e)=>{if(t===e)return{start:t.length,end:t.length,oldStart:t.length,oldEnd:t.length,oldText:"",newText:""};const o=p(t,e),s=o[0],r=s[0]===p.EQUAL?s[1].length-1:0,n=((t,e)=>{if(e[e.length-1][0]!==p.EQUAL)return t.length;const o=e.slice(0,e.length-1);let s=0;for(const t of o){const[e,o]=t;e===p.EQUAL&&(s+=o.length),e===p.DELETE&&(s+=o.length)}return s})(t,o),a=t.lastIndexOf("\n",r),i=t.indexOf("\n",n),c=-1===a?0:a+1,l=-1===i?t.length:i,u=t.slice(c,l),m=e.slice(c,e.length-(t.length-l));return{start:c,end:c+m.length,oldStart:c,oldEnd:l,oldText:u,newText:m}},exports.getCompleteState=function(t){return P.getState(t.state)},exports.getSelectedDecoration=function(t,e){const o=e.getState(t.state);if(o?.contextState.selectedSuggestionId)return O(t,e,o.contextState.selectedSuggestionId)},exports.grammarDecorationFactory=L,exports.grammarDecorationTransformer=b,exports.grammarRequest=y,exports.grammarSuggestInit=function(t,e,o){void 0!==o&&l.dispatchAction(t,e,{type:l.ActionType.UPDATE_CONTEXT,contextState:{systemPrompt:o}}),t.dispatch(t.state.tr.setMeta(e,{type:l.ActionType.INIT,metadata:{}}))},exports.grammarSuggestPluginKey=_,exports.grammarSuggestPluginV2=function(t,e={}){const{apiEndpoint:o,model:s,fallback:r,batchSize:n=4,maxRetries:p=3,backoffBase:i=2e3,debounceMs:u=1e3,systemPrompt:m,createPopup:d=F}=e,g=r?function(t){const{primaryModel:e,fallbackModel:o,failureThreshold:s}=t;let r=0;return{getCurrentModel:()=>o&&s>0&&r>=s?o:e,handleSuccess(){r=0},handleFailure(){o&&r++}}}({primaryModel:s,fallbackModel:r.fallbackModel,failureThreshold:r.failureThreshold??3}):void 0,S=v({apiKey:t,apiEndpoint:o,model:s,modelStateManager:g,pluginKey:B}),x=l.blockRunnerPlugin({pluginKey:B,unitProcessor:S,decorationFactory:L,decorationTransformer:b,widgetFactory:w,initialContextState:{...V,systemPrompt:m},options:{batchSize:n,maxRetries:p,backoffBase:i,dirtyHandling:{debounceDelay:u}}});return new a.Plugin({key:B,state:x.spec.state,props:{...x.spec.props,decorations(e){const r=x.spec.props?.decorations,n=r?r.call(x,e):c.DecorationSet.empty;if("react"===d)return n;const a=B.getState(e);if(!a?.contextState.selectedSuggestionId)return n;const p=a.decorations.find(t=>t.spec.id===a.contextState.selectedSuggestionId);if(!p)return n;const i=p.spec,l=c.Decoration.widget(p.from,(e,r)=>{const n=r();return void 0===n?document.createElement("div"):d(e,p,n,()=>k(e,B,p.spec.id),()=>G(e,B,p.spec.id),()=>W(t,i.originalText,i.replacement,{endpoint:o,model:s,modelStateManager:g}))},{id:p.spec.id,side:-1,stopEvent:()=>!0});return n.add(e.doc,[l])},handleClick(t,e,o){const s=B.getState(t.state);if(!s)return!1;const r=s.decorations.find(t=>t.from<=e&&e<=t.to);return r?(r.spec.id===s.contextState.selectedSuggestionId||U(t,B,r.spec.id),!1):(s.contextState.selectedSuggestionId&&K(t,B),!1)}},view:x.spec.view})},exports.grammarSuggestV2Key=B,exports.grammarWidgetFactory=w,exports.hasActiveRequest=function(t){return f.has(t)},exports.hasAutoCompleteRequest=function(t){return j.has(t)},exports.hasAutoCompletion=function(t){const e=Q.getState(t.state);return!!e?.suggestion},exports.isAutoCompleteEnabled=function(t){const e=Q.getState(t.state);return e?.enabled??!1},exports.rejectResult=function(t){t.dispatch(t.state.tr.setMeta(P,{type:exports.CompleteActionType.REJECT_RESULT}))},exports.requestHint=W,exports.selectSuggestion=U,exports.setAutoCompleteEnabled=function(t,e,o){e||Y(t),t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.SET_ENABLED,enabled:e,systemPrompt:o}))},exports.setAutoCompleteSystemPrompt=function(t,e){t.dispatch(t.state.tr.setMeta(Q,{type:exports.AutoCompleteActionType.SET_SYSTEM_PROMPT,systemPrompt:e}))},exports.setCompleteEnabled=(t,e)=>{t.dispatch(t.state.tr.setMeta(N,{type:"setEnabled",enabled:e}))},exports.setEnabled=function(t,e){e||h(t),t.dispatch(t.state.tr.setMeta(P,{type:exports.CompleteActionType.SET_ENABLED,enabled:e}))},exports.setGrammarSuggestEnabled=(t,e)=>{t.dispatch(t.state.tr.setMeta(_,{type:exports.GrammarSuggestMetaType.setEnabled,enabled:e}))},exports.setGrammarSystemPrompt=function(t,e,o){l.dispatchAction(t,e,{type:l.ActionType.UPDATE_CONTEXT,contextState:{systemPrompt:o}})},exports.startCustomTask=function(t,e){I(t,exports.AiPromptsWithParam.Custom,{systemPrompt:e})},exports.startTask=I,exports.streamingRequest=C;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import type { TaskType, TaskParams } from "../types";
|
|
1
2
|
export interface GrammarRequestOptions {
|
|
2
3
|
apiKey: string;
|
|
3
4
|
text: string;
|
|
4
5
|
endpoint?: string;
|
|
5
6
|
model?: string;
|
|
7
|
+
task?: TaskType;
|
|
8
|
+
params?: TaskParams;
|
|
6
9
|
}
|
|
7
10
|
export interface GrammarRequestResult {
|
|
8
11
|
result: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/api/request.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/api/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAIrD,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,MAAM,CAAC,EAAE,UAAU,CAAC;CACrB;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,oBAAoB,CAAC,CAgE/B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../../../src/api/streaming.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,KAAK,EAAkB,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAGlE,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,uBAAuB,EAChC,SAAS,EAAE,kBAAkB,GAC5B,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../../../src/api/streaming.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,KAAK,EAAkB,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAGlE,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED;;;GAGG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,uBAAuB,EAChC,SAAS,EAAE,kBAAkB,GAC5B,OAAO,CAAC,IAAI,CAAC,CA0Ef"}
|
package/dist/src/api/types.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/api/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGnC,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CAC1B;AAGD,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE;QACX,KAAK,EAAE,MAAM,EAAE,CAAC;QAChB,IAAI,CAAC,EAAE,QAAQ,CAAC;QAChB,MAAM,CAAC,EAAE,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/api/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGnC,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CAC1B;AAGD,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE;QACX,KAAK,EAAE,MAAM,EAAE,CAAC;QAChB,IAAI,CAAC,EAAE,QAAQ,CAAC;QAChB,MAAM,CAAC,EAAE,UAAU,CAAC;QACpB,MAAM,CAAC,EAAE,OAAO,CAAC;KAClB,CAAC;CACH;AAGD,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,KAAK,IAAI,CAAC;IACtD,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IACrC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACjC;AAGD,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;IACf,GAAG,CAAC,EAAE;QACJ,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC;KACd,EAAE,CAAC;CACL"}
|
|
@@ -7,9 +7,18 @@
|
|
|
7
7
|
import { EditorView } from "prosemirror-view";
|
|
8
8
|
import { AutoCompleteState } from "./types";
|
|
9
9
|
/**
|
|
10
|
-
* Enable or disable the auto-complete plugin
|
|
10
|
+
* Enable or disable the auto-complete plugin, optionally setting a custom system prompt
|
|
11
11
|
*/
|
|
12
|
-
export declare function setAutoCompleteEnabled(view: EditorView, enabled: boolean): void;
|
|
12
|
+
export declare function setAutoCompleteEnabled(view: EditorView, enabled: boolean, systemPrompt?: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* Set or clear the custom system prompt for auto-complete without toggling enabled state
|
|
15
|
+
*/
|
|
16
|
+
export declare function setAutoCompleteSystemPrompt(view: EditorView, systemPrompt: string | undefined): void;
|
|
17
|
+
/**
|
|
18
|
+
* Enable auto-complete with an optional custom system prompt.
|
|
19
|
+
* Convenience wrapper combining enable + system prompt in one call.
|
|
20
|
+
*/
|
|
21
|
+
export declare function autoCompleteInit(view: EditorView, systemPrompt?: string): void;
|
|
13
22
|
/**
|
|
14
23
|
* Accept the current completion (if showing)
|
|
15
24
|
* This is typically triggered by pressing Tab, but can be called programmatically
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/actions.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAA0B,MAAM,SAAS,CAAC;AAGpE;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/actions.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAA0B,MAAM,SAAS,CAAC;AAGpE;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,OAAO,EAChB,YAAY,CAAC,EAAE,MAAM,GACpB,IAAI,CAaN;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,IAAI,EAAE,UAAU,EAChB,YAAY,EAAE,MAAM,GAAG,SAAS,GAC/B,IAAI,CAON;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,UAAU,EAChB,YAAY,CAAC,EAAE,MAAM,GACpB,IAAI,CAQN;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAsB9D;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAQ5D;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,UAAU,GACf,iBAAiB,GAAG,SAAS,CAE/B;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAG/D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAG3D"}
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
* }
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export { AutoCompleteStatus, AutoCompleteActionType, type AutoCompleteState, type AutoCompleteAction, type AutoCompleteOptions, type AutoCompleteSetEnabledAction, type AutoCompleteStartDebounceAction, type AutoCompleteStartRequestAction, type AutoCompleteStreamUpdateAction, type AutoCompleteStreamCompleteAction, type AutoCompleteStreamErrorAction, type AutoCompleteDismissAction, type AutoCompleteAcceptAction, defaultAutoCompleteOptions, } from "./types";
|
|
32
|
+
export { AutoCompleteStatus, AutoCompleteActionType, type AutoCompleteState, type AutoCompleteAction, type AutoCompleteOptions, type AutoCompleteSetEnabledAction, type AutoCompleteSetSystemPromptAction, type AutoCompleteStartDebounceAction, type AutoCompleteStartRequestAction, type AutoCompleteStreamUpdateAction, type AutoCompleteStreamCompleteAction, type AutoCompleteStreamErrorAction, type AutoCompleteDismissAction, type AutoCompleteAcceptAction, defaultAutoCompleteOptions, } from "./types";
|
|
33
33
|
export { autoCompletePlugin, autoCompleteKey } from "./plugin";
|
|
34
|
-
export { setAutoCompleteEnabled, acceptAutoCompletion, dismissAutoCompletion, getAutoCompleteState, isAutoCompleteEnabled, hasAutoCompletion, } from "./actions";
|
|
34
|
+
export { setAutoCompleteEnabled, setAutoCompleteSystemPrompt, autoCompleteInit, acceptAutoCompletion, dismissAutoCompletion, getAutoCompleteState, isAutoCompleteEnabled, hasAutoCompletion, } from "./actions";
|
|
35
35
|
export { cancelActiveRequest as cancelAutoCompleteRequest, hasActiveRequest as hasAutoCompleteRequest, } from "./streaming";
|
|
36
36
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAGH,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,EACxB,KAAK,4BAA4B,EACjC,KAAK,+BAA+B,EACpC,KAAK,8BAA8B,EACnC,KAAK,8BAA8B,EACnC,KAAK,gCAAgC,EACrC,KAAK,6BAA6B,EAClC,KAAK,yBAAyB,EAC9B,KAAK,wBAAwB,EAC7B,0BAA0B,GAC3B,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAG/D,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,WAAW,CAAC;AAGnB,OAAO,EACL,mBAAmB,IAAI,yBAAyB,EAChD,gBAAgB,IAAI,sBAAsB,GAC3C,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAGH,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,EACxB,KAAK,4BAA4B,EACjC,KAAK,iCAAiC,EACtC,KAAK,+BAA+B,EACpC,KAAK,8BAA8B,EACnC,KAAK,8BAA8B,EACnC,KAAK,gCAAgC,EACrC,KAAK,6BAA6B,EAClC,KAAK,yBAAyB,EAC9B,KAAK,wBAAwB,EAC7B,0BAA0B,GAC3B,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAG/D,OAAO,EACL,sBAAsB,EACtB,2BAA2B,EAC3B,gBAAgB,EAChB,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,WAAW,CAAC;AAGnB,OAAO,EACL,mBAAmB,IAAI,yBAAyB,EAChD,gBAAgB,IAAI,sBAAsB,GAC3C,MAAM,aAAa,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,SAAS,EAAe,MAAM,mBAAmB,CAAC;AAEnE,OAAO,EACL,iBAAiB,EAIjB,mBAAmB,EAEpB,MAAM,SAAS,CAAC;AASjB,eAAO,MAAM,eAAe,8BAE3B,CAAC;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/plugin.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,SAAS,EAAe,MAAM,mBAAmB,CAAC;AAEnE,OAAO,EACL,iBAAiB,EAIjB,mBAAmB,EAEpB,MAAM,SAAS,CAAC;AASjB,eAAO,MAAM,eAAe,8BAE3B,CAAC;AAuJF;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,OAAO,CAAC,mBAAmB,CAAM,GACzC,MAAM,CAAC,iBAAiB,CAAC,CA+Q3B"}
|
|
@@ -21,6 +21,7 @@ interface AutoCompleteStreamingParams {
|
|
|
21
21
|
context: string;
|
|
22
22
|
apiEndpoint?: string;
|
|
23
23
|
model?: string;
|
|
24
|
+
systemPrompt?: string;
|
|
24
25
|
}
|
|
25
26
|
/**
|
|
26
27
|
* Ensure proper spacing between existing text and suggestion.
|
|
@@ -32,7 +33,7 @@ export declare function ensureLeadingSpace(view: EditorView, suggestion: string)
|
|
|
32
33
|
/**
|
|
33
34
|
* Make a streaming request to the AI API for auto-completion
|
|
34
35
|
*/
|
|
35
|
-
export declare function streamingRequest({ view, pluginKey, apiKey, context, apiEndpoint, model, }: AutoCompleteStreamingParams): Promise<void>;
|
|
36
|
+
export declare function streamingRequest({ view, pluginKey, apiKey, context, apiEndpoint, model, systemPrompt, }: AutoCompleteStreamingParams): Promise<void>;
|
|
36
37
|
/**
|
|
37
38
|
* Extract context text before cursor (up to maxLength)
|
|
38
39
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/streaming.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAG9C,OAAO,EAAE,iBAAiB,EAA0B,MAAM,SAAS,CAAC;AAKpE;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAM1D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAE1D;AAED,UAAU,2BAA2B;IACnC,IAAI,EAAE,UAAU,CAAC;IACjB,SAAS,EAAE,SAAS,CAAC,iBAAiB,CAAC,CAAC;IACxC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/streaming.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAG9C,OAAO,EAAE,iBAAiB,EAA0B,MAAM,SAAS,CAAC;AAKpE;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAM1D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAE1D;AAED,UAAU,2BAA2B;IACnC,IAAI,EAAE,UAAU,CAAC;IACjB,SAAS,EAAE,SAAS,CAAC,iBAAiB,CAAC,CAAC;IACxC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,UAAU,EAChB,UAAU,EAAE,MAAM,GACjB,MAAM,CAsBR;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CAAC,EACrC,IAAI,EACJ,SAAS,EACT,MAAM,EACN,OAAO,EACP,WAAW,EACX,KAAK,EACL,YAAY,GACb,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC,CA0D7C;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAa1E;AAED;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CA+BnE"}
|
|
@@ -17,9 +17,11 @@ export interface AutoCompleteState {
|
|
|
17
17
|
suggestion: string;
|
|
18
18
|
cursorPos: number;
|
|
19
19
|
error?: string;
|
|
20
|
+
systemPrompt?: string;
|
|
20
21
|
}
|
|
21
22
|
export declare enum AutoCompleteActionType {
|
|
22
23
|
SET_ENABLED = "SET_ENABLED",
|
|
24
|
+
SET_SYSTEM_PROMPT = "SET_SYSTEM_PROMPT",
|
|
23
25
|
START_DEBOUNCE = "START_DEBOUNCE",
|
|
24
26
|
START_REQUEST = "START_REQUEST",
|
|
25
27
|
STREAM_UPDATE = "STREAM_UPDATE",
|
|
@@ -31,6 +33,11 @@ export declare enum AutoCompleteActionType {
|
|
|
31
33
|
export interface AutoCompleteSetEnabledAction {
|
|
32
34
|
type: AutoCompleteActionType.SET_ENABLED;
|
|
33
35
|
enabled: boolean;
|
|
36
|
+
systemPrompt?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface AutoCompleteSetSystemPromptAction {
|
|
39
|
+
type: AutoCompleteActionType.SET_SYSTEM_PROMPT;
|
|
40
|
+
systemPrompt?: string;
|
|
34
41
|
}
|
|
35
42
|
export interface AutoCompleteStartDebounceAction {
|
|
36
43
|
type: AutoCompleteActionType.START_DEBOUNCE;
|
|
@@ -57,13 +64,14 @@ export interface AutoCompleteDismissAction {
|
|
|
57
64
|
export interface AutoCompleteAcceptAction {
|
|
58
65
|
type: AutoCompleteActionType.ACCEPT;
|
|
59
66
|
}
|
|
60
|
-
export type AutoCompleteAction = AutoCompleteSetEnabledAction | AutoCompleteStartDebounceAction | AutoCompleteStartRequestAction | AutoCompleteStreamUpdateAction | AutoCompleteStreamCompleteAction | AutoCompleteStreamErrorAction | AutoCompleteDismissAction | AutoCompleteAcceptAction;
|
|
67
|
+
export type AutoCompleteAction = AutoCompleteSetEnabledAction | AutoCompleteSetSystemPromptAction | AutoCompleteStartDebounceAction | AutoCompleteStartRequestAction | AutoCompleteStreamUpdateAction | AutoCompleteStreamCompleteAction | AutoCompleteStreamErrorAction | AutoCompleteDismissAction | AutoCompleteAcceptAction;
|
|
61
68
|
export interface AutoCompleteOptions {
|
|
62
69
|
debounceMs: number;
|
|
63
70
|
maxContextLength: number;
|
|
64
71
|
apiEndpoint?: string;
|
|
65
72
|
model?: string;
|
|
66
73
|
ghostTextClass?: string;
|
|
74
|
+
systemPrompt?: string;
|
|
67
75
|
}
|
|
68
76
|
export declare const defaultAutoCompleteOptions: AutoCompleteOptions;
|
|
69
77
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,oBAAY,kBAAkB;IAC5B,IAAI,SAAS,CAAE,yBAAyB;IACxC,UAAU,eAAe,CAAE,iCAAiC;IAC5D,OAAO,YAAY,CAAE,sBAAsB;IAC3C,SAAS,cAAc,CAAE,mBAAmB;IAC5C,OAAO,YAAY;CACpB;AAGD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/autoComplete/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,oBAAY,kBAAkB;IAC5B,IAAI,SAAS,CAAE,yBAAyB;IACxC,UAAU,eAAe,CAAE,iCAAiC;IAC5D,OAAO,YAAY,CAAE,sBAAsB;IAC3C,SAAS,cAAc,CAAE,mBAAmB;IAC5C,OAAO,YAAY;CACpB;AAGD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAGD,oBAAY,sBAAsB;IAChC,WAAW,gBAAgB;IAC3B,iBAAiB,sBAAsB;IACvC,cAAc,mBAAmB;IACjC,aAAa,kBAAkB;IAC/B,aAAa,kBAAkB;IAC/B,eAAe,oBAAoB;IACnC,YAAY,iBAAiB;IAC7B,OAAO,YAAY;IACnB,MAAM,WAAW;CAClB;AAGD,MAAM,WAAW,4BAA4B;IAC3C,IAAI,EAAE,sBAAsB,CAAC,WAAW,CAAC;IACzC,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,iCAAiC;IAChD,IAAI,EAAE,sBAAsB,CAAC,iBAAiB,CAAC;IAC/C,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,+BAA+B;IAC9C,IAAI,EAAE,sBAAsB,CAAC,cAAc,CAAC;IAC5C,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,8BAA8B;IAC7C,IAAI,EAAE,sBAAsB,CAAC,aAAa,CAAC;CAC5C;AAED,MAAM,WAAW,8BAA8B;IAC7C,IAAI,EAAE,sBAAsB,CAAC,aAAa,CAAC;IAC3C,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,gCAAgC;IAC/C,IAAI,EAAE,sBAAsB,CAAC,eAAe,CAAC;IAC7C,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,6BAA6B;IAC5C,IAAI,EAAE,sBAAsB,CAAC,YAAY,CAAC;IAC1C,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,yBAAyB;IACxC,IAAI,EAAE,sBAAsB,CAAC,OAAO,CAAC;CACtC;AAED,MAAM,WAAW,wBAAwB;IACvC,IAAI,EAAE,sBAAsB,CAAC,MAAM,CAAC;CACrC;AAGD,MAAM,MAAM,kBAAkB,GAC1B,4BAA4B,GAC5B,iCAAiC,GACjC,+BAA+B,GAC/B,8BAA8B,GAC9B,8BAA8B,GAC9B,gCAAgC,GAChC,6BAA6B,GAC7B,yBAAyB,GACzB,wBAAwB,CAAC;AAG7B,MAAM,WAAW,mBAAmB;IAClC,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAGD,eAAO,MAAM,0BAA0B,EAAE,mBAIxC,CAAC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { EditorView } from "prosemirror-view";
|
|
2
|
-
import { CompleteV2State, TaskType, MoodParams, TranslationParams } from "./types";
|
|
2
|
+
import { CompleteV2State, TaskType, MoodParams, TranslationParams, CustomParams } from "./types";
|
|
3
3
|
/**
|
|
4
4
|
* Start a completion task
|
|
5
5
|
*/
|
|
6
|
-
export declare function startTask(view: EditorView, taskType: TaskType, params?: MoodParams | TranslationParams): void;
|
|
6
|
+
export declare function startTask(view: EditorView, taskType: TaskType, params?: MoodParams | TranslationParams | CustomParams): void;
|
|
7
7
|
/**
|
|
8
8
|
* Accept the streamed result and apply it to the document
|
|
9
9
|
*/
|
|
@@ -28,4 +28,9 @@ export declare function clearError(view: EditorView): void;
|
|
|
28
28
|
* Get the current plugin state
|
|
29
29
|
*/
|
|
30
30
|
export declare function getCompleteState(view: EditorView): CompleteV2State | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Start a custom task with a system prompt
|
|
33
|
+
* Convenience wrapper around startTask with Custom task type
|
|
34
|
+
*/
|
|
35
|
+
export declare function startCustomTask(view: EditorView, systemPrompt: string): void;
|
|
31
36
|
//# sourceMappingURL=actions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/completeV2/actions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,
|
|
1
|
+
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/completeV2/actions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAGL,eAAe,EACf,QAAQ,EACR,UAAU,EACV,iBAAiB,EACjB,YAAY,EACb,MAAM,SAAS,CAAC;AAGjB;;GAEG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,QAAQ,EAClB,MAAM,CAAC,EAAE,UAAU,GAAG,iBAAiB,GAAG,YAAY,GACrD,IAAI,CAWN;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAMnD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAMnD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CASjD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAYnE;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAMjD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,UAAU,GACf,eAAe,GAAG,SAAS,CAE7B;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,UAAU,EAChB,YAAY,EAAE,MAAM,GACnB,IAAI,CAEN"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { CompleteStatus, CompleteActionType, type CompleteV2State, type CompleteAction, type CompleteV2Options, type StartTaskAction, type StreamUpdateAction, type StreamCompleteAction, type StreamErrorAction, type AcceptResultAction, type RejectResultAction, type CancelTaskAction, type ClearErrorAction, type SetEnabledAction, defaultCompleteV2Options, AiPromptsWithoutParam, AiPromptsWithParam, MoodParamType, TranslationTargetLanguage, type MoodParams, type TranslationParams, type HintParams, type CustomParams, type TaskParams, type TaskType, } from "./types";
|
|
2
2
|
export { completePluginV2, completeV2Key } from "./plugin";
|
|
3
|
-
export { startTask, acceptResult, rejectResult, cancelTask, setEnabled, clearError, getCompleteState, } from "./actions";
|
|
3
|
+
export { startTask, startCustomTask, acceptResult, rejectResult, cancelTask, setEnabled, clearError, getCompleteState, } from "./actions";
|
|
4
4
|
export { cancelActiveRequest, hasActiveRequest } from "./streaming";
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/completeV2/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,wBAAwB,EAExB,qBAAqB,EACrB,kBAAkB,EAClB,aAAa,EACb,yBAAyB,EACzB,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,QAAQ,GACd,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG3D,OAAO,EACL,SAAS,EACT,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,UAAU,EACV,UAAU,EACV,gBAAgB,GACjB,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/completeV2/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,wBAAwB,EAExB,qBAAqB,EACrB,kBAAkB,EAClB,aAAa,EACb,yBAAyB,EACzB,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,QAAQ,GACd,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG3D,OAAO,EACL,SAAS,EACT,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,UAAU,EACV,UAAU,EACV,gBAAgB,GACjB,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -9,10 +9,13 @@ export declare function discardSuggestion(view: EditorView, pluginKey: PluginKey
|
|
|
9
9
|
export declare function selectSuggestion(view: EditorView, pluginKey: PluginKey<GrammarState>, decorationId: object): void;
|
|
10
10
|
export declare function deselectSuggestion(view: EditorView, pluginKey: PluginKey<GrammarState>): void;
|
|
11
11
|
export declare function getSelectedDecoration(view: EditorView, pluginKey: PluginKey<GrammarState>): Decoration | undefined;
|
|
12
|
+
export declare function setGrammarSystemPrompt(view: EditorView, pluginKey: PluginKey<GrammarState>, systemPrompt: string | undefined): void;
|
|
13
|
+
export declare function grammarSuggestInit(view: EditorView, pluginKey: PluginKey<GrammarState>, systemPrompt?: string): void;
|
|
12
14
|
export declare function requestHint(apiKey: string, originalText: string, replacement: string, options?: {
|
|
13
15
|
endpoint?: string;
|
|
14
16
|
model?: string;
|
|
15
17
|
modelStateManager?: ModelStateManager;
|
|
18
|
+
systemPrompt?: string;
|
|
16
19
|
}): Promise<string>;
|
|
17
20
|
export {};
|
|
18
21
|
//# sourceMappingURL=actions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiB,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE1D,OAAO,EAGL,WAAW,EACZ,MAAM,iDAAiD,CAAC;AACzD,OAAO,EACL,mBAAmB,EAEnB,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAIjB,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,KAAK,YAAY,GAAG,WAAW,CAC7B,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,CACpB,CAAC;AAeF,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,EAAE,MAAM,GACnB,IAAI,CAoCN;AAGD,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,EAAE,MAAM,GACnB,IAAI,CAKN;AAGD,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,EAAE,MAAM,GACnB,IAAI,CAMN;AAGD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,GACjC,IAAI,CAKN;AAGD,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,GACjC,UAAU,GAAG,SAAS,CASxB;AAGD,wBAAsB,WAAW,CAC/B,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE;IACR,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiB,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE1D,OAAO,EAGL,WAAW,EACZ,MAAM,iDAAiD,CAAC;AACzD,OAAO,EACL,mBAAmB,EAEnB,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAIjB,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,KAAK,YAAY,GAAG,WAAW,CAC7B,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,CACpB,CAAC;AAeF,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,EAAE,MAAM,GACnB,IAAI,CAoCN;AAGD,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,EAAE,MAAM,GACnB,IAAI,CAKN;AAGD,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,EAAE,MAAM,GACnB,IAAI,CAMN;AAGD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,GACjC,IAAI,CAKN;AAGD,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,GACjC,UAAU,GAAG,SAAS,CASxB;AAGD,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,EAAE,MAAM,GAAG,SAAS,GAC/B,IAAI,CAKN;AAID,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,EAClC,YAAY,CAAC,EAAE,MAAM,GACpB,IAAI,CAaN;AAGD,wBAAsB,WAAW,CAC/B,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE;IACR,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,GACA,OAAO,CAAC,MAAM,CAAC,CAgDjB"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type { GrammarSuggestion, GrammarFixResult, GrammarContextState, GrammarUnitMetadata, GrammarSuggestV2Options, GrammarDecorationSpec, ModelFallbackConfig, } from "./types";
|
|
2
2
|
export { grammarSuggestPluginV2, grammarSuggestV2Key } from "./plugin";
|
|
3
|
-
export { acceptSuggestion, discardSuggestion, selectSuggestion, deselectSuggestion, getSelectedDecoration, requestHint, } from "./actions";
|
|
3
|
+
export { acceptSuggestion, discardSuggestion, selectSuggestion, deselectSuggestion, getSelectedDecoration, setGrammarSystemPrompt, grammarSuggestInit, requestHint, } from "./actions";
|
|
4
4
|
export { grammarDecorationFactory, grammarDecorationTransformer, grammarWidgetFactory, } from "./decorations";
|
|
5
5
|
export { createGrammarProcessor, type GrammarProcessorOptions, } from "./processor";
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,EACnB,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAGvE,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,EACrB,WAAW,GACZ,MAAM,WAAW,CAAC;AAGnB,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,oBAAoB,GACrB,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,sBAAsB,EACtB,KAAK,uBAAuB,GAC7B,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/index.ts"],"names":[],"mappings":"AACA,YAAY,EACV,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,EACnB,uBAAuB,EACvB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAGvE,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,EACrB,sBAAsB,EACtB,kBAAkB,EAClB,WAAW,GACZ,MAAM,WAAW,CAAC;AAGnB,OAAO,EACL,wBAAwB,EACxB,4BAA4B,EAC5B,oBAAoB,GACrB,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,sBAAsB,EACtB,KAAK,uBAAuB,GAC7B,MAAM,aAAa,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/plugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAGL,WAAW,EACZ,MAAM,iDAAiD,CAAC;AAOzD,OAAO,EACL,mBAAmB,EAEnB,gBAAgB,EAChB,uBAAuB,EACvB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAUjB,KAAK,YAAY,GAAG,WAAW,CAC7B,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,CACpB,CAAC;AAQF,eAAO,MAAM,mBAAmB,oFAIX,CAAC;AA4GtB,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,OAAO,CAAC,uBAAuB,CAAM,GAC7C,MAAM,CAAC,YAAY,CAAC,
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/plugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAGL,WAAW,EACZ,MAAM,iDAAiD,CAAC;AAOzD,OAAO,EACL,mBAAmB,EAEnB,gBAAgB,EAChB,uBAAuB,EACvB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAUjB,KAAK,YAAY,GAAG,WAAW,CAC7B,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,CACpB,CAAC;AAQF,eAAO,MAAM,mBAAmB,oFAIX,CAAC;AA4GtB,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,OAAO,CAAC,uBAAuB,CAAM,GAC7C,MAAM,CAAC,YAAY,CAAC,CAgKtB"}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { EditorView } from "prosemirror-view";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { PluginKey } from "prosemirror-state";
|
|
3
|
+
import { ProcessingUnit, RunnerState, UnitProcessorResult } from "@emergence-engineering/prosemirror-block-runner";
|
|
4
|
+
import { GrammarContextState, GrammarFixResult, GrammarUnitMetadata } from "./types";
|
|
4
5
|
import { ModelStateManager } from "./modelState";
|
|
5
6
|
export interface GrammarProcessorOptions {
|
|
6
7
|
apiKey: string;
|
|
7
8
|
apiEndpoint?: string;
|
|
8
9
|
model?: string;
|
|
9
10
|
modelStateManager?: ModelStateManager;
|
|
11
|
+
pluginKey?: PluginKey<RunnerState<GrammarFixResult, GrammarContextState, GrammarUnitMetadata>>;
|
|
10
12
|
}
|
|
11
|
-
export declare const createGrammarProcessor: (options: GrammarProcessorOptions | string) => (
|
|
13
|
+
export declare const createGrammarProcessor: (options: GrammarProcessorOptions | string) => (view: EditorView, unit: ProcessingUnit<GrammarUnitMetadata>) => Promise<UnitProcessorResult<GrammarFixResult>>;
|
|
12
14
|
//# sourceMappingURL=processor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EACL,cAAc,EACd,mBAAmB,EACpB,MAAM,iDAAiD,CAAC;
|
|
1
|
+
{"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,OAAO,EACL,cAAc,EACd,WAAW,EACX,mBAAmB,EACpB,MAAM,iDAAiD,CAAC;AAGzD,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAEhB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAoBjD,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,SAAS,CAAC,EAAE,SAAS,CACnB,WAAW,CAAC,gBAAgB,EAAE,mBAAmB,EAAE,mBAAmB,CAAC,CACxE,CAAC;CACH;AAGD,eAAO,MAAM,sBAAsB,GACjC,SAAS,uBAAuB,GAAG,MAAM,MAevC,MAAM,UAAU,EAChB,MAAM,cAAc,CAAC,mBAAmB,CAAC,KACxC,OAAO,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,CA8DjD,CAAC"}
|
|
@@ -18,6 +18,7 @@ export interface GrammarFixResult {
|
|
|
18
18
|
}
|
|
19
19
|
export interface GrammarContextState {
|
|
20
20
|
selectedSuggestionId?: object;
|
|
21
|
+
systemPrompt?: string;
|
|
21
22
|
}
|
|
22
23
|
export interface GrammarUnitMetadata {
|
|
23
24
|
paragraphIndex: number;
|
|
@@ -31,6 +32,7 @@ export interface GrammarSuggestV2Options {
|
|
|
31
32
|
maxRetries?: number;
|
|
32
33
|
backoffBase?: number;
|
|
33
34
|
debounceMs?: number;
|
|
35
|
+
systemPrompt?: string;
|
|
34
36
|
createPopup?: "react" | ((view: EditorView, decoration: Decoration, pos: number, applySuggestion: () => void, discardSuggestion: () => void, requestHint: () => Promise<string>) => HTMLElement);
|
|
35
37
|
}
|
|
36
38
|
export interface GrammarDecorationSpec {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxC,MAAM,WAAW,mBAAmB;IAClC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC;IAChC,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAGD,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB;AAGD,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,OAAO,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,iBAAiB,EAAE,CAAC;CAClC;AAGD,MAAM,WAAW,mBAAmB;IAClC,oBAAoB,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/grammarSuggestV2/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxC,MAAM,WAAW,mBAAmB;IAClC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC;IAChC,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAGD,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB;AAGD,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,OAAO,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,iBAAiB,EAAE,CAAC;CAClC;AAGD,MAAM,WAAW,mBAAmB;IAClC,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAGD,MAAM,WAAW,mBAAmB;IAClC,cAAc,EAAE,MAAM,CAAC;CACxB;AAGD,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IACzB,QAAQ,CAAC,EAAE,mBAAmB,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EACR,OAAO,GACP,CAAC,CACC,IAAI,EAAE,UAAU,EAChB,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,MAAM,EACX,eAAe,EAAE,MAAM,IAAI,EAC3B,iBAAiB,EAAE,MAAM,IAAI,EAC7B,WAAW,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,KAC/B,WAAW,CAAC,CAAC;CACvB;AAGD,MAAM,WAAW,qBAAqB;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,gBAAgB,CAAC;CAC5B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prosemirror-suggestcat-plugin",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "AI-powered grammar checking, text completion, and prompt-based editing for ProseMirror",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/src/index.d.ts",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"fast-diff": "^1.3.0",
|
|
31
31
|
"lodash.debounce": "^4.0.8",
|
|
32
32
|
"@emergence-engineering/fast-diff-merge": "0.0.6",
|
|
33
|
-
"@emergence-engineering/prosemirror-
|
|
34
|
-
"@emergence-engineering/prosemirror-
|
|
33
|
+
"@emergence-engineering/prosemirror-text-map": "0.1.5",
|
|
34
|
+
"@emergence-engineering/prosemirror-block-runner": "0.2.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"prosemirror-model": "^1.19.3",
|