fix: unit test

This commit is contained in:
RainbowBird
2025-01-06 22:10:46 +08:00
parent 8f327c3436
commit 578aad50e5
6 changed files with 53 additions and 53 deletions
+27 -5
View File
@@ -1,10 +1,32 @@
import { describe, expect, it } from 'vitest'
import { useLogg } from '@guiiai/logg'
import { messages, system, user } from 'neuri/openai'
import { initQueryAgent } from './openai'
import { beforeAll, describe, expect, it } from 'vitest'
import { initEnv } from '../composables/config'
import { basicSystemPrompt } from '../prompts/agent'
import { initLogger } from '../utils/logger'
import { initAgent } from './openai'
describe('openAI agent', () => {
it('should initialize the agent', () => {
const agent = initQueryAgent()
expect(agent).toBeDefined()
beforeAll(() => {
initLogger()
initEnv()
})
it('should initialize the agent', async () => {
const agent = await initAgent()
const text = await agent.handle(
messages(
system(basicSystemPrompt('airi')),
user('Hello, who are you?'),
),
async (c) => {
const completion = await c.reroute('query', c.messages, { model: 'gpt-4o-mini' })
return await completion?.firstContent()
},
)
expect(text?.toLowerCase()).toContain('airi')
})
})
+1 -34
View File
@@ -2,19 +2,12 @@ import type { Agent, Neuri } from 'neuri'
import { useLogg } from '@guiiai/logg'
import { agent, neuri } from 'neuri'
import { openaiConfig } from '../composables/config'
import { queryList } from './queries'
import { queryList } from './query'
// Types
interface AgentBotContext {
readonly agents: Set<Agent | Promise<Agent>>
}
// State management
const agents = new Set<Agent | Promise<Agent>>()
const logger = useLogg('openai').useGlobalConfig()
// Agent initialization
export async function initAgent(): Promise<Neuri> {
logger.log('Initializing agent')
let n = neuri()
@@ -46,29 +39,3 @@ export async function initQueryAgent(): Promise<Agent> {
return queryAgent.build()
}
// export async function initAgent(ctx: BotContext) {
// logger.log('Initializing agent')
// initQueryAgent(ctx)
// }
// export function initQueryAgent(ctx: BotContext) {
// logger.log('Initializing query agent')
// const agentBotContext = createQueryAgentBotContext(ctx.bot)
// const tools = []
// for (const query of queryList) {
// tools.push(
// tool({
// name: query.name,
// description: query.description,
// execute: query.perform(agentBotContext),
// parameters: query.schema as never,
// }),
// )
// }
// return tools
// }
@@ -72,7 +72,7 @@ function formatWearingItem(slot: string, item: string | undefined): string {
// Query implementations
function createStatsQuery(): Query {
return {
name: '!stats',
name: 'stats',
description: 'Get your bot\'s location, health, hunger, and time of day.',
schema: z.object({}),
perform: (agent: QueryAgentBotContext) => (): string => {
@@ -107,7 +107,7 @@ ${bot.modes.getMiniDocs()}`)
function createInventoryQuery(): Query {
return {
name: '!inventory',
name: 'inventory',
description: 'Get your bot\'s inventory.',
schema: z.object({}),
perform: (agent: QueryAgentBotContext) => (): string => {
@@ -133,7 +133,7 @@ WEARING: ${wearing || 'Nothing'}`)
function createNearbyBlocksQuery(): Query {
return {
name: '!nearbyBlocks',
name: 'nearbyBlocks',
description: 'Get the blocks near the bot.',
schema: z.object({}),
perform: (agent: QueryAgentBotContext) => (): string => {
@@ -145,7 +145,7 @@ function createNearbyBlocksQuery(): Query {
function createCraftableQuery(): Query {
return {
name: '!craftable',
name: 'craftable',
description: 'Get the craftable items with the bot\'s inventory.',
schema: z.object({}),
perform: (agent: QueryAgentBotContext) => (): string => {
@@ -157,7 +157,7 @@ function createCraftableQuery(): Query {
function createEntitiesQuery(): Query {
return {
name: '!entities',
name: 'entities',
description: 'Get the nearby players and entities.',
schema: z.object({}),
perform: (agent: QueryAgentBotContext) => (): string => {
@@ -182,7 +182,7 @@ function createEntitiesQuery(): Query {
function createModesQuery(): Query {
return {
name: '!modes',
name: 'modes',
description: 'Get all available modes and their docs and see which are on/off.',
schema: z.object({}),
perform: (agent: QueryAgentBotContext) => (): string => agent.bot.modes.getDocs(),
@@ -191,7 +191,7 @@ function createModesQuery(): Query {
function createSavedPlacesQuery(): Query {
return {
name: '!savedPlaces',
name: 'savedPlaces',
description: 'List all saved locations.',
schema: z.object({}),
perform: (agent: QueryAgentBotContext) => (): string =>
+3 -4
View File
@@ -1,6 +1,6 @@
import process from 'node:process'
import { Format, LogLevel, setGlobalFormat, setGlobalLogLevel, useLogg } from '@guiiai/logg'
import { useLogg } from '@guiiai/logg'
import { initAgent } from './agents/openai'
import { createCommandComponent } from './components/command'
@@ -9,13 +9,12 @@ import { createPathFinderComponent } from './components/pathfinder'
import { createStatusComponent } from './components/status'
import { createBot, useBot } from './composables/bot'
import { botConfig, initEnv } from './composables/config'
import { initLogger } from './utils/logger'
const logger = useLogg('main').useGlobalConfig()
async function main() {
setGlobalLogLevel(LogLevel.Debug)
setGlobalFormat(Format.Pretty)
initLogger()
initEnv()
createBot(botConfig)
+6 -3
View File
@@ -1,9 +1,12 @@
import type { BotContext } from '@/composables/bot'
export function basicSystemPrompt(botName: string): string {
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 genSystemPrompt(ctx: BotContext): string {
return `
You are a playful Minecraft bot named ${ctx.botName} that can converse with players, see, move,
mine, build, and interact with the world by using commands.
return `${basicSystemPrompt(ctx.botName)}
${ctx.prompt.selfPrompt}
+9
View File
@@ -0,0 +1,9 @@
import { Format, LogLevel, setGlobalFormat, setGlobalLogLevel, useLogg } from '@guiiai/logg'
export function initLogger() {
setGlobalLogLevel(LogLevel.Debug)
setGlobalFormat(Format.Pretty)
const logger = useLogg('logger').useGlobalConfig()
logger.log('Logger initialized')
}