Skip to content

Commit

Permalink
Auto ai interpret
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Mar 4, 2024
1 parent 23fc22c commit 6131d87
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/ethernaut-ai/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ const { extendEnvironment, extendConfig } = require('hardhat/config')
const requireAll = require('common/src/require-all')
const spinner = require('common/src/spinner')
const storage = require('./internal/storage')
const hardhatParser = require('./internal/hardhat-parser')

requireAll(__dirname, 'tasks')

hardhatParser.modifyArgumentsParser()

extendEnvironment((hre) => {
spinner.enable(!hre.hardhatArguments.verbose)

storage.init()

hardhatParser.preParse(hre)
})

extendConfig((config, userConfig) => {
Expand Down
101 changes: 101 additions & 0 deletions packages/ethernaut-ai/src/internal/hardhat-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const {
getEnvHardhatArguments,
} = require('hardhat/internal/core/params/env-variables')
const {
HARDHAT_PARAM_DEFINITIONS,
} = require('hardhat/internal/core/params/hardhat-params')
const { ArgumentsParser } = require('hardhat/internal/cli/ArgumentsParser')
const getNodes = require('common/src/get-nodes')
const debug = require('common/src/debug')
const output = require('common/src/output')

let _allUnparsedCLAs

function modifyArgumentsParser() {
debug.log('Modifying ArgumentsParser', 'parse')

const originalParseScopeAndTaskNames =
ArgumentsParser.prototype.parseScopeAndTaskNames

ArgumentsParser.prototype.parseScopeAndTaskNames = function (
allUnparsedCLAs,
taskDefinitions,
scopeDefinitions,
) {
return originalParseScopeAndTaskNames.call(
this,
_allUnparsedCLAs || allUnparsedCLAs,
taskDefinitions,
scopeDefinitions,
)
}
debug.log(
`ArgumentsParser modified: ${ArgumentsParser.prototype.parseScopeAndTaskNames}`,
'parse-deep',
)
}

function preParse(hre) {
debug.log(`Pre-parsing... ${JSON.stringify(process.argv, null, 2)}`, 'parse')

const envVariableArguments = getEnvHardhatArguments(
HARDHAT_PARAM_DEFINITIONS,
process.env,
)

const argumentsParser = new ArgumentsParser()
const { hardhatArguments, scopeOrTaskName, allUnparsedCLAs } =
argumentsParser.parseHardhatArguments(
HARDHAT_PARAM_DEFINITIONS,
envVariableArguments,
process.argv.slice(2),
)
debug.log(
`hardhatArguments: ${JSON.stringify(hardhatArguments, null, 2)}`,
'parse',
)
debug.log(`scopeOrTaskName: ${scopeOrTaskName}`, 'parse')
debug.log(`allUnparsedCLAs: ${allUnparsedCLAs}`, 'parse')

const nodes = getNodes(hre)
const tasks = nodes.filter((node) => !node.isScope)
const scopes = nodes.filter((node) => node.isScope)
const taskDefinitions = tasks.reduce((map, task) => {
map[task._task] = task
return map
}, {})
const scopesDefinitions = scopes.reduce((map, scope) => {
map[scope.name] = scope
return map
}, {})

try {
let { scopeName, taskName, unparsedCLAs } =
argumentsParser.parseScopeAndTaskNames(
allUnparsedCLAs,
taskDefinitions,
scopesDefinitions,
)
debug.log(`Pre-parse ok: ${scopeName} ${taskName} ${unparsedCLAs}`, 'parse')
_allUnparsedCLAs = allUnparsedCLAs
} catch (err) {
debug.log(`Pre-parse failed: ${err}`, 'parse')
const newArgs = [
'ai',
'interpret',
'--model',
'assistant-defined',
'--no-confirm',
'--new-thread',
allUnparsedCLAs.join(' '),
]
debug.log(`Modifying args to: ${newArgs}`, 'parse')
output.info('Uh? Not sure what you mean, interpreting with ai...')
_allUnparsedCLAs = newArgs
}
}

module.exports = {
modifyArgumentsParser,
preParse,
}

0 comments on commit 6131d87

Please sign in to comment.