Skip to content

Commit

Permalink
refactor: move world to skills & move manager to folder
Browse files Browse the repository at this point in the history
  • Loading branch information
luoling8192 committed Jan 18, 2025
1 parent c2b7b56 commit 8f1196e
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/agents/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { Mineflayer } from '../../libs/mineflayer'
import type { Action } from '../../libs/mineflayer/action'
import type { ActionAgent, AgentConfig } from '../../libs/mineflayer/base-agent'

import { ActionManager } from '../../composables/action'
import { useBot } from '../../composables/bot'
import { AbstractAgent } from '../../libs/mineflayer/base-agent'
import { ActionManager } from '../../manager/action'
import { actionsList } from './tools'

interface ActionState {
Expand Down
2 changes: 1 addition & 1 deletion src/agents/action/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { Action } from '../../libs/mineflayer'
import { useLogg } from '@guiiai/logg'
import { z } from 'zod'

import * as world from '../../composables/world'
import * as skills from '../../skills'
import { collectBlock } from '../../skills/actions/collect-block'
import { discard, equip, putInChest, takeFromChest, viewChest } from '../../skills/actions/inventory'
import { activateNearestBlock, placeBlock } from '../../skills/actions/world-interactions'
import * as world from '../../skills/world'

// Utils
const pad = (str: string): string => `\n${str}\n`
Expand Down
2 changes: 1 addition & 1 deletion src/agents/chat/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ChatHistory } from './types'
import { useLogg } from '@guiiai/logg'
import { system, user } from 'neuri/openai'

import { toRetriable } from '../../utils/reliability'
import { toRetriable } from '../../utils/helper'
import { genChatAgentPrompt } from '../prompt/chat'

const logger = useLogg('chat-llm').useGlobalConfig()
Expand Down
2 changes: 1 addition & 1 deletion src/agents/planning/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Action } from '../../libs/mineflayer/action'
import { useLogg } from '@guiiai/logg'
import { system, user } from 'neuri/openai'

import { toRetriable } from '../../utils/reliability'
import { toRetriable } from '../../utils/helper'
import { generatePlanningAgentSystemPrompt, generatePlanningAgentUserPrompt } from '../prompt/planning'

const logger = useLogg('planning-llm').useGlobalConfig()
Expand Down
47 changes: 23 additions & 24 deletions src/agents/prompt/llm-agent.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import type { Mineflayer } from '../../libs/mineflayer'
import { listInventory } from '../../skills/actions/inventory'

export function generateSystemBasicPrompt(botName: string): string {
// ${ctx.prompt.selfPrompt}
return `You are a playful Minecraft bot named ${botName} that can converse with players, see, move,
mine, build, and interact with the world by using commands.`
}

