This commit is contained in:
yumoqing 2024-04-19 17:13:05 +08:00
parent bff3e1726c
commit d573fa40f7
7 changed files with 116 additions and 28 deletions

View File

@ -171,10 +171,10 @@ bricks.buildEventBind = function(from_widget, widget, event, desc){
}
bricks.universal_handler = function(from_widget, widget, desc, event){
var f = bricks.buildEventHandler(from_widget, desc, event);
bricks.universal_handler = async function(from_widget, widget, desc, event){
var f = await bricks.buildEventHandler(from_widget, desc, event);
if (f){
return f(event);
return await f(event);
}
bricks.debug('universal_handler() error', 'from_widget=',
from_widget,
@ -182,7 +182,7 @@ bricks.universal_handler = function(from_widget, widget, desc, event){
'desc=', desc,
event);
}
bricks.buildEventHandler = function(w, desc, event){
bricks.buildEventHandler = async function(w, desc, event){
var target = bricks.getWidgetById(desc.target, w);
if (! target){
bricks.debug('target miss desc=', desc, 'w=', w);
@ -198,7 +198,7 @@ bricks.buildEventHandler = function(w, desc, event){
params:desc.dataparams,
script:desc.datascript
}
rtdata = bricks.getRealtimeData(w, data_desc);
rtdata = await bricks.getRealtimeData(w, data_desc);
}
if (typeof event.params == typeof {}){
rtdata = bricks.extend(rtdata, event.params);
@ -228,7 +228,7 @@ bricks.buildEventHandler = function(w, desc, event){
break;
}
}
bricks.getRealtimeData = function(w, desc){
bricks.getRealtimeData = async function(w, desc){
var target = bricks.getWidgetById(desc.widget, w);
var f;
bricks.debug('getRealtimeData() calling ...');
@ -242,7 +242,7 @@ bricks.getRealtimeData = function(w, desc){
}
if (desc.script){
f = bricks.buildScriptHandler(null, target, null, desc)
return f();
return await f();
}
debug('getRealtimeData():desc=', desc, 'f=', f);
return null;
@ -341,8 +341,8 @@ bricks.buildScriptHandler = function(w, target, rtdata, desc){
bricks.extend(params, desc.params)
bricks.extend(params, rtdata);
params = bricks.apply_data(params, rtdata);
var f = new Function('target', 'params', 'event', desc.script);
bricks.debug('params=', params, 'buildScriptHandler() ..........');
var AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
var f = new AsyncFunction('target', 'params', 'event', desc.script);
return f.bind(target, target, params);
}
bricks.buildDispatchEventHandler = function(w, target, rtdata, desc){

View File

@ -2,6 +2,11 @@ var bricks = window.bricks || {};
bricks.need_formdata_fields = ['file', 'image', 'video', 'audio'];
bricks.show_resp_message_or_error = async function(resp){
var desc = await resp.json();
await bricks.widgetBuild(desc, bricks.Body);
}
bricks.FieldGroup = class extends bricks.VBox {
constructor(opts){
opts.leftMargin = '10px';

View File

@ -51,14 +51,16 @@ bricks.LlmMsgBox = class extends bricks.HBox {
super(opts);
var img = new bricks.Icon({rate:2,url:this.icon||bricks_resource('imgs/user.png')});
this.w = new bricks.MdWidget({mdtext:' '});
this.run = new bricks.Running({target:this});
this.run = new bricks.BaseRunning({target:this});
this.w.set_css(this.msg_css||'llm_msg');
this.add_widget(img);
this.add_widget(this.run)
this.add_widget(this.w);
this.messages = [];
}
responsed(){
this.run.dismiss();
this.run.stop_timepass();
this.remove_widget(this.run);
this.w.md_content = '';
}
user_msg_format(){
@ -147,18 +149,40 @@ bricks.LlmDialog = class extends bricks.VBox {
opts.height = opts.height || '100%';
super(opts);
this.title_w = new bricks.HBox({cheight:2});
this.title_w.add_widget(new bricks.Icon({
rate:2,url:this.llm_icon||bricks_resource('imgs/llm.png')
}));
this.title_w.set_css(this.title_ccs||'llm_title');
var t_w = new bricks.Text({text:this.model});
this.title_w.add_widget(t_w);
this.add_widget(this.title_w);
this.model_info_ws = {};
this.show_models_info();
var filler = new bricks.Filler({});
this.body = new bricks.VScrollPanel({height:'100%'});
filler.add_widget(this.body);
this.add_widget(filler);
}
show_models_info(){
for(var i=0;i<this.models.length;i++){
this.show_model_info(this.models[i]);
}
}
show_model_info(model){
var w = new bricks.HBox({margin:'5px'});
w.add_widget(new bricks.Icon({url:model.icon||bricks_resource('imgs/llm.png'), rate:2}));
w.add_widget(new bricks.Text({text:model.model}));
this.title_w.add_widget(w);
this.model_info_ws[model.model] = w;
}
add_model(model){
this.models.push(model);
this.show_model_info(model);
}
delete_model(model){
for(var i=0;i<this.models.length;i++){
if (this.models[i].model == model.model){
this.models.splice(i,1);
break;
}
}
delete this.model_info_ws[model.model];
}
async set_prompt(prompt){
prompt = bricks.escapeSpecialChars(prompt);
var box = new bricks.UserMsgBox({

View File

@ -1,16 +1,13 @@
var bricks = window.bricks || {};
bricks.Running = class extends bricks.BaseModal {
bricks.BaseRunning = class extends bricks.FHBox {
/*
{
target:
icon:
"icon"
}
*/
constructor(opts){
opts.auto_open = true;
opts.archor = 'cc';
opts.width='96px';
opts.width = opts.width || 'auto';
super(opts);
this.icon_w = new bricks.Icon({
url:opts.icon || bricks_resource('imgs/running.gif')
@ -23,23 +20,41 @@ bricks.Running = class extends bricks.BaseModal {
i18n:false
});
this.time_start = new Date().getTime();
var box = new bricks.FHBox({width:'auto'});
box.add_widget(this.icon_w);
box.add_widget(this.time_w);
this.add_widget(box);
this.add_widget(this.icon_w);
this.add_widget(this.time_w);
this.showtime_task = schedule_once(this.show_timepass.bind(this), 0.05);
}
show_timepass(){
var t = new Date().getTime() - this.time_start;
var txt = bricks.formatMs(t, 1);
this.time_w.set_text(txt);
this.showtime_task = schedule_once(this.show_timepass.bind(this), 0.05);
}
dismiss(){
stop_timepass(){
if (this.showtime_task){
this.showtime_task.cancel();
this.showtime_task = null;
}
}
}
bricks.Running = class extends bricks.BaseModal {
/*
{
target:
icon:
}
*/
constructor(opts){
opts.auto_open = true;
opts.archor = 'cc';
super(opts);
this.w = new bricks.BaseRunning({icon:opts.icon});
this.add_widget(this.w);
}
dismiss(){
this.w.stop_timepass();
bricks.BaseModal.prototype.dismiss.bind(this)();
}
}

View File

@ -11,6 +11,23 @@
"height":"40px"
},
"subwidgets":[
{
"widgettype":"Button",
"options":{
"width":"80px",
"i18n":true,
"label":"click me"
},
"binds":[
{
"wid":"self",
"event":"click",
"actiontype":"script",
"target":"self",
"script":"console.log(target, params, event);"
}
]
},
{
"id":"insert",
"widgettype":"Button",

View File

@ -150,7 +150,16 @@
"required":true
}
]
},
"binds":[
{
"wid":"self",
"event":"submited",
"actiontype":"script",
"target":"self",
"script":"await bricks.show_resp_message_or_error(event.params)"
}
]
}
]
}

18
examples/script_action.ui Normal file
View File

@ -0,0 +1,18 @@
{
"id":"insert",
"widgettype":"Button",
"options":{
"width":"80px",
"i18n":true,
"label":"click me"
},
"binds":[
{
"wid":"self",
"event":"click",
"actiontype":"script",
"target":"self",
"script":"console.log(target, params, event);"
}
]
}