pal-code 0.0.5
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/LICENSE +21 -0
- package/README.md +169 -0
- package/SYSTEM.md +146 -0
- package/bin/pal-code.js +131 -0
- package/package.json +68 -0
- package/packages/cli/README.md +77 -0
- package/packages/cli/src/agent-panel.ts +94 -0
- package/packages/cli/src/commands/ext.ts +79 -0
- package/packages/cli/src/commands/mcp-registry.ts +136 -0
- package/packages/cli/src/commands/mcp.ts +186 -0
- package/packages/cli/src/commands.ts +153 -0
- package/packages/cli/src/components/message-box.ts +35 -0
- package/packages/cli/src/components/permission-prompt.ts +185 -0
- package/packages/cli/src/components/render-history.ts +143 -0
- package/packages/cli/src/components/run-view.ts +256 -0
- package/packages/cli/src/components/sidebar.ts +120 -0
- package/packages/cli/src/components/status-line.ts +26 -0
- package/packages/cli/src/config.ts +107 -0
- package/packages/cli/src/index.ts +1606 -0
- package/packages/cli/src/overlays/key-input.ts +167 -0
- package/packages/cli/src/overlays/model-picker.ts +106 -0
- package/packages/cli/src/overlays/session-picker.ts +168 -0
- package/packages/cli/src/overlays/wizard.ts +145 -0
- package/packages/cli/src/state.ts +133 -0
- package/packages/cli/src/theme.ts +88 -0
- package/packages/cli/src/turn.ts +266 -0
- package/packages/cli/src/utils/cost.ts +111 -0
- package/packages/cli/src/utils/format.test.ts +167 -0
- package/packages/cli/src/utils/format.ts +248 -0
- package/packages/cli/src/utils/highlight.ts +358 -0
- package/packages/cli/src/utils/permissions.ts +196 -0
- package/packages/core/src/agent/events.ts +36 -0
- package/packages/core/src/agent/fallback.test.ts +99 -0
- package/packages/core/src/agent/fallback.ts +344 -0
- package/packages/core/src/agent/index.ts +35 -0
- package/packages/core/src/agent/protocol.test.ts +136 -0
- package/packages/core/src/agent/resume-compaction.test.ts +116 -0
- package/packages/core/src/agent/roles.ts +188 -0
- package/packages/core/src/agent/run.test.ts +207 -0
- package/packages/core/src/agent/run.ts +713 -0
- package/packages/core/src/agent/scope.ts +34 -0
- package/packages/core/src/agent/todos.ts +66 -0
- package/packages/core/src/compaction.protocol.test.ts +90 -0
- package/packages/core/src/compaction.test.ts +101 -0
- package/packages/core/src/compaction.ts +243 -0
- package/packages/core/src/config.test.ts +40 -0
- package/packages/core/src/config.ts +387 -0
- package/packages/core/src/extensions.ts +178 -0
- package/packages/core/src/index.ts +157 -0
- package/packages/core/src/logger.ts +54 -0
- package/packages/core/src/provider/auth.ts +153 -0
- package/packages/core/src/provider/complete.ts +63 -0
- package/packages/core/src/provider/index.ts +6 -0
- package/packages/core/src/provider/openrouter.test.ts +59 -0
- package/packages/core/src/provider/openrouter.ts +304 -0
- package/packages/core/src/provider/pick-model.ts +43 -0
- package/packages/core/src/provider/registry.ts +222 -0
- package/packages/core/src/provider/types.ts +61 -0
- package/packages/core/src/repo-map.ts +177 -0
- package/packages/core/src/session/index.ts +2 -0
- package/packages/core/src/session/migration.test.ts +104 -0
- package/packages/core/src/session/store-v2-repair.test.ts +209 -0
- package/packages/core/src/session/store.test.ts +235 -0
- package/packages/core/src/session/store.ts +677 -0
- package/packages/core/src/session/types.ts +72 -0
- package/packages/core/src/snapshots.ts +107 -0
- package/packages/core/src/tools/definitions.ts +365 -0
- package/packages/core/src/tools/executor.test.ts +122 -0
- package/packages/core/src/tools/executor.ts +1363 -0
- package/packages/core/src/tools/index.ts +5 -0
- package/packages/core/src/tools/search.ts +201 -0
- package/scripts/postinstall.js +121 -0
- package/scripts/preinstall.js +51 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {describe, expect, test} from 'bun:test';
|
|
2
|
+
import {stripTerminalSequences} from '@earendil-works/pi-tui';
|
|
3
|
+
|
|
4
|
+
import {userMessageBox, wrapText, truncate, displayWidth} from './format.js';
|
|
5
|
+
|
|
6
|
+
const plain = (s: string) => stripTerminalSequences(s);
|
|
7
|
+
|
|
8
|
+
/** Every row of the box must be exactly `width` visible columns wide. */
|
|
9
|
+
function expectRectangular(box: string, width: number) {
|
|
10
|
+
const W = Math.max(6, Math.floor(width));
|
|
11
|
+
const lines = box.split('\n');
|
|
12
|
+
for (const line of lines) {
|
|
13
|
+
expect(displayWidth(line)).toBe(W);
|
|
14
|
+
}
|
|
15
|
+
expect(plain(lines[0]!).startsWith('╭')).toBe(true);
|
|
16
|
+
expect(plain(lines.at(-1)!).startsWith('╰')).toBe(true);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ROLES = ['pal', 'build', 'architect', 'web-search', ''] as const;
|
|
20
|
+
|
|
21
|
+
const INPUTS: Array<[string, string]> = [
|
|
22
|
+
['short', 'hello'],
|
|
23
|
+
['multiline', 'first line\nsecond line'],
|
|
24
|
+
['wraps', 'the quick brown fox jumps over the lazy dog and keeps running'],
|
|
25
|
+
['empty', ''],
|
|
26
|
+
['whitespace', ' '],
|
|
27
|
+
['trailing newline', 'done\n'],
|
|
28
|
+
['crlf', 'a\r\nb'],
|
|
29
|
+
['tabs', '\tindented\ttext'],
|
|
30
|
+
['ansi', `\x1b[31mred\x1b[0m and \x1b[1mbold\x1b[0m`],
|
|
31
|
+
['cjk', '你好世界,这是一个测试'],
|
|
32
|
+
['emoji', 'ship it 🚀🎉 with 👨👩👧 family'],
|
|
33
|
+
['long word', 'x'.repeat(120)],
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
describe('userMessageBox', () => {
|
|
37
|
+
test('every row is exactly `width` columns for all widths, roles and inputs', () => {
|
|
38
|
+
for (const width of [2, 4, 6, 8, 12, 20, 40, 80]) {
|
|
39
|
+
for (const role of ROLES) {
|
|
40
|
+
for (const [name, input] of INPUTS) {
|
|
41
|
+
const box = userMessageBox(input, width, role);
|
|
42
|
+
try {
|
|
43
|
+
expectRectangular(box, width);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
throw new Error(`width=${width} role=${role || '(empty)'} input=${name}: ${err}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('shows the role tag once it fits, and drops it when it does not', () => {
|
|
53
|
+
expect(plain(userMessageBox('hi', 40, 'build'))).toContain('[builder]');
|
|
54
|
+
// '[build]' is 9 wide; it needs width >= 15.
|
|
55
|
+
expect(plain(userMessageBox('hi', 14, 'build'))).not.toContain('[builder]');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('timestamped header stays rectangular across widths, roles and stamps', () => {
|
|
59
|
+
const stamps = ['Mon 14:30', 'Mon 15 Jan 14:30', 'Mon 15 Jan 2025 14:30', ''];
|
|
60
|
+
for (const width of [2, 4, 6, 8, 12, 20, 30, 40, 80, 120]) {
|
|
61
|
+
for (const role of ROLES) {
|
|
62
|
+
for (const ts of stamps) {
|
|
63
|
+
const box = userMessageBox('hello', width, role, ts);
|
|
64
|
+
try {
|
|
65
|
+
expectRectangular(box, width);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
throw new Error(`width=${width} role=${role || '(empty)'} ts="${ts}": ${err}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('timestamp renders opposite the tag when there is room', () => {
|
|
75
|
+
const box = plain(userMessageBox('hi', 60, 'build', 'Mon 14:30'));
|
|
76
|
+
const header = box.split('\n')[0]!;
|
|
77
|
+
expect(header).toContain('[builder]');
|
|
78
|
+
expect(header).toContain('Mon 14:30');
|
|
79
|
+
// tag left of timestamp
|
|
80
|
+
expect(header.indexOf('[builder]')).toBeLessThan(header.indexOf('Mon 14:30'));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('timestamp is dropped before the tag when the box is narrow', () => {
|
|
84
|
+
// Wide enough for '[builder]' (needs 15) but not for tag + timestamp.
|
|
85
|
+
const box = plain(userMessageBox('hi', 16, 'build', 'Mon 15 Jan 2025 14:30'));
|
|
86
|
+
const header = box.split('\n')[0]!;
|
|
87
|
+
expect(header).toContain('[builder]');
|
|
88
|
+
expect(header).not.toContain('2025');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('renders a golden box for a short message', () => {
|
|
92
|
+
const expected =
|
|
93
|
+
`╭─ [pal] ${'─'.repeat(10)}╮\n` + `│ hi${' '.repeat(14)} │\n` + `╰${'─'.repeat(18)}╯`;
|
|
94
|
+
expect(plain(userMessageBox('hi', 20, 'pal'))).toBe(expected);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('never throws on degenerate widths', () => {
|
|
98
|
+
for (const width of [-10, 0, 1, 3, 5]) {
|
|
99
|
+
expect(() => userMessageBox('hello world', width, 'pal')).not.toThrow();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('wrapText', () => {
|
|
105
|
+
test('wraps words to the given width', () => {
|
|
106
|
+
expect(wrapText('one two three four', 8)).toEqual(['one two', 'three', 'four']);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('hard-breaks words longer than the width', () => {
|
|
110
|
+
const lines = wrapText('abcdefghij', 4);
|
|
111
|
+
expect(lines.every((l) => displayWidth(l) <= 4)).toBe(true);
|
|
112
|
+
expect(lines.join('')).toBe('abcdefghij');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('splits on CRLF, CR and LF alike', () => {
|
|
116
|
+
expect(wrapText('a\r\nb\rc\nd', 10)).toEqual(['a', 'b', 'c', 'd']);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('keeps a hanging indent on continuation lines', () => {
|
|
120
|
+
const lines = wrapText(' alpha beta gamma delta', 12);
|
|
121
|
+
for (const line of lines.slice(1)) expect(line.startsWith(' ')).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('preserves ANSI styling across the wrap', () => {
|
|
125
|
+
const lines = wrapText('\x1b[31mred red red red red\x1b[0m', 7);
|
|
126
|
+
expect(lines.every((l) => displayWidth(l) <= 7)).toBe(true);
|
|
127
|
+
expect(lines.join('')).toContain('red');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('returns a single empty line for empty input', () => {
|
|
131
|
+
expect(wrapText('', 10)).toEqual(['']);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('truncate', () => {
|
|
136
|
+
test('never returns more than `max` characters', () => {
|
|
137
|
+
const text = 'hello world';
|
|
138
|
+
for (let max = -2; max <= text.length + 2; max++) {
|
|
139
|
+
expect(truncate(text, max).length).toBeLessThanOrEqual(Math.max(0, max));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('adds an ellipsis when there is room for one', () => {
|
|
144
|
+
expect(truncate('hello world', 8)).toBe('hello...');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('hard-cuts when the budget cannot fit an ellipsis', () => {
|
|
148
|
+
expect(truncate('hello world', 3)).toBe('hel');
|
|
149
|
+
expect(truncate('hello world', 0)).toBe('');
|
|
150
|
+
expect(truncate('hello world', -5)).toBe('');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('leaves short enough text untouched', () => {
|
|
154
|
+
expect(truncate('hi', 5)).toBe('hi');
|
|
155
|
+
expect(truncate('', 5)).toBe('');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('displayWidth', () => {
|
|
160
|
+
test('ignores ANSI codes', () => {
|
|
161
|
+
expect(displayWidth('\x1b[31mred\x1b[0m')).toBe(3);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('counts wide characters as two columns', () => {
|
|
165
|
+
expect(displayWidth('你好')).toBe(4);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import {visibleWidth, wrapTextWithAnsi, truncateToWidth} from '@earendil-works/pi-tui';
|
|
2
|
+
|
|
3
|
+
import {c, agentColors, agentLabels} from '../theme.js';
|
|
4
|
+
import type {AgentRole} from '../state.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Small text helpers shared by the renderers.
|
|
8
|
+
*
|
|
9
|
+
* Width is always measured in *terminal columns* via pi-tui's `visibleWidth`
|
|
10
|
+
* (ANSI-aware, treats CJK/emoji as two cells, combining marks as zero), never
|
|
11
|
+
* in JavaScript string length — otherwise coloured or wide text misaligns.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Visible width of a string in terminal columns. */
|
|
15
|
+
export const displayWidth = visibleWidth;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Cap a string at `max` characters, appending an ellipsis when truncated.
|
|
19
|
+
*
|
|
20
|
+
* The result is never longer than `max`, even for tiny budgets: when there is
|
|
21
|
+
* no room for the ellipsis the text is hard-cut instead (`max - 3` would go
|
|
22
|
+
* negative and yield `'...'` out of `''`).
|
|
23
|
+
*/
|
|
24
|
+
export function truncate(text: string, max: number): string {
|
|
25
|
+
if (max <= 0) return '';
|
|
26
|
+
if (text.length <= max) return text;
|
|
27
|
+
return max <= 3 ? text.substring(0, max) : text.substring(0, max - 3) + '...';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Derive a short, readable session title from the first user message.
|
|
32
|
+
*
|
|
33
|
+
* Strips common conversational filler ("can you", "help me", …), takes the
|
|
34
|
+
* first sentence, and truncates to `max` chars on a word boundary.
|
|
35
|
+
*/
|
|
36
|
+
const FILLER =
|
|
37
|
+
/^(please\s+|hey\s+|hi\s+|hello\s+|so\s+|basically\s+|actually\s+|just\s+|can you\s+|could you\s+|would you\s+|help me\s+|i need to\s+|i want to\s+|i'd like to\s+|make sure you\s+|be sure to\s+)/i;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Wrap `text` to `maxWidth` visible columns.
|
|
41
|
+
*
|
|
42
|
+
* Word-wraps (hard-breaking words longer than the width), keeps ANSI styling
|
|
43
|
+
* across breaks, accepts CRLF/CR/LF, and gives wrapped continuation lines a
|
|
44
|
+
* hanging indent matching the original line's leading whitespace so pasted
|
|
45
|
+
* code and lists stay readable.
|
|
46
|
+
*/
|
|
47
|
+
export function wrapText(text: string, maxWidth: number): string[] {
|
|
48
|
+
const width = Math.max(1, Math.floor(maxWidth));
|
|
49
|
+
const out: string[] = [];
|
|
50
|
+
|
|
51
|
+
for (const raw of text.split(/\r\n|\r|\n/)) {
|
|
52
|
+
// Normalize tabs the same way `visibleWidth` does, so measurement agrees.
|
|
53
|
+
const line = raw.replace(/\t/g, ' ');
|
|
54
|
+
const indent = (/^ */.exec(line)?.[0] ?? '').slice(0, Math.max(0, width - 1));
|
|
55
|
+
const avail = Math.max(1, width - indent.length);
|
|
56
|
+
const parts = wrapTextWithAnsi(line, avail);
|
|
57
|
+
for (const [i, part] of parts.entries()) {
|
|
58
|
+
out.push(i === 0 ? part : indent + part);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return out.length > 0 ? out : [''];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Wrap a user message in a rounded Unicode box.
|
|
67
|
+
*
|
|
68
|
+
* The role tag sits in the top border (coloured by role, dimmed border); the
|
|
69
|
+
* body carries only the message text. Every emitted row is exactly `width`
|
|
70
|
+
* visible columns wide, so the box stays aligned at any terminal size:
|
|
71
|
+
*
|
|
72
|
+
* ╭─ [builder] ─────────╮
|
|
73
|
+
* │ message text here │
|
|
74
|
+
* │ continued line │
|
|
75
|
+
* ╰─────────────────────╯
|
|
76
|
+
*
|
|
77
|
+
* Narrow terminals degrade cleanly: the tag is dropped once it no longer fits,
|
|
78
|
+
* and `width` is clamped to a floor so the borders never collapse.
|
|
79
|
+
*/
|
|
80
|
+
export function userMessageBox(
|
|
81
|
+
text: string,
|
|
82
|
+
width: number,
|
|
83
|
+
role: AgentRole = 'pal',
|
|
84
|
+
timestamp = '',
|
|
85
|
+
): string {
|
|
86
|
+
// One clamp before anything is derived, so header/body/footer agree.
|
|
87
|
+
const W = Math.max(6, Math.floor(width));
|
|
88
|
+
const inner = W - 4; // columns between "│ " and " │"
|
|
89
|
+
const textWidth = Math.max(1, inner - 1); // reserve the last inner column
|
|
90
|
+
|
|
91
|
+
const label = String(agentLabels[role] ?? role ?? '');
|
|
92
|
+
const tagText = `[${label}]`;
|
|
93
|
+
const tagWidth = visibleWidth(tagText);
|
|
94
|
+
const roleColor = agentColors[role] ?? c.cyan;
|
|
95
|
+
|
|
96
|
+
// Border glyphs are role-coloured but dimmed, so the box frames content
|
|
97
|
+
// without competing with it; the tag itself stays bright.
|
|
98
|
+
const paint = (s: string) => `${c.dim}${roleColor}${s}${c.reset}`;
|
|
99
|
+
|
|
100
|
+
const header = buildHeader(W, tagText, tagWidth, roleColor, paint, timestamp);
|
|
101
|
+
|
|
102
|
+
const body = wrapText(text, textWidth)
|
|
103
|
+
.map((line) => {
|
|
104
|
+
const safe = truncateToWidth(line.trimEnd(), textWidth, '');
|
|
105
|
+
const pad = Math.max(0, inner - visibleWidth(safe));
|
|
106
|
+
return `${paint('│')} ${safe}${c.reset}${' '.repeat(pad)} ${paint('│')}`;
|
|
107
|
+
})
|
|
108
|
+
.join('\n');
|
|
109
|
+
|
|
110
|
+
const footer = paint(`╰${'─'.repeat(W - 2)}╯`);
|
|
111
|
+
|
|
112
|
+
return `${header}\n${body}\n${footer}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Build the top border.
|
|
117
|
+
*
|
|
118
|
+
* Layout: `╭─ [tag] ─────── Mon 14:30 ─╮`. The timestamp is right-aligned,
|
|
119
|
+
* opposite the left-aligned agent tag. Width is measured in terminal columns
|
|
120
|
+
* with `visibleWidth` (never `String.length`) so the dashes fill exactly and the
|
|
121
|
+
* border can't drift — the failure mode when a tag was first added here.
|
|
122
|
+
*
|
|
123
|
+
* Both decorations are dropped, in order, when the box is too narrow:
|
|
124
|
+
* timestamp first, then the tag, then a plain border.
|
|
125
|
+
*/
|
|
126
|
+
function buildHeader(
|
|
127
|
+
W: number,
|
|
128
|
+
tagText: string,
|
|
129
|
+
tagWidth: number,
|
|
130
|
+
roleColor: string,
|
|
131
|
+
paint: (s: string) => string,
|
|
132
|
+
timestamp: string,
|
|
133
|
+
): string {
|
|
134
|
+
const tsText = timestamp ? String(timestamp) : '';
|
|
135
|
+
const tsWidth = visibleWidth(tsText);
|
|
136
|
+
|
|
137
|
+
const withTag = (ts: string, tsW: number): string => {
|
|
138
|
+
if (ts) {
|
|
139
|
+
// Target: `╭─ [tag] ─── {ts} ─╮`
|
|
140
|
+
// Fixed columns: ╭(1) ─(1) ␠(1) tag + ␠(1) [dashes] ␠(1) ts ␠(1) ─(1) ╮(1)
|
|
141
|
+
const fixed = 1 + 1 + 1 + tagWidth + 1 + 1 + tsW + 1 + 1 + 1;
|
|
142
|
+
const dashes = W - fixed;
|
|
143
|
+
if (dashes >= 1) {
|
|
144
|
+
return (
|
|
145
|
+
`${paint('╭─')} ${roleColor}${tagText}${c.reset} ` +
|
|
146
|
+
`${paint('─'.repeat(dashes))} ` +
|
|
147
|
+
`${c.dim}${ts}${c.reset} ` +
|
|
148
|
+
`${paint('─╮')}`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return '';
|
|
152
|
+
}
|
|
153
|
+
// Target: `╭─ [tag] ─────╮`
|
|
154
|
+
// Fixed columns: ╭(1) ─(1) ␠(1) tag ␠(1) [dashes] ╮(1)
|
|
155
|
+
const dashes = W - tagWidth - 5;
|
|
156
|
+
if (dashes >= 1) {
|
|
157
|
+
return `${paint('╭─')} ${roleColor}${tagText}${c.reset} ` + `${paint('─'.repeat(dashes))}${paint('╮')}`;
|
|
158
|
+
}
|
|
159
|
+
return '';
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
if (tagWidth + 6 <= W) {
|
|
163
|
+
const tagged = withTag(tsText, tsWidth);
|
|
164
|
+
if (tagged) return tagged;
|
|
165
|
+
// Timestamp didn't fit alongside the tag — keep the tag, drop the time.
|
|
166
|
+
const fallback = withTag('', 0);
|
|
167
|
+
if (fallback) return fallback;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// No room for the tag: try just the timestamp, else a plain border.
|
|
171
|
+
if (tsWidth && tsWidth + 4 <= W) {
|
|
172
|
+
const dashes = W - tsWidth - 4;
|
|
173
|
+
if (dashes >= 1) {
|
|
174
|
+
return paint(`╭${'─'.repeat(dashes)} `) + `${c.dim}${tsText}${c.reset} ` + paint('─╮');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return paint(`╭${'─'.repeat(W - 2)}╮`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function generateTitle(text: string, max = 40): string {
|
|
181
|
+
let s = text.replace(FILLER, '').trim();
|
|
182
|
+
// Take only the first sentence (up to the first period, exclamation, or newline).
|
|
183
|
+
const sentEnd = s.search(/[.!?\n]/);
|
|
184
|
+
if (sentEnd > 5) s = s.substring(0, sentEnd).trim();
|
|
185
|
+
// Normalize whitespace.
|
|
186
|
+
s = s.replace(/\s+/g, ' ');
|
|
187
|
+
if (s.length <= max) return s;
|
|
188
|
+
// Truncate on a word boundary.
|
|
189
|
+
const cut = s.lastIndexOf(' ', max - 3);
|
|
190
|
+
return (cut > 10 ? s.substring(0, cut) : s.substring(0, max - 3)) + '…';
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Clean up a model-generated title: strip wrapping quotes/markdown, collapse
|
|
195
|
+
* whitespace, drop a trailing period, and cap the length. Returns '' if nothing
|
|
196
|
+
* usable remains (so the caller keeps the provisional title).
|
|
197
|
+
*/
|
|
198
|
+
export function sanitizeTitle(raw: string, max = 60): string {
|
|
199
|
+
let s = raw.trim();
|
|
200
|
+
// Take the first line only — models sometimes add a comment after.
|
|
201
|
+
s = s.split('\n')[0].trim();
|
|
202
|
+
// Strip surrounding quotes, backticks, and markdown emphasis.
|
|
203
|
+
s = s.replace(/^["'`*_]+|["'`*_]+$/g, '').trim();
|
|
204
|
+
s = s.replace(/^(title|session)\s*[:—-]\s*/i, '').trim();
|
|
205
|
+
s = s.replace(/\s+/g, ' ');
|
|
206
|
+
s = s.replace(/[.]+$/, '');
|
|
207
|
+
if (s.length === 0) return '';
|
|
208
|
+
if (s.length > max) {
|
|
209
|
+
const cut = s.lastIndexOf(' ', max - 3);
|
|
210
|
+
s = (cut > 10 ? s.substring(0, cut) : s.substring(0, max - 3)) + '…';
|
|
211
|
+
}
|
|
212
|
+
return s;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Format a message timestamp for the message-box border.
|
|
217
|
+
*
|
|
218
|
+
* Timestamps are stored as epoch milliseconds (absolute, timezone-free) and
|
|
219
|
+
* rendered here in the viewer's local timezone. Detail escalates with age:
|
|
220
|
+
*
|
|
221
|
+
* today → `Mon 14:30`
|
|
222
|
+
* this year → `Mon 15 Jan 14:30`
|
|
223
|
+
* older → `Mon 15 Jan 2025 14:30`
|
|
224
|
+
*/
|
|
225
|
+
export function formatMessageTimestamp(epochMs: number, now = Date.now()): string {
|
|
226
|
+
if (!epochMs) return '';
|
|
227
|
+
const d = new Date(epochMs);
|
|
228
|
+
const ref = new Date(now);
|
|
229
|
+
|
|
230
|
+
const weekday = d.toLocaleDateString(undefined, {weekday: 'short'}); // Mon
|
|
231
|
+
const year = d.toLocaleDateString(undefined, {year: 'numeric'});
|
|
232
|
+
const month = d.toLocaleDateString(undefined, {month: 'short'}); // Jan
|
|
233
|
+
const day = d.getDate();
|
|
234
|
+
const hh = String(d.getHours()).padStart(2, '0');
|
|
235
|
+
const mm = String(d.getMinutes()).padStart(2, '0');
|
|
236
|
+
const time = `${hh}:${mm}`;
|
|
237
|
+
|
|
238
|
+
const sameDay =
|
|
239
|
+
d.getFullYear() === ref.getFullYear() &&
|
|
240
|
+
d.getMonth() === ref.getMonth() &&
|
|
241
|
+
d.getDate() === ref.getDate();
|
|
242
|
+
if (sameDay) return `${weekday} ${time}`;
|
|
243
|
+
|
|
244
|
+
const sameYear = d.getFullYear() === ref.getFullYear();
|
|
245
|
+
if (sameYear) return `${weekday} ${day} ${month} ${time}`;
|
|
246
|
+
|
|
247
|
+
return `${weekday} ${day} ${month} ${year} ${time}`;
|
|
248
|
+
}
|