pal-code 0.0.13 → 0.0.15

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pal-code",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "A terminal coding agent with BYOK, tool use, and extensions",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -62,7 +62,7 @@
62
62
  "gemini"
63
63
  ],
64
64
  "dependencies": {
65
- "@pal-code/core": "0.0.13",
65
+ "@pal-code/core": "0.0.15",
66
66
  "@earendil-works/pi-tui": "^0.85.1"
67
67
  },
68
68
  "devDependencies": {
@@ -6,21 +6,40 @@
6
6
  * (`codeBlockBox`) so snippets read as distinct blocks instead of running
7
7
  * into the surrounding text.
8
8
  *
9
- * Rendering must be a pure function of (content, width): pi-tui repaints
10
- * differentially and this component caches by width, so any clock or
11
- * randomness would desync the screen from the cache on repaint/resize.
9
+ * Rendering must be a pure function of (content, width, hover/success
10
+ * state): pi-tui repaints differentially and this component caches by
11
+ * width, so any clock or randomness would desync the screen from the
12
+ * cache on repaint/resize. Hover and success are explicit fields set
13
+ * through `setHovered`/`setSuccess`, never sampled from input devices.
12
14
  */
13
15
 
14
16
  import {Markdown, type Component} from '@earendil-works/pi-tui';
15
17
 
16
18
  import {markdownTheme} from '../theme.js';
17
- import {codeBlockBox, parseCodeSegments} from '../utils/code-block.js';
19
+ import {
20
+ codeBlockBox,
21
+ copyZoneRect,
22
+ COPY_LABEL,
23
+ parseCodeSegments,
24
+ SUCCESS_LABEL,
25
+ } from '../utils/code-block.js';
18
26
  import type {CodeSegment} from '../utils/code-block.js';
19
27
 
20
28
  type BodySegment =
21
29
  | {readonly kind: 'text'; readonly md: Markdown}
22
30
  | {readonly kind: 'code'; readonly code: string; readonly lang: string};
23
31
 
32
+ type CodeBoxOpts = {copy?: boolean; hover?: boolean; success?: boolean};
33
+
34
+ /** A clickable code-block header located by `AssistantBody.blockAt`. */
35
+ export type AssistantBlockHit = {
36
+ readonly code: string;
37
+ readonly lang: string;
38
+ readonly headerRow: number;
39
+ readonly boxWidth: number;
40
+ readonly zone: {startCol: number; width: number} | null;
41
+ };
42
+
24
43
  function assertNever(x: never): never {
25
44
  throw new Error(`unhandled code segment: ${JSON.stringify(x)}`);
26
45
  }
@@ -40,6 +59,12 @@ export class AssistantBody implements Component {
40
59
  private segments: readonly BodySegment[];
41
60
  private cached: string[] | null = null;
42
61
  private cachedWidth = -1;
62
+ /** Row spans of each rendered segment (blank separators excluded). */
63
+ private spans: {segIndex: number; start: number; height: number}[] = [];
64
+ /** Segment index the pointer is over, or null. */
65
+ private hoveredSeg: number | null = null;
66
+ /** Segment index showing the post-copy success chip, or null. */
67
+ private successSeg: number | null = null;
43
68
 
44
69
  constructor(content: string) {
45
70
  this.segments = parseCodeSegments(content).map(toBodySegment);
@@ -56,32 +81,106 @@ export class AssistantBody implements Component {
56
81
  this.invalidate();
57
82
  }
58
83
 
84
+ /**
85
+ * Point the hover paint at segment `i` (or clear with null). Returns
86
+ * true only when the value actually changed — repeated same-value calls
87
+ * are free and never invalidate the render cache (Phase B repaints only
88
+ * on a real change).
89
+ */
90
+ setHovered(i: number | null): boolean {
91
+ if (this.hoveredSeg === i) return false;
92
+ this.hoveredSeg = i;
93
+ this.invalidate();
94
+ return true;
95
+ }
96
+
97
+ /**
98
+ * Show the success chip on segment `i` (or clear with null). Same
99
+ * change-detection contract as `setHovered`.
100
+ */
101
+ setSuccess(i: number | null): boolean {
102
+ if (this.successSeg === i) return false;
103
+ this.successSeg = i;
104
+ this.invalidate();
105
+ return true;
106
+ }
107
+
59
108
  invalidate(): void {
60
109
  this.cached = null;
61
110
  this.cachedWidth = -1;
111
+ this.spans = [];
62
112
  }
63
113
 
64
114
  render(width: number): string[] {
65
115
  if (this.cached && this.cachedWidth === width) return this.cached;
66
116
 
67
117
  const out: string[] = [];
68
- for (const seg of this.segments) {
118
+ const spans: {segIndex: number; start: number; height: number}[] = [];
119
+ for (let i = 0; i < this.segments.length; i++) {
69
120
  if (out.length > 0) out.push(''); // one blank line between blocks
70
- out.push(...renderSegment(seg, width));
121
+ const start = out.length;
122
+ const lines = renderSegment(this.segments[i], width, {
123
+ copy: true,
124
+ hover: this.hoveredSeg === i,
125
+ success: this.successSeg === i,
126
+ });
127
+ out.push(...lines);
128
+ spans.push({segIndex: i, start, height: lines.length});
71
129
  }
72
130
 
131
+ this.spans = spans;
73
132
  this.cached = out;
74
133
  this.cachedWidth = width;
75
134
  return out;
76
135
  }
136
+
137
+ /**
138
+ * Hit-test: locate the code block whose header row is `row` (0-based,
139
+ * local to this component's rendered output at `width`). Returns null
140
+ * unless the row is exactly a code segment's header AND that header has
141
+ * a copy zone (box wide enough for the label). The zone is computed with
142
+ * the same label the paint used, so success-state clicks still land.
143
+ */
144
+ blockAt(width: number, row: number): AssistantBlockHit | null {
145
+ const lines = this.render(width);
146
+ if (row < 0 || row >= lines.length) return null;
147
+ const span = this.spans.find((s) => row >= s.start && row < s.start + s.height);
148
+ if (!span) return null;
149
+ const seg = this.segments[span.segIndex];
150
+ if (!seg || seg.kind !== 'code') return null;
151
+ if (row - span.start !== 0) return null;
152
+ const boxWidth = Math.max(6, Math.floor(width));
153
+ const label = this.successSeg === span.segIndex ? SUCCESS_LABEL : COPY_LABEL;
154
+ const zone = copyZoneRect(boxWidth, seg.lang, true, label);
155
+ if (!zone) return null;
156
+ return {code: seg.code, lang: seg.lang, headerRow: 0, boxWidth, zone};
157
+ }
158
+
159
+ /**
160
+ * Segment index owning the header row `row` (0-based, local to this
161
+ * component's rendered output at `width`), or null when the row is not a
162
+ * code-block header. Same hit rules as `blockAt`, minus the zone geometry —
163
+ * Phase B hover/success need the owning segment id to feed
164
+ * `setHovered`/`setSuccess`.
165
+ */
166
+ blockIndexAt(width: number, row: number): number | null {
167
+ const lines = this.render(width);
168
+ if (row < 0 || row >= lines.length) return null;
169
+ const span = this.spans.find((s) => row >= s.start && row < s.start + s.height);
170
+ if (!span) return null;
171
+ const seg = this.segments[span.segIndex];
172
+ if (!seg || seg.kind !== 'code') return null;
173
+ if (row - span.start !== 0) return null;
174
+ return span.segIndex;
175
+ }
77
176
  }
78
177
 
79
- function renderSegment(seg: BodySegment, width: number): string[] {
178
+ function renderSegment(seg: BodySegment, width: number, opts: CodeBoxOpts): string[] {
80
179
  switch (seg.kind) {
81
180
  case 'text':
82
181
  return seg.md.render(width);
83
182
  case 'code':
84
- return codeBlockBox(seg.code, width, seg.lang).split('\n');
183
+ return codeBlockBox(seg.code, width, seg.lang, opts).split('\n');
85
184
  default:
86
185
  return assertNever(seg);
87
186
  }
@@ -13,11 +13,11 @@
13
13
  import {visibleWidth} from '@earendil-works/pi-tui';
14
14
  import type {Component} from '@earendil-works/pi-tui';
15
15
 
16
- import {c} from '../theme.js';
16
+ import {c, surface} from '../theme.js';
17
17
  import {state} from '../state.js';
18
18
 
19
19
  /** Strip ANSI escape sequences for visible-width measurement. */
20
- const ANSI_RE = /\x1b\[[0-9;]*m/g;
20
+ const ANSI_RE = new RegExp('\x1b\\[[0-9;]*m', 'g');
21
21
 
22
22
  /** Repeat a character `n` times. */
23
23
  const rep = (ch: string, n: number): string => ch.repeat(Math.max(0, n));
@@ -38,17 +38,16 @@ export class PermissionPrompt implements Component {
38
38
  const maxBox = Math.min(60, Math.max(40, width));
39
39
  const W = maxBox;
40
40
  const inner = W - 2; // "│" content "│" — inner = total minus the two border chars
41
- const boxColor = c.dim + c.yellow;
41
+ const boxColor = surface.permission.border;
42
42
  const paint = (s: string) => `${boxColor}${s}${c.reset}`;
43
43
 
44
44
  // ── Content lines ──
45
- const title = `${c.bold}${c.yellow}🔒 Permission Request${c.reset}`;
46
45
  const summary = `${c.bold}${perm.summary}${c.reset}`;
47
46
  const actions =
48
- `${c.green}[y]${c.reset} Allow once ` +
49
- `${c.cyan}[a]${c.reset} Always allow ` +
50
- `${c.red}[n]${c.reset} Deny`;
51
- const hint = `${c.dim}Press Esc to abort${c.reset}`;
47
+ `${surface.permission.allow}[y]${c.reset} Allow once ` +
48
+ `${surface.permission.always}[a]${c.reset} Always allow ` +
49
+ `${surface.permission.deny}[n]${c.reset} Deny`;
50
+ const hint = `${surface.permission.dim}Press Esc to abort${c.reset}`;
52
51
 
53
52
  // Wrap summary if it's wider than the inner area.
54
53
  const summaryLines = wrapAnsi(summary, inner);
@@ -62,24 +61,29 @@ export class PermissionPrompt implements Component {
62
61
  // Top border: ┌─── 🔒 Permission Request ───────┐
63
62
  const tagText = '🔒 Permission Request';
64
63
  const tagVisW = vw(tagText.replace(ANSI_RE, ''));
65
- const tagFull = `${c.bold}${c.yellow}${tagText}${c.reset}`;
64
+ const tagFull = `${surface.permission.header}${tagText}${c.reset}`;
66
65
  const dashesBefore = Math.max(1, Math.floor((W - 2 - tagVisW) / 2));
67
66
  const dashesAfter = Math.max(1, W - 2 - dashesBefore - tagVisW);
68
- lines.push(
69
- paint('┌') + rep('─', dashesBefore) + tagFull + rep('─', dashesAfter) + paint('┐'),
70
- );
67
+ lines.push(paint('┌') + rep('─', dashesBefore) + tagFull + rep('─', dashesAfter) + paint('┐'));
71
68
 
72
69
  // Empty content row
73
70
  lines.push(paint('│') + rep(' ', inner) + paint('│'));
74
71
 
75
72
  // "pal wants to:" label
76
73
  const label = ' pal wants to:';
77
- lines.push(paint('│') + `${c.dim}${label}${c.reset}` + rep(' ', Math.max(0, inner - vw(label))) + paint('│'));
74
+ lines.push(
75
+ paint('│') +
76
+ `${surface.permission.dim}${label}${c.reset}` +
77
+ rep(' ', Math.max(0, inner - vw(label))) +
78
+ paint('│'),
79
+ );
78
80
 
79
81
  // Summary (possibly wrapped)
80
82
  for (const sl of summaryLines) {
81
83
  const visW = vw(sl);
82
- lines.push(paint('│') + ' ' + sl + c.reset + rep(' ', Math.max(0, inner - 2 - visW)) + paint('│'));
84
+ lines.push(
85
+ paint('│') + ' ' + sl + c.reset + rep(' ', Math.max(0, inner - 2 - visW)) + paint('│'),
86
+ );
83
87
  }
84
88
 
85
89
  // Empty row
@@ -101,7 +105,14 @@ export class PermissionPrompt implements Component {
101
105
  );
102
106
  } else {
103
107
  // Fallback: left-align if too wide
104
- lines.push(paint('│') + ' ' + actions + c.reset + rep(' ', Math.max(0, inner - 1 - actionsVisW)) + paint('│'));
108
+ lines.push(
109
+ paint('│') +
110
+ ' ' +
111
+ actions +
112
+ c.reset +
113
+ rep(' ', Math.max(0, inner - 1 - actionsVisW)) +
114
+ paint('│'),
115
+ );
105
116
  }
106
117
 
107
118
  // Empty row
@@ -112,11 +123,16 @@ export class PermissionPrompt implements Component {
112
123
  if (hintVisW <= inner) {
113
124
  const leftPad = Math.floor((inner - hintVisW) / 2);
114
125
  const rightPad = inner - hintVisW - leftPad;
126
+ lines.push(paint('│') + rep(' ', leftPad) + hint + c.reset + rep(' ', rightPad) + paint('│'));
127
+ } else {
115
128
  lines.push(
116
- paint('│') + rep(' ', leftPad) + hint + c.reset + rep(' ', rightPad) + paint('│'),
129
+ paint('│') +
130
+ ' ' +
131
+ hint +
132
+ c.reset +
133
+ rep(' ', Math.max(0, inner - 1 - hintVisW)) +
134
+ paint('│'),
117
135
  );
118
- } else {
119
- lines.push(paint('│') + ' ' + hint + c.reset + rep(' ', Math.max(0, inner - 1 - hintVisW)) + paint('│'));
120
136
  }
121
137
 
122
138
  // Empty content row
@@ -146,7 +162,7 @@ function wrapAnsi(text: string, maxWidth: number): string[] {
146
162
  let currentW = 0;
147
163
 
148
164
  // Tokenize: alternating plain text and ANSI escape sequences
149
- const tokens = text.split(/(\x1b\[[0-9;]*m)/);
165
+ const tokens = text.split(new RegExp('(\x1b\\[[0-9;]*m)'));
150
166
  let activeStyle = '';
151
167
 
152
168
  for (const token of tokens) {
@@ -9,7 +9,8 @@
9
9
  import {Text} from '@earendil-works/pi-tui';
10
10
  import type {Component} from '@earendil-works/pi-tui';
11
11
 
12
- import {c} from '../theme.js';
12
+ import {c, surface} from '../theme.js';
13
+ import {paintPanel} from '../tui/paint-panel.js';
13
14
  import {formatTokens, formatCost, formatCacheHit} from '../utils/cost.js';
14
15
  import {truncate} from '../utils/format.js';
15
16
  import {state} from '../state.js';
@@ -23,12 +24,20 @@ export interface SidebarStats {
23
24
  priced: boolean;
24
25
  }
25
26
 
26
- const TODO_ICON: Record<string, string> = {
27
- completed: `${c.green}✓${c.reset}`,
28
- in_progress: `${c.yellow}▸${c.reset}`,
29
- cancelled: `${c.dim}✗${c.reset}`,
30
- pending: `${c.dim}·${c.reset}`,
31
- };
27
+ // Todo icons were a module-scope constant, which froze the palette at import
28
+ // time (before setTheme runs). Built per call so theme swaps apply live.
29
+ function todoIcon(status: string): string {
30
+ switch (status) {
31
+ case 'completed':
32
+ return `${c.green}✓${c.reset}`;
33
+ case 'in_progress':
34
+ return `${c.yellow}▸${c.reset}`;
35
+ case 'cancelled':
36
+ return `${c.dim}✗${c.reset}`;
37
+ default:
38
+ return `${c.dim}·${c.reset}`;
39
+ }
40
+ }
32
41
 
33
42
  export class Sidebar implements Component {
34
43
  private readonly body = new Text('', 1, 0);
@@ -55,10 +64,10 @@ export class Sidebar implements Component {
55
64
  // The same symbols the old status bar used — one stat per line, left-aligned.
56
65
  const lines: string[] = [];
57
66
  if (state.sessionTitle) {
58
- lines.push(`${c.bold}${c.cyan}${truncate(state.sessionTitle, w - 4)}${c.reset}`, '');
67
+ lines.push(`${surface.sidebar.title}${truncate(state.sessionTitle, w - 4)}${c.reset}`, '');
59
68
  }
60
69
  lines.push(
61
- `${c.bold}${c.green}Info${c.reset}`,
70
+ `${surface.sidebar.itemAccent}Info${c.reset}`,
62
71
  `↑${formatTokens(s.inputTokens)}${s.inputTokens > 0 && s.cachedTokens > 0 ? ` ${c.dim}${formatCacheHit(s.cachedTokens, s.inputTokens)}${c.reset}` : ''}`,
63
72
  `↓${formatTokens(s.outputTokens)}`,
64
73
  `${formatCost(s.cost)}${s.priced ? '' : `${c.dim} (unpriced)${c.reset}`}`,
@@ -67,42 +76,46 @@ export class Sidebar implements Component {
67
76
  );
68
77
 
69
78
  // Extensions
70
- lines.push(`${c.bold}${c.green}Extensions${c.reset}`);
79
+ lines.push(`${surface.sidebar.itemAccent}Extensions${c.reset}`);
71
80
  if (state.extensions.length === 0) {
72
- lines.push(`${c.dim}· none${c.reset}`);
81
+ lines.push(`${surface.sidebar.dim}· none${c.reset}`);
73
82
  } else {
74
83
  const sorted = [...state.extensions].sort((a, b) => a.name.localeCompare(b.name));
75
84
  for (const ext of sorted) {
76
- const dot = ext.active ? `${c.green}●${c.reset}` : `${c.red}●${c.reset}`;
85
+ const dot = ext.active ? `${surface.sidebar.item}●${c.reset}` : `${c.red}●${c.reset}`;
77
86
  lines.push(`${dot} ${truncate(ext.name, w - 5)}`);
78
87
  }
79
88
  }
80
89
  lines.push('');
81
90
 
82
91
  // MCP Servers
83
- lines.push(`${c.bold}${c.green}MCP Servers${c.reset}`);
92
+ lines.push(`${surface.sidebar.itemAccent}MCP Servers${c.reset}`);
84
93
  if (state.mcpServers.length === 0) {
85
- lines.push(`${c.dim}· none${c.reset}`);
94
+ lines.push(`${surface.sidebar.dim}· none${c.reset}`);
86
95
  } else {
87
96
  const sorted = [...state.mcpServers].sort((a, b) => a.name.localeCompare(b.name));
88
97
  for (const srv of sorted) {
89
- const dot = srv.status === 'connected' ? `${c.green}●${c.reset}`
90
- : srv.status === 'starting' ? `${c.yellow}●${c.reset}`
91
- : `${c.red}●${c.reset}`;
92
- const tools = srv.toolCount > 0 ? ` ${c.dim}(${srv.toolCount} tools)${c.reset}` : '';
98
+ const dot =
99
+ srv.status === 'connected'
100
+ ? `${surface.sidebar.item}●${c.reset}`
101
+ : srv.status === 'starting'
102
+ ? `${c.yellow}●${c.reset}`
103
+ : `${c.red}●${c.reset}`;
104
+ const tools =
105
+ srv.toolCount > 0 ? ` ${surface.sidebar.dim}(${srv.toolCount} tools)${c.reset}` : '';
93
106
  lines.push(`${dot} ${truncate(srv.name, w - 5)}${tools}`);
94
107
  }
95
108
  }
96
109
  lines.push('');
97
110
 
98
- lines.push(`${c.bold}${c.green}Todos${c.reset}`);
111
+ lines.push(`${surface.sidebar.itemAccent}Todos${c.reset}`);
99
112
 
100
113
  const todos = state.currentTodos;
101
114
  if (todos.length === 0) {
102
- lines.push(`${c.dim}· none${c.reset}`);
115
+ lines.push(`${surface.sidebar.dim}· none${c.reset}`);
103
116
  } else {
104
117
  for (const t of todos) {
105
- const icon = TODO_ICON[t.status] ?? `${c.dim}·${c.reset}`;
118
+ const icon = todoIcon(t.status);
106
119
  lines.push(`${icon} ${truncate(t.content, w - 6)}`);
107
120
  }
108
121
  }
@@ -115,6 +128,10 @@ export class Sidebar implements Component {
115
128
  }
116
129
 
117
130
  render(width: number): string[] {
118
- return this.body.render(width);
131
+ // Panel fill behind every row — single choke point; bg '' (vanilla)
132
+ // passes through byte-identical. Status line and editor rows paint no
133
+ // panel: their GitHub bg is the canvas #0d1117 itself, so they sit
134
+ // flush on the canvas while the sidebar reads darker (#010409).
135
+ return this.body.render(width).map((line) => paintPanel(surface.sidebar.bg, line, width));
119
136
  }
120
137
  }