@andorra-labs/code 0.1.11 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/README.md +8 -2
  2. package/THIRD_PARTY_NOTICES.md +15 -0
  3. package/dist/src/agent-execution.js +15 -3
  4. package/dist/src/agent-mcp.js +5 -0
  5. package/dist/src/app-server/protocol.d.ts +8 -1
  6. package/dist/src/app-server/protocol.js +1 -1
  7. package/dist/src/app-server.d.ts +15 -0
  8. package/dist/src/app-server.js +89 -10
  9. package/dist/src/browser-panel-tool.d.ts +24 -3
  10. package/dist/src/browser-panel-tool.js +80 -6
  11. package/dist/src/browser-runner.js +3 -1
  12. package/dist/src/browser-view.d.ts +16 -0
  13. package/dist/src/browser-view.js +38 -2
  14. package/dist/src/claude-provider.js +15 -2
  15. package/dist/src/cli.d.ts +2 -1
  16. package/dist/src/cli.js +175 -43
  17. package/dist/src/code-intelligence/CodeIntelligenceManager.js +1 -1
  18. package/dist/src/codex-provider.js +6 -3
  19. package/dist/src/coding-tools.d.ts +2 -1
  20. package/dist/src/coding-tools.js +39 -31
  21. package/dist/src/completion.d.ts +43 -1
  22. package/dist/src/completion.js +145 -4
  23. package/dist/src/config.d.ts +1 -0
  24. package/dist/src/config.js +2 -0
  25. package/dist/src/decision/PreparedSteps.js +1 -1
  26. package/dist/src/delegation-changes.js +19 -2
  27. package/dist/src/diff-preview.d.ts +1 -0
  28. package/dist/src/diff-preview.js +12 -3
  29. package/dist/src/engine-streams.d.ts +22 -0
  30. package/dist/src/engine-streams.js +16 -0
  31. package/dist/src/extensions.js +17 -6
  32. package/dist/src/file-read-key.d.ts +8 -0
  33. package/dist/src/file-read-key.js +12 -0
  34. package/dist/src/graph-editor-session.js +2 -1
  35. package/dist/src/graph-health.d.ts +21 -0
  36. package/dist/src/graph-health.js +46 -0
  37. package/dist/src/graph-manager-chat.d.ts +52 -3
  38. package/dist/src/graph-manager-chat.js +50 -5
  39. package/dist/src/graph-runner.js +23 -3
  40. package/dist/src/graph-server.d.ts +1 -1
  41. package/dist/src/graph-server.js +39 -18
  42. package/dist/src/graph-ui/app.js +100 -30
  43. package/dist/src/graph-ui/controls.css +52 -0
  44. package/dist/src/graph-ui/icons.js +3 -0
  45. package/dist/src/graph-ui/index.html +2 -2
  46. package/dist/src/graph-ui/manager-state.js +141 -3
  47. package/dist/src/graph-ui/manager.css +2 -2
  48. package/dist/src/graph-ui/manager.js +328 -66
  49. package/dist/src/graph-ui/model-picker.js +279 -0
  50. package/dist/src/graph-ui/provider-glyphs/alibaba-cloud.png +0 -0
  51. package/dist/src/graph-ui/provider-glyphs/anthropic.png +0 -0
  52. package/dist/src/graph-ui/provider-glyphs/aws-bedrock.png +0 -0
  53. package/dist/src/graph-ui/provider-glyphs/claude.png +0 -0
  54. package/dist/src/graph-ui/provider-glyphs/deepseek.png +0 -0
  55. package/dist/src/graph-ui/provider-glyphs/fireworks.png +0 -0
  56. package/dist/src/graph-ui/provider-glyphs/kimi.png +0 -0
  57. package/dist/src/graph-ui/provider-glyphs/minimax.png +0 -0
  58. package/dist/src/graph-ui/provider-glyphs/openai.png +0 -0
  59. package/dist/src/graph-ui/provider-glyphs/opencode.png +0 -0
  60. package/dist/src/graph-ui/provider-glyphs/openrouter.png +0 -0
  61. package/dist/src/graph-ui/provider-glyphs/xai.png +0 -0
  62. package/dist/src/graph-ui/provider-glyphs/xiaomi-mimo.png +0 -0
  63. package/dist/src/graph-ui/provider-glyphs/zai.png +0 -0
  64. package/dist/src/graphs.d.ts +2 -0
  65. package/dist/src/graphs.js +7 -7
  66. package/dist/src/mcp.d.ts +52 -14
  67. package/dist/src/mcp.js +285 -128
  68. package/dist/src/memory/ProjectMemory.d.ts +77 -0
  69. package/dist/src/memory/ProjectMemory.js +310 -0
  70. package/dist/src/memory/index.d.ts +1 -0
  71. package/dist/src/memory/index.js +1 -0
  72. package/dist/src/opencode-provider.js +5 -6
  73. package/dist/src/package-name.d.ts +3 -0
  74. package/dist/src/package-name.js +3 -0
  75. package/dist/src/provider-engine.js +2 -2
  76. package/dist/src/qa/runtime/PtyManager.js +2 -1
  77. package/dist/src/react-runtime.js +16 -8
  78. package/dist/src/remote/cert.d.ts +1 -1
  79. package/dist/src/remote/cert.js +2 -7
  80. package/dist/src/remote/client.js +1 -1
  81. package/dist/src/remote/pem.d.ts +6 -0
  82. package/dist/src/remote/pem.js +11 -0
  83. package/dist/src/run-records.js +49 -10
  84. package/dist/src/runtime.d.ts +2 -0
  85. package/dist/src/sessions.d.ts +3 -1
  86. package/dist/src/sessions.js +5 -3
  87. package/dist/src/sign-in-detection.d.ts +82 -0
  88. package/dist/src/sign-in-detection.js +197 -0
  89. package/dist/src/sign-in-offer.d.ts +35 -0
  90. package/dist/src/sign-in-offer.js +116 -0
  91. package/dist/src/skills/bundled/graph-manager/SKILL.md +22 -4
  92. package/dist/src/skills/bundled/provenance.json +1 -1
  93. package/dist/src/skills/runtime-skills.d.ts +6 -0
  94. package/dist/src/skills/runtime-skills.js +13 -0
  95. package/dist/src/stray-files.d.ts +14 -0
  96. package/dist/src/stray-files.js +81 -0
  97. package/dist/src/subagent-run.js +6 -1
  98. package/dist/src/terminal-ui.d.ts +1 -1
  99. package/dist/src/terminal-ui.js +9 -3
  100. package/dist/src/tool-activity.js +1 -1
  101. package/dist/src/tool-schema-compatibility.d.ts +7 -2
  102. package/dist/src/tool-schema-compatibility.js +8 -4
  103. package/dist/src/transcript.d.ts +5 -0
  104. package/dist/src/transcript.js +11 -3
  105. package/dist/src/types.d.ts +2 -0
  106. package/dist/src/untracked-file-removal.d.ts +3 -0
  107. package/dist/src/untracked-file-removal.js +125 -0
  108. package/dist/src/update-check.js +1 -1
  109. package/dist/src/update.d.ts +11 -1
  110. package/dist/src/update.js +27 -5
  111. package/dist/src/workspace-git-metadata.d.ts +15 -0
  112. package/dist/src/workspace-git-metadata.js +50 -1
  113. package/dist/src/workspace-instructions.js +1 -1
  114. package/package.json +1 -1
