diff --git a/extension/dist/extension.umd.js b/extension/dist/extension.umd.js index d2ab9e2..1eac32e 100644 --- a/extension/dist/extension.umd.js +++ b/extension/dist/extension.umd.js @@ -142,7 +142,7 @@ ASSISTANT:`,first:`{system} ### Response: `,first:`{system} -{user}`,stops:["### User:"]}},b3=(l,I)=>{let d=Z3[l],G=d.system.replace("{system_message}",I.system),b=d.user.replace("{user_message}",I.message),Z=d.first.replace("{system}",G).replace("{user}",b);return{system:G,first:Z,user:b}},h1={OpenAI:{instance:t2,completionParams:[{name:"n",default:1},{name:"model",default:"gpt-3.5-turbo"},{name:"temperature",default:.3},{name:"max_tokens",default:2048},{name:"frequency_penalty",default:0},{name:"presence_penalty",default:0},{name:"top_p",default:1},{name:"stop",default:[]}]},Anthropic:{instance:w2,completionParams:[{name:"max_tokens_to_sample",default:1e5},{name:"top_k",default:5},{name:"top_p",default:.7},{name:"model",default:"claude-instant-1"},{name:"temperature",default:.3}]}},v5={OpenAI:{instance:o1},Llama:{instance:o1},Anthropic:{instance:o1}},W1=l=>{let I={};return h1[l].completionParams.forEach(d=>{I[d.name]=d.default}),I},e5={label:"Programming",id:H};var C1=a1(require("vscode"));var T=a1(require("vscode")),c3="my-diff",d0={provideTextDocumentContent(l){return Buffer.from(l.query,"base64").toString()}};T.workspace.registerTextDocumentContentProvider(c3,d0);var m3=(l,I,d)=>{let G=T.Uri.parse(`${c3}:/original?${Buffer.from(l).toString("base64")}`),b=T.Uri.parse(`${c3}:/suggestion?${Buffer.from(I).toString("base64")}`);T.workspace.openTextDocument(G).then(Z=>{T.workspace.openTextDocument(b).then(W=>{d&&(T.languages.setTextDocumentLanguage(Z,d),T.languages.setTextDocumentLanguage(W,d)),T.commands.executeCommand("vscode.diff",Z.uri,W.uri)})}),T.commands.executeCommand("vscode.diff",G,b)};var W3=a1(require("vscode"));var y2=a1(require("vscode"));async function w5(l){let I=y2.window.activeTextEditor,d=V3(s1(I),Y5(I)),G=r1(I),b=m1("completionParams"),Z=Object.fromEntries(Object.entries(b).filter(([p,a])=>a!=null)),W=async p=>{if(p=p.replaceAll("{{ft}}",s1(I)),p=p.replaceAll("{{language}}",d),p.includes("{{input}}")){let Y=await y2.window.showInputBox({prompt:"Elaborate, or leave blank.",value:""});if(Y===void 0)return null;p=p.replace("{{input}}",Y)}let a=/\{\{input:.*?\}\}/g,X=p.match(a);if(X)for(let Y of X){let e=await y2.window.showInputBox({prompt:Y.replace("{{input:","").replace("}}",""),value:""});if(e===void 0)return null;p=p.replace(Y,e)}let R=/\{\{:(.*?):(.*?)\}\}/g,Q=p.match(R);if(Q)for(let Y of Q){let e=/\{\{:(.*?):(.*?)\}\}/.exec(Y);if(e&&e.length===3){let i=e[1],K=e[2];if(Object.hasOwnProperty.call(b,i)){let E1=/^[0-9]+(\.[0-9]+)?$/;Z[i]=E1.test(K)?Number(K):K}p=p.replace(Y,"")}}let F=X3[s1(I)]??"";return p=p.replaceAll("{{language_instructions}}",F),p.replaceAll("{{selection}}",G.selectedText).trim()},m=await W(l.message),c=await W(l.system??m1("system"));return{message:m,system:c,completionParams:Z}}var b2=class{webviewView;editor;selection;prepared;provider;providerInstance;constructor(I,d){if(this.webviewView=I,this.editor=W3.window.activeTextEditor,this.selection=r1(this.editor),d.promptId){let G=V.get(N.promptMap())[d.promptId];if(!G)throw new Error(`Command ${d.promptId} not found`);this.runCommand(G)}else if(d.message){let G={category:"",title:"",description:"",promptId:"",insertionMethod:"none",mode:V.get(N.activeMode()),modeId:V.get(N.activeMode()).id,system:m1("system"),message:d.message};this.runCommand(G)}}async runCommand(I){try{this.prepared=await G0(I)}catch(d){d.message==="Input was cancelled"&&s5(this.webviewView,"Input cancelled");return}if(this.provider=h1[m1("provider")].instance,!this.provider){I3(`Provider "${m1("provider")}" not found`);return}t1(this.webviewView,"chatInitiated"),t1(this.webviewView,"chatMessageSent",this.prepared.message),this.providerInstance=new this.provider(this.webviewView,this.prepared,d=>{t1(this.webviewView,"chatMessageReceived",d)});try{let d=await this.providerInstance.send();this.handleResponseInsertionMethod(d)}catch(d){e1(this.webviewView,d.message),this.abort()}t1(this.webviewView,"chatEnded")}async send(I){if(!this.providerInstance){I3("No provider instance found");return}t1(this.webviewView,"chatMessageSent",I);try{await this.providerInstance.send(I)}catch(d){e1(this.webviewView,d.message),this.abort()}t1(this.webviewView,"chatEnded")}extractFirstCodeBlock(I){let G=/```[\w-]*\n([\s\S]+)\n```/.exec(I);return G?G[1]:I}handleResponseInsertionMethod(I){let{insertionMethod:d}=this.prepared,G=W3.window.activeTextEditor;switch(I=this.extractFirstCodeBlock(I),d){case"replace":{H2(this.editor,this.selection,I);break}case"before":{F5(this.editor,this.selection,I);break}case"after":{R5(this.editor,this.selection,I);break}case"none":break;case"new":{N5(I);break}default:console.warn("Unknown insertionMethod",d)}}abort(){this.providerInstance?.abort(),this.webviewView.webview.postMessage({content:{type:"aborted"}})}},G0=async l=>{let I=await w5(l);if(I.message===null)throw new Error("Input was cancelled");let d=l.insertionMethod??"replace";return{...I,insertionMethod:d,url:m1("url")}};var t1=(l,I,d)=>{l.webview.postMessage({content:{type:I,value:d}})},s5=(l,I)=>{l.webview.postMessage({content:{type:"notification",message:I}})},e1=(l,I)=>{l.webview.postMessage({content:{type:"notificationError",message:I}})},p1=(l,I)=>{l.webview.postMessage({content:{type:"notificationSuccess",message:I}})},E2=(l,I)=>{l.webview.postMessage({content:{type:"notificationWarning",message:I}})},O,Z2=class l{constructor(I,d){this._extensionPath=I;this._extensionUri=d}static viewType="wingman.mainView";static _view;resolveWebviewView(I,d,G){I.webview.options={enableScripts:!0,localResourceRoots:[this._extensionUri]},I.onDidChangeVisibility(()=>{I.visible?I.webview.postMessage({content:{type:"shown"}}):I.webview.postMessage({content:{type:"hidden"}})}),l._view=I,I.webview.html=this.getWebviewHTML(I.webview),I.webview.onDidReceiveMessage(async b=>{try{let{id:Z,content:W}=b;if(!Z||!W)throw new Error("Invalid message format");let{type:m,key:c,value:p}=W;switch(m){case"get":{if(!c)throw new Error("Missing 'key' in 'get' request");switch(c){case"prompts":{I.webview.postMessage({id:Z,content:V.get(N.promptMap())});break}case"modes":{I.webview.postMessage({id:Z,content:V.get(N.modes())});break}case"activeModePrompts":{let a=V.get(N.promptMap()),X=d2(),R=Object.values(a).filter(Q=>Q.mode.id===X.id);I.webview.postMessage({id:Z,content:R});break}case"activeMode":{if(V.get(N.activeMode())){I.webview.postMessage({id:Z,content:V.get(N.activeMode())});return}let X=V.get(N.modes());X.length>0&&(V.set(N.activeMode(),X[0]),I.webview.postMessage({id:Z,content:X[0]}));break}case"presets":{I.webview.postMessage({id:Z,content:V.get(N.activeModePresets())||[]});break}case"activePreset":{I.webview.postMessage({id:Z,content:V.get(N.activeModeActivePreset())});break}case"providers":{I.webview.postMessage({id:Z,content:Object.keys(h1)});break}case"formats":{I.webview.postMessage({id:Z,content:Object.keys(Z3)});break}case"tokenizers":{I.webview.postMessage({id:Z,content:Object.keys(v5)});break}case"welcomeWizard":{I.webview.postMessage({id:Z,content:V.get(N.welcomeWizard())});break}case"providerCompletionParams":{let a=W1(p);I.webview.postMessage({id:Z,content:a});break}case"chatHistory":{let a=V.getWorkspace(N.activeModeChatHistory())??[];I.webview.postMessage({id:Z,content:a});break}default:throw new Error(`Invalid 'key' in 'get' request: ${c}`)}break}case"restoreDefaults":{u2(),I.webview.postMessage({id:Z,content:W});break}case"replaceSelection":{if(!O)return;H2(O.editor,O.selection,p);break}case"set":{if(!c||p===void 0)throw new Error("Missing 'key' or 'value' in 'set' request");switch(c){case"activeMode":{V.set(N.activeMode(),p);break}case"activePreset":{V.set(N.activeModeActivePreset(),p),I.webview.postMessage({id:Z,content:p});break}case"welcomeWizard":{V.set(N.welcomeWizard(),p);break}case"apiKey":{p3(p);break}default:throw new Error(`Invalid 'key' in 'set' request: ${c}`)}break}case"delete":{if(!c)throw new Error("Missing 'key' in 'delete' request");switch(c){case"preset":{let X=(V.get(N.activeModePresets())||[]).filter(Q=>Q.id!==p);if(X.length===0){e1(I,"Cannot delete the last remaining preset for this mode.");return}V.set(N.activeModePresets(),X);let R=X.find(Q=>Q.id!==p);R&&V.set(N.activeModeActivePreset(),R),p1(I,"Preset deleted."),I.webview.postMessage({id:Z,content:X});break}case"prompt":{let X={...V.get(N.promptMap())||{}};delete X[p.promptId],w1[p.promptId]=void 0,V.set(N.promptMap(),X),p1(I,"Prompt deleted."),I.webview.postMessage({id:Z,content:X});break}case"mode":{let a=p,X=V.get(N.modes())||[];if(X.length===1){e1(I,"Cannot delete the last remaining mode.");return}let R=X.filter(e=>e.id!==a.id);if(V.set(N.modes(),R),V.get(N.activeMode()).id===a.id){let e=R.find(i=>i.id!==a.id);V.set(N.activeMode(),e)}let Y={...V.get(N.promptMap())||{}};Object.values(Y).forEach(e=>{e?.mode?.id===a.id&&delete Y[e?.promptId]}),V.set(N.promptMap(),Y),V.set(`${a.id}-presets`,void 0),V.set(`${a.id}-activePreset`,void 0),V.set(`${a.id}-chatHistory`,void 0),p1(I,"Mode deleted."),I.webview.postMessage({id:Z,content:!0});break}default:throw new Error(`Invalid 'key' in 'delete' request: ${c}`)}break}case"create":{if(!c||p===void 0)throw new Error("Missing 'key' or 'value' in 'create' request");switch(c){case"newPreset":{let a=V.get(N.activeModePresets())||[],X=await C1.window.showInputBox({prompt:"Enter a name for your preset"});if(!X)return;let R={...p,name:X};a.push(R),V.set(N.activeModePresets(),a),p1(I,"Preset created."),I.webview.postMessage({id:Z,content:R});break}case"mode":{let a=p;if(!a.label.trim()){E2(I,"A mode must have a name.");return}a.id=d1();let X=V.get(N.modes());if(X.find(Q=>Q.label===a.label)){e1(I,"A mode with this name already exists.");return}let R=[...X,a];V.set(N.modes(),R),V.set(`${a.id}-presets`,[{id:d1(),name:"OpenAI (gpt-3.5-turbo)",provider:"OpenAI",format:"OpenAI",tokenizer:"OpenAI",url:"https://api.openai.com/v1/chat/completions",system:"You are a helpful assistant.",completionParams:{...W1("OpenAI")}}]),V.set(`${a.id}-activePreset`,V.get(`${a.id}-presets`)[0]),p1(I,"Mode created."),I.webview.postMessage({id:Z,content:a});break}case"prompt":{let a=p;if(!a.title){E2(I,"A prompt must have a title.");return}a.promptId=l3(a.title);let R={...V.get(N.promptMap())||{},[a.promptId]:a};w1[a.promptId]=a,V.set(N.promptMap(),R),p1(I,"Prompt created."),I.webview.postMessage({id:Z,content:a});break}default:throw new Error(`Invalid 'key' in 'create' request: ${c}`)}break}case"update":{if(!c||!p)throw new Error("Missing 'key' or 'value' in 'create' request");switch(c){case"preset":{let a=V.get(N.activeModePresets())||[];if(!a.find(F=>F.id===p.id))return;let R=a.map(F=>F.id===p.id?p:F);V.set(N.activeModePresets(),R),V.get(N.activeModeActivePreset()).id===p.id&&V.set(N.activeModeActivePreset(),p),p1(I,"Preset saved."),I.webview.postMessage({id:Z,content:p});break}case"mode":{let a=p;if(!a.label.trim()){E2(I,"A mode must have a name.");return}let X=V.get(N.modes());if(!X.find(Y=>Y.id===a.id))return;let Q=X.find(Y=>Y.label===a.label);if(Q&&Q.id!==a.id){e1(I,"A mode with this name already exists.");return}let F=X.map(Y=>Y.id===a.id?a:Y);V.set(N.modes(),F),p1(I,"Mode saved."),I.webview.postMessage({id:Z,content:a});break}case"prompt":{let a=V.get(N.promptMap())||{};if(!a[p.promptId])return;let R={...a,[p.promptId]:p};w1[p.promptId]=p,V.set(N.promptMap(),R),p1(I,"Prompt saved."),I.webview.postMessage({id:Z,content:p});break}case"chatHistory":{V.setWorkspace(N.activeModeChatHistory(),p);break}default:throw new Error(`Invalid 'key' in 'create' request: ${c}`)}break}case"run":{if(!c)throw new Error("Missing 'key' in 'run' request");if(!V.get(N.activeModeActivePreset())){e1(I,"No active preset.");return}O=new b2(I,{promptId:c});break}case"send":{if(!O)return;O.send(p);break}case"sendUnprompted":{O=new b2(I,{message:p});break}case"abort":{if(!O)return;O.abort(),I.webview.postMessage({id:Z,content:{type:"aborted"}});break}case"diff":{if(!O){E2(I,"No active dispatcher.");return}m3(O.selection.selectedText,p,s1(O.editor));break}case"diffSelection":{let a=C1.window.activeTextEditor,X=r1(a);m3(X.selectedText,p,s1(a));break}default:throw new Error(`Invalid 'type' in message: ${m}`)}}catch(Z){console.error("Error processing message:",Z)}})}getWebviewHTML(I){let d=I.asWebviewUri(C1.Uri.joinPath(this._extensionUri,"dist","style.css")),G=I.asWebviewUri(C1.Uri.joinPath(this._extensionUri,"dist","webview.umd.js"));return` +{user}`,stops:["### User:"]}},b3=(l,I)=>{let d=Z3[l],G=d.system.replace("{system_message}",I.system),b=d.user.replace("{user_message}",I.message),Z=d.first.replace("{system}",G).replace("{user}",b);return{system:G,first:Z,user:b}},h1={OpenAI:{instance:t2,completionParams:[{name:"n",default:1},{name:"model",default:"gpt-3.5-turbo"},{name:"temperature",default:.3},{name:"max_tokens",default:2048},{name:"frequency_penalty",default:0},{name:"presence_penalty",default:0},{name:"top_p",default:1},{name:"stop",default:[]}]},Anthropic:{instance:w2,completionParams:[{name:"max_tokens_to_sample",default:1e5},{name:"top_k",default:5},{name:"top_p",default:.7},{name:"model",default:"claude-instant-1"},{name:"temperature",default:.3}]}},v5={OpenAI:{instance:o1},Llama:{instance:o1},Anthropic:{instance:o1}},W1=l=>{let I={};return h1[l].completionParams.forEach(d=>{I[d.name]=d.default}),I},e5={label:"Programming",id:H};var C1=a1(require("vscode"));var T=a1(require("vscode")),c3="my-diff",d0={provideTextDocumentContent(l){return Buffer.from(l.query,"base64").toString()}};T.workspace.registerTextDocumentContentProvider(c3,d0);var m3=(l,I,d)=>{let G=T.Uri.parse(`${c3}:/original?${Buffer.from(l).toString("base64")}`),b=T.Uri.parse(`${c3}:/suggestion?${Buffer.from(I).toString("base64")}`);T.workspace.openTextDocument(G).then(Z=>{T.workspace.openTextDocument(b).then(W=>{d&&(T.languages.setTextDocumentLanguage(Z,d),T.languages.setTextDocumentLanguage(W,d)),T.commands.executeCommand("vscode.diff",Z.uri,W.uri)})}),T.commands.executeCommand("vscode.diff",G,b)};var W3=a1(require("vscode"));var y2=a1(require("vscode"));async function w5(l){let I=y2.window.activeTextEditor,d=V3(s1(I),Y5(I)),G=r1(I),b=m1("completionParams"),Z=Object.fromEntries(Object.entries(b).filter(([p,a])=>a!=null)),W=async p=>{if(p=p.replaceAll("{{ft}}",s1(I)),p=p.replaceAll("{{language}}",d),p.includes("{{input}}")){let Y=await y2.window.showInputBox({prompt:"Elaborate, or leave blank.",value:""});if(Y===void 0)return null;p=p.replace("{{input}}",Y)}let a=/\{\{input:.*?\}\}/g,X=p.match(a);if(X)for(let Y of X){let e=await y2.window.showInputBox({prompt:Y.replace("{{input:","").replace("}}",""),value:""});if(e===void 0)return null;p=p.replace(Y,e)}let R=/\{\{:(.*?):(.*?)\}\}/g,Q=p.match(R);if(Q)for(let Y of Q){let e=/\{\{:(.*?):(.*?)\}\}/.exec(Y);if(e&&e.length===3){let i=e[1],K=e[2];if(Object.hasOwnProperty.call(b,i)){let E1=/^[0-9]+(\.[0-9]+)?$/;Z[i]=E1.test(K)?Number(K):K}p=p.replace(Y,"")}}let F=X3[s1(I)]??"";return p=p.replaceAll("{{language_instructions}}",F),p=p.replaceAll("{{selection}}",G.selectedText).trim(),p.replaceAll("{{file}}",I.document.getText())},m=await W(l.message),c=await W(l.system??m1("system"));return{message:m,system:c,completionParams:Z}}var b2=class{webviewView;editor;selection;prepared;provider;providerInstance;constructor(I,d){if(this.webviewView=I,this.editor=W3.window.activeTextEditor,this.selection=r1(this.editor),d.promptId){let G=V.get(N.promptMap())[d.promptId];if(!G)throw new Error(`Command ${d.promptId} not found`);this.runCommand(G)}else if(d.message){let G={category:"",title:"",description:"",promptId:"",insertionMethod:"none",mode:V.get(N.activeMode()),modeId:V.get(N.activeMode()).id,system:m1("system"),message:d.message};this.runCommand(G)}}async runCommand(I){try{this.prepared=await G0(I)}catch(d){d.message==="Input was cancelled"&&s5(this.webviewView,"Input cancelled");return}if(this.provider=h1[m1("provider")].instance,!this.provider){I3(`Provider "${m1("provider")}" not found`);return}t1(this.webviewView,"chatInitiated"),t1(this.webviewView,"chatMessageSent",this.prepared.message),this.providerInstance=new this.provider(this.webviewView,this.prepared,d=>{t1(this.webviewView,"chatMessageReceived",d)});try{let d=await this.providerInstance.send();this.handleResponseInsertionMethod(d)}catch(d){e1(this.webviewView,d.message),this.abort()}t1(this.webviewView,"chatEnded")}async send(I){if(!this.providerInstance){I3("No provider instance found");return}t1(this.webviewView,"chatMessageSent",I);try{await this.providerInstance.send(I)}catch(d){e1(this.webviewView,d.message),this.abort()}t1(this.webviewView,"chatEnded")}extractFirstCodeBlock(I){let G=/```[\w-]*\n([\s\S]+)\n```/.exec(I);return G?G[1]:I}handleResponseInsertionMethod(I){let{insertionMethod:d}=this.prepared,G=W3.window.activeTextEditor;switch(I=this.extractFirstCodeBlock(I),d){case"replace":{H2(this.editor,this.selection,I);break}case"before":{F5(this.editor,this.selection,I);break}case"after":{R5(this.editor,this.selection,I);break}case"none":break;case"new":{N5(I);break}default:console.warn("Unknown insertionMethod",d)}}abort(){this.providerInstance?.abort(),this.webviewView.webview.postMessage({content:{type:"aborted"}})}},G0=async l=>{let I=await w5(l);if(I.message===null)throw new Error("Input was cancelled");let d=l.insertionMethod??"replace";return{...I,insertionMethod:d,url:m1("url")}};var t1=(l,I,d)=>{l.webview.postMessage({content:{type:I,value:d}})},s5=(l,I)=>{l.webview.postMessage({content:{type:"notification",message:I}})},e1=(l,I)=>{l.webview.postMessage({content:{type:"notificationError",message:I}})},p1=(l,I)=>{l.webview.postMessage({content:{type:"notificationSuccess",message:I}})},E2=(l,I)=>{l.webview.postMessage({content:{type:"notificationWarning",message:I}})},O,Z2=class l{constructor(I,d){this._extensionPath=I;this._extensionUri=d}static viewType="wingman.mainView";static _view;resolveWebviewView(I,d,G){I.webview.options={enableScripts:!0,localResourceRoots:[this._extensionUri]},I.onDidChangeVisibility(()=>{I.visible?I.webview.postMessage({content:{type:"shown"}}):I.webview.postMessage({content:{type:"hidden"}})}),l._view=I,I.webview.html=this.getWebviewHTML(I.webview),I.webview.onDidReceiveMessage(async b=>{try{let{id:Z,content:W}=b;if(!Z||!W)throw new Error("Invalid message format");let{type:m,key:c,value:p}=W;switch(m){case"get":{if(!c)throw new Error("Missing 'key' in 'get' request");switch(c){case"prompts":{I.webview.postMessage({id:Z,content:V.get(N.promptMap())});break}case"modes":{I.webview.postMessage({id:Z,content:V.get(N.modes())});break}case"activeModePrompts":{let a=V.get(N.promptMap()),X=d2(),R=Object.values(a).filter(Q=>Q.mode.id===X.id);I.webview.postMessage({id:Z,content:R});break}case"activeMode":{if(V.get(N.activeMode())){I.webview.postMessage({id:Z,content:V.get(N.activeMode())});return}let X=V.get(N.modes());X.length>0&&(V.set(N.activeMode(),X[0]),I.webview.postMessage({id:Z,content:X[0]}));break}case"presets":{I.webview.postMessage({id:Z,content:V.get(N.activeModePresets())||[]});break}case"activePreset":{I.webview.postMessage({id:Z,content:V.get(N.activeModeActivePreset())});break}case"providers":{I.webview.postMessage({id:Z,content:Object.keys(h1)});break}case"formats":{I.webview.postMessage({id:Z,content:Object.keys(Z3)});break}case"tokenizers":{I.webview.postMessage({id:Z,content:Object.keys(v5)});break}case"welcomeWizard":{I.webview.postMessage({id:Z,content:V.get(N.welcomeWizard())});break}case"providerCompletionParams":{let a=W1(p);I.webview.postMessage({id:Z,content:a});break}case"chatHistory":{let a=V.getWorkspace(N.activeModeChatHistory())??[];I.webview.postMessage({id:Z,content:a});break}default:throw new Error(`Invalid 'key' in 'get' request: ${c}`)}break}case"restoreDefaults":{u2(),I.webview.postMessage({id:Z,content:W});break}case"replaceSelection":{if(!O)return;H2(O.editor,O.selection,p);break}case"set":{if(!c||p===void 0)throw new Error("Missing 'key' or 'value' in 'set' request");switch(c){case"activeMode":{V.set(N.activeMode(),p);break}case"activePreset":{V.set(N.activeModeActivePreset(),p),I.webview.postMessage({id:Z,content:p});break}case"welcomeWizard":{V.set(N.welcomeWizard(),p);break}case"apiKey":{p3(p);break}default:throw new Error(`Invalid 'key' in 'set' request: ${c}`)}break}case"delete":{if(!c)throw new Error("Missing 'key' in 'delete' request");switch(c){case"preset":{let X=(V.get(N.activeModePresets())||[]).filter(Q=>Q.id!==p);if(X.length===0){e1(I,"Cannot delete the last remaining preset for this mode.");return}V.set(N.activeModePresets(),X);let R=X.find(Q=>Q.id!==p);R&&V.set(N.activeModeActivePreset(),R),p1(I,"Preset deleted."),I.webview.postMessage({id:Z,content:X});break}case"prompt":{let X={...V.get(N.promptMap())||{}};delete X[p.promptId],w1[p.promptId]=void 0,V.set(N.promptMap(),X),p1(I,"Prompt deleted."),I.webview.postMessage({id:Z,content:X});break}case"mode":{let a=p,X=V.get(N.modes())||[];if(X.length===1){e1(I,"Cannot delete the last remaining mode.");return}let R=X.filter(e=>e.id!==a.id);if(V.set(N.modes(),R),V.get(N.activeMode()).id===a.id){let e=R.find(i=>i.id!==a.id);V.set(N.activeMode(),e)}let Y={...V.get(N.promptMap())||{}};Object.values(Y).forEach(e=>{e?.mode?.id===a.id&&delete Y[e?.promptId]}),V.set(N.promptMap(),Y),V.set(`${a.id}-presets`,void 0),V.set(`${a.id}-activePreset`,void 0),V.set(`${a.id}-chatHistory`,void 0),p1(I,"Mode deleted."),I.webview.postMessage({id:Z,content:!0});break}default:throw new Error(`Invalid 'key' in 'delete' request: ${c}`)}break}case"create":{if(!c||p===void 0)throw new Error("Missing 'key' or 'value' in 'create' request");switch(c){case"newPreset":{let a=V.get(N.activeModePresets())||[],X=await C1.window.showInputBox({prompt:"Enter a name for your preset"});if(!X)return;let R={...p,name:X};a.push(R),V.set(N.activeModePresets(),a),p1(I,"Preset created."),I.webview.postMessage({id:Z,content:R});break}case"mode":{let a=p;if(!a.label.trim()){E2(I,"A mode must have a name.");return}a.id=d1();let X=V.get(N.modes());if(X.find(Q=>Q.label===a.label)){e1(I,"A mode with this name already exists.");return}let R=[...X,a];V.set(N.modes(),R),V.set(`${a.id}-presets`,[{id:d1(),name:"OpenAI (gpt-3.5-turbo)",provider:"OpenAI",format:"OpenAI",tokenizer:"OpenAI",url:"https://api.openai.com/v1/chat/completions",system:"You are a helpful assistant.",completionParams:{...W1("OpenAI")}}]),V.set(`${a.id}-activePreset`,V.get(`${a.id}-presets`)[0]),p1(I,"Mode created."),I.webview.postMessage({id:Z,content:a});break}case"prompt":{let a=p;if(!a.title){E2(I,"A prompt must have a title.");return}a.promptId=l3(a.title);let R={...V.get(N.promptMap())||{},[a.promptId]:a};w1[a.promptId]=a,V.set(N.promptMap(),R),p1(I,"Prompt created."),I.webview.postMessage({id:Z,content:a});break}default:throw new Error(`Invalid 'key' in 'create' request: ${c}`)}break}case"update":{if(!c||!p)throw new Error("Missing 'key' or 'value' in 'create' request");switch(c){case"preset":{let a=V.get(N.activeModePresets())||[];if(!a.find(F=>F.id===p.id))return;let R=a.map(F=>F.id===p.id?p:F);V.set(N.activeModePresets(),R),V.get(N.activeModeActivePreset()).id===p.id&&V.set(N.activeModeActivePreset(),p),p1(I,"Preset saved."),I.webview.postMessage({id:Z,content:p});break}case"mode":{let a=p;if(!a.label.trim()){E2(I,"A mode must have a name.");return}let X=V.get(N.modes());if(!X.find(Y=>Y.id===a.id))return;let Q=X.find(Y=>Y.label===a.label);if(Q&&Q.id!==a.id){e1(I,"A mode with this name already exists.");return}let F=X.map(Y=>Y.id===a.id?a:Y);V.set(N.modes(),F),p1(I,"Mode saved."),I.webview.postMessage({id:Z,content:a});break}case"prompt":{let a=V.get(N.promptMap())||{};if(!a[p.promptId])return;let R={...a,[p.promptId]:p};w1[p.promptId]=p,V.set(N.promptMap(),R),p1(I,"Prompt saved."),I.webview.postMessage({id:Z,content:p});break}case"chatHistory":{V.setWorkspace(N.activeModeChatHistory(),p);break}default:throw new Error(`Invalid 'key' in 'create' request: ${c}`)}break}case"run":{if(!c)throw new Error("Missing 'key' in 'run' request");if(!V.get(N.activeModeActivePreset())){e1(I,"No active preset.");return}O=new b2(I,{promptId:c});break}case"send":{if(!O)return;O.send(p);break}case"sendUnprompted":{O=new b2(I,{message:p});break}case"abort":{if(!O)return;O.abort(),I.webview.postMessage({id:Z,content:{type:"aborted"}});break}case"diff":{if(!O){E2(I,"No active dispatcher.");return}m3(O.selection.selectedText,p,s1(O.editor));break}case"diffSelection":{let a=C1.window.activeTextEditor,X=r1(a);m3(X.selectedText,p,s1(a));break}default:throw new Error(`Invalid 'type' in message: ${m}`)}}catch(Z){console.error("Error processing message:",Z)}})}getWebviewHTML(I){let d=I.asWebviewUri(C1.Uri.joinPath(this._extensionUri,"dist","style.css")),G=I.asWebviewUri(C1.Uri.joinPath(this._extensionUri,"dist","webview.umd.js"));return`
diff --git a/extension/src/command.ts b/extension/src/command.ts index 5994386..863da3e 100644 --- a/extension/src/command.ts +++ b/extension/src/command.ts @@ -74,7 +74,8 @@ export async function createPrompt(prompt: PromptDefinition & { promptId: string const inst = languageInstructions[languageid(editor)] ?? ""; text = text.replaceAll("{{language_instructions}}", inst); - return text.replaceAll("{{selection}}", selection.selectedText).trim(); + text = text.replaceAll("{{selection}}", selection.selectedText).trim(); + return text.replaceAll("{{file}}", editor.document.getText()); }; const message = await substitute(prompt.message);