@tagma/sdk 0.5.2 → 0.6.1
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 +573 -573
- package/dist/drivers/opencode.d.ts.map +1 -1
- package/dist/drivers/opencode.js +47 -17
- package/dist/drivers/opencode.js.map +1 -1
- package/package.json +2 -2
- package/scripts/preinstall.js +31 -31
- package/src/adapters/stdin-approval.ts +106 -106
- package/src/adapters/websocket-approval.ts +224 -224
- package/src/approval.ts +131 -131
- package/src/bootstrap.ts +37 -37
- package/src/completions/exit-code.ts +34 -34
- package/src/completions/file-exists.ts +66 -66
- package/src/completions/output-check.ts +92 -92
- package/src/config-ops.ts +307 -307
- package/src/drivers/opencode.ts +68 -29
- package/src/engine.ts +1220 -1220
- package/src/hooks.ts +193 -193
- package/src/logger.ts +182 -182
- package/src/middlewares/static-context.ts +49 -49
- package/src/pipeline-runner.ts +173 -173
- package/src/registry.ts +267 -267
- package/src/runner.ts +460 -460
- package/src/schema.test.ts +101 -101
- package/src/schema.ts +338 -338
- package/src/sdk.ts +118 -118
- package/src/triggers/file.ts +164 -164
- package/src/triggers/manual.ts +86 -86
- package/src/types.ts +18 -18
- package/src/utils.ts +203 -203
- package/src/validate-raw.ts +412 -412
- package/dist/drivers/claude-code.d.ts +0 -3
- package/dist/drivers/claude-code.d.ts.map +0 -1
- package/dist/drivers/claude-code.js +0 -225
- package/dist/drivers/claude-code.js.map +0 -1
- package/dist/templates.d.ts +0 -20
- package/dist/templates.d.ts.map +0 -1
- package/dist/templates.js +0 -93
- package/dist/templates.js.map +0 -1
package/src/registry.ts
CHANGED
|
@@ -1,267 +1,267 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';
|
|
2
|
-
import { pathToFileURL } from 'node:url';
|
|
3
|
-
import type {
|
|
4
|
-
PluginCategory,
|
|
5
|
-
DriverPlugin,
|
|
6
|
-
TriggerPlugin,
|
|
7
|
-
CompletionPlugin,
|
|
8
|
-
MiddlewarePlugin,
|
|
9
|
-
PluginManifest,
|
|
10
|
-
} from './types';
|
|
11
|
-
|
|
12
|
-
type PluginType = DriverPlugin | TriggerPlugin | CompletionPlugin | MiddlewarePlugin;
|
|
13
|
-
|
|
14
|
-
const VALID_CATEGORIES: ReadonlySet<PluginCategory> = new Set([
|
|
15
|
-
'drivers',
|
|
16
|
-
'triggers',
|
|
17
|
-
'completions',
|
|
18
|
-
'middlewares',
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
const registries = {
|
|
22
|
-
drivers: new Map<string, DriverPlugin>(),
|
|
23
|
-
triggers: new Map<string, TriggerPlugin>(),
|
|
24
|
-
completions: new Map<string, CompletionPlugin>(),
|
|
25
|
-
middlewares: new Map<string, MiddlewarePlugin>(),
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Minimal contract enforcement so a malformed plugin fails fast at
|
|
30
|
-
* registration time rather than crashing the engine mid-run.
|
|
31
|
-
*
|
|
32
|
-
* For drivers we materialize `capabilities` and assert each field is a
|
|
33
|
-
* boolean — otherwise a plugin author can write
|
|
34
|
-
* get capabilities() { throw new Error('boom') }
|
|
35
|
-
* and pass the basic typeof check, then crash preflight when the engine
|
|
36
|
-
* touches `driver.capabilities.sessionResume`. (R8)
|
|
37
|
-
*/
|
|
38
|
-
function validateContract(category: PluginCategory, handler: unknown): void {
|
|
39
|
-
if (!handler || typeof handler !== 'object') {
|
|
40
|
-
throw new Error(`Plugin handler for category "${category}" must be an object`);
|
|
41
|
-
}
|
|
42
|
-
const h = handler as Record<string, unknown>;
|
|
43
|
-
if (typeof h.name !== 'string' || h.name.length === 0) {
|
|
44
|
-
throw new Error(`Plugin handler for category "${category}" must declare a non-empty "name"`);
|
|
45
|
-
}
|
|
46
|
-
switch (category) {
|
|
47
|
-
case 'drivers': {
|
|
48
|
-
if (typeof h.buildCommand !== 'function') {
|
|
49
|
-
throw new Error(`drivers plugin "${h.name}" must export buildCommand()`);
|
|
50
|
-
}
|
|
51
|
-
// Materialize capabilities — this triggers any throwing getter NOW
|
|
52
|
-
// instead of during preflight.
|
|
53
|
-
let caps: unknown;
|
|
54
|
-
try {
|
|
55
|
-
caps = h.capabilities;
|
|
56
|
-
} catch (err) {
|
|
57
|
-
throw new Error(
|
|
58
|
-
`drivers plugin "${h.name}" capabilities accessor threw: ` +
|
|
59
|
-
(err instanceof Error ? err.message : String(err)),
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
if (!caps || typeof caps !== 'object') {
|
|
63
|
-
throw new Error(`drivers plugin "${h.name}" must declare capabilities object`);
|
|
64
|
-
}
|
|
65
|
-
const c = caps as Record<string, unknown>;
|
|
66
|
-
for (const field of ['sessionResume', 'systemPrompt', 'outputFormat'] as const) {
|
|
67
|
-
if (typeof c[field] !== 'boolean') {
|
|
68
|
-
throw new Error(
|
|
69
|
-
`drivers plugin "${h.name}".capabilities.${field} must be a boolean (got ${typeof c[field]})`,
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
// Optional methods, but if present must be functions.
|
|
74
|
-
for (const opt of ['parseResult', 'resolveModel', 'resolveTools'] as const) {
|
|
75
|
-
if (h[opt] !== undefined && typeof h[opt] !== 'function') {
|
|
76
|
-
throw new Error(`drivers plugin "${h.name}".${opt} must be a function or undefined`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
case 'triggers':
|
|
82
|
-
if (typeof h.watch !== 'function') {
|
|
83
|
-
throw new Error(`triggers plugin "${h.name}" must export watch()`);
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
86
|
-
case 'completions':
|
|
87
|
-
if (typeof h.check !== 'function') {
|
|
88
|
-
throw new Error(`completions plugin "${h.name}" must export check()`);
|
|
89
|
-
}
|
|
90
|
-
break;
|
|
91
|
-
case 'middlewares':
|
|
92
|
-
// A middleware must provide at least one entry point. `enhanceDoc` is
|
|
93
|
-
// the structured PromptDocument API (preferred); `enhance` is the
|
|
94
|
-
// legacy string-in/string-out API the engine still supports for
|
|
95
|
-
// v0.x plugins. Requiring only `enhance` here rejects every built-in
|
|
96
|
-
// and every plugin written against the current types.
|
|
97
|
-
if (typeof h.enhanceDoc !== 'function' && typeof h.enhance !== 'function') {
|
|
98
|
-
throw new Error(
|
|
99
|
-
`middlewares plugin "${h.name}" must export enhanceDoc() or enhance()`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
if (h.enhanceDoc !== undefined && typeof h.enhanceDoc !== 'function') {
|
|
103
|
-
throw new Error(`middlewares plugin "${h.name}".enhanceDoc must be a function or undefined`);
|
|
104
|
-
}
|
|
105
|
-
if (h.enhance !== undefined && typeof h.enhance !== 'function') {
|
|
106
|
-
throw new Error(`middlewares plugin "${h.name}".enhance must be a function or undefined`);
|
|
107
|
-
}
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export type RegisterResult = 'registered' | 'replaced' | 'unchanged';
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Register a plugin under (category, type). Returns:
|
|
116
|
-
* - 'registered' on first registration
|
|
117
|
-
* - 'replaced' when an existing entry was overwritten with a different handler
|
|
118
|
-
* - 'unchanged' when the same handler instance was already present
|
|
119
|
-
*
|
|
120
|
-
* Throws if `category` is unknown, `type` is empty, or `handler` violates the
|
|
121
|
-
* minimum interface contract for the category.
|
|
122
|
-
*/
|
|
123
|
-
export function registerPlugin<T extends PluginType>(
|
|
124
|
-
category: PluginCategory,
|
|
125
|
-
type: string,
|
|
126
|
-
handler: T,
|
|
127
|
-
): RegisterResult {
|
|
128
|
-
if (!VALID_CATEGORIES.has(category)) {
|
|
129
|
-
throw new Error(`Unknown plugin category "${category}"`);
|
|
130
|
-
}
|
|
131
|
-
if (typeof type !== 'string' || type.length === 0) {
|
|
132
|
-
throw new Error(`Plugin type must be a non-empty string (category="${category}")`);
|
|
133
|
-
}
|
|
134
|
-
validateContract(category, handler);
|
|
135
|
-
const registry = registries[category] as Map<string, T>;
|
|
136
|
-
const existing = registry.get(type);
|
|
137
|
-
if (existing === handler) return 'unchanged';
|
|
138
|
-
const wasReplaced = existing !== undefined;
|
|
139
|
-
registry.set(type, handler);
|
|
140
|
-
if (wasReplaced) {
|
|
141
|
-
// D18: surface silent shadowing. Hot-reload flows legitimately replace
|
|
142
|
-
// handlers; installing two different plugin packages that both claim
|
|
143
|
-
// the same (category, type) does not — the second wins and breaks the
|
|
144
|
-
// first's consumers with no audit trail. A console.warn is cheap,
|
|
145
|
-
// respects existing callers that rely on 'replaced', and gives ops a
|
|
146
|
-
// grep-able signal when registrations collide unexpectedly.
|
|
147
|
-
console.warn(
|
|
148
|
-
`[tagma-sdk] registerPlugin: replaced existing ${category}/${type} — ` +
|
|
149
|
-
`check for duplicate plugin packages claiming the same type.`,
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
return wasReplaced ? 'replaced' : 'registered';
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Remove a plugin from the in-process registry. Returns true if a plugin
|
|
157
|
-
* was actually removed. Note: ESM module caching is not affected, so
|
|
158
|
-
* re-importing the same file after unregister will yield the cached module —
|
|
159
|
-
* callers wanting a fresh load must restart the host process.
|
|
160
|
-
*/
|
|
161
|
-
export function unregisterPlugin(category: PluginCategory, type: string): boolean {
|
|
162
|
-
if (!VALID_CATEGORIES.has(category)) return false;
|
|
163
|
-
return registries[category].delete(type);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function getHandler<T extends PluginType>(category: PluginCategory, type: string): T {
|
|
167
|
-
const handler = registries[category].get(type);
|
|
168
|
-
if (!handler) {
|
|
169
|
-
throw new Error(
|
|
170
|
-
`${category} type "${type}" not registered.\n` +
|
|
171
|
-
`Install the plugin: bun add @tagma/${category.replace(/s$/, '')}-${type}`,
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
return handler as T;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function hasHandler(category: PluginCategory, type: string): boolean {
|
|
178
|
-
return registries[category].has(type);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Plugin name must be a scoped npm package or a tagma-prefixed package.
|
|
182
|
-
// Reject absolute/relative paths and suspicious patterns to prevent
|
|
183
|
-
// arbitrary code execution via crafted YAML configs.
|
|
184
|
-
export const PLUGIN_NAME_RE = /^(@[a-z0-9-]+\/[a-z0-9._-]+|tagma-plugin-[a-z0-9._-]+)$/;
|
|
185
|
-
|
|
186
|
-
export function isValidPluginName(name: unknown): name is string {
|
|
187
|
-
return typeof name === 'string' && PLUGIN_NAME_RE.test(name);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Parse and validate the `tagmaPlugin` field of a `package.json` blob.
|
|
192
|
-
*
|
|
193
|
-
* Returns the strongly-typed manifest if the field is present and
|
|
194
|
-
* well-formed (`category` is one of the four known categories and `type`
|
|
195
|
-
* is a non-empty string). Returns `null` if the field is absent — that
|
|
196
|
-
* is the host's signal that the package is a library, not a plugin.
|
|
197
|
-
*
|
|
198
|
-
* Throws if the field is present but malformed: that's a packaging bug
|
|
199
|
-
* the plugin author should hear about loudly, not a silent skip.
|
|
200
|
-
*
|
|
201
|
-
* Hosts use this during auto-discovery to decide whether to load a
|
|
202
|
-
* package as a plugin without having to dynamically `import()` it.
|
|
203
|
-
*/
|
|
204
|
-
export function readPluginManifest(pkgJson: unknown): PluginManifest | null {
|
|
205
|
-
if (!pkgJson || typeof pkgJson !== 'object') return null;
|
|
206
|
-
const raw = (pkgJson as Record<string, unknown>).tagmaPlugin;
|
|
207
|
-
if (raw === undefined) return null;
|
|
208
|
-
if (!raw || typeof raw !== 'object') {
|
|
209
|
-
throw new Error('tagmaPlugin field must be an object with { category, type }');
|
|
210
|
-
}
|
|
211
|
-
const m = raw as Record<string, unknown>;
|
|
212
|
-
const category = m.category;
|
|
213
|
-
const type = m.type;
|
|
214
|
-
if (typeof category !== 'string' || !VALID_CATEGORIES.has(category as PluginCategory)) {
|
|
215
|
-
throw new Error(
|
|
216
|
-
`tagmaPlugin.category must be one of ${[...VALID_CATEGORIES].join(', ')}, got ${JSON.stringify(category)}`,
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
if (typeof type !== 'string' || type.length === 0) {
|
|
220
|
-
throw new Error(`tagmaPlugin.type must be a non-empty string, got ${JSON.stringify(type)}`);
|
|
221
|
-
}
|
|
222
|
-
return { category: category as PluginCategory, type };
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Load and register a list of plugin packages.
|
|
227
|
-
*
|
|
228
|
-
* @param pluginNames - Validated npm package names to load.
|
|
229
|
-
* @param resolveFrom - Optional absolute path to resolve plugins from (e.g. the
|
|
230
|
-
* workspace's working directory). When omitted, the default ESM resolution
|
|
231
|
-
* uses the SDK's own `node_modules`, which will fail for plugins installed
|
|
232
|
-
* only in the user's workspace. CLI callers should pass `process.cwd()` or
|
|
233
|
-
* the workspace root so that workspace-local plugins resolve correctly.
|
|
234
|
-
*/
|
|
235
|
-
export async function loadPlugins(
|
|
236
|
-
pluginNames: readonly string[],
|
|
237
|
-
resolveFrom?: string,
|
|
238
|
-
): Promise<void> {
|
|
239
|
-
for (const name of pluginNames) {
|
|
240
|
-
if (!isValidPluginName(name)) {
|
|
241
|
-
throw new Error(
|
|
242
|
-
`Plugin "${name}" rejected: plugin names must be scoped npm packages ` +
|
|
243
|
-
`(e.g. @tagma/trigger-xyz) or tagma-plugin-* packages. ` +
|
|
244
|
-
`Relative/absolute paths are not allowed.`,
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
let moduleUrl: string = name;
|
|
248
|
-
if (resolveFrom) {
|
|
249
|
-
// Resolve the package entry point relative to the caller's directory so
|
|
250
|
-
// plugins installed in the workspace's node_modules are found even when
|
|
251
|
-
// the SDK itself lives elsewhere (e.g. a global install or a monorepo
|
|
252
|
-
// sibling package).
|
|
253
|
-
const req = createRequire(resolveFrom.endsWith('/') ? resolveFrom : resolveFrom + '/');
|
|
254
|
-
const resolved = req.resolve(name);
|
|
255
|
-
moduleUrl = pathToFileURL(resolved).href;
|
|
256
|
-
}
|
|
257
|
-
const mod = await import(moduleUrl);
|
|
258
|
-
if (!mod.pluginCategory || !mod.pluginType || !mod.default) {
|
|
259
|
-
throw new Error(`Plugin "${name}" must export pluginCategory, pluginType, and default`);
|
|
260
|
-
}
|
|
261
|
-
registerPlugin(mod.pluginCategory, mod.pluginType, mod.default);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export function listRegistered(category: PluginCategory): string[] {
|
|
266
|
-
return [...registries[category].keys()];
|
|
267
|
-
}
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import type {
|
|
4
|
+
PluginCategory,
|
|
5
|
+
DriverPlugin,
|
|
6
|
+
TriggerPlugin,
|
|
7
|
+
CompletionPlugin,
|
|
8
|
+
MiddlewarePlugin,
|
|
9
|
+
PluginManifest,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
type PluginType = DriverPlugin | TriggerPlugin | CompletionPlugin | MiddlewarePlugin;
|
|
13
|
+
|
|
14
|
+
const VALID_CATEGORIES: ReadonlySet<PluginCategory> = new Set([
|
|
15
|
+
'drivers',
|
|
16
|
+
'triggers',
|
|
17
|
+
'completions',
|
|
18
|
+
'middlewares',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
const registries = {
|
|
22
|
+
drivers: new Map<string, DriverPlugin>(),
|
|
23
|
+
triggers: new Map<string, TriggerPlugin>(),
|
|
24
|
+
completions: new Map<string, CompletionPlugin>(),
|
|
25
|
+
middlewares: new Map<string, MiddlewarePlugin>(),
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Minimal contract enforcement so a malformed plugin fails fast at
|
|
30
|
+
* registration time rather than crashing the engine mid-run.
|
|
31
|
+
*
|
|
32
|
+
* For drivers we materialize `capabilities` and assert each field is a
|
|
33
|
+
* boolean — otherwise a plugin author can write
|
|
34
|
+
* get capabilities() { throw new Error('boom') }
|
|
35
|
+
* and pass the basic typeof check, then crash preflight when the engine
|
|
36
|
+
* touches `driver.capabilities.sessionResume`. (R8)
|
|
37
|
+
*/
|
|
38
|
+
function validateContract(category: PluginCategory, handler: unknown): void {
|
|
39
|
+
if (!handler || typeof handler !== 'object') {
|
|
40
|
+
throw new Error(`Plugin handler for category "${category}" must be an object`);
|
|
41
|
+
}
|
|
42
|
+
const h = handler as Record<string, unknown>;
|
|
43
|
+
if (typeof h.name !== 'string' || h.name.length === 0) {
|
|
44
|
+
throw new Error(`Plugin handler for category "${category}" must declare a non-empty "name"`);
|
|
45
|
+
}
|
|
46
|
+
switch (category) {
|
|
47
|
+
case 'drivers': {
|
|
48
|
+
if (typeof h.buildCommand !== 'function') {
|
|
49
|
+
throw new Error(`drivers plugin "${h.name}" must export buildCommand()`);
|
|
50
|
+
}
|
|
51
|
+
// Materialize capabilities — this triggers any throwing getter NOW
|
|
52
|
+
// instead of during preflight.
|
|
53
|
+
let caps: unknown;
|
|
54
|
+
try {
|
|
55
|
+
caps = h.capabilities;
|
|
56
|
+
} catch (err) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`drivers plugin "${h.name}" capabilities accessor threw: ` +
|
|
59
|
+
(err instanceof Error ? err.message : String(err)),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
if (!caps || typeof caps !== 'object') {
|
|
63
|
+
throw new Error(`drivers plugin "${h.name}" must declare capabilities object`);
|
|
64
|
+
}
|
|
65
|
+
const c = caps as Record<string, unknown>;
|
|
66
|
+
for (const field of ['sessionResume', 'systemPrompt', 'outputFormat'] as const) {
|
|
67
|
+
if (typeof c[field] !== 'boolean') {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`drivers plugin "${h.name}".capabilities.${field} must be a boolean (got ${typeof c[field]})`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Optional methods, but if present must be functions.
|
|
74
|
+
for (const opt of ['parseResult', 'resolveModel', 'resolveTools'] as const) {
|
|
75
|
+
if (h[opt] !== undefined && typeof h[opt] !== 'function') {
|
|
76
|
+
throw new Error(`drivers plugin "${h.name}".${opt} must be a function or undefined`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case 'triggers':
|
|
82
|
+
if (typeof h.watch !== 'function') {
|
|
83
|
+
throw new Error(`triggers plugin "${h.name}" must export watch()`);
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
case 'completions':
|
|
87
|
+
if (typeof h.check !== 'function') {
|
|
88
|
+
throw new Error(`completions plugin "${h.name}" must export check()`);
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case 'middlewares':
|
|
92
|
+
// A middleware must provide at least one entry point. `enhanceDoc` is
|
|
93
|
+
// the structured PromptDocument API (preferred); `enhance` is the
|
|
94
|
+
// legacy string-in/string-out API the engine still supports for
|
|
95
|
+
// v0.x plugins. Requiring only `enhance` here rejects every built-in
|
|
96
|
+
// and every plugin written against the current types.
|
|
97
|
+
if (typeof h.enhanceDoc !== 'function' && typeof h.enhance !== 'function') {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`middlewares plugin "${h.name}" must export enhanceDoc() or enhance()`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (h.enhanceDoc !== undefined && typeof h.enhanceDoc !== 'function') {
|
|
103
|
+
throw new Error(`middlewares plugin "${h.name}".enhanceDoc must be a function or undefined`);
|
|
104
|
+
}
|
|
105
|
+
if (h.enhance !== undefined && typeof h.enhance !== 'function') {
|
|
106
|
+
throw new Error(`middlewares plugin "${h.name}".enhance must be a function or undefined`);
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type RegisterResult = 'registered' | 'replaced' | 'unchanged';
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Register a plugin under (category, type). Returns:
|
|
116
|
+
* - 'registered' on first registration
|
|
117
|
+
* - 'replaced' when an existing entry was overwritten with a different handler
|
|
118
|
+
* - 'unchanged' when the same handler instance was already present
|
|
119
|
+
*
|
|
120
|
+
* Throws if `category` is unknown, `type` is empty, or `handler` violates the
|
|
121
|
+
* minimum interface contract for the category.
|
|
122
|
+
*/
|
|
123
|
+
export function registerPlugin<T extends PluginType>(
|
|
124
|
+
category: PluginCategory,
|
|
125
|
+
type: string,
|
|
126
|
+
handler: T,
|
|
127
|
+
): RegisterResult {
|
|
128
|
+
if (!VALID_CATEGORIES.has(category)) {
|
|
129
|
+
throw new Error(`Unknown plugin category "${category}"`);
|
|
130
|
+
}
|
|
131
|
+
if (typeof type !== 'string' || type.length === 0) {
|
|
132
|
+
throw new Error(`Plugin type must be a non-empty string (category="${category}")`);
|
|
133
|
+
}
|
|
134
|
+
validateContract(category, handler);
|
|
135
|
+
const registry = registries[category] as Map<string, T>;
|
|
136
|
+
const existing = registry.get(type);
|
|
137
|
+
if (existing === handler) return 'unchanged';
|
|
138
|
+
const wasReplaced = existing !== undefined;
|
|
139
|
+
registry.set(type, handler);
|
|
140
|
+
if (wasReplaced) {
|
|
141
|
+
// D18: surface silent shadowing. Hot-reload flows legitimately replace
|
|
142
|
+
// handlers; installing two different plugin packages that both claim
|
|
143
|
+
// the same (category, type) does not — the second wins and breaks the
|
|
144
|
+
// first's consumers with no audit trail. A console.warn is cheap,
|
|
145
|
+
// respects existing callers that rely on 'replaced', and gives ops a
|
|
146
|
+
// grep-able signal when registrations collide unexpectedly.
|
|
147
|
+
console.warn(
|
|
148
|
+
`[tagma-sdk] registerPlugin: replaced existing ${category}/${type} — ` +
|
|
149
|
+
`check for duplicate plugin packages claiming the same type.`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return wasReplaced ? 'replaced' : 'registered';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Remove a plugin from the in-process registry. Returns true if a plugin
|
|
157
|
+
* was actually removed. Note: ESM module caching is not affected, so
|
|
158
|
+
* re-importing the same file after unregister will yield the cached module —
|
|
159
|
+
* callers wanting a fresh load must restart the host process.
|
|
160
|
+
*/
|
|
161
|
+
export function unregisterPlugin(category: PluginCategory, type: string): boolean {
|
|
162
|
+
if (!VALID_CATEGORIES.has(category)) return false;
|
|
163
|
+
return registries[category].delete(type);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function getHandler<T extends PluginType>(category: PluginCategory, type: string): T {
|
|
167
|
+
const handler = registries[category].get(type);
|
|
168
|
+
if (!handler) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
`${category} type "${type}" not registered.\n` +
|
|
171
|
+
`Install the plugin: bun add @tagma/${category.replace(/s$/, '')}-${type}`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return handler as T;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function hasHandler(category: PluginCategory, type: string): boolean {
|
|
178
|
+
return registries[category].has(type);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Plugin name must be a scoped npm package or a tagma-prefixed package.
|
|
182
|
+
// Reject absolute/relative paths and suspicious patterns to prevent
|
|
183
|
+
// arbitrary code execution via crafted YAML configs.
|
|
184
|
+
export const PLUGIN_NAME_RE = /^(@[a-z0-9-]+\/[a-z0-9._-]+|tagma-plugin-[a-z0-9._-]+)$/;
|
|
185
|
+
|
|
186
|
+
export function isValidPluginName(name: unknown): name is string {
|
|
187
|
+
return typeof name === 'string' && PLUGIN_NAME_RE.test(name);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Parse and validate the `tagmaPlugin` field of a `package.json` blob.
|
|
192
|
+
*
|
|
193
|
+
* Returns the strongly-typed manifest if the field is present and
|
|
194
|
+
* well-formed (`category` is one of the four known categories and `type`
|
|
195
|
+
* is a non-empty string). Returns `null` if the field is absent — that
|
|
196
|
+
* is the host's signal that the package is a library, not a plugin.
|
|
197
|
+
*
|
|
198
|
+
* Throws if the field is present but malformed: that's a packaging bug
|
|
199
|
+
* the plugin author should hear about loudly, not a silent skip.
|
|
200
|
+
*
|
|
201
|
+
* Hosts use this during auto-discovery to decide whether to load a
|
|
202
|
+
* package as a plugin without having to dynamically `import()` it.
|
|
203
|
+
*/
|
|
204
|
+
export function readPluginManifest(pkgJson: unknown): PluginManifest | null {
|
|
205
|
+
if (!pkgJson || typeof pkgJson !== 'object') return null;
|
|
206
|
+
const raw = (pkgJson as Record<string, unknown>).tagmaPlugin;
|
|
207
|
+
if (raw === undefined) return null;
|
|
208
|
+
if (!raw || typeof raw !== 'object') {
|
|
209
|
+
throw new Error('tagmaPlugin field must be an object with { category, type }');
|
|
210
|
+
}
|
|
211
|
+
const m = raw as Record<string, unknown>;
|
|
212
|
+
const category = m.category;
|
|
213
|
+
const type = m.type;
|
|
214
|
+
if (typeof category !== 'string' || !VALID_CATEGORIES.has(category as PluginCategory)) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`tagmaPlugin.category must be one of ${[...VALID_CATEGORIES].join(', ')}, got ${JSON.stringify(category)}`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
if (typeof type !== 'string' || type.length === 0) {
|
|
220
|
+
throw new Error(`tagmaPlugin.type must be a non-empty string, got ${JSON.stringify(type)}`);
|
|
221
|
+
}
|
|
222
|
+
return { category: category as PluginCategory, type };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Load and register a list of plugin packages.
|
|
227
|
+
*
|
|
228
|
+
* @param pluginNames - Validated npm package names to load.
|
|
229
|
+
* @param resolveFrom - Optional absolute path to resolve plugins from (e.g. the
|
|
230
|
+
* workspace's working directory). When omitted, the default ESM resolution
|
|
231
|
+
* uses the SDK's own `node_modules`, which will fail for plugins installed
|
|
232
|
+
* only in the user's workspace. CLI callers should pass `process.cwd()` or
|
|
233
|
+
* the workspace root so that workspace-local plugins resolve correctly.
|
|
234
|
+
*/
|
|
235
|
+
export async function loadPlugins(
|
|
236
|
+
pluginNames: readonly string[],
|
|
237
|
+
resolveFrom?: string,
|
|
238
|
+
): Promise<void> {
|
|
239
|
+
for (const name of pluginNames) {
|
|
240
|
+
if (!isValidPluginName(name)) {
|
|
241
|
+
throw new Error(
|
|
242
|
+
`Plugin "${name}" rejected: plugin names must be scoped npm packages ` +
|
|
243
|
+
`(e.g. @tagma/trigger-xyz) or tagma-plugin-* packages. ` +
|
|
244
|
+
`Relative/absolute paths are not allowed.`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
let moduleUrl: string = name;
|
|
248
|
+
if (resolveFrom) {
|
|
249
|
+
// Resolve the package entry point relative to the caller's directory so
|
|
250
|
+
// plugins installed in the workspace's node_modules are found even when
|
|
251
|
+
// the SDK itself lives elsewhere (e.g. a global install or a monorepo
|
|
252
|
+
// sibling package).
|
|
253
|
+
const req = createRequire(resolveFrom.endsWith('/') ? resolveFrom : resolveFrom + '/');
|
|
254
|
+
const resolved = req.resolve(name);
|
|
255
|
+
moduleUrl = pathToFileURL(resolved).href;
|
|
256
|
+
}
|
|
257
|
+
const mod = await import(moduleUrl);
|
|
258
|
+
if (!mod.pluginCategory || !mod.pluginType || !mod.default) {
|
|
259
|
+
throw new Error(`Plugin "${name}" must export pluginCategory, pluginType, and default`);
|
|
260
|
+
}
|
|
261
|
+
registerPlugin(mod.pluginCategory, mod.pluginType, mod.default);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function listRegistered(category: PluginCategory): string[] {
|
|
266
|
+
return [...registries[category].keys()];
|
|
267
|
+
}
|