@turri-dev/cli 0.4.2-next.a6867e8 → 0.4.3-next.3fc888b
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/README.md +1 -0
- package/dist/background-skills.d.ts +4 -1
- package/dist/background-skills.js +9 -1
- package/dist/capture/agents.d.ts +391 -0
- package/dist/capture/agents.js +338 -0
- package/dist/capture/area.d.ts +58 -0
- package/dist/capture/area.js +197 -0
- package/dist/capture/assemble.d.ts +122 -0
- package/dist/capture/assemble.js +452 -0
- package/dist/capture/capture.d.ts +116 -0
- package/dist/capture/capture.js +1281 -0
- package/dist/capture/chat.d.ts +29 -0
- package/dist/capture/chat.js +94 -0
- package/dist/capture/checks.d.ts +142 -0
- package/dist/capture/checks.js +643 -0
- package/dist/capture/config.d.ts +13 -0
- package/dist/capture/config.js +60 -0
- package/dist/capture/interface.d.ts +71 -0
- package/dist/capture/interface.js +199 -0
- package/dist/capture/map.d.ts +58 -0
- package/dist/capture/map.js +530 -0
- package/dist/capture/plan.d.ts +8 -0
- package/dist/capture/plan.js +13 -0
- package/dist/capture/progress.d.ts +109 -0
- package/dist/capture/progress.js +240 -0
- package/dist/capture/report.d.ts +75 -0
- package/dist/capture/report.js +268 -0
- package/dist/capture/scripts.d.ts +26 -0
- package/dist/capture/scripts.js +33 -0
- package/dist/capture/state.d.ts +299 -0
- package/dist/capture/state.js +43 -0
- package/dist/capture/trial.d.ts +34 -0
- package/dist/capture/trial.js +89 -0
- package/dist/capture-cli.d.ts +1 -0
- package/dist/capture-cli.js +77 -0
- package/dist/chat-commands.d.ts +2 -10
- package/dist/chat-commands.js +3 -57
- package/dist/chat-tab-header.d.ts +15 -0
- package/dist/chat-tab-header.js +78 -0
- package/dist/chat-tabs.d.ts +26 -0
- package/dist/chat-tabs.js +100 -0
- package/dist/chat-terminal.js +57 -8
- package/dist/chat-waiting.d.ts +4 -1
- package/dist/chat-waiting.js +7 -4
- package/dist/chats.d.ts +12 -7
- package/dist/chats.js +35 -21
- package/dist/cli.js +32 -14
- package/dist/config-edit.js +4 -2
- package/dist/config.d.ts +15 -2
- package/dist/config.js +62 -4
- package/dist/extension.d.ts +1 -0
- package/dist/extension.js +262 -136
- package/dist/identity.js +3 -2
- package/dist/init-config.js +2 -0
- package/dist/loop-cli.js +5 -1
- package/dist/loop-tools.d.ts +2 -3
- package/dist/loop-tools.js +12 -13
- package/dist/new-project.d.ts +5 -0
- package/dist/new-project.js +11 -0
- package/dist/pi-launch.js +2 -0
- package/dist/pi-themes.d.ts +3 -0
- package/dist/pi-themes.js +21 -0
- package/dist/pi.js +2 -2
- package/dist/plain-code-blocks.d.ts +14 -0
- package/dist/plain-code-blocks.js +56 -0
- package/dist/pointer-hover.d.ts +12 -0
- package/dist/pointer-hover.js +48 -0
- package/dist/question-dialog.d.ts +5 -0
- package/dist/question-dialog.js +20 -1
- package/dist/regenerate/chunk-plan.d.ts +2 -11
- package/dist/regenerate/chunk-plan.js +9 -20
- package/dist/regenerate/command-check.d.ts +9 -0
- package/dist/regenerate/command-check.js +163 -14
- package/dist/regenerate/job-panel.d.ts +5 -107
- package/dist/regenerate/job-panel.js +4 -289
- package/dist/regenerate/jobs-tree.d.ts +1 -0
- package/dist/regenerate/jobs-tree.js +5 -1
- package/dist/regenerate/loop.d.ts +6 -14
- package/dist/regenerate/loop.js +51 -35
- package/dist/regenerate/side-panel.d.ts +6 -1
- package/dist/regenerate/side-panel.js +158 -22
- package/dist/regenerate/spec-map-view.d.ts +11 -3
- package/dist/regenerate/spec-map-view.js +22 -2
- package/dist/regenerate/test-scripts.d.ts +8 -0
- package/dist/regenerate/test-scripts.js +20 -5
- package/dist/reopen.d.ts +29 -0
- package/dist/reopen.js +47 -0
- package/dist/resume-line.d.ts +4 -1
- package/dist/resume-line.js +5 -2
- package/dist/sentinel-chat.js +7 -2
- package/dist/sentinel.d.ts +1 -1
- package/dist/sentinel.js +4 -2
- package/dist/setup.js +9 -1
- package/dist/terminal-notifications.d.ts +31 -0
- package/dist/terminal-notifications.js +134 -0
- package/docs/README.md +2 -0
- package/docs/capture.md +207 -0
- package/docs/chats.md +8 -7
- package/docs/config.md +18 -3
- package/docs/development.md +4 -1
- package/docs/hosting.md +2 -2
- package/docs/launch.md +2 -2
- package/docs/regenerating.md +9 -8
- package/docs/sandbox.md +3 -1
- package/docs/sentinel.md +11 -11
- package/docs/skills-and-gates.md +1 -1
- package/docs/theming.md +3 -3
- package/docs/using.md +12 -5
- package/genome/capture/capture.md +26 -0
- package/genome/capture/check-conventions.md +5 -0
- package/genome/capture/check-reference.md +11 -0
- package/genome/capture/compact.md +7 -0
- package/genome/capture/concepts.md +3 -0
- package/genome/capture/divide.md +9 -0
- package/genome/capture/feature.md +45 -0
- package/genome/capture/interface.md +9 -0
- package/genome/capture/modules.md +23 -0
- package/genome/capture/outline.md +11 -0
- package/genome/capture/readability.md +12 -0
- package/genome/capture/reference.md +10 -0
- package/genome/capture/split.md +9 -0
- package/genome/capture/survey.md +37 -0
- package/genome/capture/trial.md +18 -0
- package/genome/capture/verify.md +7 -0
- package/genome/rules/turri-regeneration.md +2 -2
- package/package.json +3 -3
- package/dist/regenerate/steer.d.ts +0 -34
- package/dist/regenerate/steer.js +0 -91
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { ensureInScratch } from "../regenerate/paths.js";
|
|
4
|
+
/**
|
|
5
|
+
* What a capture keeps as it goes, in `<plain>/.turri/capture/state.json`: every agent's answer, saved as it comes. A
|
|
6
|
+
* capture stopped part-way - turri quit, the machine slept - is started again from it, asking no agent twice; a capture
|
|
7
|
+
* that finished is never run over.
|
|
8
|
+
*/
|
|
9
|
+
export const STATE_VERSION = 1;
|
|
10
|
+
export function captureDir(plainDir) {
|
|
11
|
+
const scratch = join(plainDir, ".turri");
|
|
12
|
+
return ensureInScratch(scratch, join(scratch, "capture"));
|
|
13
|
+
}
|
|
14
|
+
export function stateFile(plainDir) {
|
|
15
|
+
return join(plainDir, ".turri", "capture", "state.json");
|
|
16
|
+
}
|
|
17
|
+
export function reportFile(plainDir) {
|
|
18
|
+
return join(plainDir, ".turri", "capture", "report.md");
|
|
19
|
+
}
|
|
20
|
+
/** The state a capture left in `plainDir`, or undefined when there is none - or one turri cannot read, which is started over. */
|
|
21
|
+
export function readState(plainDir) {
|
|
22
|
+
const file = stateFile(plainDir);
|
|
23
|
+
if (!existsSync(file))
|
|
24
|
+
return undefined;
|
|
25
|
+
try {
|
|
26
|
+
const state = JSON.parse(readFileSync(file, "utf8"));
|
|
27
|
+
return state.version === STATE_VERSION ? state : undefined;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function newState(root) {
|
|
34
|
+
return { version: STATE_VERSION, root, started: new Date().toISOString(), areas: {}, features: {}, reconciled: {}, edits: {}, edited: [] };
|
|
35
|
+
}
|
|
36
|
+
/** Save the state whole, through a file beside it: a capture stopped mid-write leaves the last state as it was. */
|
|
37
|
+
export function saveState(plainDir, state) {
|
|
38
|
+
const file = stateFile(plainDir);
|
|
39
|
+
captureDir(plainDir);
|
|
40
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
41
|
+
writeFileSync(temporary, `${JSON.stringify(state, null, 2)}\n`);
|
|
42
|
+
renameSync(temporary, file);
|
|
43
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AssembledModule } from "./assemble.ts";
|
|
2
|
+
import { type CodeMap } from "./map.ts";
|
|
3
|
+
import type { TrialBatch } from "./state.ts";
|
|
4
|
+
/**
|
|
5
|
+
* The scenarios tried on the original, before the ***plain is written. They are what the regenerated code's conformance
|
|
6
|
+
* tests check, written by agents that read the original's code and tests but never ran it - so a scenario can expect a
|
|
7
|
+
* terminal its test will not give, a site on the internet, wrapping at a width nobody set, or a message the original never
|
|
8
|
+
* prints. A coding agent then builds to it, or its module's conformance tests fail for good. So an agent with a shell on
|
|
9
|
+
* a copy of the original runs each as its conformance test will, and corrects what the original does otherwise.
|
|
10
|
+
*/
|
|
11
|
+
/** The most scenarios one agent tries on the original: a module's are tried by as many agents as it takes, one after another. */
|
|
12
|
+
export declare const SCENARIOS_PER_TRIAL = 20;
|
|
13
|
+
/** A scenario as an agent is given it to try. */
|
|
14
|
+
export interface TrialScenario {
|
|
15
|
+
funcId: string;
|
|
16
|
+
scenarioId: string;
|
|
17
|
+
kind: "conformance" | "acceptance";
|
|
18
|
+
text: string;
|
|
19
|
+
}
|
|
20
|
+
/** A module's scenarios, in its order, so many an agent. */
|
|
21
|
+
export declare function trialBatches(module: AssembledModule, size?: number): TrialScenario[][];
|
|
22
|
+
/**
|
|
23
|
+
* A copy of the original codebase, for an agent to install and run without touching it: every file the map has - what
|
|
24
|
+
* git tracks, none of what it ignores, so none of the dependencies installed - and the lock files beside its manifests,
|
|
25
|
+
* which the map leaves out, so the copy installs the versions the original pins; but turri's own config files.
|
|
26
|
+
*/
|
|
27
|
+
export declare function copyOriginal(map: CodeMap, target: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* A module with what its scenarios' trials found applied: each scenario the original does otherwise corrected, each that
|
|
30
|
+
* nothing outside can check removed, each functional spec the original contradicts corrected.
|
|
31
|
+
*/
|
|
32
|
+
export declare function applyTrials(module: AssembledModule, batches: TrialBatch[]): void;
|
|
33
|
+
/** The test reqs the trials of some modules found a conformance test needs, each once, in the order they were found. */
|
|
34
|
+
export declare function trialTestReqs(batches: TrialBatch[]): string[];
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join, posix } from "node:path";
|
|
3
|
+
import { LOCK_FILES } from "./map.js";
|
|
4
|
+
/**
|
|
5
|
+
* The scenarios tried on the original, before the ***plain is written. They are what the regenerated code's conformance
|
|
6
|
+
* tests check, written by agents that read the original's code and tests but never ran it - so a scenario can expect a
|
|
7
|
+
* terminal its test will not give, a site on the internet, wrapping at a width nobody set, or a message the original never
|
|
8
|
+
* prints. A coding agent then builds to it, or its module's conformance tests fail for good. So an agent with a shell on
|
|
9
|
+
* a copy of the original runs each as its conformance test will, and corrects what the original does otherwise.
|
|
10
|
+
*/
|
|
11
|
+
/** The most scenarios one agent tries on the original: a module's are tried by as many agents as it takes, one after another. */
|
|
12
|
+
export const SCENARIOS_PER_TRIAL = 20;
|
|
13
|
+
/** A module's scenarios, in its order, so many an agent. */
|
|
14
|
+
export function trialBatches(module, size = SCENARIOS_PER_TRIAL) {
|
|
15
|
+
const scenarios = module.features.flatMap((feature) => feature.specs.flatMap((spec) => [
|
|
16
|
+
...spec.conformance.map((scenario) => ({ funcId: spec.funcId, scenarioId: scenario.id, kind: "conformance", text: scenario.text })),
|
|
17
|
+
...spec.acceptance.map((scenario) => ({ funcId: spec.funcId, scenarioId: scenario.id, kind: "acceptance", text: scenario.text })),
|
|
18
|
+
]));
|
|
19
|
+
const batches = [];
|
|
20
|
+
for (let at = 0; at < scenarios.length; at += size)
|
|
21
|
+
batches.push(scenarios.slice(at, at + size));
|
|
22
|
+
return batches;
|
|
23
|
+
}
|
|
24
|
+
/** turri's own config files: a copy holding one would be a turri project of its own, read with its settings. */
|
|
25
|
+
function isTurriConfig(path) {
|
|
26
|
+
return /^config\.turri(\..+)?\.yaml$/.test(basename(path));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A copy of the original codebase, for an agent to install and run without touching it: every file the map has - what
|
|
30
|
+
* git tracks, none of what it ignores, so none of the dependencies installed - and the lock files beside its manifests,
|
|
31
|
+
* which the map leaves out, so the copy installs the versions the original pins; but turri's own config files.
|
|
32
|
+
*/
|
|
33
|
+
export function copyOriginal(map, target) {
|
|
34
|
+
const copy = (path) => {
|
|
35
|
+
const to = join(target, ...path.split("/"));
|
|
36
|
+
mkdirSync(dirname(to), { recursive: true });
|
|
37
|
+
try {
|
|
38
|
+
copyFileSync(join(map.root, ...path.split("/")), to);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
/* gone since the map was made: nothing to copy */
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
for (const file of map.files)
|
|
45
|
+
if (!isTurriConfig(file.path))
|
|
46
|
+
copy(file.path);
|
|
47
|
+
const manifestDirs = new Set(map.files.filter((file) => file.kind === "manifest").map((file) => posix.dirname(file.path)));
|
|
48
|
+
for (const dir of manifestDirs) {
|
|
49
|
+
for (const name of LOCK_FILES) {
|
|
50
|
+
const path = dir === "." ? name : posix.join(dir, name);
|
|
51
|
+
if (existsSync(join(map.root, ...path.split("/"))))
|
|
52
|
+
copy(path);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A module with what its scenarios' trials found applied: each scenario the original does otherwise corrected, each that
|
|
58
|
+
* nothing outside can check removed, each functional spec the original contradicts corrected.
|
|
59
|
+
*/
|
|
60
|
+
export function applyTrials(module, batches) {
|
|
61
|
+
const specs = module.features.flatMap((feature) => feature.specs);
|
|
62
|
+
for (const batch of batches) {
|
|
63
|
+
if (!("results" in batch))
|
|
64
|
+
continue;
|
|
65
|
+
for (const correction of batch.specs) {
|
|
66
|
+
const spec = specs.find((candidate) => candidate.funcId === correction.funcId);
|
|
67
|
+
if (spec !== undefined)
|
|
68
|
+
spec.text = correction.text;
|
|
69
|
+
}
|
|
70
|
+
for (const result of batch.results) {
|
|
71
|
+
const spec = specs.find((candidate) => candidate.funcId === result.funcId);
|
|
72
|
+
if (spec === undefined)
|
|
73
|
+
continue;
|
|
74
|
+
if (result.outcome === "corrected" && result.text !== undefined) {
|
|
75
|
+
for (const scenario of [...spec.conformance, ...spec.acceptance])
|
|
76
|
+
if (scenario.id === result.scenarioId)
|
|
77
|
+
scenario.text = result.text;
|
|
78
|
+
}
|
|
79
|
+
if (result.outcome === "removed") {
|
|
80
|
+
spec.conformance = spec.conformance.filter((scenario) => scenario.id !== result.scenarioId);
|
|
81
|
+
spec.acceptance = spec.acceptance.filter((scenario) => scenario.id !== result.scenarioId);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** The test reqs the trials of some modules found a conformance test needs, each once, in the order they were found. */
|
|
87
|
+
export function trialTestReqs(batches) {
|
|
88
|
+
return [...new Set(batches.flatMap((batch) => ("testReqs" in batch ? batch.testReqs : [])))];
|
|
89
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { relative, resolve } from "node:path";
|
|
2
|
+
import { CaptureError, runCapture } from "./capture/capture.js";
|
|
3
|
+
import { c } from "./module-report.js";
|
|
4
|
+
/**
|
|
5
|
+
* `turri capture [path] [--harness-model <m>] [--coding-model <m>] [--into <folder>] [--parallel <n>]`: capture the
|
|
6
|
+
* codebase at `path` - the current folder by default - into ***plain in `<path>/plain`, offline: nothing is asked, and
|
|
7
|
+
* what was decided is in the report. A capture stopped part-way is gone on with by running it again.
|
|
8
|
+
*/
|
|
9
|
+
const USAGE = [
|
|
10
|
+
"usage: turri capture [path] [--harness-model <m>] [--coding-model <m>] [--into <folder>] [--parallel <n>]",
|
|
11
|
+
"",
|
|
12
|
+
"Capture the codebase at path (the current folder by default) into ***plain, in <path>/plain - nothing is asked:",
|
|
13
|
+
"what capture decided, left out or could not capture is in plain/.turri/capture/report.md. Run it again to go on",
|
|
14
|
+
"with a capture that was stopped.",
|
|
15
|
+
"",
|
|
16
|
+
" --harness-model <m> the model that writes the ***plain, kept as the new project's harness-model",
|
|
17
|
+
" --coding-model <m> the model for capture analysis (low thinking) and testing scripts, kept as coding-model",
|
|
18
|
+
" --into <folder> where the ***plain goes, instead of <path>/plain",
|
|
19
|
+
" --parallel <n> how many agents work at once (4)",
|
|
20
|
+
].join("\n");
|
|
21
|
+
function option(args, name) {
|
|
22
|
+
const at = args.findIndex((arg) => arg === name || arg.startsWith(`${name}=`));
|
|
23
|
+
if (at === -1)
|
|
24
|
+
return undefined;
|
|
25
|
+
const arg = args[at];
|
|
26
|
+
return arg.includes("=") ? arg.slice(arg.indexOf("=") + 1) : args[at + 1];
|
|
27
|
+
}
|
|
28
|
+
async function main() {
|
|
29
|
+
const args = process.argv.slice(2);
|
|
30
|
+
if (args.includes("-h") || args.includes("--help")) {
|
|
31
|
+
console.log(USAGE);
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
// cli.ts hands --coding-model on as --model, as every command's agents take it.
|
|
35
|
+
const valued = ["--harness-model", "--model", "--into", "--parallel"];
|
|
36
|
+
const positional = args.filter((arg, at) => !arg.startsWith("-") && !valued.includes(args[at - 1] ?? ""));
|
|
37
|
+
const unknown = args.filter((arg, at) => arg.startsWith("-") && !valued.some((name) => arg === name || arg.startsWith(`${name}=`)) && !valued.includes(args[at - 1] ?? ""));
|
|
38
|
+
if (positional.length > 1 || unknown.length > 0) {
|
|
39
|
+
console.error(`${unknown.length > 0 ? `unknown option: ${unknown[0]}` : "capture takes one path"}\n\n${USAGE}`);
|
|
40
|
+
return 1;
|
|
41
|
+
}
|
|
42
|
+
const parallelText = option(args, "--parallel");
|
|
43
|
+
const parallel = parallelText !== undefined ? Number.parseInt(parallelText, 10) : undefined;
|
|
44
|
+
if (parallel !== undefined && (!Number.isInteger(parallel) || parallel < 1)) {
|
|
45
|
+
console.error(`--parallel takes a whole number of at least 1, not ${parallelText}.`);
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
const root = resolve(positional[0] ?? ".");
|
|
49
|
+
const into = option(args, "--into");
|
|
50
|
+
const harnessModel = option(args, "--harness-model");
|
|
51
|
+
const codingModel = option(args, "--model");
|
|
52
|
+
const started = Date.now();
|
|
53
|
+
console.log(c.bold(`capturing ${root}`));
|
|
54
|
+
try {
|
|
55
|
+
const result = await runCapture({
|
|
56
|
+
root,
|
|
57
|
+
// The captured project starts as the config in use here: the one turri reads where the command is run.
|
|
58
|
+
configFrom: process.cwd(),
|
|
59
|
+
...(into !== undefined ? { plainDir: resolve(into) } : {}),
|
|
60
|
+
...(harnessModel !== undefined ? { model: harnessModel, harnessModel } : {}),
|
|
61
|
+
...(codingModel !== undefined ? { codingModel } : {}),
|
|
62
|
+
...(parallel !== undefined ? { parallel } : {}),
|
|
63
|
+
progress: (line) => console.log(c.dim(` ${line}`)),
|
|
64
|
+
});
|
|
65
|
+
const minutes = Math.round((Date.now() - started) / 60_000);
|
|
66
|
+
console.log(`\n${c.success(c.bold(result.summary))} ${c.dim(`(${minutes} min)`)}`);
|
|
67
|
+
console.log(c.dim(`the report: ${relative(process.cwd(), result.reportFile) || result.reportFile}`));
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error(`\n${c.error(c.bold("not captured"))} ${error instanceof Error ? error.message : String(error)}`);
|
|
72
|
+
if (!(error instanceof CaptureError) && error instanceof Error && error.stack !== undefined)
|
|
73
|
+
console.error(c.dim(error.stack));
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
process.exit(await main());
|
package/dist/chat-commands.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { type
|
|
2
|
+
import { type TurriCore } from "./chats.ts";
|
|
3
3
|
/**
|
|
4
|
-
* The terminal's
|
|
5
|
-
* editor naming each chat with what it is doing.
|
|
4
|
+
* The keyboard counterparts to the terminal's chat tabs: `/chat` - new, close, a number, or a pick - and its keys.
|
|
6
5
|
*/
|
|
7
6
|
/** A fresh chat. */
|
|
8
7
|
export declare const NEW_CHAT_KEY = "alt+n";
|
|
@@ -15,11 +14,4 @@ export declare const CLOSE_CHAT_KEY = "alt+w";
|
|
|
15
14
|
export declare const OPTION_AS_META: string;
|
|
16
15
|
/** Said once a session, as its second chat opens, on a Mac. */
|
|
17
16
|
export declare function tellOfOptionOnce(session: TurriCore, platform?: NodeJS.Platform): void;
|
|
18
|
-
/** What a chat's mark in the strip and the picker says it is doing. */
|
|
19
|
-
export declare function chatMark(chat: ChatInfo, now?: number): string;
|
|
20
|
-
/** The strip: each chat by its number, its title and its mark - the one shown set apart - on one line, a rule between them. */
|
|
21
|
-
export declare function chatStrip(chats: ChatInfo[], width: number, style: {
|
|
22
|
-
shown: (text: string) => string;
|
|
23
|
-
muted: (text: string) => string;
|
|
24
|
-
}, now?: number): string[];
|
|
25
17
|
export declare function registerChats(pi: ExtensionAPI, session: TurriCore): void;
|
package/dist/chat-commands.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { truncateToWidth } from "@earendil-works/pi-tui";
|
|
2
1
|
import { MAX_CHATS } from "./chats.js";
|
|
3
|
-
import {
|
|
2
|
+
import { chatMark, closeTerminalChat } from "./chat-tabs.js";
|
|
4
3
|
/**
|
|
5
|
-
* The terminal's
|
|
6
|
-
* editor naming each chat with what it is doing.
|
|
4
|
+
* The keyboard counterparts to the terminal's chat tabs: `/chat` - new, close, a number, or a pick - and its keys.
|
|
7
5
|
*/
|
|
8
6
|
/** A fresh chat. */
|
|
9
7
|
export const NEW_CHAT_KEY = "alt+n";
|
|
@@ -11,7 +9,6 @@ export const NEW_CHAT_KEY = "alt+n";
|
|
|
11
9
|
export const CLOSE_CHAT_KEY = "alt+w";
|
|
12
10
|
/** alt+1 … alt+9: that chat, shown. */
|
|
13
11
|
const SHOW_KEYS = Array.from({ length: MAX_CHATS }, (_, index) => `alt+${index + 1}`);
|
|
14
|
-
const STRIP_WIDGET = "turri-chats";
|
|
15
12
|
/**
|
|
16
13
|
* On a Mac, alt is Option, which types ¡ and ñ unless the terminal sends it as Meta: said once a session, as the second
|
|
17
14
|
* chat opens - when the keys are first worth having.
|
|
@@ -34,33 +31,6 @@ export function tellOfOptionOnce(session, platform = process.platform) {
|
|
|
34
31
|
setTimeout(() => session.notify(OPTION_AS_META, "info"), 0);
|
|
35
32
|
});
|
|
36
33
|
}
|
|
37
|
-
/** What a chat's mark in the strip and the picker says it is doing. */
|
|
38
|
-
export function chatMark(chat, now = Date.now()) {
|
|
39
|
-
switch (chat.status) {
|
|
40
|
-
case "working":
|
|
41
|
-
return spinner(now);
|
|
42
|
-
case "question":
|
|
43
|
-
return "?";
|
|
44
|
-
case "unread":
|
|
45
|
-
return "•";
|
|
46
|
-
default:
|
|
47
|
-
return " ";
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/** Between two chats in the strip. */
|
|
51
|
-
const STRIP_DELIMITER = " │ ";
|
|
52
|
-
/** The strip: each chat by its number, its title and its mark - the one shown set apart - on one line, a rule between them. */
|
|
53
|
-
export function chatStrip(chats, width, style, now = Date.now()) {
|
|
54
|
-
if (chats.length < 2)
|
|
55
|
-
return [];
|
|
56
|
-
const room = Math.max(8, Math.floor((width - 2) / chats.length) - STRIP_DELIMITER.length - 4);
|
|
57
|
-
const parts = chats.map((chat, index) => {
|
|
58
|
-
const mark = chatMark(chat, now).trim();
|
|
59
|
-
const label = `${index + 1} ${truncateToWidth(chat.title, room, "…")}${mark !== "" ? ` ${mark}` : ""}`;
|
|
60
|
-
return chat.shown ? style.shown(label) : style.muted(label);
|
|
61
|
-
});
|
|
62
|
-
return [truncateToWidth(` ${parts.join(style.muted(STRIP_DELIMITER))}`, width, "…")];
|
|
63
|
-
}
|
|
64
34
|
const label = (chat, index) => `${index + 1} ${chatMark(chat)} ${chat.title}${chat.shown ? " (shown)" : ""}`;
|
|
65
35
|
/** What the person asked of the chats, done once pi has finished with the command or key that asked it. */
|
|
66
36
|
function later(ctx, session, work) {
|
|
@@ -88,10 +58,7 @@ function closeChat(ctx, session, which) {
|
|
|
88
58
|
const chat = chatNamed(session, which);
|
|
89
59
|
if (typeof chat === "string")
|
|
90
60
|
return ctx.ui.notify(chat, "warning");
|
|
91
|
-
|
|
92
|
-
if (chat.role === undefined && session.chats().filter((other) => other.role === undefined).length === 1)
|
|
93
|
-
return ctx.ui.notify("This is the only chat - /quit leaves turri.", "info");
|
|
94
|
-
later(ctx, session, () => session.close(chat.id));
|
|
61
|
+
later(ctx, session, () => closeTerminalChat(session, chat.id));
|
|
95
62
|
}
|
|
96
63
|
function showChat(ctx, session, which) {
|
|
97
64
|
const chat = chatNamed(session, which);
|
|
@@ -139,25 +106,4 @@ export function registerChats(pi, session) {
|
|
|
139
106
|
description: `Show chat ${index + 1}`,
|
|
140
107
|
handler: async (ctx) => showChat(ctx, session, `${index + 1}`),
|
|
141
108
|
}));
|
|
142
|
-
// The strip above the editor, while there are two chats or more: drawn again as they change, and while one works.
|
|
143
|
-
pi.on("session_start", (_event, ctx) => {
|
|
144
|
-
if (!ctx.hasUI)
|
|
145
|
-
return;
|
|
146
|
-
ctx.ui.setWidget(STRIP_WIDGET, (tui, theme) => {
|
|
147
|
-
const stop = session.onChats(() => tui.requestRender());
|
|
148
|
-
const ticker = setInterval(() => {
|
|
149
|
-
if (session.chats().some((chat) => chat.status === "working"))
|
|
150
|
-
tui.requestRender();
|
|
151
|
-
}, 250);
|
|
152
|
-
ticker.unref?.();
|
|
153
|
-
return {
|
|
154
|
-
render: (width) => chatStrip(session.chats(), width, { shown: (text) => theme.bold(theme.fg("accent", text)), muted: (text) => theme.fg("muted", text) }),
|
|
155
|
-
invalidate: () => { },
|
|
156
|
-
dispose: () => {
|
|
157
|
-
stop();
|
|
158
|
-
clearInterval(ticker);
|
|
159
|
-
},
|
|
160
|
-
};
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
109
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Component, type Container, type ScrollView, type TUI } from "@earendil-works/pi-tui";
|
|
2
|
+
import type { ChatTabs } from "./chat-tabs.ts";
|
|
3
|
+
export interface ChatScreen {
|
|
4
|
+
fullscreenLayoutRoot: Component;
|
|
5
|
+
documentContainer: Container;
|
|
6
|
+
chatContainer: Container;
|
|
7
|
+
transcriptScrollView: ScrollView;
|
|
8
|
+
}
|
|
9
|
+
/** The inline tabs are a sibling of the banner in the transcript. A single fixed row takes over when they scroll
|
|
10
|
+
* off the top (or a resumed conversation has no banner). The banner itself scrolls normally. */
|
|
11
|
+
export declare function chatTabHeader(tabs: ChatTabs): {
|
|
12
|
+
mount(screen: ChatScreen, components: Component[]): Component[];
|
|
13
|
+
afterBanner(chat: Container, entry: unknown): void;
|
|
14
|
+
watch(screen: ChatScreen, tui: TUI): () => void;
|
|
15
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { VStack } from "@earendil-works/pi-tui";
|
|
2
|
+
import { terminalPointer } from "./pointer-hover.js";
|
|
3
|
+
/** Where pi drew a child. Use the row counts its Container already measured for mouse dispatch: the width of the
|
|
4
|
+
* chat, the banner's image and any wrapped warnings all affect where the divider lands. */
|
|
5
|
+
function rowOf(parent, child) {
|
|
6
|
+
const measured = parent.childLines;
|
|
7
|
+
if (measured === undefined)
|
|
8
|
+
return undefined;
|
|
9
|
+
let row = 0;
|
|
10
|
+
for (const [index, component] of measured.children.entries()) {
|
|
11
|
+
if (component === child)
|
|
12
|
+
return row;
|
|
13
|
+
row += measured.parts[index].length;
|
|
14
|
+
}
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
/** The inline tabs are a sibling of the banner in the transcript. A single fixed row takes over when they scroll
|
|
18
|
+
* off the top (or a resumed conversation has no banner). The banner itself scrolls normally. */
|
|
19
|
+
export function chatTabHeader(tabs) {
|
|
20
|
+
let pinned = true;
|
|
21
|
+
const sticky = {
|
|
22
|
+
render: (width) => pinned ? tabs.render(width) : [],
|
|
23
|
+
handleMouse: (event) => pinned ? tabs.handleMouse?.(event) : undefined,
|
|
24
|
+
invalidate() { },
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
mount(screen, components) {
|
|
28
|
+
if (components.includes(sticky))
|
|
29
|
+
return components;
|
|
30
|
+
screen.fullscreenLayoutRoot = new VStack([sticky, { component: screen.fullscreenLayoutRoot, basis: 0, grow: 1, minSize: 1 }]);
|
|
31
|
+
return [sticky, ...components];
|
|
32
|
+
},
|
|
33
|
+
afterBanner(chat, entry) {
|
|
34
|
+
if (chat.children.includes(tabs))
|
|
35
|
+
return;
|
|
36
|
+
const at = chat.children.findIndex((child) => child.entry === entry);
|
|
37
|
+
if (at !== -1)
|
|
38
|
+
chat.children.splice(at + 1, 0, tabs);
|
|
39
|
+
},
|
|
40
|
+
watch(screen, tui) {
|
|
41
|
+
if (tui.mode !== "fullscreen") {
|
|
42
|
+
// Regular mode has native scrollback: the row below the banner scrolls with it.
|
|
43
|
+
pinned = false;
|
|
44
|
+
return () => { };
|
|
45
|
+
}
|
|
46
|
+
const view = screen.transcriptScrollView;
|
|
47
|
+
tabs.pointer = terminalPointer(tui.terminal);
|
|
48
|
+
const update = view.updateLayout;
|
|
49
|
+
view.updateLayout = function (height, viewport, redraw) {
|
|
50
|
+
update.call(this, height, viewport, redraw);
|
|
51
|
+
const chat = rowOf(screen.documentContainer, screen.chatContainer);
|
|
52
|
+
const tab = rowOf(screen.chatContainer, tabs);
|
|
53
|
+
const row = chat === undefined || tab === undefined ? -1 : chat + tab;
|
|
54
|
+
const next = row < this.scrollTop || row >= this.scrollTop + viewport;
|
|
55
|
+
if (next !== pinned) {
|
|
56
|
+
pinned = next;
|
|
57
|
+
redraw();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
// pi normally asks multiplexers for button motion only. The tabs need motion without a button too. pi disables
|
|
61
|
+
// all mouse modes itself when it leaves fullscreen, including when the terminal is suspended.
|
|
62
|
+
const mouse = tui;
|
|
63
|
+
const start = mouse.beforeTerminalStart;
|
|
64
|
+
mouse.beforeTerminalStart = function () {
|
|
65
|
+
start.call(this);
|
|
66
|
+
if (this.mouseEnabled)
|
|
67
|
+
tui.terminal.write("\x1b[?1003h");
|
|
68
|
+
};
|
|
69
|
+
const input = mouse.handleViewportInput;
|
|
70
|
+
mouse.handleViewportInput = (data) => tabs.pointer.dispatch(() => input.call(mouse, data), () => tui.requestRender());
|
|
71
|
+
return () => {
|
|
72
|
+
view.updateLayout = update;
|
|
73
|
+
mouse.beforeTerminalStart = start;
|
|
74
|
+
mouse.handleViewportInput = input;
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Component } from "@earendil-works/pi-tui";
|
|
2
|
+
import type { ChatInfo, TurriCore } from "./chats.ts";
|
|
3
|
+
import { PointerHover, type ButtonPaint } from "./pointer-hover.ts";
|
|
4
|
+
/** Shared by the tabs and the /chat picker. */
|
|
5
|
+
export declare function chatMark(chat: ChatInfo): string;
|
|
6
|
+
export type TabAction = {
|
|
7
|
+
kind: "open";
|
|
8
|
+
} | {
|
|
9
|
+
kind: "plain";
|
|
10
|
+
} | {
|
|
11
|
+
kind: "show" | "close";
|
|
12
|
+
id: string;
|
|
13
|
+
};
|
|
14
|
+
/** Shared by ×, /chat close and alt+w. The last person's chat stays open even if the sentinel has its own tab. */
|
|
15
|
+
export declare function closeTerminalChat(session: TurriCore, id: string): Promise<void>;
|
|
16
|
+
interface PanelState {
|
|
17
|
+
notifications: number;
|
|
18
|
+
open: boolean;
|
|
19
|
+
busy: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface ChatTabs extends Component {
|
|
22
|
+
pointer: PointerHover;
|
|
23
|
+
}
|
|
24
|
+
/** Mouse positions, like the layout, are in terminal cells, not string offsets. */
|
|
25
|
+
export declare function chatTabs(chats: () => ChatInfo[], style: ButtonPaint, pick: (action: TabAction) => void, panel?: () => PanelState): ChatTabs;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { stripTerminalSequences, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
2
|
+
import { PointerHover } from "./pointer-hover.js";
|
|
3
|
+
import { spinner } from "./regenerate/job-panel.js";
|
|
4
|
+
/** Shared by the tabs and the /chat picker. */
|
|
5
|
+
export function chatMark(chat) {
|
|
6
|
+
switch (chat.status) {
|
|
7
|
+
case "working": return spinner(Date.now());
|
|
8
|
+
case "question": return "?";
|
|
9
|
+
case "unread": return "•";
|
|
10
|
+
default: return " ";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const buttonRole = { open: "new", plain: "panel", show: "tab", close: "close" };
|
|
14
|
+
function buttonKey(action) {
|
|
15
|
+
return "id" in action ? `${action.kind}:${action.id}` : action.kind;
|
|
16
|
+
}
|
|
17
|
+
/** Shared by ×, /chat close and alt+w. The last person's chat stays open even if the sentinel has its own tab. */
|
|
18
|
+
export async function closeTerminalChat(session, id) {
|
|
19
|
+
const chats = session.chats();
|
|
20
|
+
const people = chats.filter((chat) => chat.role === undefined);
|
|
21
|
+
if (people.length === 1 && people[0].id === id)
|
|
22
|
+
return session.notify("This is the only chat - /quit leaves turri.", "info");
|
|
23
|
+
await session.close(id);
|
|
24
|
+
}
|
|
25
|
+
function layout(chats, width, state) {
|
|
26
|
+
if (width <= 0)
|
|
27
|
+
return [];
|
|
28
|
+
// Keep the panel button at the right edge while chat titles grow and shrink on the left.
|
|
29
|
+
const pending = state?.notifications ?? 0;
|
|
30
|
+
const badge = pending && width >= 30 ? ` · ${pending > 99 ? "99+" : pending}` : "";
|
|
31
|
+
const arrow = state?.open ? "‹" : "›";
|
|
32
|
+
const spin = state?.busy ? spinner(Date.now()) : "";
|
|
33
|
+
const panel = width >= 24 ? ` ${spin && `${spin} `}***plain${badge} ${arrow} ` : width >= 14 ? `${spin}***plain ${arrow}` : width >= 6 ? `${spin}${arrow}` : "";
|
|
34
|
+
const panelWidth = visibleWidth(panel);
|
|
35
|
+
const chatWidth = width - (panel ? panelWidth + 1 : 0);
|
|
36
|
+
// Reserve + and a close target per tab before spending cells on padding, gaps and titles.
|
|
37
|
+
const spaced = chatWidth >= chats.length * 5 + 3;
|
|
38
|
+
const plus = spaced ? " + " : "+";
|
|
39
|
+
const count = Math.min(chats.length, Math.floor((chatWidth - plus.length) / 2));
|
|
40
|
+
const room = Math.floor((chatWidth - plus.length) / Math.max(1, count)) - (spaced ? 1 : 0);
|
|
41
|
+
const parts = [];
|
|
42
|
+
for (const [index, chat] of chats.slice(0, count).entries()) {
|
|
43
|
+
const lead = !spaced ? "" : room >= 8 ? (chat.shown ? " ▸ " : " ") : (chat.shown ? "▸" : " ");
|
|
44
|
+
let label = `${lead}${index + 1}`;
|
|
45
|
+
const close = spaced ? (room > 4 ? " × " : "× ") : "×";
|
|
46
|
+
const mark = chatMark(chat).trim();
|
|
47
|
+
const suffix = mark ? ` ${mark}` : "";
|
|
48
|
+
const titleRoom = room - visibleWidth(label + close + suffix) - 1;
|
|
49
|
+
if (titleRoom > 0)
|
|
50
|
+
label += ` ${truncateToWidth(stripTerminalSequences(chat.title).replace(/\s+/g, " ").trim(), titleRoom, "…")}${suffix}`;
|
|
51
|
+
parts.push({ text: label, action: { kind: "show", id: chat.id }, shown: chat.shown }, { text: close, action: { kind: "close", id: chat.id }, shown: chat.shown });
|
|
52
|
+
if (spaced)
|
|
53
|
+
parts.push({ text: " " });
|
|
54
|
+
}
|
|
55
|
+
parts.push({ text: plus, action: { kind: "open" } });
|
|
56
|
+
if (panel) {
|
|
57
|
+
const used = parts.reduce((cells, part) => cells + visibleWidth(part.text), 0);
|
|
58
|
+
parts.push({ text: " ".repeat(width - used - panelWidth) }, { text: panel, action: { kind: "plain" }, role: state?.busy ? "regenerating" : "panel" });
|
|
59
|
+
}
|
|
60
|
+
return parts;
|
|
61
|
+
}
|
|
62
|
+
/** Mouse positions, like the layout, are in terminal cells, not string offsets. */
|
|
63
|
+
export function chatTabs(chats, style, pick, panel) {
|
|
64
|
+
let pressed;
|
|
65
|
+
return {
|
|
66
|
+
pointer: new PointerHover(),
|
|
67
|
+
render(width) {
|
|
68
|
+
return [layout(chats(), width, panel?.()).map((part) => part.action ? style(part.text, part.shown === true, this.pointer.target === buttonKey(part.action), part.role ?? buttonRole[part.action.kind]) : part.text).join("")];
|
|
69
|
+
},
|
|
70
|
+
handleMouse(event) {
|
|
71
|
+
const move = event.type === "move";
|
|
72
|
+
if (event.y !== 0 || (!move && (event.button !== "left" || (event.type !== "press" && event.type !== "click"))))
|
|
73
|
+
return;
|
|
74
|
+
const down = pressed;
|
|
75
|
+
if (!move)
|
|
76
|
+
pressed = undefined;
|
|
77
|
+
let x = 0;
|
|
78
|
+
for (const part of layout(chats(), event.width, panel?.())) {
|
|
79
|
+
const end = x + visibleWidth(part.text);
|
|
80
|
+
if (event.x >= x && event.x < end && part.action) {
|
|
81
|
+
const key = buttonKey(part.action);
|
|
82
|
+
this.pointer.target = key;
|
|
83
|
+
if (move)
|
|
84
|
+
return { handled: true, render: false };
|
|
85
|
+
// Claim the press so pi delivers a click on release, without moving focus out of the editor.
|
|
86
|
+
if (event.type === "press")
|
|
87
|
+
pressed = key;
|
|
88
|
+
// A chat can change its title or close between press and release, moving another tab under the pointer.
|
|
89
|
+
else if (down === key && !(part.shown && part.action.kind === "show")) {
|
|
90
|
+
this.pointer.target = undefined;
|
|
91
|
+
pick(part.action);
|
|
92
|
+
}
|
|
93
|
+
return { handled: true };
|
|
94
|
+
}
|
|
95
|
+
x = end;
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
invalidate() { },
|
|
99
|
+
};
|
|
100
|
+
}
|