SimpleChatTC:tooljs: Trap console.log and store in new result key
The implementations of javascript and simple_calculator now use provided helpers to trap console.log messages when they execute the code / expression provided by GenAi and inturn store the captured log messages in the newly added result key in tc_switch This should help trap the output generated by the provided code or expression as the case maybe and inturn return the same to the GenAi, for its further processing.
This commit is contained in:
parent
6d43011003
commit
fa63a86c71
|
|
@ -7,6 +7,40 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
|
let gConsoleStr = ""
|
||||||
|
/**
|
||||||
|
* @type { {(...data: any[]): void} | null}
|
||||||
|
*/
|
||||||
|
let gOrigConsoleLog = null
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {any[]} args
|
||||||
|
*/
|
||||||
|
function console_trapped(...args) {
|
||||||
|
let res = args.map((arg)=>{
|
||||||
|
if (typeof arg == 'object') {
|
||||||
|
return JSON.stringify(arg);
|
||||||
|
} else {
|
||||||
|
return String(arg);
|
||||||
|
}
|
||||||
|
}).join(' ');
|
||||||
|
gConsoleStr += res;
|
||||||
|
}
|
||||||
|
|
||||||
|
function console_redir() {
|
||||||
|
gOrigConsoleLog = console.log
|
||||||
|
console.log = console_trapped
|
||||||
|
gConsoleStr = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
function console_revert() {
|
||||||
|
if (gOrigConsoleLog !== null) {
|
||||||
|
console.log = gOrigConsoleLog
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
let js_meta = {
|
let js_meta = {
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
|
|
@ -32,8 +66,11 @@ let js_meta = {
|
||||||
* @param {any} obj
|
* @param {any} obj
|
||||||
*/
|
*/
|
||||||
function js_run(obj) {
|
function js_run(obj) {
|
||||||
|
console_redir()
|
||||||
let func = new Function(obj["code"])
|
let func = new Function(obj["code"])
|
||||||
func()
|
func()
|
||||||
|
console_revert()
|
||||||
|
tc_switch["javascript"]["result"] = gConsoleStr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -62,22 +99,27 @@ let calc_meta = {
|
||||||
* @param {any} obj
|
* @param {any} obj
|
||||||
*/
|
*/
|
||||||
function calc_run(obj) {
|
function calc_run(obj) {
|
||||||
|
console_redir()
|
||||||
let func = new Function(obj["arithexpr"])
|
let func = new Function(obj["arithexpr"])
|
||||||
func()
|
func()
|
||||||
|
console_revert()
|
||||||
|
tc_switch["simple_calculator"]["result"] = gConsoleStr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Object<string, Object>}
|
* @type {Object<string, Object<string, any>>}
|
||||||
*/
|
*/
|
||||||
export let tc_switch = {
|
export let tc_switch = {
|
||||||
"javascript": {
|
"javascript": {
|
||||||
"handler": js_run,
|
"handler": js_run,
|
||||||
"meta": js_meta
|
"meta": js_meta,
|
||||||
|
"result": ""
|
||||||
},
|
},
|
||||||
"simple_calculator": {
|
"simple_calculator": {
|
||||||
"handler": calc_run,
|
"handler": calc_run,
|
||||||
"meta": calc_meta
|
"meta": calc_meta,
|
||||||
|
"result": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import * as tjs from './tooljs.mjs'
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Object<string,Object>}
|
* @type {Object<string,Object<string,any>>}
|
||||||
*/
|
*/
|
||||||
let tc_switch = {}
|
let tc_switch = {}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue