Skip to content

Commit

Permalink
Merge branch 'main' into onboarding/util-timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 authored Sep 25, 2024
2 parents 6df67d4 + 4ea81d2 commit c914e1a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/ethernaut-ai-ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ require('ethernaut-ui/src/index')
require('ethernaut-ai/src/index')

extendEnvironment((hre) => {
const config = hre.scopes.ai.tasks.config
const config = hre.scopes.ai.tasks.model
config.paramDefinitions.model.prompt = require('./prompts/model')
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { Terminal } = require('ethernaut-common/src/test/terminal')

describe('config ui', function () {
describe('model ui', function () {
const terminal = new Terminal()

describe('when config is called with no params', function () {
describe('when model is called with no params', function () {
before('call', async function () {
await terminal.run('hardhat ai config', 2000)
await terminal.run('hardhat ai model', 2000)
})

it('displays gpt models', async function () {
Expand Down
31 changes: 31 additions & 0 deletions packages/ethernaut-ai/src/tasks/key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const types = require('ethernaut-common/src/validation/types')
const output = require('ethernaut-common/src/ui/output')
const storage = require('ethernaut-common/src/io/storage')
const { setEnvVar } = require('ethernaut-common/src/io/env')

require('../scopes/ai')
.task('key', 'Sets the openai api key')
.addParam('apiKey', 'The openai api key to use', undefined, types.string)
.setAction(async ({ apiKey }, hre) => {
try {
const config = storage.readConfig()

let summary = []

if (apiKey) {
const currentKey = process.env.OPENAI_API_KEY
setEnvVar('OPENAI_API_KEY', apiKey)
summary.push(`- API Key set to ${apiKey} (was ${currentKey})`)
}

storage.saveConfig(config)

if (summary.length === 0) {
summary.push('No changes')
}

return output.resultBox(summary.join('\n'))
} catch (err) {
return output.errorBox(err)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const output = require('ethernaut-common/src/ui/output')
const storage = require('ethernaut-common/src/io/storage')

require('../scopes/ai')
.task('config', 'Configures ai scope parameters')
.task('model', 'Sets the openai model')
.addParam('model', 'The openai model to use', undefined, types.string)
.setAction(async ({ model }, hre) => {
try {
Expand Down
52 changes: 38 additions & 14 deletions packages/ethernaut-common/src/io/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,59 @@ const { getEthernautFolderPath } = require('ethernaut-common/src/io/storage')
const envPath = path.join(getEthernautFolderPath(), '.env')

function refreshEnv() {
// Create the .env file if it doesn't exist
if (!fs.existsSync(envPath)) {
debug.log('No .env file found, creating one...', 'env')
fs.writeFileSync(envPath, '')
}

// Load the .env file
require('dotenv').config({ path: envPath })
}

async function checkEnvVar(varName, message) {
refreshEnv()

// Check if the env var exists at runtime
if (process.env[varName]) {
debug.log(`Environment variable ${varName} found`, 'env')
return
}

debug.log(`Environment variable ${varName} not found - collecting it...`)

if (!fs.existsSync(envPath)) {
debug.log('No .env file found, creating one...', 'env')
fs.writeFileSync(envPath, '')
}
// Collect the env var from the user
const varValue = await prompt({
type: 'input',
message: `Please provide a value for ${varName}${message ? `. ${message}` : ''}`,
})

// Save the env var to the .env file
setEnvVar(varName, varValue)
}

const envConfig = dotenv.parse(fs.readFileSync(envPath))
if (!envConfig[varName]) {
const varValue = await prompt({
type: 'input',
message: `Please provide a value for ${varName}${message ? `. ${message}` : ''}`,
})
debug.log(`Saved environment variable ${varName} in .env file...`)
fs.appendFileSync(envPath, `${varName}=${varValue}`)
require('dotenv').config({ path: envPath })
function loadEnvConfig() {
return dotenv.parse(fs.readFileSync(envPath))
}

function setEnvVar(varName, varValue) {
// Load and set the env var
const envConfig = loadEnvConfig()
envConfig[varName] = varValue

// Write the env var to the .env file
let envFileContent = ''
for (const [key, value] of Object.entries(envConfig)) {
envFileContent += `${key}=${value}\n`
}
fs.writeFileSync(envPath, envFileContent)
debug.log(`Saved environment variable ${varName} in .env file...`)

process.env[varName] = varValue
refreshEnv()
}

module.exports = {
refreshEnv,
checkEnvVar,
setEnvVar,
}

0 comments on commit c914e1a

Please sign in to comment.