@paperclipai/plugin-sdk 2026.3.17-canary.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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +888 -0
  3. package/dist/bundlers.d.ts +57 -0
  4. package/dist/bundlers.d.ts.map +1 -0
  5. package/dist/bundlers.js +105 -0
  6. package/dist/bundlers.js.map +1 -0
  7. package/dist/define-plugin.d.ts +218 -0
  8. package/dist/define-plugin.d.ts.map +1 -0
  9. package/dist/define-plugin.js +85 -0
  10. package/dist/define-plugin.js.map +1 -0
  11. package/dist/dev-cli.d.ts +3 -0
  12. package/dist/dev-cli.d.ts.map +1 -0
  13. package/dist/dev-cli.js +49 -0
  14. package/dist/dev-cli.js.map +1 -0
  15. package/dist/dev-server.d.ts +34 -0
  16. package/dist/dev-server.d.ts.map +1 -0
  17. package/dist/dev-server.js +194 -0
  18. package/dist/dev-server.js.map +1 -0
  19. package/dist/host-client-factory.d.ts +229 -0
  20. package/dist/host-client-factory.d.ts.map +1 -0
  21. package/dist/host-client-factory.js +353 -0
  22. package/dist/host-client-factory.js.map +1 -0
  23. package/dist/index.d.ts +84 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +84 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/protocol.d.ts +881 -0
  28. package/dist/protocol.d.ts.map +1 -0
  29. package/dist/protocol.js +297 -0
  30. package/dist/protocol.js.map +1 -0
  31. package/dist/testing.d.ts +63 -0
  32. package/dist/testing.d.ts.map +1 -0
  33. package/dist/testing.js +700 -0
  34. package/dist/testing.js.map +1 -0
  35. package/dist/types.d.ts +982 -0
  36. package/dist/types.d.ts.map +1 -0
  37. package/dist/types.js +12 -0
  38. package/dist/types.js.map +1 -0
  39. package/dist/ui/components.d.ts +257 -0
  40. package/dist/ui/components.d.ts.map +1 -0
  41. package/dist/ui/components.js +97 -0
  42. package/dist/ui/components.js.map +1 -0
  43. package/dist/ui/hooks.d.ts +120 -0
  44. package/dist/ui/hooks.d.ts.map +1 -0
  45. package/dist/ui/hooks.js +148 -0
  46. package/dist/ui/hooks.js.map +1 -0
  47. package/dist/ui/index.d.ts +50 -0
  48. package/dist/ui/index.d.ts.map +1 -0
  49. package/dist/ui/index.js +48 -0
  50. package/dist/ui/index.js.map +1 -0
  51. package/dist/ui/runtime.d.ts +3 -0
  52. package/dist/ui/runtime.d.ts.map +1 -0
  53. package/dist/ui/runtime.js +30 -0
  54. package/dist/ui/runtime.js.map +1 -0
  55. package/dist/ui/types.d.ts +308 -0
  56. package/dist/ui/types.d.ts.map +1 -0
  57. package/dist/ui/types.js +17 -0
  58. package/dist/ui/types.js.map +1 -0
  59. package/dist/worker-rpc-host.d.ts +127 -0
  60. package/dist/worker-rpc-host.d.ts.map +1 -0
  61. package/dist/worker-rpc-host.js +941 -0
  62. package/dist/worker-rpc-host.js.map +1 -0
  63. package/package.json +88 -0
