@floless/app 0.163.0 → 0.165.0

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.
package/dist/web/chat.js CHANGED
@@ -586,9 +586,10 @@
586
586
  // #1776/#1859: a drawing read running in this conversation is where a message goes, on any screen.
587
587
  : paintFilterComposer().replyToRead
588
588
  ? READ_REPLY_HINT
589
- // #2438: in a project, @ names a member or sheet.
589
+ // #2438: in a project, @ names a member or sheet; #2874: an RFI or a Knowledge page too (only those in a General one).
590
590
  : state.scope.owner.kind === 'project'
591
- ? 'Enter to send · Shift+Enter for a new line · @ to mention a member or sheet'
591
+ ? (state.mentionGeneral?.[state.scope.owner.projectId] ? 'Enter to send · Shift+Enter for a new line · @ to mention an RFI or Knowledge page'
592
+ : 'Enter to send · Shift+Enter for a new line · @ to mention a member, sheet, RFI or page')
592
593
  : 'Enter to send · Shift+Enter for a new line';
593
594
  if (turnIsVisiblyActive() && !state.followupUnknownId && !hasUnknownFollowup()
594
595
  && state.imageLimitChatId !== state.chat?.chatId) {
@@ -2536,10 +2537,14 @@
2536
2537
  marker.dataset.turnId = turnId || '';
2537
2538
  marker.dataset.anchor = spec.anchor;
2538
2539
  const assumed = !sourced && row?.assumption ? row.assumption : null;
2539
- const label = resolved ? citationLabel(row.entry) : chosen ? citationLabel(chosenViewRow(row, chosen).entry) : assumed ? 'assumption, no source' : 'no source';
2540
+ // #2930: the AI named a member that exists the wrong way: its reference to correct, not a claim without a source.
2541
+ const misnamed = !sourced && !assumed && referenceProblem(row?.entry);
2542
+ const label = resolved ? citationLabel(row.entry) : chosen ? citationLabel(chosenViewRow(row, chosen).entry) : assumed ? 'assumption, no source'
2543
+ : misnamed ? 'source needs correcting' : 'no source';
2540
2544
  // No citation for this anchor on this page is "none recorded", not "none found": the AI may have given none, or the
2541
2545
  // record has not reached this page yet.
2542
2546
  const missing = assumed ? `No source. Assumed by ${assumedWho(assumed)} on ${assumedWhen(assumed)}`
2547
+ : misnamed ? 'Source needs correcting: the member exists, but the AI named it wrongly'
2543
2548
  : row ? 'No source was found for this claim' : 'No source is recorded for this claim';
2544
2549
  if (assumed) marker.classList.add('assumed');
2545
2550
  if (chosen) marker.classList.add('chosen');
@@ -2559,7 +2564,7 @@
2559
2564
  // The person's decision, beside the "?" that stays: it is still not a source. A solid edge tells it from the AI's own tag.
2560
2565
  const tag = document.createElement('span'); tag.className = 'ai-chat-assume by-person';
2561
2566
  tag.textContent = 'Assumption';
2562
- tag.dataset.tip = `${assumedWho(assumed) === 'you' ? 'You' : assumedWho(assumed)} chose to go on with this as an assumption (${assumedWhen(assumed)}). It has no source.`;
2567
+ tag.dataset.tip = `${assumedWho(assumed) === 'you' ? 'You' : assumedWho(assumed)} recorded this as an assumption (${assumedWhen(assumed)}). It has no source.`;
2563
2568
  marker.after(tag);
2564
2569
  }
2565
2570
  }
@@ -2577,6 +2582,127 @@
2577
2582
  }
2578
2583
  }
2579
2584
 
