1212import { detectMention , parseSegments , plainText , textIndexBefore } from './mention.mjs' ;
1313import { bandSelection , BATCH_COUNTS , BATCH_MODES , cropRectAfterDrag , dragPositions , effectiveOption , fanOutParams , fitMediaBox , fitParamsToCaps , inputKindsFor , isControlTarget , kindOf , mentionCandidates as mentionCandidatesOf , modelSummary , NODE_GAP , placeOverlay , planBatchPlacement , planGridSplit , planPaste , portOfConnection , portState , pushAside , resolveEventTarget , roleOf , slotsOf as slotsOfForm , TILE_CARD , toolPrompt , upstreamNodes } from './tools.mjs' ;
1414import { cardChrome , DATA_KINDS , hasTextBox , isTypingTarget , modeLabelOf , modesFor , NODE_FORMS , NODE_KINDS , modeOf , modeRecordOf , modesOf , portKinds , portKindsFor , portList , ratioLabel , ratioOptionsFor , redrawBlocker , runModeOf , stripInputs } from './forms.mjs' ;
15- import { isBusyRefusal , runIsGone , waitForTheRun , withDeadline } from './run-watch.mjs' ;
16- import { applyAction , applyModel , inheritedTextFor , lineKind , linesDroppedByMode , materializeResults , planIncoming , planPort , promptTemplateFor , readTemplateFile , runSpecFor , syncMentions , templateFile , templateFromCanvas } from './actions.mjs' ;
15+ import { isBusyRefusal , panelClosed , panelIsShown , panelTasks , runIsGone , taskIsExpired , theRunThatIsGoing , theRunToWaitFor , waitForTheRun , withDeadline } from './run-watch.mjs' ;
16+ import { panelTitleRow } from './task-panel.mjs' ;
17+ import { applyAction , applyModel , canvasTitleAfterTemplate , inheritedTextFor , lineKind , linesDroppedByMode , materializeResults , planIncoming , planPort , promptTemplateFor , readTemplateFile , runSpecFor , syncMentions , templateFile , templateFromCanvas } from './actions.mjs' ;
1718
1819( ( ) => {
1920 'use strict' ;
@@ -101,6 +102,8 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
101102 nodeTasks : new Map ( ) , // nodeId -> taskId
102103 polling : new Set ( ) , // taskIds whose poll is still on its way
103104 pollTimer : null ,
105+ // The user put the progress panel away: it comes back when a run starts.
106+ taskPanelClosed : false ,
104107 actionTimer : null ,
105108 saveTimer : null ,
106109 historyTimer : null ,
@@ -161,6 +164,9 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
161164 const error = new Error ( payload ?. message || payload ?. error || `HTTP ${ response . status } ` ) ;
162165 error . code = payload ?. error || 'http_error' ;
163166 error . status = response . status ;
167+ // A refused run names the run that is going: the one that asked waits
168+ // for that run instead of standing still.
169+ if ( payload ?. runId ) error . runId = payload . runId ;
164170 if ( response . status === 401 ) {
165171 // The Host rotates the loopback credential when the runtime restarts,
166172 // and that happens on every plugin update. The page still holds the
@@ -3230,14 +3236,18 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
32303236 }
32313237 state . pendingPanel = false ;
32323238 const now = Date . now ( ) ;
3233- const tasks = [ ...state . tasks . values ( ) ] . filter (
3234- ( task ) => task . status === 'running' || task . status === 'waiting-question' || task . status === 'error'
3235- // A run that just finished stays a moment. Its log says how long it
3236- // waited for the machine and how long it ran, and that is the answer
3237- // to "why was this one slower?".
3238- || ( task . status === 'done' && now - ( task . finishedAt ?? 0 ) < 30_000 ) ,
3239- ) ;
3240- if ( tasks . length === 0 ) {
3239+ // The panel shows what is going on and what just finished. A run that is
3240+ // over stays a moment — a failed one too, so the reason can be read — and
3241+ // then it goes: the panel is not a history of everything that ever ran.
3242+ const tasks = panelTasks ( state . tasks . values ( ) , { now } ) ;
3243+ // A run that is over and out of the panel is let go: the table does not
3244+ // grow for ever.
3245+ for ( const [ id , task ] of state . tasks ) {
3246+ if ( taskIsExpired ( task , { now } ) ) state . tasks . delete ( id ) ;
3247+ }
3248+ // The panel sits over the canvas, so the user can put it away. It comes
3249+ // back when a run starts, because that is what the panel is for.
3250+ if ( ! panelIsShown ( { closed : state . taskPanelClosed , tasks } ) ) {
32413251 // The panel is hidden, but its old content is cleared as well: a hidden
32423252 // panel that still holds buttons is a stale control in the page.
32433253 panel . hidden = true ;
@@ -3246,7 +3256,13 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
32463256 }
32473257 panel . hidden = false ;
32483258 panel . textContent = '' ;
3249- panel . appendChild ( el ( 'div' , 'nxc-taskpanel__title' , '运行进度' ) ) ;
3259+ const title = panelTitleRow ( document , {
3260+ onPutAway : ( ) => {
3261+ state . taskPanelClosed = panelClosed ( { closed : state . taskPanelClosed , event : 'put-away' } ) ;
3262+ panel . hidden = true ;
3263+ } ,
3264+ } ) ;
3265+ panel . appendChild ( title ) ;
32503266 for ( const task of tasks ) {
32513267 const item = el ( 'div' , 'nxc-taskpanel__item' ) ;
32523268 const head = el ( 'div' , 'nxc-taskpanel__head' ) ;
@@ -3617,14 +3633,15 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
36173633 ) ;
36183634 if ( applied . error ) {
36193635 showBanner ( `模板没有铺上:${ applied . error } ` , 'error' ) ;
3620- return ;
3636+ return false ;
36213637 }
36223638 state . canvas . nodes = applied . canvas . nodes ;
36233639 state . canvas . connections = applied . canvas . connections ;
36243640 state . selection = [ ] ;
36253641 markDirty ( ) ;
36263642 render ( ) ;
36273643 showBanner ( `已铺上「${ template . title } 」,点节点上的「生成」开始` ) ;
3644+ return true ;
36283645 }
36293646
36303647 /**
@@ -3662,7 +3679,8 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
36623679 * The question is asked in the page: a native confirm() is a separate OS
36633680 * window that never reaches the screen inside the MiniApp host.
36643681 */
3665- async function deleteTemplate ( template ) { const yes = await confirmInPage ( `确定删掉模板「${ template . title } 」?画布本身不受影响。` ) ;
3682+ async function deleteTemplate ( template ) {
3683+ const yes = await confirmInPage ( `确定删掉模板「${ template . title } 」?画布本身不受影响,删掉就找不回来了。` ) ;
36663684 if ( ! yes ) return ;
36673685 try {
36683686 await api ( `/api/templates/${ encodeURIComponent ( template . id ) } ` , { method : 'DELETE' } ) ;
@@ -3920,23 +3938,26 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
39203938 }
39213939
39223940 /** Wait for the run that was started on this node to finish. */
3923- function waitForRun ( nodeId , timeoutMs = 2_700_000 ) {
3941+ function waitForRun ( nodeId , taskId = '' , timeoutMs = 2_700_000 ) {
39243942 // The run this wait is about is held on to. The page forgets which run
39253943 // belongs to a node as soon as that run is over — the node is free to run
39263944 // again — so a waiter that looked the run up again would never see it
39273945 // finish, and the chain would stand still until its own timeout.
3928- const taskId = state . nodeTasks . get ( nodeId ) ;
3946+ const id = theRunToWaitFor ( { taskId , nodeId , known : state . nodeTasks } ) ;
39293947 return waitForTheRun ( {
3930- taskId,
3931- readTask : ( id ) => state . tasks . get ( id ) ,
3948+ taskId : id ,
3949+ readTask : ( task ) => state . tasks . get ( task ) ,
39323950 timeoutMs,
39333951 } ) ;
39343952 }
39353953
39363954 /** Run a node and wait for its result. */
39373955 async function runAndWait ( node ) {
3938- await startRun ( node , { chain : false } ) ;
3939- const task = await waitForRun ( node . id ) ;
3956+ const started = await startRun ( node , { chain : false } ) ;
3957+ // The run that was started is the run that is waited for: the id is
3958+ // handed over, so a run that is over before the waiter looks it up is
3959+ // still waited for.
3960+ const task = await waitForRun ( node . id , started ?. id ?? '' ) ;
39403961 return task ?. status === 'done' ;
39413962 }
39423963
@@ -4009,6 +4030,9 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
40094030 const task = payload . run ;
40104031 state . tasks . set ( task . id , task ) ;
40114032 state . nodeTasks . set ( node . id , task . id ) ;
4033+ // A run is starting, so the progress panel is worth showing again: the
4034+ // user put it away, but this is what it is for.
4035+ state . taskPanelClosed = panelClosed ( { closed : state . taskPanelClosed , event : 'run-started' } ) ;
40124036 if ( task . sessionId ) node . metadata . sessionId = task . sessionId ;
40134037 ensurePolling ( ) ;
40144038 render ( ) ;
@@ -4024,6 +4048,14 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
40244048 showBanner ( String ( error ?. message ?? error ) ) ;
40254049 markDirty ( ) ;
40264050 render ( ) ;
4051+ // The runtime names the run that is going, so the chain can wait for
4052+ // it even when this page is the one that did not start it.
4053+ const running = theRunThatIsGoing ( error ) ;
4054+ if ( running ) {
4055+ state . nodeTasks . set ( node . id , running ) ;
4056+ ensurePolling ( ) ;
4057+ return { id : running , status : 'running' , busy : true } ;
4058+ }
40274059 return null ;
40284060 }
40294061 node . metadata . output . status = 'error' ;
@@ -4991,9 +5023,15 @@ import { applyAction, applyModel, inheritedTextFor, lineKind, linesDroppedByMode
49915023 const empty = Boolean ( state . canvas ?. id ) && ! ( state . canvas ?. nodes ?. length ) ;
49925024 if ( empty ) {
49935025 // The canvas that is open is empty: it becomes the canvas of this
4994- // template, and it takes the name of it.
4995- if ( template . title ) state . canvas . title = template . title ;
4996- applyTemplateOnCanvas ( template ) ;
5026+ // template. The name is given to it only when the steps are really on
5027+ // it, so a template that does not fit leaves no canvas with a name and
5028+ // no steps.
5029+ const applied = applyTemplateOnCanvas ( template ) ;
5030+ const title = canvasTitleAfterTemplate ( template , applied ) ;
5031+ if ( ! title ) return ;
5032+ state . canvas . title = title ;
5033+ markDirty ( ) ;
5034+ render ( ) ;
49975035 return ;
49985036 }
49995037 await createCanvas ( `${ template . title || '新画布' } ` , { askName : false } ) ;
0 commit comments