@@ -0,0 +1,941 @@
1
+ /**
2
+ * Worker-side RPC host — runs inside the child process spawned by the host.
3
+ *
4
+ * This module is the worker-side counterpart to the server's
5
+ * `PluginWorkerManager`. It:
6
+ *
7
+ * 1. Reads newline-delimited JSON-RPC 2.0 requests from **stdin**
8
+ * 2. Dispatches them to the appropriate plugin handler (events, jobs, tools, …)
9
+ * 3. Writes JSON-RPC 2.0 responses back on **stdout**
10
+ * 4. Provides a concrete `PluginContext` whose SDK client methods (e.g.
11
+ * `ctx.state.get()`, `ctx.events.emit()`) send JSON-RPC requests to the
12
+ * host on stdout and await responses on stdin.
13
+ *
14
+ * ## Message flow
15
+ *
16
+ * ```
17
+ * Host (parent) Worker (this module)
18
+ * | |
19
+ * |--- request(initialize) -------------> | → calls plugin.setup(ctx)
20
+ * |<-- response(ok:true) ---------------- |
21
+ * | |
22
+ * |--- notification(onEvent) -----------> | → dispatches to registered handler
23
+ * | |
24
+ * |<-- request(state.get) --------------- | ← SDK client call from plugin code
25
+ * |--- response(result) ----------------> |
26
+ * | |
27
+ * |--- request(shutdown) ---------------> | → calls plugin.onShutdown()
28
+ * |<-- response(void) ------------------ |
29
+ * | (process exits)
30
+ * ```
31
+ *
32
+ * @see PLUGIN_SPEC.md §12 — Process Model
33
+ * @see PLUGIN_SPEC.md §13 — Host-Worker Protocol
34
+ * @see PLUGIN_SPEC.md §14 — SDK Surface
35
+ */
36
+ import path from "node:path";
37
+ import { createInterface } from "node:readline";
38
+ import { fileURLToPath } from "node:url";
39
+ import { JSONRPC_ERROR_CODES, PLUGIN_RPC_ERROR_CODES, createRequest, createSuccessResponse, createErrorResponse, createNotification, parseMessage, serializeMessage, isJsonRpcRequest, isJsonRpcResponse, isJsonRpcNotification, isJsonRpcSuccessResponse, isJsonRpcErrorResponse, JsonRpcParseError, JsonRpcCallError, } from "./protocol.js";
40
+ // ---------------------------------------------------------------------------
41
+ // Constants
42
+ // ---------------------------------------------------------------------------
43
+ /** Default timeout for worker→host RPC calls. */
44
+ const DEFAULT_RPC_TIMEOUT_MS = 30_000;
45
+ /**
46
+ * Start the worker when this module is the process entrypoint.
47
+ *
48
+ * Call this at the bottom of your worker file so that when the host runs
49
+ * `node dist/worker.js`, the RPC host starts and the process stays alive.
50
+ * When the module is imported (e.g. for re-exports or tests), nothing runs.
51
+ *
52
+ * When `options.stdin` and `options.stdout` are provided (e.g. in tests),
53
+ * the main-module check is skipped and the host is started with those streams.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const plugin = definePlugin({ ... });
58
+ * export default plugin;
59
+ * runWorker(plugin, import.meta.url);
60
+ * ```
61
+ */
62
+ export function runWorker(plugin, moduleUrl, options) {
63
+ if (options?.stdin != null &&
64
+ options?.stdout != null) {
65
+ return startWorkerRpcHost({
66
+ plugin,
67
+ stdin: options.stdin,
68
+ stdout: options.stdout,
69
+ });
70
+ }
71
+ const entry = process.argv[1];
72
+ if (typeof entry !== "string")
73
+ return;
74
+ const thisFile = path.resolve(fileURLToPath(moduleUrl));
75
+ const entryPath = path.resolve(entry);
76
+ if (thisFile === entryPath) {
77
+ startWorkerRpcHost({ plugin });
78
+ }
79
+ }
80
+ /**
81
+ * Start the worker-side RPC host.
82
+ *
83
+ * This function is typically called from a thin bootstrap script that is the
84
+ * actual entrypoint of the child process:
85
+ *
86
+ * ```ts
87
+ * // worker-bootstrap.ts
88
+ * import plugin from "./worker.js";
89
+ * import { startWorkerRpcHost } from "@paperclipai/plugin-sdk";
90
+ *
91
+ * startWorkerRpcHost({ plugin });
92
+ * ```
93
+ *
94
+ * The host begins listening on stdin immediately. It does NOT call
95
+ * `plugin.definition.setup()` yet — that happens when the host sends the
96
+ * `initialize` RPC.
97
+ *
98
+ * @returns A handle for inspecting or stopping the RPC host
99
+ */
100
+ export function startWorkerRpcHost(options) {
101
+ const { plugin } = options;
102
+ const stdinStream = options.stdin ?? process.stdin;
103
+ const stdoutStream = options.stdout ?? process.stdout;
104
+ const rpcTimeoutMs = options.rpcTimeoutMs ?? DEFAULT_RPC_TIMEOUT_MS;
105
+ // -----------------------------------------------------------------------
106
+ // State
107
+ // -----------------------------------------------------------------------
108
+ let running = true;
109
+ let initialized = false;
110
+ let manifest = null;
111
+ let currentConfig = {};
112
+ // Plugin handler registrations (populated during setup())
113
+ const eventHandlers = [];
114
+ const jobHandlers = new Map();
115
+ const launcherRegistrations = new Map();
116
+ const dataHandlers = new Map();
117
+ const actionHandlers = new Map();
118
+ const toolHandlers = new Map();
119
+ // Agent session event callbacks (populated by sendMessage, cleared by close)
120
+ const sessionEventCallbacks = new Map();
121
+ // Pending outbound (worker→host) requests
122
+ const pendingRequests = new Map();
123
+ let nextOutboundId = 1;
124
+ const MAX_OUTBOUND_ID = Number.MAX_SAFE_INTEGER - 1;
125
+ // -----------------------------------------------------------------------
126
+ // Outbound messaging (worker → host)
127
+ // -----------------------------------------------------------------------
128
+ function sendMessage(message) {
129
+ if (!running)
130
+ return;
131
+ const serialized = serializeMessage(message);
132
+ stdoutStream.write(serialized);
133
+ }
134
+ /**
135
+ * Send a typed JSON-RPC request to the host and await the response.
136
+ */
137
+ function callHost(method, params, timeoutMs) {
138
+ return new Promise((resolve, reject) => {
139
+ if (!running) {
140
+ reject(new Error(`Cannot call "${method}" — worker RPC host is not running`));
141
+ return;
142
+ }
143
+ if (nextOutboundId >= MAX_OUTBOUND_ID) {
144
+ nextOutboundId = 1;
145
+ }
146
+ const id = nextOutboundId++;
147
+ const timeout = timeoutMs ?? rpcTimeoutMs;
148
+ let settled = false;
149
+ const settle = (fn, value) => {
150
+ if (settled)
151
+ return;
152
+ settled = true;
153
+ clearTimeout(timer);
154
+ pendingRequests.delete(id);
155
+ fn(value);
156
+ };
157
+ const timer = setTimeout(() => {
158
+ settle(reject, new JsonRpcCallError({
159
+ code: PLUGIN_RPC_ERROR_CODES.TIMEOUT,
160
+ message: `Worker→host call "${method}" timed out after ${timeout}ms`,
161
+ }));
162
+ }, timeout);
163
+ pendingRequests.set(id, {
164
+ resolve: (response) => {
165
+ if (isJsonRpcSuccessResponse(response)) {
166
+ settle(resolve, response.result);
167
+ }
168
+ else if (isJsonRpcErrorResponse(response)) {
169
+ settle(reject, new JsonRpcCallError(response.error));
170
+ }
171
+ else {
172
+ settle(reject, new Error(`Unexpected response format for "${method}"`));
173
+ }
174
+ },
175
+ timer,
176
+ });
177
+ try {
178
+ const request = createRequest(method, params, id);
179
+ sendMessage(request);
180
+ }
181
+ catch (err) {
182
+ settle(reject, err instanceof Error ? err : new Error(String(err)));
183
+ }
184
+ });
185
+ }
186
+ /**
187
+ * Send a JSON-RPC notification to the host (fire-and-forget).
188
+ */
189
+ function notifyHost(method, params) {
190
+ try {
191
+ sendMessage(createNotification(method, params));
192
+ }
193
+ catch {
194
+ // Swallow — the host may have closed stdin
195
+ }
196
+ }
197
+ // -----------------------------------------------------------------------
198
+ // Build the PluginContext (SDK surface for plugin code)
199
+ // -----------------------------------------------------------------------
200
+ function buildContext() {
201
+ return {
202
+ get manifest() {
203
+ if (!manifest)
204
+ throw new Error("Plugin context accessed before initialization");
205
+ return manifest;
206
+ },
207
+ config: {
208
+ async get() {
209
+ return callHost("config.get", {});
210
+ },
211
+ },
212
+ events: {
213
+ on(name, filterOrFn, maybeFn) {
214
+ let registration;
215
+ if (typeof filterOrFn === "function") {
216
+ registration = { name, fn: filterOrFn };
217
+ }
218
+ else {
219
+ if (!maybeFn)
220
+ throw new Error("Event handler function is required");
221
+ registration = { name, filter: filterOrFn, fn: maybeFn };
222
+ }
223
+ eventHandlers.push(registration);
224
+ // Register subscription on the host so events are forwarded to this worker
225
+ void callHost("events.subscribe", { eventPattern: name, filter: registration.filter ?? null }).catch((err) => {
226
+ notifyHost("log", {
227
+ level: "warn",
228
+ message: `Failed to subscribe to event "${name}" on host: ${err instanceof Error ? err.message : String(err)}`,
229
+ });
230
+ });
231
+ return () => {
232
+ const idx = eventHandlers.indexOf(registration);
233
+ if (idx !== -1)
234
+ eventHandlers.splice(idx, 1);
235
+ };
236
+ },
237
+ async emit(name, companyId, payload) {
238
+ await callHost("events.emit", { name, companyId, payload });
239
+ },
240
+ },
241
+ jobs: {
242
+ register(key, fn) {
243
+ jobHandlers.set(key, fn);
244
+ },
245
+ },
246
+ launchers: {
247
+ register(launcher) {
248
+ launcherRegistrations.set(launcher.id, launcher);
249
+ },
250
+ },
251
+ http: {
252
+ async fetch(url, init) {
253
+ const serializedInit = {};
254
+ if (init) {
255
+ if (init.method)
256
+ serializedInit.method = init.method;
257
+ if (init.headers) {
258
+ // Normalize headers to a plain object
259
+ if (init.headers instanceof Headers) {
260
+ const obj = {};
261
+ init.headers.forEach((v, k) => { obj[k] = v; });
262
+ serializedInit.headers = obj;
263
+ }
264
+ else if (Array.isArray(init.headers)) {
265
+ const obj = {};
266
+ for (const [k, v] of init.headers)
267
+ obj[k] = v;
268
+ serializedInit.headers = obj;
269
+ }
270
+ else {
271
+ serializedInit.headers = init.headers;
272
+ }
273
+ }
274
+ if (init.body !== undefined && init.body !== null) {
275
+ serializedInit.body = typeof init.body === "string"
276
+ ? init.body
277
+ : String(init.body);
278
+ }
279
+ }
280
+ const result = await callHost("http.fetch", {
281
+ url,
282
+ init: Object.keys(serializedInit).length > 0 ? serializedInit : undefined,
283
+ });
284
+ // Reconstruct a Response-like object from the serialized result
285
+ return new Response(result.body, {
286
+ status: result.status,
287
+ statusText: result.statusText,
288
+ headers: result.headers,
289
+ });
290
+ },
291
+ },
292
+ secrets: {
293
+ async resolve(secretRef) {
294
+ return callHost("secrets.resolve", { secretRef });
295
+ },
296
+ },
297
+ activity: {
298
+ async log(entry) {
299
+ await callHost("activity.log", {
300
+ companyId: entry.companyId,
301
+ message: entry.message,
302
+ entityType: entry.entityType,
303
+ entityId: entry.entityId,
304
+ metadata: entry.metadata,
305
+ });
306
+ },
307
+ },
308
+ state: {
309
+ async get(input) {
310
+ return callHost("state.get", {
311
+ scopeKind: input.scopeKind,
312
+ scopeId: input.scopeId,
313
+ namespace: input.namespace,
314
+ stateKey: input.stateKey,
315
+ });
316
+ },
317
+ async set(input, value) {
318
+ await callHost("state.set", {
319
+ scopeKind: input.scopeKind,
320
+ scopeId: input.scopeId,
321
+ namespace: input.namespace,
322
+ stateKey: input.stateKey,
323
+ value,
324
+ });
325
+ },
326
+ async delete(input) {
327
+ await callHost("state.delete", {
328
+ scopeKind: input.scopeKind,
329
+ scopeId: input.scopeId,
330
+ namespace: input.namespace,
331
+ stateKey: input.stateKey,
332
+ });
333
+ },
334
+ },
335
+ entities: {
336
+ async upsert(input) {
337
+ return callHost("entities.upsert", {
338
+ entityType: input.entityType,
339
+ scopeKind: input.scopeKind,
340
+ scopeId: input.scopeId,
341
+ externalId: input.externalId,
342
+ title: input.title,
343
+ status: input.status,
344
+ data: input.data,
345
+ });
346
+ },
347
+ async list(query) {
348
+ return callHost("entities.list", {
349
+ entityType: query.entityType,
350
+ scopeKind: query.scopeKind,
351
+ scopeId: query.scopeId,
352
+ externalId: query.externalId,
353
+ limit: query.limit,
354
+ offset: query.offset,
355
+ });
356
+ },
357
+ },
358
+ projects: {
359
+ async list(input) {
360
+ return callHost("projects.list", {
361
+ companyId: input.companyId,
362
+ limit: input.limit,
363
+ offset: input.offset,
364
+ });
365
+ },
366
+ async get(projectId, companyId) {
367
+ return callHost("projects.get", { projectId, companyId });
368
+ },
369
+ async listWorkspaces(projectId, companyId) {
370
+ return callHost("projects.listWorkspaces", { projectId, companyId });
371
+ },
372
+ async getPrimaryWorkspace(projectId, companyId) {
373
+ return callHost("projects.getPrimaryWorkspace", { projectId, companyId });
374
+ },
375
+ async getWorkspaceForIssue(issueId, companyId) {
376
+ return callHost("projects.getWorkspaceForIssue", { issueId, companyId });
377
+ },
378
+ },
379
+ companies: {
380
+ async list(input) {
381
+ return callHost("companies.list", {
382
+ limit: input?.limit,
383
+ offset: input?.offset,
384
+ });
385
+ },
386
+ async get(companyId) {
387
+ return callHost("companies.get", { companyId });
388
+ },
389
+ },
390
+ issues: {
391
+ async list(input) {
392
+ return callHost("issues.list", {
393
+ companyId: input.companyId,
394
+ projectId: input.projectId,
395
+ assigneeAgentId: input.assigneeAgentId,
396
+ status: input.status,
397
+ limit: input.limit,
398
+ offset: input.offset,
399
+ });
400
+ },
401
+ async get(issueId, companyId) {
402
+ return callHost("issues.get", { issueId, companyId });
403
+ },
404
+ async create(input) {
405
+ return callHost("issues.create", {
406
+ companyId: input.companyId,
407
+ projectId: input.projectId,
408
+ goalId: input.goalId,
409
+ parentId: input.parentId,
410
+ title: input.title,
411
+ description: input.description,
412
+ priority: input.priority,
413
+ assigneeAgentId: input.assigneeAgentId,
414
+ });
415
+ },
416
+ async update(issueId, patch, companyId) {
417
+ return callHost("issues.update", {
418
+ issueId,
419
+ patch: patch,
420
+ companyId,
421
+ });
422
+ },
423
+ async listComments(issueId, companyId) {
424
+ return callHost("issues.listComments", { issueId, companyId });
425
+ },
426
+ async createComment(issueId, body, companyId) {
427
+ return callHost("issues.createComment", { issueId, body, companyId });
428
+ },
429
+ documents: {
430
+ async list(issueId, companyId) {
431
+ return callHost("issues.documents.list", { issueId, companyId });
432
+ },
433
+ async get(issueId, key, companyId) {
434
+ return callHost("issues.documents.get", { issueId, key, companyId });
435
+ },
436
+ async upsert(input) {
437
+ return callHost("issues.documents.upsert", {
438
+ issueId: input.issueId,
439
+ key: input.key,
440
+ body: input.body,
441
+ companyId: input.companyId,
442
+ title: input.title,
443
+ format: input.format,
444
+ changeSummary: input.changeSummary,
445
+ });
446
+ },
447
+ async delete(issueId, key, companyId) {
448
+ return callHost("issues.documents.delete", { issueId, key, companyId });
449
+ },
450
+ },
451
+ },
452
+ agents: {
453
+ async list(input) {
454
+ return callHost("agents.list", {
455
+ companyId: input.companyId,
456
+ status: input.status,
457
+ limit: input.limit,
458
+ offset: input.offset,
459
+ });
460
+ },
461
+ async get(agentId, companyId) {
462
+ return callHost("agents.get", { agentId, companyId });
463
+ },
464
+ async pause(agentId, companyId) {
465
+ return callHost("agents.pause", { agentId, companyId });
466
+ },
467
+ async resume(agentId, companyId) {
468
+ return callHost("agents.resume", { agentId, companyId });
469
+ },
470
+ async invoke(agentId, companyId, opts) {
471
+ return callHost("agents.invoke", { agentId, companyId, prompt: opts.prompt, reason: opts.reason });
472
+ },
473
+ sessions: {
474
+ async create(agentId, companyId, opts) {
475
+ return callHost("agents.sessions.create", {
476
+ agentId,
477
+ companyId,
478
+ taskKey: opts?.taskKey,
479
+ reason: opts?.reason,
480
+ });
481
+ },
482
+ async list(agentId, companyId) {
483
+ return callHost("agents.sessions.list", { agentId, companyId });
484
+ },
485
+ async sendMessage(sessionId, companyId, opts) {
486
+ if (opts.onEvent) {
487
+ sessionEventCallbacks.set(sessionId, opts.onEvent);
488
+ }
489
+ try {
490
+ return await callHost("agents.sessions.sendMessage", {
491
+ sessionId,
492
+ companyId,
493
+ prompt: opts.prompt,
494
+ reason: opts.reason,
495
+ });
496
+ }
497
+ catch (err) {
498
+ sessionEventCallbacks.delete(sessionId);
499
+ throw err;
500
+ }
501
+ },
502
+ async close(sessionId, companyId) {
503
+ sessionEventCallbacks.delete(sessionId);
504
+ await callHost("agents.sessions.close", { sessionId, companyId });
505
+ },
506
+ },
507
+ },
508
+ goals: {
509
+ async list(input) {
510
+ return callHost("goals.list", {
511
+ companyId: input.companyId,
512
+ level: input.level,
513
+ status: input.status,
514
+ limit: input.limit,
515
+ offset: input.offset,
516
+ });
517
+ },
518
+ async get(goalId, companyId) {
519
+ return callHost("goals.get", { goalId, companyId });
520
+ },
521
+ async create(input) {
522
+ return callHost("goals.create", {
523
+ companyId: input.companyId,
524
+ title: input.title,
525
+ description: input.description,
526
+ level: input.level,
527
+ status: input.status,
528
+ parentId: input.parentId,
529
+ ownerAgentId: input.ownerAgentId,
530
+ });
531
+ },
532
+ async update(goalId, patch, companyId) {
533
+ return callHost("goals.update", {
534
+ goalId,
535
+ patch: patch,
536
+ companyId,
537
+ });
538
+ },
539
+ },
540
+ data: {
541
+ register(key, handler) {
542
+ dataHandlers.set(key, handler);
543
+ },
544
+ },
545
+ actions: {
546
+ register(key, handler) {
547
+ actionHandlers.set(key, handler);
548
+ },
549
+ },
550
+ streams: (() => {
551
+ // Track channel → companyId so emit/close don't require companyId
552
+ const channelCompanyMap = new Map();
553
+ return {
554
+ open(channel, companyId) {
555
+ channelCompanyMap.set(channel, companyId);
556
+ notifyHost("streams.open", { channel, companyId });
557
+ },
558
+ emit(channel, event) {
559
+ const companyId = channelCompanyMap.get(channel) ?? "";
560
+ notifyHost("streams.emit", { channel, companyId, event });
561
+ },
562
+ close(channel) {
563
+ const companyId = channelCompanyMap.get(channel) ?? "";
564
+ channelCompanyMap.delete(channel);
565
+ notifyHost("streams.close", { channel, companyId });
566
+ },
567
+ };
568
+ })(),
569
+ tools: {
570
+ register(name, declaration, fn) {
571
+ toolHandlers.set(name, { declaration, fn });
572
+ },
573
+ },
574
+ metrics: {
575
+ async write(name, value, tags) {
576
+ await callHost("metrics.write", { name, value, tags });
577
+ },
578
+ },
579
+ logger: {
580
+ info(message, meta) {
581
+ notifyHost("log", { level: "info", message, meta });
582
+ },
583
+ warn(message, meta) {
584
+ notifyHost("log", { level: "warn", message, meta });
585
+ },
586
+ error(message, meta) {
587
+ notifyHost("log", { level: "error", message, meta });
588
+ },
589
+ debug(message, meta) {
590
+ notifyHost("log", { level: "debug", message, meta });
591
+ },
592
+ },
593
+ };
594
+ }
595
+ const ctx = buildContext();
596
+ // -----------------------------------------------------------------------
597
+ // Inbound message handling (host → worker)
598
+ // -----------------------------------------------------------------------
599
+ /**
600
+ * Handle an incoming JSON-RPC request from the host.
601
+ *
602
+ * Dispatches to the correct handler based on the method name.
603
+ */
604
+ async function handleHostRequest(request) {
605
+ const { id, method, params } = request;
606
+ try {
607
+ const result = await dispatchMethod(method, params);
608
+ sendMessage(createSuccessResponse(id, result ?? null));
609
+ }
610
+ catch (err) {
611
+ const errorMessage = err instanceof Error ? err.message : String(err);
612
+ // Propagate specific error codes from handler errors (e.g.
613
+ // METHOD_NOT_FOUND, METHOD_NOT_IMPLEMENTED) — fall back to
614
+ // WORKER_ERROR for untyped exceptions.
615
+ const errorCode = typeof err?.code === "number"
616
+ ? err.code
617
+ : PLUGIN_RPC_ERROR_CODES.WORKER_ERROR;
618
+ sendMessage(createErrorResponse(id, errorCode, errorMessage));
619
+ }
620
+ }
621
+ /**
622
+ * Dispatch a host→worker method call to the appropriate handler.
623
+ */
624
+ async function dispatchMethod(method, params) {
625
+ switch (method) {
626
+ case "initialize":
627
+ return handleInitialize(params);
628
+ case "health":
629
+ return handleHealth();
630
+ case "shutdown":
631
+ return handleShutdown();
632
+ case "validateConfig":
633
+ return handleValidateConfig(params);
634
+ case "configChanged":
635
+ return handleConfigChanged(params);
636
+ case "onEvent":
637
+ return handleOnEvent(params);
638
+ case "runJob":
639
+ return handleRunJob(params);
640
+ case "handleWebhook":
641
+ return handleWebhook(params);
642
+ case "getData":
643
+ return handleGetData(params);
644
+ case "performAction":
645
+ return handlePerformAction(params);
646
+ case "executeTool":
647
+ return handleExecuteTool(params);
648
+ default:
649
+ throw Object.assign(new Error(`Unknown method: ${method}`), { code: JSONRPC_ERROR_CODES.METHOD_NOT_FOUND });
650
+ }
651
+ }
652
+ // -----------------------------------------------------------------------
653
+ // Host→Worker method handlers
654
+ // -----------------------------------------------------------------------
655
+ async function handleInitialize(params) {
656
+ if (initialized) {
657
+ throw new Error("Worker already initialized");
658
+ }
659
+ manifest = params.manifest;
660
+ currentConfig = params.config;
661
+ // Call the plugin's setup function
662
+ await plugin.definition.setup(ctx);
663
+ initialized = true;
664
+ // Report which optional methods this plugin implements
665
+ const supportedMethods = [];
666
+ if (plugin.definition.onValidateConfig)
667
+ supportedMethods.push("validateConfig");
668
+ if (plugin.definition.onConfigChanged)
669
+ supportedMethods.push("configChanged");
670
+ if (plugin.definition.onHealth)
671
+ supportedMethods.push("health");
672
+ if (plugin.definition.onShutdown)
673
+ supportedMethods.push("shutdown");
674
+ return { ok: true, supportedMethods };
675
+ }
676
+ async function handleHealth() {
677
+ if (plugin.definition.onHealth) {
678
+ return plugin.definition.onHealth();
679
+ }
680
+ // Default: report OK if the worker is alive
681
+ return { status: "ok" };
682
+ }
683
+ async function handleShutdown() {
684
+ if (plugin.definition.onShutdown) {
685
+ await plugin.definition.onShutdown();
686
+ }
687
+ // Schedule cleanup after we send the response.
688
+ // Use setImmediate to let the response flush before exiting.
689
+ // Only call process.exit() when running with real process streams.
690
+ // When custom streams are provided (tests), just clean up.
691
+ setImmediate(() => {
692
+ cleanup();
693
+ if (!options.stdin && !options.stdout) {
694
+ process.exit(0);
695
+ }
696
+ });
697
+ }
698
+ async function handleValidateConfig(params) {
699
+ if (!plugin.definition.onValidateConfig) {
700
+ throw Object.assign(new Error("validateConfig is not implemented by this plugin"), { code: PLUGIN_RPC_ERROR_CODES.METHOD_NOT_IMPLEMENTED });
701
+ }
702
+ return plugin.definition.onValidateConfig(params.config);
703
+ }
704
+ async function handleConfigChanged(params) {
705
+ currentConfig = params.config;
706
+ if (plugin.definition.onConfigChanged) {
707
+ await plugin.definition.onConfigChanged(params.config);
708
+ }
709
+ }
710
+ async function handleOnEvent(params) {
711
+ const event = params.event;
712
+ for (const registration of eventHandlers) {
713
+ // Check event type match
714
+ const exactMatch = registration.name === event.eventType;
715
+ const wildcardPluginAll = registration.name === "plugin.*" &&
716
+ event.eventType.startsWith("plugin.");
717
+ const wildcardPluginOne = registration.name.endsWith(".*") &&
718
+ event.eventType.startsWith(registration.name.slice(0, -1));
719
+ if (!exactMatch && !wildcardPluginAll && !wildcardPluginOne)
720
+ continue;
721
+ // Check filter
722
+ if (registration.filter && !allowsEvent(registration.filter, event))
723
+ continue;
724
+ try {
725
+ await registration.fn(event);
726
+ }
727
+ catch (err) {
728
+ // Log error but continue processing other handlers so one failing
729
+ // handler doesn't prevent the rest from running.
730
+ notifyHost("log", {
731
+ level: "error",
732
+ message: `Event handler for "${registration.name}" failed: ${err instanceof Error ? err.message : String(err)}`,
733
+ meta: { eventType: event.eventType, stack: err instanceof Error ? err.stack : undefined },
734
+ });
735
+ }
736
+ }
737
+ }
738
+ async function handleRunJob(params) {
739
+ const handler = jobHandlers.get(params.job.jobKey);
740
+ if (!handler) {
741
+ throw new Error(`No handler registered for job "${params.job.jobKey}"`);
742
+ }
743
+ await handler(params.job);
744
+ }
745
+ async function handleWebhook(params) {
746
+ if (!plugin.definition.onWebhook) {
747
+ throw Object.assign(new Error("handleWebhook is not implemented by this plugin"), { code: PLUGIN_RPC_ERROR_CODES.METHOD_NOT_IMPLEMENTED });
748
+ }
749
+ await plugin.definition.onWebhook(params);
750
+ }
751
+ async function handleGetData(params) {
752
+ const handler = dataHandlers.get(params.key);
753
+ if (!handler) {
754
+ throw new Error(`No data handler registered for key "${params.key}"`);
755
+ }
756
+ return handler(params.renderEnvironment === undefined
757
+ ? params.params
758
+ : { ...params.params, renderEnvironment: params.renderEnvironment });
759
+ }
760
+ async function handlePerformAction(params) {
761
+ const handler = actionHandlers.get(params.key);
762
+ if (!handler) {
763
+ throw new Error(`No action handler registered for key "${params.key}"`);
764
+ }
765
+ return handler(params.renderEnvironment === undefined
766
+ ? params.params
767
+ : { ...params.params, renderEnvironment: params.renderEnvironment });
768
+ }
769
+ async function handleExecuteTool(params) {
770
+ const entry = toolHandlers.get(params.toolName);
771
+ if (!entry) {
772
+ throw new Error(`No tool handler registered for "${params.toolName}"`);
773
+ }
774
+ return entry.fn(params.parameters, params.runContext);
775
+ }
776
+ // -----------------------------------------------------------------------
777
+ // Event filter helper
778
+ // -----------------------------------------------------------------------
779
+ function allowsEvent(filter, event) {
780
+ const payload = event.payload;
781
+ if (filter.companyId !== undefined) {
782
+ const companyId = event.companyId ?? String(payload?.companyId ?? "");
783
+ if (companyId !== filter.companyId)
784
+ return false;
785
+ }
786
+ if (filter.projectId !== undefined) {
787
+ const projectId = event.entityType === "project"
788
+ ? event.entityId
789
+ : String(payload?.projectId ?? "");
790
+ if (projectId !== filter.projectId)
791
+ return false;
792
+ }
793
+ if (filter.agentId !== undefined) {
794
+ const agentId = event.entityType === "agent"
795
+ ? event.entityId
796
+ : String(payload?.agentId ?? "");
797
+ if (agentId !== filter.agentId)
798
+ return false;
799
+ }
800
+ return true;
801
+ }
802
+ // -----------------------------------------------------------------------
803
+ // Inbound response handling (host → worker, response to our outbound call)
804
+ // -----------------------------------------------------------------------
805
+ function handleHostResponse(response) {
806
+ const id = response.id;
807
+ if (id === null || id === undefined)
808
+ return;
809
+ const pending = pendingRequests.get(id);
810
+ if (!pending)
811
+ return;
812
+ clearTimeout(pending.timer);
813
+ pendingRequests.delete(id);
814
+ pending.resolve(response);
815
+ }
816
+ // -----------------------------------------------------------------------
817
+ // Incoming line handler
818
+ // -----------------------------------------------------------------------
819
+ function handleLine(line) {
820
+ if (!line.trim())
821
+ return;
822
+ let message;
823
+ try {
824
+ message = parseMessage(line);
825
+ }
826
+ catch (err) {
827
+ if (err instanceof JsonRpcParseError) {
828
+ // Send parse error response
829
+ sendMessage(createErrorResponse(null, JSONRPC_ERROR_CODES.PARSE_ERROR, `Parse error: ${err.message}`));
830
+ }
831
+ return;
832
+ }
833
+ if (isJsonRpcResponse(message)) {
834
+ // This is a response to one of our outbound worker→host calls
835
+ handleHostResponse(message);
836
+ }
837
+ else if (isJsonRpcRequest(message)) {
838
+ // This is a host→worker RPC call — dispatch it
839
+ handleHostRequest(message).catch((err) => {
840
+ // Unhandled error in the async handler — send error response
841
+ const errorMessage = err instanceof Error ? err.message : String(err);
842
+ const errorCode = err?.code ?? PLUGIN_RPC_ERROR_CODES.WORKER_ERROR;
843
+ try {
844
+ sendMessage(createErrorResponse(message.id, typeof errorCode === "number" ? errorCode : PLUGIN_RPC_ERROR_CODES.WORKER_ERROR, errorMessage));
845
+ }
846
+ catch {
847
+ // Cannot send response, stdout may be closed
848
+ }
849
+ });
850
+ }
851
+ else if (isJsonRpcNotification(message)) {
852
+ // Dispatch host→worker push notifications
853
+ const notif = message;
854
+ if (notif.method === "agents.sessions.event" && notif.params) {
855
+ const event = notif.params;
856
+ const cb = sessionEventCallbacks.get(event.sessionId);
857
+ if (cb)
858
+ cb(event);
859
+ }
860
+ else if (notif.method === "onEvent" && notif.params) {
861
+ // Plugin event bus notifications — dispatch to registered event handlers
862
+ handleOnEvent(notif.params).catch((err) => {
863
+ notifyHost("log", {
864
+ level: "error",
865
+ message: `Failed to handle event notification: ${err instanceof Error ? err.message : String(err)}`,
866
+ });
867
+ });
868
+ }
869
+ }
870
+ }
871
+ // -----------------------------------------------------------------------
872
+ // Cleanup
873
+ // -----------------------------------------------------------------------
874
+ function cleanup() {
875
+ running = false;
876
+ // Close readline
877
+ if (readline) {
878
+ readline.close();
879
+ readline = null;
880
+ }
881
+ // Reject all pending outbound calls
882
+ for (const [id, pending] of pendingRequests) {
883
+ clearTimeout(pending.timer);
884
+ pending.resolve(createErrorResponse(id, PLUGIN_RPC_ERROR_CODES.WORKER_UNAVAILABLE, "Worker RPC host is shutting down"));
885
+ }
886
+ pendingRequests.clear();
887
+ sessionEventCallbacks.clear();
888
+ }
889
+ // -----------------------------------------------------------------------
890
+ // Bootstrap: wire up stdin readline
891
+ // -----------------------------------------------------------------------
892
+ let readline = createInterface({
893
+ input: stdinStream,
894
+ crlfDelay: Infinity,
895
+ });
896
+ readline.on("line", handleLine);
897
+ // If stdin closes, we should exit gracefully
898
+ readline.on("close", () => {
899
+ if (running) {
900
+ cleanup();
901
+ if (!options.stdin && !options.stdout) {
902
+ process.exit(0);
903
+ }
904
+ }
905
+ });
906
+ // Handle uncaught errors in the worker process.
907
+ // Only install these when using the real process streams (not in tests
908
+ // where the caller provides custom streams).
909
+ if (!options.stdin && !options.stdout) {
910
+ process.on("uncaughtException", (err) => {
911
+ notifyHost("log", {
912
+ level: "error",
913
+ message: `Uncaught exception: ${err.message}`,
914
+ meta: { stack: err.stack },
915
+ });
916
+ // Give the notification a moment to flush, then exit
917
+ setTimeout(() => process.exit(1), 100);
918
+ });
919
+ process.on("unhandledRejection", (reason) => {
920
+ const message = reason instanceof Error ? reason.message : String(reason);
921
+ const stack = reason instanceof Error ? reason.stack : undefined;
922
+ notifyHost("log", {
923
+ level: "error",
924
+ message: `Unhandled rejection: ${message}`,
925
+ meta: { stack },
926
+ });
927
+ });
928
+ }
929
+ // -----------------------------------------------------------------------
930
+ // Return the handle
931
+ // -----------------------------------------------------------------------
932
+ return {
933
+ get running() {
934
+ return running;
935
+ },
936
+ stop() {
937
+ cleanup();
938
+ },
939
+ };
940
+ }
941
+ //# sourceMappingURL=worker-rpc-host.js.map