@gtkx/cli 0.18.0 → 0.18.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.
Files changed (67) hide show
  1. package/dist/builder.d.ts +26 -6
  2. package/dist/builder.d.ts.map +1 -0
  3. package/dist/builder.js +8 -31
  4. package/dist/builder.js.map +1 -0
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.d.ts.map +1 -0
  7. package/dist/cli.js +6 -0
  8. package/dist/cli.js.map +1 -0
  9. package/dist/create.d.ts +1 -0
  10. package/dist/create.d.ts.map +1 -0
  11. package/dist/create.js +1 -0
  12. package/dist/create.js.map +1 -0
  13. package/dist/dev-server.d.ts +1 -0
  14. package/dist/dev-server.d.ts.map +1 -0
  15. package/dist/dev-server.js +1 -0
  16. package/dist/dev-server.js.map +1 -0
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +1 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/mcp-client.d.ts +1 -0
  22. package/dist/mcp-client.d.ts.map +1 -0
  23. package/dist/mcp-client.js +1 -0
  24. package/dist/mcp-client.js.map +1 -0
  25. package/dist/refresh-runtime.d.ts +1 -0
  26. package/dist/refresh-runtime.d.ts.map +1 -0
  27. package/dist/refresh-runtime.js +1 -0
  28. package/dist/refresh-runtime.js.map +1 -0
  29. package/dist/templates.d.ts +1 -0
  30. package/dist/templates.d.ts.map +1 -0
  31. package/dist/templates.js +1 -0
  32. package/dist/templates.js.map +1 -0
  33. package/dist/vite-plugin-gtkx-assets.d.ts +1 -0
  34. package/dist/vite-plugin-gtkx-assets.d.ts.map +1 -0
  35. package/dist/vite-plugin-gtkx-assets.js +1 -0
  36. package/dist/vite-plugin-gtkx-assets.js.map +1 -0
  37. package/dist/vite-plugin-gtkx-built-url.d.ts +18 -0
  38. package/dist/vite-plugin-gtkx-built-url.d.ts.map +1 -0
  39. package/dist/vite-plugin-gtkx-built-url.js +43 -0
  40. package/dist/vite-plugin-gtkx-built-url.js.map +1 -0
  41. package/dist/vite-plugin-gtkx-native.d.ts +14 -0
  42. package/dist/vite-plugin-gtkx-native.d.ts.map +1 -0
  43. package/dist/vite-plugin-gtkx-native.js +53 -0
  44. package/dist/vite-plugin-gtkx-native.js.map +1 -0
  45. package/dist/vite-plugin-gtkx-refresh.d.ts +1 -0
  46. package/dist/vite-plugin-gtkx-refresh.d.ts.map +1 -0
  47. package/dist/vite-plugin-gtkx-refresh.js +1 -0
  48. package/dist/vite-plugin-gtkx-refresh.js.map +1 -0
  49. package/dist/vite-plugin-swc-ssr-refresh.d.ts +1 -0
  50. package/dist/vite-plugin-swc-ssr-refresh.d.ts.map +1 -0
  51. package/dist/vite-plugin-swc-ssr-refresh.js +1 -0
  52. package/dist/vite-plugin-swc-ssr-refresh.js.map +1 -0
  53. package/package.json +8 -6
  54. package/src/builder.ts +94 -0
  55. package/src/cli.tsx +154 -0
  56. package/src/create.ts +310 -0
  57. package/src/dev-server.tsx +162 -0
  58. package/src/global.d.ts +6 -0
  59. package/src/index.ts +3 -0
  60. package/src/mcp-client.ts +518 -0
  61. package/src/refresh-runtime.ts +89 -0
  62. package/src/templates.ts +26 -0
  63. package/src/vite-plugin-gtkx-assets.ts +32 -0
  64. package/src/vite-plugin-gtkx-built-url.ts +48 -0
  65. package/src/vite-plugin-gtkx-native.ts +64 -0
  66. package/src/vite-plugin-gtkx-refresh.ts +54 -0
  67. package/src/vite-plugin-swc-ssr-refresh.ts +61 -0
