@tagma/sdk 0.7.1 → 0.7.4

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