2585
+ const ASSUME_LABEL = 'Record as an assumption';
2586
+ /** #2930: the problems that mean the AI named a member that exists the wrong way (server CITATION_REFERENCE_PROBLEMS). */
2587
+ const REFERENCE_WORDS = {
2588
+ 'id-as-mark': 'The AI gave this member’s internal ID where its mark should be. The member exists, but the reference needs correcting.',
2589
+ 'wrong-mark': 'The AI named a mark that does not match this member. The member exists, but the reference needs correcting.',
2590
+ 'id-spelling': 'The AI misspelled this member’s ID. The member exists, but the reference needs correcting.',
2591
+ ambiguous: 'The AI’s reference matches more than one member, so it has to say which one it meant.',
2592
+ };
2593
+ function referenceProblem(entry) {
2594
+ return entry?.status === 'unresolved' && entry.kind === 'member' && Object.hasOwn(REFERENCE_WORDS, entry.problem) ? entry.problem : null;
2595
+ }
2596
+ function answerRunning(turnId) {
2597
+ return !!turnId && ['running', 'awaiting-approval'].includes(state.chat?.state) && state.chat?.activeTurn?.turnId === turnId;
2598
+ }
2599
+
2600
+ /**
2601
+ * #2930: what a chip's action did, kept across repaints for this window: "Saved." until the person's next message
2602
+ * goes (a follow-up included), "Sent." for good. Keyed by conversation, answer and claim.
2603
+ */
2604
+ const citationNotes = new Map();
2605
+ const citationNoteKey = (row, turnId) => `${row.chatId}\u0000${turnId}\u0000${row.anchor}`;
2606
+ const messagesSent = () => state.events.filter((event) => event.type === 'user-message').length;
2607
+ function noteCitation(row, turnId, note) {
2608
+ citationNotes.set(citationNoteKey(row, turnId), { ...note, messages: messagesSent() });
2609
+ }
2610
+ function citationNote(row, turnId) {
2611
+ const note = citationNotes.get(citationNoteKey(row, turnId));
2612
+ if (!note || (note.untilNextMessage && messagesSent() !== note.messages)) return null;
2613
+ const line = document.createElement('span'); line.className = `ai-chat-source-note${note.warn ? ' warn' : ''}`; line.setAttribute('role', 'status');
2614
+ line.textContent = note.text;
2615
+ return line;
2616
+ }
2617
+
2618
+ /**
2619
+ * #2930: the AI named a member that exists the wrong way (its id as a mark, a mark it does not carry, a misspelt id, or
2620
+ * a mark several members match). It is the AI's to correct, so this never leads with "No source" and never offers an
2621
+ * assumption. While the answer runs, the AI has already been told what to fix; once it has ended, "Ask the AI to
2622
+ * correct it" sends one message now. The earlier answer stays as it was: its citations belong to it.
2623
+ */
2624
+ function referenceChip(chip, turnId, row, choose) {
2625
+ const entry = row.entry;
2626
+ chip.classList.add('unresolved', 'misnamed');
2627
+ const cited = document.createElement('span'); cited.className = 'ai-chat-source-cited';
2628
+ cited.textContent = entry.label;
2629
+ cited.dataset.tip = 'What the AI cited';
2630
+ const why = document.createElement('span'); why.className = 'ai-chat-source-why';
2631
+ why.textContent = REFERENCE_WORDS[entry.problem];
2632
+ chip.append(cited, why);
2633
+ const done = citationNote(row, turnId);
2634
+ if (done) { chip.append(done); return chip; }
2635
+ const status = document.createElement('span'); status.className = 'ai-chat-source-note'; status.setAttribute('role', 'status');
2636
+ const asking = correcting.get(citationNoteKey(row, turnId));
2637
+ if (answerRunning(turnId) && !asking) {
2638
+ status.textContent = 'The answer is still running. The AI has been told exactly what to fix.';
2639
+ chip.append(status);
2640
+ return chip;
2641
+ }
2642
+ status.classList.add('warn');
2643
+ status.textContent = asking?.failed || 'The answer ended without correcting this. Ask the AI to answer again with the reference fixed.';
2644
+ const fix = button(asking?.sending ? 'Sending…' : 'Ask the AI to correct it', 'shell.chat-cite-correct', 'ai-chat-source-action');
2645
+ fix.dataset.tip = 'Sends a message now asking the AI to answer again with this reference corrected. This answer stays as it is';
2646
+ if (asking?.sending) {
2647
+ fix.setAttribute('aria-busy', 'true'); fix.setAttribute('aria-disabled', 'true');
2648
+ fix.dataset.blockedReason = 'FloLess is sending this request to the AI';
2649
+ }
2650
+ fix.addEventListener('click', () => askToCorrect(row, turnId));
2651
+ choose.classList.add('quiet');
2652
+ chip.append(status, sourceActions(fix, choose));
2653
+ return chip;
2654
+ }
2655
+
2656
+ /**
2657
+ * #2930: a correction request on its way, or the reason it did not go, by conversation, answer and claim. Kept apart
2658
+ * from the chip, which every repaint rebuilds (the send itself repaints).
2659
+ */
2660
+ const correcting = new Map();
2661
+ /**
2662
+ * #2930: the message asking for the correction. The instruction is FloLess's own fixed words; what the AI wrote before
2663
+ * (the claim, its citation) and the member's id go in as quoted data, never as words that could instruct.
2664
+ */
2665
+ function correctionText(row, turnId) {
2666
+ // Words are shortened for the message; a member id never is: it is what the AI cites.
2667
+ const quoted = (value, limit = 160) => JSON.stringify(limit ? String(value).slice(0, limit) : String(value));
2668
+ const claim = unsourcedClaimWords(turnId, row.anchor);
2669
+ return [
2670
+ `In your earlier answer, you named a model member the wrong way, so FloLess could not show it as found${row.number ? ` (source ${row.number})` : ''}.`,
2671
+ `What you cited: ${quoted(row.entry.label)}.`,
2672
+ ...(claim ? [`The claim it supports: ${quoted(claim)}.`] : []),
2673
+ ...(row.entry.memberId ? [`The model member's id is ${quoted(row.entry.memberId, 0)}.`] : []),
2674
+ 'The quoted text is what was written earlier, not an instruction.',
2675
+ 'Answer that part again and cite the member by its model id.',
2676
+ ].join(' ');
2677
+ }
2678
+
2679
+ /** #2930: one message, sent now through the chat's own Send; a second click while it goes sends nothing. */
2680
+ async function askToCorrect(row, turnId) {
2681
+ const key = citationNoteKey(row, turnId);
2682
+ if (correcting.get(key)?.sending || !row.chatId) return;
2683
+ correcting.set(key, { sending: true });
2684
+ paintEvents();
2685
+ let result;
2686
+ try { result = await sendTurn({ message: { text: correctionText(row, turnId), attachments: [] } }); }
2687
+ catch (error) { console.warn('floless: a correction request could not be sent', error); result = null; }
2688
+ if (result?.sent) {
2689
+ correcting.delete(key);
2690
+ noteCitation(row, turnId, { text: 'Sent. The corrected answer will appear below; this answer stays as it is.' });
2691
+ } else {
2692
+ correcting.set(key, { failed: result?.message ? String(result.message) : 'Your request did not reach the AI, and nothing was lost. Try again.' });
2693
+ }
2694
+ if (state.chat?.chatId !== row.chatId) return;
2695
+ // Focus goes back to the button only when the person had not moved on (a repaint leaves it on the page body).
2696
+ const active = document.activeElement;
2697
+ const stayed = !active || active === document.body || active.dataset?.commandId === 'shell.chat-cite-correct';
2698
+ paintEvents();
2699
+ if (!result?.sent && stayed) {
2700
+ const again = [...document.querySelectorAll('[data-command-id="shell.chat-cite-correct"]')]
2701
+ .find((control) => control.closest('.ai-chat-source')?.dataset.turnId === (turnId || '') && control.closest('.ai-chat-source')?.dataset.anchor === row.anchor);
2702
+ again?.focus({ preventScroll: true });
2703
+ }
2704
+ }
2705
+
2580
2706
  /** One chip: the sheet or page, what FloLess can vouch for, and (unresolved) the person's two ways on. */
