From 5d70b7ae53a78f3664c23312ce6cc5970a6267e0 Mon Sep 17 00:00:00 2001 From: RainbowBird Date: Fri, 17 Jan 2025 02:03:08 +0800 Subject: [PATCH] chore: short memory --- .../minecraft/src/libs/mineflayer/core.ts | 17 +-------------- .../minecraft/src/mineflayer/llm-agent.ts | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/services/minecraft/src/libs/mineflayer/core.ts b/services/minecraft/src/libs/mineflayer/core.ts index 97c2b3852..446ac4782 100644 --- a/services/minecraft/src/libs/mineflayer/core.ts +++ b/services/minecraft/src/libs/mineflayer/core.ts @@ -116,11 +116,6 @@ export class Mineflayer extends EventEmitter { mineflayer.bot.on('end', (reason) => { mineflayer.logger.withFields({ reason }).log('Bot ended') - - // Try to reconnect after 5 seconds - setTimeout(async () => { - await mineflayer.reconnect() - }, 5000) }) mineflayer.bot.on('error', (err: Error) => { @@ -161,16 +156,6 @@ export class Mineflayer extends EventEmitter { return mineflayer } - private async reconnect() { - try { - await this.bot.connect(this.options.botConfig) - this.logger.log('Reconnected successfully') - } - catch (err) { - this.logger.errorWithError('Failed to reconnect:', err) - } - } - public async loadPlugin(plugin: MineflayerPlugin) { if (plugin.created) await plugin.created(this) @@ -199,7 +184,7 @@ export class Mineflayer extends EventEmitter { } this.components.cleanup() this.bot.removeListener('chat', this.handleCommand()) - this.bot.end() + this.bot.quit() this.removeAllListeners() } diff --git a/services/minecraft/src/mineflayer/llm-agent.ts b/services/minecraft/src/mineflayer/llm-agent.ts index 5fd4c5729..ac41dc3f7 100644 --- a/services/minecraft/src/mineflayer/llm-agent.ts +++ b/services/minecraft/src/mineflayer/llm-agent.ts @@ -2,7 +2,7 @@ import type { Client } from '@proj-airi/server-sdk' import type { Neuri, NeuriContext } from 'neuri' import type { MineflayerPlugin } from '../libs/mineflayer/plugin' import { useLogg } from '@guiiai/logg' -import { system, user } from 'neuri/openai' +import { assistant, type ChatCompletion, system, user } from 'neuri/openai' import { formBotChat } from '../libs/mineflayer/message' import { genActionAgentPrompt, genStatusPrompt } from '../prompts/agent' @@ -21,24 +21,26 @@ export function LLMAgent(options: { agent: Neuri, airiClient: Client }): Minefla const onChat = formBotChat(bot.username, async (username, message) => { logger.withFields({ username, message }).log('Chat message received') - const statusPrompt = await genStatusPrompt(bot) - bot.memory.chatHistory.push(system(statusPrompt)) + // long memory bot.memory.chatHistory.push(user(`${username}: ${message}`)) - const content = await agent.handleStateless([...bot.memory.chatHistory], async (c: NeuriContext) => { + // short memory + const statusPrompt = await genStatusPrompt(bot) + const content = await agent.handleStateless([...bot.memory.chatHistory, system(statusPrompt)], async (c: NeuriContext) => { logger.log('thinking...') const handleCompletion = async (c: NeuriContext): Promise => { - const completion = await c.reroute('action', c.messages, { model: 'openai/gpt-4o-mini' }) || { error: { message: 'Unknown error' } } + const completion = await c.reroute('action', c.messages, { model: 'openai/gpt-4o-mini' }) as ChatCompletion | { error: { message: string } } & ChatCompletion if (!completion || 'error' in completion) { - logger.withFields(c).error('Completion') + logger.withFields({ completion }).error('Completion') logger.withFields({ messages: c.messages }).log('messages') throw new Error(completion?.error?.message ?? 'Unknown error') } const content = await completion?.firstContent() logger.withFields({ usage: completion.usage, content }).log('output') - bot.memory.chatHistory.push(...c.messages) + + bot.memory.chatHistory.push(assistant(content)) return content } @@ -74,14 +76,15 @@ export function LLMAgent(options: { agent: Neuri, airiClient: Client }): Minefla const handleCompletion = async (c: NeuriContext): Promise => { const completion = await c.reroute('action', c.messages, { model: 'openai/gpt-4o-mini' }) || { error: { message: 'Unknown error' } } if (!completion || 'error' in completion) { - logger.withFields(c).error('Completion') + logger.withFields({ completion }).error('Completion') logger.withFields({ messages: c.messages }).log('messages') throw new Error(completion?.error?.message ?? 'Unknown error') } const content = await completion?.firstContent() logger.withFields({ usage: completion.usage, content }).log('output') - bot.memory.chatHistory.push(...c.messages) + + bot.memory.chatHistory.push(assistant(content)) return content }