@mearl/client 2.20.6 → 2.21.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 (105) hide show
  1. package/README.md +122 -2
  2. package/dist/cli.js +10 -5
  3. package/dist/commands.d.ts +14 -5
  4. package/dist/commands.js +21 -2
  5. package/dist/generated-browser-action-protocol.d.ts +11 -1
  6. package/dist/generated-browser-action-protocol.js +3 -0
  7. package/dist/generated-plugin-diagnostics.d.ts +110 -0
  8. package/dist/generated-plugin-diagnostics.js +1 -0
  9. package/dist/generated-plugin-execution.d.ts +27 -0
  10. package/dist/generated-plugin-execution.js +1 -0
  11. package/dist/generated-plugin-models.d.ts +64 -0
  12. package/dist/generated-plugin-models.js +1 -0
  13. package/dist/generated-plugin-pages.d.ts +76 -0
  14. package/dist/generated-plugin-pages.js +59 -0
  15. package/dist/generated-plugin-recovery.d.ts +32 -0
  16. package/dist/generated-plugin-recovery.js +1 -0
  17. package/dist/generated-plugin-state.d.ts +50 -0
  18. package/dist/generated-plugin-state.js +1 -0
  19. package/dist/generated-plugin-tasks.d.ts +74 -0
  20. package/dist/generated-plugin-tasks.js +7 -0
  21. package/dist/generated-toolbox-icon.d.ts +10 -0
  22. package/dist/generated-toolbox-icon.js +37 -0
  23. package/dist/index.d.ts +11 -0
  24. package/dist/index.js +6 -0
  25. package/dist/plugin-task-host.d.ts +1 -0
  26. package/dist/plugin-task-host.js +14 -0
  27. package/dist/plugin-worker.d.ts +1 -0
  28. package/dist/plugin-worker.js +17 -0
  29. package/dist/plugin.d.ts +57 -4
  30. package/dist/plugin.js +296 -15
  31. package/dist/pluginCliOutput.d.ts +2 -0
  32. package/dist/pluginCliOutput.js +7 -0
  33. package/dist/pluginCommandWorker.d.ts +2 -0
  34. package/dist/pluginCommandWorker.js +174 -0
  35. package/dist/pluginDiagnostics.d.ts +26 -0
  36. package/dist/pluginDiagnostics.js +309 -0
  37. package/dist/pluginExecution.d.ts +21 -0
  38. package/dist/pluginExecution.js +43 -0
  39. package/dist/pluginExecutor.d.ts +29 -0
  40. package/dist/pluginExecutor.js +223 -0
  41. package/dist/pluginExecutorProtocol.d.ts +55 -0
  42. package/dist/pluginExecutorProtocol.js +47 -0
  43. package/dist/pluginExecutorSupervisor.d.ts +4 -0
  44. package/dist/pluginExecutorSupervisor.js +63 -0
  45. package/dist/pluginHost.d.ts +20 -0
  46. package/dist/pluginHost.js +53 -0
  47. package/dist/pluginHostConnection.d.ts +35 -0
  48. package/dist/pluginHostConnection.js +257 -0
  49. package/dist/pluginHostFiles.d.ts +1 -0
  50. package/dist/pluginHostFiles.js +21 -0
  51. package/dist/pluginHostPaths.d.ts +11 -0
  52. package/dist/pluginHostPaths.js +22 -0
  53. package/dist/pluginHostServer.d.ts +7 -0
  54. package/dist/pluginHostServer.js +252 -0
  55. package/dist/pluginModelCatalog.d.ts +14 -0
  56. package/dist/pluginModelCatalog.js +51 -0
  57. package/dist/pluginModelDefinition.d.ts +6 -0
  58. package/dist/pluginModelDefinition.js +75 -0
  59. package/dist/pluginModelSessionWorker.d.ts +23 -0
  60. package/dist/pluginModelSessionWorker.js +60 -0
  61. package/dist/pluginModelSessions.d.ts +16 -0
  62. package/dist/pluginModelSessions.js +234 -0
  63. package/dist/pluginModels.d.ts +17 -0
  64. package/dist/pluginModels.js +47 -0
  65. package/dist/pluginPageBundle.js +74 -17
  66. package/dist/pluginPageDefinition.d.ts +3 -0
  67. package/dist/pluginPageDefinition.js +62 -0
  68. package/dist/pluginPageManager.d.ts +57 -0
  69. package/dist/pluginPageManager.js +877 -0
  70. package/dist/pluginPages.d.ts +21 -0
  71. package/dist/pluginPages.js +46 -0
  72. package/dist/pluginProcessTree.d.ts +2 -0
  73. package/dist/pluginProcessTree.js +31 -0
  74. package/dist/pluginRecoveryDefinition.d.ts +3 -0
  75. package/dist/pluginRecoveryDefinition.js +56 -0
  76. package/dist/pluginRegistry.d.ts +2 -3
  77. package/dist/pluginRegistry.js +14 -13
  78. package/dist/pluginRevision.d.ts +14 -0
  79. package/dist/pluginRevision.js +227 -0
  80. package/dist/pluginRuntimeCleanup.d.ts +2 -0
  81. package/dist/pluginRuntimeCleanup.js +55 -0
  82. package/dist/pluginState.d.ts +26 -0
  83. package/dist/pluginState.js +28 -0
  84. package/dist/pluginStateDefinition.d.ts +4 -0
  85. package/dist/pluginStateDefinition.js +68 -0
  86. package/dist/pluginStateManager.d.ts +24 -0
  87. package/dist/pluginStateManager.js +132 -0
  88. package/dist/pluginStateStore.d.ts +57 -0
  89. package/dist/pluginStateStore.js +396 -0
  90. package/dist/pluginTaskManager.d.ts +92 -0
  91. package/dist/pluginTaskManager.js +1080 -0
  92. package/dist/pluginTaskProtocol.d.ts +47 -0
  93. package/dist/pluginTaskProtocol.js +1 -0
  94. package/dist/pluginTaskStore.d.ts +53 -0
  95. package/dist/pluginTaskStore.js +319 -0
  96. package/dist/pluginTaskWire.d.ts +4 -0
  97. package/dist/pluginTaskWire.js +42 -0
  98. package/dist/pluginTasks.d.ts +51 -0
  99. package/dist/pluginTasks.js +181 -0
  100. package/dist/pythonPlugin.d.ts +13 -2
  101. package/dist/pythonPlugin.js +235 -30
  102. package/dist/socket.js +5 -2
  103. package/dist/unified.d.ts +1 -0
  104. package/dist/unified.js +81 -0
  105. package/package.json +7 -3
