Skip to content

Commit

Permalink
Displaying response as it is typed, other improvements
Browse files Browse the repository at this point in the history
- chatgpt-api v1.4.0
- update displayed response as the AI writes it instead of just displaying it at the end
- update session token without having to restart extension
- response remains even when closing and reopening panel
- error message for invalid session token
- updated README.md
  • Loading branch information
timkmecl committed Dec 6, 2022
1 parent fe37693 commit 228d5ce
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 231 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ To use this extension, you will need to authenticate with a valid session token

Once you have obtained a session token, you can configure the extension to use it as described in the previous section.

Extensions should then be restarted in order to work.


## Using the Extension

Expand All @@ -43,13 +41,6 @@ You can also select a code snippet in the editor and then enter a prompt in the

To insert a code snippet from the AI's response into the editor, simply click on the code block in the panel. The code will be automatically inserted at the cursor position in the active editor.


## Current Issues

*This is only applicable if you want to compile from source code*

After installing the dependencies, you need to modify the `exports` property of `node_modules\chatgpt\package.json` by moving the `default` property to be the last one in the object (`"exports": { "import": "./build/index.js", "types": "./build/index.d.ts", "default": "./build/index.js" },`)

---

Please note that this extension is currently a proof of concept and may have some limitations or bugs. We welcome feedback and contributions to improve the extension.
Expand Down
33 changes: 28 additions & 5 deletions media/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ts-check
// @ts-ignore

// This script will be run within the webview itself
// It cannot access the main VS Code APIs directly.
Expand All @@ -23,16 +23,39 @@
}
});

function fixCodeBlocks(response) {
// Use a regular expression to find all occurrences of the substring in the string
const REGEX_CODEBLOCK = new RegExp('\`\`\`', 'g');
const matches = response.match(REGEX_CODEBLOCK);

// Return the number of occurrences of the substring in the response, check if even
const count = matches ? matches.length : 0;
if (count % 2 === 0) {
return response;
} else {
// else append ``` to the end to make the last code block complete
return response.concat('\n\`\`\`');
}

}

function setResponse() {
var converter = new showdown.Converter();
var converter = new showdown.Converter({
omitExtraWLInCodeBlocks: true,
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
literalMidWordUnderscores: true,
simpleLineBreaks: true
});
response = fixCodeBlocks(response);
html = converter.makeHtml(response);
document.getElementById("response").innerHTML = html;

var preCodeBlocks = document.querySelectorAll("pre code");
for (var i = 0; i < preCodeBlocks.length; i++) {
preCodeBlocks[i].classList.add(
"p-4",
"my-4",
"p-2",
"my-2",
"block"
);
}
Expand All @@ -43,7 +66,7 @@
codeBlocks[i].innerText = codeBlocks[i].innerText.replace("Copy code", "");
}

codeBlocks[i].classList.add("p-1", "inline-flex", "max-w-full", "overflow-hidden", "border", "rounded-sm", "cursor-pointer");
codeBlocks[i].classList.add("p-1", "inline-flex", "max-w-full", "overflow-x-scroll", "border", "rounded-sm", "cursor-pointer");

codeBlocks[i].addEventListener('click', function (e) {
e.preventDefault();
Expand Down
Loading

0 comments on commit 228d5ce

Please sign in to comment.