fix(agent): route WS error events through onError instead of dead onComplete branch

This commit is contained in:
Will Miao
2026-07-06 12:18:17 +08:00
parent 2373edf73c
commit 637831248b
2 changed files with 40 additions and 26 deletions

View File

@@ -391,6 +391,15 @@ export class BulkContextMenu extends BaseContextMenu {
`Enriching metadata for ${modelPaths.length} models...` `Enriching metadata for ${modelPaths.length} models...`
); );
function cleanupCallbacks() {
const pIdx = agentManager.progressCallbacks.indexOf(onProgress);
if (pIdx >= 0) agentManager.progressCallbacks.splice(pIdx, 1);
const cIdx = agentManager.completeCallbacks.indexOf(onComplete);
if (cIdx >= 0) agentManager.completeCallbacks.splice(cIdx, 1);
const eIdx = agentManager.errorCallbacks.indexOf(onError);
if (eIdx >= 0) agentManager.errorCallbacks.splice(eIdx, 1);
}
const onProgress = (data) => { const onProgress = (data) => {
if (data.status === 'processing' && data.current_path && data.updated_data && Object.keys(data.updated_data).length > 0) { if (data.status === 'processing' && data.current_path && data.updated_data && Object.keys(data.updated_data).length > 0) {
if (state.virtualScroller?.updateSingleItem) { if (state.virtualScroller?.updateSingleItem) {
@@ -404,10 +413,7 @@ export class BulkContextMenu extends BaseContextMenu {
agentManager.onProgress(onProgress); agentManager.onProgress(onProgress);
const onComplete = (data) => { const onComplete = (data) => {
const pIdx = agentManager.progressCallbacks.indexOf(onProgress); cleanupCallbacks();
if (pIdx >= 0) agentManager.progressCallbacks.splice(pIdx, 1);
const cIdx = agentManager.completeCallbacks.indexOf(onComplete);
if (cIdx >= 0) agentManager.completeCallbacks.splice(cIdx, 1);
if (data.status === 'completed') { if (data.status === 'completed') {
progressUI.complete(data.summary || 'Enrich complete'); progressUI.complete(data.summary || 'Enrich complete');
@@ -416,24 +422,25 @@ export class BulkContextMenu extends BaseContextMenu {
{ summary: data.summary || 'Done' }, { summary: data.summary || 'Done' },
'success' 'success'
); );
} else if (data.status === 'error') {
state.loadingManager.hide();
showToast(
'toast.agent.enrichFailed',
{ error: data.error || 'Unknown error' },
'error'
);
} }
}; };
agentManager.onComplete(onComplete); agentManager.onComplete(onComplete);
const onError = (data) => {
cleanupCallbacks();
state.loadingManager.hide();
showToast(
'toast.agent.enrichFailed',
{ error: data.error || 'Unknown error' },
'error'
);
};
agentManager.onError(onError);
try { try {
await agentManager.executeSkill('enrich_hf_metadata', modelPaths); await agentManager.executeSkill('enrich_hf_metadata', modelPaths);
} catch (error) { } catch (error) {
const pIdx = agentManager.progressCallbacks.indexOf(onProgress); cleanupCallbacks();
if (pIdx >= 0) agentManager.progressCallbacks.splice(pIdx, 1);
const cIdx = agentManager.completeCallbacks.indexOf(onComplete);
if (cIdx >= 0) agentManager.completeCallbacks.splice(cIdx, 1);
state.loadingManager.hide(); state.loadingManager.hide();
showToast( showToast(
'toast.agent.enrichFailed', 'toast.agent.enrichFailed',

View File

@@ -99,6 +99,15 @@ export class LoraContextMenu extends BaseContextMenu {
'Enriching metadata with AI...' 'Enriching metadata with AI...'
); );
function cleanupCallbacks() {
const pIdx = agentManager.progressCallbacks.indexOf(onProgress);
if (pIdx >= 0) agentManager.progressCallbacks.splice(pIdx, 1);
const cIdx = agentManager.completeCallbacks.indexOf(onComplete);
if (cIdx >= 0) agentManager.completeCallbacks.splice(cIdx, 1);
const eIdx = agentManager.errorCallbacks.indexOf(onError);
if (eIdx >= 0) agentManager.errorCallbacks.splice(eIdx, 1);
}
const onProgress = (data) => { const onProgress = (data) => {
if (data.status === 'processing' && data.current_path && data.updated_data && Object.keys(data.updated_data).length > 0) { if (data.status === 'processing' && data.current_path && data.updated_data && Object.keys(data.updated_data).length > 0) {
if (state.virtualScroller?.updateSingleItem) { if (state.virtualScroller?.updateSingleItem) {
@@ -112,28 +121,26 @@ export class LoraContextMenu extends BaseContextMenu {
agentManager.onProgress(onProgress); agentManager.onProgress(onProgress);
const onComplete = (data) => { const onComplete = (data) => {
const pIdx = agentManager.progressCallbacks.indexOf(onProgress); cleanupCallbacks();
if (pIdx >= 0) agentManager.progressCallbacks.splice(pIdx, 1);
const cIdx = agentManager.completeCallbacks.indexOf(onComplete);
if (cIdx >= 0) agentManager.completeCallbacks.splice(cIdx, 1);
if (data.status === 'completed') { if (data.status === 'completed') {
progressUI.complete(data.summary || 'Enrich complete'); progressUI.complete(data.summary || 'Enrich complete');
showToast('toast.agent.enrichComplete', { summary: data.summary || 'Done' }, 'success'); showToast('toast.agent.enrichComplete', { summary: data.summary || 'Done' }, 'success');
} else if (data.status === 'error') {
state.loadingManager.hide();
showToast('toast.agent.enrichFailed', { error: data.error || 'Unknown error' }, 'error');
} }
}; };
agentManager.onComplete(onComplete); agentManager.onComplete(onComplete);
const onError = (data) => {
cleanupCallbacks();
state.loadingManager.hide();
showToast('toast.agent.enrichFailed', { error: data.error || 'Unknown error' }, 'error');
};
agentManager.onError(onError);
try { try {
await agentManager.executeSkill('enrich_hf_metadata', [filePath]); await agentManager.executeSkill('enrich_hf_metadata', [filePath]);
} catch (error) { } catch (error) {
const pIdx = agentManager.progressCallbacks.indexOf(onProgress); cleanupCallbacks();
if (pIdx >= 0) agentManager.progressCallbacks.splice(pIdx, 1);
const cIdx = agentManager.completeCallbacks.indexOf(onComplete);
if (cIdx >= 0) agentManager.completeCallbacks.splice(cIdx, 1);
state.loadingManager.hide(); state.loadingManager.hide();
showToast('toast.agent.enrichFailed', { error: error.message }, 'error'); showToast('toast.agent.enrichFailed', { error: error.message }, 'error');
} }