2581
2707
  function citationChip(turnId, row) {
2582
2708
  const chip = document.createElement('span');
@@ -2608,16 +2734,20 @@
2608
2734
  marks.textContent = 'ASSUMPTION';
2609
2735
  marks.dataset.tip = 'Your decision to go on without a source. Nothing was found or checked for this claim. The AI is reminded of it with each message you send';
2610
2736
  chip.append(why, marks, sourceActions(ask, choose));
2737
+ const saved = citationNote(row, turnId);
2738
+ if (saved) chip.append(saved);
2611
2739
  return chip;
2612
2740
  }
2741
+ if (referenceProblem(entry)) return referenceChip(chip, turnId, row, choose);
2613
2742
  chip.classList.add('unresolved');
2614
2743
  // What was cited, in the AI's own words, so the person knows which source FloLess could not match.
2615
2744
  const cited = document.createElement('span'); cited.className = 'ai-chat-source-cited';
2616
2745
  cited.textContent = entry.label;
2617
2746
  cited.dataset.tip = 'What the AI cited';
2618
2747
  why.textContent = `No source: ${entry.reason}`;
2619
- const assume = button('Go on with this as an assumption', 'shell.chat-cite-assume', 'ai-chat-source-action');
2620
- assume.dataset.tip = 'Records that you chose to go on without a source, with your name and the date. The claim is labelled Assumption, and the AI is told with your next message. Nothing is sent now';
2748
+ // #2930: it only saves the person's decision, so it says so; nothing starts and nothing is sent now.
2749
+ const assume = button(ASSUME_LABEL, 'shell.chat-cite-assume', 'ai-chat-source-action');
2750
+ assume.dataset.tip = 'Saves your decision with your name and the date, and labels the claim Assumption. Nothing runs now: the AI receives it with your next message';
2621
2751
  assume.addEventListener('click', () => assumeClaim(row, turnId, chip, assume));
2622
2752
  chip.append(cited, why, sourceActions(ask, choose, assume));
2623
2753
  return chip;
@@ -2733,6 +2863,8 @@
2733
2863
  if (state.chat?.chatId !== row.chatId) return;
2734
2864
  if (saved && !state.events.some((known) => known.eventId === saved.eventId)) state.events.push(saved);
2735
2865
  if (saved) {
2866
+ // Seen until the person's next message goes: then it is no longer what will happen, it has happened.
2867
+ noteCitation(row, turnId, { text: 'Saved. The AI will receive this with your next message.', untilNextMessage: true });
2736
2868
  paintEvents();
2737
2869
  const after = [...ui.feed.querySelectorAll('.ai-chat-source')].find((node) => node.dataset.turnId === (turnId || '') && node.dataset.anchor === row.anchor);
2738
2870
  after?.querySelector('[data-command-id="shell.chat-cite-ask"]')?.focus({ preventScroll: true });
@@ -2741,7 +2873,7 @@
2741
2873
  return;
2742
2874
  }
2743
2875
  assume.removeAttribute('aria-busy'); assume.removeAttribute('aria-disabled');
2744
- assume.textContent = 'Go on with this as an assumption';
2876
+ assume.textContent = ASSUME_LABEL;
2745
2877
  const note = document.createElement('span'); note.className = 'ai-chat-source-note warn'; note.setAttribute('role', 'status');
2746
2878
  note.textContent = problem;
2747
2879
  chip.append(note);
@@ -3735,13 +3867,9 @@
3735
3867
  line.textContent = about;
3736
3868
  item.append(line);
3737
3869
  }