@@ -0,0 +1,181 @@
1
+ import { PluginHostConnection } from './pluginHostConnection.js';
2
+ import { createHash } from 'node:crypto';
3
+ import { isProcessOwnerAlive, readRecord } from '@mearl/daemon-core';
4
+ import { pluginHostPaths } from './pluginHostPaths.js';
5
+ import { UnifiedClient, decodeBrowserTarget } from './unified.js';
6
+ import { isPluginTaskActive } from './generated-plugin-tasks.js';
7
+ import { pluginExecutionError } from './pluginExecutorProtocol.js';
8
+ function localWait(promise, signal, timeoutMs = Infinity) {
9
+ return new Promise((resolve, reject) => {
10
+ const abort = () => {
11
+ cleanup();
12
+ reject(signal?.reason ?? new Error('Plugin task wait aborted'));
13
+ };
14
+ const timer = Number.isFinite(timeoutMs)
15
+ ? setTimeout(() => {
16
+ cleanup();
17
+ reject(Object.assign(new Error('Waiting for the plugin task timed out; execution continues'), { code: 'PLUGIN_TASK_WAIT_TIMEOUT' }));
18
+ }, Math.max(0, timeoutMs))
19
+ : undefined;
20
+ const cleanup = () => {
21
+ clearTimeout(timer);
22
+ signal?.removeEventListener('abort', abort);
23
+ };
24
+ signal?.addEventListener('abort', abort, { once: true });
25
+ promise.then(value => {
26
+ cleanup();
27
+ resolve(value);
28
+ }, error => {
29
+ cleanup();
30
+ reject(error);
31
+ });
32
+ if (signal?.aborted)
33
+ abort();
34
+ });
35
+ }
36
+ /** Background execution stays on this host; disconnecting a caller never cancels its tasks. */
37
+ export class PluginTaskClient {
38
+ configuration;
39
+ connection;
40
+ constructor(configuration = {}) {
41
+ this.configuration = configuration;
42
+ this.connection = configuration.connection ?? new PluginHostConnection(configuration);
43
+ }
44
+ request(method, input = {}, timeoutMs) {
45
+ return this.connection.request(method, input, timeoutMs);
46
+ }
47
+ status() {
48
+ return this.request('status');
49
+ }
50
+ diagnose(options = {}) {
51
+ return this.request('diagnose', { ...options });
52
+ }
53
+ checkpoint(taskId) {
54
+ return this.request('checkpoint', { taskId });
55
+ }
56
+ resume(taskId, options = {}) {
57
+ return this.request('resume', {
58
+ taskId,
59
+ revision: options.revision,
60
+ environment: Object.fromEntries(Object.entries(options.environment ?? process.env).filter((entry) => entry[1] !== undefined)),
61
+ }, 45000);
62
+ }
63
+ async start(plugin, invocation, options = {}) {
64
+ const { cwd = process.cwd(), environment = process.env, executionTimeoutMs, idempotencyKey, ...routing } = options;
65
+ if (!this.connection.hasInvocationBridge &&
66
+ routing.browser &&
67
+ !decodeBrowserTarget(routing.browser)) {
68
+ const client = new UnifiedClient();
69
+ try {
70
+ const selection = await client.invoke('browser_select_browser', { browser: routing.browser }, routing);
71
+ if (!selection?.selected?.browserId)
72
+ throw new Error('Cannot resolve plugin task browser target');
73
+ routing.browser = selection.selected.browserId;
74
+ }
75
+ finally {
76
+ client.disconnect();
77
+ }
78
+ }
79
+ return this.request('start', {
80
+ plugin,
81
+ command: invocation.command,
82
+ input: invocation.input,
83
+ cwd,
84
+ environment: Object.fromEntries(Object.entries(environment).filter((entry) => entry[1] !== undefined)),
85
+ options: routing,
86
+ executionTimeoutMs,
87
+ idempotencyKey,
88
+ });
89
+ }
90
+ get(taskId) {
91
+ return this.request('get', { taskId });
92
+ }
93
+ list(activeOnly = false, page = {}) {
94
+ return this.request('list', { activeOnly, ...page });
95
+ }
96
+ cancel(taskId) {
97
+ return this.request('cancel', { taskId });
98
+ }
99
+ events(taskId, cursor = 0, waitMs = 0) {
100
+ return this.request('events', { taskId, cursor, waitMs }, waitMs + 15000);
101
+ }
102
+ resultChunk(taskId, index) {
103
+ return this.request('result', { taskId, index });
104
+ }
105
+ async result(taskId) {
106
+ const task = await this.get(taskId);
107
+ if (task.state !== 'success' || !task.result)
108
+ throw new Error('Plugin task has no successful result');
109
+ const parts = [];
110
+ for (let index = 0; index < task.result.chunks; index++) {
111
+ const chunk = await this.resultChunk(taskId, index);
112
+ if (chunk.taskId !== taskId ||
113
+ chunk.index !== index ||
114
+ chunk.sha256 !== task.result.sha256 ||
115
+ chunk.bytes !== task.result.bytes ||
116
+ chunk.chunks !== task.result.chunks)
117
+ throw new Error('Plugin task result chunk identity mismatch');
118
+ parts.push(Buffer.from(chunk.data, 'base64'));
119
+ }
120
+ const bytes = Buffer.concat(parts);
121
+ if (bytes.length !== task.result.bytes ||
122
+ createHash('sha256').update(bytes).digest('hex') !== task.result.sha256)
123
+ throw new Error('Plugin task result integrity check failed');
124
+ return JSON.parse(bytes.toString('utf8')).value;
125
+ }
126
+ async *subscribe(taskId, options = {}) {
127
+ let cursor = options.cursor ?? 0;
128
+ while (true) {
129
+ options.signal?.throwIfAborted();
130
+ const page = await localWait(this.events(taskId, cursor, 25000), options.signal);
131
+ options.signal?.throwIfAborted();
132
+ yield page;
133
+ cursor = page.nextCursor;
134
+ if (!isPluginTaskActive(page.task.state) && cursor >= page.task.sequence)
135
+ return;
136
+ }
137
+ }
138
+ async wait(taskId, options = {}) {
139
+ if (options.timeoutMs !== undefined &&
140
+ (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs < 1))
141
+ throw new Error('Invalid plugin task wait timeout');
142
+ const deadline = options.timeoutMs === undefined ? Infinity : Date.now() + options.timeoutMs;
143
+ let cursor = 0;
144
+ let lastSequence = -1;
145
+ while (true) {
146
+ options.signal?.throwIfAborted();
147
+ if (Date.now() >= deadline)
148
+ throw Object.assign(new Error('Waiting for the plugin task timed out; execution continues'), { code: 'PLUGIN_TASK_WAIT_TIMEOUT' });
149
+ const page = await localWait(this.events(taskId, cursor, Math.min(1000, Math.max(0, deadline - Date.now()))), options.signal, deadline - Date.now());
150
+ options.signal?.throwIfAborted();
151
+ cursor = page.nextCursor;
152
+ if (page.task.progress && page.task.sequence !== lastSequence)
153
+ options.onProgress?.(page.task.progress, page.task);
154
+ lastSequence = page.task.sequence;
155
+ if (page.task.state === 'success')
156
+ return options.readResult === false
157
+ ? page.task
158
+ : localWait(this.result(taskId), options.signal, deadline - Date.now());
159
+ if (!isPluginTaskActive(page.task.state))
160
+ throw pluginExecutionError(page.task.error ?? { message: `Plugin task ended with ${page.task.state}` });
161
+ }
162
+ }
163
+ async stopHost() {
164
+ const original = await this.status();
165
+ await this.request('shutdown');
166
+ this.disconnect();
167
+ const deadline = Date.now() + 15000;
168
+ while (true) {
169
+ const lease = readRecord(pluginHostPaths().lease);
170
+ if (!lease || lease.epoch !== original.host.epoch || !isProcessOwnerAlive(lease))
171
+ return;
172
+ if (Date.now() >= deadline)
173
+ throw new Error('Plugin execution service did not finish stopping');
174
+ await new Promise(resolve => setTimeout(resolve, 50));
175
+ }
176
+ }
177
+ disconnect() {
178
+ if (!this.configuration.connection)
179
+ this.connection.disconnect();
180
+ }
181
+ }
@@ -1,20 +1,31 @@
1
- import type { PluginOptions } from './plugin.js';
1
+ import type { PluginOptions, PluginApp } from './plugin.js';
2
+ import type { PluginCommandContext } from './pluginExecution.js';
2
3
  export interface PythonPluginCommand {
3
4
  description: string;
4
5
  signature: string;
5
6
  toolboxOnly: boolean;
7
+ runtimeOnly: boolean;
8
+ recoveryOnly: boolean;
9
+ migrationOnly: boolean;
6
10
  }
