@operad-io/tui 0.1.0 → 0.2.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/src/state.ts CHANGED
@@ -4,11 +4,16 @@
4
4
  * touches the terminal, so a test drives it as a plain value.
5
5
  */
6
6
 
7
+ import { bareId, formatElapsed, objectKey, parseObjectId } from "@operad-io/engine";
8
+ export { bareId };
7
9
  import type {
10
+ AskQuestion,
11
+ AskQuestionAnswer,
12
+ ChoiceInfo,
8
13
  PendingItem,
9
14
  RowMark,
10
15
  RunningItem,
11
- ViewListPage,
16
+ SectionStatus,
12
17
  ViewAction,
13
18
  ViewObject,
14
19
  ViewRow,
@@ -33,8 +38,10 @@ import {
33
38
  mainAction,
34
39
  mainKey,
35
40
  MAIN_KEY,
41
+ pasteInto,
36
42
  runKey,
37
43
  startAction,
44
+ startNote,
38
45
  typed,
39
46
  isReserved,
40
47
  resolveBindings,
@@ -44,6 +51,9 @@ import {
44
51
  type TuiCommand,
45
52
  pageActions,
46
53
  } from "./actions.ts";
54
+ import { chatCommands } from "./commands.ts";
55
+ import { chatMarkdown, chatRunRows, type ChatMarkdownOptions } from "./chat.ts";
56
+
47
57
 
48
58
  /** The two terminal themes the view knows. */
49
59
  export type TuiTheme = "light" | "dark";
@@ -65,12 +75,16 @@ const WAITING_KEEPS: readonly RowMark[] = [
65
75
  "ready_to_merge",
66
76
  ];
67
77
 
68
- /** One pending question a person answers with one of its choices. */
78
+ /** One pending question a person answers with choices, a form or a secret text. */
69
79
  export interface TuiAnswer {
70
80
  readonly runId: string;
71
81
  readonly waitId: string;
72
82
  readonly prompt: string;
73
83
  readonly choices: readonly string[];
84
+ /** What each choice says about itself; see `ChoiceInfo`. */
85
+ readonly choiceInfo?: Readonly<Record<string, ChoiceInfo>> | undefined;
86
+ /** The form to fill in, when the item asks one a choice cannot answer. */
87
+ readonly questions?: readonly AskQuestion[] | undefined;
74
88
  }
75
89
 
76
90
  /** One line of a list, in the words the view renders. */
@@ -125,6 +139,14 @@ export interface TuiChildren {
125
139
  readonly hiddenCancelled?: number | undefined;
126
140
  }
127
141
 
142
+ /** A message the client drew before the server's copy arrived. */
143
+ export interface TuiOptimistic {
144
+ readonly id: number;
145
+ readonly text: string;
146
+ /** How often the text was already in the content when it was sent. */
147
+ readonly baseline: number;
148
+ }
149
+
128
150
  /** One section of the open object, with the content fetched so far. */
129
151
  export interface TuiSection {
130
152
  readonly id: string;
@@ -155,8 +177,14 @@ export interface TuiSection {
155
177
  readonly mark?: string | undefined;
156
178
  /** The part that is still changing: drawn after the content, in the weak colour. */
157
179
  readonly tail?: string | undefined;
180
+ /** What the live section is doing, drawn as one line under the tail. */
181
+ readonly status?: SectionStatus | undefined;
158
182
  /** Grows with each content change; an older answer is dropped. */
159
183
  readonly version: number;
184
+ /** The thinking and tool runs this person opened; the rest stay folded. */
185
+ readonly folds?: readonly string[] | undefined;
186
+ /** Messages that are drawn at the end until the server's copy arrives. */
187
+ readonly optimistic?: readonly TuiOptimistic[] | undefined;
160
188
  }
161
189
 
162
190
  /** The object the right pane shows, or the one that fills a narrow screen. */
@@ -201,10 +229,22 @@ export type TuiInput =
201
229
  readonly action: string;
202
230
  readonly id: string;
203
231
  readonly text: string;
232
+ /** The cursor in `text`, in display characters; the end when unset. */
233
+ readonly cursor?: number | undefined;
234
+ /** The action is on its way; the text stays until the server answers. */
235
+ readonly sending?: boolean | undefined;
236
+ /** Why the server refused the text; it stays until the next edit. */
237
+ readonly error?: string | undefined;
204
238
  readonly required: boolean;
205
239
  /** The action asks to confirm after the note. */
206
240
  readonly confirm: boolean;
207
241
  readonly args?: Readonly<Record<string, unknown>> | undefined;
242
+ /** The note answers this pending question's choice instead of an action. */
243
+ readonly answer?: { readonly answer: TuiAnswer; readonly choice: string } | undefined;
244
+ /** A line starting with `/` is listing the chat's commands; the cursor is in that list. */
245
+ readonly commandMenu?: { readonly cursor: number } | undefined;
246
+ /** The same list, for a caller that reads this name. */
247
+ readonly commands?: { readonly cursor: number } | undefined;
208
248
  }
209
249
  | {
210
250
  /** One choice of a pending question is being chosen. */
@@ -212,6 +252,19 @@ export type TuiInput =
212
252
  readonly answer: TuiAnswer;
213
253
  readonly cursor: number;
214
254
  }
255
+ | {
256
+ /** One question of a pending form is being filled in. */
257
+ readonly kind: "form";
258
+ readonly answer: TuiAnswer;
259
+ /** The question being asked; the TUI draws one. */
260
+ readonly question: number;
261
+ /** The cursor in a choice list. */
262
+ readonly cursor: number;
263
+ /** What is typed into a free-text answer. */
264
+ readonly text: string;
265
+ /** Why the last answer was refused. */
266
+ readonly error?: string | undefined;
267
+ }
215
268
  | {
216
269
  /** One argument of an action is being asked: a list, a number or a text. */
217
270
  readonly kind: "args";
@@ -267,7 +320,8 @@ export interface TuiActionRequest {
267
320
  export interface TuiAnswerRequest {
268
321
  readonly runId: string;
269
322
  readonly waitId: string;
270
- readonly choice: string;
323
+ readonly choice?: string | undefined;
324
+ readonly answers?: readonly AskQuestionAnswer[] | undefined;
271
325
  readonly note?: string | undefined;
272
326
  readonly confirmed?: boolean | undefined;
273
327
  }
@@ -300,26 +354,15 @@ export interface TuiSectionRequest {
300
354
  readonly objectVersion: number;
301
355
  }
302
356
 
303
- /** A list page the reducer wants the caller to fetch next. */
304
- export interface TuiPageRequest {
305
- readonly tabId: string;
306
- readonly cursor: string;
307
- /** The list version the page was asked for; a newer list drops the answer. */
308
- readonly version: number;
309
- }
310
-
311
- /** The rows, cursor and paging state of one tab. */
357
+ /** The rows, cursor and load state of one tab. */
312
358
  export interface TuiList {
313
359
  readonly rows: readonly TuiRow[];
314
360
  readonly cursor: number;
315
- readonly next?: string | undefined;
316
- /** The first page has arrived. */
361
+ /** The whole list has arrived. */
317
362
  readonly loaded: boolean;
318
- /** A next page is being fetched; a second request waits for its result. */
319
- readonly loading: boolean;
320
- /** A failed page fetch, kept until the next successful page. */
363
+ /** A failed list fetch, kept until the next successful one. */
321
364
  readonly error?: string | undefined;
322
- /** Grows with each replace; an appended page for an older one is dropped. */
365
+ /** Grows with each replace; an answer for an older list is dropped. */
323
366
  readonly version: number;
324
367
  /** The filter changed and the rows of the new one have not arrived yet. */
325
368
  readonly switching?: boolean | undefined;
@@ -384,8 +427,6 @@ export interface TuiState {
384
427
  readonly refreshRequested: boolean;
385
428
  /** Set when `f` chose a filter and its first page must be fetched. */
386
429
  readonly filterRequest?: TuiFilterRequest | undefined;
387
- /** Set when the cursor nears the end of a list that has another page. */
388
- readonly pageRequest?: TuiPageRequest | undefined;
389
430
  /** The open object, if any. */
390
431
  readonly object?: TuiObject | undefined;
391
432
  /** The version of the next open; it never restarts, even after a close. */
@@ -394,9 +435,10 @@ export interface TuiState {
394
435
  readonly objectRequest?: TuiObjectRequest | undefined;
395
436
  /**
396
437
  * `c` on a tab: the parent page is opening so the action that creates `type`
397
- * can start on it; the page's answer starts it.
438
+ * can start on it; the page's answer starts it. `noteFirst` starts at the
439
+ * first message, leaving the action's arguments at their defaults.
398
440
  */
399
- readonly creating?: { readonly parent: string; readonly type: string } | undefined;
441
+ readonly creating?: { readonly parent: string; readonly type: string; readonly noteFirst?: boolean | undefined } | undefined;
400
442
  /** Set when a section must be fetched. */
401
443
  readonly sectionRequest?: TuiSectionRequest | undefined;
402
444
  /** Grows each time a list cursor moves; the shell opens what it lands on. */
@@ -405,6 +447,8 @@ export interface TuiState {
405
447
  readonly followPending: boolean;
406
448
  /** Grows with each refresh; an answer to an older refresh is dropped. */
407
449
  readonly generation: number;
450
+ /** The next id for a message drawn before the server's copy. */
451
+ readonly optimisticSeq: number;
408
452
  /** The last failure to show in the footer; cleared by a good refresh. */
409
453
  readonly status?: string | undefined;
410
454
  /** The key bindings in force; a user binding beats a plugin default. */
@@ -427,6 +471,8 @@ export interface TuiState {
427
471
  readonly notice?: string | undefined;
428
472
  /** Set when an action must be sent; the caller clears it as it sends. */
429
473
  readonly actionRequest?: TuiActionRequest | undefined;
474
+ /** The message drawn before the server's copy, while its send is unanswered. */
475
+ readonly send?: { readonly objectId: string; readonly sectionId: string; readonly id: number } | undefined;
430
476
  /** Set when an answer must be sent; the caller clears it as it sends. */
431
477
  readonly answerRequest?: TuiAnswerRequest | undefined;
432
478
  /** The settings the palette lists, global and the open object's repository. */
@@ -446,9 +492,6 @@ export interface TuiState {
446
492
  /** What the last request said about the server. */
447
493
  export type TuiConnection = "ok" | "unreachable" | "unauthorized";
448
494
 
449
- /** The number of rows from the end that make the reducer ask for a page. */
450
- export const PAGE_AHEAD = 2;
451
-
452
495
  /** Initial dimensions, matching a conventional 80 by 24 terminal. */
453
496
  export const DEFAULT_WIDTH = 80;
454
497
  export const DEFAULT_HEIGHT = 24;
@@ -469,7 +512,6 @@ function makeList(): TuiList {
469
512
  rows: [],
470
513
  cursor: 0,
471
514
  loaded: false,
472
- loading: false,
473
515
  version: 0,
474
516
  };
475
517
  }
@@ -507,13 +549,6 @@ function filtersOf(types: readonly TuiTypeInfo[] | undefined): readonly TuiFilte
507
549
  return out.length === 0 ? undefined : out;
508
550
  }
509
551
 
510
- /** The filter to ask a type for: the tab's, when the type declares it. */
511
- export function filterFor(tab: TuiTab, info: TuiTypeInfo): string | undefined {
512
- const name = tab.filter;
513
- if (name === undefined) return undefined;
514
- return info.filters?.some((f) => f.name === name) === true ? name : undefined;
515
- }
516
-
517
552
  /** The plugin tab or settings list that holds `type`, if any. */
518
553
  export function pluginTabOf(state: TuiState, type: string): TuiTab | undefined {
519
554
  return (
@@ -543,6 +578,7 @@ export function initialState(options: TuiStateOptions = {}): TuiState {
543
578
  moveSeq: 0,
544
579
  followPending: false,
545
580
  generation: 0,
581
+ optimisticSeq: 0,
546
582
  bindings: [],
547
583
  recent: options.recent ?? [],
548
584
  settings: [],
@@ -555,9 +591,6 @@ export function initialState(options: TuiStateOptions = {}): TuiState {
555
591
  };
556
592
  }
557
593
 
558
- /** A short alias for callers that build the first state. */
559
- export const createInitialState = initialState;
560
-
561
594
  /** One row of a plugin tab's list, with its type and, first in a group, the group's heading. */
562
595
  export interface TuiTreeEntry {
563
596
  readonly row: ViewRow;
@@ -625,29 +658,7 @@ export type TuiStateEvent =
625
658
  readonly generation?: number | undefined;
626
659
  }
627
660
  | {
628
- readonly type: "page";
629
- readonly tabId: string;
630
- readonly page: ViewListPage;
631
- /** Replace the list instead of appending one page. */
632
- readonly replace?: boolean | undefined;
633
- /** The filter the page was asked under; a page for another one is dropped. */
634
- readonly filter?: string | undefined;
635
- /** The filter-cycle count it was asked under; an older one is dropped. */
636
- readonly filterSeq?: number | undefined;
637
- /** For a replace: the refresh it belongs to. */
638
- readonly generation?: number | undefined;
639
- /** For an append: the list version it was asked for. */
640
- readonly version?: number | undefined;
641
- }
642
- | {
643
- readonly type: "rows";
644
- readonly tabId: string;
645
- readonly rows: readonly ViewRow[];
646
- readonly next?: string | undefined;
647
- readonly replace?: boolean | undefined;
648
- }
649
- | {
650
- readonly type: "page-error";
661
+ readonly type: "list-error";
651
662
  readonly tabId: string;
652
663
  readonly message: string;
653
664
  readonly version?: number | undefined;
@@ -700,10 +711,10 @@ export type TuiStateEvent =
700
711
  | { readonly type: "object-launched" }
701
712
  /** The caller launched the section request; it is asked for once. */
702
713
  | { readonly type: "section-launched" }
714
+ /** A bare client opens the new chat of this parent without choosing the parent. */
715
+ | { readonly type: "start-chat"; readonly parent: string }
703
716
  /** A refresh began; answers to the one before it are dropped. */
704
717
  | { readonly type: "refresh-start" }
705
- /** The caller launched the pending page request; it is asked for once. */
706
- | { readonly type: "page-launched" }
707
718
  /** Show a failure in the footer, or clear it with no message. */
708
719
  | {
709
720
  readonly type: "status";
@@ -719,6 +730,8 @@ export type TuiStateEvent =
719
730
  readonly ctrl?: boolean | undefined;
720
731
  readonly meta?: boolean | undefined;
721
732
  }
733
+ /** A bracketed paste, taken as text by the open input. */
734
+ | { readonly type: "paste"; readonly text: string }
722
735
  | {
723
736
  readonly type: "resize";
724
737
  readonly width: number;
@@ -771,13 +784,7 @@ export type TuiStateEvent =
771
784
  | { readonly type: "row-gone"; readonly id: string };
772
785
 
773
786
  /** A duration as the Running list writes it. */
774
- export function formatElapsed(ms: number): string {
775
- const value = Number.isFinite(ms) ? Math.max(0, ms) : 0;
776
- if (value < 1000) return `${Math.floor(value)}ms`;
777
- if (value < 60_000) return `${(value / 1000).toFixed(1)}s`;
778
- const whole = Math.floor(value / 1000);
779
- return `${Math.floor(whole / 60)}m ${whole % 60}s`;
780
- }
787
+ export { formatElapsed };
781
788
 
782
789
  function rowId(row: ViewRow): string {
783
790
  return row.scope === undefined ? row.id : `${row.scope}/${row.id}`;
@@ -802,7 +809,25 @@ export function rowFromView(row: ViewRow, type?: string): TuiRow {
802
809
 
803
810
  /** The item's answer, when its choices are not the object's actions. */
804
811
  function answerOfPending(item: PendingItem): TuiAnswer | undefined {
805
- if (item.waitId === undefined || item.choices.length === 0) return undefined;
812
+ if (item.waitId === undefined) return undefined;
813
+ const questions = item.questions;
814
+ if (questions !== undefined) {
815
+ // The TUI draws a form of one question; a larger form stays for a client
816
+ // that draws all of it.
817
+ if (questions.length !== 1) return undefined;
818
+ const names = new Set(item.actions.map((action) => action.action));
819
+ const asChoice = item.choices.length > 0 && item.choices.every((choice) => names.has(choice));
820
+ if (asChoice) return undefined;
821
+ return {
822
+ runId: item.runId,
823
+ waitId: item.waitId,
824
+ prompt: item.prompt,
825
+ choices: item.choices,
826
+ questions,
827
+ ...(item.choiceInfo === undefined ? {} : { choiceInfo: item.choiceInfo }),
828
+ };
829
+ }
830
+ if (item.choices.length === 0) return undefined;
806
831
  const names = new Set(item.actions.map((action) => action.action));
807
832
  // A choice that is also an action stays with the actions, as it works today.
808
833
  if (item.choices.every((choice) => names.has(choice))) return undefined;
@@ -811,12 +836,19 @@ function answerOfPending(item: PendingItem): TuiAnswer | undefined {
811
836
  waitId: item.waitId,
812
837
  prompt: item.prompt,
813
838
  choices: item.choices,
839
+ ...(item.choiceInfo === undefined ? {} : { choiceInfo: item.choiceInfo }),
814
840
  };
815
841
  }
816
842
 
817
843
  function rowFromPending(item: PendingItem): TuiRow {
818
844
  const answer = answerOfPending(item);
819
- const answerDetail = answer === undefined ? [] : [`choices: ${answer.choices.join(", ")}`];
845
+ const asked = answer?.questions?.map((question) => question.text) ?? [];
846
+ const answerDetail = [
847
+ ...asked,
848
+ ...(answer === undefined || answer.questions !== undefined || answer.choices.length === 0
849
+ ? []
850
+ : [`choices: ${answer.choices.join(", ")}`]),
851
+ ];
820
852
  // A server that sends the object's row saves a read per item: the row names
821
853
  // the object, and the question stays visible as the note. An older server
822
854
  // sends no row, and the item shows its question and flow as before.
@@ -883,7 +915,7 @@ function detailOf(parts: readonly string[]): readonly string[] {
883
915
  /** The repository an object name carries: `operad` in `ops/T-1-x`. */
884
916
  function repositoryOf(object: string | null | undefined): string {
885
917
  if (object === null || object === undefined) return "";
886
- const bare = object.slice(object.indexOf(":") + 1);
918
+ const bare = bareId(object);
887
919
  const slash = bare.lastIndexOf("/");
888
920
  return slash < 0 ? "" : bare.slice(0, slash);
889
921
  }
@@ -1081,63 +1113,90 @@ function withTab(state: TuiState, tabId: string, patch: (tab: TuiTab) => TuiTab)
1081
1113
  return { ...state, tabs };
1082
1114
  }
1083
1115
 
1084
- function nearEnd(tab: TuiTab): boolean {
1085
- const { list } = tab;
1086
- if (list.rows.length === 0) return false;
1087
- return list.cursor >= Math.max(0, list.rows.length - PAGE_AHEAD);
1088
- }
1089
-
1090
- /** Sets one page request for a tab, and never a second while one is loading. */
1091
- function requestPage(state: TuiState, tab: TuiTab): TuiState {
1092
- const { list } = tab;
1093
- if (!nearEnd(tab) || list.next === undefined || list.loading) return state;
1094
- if (state.pageRequest?.tabId === tab.id && state.pageRequest.cursor === list.next) {
1095
- return state;
1096
- }
1097
- const updated = withTab(state, tab.id, (current) => ({
1098
- ...current,
1099
- list: { ...current.list, loading: true },
1100
- }));
1101
- return {
1102
- ...updated,
1103
- pageRequest: { tabId: tab.id, cursor: list.next, version: list.version },
1104
- };
1105
- }
1106
-
1107
1116
  function activeTabOf(state: TuiState): TuiTab | undefined {
1108
1117
  return state.tabs[state.active];
1109
1118
  }
1110
1119
 
1111
- /**
1112
- * The rows a section's Markdown takes once wrapped to the pane, without its
1113
- * trailing blank. `width` defaults to no wrapping for callers that only want
1114
- * the renderer's own line count.
1115
- */
1116
- export function markdownLineCount(
1117
- markdown: string,
1118
- width = Number.POSITIVE_INFINITY,
1119
- ): number {
1120
- return layoutMarkdown(
1121
- markdown,
1122
- Number.isFinite(width) ? width : undefined,
1123
- ).length;
1124
- }
1125
-
1126
1120
  /**
1127
1121
  * The rows of a section at the pane's width. They are laid out once for each
1128
1122
  * content version of each open object and width, and kept: scrolling and
1129
1123
  * drawing read the same rows without wrapping the text again.
1130
1124
  */
1125
+ export interface SectionLayoutOptions {
1126
+ /** The moment that decides whether a chat header's date is today. */
1127
+ readonly now?: number | undefined;
1128
+ /** The zone a chat header's time is drawn in; the terminal's when unset. */
1129
+ readonly timeZone?: string | undefined;
1130
+ }
1131
+
1132
+ /** A chat's transcript is the one section whose lines the client rewrites. */
1133
+ function chatTranscript(state: TuiState, section: TuiSection): boolean {
1134
+ return state.object?.type === "chat" && section.id === "transcript";
1135
+ }
1136
+
1137
+ /** How many chat Markdown strings were built, for a test that checks the cache is used. */
1138
+ export const chatStats = { computed: 0 };
1139
+
1140
+ const CHAT_MARKDOWN_CACHE_MAX = 32;
1141
+ const chatMarkdowns = new Map<string, string>();
1142
+
1143
+ /** The local day a chat header compares against. */
1144
+ function chatDayOf(now: number, timeZone: string | undefined): string {
1145
+ try {
1146
+ return new Intl.DateTimeFormat("en-CA", {
1147
+ timeZone,
1148
+ year: "numeric",
1149
+ month: "2-digit",
1150
+ day: "2-digit",
1151
+ }).format(new Date(now));
1152
+ } catch {
1153
+ return String(now);
1154
+ }
1155
+ }
1156
+
1157
+ /** A chat transcript's Markdown, kept while its text, local day and folds stay the same. */
1158
+ export function chatMarkdownCached(
1159
+ markdown: string,
1160
+ options: ChatMarkdownOptions,
1161
+ ): string {
1162
+ const day = chatDayOf(options.now ?? Date.now(), options.timeZone);
1163
+ const open = [...(options.open ?? [])].sort().join("\u0000");
1164
+ const key = `${markdown}\u0000${day}\u0000${options.timeZone ?? ""}\u0000${open}\u0000${options.fold !== false}\u0000${options.prefix ?? "c"}`;
1165
+ const kept = chatMarkdowns.get(key);
1166
+ if (kept !== undefined) {
1167
+ chatMarkdowns.delete(key);
1168
+ chatMarkdowns.set(key, kept);
1169
+ return kept;
1170
+ }
1171
+ chatStats.computed += 1;
1172
+ const result = chatMarkdown(markdown, options);
1173
+ chatMarkdowns.set(key, result);
1174
+ if (chatMarkdowns.size > CHAT_MARKDOWN_CACHE_MAX) {
1175
+ const oldest = chatMarkdowns.keys().next().value;
1176
+ if (oldest !== undefined) chatMarkdowns.delete(oldest);
1177
+ }
1178
+ return result;
1179
+ }
1180
+
1131
1181
  export function sectionLayout(
1132
1182
  state: TuiState,
1133
1183
  section: TuiSection,
1184
+ options: SectionLayoutOptions = {},
1134
1185
  ): readonly LayoutLine[] {
1135
1186
  const object = state.object;
1136
1187
  const key = `${object?.version ?? 0}\u0000${object?.id ?? ""}\u0000${section.id}`;
1188
+ const markdown = chatTranscript(state, section)
1189
+ ? chatMarkdownCached(section.markdown, {
1190
+ ...(options.now === undefined ? {} : { now: options.now }),
1191
+ ...(options.timeZone === undefined ? {} : { timeZone: options.timeZone }),
1192
+ ...(section.folds === undefined ? {} : { open: section.folds }),
1193
+ prefix: "c",
1194
+ })
1195
+ : section.markdown;
1137
1196
  return cachedLayout(
1138
1197
  key,
1139
1198
  section.version,
1140
- section.markdown,
1199
+ markdown,
1141
1200
  sectionWidth(state),
1142
1201
  );
1143
1202
  }
@@ -1149,16 +1208,28 @@ export function sectionLayout(
1149
1208
  export function sectionTailLayout(
1150
1209
  state: TuiState,
1151
1210
  section: TuiSection,
1211
+ options: SectionLayoutOptions = {},
1152
1212
  ): readonly LayoutLine[] {
1153
- if (section.tail === undefined || section.tail === "") return [];
1213
+ const sent = section.optimistic?.map((entry) => entry.text).join("\n\n") ?? "";
1214
+ if ((section.tail === undefined || section.tail === "") && sent === "") return [];
1154
1215
  const object = state.object;
1155
1216
  const key = `${object?.version ?? 0}\u0000${object?.id ?? ""}\u0000${section.id}\u0000tail`;
1156
- return cachedLayout(key, section.version, section.tail, sectionWidth(state));
1157
- }
1158
-
1159
- /** The rows a section takes on screen: its content, then its changing tail. */
1217
+ const tail =
1218
+ section.tail !== undefined && section.tail !== "" && chatTranscript(state, section)
1219
+ ? chatMarkdownCached(section.tail, {
1220
+ ...(options.now === undefined ? {} : { now: options.now }),
1221
+ ...(options.timeZone === undefined ? {} : { timeZone: options.timeZone }),
1222
+ ...(section.folds === undefined ? {} : { open: section.folds }),
1223
+ prefix: "t",
1224
+ })
1225
+ : (section.tail ?? "");
1226
+ const markdown = sent === "" ? tail : joinMarkdown(tail, sent);
1227
+ return cachedLayout(key, section.version, markdown, sectionWidth(state));
1228
+ }
1229
+
1230
+ /** The rows a section takes on screen: its content, its changing tail and its status. */
1160
1231
  export function sectionLineCount(state: TuiState, section: TuiSection): number {
1161
- return sectionLayout(state, section).length + sectionTailLayout(state, section).length;
1232
+ return sectionLayout(state, section).length + sectionTailLayout(state, section).length + (section.status === undefined ? 0 : 1);
1162
1233
  }
1163
1234
 
1164
1235
  /** The layout's fixed columns (0-based): the list, its divider, the object pane. */
@@ -1180,12 +1251,6 @@ export function listWidthFor(width: number): number {
1180
1251
  /** The columns before a row's text: marker, glyph and their spaces. */
1181
1252
  export const ROW_INDENT = 4;
1182
1253
 
1183
- /** The row id without its type: `item:T-1-x` is `T-1-x`. */
1184
- export function bareId(id: string): string {
1185
- const at = id.indexOf(":");
1186
- return at < 0 ? id : id.slice(at + 1);
1187
- }
1188
-
1189
1254
  /** The short id and the slug of an id like `T-0031-tui-object`. */
1190
1255
  function splitId(id: string): { readonly short: string; readonly slug: string } {
1191
1256
  const match = /^([A-Za-z]+-\d+)-(.+)$/.exec(id);
@@ -1405,7 +1470,7 @@ function openObject(state: TuiState): TuiState {
1405
1470
  const typeName = row.type;
1406
1471
  const requestId =
1407
1472
  typeName !== undefined && !row.id.startsWith(`${typeName}:`)
1408
- ? `${typeName}:${row.id}`
1473
+ ? objectKey(typeName, row.id)
1409
1474
  : row.id;
1410
1475
  const object: TuiObject = {
1411
1476
  id: requestId,
@@ -1413,7 +1478,7 @@ function openObject(state: TuiState): TuiState {
1413
1478
  type:
1414
1479
  previous?.type ??
1415
1480
  typeName ??
1416
- (row.id.includes(":") ? row.id.slice(0, row.id.indexOf(":")) : row.id),
1481
+ (parseObjectId(row.id, { allowEmptyId: true })?.type ?? row.id),
1417
1482
  link: previous?.link ?? "",
1418
1483
  row,
1419
1484
  // A request for the sections died with the old open, so none is loading.
@@ -1437,9 +1502,9 @@ function openObject(state: TuiState): TuiState {
1437
1502
 
1438
1503
  /** Opens the page of an object an action created, named "<type>:<id>". */
1439
1504
  function openNamed(state: TuiState, id: string): TuiState {
1440
- const colon = id.indexOf(":");
1441
- if (colon <= 0 || colon === id.length - 1) return state;
1442
- const type = id.slice(0, colon);
1505
+ const parsed = parseObjectId(id);
1506
+ if (parsed === undefined) return state;
1507
+ const type = parsed.type;
1443
1508
  const version = state.objectVersion + 1;
1444
1509
  const object: TuiObject = {
1445
1510
  id,
@@ -1541,8 +1606,11 @@ function applyObject(
1541
1606
  if (creating !== undefined && previous.id === creating.parent) {
1542
1607
  updated = { ...updated, creating: undefined };
1543
1608
  const make = view.actions.find((action) => action.creates === creating.type);
1544
- return make === undefined
1545
- ? withNotice(updated, `${creating.parent} has no way to create a ${creating.type}`)
1609
+ if (make === undefined) {
1610
+ return withNotice(updated, `${creating.parent} has no way to create a ${creating.type}`);
1611
+ }
1612
+ return creating.noteFirst === true
1613
+ ? startNote(updated, make.name)
1546
1614
  : startAction(updated, make.name);
1547
1615
  }
1548
1616
  return updated;
@@ -1603,6 +1671,19 @@ export function joinMarkdown(before: string, after: string): string {
1603
1671
  return `${before.replace(/\n?$/, "\n")}${after}`;
1604
1672
  }
1605
1673
 
1674
+ /** How many times a message text appears in a transcript, without overlaps. */
1675
+ export function countOccurrences(text: string, part: string): number {
1676
+ if (part === "") return 0;
1677
+ let count = 0;
1678
+ let at = 0;
1679
+ for (;;) {
1680
+ const found = text.indexOf(part, at);
1681
+ if (found < 0) return count;
1682
+ count += 1;
1683
+ at = found + part.length;
1684
+ }
1685
+ }
1686
+
1606
1687
  function applySection(
1607
1688
  state: TuiState,
1608
1689
  objectId: string,
@@ -1653,9 +1734,14 @@ function applySection(
1653
1734
  // The answer's mark is the section's position now; a read that gives none
1654
1735
  // makes the next refresh read the section whole, as before.
1655
1736
  mark: content.mark,
1737
+ // A copy the server wrote replaces the message drawn before it.
1738
+ ...(value.optimistic === undefined
1739
+ ? {}
1740
+ : { optimistic: value.optimistic.filter((entry) => countOccurrences(markdown, entry.text) <= entry.baseline) }),
1656
1741
  // The answer's tail is the whole current tail; a page read that leaves it
1657
- // out keeps the one shown.
1742
+ // out keeps the one shown. The status follows the same rule.
1658
1743
  tail: pageAppend && content.tail === undefined ? value.tail : content.tail,
1744
+ status: pageAppend && content.status === undefined ? value.status : content.status,
1659
1745
  ...(content.pages === undefined ? {} : { toc: content.pages }),
1660
1746
  error: undefined,
1661
1747
  version: value.version + 1,
@@ -1907,14 +1993,8 @@ function move(state: TuiState, delta: number): TuiState {
1907
1993
  ...tab.list,
1908
1994
  cursor: tab.list.cursor + delta,
1909
1995
  });
1910
- if (next === tab.list.cursor) {
1911
- // At the last row a further Down still asks for the page the previous
1912
- // failed attempt or the end of the loaded rows left unread.
1913
- return delta > 0 ? requestPage(state, tab) : state;
1914
- }
1915
- const moved = moveCursor(state, tab.id, next);
1916
- const updated = activeTabOf(moved);
1917
- return updated === undefined ? moved : requestPage(moved, updated);
1996
+ if (next === tab.list.cursor) return state;
1997
+ return moveCursor(state, tab.id, next);
1918
1998
  }
1919
1999
 
1920
2000
  /** Moves a tab's cursor; the object pane follows it after the shell's debounce. */
@@ -1931,9 +2011,7 @@ function jump(state: TuiState, to: "top" | "bottom"): TuiState {
1931
2011
  if (tab === undefined) return state;
1932
2012
  const cursor = to === "top" ? 0 : Math.max(0, tab.list.rows.length - 1);
1933
2013
  if (cursor === tab.list.cursor) return state;
1934
- const moved = moveCursor(state, tab.id, cursor);
1935
- const updated = activeTabOf(moved);
1936
- return updated === undefined ? moved : requestPage(moved, updated);
2014
+ return moveCursor(state, tab.id, cursor);
1937
2015
  }
1938
2016
 
1939
2017
  function activate(state: TuiState, index: number): TuiState {
@@ -1975,8 +2053,8 @@ function openChild(state: TuiState): TuiState {
1975
2053
  const shown = children === undefined ? [] : displayChildren(children.rows, state.childLayout);
1976
2054
  const row = shown[children?.cursor ?? 0]?.row;
1977
2055
  if (parent === undefined || row === undefined) return state;
1978
- const type = row.type ?? (row.id.includes(":") ? row.id.slice(0, row.id.indexOf(":")) : "");
1979
- const requestId = type === "" || row.id.startsWith(`${type}:`) ? row.id : `${type}:${row.id}`;
2056
+ const type = row.type ?? parseObjectId(row.id, { allowEmptyId: true })?.type ?? "";
2057
+ const requestId = type === "" || row.id.startsWith(`${type}:`) ? row.id : objectKey(type, row.id);
1980
2058
  const version = state.objectVersion + 1;
1981
2059
  const object: TuiObject = {
1982
2060
  id: requestId,
@@ -2039,6 +2117,42 @@ function childKey(state: TuiState, key: string): TuiState | undefined {
2039
2117
  return undefined;
2040
2118
  }
2041
2119
 
2120
+ /** Enter on a chat transcript opens or closes the run at the selected line. */
2121
+ function toggleChatFold(state: TuiState): TuiState | undefined {
2122
+ const object = state.object;
2123
+ const section = activeSection(state);
2124
+ if (object?.type !== "chat" || object.back !== undefined || section?.id !== "transcript") {
2125
+ return undefined;
2126
+ }
2127
+ // Enter belongs to row navigation until the open row is the selected one.
2128
+ if (selectedRow(state)?.id !== object.source) return undefined;
2129
+ const width = sectionWidth(state);
2130
+ const open = section.folds ?? [];
2131
+ const contentRows = sectionLayout(state, section).length;
2132
+ const rows = [
2133
+ ...chatRunRows(section.markdown, { open, width, prefix: "c" }),
2134
+ ...chatRunRows(section.tail ?? "", { open, width, prefix: "t" }).map((entry) => ({
2135
+ ...entry,
2136
+ start: entry.start + contentRows,
2137
+ end: entry.end + contentRows,
2138
+ })),
2139
+ ];
2140
+ const at = section.scroll;
2141
+ const chosen =
2142
+ rows.find((entry) => at >= entry.start && at < entry.end) ??
2143
+ rows.find((entry) => entry.start >= at) ??
2144
+ rows[rows.length - 1];
2145
+ if (chosen === undefined) return undefined;
2146
+ const folds = open.includes(chosen.run.id)
2147
+ ? open.filter((id) => id !== chosen.run.id)
2148
+ : [...open, chosen.run.id];
2149
+ return withSection(state, section.id, (value) => ({
2150
+ ...value,
2151
+ folds,
2152
+ version: value.version + 1,
2153
+ }));
2154
+ }
2155
+
2042
2156
  /** Enter: opens the selected row; on the row that is already open, its next level. */
2043
2157
  function enter(state: TuiState): TuiState {
2044
2158
  const row = selectedRow(state);
@@ -2180,13 +2294,29 @@ function handleKey(
2180
2294
  if (isKey(key, "end")) return to(bottom);
2181
2295
  return state;
2182
2296
  }
2297
+ // On a chat, `/` opens its input already listing the commands a line may type.
2298
+ if (key === "/" && !shift && state.object !== undefined) {
2299
+ const commands = chatCommands(state.object.actions, state.object.row);
2300
+ const send = state.object.actions.find(
2301
+ (action) => action.plugin === "chat" && action.action === "send",
2302
+ );
2303
+ if (commands.length > 0 && send !== undefined) {
2304
+ const opened = startAction(state, send.name);
2305
+ if (opened.input?.kind === "note") {
2306
+ return inputKey(opened, opened.input, { key: "/", text: "/" });
2307
+ }
2308
+ }
2309
+ }
2183
2310
  if (isKey(key, "t") && (state.object?.children?.rows.length ?? 0) > 0) {
2184
2311
  const childLayout: TuiChildLayout = state.childLayout === "tree" ? "list" : "tree";
2185
2312
  return { ...state, childLayout, layoutRequest: childLayout, followPending: false };
2186
2313
  }
2187
2314
  const inChildren = childKey(state, key);
2188
2315
  if (inChildren !== undefined) return inChildren;
2189
- if (isKey(key, "enter", "return")) return enter(state);
2316
+ if (isKey(key, "enter", "return")) {
2317
+ const folded = toggleChatFold(state);
2318
+ return folded === undefined ? enter(state) : folded;
2319
+ }
2190
2320
  if (isKey(key, "escape", "esc", "backspace")) {
2191
2321
  const back = goBack(state);
2192
2322
  if (back !== undefined) return back;
@@ -2274,20 +2404,16 @@ function cycleFilter(state: TuiState): TuiState {
2274
2404
  filter: next,
2275
2405
  filterSeq: (current.filterSeq ?? 0) + 1,
2276
2406
  hidden: undefined,
2277
- // The rows stay until the new ones arrive; a page of the old filter is
2278
- // dropped by its version, and its paging cursor is no longer valid.
2407
+ // The rows stay until the new ones arrive; a list of the old filter is
2408
+ // dropped by its version.
2279
2409
  list: {
2280
2410
  ...current.list,
2281
- next: undefined,
2282
- loading: false,
2283
2411
  switching: true,
2284
2412
  version: current.list.version + 1,
2285
2413
  },
2286
2414
  }));
2287
- const quiet =
2288
- changed.pageRequest?.tabId === tab.id ? { ...changed, pageRequest: undefined } : changed;
2289
2415
  return {
2290
- ...quiet,
2416
+ ...changed,
2291
2417
  filterRequest: { tabId: tab.id, filter: next, seq: (tab.filterSeq ?? 0) + 1 },
2292
2418
  };
2293
2419
  }
@@ -2560,7 +2686,6 @@ function removeRow(state: TuiState, id: string): TuiState {
2560
2686
  function replaceRows(
2561
2687
  tab: TuiTab,
2562
2688
  rows: readonly TuiRow[],
2563
- next: string | undefined,
2564
2689
  ): TuiTab {
2565
2690
  const cursor = cursorAfterReplace(tab.list, rows);
2566
2691
  return {
@@ -2569,10 +2694,7 @@ function replaceRows(
2569
2694
  ...tab.list,
2570
2695
  rows,
2571
2696
  cursor,
2572
- next,
2573
2697
  loaded: true,
2574
- // The page that was in flight for the old rows is dropped when it lands.
2575
- loading: false,
2576
2698
  error: undefined,
2577
2699
  switching: undefined,
2578
2700
  version: tab.list.version + 1,
@@ -2580,12 +2702,6 @@ function replaceRows(
2580
2702
  };
2581
2703
  }
2582
2704
 
2583
- function clearRequest(before: TuiState, after: TuiState, tabId: string): TuiState {
2584
- return before.pageRequest?.tabId === tabId
2585
- ? { ...after, pageRequest: undefined }
2586
- : after;
2587
- }
2588
-
2589
2705
  /**
2590
2706
  * After a list loads or another tab is shown, the pane beside it shows the row
2591
2707
  * the cursor rests on, unless it already does.
@@ -2612,7 +2728,7 @@ function selectedAnswer(state: TuiState): TuiAnswer | undefined {
2612
2728
  function applyPending(state: TuiState, items: readonly PendingItem[]): TuiState {
2613
2729
  const rows = items.map(rowFromPending);
2614
2730
  return followLoaded(
2615
- withTab(state, "pending", (tab) => replaceRows(tab, rows, undefined)),
2731
+ withTab(state, "pending", (tab) => replaceRows(tab, rows)),
2616
2732
  "pending",
2617
2733
  );
2618
2734
  }
@@ -2620,7 +2736,7 @@ function applyPending(state: TuiState, items: readonly PendingItem[]): TuiState
2620
2736
  function applyRunning(state: TuiState, items: readonly RunningItem[]): TuiState {
2621
2737
  const rows = items.map(rowFromRunning);
2622
2738
  return followLoaded(
2623
- withTab(state, "running", (tab) => replaceRows(tab, rows, undefined)),
2739
+ withTab(state, "running", (tab) => replaceRows(tab, rows)),
2624
2740
  "running",
2625
2741
  );
2626
2742
  }
@@ -2629,9 +2745,6 @@ function appendPage(
2629
2745
  state: TuiState,
2630
2746
  tabId: string,
2631
2747
  rows: readonly TuiRow[],
2632
- next: string | undefined,
2633
- replace: boolean,
2634
- version: number | undefined,
2635
2748
  filter?: string | undefined,
2636
2749
  hidden?: number | undefined,
2637
2750
  filterSeq?: number | undefined,
@@ -2639,40 +2752,19 @@ function appendPage(
2639
2752
  const tab = state.tabs.find((candidate) => candidate.id === tabId);
2640
2753
  if (tab === undefined) return state;
2641
2754
  if (filterSeq !== undefined && filterSeq !== (tab.filterSeq ?? 0)) return state;
2642
- if (!replace && version !== undefined && version !== tab.list.version) {
2643
- return state;
2644
- }
2645
2755
  // An answer to a filter that is no longer the chosen one is never shown.
2646
2756
  if (filter !== undefined && tab.filter !== undefined && filter !== tab.filter) {
2647
2757
  return state;
2648
2758
  }
2649
- const wasSwitching = replace && tab.list.switching === true;
2650
- const updated = withTab(state, tabId, (current) => {
2651
- if (replace) return { ...replaceRows(current, rows, next), hidden };
2652
- const list: TuiList = {
2653
- ...current.list,
2654
- rows: [...current.list.rows, ...rows],
2655
- loaded: true,
2656
- loading: false,
2657
- next,
2658
- error: undefined,
2659
- };
2660
- return {
2661
- ...current,
2662
- ...(hidden === undefined ? {} : { hidden }),
2663
- list: { ...list, cursor: clampCursor(list) },
2664
- };
2665
- });
2666
- const cleared = clearRequest(state, updated, tabId);
2759
+ const wasSwitching = tab.list.switching === true;
2760
+ const updated = withTab(state, tabId, (current) => ({
2761
+ ...replaceRows(current, rows),
2762
+ hidden,
2763
+ }));
2667
2764
  // `f` keeps the open object even when the new filter hides its row or leaves
2668
2765
  // no row at all; its next level follows the new filter.
2669
- if (wasSwitching || !replace) return cleared;
2670
- return followLoaded(cleared, tabId);
2671
- }
2672
-
2673
- /** The type a page of a tab's rows is of: the tab's first. */
2674
- function firstType(state: TuiState, tabId: string): string | undefined {
2675
- return state.tabs.find((tab) => tab.id === tabId)?.types?.[0]?.type;
2766
+ if (wasSwitching) return updated;
2767
+ return followLoaded(updated, tabId);
2676
2768
  }
2677
2769
 
2678
2770
  function rowOfEntry(entry: TuiTreeEntry): TuiRow {
@@ -2757,7 +2849,7 @@ function applyChildren(
2757
2849
  }));
2758
2850
  }
2759
2851
 
2760
- function applyPageError(
2852
+ function applyListError(
2761
2853
  state: TuiState,
2762
2854
  tabId: string,
2763
2855
  message: string,
@@ -2766,15 +2858,10 @@ function applyPageError(
2766
2858
  const current = state.tabs.find((candidate) => candidate.id === tabId);
2767
2859
  if (current === undefined) return state;
2768
2860
  if (version !== undefined && version !== current.list.version) return state;
2769
- const updated = withTab(state, tabId, (tab) => ({
2861
+ return withTab(state, tabId, (tab) => ({
2770
2862
  ...tab,
2771
- list: { ...tab.list, loading: false, error: sanitize(message) },
2863
+ list: { ...tab.list, error: sanitize(message) },
2772
2864
  }));
2773
- if (updated === state) return state;
2774
- if (state.pageRequest?.tabId === tabId) {
2775
- return { ...updated, pageRequest: undefined };
2776
- }
2777
- return updated;
2778
2865
  }
2779
2866
 
2780
2867
  /** An answer to a refresh that is no longer the current one. */
@@ -2797,19 +2884,6 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2797
2884
  return stale(state, event.generation)
2798
2885
  ? state
2799
2886
  : applyRunning(state, event.items);
2800
- case "page":
2801
- if (event.replace === true && stale(state, event.generation)) return state;
2802
- return appendPage(
2803
- state,
2804
- event.tabId,
2805
- event.page.rows.map((row) => rowFromView(row, firstType(state, event.tabId))),
2806
- event.page.next,
2807
- event.replace === true,
2808
- event.version,
2809
- event.filter,
2810
- event.page.hidden,
2811
- event.filterSeq,
2812
- );
2813
2887
  case "tree": {
2814
2888
  if (stale(state, event.generation)) return state;
2815
2889
  const rows = event.entries.flatMap((entry) => rowsOfEntry(state, entry));
@@ -2817,9 +2891,6 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2817
2891
  state,
2818
2892
  event.tabId,
2819
2893
  rows,
2820
- undefined,
2821
- true,
2822
- undefined,
2823
2894
  event.filter,
2824
2895
  event.hidden,
2825
2896
  event.filterSeq,
@@ -2843,17 +2914,8 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2843
2914
  return state.filterRequest === undefined
2844
2915
  ? state
2845
2916
  : { ...state, filterRequest: undefined };
2846
- case "rows":
2847
- return appendPage(
2848
- state,
2849
- event.tabId,
2850
- event.rows.map((row) => rowFromView(row, firstType(state, event.tabId))),
2851
- event.next,
2852
- event.replace === true,
2853
- undefined,
2854
- );
2855
- case "page-error":
2856
- return applyPageError(state, event.tabId, event.message, event.version);
2917
+ case "list-error":
2918
+ return applyListError(state, event.tabId, event.message, event.version);
2857
2919
  case "object":
2858
2920
  return applyObject(state, event.object, event.version);
2859
2921
  case "object-error":
@@ -2887,10 +2949,6 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2887
2949
  return follow(state);
2888
2950
  case "refresh-start":
2889
2951
  return { ...state, generation: state.generation + 1 };
2890
- case "page-launched":
2891
- return state.pageRequest === undefined
2892
- ? state
2893
- : { ...state, pageRequest: undefined };
2894
2952
  case "object-launched":
2895
2953
  return state.objectRequest === undefined
2896
2954
  ? state
@@ -2899,6 +2957,12 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2899
2957
  return state.sectionRequest === undefined
2900
2958
  ? state
2901
2959
  : { ...state, sectionRequest: undefined };
2960
+ case "start-chat": {
2961
+ const opened = openNamed({ ...state, followPending: false }, event.parent);
2962
+ return opened === state
2963
+ ? state
2964
+ : { ...opened, creating: { parent: event.parent, type: "chat", noteFirst: true } };
2965
+ }
2902
2966
  case "status":
2903
2967
  if (stale(state, event.generation)) return state;
2904
2968
  return {
@@ -2907,6 +2971,8 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2907
2971
  };
2908
2972
  case "key":
2909
2973
  return handleKey(state, event);
2974
+ case "paste":
2975
+ return pasteInto(state, event.text);
2910
2976
  case "resize":
2911
2977
  return {
2912
2978
  ...state,
@@ -2960,7 +3026,7 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2960
3026
  if (tab === undefined) return state;
2961
3027
  return {
2962
3028
  ...state,
2963
- settingsTab: replaceRows(tab, event.entries.map(rowOfEntry), undefined),
3029
+ settingsTab: replaceRows(tab, event.entries.map(rowOfEntry)),
2964
3030
  };
2965
3031
  }
2966
3032
  case "action-done": {
@@ -2969,6 +3035,10 @@ export function reduce(state: TuiState, event: TuiStateEvent): TuiState {
2969
3035
  acting: false,
2970
3036
  actionRequest: undefined,
2971
3037
  answerRequest: undefined,
3038
+ send: undefined,
3039
+ ...(state.input?.kind === "note" && state.input.sending === true
3040
+ ? { input: undefined }
3041
+ : {}),
2972
3042
  };
2973
3043
  return event.opens === undefined ? done : openNamed(done, event.opens);
2974
3044
  }
@@ -3002,33 +3072,6 @@ export function activeTab(state: TuiState): TuiTab | undefined {
3002
3072
  return state.tabs[state.active];
3003
3073
  }
3004
3074
 
3005
- /** The tab whose cursor the keys move. */
3006
- export function activeRows(state: TuiState): readonly TuiRow[] {
3007
- return activeTab(state)?.list.rows ?? [];
3008
- }
3009
-
3010
- /** The label a tab shows, with Pending's badge count. */
3011
- export function tabLabel(tab: TuiTab): string {
3012
- if (tab.kind === "pending" && tab.list.rows.length > 0) {
3013
- return `${tab.title} ${tab.list.rows.length}`;
3014
- }
3015
- return tab.title;
3016
- }
3017
-
3018
- /** The header line, exactly as the design names it. */
3019
- export function headerText(state: TuiState): string {
3020
- const pending = state.tabs.find((tab) => tab.kind === "pending");
3021
- const running = state.tabs.find((tab) => tab.kind === "running");
3022
- const types = state.tabs
3023
- .filter((tab) => tab.kind === "plugin")
3024
- .map((tab) => tab.title);
3025
- return `operad ─ ${[
3026
- `Pending ${pending?.list.rows.length ?? 0}`,
3027
- `Running ${running?.list.rows.length ?? 0}`,
3028
- ...types,
3029
- ].join(" · ")}`;
3030
- }
3031
-
3032
3075
  /** One footer hint: its key, when it has one, and what the key does. */
3033
3076
  export interface TuiFooterSegment {
3034
3077
  readonly key?: string | undefined;
@@ -3044,7 +3087,7 @@ export function footerSegments(state: TuiState): readonly TuiFooterSegment[] {
3044
3087
  const object = state.object;
3045
3088
  const main = mainAction(state);
3046
3089
  if (object !== undefined && main !== undefined) {
3047
- add(mainKey(state), main.name);
3090
+ add(mainKey(state), object.type === "chat" && main.action === "stop" ? "stop" : main.name);
3048
3091
  if (pageActions(object.actions).length > 1) add(":", "more");
3049
3092
  }
3050
3093
  const filtering = filterKeyApplies(state);
@@ -3091,13 +3134,6 @@ export function footerSegments(state: TuiState): readonly TuiFooterSegment[] {
3091
3134
  return segments;
3092
3135
  }
3093
3136
 
3094
- /** The footer for the current screen. */
3095
- export function footerText(state: TuiState): string {
3096
- return footerSegments(state)
3097
- .map((segment) => (segment.key === undefined ? segment.hint : `${segment.key} ${segment.hint}`))
3098
- .join(" · ");
3099
- }
3100
-
3101
3137
  /** Groups of keys, shown with a blank line between them. */
3102
3138
  export const KEY_GROUPS: readonly (readonly (readonly [string, string])[])[] = [
3103
3139
  [
@@ -3107,7 +3143,10 @@ export const KEY_GROUPS: readonly (readonly (readonly [string, string])[])[] = [
3107
3143
  [
3108
3144
  ["↑↓ / j k", "move the row; on a narrow screen with an object open, scroll it"],
3109
3145
  ["g / G", "first or last row; on a narrow screen with an object open, its top or bottom"],
3110
- ["enter", "open the selected row (on a narrow screen); on the open row, move into its list; there, open the row"],
3146
+ ["enter", "open the selected row (on a narrow screen); on the open row, move into its list; there, open the row; on a chat transcript, open or close its thinking and tool run"],
3147
+ ["a", "the open object's first action; stop a chat's reply"],
3148
+ ["/", "type in a chat's input, with its commands"],
3149
+ [":", "the palette of actions, settings and views"],
3111
3150
  ],
3112
3151
  [
3113
3152
  ["PgUp PgDn / b space", "scroll the open section a page"],
@@ -3150,7 +3189,7 @@ export function rowLines(row: TuiRow): number {
3150
3189
 
3151
3190
  /**
3152
3191
  * The rows `[start, end)` that fit in `available` lines around the cursor. The
3153
- * lines after the rows (`loading more…` and a page error) are kept free.
3192
+ * line after the rows (a list error) is kept free.
3154
3193
  */
3155
3194
  export function listWindow(
3156
3195
  list: TuiList,
@@ -3158,7 +3197,7 @@ export function listWindow(
3158
3197
  ): { readonly start: number; readonly end: number } {
3159
3198
  const count = list.rows.length;
3160
3199
  if (count === 0) return { start: 0, end: 0 };
3161
- const tail = (list.loading ? 1 : 0) + (list.error === undefined ? 0 : 1);
3200
+ const tail = list.error === undefined ? 0 : 1;
3162
3201
  // The blank line after the last object is not drawn, so it needs no room.
3163
3202
  const room = Math.max(1, Math.trunc(available) - tail + 1);
3164
3203
  const cursor = Math.min(Math.max(0, list.cursor), count - 1);