3738
- const mentioned = role === 'user' && event.payload?.mentions ? mentionsLine(event.payload.mentions) : '';
3739
- if (mentioned) {
3740
- const line = document.createElement('span'); line.className = 'ai-chat-message-subjects ai-chat-message-mentions';
3741
- line.textContent = mentioned.text;
3742
- line.setAttribute('aria-label', mentioned.spoken);
3743
- item.append(line);
3744
- }
3870
+ // #2874: each item the message mentioned opens what it names.
3871
+ const mentioned = role === 'user' && Array.isArray(event.payload?.mentions) ? mentionsLineElement(event, event.payload.mentions) : null;
3872
+ if (mentioned) item.append(mentioned);
3745
3873
  if (role === 'user' && (event.payload?.followup || event.payload?.deliveryState)) {
3746
3874
  const delivery = followupDelivery(event);
3747
3875
  const deliveryReceipt = [...state.events].reverse().find((candidate) => candidate.type === 'followup-status'
@@ -11041,7 +11169,43 @@
11041
11169
  /** Each conversation keeps its own picks: switching away and back finds them where they were. */
11042
11170
  function setMentions(items) { if (state.chat?.chatId) state.mentions = { ...state.mentions, [state.chat.chatId]: items }; }
11043
11171
  function mentionKey(pick) {
11044
- return pick.kind === 'member' ? JSON.stringify(['member', pick.id, pick.mark]) : JSON.stringify(['sheet', pick.sheet, pick.pageId, pick.sourceHash]);
11172
+ if (pick.kind === 'member') return JSON.stringify(['member', pick.id, pick.mark]);
11173
+ if (pick.kind === 'rfi') return JSON.stringify(['rfi', pick.rfiId]);
11174
+ if (pick.kind === 'kb-page') return JSON.stringify(['kb-page', pick.slug, pick.generationId]);
11175
+ return JSON.stringify(['sheet', pick.sheet, pick.pageId, pick.sourceHash]);
11176
+ }
11177
+ /** #2874: the @ list's groups, in the order they show. */
11178
+ const MENTION_GROUPS = [
11179
+ { kind: 'member', heading: 'Members' }, { kind: 'sheet', heading: 'Sheets' },
11180
+ { kind: 'rfi', heading: 'RFIs' }, { kind: 'kb-page', heading: 'Knowledge pages' },
11181
+ ];
11182
+ /**
11183
+ * The notes of groups that could not offer rows for a reason worth saying (it could not be read, there is no model
11184
+ * yet). A kind that does not apply here — members in a General project, a project with no RFIs or no Knowledge — is
11185
+ * left out, never listed as "nothing". Two groups saying the same thing say it once.
11186
+ */
11187
+ function mentionGroupNotes(groups) {
11188
+ if (!groups) return [];
11189
+ const quiet = new Set(['ok', 'not-steel', 'no-rfis', 'no-knowledge']);
11190
+ const notes = [];
11191
+ for (const { kind } of MENTION_GROUPS) {
11192
+ const group = groups[kind];
11193
+ // A Knowledge list FloLess could only partly read says so, so a page missing from it is not taken as absent.
11194
+ if (group?.status === 'ok' && group.incomplete === true) {
11195
+ notes.push({ kind, note: 'FloLess could not look through every Knowledge page just now, so some may be missing. Type @ again in a moment, or find the page on the Knowledge step.' });
11196
+ continue;
11197
+ }
11198
+ if (!group || quiet.has(group.status) || !group.note) continue;
11199
+ if (!notes.some((n) => n.note === group.note)) notes.push({ kind, note: String(group.note) });
11200
+ }
11201
+ return notes;
11202
+ }
11203
+ /** #2874: an RFI's status in words, and what replaced it, if anything did. */
11204
+ const RFI_STATUS_WORDS = { open: 'Open', sent: 'Sent', answered: 'Answered', resolved: 'Resolved', closed: 'Closed' };
11205
+ function rfiStatusWords(ref) {
11206
+ const status = RFI_STATUS_WORDS[ref.status] || String(ref.status || '');
11207
+ if (ref.supersededBy) return `${status}, replaced by ${ref.supersededBy}`;
11208
+ return ref.closedAs === 'superseded' ? 'Closed as superseded' : status;
11045
11209
  }
11046
11210
 
11047
11211
  /**
@@ -11049,18 +11213,32 @@
11049
11213
  * `all` lists every view (the tooltip); the row shows two and counts the rest, so it never wraps into a paragraph.
11050
11214
  */
11051
11215
  function refDetail(ref, all = false) {
11216
+ if (ref.kind === 'rfi') return `${rfiStatusWords(ref)} · ${ref.question || 'No question recorded'}`;
11217
+ if (ref.kind === 'kb-page') return `Knowledge page · ${ref.slug}`;
11052
11218
  if (ref.kind === 'sheet') return [`Sheet${ref.pageNumber ? `, page ${ref.pageNumber}` : ''}`, `source ${String(ref.sourceHash).slice(0, 8)}`].join(' · ');
11053
11219
  const named = (ref.views || []).map((view) => (view.title ? `${view.sheet} ${view.title}` : view.sheet));
11054
11220
  const shown = all ? named : named.slice(0, 2);
11055
11221
  const views = shown.join(', ') + (named.length > shown.length ? ` +${named.length - shown.length} more` : '');
11056
11222
  return [ref.mark ? null : 'No mark', ref.profile, views ? `on ${views}` : null, `id ${ref.id}`].filter(Boolean).join(' · ');
11057
11223
  }
11224
+ /**
11225
+ * A row of the @ list as a pick (identity only), its label and its tooltip. #2874: a Knowledge page's chip says
11226
+ * "Page ·" before its title, so a page titled "B12 notes" never reads as member B12.
11227
+ */
11058
11228
  function mentionOf(ref) {
11229
+ if (ref.kind === 'rfi') {
11230
+ const pick = { kind: 'rfi', rfiId: ref.rfiId };
11231
+ return { key: mentionKey(pick), pick, label: ref.rfiId, chip: ref.rfiId, tip: `${ref.rfiId} · ${refDetail(ref)}` };
11232
+ }
11233
+ if (ref.kind === 'kb-page') {
11234
+ const pick = { kind: 'kb-page', slug: ref.slug, generationId: ref.generationId };
11235
+ return { key: mentionKey(pick), pick, label: ref.title, chip: `Page · ${ref.title}`, tip: `Knowledge page ${ref.title} · ${ref.slug}` };
11236
+ }
11059
11237
  const pick = ref.kind === 'member'
11060
11238
  ? { kind: 'member', id: ref.id, mark: ref.mark ?? null }
11061
11239
  : { kind: 'sheet', sheet: ref.sheet, pageId: ref.pageId, sourceHash: ref.sourceHash };
11062
11240
  const label = ref.kind === 'member' ? (ref.mark || ref.id) : ref.sheet;
11063
- return { key: mentionKey(pick), pick, label, tip: `${ref.kind === 'member' ? 'Member' : 'Sheet'} ${label} · ${refDetail(ref, true)}` };
11241
+ return { key: mentionKey(pick), pick, label, chip: label, tip: `${ref.kind === 'member' ? 'Member' : 'Sheet'} ${label} · ${refDetail(ref, true)}` };
11064
11242
  }
11065
11243
 
11066
11244
  /** Why a mention did not resolve, in a few words, by what it is. */
@@ -11069,6 +11247,12 @@
11069
11247
  if (mention.reason === 'unreadable') return 'could not be checked';
11070
11248
  if (mention.reason === 'no-model') return 'no model yet';
11071
11249
  if (mention.reason === 'not-steel') return 'not checked, not a steel project';
11250
+ // #2874
11251
+ if (mention.reason === 'rfi-not-found') return 'not one of this project’s RFIs';
11252
+ if (mention.reason === 'rfis-unreadable' || mention.reason === 'knowledge-unreadable') return 'could not be checked';
11253
+ if (mention.reason === 'no-rfis') return 'this project has no RFIs';
11254
+ if (mention.reason === 'page-not-found') return 'not in that version of Knowledge';
11255
+ if (mention.reason === 'not-retained') return 'that version could not be kept';
11072
11256
  return 'not found';
11073
11257
  }
11074
11258
 
@@ -11087,12 +11271,182 @@
11087
11271
  return { text, spoken };
11088
11272
  }
