This commit is contained in:
mike dupont 2023-12-08 13:44:39 -05:00
parent 593985dad3
commit 9ec7eb1c3b
5 changed files with 91 additions and 39 deletions

48
.gitignore vendored
View file

@ -107,3 +107,51 @@ tests/test-tokenizer-1-bpe
*~
.#*
#*
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
# Org-mode
.org-id-locations
*_archive
# flymake-mode
*_flymake.*
# eshell files
/eshell/history
/eshell/lastdir
# elpa packages
/elpa/
# reftex files
*.rel
# AUCTeX auto folder
/auto/
# cask packages
.cask/
dist/
# Flycheck
flycheck_*.el
# server auth directory
/server/
# projectiles files
.projectile
# directory configuration
.dir-locals.el
# network security
/network-security.data

View file

@ -1,6 +1,6 @@
set(TARGET main)
add_executable(${TARGET} main.cpp)
install(TARGETS ${TARGET} RUNTIME)
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} libnode.so plugin_nodejs.o)
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} libnode.so )
target_compile_features(${TARGET} PRIVATE cxx_std_20)

View file

@ -138,6 +138,7 @@ int main(int argc, char ** argv) {
// save choice to use color for later
// (note for later: this is a slightly awkward choice)
console::init(params.simple_io, params.use_color);
process_output_plugin_node_init();
atexit([]() { console::cleanup(); });
if (params.logits_all) {
@ -910,5 +911,6 @@ int main(int argc, char ** argv) {
LOG_TEE("Log end\n");
#endif // LOG_DISABLE_LOGS
process_output_plugin_node_destroy();
return 0;
}

View file

@ -7,16 +7,30 @@
#include <libnode/node_api.h>
#include <libnode/js_native_api.h>
#include <libnode/js_native_api_types.h>
class Context {
public:
napi_platform platform;
};
static Context context;
void process_output_plugin_node_init()
{
if (napi_create_platform(0, NULL, 0, NULL, NULL, 0, &context.platform) != napi_ok) {
fprintf(stderr, "Failed creating the platform\n");
return "error";
}
}
std::string process_output_plugin_node(const std::string start,
const std::string state,
const std::string input) {
// !!! All napi calls for one given environment must
// !!! be made from the same thread that created it
// (except everything napi_threadsafe_function related)
// This the V8 engine, there must be only one
napi_platform platform;
// This is a V8 isolate, there may be multiple
napi_env env;
// This holds local references, when it is closed
@ -30,24 +44,13 @@ std::string process_output_plugin_node(const std::string start,
const char *main_script = "console.log('hello world'); "
"function callMe() { console.log('called you'); }"
// or you can use vm.runInThisContext
"global.callMe = callMe;";
// Do only once
if (napi_create_platform(0, NULL, 0, NULL, NULL, 0, &platform) != napi_ok) {
fprintf(stderr, "Failed creating the platform\n");
return "error";
}
// Do for each environment (V8 isolate)
// 'hello world' will be printed here
if (napi_create_environment(platform, NULL, main_script, &env) != napi_ok) {
if (napi_create_environment(context.platform, NULL, main_script, &env) != napi_ok) {
fprintf(stderr, "Failed running JS\n");
return "error1";
}
// Here you can interact with the environment through Node-API env
// (refer to the Node-API doc)
if (napi_get_global(env, &global) != napi_ok) {
fprintf(stderr, "Failed accessing the global object\n");
return "Failed accessing the global object";
@ -57,34 +60,30 @@ std::string process_output_plugin_node(const std::string start,
fprintf(stderr, "Failed accessing the global object\n");
return "Failed accessing the global object";
}
// This cycle can be repeated
{
// Call a JS function
// V8 will run in this thread
if (napi_call_function(env, global, cb, 0, NULL, &result) != napi_ok) {
fprintf(stderr, "Failed calling JS callback\n");
return "Failed calling JS callback";
}
// (optional) Call this to flush all pending async callbacks
// V8 will run in this thread
if (napi_run_environment(env) != napi_ok) {
fprintf(stderr, "Failed flushing pending JS callbacks\n");
return "Failed flushing pending JS callbacks";
}
}
// Shutdown everyhing
napi_close_handle_scope(env, scope);
if (napi_destroy_environment(env, NULL) != napi_ok) {
return "destroy";
}
if (napi_destroy_platform(platform) != napi_ok) {
fprintf(stderr, "Failed destroying the platform\n");
return "Failed destroying the platform";
}
return "OK";
}
void process_output_plugin_node_destroy();
void process_output_plugin_node_destroy()
{
if (napi_destroy_platform(context.platform) != napi_ok) {
fprintf(stderr, "Failed destroying the platform\n");
//return "Failed destroying the platform";
}
}

View file

@ -1,3 +1,6 @@
void process_output_plugin_node_init();
void process_output_plugin_node_destroy();
std::string process_output_plugin_node(const std::string start,
const std::string state,
const std::string input);