pi-odd-worktree 2.1.0 → 3.0.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/README.md +54 -319
- package/extensions/odd-worktree-tools.ts +4145 -2529
- package/lib/bash-activity.js +1164 -0
- package/lib/bash-design.js +684 -0
- package/lib/bash-destructive.js +559 -0
- package/lib/bash-frame.js +205 -0
- package/lib/card-content.js +23 -0
- package/lib/card-style.js +175 -0
- package/lib/checkout-facts.js +30 -70
- package/lib/codegraph-card-preview.js +628 -0
- package/lib/codegraph-card.js +515 -0
- package/lib/codegraph-compaction.js +76 -0
- package/lib/codegraph-daemon.js +246 -0
- package/lib/codegraph-edit-sync.js +182 -0
- package/lib/codegraph-guard-target.js +337 -0
- package/lib/codegraph-guard.js +211 -0
- package/lib/codegraph-index.js +1020 -0
- package/lib/codegraph-lock.js +607 -0
- package/lib/codegraph-mcp-config.js +134 -0
- package/lib/codegraph-mcp.js +123 -0
- package/lib/codegraph-paths.js +69 -0
- package/lib/codegraph-prompt/explore.md +11 -0
- package/lib/codegraph-prompt/fix.md +6 -0
- package/lib/codegraph-prompt/generic.md +8 -0
- package/lib/codegraph-prompt/judge.md +9 -0
- package/lib/codegraph-prompt/orchestrator.md +17 -0
- package/lib/codegraph-prompt/verify.md +10 -0
- package/lib/codegraph-prompt/worker.md +7 -0
- package/lib/codegraph-role.js +55 -0
- package/lib/codegraph-rules.js +146 -0
- package/lib/codegraph-session.js +303 -383
- package/lib/codegraph-warmup.js +139 -0
- package/lib/file-card-markdown.js +141 -0
- package/lib/file-card-resolver.js +245 -0
- package/lib/file-card.js +659 -0
- package/lib/files-index.js +387 -0
- package/lib/files-insert.js +81 -0
- package/lib/files-orders.js +326 -0
- package/lib/files-picker-view.js +1416 -0
- package/lib/files-preview.js +338 -0
- package/lib/files-scope-detail.js +171 -0
- package/lib/files-scope-view.js +507 -0
- package/lib/gh-issues.js +308 -14
- package/lib/issue-preview.js +26 -7
- package/lib/issues-bash-mutation.js +206 -0
- package/lib/issues-cache-store.js +261 -0
- package/lib/issues-section.js +710 -0
- package/lib/issues-view.js +649 -52
- package/lib/odd-prompt/odd-worktrees.md +21 -0
- package/lib/odd-worktrees-prompt.js +10 -28
- package/lib/related-issues-cache.js +210 -0
- package/lib/shell-card.js +825 -0
- package/lib/skill-odd-card.js +183 -0
- package/lib/tui-paint.js +4 -5
- package/lib/warp-create-panel.js +538 -250
- package/lib/warp-create-picker.js +56 -9
- package/lib/warp-delete-confirm.js +19 -28
- package/lib/warp-selector-view.js +160 -82
- package/lib/which-command.js +90 -0
- package/lib/worktree-card.js +267 -897
- package/lib/worktree-cards.js +383 -0
- package/lib/worktree-create.js +287 -0
- package/lib/worktree-git.js +3 -3
- package/lib/worktree-guard.js +548 -0
- package/lib/worktree-hooks.js +421 -712
- package/lib/worktree-list.js +120 -0
- package/lib/worktree-registry.js +162 -879
- package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
- package/lib/worktree-setup-runner.js +376 -0
- package/lib/worktree-setup.js +140 -0
- package/lib/worktree-warp-busy.js +462 -85
- package/lib/worktree-warp-sessions.js +1 -2
- package/package.json +2 -2
- package/tools/bash-activity.test.js +1419 -0
- package/tools/bash-design.test.js +1091 -0
- package/tools/bash-destructive.test.js +139 -0
- package/tools/card-content.test.js +44 -0
- package/tools/card-style.test.js +298 -0
- package/tools/codegraph-card-preview.test.js +254 -0
- package/tools/codegraph-card.test.js +1080 -0
- package/tools/codegraph-compaction.test.js +378 -0
- package/tools/codegraph-daemon.test.js +248 -0
- package/tools/codegraph-edit-sync.test.js +295 -0
- package/tools/codegraph-guard-target.test.js +174 -0
- package/tools/codegraph-guard.test.js +238 -0
- package/tools/codegraph-index.test.js +1948 -0
- package/tools/codegraph-lock.test.js +932 -0
- package/tools/codegraph-mcp-config.test.js +181 -0
- package/tools/codegraph-mcp.test.js +194 -0
- package/tools/codegraph-paths.test.js +44 -0
- package/tools/codegraph-role.test.js +30 -0
- package/tools/codegraph-rules.test.js +250 -0
- package/tools/codegraph-session.test.js +660 -391
- package/tools/codegraph-warmup.test.js +316 -0
- package/tools/commands.test.js +510 -31
- package/tools/file-card-markdown.test.js +188 -0
- package/tools/file-card.test.js +1289 -0
- package/tools/files-bridge.test.js +575 -0
- package/tools/files-command.test.js +610 -0
- package/tools/files-index.test.js +473 -0
- package/tools/files-insert.test.js +139 -0
- package/tools/files-orders.test.js +597 -0
- package/tools/files-picker-view.test.js +1871 -0
- package/tools/files-preview.test.js +452 -0
- package/tools/files-scope-detail.test.js +264 -0
- package/tools/files-scope-view.test.js +614 -0
- package/tools/fixtures/card-codegraph-outputs.js +110 -0
- package/tools/fixtures/codegraph-cli-stub.js +16 -0
- package/tools/fixtures/codegraph-init-process.js +28 -0
- package/tools/fixtures/codegraph-lock-worker.js +117 -0
- package/tools/fixtures/daemon-fake.js +98 -0
- package/tools/fixtures/daemon-mcp-server.js +39 -0
- package/tools/fixtures/daemon-warmup-worker.js +12 -0
- package/tools/fixtures/load-extension.js +136 -3
- package/tools/gh-issues-feedback.test.js +31 -7
- package/tools/gh-issues.test.js +391 -1
- package/tools/issue-preview.test.js +44 -7
- package/tools/issues-bash-mutation.test.js +101 -0
- package/tools/issues-cache-store.test.js +222 -0
- package/tools/issues-command.test.js +1512 -66
- package/tools/issues-open-guard.test.js +1096 -0
- package/tools/issues-section.test.js +4195 -0
- package/tools/issues-seed-query-regression.test.js +67 -0
- package/tools/issues-view.test.js +1489 -60
- package/tools/odd-worktrees-prompt.test.js +98 -65
- package/tools/package-surface.test.js +431 -36
- package/tools/related-issues-cache.test.js +324 -0
- package/tools/shell-card.test.js +834 -0
- package/tools/skill-odd-card.test.js +200 -0
- package/tools/t5-session-identity.test.js +44 -22
- package/tools/warp-busy-guard.test.js +2565 -309
- package/tools/warp-create-panel.test.js +499 -74
- package/tools/warp-create-picker.test.js +101 -5
- package/tools/warp-delete-confirm.test.js +49 -15
- package/tools/warp-empty-session.test.js +0 -1
- package/tools/warp-fallback-regression.test.js +533 -0
- package/tools/warp-selector-view.test.js +460 -196
- package/tools/which-command.test.js +126 -0
- package/tools/worktree-card.test.js +551 -0
- package/tools/worktree-cards.test.js +845 -0
- package/tools/worktree-changes.test.js +4 -5
- package/tools/worktree-create.test.js +489 -0
- package/tools/worktree-guard.test.js +823 -0
- package/tools/worktree-hooks.test.js +905 -0
- package/tools/worktree-list.test.js +170 -0
- package/tools/worktree-registry.test.js +89 -230
- package/tools/worktree-remove-async.test.js +318 -0
- package/tools/worktree-setup-runner.test.js +276 -0
- package/tools/worktree-setup.test.js +140 -0
- package/tools/worktree-tools.test.js +419 -199
- package/lib/codegraph-commands.js +0 -165
- package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
- package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
- package/lib/worktree-model-result.js +0 -291
- package/tools/codegraph-commands.test.js +0 -371
- package/tools/herdr-free-create.test.js +0 -161
- package/tools/worktree-close-herdr-async.test.js +0 -204
- package/tools/worktree-odd-migration.test.js +0 -224
- package/tools/worktree-registry-cache.test.js +0 -163
- package/tools/worktree-registry-odd.test.js +0 -231
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The warm-up: make this checkout's CodeGraph daemon open its database and start a read worker, so the next
|
|
3
|
+
// real call is fast. A live daemon already registered for this checkout is reused — one read through it is the
|
|
4
|
+
// warm-up and no second server is spawned beside it. Only when this checkout has no live daemon is
|
|
5
|
+
// `codegraph serve --mcp` started, through CodeGraph's own proxy, bounded, and never killed: the daemon it shares
|
|
6
|
+
// with the user's own sessions must survive the warm-up client. Nothing here can fail or hold the host.
|
|
7
|
+
|
|
8
|
+
const { spawn: nodeSpawn } = require('node:child_process');
|
|
9
|
+
const { pathToFileURL } = require('node:url');
|
|
10
|
+
const { tmpdir } = require('node:os');
|
|
11
|
+
const { daemonStatus, exploreViaDaemon, NO_PROJECT_TEXT, projectArgument } = require('./codegraph-daemon.js');
|
|
12
|
+
const { serveCommand } = require('./codegraph-paths.js');
|
|
13
|
+
|
|
14
|
+
/** A cold daemon on a network share can need over a minute for its first read. */
|
|
15
|
+
const WARMUP_TIMEOUT_MS = 120_000;
|
|
16
|
+
/** Any query works: the point is that a read call makes the daemon open its database and start a worker. */
|
|
17
|
+
const WARMUP_QUERY = 'main';
|
|
18
|
+
/** The command line the warm-up runs; only tests and callers with another install ask for another one. */
|
|
19
|
+
const WARMUP_COMMAND = 'codegraph serve --mcp';
|
|
20
|
+
/** A background warm-up must never hold its caller: a live daemon is asked within this budget. */
|
|
21
|
+
const WARMUP_BACKGROUND_LIVE_MS = 5_000;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Talks MCP to `codegraph serve --mcp -p <project>` for the project in `cwd`, started from the temp directory with
|
|
25
|
+
* the project spelled as its daemon registered it: initialize, one `codegraph_explore`, then closes stdin and lets
|
|
26
|
+
* the server go on its own. The project daemon is shared with the user's own sessions, so this never kills the
|
|
27
|
+
* process tree (that would take the daemon with it) and gives up waiting instead; it releases the server's stdout
|
|
28
|
+
* so a server that never leaves cannot keep the process alive. Never rejects.
|
|
29
|
+
*/
|
|
30
|
+
function exploreOnce(cwd, query, timeoutMs, command = WARMUP_COMMAND, options = {}) {
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
let settled = false;
|
|
33
|
+
let exploreStart = 0;
|
|
34
|
+
let child;
|
|
35
|
+
const finish = (ok) => {
|
|
36
|
+
if (settled) return;
|
|
37
|
+
settled = true;
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
// Destroy, not end: an ended pipe still holds the event loop until the server exits.
|
|
40
|
+
try { child?.stdin?.destroy(); } catch { /* already closed */ }
|
|
41
|
+
// The server may never leave on its own: its piped stdout would keep the event loop of Pi alive.
|
|
42
|
+
try { child?.stdout?.destroy(); } catch { /* already gone */ }
|
|
43
|
+
try { child?.unref(); } catch { /* already gone */ }
|
|
44
|
+
resolve({ ok, exploreMs: exploreStart === 0 ? 0 : Date.now() - exploreStart });
|
|
45
|
+
};
|
|
46
|
+
const timer = setTimeout(() => finish(false), timeoutMs);
|
|
47
|
+
if (options.background === true) timer.unref?.();
|
|
48
|
+
const project = options.project ?? projectArgument(cwd, { registryDir: options.registryDir });
|
|
49
|
+
try {
|
|
50
|
+
// Windows needs a shell for the .cmd shim; the command line is fixed plus the quoted project path.
|
|
51
|
+
const serve = serveCommand(command, project);
|
|
52
|
+
child = (options.spawn ?? nodeSpawn)(serve.command, {
|
|
53
|
+
cwd: tmpdir(),
|
|
54
|
+
env: { ...process.env, ...serve.env },
|
|
55
|
+
shell: true,
|
|
56
|
+
windowsHide: true,
|
|
57
|
+
stdio: ['pipe', 'pipe', 'ignore'],
|
|
58
|
+
});
|
|
59
|
+
if (options.background === true) {
|
|
60
|
+
child.unref();
|
|
61
|
+
child.stdin?.unref?.();
|
|
62
|
+
child.stdout?.unref?.();
|
|
63
|
+
}
|
|
64
|
+
} catch {
|
|
65
|
+
finish(false);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const send = (message) => child?.stdin?.write(`${JSON.stringify(message)}\n`);
|
|
69
|
+
let buffer = '';
|
|
70
|
+
child.stdout?.on('data', (chunk) => {
|
|
71
|
+
buffer += chunk.toString('utf8');
|
|
72
|
+
let newline;
|
|
73
|
+
while ((newline = buffer.indexOf('\n')) >= 0) {
|
|
74
|
+
const line = buffer.slice(0, newline).trim();
|
|
75
|
+
buffer = buffer.slice(newline + 1);
|
|
76
|
+
if (line.length === 0) continue;
|
|
77
|
+
let message;
|
|
78
|
+
try { message = JSON.parse(line); } catch { continue; }
|
|
79
|
+
if (message.id === 1) {
|
|
80
|
+
send({ jsonrpc: '2.0', method: 'notifications/initialized' });
|
|
81
|
+
exploreStart = Date.now();
|
|
82
|
+
send({ jsonrpc: '2.0', id: 2, method: 'tools/call', params: { name: 'codegraph_explore', arguments: { query } } });
|
|
83
|
+
} else if (message.id === 2) {
|
|
84
|
+
// CodeGraph reports a crashed query worker with isError and a text, not with a JSON-RPC error.
|
|
85
|
+
const text = message.result?.content?.[0]?.text;
|
|
86
|
+
const failed = message.error !== undefined || message.result?.isError === true;
|
|
87
|
+
finish(!failed && typeof text === 'string' && !text.startsWith(NO_PROJECT_TEXT));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
child.on('error', () => finish(false));
|
|
92
|
+
child.on('close', () => finish(false));
|
|
93
|
+
child.stdin?.on('error', () => { /* the server exited first */ });
|
|
94
|
+
send({
|
|
95
|
+
jsonrpc: '2.0',
|
|
96
|
+
id: 1,
|
|
97
|
+
method: 'initialize',
|
|
98
|
+
params: {
|
|
99
|
+
protocolVersion: '2024-11-05',
|
|
100
|
+
capabilities: {},
|
|
101
|
+
clientInfo: { name: 'odd-worktree-codegraph', version: '0' },
|
|
102
|
+
rootUri: pathToFileURL(project).href,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Warms this checkout's daemon, reusing a live one before starting a server. `initRunning` is the caller's own
|
|
110
|
+
* fact — the index coordination knows it — and while an init runs the warm-up is skipped, so the two never touch
|
|
111
|
+
* the index together. The answer comes from this checkout's own registry, never from MCP configuration, and a
|
|
112
|
+
* warm-up that cannot run answers false instead of failing its caller.
|
|
113
|
+
*/
|
|
114
|
+
async function warmUpDaemon(cwd, timeoutMs = WARMUP_TIMEOUT_MS, options = {}) {
|
|
115
|
+
try {
|
|
116
|
+
if (options.initRunning === true) return false;
|
|
117
|
+
const status = options.status ?? ((root) => daemonStatus(root, { registryDir: options.registryDir }));
|
|
118
|
+
if (status(cwd) === 'live') {
|
|
119
|
+
// A fire-and-forget warm-up is bounded tightly: the daemon is already there, and nothing may keep the caller.
|
|
120
|
+
const budget = options.background === true ? Math.min(timeoutMs, WARMUP_BACKGROUND_LIVE_MS) : timeoutMs;
|
|
121
|
+
const ask = options.ask ?? ((root, query, ms) => exploreViaDaemon(root, query, ms, { registryDir: options.registryDir }));
|
|
122
|
+
const answered = await ask(cwd, WARMUP_QUERY, budget);
|
|
123
|
+
// A live daemon that does not answer is not doubled: a second server would only be slower.
|
|
124
|
+
return answered.status === 'ok';
|
|
125
|
+
}
|
|
126
|
+
return (await exploreOnce(cwd, WARMUP_QUERY, timeoutMs, options.command ?? WARMUP_COMMAND, options)).ok;
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
module.exports = {
|
|
133
|
+
WARMUP_TIMEOUT_MS,
|
|
134
|
+
WARMUP_QUERY,
|
|
135
|
+
WARMUP_COMMAND,
|
|
136
|
+
WARMUP_BACKGROUND_LIVE_MS,
|
|
137
|
+
exploreOnce,
|
|
138
|
+
warmUpDaemon,
|
|
139
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// T3 (S3/S5): the file card's document body, drawn by Pi's OWN Markdown renderer.
|
|
3
|
+
//
|
|
4
|
+
// A Markdown document is a document, not a set of rows: this module hands the source
|
|
5
|
+
// to `Markdown` from `@earendil-works/pi-tui` — the same component the transcript uses —
|
|
6
|
+
// with a theme table mapping every Markdown element onto a THEME ROLE, so a heading, a
|
|
7
|
+
// bullet, an inline code span, a fence, a quote and a rule all wear the theme's own
|
|
8
|
+
// colours instead of one body colour painted over the whole block. The table is the
|
|
9
|
+
// reference PR's (`lib/skill-odd-card.ts:fileCardMarkdownTheme`, gentle-shell #3061),
|
|
10
|
+
// and the roles are the host's own (`theme-tokens.ts`: `mdHeading`, `mdLink`,
|
|
11
|
+
// `mdLinkUrl`, `mdCode`, `mdCodeBlock`, `mdCodeBlockBorder`, `mdQuote`,
|
|
12
|
+
// `mdQuoteBorder`, `mdHr`, `mdListBullet`).
|
|
13
|
+
//
|
|
14
|
+
// The rows it returns are ALREADY PAINTED. That is the point of using Pi's renderer —
|
|
15
|
+
// and it is why the substrate places them with an ANSI-aware width instead of a plain
|
|
16
|
+
// codepoint count (`cardPaintedRows`, `lib/shell-card.js`), never re-painting them with
|
|
17
|
+
// a body role of ours. This is the second painted input invariant 17 allows: a line may
|
|
18
|
+
// arrive painted and still be MEASURED, as Pi's own `keyHint` is, but nothing painted is
|
|
19
|
+
// ever painted a second time — and the measurement is the host's ANSI-aware width, never
|
|
20
|
+
// a codepoint count that would cut through an escape sequence.
|
|
21
|
+
//
|
|
22
|
+
// A host without the TUI package, a renderer that throws, and a document that renders
|
|
23
|
+
// no row all answer `[]`, so the caller keeps the wrapped plain source it drew before.
|
|
24
|
+
// This package ships no node_modules: the package is resolved INSIDE the call, never at
|
|
25
|
+
// load, exactly as every other pi-tui use in this repo does it.
|
|
26
|
+
|
|
27
|
+
/** The role each Markdown element is painted in; the source of truth for the table below. */
|
|
28
|
+
const MARKDOWN_ROLES = Object.freeze({
|
|
29
|
+
heading: 'mdHeading',
|
|
30
|
+
link: 'mdLink',
|
|
31
|
+
linkUrl: 'mdLinkUrl',
|
|
32
|
+
code: 'mdCode',
|
|
33
|
+
codeBlock: 'mdCodeBlock',
|
|
34
|
+
codeBlockBorder: 'mdCodeBlockBorder',
|
|
35
|
+
quote: 'mdQuote',
|
|
36
|
+
quoteBorder: 'mdQuoteBorder',
|
|
37
|
+
hr: 'mdHr',
|
|
38
|
+
listBullet: 'mdListBullet',
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// The text attributes the host theme carries beside `fg`. A theme without one answers
|
|
42
|
+
// with the text itself, so a document never loses a word to a missing attribute.
|
|
43
|
+
const MARKDOWN_ATTRIBUTES = Object.freeze(['bold', 'italic', 'underline', 'strikethrough']);
|
|
44
|
+
|
|
45
|
+
const asObject = (value) => (value !== null && typeof value === 'object' ? value : {});
|
|
46
|
+
|
|
47
|
+
/** One role as a paint function: a theme that cannot paint it leaves the text as it was. */
|
|
48
|
+
function rolePainter(theme, role) {
|
|
49
|
+
return (text) => {
|
|
50
|
+
const value = String(text ?? '');
|
|
51
|
+
try {
|
|
52
|
+
if (typeof theme.fg === 'function') {
|
|
53
|
+
const painted = theme.fg(role, value);
|
|
54
|
+
if (typeof painted === 'string') return painted;
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
// A hostile theme never costs the document a word.
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One text attribute as a paint function, or the identity when the theme has none. */
|
|
64
|
+
function attributePainter(theme, name) {
|
|
65
|
+
return (text) => {
|
|
66
|
+
const value = String(text ?? '');
|
|
67
|
+
try {
|
|
68
|
+
const apply = theme[name];
|
|
69
|
+
if (typeof apply === 'function') {
|
|
70
|
+
const painted = apply.call(theme, value);
|
|
71
|
+
if (typeof painted === 'string') return painted;
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// Same as above: an attribute that fails keeps the plain text.
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Pi's `MarkdownTheme` built from a card's theme: every element in its theme role and
|
|
82
|
+
* every attribute from the theme's own handlers.
|
|
83
|
+
*/
|
|
84
|
+
function fileCardMarkdownTheme(theme) {
|
|
85
|
+
const paint = asObject(theme);
|
|
86
|
+
const table = {};
|
|
87
|
+
for (const [element, role] of Object.entries(MARKDOWN_ROLES)) table[element] = rolePainter(paint, role);
|
|
88
|
+
for (const attribute of MARKDOWN_ATTRIBUTES) table[attribute] = attributePainter(paint, attribute);
|
|
89
|
+
return table;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// The renderer the host provides, resolved once: `undefined` means "not looked yet",
|
|
93
|
+
// `null` means "this host has none", which is a stable answer for the whole process.
|
|
94
|
+
let resolved = false;
|
|
95
|
+
let markdownClass;
|
|
96
|
+
|
|
97
|
+
/** Pi's `Markdown` class, or undefined when this host has no TUI package. Never throws. */
|
|
98
|
+
function loadMarkdownClass() {
|
|
99
|
+
if (resolved) return markdownClass ?? undefined;
|
|
100
|
+
resolved = true;
|
|
101
|
+
try {
|
|
102
|
+
const tui = require('@earendil-works/pi-tui');
|
|
103
|
+
markdownClass = tui !== null && typeof tui === 'object' && typeof tui.Markdown === 'function' ? tui.Markdown : null;
|
|
104
|
+
} catch {
|
|
105
|
+
markdownClass = null;
|
|
106
|
+
}
|
|
107
|
+
return markdownClass ?? undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The document's rows, already painted by Pi's renderer, or `[]` when it cannot draw
|
|
112
|
+
* one. `width` is the width the rows must fit: the renderer wraps to it, and the
|
|
113
|
+
* substrate measures the result with the same ANSI-aware helpers.
|
|
114
|
+
*
|
|
115
|
+
* `MarkdownClass` is the renderer to use: omitted, this host's own package is resolved;
|
|
116
|
+
* `null` states a host that has none, which answers no rows without asking for one.
|
|
117
|
+
*/
|
|
118
|
+
function markdownRows(source, theme, width, MarkdownClass) {
|
|
119
|
+
const target = Math.max(0, Math.floor(Number.isFinite(width) ? width : 0));
|
|
120
|
+
if (typeof source !== 'string' || source.trim() === '' || target === 0) return [];
|
|
121
|
+
const Renderer = MarkdownClass === undefined ? loadMarkdownClass() : MarkdownClass;
|
|
122
|
+
if (typeof Renderer !== 'function') return [];
|
|
123
|
+
try {
|
|
124
|
+
const drawn = new Renderer(source, 0, 0, fileCardMarkdownTheme(theme)).render(target);
|
|
125
|
+
if (!Array.isArray(drawn)) return [];
|
|
126
|
+
const rows = drawn.filter((row) => typeof row === 'string');
|
|
127
|
+
// A renderer that draws only blanks states nothing: the caller's plain source is
|
|
128
|
+
// the better answer, so this is "no rows" rather than a body of empty lines.
|
|
129
|
+
return rows.some((row) => row.trim() !== '') ? rows : [];
|
|
130
|
+
} catch {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
module.exports = {
|
|
136
|
+
MARKDOWN_ATTRIBUTES,
|
|
137
|
+
MARKDOWN_ROLES,
|
|
138
|
+
fileCardMarkdownTheme,
|
|
139
|
+
loadMarkdownClass,
|
|
140
|
+
markdownRows,
|
|
141
|
+
};
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// T3 (S1/S2): the seam that makes a `read`, `edit` or `write` call whose path names a
|
|
3
|
+
// skill or an ODD artifact this package's file card.
|
|
4
|
+
//
|
|
5
|
+
// Shaped exactly like `lib/bash-card-resolver.js`, and for the same reason: the host
|
|
6
|
+
// resolves drawing through `pi.registerToolRenderer((toolName, next) => …)`, where
|
|
7
|
+
// `next()` is what the remaining resolvers (in extension load order) and then the
|
|
8
|
+
// registered tool would draw. So this module REGISTERS NOTHING, EXECUTES NOTHING and
|
|
9
|
+
// SHADOWS NOTHING: it chooses renderers for the three file tools and hands every other
|
|
10
|
+
// row back exactly as the chain resolved it.
|
|
11
|
+
//
|
|
12
|
+
// The two decisions this module owns:
|
|
13
|
+
// * WHICH ROWS IT CLAIMS. `read`, `edit` and `write` only, and only when the host
|
|
14
|
+
// offers a usable measurement seam. Whether one particular CALL is this card's is
|
|
15
|
+
// decided per call, inside the slots (`lib/file-card.js`): the path's identity and
|
|
16
|
+
// the bridge's `Card tools` line are read on every paint, so a row whose path names
|
|
17
|
+
// neither is the chain's own drawing and a switch flipped while the transcript is
|
|
18
|
+
// on screen repaints the row already there.
|
|
19
|
+
// * THE SEAMS. Text measurement and Pi's Markdown renderer are injected, so a host
|
|
20
|
+
// that cannot provide them (no TUI package, an older build) gets its own rows back
|
|
21
|
+
// instead of a card measured against nothing.
|
|
22
|
+
//
|
|
23
|
+
// Colour is not decided here: it arrives from the host's render arguments, with the
|
|
24
|
+
// factory's `theme` option as the fallback for a host that passes none.
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
FILE_TOOLS,
|
|
28
|
+
fileCallSlot,
|
|
29
|
+
fileResultSlot,
|
|
30
|
+
inheritedRenderers,
|
|
31
|
+
inheritedShell,
|
|
32
|
+
} = require('./file-card.js');
|
|
33
|
+
const { CARD_CONTENT, cardContentLevel } = require('./card-content.js');
|
|
34
|
+
// Pi's Markdown renderer, resolved inside the call and never at load: this package
|
|
35
|
+
// ships no node_modules.
|
|
36
|
+
const { loadMarkdownClass } = require('./file-card-markdown.js');
|
|
37
|
+
|
|
38
|
+
// ---- the one switch (S1/S7) ----
|
|
39
|
+
//
|
|
40
|
+
// The file card no longer owns a preference: it is painted while the bridge's
|
|
41
|
+
// `Card tools` line answers that this package's activity design draws the tool being
|
|
42
|
+
// drawn, and the host's own card is painted for every other value (the bridge's
|
|
43
|
+
// `native`, a design that has left the line, and a design that does not cover this
|
|
44
|
+
// tool alike).
|
|
45
|
+
//
|
|
46
|
+
// The answer comes from the bridge's OWN reader, through the same published object
|
|
47
|
+
// every other bridge surface here uses (`lib/bash-design.js` is handed it at
|
|
48
|
+
// registration; the extension reads it from the slot below). Nothing is duplicated:
|
|
49
|
+
// no store path, no key, no resolution rule — and nothing is cached, because the
|
|
50
|
+
// question is what repaints a card already on screen after a mid-session switch.
|
|
51
|
+
const BRIDGE_API_SLOT = Symbol.for('gentle-shell-bridge/api/v1');
|
|
52
|
+
const BRIDGE_API_SCHEMA = 'gentle-shell-bridge/api';
|
|
53
|
+
const BRIDGE_API_VERSION = 1;
|
|
54
|
+
const ACTIVITY_DESIGN_ID = 'odd-worktree.bash-activity';
|
|
55
|
+
|
|
56
|
+
/** The bridge's published object after its own gate, or undefined when nothing usable is published. */
|
|
57
|
+
function bridgeApi() {
|
|
58
|
+
try {
|
|
59
|
+
const api = globalThis[BRIDGE_API_SLOT];
|
|
60
|
+
if (api === null || typeof api !== 'object') return undefined;
|
|
61
|
+
if (api.schema !== BRIDGE_API_SCHEMA || api.version !== BRIDGE_API_VERSION) return undefined;
|
|
62
|
+
return api;
|
|
63
|
+
} catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Whether the activity design draws `tool` right now, asked per paint and never cached.
|
|
70
|
+
*
|
|
71
|
+
* The reader is an OPTIONAL member of the published object (gentle-shell-bridge@0.7.0):
|
|
72
|
+
* a bridge older than that, or no bridge at all, has none, and then the card paints
|
|
73
|
+
* exactly as it did before the switch was joined — an unavailable gate never hides a
|
|
74
|
+
* card. The bridge's reader never throws (an absent or unreadable store answers
|
|
75
|
+
* `native`), and this one does not either: an answer it cannot read keeps the card
|
|
76
|
+
* visible rather than hiding a row behind a version it does not know.
|
|
77
|
+
*/
|
|
78
|
+
function activitySelected(tool) {
|
|
79
|
+
const api = bridgeApi();
|
|
80
|
+
if (api === undefined || typeof api.resolveToolDesignSelection !== 'function') return true;
|
|
81
|
+
try {
|
|
82
|
+
const selection = api.resolveToolDesignSelection(tool);
|
|
83
|
+
const answer = selection !== null && typeof selection === 'object' ? selection : {};
|
|
84
|
+
if (answer.kind === 'native' || answer.kind === 'missing' || answer.kind === 'not-covered') return false;
|
|
85
|
+
if (answer.kind !== 'design') return true;
|
|
86
|
+
const design = answer.design !== null && typeof answer.design === 'object' ? answer.design : {};
|
|
87
|
+
return typeof design.id !== 'string' ? true : design.id === ACTIVITY_DESIGN_ID;
|
|
88
|
+
} catch {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const METRICS_FUNCTIONS = ['visibleWidth', 'truncateToWidth', 'wrapTextWithAnsi'];
|
|
94
|
+
|
|
95
|
+
const asObject = (value) => (value !== null && typeof value === 'object' ? value : {});
|
|
96
|
+
const hasFunction = (value, name) => value !== null && typeof value === 'object' && typeof value[name] === 'function';
|
|
97
|
+
|
|
98
|
+
function assertMetrics(value) {
|
|
99
|
+
if (!hasFunction(value, METRICS_FUNCTIONS[0])) {
|
|
100
|
+
throw new TypeError('createFileCardResolver requires an injected metrics object ({visibleWidth, truncateToWidth, wrapTextWithAnsi})');
|
|
101
|
+
}
|
|
102
|
+
for (const name of METRICS_FUNCTIONS) {
|
|
103
|
+
if (!hasFunction(value, name)) throw new TypeError(`createFileCardResolver requires metrics.${name} to be a function`);
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The measurement seam a caller injected, validated once at construction: the object
|
|
110
|
+
* itself, or a function the factory calls on every paint (the host's TUI package is
|
|
111
|
+
* resolved inside the hook, never at load).
|
|
112
|
+
*/
|
|
113
|
+
function metricsSeam(value) {
|
|
114
|
+
if (typeof value === 'function') return value;
|
|
115
|
+
assertMetrics(value);
|
|
116
|
+
return () => value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Whether a value is a usable text-measurement seam. */
|
|
120
|
+
function usableMetrics(value) {
|
|
121
|
+
return METRICS_FUNCTIONS.every((name) => hasFunction(value, name)) ? value : undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* T3 (S1/S2): the file card, for `read`, `edit` and `write`.
|
|
126
|
+
*
|
|
127
|
+
* `createFileCardResolver({ metrics, registerToolRenderer, theme, markdownClass })` takes
|
|
128
|
+
* every host seam as an option:
|
|
129
|
+
* * `metrics` (required) — the text-measurement seam, either the object itself or a
|
|
130
|
+
* function returning it, so a runtime lookup (the host's TUI package) is resolved
|
|
131
|
+
* per paint and never at load;
|
|
132
|
+
* * `registerToolRenderer` — the host hook this factory registers through. Absent,
|
|
133
|
+
* or a hook that throws, is no hook: the factory reports `registered: false` and
|
|
134
|
+
* nothing of ours is drawn, which is what an older host needs;
|
|
135
|
+
* * `theme` — the theme used when a render arrives without one;
|
|
136
|
+
* * `markdownClass` — Pi's `Markdown` renderer. Omitted, the host package is resolved
|
|
137
|
+
* lazily per paint; `null`, a host that has none, which keeps the plain body.
|
|
138
|
+
*
|
|
139
|
+
* It returns `{ registered, resolver }`; `resolver(toolName, next)` is the function the
|
|
140
|
+
* host hook receives, and it returns every tool it does not own exactly as `next()`
|
|
141
|
+
* resolved it.
|
|
142
|
+
*/
|
|
143
|
+
function createFileCardResolver(options) {
|
|
144
|
+
const opts = asObject(options);
|
|
145
|
+
const metrics = metricsSeam(opts.metrics);
|
|
146
|
+
const seams = {
|
|
147
|
+
theme: opts.theme,
|
|
148
|
+
metrics() {
|
|
149
|
+
try {
|
|
150
|
+
return usableMetrics(metrics());
|
|
151
|
+
} catch {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
/**
|
|
156
|
+
* The one switch, read now (S1/S7): the bridge's `Card tools` line answers with the
|
|
157
|
+
* activity design for `tool`. A bridge with no reader — older than 0.7.0, or none at
|
|
158
|
+
* all — paints the card, which is the behaviour it had before.
|
|
159
|
+
*/
|
|
160
|
+
enabled(tool) {
|
|
161
|
+
try {
|
|
162
|
+
return activitySelected(tool) === true;
|
|
163
|
+
} catch {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
/** Pi's Markdown renderer, read now: the injected seam wins, else the host package. */
|
|
168
|
+
markdownClass() {
|
|
169
|
+
try {
|
|
170
|
+
if (opts.markdownClass !== undefined) {
|
|
171
|
+
return typeof opts.markdownClass === 'function' ? opts.markdownClass : undefined;
|
|
172
|
+
}
|
|
173
|
+
return loadMarkdownClass();
|
|
174
|
+
} catch {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
/** The conversation's content level, read now (S2): `minimal` or the safe `default`. */
|
|
179
|
+
content() {
|
|
180
|
+
try {
|
|
181
|
+
return cardContentLevel() === CARD_CONTENT.MINIMAL ? CARD_CONTENT.MINIMAL : CARD_CONTENT.DEFAULT;
|
|
182
|
+
} catch {
|
|
183
|
+
return CARD_CONTENT.DEFAULT;
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
const resolver = (toolName, next) => {
|
|
188
|
+
const inherited = inheritedRenderers(next);
|
|
189
|
+
// Every tool this card does not own goes back untouched: those rows are the
|
|
190
|
+
// chain's, byte for byte, whatever this card would have drawn.
|
|
191
|
+
if (!FILE_TOOLS.includes(toolName)) return inherited;
|
|
192
|
+
// A host with no usable measurement seam keeps its own drawing: a card measured
|
|
193
|
+
// against nothing is worse than no card.
|
|
194
|
+
if (seams.metrics() === undefined) return inherited;
|
|
195
|
+
// The shell the chain's own renderer draws in, read ONCE here because a resolver
|
|
196
|
+
// never sees a call's arguments and `renderShell` is fixed per tool: the host reads
|
|
197
|
+
// `renderShell ?? 'default'`, so only a literal `self` is one. A `self` renderer
|
|
198
|
+
// (Pi's `edit`) frames itself and the host draws NO box around it, so its rows are
|
|
199
|
+
// handed back exactly as drawn; every other tool gets the host's box reproduced.
|
|
200
|
+
const shell = inheritedShell(inherited);
|
|
201
|
+
// This card frames itself, so the host must not frame it too: the host wraps every
|
|
202
|
+
// tool whose definition does not say `renderShell: 'self'` in its own filled box,
|
|
203
|
+
// and a card drawn inside that box is two designs at once.
|
|
204
|
+
return {
|
|
205
|
+
renderShell: 'self',
|
|
206
|
+
renderCall: fileCallSlot(toolName, inherited, seams, shell),
|
|
207
|
+
renderResult: fileResultSlot(toolName, inherited, seams, shell),
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
const register = opts.registerToolRenderer;
|
|
211
|
+
if (typeof register !== 'function') return { registered: false, resolver };
|
|
212
|
+
try {
|
|
213
|
+
register(resolver);
|
|
214
|
+
return { registered: true, resolver };
|
|
215
|
+
} catch {
|
|
216
|
+
return { registered: false, resolver };
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The extension's one-line wiring point: register through the host's hook when the
|
|
222
|
+
* host has one, and report the outcome rather than throwing. A host without
|
|
223
|
+
* `registerToolRenderer` draws its own rows and loses nothing (no card is registered,
|
|
224
|
+
* no tool is defined, no command is added).
|
|
225
|
+
*/
|
|
226
|
+
function registerFileCard(pi, deps = {}) {
|
|
227
|
+
// A host with no hook is asked nothing: registering through a seam that does not
|
|
228
|
+
// exist would report a card that is never drawn.
|
|
229
|
+
if (typeof pi?.registerToolRenderer !== 'function') return { registered: false, resolver: undefined };
|
|
230
|
+
try {
|
|
231
|
+
return createFileCardResolver({
|
|
232
|
+
metrics: deps.metrics,
|
|
233
|
+
theme: deps.theme,
|
|
234
|
+
registerToolRenderer: (resolver) => pi.registerToolRenderer(resolver),
|
|
235
|
+
});
|
|
236
|
+
} catch {
|
|
237
|
+
// A host without a usable measurement seam draws its own row: the card is cosmetic.
|
|
238
|
+
return { registered: false, resolver: undefined };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
module.exports = {
|
|
243
|
+
createFileCardResolver,
|
|
244
|
+
registerFileCard,
|
|
245
|
+
};
|