Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: planning agent v2 #2

Merged
merged 25 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ words:
- aichat
- airi
- antfu
- awilix
- bumpp
- collectblock
- convo
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@guiiai/logg": "^1.0.7",
"@proj-airi/server-sdk": "^0.1.4",
"@typeschema/zod": "^0.14.0",
"awilix": "^12.0.4",
"dotenv": "^16.4.7",
"es-toolkit": "^1.31.0",
"eventemitter3": "^5.0.1",
Expand Down Expand Up @@ -51,7 +52,7 @@
"vitest": "^3.0.2"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged && pnpm typecheck"
"pre-commit": "pnpm lint-staged"
},
"lint-staged": {
"*": "eslint --fix"
Expand Down
43 changes: 43 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions src/agents/action/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import type { Mineflayer } from '../../libs/mineflayer'
import type { Action } from '../../libs/mineflayer/action'
import type { ActionAgent, AgentConfig } from '../../libs/mineflayer/base-agent'
import type { PlanStep } from '../planning/llm-handler'

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

interface ActionState {
executing: boolean
label: string
startTime: number
}

/**
* ActionAgentImpl implements the ActionAgent interface to handle action execution
* Manages action lifecycle, state tracking and error handling
*/
export class ActionAgentImpl extends AbstractAgent implements ActionAgent {
public readonly type = 'action' as const
private actions: Map<string, Action>
private mineflayer: Mineflayer
private currentActionState: ActionState

constructor(config: AgentConfig) {
super(config)
this.actions = new Map()
this.mineflayer = useBot().bot
this.currentActionState = {
executing: false,
label: '',
startTime: 0,
}
}

protected async initializeAgent(): Promise<void> {
this.logger.log('Initializing action agent')
actionsList.forEach(action => this.actions.set(action.name, action))

// Set up event listeners
this.on('message', async ({ sender, message }) => {
await this.handleAgentMessage(sender, message)
})
}

protected async destroyAgent(): Promise<void> {
this.actions.clear()
this.removeAllListeners()
}

public async performAction(step: PlanStep): Promise<string> {
if (!this.initialized) {
throw new Error('Action agent not initialized')
}

const action = this.actions.get(step.tool)
if (!action) {
throw new Error(`Unknown action: ${step.tool}`)
}

this.logger.withFields({
action: step.tool,
description: step.description,
params: step.params,
}).log('Performing action')

// Update action state
this.updateActionState(true, step.description)

try {
// Execute action with provided parameters
const result = await action.perform(this.mineflayer)(...Object.values(step.params))
return this.formatActionOutput({
message: result,
timedout: false,
interrupted: false,
})
}
catch (error) {
this.logger.withError(error).error('Action failed')
throw error
}
finally {
this.updateActionState(false)
}
}

public getAvailableActions(): Action[] {
return Array.from(this.actions.values())
}

private async handleAgentMessage(sender: string, message: string): Promise<void> {
if (sender === 'system' && message.includes('interrupt') && this.currentActionState.executing) {
// Handle interruption
this.logger.log('Received interrupt request')
// Additional interrupt handling logic here
}
}

private updateActionState(executing: boolean, label = ''): void {
this.currentActionState = {
executing,
label,
startTime: executing ? Date.now() : this.currentActionState.startTime,
}
}

private formatActionOutput(result: { message: string | null, timedout: boolean, interrupted: boolean }): string {
if (result.timedout) {
return 'Action timed out'
}
if (result.interrupted) {
return 'Action was interrupted'
}
return result.message || 'Action completed successfully'
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { messages, system, user } from 'neuri/openai'
import { beforeAll, describe, expect, it } from 'vitest'

import { initBot, useBot } from '../composables/bot'
import { botConfig, initEnv } from '../composables/config'
import { genSystemBasicPrompt } from '../prompts/agent'
import { initLogger } from '../utils/logger'
import { initAgent } from './openai'
import { initBot, useBot } from '../../composables/bot'
import { botConfig, initEnv } from '../../composables/config'
import { createNeuriAgent } from '../../composables/neuri'
import { initLogger } from '../../utils/logger'
import { generateSystemBasicPrompt } from '../prompt/llm-agent.plugin'

describe('openAI agent', { timeout: 0 }, () => {
beforeAll(() => {
Expand All @@ -16,13 +16,13 @@ describe('openAI agent', { timeout: 0 }, () => {

it('should initialize the agent', async () => {
const { bot } = useBot()
const agent = await initAgent(bot)
const agent = await createNeuriAgent(bot)

await new Promise<void>((resolve) => {
bot.bot.once('spawn', async () => {
const text = await agent.handle(
messages(
system(genSystemBasicPrompt('airi')),
system(generateSystemBasicPrompt('airi')),
user('Hello, who are you?'),
),
async (c) => {
Expand Down
84 changes: 84 additions & 0 deletions src/agents/action/llm-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type { Agent } from 'neuri'
import type { Message } from 'neuri/openai'
import type { Mineflayer } from '../../libs/mineflayer'
import type { PlanStep } from '../planning/llm-handler'

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

import { BaseLLMHandler } from '../../libs/llm/base'
import { actionsList } from './tools'

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

Object.values(actionsList).forEach((action) => {
actionAgent = actionAgent.tool(
action.name,
action.schema,
async ({ parameters }) => {
logger.withFields({ name: action.name, parameters }).log('Calling action')
mineflayer.memory.actions.push(action)
const fn = action.perform(mineflayer)
return await fn(...Object.values(parameters))
},
{ description: action.description },
)
})

return actionAgent.build()
}

export class ActionLLMHandler extends BaseLLMHandler {
public async executeStep(step: PlanStep): Promise<string> {
const systemPrompt = this.generateActionSystemPrompt()
const userPrompt = this.generateActionUserPrompt(step)
const messages = [system(systemPrompt), user(userPrompt)]

const result = await this.handleAction(messages)
return result
}

private generateActionSystemPrompt(): string {
return `You are a Minecraft bot action executor. Your task is to execute a given step using available tools.
You have access to various tools that can help you accomplish tasks.
When using a tool:
1. Choose the most appropriate tool for the task
2. Determine the correct parameters based on the context
3. Handle any errors or unexpected situations

Remember to:
- Be precise with tool parameters
- Consider the current state of the bot
- Handle failures gracefully`
}

private generateActionUserPrompt(step: PlanStep): string {
return `Execute this step: ${step.description}

Suggested tool: ${step.tool}
Params: ${JSON.stringify(step.params)}

Please use the appropriate tool with the correct parameters to accomplish this step.
If the suggested tool is not appropriate, you may choose a different one.`
}

public async handleAction(messages: Message[]): Promise<string> {
const result = await this.config.agent.handleStateless(messages, async (context) => {
this.logger.log('Processing action...')
const retryHandler = this.createRetryHandler(
async ctx => (await this.handleCompletion(ctx, 'action', ctx.messages)).content,
)
return await retryHandler(context)
})

if (!result) {
throw new Error('Failed to process action')
}

return result
}
}
Loading
Loading