Skip to content

Commit

Permalink
Merge pull request #67 from neatwork-ai/hotfix-lazy-api-key-model-setup
Browse files Browse the repository at this point in the history
Hotfix lazy api key model setup
  • Loading branch information
kaizu-xyz authored Nov 7, 2023
2 parents c0e8798 + 8a4ef98 commit c5a003a
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 40 deletions.
47 changes: 47 additions & 0 deletions bin/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Get the directory
DIR="$(dirname "$0")"

# Run the first script
bash "$DIR/build_wasm.sh"

# Check if the first script ran successfully
if [ $? -eq 0 ]; then
echo "Built WASM Library successfully"
else
echo "WASM Lib failed"
exit 1
fi

# Run the second script
bash "$DIR/build_webview.sh"

# Check if the second script ran successfully
if [ $? -eq 0 ]; then
echo "Built VSCE successfully"
else
echo "VSCE failed"
exit 1
fi

#!/bin/sh

# Navigate to the wasm-lib directory
cd vsce/

# Compile the wasm library (assuming you're using wasm-pack)
vsce publish

cd ../


# # Check if the second script ran successfully
# if [ $? -eq 0 ]; then
# echo "Built VSCE successfully"
# else
# echo "VSCE failed"
# exit 1
# fi

echo "Publishing complete"
2 changes: 1 addition & 1 deletion crates/neatcoder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "neatcoder"
version = "0.2.0"
version = "0.2.1"
edition = "2021"

[lib]
Expand Down
5 changes: 5 additions & 0 deletions vsce/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to the "neatcoder" extension will be documented in this file

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [0.2.1] - 05/11/2023

### Changed
- Lazily setup API Keys and LLM Model

## [0.2.0] - 03/11/2023

### Added
Expand Down
4 changes: 2 additions & 2 deletions vsce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Turn your IDE into an AI Software Engineer with Neatcoder 🧞‍♂️

Neatwork AI enhances VS Code by integrating advanced AI capabilities. It provides you with the ability to dynamically scaffold entire codebases from a simple project description. Given that most software interacts with other software, Neatwork AI allows you to make its AI aware of external interfaces the software ought to communicate with.
Neatwork AI enhances VS Code by integrating advanced AI capabilities. It provides you with the ability to dynamically scaffold entire codebases from a simple project description. Given that most software interacts with other software, Neatwork AI allows you to make its AI aware of external interfaces, such as data models from your Databases or endpoints from your APIs.

## Features

Expand Down Expand Up @@ -83,7 +83,7 @@ TLDR:
1. Are solely stored locally;
2. We do not store nor have access to them.
2. We collect analytics to learn how users are using the extension, for the purpose of improving the product
3. The data policy might change in future versions
3. The data policy might change in future versions as the product evolves

