@kirkchinese/harness-forge-core 0.4.0 → 0.5.0
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/lib/assess.js +24 -4
- package/lib/contracts.d.ts +1 -1
- package/lib/dialects/claude-code/index.js +38 -41
- package/lib/dialects/claude-code/sessions/discover.js +31 -17
- package/lib/dialects/claude-code/sessions/parse.js +89 -83
- package/lib/dialects/claude-code/sessions/title.d.ts +23 -0
- package/lib/dialects/claude-code/sessions/title.js +74 -0
- package/lib/dialects/claude-code/statusline.d.ts +12 -0
- package/lib/dialects/claude-code/statusline.js +74 -0
- package/lib/dialects/claude-code/user-assets.d.ts +37 -6
- package/lib/dialects/claude-code/user-assets.js +155 -34
- package/lib/dialects/codex/index.js +8 -1
- package/lib/dialects/codex/mcp-server.d.ts +17 -0
- package/lib/dialects/codex/mcp-server.js +116 -0
- package/lib/dialects/codex/plugins.d.ts +16 -0
- package/lib/dialects/codex/plugins.js +120 -0
- package/lib/dialects/codex/user-assets.d.ts +10 -6
- package/lib/dialects/codex/user-assets.js +26 -42
- package/lib/dialects/opencode/index.js +43 -8
- package/lib/dialects/opencode/inventory.d.ts +100 -0
- package/lib/dialects/opencode/inventory.js +318 -0
- package/lib/dialects/opencode/plugin-id.d.ts +21 -0
- package/lib/dialects/opencode/plugin-id.js +296 -0
- package/lib/dialects/opencode/scan.d.ts +22 -0
- package/lib/dialects/opencode/scan.js +119 -121
- package/lib/dialects/types.d.ts +60 -6
- package/lib/discover.d.ts +32 -7
- package/lib/discover.js +58 -17
- package/lib/fingerprint.js +7 -0
- package/lib/harness-installs.d.ts +42 -0
- package/lib/harness-installs.js +59 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +5 -0
- package/lib/instructions.d.ts +28 -2
- package/lib/instructions.js +202 -30
- package/lib/license.js +12 -11
- package/lib/manifest.d.ts +6 -0
- package/lib/manifest.js +41 -12
- package/lib/marketplace.d.ts +15 -1
- package/lib/marketplace.js +55 -38
- package/lib/paths.d.ts +65 -0
- package/lib/paths.js +152 -2
- package/lib/plan.js +3 -1
- package/lib/port-brief.d.ts +28 -5
- package/lib/port-brief.js +52 -10
- package/lib/remote.d.ts +121 -0
- package/lib/remote.js +352 -0
- package/lib/sessions/index.d.ts +1 -0
- package/lib/sessions/index.js +1 -0
- package/lib/sessions/lines.d.ts +57 -0
- package/lib/sessions/lines.js +122 -0
- package/lib/text.d.ts +9 -0
- package/lib/text.js +19 -0
- package/lib/trust.js +19 -6
- package/lib/types.d.ts +48 -1
- package/package.json +1 -1
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OpenCode plugins one config directory loads, enumerated the way OpenCode
|
|
3
|
+
* finds them (https://opencode.ai/docs/plugins/, and the V1 docs at
|
|
4
|
+
* https://opencode.ai/v1/docs/plugins.md):
|
|
5
|
+
*
|
|
6
|
+
* - `.ts`/`.js`/`.mjs` files and package directories right inside its
|
|
7
|
+
* `plugins/` folder (V1 also read `plugin/`; V2 discovers both);
|
|
8
|
+
* - the `plugins` (V2) or `plugin` (V1) entries of its `opencode.json(c)`:
|
|
9
|
+
* local paths, resolved from the config file and required to be
|
|
10
|
+
* directories, and npm package specs, found where OpenCode installed them:
|
|
11
|
+
* `node_modules` of the config directory that declared them, else OpenCode's
|
|
12
|
+
* cache `~/.cache/opencode/node_modules`.
|
|
13
|
+
*
|
|
14
|
+
* Nothing below those folders is searched: a package's own files are its own.
|
|
15
|
+
* @module @kirkchinese/harness-forge-core
|
|
16
|
+
*/
|
|
17
|
+
import { readdirSync } from 'node:fs';
|
|
18
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
import { contentPath, isDirectory, isFile, isPlainObject, reachesNewShare, readJsonObject, readText } from '../../paths.js';
|
|
21
|
+
import { stripJsonc } from './scan.js';
|
|
22
|
+
import { declaredPluginId } from './plugin-id.js';
|
|
23
|
+
/** The plugin folders of a config directory: V2's `plugins`, and V1's `plugin`, which V2 still reads. */
|
|
24
|
+
export const OPENCODE_PLUGIN_FOLDERS = ['plugins', 'plugin'];
|
|
25
|
+
/** The files OpenCode loads as plugins from a plugins folder. */
|
|
26
|
+
const PLUGIN_FILE = /\.(ts|tsx|js|mjs)$/u;
|
|
27
|
+
/** The config files of a directory, as OpenCode names them. */
|
|
28
|
+
export function openCodeConfigFiles(directory) {
|
|
29
|
+
return ['opencode.json', 'opencode.jsonc'].map(name => join(directory, name)).filter(isFile);
|
|
30
|
+
}
|
|
31
|
+
/** Read one `opencode.json` or `opencode.jsonc`. */
|
|
32
|
+
export function readOpenCodeConfigFile(path) {
|
|
33
|
+
if (path.endsWith('.json'))
|
|
34
|
+
return readJsonObject(path);
|
|
35
|
+
const text = readText(path);
|
|
36
|
+
if (text === undefined)
|
|
37
|
+
return undefined;
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(stripJsonc(text));
|
|
40
|
+
return isPlainObject(parsed) ? parsed : undefined;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** The plugin entries of a config, in order: V2 `plugins` (strings or `{ package }`), else V1 `plugin` (strings or `[spec, options]`). */
|
|
47
|
+
function configuredOperations(config) {
|
|
48
|
+
const entries = Array.isArray(config['plugins']) ? config['plugins'] : Array.isArray(config['plugin']) ? config['plugin'] : [];
|
|
49
|
+
const operations = [];
|
|
50
|
+
for (const entry of entries) {
|
|
51
|
+
const spec = typeof entry === 'string' ? entry
|
|
52
|
+
: isPlainObject(entry) && typeof entry['package'] === 'string' ? entry['package']
|
|
53
|
+
: Array.isArray(entry) && typeof entry[0] === 'string' ? entry[0] : undefined;
|
|
54
|
+
// A lone `-` names nothing to remove (OpenCode refuses it).
|
|
55
|
+
if (spec === undefined || spec.length === 0 || spec === '-')
|
|
56
|
+
continue;
|
|
57
|
+
operations.push(typeof entry === 'string' && spec.startsWith('-') ? { type: 'remove', target: spec.slice(1) } : { type: 'add', target: spec });
|
|
58
|
+
}
|
|
59
|
+
return operations;
|
|
60
|
+
}
|
|
61
|
+
/** The plugin specs a config loads: its entries that add, not those that switch plugins on or off by id. */
|
|
62
|
+
export function configuredPluginSpecs(config) {
|
|
63
|
+
return configuredOperations(config).flatMap(operation => operation.type === 'add' && !isSelector(operation.target) ? [operation.target] : []);
|
|
64
|
+
}
|
|
65
|
+
/** Whether a target selects plugins by id pattern rather than naming one: `*`, `<prefix>.*`, or a built-in `opencode.` id. */
|
|
66
|
+
function isSelector(target) {
|
|
67
|
+
return target === '*' || target.endsWith('.*') || target.startsWith('opencode.');
|
|
68
|
+
}
|
|
69
|
+
/** Whether a selector matches a plugin id: `*` any, `<prefix>.*` ids starting with `<prefix>.`, else the id itself. */
|
|
70
|
+
function matches(selector, id) {
|
|
71
|
+
if (selector === '*')
|
|
72
|
+
return true;
|
|
73
|
+
return selector.endsWith('.*') ? id.startsWith(selector.slice(0, -1)) : selector === id;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Every plugin `location` loads, each with whether OpenCode runs it, resolved the way OpenCode resolves them
|
|
77
|
+
* (`scan` in `packages/core/src/config/plugin/source.ts` and `resolve` in `packages/core/src/plugin/supervisor.ts`
|
|
78
|
+
* of https://github.com/anomalyco/opencode; documented at https://opencode.ai/docs/plugins/#control):
|
|
79
|
+
*
|
|
80
|
+
* - OpenCode reads its config directories lowest precedence first: the global one, then a project's (`inherited`
|
|
81
|
+
* are those it reads before `location`). Their plugins folders come first, then the entries of their config
|
|
82
|
+
* files, in order ("Plugin arrays from applicable config files are applied from lowest to highest precedence").
|
|
83
|
+
* - Each entry is processed in order. `-<id>` switches off the plugins loaded so far whose id it matches; `*`
|
|
84
|
+
* matches every id, `<prefix>.*` the ids starting with `<prefix>.`. An entry that matches the id of a plugin
|
|
85
|
+
* loaded so far, or is a selector, switches the matching plugins on again ("A later ID re-enables a plugin");
|
|
86
|
+
* any other entry loads a plugin, on.
|
|
87
|
+
* - A plugin's id is what its server part declares ({@link declaredPluginId}). Whether an entry applies to a plugin
|
|
88
|
+
* whose id cannot be told is not guessed: `*` and `-*` apply to every plugin, but after any other entry that
|
|
89
|
+
* could switch it, by id or by loading a plugin that could share its id, its state is unknown, with why.
|
|
90
|
+
*
|
|
91
|
+
* Only `location`'s own plugins are returned. A configured entry that cannot be resolved is skipped, with a warning
|
|
92
|
+
* when it is `location`'s.
|
|
93
|
+
*/
|
|
94
|
+
export function openCodeInventory(location, inherited = []) {
|
|
95
|
+
const loaded = new Map();
|
|
96
|
+
const warnings = [];
|
|
97
|
+
const unread = [];
|
|
98
|
+
const on = { enabled: true };
|
|
99
|
+
const off = { enabled: false };
|
|
100
|
+
/** An entry that might switch `plugin` to `to`, as `because` says: it does not change a state already `to`. */
|
|
101
|
+
const perhaps = (plugin, to, because) => {
|
|
102
|
+
if (plugin.state.enabled !== to)
|
|
103
|
+
plugin.state = { enabled: undefined, unknownBecause: because };
|
|
104
|
+
};
|
|
105
|
+
/** Switch the plugins `target` selects to `to`: surely those whose id it matches, perhaps those whose id is not known. */
|
|
106
|
+
const select = (target, to) => {
|
|
107
|
+
const selected = [];
|
|
108
|
+
for (const plugin of loaded.values()) {
|
|
109
|
+
if ('id' in plugin.id) {
|
|
110
|
+
if (!matches(target, plugin.id.id))
|
|
111
|
+
continue;
|
|
112
|
+
plugin.state = to ? on : off;
|
|
113
|
+
selected.push(plugin);
|
|
114
|
+
}
|
|
115
|
+
else if (target === '*')
|
|
116
|
+
plugin.state = to ? on : off;
|
|
117
|
+
else
|
|
118
|
+
perhaps(plugin, to, plugin.id.problem);
|
|
119
|
+
}
|
|
120
|
+
return selected;
|
|
121
|
+
};
|
|
122
|
+
const load = (entry, owner) => {
|
|
123
|
+
const key = entry.kind === 'file' ? entry.path : entry.kind === 'package' ? entry.root : `spec:${entry.spec}`;
|
|
124
|
+
const plugin = loaded.get(key) ?? { entry, id: pluginIdOf(entry), owner, state: on };
|
|
125
|
+
loaded.set(key, plugin);
|
|
126
|
+
plugin.state = on;
|
|
127
|
+
// Loading a plugin switches on its id: every plugin that shares it, and perhaps one whose id is not known.
|
|
128
|
+
for (const other of loaded.values()) {
|
|
129
|
+
if (other === plugin)
|
|
130
|
+
continue;
|
|
131
|
+
const problem = 'id' in other.id ? ('id' in plugin.id ? undefined : plugin.id.problem) : other.id.problem;
|
|
132
|
+
if (problem !== undefined)
|
|
133
|
+
perhaps(other, true, problem);
|
|
134
|
+
else if (idOf(other) === idOf(plugin))
|
|
135
|
+
other.state = on;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const idOf = (plugin) => 'id' in plugin.id ? plugin.id.id : undefined;
|
|
139
|
+
const chain = [...inherited, location];
|
|
140
|
+
for (const owner of chain)
|
|
141
|
+
for (const entry of folderPlugins(owner.configDir, unread))
|
|
142
|
+
load(entry, owner);
|
|
143
|
+
for (const owner of chain) {
|
|
144
|
+
for (const file of owner.configFiles) {
|
|
145
|
+
const config = readOpenCodeConfigFile(file);
|
|
146
|
+
if (config === undefined) {
|
|
147
|
+
// Listed because it exists: it does not parse (OpenCode may be writing it). What it configures is not known.
|
|
148
|
+
const problem = `${file} could not be read as OpenCode configuration; its plugin entries are not known`;
|
|
149
|
+
unread.push(problem);
|
|
150
|
+
if (owner === location)
|
|
151
|
+
warnings.push(problem);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
for (const operation of configuredOperations(config)) {
|
|
155
|
+
if (operation.type === 'remove') {
|
|
156
|
+
select(operation.target, false);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const known = [...loaded.values()].some(plugin => 'id' in plugin.id && matches(operation.target, plugin.id.id));
|
|
160
|
+
if (known || isSelector(operation.target)) {
|
|
161
|
+
select(operation.target, true);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
load(resolveSpec(operation.target, dirname(file), owner), owner);
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
// Reported once, by the listing of the config directory whose entry it is.
|
|
169
|
+
if (owner === location)
|
|
170
|
+
warnings.push(`OpenCode plugin entry "${operation.target}" in ${file} cannot be resolved and is skipped: ${error instanceof Error ? error.message : String(error)}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const entries = [...loaded.values()]
|
|
176
|
+
.filter(plugin => plugin.owner === location)
|
|
177
|
+
.map(plugin => ({ ...plugin.entry, ...plugin.state }));
|
|
178
|
+
return { entries, warnings, unread };
|
|
179
|
+
}
|
|
180
|
+
/** The plugins right inside a config directory's plugins folders: files, and package directories with an entry. */
|
|
181
|
+
function folderPlugins(configDir, unread) {
|
|
182
|
+
const out = [];
|
|
183
|
+
for (const folder of OPENCODE_PLUGIN_FOLDERS) {
|
|
184
|
+
const dir = join(configDir, folder);
|
|
185
|
+
let names;
|
|
186
|
+
try {
|
|
187
|
+
names = readdirSync(dir);
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
// No such folder holds no plugins; one that cannot be listed holds unknown ones.
|
|
191
|
+
const code = error.code;
|
|
192
|
+
if (code !== 'ENOENT' && code !== 'ENOTDIR')
|
|
193
|
+
unread.push(`${dir} could not be listed (${error instanceof Error ? error.message : String(error)})`);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
for (const name of names.sort((a, b) => a.localeCompare(b, 'en'))) {
|
|
197
|
+
const path = join(dir, name);
|
|
198
|
+
if (isFile(path) && PLUGIN_FILE.test(name))
|
|
199
|
+
out.push({ kind: 'file', path });
|
|
200
|
+
else if (isDirectory(path)) {
|
|
201
|
+
const entryPath = pluginEntryOf(path);
|
|
202
|
+
if (entryPath !== undefined)
|
|
203
|
+
out.push({ kind: 'package', root: path, entryPath });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* A plugin's id: the one its server entry declares ({@link declaredPluginId}), else, for a package without a server
|
|
211
|
+
* entry, its package name (https://opencode.ai/docs/build/plugins: "A package with a GUI part and no server part
|
|
212
|
+
* takes its package name (or a local directory's name) as its `id`"). An entry with nothing on disk declares none.
|
|
213
|
+
*/
|
|
214
|
+
function pluginIdOf(entry) {
|
|
215
|
+
if (entry.kind === 'unresolved')
|
|
216
|
+
return { problem: 'missing' };
|
|
217
|
+
const entryPath = entry.kind === 'file' ? entry.path : entry.entryPath;
|
|
218
|
+
if (entryPath !== undefined)
|
|
219
|
+
return declaredPluginId(readText(entryPath) ?? '');
|
|
220
|
+
if (entry.kind !== 'package')
|
|
221
|
+
return { problem: 'missing' };
|
|
222
|
+
const name = readJsonObject(join(entry.root, 'package.json'))?.['name'];
|
|
223
|
+
return { id: typeof name === 'string' ? name : basename(entry.root) };
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* A configured spec, resolved as OpenCode resolves it. Throws for a spec no path can be made of (a `file:` URL with
|
|
227
|
+
* a host, outside Windows) and for one that leads to a network share the configuration is not on: on Windows,
|
|
228
|
+
* reading it would sign in to that server.
|
|
229
|
+
*/
|
|
230
|
+
function resolveSpec(spec, base, location) {
|
|
231
|
+
const local = spec.startsWith('file:') ? fileURLToPath(spec.startsWith('file://') ? spec : `file://${resolve(base, spec.slice('file:'.length))}`)
|
|
232
|
+
: spec.startsWith('.') || spec.startsWith('/') || /^[A-Za-z]:[\\/]/u.test(spec) ? resolve(base, spec) : undefined;
|
|
233
|
+
if (local !== undefined && (reachesNewShare(spec, base) || contentPath(local, base) === undefined))
|
|
234
|
+
throw new Error(`${spec} is on a network share; Harness Forge does not read plugins from one`);
|
|
235
|
+
if (local !== undefined)
|
|
236
|
+
return isDirectory(local) ? packageAt(local, spec) : { kind: 'unresolved', spec, candidates: [local] };
|
|
237
|
+
const name = packageName(spec);
|
|
238
|
+
if (name === undefined)
|
|
239
|
+
return { kind: 'unresolved', spec };
|
|
240
|
+
const candidates = [location.configDir, location.cacheDir]
|
|
241
|
+
.filter((dir) => dir !== undefined)
|
|
242
|
+
.map(dir => join(dir, 'node_modules', ...name.split('/')));
|
|
243
|
+
const installed = candidates.find(dir => isFile(join(dir, 'package.json')));
|
|
244
|
+
return installed !== undefined ? packageAt(installed, spec) : { kind: 'unresolved', spec, candidates };
|
|
245
|
+
}
|
|
246
|
+
function packageAt(root, spec) {
|
|
247
|
+
const entryPath = pluginEntryOf(root);
|
|
248
|
+
return { kind: 'package', root, spec, ...(entryPath !== undefined ? { entryPath } : {}) };
|
|
249
|
+
}
|
|
250
|
+
/** The npm package name of a spec (`name`, `name@1.2.0`, `@scope/name@^1`); undefined for git and URL specs. */
|
|
251
|
+
export function packageName(spec) {
|
|
252
|
+
if (/^[a-z][a-z0-9+.-]*:/iu.test(spec))
|
|
253
|
+
return undefined;
|
|
254
|
+
const match = /^((?:@[^/@\s]+\/)?[^/@\s]+)(?:@[^\s]*)?$/u.exec(spec);
|
|
255
|
+
return match?.[1];
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* A plugin package's server entry, as OpenCode loads it: `exports` `./server`
|
|
259
|
+
* or `.`, else `main`/`module`, else `server` or `index` at its root. A
|
|
260
|
+
* source checkout whose build output is absent runs from its TypeScript
|
|
261
|
+
* source (`dist/x.js` → `src/x.ts` or `x.ts`), as Bun runs it.
|
|
262
|
+
*/
|
|
263
|
+
export function pluginEntryOf(directory) {
|
|
264
|
+
const packageJson = readJsonObject(join(directory, 'package.json'));
|
|
265
|
+
const targets = [];
|
|
266
|
+
const exportsField = packageJson?.['exports'];
|
|
267
|
+
const conditional = (value) => {
|
|
268
|
+
if (typeof value === 'string')
|
|
269
|
+
return value;
|
|
270
|
+
if (!isPlainObject(value))
|
|
271
|
+
return undefined;
|
|
272
|
+
for (const key of ['bun', 'import', 'default', 'node', 'require']) {
|
|
273
|
+
const found = conditional(value[key]);
|
|
274
|
+
if (found !== undefined)
|
|
275
|
+
return found;
|
|
276
|
+
}
|
|
277
|
+
return undefined;
|
|
278
|
+
};
|
|
279
|
+
if (typeof exportsField === 'string')
|
|
280
|
+
targets.push(exportsField);
|
|
281
|
+
else if (isPlainObject(exportsField)) {
|
|
282
|
+
for (const key of ['./server', '.']) {
|
|
283
|
+
const target = conditional(exportsField[key]);
|
|
284
|
+
if (target !== undefined)
|
|
285
|
+
targets.push(target);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
for (const key of ['main', 'module'])
|
|
289
|
+
if (typeof packageJson?.[key] === 'string')
|
|
290
|
+
targets.push(packageJson[key]);
|
|
291
|
+
for (const target of targets) {
|
|
292
|
+
// A field of the package's own `package.json`: checked against the package's folder (see `contentPath`).
|
|
293
|
+
const resolved = contentPath(target, directory);
|
|
294
|
+
if (resolved === undefined)
|
|
295
|
+
continue;
|
|
296
|
+
if (isFile(resolved))
|
|
297
|
+
return resolved;
|
|
298
|
+
const source = sourceOf(directory, target);
|
|
299
|
+
if (source !== undefined)
|
|
300
|
+
return source;
|
|
301
|
+
}
|
|
302
|
+
for (const name of ['server.ts', 'server.tsx', 'server.js', 'index.ts', 'index.tsx', 'index.js', 'index.mjs']) {
|
|
303
|
+
const candidate = join(directory, name);
|
|
304
|
+
if (isFile(candidate))
|
|
305
|
+
return candidate;
|
|
306
|
+
}
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
/** The TypeScript source of a build output path, when the checkout has it. */
|
|
310
|
+
function sourceOf(directory, target) {
|
|
311
|
+
const relative = target.replace(/^\.\//u, '').replace(/^(dist|build|lib|out)\//u, '').replace(/\.(m|c)?js$/u, '');
|
|
312
|
+
for (const candidate of [`src/${relative}.ts`, `src/${relative}.tsx`, `${relative}.ts`, `${relative}.tsx`]) {
|
|
313
|
+
const path = join(directory, candidate);
|
|
314
|
+
if (isFile(path))
|
|
315
|
+
return path;
|
|
316
|
+
}
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The id an OpenCode plugin declares, read from its source without running it. OpenCode takes a plugin's id from
|
|
3
|
+
* its server part's `Plugin.define({ id })` (https://opencode.ai/docs/build/plugins: "The plugin's `id` comes from
|
|
4
|
+
* its server part's `Plugin.define`"; every server and Effect example declares it so). The source is cut into
|
|
5
|
+
* tokens by a small linear scanner that steps over comments, strings, template literals and regular expressions,
|
|
6
|
+
* so text that only looks like a declaration is not one.
|
|
7
|
+
* @module @kirkchinese/harness-forge-core
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Why a plugin's id cannot be told: no declaration, more than one, one whose id is not a plain string literal, or
|
|
11
|
+
* one whose literal has an escape JavaScript refuses in a module (a legacy octal, a malformed `\x` or `\u`).
|
|
12
|
+
*/
|
|
13
|
+
export type PluginIdProblem = 'missing' | 'ambiguous' | 'dynamic' | 'escape';
|
|
14
|
+
/** The id a plugin declares, or why it cannot be told. */
|
|
15
|
+
export type PluginIdRead = {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
} | {
|
|
18
|
+
readonly problem: PluginIdProblem;
|
|
19
|
+
};
|
|
20
|
+
/** The declared id of a plugin's source: one `Plugin.define({ id: "<literal>" })`, else the problem. */
|
|
21
|
+
export declare function declaredPluginId(source: string): PluginIdRead;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The id an OpenCode plugin declares, read from its source without running it. OpenCode takes a plugin's id from
|
|
3
|
+
* its server part's `Plugin.define({ id })` (https://opencode.ai/docs/build/plugins: "The plugin's `id` comes from
|
|
4
|
+
* its server part's `Plugin.define`"; every server and Effect example declares it so). The source is cut into
|
|
5
|
+
* tokens by a small linear scanner that steps over comments, strings, template literals and regular expressions,
|
|
6
|
+
* so text that only looks like a declaration is not one.
|
|
7
|
+
* @module @kirkchinese/harness-forge-core
|
|
8
|
+
*/
|
|
9
|
+
/** The value of a name or literal whose escape JavaScript would refuse: the id it is part of cannot be told. */
|
|
10
|
+
const BAD_ESCAPE = Symbol('bad escape');
|
|
11
|
+
/** The single-character escapes of a string literal (ECMA-262 `SingleEscapeCharacter`), and what they stand for. */
|
|
12
|
+
const SINGLE_ESCAPES = { b: '\b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', "'": "'", '"': '"', '\\': '\\', '`': '`' };
|
|
13
|
+
/** The line terminators a backslash continues a literal over (ECMA-262 `LineContinuation`); CRLF is one. */
|
|
14
|
+
const LINE_TERMINATORS = new Set(['\n', '\r', '\u2028', '\u2029']);
|
|
15
|
+
/** The largest code point `\u{…}` may name. */
|
|
16
|
+
const MAX_CODE_POINT = 0x10ffff;
|
|
17
|
+
/** Keywords after which a `/` starts a regular expression rather than a division. */
|
|
18
|
+
const REGEX_AFTER_KEYWORDS = new Set(['return', 'typeof', 'case', 'do', 'else', 'in', 'instanceof', 'new', 'delete', 'void', 'throw', 'yield', 'await', 'of']);
|
|
19
|
+
const NAME_START = /[A-Za-z_$\u0080-\uffff]/u;
|
|
20
|
+
const NAME_PART = /[\w$\u0080-\uffff]/u;
|
|
21
|
+
/** The declared id of a plugin's source: one `Plugin.define({ id: "<literal>" })`, else the problem. */
|
|
22
|
+
export function declaredPluginId(source) {
|
|
23
|
+
const tokens = tokenize(source);
|
|
24
|
+
const reads = [];
|
|
25
|
+
for (let at = 0; at + 3 < tokens.length; at++) {
|
|
26
|
+
if (!isName(tokens[at], 'Plugin') || !isPunct(tokens[at + 1], '.') || !isName(tokens[at + 2], 'define') || !isPunct(tokens[at + 3], '('))
|
|
27
|
+
continue;
|
|
28
|
+
reads.push(isPunct(tokens[at + 4], '{') ? idOfObject(tokens, at + 5) : { problem: 'dynamic' });
|
|
29
|
+
}
|
|
30
|
+
if (reads.length === 0)
|
|
31
|
+
return { problem: 'missing' };
|
|
32
|
+
if (reads.length > 1)
|
|
33
|
+
return { problem: 'ambiguous' };
|
|
34
|
+
return reads[0];
|
|
35
|
+
}
|
|
36
|
+
/** The `id` of an object literal whose properties start at `start`, only its own top-level properties read. */
|
|
37
|
+
function idOfObject(tokens, start) {
|
|
38
|
+
const ids = [];
|
|
39
|
+
let depth = 0;
|
|
40
|
+
let propertyStart = true;
|
|
41
|
+
for (let at = start; at < tokens.length; at++) {
|
|
42
|
+
const token = tokens[at];
|
|
43
|
+
if (depth === 0 && propertyStart) {
|
|
44
|
+
propertyStart = false;
|
|
45
|
+
// A spread or a computed key may bring an id that cannot be read here.
|
|
46
|
+
if (isPunct(token, '...') || isPunct(token, '['))
|
|
47
|
+
ids.push({ problem: 'dynamic' });
|
|
48
|
+
// A key whose escape cannot be decoded may be `id`.
|
|
49
|
+
else if ((token.type === 'name' || token.type === 'string') && token.value === BAD_ESCAPE)
|
|
50
|
+
ids.push({ problem: 'escape' });
|
|
51
|
+
else if ((token.type === 'name' || token.type === 'string') && token.value === 'id') {
|
|
52
|
+
const next = tokens[at + 1];
|
|
53
|
+
const value = tokens[at + 2];
|
|
54
|
+
const after = tokens[at + 3];
|
|
55
|
+
if (!isPunct(next, ':') || value?.type !== 'string' || value.value === undefined || !(isPunct(after, ',') || isPunct(after, '}')))
|
|
56
|
+
ids.push({ problem: 'dynamic' });
|
|
57
|
+
else
|
|
58
|
+
ids.push(value.value === BAD_ESCAPE ? { problem: 'escape' } : { id: value.value });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (token.type !== 'punct')
|
|
62
|
+
continue;
|
|
63
|
+
if (token.value === '{' || token.value === '(' || token.value === '[')
|
|
64
|
+
depth++;
|
|
65
|
+
else if (token.value === '}' || token.value === ')' || token.value === ']') {
|
|
66
|
+
if (depth === 0)
|
|
67
|
+
break;
|
|
68
|
+
depth--;
|
|
69
|
+
}
|
|
70
|
+
else if (token.value === ',' && depth === 0)
|
|
71
|
+
propertyStart = true;
|
|
72
|
+
}
|
|
73
|
+
if (ids.length === 0)
|
|
74
|
+
return { problem: 'missing' };
|
|
75
|
+
if (ids.length > 1)
|
|
76
|
+
return { problem: 'ambiguous' };
|
|
77
|
+
return ids[0];
|
|
78
|
+
}
|
|
79
|
+
function isName(token, value) {
|
|
80
|
+
return token?.type === 'name' && token.value === value;
|
|
81
|
+
}
|
|
82
|
+
function isPunct(token, value) {
|
|
83
|
+
return token?.type === 'punct' && token.value === value;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The tokens of JavaScript or TypeScript source, in one pass: names, punctuation, and string and template literals
|
|
87
|
+
* with their values; comments and regular expressions dropped. The code inside a template's `${…}` is tokenized
|
|
88
|
+
* like the rest.
|
|
89
|
+
*/
|
|
90
|
+
function tokenize(source) {
|
|
91
|
+
const tokens = [];
|
|
92
|
+
/** For each template whose `${…}` is open, the brace depth within it. */
|
|
93
|
+
const templates = [];
|
|
94
|
+
let at = 0;
|
|
95
|
+
const regexAllowed = () => {
|
|
96
|
+
const last = tokens[tokens.length - 1];
|
|
97
|
+
if (last === undefined)
|
|
98
|
+
return true;
|
|
99
|
+
if (last.type === 'name')
|
|
100
|
+
return typeof last.value === 'string' && REGEX_AFTER_KEYWORDS.has(last.value);
|
|
101
|
+
if (last.type === 'string')
|
|
102
|
+
return false;
|
|
103
|
+
return last.value !== ')' && last.value !== ']' && last.value !== '}';
|
|
104
|
+
};
|
|
105
|
+
/** The source text of an escape starting at `at` (the backslash): two characters, three for a backslash before CRLF. */
|
|
106
|
+
const escapeAt = () => {
|
|
107
|
+
const length = source[at + 1] === '\r' && source[at + 2] === '\n' ? 3 : 2;
|
|
108
|
+
const raw = source.slice(at, at + length);
|
|
109
|
+
at += length;
|
|
110
|
+
return raw;
|
|
111
|
+
};
|
|
112
|
+
/** Read a template's text from `at` (after the backtick or a closing `}`) to its end or its next `${`, decoded. */
|
|
113
|
+
const templateText = () => {
|
|
114
|
+
let raw = '';
|
|
115
|
+
while (at < source.length) {
|
|
116
|
+
const char = source[at];
|
|
117
|
+
if (char === '\\')
|
|
118
|
+
raw += escapeAt();
|
|
119
|
+
else if (char === '`') {
|
|
120
|
+
at++;
|
|
121
|
+
return { text: decodeLiteral(raw, true), substitution: false };
|
|
122
|
+
}
|
|
123
|
+
else if (char === '$' && source[at + 1] === '{') {
|
|
124
|
+
at += 2;
|
|
125
|
+
templates.push(0);
|
|
126
|
+
return { text: decodeLiteral(raw, true), substitution: true };
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
raw += char;
|
|
130
|
+
at++;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return { text: decodeLiteral(raw, true), substitution: false };
|
|
134
|
+
};
|
|
135
|
+
while (at < source.length) {
|
|
136
|
+
const char = source[at];
|
|
137
|
+
const next = source[at + 1];
|
|
138
|
+
if (/\s/u.test(char)) {
|
|
139
|
+
at++;
|
|
140
|
+
}
|
|
141
|
+
else if (char === '/' && next === '/') {
|
|
142
|
+
while (at < source.length && source[at] !== '\n')
|
|
143
|
+
at++;
|
|
144
|
+
}
|
|
145
|
+
else if (char === '/' && next === '*') {
|
|
146
|
+
const end = source.indexOf('*/', at + 2);
|
|
147
|
+
at = end === -1 ? source.length : end + 2;
|
|
148
|
+
}
|
|
149
|
+
else if (char === '"' || char === "'") {
|
|
150
|
+
let raw = '';
|
|
151
|
+
at++;
|
|
152
|
+
while (at < source.length && source[at] !== char && source[at] !== '\n') {
|
|
153
|
+
if (source[at] === '\\')
|
|
154
|
+
raw += escapeAt();
|
|
155
|
+
else
|
|
156
|
+
raw += source[at++];
|
|
157
|
+
}
|
|
158
|
+
at++;
|
|
159
|
+
tokens.push({ type: 'string', value: decodeLiteral(raw, false) });
|
|
160
|
+
}
|
|
161
|
+
else if (char === '`') {
|
|
162
|
+
at++;
|
|
163
|
+
const part = templateText();
|
|
164
|
+
tokens.push({ type: 'string', value: part.substitution ? undefined : part.text });
|
|
165
|
+
}
|
|
166
|
+
else if (char === '}' && templates.length > 0 && templates[templates.length - 1] === 0) {
|
|
167
|
+
// The end of a template's `${…}`: its text goes on.
|
|
168
|
+
templates.pop();
|
|
169
|
+
at++;
|
|
170
|
+
templateText();
|
|
171
|
+
}
|
|
172
|
+
else if (char === '/' && regexAllowed()) {
|
|
173
|
+
at++;
|
|
174
|
+
let inClass = false;
|
|
175
|
+
while (at < source.length && source[at] !== '\n') {
|
|
176
|
+
const current = source[at];
|
|
177
|
+
if (current === '\\')
|
|
178
|
+
at += 2;
|
|
179
|
+
else {
|
|
180
|
+
at++;
|
|
181
|
+
if (current === '[')
|
|
182
|
+
inClass = true;
|
|
183
|
+
else if (current === ']')
|
|
184
|
+
inClass = false;
|
|
185
|
+
else if (current === '/' && !inClass)
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
while (at < source.length && NAME_PART.test(source[at]))
|
|
190
|
+
at++;
|
|
191
|
+
}
|
|
192
|
+
else if (NAME_START.test(char) || (char === '\\' && next === 'u')) {
|
|
193
|
+
// A name may spell a character as `\uXXXX` or `\u{…}`: `i\u0064` is `id`.
|
|
194
|
+
let raw = '';
|
|
195
|
+
while (at < source.length && (NAME_PART.test(source[at]) || (source[at] === '\\' && source[at + 1] === 'u'))) {
|
|
196
|
+
if (source[at] === '\\') {
|
|
197
|
+
const end = source[at + 2] === '{' ? source.indexOf('}', at) + 1 : at + 6;
|
|
198
|
+
raw += source.slice(at, end > at ? end : source.length);
|
|
199
|
+
at = end > at ? end : source.length;
|
|
200
|
+
}
|
|
201
|
+
else
|
|
202
|
+
raw += source[at++];
|
|
203
|
+
}
|
|
204
|
+
tokens.push({ type: 'name', value: decodeLiteral(raw, false) });
|
|
205
|
+
}
|
|
206
|
+
else if (/\d/u.test(char)) {
|
|
207
|
+
while (at < source.length && /[\w.]/u.test(source[at]))
|
|
208
|
+
at++;
|
|
209
|
+
// A number: like a name, no regular expression follows it.
|
|
210
|
+
tokens.push({ type: 'name', value: '0' });
|
|
211
|
+
}
|
|
212
|
+
else if (source.startsWith('...', at)) {
|
|
213
|
+
at += 3;
|
|
214
|
+
tokens.push({ type: 'punct', value: '...' });
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
if (templates.length > 0) {
|
|
218
|
+
if (char === '{')
|
|
219
|
+
templates[templates.length - 1]++;
|
|
220
|
+
else if (char === '}')
|
|
221
|
+
templates[templates.length - 1]--;
|
|
222
|
+
}
|
|
223
|
+
at++;
|
|
224
|
+
tokens.push({ type: 'punct', value: char });
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return tokens;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* The value of a string or template literal's text between its quotes, as JavaScript reads it in a module (strict
|
|
231
|
+
* code, ECMA-262 "String Literals" and "Template Literal Lexical Components"): `\xHH`, `\uXXXX`, `\u{…}`, the
|
|
232
|
+
* single-character escapes, `\0` not before a digit, line continuations, and any other character standing for
|
|
233
|
+
* itself. A legacy octal (`\1`, `\01`), `\8`, `\9`, or a malformed `\x` or `\u` is refused: {@link BAD_ESCAPE}.
|
|
234
|
+
* In a template, a raw CR or CRLF reads as LF.
|
|
235
|
+
*/
|
|
236
|
+
function decodeLiteral(raw, template) {
|
|
237
|
+
let out = '';
|
|
238
|
+
for (let at = 0; at < raw.length;) {
|
|
239
|
+
const char = raw[at];
|
|
240
|
+
if (char !== '\\') {
|
|
241
|
+
if (template && char === '\r') {
|
|
242
|
+
out += '\n';
|
|
243
|
+
at += raw[at + 1] === '\n' ? 2 : 1;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
out += char;
|
|
247
|
+
at++;
|
|
248
|
+
}
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const next = raw[at + 1];
|
|
252
|
+
if (next === undefined)
|
|
253
|
+
return BAD_ESCAPE;
|
|
254
|
+
if (LINE_TERMINATORS.has(next)) {
|
|
255
|
+
at += next === '\r' && raw[at + 2] === '\n' ? 3 : 2;
|
|
256
|
+
}
|
|
257
|
+
else if (next in SINGLE_ESCAPES) {
|
|
258
|
+
out += SINGLE_ESCAPES[next];
|
|
259
|
+
at += 2;
|
|
260
|
+
}
|
|
261
|
+
else if (next === '0' && !/\d/u.test(raw[at + 2] ?? '')) {
|
|
262
|
+
out += '\0';
|
|
263
|
+
at += 2;
|
|
264
|
+
}
|
|
265
|
+
else if (/\d/u.test(next)) {
|
|
266
|
+
return BAD_ESCAPE;
|
|
267
|
+
}
|
|
268
|
+
else if (next === 'x') {
|
|
269
|
+
const hex = raw.slice(at + 2, at + 4);
|
|
270
|
+
if (!/^[\da-f]{2}$/iu.test(hex))
|
|
271
|
+
return BAD_ESCAPE;
|
|
272
|
+
out += String.fromCharCode(Number.parseInt(hex, 16));
|
|
273
|
+
at += 4;
|
|
274
|
+
}
|
|
275
|
+
else if (next === 'u' && raw[at + 2] === '{') {
|
|
276
|
+
const end = raw.indexOf('}', at + 3);
|
|
277
|
+
const hex = end === -1 ? '' : raw.slice(at + 3, end);
|
|
278
|
+
if (!/^[\da-f]+$/iu.test(hex) || Number.parseInt(hex, 16) > MAX_CODE_POINT)
|
|
279
|
+
return BAD_ESCAPE;
|
|
280
|
+
out += String.fromCodePoint(Number.parseInt(hex, 16));
|
|
281
|
+
at = end + 1;
|
|
282
|
+
}
|
|
283
|
+
else if (next === 'u') {
|
|
284
|
+
const hex = raw.slice(at + 2, at + 6);
|
|
285
|
+
if (!/^[\da-f]{4}$/iu.test(hex))
|
|
286
|
+
return BAD_ESCAPE;
|
|
287
|
+
out += String.fromCharCode(Number.parseInt(hex, 16));
|
|
288
|
+
at += 6;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
out += next;
|
|
292
|
+
at += 2;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type OpenCodeLocation } from './inventory.js';
|
|
2
|
+
import type { UserEnablement } from '../types.js';
|
|
1
3
|
import type { ForeignPlugin, HookComponent } from '../../types.js';
|
|
2
4
|
/** Strip JSONC comments and trailing commas without touching string literals. */
|
|
3
5
|
export declare function stripJsonc(text: string): string;
|
|
@@ -13,5 +15,25 @@ export declare function scanOpenCodeProject(root: string, options?: {
|
|
|
13
15
|
plugin: ForeignPlugin;
|
|
14
16
|
warnings: string[];
|
|
15
17
|
};
|
|
18
|
+
/** OpenCode's global config directory (https://opencode.ai/docs/plugins/: "Global plugins use the same discovery layout under the OpenCode config directory"). */
|
|
19
|
+
export declare function openCodeConfigDir(home: string): string;
|
|
20
|
+
/** OpenCode's package cache, where V1 installed npm plugins (https://opencode.ai/v1/docs/plugins.md: "cached in ~/.cache/opencode/node_modules/"). */
|
|
21
|
+
export declare function openCodeCacheDir(home: string): string;
|
|
22
|
+
/** Where OpenCode keeps a global config directory's plugins, its npm cache included. */
|
|
23
|
+
export declare function openCodeGlobalLocation(home: string): OpenCodeLocation;
|
|
24
|
+
/**
|
|
25
|
+
* Whether OpenCode runs the person's plugin files: on when it surely runs one; else not known when it may run one
|
|
26
|
+
* (why, from the first such file), else off when its config switches off every one.
|
|
27
|
+
*/
|
|
28
|
+
export declare function openCodeUserEnablement(home: string): UserEnablement;
|
|
29
|
+
/**
|
|
30
|
+
* The plugin files a person keeps right in OpenCode's global plugins folder, as one plugin `opencode:user`
|
|
31
|
+
* rooted at the config directory (where OpenCode installs their dependencies). Package directories and
|
|
32
|
+
* configured packages are plugins of their own (`harnessPlugins`). When OpenCode surely runs some files, the others
|
|
33
|
+
* (switched off, or not known to run) are left out, with why; else the plugin holds them all, and is off or not
|
|
34
|
+
* known to run ({@link openCodeUserEnablement}).
|
|
35
|
+
* @returns undefined when there is no such file.
|
|
36
|
+
*/
|
|
37
|
+
export declare function scanOpenCodeUser(home: string): ForeignPlugin | undefined;
|
|
16
38
|
/** A hook component placeholder for OpenCode events, when one is reported. */
|
|
17
39
|
export declare function openCodeHooks(id: string, events: readonly string[]): HookComponent;
|