code interpreter: support variadic console.log (#3371)

Signed-off-by: Jared Van Bortel <jared@nomic.ai>
This commit is contained in:
Jared Van Bortel 2025-01-07 17:58:04 -05:00 committed by GitHub
parent 22f6a7f1bc
commit e2541a24b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -5,7 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
## [Unreleased]
## Fixed
- Fix the timeout error in code interpreter ([#3369](https://github.com/nomic-ai/gpt4all/pull/3369))
- Fix code interpreter console.log not accepting multiple arguments ([#3371](https://github.com/nomic-ai/gpt4all/pull/3371))
## [3.6.1] - 2024-12-20

View File

@ -5,6 +5,9 @@
#include <QThread>
#include <QVariant>
using namespace Qt::Literals::StringLiterals;
QString CodeInterpreter::run(const QList<ToolParam> &params, qint64 timeout)
{
m_error = ToolEnums::Error::NoError;
@ -92,7 +95,25 @@ CodeInterpreterWorker::CodeInterpreterWorker()
void CodeInterpreterWorker::request(const QString &code)
{
JavaScriptConsoleCapture consoleCapture;
QJSValue consoleObject = m_engine.newQObject(&consoleCapture);
QJSValue consoleInternalObject = m_engine.newQObject(&consoleCapture);
m_engine.globalObject().setProperty("console_internal", consoleInternalObject);
// preprocess console.log args in JS since Q_INVOKE doesn't support varargs
auto consoleObject = m_engine.evaluate(uR"(
class Console {
log(...args) {
if (args.length && typeof args[0] === 'string')
throw new Error('console.log string formatting not supported');
let cat = '';
for (const arg of args) {
cat += String(arg);
}
console_internal.log(cat);
}
}
new Console();
)"_s);
m_engine.globalObject().setProperty("console", consoleObject);
const QJSValue result = m_engine.evaluate(code);