Skip to content

Commit

Permalink
refactor: upgradeBrains
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu committed Jul 12, 2024
1 parent 57bcdfc commit 2466801
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
49 changes: 46 additions & 3 deletions src/lib/brain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,43 @@ import EventEmitter from 'events'
EventEmitter.defaultMaxListeners = 1000

// note: need initTools() first
export async function upgradeBrains(url?: string) {
export async function upgradeBrains(flags?: any) {
const brains = ToolFunc.get(BRAINS_FUNC_NAME) as LlmModelsFunc
const count = await brains.$refresh({url})
return count
let _models: AIModelSettings[]|undefined
let shouldBreak: boolean|undefined

brains.on('brain:refresh', onRefresh)
process.on('SIGINT', interrupted)

try {
const count = await brains.$refresh(flags)
return count
} finally {
brains.off('brain:refresh', onRefresh)
process.off('SIGINT', interrupted)
if (shouldBreak) {console.log('saved.')}
}

function onRefresh(tool_name: string, act: string, model: AIModelSettings, models: AIModelSettings[]|string){
let s = model._id!
if (s.includes(':')) {s = ' ' + s}
if (Array.isArray(models)) {
_models = models
} else if(models && typeof models === 'string') {
s += ' ' + models
}
console.log(act, s)
if (shouldBreak) {this.result = true}
}

async function interrupted() {
if (_models) {
console.log('wait to exit...')
shouldBreak = true
} else {
process.exit(0)
}
}
}

export async function listBrains(userConfig: any, flags: any) {
Expand Down Expand Up @@ -60,6 +93,16 @@ export async function searchBrains(brainDir: string, flags: any) {
return result
}

export async function verifyBrains(brains: AIModelSettings[]) {
const brainFunc = ToolFunc.get(BRAINS_FUNC_NAME) as LlmModelsFunc
for (const brain of brains) {
const changed = await brainFunc.verifyFileExists(brain)
if (changed) {
brainFunc.put({id: brain._id, val: brain})
}
}
}

export function printBrains(brains: AIModelSettings[], flags?: {count?: number}) {
let maxArrayLength = flags?.count ?? 100
if (maxArrayLength >= 0) {maxArrayLength--}
Expand Down
11 changes: 9 additions & 2 deletions src/oclif/commands/brain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Args, Flags } from '@oclif/core'
import { AICommand } from '../../lib/ai-command.js'
import { showBanner } from '../../lib/help.js'
import { parseJsJson } from '@isdk/ai-tool'
import { listBrains, printBrains, upgradeBrains } from '../../../lib/brain.js'
import { listBrains, printBrains, upgradeBrains, verifyBrains } from '../../../lib/brain.js'

export default class Brain extends AICommand {
static args = {
Expand Down Expand Up @@ -34,6 +34,12 @@ export default class Brain extends AICommand {
description: 'the hub mirror url',
dependsOn: ['refresh'],
}),
verifyQuant: Flags.boolean({
char: 'v',
aliases: ['verify-quant'],
description: 'whether verify quant when refresh',
dependsOn: ['refresh'],
}),
}

static summary = '🧠 The AI Agent Brains(LLM) Manager.'
Expand Down Expand Up @@ -61,7 +67,7 @@ export default class Brain extends AICommand {
await this.config.runHook('init_tools', {id: 'brain', userConfig})

if (flags.refresh) {
const count = await upgradeBrains(flags.hubUrl)
const count = await upgradeBrains(flags)
this.log(`${count} brains updated`)
return count
}
Expand All @@ -70,6 +76,7 @@ export default class Brain extends AICommand {
flags.name = args.name
flags.downloaded = true
const result = await listBrains(userConfig, flags)
if (result) { await verifyBrains(result) }
if (!isJson) {
if (!result || result.length === 0) {
this.log('No brains found')
Expand Down
2 changes: 1 addition & 1 deletion src/oclif/commands/brain/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class AIBrainListCommand extends AICommand {
await this.config.runHook('init_tools', {id: 'brain', userConfig})

if (flags.refresh) {
const count = await upgradeBrains(flags.hubUrl)
const count = await upgradeBrains(flags)
this.log(`${count} brains updated`)
}

Expand Down

0 comments on commit 2466801

Please sign in to comment.