@@ -0,0 +1,162 @@
1
+ import { events } from "@gtkx/ffi";
2
+ import { setHotReloading, update } from "@gtkx/react";
3
+ import { createServer, type InlineConfig, type ViteDevServer } from "vite";
4
+ import { isReactRefreshBoundary, performRefresh } from "./refresh-runtime.js";
5
+ import { gtkxAssets } from "./vite-plugin-gtkx-assets.js";
6
+ import { gtkxRefresh } from "./vite-plugin-gtkx-refresh.js";
7
+ import { swcSsrRefresh } from "./vite-plugin-swc-ssr-refresh.js";
8
+
9
+ /**
10
+ * Options for the GTKX development server.
11
+ */
12
+ export type DevServerOptions = {
13
+ /** Path to the entry file (e.g., "src/dev.tsx") */
14
+ entry: string;
15
+ /** Additional Vite configuration */
16
+ vite?: InlineConfig;
17
+ };
18
+
19
+ type AppModule = {
20
+ default: () => React.ReactNode;
21
+ };
22
+
23
+ /**
24
+ * Creates a Vite-based development server with hot module replacement.
25
+ *
26
+ * Provides fast refresh for React components and full reload for other changes.
27
+ * The server watches for file changes and automatically updates the running
28
+ * GTK application.
29
+ *
30
+ * @param options - Server configuration including entry point and Vite options
31
+ * @returns A Vite development server instance
32
+ *
33
+ * @example
34
+ * ```tsx
35
+ * import { createDevServer } from "@gtkx/cli";
36
+ * import { render } from "@gtkx/react";
37
+ *
38
+ * const server = await createDevServer({
39
+ * entry: "./src/dev.tsx",
40
+ * });
41
+ *
42
+ * const mod = await server.ssrLoadModule("./src/dev.tsx");
43
+ * render(<mod.default />, mod.appId);
44
+ * ```
45
+ *
46
+ * @see {@link DevServerOptions} for configuration options
47
+ */
48
+ export const createDevServer = async (options: DevServerOptions): Promise<ViteDevServer> => {
49
+ const { entry, vite: viteConfig } = options;
50
+
51
+ const moduleExports = new Map<string, Record<string, unknown>>();
52
+
53
+ const server = await createServer({
54
+ ...viteConfig,
55
+ appType: "custom",
56
+ plugins: [
57
+ gtkxAssets(),
58
+ swcSsrRefresh(),
59
+ gtkxRefresh(),
60
+ {
61
+ name: "gtkx:remove-react-dom-optimized",
62
+ enforce: "post",
63
+ config(config) {
64
+ config.optimizeDeps ??= {};
65
+ config.optimizeDeps.include = config.optimizeDeps.include?.filter(
66
+ (dep) => dep !== "react-dom" && !dep.startsWith("react-dom/"),
67
+ );
68
+ },
69
+ },
70
+ ],
71
+ server: {
72
+ ...viteConfig?.server,
73
+ middlewareMode: true,
74
+ },
75
+ optimizeDeps: {
76
+ ...viteConfig?.optimizeDeps,
77
+ noDiscovery: true,
78
+ include: [],
79
+ },
80
+ ssr: {
81
+ ...viteConfig?.ssr,
82
+ external: true,
83
+ },
84
+ });
85
+
86
+ const loadModule = async (): Promise<AppModule> => {
87
+ const mod = (await server.ssrLoadModule(entry)) as AppModule;
88
+ moduleExports.set(entry, { ...mod });
89
+ return mod;
90
+ };
91
+
92
+ const invalidateAllModules = (): void => {
93
+ for (const module of server.moduleGraph.idToModuleMap.values()) {
94
+ server.moduleGraph.invalidateModule(module);
95
+ }
96
+ };
97
+
98
+ const invalidateModuleAndImporters = (filePath: string): void => {
99
+ const module = server.moduleGraph.getModuleById(filePath);
100
+
101
+ if (module) {
102
+ server.moduleGraph.invalidateModule(module);
103
+
104
+ for (const importer of module.importers) {
105
+ server.moduleGraph.invalidateModule(importer);
106
+ }
107
+ }
108
+ };
109
+
110
+ events.on("stop", () => {
111
+ server.close();
112
+ });
113
+
114
+ server.watcher.on("change", async (changedPath) => {
115
+ try {
116
+ const module = server.moduleGraph.getModuleById(changedPath);
117
+
118
+ if (!module) {
119
+ return;
120
+ }
121
+
122
+ console.log(`[gtkx] File changed: ${changedPath}`);
123
+
124
+ invalidateModuleAndImporters(changedPath);
125
+
126
+ const newMod = (await server.ssrLoadModule(changedPath)) as Record<string, unknown>;
127
+ moduleExports.set(changedPath, { ...newMod });
128
+
129
+ if (isReactRefreshBoundary(newMod)) {
130
+ console.log("[gtkx] Fast refreshing...");
131
+ performRefresh();
132
+ console.log("[gtkx] Fast refresh complete");
133
+ return;
134
+ }
135
+
136
+ console.log("[gtkx] Full reload...");
137
+ invalidateAllModules();
138
+
139
+ const mod = await loadModule();
140
+ const App = mod.default;
141
+
142
+ if (typeof App !== "function") {
143
+ console.error("[gtkx] Entry file must export a default function component");
144
+ return;
145
+ }
146
+
147
+ setHotReloading(true);
148
+ try {
149
+ await update(<App />);
150
+ } finally {
151
+ setHotReloading(false);
152
+ }
153
+ console.log("[gtkx] Full reload complete");
154
+ } catch (error) {
155
+ console.error("[gtkx] Hot reload failed:", error);
156
+ }
157
+ });
158
+
159
+ return server;
160
+ };
161
+
162
+ export type { ViteDevServer };
@@ -0,0 +1,6 @@
1
+ declare global {
2
+ var $RefreshReg$: (type: unknown, id: string) => void;
3
+ var $RefreshSig$: () => (type: unknown) => unknown;
4
+ }
5
+
6
+ export {};
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { type BuildOptions, build } from "./builder.js";
2
+ export { createApp } from "./create.js";
3
+ export { createDevServer, type DevServerOptions } from "./dev-server.js";
@@ -0,0 +1,518 @@
1
+ import * as net from "node:net";
2
+ import { getNativeInterface } from "@gtkx/ffi";
3
+ import * as Gio from "@gtkx/ffi/gio";
4
+ import { Value } from "@gtkx/ffi/gobject";
5
+ import * as Gtk from "@gtkx/ffi/gtk";
6
+ import {
7
+ DEFAULT_SOCKET_PATH,
8
+ type IpcMethod,
9
+ type IpcRequest,
10
+ IpcRequestSchema,
11
+ type IpcResponse,
12
+ IpcResponseSchema,
13
+ McpError,
14
+ McpErrorCode,
15
+ methodNotFoundError,
16
+ type SerializedWidget,
17
+ widgetNotFoundError,
18
+ } from "@gtkx/mcp";
19
+
20
+ const widgetIdMap = new WeakMap<Gtk.Widget, string>();
21
+ let nextWidgetId = 0;
22
+
23
+ const getWidgetId = (widget: Gtk.Widget): string => {
24
+ let id = widgetIdMap.get(widget);
25
+ if (!id) {
26
+ id = String(nextWidgetId++);
27
+ widgetIdMap.set(widget, id);
28
+ }
29
+ return id;
30
+ };
31
+
32
+ type TestingModule = typeof import("@gtkx/testing");
33
+
34
+ let testingModule: TestingModule | null = null;
35
+ let testingLoadError: Error | null = null;
36
+
37
+ const loadTestingModule = async (): Promise<TestingModule> => {
38
+ if (testingModule) return testingModule;
39
+ if (testingLoadError) throw testingLoadError;
40
+
41
+ try {
42
+ testingModule = await import("@gtkx/testing");
43
+ return testingModule;
44
+ } catch (cause) {
45
+ testingLoadError = new Error(
46
+ "@gtkx/testing is not installed. Install it to enable MCP widget interactions: pnpm add -D @gtkx/testing",
47
+ { cause },
48
+ );
49
+ throw testingLoadError;
50
+ }
51
+ };
52
+
53
+ type McpClientOptions = {
54
+ socketPath?: string;
55
+ appId: string;
56
+ };
57
+
58
+ type PendingRequest = {
59
+ resolve: (result: unknown) => void;
60
+ reject: (error: Error) => void;
61
+ timeout: NodeJS.Timeout;
62
+ };
63
+
64
+ const RECONNECT_DELAY_MS = 2000;
65
+ const REQUEST_TIMEOUT_MS = 30000;
66
+
67
+ const formatRole = (role: Gtk.AccessibleRole | undefined): string => {
68
+ if (role === undefined) return "UNKNOWN";
69
+ return Gtk.AccessibleRole[role] ?? String(role);
70
+ };
71
+
72
+ const getWidgetText = (widget: Gtk.Widget): string | null => {
73
+ if ("getLabel" in widget && typeof widget.getLabel === "function") {
74
+ return widget.getLabel() ?? null;
75
+ }
76
+
77
+ if ("getText" in widget && typeof widget.getText === "function") {
78
+ return widget.getText() ?? null;
79
+ }
80
+
81
+ if ("getTitle" in widget && typeof widget.getTitle === "function") {
82
+ return widget.getTitle() ?? null;
83
+ }
84
+
85
+ return getNativeInterface(widget, Gtk.Editable)?.getText() ?? null;
86
+ };
87
+
88
+ const serializeWidget = (widget: Gtk.Widget): SerializedWidget => {
89
+ const children: SerializedWidget[] = [];
90
+ let child = widget.getFirstChild();
91
+ while (child) {
92
+ children.push(serializeWidget(child));
93
+ child = child.getNextSibling();
94
+ }
95
+
96
+ const text = getWidgetText(widget);
97
+
98
+ return {
99
+ id: getWidgetId(widget),
100
+ type: widget.constructor.name,
101
+ role: formatRole(widget.getAccessibleRole()),
102
+ name: widget.getName() || null,
103
+ label: text,
104
+ text,
105
+ sensitive: widget.getSensitive(),
106
+ visible: widget.getVisible(),
107
+ cssClasses: widget.getCssClasses() ?? [],
108
+ children,
109
+ };
110
+ };
111
+
112
+ const widgetRegistry = new Map<string, Gtk.Widget>();
113
+
114
+ const registerWidgets = (widget: Gtk.Widget): void => {
115
+ const idStr = getWidgetId(widget);
116
+ widgetRegistry.set(idStr, widget);
117
+ let child = widget.getFirstChild();
118
+ while (child) {
119
+ registerWidgets(child);
120
+ child = child.getNextSibling();
121
+ }
122
+ };
123
+
124
+ const getWidgetById = (id: string): Gtk.Widget | undefined => {
125
+ return widgetRegistry.get(id);
126
+ };
127
+
128
+ const refreshWidgetRegistry = (): void => {
129
+ widgetRegistry.clear();
130
+ const windows = Gtk.Window.listToplevels();
131
+ for (const window of windows) {
132
+ registerWidgets(window);
133
+ }
134
+ };
135
+
136
+ class McpClient {
137
+ private socket: net.Socket | null = null;
138
+ private buffer = "";
139
+ private socketPath: string;
140
+ private appId: string;
141
+ private reconnectTimer: NodeJS.Timeout | null = null;
142
+ private hasConnected = false;
143
+ private isStopping = false;
144
+ private pendingRequests = new Map<string, PendingRequest>();
145
+
146
+ constructor(options: McpClientOptions) {
147
+ this.socketPath = options.socketPath ?? DEFAULT_SOCKET_PATH;
148
+ this.appId = options.appId;
149
+ }
150
+
151
+ async connect(): Promise<void> {
152
+ return new Promise((resolve, reject) => {
153
+ this.attemptConnect(resolve, reject);
154
+ });
155
+ }
156
+
157
+ private attemptConnect(onSuccess?: () => void, onError?: (error: Error) => void): void {
158
+ let settled = false;
159
+
160
+ const settle = <T extends unknown[]>(callback: ((...args: T) => void) | undefined, ...args: T) => {
161
+ if (settled) return;
162
+ settled = true;
163
+ callback?.(...args);
164
+ };
165
+
166
+ this.socket = net.createConnection(this.socketPath, () => {
167
+ console.log(`[gtkx] Connected to MCP server at ${this.socketPath}`);
168
+ this.hasConnected = true;
169
+ this.register()
170
+ .then(() => {
171
+ console.log("[gtkx] Registered with MCP server");
172
+ settle(onSuccess);
173
+ })
174
+ .catch((error) => {
175
+ console.error("[gtkx] Failed to register with MCP server:", error.message);
176
+ settle(onError, error instanceof Error ? error : new Error(String(error)));
177
+ });
178
+ });
179
+
180
+ this.socket.on("data", (data: Buffer) => this.handleData(data));
181
+
182
+ this.socket.on("close", () => {
183
+ if (this.hasConnected) {
184
+ console.log("[gtkx] Disconnected from MCP server");
185
+ this.hasConnected = false;
186
+ }
187
+ this.socket = null;
188
+ this.rejectPendingRequests(new Error("Connection closed"));
189
+ this.scheduleReconnect();
190
+ });
191
+
192
+ this.socket.on("error", (error) => {
193
+ const code = (error as NodeJS.ErrnoException).code;
194
+ const isDisconnectError =
195
+ code === "ENOENT" || code === "ECONNREFUSED" || code === "EPIPE" || code === "ECONNRESET";
196
+ if (isDisconnectError) {
197
+ this.scheduleReconnect();
198
+ } else {
199
+ console.error("[gtkx] Socket error:", error.message);
200
+ }
201
+ settle(onError, error);
202
+ });
203
+ }
204
+
205
+ private scheduleReconnect(): void {
206
+ if (this.reconnectTimer || this.isStopping) return;
207
+ this.reconnectTimer = setTimeout(() => {
208
+ this.reconnectTimer = null;
209
+ this.attemptConnect();
210
+ }, RECONNECT_DELAY_MS);
211
+ }
212
+
213
+ disconnect(): void {
214
+ this.isStopping = true;
215
+ if (this.reconnectTimer) {
216
+ clearTimeout(this.reconnectTimer);
217
+ this.reconnectTimer = null;
218
+ }
219
+ this.rejectPendingRequests(new Error("Client disconnected"));
220
+ if (this.socket) {
221
+ this.send({ id: crypto.randomUUID(), method: "app.unregister" });
222
+ this.socket.destroy();
223
+ this.socket = null;
224
+ }
225
+ this.hasConnected = false;
226
+ }
227
+
228
+ private rejectPendingRequests(error: Error): void {
229
+ for (const pending of this.pendingRequests.values()) {
230
+ clearTimeout(pending.timeout);
231
+ pending.reject(error);
232
+ }
233
+ this.pendingRequests.clear();
234
+ }
235
+
236
+ private async register(): Promise<void> {
237
+ await this.sendRequest("app.register", {
238
+ appId: this.appId,
239
+ pid: process.pid,
240
+ });
241
+ }
242
+
243
+ private send(message: IpcRequest | IpcResponse): void {
244
+ if (!this.socket || !this.socket.writable) return;
245
+ this.socket.write(`${JSON.stringify(message)}\n`);
246
+ }
247
+
248
+ private sendRequest(method: IpcMethod, params?: unknown): Promise<unknown> {
249
+ return new Promise((resolve, reject) => {
250
+ if (!this.socket || !this.socket.writable) {
251
+ reject(new Error("Socket not connected"));
252
+ return;
253
+ }
254
+
255
+ const id = crypto.randomUUID();
256
+ const timeout = setTimeout(() => {
257
+ this.pendingRequests.delete(id);
258
+ reject(new Error(`Request timed out: ${method}`));
259
+ }, REQUEST_TIMEOUT_MS);
260
+
261
+ this.pendingRequests.set(id, { resolve, reject, timeout });
262
+ this.send({ id, method, params });
263
+ });
264
+ }
265
+
266
+ private handleData(data: Buffer): void {
267
+ this.buffer += data.toString();
268
+
269
+ let newlineIndex = this.buffer.indexOf("\n");
270
+ while (newlineIndex !== -1) {
271
+ const line = this.buffer.slice(0, newlineIndex);
272
+ this.buffer = this.buffer.slice(newlineIndex + 1);
273
+
274
+ if (line.trim()) {
275
+ this.processMessage(line);
276
+ }
277
+ newlineIndex = this.buffer.indexOf("\n");
278
+ }
279
+ }
280
+
281
+ private processMessage(line: string): void {
282
+ let parsed: unknown;
283
+ try {
284
+ parsed = JSON.parse(line);
285
+ } catch {
286
+ console.warn("[gtkx] Received invalid JSON from MCP server");
287
+ return;
288
+ }
289
+
290
+ const responseResult = IpcResponseSchema.safeParse(parsed);
291
+ if (responseResult.success) {
292
+ const response = responseResult.data;
293
+ const pending = this.pendingRequests.get(response.id);
294
+ if (pending) {
295
+ clearTimeout(pending.timeout);
296
+ this.pendingRequests.delete(response.id);
297
+ if (response.error) {
298
+ pending.reject(new Error(response.error.message));
299
+ } else {
300
+ pending.resolve(response.result);
301
+ }
302
+ return;
303
+ }
304
+ }
305
+
306
+ const requestResult = IpcRequestSchema.safeParse(parsed);
307
+ if (!requestResult.success) {
308
+ return;
309
+ }
310
+
311
+ this.handleRequest(requestResult.data).catch((error) => {
312
+ console.error("[gtkx] Error handling request:", error);
313
+ });
314
+ }
315
+
316
+ private async handleRequest(request: IpcRequest): Promise<void> {
317
+ const { id, method, params } = request;
318
+
319
+ try {
320
+ const result = await this.executeMethod(method as IpcMethod, params);
321
+ this.send({ id, result });
322
+ } catch (error) {
323
+ if (error instanceof McpError) {
324
+ this.send({ id, error: error.toIpcError() });
325
+ } else {
326
+ const message = error instanceof Error ? error.message : String(error);
327
+ this.send({
328
+ id,
329
+ error: {
330
+ code: McpErrorCode.INTERNAL_ERROR,
331
+ message,
332
+ },
333
+ });
334
+ }
335
+ }
336
+ }
337
+
338
+ private async executeMethod(method: IpcMethod, params: unknown): Promise<unknown> {
339
+ const app = Gio.Application.getDefault() as Gtk.Application | null;
340
+
341
+ if (!app) {
342
+ throw new Error("Application not initialized");
343
+ }
344
+
345
+ refreshWidgetRegistry();
346
+
347
+ switch (method) {
348
+ case "app.getWindows": {
349
+ const windows = Gtk.Window.listToplevels();
350
+ return {
351
+ windows: windows.map((w) => ({
352
+ id: getWidgetId(w),
353
+ title: (w as Gtk.Window).getTitle?.() ?? null,
354
+ })),
355
+ };
356
+ }
357
+
358
+ case "widget.getTree": {
359
+ const testing = await loadTestingModule();
360
+ return { tree: testing.prettyWidget(app, { includeIds: true, highlight: false }) };
361
+ }
362
+
363
+ case "widget.query": {
364
+ const testing = await loadTestingModule();
365
+ const p = params as { queryType: string; value: string | number; options?: Record<string, unknown> };
366
+ let widgets: Gtk.Widget[] = [];
367
+
368
+ switch (p.queryType) {
369
+ case "role": {
370
+ const roleValue =
371
+ typeof p.value === "string"
372
+ ? (Gtk.AccessibleRole[p.value as keyof typeof Gtk.AccessibleRole] as Gtk.AccessibleRole)
373
+ : (p.value as Gtk.AccessibleRole);
374
+ widgets = await testing.findAllByRole(app, roleValue, p.options);
375
+ break;
376
+ }
377
+ case "text":
378
+ widgets = await testing.findAllByText(app, String(p.value), p.options);
379
+ break;
380
+ case "testId":
381
+ widgets = await testing.findAllByTestId(app, String(p.value), p.options);
382
+ break;
383
+ case "labelText":
384
+ widgets = await testing.findAllByLabelText(app, String(p.value), p.options);
385
+ break;
386
+ default:
387
+ throw new Error(`Unknown query type: ${p.queryType}`);
388
+ }
389
+
390
+ return { widgets: widgets.map((w) => serializeWidget(w)) };
391
+ }
392
+
393
+ case "widget.getProps": {
394
+ const p = params as { widgetId: string };
395
+ const widget = getWidgetById(p.widgetId);
396
+ if (!widget) {
397
+ throw widgetNotFoundError(p.widgetId);
398
+ }
399
+ return serializeWidget(widget);
400
+ }
401
+
402
+ case "widget.click": {
403
+ const testing = await loadTestingModule();
404
+ const p = params as { widgetId: string };
405
+ const widget = getWidgetById(p.widgetId);
406
+ if (!widget) {
407
+ throw widgetNotFoundError(p.widgetId);
408
+ }
409
+ await testing.userEvent.click(widget);
410
+ return { success: true };
411
+ }
412
+
413
+ case "widget.type": {
414
+ const testing = await loadTestingModule();
415
+ const p = params as { widgetId: string; text: string; clear?: boolean };
416
+ const widget = getWidgetById(p.widgetId);
417
+ if (!widget) {
418
+ throw widgetNotFoundError(p.widgetId);
419
+ }
420
+ if (p.clear) {
421
+ await testing.userEvent.clear(widget);
422
+ }
423
+ await testing.userEvent.type(widget, p.text);
424
+ return { success: true };
425
+ }
426
+
427
+ case "widget.fireEvent": {
428
+ const testing = await loadTestingModule();
429
+ const p = params as {
430
+ widgetId: string;
431
+ signal: string;
432
+ args?: (unknown | { type: string; value: unknown })[];
433
+ };
434
+ const widget = getWidgetById(p.widgetId);
435
+ if (!widget) {
436
+ throw widgetNotFoundError(p.widgetId);
437
+ }
438
+ const signalArgs = (p.args ?? []).map((arg) => {
439
+ const isTypedArg = typeof arg === "object" && arg !== null && "type" in arg && "value" in arg;
440
+ const argType = isTypedArg ? (arg as { type: string }).type : typeof arg;
441
+ const argValue = isTypedArg ? (arg as { value: unknown }).value : arg;
442
+
443
+ switch (argType) {
444
+ case "boolean":
445
+ return Value.newFromBoolean(argValue as boolean);
446
+ case "int":
447
+ return Value.newFromInt(argValue as number);
448
+ case "uint":
449
+ return Value.newFromUint(argValue as number);
450
+ case "int64":
451
+ return Value.newFromInt64(argValue as number);
452
+ case "uint64":
453
+ return Value.newFromUint64(argValue as number);
454
+ case "float":
455
+ return Value.newFromFloat(argValue as number);
456
+ case "double":
457
+ case "number":
458
+ return Value.newFromDouble(argValue as number);
459
+ case "string":
460
+ return Value.newFromString(argValue as string | null);
461
+ default:
462
+ throw new McpError(McpErrorCode.INVALID_REQUEST, `Unknown argument type: ${argType}`);
463
+ }
464
+ });
465
+ await testing.fireEvent(widget, p.signal, ...signalArgs);
466
+ return { success: true };
467
+ }
468
+
469
+ case "widget.screenshot": {
470
+ const testing = await loadTestingModule();
471
+ const p = params as { windowId?: string };
472
+
473
+ let targetWindow: Gtk.Window;
474
+
475
+ if (p.windowId) {
476
+ const widget = getWidgetById(p.windowId);
477
+ if (!widget) {
478
+ throw widgetNotFoundError(p.windowId);
479
+ }
480
+ targetWindow = widget as Gtk.Window;
481
+ } else {
482
+ const windows = app.getWindows();
483
+ if (windows.length === 0) {
484
+ throw new Error("No windows available for screenshot");
485
+ }
486
+ targetWindow = windows[0] as Gtk.Window;
487
+ }
488
+
489
+ const result = await testing.screenshot(targetWindow);
490
+ return { data: result.data, mimeType: result.mimeType };
491
+ }
492
+
493
+ default:
494
+ throw methodNotFoundError(method);
495
+ }
496
+ }
497
+ }
498
+
499
+ let globalClient: McpClient | null = null;
500
+
501
+ export const startMcpClient = async (appId: string): Promise<McpClient> => {
502
+ if (globalClient) {
503
+ return globalClient;
504
+ }
505
+
506
+ globalClient = new McpClient({ appId });
507
+
508
+ await globalClient.connect().catch(() => {});
509
+
510
+ return globalClient;
511
+ };
512
+
513
+ export const stopMcpClient = (): void => {
514
+ if (globalClient) {
515
+ globalClient.disconnect();
516
+ globalClient = null;
517
+ }
518
+ };