export function generateActionAgentPrompt(mineflayer: Mineflayer): string {
// ${ctx.prompt.selfPrompt}

return `${generateSystemBasicPrompt(mineflayer.username)}
Act human-like as if you were a typical Minecraft player, rather than an AI. Be very brief in
Expand All @@ -24,28 +23,28 @@ Do not use any emojis. Just call the function given you if needed.
}

export async function generateStatusPrompt(mineflayer: Mineflayer): Promise<string> {
// Get inventory items
const inventory = await listInventory(mineflayer)
if (inventory.length === 0) {
return `I will give you the following information:
${mineflayer.status.toOneLiner()}

Inventory:
[Empty]
Item in hand:
[Empty]
`
}
const inventoryStr = inventory.map(item => `${item.name} x ${item.count}`).join(', ')
const itemInHand = `${inventory[0].name} x ${inventory[0].count}` // TODO: mock

return `I will give you the following information:
${mineflayer.status.toOneLiner()}
Inventory:
${inventoryStr}
Item in hand:
${itemInHand}
`
// Format inventory string
const inventoryStr = inventory.length === 0
? '[Empty]'
: inventory.map(item => `${item.name} x ${item.count}`).join(', ')

// Get currently held item
const itemInHand = inventory.length === 0
? '[Empty]'
: `${inventory[0].name} x ${inventory[0].count}` // TODO: mock

// Build status message
return [
'I will give you the following information:',
mineflayer.status.toOneLiner(),
'',
'Inventory:',
inventoryStr,
'',
'Item in hand:',
itemInHand,
].join('\n')
}
2 changes: 1 addition & 1 deletion src/libs/llm/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { LLMConfig, LLMResponse } from './types'

import { useLogg } from '@guiiai/logg'

import { toRetriable } from '../../utils/reliability'
import { toRetriable } from '../../utils/helper'

export abstract class BaseLLMHandler {
protected logger = useLogg('llm-handler').useGlobalConfig()
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plugins/llm-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { assistant, system, user } from 'neuri/openai'
import { generateActionAgentPrompt, generateStatusPrompt } from '../agents/prompt/llm-agent.plugin'
import { createAppContainer } from '../container'
import { ChatMessageHandler } from '../libs/mineflayer/message'
import { toRetriable } from '../utils/reliability'
import { toRetriable } from '../utils/helper'

interface MineflayerWithAgents extends Mineflayer {
planning: PlanningAgent
Expand Down
2 changes: 1 addition & 1 deletion src/skills/actions/collect-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { Mineflayer } from '../../libs/mineflayer'
import { useLogg } from '@guiiai/logg'
import pathfinder from 'mineflayer-pathfinder'

import { getNearestBlocks } from '../../composables/world'
import { breakBlockAt } from '../blocks'
import { getNearestBlocks } from '../world'
import { ensurePickaxe } from './ensure'
import { pickupNearbyItems } from './world-interactions'

Expand Down
2 changes: 1 addition & 1 deletion src/skills/actions/gather-wood.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { Mineflayer } from '../../libs/mineflayer'

import { useLogg } from '@guiiai/logg'

import { getNearestBlocks } from '../../composables/world'
import { sleep } from '../../utils/helper'
import { breakBlockAt } from '../blocks'
import { goToPosition, moveAway } from '../movement'
import { getNearestBlocks } from '../world'
import { pickupNearbyItems } from './world-interactions'

const logger = useLogg('Action:GatherWood').useGlobalConfig()
Expand Down
2 changes: 1 addition & 1 deletion src/skills/actions/inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { Mineflayer } from '../../libs/mineflayer'

import { useLogg } from '@guiiai/logg'

import { getNearestBlock } from '../../composables/world'
import { goToPlayer, goToPosition } from '../movement'
import { getNearestBlock } from '../world'

const logger = useLogg('Action:Inventory').useGlobalConfig()

Expand Down
2 changes: 1 addition & 1 deletion src/skills/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { BlockFace } from './base'
import pathfinderModel, { type SafeBlock } from 'mineflayer-pathfinder'
import { Vec3 } from 'vec3'

import { getNearestBlock, getNearestBlocks, getPosition, shouldPlaceTorch } from '../composables/world'
import { getBlockId, makeItem } from '../utils/mcdata'
import { log } from './base'
import { goToPosition } from './movement'
import { getNearestBlock, getNearestBlocks, getPosition, shouldPlaceTorch } from './world'

const { goals, Movements } = pathfinderModel

Expand Down
2 changes: 1 addition & 1 deletion src/skills/combat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { Mineflayer } from '../libs/mineflayer'

import pathfinderModel from 'mineflayer-pathfinder'

import { getNearbyEntities, getNearestEntityWhere } from '../composables/world'
import { sleep } from '../utils/helper'
import { isHostile } from '../utils/mcdata'
import { log } from './base'
import { getNearbyEntities, getNearestEntityWhere } from './world'

const { goals } = pathfinderModel

Expand Down
2 changes: 1 addition & 1 deletion src/skills/crafting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type { Mineflayer } from '../libs/mineflayer'

import { useLogg } from '@guiiai/logg'

import { getInventoryCounts, getNearestBlock, getNearestFreeSpace } from '../composables/world'
import { getItemId, getItemName } from '../utils/mcdata'
import { ensureCraftingTable } from './actions/ensure'
import { collectBlock, placeBlock } from './blocks'
import { goToNearestBlock, goToPosition, moveAway } from './movement'
import { getInventoryCounts, getNearestBlock, getNearestFreeSpace } from './world'

const logger = useLogg('Skill:Crafting').useGlobalConfig()

Expand Down
2 changes: 1 addition & 1 deletion src/skills/inventory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Mineflayer } from '../libs/mineflayer'

import { getNearestBlock } from '../composables/world'
import { log } from './base'
import { goToPlayer, goToPosition } from './movement'
import { getNearestBlock } from './world'

export async function equip(mineflayer: Mineflayer, itemName: string): Promise<boolean> {
const item = mineflayer.bot.inventory.slots.find(slot => slot && slot.name === itemName)
Expand Down
2 changes: 1 addition & 1 deletion src/skills/movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { randomInt } from 'es-toolkit'
import pathfinder from 'mineflayer-pathfinder'
import { Vec3 } from 'vec3'

import { getNearestBlock, getNearestEntityWhere } from '../composables/world'
import { sleep } from '../utils/helper'
import { log } from './base'
import { getNearestBlock, getNearestEntityWhere } from './world'

const logger = useLogg('Skill:Movement').useGlobalConfig()
const { goals, Movements } = pathfinder
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

/**
* Returns a retirable anonymous function with configured retryLimit and delayInterval
*
* @param retryLimit Number of retry attempts
* @param delayInterval Delay between retries in milliseconds
* @param func Function to be called
* @returns A wrapped function with the same signature as func
*/
export function toRetriable<A, R>(
retryLimit: number,
delayInterval: number,
func: (...args: A[]) => Promise<R>,
hooks?: {
onError?: (err: unknown) => void
},
): (...args: A[]) => Promise<R> {
let retryCount = 0
return async function (args: A): Promise<R> {
try {
return await func(args)
}
catch (err) {
if (hooks?.onError) {
hooks.onError(err)
}

if (retryCount < retryLimit) {
retryCount++
await sleep(delayInterval)
return await toRetriable(retryLimit - retryCount, delayInterval, func)(args)
}
else {
throw err
}
}
}
}
39 changes: 0 additions & 39 deletions src/utils/reliability.ts

This file was deleted.

0 comments on commit 8f1196e

Please sign in to comment.