pal-code 0.0.17 → 0.0.19
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-ui.ts +66 -0
- package/packages/cli/src/commands/ext-registry.ts +19 -0
- package/packages/cli/src/commands/ext.ts +82 -4
- package/packages/cli/src/commands.ts +31 -101
- package/packages/cli/src/components/assistant-body.ts +2 -1
- package/packages/cli/src/components/message-box.ts +1 -1
- package/packages/cli/src/components/sidebar.ts +8 -2
- package/packages/cli/src/config.ts +6 -129
- package/packages/cli/src/index.ts +160 -47
- package/packages/cli/src/overlays/key-input.ts +140 -120
- package/packages/cli/src/overlays/model-picker.ts +19 -57
- package/packages/cli/src/overlays/select.ts +102 -0
- package/packages/cli/src/overlays/session-picker.ts +12 -39
- 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 +20 -74
- package/packages/cli/src/tui/render/markdown.ts +529 -0
- package/packages/cli/src/turn.ts +0 -298
|
@@ -49,6 +49,7 @@ import {
|
|
|
49
49
|
} from './state.js';
|
|
50
50
|
import {Sidebar} from './components/sidebar.js';
|
|
51
51
|
import {cmdHelp, cmdCost} from './commands.js';
|
|
52
|
+
import {autocompleteItems} from './slash-commands.js';
|
|
52
53
|
import {
|
|
53
54
|
loadUpdateCache,
|
|
54
55
|
planStartupNotice,
|
|
@@ -83,6 +84,10 @@ import {
|
|
|
83
84
|
PROVIDERS,
|
|
84
85
|
loadCustomProviders,
|
|
85
86
|
providerForModel,
|
|
87
|
+
saveKey,
|
|
88
|
+
getProvider,
|
|
89
|
+
detectProvider,
|
|
90
|
+
clearModelCache,
|
|
86
91
|
clearTodos,
|
|
87
92
|
resetShell,
|
|
88
93
|
compactionBoundary,
|
|
@@ -118,7 +123,7 @@ import type {SessionStore as SessionStoreType} from '@pal-code/core';
|
|
|
118
123
|
// (see buildModelOptions). Static curated lists live in @pal-code/core PROVIDERS as fallback.
|
|
119
124
|
|
|
120
125
|
// Version synced from version.json via `node scripts/sync-version.js`.
|
|
121
|
-
const VERSION = '0.0.
|
|
126
|
+
const VERSION = '0.0.19';
|
|
122
127
|
|
|
123
128
|
// ── Canvas sync (OSC 10/11) ──
|
|
124
129
|
// Raw reply payloads captured after a successful startup query + set, or null
|
|
@@ -141,9 +146,16 @@ function restoreCanvas(): void {
|
|
|
141
146
|
}
|
|
142
147
|
|
|
143
148
|
import {showModelSelector} from './overlays/model-picker.js';
|
|
149
|
+
import {showSelectOverlay} from './overlays/select.js';
|
|
144
150
|
import {showKeyInput} from './overlays/key-input.js';
|
|
145
151
|
import {runSetupWizard} from './overlays/wizard.js';
|
|
146
|
-
import {
|
|
152
|
+
import {
|
|
153
|
+
ensureExtensionsLoaded,
|
|
154
|
+
isReadOnlyPrimary,
|
|
155
|
+
reloadExtensions,
|
|
156
|
+
runTurn as runAgentTurn,
|
|
157
|
+
} from '@pal-code/core';
|
|
158
|
+
import {approveTool, createTuiAgentUi} from './agent-ui.js';
|
|
147
159
|
|
|
148
160
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
149
161
|
// Main
|
|
@@ -157,6 +169,15 @@ async function main() {
|
|
|
157
169
|
process.exit(0);
|
|
158
170
|
}
|
|
159
171
|
|
|
172
|
+
// The desktop/web frontend is not part of the shipped build: `packages/ui` is
|
|
173
|
+
// private and `dist/` is excluded from the published package, so importing it
|
|
174
|
+
// from an installed copy cannot resolve. Report that in one line rather than
|
|
175
|
+
// surfacing a module-resolution stack trace.
|
|
176
|
+
if (flag === 'ui') {
|
|
177
|
+
console.error('pal ui is not available in this build.');
|
|
178
|
+
process.exit(1);
|
|
179
|
+
}
|
|
180
|
+
|
|
160
181
|
// Anchor to the project root (where .pal.json lives) so that process.cwd()
|
|
161
182
|
// reflects the user's project, not wherever the CLI package happens to be.
|
|
162
183
|
const root = resolveProjectRoot();
|
|
@@ -348,6 +369,8 @@ async function main() {
|
|
|
348
369
|
editorRow.addChild(roleIndicator); // hidden initially (empty text)
|
|
349
370
|
editorRow.addChild(editor);
|
|
350
371
|
|
|
372
|
+
const agentUi = createTuiAgentUi(tui, messagesContainer);
|
|
373
|
+
|
|
351
374
|
const sidebar = new Sidebar();
|
|
352
375
|
// Fixed sidebar width used by both the overlay compositor and the transcript
|
|
353
376
|
// layout. Both must agree — if the layout reserves N columns but the overlay
|
|
@@ -772,24 +795,7 @@ async function main() {
|
|
|
772
795
|
// ── Autocomplete ──
|
|
773
796
|
// ── Autocomplete ──
|
|
774
797
|
editor.setAutocompleteProvider(
|
|
775
|
-
new CombinedAutocompleteProvider(
|
|
776
|
-
[
|
|
777
|
-
{name: 'help', description: 'Show available commands'},
|
|
778
|
-
{name: 'exit', description: 'Save session and quit'},
|
|
779
|
-
{name: 'clear', description: 'Reset conversation'},
|
|
780
|
-
{name: 'new', description: 'Start a new session'},
|
|
781
|
-
{name: 'sessions', description: 'List recent sessions'},
|
|
782
|
-
{name: 'resume', description: 'Load a previous session'},
|
|
783
|
-
{name: 'cost', description: 'Show token usage and cost'},
|
|
784
|
-
{name: 'model', description: 'Switch model'},
|
|
785
|
-
{name: 'theme', description: 'Switch color theme (vanilla-pi, github-dark)'},
|
|
786
|
-
{name: 'ext', description: 'Manage extensions'},
|
|
787
|
-
{name: 'export', description: 'Export config to a versionable JSON file'},
|
|
788
|
-
{name: 'import', description: 'Import config from an export file'},
|
|
789
|
-
{name: 'roles', description: 'List agent roles'},
|
|
790
|
-
],
|
|
791
|
-
process.cwd(),
|
|
792
|
-
),
|
|
798
|
+
new CombinedAutocompleteProvider(autocompleteItems(isDebugEnabled()), process.cwd()),
|
|
793
799
|
);
|
|
794
800
|
|
|
795
801
|
// ── Focus editor ──
|
|
@@ -892,6 +898,7 @@ async function main() {
|
|
|
892
898
|
// Re-read keys + rebuild the live provider after `/key` saves one, so it
|
|
893
899
|
// takes effect without a restart.
|
|
894
900
|
state.reloadKeys = () => {
|
|
901
|
+
clearModelCache();
|
|
895
902
|
allKeys = resolveAllKeys();
|
|
896
903
|
const {provider: modelDef} = providerForModel(config.model, Object.keys(allKeys));
|
|
897
904
|
if (!providerAccessible(modelDef, allKeys)) {
|
|
@@ -1163,6 +1170,10 @@ async function main() {
|
|
|
1163
1170
|
// Restore the editor
|
|
1164
1171
|
editorRow.addChild(editor);
|
|
1165
1172
|
tui.setFocus(editor);
|
|
1173
|
+
// Repaint the status line: the resolver already cleared
|
|
1174
|
+
// overlayMode/pendingPermission, but `requestRender` alone would leave
|
|
1175
|
+
// StatusLine's cached "🔒 Awaiting permission..." text on screen.
|
|
1176
|
+
state.refreshStatusLine();
|
|
1166
1177
|
tui.requestRender();
|
|
1167
1178
|
};
|
|
1168
1179
|
|
|
@@ -1353,7 +1364,7 @@ async function main() {
|
|
|
1353
1364
|
// mode AND runs that task in the same go; `/execute` returns to the
|
|
1354
1365
|
// orchestrator to implement what was planned.
|
|
1355
1366
|
if (trimmed === '/plan' || trimmed.startsWith('/plan ')) {
|
|
1356
|
-
await ensureExtensionsLoaded(config.extensions);
|
|
1367
|
+
await ensureExtensionsLoaded(config.extensions, agentUi, state.currentAgentRole);
|
|
1357
1368
|
const arg = trimmed.slice('/plan'.length).trim();
|
|
1358
1369
|
|
|
1359
1370
|
if (arg === 'off') {
|
|
@@ -1386,7 +1397,7 @@ async function main() {
|
|
|
1386
1397
|
}
|
|
1387
1398
|
}
|
|
1388
1399
|
if (trimmed === '/execute' || trimmed.startsWith('/execute ')) {
|
|
1389
|
-
await ensureExtensionsLoaded(config.extensions);
|
|
1400
|
+
await ensureExtensionsLoaded(config.extensions, agentUi, state.currentAgentRole);
|
|
1390
1401
|
const arg = trimmed.slice('/execute'.length).trim();
|
|
1391
1402
|
switchPrimary(defaultPrimaryName());
|
|
1392
1403
|
if (arg) {
|
|
@@ -1418,8 +1429,11 @@ async function main() {
|
|
|
1418
1429
|
if (trimmed === '/reload') {
|
|
1419
1430
|
// Re-import extensions (cache-bust via timestamp query param).
|
|
1420
1431
|
// Useful during extension development — no full restart needed.
|
|
1421
|
-
const
|
|
1422
|
-
|
|
1432
|
+
const toolCount = await reloadExtensions(
|
|
1433
|
+
config.extensions,
|
|
1434
|
+
agentUi,
|
|
1435
|
+
state.currentAgentRole,
|
|
1436
|
+
);
|
|
1423
1437
|
messagesContainer.addChild(
|
|
1424
1438
|
new Text(`${c.green} Extensions reloaded (${toolCount} tools)${c.reset}`, 1, 0),
|
|
1425
1439
|
);
|
|
@@ -1635,20 +1649,31 @@ async function main() {
|
|
|
1635
1649
|
}
|
|
1636
1650
|
if (trimmed === '/theme') {
|
|
1637
1651
|
const active = activeTheme();
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
:
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1652
|
+
showSelectOverlay(
|
|
1653
|
+
{
|
|
1654
|
+
title: 'Select theme',
|
|
1655
|
+
items: listThemes().map((name) => ({
|
|
1656
|
+
value: name,
|
|
1657
|
+
label: name,
|
|
1658
|
+
description: name === active.name ? 'active' : undefined,
|
|
1659
|
+
})),
|
|
1660
|
+
selectedValue: active.name,
|
|
1661
|
+
height: 10,
|
|
1662
|
+
onSelect: (item) => {
|
|
1663
|
+
if (!setTheme(item.value)) return;
|
|
1664
|
+
saveSetting('theme', item.value);
|
|
1665
|
+
messagesContainer.addChild(
|
|
1666
|
+
new Text(`${c.green} ✓ theme: ${item.value} (saved)${c.reset}`, 1, 0),
|
|
1667
|
+
);
|
|
1668
|
+
messagesContainer.invalidate();
|
|
1669
|
+
editorRow.invalidate();
|
|
1670
|
+
state.refreshStatusLine();
|
|
1671
|
+
refreshSidebar();
|
|
1672
|
+
tui.requestRender();
|
|
1673
|
+
},
|
|
1674
|
+
},
|
|
1675
|
+
{container: messagesContainer, editor, editorRow, tui},
|
|
1650
1676
|
);
|
|
1651
|
-
tui.requestRender();
|
|
1652
1677
|
return;
|
|
1653
1678
|
}
|
|
1654
1679
|
if (trimmed.startsWith('/theme ')) {
|
|
@@ -1704,6 +1729,32 @@ async function main() {
|
|
|
1704
1729
|
tui.requestRender();
|
|
1705
1730
|
return;
|
|
1706
1731
|
}
|
|
1732
|
+
if (trimmed === '/rotation' || trimmed.startsWith('/rotation ')) {
|
|
1733
|
+
const arg = trimmed.slice('/rotation'.length).trim().toLowerCase();
|
|
1734
|
+
const explicit =
|
|
1735
|
+
arg === 'on' || arg === 'true' || arg === 'enable' || arg === '1'
|
|
1736
|
+
? true
|
|
1737
|
+
: arg === 'off' || arg === 'false' || arg === 'disable' || arg === '0'
|
|
1738
|
+
? false
|
|
1739
|
+
: null;
|
|
1740
|
+
const enabled = explicit ?? !config.modelRotation;
|
|
1741
|
+
config.modelRotation = enabled;
|
|
1742
|
+
saveSetting('modelRotation', enabled);
|
|
1743
|
+
messagesContainer.addChild(
|
|
1744
|
+
new Text(
|
|
1745
|
+
`${c.green} ✓ Model rotation ${enabled ? 'enabled' : 'disabled'}${c.reset} ${c.dim}${
|
|
1746
|
+
enabled
|
|
1747
|
+
? '— failing turns switch to the next available model/provider.'
|
|
1748
|
+
: `— failing turns stay on ${config.model}.`
|
|
1749
|
+
}${c.reset}`,
|
|
1750
|
+
1,
|
|
1751
|
+
0,
|
|
1752
|
+
),
|
|
1753
|
+
);
|
|
1754
|
+
state.refreshStatusLine();
|
|
1755
|
+
tui.requestRender();
|
|
1756
|
+
return;
|
|
1757
|
+
}
|
|
1707
1758
|
if (trimmed === '/keys') {
|
|
1708
1759
|
const keys = resolveAllKeys();
|
|
1709
1760
|
const lines = PROVIDERS.map((p) => {
|
|
@@ -1724,6 +1775,35 @@ async function main() {
|
|
|
1724
1775
|
showKeyInput(config, messagesContainer, editor, editorRow, tui);
|
|
1725
1776
|
return;
|
|
1726
1777
|
}
|
|
1778
|
+
if (trimmed.startsWith('/key ')) {
|
|
1779
|
+
const rest = trimmed.slice('/key '.length).trim();
|
|
1780
|
+
const [first, ...remaining] = rest.split(/\s+/);
|
|
1781
|
+
const named = getProvider(first);
|
|
1782
|
+
const rawKey = named ? remaining.join(' ') : rest;
|
|
1783
|
+
if (!rawKey) {
|
|
1784
|
+
showKeyInput(config, messagesContainer, editor, editorRow, tui);
|
|
1785
|
+
return;
|
|
1786
|
+
}
|
|
1787
|
+
const def = named ?? detectProvider(rawKey);
|
|
1788
|
+
if (!def) {
|
|
1789
|
+
messagesContainer.addChild(
|
|
1790
|
+
new Text(
|
|
1791
|
+
`${c.red} Could not detect a provider for that key.${c.reset}\n${c.dim} Use /key <provider-id> <key>, e.g. /key zen sk-ot-...${c.reset}`,
|
|
1792
|
+
1,
|
|
1793
|
+
0,
|
|
1794
|
+
),
|
|
1795
|
+
);
|
|
1796
|
+
tui.requestRender();
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
saveKey(def.id, rawKey);
|
|
1800
|
+
if (state.reloadKeys) state.reloadKeys();
|
|
1801
|
+
messagesContainer.addChild(
|
|
1802
|
+
new Text(`${c.green} ✓ ${def.name} key saved — active now${c.reset}`, 1, 0),
|
|
1803
|
+
);
|
|
1804
|
+
tui.requestRender();
|
|
1805
|
+
return;
|
|
1806
|
+
}
|
|
1727
1807
|
if (trimmed === '/ext' || trimmed.startsWith('/ext ')) {
|
|
1728
1808
|
const args = trimmed.slice('/ext'.length).trim();
|
|
1729
1809
|
const parts = args.split(/\s+/);
|
|
@@ -1754,16 +1834,48 @@ async function main() {
|
|
|
1754
1834
|
} else if (sub === 'list') {
|
|
1755
1835
|
const extensions = state.extensions;
|
|
1756
1836
|
const lines = extensions.map((ext) => {
|
|
1757
|
-
const status =
|
|
1758
|
-
|
|
1837
|
+
const status =
|
|
1838
|
+
ext.status === 'active'
|
|
1839
|
+
? `${c.green}active${c.reset}`
|
|
1840
|
+
: ext.status === 'failed'
|
|
1841
|
+
? `${c.red}failed${c.reset}`
|
|
1842
|
+
: `${c.dim}inactive${c.reset}`;
|
|
1843
|
+
const detail = ext.reason ? ` ${c.dim}— ${ext.reason}${c.reset}` : '';
|
|
1844
|
+
return ` ${ext.name.padEnd(24)} ${status}${detail}`;
|
|
1759
1845
|
});
|
|
1760
|
-
|
|
1846
|
+
|
|
1847
|
+
// Installed but never requested: shown separately so an install can
|
|
1848
|
+
// never look like it silently vanished. These are on disk under
|
|
1849
|
+
// ~/.pal/extensions but absent from config.json, so pal never loads them.
|
|
1850
|
+
const {listInstalledExtensions} = await import('./commands/ext.js');
|
|
1851
|
+
const loadedNames = new Set(
|
|
1852
|
+
extensions.map((ext) => ext.name.replace(/^@pal-code\//, '')),
|
|
1853
|
+
);
|
|
1854
|
+
const unloaded = listInstalledExtensions().filter((pkg) => {
|
|
1855
|
+
const short = pkg.replace(/^@pal-code\//, '');
|
|
1856
|
+
return !loadedNames.has(short);
|
|
1857
|
+
});
|
|
1858
|
+
|
|
1859
|
+
if (lines.length === 0 && unloaded.length === 0) {
|
|
1761
1860
|
const {emptyListMessage} = await import('./commands/ext-registry.js');
|
|
1762
1861
|
messagesContainer.addChild(new Text(`${c.dim} ${emptyListMessage()}${c.reset}`, 1, 0));
|
|
1763
1862
|
} else {
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1863
|
+
const body: string[] = [];
|
|
1864
|
+
if (lines.length > 0) {
|
|
1865
|
+
body.push(`${c.bold}Loaded extensions${c.reset}`, ...lines);
|
|
1866
|
+
}
|
|
1867
|
+
if (unloaded.length > 0) {
|
|
1868
|
+
if (body.length > 0) body.push('');
|
|
1869
|
+
body.push(
|
|
1870
|
+
`${c.bold}Installed but not loaded${c.reset}`,
|
|
1871
|
+
...unloaded.map(
|
|
1872
|
+
(pkg) =>
|
|
1873
|
+
` ${pkg.replace(/^@pal-code\//, '').padEnd(24)} ${c.dim}not in config${c.reset}`,
|
|
1874
|
+
),
|
|
1875
|
+
`${c.dim} Load one with: /ext add <name>${c.reset}`,
|
|
1876
|
+
);
|
|
1877
|
+
}
|
|
1878
|
+
messagesContainer.addChild(new Text(body.join('\n'), 1, 0));
|
|
1767
1879
|
}
|
|
1768
1880
|
} else {
|
|
1769
1881
|
messagesContainer.addChild(
|
|
@@ -1992,19 +2104,20 @@ async function main() {
|
|
|
1992
2104
|
.map((r) => `[messages ${r.fromSeq}-${r.toSeq}]\n${r.summary}`)
|
|
1993
2105
|
.join('\n\n');
|
|
1994
2106
|
}
|
|
1995
|
-
const result = await
|
|
2107
|
+
const result = await runAgentTurn(
|
|
1996
2108
|
message,
|
|
1997
2109
|
messages,
|
|
1998
2110
|
config,
|
|
1999
|
-
tui,
|
|
2000
|
-
messagesContainer,
|
|
2001
2111
|
allKeys,
|
|
2002
2112
|
createProvider,
|
|
2113
|
+
agentUi,
|
|
2114
|
+
approveTool,
|
|
2003
2115
|
state.currentAgentRole,
|
|
2004
2116
|
// Resume from the persisted high-water mark so an already-summarized
|
|
2005
2117
|
// prefix is neither re-sent nor re-summarized.
|
|
2006
2118
|
store && currentSessionId ? store.getCompactedUpto(currentSessionId) : 0,
|
|
2007
2119
|
resumedNote,
|
|
2120
|
+
state.activeAbort?.signal,
|
|
2008
2121
|
);
|
|
2009
2122
|
answered = true;
|
|
2010
2123
|
if (state.activeRun) {
|
|
@@ -2142,7 +2255,7 @@ async function main() {
|
|
|
2142
2255
|
tui.start();
|
|
2143
2256
|
|
|
2144
2257
|
// Load extensions in the background — fast startup, ready before first message.
|
|
2145
|
-
ensureExtensionsLoaded(config.extensions).catch(() => {});
|
|
2258
|
+
ensureExtensionsLoaded(config.extensions, agentUi, state.currentAgentRole).catch(() => {});
|
|
2146
2259
|
|
|
2147
2260
|
// Initialize session store AFTER tui starts (bun:sqlite import must not run before pi-tui stdin setup)
|
|
2148
2261
|
const {SessionStore} = await import('@pal-code/core');
|
|
@@ -2,13 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
import {Text} from '../tui/components/text.js';
|
|
4
4
|
import {Input} from '@earendil-works/pi-tui';
|
|
5
|
-
import type {TUI, Container, Editor} from '@earendil-works/pi-tui';
|
|
6
|
-
import {
|
|
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';
|
|
7
14
|
import type {Config} from '../config.js';
|
|
8
15
|
|
|
9
16
|
import {c} from '../theme.js';
|
|
10
17
|
import {state} from '../state.js';
|
|
11
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__';
|
|
12
23
|
|
|
13
24
|
export function showKeyInput(
|
|
14
25
|
config: Config,
|
|
@@ -17,94 +28,65 @@ export function showKeyInput(
|
|
|
17
28
|
editorRow: Container,
|
|
18
29
|
tui: TUI,
|
|
19
30
|
): void {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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},
|
|
35
66
|
);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const input = new Input({
|
|
39
|
-
prompt: `\n ${c.bold}Provider number or paste API key:${c.reset} `,
|
|
40
|
-
placeholder: 'e.g. 2 or paste sk-...',
|
|
41
|
-
});
|
|
42
|
-
container.addChild(input);
|
|
43
|
-
tui.setFocus(input);
|
|
44
|
-
tui.requestRender();
|
|
45
|
-
|
|
46
|
-
input.onSubmit = (value: string) => {
|
|
47
|
-
const answer = value.trim();
|
|
48
|
-
if (!answer) {
|
|
49
|
-
cleanup();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Check if it's a provider number
|
|
54
|
-
const asNum = Number(answer);
|
|
55
|
-
if (Number.isInteger(asNum) && asNum >= 1 && asNum <= PROVIDERS.length) {
|
|
56
|
-
const provider = PROVIDERS[asNum - 1];
|
|
57
|
-
if (provider.keyOptional) {
|
|
58
|
-
// Keyless — save directly
|
|
59
|
-
saveSetting('keyless', provider.id);
|
|
60
|
-
saveSetting('model', `${provider.id}/${provider.defaultModel}`);
|
|
61
|
-
if (state.reloadKeys) state.reloadKeys();
|
|
62
|
-
|
|
63
|
-
const savedMsg = new Text(
|
|
64
|
-
`\n${c.green}✓ Keyless ${provider.name} mode enabled${c.reset}\n${c.green}✓ Default model:${c.reset} ${provider.id}/${provider.defaultModel}\n`,
|
|
65
|
-
1,
|
|
66
|
-
0,
|
|
67
|
-
);
|
|
68
|
-
container.addChild(savedMsg);
|
|
69
|
-
cleanup();
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Needs a key — show key input for this provider
|
|
74
|
-
cleanup();
|
|
75
|
-
showKeyPrompt(provider.id, provider.name, container, editor, editorRow, tui);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
67
|
+
}
|
|
78
68
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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();
|
|
84
79
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
`\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`,
|
|
88
83
|
1,
|
|
89
84
|
0,
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
input.onEscape = () => cleanup();
|
|
96
|
-
|
|
97
|
-
function cleanup() {
|
|
98
|
-
container.removeChild(header);
|
|
99
|
-
container.removeChild(input);
|
|
100
|
-
editorRow.addChild(editor);
|
|
101
|
-
tui.setFocus(editor);
|
|
102
|
-
state.overlayMode = 'none';
|
|
103
|
-
tui.requestRender();
|
|
104
|
-
}
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
tui.requestRender();
|
|
105
88
|
}
|
|
106
89
|
|
|
107
|
-
/** Second step: prompt for the actual API key for a specific provider. */
|
|
108
90
|
function showKeyPrompt(
|
|
109
91
|
providerId: string,
|
|
110
92
|
providerName: string,
|
|
@@ -113,42 +95,79 @@ function showKeyPrompt(
|
|
|
113
95
|
editorRow: Container,
|
|
114
96
|
tui: TUI,
|
|
115
97
|
): void {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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},
|
|
120
113
|
);
|
|
121
|
-
|
|
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);
|
|
122
162
|
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
});
|
|
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);
|
|
127
166
|
container.addChild(input);
|
|
128
167
|
tui.setFocus(input);
|
|
129
168
|
tui.requestRender();
|
|
130
169
|
|
|
131
|
-
|
|
132
|
-
const key = value.trim();
|
|
133
|
-
if (!key) {
|
|
134
|
-
container.removeChild(label);
|
|
135
|
-
container.removeChild(input);
|
|
136
|
-
editorRow.addChild(editor);
|
|
137
|
-
tui.setFocus(editor);
|
|
138
|
-
state.overlayMode = 'none';
|
|
139
|
-
tui.requestRender();
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
saveKey(providerId, key);
|
|
144
|
-
if (state.reloadKeys) state.reloadKeys();
|
|
145
|
-
|
|
146
|
-
const savedMsg = new Text(
|
|
147
|
-
`\n${c.green}✓ ${providerName} key saved — active now${c.reset}\n`,
|
|
148
|
-
1,
|
|
149
|
-
0,
|
|
150
|
-
);
|
|
151
|
-
container.addChild(savedMsg);
|
|
170
|
+
const close = (): void => {
|
|
152
171
|
container.removeChild(label);
|
|
153
172
|
container.removeChild(input);
|
|
154
173
|
editorRow.addChild(editor);
|
|
@@ -157,12 +176,13 @@ function showKeyPrompt(
|
|
|
157
176
|
tui.requestRender();
|
|
158
177
|
};
|
|
159
178
|
|
|
160
|
-
input.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
179
|
+
input.onSubmit = (value: string) => {
|
|
180
|
+
const trimmed = value.trim();
|
|
181
|
+
if (!trimmed) {
|
|
182
|
+
close();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
opts.onSubmit(trimmed, close);
|
|
167
186
|
};
|
|
187
|
+
input.onEscape = close;
|
|
168
188
|
}
|