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

View File

@ -51,14 +51,16 @@ bricks.LlmMsgBox = class extends bricks.HBox {
super(opts); super(opts);
var img = new bricks.Icon({rate:2,url:this.icon||bricks_resource('imgs/user.png')}); var img = new bricks.Icon({rate:2,url:this.icon||bricks_resource('imgs/user.png')});
this.w = new bricks.MdWidget({mdtext:' '}); 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.w.set_css(this.msg_css||'llm_msg');
this.add_widget(img); this.add_widget(img);
this.add_widget(this.run)
this.add_widget(this.w); this.add_widget(this.w);
this.messages = []; this.messages = [];
} }
responsed(){ responsed(){
this.run.dismiss(); this.run.stop_timepass();
this.remove_widget(this.run);
this.w.md_content = ''; this.w.md_content = '';
} }
user_msg_format(){ user_msg_format(){
@ -147,18 +149,40 @@ bricks.LlmDialog = class extends bricks.VBox {
opts.height = opts.height || '100%'; opts.height = opts.height || '100%';
super(opts); super(opts);
this.title_w = new bricks.HBox({cheight:2}); 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'); 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.add_widget(this.title_w);
this.model_info_ws = {};
this.show_models_info();
var filler = new bricks.Filler({}); var filler = new bricks.Filler({});
this.body = new bricks.VScrollPanel({height:'100%'}); this.body = new bricks.VScrollPanel({height:'100%'});
filler.add_widget(this.body); filler.add_widget(this.body);
this.add_widget(filler); 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){ async set_prompt(prompt){
prompt = bricks.escapeSpecialChars(prompt); prompt = bricks.escapeSpecialChars(prompt);
var box = new bricks.UserMsgBox({ var box = new bricks.UserMsgBox({

View File

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

View File

@ -11,6 +11,23 @@
"height":"40px" "height":"40px"
}, },
"subwidgets":[ "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", "id":"insert",
"widgettype":"Button", "widgettype":"Button",

View File

@ -150,7 +150,16 @@
"required":true "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);"
}
]
}