7
11
  export interface PythonPluginDescriptor {
8
12
  commands: PythonPluginCommand[];
9
13
  description?: string;
10
14
  name?: string;
11
15
  toolbox?: unknown;
16
+ pageRuntime?: unknown;
12
17
  appProfiles?: unknown;
18
+ stateStorage?: unknown;
19
+ taskRecovery?: unknown;
13
20
  }
14
21
  export interface PythonPluginApp {
22
+ state?: PluginApp['state'];
23
+ model?: PluginApp['model'];
24
+ checkpoint?: PluginApp['checkpoint'];
25
+ page?: PluginApp['page'];
15
26
  readonly options: Readonly<PluginOptions>;
16
27
  invoke(action: string, payload?: Record<string, any>, options?: PluginOptions): Promise<any>;
17
28
  progress(message: string): void;
18
29
  }
19
30
  export declare function describePythonPlugin(entry: string, options: Readonly<PluginOptions>): Promise<PythonPluginDescriptor>;
20
- export declare function runPythonPluginCommand(entry: string, command: string, input: Record<string, any>, app: PythonPluginApp): Promise<unknown>;
31
+ export declare function runPythonPluginCommand(entry: string, command: string, input: Record<string, any> | import('./generated-plugin-recovery.js').PluginCheckpointMigrationInput, app: PythonPluginApp, context: PluginCommandContext, recovery?: boolean | 'migration', expectedDescriptor?: PythonPluginDescriptor, runtimeOnly?: boolean): Promise<unknown>;