console API starts to work...

This commit is contained in:
STEINNI
2025-11-18 21:13:10 +00:00
parent a0a0bba6f8
commit 3b67e25986
7 changed files with 109 additions and 152 deletions
+55 -6
View File
@@ -67,13 +67,62 @@ class KeyframeView extends WindozDomContent {
async execCommand(event){
console.log('cmd:', this.outputs.commands)
if(this.outputs.commands.trim()=='help'){
this.outputs.results.innerHTML = `
Type any javascript at your own risks.
To create an agent : this.newAgent(type, properties)
`
if(this.outputs.commands.value.trim()=='\\help'){
this.outputs.results.innerHTML += await app.Assets.loadHtml({ name: 'help/KFconsoleHelp.html' })
} else if(this.outputs.commands.value.trim()=='\\clear'){
this.outputs.results.innerHTML = ''
} else {
this.kfArena.evalCmd(this.outputs.commands)
this.outputs.results.innerHTML += await this.evalCmd(this.outputs.commands.value)
}
const lines = this.outputs.results.querySelectorAll('div.line')
if(lines.length > 100) {
for(let i=0; i<(lines.length-100); i++) lines[i].remove()
}
}
async evalCmd(code){
const api = {
newAgent: async (type, properties) => {
if(Array.from(this.outputs.agentsSelector.options).find(item => item.value==type)){
this.outputs.agentsSelector.value = type
await this.onChangeAgent()
const defaultValues = this.getFieldsValues('div[data-output="agentProperties"]')
return(this.newAgent(type, { ...defaultValues, ...properties })) //TODO: deepMerge
} else {
throw(`Invalid agent type: ${type}`)
}
},
removeAgent: (aid) => {
},
updateAgent: (aid, properties) => {
},
}
try {
const fn = new Function(...Object.keys(api), `
return(
(async () => {
const logs = []
const log = (item) => { logs.push(item) }
${code}
return(logs)
})()
)
`)
const res = await fn(...Object.values(api))
return(
'<div class="line">'+
res.map(item => {
if(typeof(item) == 'object') return(JSON.stringify(item))
return(item)
}).join('</div><div class="line">')
+'</div>'
)
} catch (err) {
return(`<div class="error">${err.name}: ${err.message}</div>`)
}
}