🐛 fix chat feedback

This commit is contained in:
Nate Sesti 2024-01-11 11:51:12 -08:00
parent 78140e5f6c
commit ae5137398b
7 changed files with 82 additions and 48 deletions

View file

@ -66,7 +66,7 @@ export async function ideRequest(type: string, message: any): Promise<any> {
export async function* ideStreamRequest(
type: string,
message: any
): AsyncGenerator<any> {
): AsyncGenerator<any, any> {
const messageId = uuidv4();
postToIde(type, { message, messageId });
@ -74,12 +74,14 @@ export async function* ideStreamRequest(
let buffer = "";
let index = 0;
let done = false;
let returnVal = undefined;
const handler = (event: any) => {
if (event.data.messageId === messageId) {
if (event.data.message.done) {
window.removeEventListener("message", handler);
done = true;
returnVal = event.data.message.data;
} else {
buffer += event.data.message.content;
}
@ -101,4 +103,6 @@ export async function* ideStreamRequest(
index = buffer.length;
yield chunk;
}
return returnVal;
}

8
core/index.d.ts vendored
View file

@ -1,5 +1,9 @@
import { Chunk } from "./index/chunk";
export interface LLMReturnValue {
prompt: string;
completion: string;
}
export interface ILLM extends LLMOptions {
get providerName(): ModelProvider;
@ -31,12 +35,12 @@ export interface ILLM extends LLMOptions {
streamComplete(
prompt: string,
options?: LLMFullCompletionOptions
): AsyncGenerator<string>;
): AsyncGenerator<string, LLMReturnValue>;
streamChat(
messages: ChatMessage[],
options?: LLMFullCompletionOptions
): AsyncGenerator<ChatMessage>;
): AsyncGenerator<ChatMessage, LLMReturnValue>;
chat(
messages: ChatMessage[],

View file

@ -5,6 +5,7 @@ import {
ILLM,
LLMFullCompletionOptions,
LLMOptions,
LLMReturnValue,
ModelProvider,
RequestOptions,
TemplateType,
@ -377,14 +378,22 @@ export abstract class BaseLLM implements ILLM {
options: LLMFullCompletionOptions = {}
) {
if (!this._shouldRequestDirectly()) {
for await (const content of ideStreamRequest("llmStreamComplete", {
const gen = ideStreamRequest("llmStreamComplete", {
prompt,
title: this.title,
completionOptions: options,
})) {
yield content;
});
let next = await gen.next();
while (!next.done) {
yield next.value;
next = await gen.next();
}
return;
return {
prompt: next.value?.prompt,
completion: next.value?.completion,
};
}
const { completionOptions, log, raw } =
@ -417,6 +426,8 @@ export abstract class BaseLLM implements ILLM {
}
this._logTokensGenerated(completionOptions.model, completion);
return { prompt, completion };
}
async complete(prompt: string, options: LLMFullCompletionOptions = {}) {
@ -470,16 +481,19 @@ export abstract class BaseLLM implements ILLM {
async *streamChat(
messages: ChatMessage[],
options: LLMFullCompletionOptions = {}
): AsyncGenerator<ChatMessage> {
): AsyncGenerator<ChatMessage, LLMReturnValue> {
if (!this._shouldRequestDirectly()) {
for await (const content of ideStreamRequest("llmStreamChat", {
const gen = ideStreamRequest("llmStreamChat", {
messages,
title: this.title,
completionOptions: options,
})) {
yield { role: "user", content };
});
let next = await gen.next();
while (!next.done) {
yield { role: "user", content: next.value };
next = await gen.next();
}
return;
return { prompt: next.value?.prompt, completion: next.value?.completion };
}
const { completionOptions, log, raw } =
@ -525,6 +539,7 @@ export abstract class BaseLLM implements ILLM {
}
this._logTokensGenerated(completionOptions.model, completion);
return { prompt, completion };
}
protected async *_streamComplete(

View file

@ -107,7 +107,8 @@ describe("/edit slash command", () => {
contextItemToRangeInFileWithContents(TEST_CONTEXT_ITEM2),
fullFile,
new FreeTrial({ model: "gpt-3.5-turbo" }),
"implement this function"
"implement this function",
1200
);
expect(filePrefix).toEqual(`${f1}`);

View file

@ -1,6 +1,6 @@
import { FileEdit, RangeInFile } from "core";
import { getConfigJsonPath } from "core/util/paths";
import { readFileSync } from "fs";
import { getConfigJsonPath, getDevDataFilePath } from "core/util/paths";
import { readFileSync, writeFileSync } from "fs";
import * as path from "path";
import * as vscode from "vscode";
import { debugPanelWebview, getSidebarContent } from "./debugPanel";
@ -564,6 +564,12 @@ class IdeProtocolClient {
input,
});
}
logDevData(tableName: string, data: any) {
const filepath: string = getDevDataFilePath(tableName);
const jsonLine = JSON.stringify(data);
writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" });
}
}
export default IdeProtocolClient;

View file

@ -1,9 +1,5 @@
import { DiffLine, FileEdit, ModelDescription } from "core";
import {
editConfigJson,
getConfigJsonPath,
getDevDataFilePath,
} from "core/util/paths";
import { editConfigJson, getConfigJsonPath } from "core/util/paths";
import { readFileSync, writeFileSync } from "fs";
import * as io from "socket.io-client";
import * as vscode from "vscode";
@ -433,9 +429,7 @@ export function getSidebarContent(
break;
}
case "logDevData": {
const filepath: string = getDevDataFilePath(data.tableName);
const jsonLine = JSON.stringify(data.data);
writeFileSync(filepath, `${jsonLine}\n`, { flag: "a" });
ideProtocolClient.logDevData(data.tableName, data.data);
break;
}
case "addModel": {
@ -483,24 +477,32 @@ export function getSidebarContent(
}
case "llmStreamComplete": {
const model = await llmFromTitle(data.message.title);
for await (const update of model.streamComplete(
const gen = model.streamComplete(
data.message.prompt,
data.message.completionOptions
)) {
respond({ content: update });
);
let next = await gen.next();
while (!next.done) {
respond({ content: next.value });
next = await gen.next();
}
respond({ done: true });
respond({ done: true, data: next.value });
break;
}
case "llmStreamChat": {
const model = await llmFromTitle(data.message.title);
for await (const update of model.streamChat(
const gen = model.streamChat(
data.message.messages,
data.message.completionOptions
)) {
respond({ content: update.content });
);
let next = await gen.next();
while (!next.done) {
respond({ content: next.value.content });
next = await gen.next();
}
respond({ done: true });
respond({ done: true, data: next.value });
break;
}
case "llmComplete": {
@ -557,7 +559,7 @@ export function getSidebarContent(
vscode.commands.executeCommand("workbench.action.toggleDevTools");
}
});
respond({ done: true });
respond({ done: true, data: {} });
}
});

View file

@ -1,7 +1,13 @@
import { Dispatch } from "@reduxjs/toolkit";
import { JSONContent } from "@tiptap/react";
import { ChatHistory, ChatHistoryItem, ChatMessage, SlashCommand } from "core";
import {
ChatHistory,
ChatHistoryItem,
ChatMessage,
LLMReturnValue,
SlashCommand,
} from "core";
import { ExtensionIde } from "core/ide";
import { ideStreamRequest } from "core/ide/messaging";
import { constructMessages } from "core/llm/constructMessages";
@ -48,11 +54,20 @@ function useChatHandler(dispatch: Dispatch) {
}, [active]);
async function _streamNormalInput(messages: ChatMessage[]) {
for await (const update of defaultModel.streamChat(messages)) {
const gen = defaultModel.streamChat(messages);
let next = await gen.next();
while (!next.done) {
if (!activeRef.current) {
break;
}
dispatch(streamUpdate(update.content));
dispatch(streamUpdate((next.value as ChatMessage).content));
next = await gen.next();
}
let returnVal = next.value as LLMReturnValue;
if (returnVal) {
dispatch(addLogs([[returnVal?.prompt, returnVal?.completion]]));
}
}
@ -184,30 +199,17 @@ function useChatHandler(dispatch: Dispatch) {
// Determine if the input is a slash command
let commandAndInput = getSlashCommandForInput(content);
const logs = [];
const writeLog = async (log: string) => {
logs.push(log);
};
defaultModel.writeLog = writeLog;
if (!commandAndInput) {
await _streamNormalInput(messages);
} else {
const [slashCommand, commandInput] = commandAndInput;
await _streamSlashCommand(messages, slashCommand, commandInput);
}
const pairedLogs = [];
for (let i = 0; i < logs.length; i += 2) {
pairedLogs.push([logs[i], logs[i + 1]]);
}
dispatch(addLogs(pairedLogs));
} catch (e) {
console.log("Continue: error streaming response: ", e);
errorPopup(`Error streaming response: ${e.message}`);
} finally {
dispatch(setInactive());
defaultModel.writeLog = undefined;
}
}