Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(chat): next step
  • Loading branch information
gespispace committed Apr 30, 2024
commit 005c3e0d514ec8ba02a6418cf7b708d06ffbcf5c
43 changes: 17 additions & 26 deletions src/common/panel/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as vscode from "vscode";
import { getUri } from "../utils/getUri";
import { getNonce } from "../utils/getNonce";
import { chat } from "../chat";
import { ChatMessage } from "../prompt/promptChat";
import { Chat, ChatMessage } from "../prompt/promptChat";
import { state } from "../utils/state";

export type MessageType =
Expand All @@ -20,32 +20,32 @@ export type MessageType =
data: any;
};

type MessageFromWebview =
type MessageToExtention =
| {
type: "send-message";
id: string;
data: ChatMessage[];
}
| {
type: "abort-generate";
id: string;
}
| {
type: "get-chat-history";
id: string;
type: "get-chat";
chatId: string;
}
| {
type: "save-chat-history";
id: string;
type: "save-chat";
chatId: string;
data: ChatMessage[];
data: Chat;
}
| {
type: "get-chats";
id: string;
};

type MessageFromWebview = MessageToExtention & {
id: string;
};

export class ChatPanel implements vscode.WebviewViewProvider {
private disposables: Disposable[] = [];
private webview: Webview | undefined;
Expand Down Expand Up @@ -137,14 +137,14 @@ export class ChatPanel implements vscode.WebviewViewProvider {
chatMessage: message.data,
});
break;
case "get-chat-history":
await this.handleGetChatHistory({
case "get-chat":
await this.handleGetChat({
id: message.id,
chatId: message.chatId,
});
break;
case "save-chat-history":
await this.handleSaveChatHistory({
case "save-chat":
await this.handleSaveChat({
id: message.id,
chatId: message.chatId,
history: message.data,
Expand Down Expand Up @@ -195,17 +195,8 @@ export class ChatPanel implements vscode.WebviewViewProvider {
sendResponse("", true);
}

private async handleGetChatHistory({
chatId,
id,
}: {
chatId: string;
id: string;
}) {
const sendResponse = (
messageToResponse: ChatMessage[] | null,
done: boolean
) => {
private async handleGetChat({ chatId, id }: { chatId: string; id: string }) {
const sendResponse = (messageToResponse: Chat | null, done: boolean) => {
this.postMessage({
type: "e2w-response",
id: id,
Expand All @@ -222,13 +213,13 @@ export class ChatPanel implements vscode.WebviewViewProvider {
}
}

private async handleSaveChatHistory({
private async handleSaveChat({
chatId,
history,
id,
}: {
chatId: string;
history: ChatMessage[];
history: Chat;
id: string;
}) {
await state.global.update(`chat-${chatId}`, history);
Expand Down
8 changes: 8 additions & 0 deletions src/common/prompt/promptChat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
export type ChatMessage = {
role: string;
content: string;
// chatMessageId: string;
};

export type Chat = {
messages: ChatMessage[];
chatId: string;
date: number;
title: string;
};

const promptBaseDefault = `You are an AI programming assistant, utilizing the DeepSeek Coder model, developed by DeepSeek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer.
Expand Down
8 changes: 4 additions & 4 deletions src/common/utils/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode";
import type { Spec } from "../download";
import { ChatMessage } from "../prompt/promptChat";
import { Chat } from "../prompt/promptChat";

const StateValues = {
inlineSuggestModeAuto: true,
Expand All @@ -9,7 +9,7 @@ const StateValues = {
type StateValuesType = {
inlineSuggestModeAuto: boolean;
serverSpec: Spec | null;
[key: `chat-${string}`]: ChatMessage[] | undefined;
[key: `chat-${string}`]: Chat | undefined;
};

class State {
Expand All @@ -34,14 +34,14 @@ class State {
await this.state?.update(key, value);
}

public getChats(): ChatMessage[][] {
public getChats(): Chat[] {
const allKeys = (this.state?.keys() ||
[]) as unknown as (keyof StateValuesType)[];

return allKeys
.filter((key) => key.startsWith("chat-"))
.map((key) => {
return this.get(key as `chat-${string}`) as ChatMessage[];
return this.get(key as `chat-${string}`) as Chat;
});
}
}
Expand Down
21 changes: 17 additions & 4 deletions webviews/src/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export type ChatMessage = {
chatMessageId: string;
};

export type Chat = {
messages: ChatMessage[];
chatId: string;
date: number;
title: string;
};

export const useChat = (chatId?: string) => {
const { chatMessages, setChatMessages } = useChatMessages();

Expand All @@ -21,9 +28,9 @@ export const useChat = (chatId?: string) => {
useEffect(() => {
const getChatHistory = async () => {
if (chatId) {
const history = await vscode.getChatHistory(chatId);
if (history) {
setChatMessages(history);
const chat = await vscode.getChat(chatId);
if (chat) {
setChatMessages(chat.messages);
}
}
};
Expand Down Expand Up @@ -58,7 +65,13 @@ export const useChat = (chatId?: string) => {
}
setChatMessages((chatHistoryLocal) => {
(async () => {
await vscode.saveChatHistory(chatIdLocal, chatHistoryLocal);
const chat = {
chatId: chatIdLocal,
date: Date.now(),
messages: chatHistoryLocal,
title: "Chat with AI",
} satisfies Chat;
await vscode.saveChatHistory(chatIdLocal, chat);
})();
return chatHistoryLocal;
});
Expand Down
20 changes: 10 additions & 10 deletions webviews/src/utilities/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WebviewApi } from "vscode-webview";
import { randomId } from "./messageId";
import { ChatMessage } from "../hooks/useChat";
import { Chat, ChatMessage } from "../hooks/useChat";
import { Transform } from "./transformCallback2AsyncGenerator";

export type MessageType =
Expand All @@ -27,13 +27,13 @@ type MessageToExtention =
id: string;
}
| {
type: "get-chat-history";
type: "get-chat";
chatId: string;
}
| {
type: "save-chat-history";
type: "save-chat";
chatId: string;
data: ChatMessage[];
data: Chat;
}
| {
type: "get-chats";
Expand Down Expand Up @@ -119,11 +119,11 @@ class VSCodeAPIWrapper {
return transform.stream();
}

public getChatHistory(chatId: string) {
return new Promise<ChatMessage[] | null>((resolve) => {
public getChat(chatId: string) {
return new Promise<Chat | null>((resolve) => {
this.postMessageCallback(
{
type: "get-chat-history",
type: "get-chat",
chatId: chatId,
},
(message) => {
Expand All @@ -133,11 +133,11 @@ class VSCodeAPIWrapper {
});
}

public saveChatHistory(chatId: string, history: ChatMessage[]) {
public saveChatHistory(chatId: string, history: Chat) {
return new Promise<void>((resolve) => {
this.postMessageCallback(
{
type: "save-chat-history",
type: "save-chat",
chatId: chatId,
data: history,
},
Expand All @@ -149,7 +149,7 @@ class VSCodeAPIWrapper {
}

public getChats() {
return new Promise<ChatMessage[][]>((resolve) => {
return new Promise<Chat[]>((resolve) => {
this.postMessageCallback({
type: "get-chats",
});
Expand Down