@rinkuto/koishi-plugin-chatgpt
Version:
使用open ai的gpt-3.5-turbo模型
95 lines (94 loc) • 3.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Memory = exports.Memories = exports.MemoryManager = void 0;
class MemoryManager {
static getMemory(id, isPerson = true) {
if (isPerson === false) {
return this.groupMemory.get(id);
}
return this.userMemory.get(id);
}
static addMemory(userId, username, content, groupId) {
if (groupId === undefined) {
this.groupMemory.get(userId).addMemory(userId, username, content);
}
else {
this.userMemory.get(userId).addMemory(userId, username, content);
}
}
static createUserMemory(userId, username, groupId) {
if (groupId === undefined) {
if (this.userMemory.has(userId) === false) {
this.userMemory.set(userId, new Memories(this.config.maxMemoryLength, username, userId));
for (let key in this.config.sampleDialog) {
this.userMemory.get(userId).addMemory(userId, username, { user: key, bot: this.config.sampleDialog[key] });
}
}
}
else {
if (this.groupMemory.has(groupId) === false) {
this.groupMemory.set(groupId, new Memories(this.config.maxMemoryLength));
for (let key in this.config.sampleDialog) {
this.groupMemory.get(groupId).addMemory(userId, username, { user: key, bot: this.config.sampleDialog[key] });
}
}
}
}
static setConfig(config) {
this.config = config;
}
static clearMemory(id, isPerson = true) {
if (isPerson === false) {
this.groupMemory.delete(id);
}
else {
this.userMemory.delete(id);
}
}
}
exports.MemoryManager = MemoryManager;
MemoryManager.userMemory = new Map();
MemoryManager.groupMemory = new Map();
class Memories {
addMemory(userId, username, content) {
if (this.memories.length >= this.maxMemoryLength) {
this.memories.shift();
}
this.memories.push(new Memory(userId, username, content));
}
constructor(maxMemoryLength, username, userId) {
this.memories = [];
this.username = '';
this.userId = '';
this.isPerson = false;
this.maxMemoryLength = maxMemoryLength;
if (username !== undefined && userId !== undefined) {
this.username = username;
this.userId = userId;
this.isPerson = true;
}
}
getMemory() {
return this.memories.map((memory) => memory.getMemory());
}
}
exports.Memories = Memories;
class Memory {
constructor(userId, username, content) {
this.user = {
name: username,
userId: userId,
content: content.user,
};
this.bot = {
content: content.bot,
};
}
getMemory() {
return {
user: this.user,
bot: this.bot,
};
}
}
exports.Memory = Memory;