every-plugin 0.1.0 → 0.1.2
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 +1 -329
- package/dist/errors.d.ts +11 -7
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +3 -14
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -3
- package/dist/plugin.d.ts +149 -103
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +155 -23
- package/dist/runtime/errors.d.ts +41 -53
- package/dist/runtime/errors.d.ts.map +1 -1
- package/dist/runtime/errors.js +48 -8
- package/dist/runtime/index.d.ts +17 -66
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +22 -126
- package/dist/runtime/services/module-federation.service.d.ts +10 -38
- package/dist/runtime/services/module-federation.service.d.ts.map +1 -1
- package/dist/runtime/services/module-federation.service.js +131 -124
- package/dist/runtime/services/plugin.service.d.ts +21 -67
- package/dist/runtime/services/plugin.service.d.ts.map +1 -1
- package/dist/runtime/services/plugin.service.js +265 -328
- package/dist/runtime/services/secrets.service.d.ts +9 -29
- package/dist/runtime/services/secrets.service.js +26 -30
- package/dist/runtime/services.d.ts +1 -1
- package/dist/runtime/streaming/index.d.ts +4 -0
- package/dist/runtime/streaming/index.d.ts.map +1 -0
- package/dist/runtime/streaming/index.js +2 -0
- package/dist/runtime/streaming/state-effects.d.ts +28 -0
- package/dist/runtime/streaming/state-effects.d.ts.map +1 -0
- package/dist/runtime/streaming/state-effects.js +30 -0
- package/dist/runtime/streaming/streaming.d.ts +8 -0
- package/dist/runtime/streaming/streaming.d.ts.map +1 -0
- package/dist/runtime/streaming/streaming.js +113 -0
- package/dist/runtime/streaming/types.d.ts +19 -0
- package/dist/runtime/streaming/types.d.ts.map +1 -0
- package/dist/runtime/streaming/types.js +1 -0
- package/dist/runtime/test-utils.d.ts +1 -1
- package/dist/runtime/test-utils.js +4 -10
- package/dist/runtime/types.d.ts +28 -28
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/runtime/validation.d.ts +2 -7
- package/dist/runtime/validation.js +11 -14
- package/dist/testing/index.d.ts +25 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +40 -0
- package/dist/testing/mocks/module-federation.service.d.ts +8 -0
- package/dist/testing/mocks/module-federation.service.d.ts.map +1 -0
- package/dist/testing/mocks/module-federation.service.js +35 -0
- package/package.json +27 -14
- package/dist/runtime/mf.test.d.ts +0 -2
- package/dist/runtime/mf.test.d.ts.map +0 -1
- package/dist/runtime/mf.test.js +0 -135
- package/dist/runtime/runtime.test.d.ts +0 -2
- package/dist/runtime/runtime.test.d.ts.map +0 -1
- package/dist/runtime/runtime.test.js +0 -378
- package/dist/runtime/streaming.d.ts +0 -29
- package/dist/runtime/streaming.d.ts.map +0 -1
- package/dist/runtime/streaming.js +0 -59
- package/dist/runtime/streaming.test.d.ts +0 -2
- package/dist/runtime/streaming.test.d.ts.map +0 -1
- package/dist/runtime/streaming.test.js +0 -271
- package/dist/source.d.ts +0 -70
- package/dist/source.d.ts.map +0 -1
- package/dist/source.js +0 -37
|
@@ -1,338 +1,275 @@
|
|
|
1
|
+
import { call } from "@orpc/server";
|
|
1
2
|
import { Cache, Duration, Effect, Hash, Layer, Schedule } from "effect";
|
|
2
3
|
import { PluginLoggerTag } from "../../plugin";
|
|
3
4
|
import { PluginRuntimeError } from "../errors";
|
|
5
|
+
import { createSourceStream } from "../streaming";
|
|
4
6
|
import { validate } from "../validation";
|
|
5
7
|
import { ModuleFederationService } from "./module-federation.service";
|
|
6
8
|
import { SecretsService } from "./secrets.service";
|
|
7
9
|
export class PluginService extends Effect.Tag("PluginService")() {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return validatedOutput;
|
|
272
|
-
}),
|
|
273
|
-
usePlugin: (pluginId, config) => {
|
|
274
|
-
const configHash = generateConfigHash(config);
|
|
275
|
-
const cacheKey = `${pluginId}:${configHash}`;
|
|
276
|
-
// Store config for cache lookup
|
|
277
|
-
configByHash.set(configHash, config);
|
|
278
|
-
return cache.get(cacheKey).pipe(Effect.map((plugin) => plugin));
|
|
279
|
-
},
|
|
280
|
-
shutdownPlugin: (plugin) =>
|
|
281
|
-
Effect.gen(function* () {
|
|
282
|
-
const pluginLayer = Layer.succeed(PluginLoggerTag, logger);
|
|
283
|
-
// Remove from active plugins
|
|
284
|
-
activePlugins.delete(plugin);
|
|
285
|
-
// Try to invalidate from cache if it exists there
|
|
286
|
-
const configHash = generateConfigHash(plugin.config);
|
|
287
|
-
const cacheKey = `${plugin.plugin.id}:${configHash}`;
|
|
288
|
-
yield* cache.invalidate(cacheKey).pipe(
|
|
289
|
-
Effect.catchAll(() => Effect.void), // Ignore if not in cache
|
|
290
|
-
);
|
|
291
|
-
// Clean up config hash
|
|
292
|
-
configByHash.delete(configHash);
|
|
293
|
-
// Shutdown the plugin
|
|
294
|
-
yield* plugin.plugin.shutdown().pipe(
|
|
295
|
-
Effect.mapError(
|
|
296
|
-
(error) =>
|
|
297
|
-
new PluginRuntimeError({
|
|
298
|
-
pluginId: plugin.plugin.id,
|
|
299
|
-
operation: "shutdown-plugin",
|
|
300
|
-
cause: error,
|
|
301
|
-
retryable: false,
|
|
302
|
-
}),
|
|
303
|
-
),
|
|
304
|
-
Effect.provide(pluginLayer),
|
|
305
|
-
);
|
|
306
|
-
}),
|
|
307
|
-
clearCache: () =>
|
|
308
|
-
Effect.sync(() => {
|
|
309
|
-
configByHash.clear();
|
|
310
|
-
}),
|
|
311
|
-
cleanup: () =>
|
|
312
|
-
Effect.gen(function* () {
|
|
313
|
-
const pluginLayer = Layer.succeed(PluginLoggerTag, logger);
|
|
314
|
-
// Call shutdown on all active plugins
|
|
315
|
-
for (const initializedPlugin of activePlugins) {
|
|
316
|
-
yield* initializedPlugin.plugin.shutdown().pipe(
|
|
317
|
-
Effect.catchAll((error) => {
|
|
318
|
-
// Log shutdown errors but don't fail the cleanup
|
|
319
|
-
return logger.logError(
|
|
320
|
-
`Failed to shutdown plugin ${initializedPlugin.plugin.id}`,
|
|
321
|
-
error,
|
|
322
|
-
{ pluginId: initializedPlugin.plugin.id },
|
|
323
|
-
);
|
|
324
|
-
}),
|
|
325
|
-
Effect.provide(pluginLayer),
|
|
326
|
-
);
|
|
327
|
-
}
|
|
328
|
-
// Clear the active plugins set
|
|
329
|
-
activePlugins.clear();
|
|
330
|
-
// Clear cache
|
|
331
|
-
configByHash.clear();
|
|
332
|
-
}),
|
|
333
|
-
};
|
|
334
|
-
}),
|
|
335
|
-
);
|
|
10
|
+
static Live = (registry, secrets) => Layer.effect(PluginService, Effect.gen(function* () {
|
|
11
|
+
const moduleFederationService = yield* ModuleFederationService;
|
|
12
|
+
const secretsService = yield* SecretsService;
|
|
13
|
+
const logger = yield* PluginLoggerTag;
|
|
14
|
+
const activePlugins = new Set();
|
|
15
|
+
const retrySchedule = Schedule.exponential(Duration.millis(100)).pipe(Schedule.compose(Schedule.recurs(2)));
|
|
16
|
+
const resolveUrl = (baseUrl, version) => version && version !== "latest"
|
|
17
|
+
? baseUrl.replace("@latest", `@${version}`)
|
|
18
|
+
: baseUrl;
|
|
19
|
+
// Implementation functions
|
|
20
|
+
const loadPluginImpl = (pluginId) => Effect.gen(function* () {
|
|
21
|
+
const metadata = registry[pluginId];
|
|
22
|
+
if (!metadata) {
|
|
23
|
+
return yield* Effect.fail(new PluginRuntimeError({
|
|
24
|
+
pluginId,
|
|
25
|
+
operation: "load-plugin",
|
|
26
|
+
cause: new Error(`Plugin ${pluginId} not found in registry`),
|
|
27
|
+
retryable: false,
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
const url = resolveUrl(metadata.remoteUrl);
|
|
31
|
+
// Register and load the remote constructor
|
|
32
|
+
yield* moduleFederationService.registerRemote(pluginId, url).pipe(Effect.mapError((error) => new PluginRuntimeError({
|
|
33
|
+
pluginId,
|
|
34
|
+
operation: "register-remote",
|
|
35
|
+
cause: error.cause,
|
|
36
|
+
retryable: true,
|
|
37
|
+
})));
|
|
38
|
+
logger.logDebug(`Trying to load ${pluginId}:${url}`);
|
|
39
|
+
const ctor = yield* moduleFederationService.loadRemoteConstructor(pluginId, url).pipe(Effect.mapError((error) => new PluginRuntimeError({
|
|
40
|
+
pluginId,
|
|
41
|
+
operation: "load-remote",
|
|
42
|
+
cause: error.cause,
|
|
43
|
+
retryable: false
|
|
44
|
+
})));
|
|
45
|
+
return {
|
|
46
|
+
ctor,
|
|
47
|
+
metadata: {
|
|
48
|
+
pluginId,
|
|
49
|
+
version: metadata.version,
|
|
50
|
+
description: metadata.description,
|
|
51
|
+
type: metadata.type,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
const instantiatePluginImpl = (pluginConstructor) => Effect.gen(function* () {
|
|
56
|
+
const instance = yield* Effect.try({
|
|
57
|
+
try: () => new pluginConstructor.ctor(),
|
|
58
|
+
catch: (error) => new PluginRuntimeError({
|
|
59
|
+
pluginId: pluginConstructor.metadata.pluginId,
|
|
60
|
+
operation: "instantiate-plugin",
|
|
61
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
62
|
+
retryable: false,
|
|
63
|
+
}),
|
|
64
|
+
});
|
|
65
|
+
// Validate plugin ID matches
|
|
66
|
+
if (instance.id !== pluginConstructor.metadata.pluginId) {
|
|
67
|
+
return yield* Effect.fail(new PluginRuntimeError({
|
|
68
|
+
pluginId: pluginConstructor.metadata.pluginId,
|
|
69
|
+
operation: "validate-plugin-id",
|
|
70
|
+
cause: new Error(`Plugin ID mismatch: expected ${pluginConstructor.metadata.pluginId}, got ${instance.id}`),
|
|
71
|
+
retryable: false,
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
plugin: instance,
|
|
76
|
+
metadata: pluginConstructor.metadata,
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
const initializePluginImpl = (pluginInstance, config) => Effect.gen(function* () {
|
|
80
|
+
const { plugin } = pluginInstance;
|
|
81
|
+
// Validate raw config using plugin's Zod schema
|
|
82
|
+
const validatedConfig = yield* validate(plugin.configSchema, config, plugin.id, "config").pipe(Effect.mapError((validationError) => new PluginRuntimeError({
|
|
83
|
+
pluginId: plugin.id,
|
|
84
|
+
operation: "validate-config",
|
|
85
|
+
cause: validationError.zodError,
|
|
86
|
+
retryable: false,
|
|
87
|
+
})));
|
|
88
|
+
// Hydrate secrets
|
|
89
|
+
const hydratedConfig = yield* secretsService.hydrateSecrets(validatedConfig, secrets);
|
|
90
|
+
// Validate hydrated config
|
|
91
|
+
const finalConfig = yield* validate(plugin.configSchema, hydratedConfig, plugin.id, "config").pipe(Effect.mapError((validationError) => new PluginRuntimeError({
|
|
92
|
+
pluginId: plugin.id,
|
|
93
|
+
operation: "validate-hydrated-config",
|
|
94
|
+
cause: validationError.zodError,
|
|
95
|
+
retryable: false,
|
|
96
|
+
})));
|
|
97
|
+
// Initialize plugin with retry logic
|
|
98
|
+
const pluginLayer = Layer.succeed(PluginLoggerTag, logger);
|
|
99
|
+
yield* plugin.initialize(finalConfig).pipe(Effect.mapError((error) => new PluginRuntimeError({
|
|
100
|
+
pluginId: plugin.id,
|
|
101
|
+
operation: "initialize-plugin",
|
|
102
|
+
cause: error,
|
|
103
|
+
retryable: error.retryable ?? false,
|
|
104
|
+
})), Effect.retry(retrySchedule.pipe(Schedule.whileInput((error) => error.retryable))), Effect.provide(pluginLayer));
|
|
105
|
+
const initializedPlugin = {
|
|
106
|
+
plugin,
|
|
107
|
+
metadata: pluginInstance.metadata,
|
|
108
|
+
config: finalConfig, // TODO: config type
|
|
109
|
+
};
|
|
110
|
+
activePlugins.add(initializedPlugin);
|
|
111
|
+
return initializedPlugin;
|
|
112
|
+
});
|
|
113
|
+
// Cache functionality
|
|
114
|
+
const generateConfigHash = (config) => {
|
|
115
|
+
return Hash.structure(config).toString();
|
|
116
|
+
};
|
|
117
|
+
// Store original config by hash for cache lookup
|
|
118
|
+
const configByHash = new Map();
|
|
119
|
+
const cache = yield* Cache.make({
|
|
120
|
+
capacity: 1024,
|
|
121
|
+
timeToLive: Duration.minutes(60),
|
|
122
|
+
lookup: (cacheKey) => {
|
|
123
|
+
const [pluginId, configHash] = cacheKey.split(':');
|
|
124
|
+
if (!pluginId || !configHash) {
|
|
125
|
+
return Effect.fail(new PluginRuntimeError({
|
|
126
|
+
operation: "cache-lookup",
|
|
127
|
+
cause: new Error(`Invalid cache key format: ${cacheKey}`),
|
|
128
|
+
retryable: false,
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
const config = configByHash.get(configHash);
|
|
132
|
+
if (!config) {
|
|
133
|
+
return Effect.fail(new PluginRuntimeError({
|
|
134
|
+
operation: "cache-lookup",
|
|
135
|
+
cause: new Error(`No config found for hash: ${configHash}`),
|
|
136
|
+
retryable: false,
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
return Effect.gen(function* () {
|
|
140
|
+
const ctor = yield* loadPluginImpl(pluginId);
|
|
141
|
+
const instance = yield* instantiatePluginImpl(ctor);
|
|
142
|
+
return yield* initializePluginImpl(instance, config);
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
const usePluginImpl = (pluginId, config) => {
|
|
147
|
+
const configHash = generateConfigHash(config);
|
|
148
|
+
const cacheKey = `${pluginId}:${configHash}`;
|
|
149
|
+
// Store config for cache lookup
|
|
150
|
+
configByHash.set(configHash, config);
|
|
151
|
+
return cache.get(cacheKey).pipe(Effect.map(plugin => plugin));
|
|
152
|
+
};
|
|
153
|
+
// Extract executePlugin implementation for reuse
|
|
154
|
+
const executePluginImpl = (initializedPlugin, input) => Effect.gen(function* () {
|
|
155
|
+
const { plugin } = initializedPlugin;
|
|
156
|
+
// Validate input
|
|
157
|
+
const validatedInput = yield* validate(plugin.inputSchema, input, plugin.id, "input").pipe(Effect.mapError((validationError) => {
|
|
158
|
+
return new PluginRuntimeError({
|
|
159
|
+
pluginId: plugin.id,
|
|
160
|
+
operation: "validate-input",
|
|
161
|
+
cause: validationError.zodError,
|
|
162
|
+
retryable: false,
|
|
163
|
+
});
|
|
164
|
+
}));
|
|
165
|
+
// Get oRPC router and call procedure directly
|
|
166
|
+
const router = plugin.createRouter();
|
|
167
|
+
const procedureName = validatedInput.procedure;
|
|
168
|
+
if (!(procedureName in router)) {
|
|
169
|
+
return yield* Effect.fail(new PluginRuntimeError({
|
|
170
|
+
pluginId: plugin.id,
|
|
171
|
+
operation: "execute-plugin",
|
|
172
|
+
cause: new Error(`Unknown procedure: ${procedureName}`),
|
|
173
|
+
retryable: false,
|
|
174
|
+
}));
|
|
175
|
+
}
|
|
176
|
+
const context = 'state' in validatedInput ? { state: validatedInput.state } : {};
|
|
177
|
+
const output = yield* Effect.tryPromise({
|
|
178
|
+
try: () => {
|
|
179
|
+
const procedure = router[procedureName];
|
|
180
|
+
;
|
|
181
|
+
if (!procedure) {
|
|
182
|
+
throw new Error(`Procedure ${procedureName} not found in router`);
|
|
183
|
+
}
|
|
184
|
+
// Use oRPC call utility as per docs
|
|
185
|
+
return call(procedure, validatedInput.input, { context });
|
|
186
|
+
},
|
|
187
|
+
catch: (error) => new PluginRuntimeError({
|
|
188
|
+
pluginId: plugin.id,
|
|
189
|
+
operation: "execute-plugin",
|
|
190
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
191
|
+
retryable: true,
|
|
192
|
+
}),
|
|
193
|
+
}).pipe(Effect.retry(retrySchedule.pipe(Schedule.whileInput((error) => error.retryable))));
|
|
194
|
+
return output;
|
|
195
|
+
});
|
|
196
|
+
return {
|
|
197
|
+
loadPlugin: loadPluginImpl,
|
|
198
|
+
instantiatePlugin: instantiatePluginImpl,
|
|
199
|
+
initializePlugin: initializePluginImpl,
|
|
200
|
+
executePlugin: executePluginImpl,
|
|
201
|
+
usePlugin: usePluginImpl,
|
|
202
|
+
streamPlugin: (pluginId, config, input, options) => Effect.gen(function* () {
|
|
203
|
+
const initializedPlugin = yield* usePluginImpl(pluginId, config);
|
|
204
|
+
// Check if procedure is streamable and validate state
|
|
205
|
+
const procedureName = input.procedure;
|
|
206
|
+
const isStreamable = initializedPlugin.plugin.isStreamable(procedureName);
|
|
207
|
+
if (!isStreamable) {
|
|
208
|
+
return yield* Effect.fail(new PluginRuntimeError({
|
|
209
|
+
pluginId,
|
|
210
|
+
operation: "stream-plugin-validate",
|
|
211
|
+
cause: new Error(`Procedure ${procedureName} is not streamable`),
|
|
212
|
+
retryable: false,
|
|
213
|
+
}));
|
|
214
|
+
}
|
|
215
|
+
// Check stateSchema exists for streamable procedures
|
|
216
|
+
if (!('stateSchema' in initializedPlugin.plugin) || !initializedPlugin.plugin.stateSchema) {
|
|
217
|
+
return yield* Effect.fail(new PluginRuntimeError({
|
|
218
|
+
pluginId: initializedPlugin.plugin.id,
|
|
219
|
+
operation: "validate-state",
|
|
220
|
+
cause: new Error(`Streamable plugin ${initializedPlugin.plugin.id} must have a stateSchema`),
|
|
221
|
+
retryable: false,
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
// Validate initial state - allow null for initial calls
|
|
225
|
+
const nullableStateSchema = initializedPlugin.plugin.stateSchema.nullable();
|
|
226
|
+
yield* validate(nullableStateSchema, input.state, initializedPlugin.plugin.id, "state").pipe(Effect.mapError((validationError) => new PluginRuntimeError({
|
|
227
|
+
pluginId: initializedPlugin.plugin.id,
|
|
228
|
+
operation: "validate-state",
|
|
229
|
+
cause: validationError.zodError,
|
|
230
|
+
retryable: false,
|
|
231
|
+
})));
|
|
232
|
+
// Create and return the stream
|
|
233
|
+
return createSourceStream(initializedPlugin, executePluginImpl, input, options);
|
|
234
|
+
}),
|
|
235
|
+
shutdownPlugin: (plugin) => Effect.gen(function* () {
|
|
236
|
+
const pluginLayer = Layer.succeed(PluginLoggerTag, logger);
|
|
237
|
+
// Remove from active plugins
|
|
238
|
+
activePlugins.delete(plugin);
|
|
239
|
+
// Try to invalidate from cache if it exists there
|
|
240
|
+
const configHash = generateConfigHash(plugin.config);
|
|
241
|
+
const cacheKey = `${plugin.plugin.id}:${configHash}`;
|
|
242
|
+
yield* cache.invalidate(cacheKey).pipe(Effect.catchAll(() => Effect.void) // Ignore if not in cache
|
|
243
|
+
);
|
|
244
|
+
// Clean up config hash
|
|
245
|
+
configByHash.delete(configHash);
|
|
246
|
+
// Shutdown the plugin
|
|
247
|
+
yield* plugin.plugin.shutdown().pipe(Effect.mapError((error) => new PluginRuntimeError({
|
|
248
|
+
pluginId: plugin.plugin.id,
|
|
249
|
+
operation: "shutdown-plugin",
|
|
250
|
+
cause: error,
|
|
251
|
+
retryable: false,
|
|
252
|
+
})), Effect.provide(pluginLayer));
|
|
253
|
+
}),
|
|
254
|
+
clearCache: () => Effect.sync(() => {
|
|
255
|
+
configByHash.clear();
|
|
256
|
+
}),
|
|
257
|
+
cleanup: () => Effect.gen(function* () {
|
|
258
|
+
const pluginLayer = Layer.succeed(PluginLoggerTag, logger);
|
|
259
|
+
// Call shutdown on all active plugins
|
|
260
|
+
for (const initializedPlugin of activePlugins) {
|
|
261
|
+
yield* initializedPlugin.plugin.shutdown().pipe(Effect.catchAll((error) => {
|
|
262
|
+
// Log shutdown errors but don't fail the cleanup
|
|
263
|
+
return logger.logError(`Failed to shutdown plugin ${initializedPlugin.plugin.id}`, error, { pluginId: initializedPlugin.plugin.id });
|
|
264
|
+
}), Effect.provide(pluginLayer));
|
|
265
|
+
}
|
|
266
|
+
// Clear the active plugins set
|
|
267
|
+
activePlugins.clear();
|
|
268
|
+
// Clear cache
|
|
269
|
+
configByHash.clear();
|
|
270
|
+
}),
|
|
271
|
+
};
|
|
272
|
+
}));
|
|
336
273
|
}
|
|
337
274
|
export const PluginServiceTag = PluginService;
|
|
338
275
|
export const PluginServiceLive = PluginService.Live;
|