Skip to content

Commit

Permalink
unprompted chat, focus input on webview reveal
Browse files Browse the repository at this point in the history
  • Loading branch information
nvms committed Dec 8, 2023
1 parent 2e65e25 commit cf705d3
Show file tree
Hide file tree
Showing 8 changed files with 4,847 additions and 4,718 deletions.
14 changes: 7 additions & 7 deletions extension/dist/extension.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion extension/dist/style.css

Large diffs are not rendered by default.

9,385 changes: 4,726 additions & 4,659 deletions extension/dist/webview.es.js

Large diffs are not rendered by default.

88 changes: 44 additions & 44 deletions extension/dist/webview.umd.js

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions extension/src/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { ChatEvents, InsertionMethod, Preset, PromptDefinition } from "../../shared";
import { ChatEvents, InsertionMethod, Mode, Preset, PromptDefinition } from "../../shared";
import { createPrompt } from "./command";
import { providers } from "./providers/common";
import { OpenAIProvider } from "./providers/openai";
Expand All @@ -8,7 +8,8 @@ import { addTextAfterSelection, addTextBeforeSelection, addTextToNewBuffer, aler
import { sendEvent, sendNotification, sendNotificationError } from "./views/main";

interface DispatcherOptions {
promptId: string;
promptId?: string;
message?: string;
}

export class Dispatcher {
Expand All @@ -24,13 +25,30 @@ export class Dispatcher {
this.editor = vscode.window.activeTextEditor;
this.selection = getSelectionInfo(this.editor);

const command = State.get(stateKeys.promptMap())[options.promptId];

if (!command) {
throw new Error(`Command ${options.promptId} not found`);
if (options.promptId) {
const command = State.get(stateKeys.promptMap())[options.promptId];

if (!command) {
throw new Error(`Command ${options.promptId} not found`);
}

this.runCommand(command);
} else if (options.message) {
const command: PromptDefinition & { mode: Mode; promptId: string; } = {
category: "",
title: "",
description: "",
promptId: "",
insertionMethod: InsertionMethod.None,
mode: State.get(stateKeys.activeMode()) as Mode,
modeId: (State.get(stateKeys.activeMode()) as Mode).id,
system: getActiveModeActivePresetKeyValue("system") as string,
message: options.message,
};

this.runCommand(command);
}

this.runCommand(command);
}

async runCommand(command: PromptDefinition & { promptId }) {
Expand Down
14 changes: 14 additions & 0 deletions extension/src/views/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ export class MainViewProvider implements vscode.WebviewViewProvider {
localResourceRoots: [this._extensionUri],
};

webviewView.onDidChangeVisibility(() => {
if (webviewView.visible) {
// SecondaryViewProvider.postMessage({ type: "shown" });
webviewView.webview.postMessage({ content: { type: "shown" } });
} else {
// SecondaryViewProvider.postMessage({ type: "hidden" });
webviewView.webview.postMessage({ content: { type: "hidden" } });
}
});

MainViewProvider._view = webviewView;

webviewView.webview.html = this.getWebviewHTML(webviewView.webview);
Expand Down Expand Up @@ -447,6 +457,10 @@ export class MainViewProvider implements vscode.WebviewViewProvider {
currentDispatcher.send(value);
break;
}
case "sendUnprompted": {
currentDispatcher = new Dispatcher(webviewView, { message: value });
break;
}
case "abort": {
if (!currentDispatcher) return;

Expand Down
26 changes: 26 additions & 0 deletions webview/src/components/ModeView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@
}
activeMode.subscribe(getHistory);
const listenShown = extComm.on("shown", () => {
tick().then(() => input?.focus());
});
const listenHidden = extComm.on("hidden", () => {
input?.blur();
});
onMount(() => {
tick().then(() => input?.focus());
Expand All @@ -154,6 +162,8 @@
listenChatMessageReceived();
listenChatMessageSent();
listenAbort();
listenShown();
listenHidden();
};
});
Expand Down Expand Up @@ -191,6 +201,14 @@
_setInputValue("");
};
const onNewChatSubmit = (e) => {
const input = e.detail;
if (input.trim() === "") { return; }
if (responseInProgress) return;
extComm.SEND_UNPROMPTED(input);
_setInputValue("");
};
let setInputValue;
function _setInputValue (v) {
Expand Down Expand Up @@ -277,6 +295,14 @@
placeholder="Say something..."
on:submit={onChatSubmit}
/>
{:else}
<GrowingTextarea
bind:this={input}
bind:setValue={setInputValue}
class="w-full border border-panel p-2"
placeholder="Say something..."
on:submit={onNewChatSubmit}
/>
{/if}
</div>
<status-bar class="w-full max-w-[768px] pt-2 flex justify-between h-8">
Expand Down
4 changes: 4 additions & 0 deletions webview/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class ExtensionCommunication {
return this.sendMessage({ type: "send", value });
}

public SEND_UNPROMPTED(value: string): Promise<any> {
return this.sendMessage({ type: "sendUnprompted", value });
}

public REPLACE_SELECTION(value: string): Promise<any> {
return this.sendMessage({ type: "replaceSelection", value });
}
Expand Down

0 comments on commit cf705d3

Please sign in to comment.