Skip to content

Commit

Permalink
refactor: split init agent to composable/neuri
Browse files Browse the repository at this point in the history
  • Loading branch information
luoling8192 committed Jan 18, 2025
1 parent 8f1196e commit 9d9ba91
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
36 changes: 3 additions & 33 deletions src/agents/action/llm.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
import type { Agent, Neuri } from 'neuri'
import type { Agent } from 'neuri'
import type { Mineflayer } from '../../libs/mineflayer'

import { useLogg } from '@guiiai/logg'
import { agent, neuri } from 'neuri'
import { agent } from 'neuri'

import { openaiConfig } from '../../composables/config'
import { actionsList } from './tools'

let neuriAgent: Neuri | undefined
const agents = new Set<Agent | Promise<Agent>>()

const logger = useLogg('action-llm').useGlobalConfig()

export async function initAgent(mineflayer: Mineflayer): Promise<Neuri> {
logger.log('Initializing agent')
let n = neuri()

agents.add(initActionAgent(mineflayer))

agents.forEach(agent => n = n.agent(agent))

neuriAgent = await n.build({
provider: {
apiKey: openaiConfig.apiKey,
baseURL: openaiConfig.baseUrl,
},
})

return neuriAgent
}

export function getAgent(): Neuri {
if (!neuriAgent) {
throw new Error('Agent not initialized')
}
return neuriAgent
}

export async function initActionAgent(mineflayer: Mineflayer): Promise<Agent> {
const logger = useLogg('action-llm').useGlobalConfig()
logger.log('Initializing action agent')
let actionAgent = agent('action')

Expand Down
4 changes: 2 additions & 2 deletions src/agents/planning/llm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Neuri } from 'neuri'
import type { Neuri, NeuriContext } from 'neuri'
import type { Action } from '../../libs/mineflayer/action'

import { useLogg } from '@guiiai/logg'
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function generatePlanWithLLM(
const content = await config.agent.handleStateless(messages, async (c) => {
logger.log('Generating plan...')

const handleCompletion = async (c: any): Promise<string> => {
const handleCompletion = async (c: NeuriContext): Promise<string> => {
const completion = await c.reroute('action', c.messages, {
model: config.model ?? 'openai/gpt-4o-mini',
})
Expand Down
38 changes: 38 additions & 0 deletions src/composables/neuri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Agent, Neuri } from 'neuri'
import type { Mineflayer } from '../libs/mineflayer'

import { useLogg } from '@guiiai/logg'
import { neuri } from 'neuri'

import { initActionAgent } from '../agents/action/llm'
import { openaiConfig } from './config'

let neuriAgent: Neuri | undefined
const agents = new Set<Agent | Promise<Agent>>()

const logger = useLogg('action-llm').useGlobalConfig()

export async function initNeuriAgent(mineflayer: Mineflayer): Promise<Neuri> {
logger.log('Initializing agent')
let n = neuri()

agents.add(initActionAgent(mineflayer))

agents.forEach(agent => n = n.agent(agent))

neuriAgent = await n.build({
provider: {
apiKey: openaiConfig.apiKey,
baseURL: openaiConfig.baseUrl,
},
})

return neuriAgent
}

export function getAgent(): Neuri {
if (!neuriAgent) {
throw new Error('Agent not initialized')
}
return neuriAgent
}
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { pathfinder as MineflayerPathfinder } from 'mineflayer-pathfinder'
import { plugin as MineflayerPVP } from 'mineflayer-pvp'
import { plugin as MineflayerTool } from 'mineflayer-tool'

import { initAgent } from './agents/action/llm'
import { initBot } from './composables/bot'
import { botConfig, initEnv } from './composables/config'
import { initNeuriAgent } from './composables/neuri'
import { wrapPlugin } from './libs/mineflayer'
import { LLMAgent } from './plugins/llm-agent'
import { initLogger } from './utils/logger'
Expand All @@ -36,7 +36,7 @@ async function main() {
const airiClient = new Client({ name: 'minecraft-bot', url: 'ws://localhost:6121/ws' })

// Dynamically load LLMAgent after the bot is initialized
const agent = await initAgent(bot)
const agent = await initNeuriAgent(bot)
await bot.loadPlugin(LLMAgent({ agent, airiClient }))

process.on('SIGINT', () => {
Expand Down

0 comments on commit 9d9ba91

Please sign in to comment.