pal-code 0.0.11 → 0.0.13

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.
@@ -17,9 +17,7 @@ export function cmdHelp(container: Container, tui: TUI): void {
17
17
  container.addChild(new Text(` ${c.bold}Available commands:${c.reset}`, 1, 0));
18
18
  container.addChild(new Text(` ${c.cyan}/help${c.reset} Show this help message`, 1, 0));
19
19
  container.addChild(new Text(` ${c.cyan}/exit${c.reset} Save session and quit`, 1, 0));
20
- container.addChild(
21
- new Text(` ${c.cyan}/new${c.reset} Start a new session`, 1, 0),
22
- );
20
+ container.addChild(new Text(` ${c.cyan}/new${c.reset} Start a new session`, 1, 0));
23
21
  container.addChild(new Text(` ${c.cyan}/sessions${c.reset} List recent sessions`, 1, 0));
24
22
  container.addChild(
25
23
  new Text(` ${c.cyan}/undo${c.reset} Restore the file from your last edit`, 1, 0),
@@ -49,6 +47,16 @@ export function cmdHelp(container: Container, tui: TUI): void {
49
47
  container.addChild(
50
48
  new Text(` ${c.cyan}/key${c.reset} Set API key interactively`, 1, 0),
51
49
  );
50
+ container.addChild(
51
+ new Text(
52
+ ` ${c.cyan}/export${c.reset} [file] Export config to JSON (secrets excluded)`,
53
+ 1,
54
+ 0,
55
+ ),
56
+ );
57
+ container.addChild(
58
+ new Text(` ${c.cyan}/import${c.reset} <file> Merge an export (local API keys kept)`, 1, 0),
59
+ );
52
60
  container.addChild(
53
61
  new Text(` ${c.cyan}/roles${c.reset} List agent roles (agents extension)`, 1, 0),
54
62
  );
@@ -63,34 +71,34 @@ export function cmdHelp(container: Container, tui: TUI): void {
63
71
  new Text(` ${c.cyan}/execute${c.reset} Leave plan mode and implement`, 1, 0),
64
72
  );
65
73
  container.addChild(
66
- new Text(` ${c.cyan}/ext add${c.reset} <pkg> Install an extension`, 1, 0),
74
+ new Text(` ${c.cyan}/ext add${c.reset} <pkg> Install an extension from npm`, 1, 0),
67
75
  );
68
76
  container.addChild(
69
77
  new Text(` ${c.cyan}/ext remove${c.reset} <pkg> Remove an extension`, 1, 0),
70
78
  );
71
- container.addChild(
72
- new Text(` ${c.cyan}/ext list${c.reset} List loaded extensions`, 1, 0),
73
- );
74
79
  container.addChild(
75
80
  new Text(
76
- ` ${c.cyan}/mcp add${c.reset} <name> <cmd> [args] Add a local MCP server`,
81
+ ` ${c.cyan}/ext search${c.reset} <q> Search the npm registry for extensions`,
77
82
  1,
78
83
  0,
79
84
  ),
80
85
  );
81
86
  container.addChild(
82
- new Text(
83
- ` ${c.cyan}/mcp add-remote${c.reset} <name> <url> Add a remote MCP server`,
84
- 1,
85
- 0,
86
- ),
87
+ new Text(` ${c.cyan}/ext list${c.reset} List loaded extensions`, 1, 0),
87
88
  );
88
89
  container.addChild(
89
- new Text(` ${c.cyan}/mcp remove${c.reset} <name> Remove an MCP server`, 1, 0),
90
+ new Text(` ${c.cyan}/ext browse${c.reset} Browse curated extensions`, 1, 0),
90
91
  );
91
92
  container.addChild(
92
- new Text(` ${c.cyan}/mcp list${c.reset} List MCP servers`, 1, 0),
93
+ new Text(` ${c.cyan}/mcp add${c.reset} <name> <cmd> [args] Add a local MCP server`, 1, 0),
93
94
  );
95
+ container.addChild(
96
+ new Text(` ${c.cyan}/mcp add-remote${c.reset} <name> <url> Add a remote MCP server`, 1, 0),
97
+ );
98
+ container.addChild(
99
+ new Text(` ${c.cyan}/mcp remove${c.reset} <name> Remove an MCP server`, 1, 0),
100
+ );
101
+ container.addChild(new Text(` ${c.cyan}/mcp list${c.reset} List MCP servers`, 1, 0));
94
102
  container.addChild(
95
103
  new Text(` ${c.cyan}/mcp browse${c.reset} Browse curated MCP servers`, 1, 0),
96
104
  );
@@ -100,6 +108,13 @@ export function cmdHelp(container: Container, tui: TUI): void {
100
108
  container.addChild(
101
109
  new Text(` ${c.cyan}/permission${c.reset} Show permission rules and whitelist`, 1, 0),
102
110
  );
111
+ container.addChild(
112
+ new Text(
113
+ ` ${c.cyan}/copy${c.reset} [code] Copy last reply (or code block) to clipboard`,
114
+ 1,
115
+ 0,
116
+ ),
117
+ );
103
118
  if (isDebugEnabled()) {
104
119
  container.addChild(
105
120
  new Text(` ${c.cyan}/reload${c.reset} Hot-reload extensions (dev)`, 1, 0),
@@ -0,0 +1,88 @@
1
+ /**
2
+ * AssistantBody — the static rendering of one assistant message.
3
+ *
4
+ * Prose is handed to pi-tui's Markdown exactly as before; fenced code
5
+ * segments are extracted and drawn as language-tagged boxes
6
+ * (`codeBlockBox`) so snippets read as distinct blocks instead of running
7
+ * into the surrounding text.
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.
12
+ */
13
+
14
+ import {Markdown, type Component} from '@earendil-works/pi-tui';
15
+
16
+ import {markdownTheme} from '../theme.js';
17
+ import {codeBlockBox, parseCodeSegments} from '../utils/code-block.js';
18
+ import type {CodeSegment} from '../utils/code-block.js';
19
+
20
+ type BodySegment =
21
+ | {readonly kind: 'text'; readonly md: Markdown}
22
+ | {readonly kind: 'code'; readonly code: string; readonly lang: string};
23
+
24
+ function assertNever(x: never): never {
25
+ throw new Error(`unhandled code segment: ${JSON.stringify(x)}`);
26
+ }
27
+
28
+ function toBodySegment(seg: CodeSegment): BodySegment {
29
+ switch (seg.kind) {
30
+ case 'text':
31
+ return {kind: 'text', md: new Markdown(seg.text, 1, 0, markdownTheme)};
32
+ case 'code':
33
+ return {kind: 'code', code: seg.lines.join('\n'), lang: seg.lang};
34
+ default:
35
+ return assertNever(seg);
36
+ }
37
+ }
38
+
39
+ export class AssistantBody implements Component {
40
+ private segments: readonly BodySegment[];
41
+ private cached: string[] | null = null;
42
+ private cachedWidth = -1;
43
+
44
+ constructor(content: string) {
45
+ this.segments = parseCodeSegments(content).map(toBodySegment);
46
+ }
47
+
48
+ /**
49
+ * Swap the message content (streaming path) — re-parse segments and drop
50
+ * the width cache so the next render rebuilds. Callers repaint via
51
+ * `requestRender()`; an open trailing fence boxes immediately and closes
52
+ * when the closing fence streams in.
53
+ */
54
+ setText(content: string): void {
55
+ this.segments = parseCodeSegments(content).map(toBodySegment);
56
+ this.invalidate();
57
+ }
58
+
59
+ invalidate(): void {
60
+ this.cached = null;
61
+ this.cachedWidth = -1;
62
+ }
63
+
64
+ render(width: number): string[] {
65
+ if (this.cached && this.cachedWidth === width) return this.cached;
66
+
67
+ const out: string[] = [];
68
+ for (const seg of this.segments) {
69
+ if (out.length > 0) out.push(''); // one blank line between blocks
70
+ out.push(...renderSegment(seg, width));
71
+ }
72
+
73
+ this.cached = out;
74
+ this.cachedWidth = width;
75
+ return out;
76
+ }
77
+ }
78
+
79
+ function renderSegment(seg: BodySegment, width: number): string[] {
80
+ switch (seg.kind) {
81
+ case 'text':
82
+ return seg.md.render(width);
83
+ case 'code':
84
+ return codeBlockBox(seg.code, width, seg.lang).split('\n');
85
+ default:
86
+ return assertNever(seg);
87
+ }
88
+ }
@@ -10,10 +10,11 @@
10
10
  * than replaying hundreds of blocks at once.
11
11
  */
12
12
 
13
- import {Container, Text, Markdown} from '@earendil-works/pi-tui';
13
+ import {type Container, Text} from '@earendil-works/pi-tui';
14
14
  import type {StoredMessage, CompactionRecord} from '@pal-code/core';
15
15
 
16
- import {c, markdownTheme} from '../theme.js';
16
+ import {c} from '../theme.js';
17
+ import {AssistantBody} from './assistant-body.js';
17
18
  import {MessageBox} from './message-box.js';
18
19
  import {formatMessageTimestamp} from '../utils/format.js';
19
20
  import type {AgentRole} from '../state.js';
@@ -94,9 +95,9 @@ function renderMessage(container: Container, m: StoredMessage): void {
94
95
  return;
95
96
  }
96
97
 
97
- // assistant (with or without tool calls) — render the prose it produced
98
+ // assistant (with or without tool calls) — prose plus boxed code fences
98
99
  if (m.content && m.content.trim()) {
99
- container.addChild(new Markdown(m.content, 1, 0, markdownTheme));
100
+ container.addChild(new AssistantBody(m.content));
100
101
  }
101
102
  if (m.toolCalls?.length) {
102
103
  for (const tc of m.toolCalls) {
@@ -7,11 +7,12 @@
7
7
  * `finish()` collapses it to a one-line summary; `expand()` re-inflates it.
8
8
  */
9
9
 
10
- import {Container, Text, Markdown} from '@earendil-works/pi-tui';
10
+ import {Container, Text} from '@earendil-works/pi-tui';
11
11
  import type {TUI} from '@earendil-works/pi-tui';
12
12
 
13
- import {c, markdownTheme} from '../theme.js';
13
+ import {c} from '../theme.js';
14
14
  import {truncate} from '../utils/format.js';
15
+ import {AssistantBody} from './assistant-body.js';
15
16
  // ═══════════════════════════════════════════════════════════════════════════════
16
17
  // Live run view — one mutable block per turn instead of an append-only log
17
18
  // ═══════════════════════════════════════════════════════════════════════════════
@@ -40,8 +41,8 @@ export class RunView {
40
41
  private readonly rowTexts: Text[] = [];
41
42
  private summaryText: Text | null = null;
42
43
  /** All streaming answer blocks from every round — only the last is active. */
43
- private responses: Markdown[] = [];
44
- private response: Markdown | null = null;
44
+ private responses: AssistantBody[] = [];
45
+ private response: AssistantBody | null = null;
45
46
  /** True after finish() re-parented responses to `this.parent`. */
46
47
  private reParented = false;
47
48
  private roundNo = 0;
@@ -131,9 +132,9 @@ export class RunView {
131
132
  }
132
133
 
133
134
  /** The streaming answer block for the current round, created on first use. */
134
- responseBlock(): Markdown {
135
+ responseBlock(): AssistantBody {
135
136
  if (!this.response) {
136
- this.response = new Markdown('', 1, 0, markdownTheme);
137
+ this.response = new AssistantBody('');
137
138
  this.responses.push(this.response);
138
139
  this.container.addChild(this.response);
139
140
  }
@@ -64,6 +64,25 @@ function strEnv(name: string, fallback: string): string {
64
64
  return raw && raw.trim() ? raw : fallback;
65
65
  }
66
66
 
67
+ /**
68
+ * Read a positive integer out of a config file value.
69
+ *
70
+ * Returns undefined for anything that isn't a finite number at or above `min`,
71
+ * so a typo in `~/.pal/config.json` falls through to the default rather than
72
+ * poisoning the context limit with NaN (which would disable compaction).
73
+ */
74
+ function intConfig(value: unknown, label: string, min = 1000): number | undefined {
75
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < min) {
76
+ if (value !== undefined) {
77
+ console.error(
78
+ `pal: ignoring invalid ${label}=${JSON.stringify(value)} in config.json — must be a number >= ${min}`,
79
+ );
80
+ }
81
+ return undefined;
82
+ }
83
+ return Math.floor(value);
84
+ }
85
+
67
86
  export function loadConfig(): Config {
68
87
  const saved = readPalConfig();
69
88
  const savedModel =
@@ -75,7 +94,14 @@ export function loadConfig(): Config {
75
94
 
76
95
  // Extensions: use saved list or default to the shipped set.
77
96
  const savedExtensions = Array.isArray(saved.extensions) ? saved.extensions : undefined;
78
- const extensions = savedExtensions ?? ['@pal-code/web-search', '@pal-code/agents', '@pal-code/mcp'];
97
+ const extensions = savedExtensions ?? [
98
+ '@pal-code/web-search',
99
+ '@pal-code/agents',
100
+ '@pal-code/mcp',
101
+ ];
102
+
103
+ // Context limit: env wins, then config.json, then the built-in default.
104
+ const savedContextLimit = intConfig(saved.contextLimit, 'contextLimit');
79
105
 
80
106
  return {
81
107
  model: strEnv('MODEL', strEnv('PAL_MODEL', savedModel ?? 'deepseek/deepseek-chat')),
@@ -83,7 +109,7 @@ export function loadConfig(): Config {
83
109
  maxToolRounds: intEnv('MAX_TOOL_ROUNDS', 100, 1),
84
110
  maxRetries: intEnv('MAX_RETRIES', 1, 0),
85
111
  retryBaseMs: intEnv('RETRY_BASE_MS', 2000, 0),
86
- contextLimit: intEnv('CONTEXT_LIMIT', 100_000, 1000),
112
+ contextLimit: intEnv('CONTEXT_LIMIT', savedContextLimit ?? 100_000, 1000),
87
113
  reasoningEffort: effortEnv(),
88
114
  extensions,
89
115
  };