@operad-io/tui 0.1.1 → 0.3.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/package.json +3 -4
- package/src/actions.ts +428 -24
- package/src/app.ts +105 -21
- package/src/chat.ts +241 -0
- package/src/client.ts +18 -37
- package/src/commands.ts +128 -0
- package/src/events.ts +113 -19
- package/src/index.ts +1 -0
- package/src/live.ts +39 -4
- package/src/markdown.ts +1 -36
- package/src/recent.ts +3 -6
- package/src/state.ts +313 -273
- package/src/theme.ts +8 -18
- package/src/view/index.ts +130 -25
- package/src/wrap.ts +2 -9
package/src/app.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import {
|
|
8
8
|
CliRenderEvents,
|
|
9
9
|
createCliRenderer,
|
|
10
|
+
decodePasteBytes,
|
|
10
11
|
type CliRenderer,
|
|
11
12
|
} from "@opentui/core";
|
|
12
13
|
import type {
|
|
@@ -25,7 +26,7 @@ import {
|
|
|
25
26
|
type TuiActInput,
|
|
26
27
|
type TuiKeyBinding,
|
|
27
28
|
} from "./client.ts";
|
|
28
|
-
import { defaultOperadHome, messageOf } from "@operad-io/engine";
|
|
29
|
+
import { defaultOperadHome, messageOf, parseObjectId } from "@operad-io/engine";
|
|
29
30
|
import { loadRecent, saveRecent } from "./recent.ts";
|
|
30
31
|
import { discoverServer } from "./discover.ts";
|
|
31
32
|
import { TuiEvents, retryDelay, type TuiEvent } from "./events.ts";
|
|
@@ -93,6 +94,8 @@ export interface TuiRunOptions {
|
|
|
93
94
|
readonly onLog?: ((message: string) => void) | undefined;
|
|
94
95
|
/** Never start `operad serve`, even when none answers. */
|
|
95
96
|
readonly noStart?: boolean | undefined;
|
|
97
|
+
/** Open this parent's new chat when the screen starts; a bare `operad` passes it. */
|
|
98
|
+
readonly startChat?: string | undefined;
|
|
96
99
|
/** The `operad` entry file a started serve runs; without it none is started. */
|
|
97
100
|
readonly serveEntry?: string | undefined;
|
|
98
101
|
/** Prints a line before the full screen opens. */
|
|
@@ -112,6 +115,8 @@ export interface TuiRunHooks {
|
|
|
112
115
|
| ((options: EnsureServerOptions) => Promise<string>)
|
|
113
116
|
| undefined;
|
|
114
117
|
readonly fetch?: FetchLike | undefined;
|
|
118
|
+
/** Turns the terminal's bracketed paste on or off; a test can watch it. */
|
|
119
|
+
readonly pasteMode?: ((on: boolean) => void) | undefined;
|
|
115
120
|
}
|
|
116
121
|
|
|
117
122
|
/** One key press, as far as the reducer cares. */
|
|
@@ -191,6 +196,8 @@ export interface TuiShellDeps {
|
|
|
191
196
|
readonly onFinish?: (() => void) | undefined;
|
|
192
197
|
/** Spaces the live refetches and the retries; the real clock by default. */
|
|
193
198
|
readonly clock?: TuiClock | undefined;
|
|
199
|
+
/** Turns the terminal's bracketed paste on while an input is open. */
|
|
200
|
+
readonly pasteMode?: ((on: boolean) => void) | undefined;
|
|
194
201
|
/** The server's URL, named when it does not answer. */
|
|
195
202
|
readonly url?: string | undefined;
|
|
196
203
|
/**
|
|
@@ -200,6 +207,8 @@ export interface TuiShellDeps {
|
|
|
200
207
|
readonly followDelayMs?: number | undefined;
|
|
201
208
|
/** The palette entries used in earlier runs, the latest first. */
|
|
202
209
|
readonly recent?: readonly string[] | undefined;
|
|
210
|
+
/** Open this parent's new chat once the first lists arrive. */
|
|
211
|
+
readonly startChat?: string | undefined;
|
|
203
212
|
/** The theme list of the `theme` command; without it the list is empty. */
|
|
204
213
|
readonly themes?: TuiThemes | undefined;
|
|
205
214
|
/** Called with the list of recent entries each time it changes. */
|
|
@@ -222,6 +231,7 @@ export interface TuiThemes {
|
|
|
222
231
|
export interface TuiShell {
|
|
223
232
|
readonly state: TuiState;
|
|
224
233
|
key(input: TuiKeyInput): void;
|
|
234
|
+
paste(text: string): void;
|
|
225
235
|
resize(width: number, height: number): void;
|
|
226
236
|
setTheme(theme: TuiTheme): void;
|
|
227
237
|
/** Repaints after the theme files changed; `problem` is shown in the footer, none clears it. */
|
|
@@ -285,6 +295,8 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
285
295
|
themeCurrent: deps.themes?.current(),
|
|
286
296
|
});
|
|
287
297
|
let shownPreview: string | undefined;
|
|
298
|
+
let pasteOpen = false;
|
|
299
|
+
let startChatPending = deps.startChat !== undefined;
|
|
288
300
|
let finished = false;
|
|
289
301
|
let refreshAbort: AbortController | undefined;
|
|
290
302
|
// The requests of the open object: its read and its section pages. They end
|
|
@@ -336,6 +348,10 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
336
348
|
objectRequests.clear();
|
|
337
349
|
events?.stop();
|
|
338
350
|
abort.abort();
|
|
351
|
+
if (pasteOpen) {
|
|
352
|
+
pasteOpen = false;
|
|
353
|
+
deps.pasteMode?.(false);
|
|
354
|
+
}
|
|
339
355
|
try {
|
|
340
356
|
deps.onFinish?.();
|
|
341
357
|
} finally {
|
|
@@ -600,8 +616,7 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
600
616
|
): string | undefined => {
|
|
601
617
|
const parent = entry.row.parent;
|
|
602
618
|
if (parent === undefined || parent.length === 0) return undefined;
|
|
603
|
-
const
|
|
604
|
-
const parentType = colon > 0 ? parent.slice(0, colon) : undefined;
|
|
619
|
+
const parentType = parseObjectId(parent, { allowEmptyId: true })?.type;
|
|
605
620
|
if (parentType !== undefined && !ownTypes.has(parentType)) return parent;
|
|
606
621
|
if (parentType === undefined && !own.has(parent)) return parent;
|
|
607
622
|
const owner = byForm.get(parent);
|
|
@@ -682,10 +697,9 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
682
697
|
readonly type: string;
|
|
683
698
|
readonly label: string;
|
|
684
699
|
} {
|
|
685
|
-
const colon = parent.indexOf(":");
|
|
686
700
|
return {
|
|
687
701
|
row: { id: parent, title: parent, state: "", waiting: false, running: false },
|
|
688
|
-
type:
|
|
702
|
+
type: parseObjectId(parent, { allowEmptyId: true })?.type ?? "",
|
|
689
703
|
label: parent,
|
|
690
704
|
};
|
|
691
705
|
}
|
|
@@ -769,7 +783,7 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
769
783
|
} catch (error) {
|
|
770
784
|
if (abort.signal.aborted) return;
|
|
771
785
|
noteConnection(error);
|
|
772
|
-
dispatch({ type: "
|
|
786
|
+
dispatch({ type: "list-error", tabId: request.tabId, message: messageOf(error) });
|
|
773
787
|
report(error);
|
|
774
788
|
}
|
|
775
789
|
}
|
|
@@ -844,7 +858,7 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
844
858
|
});
|
|
845
859
|
} else {
|
|
846
860
|
dispatch({
|
|
847
|
-
type: "
|
|
861
|
+
type: "list-error",
|
|
848
862
|
tabId: tab.id,
|
|
849
863
|
message: messageOf(result.error),
|
|
850
864
|
});
|
|
@@ -861,6 +875,11 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
861
875
|
retryAttempt = 0;
|
|
862
876
|
}
|
|
863
877
|
if (!failed) dispatch({ type: "status", generation });
|
|
878
|
+
if (startChatPending && typesResult.ok) {
|
|
879
|
+
const parent = deps.startChat;
|
|
880
|
+
startChatPending = false;
|
|
881
|
+
if (parent !== undefined) dispatch({ type: "start-chat", parent });
|
|
882
|
+
}
|
|
864
883
|
if (state.connection === "unreachable") scheduleRetry();
|
|
865
884
|
}
|
|
866
885
|
|
|
@@ -876,15 +895,9 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
876
895
|
}, delay);
|
|
877
896
|
}
|
|
878
897
|
|
|
879
|
-
/**
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
if (targets.all || state.connection !== "ok") {
|
|
883
|
-
await refresh();
|
|
884
|
-
return;
|
|
885
|
-
}
|
|
886
|
-
const signal = abort.signal;
|
|
887
|
-
const fail = (error: unknown): void => {
|
|
898
|
+
/** What a live read says when it fails: the footer line, or the connection state. */
|
|
899
|
+
function liveFail(signal: AbortSignal): (error: unknown) => void {
|
|
900
|
+
return (error: unknown): void => {
|
|
888
901
|
if (signal.aborted || finished) return;
|
|
889
902
|
if (noteConnection(error)) {
|
|
890
903
|
if (state.connection === "unreachable") scheduleRetry();
|
|
@@ -893,6 +906,17 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
893
906
|
dispatch({ type: "status", message: messageOf(error) });
|
|
894
907
|
report(error);
|
|
895
908
|
};
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/** Fetches the targets an event asked for; a lost server asks for everything. */
|
|
912
|
+
async function refetch(targets: Refetch): Promise<void> {
|
|
913
|
+
if (finished || state.connection === "unauthorized") return;
|
|
914
|
+
if (targets.all || state.connection !== "ok") {
|
|
915
|
+
await refresh();
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
const signal = abort.signal;
|
|
919
|
+
const fail = liveFail(signal);
|
|
896
920
|
const work: Promise<void>[] = [];
|
|
897
921
|
if (targets.lists) {
|
|
898
922
|
work.push(refreshPendingRunning(signal, fail));
|
|
@@ -1222,7 +1246,7 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
1222
1246
|
});
|
|
1223
1247
|
if (abort.signal.aborted) return;
|
|
1224
1248
|
dispatch({ type: "action-done", ...settledNotice(ran) });
|
|
1225
|
-
|
|
1249
|
+
refetchObjectAndRow(request.id);
|
|
1226
1250
|
} catch (error) {
|
|
1227
1251
|
if (abort.signal.aborted) return;
|
|
1228
1252
|
if (error instanceof TuiError && error.status !== undefined) {
|
|
@@ -1247,7 +1271,9 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
1247
1271
|
await answer.call(client, { ...request, signal: abort.signal });
|
|
1248
1272
|
if (abort.signal.aborted) return;
|
|
1249
1273
|
dispatch({ type: "action-done" });
|
|
1250
|
-
|
|
1274
|
+
refetchObjectAndRow(answeredRow(request));
|
|
1275
|
+
// The answered item leaves Pending now rather than at the next list window.
|
|
1276
|
+
guard(refreshPendingRunning(abort.signal, liveFail(abort.signal)));
|
|
1251
1277
|
} catch (error) {
|
|
1252
1278
|
if (abort.signal.aborted) return;
|
|
1253
1279
|
if (error instanceof TuiError && error.status !== undefined) {
|
|
@@ -1308,6 +1334,31 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
1308
1334
|
}
|
|
1309
1335
|
}
|
|
1310
1336
|
|
|
1337
|
+
/** The row an answer is about, when a pending list still holds it. */
|
|
1338
|
+
function answeredRow(request: TuiAnswerRequest): string | undefined {
|
|
1339
|
+
for (const tab of state.tabs) {
|
|
1340
|
+
for (const row of tab.list.rows) {
|
|
1341
|
+
if (row.answer?.runId === request.runId && row.answer.waitId === request.waitId) {
|
|
1342
|
+
return row.id;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
return state.object?.id;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
/** After an action or answer: only the open object and the row it changed. */
|
|
1350
|
+
function refetchObjectAndRow(id: string | undefined): void {
|
|
1351
|
+
if (finished) return;
|
|
1352
|
+
const signal = abort.signal;
|
|
1353
|
+
const fail = liveFail(signal);
|
|
1354
|
+
const work: Promise<void>[] = [];
|
|
1355
|
+
if (state.object !== undefined) {
|
|
1356
|
+
work.push(refreshObjectPane(signal, state.object, fail, false));
|
|
1357
|
+
}
|
|
1358
|
+
if (id !== undefined) work.push(refreshRow(id, signal, fail));
|
|
1359
|
+
guard(Promise.all(work).then(() => undefined));
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1311
1362
|
function requestRefresh(): void {
|
|
1312
1363
|
if (finished) return;
|
|
1313
1364
|
guard(refresh());
|
|
@@ -1318,9 +1369,6 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
1318
1369
|
const before = state.recent;
|
|
1319
1370
|
state = reduce(state, event);
|
|
1320
1371
|
if (state.recent !== before) deps.onRecent?.(state.recent);
|
|
1321
|
-
// A request is asked for once: it leaves the state as it is launched.
|
|
1322
|
-
const request = state.pageRequest;
|
|
1323
|
-
if (request !== undefined) state = reduce(state, { type: "page-launched" });
|
|
1324
1372
|
const objectRequest = state.objectRequest;
|
|
1325
1373
|
if (objectRequest !== undefined) {
|
|
1326
1374
|
state = reduce(state, { type: "object-launched" });
|
|
@@ -1392,6 +1440,11 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
1392
1440
|
}, deps.followDelayMs ?? 150)
|
|
1393
1441
|
: undefined;
|
|
1394
1442
|
}
|
|
1443
|
+
const inputOpen = state.input !== undefined;
|
|
1444
|
+
if (inputOpen !== pasteOpen) {
|
|
1445
|
+
pasteOpen = inputOpen;
|
|
1446
|
+
deps.pasteMode?.(pasteOpen);
|
|
1447
|
+
}
|
|
1395
1448
|
view.render(state);
|
|
1396
1449
|
if (state.quit) {
|
|
1397
1450
|
finish();
|
|
@@ -1434,6 +1487,9 @@ export function startShell(deps: TuiShellDeps): TuiShell {
|
|
|
1434
1487
|
get state(): TuiState {
|
|
1435
1488
|
return state;
|
|
1436
1489
|
},
|
|
1490
|
+
paste(text: string): void {
|
|
1491
|
+
dispatch({ type: "paste", text });
|
|
1492
|
+
},
|
|
1437
1493
|
key(input: TuiKeyInput): void {
|
|
1438
1494
|
const sequence = input.sequence ?? "";
|
|
1439
1495
|
const plain = input.ctrl !== true && input.meta !== true;
|
|
@@ -1531,6 +1587,23 @@ export async function runTui(options: TuiRunOptions = {}): Promise<number> {
|
|
|
1531
1587
|
let shell: TuiShell | undefined;
|
|
1532
1588
|
let code = 0;
|
|
1533
1589
|
let stopThemeWatch: (() => void) | undefined;
|
|
1590
|
+
let pasteOn = false;
|
|
1591
|
+
const writePasteMode =
|
|
1592
|
+
options.hooks?.pasteMode ??
|
|
1593
|
+
((on: boolean): void => {
|
|
1594
|
+
// The mode is a terminal's; a redirected stream has no paste to bracket.
|
|
1595
|
+
if (process.stdout.isTTY !== true) return;
|
|
1596
|
+
try {
|
|
1597
|
+
process.stdout.write(on ? "\u001b[?2004h" : "\u001b[?2004l");
|
|
1598
|
+
} catch {
|
|
1599
|
+
// A stream that cannot take the mode is no reason to stop the app.
|
|
1600
|
+
}
|
|
1601
|
+
});
|
|
1602
|
+
const setPasteMode = (on: boolean): void => {
|
|
1603
|
+
if (on === pasteOn) return;
|
|
1604
|
+
pasteOn = on;
|
|
1605
|
+
writePasteMode(on);
|
|
1606
|
+
};
|
|
1534
1607
|
const onAbort = (): void => shell?.stop();
|
|
1535
1608
|
const onSignal = (): void => shell?.stop();
|
|
1536
1609
|
|
|
@@ -1572,6 +1645,7 @@ export async function runTui(options: TuiRunOptions = {}): Promise<number> {
|
|
|
1572
1645
|
url: location.url,
|
|
1573
1646
|
themes,
|
|
1574
1647
|
recent: loadRecent(themeHome),
|
|
1648
|
+
...(options.startChat === undefined ? {} : { startChat: options.startChat }),
|
|
1575
1649
|
onRecent: (recent) => saveRecent(themeHome, recent),
|
|
1576
1650
|
events: (signal) =>
|
|
1577
1651
|
new TuiEvents({ url: location.url, token: location.token, signal }),
|
|
@@ -1582,6 +1656,7 @@ export async function runTui(options: TuiRunOptions = {}): Promise<number> {
|
|
|
1582
1656
|
width: created.width,
|
|
1583
1657
|
height: created.height,
|
|
1584
1658
|
theme: themeOf(created, "dark"),
|
|
1659
|
+
pasteMode: setPasteMode,
|
|
1585
1660
|
childLayout,
|
|
1586
1661
|
onChildLayout: (layout) => saveChildLayout(themeHome, layout),
|
|
1587
1662
|
onFatal: (message) => {
|
|
@@ -1610,6 +1685,9 @@ export async function runTui(options: TuiRunOptions = {}): Promise<number> {
|
|
|
1610
1685
|
created.keyInput.on("keypress", (key) => {
|
|
1611
1686
|
guarded(() => started.key(key));
|
|
1612
1687
|
});
|
|
1688
|
+
created.keyInput.on("paste", (event) => {
|
|
1689
|
+
guarded(() => started.paste(decodePasteBytes(event.bytes)));
|
|
1690
|
+
});
|
|
1613
1691
|
created.on(CliRenderEvents.RESIZE, () => {
|
|
1614
1692
|
guarded(() => started.resize(created.width, created.height));
|
|
1615
1693
|
});
|
|
@@ -1641,6 +1719,12 @@ export async function runTui(options: TuiRunOptions = {}): Promise<number> {
|
|
|
1641
1719
|
messages.push(messageOf(error));
|
|
1642
1720
|
code = 1;
|
|
1643
1721
|
}
|
|
1722
|
+
try {
|
|
1723
|
+
setPasteMode(false);
|
|
1724
|
+
} catch (error) {
|
|
1725
|
+
messages.push(messageOf(error));
|
|
1726
|
+
code = 1;
|
|
1727
|
+
}
|
|
1644
1728
|
if (renderer !== undefined) {
|
|
1645
1729
|
if (!renderer.isDestroyed) renderer.destroy();
|
|
1646
1730
|
// destroy() only starts the restore (mouse tracking off, keyboard
|
package/src/chat.ts
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a chat's transcript reads in the terminal: a message header says who and
|
|
3
|
+
* when in the reader's own time zone, and a run of thinking and tool lines folds
|
|
4
|
+
* into one line. The server's Markdown is not changed; only these lines are.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { layoutMarkdown } from "./layout.ts";
|
|
8
|
+
|
|
9
|
+
/** One run of thinking and tool lines in a transcript. */
|
|
10
|
+
export interface ChatFoldRun {
|
|
11
|
+
/** Stable for one position in one part of a transcript. */
|
|
12
|
+
readonly id: string;
|
|
13
|
+
/** How many thinking and tool lines the run holds. */
|
|
14
|
+
readonly count: number;
|
|
15
|
+
/** The last of them, as written. */
|
|
16
|
+
readonly last: string;
|
|
17
|
+
/** The first Markdown line of the run. */
|
|
18
|
+
readonly start: number;
|
|
19
|
+
/** The line after the run, including the blank lines that follow it. */
|
|
20
|
+
readonly end: number;
|
|
21
|
+
/** The last thinking or tool line. */
|
|
22
|
+
readonly lastAt: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Where one run's rows begin and end in the drawn transcript. */
|
|
26
|
+
export interface ChatRunRows {
|
|
27
|
+
readonly run: ChatFoldRun;
|
|
28
|
+
readonly start: number;
|
|
29
|
+
readonly end: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ChatMarkdownOptions {
|
|
33
|
+
/** The moment that decides whether a header's date is today. */
|
|
34
|
+
readonly now?: number | undefined;
|
|
35
|
+
/** The zone the header's time is drawn in; the terminal's when unset. */
|
|
36
|
+
readonly timeZone?: string | undefined;
|
|
37
|
+
/** The runs a person opened. */
|
|
38
|
+
readonly open?: readonly string[] | undefined;
|
|
39
|
+
/** Fold thinking and tool runs; true unless a caller says otherwise. */
|
|
40
|
+
readonly fold?: boolean | undefined;
|
|
41
|
+
/** The part of a transcript the run ids belong to: content or tail. */
|
|
42
|
+
readonly prefix?: string | undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** One output line, with the run it belongs to when it is part of one. */
|
|
46
|
+
interface ChatSegment {
|
|
47
|
+
readonly text: string;
|
|
48
|
+
readonly run?: ChatFoldRun | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const HEADER = /^\*\*([^*]+)\*\*(?: via ([^·]+?))? · (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})(.*)$/;
|
|
52
|
+
const FENCE = /^\s{0,3}(?:```|~~~)/;
|
|
53
|
+
|
|
54
|
+
function dateParts(
|
|
55
|
+
at: Date,
|
|
56
|
+
timeZone: string | undefined,
|
|
57
|
+
): { readonly year: string; readonly month: string; readonly day: string; readonly hour: string; readonly minute: string } | undefined {
|
|
58
|
+
try {
|
|
59
|
+
const parts = new Intl.DateTimeFormat("en-CA", {
|
|
60
|
+
timeZone,
|
|
61
|
+
year: "numeric",
|
|
62
|
+
month: "2-digit",
|
|
63
|
+
day: "2-digit",
|
|
64
|
+
hour: "2-digit",
|
|
65
|
+
minute: "2-digit",
|
|
66
|
+
hourCycle: "h23",
|
|
67
|
+
}).formatToParts(at);
|
|
68
|
+
const part = (type: Intl.DateTimeFormatPartTypes): string | undefined =>
|
|
69
|
+
parts.find((one) => one.type === type)?.value;
|
|
70
|
+
const year = part("year");
|
|
71
|
+
const month = part("month");
|
|
72
|
+
const day = part("day");
|
|
73
|
+
const hour = part("hour");
|
|
74
|
+
const minute = part("minute");
|
|
75
|
+
return year === undefined || month === undefined || day === undefined || hour === undefined || minute === undefined
|
|
76
|
+
? undefined
|
|
77
|
+
: { year, month, day, hour, minute };
|
|
78
|
+
} catch {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** One header line with the transport left out and the time in the reader's zone. */
|
|
84
|
+
function headerLine(line: string, now: number, timeZone: string | undefined): string {
|
|
85
|
+
const match = HEADER.exec(line);
|
|
86
|
+
if (match === null) return line;
|
|
87
|
+
const [, who = "", , year = "", month = "", day = "", hour = "", minute = "", rest = ""] = match;
|
|
88
|
+
const at = new Date(Date.UTC(Number(year), Number(month) - 1, Number(day), Number(hour), Number(minute)));
|
|
89
|
+
if (Number.isNaN(at.getTime())) return line;
|
|
90
|
+
const local = dateParts(at, timeZone);
|
|
91
|
+
const today = dateParts(new Date(now), timeZone);
|
|
92
|
+
if (local === undefined || today === undefined) return line;
|
|
93
|
+
const isToday =
|
|
94
|
+
local.year === today.year && local.month === today.month && local.day === today.day;
|
|
95
|
+
const when = isToday
|
|
96
|
+
? `${local.hour}:${local.minute}`
|
|
97
|
+
: `${local.year}-${local.month}-${local.day} ${local.hour}:${local.minute}`;
|
|
98
|
+
return `**${who}** · ${when}${rest}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
type LineKind = "thinking" | "tool" | "text";
|
|
102
|
+
|
|
103
|
+
function kindOf(line: string, code: boolean): LineKind {
|
|
104
|
+
if (code) return "text";
|
|
105
|
+
const trimmed = line.trimStart();
|
|
106
|
+
if (trimmed === ">" || trimmed.startsWith("> ")) return "thinking";
|
|
107
|
+
if (trimmed.startsWith("tool ")) return "tool";
|
|
108
|
+
return "text";
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function codeLines(lines: readonly string[]): readonly boolean[] {
|
|
112
|
+
const out: boolean[] = [];
|
|
113
|
+
let code = false;
|
|
114
|
+
for (const line of lines) {
|
|
115
|
+
if (FENCE.test(line)) {
|
|
116
|
+
out.push(true);
|
|
117
|
+
code = !code;
|
|
118
|
+
} else {
|
|
119
|
+
out.push(code);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The thinking and tool runs in one part of a chat transcript. */
|
|
126
|
+
export function chatRuns(markdown: string, prefix = "c"): readonly ChatFoldRun[] {
|
|
127
|
+
if (markdown === "") return [];
|
|
128
|
+
const lines = markdown.split("\n");
|
|
129
|
+
const code = codeLines(lines);
|
|
130
|
+
const runs: ChatFoldRun[] = [];
|
|
131
|
+
let at = 0;
|
|
132
|
+
while (at < lines.length) {
|
|
133
|
+
if (kindOf(lines[at] ?? "", code[at] === true) === "text") {
|
|
134
|
+
at += 1;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const start = at;
|
|
138
|
+
let last = "";
|
|
139
|
+
let count = 0;
|
|
140
|
+
let lastAt = start;
|
|
141
|
+
while (at < lines.length) {
|
|
142
|
+
const line = lines[at] ?? "";
|
|
143
|
+
const kind = kindOf(line, code[at] === true);
|
|
144
|
+
if (kind === "text" && line.trim() !== "") break;
|
|
145
|
+
if (kind !== "text") {
|
|
146
|
+
count += 1;
|
|
147
|
+
last = line;
|
|
148
|
+
lastAt = at;
|
|
149
|
+
}
|
|
150
|
+
at += 1;
|
|
151
|
+
}
|
|
152
|
+
runs.push({
|
|
153
|
+
id: `${prefix}:${start}`,
|
|
154
|
+
count,
|
|
155
|
+
last,
|
|
156
|
+
start,
|
|
157
|
+
end: at,
|
|
158
|
+
lastAt,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return runs;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The output lines of one part of a transcript, each with the run it belongs to. */
|
|
165
|
+
function chatSegments(markdown: string, options: ChatMarkdownOptions): ChatSegment[] {
|
|
166
|
+
if (markdown === "") return [];
|
|
167
|
+
const now = options.now ?? Date.now();
|
|
168
|
+
const timeZone = options.timeZone;
|
|
169
|
+
const open = new Set(options.open ?? []);
|
|
170
|
+
const prefix = options.prefix ?? "c";
|
|
171
|
+
const lines = markdown.split("\n");
|
|
172
|
+
const code = codeLines(lines);
|
|
173
|
+
const source = lines.map((line, at) =>
|
|
174
|
+
code[at] === true ? line : headerLine(line, now, timeZone),
|
|
175
|
+
);
|
|
176
|
+
if (options.fold === false) return source.map((text) => ({ text }));
|
|
177
|
+
const runs = chatRuns(markdown, prefix);
|
|
178
|
+
if (runs.length === 0) return source.map((text) => ({ text }));
|
|
179
|
+
const byStart = new Map(runs.map((run) => [run.start, run]));
|
|
180
|
+
const segments: ChatSegment[] = [];
|
|
181
|
+
let at = 0;
|
|
182
|
+
while (at < source.length) {
|
|
183
|
+
const run = byStart.get(at);
|
|
184
|
+
if (run === undefined) {
|
|
185
|
+
segments.push({ text: source[at] ?? "" });
|
|
186
|
+
at += 1;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (open.has(run.id)) {
|
|
190
|
+
for (let line = run.start; line < run.end; line += 1) {
|
|
191
|
+
segments.push({ text: source[line] ?? "", run });
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
segments.push({ text: `${run.count} · ${run.last}`, run });
|
|
195
|
+
// The blank lines after the last line keep the next message apart.
|
|
196
|
+
for (let line = run.lastAt + 1; line < run.end; line += 1) {
|
|
197
|
+
segments.push({ text: source[line] ?? "" });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
at = run.end;
|
|
201
|
+
}
|
|
202
|
+
return segments;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A chat transcript's Markdown as the terminal draws it. */
|
|
206
|
+
export function chatMarkdown(markdown: string, options: ChatMarkdownOptions = {}): string {
|
|
207
|
+
return chatSegments(markdown, options).map((segment) => segment.text).join("\n");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Rows a prefix takes when the next line is appended, without trimming its blanks. */
|
|
211
|
+
function rowsThrough(lines: readonly string[], width: number): number {
|
|
212
|
+
const text = `${lines.join("\n")}\nSENTINEL`;
|
|
213
|
+
return layoutMarkdown(text, width).length - 1;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Where each run's rows lie in one drawn part of a transcript. */
|
|
217
|
+
export function chatRunRows(
|
|
218
|
+
markdown: string,
|
|
219
|
+
options: ChatMarkdownOptions & { readonly width: number },
|
|
220
|
+
): readonly ChatRunRows[] {
|
|
221
|
+
const segments = chatSegments(markdown, options);
|
|
222
|
+
const seen: ChatRunRows[] = [];
|
|
223
|
+
const prefix: string[] = [];
|
|
224
|
+
let at = 0;
|
|
225
|
+
while (at < segments.length) {
|
|
226
|
+
const run = segments[at]?.run;
|
|
227
|
+
if (run === undefined) {
|
|
228
|
+
prefix.push(segments[at]?.text ?? "");
|
|
229
|
+
at += 1;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const start = rowsThrough(prefix, options.width);
|
|
233
|
+
let end = at;
|
|
234
|
+
while (end < segments.length && segments[end]?.run?.id === run.id) end += 1;
|
|
235
|
+
const through = [...prefix, ...segments.slice(at, end).map((segment) => segment.text)];
|
|
236
|
+
seen.push({ run, start, end: rowsThrough(through, options.width) });
|
|
237
|
+
prefix.push(...segments.slice(at, end).map((segment) => segment.text));
|
|
238
|
+
at = end;
|
|
239
|
+
}
|
|
240
|
+
return seen;
|
|
241
|
+
}
|
package/src/client.ts
CHANGED
|
@@ -5,12 +5,15 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import type {
|
|
8
|
+
KeyBinding,
|
|
8
9
|
PendingItem,
|
|
9
10
|
RunningItem,
|
|
11
|
+
SettingInfo,
|
|
10
12
|
ViewActionRun,
|
|
11
13
|
ViewListPage,
|
|
12
14
|
ViewObject,
|
|
13
15
|
ViewSectionContent,
|
|
16
|
+
ViewTypeInfo,
|
|
14
17
|
} from "@operad-io/engine";
|
|
15
18
|
import { isRecord, messageOf } from "@operad-io/engine";
|
|
16
19
|
|
|
@@ -29,46 +32,21 @@ export interface TuiLevel {
|
|
|
29
32
|
readonly parent?: string | undefined;
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
/** One object type a client can list. */
|
|
33
|
-
export
|
|
34
|
-
readonly
|
|
35
|
-
readonly
|
|
36
|
-
/** The plugin the type belongs to. */
|
|
37
|
-
readonly plugin?: string;
|
|
38
|
-
/** The type's filters in display order; absent when it declares none. */
|
|
39
|
-
readonly filters?: readonly { readonly name: string; readonly states?: readonly string[] }[] | undefined;
|
|
40
|
-
/** The filter a list opens on; present with `filters`. */
|
|
41
|
-
readonly defaultFilter?: string | undefined;
|
|
42
|
-
/** The object types an action that creates this type works on; absent when none does. */
|
|
43
|
-
readonly parents?: readonly string[] | undefined;
|
|
44
|
-
/**
|
|
45
|
-
* The plugin's objects belong under the settings view instead of a tab of
|
|
46
|
-
* their own; a client gives that plugin no top-bar tab.
|
|
47
|
-
*/
|
|
35
|
+
/** One object type a client can list; an older server may name no plugin. */
|
|
36
|
+
export type TuiTypeInfo = Omit<ViewTypeInfo, "plugin" | "settings" | "pluginTitle"> & {
|
|
37
|
+
readonly pluginTitle?: string | undefined;
|
|
38
|
+
readonly plugin?: string | undefined;
|
|
48
39
|
readonly settings?: boolean | undefined;
|
|
49
|
-
}
|
|
40
|
+
};
|
|
50
41
|
|
|
51
|
-
/** One entry of `GET /v1/keys
|
|
52
|
-
export
|
|
53
|
-
readonly key: string;
|
|
54
|
-
readonly action: string;
|
|
55
|
-
readonly source: "plugin" | "user";
|
|
56
|
-
}
|
|
42
|
+
/** One entry of `GET /v1/keys`: the server sends only what a terminal needs. */
|
|
43
|
+
export type TuiKeyBinding = Pick<KeyBinding, "key" | "action" | "source">;
|
|
57
44
|
|
|
58
|
-
/** One entry of `GET /v1/settings
|
|
59
|
-
export
|
|
60
|
-
/** `<plugin>.<path>`; a project file adds its name. */
|
|
61
|
-
readonly name: string;
|
|
62
|
-
/** `global`, or the name of the repository the setting belongs to. */
|
|
63
|
-
readonly scope: string;
|
|
45
|
+
/** One entry of `GET /v1/settings`; a server value is JSON, a test may give any value. */
|
|
46
|
+
export type TuiSetting = Omit<SettingInfo, "value" | "default"> & {
|
|
64
47
|
readonly value?: unknown;
|
|
65
48
|
readonly default?: unknown;
|
|
66
|
-
|
|
67
|
-
readonly type?: string | undefined;
|
|
68
|
-
readonly enum?: readonly (string | number | boolean)[] | undefined;
|
|
69
|
-
readonly editable: boolean;
|
|
70
|
-
readonly confirm: boolean;
|
|
71
|
-
}
|
|
49
|
+
};
|
|
72
50
|
|
|
73
51
|
/** What `POST /v1/settings` carries. */
|
|
74
52
|
export interface TuiSetInput {
|
|
@@ -109,7 +87,9 @@ export interface TuiActInput {
|
|
|
109
87
|
export interface TuiAnswerInput {
|
|
110
88
|
readonly runId: string;
|
|
111
89
|
readonly waitId: string;
|
|
112
|
-
readonly choice
|
|
90
|
+
readonly choice?: string | undefined;
|
|
91
|
+
/** The person's answers to a form's questions. */
|
|
92
|
+
readonly answers?: readonly unknown[] | undefined;
|
|
113
93
|
readonly note?: string | undefined;
|
|
114
94
|
readonly confirmed?: boolean | undefined;
|
|
115
95
|
/** Cancels the request; it then rejects. */
|
|
@@ -402,7 +382,8 @@ export class TuiClient {
|
|
|
402
382
|
body: {
|
|
403
383
|
runId: input.runId,
|
|
404
384
|
waitId: input.waitId,
|
|
405
|
-
choice: input.choice,
|
|
385
|
+
...(input.choice === undefined ? {} : { choice: input.choice }),
|
|
386
|
+
...(input.answers === undefined ? {} : { answers: input.answers }),
|
|
406
387
|
...(input.note === undefined ? {} : { note: input.note }),
|
|
407
388
|
...(input.confirmed === true ? { confirmed: true } : {}),
|
|
408
389
|
},
|