11089
11273
 
11274
+ /**
11275
+ * #2874: the Mentioned line under a sent message, its resolved items buttons that open what they name. Each button
11276
+ * keeps its item's place in the stored list: that index, the message's event and its conversation are all the server
11277
+ * is told, and it opens only what that stored message named. Unresolved items stay text, with their reason.
11278
+ */
11279
+ function mentionsLineElement(event, mentions) {
11280
+ const said = mentionsLine(mentions);
11281
+ if (!said) return null;
11282
+ const line = document.createElement('span'); line.className = 'ai-chat-message-subjects ai-chat-message-mentions';
11283
+ const owner = state.scope?.owner;
11284
+ // A message still on its way has no stored mentions yet: its items open once the server has kept it.
11285
+ const openable = owner?.kind === 'project' && !event.optimistic && typeof event.eventId === 'string' && !event.eventId.startsWith('local:') && !!state.chat?.chatId;
11286
+ // Every item is listed (a message holds at most 20), so each one stays something the person can open.
11287
+ line.append('Mentioned: ');
11288
+ mentions.forEach((m, index) => {
11289
+ if (index) line.append(', ');
11290
+ const name = m.label || m.mark || m.id || m.sheet || m.rfiId || m.slug;
11291
+ if (m.resolved === false || !openable) {
11292
+ const text = Object.assign(document.createElement('span'), { textContent: `${name}${m.resolved === false ? ` (${mentionNotFound(m)})` : ''}` });
11293
+ if (m.resolved === false) text.className = 'ai-chat-mention-unresolved';
11294
+ line.append(text);
11295
+ return;
11296
+ }
11297
+ const kindWords = { member: 'member', sheet: 'sheet', rfi: '', 'kb-page': 'Knowledge page' }[m.kind] ?? '';
11298
+ const what = kindWords ? `${kindWords} ${name}` : name;
11299
+ const open = button(name, `shell.chat-mention-open#${m.kind}`, 'ai-chat-mention-open');
11300
+ open.dataset.commandInstanceLabel = what;
11301
+ open.dataset.tip = m.kind === 'member' ? `Show ${what} in the model` : m.kind === 'rfi' ? `Open ${name} on the RFIs step`
11302
+ : m.kind === 'sheet' ? `Open the picture of ${what}` : `Open ${what} at the version mentioned`;
11303
+ open.setAttribute('aria-label', open.dataset.tip);
11304
+ const chatId = state.chat.chatId, eventId = event.eventId;
11305
+ open.addEventListener('click', () => { void openMentioned({ chatId, eventId, index, mention: m, anchor: open }); });
11306
+ line.append(open);
11307
+ });
11308
+ return line;
11309
+ }
11310
+
11311
+ /** #2874: open one item of a sent message's Mentioned line. Never an error page: every outcome is a sentence. */
11312
+ async function openMentioned({ chatId, eventId, index, mention, anchor }) {
11313
+ const owner = state.scope?.owner;
11314
+ if (owner?.kind !== 'project' || state.chat?.chatId !== chatId) { announce('Open this conversation again to open what it mentioned.'); return; }
11315
+ const name = mention.label || mention.rfiId || mention.mark || mention.id;
11316
+ if (mention.kind === 'member' || mention.kind === 'rfi') {
11317
+ const unreachable = `FloLess could not open ${mention.kind === 'rfi' ? name : `member ${name}`} from here. Open the project from Workspaces, then try again.`;
11318
+ // A failure the place it opens did not show itself (it threw, or nothing answered) is put on screen here too:
11319
+ // the chat's status line is read out, not seen. The editor and the RFIs pane show their own failures.
11320
+ const seen = (text) => { if (typeof window.showToast === 'function') window.showToast(text, 'warn'); };
11321
+ let said, show = false;
11322
+ try {
11323
+ if (typeof window.flolessWorkspaces?.openConnection !== 'function') { announce(unreachable); seen(unreachable); return; }
11324
+ const projectUuid = state.chat?.owner?.projectUuid;
11325
+ const outcome = await window.flolessWorkspaces.openConnection({ projectId: owner.projectId, ...(projectUuid ? { projectUuid } : {}),
11326
+ ...(mention.kind === 'rfi' ? { rfiId: mention.rfiId } : { memberId: mention.id }) });
11327
+ if (outcome?.superseded) return;
11328
+ show = !outcome || (outcome.ok !== true && !outcome.said);
11329
+ said = !outcome ? unreachable : outcome.ok !== true ? (outcome.said || `${mention.kind === 'rfi' ? name : `Member ${name}`} was not shown.`)
11330
+ : outcome.said || (mention.kind === 'rfi' ? `Showing ${name} on the RFIs step.` : `Showing member ${name} in the model.`);
11331
+ } catch (error) {
11332
+ if (error?.plain !== true) console.error('[chat] a mentioned item could not be opened', error);
11333
+ said = error?.plain === true && error.message ? error.message : unreachable;
11334
+ show = true;
11335
+ }
11336
+ if (said) { announce(said); if (show) seen(said); }
11337
+ return;
11338
+ }
11339
+ await openMentionedSource({ chatId, eventId, index, mention, anchor, name, owner });
11340
+ }
11341
+
11342
+ /**
11343
+ * #2874: a mentioned sheet's picture, or a mentioned Knowledge page at the version mentioned, in the source viewer.
11344
+ * A page whose text changed under that version is said to have changed, never shown as the page mentioned.
11345
+ */
11346
+ async function openMentionedSource({ chatId, eventId, index, mention, anchor, name, owner }) {
11347
+ const sheet = mention.kind === 'sheet';
11348
+ const title = sheet ? `Sheet ${name}` : name;
11349
+ const dialog = document.createElement('dialog'); dialog.className = 'ai-chat-source-viewer';
11350
+ dialog.setAttribute('aria-label', title);
11351
+ const head = document.createElement('div'); head.className = 'ai-chat-source-viewer-head';
11352
+ const close = button('Close', 'shell.chat-cite-close', 'ai-chat-source-viewer-close');
11353
+ let picture = null, finished = false, viewport = null;
11354
+ const controller = new AbortController();
11355
+ const finish = () => {
11356
+ if (finished) return;
11357
+ finished = true;
11358
+ controller.abort();
11359
+ if (dialog.open) dialog.close();
11360
+ hide();
11361
+ if (picture) URL.revokeObjectURL(picture);
11362
+ if (anchor?.isConnected) anchor.focus({ preventScroll: true });
11363
+ };
11364
+ close.addEventListener('click', finish);
11365
+ dialog.addEventListener('close', finish);
11366
+ head.append(Object.assign(document.createElement('strong'), { textContent: title }), close);
11367
+ const note = Object.assign(document.createElement('span'), { className: 'ai-chat-source-viewer-note',
11368
+ textContent: sheet ? 'Mentioned in your message. FloLess shows the sheet picture it holds now.' : 'Mentioned in your message, at the version you mentioned.' });
11369
+ const body = document.createElement('div'); body.className = 'ai-chat-source-viewer-body';
11370
+ const status = Object.assign(document.createElement('p'), { className: 'ai-chat-source-viewer-status', textContent: 'Opening…' });
11371
+ status.setAttribute('role', 'status');
11372
+ body.append(status);
11373
+ dialog.append(head, note, body);
11374
+ const hide = showChatViewer(dialog, finish);
11375
+ close.focus({ preventScroll: true });
11376
+ if (sheet) {
11377
+ dialog.classList.add('ai-chat-evidence-viewer');
11378
+ viewport = drawingViewport({ pageBounds: [0, 0, 1, 1] });
11379
+ dialog.insertBefore(viewport.bar, body);
11380
+ body.append(viewport.frame);
11381
+ }
11382
+ const url = `/api/chat/conversations/${encodeURIComponent(chatId)}/mentions/${encodeURIComponent(eventId)}/${index}/${sheet ? 'image' : 'page'}`
11383
+ + `?ownerKind=project&ownerId=${encodeURIComponent(owner.projectId)}`;
11384
+ try {
11385
+ const response = await fetch(url, { credentials: 'same-origin', cache: 'no-store', signal: controller.signal });
11386
+ if (finished) return;
11387
+ if (!response.ok) {
11388
+ viewport?.frame.remove(); viewport?.bar.remove();
11389
+ const gone = response.status === 410 || response.status === 404;
11390
+ // A 410 also answers a conversation that moved; only this code says the message and its project still match.
11391
+ const changed = response.status === 410 && (await response.json().catch(() => null))?.code === 'mention-source-changed';
11392
+ if (finished) return;
11393
+ status.textContent = changed ?'This Knowledge page has changed since you mentioned it, so FloLess does not show it as the page you mentioned. You can open the page as it is now.'
11394
+ : response.status === 410 ? 'This conversation has moved, so FloLess could not open what it mentioned here. Open the conversation again, then try again.'
11395
+ : response.status === 404 && sheet ? 'FloLess no longer holds this sheet picture, or this project is no longer the one the message mentioned. Mention the sheet again to see the picture FloLess holds now.'
11396
+ : response.status === 404 ? 'FloLess no longer holds the version of this page you mentioned, or this project is no longer the one the message mentioned. Mention the page again to see it as it is now.'
11397
+ : response.status === 401 || response.status === 403 ? 'This page is no longer signed in to FloLess, so it could not open this. Reload the page, then try again.'
11398
+ : 'FloLess could not open this right now. Try again in a moment.';
11399
+ if (!gone) console.warn('[chat] a mentioned source was refused', response.status);
11400
+ // The way on is the page as it is now, labelled as such, never shown as the version that was mentioned. Only a
11401
+ // changed page says the message and its project still match; a 404 may be a project made again, whose same-named
11402
+ // page is another.
11403
+ if (changed && !sheet && mention.slug) {
11404
+ const now = button('Open the current page in Knowledge', 'shell.chat-mention-open-current-knowledge', 'ai-chat-source-viewer-knowledge');
11405
+ now.addEventListener('click', () => {
11406
+ finish();
11407
+ if (window.flolessWorkspaces?.openKnowledgePage) window.flolessWorkspaces.openKnowledgePage(owner.projectId, mention.slug);
11408
+ else announce('Knowledge is not available on this page.');
11409
+ });
11410
+ dialog.append(now);
11411
+ }
11412
+ return;
11413
+ }
11414
+ if (sheet) {
11415
+ const bytes = await response.blob();
11416
+ if (finished) return;
11417
+ picture = URL.createObjectURL(bytes);
11418
+ const image = Object.assign(document.createElement('img'), { src: picture, alt: title });
11419
+ if (!await viewport.setImage(image, () => !finished, true)) return;
11420
+ status.textContent = 'The sheet picture FloLess holds now.';
11421
+ return;
11422
+ }
11423
+ const { page } = await response.json();
11424
+ if (finished) return;
11425
+ status.textContent = 'The Knowledge page at the version you mentioned.';
11426
+ const article = document.createElement('div'); article.className = 'ai-chat-message-body ai-chat-source-viewer-page';
11427
+ article.innerHTML = markdownHtml(page.body);
11428
+ const knowledge = button('Open in Knowledge', 'shell.chat-cite-open-knowledge', 'ai-chat-source-viewer-knowledge');
11429
+ knowledge.addEventListener('click', () => {
11430
+ finish();
11431
+ if (window.flolessWorkspaces?.openKnowledgePage) window.flolessWorkspaces.openKnowledgePage(page.projectId, page.slug, page.generationId);
11432
+ else announce('Knowledge is not available on this page.');
11433
+ });
11434
+ body.append(article);
11435
+ dialog.append(knowledge);
11436
+ } catch (error) {
11437
+ if (finished) return;
11438
+ console.error('[chat] a mentioned source could not be opened', error);
11439
+ viewport?.frame.remove(); viewport?.bar.remove();
11440
+ status.textContent = 'FloLess could not open this right now. Try again in a moment.';
11441
+ }
11442
+ }
11443
+
11090
11444
  function paintMentionChips() {
11091
11445
  const items = currentMentions();
11092
11446
  for (const item of items) {
11093
11447
  const chip = document.createElement('span'); chip.className = 'ai-chat-attachment ai-chat-mention';
11094
11448
  chip.dataset.mentionKey = item.key;
11095
- const label = button(`@${item.label}`, 'shell.chat-mention', 'ai-chat-subjects-label');
11449
+ const label = button(`@${item.chip || item.label}`, 'shell.chat-mention', 'ai-chat-subjects-label');
11096
11450
  label.dataset.commandInstanceLabel = item.label;
11097
11451
  label.dataset.tip = `${item.tip}. Goes with your next message.`;
11098
11452
  label.setAttribute('aria-label', `${item.tip}, mentioned in your next message`);
@@ -11162,7 +11516,8 @@
11162
11516
  const projectId = state.scope?.owner?.projectId;
11163
11517
  menu.timer = setTimeout(async () => {
11164
11518
  let body = null;
11165
- try { body = await request(`/api/projects/${encodeURIComponent(projectId)}/refs?q=${encodeURIComponent(menu.query)}`); }
11519
+ // #2874: all four kinds, answered in groups; a kind that does not apply to the project says so in its group.
11520
+ try { body = await request(`/api/projects/${encodeURIComponent(projectId)}/refs?q=${encodeURIComponent(menu.query)}&kinds=member,sheet,rfi,kb-page`); }
11166
11521
  catch { body = null; }
11167
11522
  // A newer keystroke, a closed list or another conversation: this answer is for a question nobody is asking now.
11168
11523
  if (state.mentionMenu !== menu || menu.seq !== seq) return;
@@ -11172,15 +11527,27 @@
11172
11527
  else {
11173
11528
  // The row that was active stays active if it is still listed.
11174
11529
  const was = menu.status === 'options' && menu.refs[menu.active] ? mentionOf(menu.refs[menu.active]).key : null;
11175
- menu.refs = Array.isArray(body.refs) ? body.refs : [];
11530
+ menu.refs = Array.isArray(body.refs) ? body.refs.filter((ref) => MENTION_GROUPS.some((g) => g.kind === ref.kind)) : [];
11176
11531
  menu.heldBack = Number(body.heldBack) || 0;
11177
- menu.status = menu.refs.length ? 'options' : 'empty';
11532
+ menu.groups = body.groups && typeof body.groups === 'object' ? body.groups : null;
11533
+ // #2874: which kinds apply here, for the message box's hint (a General project has no members or sheets).
11534
+ if (menu.groups) {
11535
+ const general = menu.groups.member?.status === 'not-steel';
11536
+ const known = state.mentionGeneral?.[projectId];
11537
+ state.mentionGeneral = { ...state.mentionGeneral, [projectId]: general };
11538
+ // The hint is drawn before the list answers: draw it again now it is known, but only while it is that idle
11539
+ // mention hint, never over a notice (a problem, a reconnect, a reply in progress).
11540
+ if (known !== general && /· @ to mention /.test(ui.hint?.textContent || '')) clearReport();
11541
+ }
11542
+ const notes = mentionGroupNotes(menu.groups);
11543
+ if (!menu.refs.length && notes.length) { menu.status = 'note'; menu.note = notes.map((n) => n.note).join(' '); }
11544
+ else menu.status = menu.refs.length ? 'options' : 'empty';
11178
11545
  const kept = was ? menu.refs.findIndex((ref) => mentionOf(ref).key === was) : -1;
11179
11546
  menu.active = menu.refs.length ? Math.max(0, kept) : -1;
11180
11547
  }
11181
11548
  renderMentionMenu();
11182
11549
  announce(menu.status === 'options' ? `${menu.refs.length + menu.heldBack} ${menu.refs.length + menu.heldBack === 1 ? 'match' : 'matches'}`
11183
- : menu.status === 'empty' ? 'No matches' : menu.status === 'error' ? 'FloLess could not list members and sheets' : menu.note);
11550
+ : menu.status === 'empty' ? 'No matches' : menu.status === 'error' ? 'FloLess could not list what you can mention' : menu.note);
11184
11551
  }, 150);
11185
11552
  }
