examples/ai/browser_host.c
/* Link the compiled Basalt program to two browser host functions. */
#include <emscripten.h>
#include "system.h"
EM_JS(char *, read_prompt, (), {
return stringToNewUTF8(Module["promptText"]);
});
EM_ASYNC_JS(char *, model_answer, (const char *p,size_t len,int mode), {
const result=await Module["infer"](UTF8ToString(p,len),mode);
return stringToNewUTF8(result);
});
static Word browser_external(External *ext,const Word *args) {
char *result=NULL;
if (!strcmp(ext->path,"browser") && !strcmp(ext->name,"browser_prompt") && ext->count==0)
result=read_prompt();
else if (!strcmp(ext->path,"browser") && !strcmp(ext->name,"browser_qwen") && ext->count==2) {
Buffer *b=buffer_at(args[0]);
result=model_answer((const char *)b->data,b->size,(int)args[1]);
} else system_fail("Unknown browser extension");
if (!result) return 0;
Word id=buffer_copy(result,strlen(result));
free(result);
return id;
}
#define external_call browser_external
#define main basalt_entry
#include "qwen-generated.c"
#undef main
EMSCRIPTEN_KEEPALIVE int run_demo(int mode) {
char *args[]={"basalt",mode?"1":"0",NULL};
return basalt_entry(2,args);
}