From 5fb7796df1d0b416ae978ecd95be076a037d30a7 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 11 Aug 2026 14:00:42 +0800 Subject: [PATCH] feat: AgentIO abort previous request on new input --- bricks/agent.js | 8 ++++++++ bricks/jsoncall.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/bricks/agent.js b/bricks/agent.js index 7d518ad..7430077 100644 --- a/bricks/agent.js +++ b/bricks/agent.js @@ -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){ diff --git a/bricks/jsoncall.js b/bricks/jsoncall.js index 02d1bcc..c31f9ee 100644 --- a/bricks/jsoncall.js +++ b/bricks/jsoncall.js @@ -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');