Repository navigation
Expand file tree
/
Copy pathChatMemory.ts
More file actions
160 lines (140 loc) · 4.6 KB
/
Copy pathChatMemory.ts
File metadata and controls
160 lines (140 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { ExtensionContext } from "vscode";
export class ChatMessage {
prefix?: string;
content: string;
constructor(content: string) {
this.content = content;
}
toString(): string {
return `${this.prefix}${this.content}`;
}
}
export class HumanMessage extends ChatMessage {
prefix = "## human:";
}
export class AIMessage extends ChatMessage {
prefix = "## assistant:";
append(text: string) {
this.content += text;
this.content = this.content.replace("|end|", "");
this.content = this.content.replace("|<end>|", "");
this.content = this.content.replace("<|endoftext|>", "");
}
}
export class ChatItem {
humanMessage: HumanMessage;
aiMessage: AIMessage;
aiMsgId: string = "0";
constructor(humanMessage: HumanMessage, aiMessage: AIMessage) {
this.humanMessage = humanMessage;
this.aiMessage = aiMessage;
}
toString(): string {
return `${this.humanMessage.toString()}|<end>|${this.aiMessage.toString()}`;
}
}
export class SessionItem {
time = new Date();
id = this.time.toISOString();
title = "";
chatList = new Array<ChatItem>();
addChatItem(chatItem: ChatItem) {
this.chatList.push(chatItem);
this.title = this.chatList[0].humanMessage.content;
}
addChatList(list: ChatItem[]) {
for (const chat of list) {
const hmMsg = new HumanMessage(chat.humanMessage.content);
const aiMsg = new AIMessage(chat.aiMessage.content);
const item = new ChatItem(hmMsg, aiMsg);
if (chat.aiMsgId) {
item.aiMsgId = chat.aiMsgId;
}
this.addChatItem(item);
}
}
getSlicePrompt(start:number, end:number) {
let history = "";
for (let i = start; i <= end; i++) {
const chatItem = this.chatList[i];
if (history.length > 0) {
history += "\n";
}
history += chatItem.toString();
if (i < end) {
history += "|<end>|";
}
}
return history;
}
}
export class SessionStore {
private _storeKey = "sessionHistory";
constructor(private readonly _extensionContext: ExtensionContext) {
const keys = this._extensionContext.globalState.keys();
for (const key of keys) {
const value = this._extensionContext.globalState.get(key);
console.log(key, value);
}
// this._extensionContext.globalState.update(this._storeKey, undefined);
}
update(updateItem: SessionItem) {
// console.log("updateItem:", updateItem);
if (updateItem.chatList.length === 0) {
return;
}
const sessionHistory = this.getSessionHistory();
let exist = false;
for (let i = 0; i < sessionHistory.length; i++) {
let item = sessionHistory[i];
if (item.id === updateItem.id) {
sessionHistory[i] = updateItem;
exist = true;
break;
}
}
if (!exist) {
sessionHistory.push(updateItem);
}
this._extensionContext.globalState.update(this._storeKey, sessionHistory);
}
delete(deleteItem: SessionItem) {
const sessionHistory = this.getSessionHistory();
const newArray = new Array<SessionItem>();
for (const item of sessionHistory) {
if (item.id !== deleteItem.id) {
newArray.push(item);
}
}
this._extensionContext.globalState.update(this._storeKey, newArray);
}
getSessionItemById(itemId: string): SessionItem {
const sessionHistory = this.getSessionHistory();
for (const item of sessionHistory) {
if (item.id === itemId) {
return item;
}
}
return new SessionItem();
}
getSessionHistory(): SessionItem[] {
let oldArray = this._extensionContext.globalState.get<Array<SessionItem>>(this._storeKey);
if (!oldArray) {
oldArray = new Array<SessionItem>();
}
let newArray = new Array<SessionItem>();
for (var item of oldArray) {
if (item.chatList.length === 0) {
continue;
}
let newObj = new SessionItem();
newObj.id = item.id;
newObj.time = new Date(item.time);
newObj.addChatList(item.chatList);
newArray.push(newObj);
}
return newArray.sort((a: SessionItem, b: SessionItem): number => {
return a.time < b.time ? 1 : -1;
});
}
}