You can find the full policy at [https://www.neatwork.ai/privacy](https://www.neatwork.ai/privacy)

Expand Down
2 changes: 1 addition & 1 deletion vsce/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "neatcoder",
"displayName": "Neatwork AI - GPT4 on Steroids",
"description": "Turn your IDE into an AI Sofware engineer.",
"version": "0.2.0",
"version": "0.2.1",
"publisher": "NeatworkAi",
"repository": {
"url": "https://github.com/neatwork-ai/neatcoder-issues.git",
Expand Down
3 changes: 3 additions & 0 deletions vsce/src/chat/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getChatById,
getOrCreateConfigPath,
getOrInitConfig,
getOrSetApiKey,
getOrSetModelVersion,
getRoot,
storeChat,
Expand Down Expand Up @@ -137,6 +138,8 @@ const setupWebviewSockets = async (
const msgs: Array<wasm.Message> = message.msgs;
const isFirst = msgs.length === 1 ? true : false;

await getOrSetApiKey();

promptLLM(panel, message);

if (isFirst) {
Expand Down
10 changes: 6 additions & 4 deletions vsce/src/chat/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import * as https from "https";
import * as url from "url";
import { MessageBuffer } from "../utils/httpClient";

export function buildRequest(
export async function buildRequest(
msgs: Array<wasm.Message>,
stream: boolean
): [any, any] {
const apiKey = getOrSetApiKey();
): Promise<[any, any]> {
console.log("GOING FOR IT");
const apiKey = await getOrSetApiKey();
console.log("Skipping API KEY");

try {
console.log("Messages: " + JSON.stringify(msgs.map((msg) => msg.payload)));
Expand All @@ -31,7 +33,7 @@ export async function promptLLM(
const msgs: Array<wasm.Message> = message.msgs;
const stream = message.stream;

const [apiKey, body] = buildRequest(msgs, stream);
const [apiKey, body] = await buildRequest(msgs, stream);

return new Promise((resolve, reject) => {
let messageBuffer = new MessageBuffer();
Expand Down
7 changes: 3 additions & 4 deletions vsce/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TasksCompletedProvider,
} from "./taskPool";
import { initCodeBase, appDataManager, setupDotNeatWatcher } from "./core";
import { getOrSetApiKey, initStatusBar, initLogger, logger } from "./utils";
import { initStatusBar, initLogger, logger } from "./utils";
import { ChatProvider, initChat, setupChatWatcher } from "./chat";
import { getOrSetModelVersion, setModelVersion } from "./utils/utils";
import { ChatItem } from "./chat/providers";
Expand All @@ -45,7 +45,6 @@ export async function activate(context: vscode.ExtensionContext) {

initStatusBar(context);
initLogger(context);
getOrSetApiKey();

// Create the output channel for logging
logger.appendLine("[INFO] Extension Name: Neatcoder");
Expand All @@ -64,9 +63,9 @@ export async function activate(context: vscode.ExtensionContext) {
// Read or Initialize Application state

let appManager = new appDataManager(jobQueueProvider, auditTrailProvider);
let llmParams = await getLLMParams();
// let llmParams = await getLLMParams();

logger.appendLine("[INFO] Extension Activated. llmParams: " + llmParams);
// logger.appendLine("[INFO] Extension Activated. llmParams: " + llmParams);

// === Setup File Watchers ===

Expand Down
4 changes: 2 additions & 2 deletions vsce/src/utils/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let isCodeBlockEnded = false;
* @return {Promise<object>} - A promise that resolves to the response object from the OpenAI API.
*/
export async function makeRequest(body: string): Promise<object> {
const apiKey = getOrSetApiKey();
const apiKey = await getOrSetApiKey();

try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
Expand Down Expand Up @@ -61,14 +61,14 @@ export async function makeStreamingRequest(
activeTextDocument: TextDocument
): Promise<void> {
cleanup();
const apiKey = await getOrSetApiKey();

return new Promise((resolve, reject) => {
// let responseLog: string[] = []; // TODO: Only debug
let streamedTokens = 0;

let messageBuffer = new MessageBuffer();

const apiKey = getOrSetApiKey();
try {
const urlString = "https://api.openai.com/v1/chat/completions";
const parsedUrl = url.parse(urlString);
Expand Down
71 changes: 46 additions & 25 deletions vsce/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,30 +230,48 @@ export function getFilename(filepath: string): string {
return parts[parts.length - 1];
}

export function getOrSetApiKey(): any {
let config = vscode.workspace.getConfiguration("extension");
let apiKey = config.get("apiKey");

if (!apiKey) {
vscode.window
.showInputBox({
prompt: "Please enter your API key",
ignoreFocusOut: true,
})
.then((value) => {
if (value) {
config.update("apiKey", value, vscode.ConfigurationTarget.Global);
vscode.window.showInformationMessage("API key saved!");
} else {
// Handle the case where the input box was dismissed without entering a value
vscode.window.showErrorMessage(
"API key is required to use this extension."
);
}
});
}

return apiKey;
export function getOrSetApiKey(): Promise<any> {
return new Promise((resolve, reject) => {
let config = vscode.workspace.getConfiguration("extension");
let apiKey = config.get("apiKey");

if (apiKey) {
// If the API key is already set, resolve the Promise immediately.
resolve(apiKey);
} else {
// Show the input box to the user to enter the API key.
vscode.window
.showInputBox({
prompt: "Please enter your API key",
ignoreFocusOut: true,
})
.then((value) => {
if (value) {
// Update the configuration with the new API key.
config
.update("apiKey", value, vscode.ConfigurationTarget.Global)
.then(
() => {
vscode.window.showInformationMessage("API key saved!");
resolve(value); // Resolve the Promise with the new API key.
},
(error) => {
vscode.window.showErrorMessage(
`Failed to save API key: ${error}`
);
reject(error); // Reject the Promise if there was an error saving the API key.
}
);
} else {
// Handle the case where the input box was dismissed without entering a value.
vscode.window.showErrorMessage(
"API key is required to use this extension."
);
reject("API key not provided."); // Reject the Promise as no API key was provided.
}
});
}
});
}

export async function getOrSetModelVersion(): Promise<wasm.OpenAIModels | null> {
Expand All @@ -263,7 +281,10 @@ export async function getOrSetModelVersion(): Promise<wasm.OpenAIModels | null>
if (!modelVersion) {
const value = await vscode.window.showQuickPick(
["gpt-3.5-turbo-16k", "gpt-4"],
{ canPickMany: false }
{
canPickMany: false,
placeHolder: "Select an OpenAI model", // This is the placeholder text
}
);
if (value) {
await config.update(
Expand Down
2 changes: 1 addition & 1 deletion webview/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chat-view",
"version": "0.2.0",
"version": "0.2.1",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
Expand Down

0 comments on commit c5a003a

Please sign in to comment.