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.
- package/README.md +5 -0
- package/package.json +3 -2
- package/packages/cli/src/commands/config-export.ts +99 -0
- package/packages/cli/src/commands/config-io.ts +264 -0
- package/packages/cli/src/commands/ext-registry.ts +83 -0
- package/packages/cli/src/commands/ext.ts +137 -7
- package/packages/cli/src/commands/portable.ts +295 -0
- package/packages/cli/src/commands/update-check.ts +180 -0
- package/packages/cli/src/commands/update.ts +185 -0
- package/packages/cli/src/commands.ts +30 -15
- package/packages/cli/src/components/assistant-body.ts +88 -0
- package/packages/cli/src/components/render-history.ts +5 -4
- package/packages/cli/src/components/run-view.ts +7 -6
- package/packages/cli/src/config.ts +28 -2
- package/packages/cli/src/index.ts +247 -40
- package/packages/cli/src/state.ts +26 -0
- package/packages/cli/src/turn.ts +29 -22
- package/packages/cli/src/utils/code-block.ts +123 -0
- package/packages/cli/src/utils/format.ts +25 -3
- package/packages/cli/src/utils/osc52.ts +55 -0
package/README.md
CHANGED
|
@@ -102,6 +102,11 @@ API keys are resolved from (in order):
|
|
|
102
102
|
|
|
103
103
|
Keyless providers are opted into by setting `"keyless": "<provider-id>"`.
|
|
104
104
|
|
|
105
|
+
Non-key settings can also live in `~/.pal/config.json` — for example
|
|
106
|
+
`"model"`, `"systemPrompt"`, `"extensions"`, and `"contextLimit"`. A
|
|
107
|
+
`contextLimit` set here is overridden by the `CONTEXT_LIMIT` env var, and
|
|
108
|
+
invalid values are ignored in favour of the defaults.
|
|
109
|
+
|
|
105
110
|
### Data storage
|
|
106
111
|
|
|
107
112
|
All user data lives in `~/.pal/`:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pal-code",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "A terminal coding agent with BYOK, tool use, and extensions",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"start": "cd packages/cli && bun src/index.ts",
|
|
31
31
|
"dev": "cd packages/cli && bun src/index.ts",
|
|
32
32
|
"test": "bun test",
|
|
33
|
+
"eval": "bun run evals/run.ts",
|
|
33
34
|
"typecheck": "bunx tsc --noEmit",
|
|
34
35
|
"lint": "biome check .",
|
|
35
36
|
"lint:fix": "biome check --write .",
|
|
@@ -61,7 +62,7 @@
|
|
|
61
62
|
"gemini"
|
|
62
63
|
],
|
|
63
64
|
"dependencies": {
|
|
64
|
-
"@pal-code/core": "0.0.
|
|
65
|
+
"@pal-code/core": "0.0.13",
|
|
65
66
|
"@earendil-works/pi-tui": "^0.85.1"
|
|
66
67
|
},
|
|
67
68
|
"devDependencies": {
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config as code — the export side of `pal export [file]` / `/export`.
|
|
3
|
+
*
|
|
4
|
+
* Writes one versionable JSON file built from ~/.pal/config.json (minus key
|
|
5
|
+
* material), ~/.pal/providers.json (sanitized), optional hooks/agents/skills
|
|
6
|
+
* references, and the project .pal.json pattern fields. Corrupt local files
|
|
7
|
+
* abort the export instead of producing a partial document.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {resolve} from 'node:path';
|
|
11
|
+
|
|
12
|
+
import {readPalConfig} from '@pal-code/core';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
EXPORT_VERSION,
|
|
16
|
+
HOOKS_LIKE_FIELDS,
|
|
17
|
+
PROJECT_FIELDS,
|
|
18
|
+
collectSecretStrings,
|
|
19
|
+
isHooksLike,
|
|
20
|
+
isRecord,
|
|
21
|
+
isSecretField,
|
|
22
|
+
sanitizeProvider,
|
|
23
|
+
scrubSecrets,
|
|
24
|
+
} from './portable.js';
|
|
25
|
+
import type {PortableConfig, PortableProject} from './portable.js';
|
|
26
|
+
|
|
27
|
+
import {
|
|
28
|
+
loadLocalProviders,
|
|
29
|
+
projectFilePath,
|
|
30
|
+
providersJsonPath,
|
|
31
|
+
readLocalJson,
|
|
32
|
+
writeJson,
|
|
33
|
+
} from './config-io.js';
|
|
34
|
+
import type {ConfigCommandResult} from './config-io.js';
|
|
35
|
+
|
|
36
|
+
const DEFAULT_EXPORT_FILE = 'pal-export.json';
|
|
37
|
+
|
|
38
|
+
/** Build the versionable export document from local state; refuses on corrupt local files. */
|
|
39
|
+
export function buildPortableConfig():
|
|
40
|
+
| {ok: true; doc: PortableConfig}
|
|
41
|
+
| {ok: false; message: string} {
|
|
42
|
+
const cfg = readPalConfig();
|
|
43
|
+
const secrets = collectSecretStrings(cfg, false);
|
|
44
|
+
|
|
45
|
+
const providers = loadLocalProviders(providersJsonPath());
|
|
46
|
+
if (!providers.ok) return {ok: false, message: `Export failed: ${providers.message}`};
|
|
47
|
+
secrets.push(...collectSecretStrings(providers.raw, false));
|
|
48
|
+
|
|
49
|
+
const settings: Record<string, unknown> = {};
|
|
50
|
+
for (const [k, v] of Object.entries(cfg)) {
|
|
51
|
+
if (isSecretField(k) || isHooksLike(k)) continue;
|
|
52
|
+
settings[k] = scrubSecrets(v, secrets);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const doc: PortableConfig = {version: EXPORT_VERSION, settings};
|
|
56
|
+
for (const field of HOOKS_LIKE_FIELDS) {
|
|
57
|
+
if (cfg[field] !== undefined) doc[field] = scrubSecrets(cfg[field], secrets);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (Array.isArray(providers.raw)) {
|
|
61
|
+
const entries = providers.raw
|
|
62
|
+
.filter(
|
|
63
|
+
(entry): entry is Record<string, unknown> =>
|
|
64
|
+
isRecord(entry) && typeof entry.id === 'string' && typeof entry.baseUrl === 'string',
|
|
65
|
+
)
|
|
66
|
+
.map((entry) => scrubSecrets(sanitizeProvider(entry), secrets));
|
|
67
|
+
if (entries.length > 0) doc.providers = entries;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const projectPath = projectFilePath();
|
|
71
|
+
if (projectPath) {
|
|
72
|
+
const project = readLocalJson(projectPath);
|
|
73
|
+
if (project.state === 'invalid') {
|
|
74
|
+
return {ok: false, message: `Export failed: ${projectPath} is not valid JSON`};
|
|
75
|
+
}
|
|
76
|
+
if (project.state === 'ok') {
|
|
77
|
+
if (!isRecord(project.value)) {
|
|
78
|
+
return {ok: false, message: `Export failed: ${projectPath} must contain a JSON object`};
|
|
79
|
+
}
|
|
80
|
+
const patterns: PortableProject = {};
|
|
81
|
+
for (const field of PROJECT_FIELDS) {
|
|
82
|
+
const v = project.value[field];
|
|
83
|
+
if (v !== undefined) Object.assign(patterns, {[field]: v});
|
|
84
|
+
}
|
|
85
|
+
if (Object.keys(patterns).length > 0) doc.project = patterns;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return {ok: true, doc};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function exportConfig(file?: string): ConfigCommandResult {
|
|
93
|
+
const built = buildPortableConfig();
|
|
94
|
+
if (!built.ok) return built;
|
|
95
|
+
|
|
96
|
+
const target = resolve(file ?? DEFAULT_EXPORT_FILE);
|
|
97
|
+
writeJson(target, built.doc, true);
|
|
98
|
+
return {ok: true, message: `Exported pal config to ${target} (secrets excluded).`};
|
|
99
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config as code — the import side of `pal import <file>` / `/import`, plus
|
|
3
|
+
* the local-state helpers shared with the export side (config-export.ts).
|
|
4
|
+
*
|
|
5
|
+
* Import merges an export file back into local config: local API keys are
|
|
6
|
+
* never overwritten or removed, every change is reported as a diff, and a
|
|
7
|
+
* malformed file is rejected before any write. The file is fully parsed and
|
|
8
|
+
* every write is planned before the first byte hits disk.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'node:fs';
|
|
12
|
+
import {dirname, join, resolve} from 'node:path';
|
|
13
|
+
|
|
14
|
+
import {readPalConfig, resolveProjectRoot, saveSetting} from '@pal-code/core';
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
HOOKS_LIKE_FIELDS,
|
|
18
|
+
PROJECT_FIELDS,
|
|
19
|
+
fmtValue,
|
|
20
|
+
isRecord,
|
|
21
|
+
isSecretField,
|
|
22
|
+
jsonEqual,
|
|
23
|
+
parsePortableConfig,
|
|
24
|
+
} from './portable.js';
|
|
25
|
+
import type {PortableConfig} from './portable.js';
|
|
26
|
+
|
|
27
|
+
export interface ConfigCommandResult {
|
|
28
|
+
ok: boolean;
|
|
29
|
+
message: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const home = () => process.env.USERPROFILE ?? process.env.HOME ?? '';
|
|
33
|
+
export const providersJsonPath = () => join(home(), '.pal', 'providers.json');
|
|
34
|
+
|
|
35
|
+
type LocalJson =
|
|
36
|
+
| {state: 'absent'}
|
|
37
|
+
| {state: 'ok'; value: unknown}
|
|
38
|
+
| {state: 'invalid'; error: string};
|
|
39
|
+
|
|
40
|
+
export function readLocalJson(path: string): LocalJson {
|
|
41
|
+
if (!existsSync(path)) return {state: 'absent'};
|
|
42
|
+
try {
|
|
43
|
+
return {state: 'ok', value: JSON.parse(readFileSync(path, 'utf-8'))};
|
|
44
|
+
} catch (err) {
|
|
45
|
+
return {state: 'invalid', error: errMsg(err)};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function errMsg(err: unknown): string {
|
|
50
|
+
return err instanceof Error ? err.message : String(err);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function writeJson(path: string, value: unknown, trailingNewline: boolean): void {
|
|
54
|
+
mkdirSync(dirname(path), {recursive: true});
|
|
55
|
+
const json = JSON.stringify(value, null, 2);
|
|
56
|
+
writeFileSync(path, trailingNewline ? `${json}\n` : json, 'utf-8');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Nearest project config file path, or null when the project has none yet. */
|
|
60
|
+
export function projectFilePath(): string | null {
|
|
61
|
+
const root = resolveProjectRoot();
|
|
62
|
+
if (!root) return null;
|
|
63
|
+
for (const name of ['.pal.json', '.pal.config.json']) {
|
|
64
|
+
const path = join(root, name);
|
|
65
|
+
if (existsSync(path)) return path;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function loadLocalProviders(
|
|
71
|
+
path: string,
|
|
72
|
+
): {ok: true; list: Record<string, unknown>[]; raw: unknown} | {ok: false; message: string} {
|
|
73
|
+
const local = readLocalJson(path);
|
|
74
|
+
if (local.state === 'invalid') {
|
|
75
|
+
return {ok: false, message: `${path} is not valid JSON (${local.error})`};
|
|
76
|
+
}
|
|
77
|
+
if (local.state === 'absent') return {ok: true, list: [], raw: undefined};
|
|
78
|
+
if (!Array.isArray(local.value)) {
|
|
79
|
+
return {ok: false, message: `${path} must contain a JSON array`};
|
|
80
|
+
}
|
|
81
|
+
const list: Record<string, unknown>[] = [];
|
|
82
|
+
for (const entry of local.value) {
|
|
83
|
+
if (!isRecord(entry)) return {ok: false, message: `${path} contains a non-object entry`};
|
|
84
|
+
list.push(entry);
|
|
85
|
+
}
|
|
86
|
+
return {ok: true, list, raw: local.value};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface ImportPlan {
|
|
90
|
+
settings: Array<[string, unknown]>;
|
|
91
|
+
skippedSecretFields: string[];
|
|
92
|
+
providers: Record<string, unknown>[];
|
|
93
|
+
providersPath: string;
|
|
94
|
+
providersChanged: boolean;
|
|
95
|
+
projectPath: string;
|
|
96
|
+
project: Record<string, unknown>;
|
|
97
|
+
projectChanged: boolean;
|
|
98
|
+
diff: string[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function planSettings(
|
|
102
|
+
doc: PortableConfig,
|
|
103
|
+
): Pick<ImportPlan, 'settings' | 'skippedSecretFields' | 'diff'> {
|
|
104
|
+
const incoming: Record<string, unknown> = {...doc.settings};
|
|
105
|
+
for (const field of HOOKS_LIKE_FIELDS) {
|
|
106
|
+
if (doc[field] !== undefined) incoming[field] = doc[field];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const localCfg = readPalConfig();
|
|
110
|
+
const settings: Array<[string, unknown]> = [];
|
|
111
|
+
const skippedSecretFields: string[] = [];
|
|
112
|
+
const diff: string[] = [];
|
|
113
|
+
|
|
114
|
+
for (const [k, v] of Object.entries(incoming)) {
|
|
115
|
+
if (isSecretField(k)) {
|
|
116
|
+
skippedSecretFields.push(k);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (!jsonEqual(localCfg[k], v)) {
|
|
120
|
+
diff.push(`${k}: ${fmtValue(localCfg[k])} → ${fmtValue(v)}`);
|
|
121
|
+
settings.push([k, v]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return {settings, skippedSecretFields, diff};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function planProviders(
|
|
128
|
+
doc: PortableConfig,
|
|
129
|
+
diff: string[],
|
|
130
|
+
):
|
|
131
|
+
| {
|
|
132
|
+
ok: true;
|
|
133
|
+
providers: Record<string, unknown>[];
|
|
134
|
+
providersPath: string;
|
|
135
|
+
providersChanged: boolean;
|
|
136
|
+
}
|
|
137
|
+
| {ok: false; message: string} {
|
|
138
|
+
const providersPath = providersJsonPath();
|
|
139
|
+
const local = loadLocalProviders(providersPath);
|
|
140
|
+
if (!local.ok) return {ok: false, message: `Import failed: ${local.message}`};
|
|
141
|
+
|
|
142
|
+
const merged = [...local.list];
|
|
143
|
+
const localIds = new Set(local.list.map((entry) => String(entry.id)));
|
|
144
|
+
let changed = false;
|
|
145
|
+
|
|
146
|
+
for (const incoming of doc.providers ?? []) {
|
|
147
|
+
const incomingId = String(incoming.id);
|
|
148
|
+
if (localIds.has(incomingId)) {
|
|
149
|
+
if (
|
|
150
|
+
!jsonEqual(
|
|
151
|
+
local.list.find((entry) => entry.id === incoming.id),
|
|
152
|
+
incoming,
|
|
153
|
+
)
|
|
154
|
+
) {
|
|
155
|
+
diff.push(`provider ${incomingId}: kept local definition (imported copy ignored)`);
|
|
156
|
+
}
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
merged.push(incoming);
|
|
160
|
+
localIds.add(incomingId);
|
|
161
|
+
changed = true;
|
|
162
|
+
diff.push(`+ provider ${incomingId} (${fmtValue(incoming.name ?? incoming.baseUrl)})`);
|
|
163
|
+
}
|
|
164
|
+
return {ok: true, providers: merged, providersPath, providersChanged: changed};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function planProject(
|
|
168
|
+
doc: PortableConfig,
|
|
169
|
+
diff: string[],
|
|
170
|
+
):
|
|
171
|
+
| {ok: true; projectPath: string; project: Record<string, unknown>; projectChanged: boolean}
|
|
172
|
+
| {ok: false; message: string} {
|
|
173
|
+
let projectPath = projectFilePath();
|
|
174
|
+
let local: Record<string, unknown> = {};
|
|
175
|
+
if (projectPath) {
|
|
176
|
+
const existing = readLocalJson(projectPath);
|
|
177
|
+
if (existing.state === 'invalid') {
|
|
178
|
+
return {
|
|
179
|
+
ok: false,
|
|
180
|
+
message: `Import failed: ${projectPath} is not valid JSON (${existing.error})`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
if (existing.state === 'ok') {
|
|
184
|
+
if (!isRecord(existing.value)) {
|
|
185
|
+
return {ok: false, message: `Import failed: ${projectPath} must contain a JSON object`};
|
|
186
|
+
}
|
|
187
|
+
local = existing.value;
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
projectPath = join(process.cwd(), '.pal.json');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const next = {...local};
|
|
194
|
+
let changed = false;
|
|
195
|
+
if (doc.project) {
|
|
196
|
+
for (const field of PROJECT_FIELDS) {
|
|
197
|
+
if (field === 'ignore') {
|
|
198
|
+
const incoming = doc.project.ignore;
|
|
199
|
+
if (incoming === undefined) continue;
|
|
200
|
+
const existing = Array.isArray(local.ignore)
|
|
201
|
+
? local.ignore.filter((p): p is string => typeof p === 'string')
|
|
202
|
+
: [];
|
|
203
|
+
const added = incoming.filter((p) => !existing.includes(p));
|
|
204
|
+
if (added.length > 0) {
|
|
205
|
+
next.ignore = [...existing, ...added];
|
|
206
|
+
changed = true;
|
|
207
|
+
diff.push(`project.ignore: +${added.join(' +')}`);
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
const incoming = doc.project[field];
|
|
211
|
+
if (incoming === undefined) continue;
|
|
212
|
+
if (!jsonEqual(local[field], incoming)) {
|
|
213
|
+
diff.push(`project.${field}: ${fmtValue(local[field])} → ${fmtValue(incoming)}`);
|
|
214
|
+
next[field] = incoming;
|
|
215
|
+
changed = true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return {ok: true, projectPath, project: next, projectChanged: changed};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Merge an export file into local config; a malformed file leaves local config untouched. */
|
|
224
|
+
export function importConfig(file: string): ConfigCommandResult {
|
|
225
|
+
const source = resolve(file);
|
|
226
|
+
if (!existsSync(source)) {
|
|
227
|
+
return {ok: false, message: `Import failed: file not found: ${source}`};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let parsed: unknown;
|
|
231
|
+
try {
|
|
232
|
+
parsed = JSON.parse(readFileSync(source, 'utf-8'));
|
|
233
|
+
} catch (err) {
|
|
234
|
+
return {ok: false, message: `Import failed: ${source} is not valid JSON: ${errMsg(err)}`};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const result = parsePortableConfig(parsed);
|
|
238
|
+
if (!result.ok) {
|
|
239
|
+
return {ok: false, message: `Import failed: ${result.message}`};
|
|
240
|
+
}
|
|
241
|
+
const doc = result.doc;
|
|
242
|
+
|
|
243
|
+
const settingsPlan = planSettings(doc);
|
|
244
|
+
const providersPlan = planProviders(doc, settingsPlan.diff);
|
|
245
|
+
if (!providersPlan.ok) return providersPlan;
|
|
246
|
+
const projectPlan = planProject(doc, settingsPlan.diff);
|
|
247
|
+
if (!projectPlan.ok) return projectPlan;
|
|
248
|
+
|
|
249
|
+
for (const [key, value] of settingsPlan.settings) saveSetting(key, value);
|
|
250
|
+
if (providersPlan.providersChanged) {
|
|
251
|
+
writeJson(providersPlan.providersPath, providersPlan.providers, false);
|
|
252
|
+
}
|
|
253
|
+
if (projectPlan.projectChanged) {
|
|
254
|
+
writeJson(projectPlan.projectPath, projectPlan.project, false);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const lines = settingsPlan.diff.length
|
|
258
|
+
? settingsPlan.diff.map((line) => ` ${line}`)
|
|
259
|
+
: [' (no changes)'];
|
|
260
|
+
const skipped = settingsPlan.skippedSecretFields.length
|
|
261
|
+
? `\n skipped secret fields (never imported): ${settingsPlan.skippedSecretFields.join(', ')}`
|
|
262
|
+
: '';
|
|
263
|
+
return {ok: true, message: `Imported ${source}:\n${lines.join('\n')}${skipped}`};
|
|
264
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Curated extension registry — the official `@pal-code/*` packages.
|
|
3
|
+
*
|
|
4
|
+
* A handpicked list for users who don't know what to search for. It backs
|
|
5
|
+
* /ext browse, the empty-/ext-list next-step hint, and the blank-/ext-search
|
|
6
|
+
* featured list — discovery without npm keyword knowledge.
|
|
7
|
+
*
|
|
8
|
+
* Data only: no imports, no network. To add an extension, append to the
|
|
9
|
+
* REGISTRY array below.
|
|
10
|
+
* Format: { name, description, package, tags }
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export interface ExtRegistryEntry {
|
|
14
|
+
/** Display name. */
|
|
15
|
+
name: string;
|
|
16
|
+
/** One-line why-install description. */
|
|
17
|
+
description: string;
|
|
18
|
+
/** npm package installed by `pal add <package>`. */
|
|
19
|
+
package: string;
|
|
20
|
+
/** Tags for search matching. */
|
|
21
|
+
tags: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const REGISTRY: ExtRegistryEntry[] = [
|
|
25
|
+
{
|
|
26
|
+
name: 'Agents',
|
|
27
|
+
description: 'Two-agent roles plus delegate / delegate_parallel orchestration',
|
|
28
|
+
package: '@pal-code/agents',
|
|
29
|
+
tags: ['agents', 'orchestration', 'delegate', 'delegate_parallel', 'roles', 'subagent'],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: 'Web Search',
|
|
33
|
+
description: 'web_search and fetch_url tools for the main agent',
|
|
34
|
+
package: '@pal-code/web-search',
|
|
35
|
+
tags: ['web', 'search', 'fetch', 'fetch_url', 'internet', 'browser'],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'MCP',
|
|
39
|
+
description: 'Bridge MCP servers into pal as tools',
|
|
40
|
+
package: '@pal-code/mcp',
|
|
41
|
+
tags: ['mcp', 'model context protocol', 'server', 'bridge', 'integrations'],
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
/** ANSI codes used when rendering the browse list (omit for plain text). */
|
|
46
|
+
export interface BrowseColors {
|
|
47
|
+
bold: string;
|
|
48
|
+
cyan: string;
|
|
49
|
+
dim: string;
|
|
50
|
+
green: string;
|
|
51
|
+
reset: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const PLAIN: BrowseColors = {bold: '', cyan: '', dim: '', green: '', reset: ''};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Render the curated registry as the /ext browse listing — also reused as
|
|
58
|
+
* the featured list for a blank /ext search.
|
|
59
|
+
*
|
|
60
|
+
* Plain by default so tests can assert on the text; pass the CLI colour
|
|
61
|
+
* palette to mirror the /mcp browse presentation.
|
|
62
|
+
*/
|
|
63
|
+
export function formatBrowse(colors: BrowseColors = PLAIN): string {
|
|
64
|
+
const c = colors;
|
|
65
|
+
const lines: string[] = [`${c.bold}Featured extensions${c.reset}`, ''];
|
|
66
|
+
for (const entry of REGISTRY) {
|
|
67
|
+
lines.push(` ${c.cyan}${entry.name}${c.reset} ${c.dim}${entry.package}${c.reset}`);
|
|
68
|
+
lines.push(` ${c.dim}${entry.description}${c.reset}`);
|
|
69
|
+
lines.push(` ${c.green}pal add ${entry.package}${c.reset}`);
|
|
70
|
+
lines.push('');
|
|
71
|
+
}
|
|
72
|
+
lines.push(`${c.dim}Search npm with /ext search <query>.${c.reset}`);
|
|
73
|
+
return lines.join('\n');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* /ext list with nothing configured or loaded: point at a first install
|
|
78
|
+
* instead of dead-ending at "No extensions loaded."
|
|
79
|
+
*/
|
|
80
|
+
export function emptyListMessage(): string {
|
|
81
|
+
const starter = REGISTRY[0].package;
|
|
82
|
+
return `No extensions loaded. Start with: /ext add ${starter} — or /ext browse`;
|
|
83
|
+
}
|
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Extension install/uninstall commands.
|
|
2
|
+
* Extension install/uninstall commands + npm-native discovery.
|
|
3
3
|
*
|
|
4
|
-
* `pal add <pkg>`
|
|
5
|
-
* registers the package in
|
|
6
|
-
* `pal remove <pkg>` does the reverse.
|
|
4
|
+
* `pal add <pkg>` validates the name against the public npm registry, runs
|
|
5
|
+
* `bun add <pkg>` inside ~/.pal/extensions/, and registers the package in
|
|
6
|
+
* ~/.pal/config.json. `pal remove <pkg>` does the reverse.
|
|
7
|
+
*
|
|
8
|
+
* Discovery queries the npm registry search API directly from the client —
|
|
9
|
+
* no backend and no hand-maintained manifest: install/search work for any
|
|
10
|
+
* package on npm, not just `@pal-code/*`.
|
|
7
11
|
*/
|
|
8
12
|
|
|
9
13
|
import {homedir} from 'node:os';
|
|
10
14
|
import {join} from 'node:path';
|
|
11
|
-
import {existsSync, mkdirSync,
|
|
15
|
+
import {existsSync, mkdirSync, writeFileSync} from 'node:fs';
|
|
12
16
|
import {execSync} from 'node:child_process';
|
|
13
17
|
import {readPalConfig, saveSetting} from '@pal-code/core';
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
import {formatBrowse} from './ext-registry.js';
|
|
20
|
+
|
|
21
|
+
export const EXTENSIONS_DIR = join(homedir(), '.pal', 'extensions');
|
|
22
|
+
|
|
23
|
+
const NPM_SEARCH_URL = 'https://registry.npmjs.org/-/v1/search';
|
|
24
|
+
const NPM_REGISTRY_URL = 'https://registry.npmjs.org';
|
|
25
|
+
|
|
26
|
+
/** Conservative npm-name pattern (also blocks shell metacharacters in execSync). */
|
|
27
|
+
const NPM_NAME_RE = /^(?:@[A-Za-z0-9][A-Za-z0-9._~-]*\/)?[A-Za-z0-9][A-Za-z0-9._~-]*$/;
|
|
16
28
|
|
|
17
29
|
function ensureExtensionsDir(): void {
|
|
18
30
|
if (!existsSync(EXTENSIONS_DIR)) {
|
|
@@ -35,7 +47,125 @@ export interface ExtCommandResult {
|
|
|
35
47
|
message: string;
|
|
36
48
|
}
|
|
37
49
|
|
|
38
|
-
export
|
|
50
|
+
export interface NpmSearchHit {
|
|
51
|
+
name: string;
|
|
52
|
+
description: string;
|
|
53
|
+
version: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ExtSearchResult extends ExtCommandResult {
|
|
57
|
+
results: NpmSearchHit[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
61
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Shape-tolerant parse of an npm search API response body. */
|
|
65
|
+
export function parseNpmSearch(data: unknown): NpmSearchHit[] {
|
|
66
|
+
if (!isRecord(data) || !Array.isArray(data.objects)) return [];
|
|
67
|
+
const hits: NpmSearchHit[] = [];
|
|
68
|
+
for (const entry of data.objects) {
|
|
69
|
+
if (!isRecord(entry) || !isRecord(entry.package)) continue;
|
|
70
|
+
const pkg = entry.package;
|
|
71
|
+
if (typeof pkg.name !== 'string') continue;
|
|
72
|
+
hits.push({
|
|
73
|
+
name: pkg.name,
|
|
74
|
+
description: typeof pkg.description === 'string' ? pkg.description : '',
|
|
75
|
+
version: typeof pkg.version === 'string' ? pkg.version : '',
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return hits;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function truncate(text: string, max: number): string {
|
|
82
|
+
return text.length <= max ? text : `${text.slice(0, max - 1)}…`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Query the public npm registry search API (client-side, zero backend).
|
|
87
|
+
* Hits whose name contains the query (e.g. `@pal-code/*` for "pal") are
|
|
88
|
+
* ranked ahead of npm's popularity ordering.
|
|
89
|
+
*/
|
|
90
|
+
export async function extSearch(query: string): Promise<ExtSearchResult> {
|
|
91
|
+
const text = query.trim();
|
|
92
|
+
if (!text) {
|
|
93
|
+
return {ok: false, message: 'Usage: /ext search <query>', results: []};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let data: unknown;
|
|
97
|
+
try {
|
|
98
|
+
const res = await fetch(`${NPM_SEARCH_URL}?text=${encodeURIComponent(text)}&size=30`);
|
|
99
|
+
if (!res.ok) {
|
|
100
|
+
return {
|
|
101
|
+
ok: false,
|
|
102
|
+
message: `npm search failed with HTTP ${res.status}.`,
|
|
103
|
+
results: [],
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
data = await res.json();
|
|
107
|
+
} catch (err) {
|
|
108
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
109
|
+
return {ok: false, message: `npm search failed: ${msg}`, results: []};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const needle = text.toLowerCase();
|
|
113
|
+
const results = parseNpmSearch(data).sort((a, b) => {
|
|
114
|
+
const aMatch = a.name.toLowerCase().includes(needle) ? 0 : 1;
|
|
115
|
+
const bMatch = b.name.toLowerCase().includes(needle) ? 0 : 1;
|
|
116
|
+
return aMatch - bMatch;
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (results.length === 0) {
|
|
120
|
+
return {ok: false, message: `No npm packages matched "${text}".`, results};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const lines = results.map(
|
|
124
|
+
(hit) => ` ${hit.name.padEnd(28)} ${hit.version.padEnd(10)} ${truncate(hit.description, 64)}`,
|
|
125
|
+
);
|
|
126
|
+
return {
|
|
127
|
+
ok: true,
|
|
128
|
+
message: `npm results for "${text}" (${results.length}):\n${lines.join('\n')}`,
|
|
129
|
+
results,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* /ext search entry point. A blank query shows the curated featured list so a
|
|
135
|
+
* user who doesn't know what to search for still gets a next step; a real
|
|
136
|
+
* query goes to npm via extSearch unchanged.
|
|
137
|
+
*/
|
|
138
|
+
export async function extSearchOrBrowse(query: string): Promise<ExtSearchResult> {
|
|
139
|
+
if (!query.trim()) {
|
|
140
|
+
return {ok: true, message: formatBrowse(), results: []};
|
|
141
|
+
}
|
|
142
|
+
return extSearch(query);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 404 → package is absent from npm; other failures → unknown (offline-tolerant). */
|
|
146
|
+
async function lookupNpmPackage(pkg: string): Promise<'found' | 'missing' | 'unknown'> {
|
|
147
|
+
const url = `${NPM_REGISTRY_URL}/${pkg.replaceAll('/', '%2F')}`;
|
|
148
|
+
try {
|
|
149
|
+
const res = await fetch(url);
|
|
150
|
+
if (res.status === 404) return 'missing';
|
|
151
|
+
return res.ok ? 'found' : 'unknown';
|
|
152
|
+
} catch {
|
|
153
|
+
return 'unknown';
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function extAdd(pkg: string): Promise<ExtCommandResult> {
|
|
158
|
+
if (!NPM_NAME_RE.test(pkg)) {
|
|
159
|
+
return {ok: false, message: `Invalid package name: ${pkg}`};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Validate against npm before installing; on registry/network failure fall
|
|
163
|
+
// through to `bun add` so offline installs still work (bun will 404 itself).
|
|
164
|
+
const lookup = await lookupNpmPackage(pkg);
|
|
165
|
+
if (lookup === 'missing') {
|
|
166
|
+
return {ok: false, message: `"${pkg}" was not found on the npm registry.`};
|
|
167
|
+
}
|
|
168
|
+
|
|
39
169
|
try {
|
|
40
170
|
ensureExtensionsDir();
|
|
41
171
|
|