feat: AgentIO abort previous request on new input

This commit is contained in:
yumoqing 2026-08-11 14:00:42 +08:00
parent f9322531d0
commit 5fb7796df1
2 changed files with 36 additions and 0 deletions

View File

@ -343,6 +343,11 @@ bricks.AgentIO = class extends bricks.VBox {
console.log('chunk end');
}
async user_inputed(e){
// 中止上一次请求
if (this._current_hr) {
this._current_hr.abort();
this._current_hr = null;
}
this.show_input(e.params);
var params = e.params;
params.llmid = this.agent_using_llmid;
@ -361,12 +366,15 @@ bricks.AgentIO = class extends bricks.VBox {
});
this.msg_box.add_widget(mout);
var hr = new bricks.HttpResponseStream();
this._current_hr = hr;
var resp = await hr.post(this.opts.url, {params:params});
if (! resp) {
mout.run_stopped();
this._current_hr = null;
return;
}
await hr.handle_chunk(resp, this.chunk_response.bind(this, mout));
this._current_hr = null;
this.chunk_ended();
}
async show_input(params){

View File

@ -247,6 +247,34 @@ bricks.HttpResponse = class extends bricks.HttpText {
}
bricks.HttpResponseStream = class extends bricks.HttpResponse {
constructor(){
super();
this._controller = new AbortController();
}
abort(){
this._controller.abort();
}
async bricks_fetch(url, {method='GET', headers=null, params=null, signal=null}={}){
url = this.url_parse(url);
var data = this.add_own_params(params);
var header = this.add_own_headers(headers);
var _params = {
method:method,
signal: signal || this._controller.signal
}
if (data instanceof FormData){
method = 'POST';
_params.body = data;
} else {
if (method == 'GET' || method == 'HEAD') {
let pstr = url_params(data);
url = url + '?' + pstr;
} else {
_params.body = JSON.stringify(data);
}
}
return await fetch(url, _params);
}
async handle_chunk(resp, handler){
const reader = resp.body.getReader();
const decoder = new TextDecoder('utf-8');