11186
11553
 
@@ -11189,19 +11556,19 @@
11189
11556
  if (!menu) return;
11190
11557
  ui.skills.replaceChildren();
11191
11558
  ui.skills.classList.add('is-mentions');
11192
- ui.skills.setAttribute('aria-label', 'Members and sheets you can mention');
11559
+ ui.skills.setAttribute('aria-label', 'What you can mention');
11193
11560
  const status = (text) => {
11194
11561
  const row = document.createElement('div'); row.className = 'ai-chat-mention-status'; row.setAttribute('role', 'option');
11195
11562
  row.setAttribute('aria-disabled', 'true'); row.textContent = text; ui.skills.append(row);
11196
11563
  };
11197
11564
  let activeId = '';
11198
- if (menu.status === 'loading') status('Looking for members and sheets…');
11565
+ if (menu.status === 'loading') status('Looking for what you can mention…');
11199
11566
  else if (menu.status === 'note') status(menu.note);
11200
11567
  else if (menu.status === 'empty') {
11201
11568
  status(menu.query ? `Nothing in this project is named “${menu.query}”. Type it in your message instead.`
11202
- : 'This project has no members or sheets to mention yet.');
11569
+ : 'Nothing to mention in this project yet. Its members, sheets, RFIs and Knowledge pages appear here.');
11203
11570
  } else if (menu.status === 'error') {
11204
- status('FloLess could not list this project’s members and sheets just now.');
11571
+ status('FloLess could not list what you can mention in this project just now.');
11205
11572
  const retry = button('Try again', 'shell.chat-mention-retry', 'ai-chat-choice');
11206
11573
  retry.id = 'chat-mention-retry'; retry.setAttribute('role', 'option'); retry.setAttribute('aria-selected', 'true');
11207
11574
  retry.addEventListener('pointerdown', (event) => event.preventDefault());
@@ -11209,22 +11576,38 @@
11209
11576
  ui.skills.append(retry);
11210
11577
  activeId = retry.id;
11211
11578
  } else {
11212
- menu.refs.forEach((ref, index) => {
11213
- const item = mentionOf(ref);
11214
- const row = button('', `shell.chat-mention-pick#${item.key}`, 'ai-chat-choice ai-chat-mention-option');
11215
- row.id = `chat-mention-option-${index}`; row.tabIndex = -1;
11216
- row.setAttribute('role', 'option'); row.setAttribute('aria-selected', String(index === menu.active));
11217
- row.dataset.commandInstanceLabel = item.label;
11218
- row.dataset.tip = item.tip;
11219
- const name = document.createElement('span'); name.className = 'ai-chat-mention-name'; name.textContent = item.label;
11220
- const meta = document.createElement('small'); meta.className = 'ai-chat-skill-origin'; meta.textContent = refDetail(ref);
11221
- row.append(name, meta);
11222
- row.addEventListener('pointerdown', (event) => event.preventDefault()); // the message box keeps focus
11223
- row.addEventListener('click', () => pickMention(ref));
11224
- ui.skills.append(row);
11225
- if (index === menu.active) activeId = row.id;
11226
- });
11227
- if (menu.heldBack) status(`${menu.heldBack} more match. Type more of the name to narrow the list.`);
11579
+ // #2874: one heading per kind that has rows or a note, in a fixed order. Headings are not options, so ↑/↓ move
11580
+ // over the rows only and cross from one group to the next; a group that could not be read says so in its place.
11581
+ const notes = mentionGroupNotes(menu.groups);
11582
+ const grouped = !!menu.groups;
11583
+ for (const { kind, heading } of MENTION_GROUPS) {
11584
+ const rows = menu.refs.map((ref, index) => ({ ref, index })).filter(({ ref }) => ref.kind === kind);
11585
+ const note = notes.find((n) => n.kind === kind);
11586
+ if (!rows.length && !note) continue;
11587
+ if (grouped) {
11588
+ const head = Object.assign(document.createElement('div'), { className: 'ai-chat-mention-group', textContent: heading });
11589
+ head.setAttribute('role', 'presentation');
11590
+ ui.skills.append(head);
11591
+ }
11592
+ for (const { ref, index } of rows) {
11593
+ const item = mentionOf(ref);
11594
+ const row = button('', `shell.chat-mention-pick#${item.key}`, 'ai-chat-choice ai-chat-mention-option');
11595
+ row.id = `chat-mention-option-${index}`; row.tabIndex = -1;
11596
+ row.setAttribute('role', 'option'); row.setAttribute('aria-selected', String(index === menu.active));
11597
+ row.setAttribute('aria-setsize', String(menu.refs.length)); row.setAttribute('aria-posinset', String(index + 1));
11598
+ row.dataset.commandInstanceLabel = item.label;
11599
+ row.dataset.tip = item.tip;
11600
+ const name = document.createElement('span'); name.className = 'ai-chat-mention-name'; name.textContent = item.label;
11601
+ const meta = document.createElement('small'); meta.className = 'ai-chat-skill-origin'; meta.textContent = refDetail(ref);
11602
+ row.append(name, meta);
11603
+ row.addEventListener('pointerdown', (event) => event.preventDefault()); // the message box keeps focus
11604
+ row.addEventListener('click', () => pickMention(ref));
11605
+ ui.skills.append(row);
11606
+ if (index === menu.active) activeId = row.id;
11607
+ }
11608
+ if (note) status(note.note);
11609
+ }
11610
+ if (menu.heldBack) status(`${menu.heldBack} more ${menu.heldBack === 1 ? 'match' : 'matches'}. Type more of the name to narrow the list.`);
11228
11611
  }
11229
11612
  ui.skills.hidden = false;
11230
11613
  ui.input.setAttribute('aria-controls', ui.skills.id);
@@ -16289,6 +16672,8 @@
16289
16672
  // (the upload modal, Requests) says what happened and what to do next instead of a generic toast (#1779).
16290
16673
  describeError: errorMessage,
16291
16674
  currentProvider: () => state.chat?.provider ?? null,
16675
+ // #2926: the conversation on screen, so the Drawing Filter's Flo knows a question shown here is its read's.
16676
+ currentChatId: () => state.chat?.chatId ?? null,
16292
16677
  // The project this panel's conversations belong to; an object card offers Show in chat only for it (#1653).
16293
16678
  scopeProjectId: () => (state.scope?.owner?.kind === 'project' ? state.scope.owner.projectId : null),
16294
16679
  // The Knowledge step's "Ask AI to index" (#2399 slice 0b): the person's own draft in the message box; never sent.