@@ -6,7 +6,7 @@ globalThis.AndorraManager = (() => {
6
6
  const byId = id => document.getElementById(id);
7
7
  const button = byId('graph-manager'), tip = byId('manager-tip'), panel = byId('manager-panel');
8
8
  if (!logic || !button || !tip || !panel) return undefined;
9
- const providerSelect = byId('manager-provider'), modelSelect = byId('manager-model');
9
+ const modelsSlot = byId('manager-models');
10
10
  const notices = byId('manager-notices'), log = byId('manager-transcript'), input = byId('manager-input');
11
11
  const sendButton = byId('manager-send'), stopButton = byId('manager-stop'), runLabel = byId('manager-run-state');
12
12
 
@@ -23,9 +23,11 @@ globalThis.AndorraManager = (() => {
23
23
  /** The engine's run state, read from `state` events and each snapshot; the transcript does not keep it. */
24
24
  let runState = 'idle';
25
25
  let source = null, retryTimer = null, retryMs = 1000, connected = false;
26
+ /** The graph whose conversation the stream follows. Each graph has its own; switching graphs switches it. */
27
+ let streamGraphId = null;
26
28
  /** The canvas of the latest message the chat took, as JSON; what the agent's save is compared against. */
27
29
  let sentCanvas = null;
28
- /** The graph the conversation is about, once it has a message. */
30
+ /** The graph the conversation is about: the open one, since every graph has its own conversation. */
29
31
  let chatGraphId = null;
30
32
  /** Messages posted and not yet echoed by the stream, oldest first: { text, canvas, graphId }. */
31
33
  let posted = [];
@@ -33,6 +35,36 @@ globalThis.AndorraManager = (() => {
33
35
  let beforeLastMessage = null;
34
36
  /** A message sent into a working run changed the canvas: the agent's next save may predate it, so it is offered. */
35
37
  let unsureCanvas = false;
38
+ /**
39
+ * What a live draft change is merged against, so edits the user made since win: the canvas of the
40
+ * latest message the chat accepted (JSON), updated by the agent's own version of each node it then
41
+ * changed and of each graph field it patched. A newly accepted message rebases all of it: what it
42
+ * carried is shared with the agent from then on, so the agent's next change of it is taken.
43
+ */
44
+ let mergeCanvas = null;
45
+ /** The agent's latest version of each connection it set, as JSON, like `agentNodes`. */
46
+ const agentEdges = new Map();
47
+ /** The latest user message this panel has seen in its conversation, to tell a reconnect from a conversation that moved on. */
48
+ let latestUserSeen = null;
49
+ const agentNodes = new Map();
50
+ let agentFields = {};
51
+ function rebase(canvas) {
52
+ mergeCanvas = canvas;
53
+ agentNodes.clear(); agentEdges.clear(); agentFields = {};
54
+ }
55
+ /** The merge state before the latest accepted message, restored if the chat refuses that message. */
56
+ let mergeBeforeMessage = null;
57
+ function mergeState() {
58
+ return { mergeCanvas, nodes: new Map(agentNodes), edges: new Map(agentEdges), fields: { ...agentFields } };
59
+ }
60
+ function restoreMerge(saved) {
61
+ mergeCanvas = saved.mergeCanvas;
62
+ agentNodes.clear(); for (const [key, value] of saved.nodes) agentNodes.set(key, value);
63
+ agentEdges.clear(); for (const [key, value] of saved.edges) agentEdges.set(key, value);
64
+ agentFields = { ...saved.fields };
65
+ }
66
+ /** Draft changes that arrived while the user was typing in the editor, applied once they leave the field. */
67
+ let deferredEdits = [];
36
68
  let shownGraphId = null;
37
69
  let choice;
38
70
  let providersSeen = '';
@@ -40,8 +72,6 @@ globalThis.AndorraManager = (() => {
40
72
  let offeredRevision = null;
41
73
  /** A graph the agent created beside the bundled preset, which it cannot change: { id, label }. Opened only on click. */
42
74
  let createdOffer = null;
43
- /** The graph the user moved to while a conversation about another one is open. */
44
- let switchOffer = null;
45
75
  let localError = '';
46
76
  /** What this panel answered each approval with, to label the closed card. */
47
77
  const decisions = new Map();
@@ -75,14 +105,28 @@ globalThis.AndorraManager = (() => {
75
105
  if (graph.id !== shownGraphId) {
76
106
  shownGraphId = graph.id;
77
107
  hideTip();
78
- offeredRevision = null;
79
- createdOffer = null;
80
- switchOffer = chatGraphId && chatGraphId !== graph.id && transcript.length ? graph.id : null;
81
- renderNotices();
108
+ switchConversation(graph.id);
82
109
  }
83
110
  if (open) fillModels();
84
111
  }
85
112
 
113
+ /**
114
+ * Another graph is open: the panel leaves the last graph's conversation, which carries on without
115
+ * it, and follows this graph's own. Nothing of one graph's chat is shown beside another.
116
+ */
117
+ function switchConversation(graphId) {
118
+ disconnect();
119
+ streamGraphId = graphId;
120
+ chatGraphId = graphId;
121
+ transcript = []; runState = 'idle';
122
+ posted = []; sentCanvas = null; beforeLastMessage = null; unsureCanvas = false;
123
+ offeredRevision = null; createdOffer = null; localError = '';
124
+ decisions.clear(); answering.clear(); openRows.clear();
125
+ rebase(null); latestUserSeen = null; mergeBeforeMessage = null; deferredEdits = [];
126
+ rendered = [];
127
+ if (open) { connect(); render(); }
128
+ }
129
+
86
130
  function graphOpened(reason) {
87
131
  heading();
88
132
  if (!open && state.graph && logic.shouldShowTip(storage(), reason)) tip.hidden = false;
@@ -118,47 +162,44 @@ globalThis.AndorraManager = (() => {
118
162
  const modelsOf = provider => [...new Set([provider.model, ...(provider.models || [])].filter(Boolean))];
119
163
  const providerOf = id => state.providers.find(entry => entry.id === id);
120
164
 
121
- function option(value, label) { const item = node('option', label); item.value = value; return item; }
122
-
123
- /** Fills both selects from the editor's provider registry, keeping the choice while it is still offered. */
165
+ const entryOf = provider => ({ id: provider.id, label: titleFor(provider), type: provider.type, baseUrl: provider.baseUrl });
166
+ // The chat's model picker: every configured provider's models grouped under its name. Graph
167
+ // Manager always runs a named pair, so there is no default row.
168
+ const picker = typeof modelPickerButton === 'function' ? modelPickerButton({
169
+ ariaLabel: 'Graph Manager model',
170
+ current: () => {
171
+ const provider = choice && providerOf(choice.provider);
172
+ return { model: choice?.model || 'No model available', provider: provider ? titleFor(provider) : '', entry: provider ? entryOf(provider) : undefined };
173
+ },
174
+ groups: () => state.providers.map(provider => ({ provider: entryOf(provider), models: modelsOf(provider).map(model => ({ model, note: '' })) })),
175
+ selected: () => choice,
176
+ pick: next => {
177
+ if (!providerOf(next.provider) || !next.model) return;
178
+ choice = { provider: next.provider, model: next.model };
179
+ logic.rememberModel(storage(), choice);
180
+ picker.redraw();
181
+ },
182
+ }) : null;
183
+ if (picker) { picker.id = 'manager-model-picker'; modelsSlot?.append(picker); }
184
+
185
+ /** Fills the picker from the editor's provider registry, keeping the choice while it is still offered. */
124
186
  function fillModels() {
125
187
  const signature = JSON.stringify(state.providers);
126
188
  if (signature === providersSeen && choice) return;
127
189
  providersSeen = signature;
128
190
  const kept = choice && modelsOf(providerOf(choice.provider) || {}).includes(choice.model);
129
191
  if (!kept) choice = logic.initialModel(storage(), state.graph, state.providers);
130
- providerSelect.replaceChildren(...state.providers.map(provider => option(provider.id, titleFor(provider))));
131
- providerSelect.disabled = modelSelect.disabled = !choice;
132
- if (choice) providerSelect.value = choice.provider;
133
- fillModelOptions();
192
+ if (picker) { picker.disabled = !choice; picker.redraw(); globalThis.AndorraModelPicker?.refresh?.(picker); }
134
193
  renderNotices();
135
194
  }
136
- function fillModelOptions() {
137
- modelSelect.replaceChildren(...(choice ? modelsOf(providerOf(choice.provider) || {}) : []).map(model => option(model, model)));
138
- if (choice) modelSelect.value = choice.model;
139
- for (const select of [providerSelect, modelSelect]) globalThis.AndorraControls?.refreshSelect?.(select);
140
- }
141
- providerSelect.onchange = () => {
142
- const provider = providerOf(providerSelect.value);
143
- const model = provider && modelsOf(provider)[0];
144
- if (!model) return;
145
- choice = { provider: provider.id, model };
146
- logic.rememberModel(storage(), choice);
147
- fillModelOptions();
148
- };
149
- modelSelect.onchange = () => {
150
- if (!choice || !modelSelect.value) return;
151
- choice = { provider: choice.provider, model: modelSelect.value };
152
- logic.rememberModel(storage(), choice);
153
- };
154
195
 
155
196
  // ------------------------------------------------------------------ Stream
156
197
 
157
198
  function connect() {
158
- if (source || !open || !token || typeof EventSource !== 'function') return;
199
+ if (source || !open || !token || !streamGraphId || typeof EventSource !== 'function') return;
159
200
  clearTimeout(retryTimer);
160
201
  // EventSource cannot send an Authorization header, so the stream takes the session token in its query.
161
- const stream = source = new EventSource(`/api/manager/stream?token=${encodeURIComponent(token)}`);
202
+ const stream = source = new EventSource(`/api/manager/stream?token=${encodeURIComponent(token)}&graph=${encodeURIComponent(streamGraphId)}`);
162
203
  stream.onopen = () => { if (source !== stream) return; connected = true; retryMs = 1000; renderRunState(); };
163
204
  stream.onmessage = message => {
164
205
  if (source !== stream) return;
@@ -195,11 +236,15 @@ globalThis.AndorraManager = (() => {
195
236
  const latest = transcript.filter(entry => entry.type === 'user').pop();
196
237
  const own = latest && posted.find(entry => entry.text === latest.text && entry.whileIdle);
197
238
  sentCanvas = own ? own.canvas : null;
239
+ // The merge base outlives a reconnect (the panel closed and reopened) when no message came in
240
+ // meanwhile: what the agent and the user shared is still what they share. Otherwise only this
241
+ // panel's own latest message says what was shared; with none, nothing is known.
242
+ if (!latest) rebase(null);
243
+ else if (latest.text !== latestUserSeen) rebase(own ? own.canvas : null);
244
+ latestUserSeen = latest ? latest.text : null;
198
245
  if (own) chatGraphId = own.graphId;
199
246
  posted = []; beforeLastMessage = null; unsureCanvas = false;
200
- if (!transcript.length) { chatGraphId = null; switchOffer = null; createdOffer = null; decisions.clear(); }
201
- // A conversation found already under way (a reload, another window) is taken to be about the open graph.
202
- else if (!chatGraphId) chatGraphId = state.graph?.id ?? null;
247
+ if (!transcript.length) { createdOffer = null; decisions.clear(); }
203
248
  // A graph deleted while this panel was not listening leaves the list all the same, and
204
249
  // nothing of it is offered: its latest revision is the deletion.
205
250
  const deleted = logic.deletedGraphs(transcript);
@@ -214,19 +259,24 @@ globalThis.AndorraManager = (() => {
214
259
  const created = transcript.map(entry => logic.createdGraph(state.graph?.id, entry)).filter(Boolean).pop();
215
260
  if (created && !deleted.has(created.id)) createdOffer = created;
216
261
  } else if (event.type === 'user') {
262
+ latestUserSeen = event.text;
217
263
  accepted(event.text);
218
264
  } else if (event.type === 'error') {
219
- refused();
265
+ // A warning follows a message that was delivered; only a real error refuses one.
266
+ if (!event.warning) refused();
220
267
  } else if (event.type === 'state') {
221
268
  runState = event.state;
222
269
  // The run is over: its message was read or no longer matters, and no save follows without a new one.
223
- if (runState === 'idle') { beforeLastMessage = null; unsureCanvas = false; }
270
+ if (runState === 'idle') { beforeLastMessage = null; unsureCanvas = false; mergeBeforeMessage = null; }
224
271
  } else if (event.type === 'resolved') {
225
272
  answering.delete(event.id);
226
273
  } else if (event.type === 'graph.saved') {
227
274
  void graphSaved(event);
228
275
  } else if (event.type === 'graph.deleted') {
229
276
  graphDeleted(event);
277
+ } else if (event.type === 'graph.edit') {
278
+ liveEdit(event);
279
+ return;
230
280
  }
231
281
  render();
232
282
  }
@@ -243,22 +293,30 @@ globalThis.AndorraManager = (() => {
243
293
  // agent now works from a canvas this panel never saw, so nothing here is known to be what it
244
294
  // has. A save that follows is offered, never loaded over what is on this canvas.
245
295
  sentCanvas = null; beforeLastMessage = null;
296
+ rebase(null);
246
297
  return;
247
298
  }
248
299
  const [message] = posted.splice(index, 1);
249
300
  if (runState !== 'idle') {
301
+ // A steered message is read only at the run's next turn, and a draft change already under way
302
+ // can land before then, made without it. The merge base stays what the agent had, so what the
303
+ // user changed for this message stays theirs until a message sent while idle shares it.
250
304
  // Nothing on the stream says whether a working run read this message before its next save.
251
305
  if (message.canvas !== sentCanvas) unsureCanvas = true;
252
306
  chatGraphId = message.graphId;
253
307
  return;
254
308
  }
255
309
  beforeLastMessage = { sentCanvas, chatGraphId };
310
+ mergeBeforeMessage = mergeState();
311
+ rebase(message.canvas);
256
312
  sentCanvas = message.canvas; chatGraphId = message.graphId;
257
313
  }
258
314
  /** The chat reported an error: a message still open to refusal is taken as refused, which only ever makes a save an offer. */
259
315
  function refused() {
260
316
  if (beforeLastMessage) ({ sentCanvas, chatGraphId } = beforeLastMessage);
261
- beforeLastMessage = null; unsureCanvas = false;
317
+ // The refused message's canvas was never shared with the agent, so what was shared before it is the base again.
318
+ if (mergeBeforeMessage) restoreMerge(mergeBeforeMessage);
319
+ beforeLastMessage = null; unsureCanvas = false; mergeBeforeMessage = null;
262
320
  }
263
321
 
264
322
  /**
@@ -312,6 +370,160 @@ globalThis.AndorraManager = (() => {
312
370
  offeredRevision = event.revision;
313
371
  renderNotices();
314
372
  }
373
+ /** The version of a node, or of the graph's fields, the agent and the user last shared, or undefined. */
374
+ /**
375
+ * `sharedNode` and `sharedEdge` answer with the version both last shared, `null` when the agent
376
+ * removed it (so a later re-add of the same id is the agent's, not a user deletion), or undefined
377
+ * when nothing is known.
378
+ */
379
+ function sharedNode(id) {
380
+ if (agentNodes.has(id)) return JSON.parse(agentNodes.get(id));
381
+ try { return mergeCanvas ? JSON.parse(mergeCanvas).nodes?.[id] : undefined; } catch { return undefined; }
382
+ }
383
+ function sharedEdge(id) {
384
+ if (agentEdges.has(id)) return JSON.parse(agentEdges.get(id));
385
+ try { return mergeCanvas ? (JSON.parse(mergeCanvas).edges || []).find(edge => edge && edge.id === id) : undefined; } catch { return undefined; }
386
+ }
387
+ function sharedFields(keys) {
388
+ let sent = {};
389
+ try { sent = mergeCanvas ? JSON.parse(mergeCanvas) : {}; } catch { /* Nothing shared is known. */ }
390
+ return Object.fromEntries(keys.map(key => [key, Object.hasOwn(agentFields, key) ? JSON.parse(agentFields[key]) : sent[key]]));
391
+ }
392
+
393
+ /**
394
+ * The agent changed its draft of the open graph: the canvas shows the change at once, as an unsaved
395
+ * edit the user can undo. Nothing is saved: the draft is saved only when the user approves its
396
+ * publish, and that save then opens over these edits.
397
+ *
398
+ * A change to a node or to graph fields is merged field by field: what the user changed since the
399
+ * agent last saw it stays the user's. When the canvas was the one the agent works from and nothing
400
+ * of the user's was kept, it still is after the edit, so the approved save loads without asking;
401
+ * otherwise that save is offered, never loaded over the user's edits.
402
+ */
403
+ function liveEdit(event) {
404
+ if (!state.graph || event.graphId !== state.graph.id || !logic.soundEdit(event.edit)) return;
405
+ // A field being typed in holds objects of the graph and commits into them when it is left. The
406
+ // change waits until then and is applied with the inspector drawn afresh, so no control is left
407
+ // bound to an object the change replaced or moved.
408
+ if (typingInEditor()) {
409
+ deferredEdits.push(event);
410
+ status('Graph Manager changed the draft; it shows when you leave this field.');
411
+ return;
412
+ }
413
+ applyLive(event);
414
+ }
415
+ function flushDeferred() {
416
+ if (!deferredEdits.length || typingInEditor()) return;
417
+ for (const event of deferredEdits.splice(0)) applyLive(event);
418
+ }
419
+ // `change` commits a field before focus leaves it; the deferred changes follow once it has.
420
+ document.addEventListener('focusout', () => { setTimeout(flushDeferred, 0); });
421
+
422
+ function applyLive(event) {
423
+ const graph = state.graph;
424
+ if (!graph || event.graphId !== graph.id) return;
425
+ let edit = event.edit, kept = [];
426
+ if (edit.kind === 'node' && !graph.nodes?.[edit.node.id] && sharedNode(edit.node.id)) {
427
+ // The card was shared with the agent and is gone from the canvas: the user deleted it, and the
428
+ // agent's change does not bring it back. A card the agent adds is one nothing shared before.
429
+ edit = null;
430
+ kept = ['node'];
431
+ } else if (edit.kind === 'edge' && !(graph.edges || []).some(item => item && item.id === edit.edge.id) && sharedEdge(edit.edge.id)) {
432
+ edit = null;
433
+ kept = ['edge'];
434
+ } else if (edit.kind === 'node' && graph.nodes?.[edit.node.id]) {
435
+ const base = sharedNode(edit.node.id);
436
+ if (!base) {
437
+ // Nothing says what the user and the agent last shared of this card, so a field that differs
438
+ // may be the user's own edit: the card stays as it is, and the approved save is offered.
439
+ edit = null;
440
+ kept = ['node'];
441
+ } else {
442
+ const result = logic.mergeFields(base, graph.nodes[edit.node.id], edit.node);
443
+ kept = result.kept;
444
+ edit = { kind: 'node', node: { ...result.merged, id: edit.node.id } };
445
+ }
446
+ } else if (edit.kind === 'edge' && (graph.edges || []).some(item => item && item.id === edit.edge.id)) {
447
+ const base = sharedEdge(edit.edge.id);
448
+ if (!base) {
449
+ edit = null;
450
+ kept = ['edge'];
451
+ } else {
452
+ const result = logic.mergeFields(base, graph.edges.find(item => item && item.id === edit.edge.id), edit.edge);
453
+ kept = result.kept;
454
+ edit = { kind: 'edge', edge: { ...result.merged, id: edit.edge.id } };
455
+ }
456
+ } else if (edit.kind === 'node.remove' && graph.nodes?.[edit.nodeId]) {
457
+ // A card the user changed, or connected anew, since the agent last saw it stays: removing it
458
+ // would take the user's work with it. The approved save is offered instead.
459
+ const id = edit.nodeId;
460
+ const base = sharedNode(id);
461
+ const sameEdge = item => { const shared = sharedEdge(item.id); return !!shared && JSON.stringify(shared) === JSON.stringify(item); };
462
+ if (!base || JSON.stringify(base) !== JSON.stringify(graph.nodes[id])
463
+ || (graph.edges || []).some(item => item && (item.source === id || item.target === id) && !sameEdge(item))) {
464
+ edit = null;
465
+ kept = ['node'];
466
+ }
467
+ } else if (edit.kind === 'edge.remove' && (graph.edges || []).some(item => item && item.id === edit.edgeId)) {
468
+ const base = sharedEdge(edit.edgeId);
469
+ if (!base || JSON.stringify(base) !== JSON.stringify(graph.edges.find(item => item && item.id === edit.edgeId))) {
470
+ edit = null;
471
+ kept = ['edge'];
472
+ }
473
+ } else if (edit.kind === 'graph') {
474
+ const keys = Object.keys(edit.patch);
475
+ const current = Object.fromEntries(keys.map(key => [key, graph[key]]));
476
+ const result = logic.mergeFields(sharedFields(keys), current, edit.patch, keys);
477
+ kept = result.kept;
478
+ const patch = Object.fromEntries(keys.filter(key => result.merged[key] !== undefined).map(key => [key, result.merged[key]]));
479
+ edit = Object.keys(patch).length ? { kind: 'graph', patch } : null;
480
+ }
481
+ // The agent's own version is the base for its next change, whatever the canvas kept.
482
+ if (event.edit.kind === 'node') agentNodes.set(event.edit.node.id, JSON.stringify(event.edit.node));
483
+ if (event.edit.kind === 'node.remove') {
484
+ // The removal is remembered as such, with the connections it took in the agent's draft.
485
+ const id = event.edit.nodeId;
486
+ // The connections as the agent's draft has them now: the shared ones, with each it has since
487
+ // set, rerouted or removed laid over them. Only those still touching the card went with it.
488
+ const current = new Map();
489
+ try { for (const item of (mergeCanvas ? JSON.parse(mergeCanvas).edges || [] : [])) if (item && typeof item.id === 'string') current.set(item.id, item); } catch { /* Nothing shared is known. */ }
490
+ for (const [edgeId, json] of agentEdges) { const item = JSON.parse(json); if (item) current.set(edgeId, item); else current.delete(edgeId); }
491
+ for (const [edgeId, item] of current) if (item.source === id || item.target === id) agentEdges.set(edgeId, 'null');
492
+ agentNodes.set(id, 'null');
493
+ }
494
+ if (event.edit.kind === 'edge') agentEdges.set(event.edit.edge.id, JSON.stringify(event.edit.edge));
495
+ if (event.edit.kind === 'edge.remove') agentEdges.set(event.edit.edgeId, 'null');
496
+ if (event.edit.kind === 'graph') for (const [key, value] of Object.entries(event.edit.patch)) agentFields[key] = JSON.stringify(value);
497
+ if (!edit) {
498
+ if (kept.length) { sentCanvas = null; status('Graph Manager changed the draft; your own edits were kept. Approve its save, then load it to see its changes.'); }
499
+ return;
500
+ }
501
+ const trial = JSON.parse(JSON.stringify(graph));
502
+ if (!logic.applyEdit(trial, edit) || JSON.stringify(trial) === JSON.stringify(graph)) return;
503
+ const followed = JSON.stringify(graph) === sentCanvas && !kept.length;
504
+ try {
505
+ mutate(() => {
506
+ logic.applyEdit(state.graph, edit);
507
+ if (edit.kind === 'node.remove') {
508
+ state.selectedIds = state.selectedIds.filter(id => id !== edit.nodeId);
509
+ if (state.selected === edit.nodeId) state.selected = null;
510
+ }
511
+ if (edit.kind === 'edge.remove' && state.selectedEdge === edit.edgeId) state.selectedEdge = null;
512
+ }, true);
513
+ } catch {
514
+ // A change the editor cannot draw is taken back rather than left to break every redraw. The
515
+ // whole editor is redrawn (the window's `render`, not this panel's), so the canvas and the
516
+ // inspector are bound to the restored graph.
517
+ if (state.undo.length) {
518
+ state.graph = state.undo.pop();
519
+ try { refreshDirty(); globalThis.render(); } catch { /* The restored graph drew before this change. */ }
520
+ }
521
+ status('Graph Manager made a change the editor cannot show; it was left out.', true);
522
+ return;
523
+ }
524
+ if (followed) sentCanvas = JSON.stringify(state.graph);
525
+ status(kept.length ? 'Graph Manager changed the draft; your own edits were kept. Approve its save to keep its changes.' : 'Graph Manager changed the draft. Approve its save to keep it.');
526
+ }
315
527
  /**
316
528
  * The agent deleted a graph, which publishes a revision marked as deleted. It leaves the list. When
317
529
  * it is the open graph that revision is never loaded or offered: the canvas stays as it is, still
@@ -329,12 +541,11 @@ globalThis.AndorraManager = (() => {
329
541
  }
330
542
  /** Opens the graph the agent created. Unsaved edits on the canvas are asked about first, as for any other graph. */
331
543
  async function openCreated(offer) {
332
- const before = chatGraphId;
333
- // The conversation is about the graph it created from here on, so opening it offers no new chat.
334
- if (transcript.length) chatGraphId = offer.id;
335
- if (!await openGraph(offer.id) || state.graph?.id !== offer.id) { chatGraphId = before; renderNotices(); return; }
544
+ // The new graph has a conversation of its own, which the panel follows once it is open.
545
+ if (!await openGraph(offer.id) || state.graph?.id !== offer.id) { renderNotices(); return; }
336
546
  // The canvas is the agent's own save: a further save in the same run replaces it freely.
337
547
  sentCanvas = JSON.stringify(state.graph);
548
+ rebase(sentCanvas);
338
549
  beforeLastMessage = null; unsureCanvas = false;
339
550
  createdOffer = null;
340
551
  renderNotices();
@@ -344,6 +555,7 @@ globalThis.AndorraManager = (() => {
344
555
  if (!await openGraph(id, { discard: true, ...(fetched ? { fetched } : {}) }) || state.graph?.id !== id) return false;
345
556
  // The canvas is now the agent's own save: a further save in the same run replaces it as freely.
346
557
  sentCanvas = JSON.stringify(state.graph);
558
+ rebase(sentCanvas);
347
559
  beforeLastMessage = null; unsureCanvas = false;
348
560
  offeredRevision = null;
349
561
  status(`Graph Manager saved version ${state.graph.revision}`);
@@ -370,7 +582,6 @@ globalThis.AndorraManager = (() => {
370
582
  // A message sent while idle starts its own run; a snapshot may stand in for its echo.
371
583
  message.whileIdle = runState === 'idle';
372
584
  posted.push(message);
373
- switchOffer = null;
374
585
  input.value = '';
375
586
  const sent = await post('/api/manager/message', { provider: choice.provider, model: choice.model, text, graph: JSON.parse(message.canvas), saved: revision > 0, revision });
376
587
  if (sent) return;
@@ -389,8 +600,7 @@ globalThis.AndorraManager = (() => {
389
600
  }
390
601
 
391
602
  async function newChat() {
392
- switchOffer = null;
393
- await post('/api/manager/reset');
603
+ await post('/api/manager/reset', { graphId: streamGraphId });
394
604
  }
395
605
 
396
606
  // ------------------------------------------------------------------ Drawing
@@ -487,17 +697,67 @@ globalThis.AndorraManager = (() => {
487
697
  }
488
698
  const workRow = entry => entry.type === 'thought' ? thoughtRow(entry) : toolRow(entry);
489
699
 
700
+ /**
701
+ * A streaming thought whose only change is more words: its preview and body take the new text
702
+ * where they are. Rebuilding the row instead (every ~80ms while a model thinks) swapped the
703
+ * element out between a press and its release, so the click that opens it never landed.
704
+ * False when anything else changed, and the row is drawn again.
705
+ */
706
+ function patchThought(old, entry) {
707
+ if (old.entry.type !== 'thought' || entry.type !== 'thought' || old.entry.status !== 'thinking' || entry.status !== 'thinking'
708
+ || old.entry.startedAt !== entry.startedAt || old.entry.truncated !== entry.truncated) return false;
709
+ const head = old.element.querySelector(':scope > .manager-fold-head');
710
+ const body = old.element.querySelector('.manager-activity-text');
711
+ if (!head || !body) return false;
712
+ const preview = lastLine(entry.text);
713
+ let tail = head.querySelector('.manager-preview-tail');
714
+ if (preview && !tail) {
715
+ tail = span(undefined, 'manager-activity-preview manager-preview-tail');
716
+ tail.append(span(preview));
717
+ head.insertBefore(tail, head.querySelector('[data-since]'));
718
+ } else if (tail) {
719
+ if (preview) tail.firstChild.textContent = preview; else tail.remove();
720
+ }
721
+ // An open thought keeps following its newest words only while the reader is at its end.
722
+ const following = body.scrollHeight - body.scrollTop - body.clientHeight < 24;
723
+ body.textContent = entry.text || 'Still thinking; nothing has streamed yet.';
724
+ if (following) body.scrollTop = body.scrollHeight;
725
+ return true;
726
+ }
727
+
728
+ /**
729
+ * The stretch that is still running, reconciled row by row against what it drew last time: a row
730
+ * whose entry is unchanged stays the same element, a streaming thought is patched in place, and
731
+ * only a row that really changed (a call settling) is drawn again. Keeping the elements is what
732
+ * lets a click on any row land while the stretch streams.
733
+ */
734
+ function liveBlock(block, before) {
735
+ const container = before?.element ?? node('div', undefined, 'manager-work-live');
736
+ const rows = new Map();
737
+ let previous = null;
738
+ for (const entry of block.entries) {
739
+ const old = before?.rows?.get(entry.id);
740
+ let element;
741
+ if (old && (old.entry === entry || patchThought(old, entry))) element = old.element;
742
+ else {
743
+ element = workRow(entry);
744
+ if (old) old.element.replaceWith(element);
745
+ }
746
+ // A new row goes after the one before it; a kept row is not moved.
747
+ if (element.parentNode !== container) { if (previous) previous.after(element); else container.prepend(element); }
748
+ rows.set(entry.id, { entry, element });
749
+ previous = element;
750
+ }
751
+ for (const [id, old] of before?.rows ?? []) if (!rows.has(id)) old.element.remove();
752
+ return { element: container, rows };
753
+ }
754
+
490
755
  /**
491
756
  * A stretch of thinking and calls between messages. While it runs every row shows; once it
492
757
  * settles it condenses to the one line the app's turn shows (`Thought for 8s, made 4 edits`), with
493
758
  * its wall time at the edge, and opens back to the same rows.
494
759
  */
495
760
  function workBlock(block) {
496
- if (!block.settled) {
497
- const live = node('div', undefined, 'manager-work-live');
498
- live.append(...block.entries.map(workRow));
499
- return live;
500
- }
501
761
  const failed = block.entries.filter(entry => entry.type === 'tool' && entry.status === 'failed').length;
502
762
  const label = logic.workLabel(block.entries);
503
763
  const duration = logic.workDuration(block.entries);
@@ -557,14 +817,14 @@ globalThis.AndorraManager = (() => {
557
817
  const item = card(entry, 'Graph Manager asks to', entry.text);
558
818
  if (entry.answered) return closed(item, entry, 'Answered');
559
819
  return actions(item, entry, [
560
- control('Approve', () => settle(entry, '/api/manager/approve', { id: entry.id, approved: true }, 'Approved'), 'btn-small primary'),
561
- control('Deny', () => settle(entry, '/api/manager/approve', { id: entry.id, approved: false }, 'Denied')),
820
+ control('Approve', () => settle(entry, '/api/manager/approve', { graphId: streamGraphId, id: entry.id, approved: true }, 'Approved'), 'btn-small primary'),
821
+ control('Deny', () => settle(entry, '/api/manager/approve', { graphId: streamGraphId, id: entry.id, approved: false }, 'Denied')),
562
822
  ]);
563
823
  }
564
824
  function questionCard(entry) {
565
825
  const item = card(entry, 'Graph Manager asks', entry.question);
566
826
  if (entry.answered) return closed(item, entry, 'Answered');
567
- const answer = value => settle(entry, '/api/manager/answer', { id: entry.id, value }, `Answered: ${value}`);
827
+ const answer = value => settle(entry, '/api/manager/answer', { graphId: streamGraphId, id: entry.id, value }, `Answered: ${value}`);
568
828
  if (Array.isArray(entry.choices) && entry.choices.length) {
569
829
  return actions(item, entry, entry.choices.map(value => control(String(value), () => answer(String(value)))));
570
830
  }
@@ -588,7 +848,7 @@ globalThis.AndorraManager = (() => {
588
848
  const atEnd = log.scrollHeight - log.scrollTop - log.clientHeight < 40;
589
849
  const next = [];
590
850
  if (!transcript.length) {
591
- log.replaceChildren(node('p', 'Ask Graph Manager to build or change this graph. It sees the canvas as it is now, unsaved edits included, and asks before it saves anything.', 'help manager-empty'));
851
+ log.replaceChildren(node('p', 'Ask Graph Manager to build or change this graph. It sees the canvas as it is now, unsaved edits included. Its changes appear on the canvas as it makes them, and nothing is saved until you approve.', 'help manager-empty'));
592
852
  rendered = [];
593
853
  return;
594
854
  }
@@ -596,11 +856,19 @@ globalThis.AndorraManager = (() => {
596
856
  logic.blocks(transcript, runState !== 'idle').forEach((block, index) => {
597
857
  const before = rendered[index], pending = block.kind === 'entry' && answering.has(block.entry.id);
598
858
  if (sameBlock(before, block, pending)) { next.push(before); return; }
599
- const element = block.kind === 'work' ? workBlock(block) : row(block.entry);
859
+ const live = block.kind === 'work' && !block.settled;
860
+ // The same running stretch as last time: its rows are reconciled in place, not redrawn.
861
+ if (live && before?.block.kind === 'work' && !before.block.settled && before.block.key === block.key && before.rows) {
862
+ const { rows } = liveBlock(block, before);
863
+ next.push({ block, pending, element: before.element, rows });
864
+ return;
865
+ }
866
+ const drawn = live ? liveBlock(block) : { element: block.kind === 'work' ? workBlock(block) : row(block.entry) };
867
+ const element = drawn.element;
600
868
  if (before?.element && element) before.element.replaceWith(element);
601
869
  else { before?.element?.remove(); if (element) log.append(element); }
602
870
  if (element) followThought(element);
603
- next.push({ block, pending, element });
871
+ next.push({ block, pending, element, ...(drawn.rows ? { rows: drawn.rows } : {}) });
604
872
  });
605
873
  for (const stale of rendered.slice(next.length)) stale.element?.remove();
606
874
  rendered = next;
@@ -629,12 +897,6 @@ globalThis.AndorraManager = (() => {
629
897
  function renderNotices() {
630
898
  const bars = [];
631
899
  if (!state.providers.length) bars.push(notice('manager-warning', 'Add a model provider to use Graph Manager.', []));
632
- if (switchOffer && state.graph?.id === switchOffer) {
633
- bars.push(notice('manager-switch', `Start a new chat about ${state.graph.name || 'this graph'}?`, [
634
- control('New chat', () => { void newChat(); }, 'btn-small primary'),
635
- control('Keep this chat', () => { switchOffer = null; renderNotices(); }),
636
- ]));
637
- }
638
900
  if (offeredRevision !== null && state.graph) {
639
901
  const id = state.graph.id;
640
902
  bars.push(notice('manager-load', `Graph Manager saved version ${offeredRevision}. Load it?`, [
@@ -669,7 +931,7 @@ globalThis.AndorraManager = (() => {
669
931
  byId('manager-close').onclick = () => { setOpen(false); button.focus(); };
670
932
  byId('manager-new-chat').onclick = () => { void newChat(); };
671
933
  sendButton.onclick = () => { void send(); };
672
- stopButton.onclick = () => { void post('/api/manager/cancel'); };
934
+ stopButton.onclick = () => { void post('/api/manager/cancel', { graphId: streamGraphId }); };
673
935
  input.onkeydown = event => {
674
936
  if (event.key !== 'Enter' || event.shiftKey || event.isComposing) return;
675
937
  event.preventDefault();