pal-code 0.0.16 → 0.0.18
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 +9 -6
- package/SYSTEM.md +17 -2
- package/package.json +10 -5
- package/packages/cli/src/agent-panel.ts +1 -1
- package/packages/cli/src/agent-ui.ts +66 -0
- package/packages/cli/src/commands/ext-registry.ts +7 -0
- package/packages/cli/src/commands.ts +32 -102
- package/packages/cli/src/components/assistant-body.ts +4 -3
- package/packages/cli/src/components/message-box.ts +2 -2
- package/packages/cli/src/components/permission-prompt.ts +3 -3
- package/packages/cli/src/components/render-history.ts +13 -12
- package/packages/cli/src/components/run-view.ts +109 -33
- package/packages/cli/src/components/sidebar.ts +81 -14
- package/packages/cli/src/components/status-line.ts +3 -3
- package/packages/cli/src/config.ts +6 -129
- package/packages/cli/src/index.ts +194 -69
- package/packages/cli/src/overlays/key-input.ts +142 -121
- package/packages/cli/src/overlays/model-picker.ts +19 -56
- package/packages/cli/src/overlays/select.ts +102 -0
- package/packages/cli/src/overlays/session-picker.ts +14 -40
- package/packages/cli/src/overlays/wizard.ts +107 -42
- package/packages/cli/src/prompt/select.ts +181 -0
- package/packages/cli/src/slash-commands.ts +105 -0
- package/packages/cli/src/state.ts +32 -74
- package/packages/cli/src/theme.ts +13 -1
- package/packages/cli/src/themes/github-dark.ts +14 -3
- package/packages/cli/src/themes/types.ts +26 -2
- package/packages/cli/src/themes/vanilla-pi.ts +23 -9
- package/packages/cli/src/tui/components/text.ts +102 -0
- package/packages/cli/src/tui/render/code-block.ts +325 -0
- package/packages/cli/src/{utils → tui/render}/format.ts +31 -24
- package/packages/cli/src/{utils → tui/render}/highlight.ts +94 -2
- package/packages/cli/src/tui/render/markdown.ts +529 -0
- package/packages/cli/src/tui/surface/index.ts +9 -0
- package/packages/cli/src/tui/{paint-panel.ts → surface/paint-panel.ts} +1 -1
- package/packages/cli/src/tui/theme-types.ts +59 -0
- package/packages/cli/src/tui/tty-width.ts +20 -0
- package/packages/cli/src/tui/types.ts +53 -0
- package/packages/cli/src/turn.ts +0 -292
- package/packages/cli/src/utils/code-block.ts +0 -200
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
/** `/key` overlay — provider picker then paste a key, hot-reload. */
|
|
2
2
|
|
|
3
|
-
import {Text
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
3
|
+
import {Text} from '../tui/components/text.js';
|
|
4
|
+
import {Input} from '@earendil-works/pi-tui';
|
|
5
|
+
import type {TUI, Container, Editor, SelectItem} from '@earendil-works/pi-tui';
|
|
6
|
+
import {
|
|
7
|
+
PROVIDERS,
|
|
8
|
+
detectProvider,
|
|
9
|
+
getProvider,
|
|
10
|
+
resolveAllKeys,
|
|
11
|
+
saveKey,
|
|
12
|
+
saveSetting,
|
|
13
|
+
} from '@pal-code/core';
|
|
6
14
|
import type {Config} from '../config.js';
|
|
7
15
|
|
|
8
16
|
import {c} from '../theme.js';
|
|
9
17
|
import {state} from '../state.js';
|
|
10
18
|
import {providerHint} from './wizard.js';
|
|
19
|
+
import {showSelectOverlay} from './select.js';
|
|
20
|
+
import type {SelectContext} from './select.js';
|
|
21
|
+
|
|
22
|
+
const PASTE_KEY = '__paste__';
|
|
11
23
|
|
|
12
24
|
export function showKeyInput(
|
|
13
25
|
config: Config,
|
|
@@ -16,94 +28,65 @@ export function showKeyInput(
|
|
|
16
28
|
editorRow: Container,
|
|
17
29
|
tui: TUI,
|
|
18
30
|
): void {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
const configured = resolveAllKeys();
|
|
32
|
+
const current = config.model.includes('/') ? getProvider(config.model.split('/')[0]) : null;
|
|
33
|
+
|
|
34
|
+
const items: SelectItem[] = [
|
|
35
|
+
...PROVIDERS.map((p) => ({
|
|
36
|
+
value: p.id,
|
|
37
|
+
label: p.name,
|
|
38
|
+
description: [providerHint(p), p.keyOptional && !configured[p.id] ? 'free-ok' : '']
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.join(' · '),
|
|
41
|
+
})),
|
|
42
|
+
{value: PASTE_KEY, label: 'Paste an API key…', description: 'Auto-detect the provider'},
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
showSelectOverlay(
|
|
46
|
+
{
|
|
47
|
+
title: 'Add or change API key',
|
|
48
|
+
items,
|
|
49
|
+
selectedValue: current?.id,
|
|
50
|
+
noMatch: 'No matching providers.',
|
|
51
|
+
onSelect: (item) => {
|
|
52
|
+
if (item.value === PASTE_KEY) {
|
|
53
|
+
showPastePrompt(container, editor, editorRow, tui);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const provider = getProvider(item.value);
|
|
57
|
+
if (!provider) return;
|
|
58
|
+
if (provider.keyOptional && !resolveAllKeys()[provider.id]) {
|
|
59
|
+
enableKeyless(provider.id, provider.name, provider.defaultModel, container, tui);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
showKeyPrompt(provider.id, provider.name, container, editor, editorRow, tui);
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
{container, editor, editorRow, tui},
|
|
34
66
|
);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const input = new Input({
|
|
38
|
-
prompt: `\n ${c.bold}Provider number or paste API key:${c.reset} `,
|
|
39
|
-
placeholder: 'e.g. 2 or paste sk-...',
|
|
40
|
-
});
|
|
41
|
-
container.addChild(input);
|
|
42
|
-
tui.setFocus(input);
|
|
43
|
-
tui.requestRender();
|
|
44
|
-
|
|
45
|
-
input.onSubmit = (value: string) => {
|
|
46
|
-
const answer = value.trim();
|
|
47
|
-
if (!answer) {
|
|
48
|
-
cleanup();
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Check if it's a provider number
|
|
53
|
-
const asNum = Number(answer);
|
|
54
|
-
if (Number.isInteger(asNum) && asNum >= 1 && asNum <= PROVIDERS.length) {
|
|
55
|
-
const provider = PROVIDERS[asNum - 1];
|
|
56
|
-
if (provider.keyOptional) {
|
|
57
|
-
// Keyless — save directly
|
|
58
|
-
saveSetting('keyless', provider.id);
|
|
59
|
-
saveSetting('model', `${provider.id}/${provider.defaultModel}`);
|
|
60
|
-
if (state.reloadKeys) state.reloadKeys();
|
|
61
|
-
|
|
62
|
-
const savedMsg = new Text(
|
|
63
|
-
`\n${c.green}✓ Keyless ${provider.name} mode enabled${c.reset}\n${c.green}✓ Default model:${c.reset} ${provider.id}/${provider.defaultModel}\n`,
|
|
64
|
-
1,
|
|
65
|
-
0,
|
|
66
|
-
);
|
|
67
|
-
container.addChild(savedMsg);
|
|
68
|
-
cleanup();
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Needs a key — show key input for this provider
|
|
73
|
-
cleanup();
|
|
74
|
-
showKeyPrompt(provider.id, provider.name, container, editor, editorRow, tui);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
67
|
+
}
|
|
77
68
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
69
|
+
function enableKeyless(
|
|
70
|
+
providerId: string,
|
|
71
|
+
providerName: string,
|
|
72
|
+
defaultModel: string,
|
|
73
|
+
container: Container,
|
|
74
|
+
tui: TUI,
|
|
75
|
+
): void {
|
|
76
|
+
saveSetting('keyless', providerId);
|
|
77
|
+
saveSetting('model', `${providerId}/${defaultModel}`);
|
|
78
|
+
if (state.reloadKeys) state.reloadKeys();
|
|
83
79
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
`\n${c.green}✓ ${providerName}
|
|
80
|
+
container.addChild(
|
|
81
|
+
new Text(
|
|
82
|
+
`\n${c.green}✓ Keyless ${providerName} mode enabled${c.reset}\n${c.green}✓ Default model:${c.reset} ${providerId}/${defaultModel}\n`,
|
|
87
83
|
1,
|
|
88
84
|
0,
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
input.onEscape = () => cleanup();
|
|
95
|
-
|
|
96
|
-
function cleanup() {
|
|
97
|
-
container.removeChild(header);
|
|
98
|
-
container.removeChild(input);
|
|
99
|
-
editorRow.addChild(editor);
|
|
100
|
-
tui.setFocus(editor);
|
|
101
|
-
state.overlayMode = 'none';
|
|
102
|
-
tui.requestRender();
|
|
103
|
-
}
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
tui.requestRender();
|
|
104
88
|
}
|
|
105
89
|
|
|
106
|
-
/** Second step: prompt for the actual API key for a specific provider. */
|
|
107
90
|
function showKeyPrompt(
|
|
108
91
|
providerId: string,
|
|
109
92
|
providerName: string,
|
|
@@ -112,42 +95,79 @@ function showKeyPrompt(
|
|
|
112
95
|
editorRow: Container,
|
|
113
96
|
tui: TUI,
|
|
114
97
|
): void {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
98
|
+
openInput(
|
|
99
|
+
{
|
|
100
|
+
title: `Paste your ${providerName} API key:`,
|
|
101
|
+
prompt: ` ${c.cyan}API Key:${c.reset} `,
|
|
102
|
+
placeholder: `paste your ${providerName} API key...`,
|
|
103
|
+
onSubmit: (key, close) => {
|
|
104
|
+
saveKey(providerId, key);
|
|
105
|
+
if (state.reloadKeys) state.reloadKeys();
|
|
106
|
+
container.addChild(
|
|
107
|
+
new Text(`\n${c.green}✓ ${providerName} key saved — active now${c.reset}\n`, 1, 0),
|
|
108
|
+
);
|
|
109
|
+
close();
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{container, editor, editorRow, tui},
|
|
119
113
|
);
|
|
120
|
-
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function showPastePrompt(
|
|
117
|
+
container: Container,
|
|
118
|
+
editor: Editor,
|
|
119
|
+
editorRow: Container,
|
|
120
|
+
tui: TUI,
|
|
121
|
+
): void {
|
|
122
|
+
openInput(
|
|
123
|
+
{
|
|
124
|
+
title: 'Paste an API key:',
|
|
125
|
+
prompt: ` ${c.cyan}API Key:${c.reset} `,
|
|
126
|
+
placeholder: 'paste any provider API key...',
|
|
127
|
+
onSubmit: (key, close) => {
|
|
128
|
+
const detected = detectProvider(key);
|
|
129
|
+
if (!detected) {
|
|
130
|
+
container.addChild(
|
|
131
|
+
new Text(
|
|
132
|
+
`\n${c.red}Could not detect a provider for that key.${c.reset}\n${c.dim}Pick the provider from the list instead.${c.reset}\n`,
|
|
133
|
+
1,
|
|
134
|
+
0,
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
} else {
|
|
138
|
+
saveKey(detected.id, key);
|
|
139
|
+
if (state.reloadKeys) state.reloadKeys();
|
|
140
|
+
container.addChild(
|
|
141
|
+
new Text(`\n${c.green}✓ ${detected.name} key saved — active now${c.reset}\n`, 1, 0),
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
close();
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
{container, editor, editorRow, tui},
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface InputOverlayOptions {
|
|
152
|
+
title: string;
|
|
153
|
+
prompt: string;
|
|
154
|
+
placeholder: string;
|
|
155
|
+
onSubmit: (value: string, close: () => void) => void;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function openInput(opts: InputOverlayOptions, ctx: SelectContext): void {
|
|
159
|
+
const {container, editor, editorRow, tui} = ctx;
|
|
160
|
+
state.overlayMode = 'key-input';
|
|
161
|
+
editorRow.removeChild(editor);
|
|
121
162
|
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
});
|
|
163
|
+
const label = new Text(`\n ${c.bold}${opts.title}${c.reset}\n`, 1, 0);
|
|
164
|
+
const input = new Input({prompt: opts.prompt, placeholder: opts.placeholder});
|
|
165
|
+
container.addChild(label);
|
|
126
166
|
container.addChild(input);
|
|
127
167
|
tui.setFocus(input);
|
|
128
168
|
tui.requestRender();
|
|
129
169
|
|
|
130
|
-
|
|
131
|
-
const key = value.trim();
|
|
132
|
-
if (!key) {
|
|
133
|
-
container.removeChild(label);
|
|
134
|
-
container.removeChild(input);
|
|
135
|
-
editorRow.addChild(editor);
|
|
136
|
-
tui.setFocus(editor);
|
|
137
|
-
state.overlayMode = 'none';
|
|
138
|
-
tui.requestRender();
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
saveKey(providerId, key);
|
|
143
|
-
if (state.reloadKeys) state.reloadKeys();
|
|
144
|
-
|
|
145
|
-
const savedMsg = new Text(
|
|
146
|
-
`\n${c.green}✓ ${providerName} key saved — active now${c.reset}\n`,
|
|
147
|
-
1,
|
|
148
|
-
0,
|
|
149
|
-
);
|
|
150
|
-
container.addChild(savedMsg);
|
|
170
|
+
const close = (): void => {
|
|
151
171
|
container.removeChild(label);
|
|
152
172
|
container.removeChild(input);
|
|
153
173
|
editorRow.addChild(editor);
|
|
@@ -156,12 +176,13 @@ function showKeyPrompt(
|
|
|
156
176
|
tui.requestRender();
|
|
157
177
|
};
|
|
158
178
|
|
|
159
|
-
input.
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
179
|
+
input.onSubmit = (value: string) => {
|
|
180
|
+
const trimmed = value.trim();
|
|
181
|
+
if (!trimmed) {
|
|
182
|
+
close();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
opts.onSubmit(trimmed, close);
|
|
166
186
|
};
|
|
187
|
+
input.onEscape = close;
|
|
167
188
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/** Model picker — lists exactly what each configured provider reports. */
|
|
2
2
|
|
|
3
|
-
import {Text, SelectList} from '@earendil-works/pi-tui';
|
|
4
3
|
import type {TUI, Container, Editor, SelectItem} from '@earendil-works/pi-tui';
|
|
5
4
|
import {PROVIDERS, resolveModelIds, saveSetting} from '@pal-code/core';
|
|
6
5
|
import type {Provider, ProviderKeys} from '@pal-code/core';
|
|
7
6
|
|
|
8
|
-
import {c, surface} from '../theme.js';
|
|
9
7
|
import {state} from '../state.js';
|
|
8
|
+
import {showSelectOverlay} from './select.js';
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* Build the /model picker from each provider's live /models response.
|
|
@@ -40,7 +39,7 @@ export async function buildModelOptions(allKeys: ProviderKeys): Promise<SelectIt
|
|
|
40
39
|
* Swap the editor for an inline model list. Selecting persists the choice and
|
|
41
40
|
* rebuilds the live provider; the caller's `providerRef` is updated in place.
|
|
42
41
|
*/
|
|
43
|
-
export
|
|
42
|
+
export function showModelSelector(
|
|
44
43
|
config: {model: string},
|
|
45
44
|
container: Container,
|
|
46
45
|
editor: Editor,
|
|
@@ -49,58 +48,22 @@ export async function showModelSelector(
|
|
|
49
48
|
providerRef: {current: Provider},
|
|
50
49
|
createProviderFn: (model: string) => Provider,
|
|
51
50
|
allKeys: ProviderKeys,
|
|
52
|
-
):
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
scrollInfo: (text: string) => `${surface.overlay.scrollInfo}${text}${c.reset}`,
|
|
70
|
-
noMatch: (text: string) => `${surface.overlay.noMatch}${text}${c.reset}`,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Highlight the current model
|
|
74
|
-
const currentIdx = options.findIndex((m) => m.value === config.model);
|
|
75
|
-
if (currentIdx >= 0) selectList.setSelectedIndex(currentIdx);
|
|
76
|
-
|
|
77
|
-
// Swap the placeholder for the real prompt + list
|
|
78
|
-
label.setText(
|
|
79
|
-
`\n${c.bold}Select model:${c.reset} ${c.dim}(type to filter, ↑↓ navigate, Enter select, Esc cancel)${c.reset}\n`,
|
|
51
|
+
): void {
|
|
52
|
+
showSelectOverlay(
|
|
53
|
+
{
|
|
54
|
+
title: 'Select model',
|
|
55
|
+
items: buildModelOptions(allKeys),
|
|
56
|
+
loadingText: 'loading…',
|
|
57
|
+
selectedValue: config.model,
|
|
58
|
+
noMatch: 'No models available.',
|
|
59
|
+
onSelect: (item) => {
|
|
60
|
+
config.model = item.value;
|
|
61
|
+
saveSetting('model', item.value);
|
|
62
|
+
providerRef.current = createProviderFn(item.value);
|
|
63
|
+
providerRef.current.init();
|
|
64
|
+
state.refreshStatusLine();
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{container, editor, editorRow, tui},
|
|
80
68
|
);
|
|
81
|
-
container.addChild(selectList);
|
|
82
|
-
tui.setFocus(selectList);
|
|
83
|
-
tui.requestRender();
|
|
84
|
-
|
|
85
|
-
const close = (): void => {
|
|
86
|
-
container.removeChild(label);
|
|
87
|
-
container.removeChild(selectList);
|
|
88
|
-
editorRow.addChild(editor);
|
|
89
|
-
tui.setFocus(editor);
|
|
90
|
-
state.overlayMode = 'none';
|
|
91
|
-
tui.requestRender();
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
selectList.onSelect = (item) => {
|
|
95
|
-
config.model = item.value;
|
|
96
|
-
saveSetting('model', item.value);
|
|
97
|
-
// Recreate the provider for the new model
|
|
98
|
-
providerRef.current = createProviderFn(item.value);
|
|
99
|
-
providerRef.current.init();
|
|
100
|
-
close();
|
|
101
|
-
// Reflect the new model name in the status bar immediately.
|
|
102
|
-
state.refreshStatusLine();
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
selectList.onCancel = close;
|
|
106
69
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/** Standard filterable list overlay shared by every "pick one" flow. */
|
|
2
|
+
|
|
3
|
+
import {SelectList} from '@earendil-works/pi-tui';
|
|
4
|
+
import type {TUI, Container, Editor, SelectItem} from '@earendil-works/pi-tui';
|
|
5
|
+
|
|
6
|
+
import {Text} from '../tui/components/text.js';
|
|
7
|
+
import {c, surface} from '../theme.js';
|
|
8
|
+
import {state} from '../state.js';
|
|
9
|
+
|
|
10
|
+
export interface SelectContext {
|
|
11
|
+
container: Container;
|
|
12
|
+
editor: Editor;
|
|
13
|
+
editorRow: Container;
|
|
14
|
+
tui: TUI;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SelectOverlayOptions {
|
|
18
|
+
title: string;
|
|
19
|
+
items: SelectItem[] | Promise<SelectItem[]>;
|
|
20
|
+
hint?: string;
|
|
21
|
+
height?: number;
|
|
22
|
+
selectedValue?: string;
|
|
23
|
+
noMatch?: string;
|
|
24
|
+
loadingText?: string;
|
|
25
|
+
onSelect: (item: SelectItem) => void;
|
|
26
|
+
onCancel?: () => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const DEFAULT_HINT = 'type to filter, ↑↓ navigate, Enter select, Esc cancel';
|
|
30
|
+
|
|
31
|
+
export function showSelectOverlay(opts: SelectOverlayOptions, ctx: SelectContext): void {
|
|
32
|
+
const {container, editor, editorRow, tui} = ctx;
|
|
33
|
+
state.overlayMode = 'select';
|
|
34
|
+
editorRow.removeChild(editor);
|
|
35
|
+
|
|
36
|
+
const header = (text: string): string =>
|
|
37
|
+
`\n${c.bold}${opts.title}:${c.reset} ${c.dim}${text}${c.reset}\n`;
|
|
38
|
+
const pending = typeof (opts.items as {then?: unknown}).then === 'function';
|
|
39
|
+
|
|
40
|
+
const label = new Text(
|
|
41
|
+
header(pending ? (opts.loadingText ?? 'loading…') : (opts.hint ?? DEFAULT_HINT)),
|
|
42
|
+
1,
|
|
43
|
+
0,
|
|
44
|
+
);
|
|
45
|
+
container.addChild(label);
|
|
46
|
+
tui.requestRender();
|
|
47
|
+
|
|
48
|
+
const restore = (): void => {
|
|
49
|
+
editorRow.addChild(editor);
|
|
50
|
+
tui.setFocus(editor);
|
|
51
|
+
state.overlayMode = 'none';
|
|
52
|
+
tui.requestRender();
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const render = (items: SelectItem[]): void => {
|
|
56
|
+
if (items.length === 0) {
|
|
57
|
+
label.setText(header(opts.noMatch ?? 'Nothing to select.'));
|
|
58
|
+
restore();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
label.setText(header(opts.hint ?? DEFAULT_HINT));
|
|
63
|
+
|
|
64
|
+
const list = new SelectList(items, opts.height ?? 14, {
|
|
65
|
+
selectedPrefix: (_text: string) => `${surface.overlay.selectedPrefix}> ${c.reset}`,
|
|
66
|
+
selectedText: (text: string) => `${surface.overlay.selectedText}${text}${c.reset}`,
|
|
67
|
+
description: (text: string) => ` ${surface.overlay.description}${text}${c.reset}`,
|
|
68
|
+
scrollInfo: (text: string) => `${surface.overlay.scrollInfo}${text}${c.reset}`,
|
|
69
|
+
noMatch: (text: string) => `${surface.overlay.noMatch}${text}${c.reset}`,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (opts.selectedValue) {
|
|
73
|
+
const index = items.findIndex((item) => item.value === opts.selectedValue);
|
|
74
|
+
if (index >= 0) list.setSelectedIndex(index);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
container.addChild(list);
|
|
78
|
+
tui.setFocus(list);
|
|
79
|
+
tui.requestRender();
|
|
80
|
+
|
|
81
|
+
const close = (): void => {
|
|
82
|
+
container.removeChild(label);
|
|
83
|
+
container.removeChild(list);
|
|
84
|
+
restore();
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
list.onSelect = (item) => {
|
|
88
|
+
close();
|
|
89
|
+
opts.onSelect(item);
|
|
90
|
+
};
|
|
91
|
+
list.onCancel = () => {
|
|
92
|
+
close();
|
|
93
|
+
opts.onCancel?.();
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
if (pending) {
|
|
98
|
+
(opts.items as Promise<SelectItem[]>).then(render).catch(() => render([]));
|
|
99
|
+
} else {
|
|
100
|
+
render(opts.items as SelectItem[]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -5,19 +5,20 @@
|
|
|
5
5
|
* explicit id or unambiguous prefix.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {Text
|
|
8
|
+
import {Text} from '../tui/components/text.js';
|
|
9
9
|
import type {TUI, Container, Editor, SelectItem} from '@earendil-works/pi-tui';
|
|
10
10
|
|
|
11
|
-
import {c
|
|
12
|
-
import {truncate} from '../
|
|
11
|
+
import {c} from '../theme.js';
|
|
12
|
+
import {truncate} from '../tui/render/format.js';
|
|
13
13
|
import type {ConversationMessage as Message, Session, SessionStore} from '@pal-code/core';
|
|
14
|
+
import {showSelectOverlay} from './select.js';
|
|
14
15
|
|
|
15
16
|
export function groupByDate(sessions: readonly Session[]): Map<string, Session[]> {
|
|
16
17
|
const now = new Date();
|
|
17
18
|
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
|
|
18
19
|
const yesterday = today - 86_400_000;
|
|
19
20
|
const weekAgo = today - 7 * 86_400_000;
|
|
20
|
-
const groups = new Map<string,
|
|
21
|
+
const groups = new Map<string, Session[]>();
|
|
21
22
|
for (const s of sessions) {
|
|
22
23
|
const ts = s.createdAt ?? s.updatedAt ?? 0;
|
|
23
24
|
let label: string;
|
|
@@ -71,43 +72,16 @@ export function showSessionSelector(
|
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
`\n${c.bold}Select session:${c.reset} ${c.dim}(type to filter, ↑↓ navigate, Enter select, Esc cancel)${c.reset}\n`,
|
|
84
|
-
1,
|
|
85
|
-
0,
|
|
75
|
+
showSelectOverlay(
|
|
76
|
+
{
|
|
77
|
+
title: 'Select session',
|
|
78
|
+
items,
|
|
79
|
+
height: 12,
|
|
80
|
+
noMatch: ' No matching sessions.',
|
|
81
|
+
onSelect: (item) => onSessionSelected(item.value),
|
|
82
|
+
},
|
|
83
|
+
{container, editor, editorRow, tui},
|
|
86
84
|
);
|
|
87
|
-
container.addChild(label);
|
|
88
|
-
container.addChild(selectList);
|
|
89
|
-
tui.setFocus(selectList);
|
|
90
|
-
tui.requestRender();
|
|
91
|
-
|
|
92
|
-
selectList.onSelect = (item) => {
|
|
93
|
-
container.removeChild(label);
|
|
94
|
-
container.removeChild(selectList);
|
|
95
|
-
editorRow.addChild(editor);
|
|
96
|
-
tui.setFocus(editor);
|
|
97
|
-
onSessionSelected(item.value);
|
|
98
|
-
tui.requestRender();
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
selectList.onCancel = () => {
|
|
102
|
-
container.removeChild(label);
|
|
103
|
-
container.removeChild(selectList);
|
|
104
|
-
editorRow.addChild(editor);
|
|
105
|
-
tui.setFocus(editor);
|
|
106
|
-
tui.requestRender();
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
// Remove editor from view while selecting
|
|
110
|
-
editorRow.removeChild(editor);
|
|
111
85
|
}
|
|
112
86
|
|
|
113
87
|
/**
|